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
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 # use warnings; # FIXME: someday
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 W(aiting) : the reserve has an itemnumber affected, and is on the way
61 F(inished) : the reserve has been completed, and is done
62 - itemnumber : empty : the reserve is still unaffected to an item
63 filled: the reserve is attached to an item
64 The complete workflow is :
65 ==== 1st use case ====
66 patron request a document, 1st available : P >0, F=NULL, I=NULL
67 a library having it run "transfertodo", and clic on the list
68 if there is no transfer to do, the reserve waiting
69 patron can pick it up P =0, F=W, I=filled
70 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
71 The pickup library recieve the book, it check in P =0, F=W, I=filled
72 The patron borrow the book P =0, F=F, I=filled
74 ==== 2nd use case ====
75 patron requests a document, a given item,
76 If pickup is holding branch P =0, F=W, I=filled
77 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
78 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
79 The patron borrow the book P =0, F=F, I=filled
88 # set the version for version checking
95 &GetReservesFromItemnumber
96 &GetReservesFromBiblionumber
97 &GetReservesFromBorrowernumber
111 &ModReserveMinusPriority
117 &CancelExpiredReserves
119 &IsAvailableForItemLevelRequest
122 &ToggleLowestPriority
128 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
134 $branch, $borrowernumber, $biblionumber,
135 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
136 $title, $checkitem, $found
139 GetReserveFee($borrowernumber, $biblionumber, $constraint,
141 my $dbh = C4::Context->dbh;
142 my $const = lc substr( $constraint, 0, 1 );
143 $resdate = format_date_in_iso( $resdate ) if ( $resdate );
144 $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
145 $expdate = format_date_in_iso( $expdate ) if ( $expdate );
146 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
147 # Make room in reserves for this before those of a later reserve date
148 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
152 # If the reserv had the waiting status, we had the value of the resdate
153 if ( $found eq 'W' ) {
154 $waitingdate = $resdate;
158 # updates take place here
160 my $nextacctno = &getnextacctno( $borrowernumber );
162 INSERT INTO accountlines
163 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
165 (?,?,now(),?,?,'Res',?)
167 my $usth = $dbh->prepare($query);
168 $usth->execute( $borrowernumber, $nextacctno, $fee,
169 "Reserve Charge - $title", $fee );
175 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
176 priority,reservenotes,itemnumber,found,waitingdate,expirationdate)
181 my $sth = $dbh->prepare($query);
183 $borrowernumber, $biblionumber, $resdate, $branch,
184 $const, $priority, $notes, $checkitem,
185 $found, $waitingdate, $expdate
188 # Send e-mail to librarian if syspref is active
189 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
190 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
191 my $biblio = GetBiblioData($biblionumber);
192 my $letter = C4::Letters::getletter( 'reserves', 'HOLDPLACED');
193 my $admin_email_address = C4::Context->preference('KohaAdminEmailAddress');
195 my %keys = (%$borrower, %$biblio);
196 foreach my $key (keys %keys) {
197 my $replacefield = "<<$key>>";
198 $letter->{content} =~ s/$replacefield/$keys{$key}/g;
199 $letter->{title} =~ s/$replacefield/$keys{$key}/g;
202 C4::Letters::EnqueueLetter(
204 borrowernumber => $borrowernumber,
205 message_transport_type => 'email',
206 from_address => $admin_email_address,
207 to_address => $admin_email_address,
216 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
218 INSERT INTO reserveconstraints
219 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
223 $sth = $dbh->prepare($query); # keep prepare outside the loop!
224 foreach (@$bibitems) {
225 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
228 return; # FIXME: why not have a useful return value?
231 =item GetReservesFromBiblionumber
233 ($count, $title_reserves) = &GetReserves($biblionumber);
235 This function gets the list of reservations for one C<$biblionumber>, returning a count
236 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
240 sub GetReservesFromBiblionumber {
241 my ($biblionumber) = shift or return (0, []);
242 my ($all_dates) = shift;
243 my $dbh = C4::Context->dbh;
245 # Find the desired items in the reserves
248 timestamp AS rtimestamp,
260 WHERE biblionumber = ? ";
261 unless ( $all_dates ) {
262 $query .= "AND reservedate <= CURRENT_DATE()";
264 $query .= "ORDER BY priority";
265 my $sth = $dbh->prepare($query);
266 $sth->execute($biblionumber);
269 while ( my $data = $sth->fetchrow_hashref ) {
271 # FIXME - What is this doing? How do constraints work?
272 if ($data->{constrainttype} eq 'o') {
274 SELECT biblioitemnumber
275 FROM reserveconstraints
276 WHERE biblionumber = ?
277 AND borrowernumber = ?
280 my $csth = $dbh->prepare($query);
281 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
283 while ( my $bibitemnos = $csth->fetchrow_array ) {
284 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
286 my $count = scalar @bibitemno;
288 # if we have two or more different specific itemtypes
289 # reserved by same person on same day
292 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
293 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
296 # Look up the book we just found.
297 $bdata = GetBiblioItemData( $bibitemno[0] );
299 # Add the results of this latest search to the current
301 # FIXME - An 'each' would probably be more efficient.
302 foreach my $key ( keys %$bdata ) {
303 $data->{$key} = $bdata->{$key};
306 push @results, $data;
308 return ( $#results + 1, \@results );
311 =item GetReservesFromItemnumber
313 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
315 TODO :: Description here
319 sub GetReservesFromItemnumber {
320 my ( $itemnumber, $all_dates ) = @_;
321 my $dbh = C4::Context->dbh;
323 SELECT reservedate,borrowernumber,branchcode
327 unless ( $all_dates ) {
328 $query .= " AND reservedate <= CURRENT_DATE()";
330 my $sth_res = $dbh->prepare($query);
331 $sth_res->execute($itemnumber);
332 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
333 return ( $reservedate, $borrowernumber, $branchcode );
336 =item GetReservesFromBorrowernumber
338 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
344 sub GetReservesFromBorrowernumber {
345 my ( $borrowernumber, $status ) = @_;
346 my $dbh = C4::Context->dbh;
349 $sth = $dbh->prepare("
352 WHERE borrowernumber=?
356 $sth->execute($borrowernumber,$status);
358 $sth = $dbh->prepare("
361 WHERE borrowernumber=?
364 $sth->execute($borrowernumber);
366 my $data = $sth->fetchall_arrayref({});
369 #-------------------------------------------------------------------------------------
370 =item CanBookBeReserved
372 $error = &CanBookBeReserved($borrowernumber, $biblionumber)
376 sub CanBookBeReserved{
377 my ($borrowernumber, $biblionumber) = @_;
379 my $dbh = C4::Context->dbh;
380 my $biblio = GetBiblioData($biblionumber);
381 my $borrower = C4::Members::GetMember(borrowernumber=>$borrowernumber);
382 my $controlbranch = C4::Context->preference('ReservesControlBranch');
383 my $itype = C4::Context->preference('item-level_itypes');
384 my $reservesrights= 0;
385 my $reservescount = 0;
387 # we retrieve the user rights
389 my $rightsquery = "SELECT categorycode, itemtype, branchcode, reservesallowed
391 WHERE categorycode IN (?, '*')";
392 push @args,$borrower->{categorycode};
394 if($controlbranch eq "ItemHomeLibrary"){
395 $rightsquery .= " AND branchcode = '*'";
396 }elsif($controlbranch eq "PatronLibrary"){
397 $rightsquery .= " AND branchcode IN (?,'*')";
398 push @args, $borrower->{branchcode};
402 $rightsquery .= " AND itemtype IN (?,'*')";
403 push @args, $biblio->{itemtype};
405 $rightsquery .= " AND itemtype = '*'";
408 $rightsquery .= " ORDER BY categorycode DESC, itemtype DESC, branchcode DESC";
409 my $sthrights = $dbh->prepare($rightsquery);
410 $sthrights->execute(@args);
412 if(my $row = $sthrights->fetchrow_hashref()){
413 $reservesrights = $row->{reservesallowed};
417 # we count how many reserves the borrower have
418 my $countquery = "SELECT count(*) as count
420 LEFT JOIN items USING (itemnumber)
421 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
422 LEFT JOIN borrowers USING (borrowernumber)
423 WHERE borrowernumber = ?
425 push @args, $borrowernumber;
428 $countquery .= "AND itemtype = ?";
429 push @args, $biblio->{itemtype};
432 if($controlbranch eq "PatronLibrary"){
433 $countquery .= " AND borrowers.branchcode = ? ";
434 push @args, $borrower->{branchcode};
437 my $sthcount = $dbh->prepare($countquery);
438 $sthcount->execute(@args);
440 if(my $row = $sthcount->fetchrow_hashref()){
441 $reservescount = $row->{count};
443 if($reservescount < $reservesrights){
451 =item CanItemBeReserved
453 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
455 this function return 1 if an item can be issued by this borrower.
459 sub CanItemBeReserved{
460 my ($borrowernumber, $itemnumber) = @_;
462 my $dbh = C4::Context->dbh;
463 my $allowedreserves = 0;
465 my $controlbranch = C4::Context->preference('ReservesControlBranch');
466 my $itype = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
468 # we retrieve borrowers and items informations #
469 my $item = GetItem($itemnumber);
470 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
472 # we retrieve user rights on this itemtype and branchcode
473 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
475 WHERE (categorycode in (?,'*') )
476 AND (itemtype IN (?,'*'))
477 AND (branchcode IN (?,'*'))
484 my $querycount ="SELECT
487 LEFT JOIN items USING (itemnumber)
488 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
489 LEFT JOIN borrowers USING (borrowernumber)
490 WHERE borrowernumber = ?
494 my $itemtype = $item->{$itype};
495 my $categorycode = $borrower->{categorycode};
497 my $branchfield = "reserves.branchcode";
499 if( $controlbranch eq "ItemHomeLibrary" ){
500 $branchfield = "items.homebranch";
501 $branchcode = $item->{homebranch};
502 }elsif( $controlbranch eq "PatronLibrary" ){
503 $branchfield = "borrowers.branchcode";
504 $branchcode = $borrower->{branchcode};
508 $sth->execute($categorycode, $itemtype, $branchcode);
509 if(my $rights = $sth->fetchrow_hashref()){
510 $itemtype = $rights->{itemtype};
511 $allowedreserves = $rights->{reservesallowed};
518 $querycount .= "AND $branchfield = ?";
520 $querycount .= " AND $itype = ?" if ($itemtype ne "*");
521 my $sthcount = $dbh->prepare($querycount);
523 if($itemtype eq "*"){
524 $sthcount->execute($borrowernumber, $branchcode);
526 $sthcount->execute($borrowernumber, $branchcode, $itemtype);
529 my $reservecount = "0";
530 if(my $rowcount = $sthcount->fetchrow_hashref()){
531 $reservecount = $rowcount->{count};
534 # we check if it's ok or not
535 if( $reservecount < $allowedreserves ){
541 #--------------------------------------------------------------------------------
542 =item GetReserveCount
544 $number = &GetReserveCount($borrowernumber);
546 this function returns the number of reservation for a borrower given on input arg.
550 sub GetReserveCount {
551 my ($borrowernumber) = @_;
553 my $dbh = C4::Context->dbh;
556 SELECT COUNT(*) AS counter
558 WHERE borrowernumber = ?
560 my $sth = $dbh->prepare($query);
561 $sth->execute($borrowernumber);
562 my $row = $sth->fetchrow_hashref;
563 return $row->{counter};
566 =item GetOtherReserves
568 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
570 Check queued list of this document and check if this document must be transfered
574 sub GetOtherReserves {
575 my ($itemnumber) = @_;
578 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
579 if ($checkreserves) {
580 my $iteminfo = GetItem($itemnumber);
581 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
582 $messages->{'transfert'} = $checkreserves->{'branchcode'};
583 #minus priorities of others reservs
584 ModReserveMinusPriority(
586 $checkreserves->{'borrowernumber'},
587 $iteminfo->{'biblionumber'}
590 #launch the subroutine dotransfer
591 C4::Items::ModItemTransfer(
593 $iteminfo->{'holdingbranch'},
594 $checkreserves->{'branchcode'}
599 #step 2b : case of a reservation on the same branch, set the waiting status
601 $messages->{'waiting'} = 1;
602 ModReserveMinusPriority(
604 $checkreserves->{'borrowernumber'},
605 $iteminfo->{'biblionumber'}
607 ModReserveStatus($itemnumber,'W');
610 $nextreservinfo = $checkreserves->{'borrowernumber'};
613 return ( $messages, $nextreservinfo );
618 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
620 Calculate the fee for a reserve
625 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
628 my $dbh = C4::Context->dbh;
629 my $const = lc substr( $constraint, 0, 1 );
631 SELECT * FROM borrowers
632 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
633 WHERE borrowernumber = ?
635 my $sth = $dbh->prepare($query);
636 $sth->execute($borrowernumber);
637 my $data = $sth->fetchrow_hashref;
639 my $fee = $data->{'reservefee'};
640 my $cntitems = @- > $bibitems;
644 # check for items on issue
645 # first find biblioitem records
647 my $sth1 = $dbh->prepare(
648 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
649 WHERE (biblio.biblionumber = ?)"
651 $sth1->execute($biblionumber);
652 while ( my $data1 = $sth1->fetchrow_hashref ) {
653 if ( $const eq "a" ) {
654 push @biblioitems, $data1;
659 while ( $x < $cntitems ) {
660 if ( @$bibitems->{'biblioitemnumber'} ==
661 $data->{'biblioitemnumber'} )
667 if ( $const eq 'o' ) {
669 push @biblioitems, $data1;
674 push @biblioitems, $data1;
680 my $cntitemsfound = @biblioitems;
684 while ( $x < $cntitemsfound ) {
685 my $bitdata = $biblioitems[$x];
686 my $sth2 = $dbh->prepare(
688 WHERE biblioitemnumber = ?"
690 $sth2->execute( $bitdata->{'biblioitemnumber'} );
691 while ( my $itdata = $sth2->fetchrow_hashref ) {
692 my $sth3 = $dbh->prepare(
693 "SELECT * FROM issues
694 WHERE itemnumber = ?"
696 $sth3->execute( $itdata->{'itemnumber'} );
697 if ( my $isdata = $sth3->fetchrow_hashref ) {
705 if ( $allissued == 0 ) {
707 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
708 $rsth->execute($biblionumber);
709 if ( my $rdata = $rsth->fetchrow_hashref ) {
719 =item GetReservesToBranch
721 @transreserv = GetReservesToBranch( $frombranch );
723 Get reserve list for a given branch
727 sub GetReservesToBranch {
728 my ( $frombranch ) = @_;
729 my $dbh = C4::Context->dbh;
730 my $sth = $dbh->prepare(
731 "SELECT borrowernumber,reservedate,itemnumber,timestamp
736 $sth->execute( $frombranch );
739 while ( my $data = $sth->fetchrow_hashref ) {
740 $transreserv[$i] = $data;
743 return (@transreserv);
746 =item GetReservesForBranch
748 @transreserv = GetReservesForBranch($frombranch);
752 sub GetReservesForBranch {
753 my ($frombranch) = @_;
754 my $dbh = C4::Context->dbh;
755 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
760 $query .= " AND branchcode=? ";
762 $query .= "ORDER BY waitingdate" ;
763 my $sth = $dbh->prepare($query);
765 $sth->execute($frombranch);
772 while ( my $data = $sth->fetchrow_hashref ) {
773 $transreserv[$i] = $data;
776 return (@transreserv);
781 ($status, $reserve) = &CheckReserves($itemnumber);
782 ($status, $reserve) = &CheckReserves(undef, $barcode);
784 Find a book in the reserves.
786 C<$itemnumber> is the book's item number.
788 As I understand it, C<&CheckReserves> looks for the given item in the
789 reserves. If it is found, that's a match, and C<$status> is set to
792 Otherwise, it finds the most important item in the reserves with the
793 same biblio number as this book (I'm not clear on this) and returns it
794 with C<$status> set to C<Reserved>.
796 C<&CheckReserves> returns a two-element list:
798 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
800 C<$reserve> is the reserve item that matched. It is a
801 reference-to-hash whose keys are mostly the fields of the reserves
802 table in the Koha database.
807 my ( $item, $barcode ) = @_;
808 my $dbh = C4::Context->dbh;
811 SELECT items.biblionumber,
812 items.biblioitemnumber,
813 itemtypes.notforloan,
814 items.notforloan AS itemnotforloan,
817 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
818 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
822 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
823 $sth->execute($item);
826 $sth = $dbh->prepare("$select WHERE barcode = ?");
827 $sth->execute($barcode);
829 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
830 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber ) = $sth->fetchrow_array;
832 return ( 0, 0 ) unless $itemnumber; # bail if we got nothing.
834 # if item is not for loan it cannot be reserved either.....
835 # execpt where items.notforloan < 0 : This indicates the item is holdable.
836 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
838 # Find this item in the reserves
839 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber );
841 # $priority and $highest are used to find the most important item
842 # in the list returned by &_Findgroupreserve. (The lower $priority,
843 # the more important the item.)
844 # $highest is the most important item we've seen so far.
846 if (scalar @reserves) {
847 my $priority = 10000000;
848 foreach my $res (@reserves) {
849 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
850 return ( "Waiting", $res ); # Found it
852 # See if this item is more important than what we've got so far
853 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
854 $priority = $res->{'priority'};
861 # If we get this far, then no exact match was found.
862 # We return the most important (i.e. next) reservation.
864 $highest->{'itemnumber'} = $item;
865 return ( "Reserved", $highest );
872 =item CancelExpiredReserves
874 CancelExpiredReserves();
876 Cancels all reserves with an expiration date from before today.
880 sub CancelExpiredReserves {
882 my $dbh = C4::Context->dbh;
883 my $sth = $dbh->prepare( "
884 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
885 AND expirationdate IS NOT NULL
889 while ( my $res = $sth->fetchrow_hashref() ) {
890 CancelReserve( $res->{'biblionumber'}, '', $res->{'borrowernumber'} );
897 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
901 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
902 cancel, but not both: if both are given, C<&CancelReserve> does
905 C<$borrowernumber> is the borrower number of the patron on whose
906 behalf the book was reserved.
908 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
909 priorities of the other people who are waiting on the book.
914 my ( $biblio, $item, $borr ) = @_;
915 my $dbh = C4::Context->dbh;
916 if ( $item and $borr ) {
917 # removing a waiting reserve record....
918 # update the database...
921 SET cancellationdate = now(),
925 AND borrowernumber = ?
927 my $sth = $dbh->prepare($query);
928 $sth->execute( $item, $borr );
931 INSERT INTO old_reserves
932 SELECT * FROM reserves
934 AND borrowernumber = ?
936 $sth = $dbh->prepare($query);
937 $sth->execute( $item, $borr );
941 AND borrowernumber = ?
943 $sth = $dbh->prepare($query);
944 $sth->execute( $item, $borr );
947 # removing a reserve record....
948 # get the prioritiy on this record....
951 SELECT priority FROM reserves
952 WHERE biblionumber = ?
953 AND borrowernumber = ?
954 AND cancellationdate IS NULL
955 AND itemnumber IS NULL
957 my $sth = $dbh->prepare($query);
958 $sth->execute( $biblio, $borr );
959 ($priority) = $sth->fetchrow_array;
963 SET cancellationdate = now(),
966 WHERE biblionumber = ?
967 AND borrowernumber = ?
970 # update the database, removing the record...
971 $sth = $dbh->prepare($query);
972 $sth->execute( $biblio, $borr );
976 INSERT INTO old_reserves
977 SELECT * FROM reserves
978 WHERE biblionumber = ?
979 AND borrowernumber = ?
981 $sth = $dbh->prepare($query);
982 $sth->execute( $biblio, $borr );
986 WHERE biblionumber = ?
987 AND borrowernumber = ?
989 $sth = $dbh->prepare($query);
990 $sth->execute( $biblio, $borr );
992 # now fix the priority on the others....
993 _FixPriority( $priority, $biblio );
1001 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
1005 Change a hold request's priority or cancel it.
1007 C<$rank> specifies the effect of the change. If C<$rank>
1008 is 'W' or 'n', nothing happens. This corresponds to leaving a
1009 request alone when changing its priority in the holds queue
1012 If C<$rank> is 'del', the hold request is cancelled.
1014 If C<$rank> is an integer greater than zero, the priority of
1015 the request is set to that value. Since priority != 0 means
1016 that the item is not waiting on the hold shelf, setting the
1017 priority to a non-zero value also sets the request's found
1018 status and waiting date to NULL.
1020 The optional C<$itemnumber> parameter is used only when
1021 C<$rank> is a non-zero integer; if supplied, the itemnumber
1022 of the hold request is set accordingly; if omitted, the itemnumber
1025 FIXME: Note that the forgoing can have the effect of causing
1026 item-level hold requests to turn into title-level requests. This
1027 will be fixed once reserves has separate columns for requested
1028 itemnumber and supplying itemnumber.
1033 #subroutine to update a reserve
1034 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
1035 return if $rank eq "W";
1036 return if $rank eq "n";
1037 my $dbh = C4::Context->dbh;
1038 if ( $rank eq "del" ) {
1041 SET cancellationdate=now()
1042 WHERE biblionumber = ?
1043 AND borrowernumber = ?
1045 my $sth = $dbh->prepare($query);
1046 $sth->execute( $biblio, $borrower );
1049 INSERT INTO old_reserves
1052 WHERE biblionumber = ?
1053 AND borrowernumber = ?
1055 $sth = $dbh->prepare($query);
1056 $sth->execute( $biblio, $borrower );
1058 DELETE FROM reserves
1059 WHERE biblionumber = ?
1060 AND borrowernumber = ?
1062 $sth = $dbh->prepare($query);
1063 $sth->execute( $biblio, $borrower );
1066 elsif ($rank =~ /^\d+/ and $rank > 0) {
1068 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1069 WHERE biblionumber = ?
1070 AND borrowernumber = ?
1072 my $sth = $dbh->prepare($query);
1073 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
1075 _FixPriority( $biblio, $borrower, $rank);
1079 =item ModReserveFill
1081 &ModReserveFill($reserve);
1083 Fill a reserve. If I understand this correctly, this means that the
1084 reserved book has been found and given to the patron who reserved it.
1086 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1087 whose keys are fields from the reserves table in the Koha database.
1091 sub ModReserveFill {
1093 my $dbh = C4::Context->dbh;
1094 # fill in a reserve record....
1095 my $biblionumber = $res->{'biblionumber'};
1096 my $borrowernumber = $res->{'borrowernumber'};
1097 my $resdate = $res->{'reservedate'};
1099 # get the priority on this record....
1101 my $query = "SELECT priority
1103 WHERE biblionumber = ?
1104 AND borrowernumber = ?
1105 AND reservedate = ?";
1106 my $sth = $dbh->prepare($query);
1107 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1108 ($priority) = $sth->fetchrow_array;
1111 # update the database...
1112 $query = "UPDATE reserves
1115 WHERE biblionumber = ?
1117 AND borrowernumber = ?
1119 $sth = $dbh->prepare($query);
1120 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1123 # move to old_reserves
1124 $query = "INSERT INTO old_reserves
1125 SELECT * FROM reserves
1126 WHERE biblionumber = ?
1128 AND borrowernumber = ?
1130 $sth = $dbh->prepare($query);
1131 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1132 $query = "DELETE FROM reserves
1133 WHERE biblionumber = ?
1135 AND borrowernumber = ?
1137 $sth = $dbh->prepare($query);
1138 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1140 # now fix the priority on the others (if the priority wasn't
1141 # already sorted!)....
1142 unless ( $priority == 0 ) {
1143 _FixPriority( $priority, $biblionumber );
1147 =item ModReserveStatus
1149 &ModReserveStatus($itemnumber, $newstatus);
1151 Update the reserve status for the active (priority=0) reserve.
1153 $itemnumber is the itemnumber the reserve is on
1155 $newstatus is the new status.
1159 sub ModReserveStatus {
1161 #first : check if we have a reservation for this item .
1162 my ($itemnumber, $newstatus) = @_;
1163 my $dbh = C4::Context->dbh;
1164 my $query = " UPDATE reserves
1165 SET found=?,waitingdate = now()
1170 my $sth_set = $dbh->prepare($query);
1171 $sth_set->execute( $newstatus, $itemnumber );
1173 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1174 CartToShelf( $itemnumber );
1178 =item ModReserveAffect
1180 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1182 This function affect an item and a status for a given reserve
1183 The itemnumber parameter is used to find the biblionumber.
1184 with the biblionumber & the borrowernumber, we can affect the itemnumber
1185 to the correct reserve.
1187 if $transferToDo is not set, then the status is set to "Waiting" as well.
1188 otherwise, a transfer is on the way, and the end of the transfer will
1189 take care of the waiting status
1192 sub ModReserveAffect {
1193 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1194 my $dbh = C4::Context->dbh;
1196 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1197 # attached to $itemnumber
1198 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1199 $sth->execute($itemnumber);
1200 my ($biblionumber) = $sth->fetchrow;
1202 # get request - need to find out if item is already
1203 # waiting in order to not send duplicate hold filled notifications
1204 my $request = GetReserveInfo($borrowernumber, $biblionumber);
1205 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1207 # If we affect a reserve that has to be transfered, don't set to Waiting
1209 if ($transferToDo) {
1214 WHERE borrowernumber = ?
1215 AND biblionumber = ?
1219 # affect the reserve to Waiting as well.
1226 WHERE borrowernumber = ?
1227 AND biblionumber = ?
1230 $sth = $dbh->prepare($query);
1231 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1232 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1234 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1235 CartToShelf( $itemnumber );
1241 =item ModReserveCancelAll
1243 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1245 function to cancel reserv,check other reserves, and transfer document if it's necessary
1249 sub ModReserveCancelAll {
1252 my ( $itemnumber, $borrowernumber ) = @_;
1254 #step 1 : cancel the reservation
1255 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1257 #step 2 launch the subroutine of the others reserves
1258 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1260 return ( $messages, $nextreservinfo );
1263 =item ModReserveMinusPriority
1265 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1267 Reduce the values of queuded list
1271 sub ModReserveMinusPriority {
1272 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1274 #first step update the value of the first person on reserv
1275 my $dbh = C4::Context->dbh;
1278 SET priority = 0 , itemnumber = ?
1279 WHERE borrowernumber=?
1282 my $sth_upd = $dbh->prepare($query);
1283 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1284 # second step update all others reservs
1285 _FixPriority($biblionumber, $borrowernumber, '0');
1288 =item GetReserveInfo
1290 &GetReserveInfo($borrowernumber,$biblionumber);
1292 Get item and borrower details for a current hold.
1293 Current implementation this query should have a single result.
1296 sub GetReserveInfo {
1297 my ( $borrowernumber, $biblionumber ) = @_;
1298 my $dbh = C4::Context->dbh;
1299 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1300 reserves.biblionumber, reserves.branchcode,
1301 notificationdate, reminderdate, priority, found,
1302 firstname, surname, phone,
1303 email, address, address2,
1304 cardnumber, city, zipcode,
1305 biblio.title, biblio.author,
1306 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1308 FROM reserves left join items
1309 ON items.itemnumber=reserves.itemnumber ,
1312 reserves.borrowernumber=? &&
1313 reserves.biblionumber=? &&
1314 reserves.borrowernumber=borrowers.borrowernumber &&
1315 reserves.biblionumber=biblio.biblionumber ";
1316 my $sth = $dbh->prepare($strsth);
1317 $sth->execute($borrowernumber,$biblionumber);
1319 my $data = $sth->fetchrow_hashref;
1324 =item IsAvailableForItemLevelRequest
1328 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1332 Checks whether a given item record is available for an
1333 item-level hold request. An item is available if
1335 * it is not lost AND
1336 * it is not damaged AND
1337 * it is not withdrawn AND
1338 * does not have a not for loan value > 0
1340 Whether or not the item is currently on loan is
1341 also checked - if the AllowOnShelfHolds system preference
1342 is ON, an item can be requested even if it is currently
1343 on loan to somebody else. If the system preference
1344 is OFF, an item that is currently checked out cannot
1345 be the target of an item-level hold request.
1347 Note that IsAvailableForItemLevelRequest() does not
1348 check if the staff operator is authorized to place
1349 a request on the item - in particular,
1350 this routine does not check IndependantBranches
1351 and canreservefromotherbranches.
1355 sub IsAvailableForItemLevelRequest {
1356 my $itemnumber = shift;
1358 my $item = GetItem($itemnumber);
1360 # must check the notforloan setting of the itemtype
1361 # FIXME - a lot of places in the code do this
1362 # or something similar - need to be
1364 my $dbh = C4::Context->dbh;
1365 my $notforloan_query;
1366 if (C4::Context->preference('item-level_itypes')) {
1367 $notforloan_query = "SELECT itemtypes.notforloan
1369 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1370 WHERE itemnumber = ?";
1372 $notforloan_query = "SELECT itemtypes.notforloan
1374 JOIN biblioitems USING (biblioitemnumber)
1375 JOIN itemtypes USING (itemtype)
1376 WHERE itemnumber = ?";
1378 my $sth = $dbh->prepare($notforloan_query);
1379 $sth->execute($itemnumber);
1380 my $notforloan_per_itemtype = 0;
1381 if (my ($notforloan) = $sth->fetchrow_array) {
1382 $notforloan_per_itemtype = 1 if $notforloan;
1385 my $available_per_item = 1;
1386 $available_per_item = 0 if $item->{itemlost} or
1387 ( $item->{notforloan} > 0 ) or
1388 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1389 $item->{wthdrawn} or
1390 $notforloan_per_itemtype;
1393 if (C4::Context->preference('AllowOnShelfHolds')) {
1394 return $available_per_item;
1396 return ($available_per_item and $item->{onloan});
1401 AlterPriority( $where, $borrowernumber, $biblionumber, $reservedate );
1403 This function changes a reserve's priority up, down, to the top, or to the bottom.
1404 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1408 my ( $where, $borrowernumber, $biblionumber ) = @_;
1410 my $dbh = C4::Context->dbh;
1412 ## Find this reserve
1413 my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? AND cancellationdate IS NULL');
1414 $sth->execute( $biblionumber, $borrowernumber );
1415 my $reserve = $sth->fetchrow_hashref();
1418 if ( $where eq 'up' || $where eq 'down' ) {
1420 my $priority = $reserve->{'priority'};
1421 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1422 _FixPriority( $biblionumber, $borrowernumber, $priority )
1424 } elsif ( $where eq 'top' ) {
1426 _FixPriority( $biblionumber, $borrowernumber, '1' )
1428 } elsif ( $where eq 'bottom' ) {
1430 _FixPriority( $biblionumber, $borrowernumber, '999999' )
1435 =item ToggleLowestPriority
1436 ToggleLowestPriority( $borrowernumber, $biblionumber );
1438 This function sets the lowestPriority field to true if is false, and false if it is true.
1441 sub ToggleLowestPriority {
1442 my ( $borrowernumber, $biblionumber ) = @_;
1444 my $dbh = C4::Context->dbh;
1446 my $sth = $dbh->prepare(
1447 "UPDATE reserves SET lowestPriority = NOT lowestPriority
1448 WHERE biblionumber = ?
1449 AND borrowernumber = ?"
1457 _FixPriority( $biblionumber, $borrowernumber, '999999' );
1462 &_FixPriority($biblio,$borrowernumber,$rank,$ignoreSetLowestRank);
1464 Only used internally (so don't export it)
1465 Changed how this functions works #
1466 Now just gets an array of reserves in the rank order and updates them with
1467 the array index (+1 as array starts from 0)
1468 and if $rank is supplied will splice item from the array and splice it back in again
1469 in new priority rank
1474 my ( $biblio, $borrowernumber, $rank, $ignoreSetLowestRank ) = @_;
1475 my $dbh = C4::Context->dbh;
1476 if ( $rank eq "del" ) {
1477 CancelReserve( $biblio, undef, $borrowernumber );
1479 if ( $rank eq "W" || $rank eq "0" ) {
1481 # make sure priority for waiting items is 0
1485 WHERE biblionumber = ?
1486 AND borrowernumber = ?
1489 my $sth = $dbh->prepare($query);
1490 $sth->execute( $biblio, $borrowernumber );
1496 # FIXME adding a new security in returned elements for changing priority,
1497 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1498 # This is wrong a waiting reserve has W set
1499 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1501 SELECT borrowernumber, reservedate, constrainttype
1503 WHERE biblionumber = ?
1504 AND ((found <> 'W') or found is NULL)
1505 ORDER BY priority ASC
1507 my $sth = $dbh->prepare($query);
1508 $sth->execute($biblio);
1509 while ( my $line = $sth->fetchrow_hashref ) {
1510 push( @reservedates, $line );
1511 push( @priority, $line );
1514 # To find the matching index
1516 my $key = -1; # to allow for 0 to be a valid result
1517 for ( $i = 0 ; $i < @priority ; $i++ ) {
1518 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1519 $key = $i; # save the index
1524 # if index exists in array then move it to new position
1525 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1526 my $new_rank = $rank -
1527 1; # $new_rank is what you want the new index to be in the array
1528 my $moving_item = splice( @priority, $key, 1 );
1529 splice( @priority, $new_rank, 0, $moving_item );
1532 # now fix the priority on those that are left....
1536 WHERE biblionumber = ?
1537 AND borrowernumber = ?
1541 $sth = $dbh->prepare($query);
1542 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1545 $priority[$j]->{'borrowernumber'},
1546 $priority[$j]->{'reservedate'}
1551 $sth = $dbh->prepare( "SELECT borrowernumber FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1554 unless ( $ignoreSetLowestRank ) {
1555 while ( my $res = $sth->fetchrow_hashref() ) {
1556 _FixPriority( $biblio, $res->{'borrowernumber'}, '999999', 1 );
1561 =item _Findgroupreserve
1563 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1565 Looks for an item-specific match first, then for a title-level match, returning the
1566 first match found. If neither, then we look for a 3rd kind of match based on
1567 reserve constraints.
1569 TODO: add more explanation about reserve constraints
1571 C<&_Findgroupreserve> returns :
1572 C<@results> is an array of references-to-hash whose keys are mostly
1573 fields from the reserves table of the Koha database, plus
1574 C<biblioitemnumber>.
1578 sub _Findgroupreserve {
1579 my ( $bibitem, $biblio, $itemnumber ) = @_;
1580 my $dbh = C4::Context->dbh;
1582 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1583 # check for exact targetted match
1584 my $item_level_target_query = qq/
1585 SELECT reserves.biblionumber AS biblionumber,
1586 reserves.borrowernumber AS borrowernumber,
1587 reserves.reservedate AS reservedate,
1588 reserves.branchcode AS branchcode,
1589 reserves.cancellationdate AS cancellationdate,
1590 reserves.found AS found,
1591 reserves.reservenotes AS reservenotes,
1592 reserves.priority AS priority,
1593 reserves.timestamp AS timestamp,
1594 biblioitems.biblioitemnumber AS biblioitemnumber,
1595 reserves.itemnumber AS itemnumber
1597 JOIN biblioitems USING (biblionumber)
1598 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1601 AND item_level_request = 1
1603 AND reservedate <= CURRENT_DATE()
1605 my $sth = $dbh->prepare($item_level_target_query);
1606 $sth->execute($itemnumber);
1608 if ( my $data = $sth->fetchrow_hashref ) {
1609 push( @results, $data );
1611 return @results if @results;
1613 # check for title-level targetted match
1614 my $title_level_target_query = qq/
1615 SELECT reserves.biblionumber AS biblionumber,
1616 reserves.borrowernumber AS borrowernumber,
1617 reserves.reservedate AS reservedate,
1618 reserves.branchcode AS branchcode,
1619 reserves.cancellationdate AS cancellationdate,
1620 reserves.found AS found,
1621 reserves.reservenotes AS reservenotes,
1622 reserves.priority AS priority,
1623 reserves.timestamp AS timestamp,
1624 biblioitems.biblioitemnumber AS biblioitemnumber,
1625 reserves.itemnumber AS itemnumber
1627 JOIN biblioitems USING (biblionumber)
1628 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1631 AND item_level_request = 0
1632 AND hold_fill_targets.itemnumber = ?
1633 AND reservedate <= CURRENT_DATE()
1635 $sth = $dbh->prepare($title_level_target_query);
1636 $sth->execute($itemnumber);
1638 if ( my $data = $sth->fetchrow_hashref ) {
1639 push( @results, $data );
1641 return @results if @results;
1644 SELECT reserves.biblionumber AS biblionumber,
1645 reserves.borrowernumber AS borrowernumber,
1646 reserves.reservedate AS reservedate,
1647 reserves.branchcode AS branchcode,
1648 reserves.cancellationdate AS cancellationdate,
1649 reserves.found AS found,
1650 reserves.reservenotes AS reservenotes,
1651 reserves.priority AS priority,
1652 reserves.timestamp AS timestamp,
1653 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1654 reserves.itemnumber AS itemnumber
1656 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1657 WHERE reserves.biblionumber = ?
1658 AND ( ( reserveconstraints.biblioitemnumber = ?
1659 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1660 AND reserves.reservedate = reserveconstraints.reservedate )
1661 OR reserves.constrainttype='a' )
1662 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1663 AND reserves.reservedate <= CURRENT_DATE()
1665 $sth = $dbh->prepare($query);
1666 $sth->execute( $biblio, $bibitem, $itemnumber );
1668 while ( my $data = $sth->fetchrow_hashref ) {
1669 push( @results, $data );
1674 =item _koha_notify_reserve
1678 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1682 Sends a notification to the patron that their hold has been filled (through
1683 ModReserveAffect, _not_ ModReserveFill)
1687 sub _koha_notify_reserve {
1688 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1690 my $dbh = C4::Context->dbh;
1691 my $borrower = C4::Members::GetMember( $borrowernumber );
1695 if ( $borrower->{'email'} || $borrower->{'smsalertnumber'} ) {
1696 $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1698 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1699 $letter_code = $messagingprefs->{'letter_code'};
1701 $letter_code = 'HOLD_PRINT';
1705 my $sth = $dbh->prepare("
1708 WHERE borrowernumber = ?
1709 AND biblionumber = ?
1711 $sth->execute( $borrowernumber, $biblionumber );
1712 my $reserve = $sth->fetchrow_hashref;
1713 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1715 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1717 my $letter = getletter( 'reserves', $letter_code );
1718 die "Could not find a letter called '$letter_code' in the 'reserves' module" unless( $letter );
1720 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1721 C4::Letters::parseletter( $letter, 'borrowers', $borrowernumber );
1722 C4::Letters::parseletter( $letter, 'biblio', $biblionumber );
1723 C4::Letters::parseletter( $letter, 'reserves', $borrowernumber, $biblionumber );
1725 if ( $reserve->{'itemnumber'} ) {
1726 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1728 my $today = C4::Dates->new()->output();
1729 $letter->{'title'} =~ s/<<today>>/$today/g;
1730 $letter->{'content'} =~ s/<<today>>/$today/g;
1731 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1733 if ( $print_mode ) {
1734 C4::Letters::EnqueueLetter( {
1736 borrowernumber => $borrowernumber,
1737 message_transport_type => 'print',
1743 if ( grep { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1744 # aka, 'email' in ->{'transports'}
1745 C4::Letters::EnqueueLetter(
1746 { letter => $letter,
1747 borrowernumber => $borrowernumber,
1748 message_transport_type => 'email',
1749 from_address => $admin_email_address,
1754 if ( grep { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1755 C4::Letters::EnqueueLetter(
1756 { letter => $letter,
1757 borrowernumber => $borrowernumber,
1758 message_transport_type => 'sms',
1764 =item _ShiftPriorityByDateAndPriority
1768 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1772 This increments the priority of all reserves after the one
1773 with either the lowest date after C<$reservedate>
1774 or the lowest priority after C<$priority>.
1776 It effectively makes room for a new reserve to be inserted with a certain
1777 priority, which is returned.
1779 This is most useful when the reservedate can be set by the user. It allows
1780 the new reserve to be placed before other reserves that have a later
1781 reservedate. Since priority also is set by the form in reserves/request.pl
1782 the sub accounts for that too.
1786 sub _ShiftPriorityByDateAndPriority {
1787 my ( $biblio, $resdate, $new_priority ) = @_;
1789 my $dbh = C4::Context->dbh;
1790 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC";
1791 my $sth = $dbh->prepare( $query );
1792 $sth->execute( $biblio, $resdate, $new_priority );
1793 my ( $min_priority ) = $sth->fetchrow;
1794 $sth->finish; # $sth might have more data.
1795 $new_priority = $min_priority if ( $min_priority );
1796 my $updated_priority = $new_priority + 1;
1801 WHERE biblionumber = ?
1802 AND borrowernumber = ?
1805 my $sth_update = $dbh->prepare( $query );
1807 $query = "SELECT * FROM reserves WHERE priority >= ?";
1808 $sth = $dbh->prepare( $query );
1809 $sth->execute( $new_priority );
1810 while ( my $row = $sth->fetchrow_hashref ) {
1811 $sth_update->execute( $updated_priority, $biblio, $row->{borrowernumber}, $row->{reservedate} );
1812 $updated_priority++;
1815 return $new_priority; # so the caller knows what priority they end up at
1822 Koha Developement team <info@koha.org>