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
32 # for _koha_notify_reserve
33 use C4::Members::Messaging;
34 use C4::Members qw( GetMember );
36 use C4::Branch qw( GetBranchDetail );
37 use C4::Dates qw( format_date_in_iso );
38 use List::MoreUtils qw( firstidx );
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 F(inished) : the reserve has been completed, and is done
61 - itemnumber : empty : the reserve is still unaffected to an item
62 filled: the reserve is attached to an item
63 The complete workflow is :
64 ==== 1st use case ====
65 patron request a document, 1st available : P >0, F=NULL, I=NULL
66 a library having it run "transfertodo", and clic on the list
67 if there is no transfer to do, the reserve waiting
68 patron can pick it up P =0, F=W, I=filled
69 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
70 The pickup library recieve the book, it check in P =0, F=W, I=filled
71 The patron borrow the book P =0, F=F, I=filled
73 ==== 2nd use case ====
74 patron requests a document, a given item,
75 If pickup is holding branch P =0, F=W, I=filled
76 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
77 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
78 The patron borrow the book P =0, F=F, I=filled
87 # set the version for version checking
94 &GetReservesFromItemnumber
95 &GetReservesFromBiblionumber
96 &GetReservesFromBorrowernumber
110 &ModReserveMinusPriority
115 &IsAvailableForItemLevelRequest
121 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
127 $branch, $borrowernumber, $biblionumber,
128 $constraint, $bibitems, $priority, $resdate, $notes,
129 $title, $checkitem, $found
132 GetReserveFee($borrowernumber, $biblionumber, $constraint,
134 my $dbh = C4::Context->dbh;
135 my $const = lc substr( $constraint, 0, 1 );
136 $resdate = format_date_in_iso( $resdate ) if ( $resdate );
137 $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
138 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
139 # Make room in reserves for this before those of a later reserve date
140 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
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 );
167 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
168 priority,reservenotes,itemnumber,found,waitingdate)
173 my $sth = $dbh->prepare($query);
175 $borrowernumber, $biblionumber, $resdate, $branch,
176 $const, $priority, $notes, $checkitem,
181 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
183 INSERT INTO reserveconstraints
184 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
188 $sth = $dbh->prepare($query); # keep prepare outside the loop!
189 foreach (@$bibitems) {
190 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
192 return; # FIXME: why not have a useful return value?
195 =item GetReservesFromBiblionumber
197 ($count, $title_reserves) = &GetReserves($biblionumber);
199 This function gets the list of reservations for one C<$biblionumber>, returning a count
200 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
204 sub GetReservesFromBiblionumber {
205 my ($biblionumber) = shift or return (0, []);
206 my ($all_dates) = shift;
207 my $dbh = C4::Context->dbh;
209 # Find the desired items in the reserves
212 timestamp AS rtimestamp,
222 WHERE biblionumber = ? ";
223 unless ( $all_dates ) {
224 $query .= "AND reservedate <= CURRENT_DATE()";
226 $query .= "ORDER BY priority";
227 my $sth = $dbh->prepare($query);
228 $sth->execute($biblionumber);
231 while ( my $data = $sth->fetchrow_hashref ) {
233 # FIXME - What is this doing? How do constraints work?
234 if ($data->{constrainttype} eq 'o') {
236 SELECT biblioitemnumber
237 FROM reserveconstraints
238 WHERE biblionumber = ?
239 AND borrowernumber = ?
242 my $csth = $dbh->prepare($query);
243 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
245 while ( my $bibitemnos = $csth->fetchrow_array ) {
246 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
248 my $count = scalar @bibitemno;
250 # if we have two or more different specific itemtypes
251 # reserved by same person on same day
254 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
255 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
258 # Look up the book we just found.
259 $bdata = GetBiblioItemData( $bibitemno[0] );
261 # Add the results of this latest search to the current
263 # FIXME - An 'each' would probably be more efficient.
264 foreach my $key ( keys %$bdata ) {
265 $data->{$key} = $bdata->{$key};
268 push @results, $data;
270 return ( $#results + 1, \@results );
273 =item GetReservesFromItemnumber
275 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
277 TODO :: Description here
281 sub GetReservesFromItemnumber {
282 my ( $itemnumber, $all_dates ) = @_;
283 my $dbh = C4::Context->dbh;
285 SELECT reservedate,borrowernumber,branchcode
289 unless ( $all_dates ) {
290 $query .= " AND reservedate <= CURRENT_DATE()";
292 my $sth_res = $dbh->prepare($query);
293 $sth_res->execute($itemnumber);
294 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
295 return ( $reservedate, $borrowernumber, $branchcode );
298 =item GetReservesFromBorrowernumber
300 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
306 sub GetReservesFromBorrowernumber {
307 my ( $borrowernumber, $status ) = @_;
308 my $dbh = C4::Context->dbh;
311 $sth = $dbh->prepare("
314 WHERE borrowernumber=?
318 $sth->execute($borrowernumber,$status);
320 $sth = $dbh->prepare("
323 WHERE borrowernumber=?
326 $sth->execute($borrowernumber);
328 my $data = $sth->fetchall_arrayref({});
331 #-------------------------------------------------------------------------------------
333 =item GetReserveCount
335 $number = &GetReserveCount($borrowernumber);
337 this function returns the number of reservation for a borrower given on input arg.
341 sub GetReserveCount {
342 my ($borrowernumber) = @_;
344 my $dbh = C4::Context->dbh;
347 SELECT COUNT(*) AS counter
349 WHERE borrowernumber = ?
351 my $sth = $dbh->prepare($query);
352 $sth->execute($borrowernumber);
353 my $row = $sth->fetchrow_hashref;
354 return $row->{counter};
357 =item GetOtherReserves
359 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
361 Check queued list of this document and check if this document must be transfered
365 sub GetOtherReserves {
366 my ($itemnumber) = @_;
369 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
370 if ($checkreserves) {
371 my $iteminfo = GetItem($itemnumber);
372 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
373 $messages->{'transfert'} = $checkreserves->{'branchcode'};
374 #minus priorities of others reservs
375 ModReserveMinusPriority(
377 $checkreserves->{'borrowernumber'},
378 $iteminfo->{'biblionumber'}
381 #launch the subroutine dotransfer
382 C4::Items::ModItemTransfer(
384 $iteminfo->{'holdingbranch'},
385 $checkreserves->{'branchcode'}
390 #step 2b : case of a reservation on the same branch, set the waiting status
392 $messages->{'waiting'} = 1;
393 ModReserveMinusPriority(
395 $checkreserves->{'borrowernumber'},
396 $iteminfo->{'biblionumber'}
398 ModReserveStatus($itemnumber,'W');
401 $nextreservinfo = $checkreserves->{'borrowernumber'};
404 return ( $messages, $nextreservinfo );
409 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
411 Calculate the fee for a reserve
416 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
419 my $dbh = C4::Context->dbh;
420 my $const = lc substr( $constraint, 0, 1 );
422 SELECT * FROM borrowers
423 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
424 WHERE borrowernumber = ?
426 my $sth = $dbh->prepare($query);
427 $sth->execute($borrowernumber);
428 my $data = $sth->fetchrow_hashref;
430 my $fee = $data->{'reservefee'};
431 my $cntitems = @- > $bibitems;
435 # check for items on issue
436 # first find biblioitem records
438 my $sth1 = $dbh->prepare(
439 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
440 WHERE (biblio.biblionumber = ?)"
442 $sth1->execute($biblionumber);
443 while ( my $data1 = $sth1->fetchrow_hashref ) {
444 if ( $const eq "a" ) {
445 push @biblioitems, $data1;
450 while ( $x < $cntitems ) {
451 if ( @$bibitems->{'biblioitemnumber'} ==
452 $data->{'biblioitemnumber'} )
458 if ( $const eq 'o' ) {
460 push @biblioitems, $data1;
465 push @biblioitems, $data1;
471 my $cntitemsfound = @biblioitems;
475 while ( $x < $cntitemsfound ) {
476 my $bitdata = $biblioitems[$x];
477 my $sth2 = $dbh->prepare(
479 WHERE biblioitemnumber = ?"
481 $sth2->execute( $bitdata->{'biblioitemnumber'} );
482 while ( my $itdata = $sth2->fetchrow_hashref ) {
483 my $sth3 = $dbh->prepare(
484 "SELECT * FROM issues
485 WHERE itemnumber = ?"
487 $sth3->execute( $itdata->{'itemnumber'} );
488 if ( my $isdata = $sth3->fetchrow_hashref ) {
496 if ( $allissued == 0 ) {
498 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
499 $rsth->execute($biblionumber);
500 if ( my $rdata = $rsth->fetchrow_hashref ) {
510 =item GetReservesToBranch
512 @transreserv = GetReservesToBranch( $frombranch );
514 Get reserve list for a given branch
518 sub GetReservesToBranch {
519 my ( $frombranch ) = @_;
520 my $dbh = C4::Context->dbh;
521 my $sth = $dbh->prepare(
522 "SELECT borrowernumber,reservedate,itemnumber,timestamp
527 $sth->execute( $frombranch );
530 while ( my $data = $sth->fetchrow_hashref ) {
531 $transreserv[$i] = $data;
534 return (@transreserv);
537 =item GetReservesForBranch
539 @transreserv = GetReservesForBranch($frombranch);
543 sub GetReservesForBranch {
544 my ($frombranch) = @_;
545 my $dbh = C4::Context->dbh;
546 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
551 $query .= " AND branchcode=? ";
553 $query .= "ORDER BY waitingdate" ;
554 my $sth = $dbh->prepare($query);
556 $sth->execute($frombranch);
563 while ( my $data = $sth->fetchrow_hashref ) {
564 $transreserv[$i] = $data;
567 return (@transreserv);
572 ($status, $reserve) = &CheckReserves($itemnumber);
573 ($status, $reserve) = &CheckReserves(undef, $barcode);
575 Find a book in the reserves.
577 C<$itemnumber> is the book's item number.
579 As I understand it, C<&CheckReserves> looks for the given item in the
580 reserves. If it is found, that's a match, and C<$status> is set to
583 Otherwise, it finds the most important item in the reserves with the
584 same biblio number as this book (I'm not clear on this) and returns it
585 with C<$status> set to C<Reserved>.
587 C<&CheckReserves> returns a two-element list:
589 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
591 C<$reserve> is the reserve item that matched. It is a
592 reference-to-hash whose keys are mostly the fields of the reserves
593 table in the Koha database.
598 my ( $item, $barcode ) = @_;
599 my $dbh = C4::Context->dbh;
602 SELECT items.biblionumber,
603 items.biblioitemnumber,
604 itemtypes.notforloan,
605 items.notforloan AS itemnotforloan,
608 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
609 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
613 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
614 $sth->execute($item);
617 $sth = $dbh->prepare("$select WHERE barcode = ?");
618 $sth->execute($barcode);
620 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
621 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber ) = $sth->fetchrow_array;
623 return ( 0, 0 ) unless $itemnumber; # bail if we got nothing.
625 # if item is not for loan it cannot be reserved either.....
626 # execpt where items.notforloan < 0 : This indicates the item is holdable.
627 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
629 # Find this item in the reserves
630 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber );
632 # $priority and $highest are used to find the most important item
633 # in the list returned by &_Findgroupreserve. (The lower $priority,
634 # the more important the item.)
635 # $highest is the most important item we've seen so far.
637 if (scalar @reserves) {
638 my $priority = 10000000;
639 foreach my $res (@reserves) {
640 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
641 return ( "Waiting", $res ); # Found it
643 # See if this item is more important than what we've got so far
644 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
645 $priority = $res->{'priority'};
652 # If we get this far, then no exact match was found.
653 # We return the most important (i.e. next) reservation.
655 $highest->{'itemnumber'} = $item;
656 return ( "Reserved", $highest );
665 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
669 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
670 cancel, but not both: if both are given, C<&CancelReserve> does
673 C<$borrowernumber> is the borrower number of the patron on whose
674 behalf the book was reserved.
676 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
677 priorities of the other people who are waiting on the book.
682 my ( $biblio, $item, $borr ) = @_;
683 my $dbh = C4::Context->dbh;
684 if ( $item and $borr ) {
685 # removing a waiting reserve record....
686 # update the database...
689 SET cancellationdate = now(),
693 AND borrowernumber = ?
695 my $sth = $dbh->prepare($query);
696 $sth->execute( $item, $borr );
699 INSERT INTO old_reserves
700 SELECT * FROM reserves
702 AND borrowernumber = ?
704 $sth = $dbh->prepare($query);
705 $sth->execute( $item, $borr );
709 AND borrowernumber = ?
711 $sth = $dbh->prepare($query);
712 $sth->execute( $item, $borr );
715 # removing a reserve record....
716 # get the prioritiy on this record....
719 SELECT priority FROM reserves
720 WHERE biblionumber = ?
721 AND borrowernumber = ?
722 AND cancellationdate IS NULL
723 AND itemnumber IS NULL
725 my $sth = $dbh->prepare($query);
726 $sth->execute( $biblio, $borr );
727 ($priority) = $sth->fetchrow_array;
731 SET cancellationdate = now(),
734 WHERE biblionumber = ?
735 AND borrowernumber = ?
738 # update the database, removing the record...
739 $sth = $dbh->prepare($query);
740 $sth->execute( $biblio, $borr );
744 INSERT INTO old_reserves
745 SELECT * FROM reserves
746 WHERE biblionumber = ?
747 AND borrowernumber = ?
749 $sth = $dbh->prepare($query);
750 $sth->execute( $biblio, $borr );
754 WHERE biblionumber = ?
755 AND borrowernumber = ?
757 $sth = $dbh->prepare($query);
758 $sth->execute( $biblio, $borr );
760 # now fix the priority on the others....
761 _FixPriority( $priority, $biblio );
769 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
773 Change a hold request's priority or cancel it.
775 C<$rank> specifies the effect of the change. If C<$rank>
776 is 'W' or 'n', nothing happens. This corresponds to leaving a
777 request alone when changing its priority in the holds queue
780 If C<$rank> is 'del', the hold request is cancelled.
782 If C<$rank> is an integer greater than zero, the priority of
783 the request is set to that value. Since priority != 0 means
784 that the item is not waiting on the hold shelf, setting the
785 priority to a non-zero value also sets the request's found
786 status and waiting date to NULL.
788 The optional C<$itemnumber> parameter is used only when
789 C<$rank> is a non-zero integer; if supplied, the itemnumber
790 of the hold request is set accordingly; if omitted, the itemnumber
793 FIXME: Note that the forgoing can have the effect of causing
794 item-level hold requests to turn into title-level requests. This
795 will be fixed once reserves has separate columns for requested
796 itemnumber and supplying itemnumber.
801 #subroutine to update a reserve
802 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
803 return if $rank eq "W";
804 return if $rank eq "n";
805 my $dbh = C4::Context->dbh;
806 if ( $rank eq "del" ) {
809 SET cancellationdate=now()
810 WHERE biblionumber = ?
811 AND borrowernumber = ?
813 my $sth = $dbh->prepare($query);
814 $sth->execute( $biblio, $borrower );
817 INSERT INTO old_reserves
820 WHERE biblionumber = ?
821 AND borrowernumber = ?
823 $sth = $dbh->prepare($query);
824 $sth->execute( $biblio, $borrower );
827 WHERE biblionumber = ?
828 AND borrowernumber = ?
830 $sth = $dbh->prepare($query);
831 $sth->execute( $biblio, $borrower );
834 elsif ($rank =~ /^\d+/ and $rank > 0) {
836 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
837 WHERE biblionumber = ?
838 AND borrowernumber = ?
840 my $sth = $dbh->prepare($query);
841 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
843 _FixPriority( $biblio, $borrower, $rank);
849 &ModReserveFill($reserve);
851 Fill a reserve. If I understand this correctly, this means that the
852 reserved book has been found and given to the patron who reserved it.
854 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
855 whose keys are fields from the reserves table in the Koha database.
861 my $dbh = C4::Context->dbh;
862 # fill in a reserve record....
863 my $biblionumber = $res->{'biblionumber'};
864 my $borrowernumber = $res->{'borrowernumber'};
865 my $resdate = $res->{'reservedate'};
867 # get the priority on this record....
869 my $query = "SELECT priority
871 WHERE biblionumber = ?
872 AND borrowernumber = ?
873 AND reservedate = ?";
874 my $sth = $dbh->prepare($query);
875 $sth->execute( $biblionumber, $borrowernumber, $resdate );
876 ($priority) = $sth->fetchrow_array;
879 # update the database...
880 $query = "UPDATE reserves
883 WHERE biblionumber = ?
885 AND borrowernumber = ?
887 $sth = $dbh->prepare($query);
888 $sth->execute( $biblionumber, $resdate, $borrowernumber );
891 # move to old_reserves
892 $query = "INSERT INTO old_reserves
893 SELECT * FROM reserves
894 WHERE biblionumber = ?
896 AND borrowernumber = ?
898 $sth = $dbh->prepare($query);
899 $sth->execute( $biblionumber, $resdate, $borrowernumber );
900 $query = "DELETE FROM reserves
901 WHERE biblionumber = ?
903 AND borrowernumber = ?
905 $sth = $dbh->prepare($query);
906 $sth->execute( $biblionumber, $resdate, $borrowernumber );
908 # now fix the priority on the others (if the priority wasn't
909 # already sorted!)....
910 unless ( $priority == 0 ) {
911 _FixPriority( $priority, $biblionumber );
915 =item ModReserveStatus
917 &ModReserveStatus($itemnumber, $newstatus);
919 Update the reserve status for the active (priority=0) reserve.
921 $itemnumber is the itemnumber the reserve is on
923 $newstatus is the new status.
927 sub ModReserveStatus {
929 #first : check if we have a reservation for this item .
930 my ($itemnumber, $newstatus) = @_;
931 my $dbh = C4::Context->dbh;
932 my $query = " UPDATE reserves
933 SET found=?,waitingdate = now()
938 my $sth_set = $dbh->prepare($query);
939 $sth_set->execute( $newstatus, $itemnumber );
941 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
942 CartToShelf( $itemnumber );
946 =item ModReserveAffect
948 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
950 This function affect an item and a status for a given reserve
951 The itemnumber parameter is used to find the biblionumber.
952 with the biblionumber & the borrowernumber, we can affect the itemnumber
953 to the correct reserve.
955 if $transferToDo is not set, then the status is set to "Waiting" as well.
956 otherwise, a transfer is on the way, and the end of the transfer will
957 take care of the waiting status
960 sub ModReserveAffect {
961 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
962 my $dbh = C4::Context->dbh;
964 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
965 # attached to $itemnumber
966 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
967 $sth->execute($itemnumber);
968 my ($biblionumber) = $sth->fetchrow;
970 # get request - need to find out if item is already
971 # waiting in order to not send duplicate hold filled notifications
972 my $request = GetReserveInfo($borrowernumber, $biblionumber);
973 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
975 # If we affect a reserve that has to be transfered, don't set to Waiting
982 WHERE borrowernumber = ?
987 # affect the reserve to Waiting as well.
994 WHERE borrowernumber = ?
998 $sth = $dbh->prepare($query);
999 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1000 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1002 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1003 CartToShelf( $itemnumber );
1009 =item ModReserveCancelAll
1011 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1013 function to cancel reserv,check other reserves, and transfer document if it's necessary
1017 sub ModReserveCancelAll {
1020 my ( $itemnumber, $borrowernumber ) = @_;
1022 #step 1 : cancel the reservation
1023 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1025 #step 2 launch the subroutine of the others reserves
1026 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1028 return ( $messages, $nextreservinfo );
1031 =item ModReserveMinusPriority
1033 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1035 Reduce the values of queuded list
1039 sub ModReserveMinusPriority {
1040 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1042 #first step update the value of the first person on reserv
1043 my $dbh = C4::Context->dbh;
1046 SET priority = 0 , itemnumber = ?
1047 WHERE borrowernumber=?
1050 my $sth_upd = $dbh->prepare($query);
1051 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1052 # second step update all others reservs
1053 _FixPriority($biblionumber, $borrowernumber, '0');
1056 =item GetReserveInfo
1058 &GetReserveInfo($borrowernumber,$biblionumber);
1060 Get item and borrower details for a current hold.
1061 Current implementation this query should have a single result.
1064 sub GetReserveInfo {
1065 my ( $borrowernumber, $biblionumber ) = @_;
1066 my $dbh = C4::Context->dbh;
1067 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1068 reserves.biblionumber, reserves.branchcode,
1069 notificationdate, reminderdate, priority, found,
1070 firstname, surname, phone,
1071 email, address, address2,
1072 cardnumber, city, zipcode,
1073 biblio.title, biblio.author,
1074 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1076 FROM reserves left join items
1077 ON items.itemnumber=reserves.itemnumber ,
1080 reserves.borrowernumber=? &&
1081 reserves.biblionumber=? &&
1082 reserves.borrowernumber=borrowers.borrowernumber &&
1083 reserves.biblionumber=biblio.biblionumber ";
1084 my $sth = $dbh->prepare($strsth);
1085 $sth->execute($borrowernumber,$biblionumber);
1087 my $data = $sth->fetchrow_hashref;
1092 =item IsAvailableForItemLevelRequest
1096 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1100 Checks whether a given item record is available for an
1101 item-level hold request. An item is available if
1103 * it is not lost AND
1104 * it is not damaged AND
1105 * it is not withdrawn AND
1106 * does not have a not for loan value > 0
1108 Whether or not the item is currently on loan is
1109 also checked - if the AllowOnShelfHolds system preference
1110 is ON, an item can be requested even if it is currently
1111 on loan to somebody else. If the system preference
1112 is OFF, an item that is currently checked out cannot
1113 be the target of an item-level hold request.
1115 Note that IsAvailableForItemLevelRequest() does not
1116 check if the staff operator is authorized to place
1117 a request on the item - in particular,
1118 this routine does not check IndependantBranches
1119 and canreservefromotherbranches.
1123 sub IsAvailableForItemLevelRequest {
1124 my $itemnumber = shift;
1126 my $item = GetItem($itemnumber);
1128 # must check the notforloan setting of the itemtype
1129 # FIXME - a lot of places in the code do this
1130 # or something similar - need to be
1132 my $dbh = C4::Context->dbh;
1133 my $notforloan_query;
1134 if (C4::Context->preference('item-level_itypes')) {
1135 $notforloan_query = "SELECT itemtypes.notforloan
1137 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1138 WHERE itemnumber = ?";
1140 $notforloan_query = "SELECT itemtypes.notforloan
1142 JOIN biblioitems USING (biblioitemnumber)
1143 JOIN itemtypes USING (itemtype)
1144 WHERE itemnumber = ?";
1146 my $sth = $dbh->prepare($notforloan_query);
1147 $sth->execute($itemnumber);
1148 my $notforloan_per_itemtype = 0;
1149 if (my ($notforloan) = $sth->fetchrow_array) {
1150 $notforloan_per_itemtype = 1 if $notforloan;
1153 my $available_per_item = 1;
1154 $available_per_item = 0 if $item->{itemlost} or
1155 ( $item->{notforloan} > 0 ) or
1156 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1157 $item->{wthdrawn} or
1158 $notforloan_per_itemtype;
1161 if (C4::Context->preference('AllowOnShelfHolds')) {
1162 return $available_per_item;
1164 return ($available_per_item and $item->{onloan});
1170 &_FixPriority($biblio,$borrowernumber,$rank);
1172 Only used internally (so don't export it)
1173 Changed how this functions works #
1174 Now just gets an array of reserves in the rank order and updates them with
1175 the array index (+1 as array starts from 0)
1176 and if $rank is supplied will splice item from the array and splice it back in again
1177 in new priority rank
1182 my ( $biblio, $borrowernumber, $rank ) = @_;
1183 my $dbh = C4::Context->dbh;
1184 if ( $rank eq "del" ) {
1185 CancelReserve( $biblio, undef, $borrowernumber );
1187 if ( $rank eq "W" || $rank eq "0" ) {
1189 # make sure priority for waiting items is 0
1193 WHERE biblionumber = ?
1194 AND borrowernumber = ?
1197 my $sth = $dbh->prepare($query);
1198 $sth->execute( $biblio, $borrowernumber );
1204 # FIXME adding a new security in returned elements for changing priority,
1205 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1206 # This is wrong a waiting reserve has W set
1207 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1209 SELECT borrowernumber, reservedate, constrainttype
1211 WHERE biblionumber = ?
1212 AND ((found <> 'W') or found is NULL)
1213 ORDER BY priority ASC
1215 my $sth = $dbh->prepare($query);
1216 $sth->execute($biblio);
1217 while ( my $line = $sth->fetchrow_hashref ) {
1218 push( @reservedates, $line );
1219 push( @priority, $line );
1222 # To find the matching index
1224 my $key = -1; # to allow for 0 to be a valid result
1225 for ( $i = 0 ; $i < @priority ; $i++ ) {
1226 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1227 $key = $i; # save the index
1232 # if index exists in array then move it to new position
1233 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1234 my $new_rank = $rank -
1235 1; # $new_rank is what you want the new index to be in the array
1236 my $moving_item = splice( @priority, $key, 1 );
1237 splice( @priority, $new_rank, 0, $moving_item );
1240 # now fix the priority on those that are left....
1244 WHERE biblionumber = ?
1245 AND borrowernumber = ?
1249 $sth = $dbh->prepare($query);
1250 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1253 $priority[$j]->{'borrowernumber'},
1254 $priority[$j]->{'reservedate'}
1260 =item _Findgroupreserve
1262 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1264 Looks for an item-specific match first, then for a title-level match, returning the
1265 first match found. If neither, then we look for a 3rd kind of match based on
1266 reserve constraints.
1268 TODO: add more explanation about reserve constraints
1270 C<&_Findgroupreserve> returns :
1271 C<@results> is an array of references-to-hash whose keys are mostly
1272 fields from the reserves table of the Koha database, plus
1273 C<biblioitemnumber>.
1277 sub _Findgroupreserve {
1278 my ( $bibitem, $biblio, $itemnumber ) = @_;
1279 my $dbh = C4::Context->dbh;
1281 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1282 # check for exact targetted match
1283 my $item_level_target_query = qq/
1284 SELECT reserves.biblionumber AS biblionumber,
1285 reserves.borrowernumber AS borrowernumber,
1286 reserves.reservedate AS reservedate,
1287 reserves.branchcode AS branchcode,
1288 reserves.cancellationdate AS cancellationdate,
1289 reserves.found AS found,
1290 reserves.reservenotes AS reservenotes,
1291 reserves.priority AS priority,
1292 reserves.timestamp AS timestamp,
1293 biblioitems.biblioitemnumber AS biblioitemnumber,
1294 reserves.itemnumber AS itemnumber
1296 JOIN biblioitems USING (biblionumber)
1297 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1300 AND item_level_request = 1
1302 AND reservedate <= CURRENT_DATE()
1304 my $sth = $dbh->prepare($item_level_target_query);
1305 $sth->execute($itemnumber);
1307 if ( my $data = $sth->fetchrow_hashref ) {
1308 push( @results, $data );
1310 return @results if @results;
1312 # check for title-level targetted match
1313 my $title_level_target_query = qq/
1314 SELECT reserves.biblionumber AS biblionumber,
1315 reserves.borrowernumber AS borrowernumber,
1316 reserves.reservedate AS reservedate,
1317 reserves.branchcode AS branchcode,
1318 reserves.cancellationdate AS cancellationdate,
1319 reserves.found AS found,
1320 reserves.reservenotes AS reservenotes,
1321 reserves.priority AS priority,
1322 reserves.timestamp AS timestamp,
1323 biblioitems.biblioitemnumber AS biblioitemnumber,
1324 reserves.itemnumber AS itemnumber
1326 JOIN biblioitems USING (biblionumber)
1327 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1330 AND item_level_request = 0
1331 AND hold_fill_targets.itemnumber = ?
1332 AND reservedate <= CURRENT_DATE()
1334 $sth = $dbh->prepare($title_level_target_query);
1335 $sth->execute($itemnumber);
1337 if ( my $data = $sth->fetchrow_hashref ) {
1338 push( @results, $data );
1340 return @results if @results;
1343 SELECT reserves.biblionumber AS biblionumber,
1344 reserves.borrowernumber AS borrowernumber,
1345 reserves.reservedate AS reservedate,
1346 reserves.branchcode AS branchcode,
1347 reserves.cancellationdate AS cancellationdate,
1348 reserves.found AS found,
1349 reserves.reservenotes AS reservenotes,
1350 reserves.priority AS priority,
1351 reserves.timestamp AS timestamp,
1352 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1353 reserves.itemnumber AS itemnumber
1355 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1356 WHERE reserves.biblionumber = ?
1357 AND ( ( reserveconstraints.biblioitemnumber = ?
1358 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1359 AND reserves.reservedate = reserveconstraints.reservedate )
1360 OR reserves.constrainttype='a' )
1361 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1362 AND reserves.reservedate <= CURRENT_DATE()
1364 $sth = $dbh->prepare($query);
1365 $sth->execute( $biblio, $bibitem, $itemnumber );
1367 while ( my $data = $sth->fetchrow_hashref ) {
1368 push( @results, $data );
1373 =item _koha_notify_reserve
1377 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1381 Sends a notification to the patron that their hold has been filled (through
1382 ModReserveAffect, _not_ ModReserveFill)
1386 sub _koha_notify_reserve {
1387 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1389 my $dbh = C4::Context->dbh;
1390 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1392 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1394 my $sth = $dbh->prepare("
1397 WHERE borrowernumber = ?
1398 AND biblionumber = ?
1400 $sth->execute( $borrowernumber, $biblionumber );
1401 my $reserve = $sth->fetchrow_hashref;
1402 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1404 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1406 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1408 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1409 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1410 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1411 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1413 if ( $reserve->{'itemnumber'} ) {
1414 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1416 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1418 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1419 # aka, 'email' in ->{'transports'}
1420 C4::Letters::EnqueueLetter(
1421 { letter => $letter,
1422 borrowernumber => $borrowernumber,
1423 message_transport_type => 'email',
1424 from_address => $admin_email_address,
1429 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1430 C4::Letters::EnqueueLetter(
1431 { letter => $letter,
1432 borrowernumber => $borrowernumber,
1433 message_transport_type => 'sms',
1439 =item _ShiftPriorityByDateAndPriority
1443 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1447 This increments the priority of all reserves after the one
1448 with either the lowest date after C<$reservedate>
1449 or the lowest priority after C<$priority>.
1451 It effectively makes room for a new reserve to be inserted with a certain
1452 priority, which is returned.
1454 This is most useful when the reservedate can be set by the user. It allows
1455 the new reserve to be placed before other reserves that have a later
1456 reservedate. Since priority also is set by the form in reserves/request.pl
1457 the sub accounts for that too.
1461 sub _ShiftPriorityByDateAndPriority {
1462 my ( $biblio, $resdate, $new_priority ) = @_;
1464 my $dbh = C4::Context->dbh;
1465 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC";
1466 my $sth = $dbh->prepare( $query );
1467 $sth->execute( $biblio, $resdate, $new_priority );
1468 my ( $min_priority ) = $sth->fetchrow;
1469 $sth->finish; # $sth might have more data.
1470 $new_priority = $min_priority if ( $min_priority );
1471 my $updated_priority = $new_priority + 1;
1476 WHERE biblionumber = ?
1477 AND borrowernumber = ?
1480 my $sth_update = $dbh->prepare( $query );
1482 $query = "SELECT * FROM reserves WHERE priority >= ?";
1483 $sth = $dbh->prepare( $query );
1484 $sth->execute( $new_priority );
1485 while ( my $row = $sth->fetchrow_hashref ) {
1486 $sth_update->execute( $updated_priority, $biblio, $row->{borrowernumber}, $row->{reservedate} );
1487 $updated_priority++;
1490 return $new_priority; # so the caller knows what priority they end up at
1497 Koha Developement team <info@koha.org>