2 #-----------------------------------
3 # Script Name: build_holds_queue.pl
4 # Description: builds a holds queue in the tmp_holdsqueue table
5 #-----------------------------------
9 # find Koha's Perl modules
10 # test carefully before changing this
12 eval { require "$FindBin::Bin/../kohalib.pl" };
21 my $branches = GetBranches();
23 # obtain the ranked list of weights for the case of static weighting
24 my $syspref = C4::Context->preference("StaticHoldsQueueWeight");
26 #@branch_loop = split(/,/, $syspref) if $syspref;
28 # TODO: Add Randomization Option
30 # If no syspref is set, use system-order to determine priority
32 for my $branch_hash (sort keys %$branches) {
33 push @branch_loop, $branch_hash;
34 #{value => "$branch_hash" , branchname => $branches->{$branch_hash}->{'branchname'}, };
38 # if Randomization is enabled, randomize this array
39 @branch_loop = randarray(@branch_loop) if C4::Context->preference("RandomizeHoldsQueueWeight");;
41 my ($biblionumber,$itemnumber,$barcode,$holdingbranch,$pickbranch,$notes,$cardnumber,$surname,$firstname,$phone,$title,$callno,$rdate,$borrno);
43 my $dbh = C4::Context->dbh;
45 $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info
47 my $sth=$dbh->prepare("
48 SELECT biblionumber,itemnumber,reserves.branchcode,reservenotes,borrowers.borrowernumber,cardnumber,surname,firstname,phone,reservedate
49 FROM reserves,borrowers
50 WHERE reserves.found IS NULL
51 AND reserves.borrowernumber=borrowers.borrowernumber
53 GROUP BY biblionumber");
55 my $sth_load=$dbh->prepare("
56 INSERT INTO tmp_holdsqueue (biblionumber,itemnumber,barcode,surname,firstname,phone,borrowernumber,cardnumber,reservedate,title,itemcallnumber,holdingbranch,pickbranch,notes)
57 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
59 $sth->execute(); # get the list of biblionumbers for unfilled holds
62 while (my $data=$sth->fetchrow_hashref){
63 # get the basic hold info
64 $biblionumber = $data->{'biblionumber'};
65 $pickbranch = $data->{'branchcode'};
66 $notes = $data->{'reservenotes'};
67 $borrno = $data->{'borrowernumber'};
68 $cardnumber = $data->{'cardnumber'};
69 $surname = $data->{'surname'};
70 $firstname = $data->{'firstname'};
71 $phone = $data->{'phone'};
72 $rdate = $data->{'reservedate'};
74 my @items = GetItemsInfo($biblionumber,''); # get the items for this biblio
75 my @itemorder; # prepare a new array to hold re-ordered items
77 # Make sure someone(else) doesn't already have this item waiting for them
78 my $found_sth = $dbh->prepare("
79 SELECT found FROM reserves WHERE itemnumber=? AND found = ? AND cancellationdate IS NULL");
81 # The following lines take the retrieved items and run them through various
82 # tests to decide if they are to be used and then put them in the preferred
84 foreach my $itm (@items) {
86 $found_sth->execute($itm->{itemnumber},"W");
87 my $found = $found_sth->fetchrow_hashref();
89 $itm->{"found"} = $found->{"found"};
91 if ($itm->{"notforloan"}) {
93 next if $itm->{"notforloan"}== -1;
95 if ( ( (!$itm->{"binding"}) ||
96 # Item is at not at bindery, not checked out, and not lost
97 ($itm->{"binding"}<1)) && (!$itm->{"found"}) && (!$itm->{"datedue"}) && ( (!$itm->{"itemlost"}) ||
99 # Item is not lost and not notforloan
100 ($itm->{"itemlost"}==0) ) && ( ($itm->{"notforloan"}==0) ||
102 # Item is not notforloan
103 (!$itm->{"notforloan"}) ) ) {
105 warn "patron requested pickup at $pickbranch for item in ".$itm->{'holdingbranch'};
107 # This selects items for fulfilment, and weights them based on
110 # always prefer a direct match
111 if ($itm->{'holdingbranch'} eq $pickbranch) {
112 warn "Found match in pickuplibrary";
113 $itemorder[$weight]=$itm;
116 for my $branchcode (@branch_loop) {
118 if ($itm->{'homebranch'} eq $branchcode) {
119 warn "Match found with weight $weight in ".$branchcode;
120 $itemorder[$weight]=$itm;
126 my $count = @itemorder;
127 warn "Empty array" if $count<1;
128 next GETIT if $count<1; # if the re-ordered array is empty, skip to next
131 foreach my $itmlist (@itemorder) {
133 $barcode = $itmlist->{'barcode'};
134 $itemnumber = $itmlist->{'itemnumber'};
135 $holdingbranch = $itmlist->{'holdingbranch'};
136 $title = $itmlist->{'title'};
137 $callno = $itmlist->{'itemcallnumber'};
138 last PREP; # we only want the first def item in the array
141 $sth_load->execute($biblionumber,$itemnumber,$barcode,$surname,$firstname,$phone,$borrno,$cardnumber,$rdate,$title,$callno,$holdingbranch,$pickbranch,$notes);
150 my $seed = $#array + 1;
151 my $randnum = int(rand($seed));
152 $rand[$randnum] = shift(@array);
154 my $randnum = int(rand($seed));
155 if ($rand[$randnum] eq undef) {
156 $rand[$randnum] = shift(@array);
158 last if ($#array == -1);