3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007-2010 BibLibre Paul POULAIN
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #use warnings; FIXME - Bug 2505
33 # for _koha_notify_reserve
34 use C4::Members::Messaging;
37 use C4::Branch qw( GetBranchDetail );
38 use C4::Dates qw( format_date_in_iso );
39 use List::MoreUtils qw( firstidx );
41 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
45 C4::Reserves - Koha functions for dealing with reservation.
53 This modules provides somes functions to deal with reservations.
55 Reserves are stored in reserves table.
56 The following columns contains important values :
57 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
58 =0 : then the reserve is being dealed
59 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
60 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
61 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
62 F(inished) : the reserve has been completed, and is done
63 - itemnumber : empty : the reserve is still unaffected to an item
64 filled: the reserve is attached to an item
65 The complete workflow is :
66 ==== 1st use case ====
67 patron request a document, 1st available : P >0, F=NULL, I=NULL
68 a library having it run "transfertodo", and clic on the list
69 if there is no transfer to do, the reserve waiting
70 patron can pick it up P =0, F=W, I=filled
71 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
72 The pickup library recieve the book, it check in P =0, F=W, I=filled
73 The patron borrow the book P =0, F=F, I=filled
75 ==== 2nd use case ====
76 patron requests a document, a given item,
77 If pickup is holding branch P =0, F=W, I=filled
78 If transfer needed, write in branchtransfer P =0, F=T, I=filled
79 The pickup library receive the book, it checks it in P =0, F=W, I=filled
80 The patron borrow the book P =0, F=F, I=filled
87 # set the version for version checking
94 &GetReservesFromItemnumber
95 &GetReservesFromBiblionumber
96 &GetReservesFromBorrowernumber
111 &ModReserveMinusPriority
118 &CancelExpiredReserves
120 &IsAvailableForItemLevelRequest
123 &ToggleLowestPriority
127 @EXPORT_OK = qw( MergeHolds );
132 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
138 $branch, $borrowernumber, $biblionumber,
139 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
140 $title, $checkitem, $found
143 GetReserveFee($borrowernumber, $biblionumber, $constraint,
145 my $dbh = C4::Context->dbh;
146 my $const = lc substr( $constraint, 0, 1 );
147 $resdate = format_date_in_iso( $resdate ) if ( $resdate );
148 $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
150 $expdate = format_date_in_iso( $expdate );
152 undef $expdate; # make reserves.expirationdate default to null rather than '0000-00-00'
154 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
155 # Make room in reserves for this before those of a later reserve date
156 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
160 # If the reserv had the waiting status, we had the value of the resdate
161 if ( $found eq 'W' ) {
162 $waitingdate = $resdate;
166 # updates take place here
168 my $nextacctno = &getnextacctno( $borrowernumber );
170 INSERT INTO accountlines
171 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
173 (?,?,now(),?,?,'Res',?)
175 my $usth = $dbh->prepare($query);
176 $usth->execute( $borrowernumber, $nextacctno, $fee,
177 "Reserve Charge - $title", $fee );
183 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
184 priority,reservenotes,itemnumber,found,waitingdate,expirationdate)
189 my $sth = $dbh->prepare($query);
191 $borrowernumber, $biblionumber, $resdate, $branch,
192 $const, $priority, $notes, $checkitem,
193 $found, $waitingdate, $expdate
196 # Send e-mail to librarian if syspref is active
197 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
198 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
199 my $branch_details = C4::Branch::GetBranchDetail($borrower->{branchcode});
200 if ( my $letter = C4::Letters::GetPreparedLetter (
201 module => 'reserves',
202 letter_code => 'HOLDPLACED',
203 branchcode => $branch,
205 'branches' => $branch_details,
206 'borrowers' => $borrower,
207 'biblio' => $biblionumber,
211 my $admin_email_address =$branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
213 C4::Letters::EnqueueLetter(
215 borrowernumber => $borrowernumber,
216 message_transport_type => 'email',
217 from_address => $admin_email_address,
218 to_address => $admin_email_address,
225 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
227 INSERT INTO reserveconstraints
228 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
232 $sth = $dbh->prepare($query); # keep prepare outside the loop!
233 foreach (@$bibitems) {
234 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
237 return; # FIXME: why not have a useful return value?
240 =head2 GetReservesFromBiblionumber
242 ($count, $title_reserves) = &GetReserves($biblionumber);
244 This function gets the list of reservations for one C<$biblionumber>, returning a count
245 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
249 sub GetReservesFromBiblionumber {
250 my ($biblionumber) = shift or return (0, []);
251 my ($all_dates) = shift;
252 my $dbh = C4::Context->dbh;
254 # Find the desired items in the reserves
257 timestamp AS rtimestamp,
269 WHERE biblionumber = ? ";
270 unless ( $all_dates ) {
271 $query .= "AND reservedate <= CURRENT_DATE()";
273 $query .= "ORDER BY priority";
274 my $sth = $dbh->prepare($query);
275 $sth->execute($biblionumber);
278 while ( my $data = $sth->fetchrow_hashref ) {
280 # FIXME - What is this doing? How do constraints work?
281 if ($data->{constrainttype} eq 'o') {
283 SELECT biblioitemnumber
284 FROM reserveconstraints
285 WHERE biblionumber = ?
286 AND borrowernumber = ?
289 my $csth = $dbh->prepare($query);
290 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
292 while ( my $bibitemnos = $csth->fetchrow_array ) {
293 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
295 my $count = scalar @bibitemno;
297 # if we have two or more different specific itemtypes
298 # reserved by same person on same day
301 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
302 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
305 # Look up the book we just found.
306 $bdata = GetBiblioItemData( $bibitemno[0] );
308 # Add the results of this latest search to the current
310 # FIXME - An 'each' would probably be more efficient.
311 foreach my $key ( keys %$bdata ) {
312 $data->{$key} = $bdata->{$key};
315 push @results, $data;
317 return ( $#results + 1, \@results );
320 =head2 GetReservesFromItemnumber
322 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
324 TODO :: Description here
328 sub GetReservesFromItemnumber {
329 my ( $itemnumber, $all_dates ) = @_;
330 my $dbh = C4::Context->dbh;
332 SELECT reservedate,borrowernumber,branchcode
336 unless ( $all_dates ) {
337 $query .= " AND reservedate <= CURRENT_DATE()";
339 my $sth_res = $dbh->prepare($query);
340 $sth_res->execute($itemnumber);
341 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
342 return ( $reservedate, $borrowernumber, $branchcode );
345 =head2 GetReservesFromBorrowernumber
347 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
353 sub GetReservesFromBorrowernumber {
354 my ( $borrowernumber, $status ) = @_;
355 my $dbh = C4::Context->dbh;
358 $sth = $dbh->prepare("
361 WHERE borrowernumber=?
365 $sth->execute($borrowernumber,$status);
367 $sth = $dbh->prepare("
370 WHERE borrowernumber=?
373 $sth->execute($borrowernumber);
375 my $data = $sth->fetchall_arrayref({});
378 #-------------------------------------------------------------------------------------
379 =head2 CanBookBeReserved
381 $error = &CanBookBeReserved($borrowernumber, $biblionumber)
385 sub CanBookBeReserved{
386 my ($borrowernumber, $biblionumber) = @_;
388 my @items = get_itemnumbers_of($biblionumber);
389 #get items linked via host records
390 my @hostitems = get_hostitemnumbers_of($biblionumber);
392 push (@items,@hostitems);
395 foreach my $item (@items){
396 return 1 if CanItemBeReserved($borrowernumber, $item);
401 =head2 CanItemBeReserved
403 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
405 This function return 1 if an item can be issued by this borrower.
409 sub CanItemBeReserved{
410 my ($borrowernumber, $itemnumber) = @_;
412 my $dbh = C4::Context->dbh;
413 my $allowedreserves = 0;
415 my $controlbranch = C4::Context->preference('ReservesControlBranch');
416 my $itype = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
418 # we retrieve borrowers and items informations #
419 my $item = GetItem($itemnumber);
420 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
422 # we retrieve user rights on this itemtype and branchcode
423 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
425 WHERE (categorycode in (?,'*') )
426 AND (itemtype IN (?,'*'))
427 AND (branchcode IN (?,'*'))
434 my $querycount ="SELECT
437 LEFT JOIN items USING (itemnumber)
438 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
439 LEFT JOIN borrowers USING (borrowernumber)
440 WHERE borrowernumber = ?
444 my $itemtype = $item->{$itype};
445 my $categorycode = $borrower->{categorycode};
447 my $branchfield = "reserves.branchcode";
449 if( $controlbranch eq "ItemHomeLibrary" ){
450 $branchfield = "items.homebranch";
451 $branchcode = $item->{homebranch};
452 }elsif( $controlbranch eq "PatronLibrary" ){
453 $branchfield = "borrowers.branchcode";
454 $branchcode = $borrower->{branchcode};
458 $sth->execute($categorycode, $itemtype, $branchcode);
459 if(my $rights = $sth->fetchrow_hashref()){
460 $itemtype = $rights->{itemtype};
461 $allowedreserves = $rights->{reservesallowed};
468 $querycount .= "AND $branchfield = ?";
470 $querycount .= " AND $itype = ?" if ($itemtype ne "*");
471 my $sthcount = $dbh->prepare($querycount);
473 if($itemtype eq "*"){
474 $sthcount->execute($borrowernumber, $branchcode);
476 $sthcount->execute($borrowernumber, $branchcode, $itemtype);
479 my $reservecount = "0";
480 if(my $rowcount = $sthcount->fetchrow_hashref()){
481 $reservecount = $rowcount->{count};
484 # we check if it's ok or not
485 if( $reservecount < $allowedreserves ){
491 #--------------------------------------------------------------------------------
492 =head2 GetReserveCount
494 $number = &GetReserveCount($borrowernumber);
496 this function returns the number of reservation for a borrower given on input arg.
500 sub GetReserveCount {
501 my ($borrowernumber) = @_;
503 my $dbh = C4::Context->dbh;
506 SELECT COUNT(*) AS counter
508 WHERE borrowernumber = ?
510 my $sth = $dbh->prepare($query);
511 $sth->execute($borrowernumber);
512 my $row = $sth->fetchrow_hashref;
513 return $row->{counter};
516 =head2 GetOtherReserves
518 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
520 Check queued list of this document and check if this document must be transfered
524 sub GetOtherReserves {
525 my ($itemnumber) = @_;
528 my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
529 if ($checkreserves) {
530 my $iteminfo = GetItem($itemnumber);
531 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
532 $messages->{'transfert'} = $checkreserves->{'branchcode'};
533 #minus priorities of others reservs
534 ModReserveMinusPriority(
536 $checkreserves->{'borrowernumber'},
537 $iteminfo->{'biblionumber'}
540 #launch the subroutine dotransfer
541 C4::Items::ModItemTransfer(
543 $iteminfo->{'holdingbranch'},
544 $checkreserves->{'branchcode'}
549 #step 2b : case of a reservation on the same branch, set the waiting status
551 $messages->{'waiting'} = 1;
552 ModReserveMinusPriority(
554 $checkreserves->{'borrowernumber'},
555 $iteminfo->{'biblionumber'}
557 ModReserveStatus($itemnumber,'W');
560 $nextreservinfo = $checkreserves->{'borrowernumber'};
563 return ( $messages, $nextreservinfo );
568 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
570 Calculate the fee for a reserve
575 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
578 my $dbh = C4::Context->dbh;
579 my $const = lc substr( $constraint, 0, 1 );
581 SELECT * FROM borrowers
582 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
583 WHERE borrowernumber = ?
585 my $sth = $dbh->prepare($query);
586 $sth->execute($borrowernumber);
587 my $data = $sth->fetchrow_hashref;
589 my $fee = $data->{'reservefee'};
590 my $cntitems = @- > $bibitems;
594 # check for items on issue
595 # first find biblioitem records
597 my $sth1 = $dbh->prepare(
598 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
599 WHERE (biblio.biblionumber = ?)"
601 $sth1->execute($biblionumber);
602 while ( my $data1 = $sth1->fetchrow_hashref ) {
603 if ( $const eq "a" ) {
604 push @biblioitems, $data1;
609 while ( $x < $cntitems ) {
610 if ( @$bibitems->{'biblioitemnumber'} ==
611 $data->{'biblioitemnumber'} )
617 if ( $const eq 'o' ) {
619 push @biblioitems, $data1;
624 push @biblioitems, $data1;
630 my $cntitemsfound = @biblioitems;
634 while ( $x < $cntitemsfound ) {
635 my $bitdata = $biblioitems[$x];
636 my $sth2 = $dbh->prepare(
638 WHERE biblioitemnumber = ?"
640 $sth2->execute( $bitdata->{'biblioitemnumber'} );
641 while ( my $itdata = $sth2->fetchrow_hashref ) {
642 my $sth3 = $dbh->prepare(
643 "SELECT * FROM issues
644 WHERE itemnumber = ?"
646 $sth3->execute( $itdata->{'itemnumber'} );
647 if ( my $isdata = $sth3->fetchrow_hashref ) {
655 if ( $allissued == 0 ) {
657 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
658 $rsth->execute($biblionumber);
659 if ( my $rdata = $rsth->fetchrow_hashref ) {
669 =head2 GetReservesToBranch
671 @transreserv = GetReservesToBranch( $frombranch );
673 Get reserve list for a given branch
677 sub GetReservesToBranch {
678 my ( $frombranch ) = @_;
679 my $dbh = C4::Context->dbh;
680 my $sth = $dbh->prepare(
681 "SELECT borrowernumber,reservedate,itemnumber,timestamp
686 $sth->execute( $frombranch );
689 while ( my $data = $sth->fetchrow_hashref ) {
690 $transreserv[$i] = $data;
693 return (@transreserv);
696 =head2 GetReservesForBranch
698 @transreserv = GetReservesForBranch($frombranch);
702 sub GetReservesForBranch {
703 my ($frombranch) = @_;
704 my $dbh = C4::Context->dbh;
705 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
710 $query .= " AND branchcode=? ";
712 $query .= "ORDER BY waitingdate" ;
713 my $sth = $dbh->prepare($query);
715 $sth->execute($frombranch);
722 while ( my $data = $sth->fetchrow_hashref ) {
723 $transreserv[$i] = $data;
726 return (@transreserv);
729 sub GetReserveStatus {
730 my ($itemnumber) = @_;
732 my $dbh = C4::Context->dbh;
734 my $itemstatus = $dbh->prepare("SELECT found FROM reserves WHERE itemnumber = ?");
736 $itemstatus->execute($itemnumber);
737 my ($found) = $itemstatus->fetchrow_array;
743 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
744 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
746 Find a book in the reserves.
748 C<$itemnumber> is the book's item number.
750 As I understand it, C<&CheckReserves> looks for the given item in the
751 reserves. If it is found, that's a match, and C<$status> is set to
754 Otherwise, it finds the most important item in the reserves with the
755 same biblio number as this book (I'm not clear on this) and returns it
756 with C<$status> set to C<Reserved>.
758 C<&CheckReserves> returns a two-element list:
760 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
762 C<$reserve> is the reserve item that matched. It is a
763 reference-to-hash whose keys are mostly the fields of the reserves
764 table in the Koha database.
769 my ( $item, $barcode ) = @_;
770 my $dbh = C4::Context->dbh;
773 if (C4::Context->preference('item-level_itypes')){
775 SELECT items.biblionumber,
776 items.biblioitemnumber,
777 itemtypes.notforloan,
778 items.notforloan AS itemnotforloan,
781 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
782 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
787 SELECT items.biblionumber,
788 items.biblioitemnumber,
789 itemtypes.notforloan,
790 items.notforloan AS itemnotforloan,
793 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
794 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
799 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
800 $sth->execute($item);
803 $sth = $dbh->prepare("$select WHERE barcode = ?");
804 $sth->execute($barcode);
806 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
807 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber ) = $sth->fetchrow_array;
809 return ( '' ) unless $itemnumber; # bail if we got nothing.
811 # if item is not for loan it cannot be reserved either.....
812 # execpt where items.notforloan < 0 : This indicates the item is holdable.
813 return ( '' ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
815 # Find this item in the reserves
816 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber );
818 # $priority and $highest are used to find the most important item
819 # in the list returned by &_Findgroupreserve. (The lower $priority,
820 # the more important the item.)
821 # $highest is the most important item we've seen so far.
823 if (scalar @reserves) {
824 my $priority = 10000000;
825 foreach my $res (@reserves) {
826 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
827 return ( "Waiting", $res, \@reserves ); # Found it
829 # See if this item is more important than what we've got so far
830 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
831 my $borrowerinfo=C4::Members::GetMember(borrowernumber => $res->{'borrowernumber'});
832 my $iteminfo=C4::Items::GetItem($itemnumber);
833 my $branch=C4::Circulation::_GetCircControlBranch($iteminfo,$borrowerinfo);
834 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
835 next if ($branchitemrule->{'holdallowed'} == 0);
836 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
837 $priority = $res->{'priority'};
844 # If we get this far, then no exact match was found.
845 # We return the most important (i.e. next) reservation.
847 $highest->{'itemnumber'} = $item;
848 return ( "Reserved", $highest, \@reserves );
854 =head2 CancelExpiredReserves
856 CancelExpiredReserves();
858 Cancels all reserves with an expiration date from before today.
862 sub CancelExpiredReserves {
864 # Cancel reserves that have passed their expiration date.
865 my $dbh = C4::Context->dbh;
866 my $sth = $dbh->prepare( "
867 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
868 AND expirationdate IS NOT NULL
872 while ( my $res = $sth->fetchrow_hashref() ) {
873 CancelReserve( $res->{'biblionumber'}, '', $res->{'borrowernumber'} );
876 # Cancel reserves that have been waiting too long
877 if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
878 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
879 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
881 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
882 $sth = $dbh->prepare( $query );
883 $sth->execute( $max_pickup_delay );
885 while (my $res = $sth->fetchrow_hashref ) {
887 manualinvoice($res->{'borrowernumber'}, $res->{'itemnumber'}, 'Hold waiting too long', 'F', $charge);
890 CancelReserve( $res->{'biblionumber'}, '', $res->{'borrowernumber'} );
898 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
902 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
903 cancel, but not both: if both are given, C<&CancelReserve> does
906 C<$borrowernumber> is the borrower number of the patron on whose
907 behalf the book was reserved.
909 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
910 priorities of the other people who are waiting on the book.
915 my ( $biblio, $item, $borr ) = @_;
916 my $dbh = C4::Context->dbh;
917 if ( $item and $borr ) {
918 # removing a waiting reserve record....
919 # update the database...
922 SET cancellationdate = now(),
926 AND borrowernumber = ?
928 my $sth = $dbh->prepare($query);
929 $sth->execute( $item, $borr );
932 INSERT INTO old_reserves
933 SELECT * FROM reserves
935 AND borrowernumber = ?
937 $sth = $dbh->prepare($query);
938 $sth->execute( $item, $borr );
942 AND borrowernumber = ?
944 $sth = $dbh->prepare($query);
945 $sth->execute( $item, $borr );
948 # removing a reserve record....
949 # get the prioritiy on this record....
952 SELECT priority FROM reserves
953 WHERE biblionumber = ?
954 AND borrowernumber = ?
955 AND cancellationdate IS NULL
956 AND itemnumber IS NULL
958 my $sth = $dbh->prepare($query);
959 $sth->execute( $biblio, $borr );
960 ($priority) = $sth->fetchrow_array;
964 SET cancellationdate = now(),
967 WHERE biblionumber = ?
968 AND borrowernumber = ?
971 # update the database, removing the record...
972 $sth = $dbh->prepare($query);
973 $sth->execute( $biblio, $borr );
977 INSERT INTO old_reserves
978 SELECT * FROM reserves
979 WHERE biblionumber = ?
980 AND borrowernumber = ?
982 $sth = $dbh->prepare($query);
983 $sth->execute( $biblio, $borr );
987 WHERE biblionumber = ?
988 AND borrowernumber = ?
990 $sth = $dbh->prepare($query);
991 $sth->execute( $biblio, $borr );
993 # now fix the priority on the others....
994 _FixPriority( $biblio, $borr );
1000 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
1002 Change a hold request's priority or cancel it.
1004 C<$rank> specifies the effect of the change. If C<$rank>
1005 is 'W' or 'n', nothing happens. This corresponds to leaving a
1006 request alone when changing its priority in the holds queue
1009 If C<$rank> is 'del', the hold request is cancelled.
1011 If C<$rank> is an integer greater than zero, the priority of
1012 the request is set to that value. Since priority != 0 means
1013 that the item is not waiting on the hold shelf, setting the
1014 priority to a non-zero value also sets the request's found
1015 status and waiting date to NULL.
1017 The optional C<$itemnumber> parameter is used only when
1018 C<$rank> is a non-zero integer; if supplied, the itemnumber
1019 of the hold request is set accordingly; if omitted, the itemnumber
1022 B<FIXME:> Note that the forgoing can have the effect of causing
1023 item-level hold requests to turn into title-level requests. This
1024 will be fixed once reserves has separate columns for requested
1025 itemnumber and supplying itemnumber.
1030 #subroutine to update a reserve
1031 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
1032 return if $rank eq "W";
1033 return if $rank eq "n";
1034 my $dbh = C4::Context->dbh;
1035 if ( $rank eq "del" ) {
1038 SET cancellationdate=now()
1039 WHERE biblionumber = ?
1040 AND borrowernumber = ?
1042 my $sth = $dbh->prepare($query);
1043 $sth->execute( $biblio, $borrower );
1046 INSERT INTO old_reserves
1049 WHERE biblionumber = ?
1050 AND borrowernumber = ?
1052 $sth = $dbh->prepare($query);
1053 $sth->execute( $biblio, $borrower );
1055 DELETE FROM reserves
1056 WHERE biblionumber = ?
1057 AND borrowernumber = ?
1059 $sth = $dbh->prepare($query);
1060 $sth->execute( $biblio, $borrower );
1063 elsif ($rank =~ /^\d+/ and $rank > 0) {
1065 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1066 WHERE biblionumber = ?
1067 AND borrowernumber = ?
1069 my $sth = $dbh->prepare($query);
1070 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
1072 _FixPriority( $biblio, $borrower, $rank);
1076 =head2 ModReserveFill
1078 &ModReserveFill($reserve);
1080 Fill a reserve. If I understand this correctly, this means that the
1081 reserved book has been found and given to the patron who reserved it.
1083 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1084 whose keys are fields from the reserves table in the Koha database.
1088 sub ModReserveFill {
1090 my $dbh = C4::Context->dbh;
1091 # fill in a reserve record....
1092 my $biblionumber = $res->{'biblionumber'};
1093 my $borrowernumber = $res->{'borrowernumber'};
1094 my $resdate = $res->{'reservedate'};
1096 # get the priority on this record....
1098 my $query = "SELECT priority
1100 WHERE biblionumber = ?
1101 AND borrowernumber = ?
1102 AND reservedate = ?";
1103 my $sth = $dbh->prepare($query);
1104 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1105 ($priority) = $sth->fetchrow_array;
1108 # update the database...
1109 $query = "UPDATE reserves
1112 WHERE biblionumber = ?
1114 AND borrowernumber = ?
1116 $sth = $dbh->prepare($query);
1117 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1120 # move to old_reserves
1121 $query = "INSERT INTO old_reserves
1122 SELECT * FROM reserves
1123 WHERE biblionumber = ?
1125 AND borrowernumber = ?
1127 $sth = $dbh->prepare($query);
1128 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1129 $query = "DELETE FROM reserves
1130 WHERE biblionumber = ?
1132 AND borrowernumber = ?
1134 $sth = $dbh->prepare($query);
1135 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1137 # now fix the priority on the others (if the priority wasn't
1138 # already sorted!)....
1139 unless ( $priority == 0 ) {
1140 _FixPriority( $biblionumber, $borrowernumber );
1144 =head2 ModReserveStatus
1146 &ModReserveStatus($itemnumber, $newstatus);
1148 Update the reserve status for the active (priority=0) reserve.
1150 $itemnumber is the itemnumber the reserve is on
1152 $newstatus is the new status.
1156 sub ModReserveStatus {
1158 #first : check if we have a reservation for this item .
1159 my ($itemnumber, $newstatus) = @_;
1160 my $dbh = C4::Context->dbh;
1162 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1163 my $sth_set = $dbh->prepare($query);
1164 $sth_set->execute( $newstatus, $itemnumber );
1166 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1167 CartToShelf( $itemnumber );
1171 =head2 ModReserveAffect
1173 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1175 This function affect an item and a status for a given reserve
1176 The itemnumber parameter is used to find the biblionumber.
1177 with the biblionumber & the borrowernumber, we can affect the itemnumber
1178 to the correct reserve.
1180 if $transferToDo is not set, then the status is set to "Waiting" as well.
1181 otherwise, a transfer is on the way, and the end of the transfer will
1182 take care of the waiting status
1186 sub ModReserveAffect {
1187 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1188 my $dbh = C4::Context->dbh;
1190 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1191 # attached to $itemnumber
1192 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1193 $sth->execute($itemnumber);
1194 my ($biblionumber) = $sth->fetchrow;
1196 # get request - need to find out if item is already
1197 # waiting in order to not send duplicate hold filled notifications
1198 my $request = GetReserveInfo($borrowernumber, $biblionumber);
1199 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1201 # If we affect a reserve that has to be transfered, don't set to Waiting
1203 if ($transferToDo) {
1209 WHERE borrowernumber = ?
1210 AND biblionumber = ?
1214 # affect the reserve to Waiting as well.
1219 waitingdate = NOW(),
1221 WHERE borrowernumber = ?
1222 AND biblionumber = ?
1225 $sth = $dbh->prepare($query);
1226 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1227 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1229 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1230 CartToShelf( $itemnumber );
1236 =head2 ModReserveCancelAll
1238 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1240 function to cancel reserv,check other reserves, and transfer document if it's necessary
1244 sub ModReserveCancelAll {
1247 my ( $itemnumber, $borrowernumber ) = @_;
1249 #step 1 : cancel the reservation
1250 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1252 #step 2 launch the subroutine of the others reserves
1253 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1255 return ( $messages, $nextreservinfo );
1258 =head2 ModReserveMinusPriority
1260 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1262 Reduce the values of queuded list
1266 sub ModReserveMinusPriority {
1267 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1269 #first step update the value of the first person on reserv
1270 my $dbh = C4::Context->dbh;
1273 SET priority = 0 , itemnumber = ?
1274 WHERE borrowernumber=?
1277 my $sth_upd = $dbh->prepare($query);
1278 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1279 # second step update all others reservs
1280 _FixPriority($biblionumber, $borrowernumber, '0');
1283 =head2 GetReserveInfo
1285 &GetReserveInfo($borrowernumber,$biblionumber);
1287 Get item and borrower details for a current hold.
1288 Current implementation this query should have a single result.
1292 sub GetReserveInfo {
1293 my ( $borrowernumber, $biblionumber ) = @_;
1294 my $dbh = C4::Context->dbh;
1298 reserves.borrowernumber,
1299 reserves.biblionumber,
1300 reserves.branchcode,
1301 reserves.waitingdate,
1317 items.holdingbranch,
1318 items.itemcallnumber,
1324 LEFT JOIN items USING(itemnumber)
1325 LEFT JOIN borrowers USING(borrowernumber)
1326 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1328 reserves.borrowernumber=?
1329 AND reserves.biblionumber=?";
1330 my $sth = $dbh->prepare($strsth);
1331 $sth->execute($borrowernumber,$biblionumber);
1333 my $data = $sth->fetchrow_hashref;
1338 =head2 IsAvailableForItemLevelRequest
1340 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1342 Checks whether a given item record is available for an
1343 item-level hold request. An item is available if
1345 * it is not lost AND
1346 * it is not damaged AND
1347 * it is not withdrawn AND
1348 * does not have a not for loan value > 0
1350 Whether or not the item is currently on loan is
1351 also checked - if the AllowOnShelfHolds system preference
1352 is ON, an item can be requested even if it is currently
1353 on loan to somebody else. If the system preference
1354 is OFF, an item that is currently checked out cannot
1355 be the target of an item-level hold request.
1357 Note that IsAvailableForItemLevelRequest() does not
1358 check if the staff operator is authorized to place
1359 a request on the item - in particular,
1360 this routine does not check IndependantBranches
1361 and canreservefromotherbranches.
1365 sub IsAvailableForItemLevelRequest {
1366 my $itemnumber = shift;
1368 my $item = GetItem($itemnumber);
1370 # must check the notforloan setting of the itemtype
1371 # FIXME - a lot of places in the code do this
1372 # or something similar - need to be
1374 my $dbh = C4::Context->dbh;
1375 my $notforloan_query;
1376 if (C4::Context->preference('item-level_itypes')) {
1377 $notforloan_query = "SELECT itemtypes.notforloan
1379 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1380 WHERE itemnumber = ?";
1382 $notforloan_query = "SELECT itemtypes.notforloan
1384 JOIN biblioitems USING (biblioitemnumber)
1385 JOIN itemtypes USING (itemtype)
1386 WHERE itemnumber = ?";
1388 my $sth = $dbh->prepare($notforloan_query);
1389 $sth->execute($itemnumber);
1390 my $notforloan_per_itemtype = 0;
1391 if (my ($notforloan) = $sth->fetchrow_array) {
1392 $notforloan_per_itemtype = 1 if $notforloan;
1395 my $available_per_item = 1;
1396 $available_per_item = 0 if $item->{itemlost} or
1397 ( $item->{notforloan} > 0 ) or
1398 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1399 $item->{wthdrawn} or
1400 $notforloan_per_itemtype;
1403 if (C4::Context->preference('AllowOnShelfHolds')) {
1404 return $available_per_item;
1406 return ($available_per_item and ($item->{onloan} or GetReserveStatus($itemnumber) eq "W"));
1410 =head2 AlterPriority
1412 AlterPriority( $where, $borrowernumber, $biblionumber, $reservedate );
1414 This function changes a reserve's priority up, down, to the top, or to the bottom.
1415 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1420 my ( $where, $borrowernumber, $biblionumber ) = @_;
1422 my $dbh = C4::Context->dbh;
1424 ## Find this reserve
1425 my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? AND cancellationdate IS NULL');
1426 $sth->execute( $biblionumber, $borrowernumber );
1427 my $reserve = $sth->fetchrow_hashref();
1430 if ( $where eq 'up' || $where eq 'down' ) {
1432 my $priority = $reserve->{'priority'};
1433 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1434 _FixPriority( $biblionumber, $borrowernumber, $priority )
1436 } elsif ( $where eq 'top' ) {
1438 _FixPriority( $biblionumber, $borrowernumber, '1' )
1440 } elsif ( $where eq 'bottom' ) {
1442 _FixPriority( $biblionumber, $borrowernumber, '999999' )
1447 =head2 ToggleLowestPriority
1449 ToggleLowestPriority( $borrowernumber, $biblionumber );
1451 This function sets the lowestPriority field to true if is false, and false if it is true.
1455 sub ToggleLowestPriority {
1456 my ( $borrowernumber, $biblionumber ) = @_;
1458 my $dbh = C4::Context->dbh;
1460 my $sth = $dbh->prepare(
1461 "UPDATE reserves SET lowestPriority = NOT lowestPriority
1462 WHERE biblionumber = ?
1463 AND borrowernumber = ?"
1471 _FixPriority( $biblionumber, $borrowernumber, '999999' );
1476 &_FixPriority($biblio,$borrowernumber,$rank,$ignoreSetLowestRank);
1478 Only used internally (so don't export it)
1479 Changed how this functions works #
1480 Now just gets an array of reserves in the rank order and updates them with
1481 the array index (+1 as array starts from 0)
1482 and if $rank is supplied will splice item from the array and splice it back in again
1483 in new priority rank
1488 my ( $biblio, $borrowernumber, $rank, $ignoreSetLowestRank ) = @_;
1489 my $dbh = C4::Context->dbh;
1490 if ( $rank eq "del" ) {
1491 CancelReserve( $biblio, undef, $borrowernumber );
1493 if ( $rank eq "W" || $rank eq "0" ) {
1495 # make sure priority for waiting or in-transit items is 0
1499 WHERE biblionumber = ?
1500 AND borrowernumber = ?
1501 AND found IN ('W', 'T')
1503 my $sth = $dbh->prepare($query);
1504 $sth->execute( $biblio, $borrowernumber );
1510 # FIXME adding a new security in returned elements for changing priority,
1511 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1512 # This is wrong a waiting reserve has W set
1513 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1515 SELECT borrowernumber, reservedate, constrainttype
1517 WHERE biblionumber = ?
1518 AND ((found <> 'W' AND found <> 'T') or found is NULL)
1519 ORDER BY priority ASC
1521 my $sth = $dbh->prepare($query);
1522 $sth->execute($biblio);
1523 while ( my $line = $sth->fetchrow_hashref ) {
1524 push( @reservedates, $line );
1525 push( @priority, $line );
1528 # To find the matching index
1530 my $key = -1; # to allow for 0 to be a valid result
1531 for ( $i = 0 ; $i < @priority ; $i++ ) {
1532 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1533 $key = $i; # save the index
1538 # if index exists in array then move it to new position
1539 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1540 my $new_rank = $rank -
1541 1; # $new_rank is what you want the new index to be in the array
1542 my $moving_item = splice( @priority, $key, 1 );
1543 splice( @priority, $new_rank, 0, $moving_item );
1546 # now fix the priority on those that are left....
1550 WHERE biblionumber = ?
1551 AND borrowernumber = ?
1555 $sth = $dbh->prepare($query);
1556 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1559 $priority[$j]->{'borrowernumber'},
1560 $priority[$j]->{'reservedate'}
1565 $sth = $dbh->prepare( "SELECT borrowernumber FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1568 unless ( $ignoreSetLowestRank ) {
1569 while ( my $res = $sth->fetchrow_hashref() ) {
1570 _FixPriority( $biblio, $res->{'borrowernumber'}, '999999', 1 );
1575 =head2 _Findgroupreserve
1577 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1579 Looks for an item-specific match first, then for a title-level match, returning the
1580 first match found. If neither, then we look for a 3rd kind of match based on
1581 reserve constraints.
1583 TODO: add more explanation about reserve constraints
1585 C<&_Findgroupreserve> returns :
1586 C<@results> is an array of references-to-hash whose keys are mostly
1587 fields from the reserves table of the Koha database, plus
1588 C<biblioitemnumber>.
1592 sub _Findgroupreserve {
1593 my ( $bibitem, $biblio, $itemnumber ) = @_;
1594 my $dbh = C4::Context->dbh;
1596 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1597 # check for exact targetted match
1598 my $item_level_target_query = qq/
1599 SELECT reserves.biblionumber AS biblionumber,
1600 reserves.borrowernumber AS borrowernumber,
1601 reserves.reservedate AS reservedate,
1602 reserves.branchcode AS branchcode,
1603 reserves.cancellationdate AS cancellationdate,
1604 reserves.found AS found,
1605 reserves.reservenotes AS reservenotes,
1606 reserves.priority AS priority,
1607 reserves.timestamp AS timestamp,
1608 biblioitems.biblioitemnumber AS biblioitemnumber,
1609 reserves.itemnumber AS itemnumber
1611 JOIN biblioitems USING (biblionumber)
1612 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1615 AND item_level_request = 1
1617 AND reservedate <= CURRENT_DATE()
1619 my $sth = $dbh->prepare($item_level_target_query);
1620 $sth->execute($itemnumber);
1622 if ( my $data = $sth->fetchrow_hashref ) {
1623 push( @results, $data );
1625 return @results if @results;
1627 # check for title-level targetted match
1628 my $title_level_target_query = qq/
1629 SELECT reserves.biblionumber AS biblionumber,
1630 reserves.borrowernumber AS borrowernumber,
1631 reserves.reservedate AS reservedate,
1632 reserves.branchcode AS branchcode,
1633 reserves.cancellationdate AS cancellationdate,
1634 reserves.found AS found,
1635 reserves.reservenotes AS reservenotes,
1636 reserves.priority AS priority,
1637 reserves.timestamp AS timestamp,
1638 biblioitems.biblioitemnumber AS biblioitemnumber,
1639 reserves.itemnumber AS itemnumber
1641 JOIN biblioitems USING (biblionumber)
1642 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1645 AND item_level_request = 0
1646 AND hold_fill_targets.itemnumber = ?
1647 AND reservedate <= CURRENT_DATE()
1649 $sth = $dbh->prepare($title_level_target_query);
1650 $sth->execute($itemnumber);
1652 if ( my $data = $sth->fetchrow_hashref ) {
1653 push( @results, $data );
1655 return @results if @results;
1658 SELECT reserves.biblionumber AS biblionumber,
1659 reserves.borrowernumber AS borrowernumber,
1660 reserves.reservedate AS reservedate,
1661 reserves.waitingdate AS waitingdate,
1662 reserves.branchcode AS branchcode,
1663 reserves.cancellationdate AS cancellationdate,
1664 reserves.found AS found,
1665 reserves.reservenotes AS reservenotes,
1666 reserves.priority AS priority,
1667 reserves.timestamp AS timestamp,
1668 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1669 reserves.itemnumber AS itemnumber
1671 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1672 WHERE reserves.biblionumber = ?
1673 AND ( ( reserveconstraints.biblioitemnumber = ?
1674 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1675 AND reserves.reservedate = reserveconstraints.reservedate )
1676 OR reserves.constrainttype='a' )
1677 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1678 AND reserves.reservedate <= CURRENT_DATE()
1680 $sth = $dbh->prepare($query);
1681 $sth->execute( $biblio, $bibitem, $itemnumber );
1683 while ( my $data = $sth->fetchrow_hashref ) {
1684 push( @results, $data );
1689 =head2 _koha_notify_reserve
1691 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1693 Sends a notification to the patron that their hold has been filled (through
1694 ModReserveAffect, _not_ ModReserveFill)
1698 sub _koha_notify_reserve {
1699 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1701 my $dbh = C4::Context->dbh;
1702 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
1704 # Try to get the borrower's email address
1706 my $which_address = C4::Context->preference('AutoEmailPrimaryAddress');
1707 # If the system preference is set to 'first valid' (value == OFF), look up email address
1708 if ($which_address eq 'OFF') {
1709 $to_address = C4::Members::GetFirstValidEmailAddress( $borrowernumber );
1711 $to_address = $borrower->{$which_address};
1717 if ( $to_address || $borrower->{'smsalertnumber'} ) {
1718 $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold_Filled' } );
1720 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1721 $letter_code = $messagingprefs->{'letter_code'};
1723 $letter_code = 'HOLD_PRINT';
1727 my $sth = $dbh->prepare("
1730 WHERE borrowernumber = ?
1731 AND biblionumber = ?
1733 $sth->execute( $borrowernumber, $biblionumber );
1734 my $reserve = $sth->fetchrow_hashref;
1735 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1737 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1739 my $letter = C4::Letters::GetPreparedLetter (
1740 module => 'reserves',
1741 letter_code => $letter_code,
1742 branchcode => $reserve->{branchcode},
1744 'branches' => $branch_details,
1745 'borrowers' => $borrower,
1746 'biblio' => $biblionumber,
1747 'reserves' => $reserve,
1748 'items', $reserve->{'itemnumber'},
1750 substitute => { today => C4::Dates->new()->output() },
1751 ) or die "Could not find a letter called '$letter_code' in the 'reserves' module";
1755 if ( $print_mode ) {
1756 C4::Letters::EnqueueLetter( {
1758 borrowernumber => $borrowernumber,
1759 message_transport_type => 'print',
1765 if ( grep { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1766 # aka, 'email' in ->{'transports'}
1767 C4::Letters::EnqueueLetter(
1768 { letter => $letter,
1769 borrowernumber => $borrowernumber,
1770 message_transport_type => 'email',
1771 from_address => $admin_email_address,
1776 if ( grep { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1777 C4::Letters::EnqueueLetter(
1778 { letter => $letter,
1779 borrowernumber => $borrowernumber,
1780 message_transport_type => 'sms',
1786 =head2 _ShiftPriorityByDateAndPriority
1788 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1790 This increments the priority of all reserves after the one
1791 with either the lowest date after C<$reservedate>
1792 or the lowest priority after C<$priority>.
1794 It effectively makes room for a new reserve to be inserted with a certain
1795 priority, which is returned.
1797 This is most useful when the reservedate can be set by the user. It allows
1798 the new reserve to be placed before other reserves that have a later
1799 reservedate. Since priority also is set by the form in reserves/request.pl
1800 the sub accounts for that too.
1804 sub _ShiftPriorityByDateAndPriority {
1805 my ( $biblio, $resdate, $new_priority ) = @_;
1807 my $dbh = C4::Context->dbh;
1808 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1809 my $sth = $dbh->prepare( $query );
1810 $sth->execute( $biblio, $resdate, $new_priority );
1811 my $min_priority = $sth->fetchrow;
1812 # if no such matches are found, $new_priority remains as original value
1813 $new_priority = $min_priority if ( $min_priority );
1815 # Shift the priority up by one; works in conjunction with the next SQL statement
1816 $query = "UPDATE reserves
1817 SET priority = priority+1
1818 WHERE biblionumber = ?
1819 AND borrowernumber = ?
1822 my $sth_update = $dbh->prepare( $query );
1824 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1825 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1826 $sth = $dbh->prepare( $query );
1827 $sth->execute( $new_priority, $biblio );
1828 while ( my $row = $sth->fetchrow_hashref ) {
1829 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
1832 return $new_priority; # so the caller knows what priority they wind up receiving
1837 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
1839 Use when checking out an item to handle reserves
1840 If $cancelreserve boolean is set to true, it will remove existing reserve
1845 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
1847 my ( $restype, $res, $all_reserves ) = CheckReserves( $itemnumber );
1850 my $biblionumber = $res->{biblionumber};
1851 my $biblioitemnumber = $res->{biblioitemnumber};
1853 if ($res->{borrowernumber} == $borrowernumber) {
1854 ModReserveFill($res);
1858 # The item is reserved by someone else.
1859 # Find this item in the reserves
1862 foreach (@$all_reserves) {
1863 $_->{'borrowernumber'} == $borrowernumber or next;
1864 $_->{'biblionumber'} == $biblionumber or next;
1871 # The item is reserved by the current patron
1872 ModReserveFill($borr_res);
1875 if ($cancelreserve) { # cancel reserves on this item
1876 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
1877 CancelReserve($res->{'biblionumber'}, 0, $res->{'borrowernumber'});
1884 MergeHolds($dbh,$to_biblio, $from_biblio);
1886 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
1891 my ( $dbh, $to_biblio, $from_biblio ) = @_;
1892 my $sth = $dbh->prepare(
1893 "SELECT count(*) as reservenumber FROM reserves WHERE biblionumber = ?"
1895 $sth->execute($from_biblio);
1896 if ( my $data = $sth->fetchrow_hashref() ) {
1898 # holds exist on old record, if not we don't need to do anything
1899 $sth = $dbh->prepare(
1900 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
1901 $sth->execute( $to_biblio, $from_biblio );
1904 # don't reorder those already waiting
1906 $sth = $dbh->prepare(
1907 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
1909 my $upd_sth = $dbh->prepare(
1910 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
1911 AND reservedate = ? AND constrainttype = ? AND (itemnumber = ? or itemnumber is NULL) "
1913 $sth->execute( $to_biblio, 'W', 'T' );
1915 while ( my $reserve = $sth->fetchrow_hashref() ) {
1917 $priority, $to_biblio,
1918 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
1919 $reserve->{'constrainttype'}, $reserve->{'itemnumber'}
1928 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
1930 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
1935 my ($branch, $borrowernumber, $biblionumber) = @_;
1937 # return unless ( C4::Context->boolean_preference('printreserveslips') );
1939 my $reserve = GetReserveInfo($borrowernumber,$biblionumber )
1942 return C4::Letters::GetPreparedLetter (
1943 module => 'circulation',
1944 letter_code => 'RESERVESLIP',
1945 branchcode => $branch,
1947 'reserves' => $reserve,
1948 'branches' => $reserve->{branchcode},
1949 'borrowers' => $reserve,
1950 'biblio' => $reserve,
1951 'items' => $reserve,
1958 Koha Development Team <http://koha-community.org/>