3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007 BibLibre Paul POULAIN
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
24 # use warnings; # FIXME: someday
27 use C4::Dates qw/format_date format_date_in_iso/;
34 # for _koha_notify_reserve
35 use C4::Members::Messaging;
37 use C4::Branch qw( GetBranchDetail );
38 use List::MoreUtils qw( firstidx any );
40 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
44 C4::Reserves - Koha functions for dealing with reservation.
52 this modules provides somes functions to deal with reservations.
54 Reserves are stored in reserves table.
55 The following columns contains important values :
56 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
57 =0 : then the reserve is being dealed
58 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
59 W(aiting) : the reserve has an itemnumber affected, and is on the way
60 T(ransfet) : the reserve has an itemnumber affected, and is beeing transfered to pickup branch
61 F(inished) : the reserve has been completed, and is done
62 - itemnumber : empty : the reserve is still unaffected to an item
63 filled: the reserve is attached to an item
64 The complete workflow is :
65 ==== 1st use case ====
66 patron request a document, 1st available : P >0, F=NULL, I=NULL
67 a library having it run "transfertodo", and clic on the list
68 if there is no transfer to do, the reserve waiting
69 patron can pick it up P =0, F=W, I=filled
70 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
71 The pickup library recieve the book, it check in P =0, F=W, I=filled
72 The patron borrow the book P =0, F=F, I=filled
74 ==== 2nd use case ====
75 patron requests a document, a given item,
76 If pickup is holding branch P =0, F=W, I=filled
77 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
78 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
79 The patron borrow the book P =0, F=F, I=filled
88 # set the version for version checking
96 &GetReservesFromItemnumber
97 &GetReservesFromBiblionumber
98 &GetReservesFromBorrowernumber
113 &ModReserveMinusPriority
120 &IsAvailableForItemLevelRequest
126 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found, $from)
132 $branch, $borrowernumber, $biblionumber,
133 $constraint, $bibitems, $priority, $notes,
134 $title, $checkitem, $found, $from
137 GetReserveFee($borrowernumber, $biblionumber, $constraint,
139 my $dbh = C4::Context->dbh;
140 my $const = lc substr( $constraint, 0, 1 );
141 my @datearr = localtime(time);
143 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
146 # If the reserv had the waiting status, we had the value of the resdate
147 if ( $found eq 'W' ) {
148 $waitingdate = $resdate;
152 # updates take place here
154 my $nextacctno = &getnextacctno( $borrowernumber );
156 INSERT INTO accountlines
157 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
159 (?,?,now(),?,?,'Res',?)
161 my $usth = $dbh->prepare($query);
162 $usth->execute( $borrowernumber, $nextacctno, $fee,
163 "Reserve Charge - $title", $fee );
169 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
170 priority,reservenotes,itemnumber,found,waitingdate)
175 my $sth = $dbh->prepare($query);
177 $borrowernumber, $biblionumber, $resdate, $branch,
178 $const, $priority, $notes, $checkitem,
182 # Send e-mail to librarian if syspref is active
183 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
184 my $borrower = GetMemberDetails($borrowernumber);
185 my $biblio = GetBiblioData($biblionumber);
186 my $lettertype = ($from eq "intranet") ? "STAFFHOLDPLACED" : "HOLDPLACED";
187 my $letter = C4::Letters::getletter( 'reserves', $lettertype);
188 my $admin_email_address = C4::Context->preference('KohaAdminEmailAddress');
190 my %keys = (%$borrower, %$biblio);
191 foreach my $key (keys %keys) {
192 my $replacefield = "<<$key>>";
193 $letter->{content} =~ s/$replacefield/$keys{$key}/g;
194 $letter->{title} =~ s/$replacefield/$keys{$key}/g;
197 C4::Letters::EnqueueLetter(
199 borrowernumber => $borrowernumber,
200 message_transport_type => 'email',
201 from_address => $admin_email_address,
202 to_address => $admin_email_address,
211 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
213 INSERT INTO reserveconstraints
214 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
218 $sth = $dbh->prepare($query); # keep prepare outside the loop!
219 foreach (@$bibitems) {
220 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
223 return; # FIXME: why not have a useful return value?
228 =item GetPendingReserves
232 sub GetPendingReserves {
233 my ($filters, $startindex, $results) = @_;
235 $startindex = "0" if not $startindex;
238 my $indepbranch = C4::Context->preference('IndependantBranches') ? C4::Context->userenv->{'branch'} : undef;
239 my $dbh = C4::Context->dbh;
241 my $query = "SELECT DISTINCT(biblionumber) AS biblionumber
243 LEFT JOIN biblio USING(biblionumber)
244 WHERE reserves.found IS NULL ";
247 $query .= " AND branchcode = ? ";
248 push @query_params, $indepbranch;
251 my $sth = $dbh->prepare($query);
252 $sth->execute(@query_params);
256 while ( my $reserve = $sth->fetchrow_hashref ) {
258 unless( $line = $reserves{$reserve->{biblionumber}} ){
260 my $biblio = GetBiblioData($reserve->{biblionumber});
261 my @items = GetItemsInfo($reserve->{biblionumber});
263 $line->{title} = $biblio->{title};
264 foreach my $item (@items){
265 next if ($indepbranch && $indepbranch ne $item->{holdingbranch});
267 $line->{holdingbranches}->{$item->{holdingbranch}} = 1;
268 $line->{callnumbers}->{$item->{itemcallnumber}} = 1;
269 $line->{locations}->{$item->{location}} = 1;
270 $line->{itemtypes}->{$item->{itemtype}} = 1;
273 $line->{reservecount}++;
274 $reserves{$reserve->{biblionumber}} = $line if($line->{count});
278 foreach my $rkey (keys %reserves){
279 my $line = $reserves{$rkey};
280 $line->{biblionumber} = $rkey;
282 foreach my $datatype (qw/holdingbranches callnumbers locations itemtypes/){
284 foreach my $data (keys %{$line->{$datatype}}){
285 push @newdatas, { 'value' => $data}
287 $line->{$datatype} = \@newdatas;
290 foreach my $key (keys %$filters){
291 my $value = $filters->{$key};
292 $filtered = 0 if not (any { $_->{value} =~ /^$value$/ } @{$line->{$key}}) and $value;
294 push @reserves, $line if $filtered; # if (any { $_->{value} =~ /^FOSPC$/ } @{$line->{holdingbranches}});
297 my $count = scalar @reserves;
298 my $endindex = ($count > $startindex + $results) ? $startindex + $results : $count;
301 @reserves = @reserves[$startindex..$endindex];
305 return ($count, \@reserves);
308 =item GetReservesFromBiblionumber
310 ($count, $title_reserves) = &GetReserves($biblionumber);
312 This function gets the list of reservations for one C<$biblionumber>, returning a count
313 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
317 sub GetReservesFromBiblionumber {
318 my ($biblionumber) = shift or return (0, []);
319 my $dbh = C4::Context->dbh;
321 # Find the desired items in the reserves
324 timestamp AS rtimestamp,
334 WHERE biblionumber = ?
336 my $sth = $dbh->prepare($query);
337 $sth->execute($biblionumber);
340 while ( my $data = $sth->fetchrow_hashref ) {
342 # FIXME - What is this doing? How do constraints work?
343 if ($data->{constrainttype} eq 'o') {
345 SELECT biblioitemnumber
346 FROM reserveconstraints
347 WHERE biblionumber = ?
348 AND borrowernumber = ?
351 my $csth = $dbh->prepare($query);
352 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
354 while ( my $bibitemnos = $csth->fetchrow_array ) {
355 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
357 my $count = scalar @bibitemno;
359 # if we have two or more different specific itemtypes
360 # reserved by same person on same day
363 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
364 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
367 # Look up the book we just found.
368 $bdata = GetBiblioItemData( $bibitemno[0] );
370 # Add the results of this latest search to the current
372 # FIXME - An 'each' would probably be more efficient.
373 foreach my $key ( keys %$bdata ) {
374 $data->{$key} = $bdata->{$key};
377 push @results, $data;
379 return ( $#results + 1, \@results );
382 =item GetReservesFromItemnumber
384 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
386 TODO :: Description here
390 sub GetReservesFromItemnumber {
391 my ( $itemnumber ) = @_;
392 my $dbh = C4::Context->dbh;
394 SELECT reservedate,borrowernumber,branchcode
398 my $sth_res = $dbh->prepare($query);
399 $sth_res->execute($itemnumber);
400 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
401 return ( $reservedate, $borrowernumber, $branchcode );
404 =item GetReservesFromBorrowernumber
406 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
412 sub GetReservesFromBorrowernumber {
413 my ( $borrowernumber, $status ) = @_;
414 my $dbh = C4::Context->dbh;
417 $sth = $dbh->prepare("
420 WHERE borrowernumber=?
424 $sth->execute($borrowernumber,$status);
426 $sth = $dbh->prepare("
429 WHERE borrowernumber=?
432 $sth->execute($borrowernumber);
434 my $data = $sth->fetchall_arrayref({});
437 #-------------------------------------------------------------------------------------
438 =item CanBookBeReserved
440 $error = &CanBookBeReserved($borrowernumber, $biblionumber)
444 sub CanBookBeReserved{
445 my ($borrowernumber, $biblionumber) = @_;
447 my $dbh = C4::Context->dbh;
448 my $biblio = GetBiblioData($biblionumber);
449 my $borrower = C4::Members::GetMember(borrowernumber=>$borrowernumber);
450 my $controlbranch = C4::Context->preference('ReservesControlBranch');
451 my $itype = C4::Context->preference('item-level_itypes');
452 my $reservesrights= C4::Context->preference('maxreserves');
453 my $reservescount = 0;
455 # we retrieve the user rights
460 if($controlbranch eq "ItemHomeLibrary"){
462 }elsif($controlbranch eq "PatronLibrary"){
463 $branchcode = $borrower->{branchcode};
466 $reservescount = GetReserveCount($borrowernumber);
468 if($reservescount < $reservesrights){
476 =item CanItemBeReserved
478 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
480 this function return 1 if an item can be issued by this borrower.
484 sub CanItemBeReserved{
485 my ($borrowernumber, $itemnumber) = @_;
487 my $dbh = C4::Context->dbh;
489 my $controlbranch = C4::Context->preference('ReservesControlBranch') || "ItemHomeLibrary";
490 my $itype = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
491 my $allowedreserves = C4::Context->preference('maxreserves');
493 # we retrieve borrowers and items informations #
494 my $item = C4::Items::GetItem($itemnumber);
495 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
497 my $branchcode = "*";
498 my $branchfield = "reserves.branchcode";
500 if( $controlbranch eq "ItemHomeLibrary" ){
501 $branchcode = $item->{homebranch};
502 }elsif( $controlbranch eq "PatronLibrary" ){
503 $branchcode = $borrower->{branchcode};
506 # we retrieve user rights on this itemtype and branchcode
507 my $issuingrule = C4::Circulation::GetIssuingRule($borrower->{categorycode}, $item->{$itype}, $branchcode);
511 my $reservecount = GetReserveCount($borrowernumber);
513 # we check if it's ok or not
514 if(( $reservecount < $allowedreserves ) and $issuingrule->{maxissueqty} ){
520 #-------------------------------------------------------------------------------------
522 =item GetReserveCount
524 $number = &GetReserveCount($borrowernumber);
526 this function returns the number of reservation for a borrower given on input arg.
530 sub GetReserveCount {
531 my ($borrowernumber) = @_;
533 my $dbh = C4::Context->dbh;
536 SELECT COUNT(*) AS counter
538 WHERE borrowernumber = ?
540 my $sth = $dbh->prepare($query);
541 $sth->execute($borrowernumber);
542 my $row = $sth->fetchrow_hashref;
543 return $row->{counter};
546 =item GetOtherReserves
548 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
550 Check queued list of this document and check if this document must be transfered
554 sub GetOtherReserves {
555 my ($itemnumber) = @_;
558 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
559 if ($checkreserves) {
560 my $iteminfo = GetItem($itemnumber);
561 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
562 $messages->{'transfert'} = $checkreserves->{'branchcode'};
563 #minus priorities of others reservs
564 ModReserveMinusPriority(
566 $checkreserves->{'borrowernumber'},
567 $iteminfo->{'biblionumber'}
570 #launch the subroutine dotransfer
571 C4::Items::ModItemTransfer(
573 $iteminfo->{'holdingbranch'},
574 $checkreserves->{'branchcode'}
579 #step 2b : case of a reservation on the same branch, set the waiting status
581 $messages->{'waiting'} = 1;
582 ModReserveMinusPriority(
584 $checkreserves->{'borrowernumber'},
585 $iteminfo->{'biblionumber'}
587 ModReserveStatus($itemnumber,'W');
590 $nextreservinfo = $checkreserves->{'borrowernumber'};
593 return ( $messages, $nextreservinfo );
598 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
600 Calculate the fee for a reserve
605 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
608 my $dbh = C4::Context->dbh;
609 my $const = lc substr( $constraint, 0, 1 );
611 SELECT * FROM borrowers
612 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
613 WHERE borrowernumber = ?
615 my $sth = $dbh->prepare($query);
616 $sth->execute($borrowernumber);
617 my $data = $sth->fetchrow_hashref;
619 my $fee = $data->{'reservefee'};
620 my $cntitems = @- > $bibitems;
624 # check for items on issue
625 # first find biblioitem records
627 my $sth1 = $dbh->prepare(
628 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
629 WHERE (biblio.biblionumber = ?)"
631 $sth1->execute($biblionumber);
632 while ( my $data1 = $sth1->fetchrow_hashref ) {
633 if ( $const eq "a" ) {
634 push @biblioitems, $data1;
639 while ( $x < $cntitems ) {
640 if ( @$bibitems->{'biblioitemnumber'} ==
641 $data->{'biblioitemnumber'} )
647 if ( $const eq 'o' ) {
649 push @biblioitems, $data1;
654 push @biblioitems, $data1;
660 my $cntitemsfound = @biblioitems;
664 while ( $x < $cntitemsfound ) {
665 my $bitdata = $biblioitems[$x];
666 my $sth2 = $dbh->prepare(
668 WHERE biblioitemnumber = ?"
670 $sth2->execute( $bitdata->{'biblioitemnumber'} );
671 while ( my $itdata = $sth2->fetchrow_hashref ) {
672 my $sth3 = $dbh->prepare(
673 "SELECT * FROM issues
674 WHERE itemnumber = ?"
676 $sth3->execute( $itdata->{'itemnumber'} );
677 if ( my $isdata = $sth3->fetchrow_hashref ) {
685 if ( $allissued == 0 ) {
687 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
688 $rsth->execute($biblionumber);
689 if ( my $rdata = $rsth->fetchrow_hashref ) {
699 =item GetReservesToBranch
701 @transreserv = GetReservesToBranch( $frombranch );
703 Get reserve list for a given branch
707 sub GetReservesToBranch {
708 my ( $frombranch ) = @_;
709 my $dbh = C4::Context->dbh;
710 my $sth = $dbh->prepare(
711 "SELECT borrowernumber,reservedate,itemnumber,timestamp
716 $sth->execute( $frombranch );
719 while ( my $data = $sth->fetchrow_hashref ) {
720 $transreserv[$i] = $data;
723 return (@transreserv);
726 =item GetReservesForBranch
728 @transreserv = GetReservesForBranch($frombranch);
732 sub GetReservesForBranch {
733 my ($frombranch) = @_;
734 my $dbh = C4::Context->dbh;
735 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
740 $query .= " AND branchcode=? ";
742 $query .= "ORDER BY waitingdate" ;
743 my $sth = $dbh->prepare($query);
745 $sth->execute($frombranch);
752 while ( my $data = $sth->fetchrow_hashref ) {
753 $transreserv[$i] = $data;
756 return (@transreserv);
759 sub GetReserveStatus {
760 my ($itemnumber) = @_;
762 my $dbh = C4::Context->dbh;
764 my $itemstatus = $dbh->prepare("SELECT found FROM reserves WHERE itemnumber = ?");
766 $itemstatus->execute($itemnumber);
767 my ($found) = $itemstatus->fetchrow_array;
773 ($status, $reserve) = &CheckReserves($itemnumber);
775 Find a book in the reserves.
777 C<$itemnumber> is the book's item number.
779 As I understand it, C<&CheckReserves> looks for the given item in the
780 reserves. If it is found, that's a match, and C<$status> is set to
783 Otherwise, it finds the most important item in the reserves with the
784 same biblio number as this book (I'm not clear on this) and returns it
785 with C<$status> set to C<Reserved>.
787 C<&CheckReserves> returns a two-element list:
789 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
791 C<$reserve> is the reserve item that matched. It is a
792 reference-to-hash whose keys are mostly the fields of the reserves
793 table in the Koha database.
798 my ( $item, $barcode ) = @_;
799 my $dbh = C4::Context->dbh;
802 my $qitem = $dbh->quote($item);
803 # Look up the item by itemnumber
805 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
807 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
808 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
809 WHERE itemnumber=$qitem
811 $sth = $dbh->prepare($query);
814 my $qbc = $dbh->quote($barcode);
815 # Look up the item by barcode
817 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
819 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
820 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
821 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
822 AND biblioitems.itemtype = itemtypes.itemtype
825 $sth = $dbh->prepare($query);
827 # FIXME - This function uses $item later on. Ought to set it here.
830 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
832 # if item is not for loan it cannot be reserved either.....
833 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
834 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
836 # get the reserves...
837 # Find this item in the reserves
838 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
839 my $count = scalar @reserves;
841 # $priority and $highest are used to find the most important item
842 # in the list returned by &_Findgroupreserve. (The lower $priority,
843 # the more important the item.)
844 # $highest is the most important item we've seen so far.
845 my $priority = 10000000;
848 foreach my $res (@reserves) {
849 # FIXME - $item might be undefined or empty: the caller
850 # might be searching by barcode.
851 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
853 return ( "Waiting", $res );
856 # See if this item is more important than what we've got
858 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
860 $priority = $res->{'priority'};
867 # If we get this far, then no exact match was found. Print the
868 # most important item on the list. I think this tells us who's
869 # next in line to get this book.
870 if ($highest) { # FIXME - $highest might be undefined
871 $highest->{'itemnumber'} = $item;
872 return ( "Reserved", $highest );
881 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
885 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
886 cancel, but not both: if both are given, C<&CancelReserve> does
889 C<$borrowernumber> is the borrower number of the patron on whose
890 behalf the book was reserved.
892 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
893 priorities of the other people who are waiting on the book.
898 my ( $biblio, $item, $borr ) = @_;
899 my $dbh = C4::Context->dbh;
900 if ( $item and $borr ) {
901 # removing a waiting reserve record....
902 # update the database...
905 SET cancellationdate = now(),
909 AND borrowernumber = ?
911 my $sth = $dbh->prepare($query);
912 $sth->execute( $item, $borr );
915 INSERT INTO old_reserves
916 SELECT * FROM reserves
918 AND borrowernumber = ?
920 $sth = $dbh->prepare($query);
921 $sth->execute( $item, $borr );
925 AND borrowernumber = ?
927 $sth = $dbh->prepare($query);
928 $sth->execute( $item, $borr );
931 # removing a reserve record....
932 # get the prioritiy on this record....
935 SELECT priority FROM reserves
936 WHERE biblionumber = ?
937 AND borrowernumber = ?
938 AND cancellationdate IS NULL
939 AND itemnumber IS NULL
941 my $sth = $dbh->prepare($query);
942 $sth->execute( $biblio, $borr );
943 ($priority) = $sth->fetchrow_array;
947 SET cancellationdate = now(),
950 WHERE biblionumber = ?
951 AND borrowernumber = ?
954 # update the database, removing the record...
955 $sth = $dbh->prepare($query);
956 $sth->execute( $biblio, $borr );
960 INSERT INTO old_reserves
961 SELECT * FROM reserves
962 WHERE biblionumber = ?
963 AND borrowernumber = ?
965 $sth = $dbh->prepare($query);
966 $sth->execute( $biblio, $borr );
970 WHERE biblionumber = ?
971 AND borrowernumber = ?
973 $sth = $dbh->prepare($query);
974 $sth->execute( $biblio, $borr );
976 # now fix the priority on the others....
977 _FixPriority( $priority, $biblio );
985 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
989 Change a hold request's priority or cancel it.
991 C<$rank> specifies the effect of the change. If C<$rank>
992 is 'W' or 'n', nothing happens. This corresponds to leaving a
993 request alone when changing its priority in the holds queue
996 If C<$rank> is 'del', the hold request is cancelled.
998 If C<$rank> is an integer greater than zero, the priority of
999 the request is set to that value. Since priority != 0 means
1000 that the item is not waiting on the hold shelf, setting the
1001 priority to a non-zero value also sets the request's found
1002 status and waiting date to NULL.
1004 The optional C<$itemnumber> parameter is used only when
1005 C<$rank> is a non-zero integer; if supplied, the itemnumber
1006 of the hold request is set accordingly; if omitted, the itemnumber
1009 FIXME: Note that the forgoing can have the effect of causing
1010 item-level hold requests to turn into title-level requests. This
1011 will be fixed once reserves has separate columns for requested
1012 itemnumber and supplying itemnumber.
1017 #subroutine to update a reserve
1018 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
1019 return if $rank eq "W";
1020 return if $rank eq "n";
1021 my $dbh = C4::Context->dbh;
1022 if ( $rank eq "del" ) {
1025 SET cancellationdate=now()
1026 WHERE biblionumber = ?
1027 AND borrowernumber = ?
1029 my $sth = $dbh->prepare($query);
1030 $sth->execute( $biblio, $borrower );
1033 INSERT INTO old_reserves
1036 WHERE biblionumber = ?
1037 AND borrowernumber = ?
1039 $sth = $dbh->prepare($query);
1040 $sth->execute( $biblio, $borrower );
1042 DELETE FROM reserves
1043 WHERE biblionumber = ?
1044 AND borrowernumber = ?
1046 $sth = $dbh->prepare($query);
1047 $sth->execute( $biblio, $borrower );
1050 elsif ($rank =~ /^\d+/ and $rank > 0) {
1052 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1053 WHERE biblionumber = ?
1054 AND borrowernumber = ?
1056 my $sth = $dbh->prepare($query);
1057 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
1059 _FixPriority( $biblio, $borrower, $rank);
1063 =item ModReserveFill
1065 &ModReserveFill($reserve);
1067 Fill a reserve. If I understand this correctly, this means that the
1068 reserved book has been found and given to the patron who reserved it.
1070 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1071 whose keys are fields from the reserves table in the Koha database.
1075 sub ModReserveFill {
1077 my $dbh = C4::Context->dbh;
1078 # fill in a reserve record....
1079 my $biblionumber = $res->{'biblionumber'};
1080 my $borrowernumber = $res->{'borrowernumber'};
1081 my $resdate = $res->{'reservedate'};
1083 # get the priority on this record....
1085 my $query = "SELECT priority
1087 WHERE biblionumber = ?
1088 AND borrowernumber = ?
1089 AND reservedate = ?";
1090 my $sth = $dbh->prepare($query);
1091 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1092 ($priority) = $sth->fetchrow_array;
1095 # update the database...
1096 $query = "UPDATE reserves
1099 WHERE biblionumber = ?
1101 AND borrowernumber = ?
1103 $sth = $dbh->prepare($query);
1104 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1107 # move to old_reserves
1108 $query = "INSERT INTO old_reserves
1109 SELECT * FROM reserves
1110 WHERE biblionumber = ?
1112 AND borrowernumber = ?
1114 $sth = $dbh->prepare($query);
1115 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1116 $query = "DELETE FROM reserves
1117 WHERE biblionumber = ?
1119 AND borrowernumber = ?
1121 $sth = $dbh->prepare($query);
1122 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1124 # now fix the priority on the others (if the priority wasn't
1125 # already sorted!)....
1126 unless ( $priority == 0 ) {
1127 _FixPriority( $priority, $biblionumber );
1131 =item ModReserveStatus
1133 &ModReserveStatus($itemnumber, $newstatus);
1135 Update the reserve status for the active (priority=0) reserve.
1137 $itemnumber is the itemnumber the reserve is on
1139 $newstatus is the new status.
1143 sub ModReserveStatus {
1145 #first : check if we have a reservation for this item .
1146 my ($itemnumber, $newstatus) = @_;
1147 my $dbh = C4::Context->dbh;
1148 my $query = " UPDATE reserves
1149 SET found=?,waitingdate = now()
1154 my $sth_set = $dbh->prepare($query);
1155 $sth_set->execute( $newstatus, $itemnumber );
1158 =item ModReserveAffect
1160 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1162 This function affect an item and a status for a given reserve
1163 The itemnumber parameter is used to find the biblionumber.
1164 with the biblionumber & the borrowernumber, we can affect the itemnumber
1165 to the correct reserve.
1167 if $transferToDo is not set, then the status is set to "Waiting" as well.
1168 otherwise, a transfer is on the way, and the end of the transfer will
1169 take care of the waiting status
1172 sub ModReserveAffect {
1173 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1174 my $dbh = C4::Context->dbh;
1176 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1177 # attached to $itemnumber
1178 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1179 $sth->execute($itemnumber);
1180 my ($biblionumber) = $sth->fetchrow;
1181 # If we affect a reserve that has to be transfered, don't set to Waiting
1183 if ($transferToDo) {
1189 WHERE borrowernumber = ?
1190 AND biblionumber = ?
1194 # affect the reserve to Waiting as well.
1201 WHERE borrowernumber = ?
1202 AND biblionumber = ?
1205 $sth = $dbh->prepare($query);
1206 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1209 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1214 =item ModReserveCancelAll
1216 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1218 function to cancel reserv,check other reserves, and transfer document if it's necessary
1222 sub ModReserveCancelAll {
1225 my ( $itemnumber, $borrowernumber ) = @_;
1227 #step 1 : cancel the reservation
1228 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1230 #step 2 launch the subroutine of the others reserves
1231 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1233 return ( $messages, $nextreservinfo );
1236 =item ModReserveMinusPriority
1238 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1240 Reduce the values of queuded list
1244 sub ModReserveMinusPriority {
1245 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1247 #first step update the value of the first person on reserv
1248 my $dbh = C4::Context->dbh;
1251 SET priority = 0 , itemnumber = ?
1252 WHERE borrowernumber=?
1255 my $sth_upd = $dbh->prepare($query);
1256 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1257 # second step update all others reservs
1258 _FixPriority($biblionumber, $borrowernumber, '0');
1261 =item GetReserveInfo
1263 &GetReserveInfo($borrowernumber,$biblionumber);
1265 Get item and borrower details for a current hold.
1266 Current implementation this query should have a single result.
1269 sub GetReserveInfo {
1270 my ( $borrowernumber, $biblionumber ) = @_;
1271 my $dbh = C4::Context->dbh;
1272 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1273 reserves.biblionumber, reserves.branchcode,
1274 notificationdate, reminderdate, priority, found,
1275 firstname, surname, phone,
1276 email, address, address2,
1277 cardnumber, city, zipcode,
1278 biblio.title, biblio.author,
1279 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1281 FROM reserves left join items
1282 ON items.itemnumber=reserves.itemnumber ,
1285 reserves.borrowernumber=? &&
1286 reserves.biblionumber=? &&
1287 reserves.borrowernumber=borrowers.borrowernumber &&
1288 reserves.biblionumber=biblio.biblionumber ";
1289 my $sth = $dbh->prepare($strsth);
1290 $sth->execute($borrowernumber,$biblionumber);
1292 my $data = $sth->fetchrow_hashref;
1297 =item IsAvailableForItemLevelRequest
1301 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1305 Checks whether a given item record is available for an
1306 item-level hold request. An item is available if
1308 * it is not lost AND
1309 * it is not damaged AND
1310 * it is not withdrawn AND
1311 * does not have a not for loan value > 0
1313 Whether or not the item is currently on loan is
1314 also checked - if the AllowOnShelfHolds system preference
1315 is ON, an item can be requested even if it is currently
1316 on loan to somebody else. If the system preference
1317 is OFF, an item that is currently checked out cannot
1318 be the target of an item-level hold request.
1320 Note that IsAvailableForItemLevelRequest() does not
1321 check if the staff operator is authorized to place
1322 a request on the item - in particular,
1323 this routine does not check IndependantBranches
1324 and canreservefromotherbranches.
1328 sub IsAvailableForItemLevelRequest {
1329 my $itemnumber = shift;
1331 my $item = GetItem($itemnumber);
1333 # must check the notforloan setting of the itemtype
1334 # FIXME - a lot of places in the code do this
1335 # or something similar - need to be
1337 my $dbh = C4::Context->dbh;
1338 my $notforloan_query;
1339 if (C4::Context->preference('item-level_itypes')) {
1340 $notforloan_query = "SELECT itemtypes.notforloan
1342 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1343 WHERE itemnumber = ?";
1345 $notforloan_query = "SELECT itemtypes.notforloan
1347 JOIN biblioitems USING (biblioitemnumber)
1348 JOIN itemtypes USING (itemtype)
1349 WHERE itemnumber = ?";
1351 my $sth = $dbh->prepare($notforloan_query);
1352 $sth->execute($itemnumber);
1353 my $notforloan_per_itemtype = 0;
1354 if (my ($notforloan) = $sth->fetchrow_array) {
1355 $notforloan_per_itemtype = 1 if $notforloan;
1358 my $available_per_item = 1;
1359 $available_per_item = 0 if $item->{itemlost} or
1360 ( $item->{notforloan} > 0 ) or
1361 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1362 $item->{wthdrawn} or
1363 $notforloan_per_itemtype;
1366 if (C4::Context->preference('AllowOnShelfHolds')) {
1367 return $available_per_item;
1369 return ($available_per_item and ($item->{onloan} or GetReserveStatus($itemnumber) eq "W"));
1375 &_FixPriority($biblio,$borrowernumber,$rank);
1377 Only used internally (so don't export it)
1378 Changed how this functions works #
1379 Now just gets an array of reserves in the rank order and updates them with
1380 the array index (+1 as array starts from 0)
1381 and if $rank is supplied will splice item from the array and splice it back in again
1382 in new priority rank
1387 my ( $biblio, $borrowernumber, $rank ) = @_;
1388 my $dbh = C4::Context->dbh;
1389 if ( $rank eq "del" ) {
1390 CancelReserve( $biblio, undef, $borrowernumber );
1392 if ( $rank eq "W" || $rank eq "0" ) {
1394 # make sure priority for waiting items is 0
1398 WHERE biblionumber = ?
1399 AND borrowernumber = ?
1402 my $sth = $dbh->prepare($query);
1403 $sth->execute( $biblio, $borrowernumber );
1409 # FIXME adding a new security in returned elements for changing priority,
1410 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1411 # This is wrong a waiting reserve has W set
1412 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1414 SELECT borrowernumber, reservedate, constrainttype
1416 WHERE biblionumber = ?
1417 AND ((found <> 'W') or found is NULL)
1418 ORDER BY priority ASC
1420 my $sth = $dbh->prepare($query);
1421 $sth->execute($biblio);
1422 while ( my $line = $sth->fetchrow_hashref ) {
1423 push( @reservedates, $line );
1424 push( @priority, $line );
1427 # To find the matching index
1429 my $key = -1; # to allow for 0 to be a valid result
1430 for ( $i = 0 ; $i < @priority ; $i++ ) {
1431 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1432 $key = $i; # save the index
1437 # if index exists in array then move it to new position
1438 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1439 my $new_rank = $rank -
1440 1; # $new_rank is what you want the new index to be in the array
1441 my $moving_item = splice( @priority, $key, 1 );
1442 splice( @priority, $new_rank, 0, $moving_item );
1445 # now fix the priority on those that are left....
1449 WHERE biblionumber = ?
1450 AND borrowernumber = ?
1454 $sth = $dbh->prepare($query);
1455 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1458 $priority[$j]->{'borrowernumber'},
1459 $priority[$j]->{'reservedate'}
1465 =item _Findgroupreserve
1467 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1469 Looks for an item-specific match first, then for a title-level match, returning the
1470 first match found. If neither, then we look for a 3rd kind of match based on
1471 reserve constraints.
1473 TODO: add more explanation about reserve constraints
1475 C<&_Findgroupreserve> returns :
1476 C<@results> is an array of references-to-hash whose keys are mostly
1477 fields from the reserves table of the Koha database, plus
1478 C<biblioitemnumber>.
1482 sub _Findgroupreserve {
1483 my ( $bibitem, $biblio, $itemnumber ) = @_;
1484 my $dbh = C4::Context->dbh;
1486 # check for exact targetted match
1487 # This select is valid for both item_level and biblio_level
1488 my $item_level_target_query = qq/
1489 SELECT reserves.biblionumber AS biblionumber,
1490 reserves.borrowernumber AS borrowernumber,
1491 reserves.reservedate AS reservedate,
1492 reserves.branchcode AS branchcode,
1493 reserves.cancellationdate AS cancellationdate,
1494 reserves.found AS found,
1495 reserves.reservenotes AS reservenotes,
1496 reserves.priority AS priority,
1497 reserves.timestamp AS timestamp,
1498 biblioitems.biblioitemnumber AS biblioitemnumber,
1499 reserves.itemnumber AS itemnumber
1501 JOIN biblioitems USING (biblionumber)
1502 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1505 AND hold_fill_targets.itemnumber = ?
1508 my $sth = $dbh->prepare($item_level_target_query);
1509 $sth->execute($itemnumber);
1510 my $data = $sth->fetchall_arrayref({});
1511 return @$data if (@$data);
1513 # check for title-level targetted match
1514 my $title_level_target_query = qq/
1515 SELECT reserves.biblionumber AS biblionumber,
1516 reserves.borrowernumber AS borrowernumber,
1517 reserves.reservedate AS reservedate,
1518 reserves.branchcode AS branchcode,
1519 reserves.cancellationdate AS cancellationdate,
1520 reserves.found AS found,
1521 reserves.reservenotes AS reservenotes,
1522 reserves.priority AS priority,
1523 reserves.timestamp AS timestamp,
1524 biblioitems.biblioitemnumber AS biblioitemnumber,
1525 reserves.itemnumber AS itemnumber
1527 JOIN biblioitems USING (biblionumber)
1528 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1531 AND item_level_request = 0
1532 AND hold_fill_targets.itemnumber = ?
1534 $sth = $dbh->prepare($title_level_target_query);
1535 $sth->execute($itemnumber);
1536 $data = $sth->fetchall_arrayref({});
1537 return @$data if (@$data);
1540 SELECT reserves.biblionumber AS biblionumber,
1541 reserves.borrowernumber AS borrowernumber,
1542 reserves.reservedate AS reservedate,
1543 reserves.branchcode AS branchcode,
1544 reserves.cancellationdate AS cancellationdate,
1545 reserves.found AS found,
1546 reserves.reservenotes AS reservenotes,
1547 reserves.priority AS priority,
1548 reserves.timestamp AS timestamp,
1549 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1550 reserves.itemnumber AS itemnumber
1552 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1553 WHERE reserves.biblionumber = ?
1554 AND ( ( reserveconstraints.biblioitemnumber = ?
1555 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1556 AND reserves.reservedate = reserveconstraints.reservedate )
1557 OR reserves.constrainttype='a' )
1558 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1560 $sth = $dbh->prepare($query);
1561 $sth->execute( $biblio, $bibitem, $itemnumber );
1562 $data = $sth->fetchall_arrayref({});
1563 return @$data if (@$data);
1567 =item _koha_notify_reserve
1571 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1575 Sends a notification to the patron that their hold has been filled (through
1576 ModReserveAffect, _not_ ModReserveFill)
1580 sub _koha_notify_reserve {
1581 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1583 my $dbh = C4::Context->dbh;
1584 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1586 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1588 my $sth = $dbh->prepare("
1591 WHERE borrowernumber = ?
1592 AND biblionumber = ?
1594 $sth->execute( $borrowernumber, $biblionumber );
1595 my $reserve = $sth->fetchrow_hashref;
1596 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1598 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1600 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1602 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1603 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1604 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1605 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1607 if ( $reserve->{'itemnumber'} ) {
1608 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1610 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1612 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1613 # aka, 'email' in ->{'transports'}
1614 C4::Letters::EnqueueLetter(
1615 { letter => $letter,
1616 borrowernumber => $borrowernumber,
1617 message_transport_type => 'email',
1618 from_address => $admin_email_address,
1623 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1624 C4::Letters::EnqueueLetter(
1625 { letter => $letter,
1626 borrowernumber => $borrowernumber,
1627 message_transport_type => 'sms',
1637 Koha Developement team <info@koha.org>