2 # NOTE: This file uses standard 8-character tabs
6 # Copyright 2000-2002 Katipo Communications
7 # 2006 SAN Ouest Provence
8 # 2007 BibLibre Paul POULAIN
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
34 # for _koha_notify_reserve
35 use C4::Members::Messaging;
36 use C4::Members qw( GetMember );
38 use C4::Branch qw( GetBranchDetail );
39 use List::MoreUtils qw( firstidx );
41 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
43 my $library_name = C4::Context->preference("LibraryName");
47 C4::Reserves - Koha functions for dealing with reservation.
55 this modules provides somes functions to deal with reservations.
57 Reserves are stored in reserves table.
58 The following columns contains important values :
59 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
60 =0 : then the reserve is being dealed
61 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
62 W(aiting) : the reserve has an itemnumber affected, and is on the way
63 F(inished) : the reserve has been completed, and is done
64 - itemnumber : empty : the reserve is still unaffected to an item
65 filled: the reserve is attached to an item
66 The complete workflow is :
67 ==== 1st use case ====
68 patron request a document, 1st available : P >0, F=NULL, I=NULL
69 a library having it run "transfertodo", and clic on the list
70 if there is no transfer to do, the reserve waiting
71 patron can pick it up P =0, F=W, I=filled
72 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
73 The pickup library recieve the book, it check in P =0, F=W, I=filled
74 The patron borrow the book P =0, F=F, I=filled
76 ==== 2nd use case ====
77 patron requests a document, a given item,
78 If pickup is holding branch P =0, F=W, I=filled
79 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
80 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
81 The patron borrow the book P =0, F=F, I=filled
90 # set the version for version checking
97 &GetReservesFromItemnumber
98 &GetReservesFromBiblionumber
99 &GetReservesFromBorrowernumber
100 &GetReservesForBranch
113 &ModReserveMinusPriority
118 &IsAvailableForItemLevelRequest
124 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
130 $branch, $borrowernumber, $biblionumber,
131 $constraint, $bibitems, $priority, $notes,
132 $title, $checkitem, $found
135 GetReserveFee($borrowernumber, $biblionumber, $constraint,
137 my $dbh = C4::Context->dbh;
138 my $const = lc substr( $constraint, 0, 1 );
139 my @datearr = localtime(time);
141 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
144 # If the reserv had the waiting status, we had the value of the resdate
145 if ( $found eq 'W' ) {
146 $waitingdate = $resdate;
150 # updates take place here
152 my $nextacctno = &getnextacctno( $borrowernumber );
154 INSERT INTO accountlines
155 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
157 (?,?,now(),?,?,'Res',?)
159 my $usth = $dbh->prepare($query);
160 $usth->execute( $borrowernumber, $nextacctno, $fee,
161 "Reserve Charge - $title", $fee );
168 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
169 priority,reservenotes,itemnumber,found,waitingdate)
174 my $sth = $dbh->prepare($query);
176 $borrowernumber, $biblionumber, $resdate, $branch,
177 $const, $priority, $notes, $checkitem,
183 if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
184 my $numitems = @$bibitems;
186 while ( $i < $numitems ) {
187 my $biblioitem = @$bibitems[$i];
189 INSERT INTO reserveconstraints
190 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
194 my $sth = $dbh->prepare("");
195 $sth->execute( $borrowernumber, $biblionumber, $resdate,
204 =item GetReservesFromBiblionumber
206 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
208 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
210 Only 1 argument has to be passed.
214 sub GetReservesFromBiblionumber {
215 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
216 my $dbh = C4::Context->dbh;
218 # Find the desired items in the reserves
221 timestamp AS rtimestamp,
231 WHERE biblionumber = ?
233 my $sth = $dbh->prepare($query);
234 $sth->execute($biblionumber);
237 while ( my $data = $sth->fetchrow_hashref ) {
239 # FIXME - What is this if-statement doing? How do constraints work?
240 if ( $data->{constrainttype} eq 'o' ) {
242 SELECT biblioitemnumber
243 FROM reserveconstraints
244 WHERE biblionumber = ?
245 AND borrowernumber = ?
248 my $csth = $dbh->prepare($query);
249 $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
250 $data->{reservedate}, );
253 while ( my $bibitemnos = $csth->fetchrow_array ) {
254 push( @bibitemno, $bibitemnos );
256 my $count = @bibitemno;
258 # if we have two or more different specific itemtypes
259 # reserved by same person on same day
262 $bdata = GetBiblioItemData( $bibitemno[$i] );
267 # Look up the book we just found.
268 $bdata = GetBiblioItemData( $bibitemno[0] );
272 # Add the results of this latest search to the current
274 # FIXME - An 'each' would probably be more efficient.
275 foreach my $key ( keys %$bdata ) {
276 $data->{$key} = $bdata->{$key};
279 push @results, $data;
282 return ( $#results + 1, \@results );
285 =item GetReservesFromItemnumber
287 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
289 TODO :: Description here
293 sub GetReservesFromItemnumber {
294 my ( $itemnumber ) = @_;
295 my $dbh = C4::Context->dbh;
297 SELECT reservedate,borrowernumber,branchcode
301 my $sth_res = $dbh->prepare($query);
302 $sth_res->execute($itemnumber);
303 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
304 return ( $reservedate, $borrowernumber, $branchcode );
307 =item GetReservesFromBorrowernumber
309 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
315 sub GetReservesFromBorrowernumber {
316 my ( $borrowernumber, $status ) = @_;
317 my $dbh = C4::Context->dbh;
320 $sth = $dbh->prepare("
323 WHERE borrowernumber=?
327 $sth->execute($borrowernumber,$status);
329 $sth = $dbh->prepare("
332 WHERE borrowernumber=?
335 $sth->execute($borrowernumber);
337 my $data = $sth->fetchall_arrayref({});
340 #-------------------------------------------------------------------------------------
342 =item GetReserveCount
344 $number = &GetReserveCount($borrowernumber);
346 this function returns the number of reservation for a borrower given on input arg.
350 sub GetReserveCount {
351 my ($borrowernumber) = @_;
353 my $dbh = C4::Context->dbh;
356 SELECT COUNT(*) AS counter
358 WHERE borrowernumber = ?
360 my $sth = $dbh->prepare($query);
361 $sth->execute($borrowernumber);
362 my $row = $sth->fetchrow_hashref;
365 return $row->{counter};
368 =item GetOtherReserves
370 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
372 Check queued list of this document and check if this document must be transfered
376 sub GetOtherReserves {
377 my ($itemnumber) = @_;
380 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
381 if ($checkreserves) {
382 my $iteminfo = GetItem($itemnumber);
383 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
384 $messages->{'transfert'} = $checkreserves->{'branchcode'};
385 #minus priorities of others reservs
386 ModReserveMinusPriority(
388 $checkreserves->{'borrowernumber'},
389 $iteminfo->{'biblionumber'}
392 #launch the subroutine dotransfer
393 C4::Circulation::ModItemTransfer(
395 $iteminfo->{'holdingbranch'},
396 $checkreserves->{'branchcode'}
401 #step 2b : case of a reservation on the same branch, set the waiting status
403 $messages->{'waiting'} = 1;
404 ModReserveMinusPriority(
406 $checkreserves->{'borrowernumber'},
407 $iteminfo->{'biblionumber'}
409 ModReserveStatus($itemnumber,'W');
412 $nextreservinfo = $checkreserves->{'borrowernumber'};
415 return ( $messages, $nextreservinfo );
420 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
422 Calculate the fee for a reserve
427 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
430 my $dbh = C4::Context->dbh;
431 my $const = lc substr( $constraint, 0, 1 );
433 SELECT * FROM borrowers
434 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
435 WHERE borrowernumber = ?
437 my $sth = $dbh->prepare($query);
438 $sth->execute($borrowernumber);
439 my $data = $sth->fetchrow_hashref;
441 my $fee = $data->{'reservefee'};
442 my $cntitems = @- > $bibitems;
446 # check for items on issue
447 # first find biblioitem records
449 my $sth1 = $dbh->prepare(
450 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
451 WHERE (biblio.biblionumber = ?)"
453 $sth1->execute($biblionumber);
454 while ( my $data1 = $sth1->fetchrow_hashref ) {
455 if ( $const eq "a" ) {
456 push @biblioitems, $data1;
461 while ( $x < $cntitems ) {
462 if ( @$bibitems->{'biblioitemnumber'} ==
463 $data->{'biblioitemnumber'} )
469 if ( $const eq 'o' ) {
471 push @biblioitems, $data1;
476 push @biblioitems, $data1;
482 my $cntitemsfound = @biblioitems;
486 while ( $x < $cntitemsfound ) {
487 my $bitdata = $biblioitems[$x];
488 my $sth2 = $dbh->prepare(
490 WHERE biblioitemnumber = ?"
492 $sth2->execute( $bitdata->{'biblioitemnumber'} );
493 while ( my $itdata = $sth2->fetchrow_hashref ) {
494 my $sth3 = $dbh->prepare(
495 "SELECT * FROM issues
496 WHERE itemnumber = ?"
498 $sth3->execute( $itdata->{'itemnumber'} );
499 if ( my $isdata = $sth3->fetchrow_hashref ) {
507 if ( $allissued == 0 ) {
509 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
510 $rsth->execute($biblionumber);
511 if ( my $rdata = $rsth->fetchrow_hashref ) {
521 =item GetReservesToBranch
523 @transreserv = GetReservesToBranch( $frombranch );
525 Get reserve list for a given branch
529 sub GetReservesToBranch {
530 my ( $frombranch ) = @_;
531 my $dbh = C4::Context->dbh;
532 my $sth = $dbh->prepare(
533 "SELECT borrowernumber,reservedate,itemnumber,timestamp
538 $sth->execute( $frombranch );
541 while ( my $data = $sth->fetchrow_hashref ) {
542 $transreserv[$i] = $data;
546 return (@transreserv);
549 =item GetReservesForBranch
551 @transreserv = GetReservesForBranch($frombranch);
555 sub GetReservesForBranch {
556 my ($frombranch) = @_;
557 my $dbh = C4::Context->dbh;
558 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
563 $query .= " AND branchcode=? ";
565 $query .= "ORDER BY waitingdate" ;
566 my $sth = $dbh->prepare($query);
568 $sth->execute($frombranch);
575 while ( my $data = $sth->fetchrow_hashref ) {
576 $transreserv[$i] = $data;
580 return (@transreserv);
585 ($status, $reserve) = &CheckReserves($itemnumber);
587 Find a book in the reserves.
589 C<$itemnumber> is the book's item number.
591 As I understand it, C<&CheckReserves> looks for the given item in the
592 reserves. If it is found, that's a match, and C<$status> is set to
595 Otherwise, it finds the most important item in the reserves with the
596 same biblio number as this book (I'm not clear on this) and returns it
597 with C<$status> set to C<Reserved>.
599 C<&CheckReserves> returns a two-element list:
601 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
603 C<$reserve> is the reserve item that matched. It is a
604 reference-to-hash whose keys are mostly the fields of the reserves
605 table in the Koha database.
610 my ( $item, $barcode ) = @_;
611 my $dbh = C4::Context->dbh;
614 my $qitem = $dbh->quote($item);
615 # Look up the item by itemnumber
617 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
619 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
620 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
621 WHERE itemnumber=$qitem
623 $sth = $dbh->prepare($query);
626 my $qbc = $dbh->quote($barcode);
627 # Look up the item by barcode
629 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
631 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
632 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
633 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
634 AND biblioitems.itemtype = itemtypes.itemtype
637 $sth = $dbh->prepare($query);
639 # FIXME - This function uses $item later on. Ought to set it here.
642 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
644 # if item is not for loan it cannot be reserved either.....
645 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
646 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
648 # get the reserves...
649 # Find this item in the reserves
650 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
651 my $count = scalar @reserves;
653 # $priority and $highest are used to find the most important item
654 # in the list returned by &_Findgroupreserve. (The lower $priority,
655 # the more important the item.)
656 # $highest is the most important item we've seen so far.
657 my $priority = 10000000;
660 foreach my $res (@reserves) {
661 # FIXME - $item might be undefined or empty: the caller
662 # might be searching by barcode.
663 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
665 return ( "Waiting", $res );
668 # See if this item is more important than what we've got
670 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
672 $priority = $res->{'priority'};
679 # If we get this far, then no exact match was found. Print the
680 # most important item on the list. I think this tells us who's
681 # next in line to get this book.
682 if ($highest) { # FIXME - $highest might be undefined
683 $highest->{'itemnumber'} = $item;
684 return ( "Reserved", $highest );
693 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
697 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
698 cancel, but not both: if both are given, C<&CancelReserve> does
701 C<$borrowernumber> is the borrower number of the patron on whose
702 behalf the book was reserved.
704 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
705 priorities of the other people who are waiting on the book.
710 my ( $biblio, $item, $borr ) = @_;
711 my $dbh = C4::Context->dbh;
712 if ( $item and $borr ) {
713 # removing a waiting reserve record....
714 # update the database...
717 SET cancellationdate = now(),
721 AND borrowernumber = ?
723 my $sth = $dbh->prepare($query);
724 $sth->execute( $item, $borr );
727 INSERT INTO old_reserves
728 SELECT * FROM reserves
730 AND borrowernumber = ?
732 $sth = $dbh->prepare($query);
733 $sth->execute( $item, $borr );
737 AND borrowernumber = ?
739 $sth = $dbh->prepare($query);
740 $sth->execute( $item, $borr );
743 # removing a reserve record....
744 # get the prioritiy on this record....
747 SELECT priority FROM reserves
748 WHERE biblionumber = ?
749 AND borrowernumber = ?
750 AND cancellationdate IS NULL
751 AND itemnumber IS NULL
753 my $sth = $dbh->prepare($query);
754 $sth->execute( $biblio, $borr );
755 ($priority) = $sth->fetchrow_array;
759 SET cancellationdate = now(),
762 WHERE biblionumber = ?
763 AND borrowernumber = ?
766 # update the database, removing the record...
767 $sth = $dbh->prepare($query);
768 $sth->execute( $biblio, $borr );
772 INSERT INTO old_reserves
773 SELECT * FROM reserves
774 WHERE biblionumber = ?
775 AND borrowernumber = ?
777 $sth = $dbh->prepare($query);
778 $sth->execute( $biblio, $borr );
782 WHERE biblionumber = ?
783 AND borrowernumber = ?
785 $sth = $dbh->prepare($query);
786 $sth->execute( $biblio, $borr );
788 # now fix the priority on the others....
789 _FixPriority( $priority, $biblio );
797 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
801 Change a hold request's priority or cancel it.
803 C<$rank> specifies the effect of the change. If C<$rank>
804 is 'W' or 'n', nothing happens. This corresponds to leaving a
805 request alone when changing its priority in the holds queue
808 If C<$rank> is 'del', the hold request is cancelled.
810 If C<$rank> is an integer greater than zero, the priority of
811 the request is set to that value. Since priority != 0 means
812 that the item is not waiting on the hold shelf, setting the
813 priority to a non-zero value also sets the request's found
814 status and waiting date to NULL.
816 The optional C<$itemnumber> parameter is used only when
817 C<$rank> is a non-zero integer; if supplied, the itemnumber
818 of the hold request is set accordingly; if omitted, the itemnumber
821 FIXME: Note that the forgoing can have the effect of causing
822 item-level hold requests to turn into title-level requests. This
823 will be fixed once reserves has separate columns for requested
824 itemnumber and supplying itemnumber.
829 #subroutine to update a reserve
830 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
831 return if $rank eq "W";
832 return if $rank eq "n";
833 my $dbh = C4::Context->dbh;
834 if ( $rank eq "del" ) {
837 SET cancellationdate=now()
838 WHERE biblionumber = ?
839 AND borrowernumber = ?
841 my $sth = $dbh->prepare($query);
842 $sth->execute( $biblio, $borrower );
845 INSERT INTO old_reserves
848 WHERE biblionumber = ?
849 AND borrowernumber = ?
851 $sth = $dbh->prepare($query);
852 $sth->execute( $biblio, $borrower );
855 WHERE biblionumber = ?
856 AND borrowernumber = ?
858 $sth = $dbh->prepare($query);
859 $sth->execute( $biblio, $borrower );
862 elsif ($rank =~ /^\d+/ and $rank > 0) {
864 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
865 WHERE biblionumber = ?
866 AND borrowernumber = ?
868 my $sth = $dbh->prepare($query);
869 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
871 _FixPriority( $biblio, $borrower, $rank);
877 &ModReserveFill($reserve);
879 Fill a reserve. If I understand this correctly, this means that the
880 reserved book has been found and given to the patron who reserved it.
882 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
883 whose keys are fields from the reserves table in the Koha database.
889 my $dbh = C4::Context->dbh;
890 # fill in a reserve record....
891 my $biblionumber = $res->{'biblionumber'};
892 my $borrowernumber = $res->{'borrowernumber'};
893 my $resdate = $res->{'reservedate'};
895 # get the priority on this record....
897 my $query = "SELECT priority
899 WHERE biblionumber = ?
900 AND borrowernumber = ?
901 AND reservedate = ?";
902 my $sth = $dbh->prepare($query);
903 $sth->execute( $biblionumber, $borrowernumber, $resdate );
904 ($priority) = $sth->fetchrow_array;
907 # update the database...
908 $query = "UPDATE reserves
911 WHERE biblionumber = ?
913 AND borrowernumber = ?
915 $sth = $dbh->prepare($query);
916 $sth->execute( $biblionumber, $resdate, $borrowernumber );
919 # move to old_reserves
920 $query = "INSERT INTO old_reserves
921 SELECT * FROM reserves
922 WHERE biblionumber = ?
924 AND borrowernumber = ?
926 $sth = $dbh->prepare($query);
927 $sth->execute( $biblionumber, $resdate, $borrowernumber );
928 $query = "DELETE FROM reserves
929 WHERE biblionumber = ?
931 AND borrowernumber = ?
933 $sth = $dbh->prepare($query);
934 $sth->execute( $biblionumber, $resdate, $borrowernumber );
936 # now fix the priority on the others (if the priority wasn't
937 # already sorted!)....
938 unless ( $priority == 0 ) {
939 _FixPriority( $priority, $biblionumber );
943 =item ModReserveStatus
945 &ModReserveStatus($itemnumber, $newstatus);
947 Update the reserve status for the active (priority=0) reserve.
949 $itemnumber is the itemnumber the reserve is on
951 $newstatus is the new status.
955 sub ModReserveStatus {
957 #first : check if we have a reservation for this item .
958 my ($itemnumber, $newstatus) = @_;
959 my $dbh = C4::Context->dbh;
960 my $query = " UPDATE reserves
961 SET found=?,waitingdate = now()
966 my $sth_set = $dbh->prepare($query);
967 $sth_set->execute( $newstatus, $itemnumber );
971 =item ModReserveAffect
973 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
975 This function affect an item and a status for a given reserve
976 The itemnumber parameter is used to find the biblionumber.
977 with the biblionumber & the borrowernumber, we can affect the itemnumber
978 to the correct reserve.
980 if $transferToDo is not set, then the status is set to "Waiting" as well.
981 otherwise, a transfer is on the way, and the end of the transfer will
982 take care of the waiting status
985 sub ModReserveAffect {
986 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
987 my $dbh = C4::Context->dbh;
989 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
990 # attached to $itemnumber
991 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
992 $sth->execute($itemnumber);
993 my ($biblionumber) = $sth->fetchrow;
994 # If we affect a reserve that has to be transfered, don't set to Waiting
1001 WHERE borrowernumber = ?
1002 AND biblionumber = ?
1006 # affect the reserve to Waiting as well.
1013 WHERE borrowernumber = ?
1014 AND biblionumber = ?
1017 $sth = $dbh->prepare($query);
1018 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1021 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1026 =item ModReserveCancelAll
1028 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1030 function to cancel reserv,check other reserves, and transfer document if it's necessary
1034 sub ModReserveCancelAll {
1037 my ( $itemnumber, $borrowernumber ) = @_;
1039 #step 1 : cancel the reservation
1040 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1042 #step 2 launch the subroutine of the others reserves
1043 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1045 return ( $messages, $nextreservinfo );
1048 =item ModReserveMinusPriority
1050 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1052 Reduce the values of queuded list
1056 sub ModReserveMinusPriority {
1057 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1059 #first step update the value of the first person on reserv
1060 my $dbh = C4::Context->dbh;
1063 SET priority = 0 , itemnumber = ?
1064 WHERE borrowernumber=?
1067 my $sth_upd = $dbh->prepare($query);
1068 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1070 # second step update all others reservs
1071 _FixPriority($biblionumber, $borrowernumber, '0');
1074 =item GetReserveInfo
1076 &GetReserveInfo($borrowernumber,$biblionumber);
1078 Get item and borrower details for a current hold.
1079 Current implementation this query should have a single result.
1082 sub GetReserveInfo {
1083 my ( $borrowernumber, $biblionumber ) = @_;
1084 my $dbh = C4::Context->dbh;
1085 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1086 reserves.biblionumber, reserves.branchcode,
1087 notificationdate, reminderdate, priority, found,
1088 firstname, surname, phone,
1089 email, address, address2,
1090 cardnumber, city, zipcode,
1091 biblio.title, biblio.author,
1092 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1094 FROM reserves left join items
1095 ON items.itemnumber=reserves.itemnumber ,
1098 reserves.borrowernumber=? &&
1099 reserves.biblionumber=? &&
1100 reserves.borrowernumber=borrowers.borrowernumber &&
1101 reserves.biblionumber=biblio.biblionumber ";
1102 my $sth = $dbh->prepare($strsth);
1103 $sth->execute($borrowernumber,$biblionumber);
1105 my $data = $sth->fetchrow_hashref;
1110 =item IsAvailableForItemLevelRequest
1114 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1118 Checks whether a given item record is available for an
1119 item-level hold request. An item is available if
1121 * it is not lost AND
1122 * it is not damaged AND
1123 * it is not withdrawn AND
1124 * does not have a not for loan value > 0
1126 Whether or not the item is currently on loan is
1127 also checked - if the AllowOnShelfHolds system preference
1128 is ON, an item can be requested even if it is currently
1129 on loan to somebody else. If the system preference
1130 is OFF, an item that is currently checked out cannot
1131 be the target of an item-level hold request.
1133 Note that IsAvailableForItemLevelRequest() does not
1134 check if the staff operator is authorized to place
1135 a request on the item - in particular,
1136 this routine does not check IndependantBranches
1137 and canreservefromotherbranches.
1141 sub IsAvailableForItemLevelRequest {
1142 my $itemnumber = shift;
1144 my $item = GetItem($itemnumber);
1146 # must check the notforloan setting of the itemtype
1147 # FIXME - a lot of places in the code do this
1148 # or something similar - need to be
1150 my $dbh = C4::Context->dbh;
1151 my $notforloan_query;
1152 if (C4::Context->preference('item-level_itypes')) {
1153 $notforloan_query = "SELECT itemtypes.notforloan
1155 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1156 WHERE itemnumber = ?";
1158 $notforloan_query = "SELECT itemtypes.notforloan
1160 JOIN biblioitems USING (biblioitemnumber)
1161 JOIN itemtypes USING (itemtype)
1162 WHERE itemnumber = ?";
1164 my $sth = $dbh->prepare($notforloan_query);
1165 $sth->execute($itemnumber);
1166 my $notforloan_per_itemtype = 0;
1167 if (my ($notforloan) = $sth->fetchrow_array) {
1168 $notforloan_per_itemtype = 1 if $notforloan;
1171 my $available_per_item = 1;
1172 $available_per_item = 0 if $item->{itemlost} or
1173 ( $item->{notforloan} > 0 ) or
1174 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1175 $item->{wthdrawn} or
1176 $notforloan_per_itemtype;
1178 if (C4::Context->preference('AllowOnShelfHolds')) {
1179 return $available_per_item;
1181 return ($available_per_item and $item->{onloan});
1187 &_FixPriority($biblio,$borrowernumber,$rank);
1189 Only used internally (so don't export it)
1190 Changed how this functions works #
1191 Now just gets an array of reserves in the rank order and updates them with
1192 the array index (+1 as array starts from 0)
1193 and if $rank is supplied will splice item from the array and splice it back in again
1194 in new priority rank
1199 my ( $biblio, $borrowernumber, $rank ) = @_;
1200 my $dbh = C4::Context->dbh;
1201 if ( $rank eq "del" ) {
1202 CancelReserve( $biblio, undef, $borrowernumber );
1204 if ( $rank eq "W" || $rank eq "0" ) {
1206 # make sure priority for waiting items is 0
1210 WHERE biblionumber = ?
1211 AND borrowernumber = ?
1214 my $sth = $dbh->prepare($query);
1215 $sth->execute( $biblio, $borrowernumber );
1221 # FIXME adding a new security in returned elements for changing priority,
1222 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1223 # This is wrong a waiting reserve has W set
1224 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1226 SELECT borrowernumber, reservedate, constrainttype
1228 WHERE biblionumber = ?
1229 AND ((found <> 'W') or found is NULL)
1230 ORDER BY priority ASC
1232 my $sth = $dbh->prepare($query);
1233 $sth->execute($biblio);
1234 while ( my $line = $sth->fetchrow_hashref ) {
1235 push( @reservedates, $line );
1236 push( @priority, $line );
1239 # To find the matching index
1241 my $key = -1; # to allow for 0 to be a valid result
1242 for ( $i = 0 ; $i < @priority ; $i++ ) {
1243 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1244 $key = $i; # save the index
1249 # if index exists in array then move it to new position
1250 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1251 my $new_rank = $rank -
1252 1; # $new_rank is what you want the new index to be in the array
1253 my $moving_item = splice( @priority, $key, 1 );
1254 splice( @priority, $new_rank, 0, $moving_item );
1257 # now fix the priority on those that are left....
1261 WHERE biblionumber = ?
1262 AND borrowernumber = ?
1266 $sth = $dbh->prepare($query);
1267 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1270 $priority[$j]->{'borrowernumber'},
1271 $priority[$j]->{'reservedate'}
1277 =item _Findgroupreserve
1279 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1282 I don't know what this does, because I don't understand how reserve
1283 constraints work. I think the idea is that you reserve a particular
1284 biblio, and the constraint allows you to restrict it to a given
1285 biblioitem (e.g., if you want to borrow the audio book edition of "The
1286 Prophet", rather than the first available publication).
1288 C<&_Findgroupreserve> returns :
1289 C<@results> is an array of references-to-hash whose keys are mostly
1290 fields from the reserves table of the Koha database, plus
1291 C<biblioitemnumber>.
1295 sub _Findgroupreserve {
1296 my ( $bibitem, $biblio, $itemnumber ) = @_;
1297 my $dbh = C4::Context->dbh;
1299 # check for exact targetted match
1300 my $item_level_target_query = qq/
1301 SELECT reserves.biblionumber AS biblionumber,
1302 reserves.borrowernumber AS borrowernumber,
1303 reserves.reservedate AS reservedate,
1304 reserves.branchcode AS branchcode,
1305 reserves.cancellationdate AS cancellationdate,
1306 reserves.found AS found,
1307 reserves.reservenotes AS reservenotes,
1308 reserves.priority AS priority,
1309 reserves.timestamp AS timestamp,
1310 biblioitems.biblioitemnumber AS biblioitemnumber,
1311 reserves.itemnumber AS itemnumber
1313 JOIN biblioitems USING (biblionumber)
1314 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1317 AND item_level_request = 1
1320 my $sth = $dbh->prepare($item_level_target_query);
1321 $sth->execute($itemnumber);
1323 if ( my $data = $sth->fetchrow_hashref ) {
1324 push( @results, $data );
1326 return @results if @results;
1328 # check for title-level targetted match
1329 my $title_level_target_query = qq/
1330 SELECT reserves.biblionumber AS biblionumber,
1331 reserves.borrowernumber AS borrowernumber,
1332 reserves.reservedate AS reservedate,
1333 reserves.branchcode AS branchcode,
1334 reserves.cancellationdate AS cancellationdate,
1335 reserves.found AS found,
1336 reserves.reservenotes AS reservenotes,
1337 reserves.priority AS priority,
1338 reserves.timestamp AS timestamp,
1339 biblioitems.biblioitemnumber AS biblioitemnumber,
1340 reserves.itemnumber AS itemnumber
1342 JOIN biblioitems USING (biblionumber)
1343 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1346 AND item_level_request = 0
1347 AND hold_fill_targets.itemnumber = ?
1349 $sth = $dbh->prepare($title_level_target_query);
1350 $sth->execute($itemnumber);
1352 if ( my $data = $sth->fetchrow_hashref ) {
1353 push( @results, $data );
1355 return @results if @results;
1358 SELECT reserves.biblionumber AS biblionumber,
1359 reserves.borrowernumber AS borrowernumber,
1360 reserves.reservedate AS reservedate,
1361 reserves.branchcode AS branchcode,
1362 reserves.cancellationdate AS cancellationdate,
1363 reserves.found AS found,
1364 reserves.reservenotes AS reservenotes,
1365 reserves.priority AS priority,
1366 reserves.timestamp AS timestamp,
1367 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1368 reserves.itemnumber AS itemnumber
1370 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1371 WHERE reserves.biblionumber = ?
1372 AND ( ( reserveconstraints.biblioitemnumber = ?
1373 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1374 AND reserves.reservedate =reserveconstraints.reservedate )
1375 OR reserves.constrainttype='a' )
1376 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1378 $sth = $dbh->prepare($query);
1379 $sth->execute( $biblio, $bibitem, $itemnumber );
1381 while ( my $data = $sth->fetchrow_hashref ) {
1382 push( @results, $data );
1388 =item _koha_notify_reserve
1392 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1396 Sends a notification to the patron that their hold has been filled (through
1397 ModReserveAffect, _not_ ModReserveFill)
1401 sub _koha_notify_reserve {
1402 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1404 my $dbh = C4::Context->dbh;
1405 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1407 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1409 my $sth = $dbh->prepare("
1412 WHERE borrowernumber = ?
1413 AND biblionumber = ?
1415 $sth->execute( $borrowernumber, $biblionumber );
1416 my $reserve = $sth->fetchrow_hashref;
1417 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1419 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1421 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1423 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1424 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1425 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1426 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1428 if ( $reserve->{'itemnumber'} ) {
1429 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1431 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1433 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1434 # aka, 'email' in ->{'transports'}
1435 C4::Letters::EnqueueLetter(
1436 { letter => $letter,
1437 borrowernumber => $borrowernumber,
1438 message_transport_type => 'email',
1439 from_address => $admin_email_address,
1444 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1445 C4::Letters::EnqueueLetter(
1446 { letter => $letter,
1447 borrowernumber => $borrowernumber,
1448 message_transport_type => 'sms',
1458 Koha Developement team <info@koha.org>