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
34 # for _koha_notify_reserve
35 use C4::Members::Messaging;
38 use C4::Branch qw( GetBranchDetail );
39 use C4::Dates qw( format_date_in_iso );
40 use List::MoreUtils qw( firstidx );
42 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
46 C4::Reserves - Koha functions for dealing with reservation.
54 This modules provides somes functions to deal with reservations.
56 Reserves are stored in reserves table.
57 The following columns contains important values :
58 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
59 =0 : then the reserve is being dealed
60 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
61 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
62 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
63 F(inished) : the reserve has been completed, and is done
64 - itemnumber : empty : the reserve is still unaffected to an item
65 filled: the reserve is attached to an item
66 The complete workflow is :
67 ==== 1st use case ====
68 patron request a document, 1st available : P >0, F=NULL, I=NULL
69 a library having it run "transfertodo", and clic on the list
70 if there is no transfer to do, the reserve waiting
71 patron can pick it up P =0, F=W, I=filled
72 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
73 The pickup library recieve the book, it check in P =0, F=W, I=filled
74 The patron borrow the book P =0, F=F, I=filled
76 ==== 2nd use case ====
77 patron requests a document, a given item,
78 If pickup is holding branch P =0, F=W, I=filled
79 If transfer needed, write in branchtransfer P =0, F=T, I=filled
80 The pickup library receive the book, it checks it in P =0, F=W, I=filled
81 The patron borrow the book P =0, F=F, I=filled
88 # set the version for version checking
95 &GetReservesFromItemnumber
96 &GetReservesFromBiblionumber
97 &GetReservesFromBorrowernumber
112 &ModReserveMinusPriority
118 &CancelExpiredReserves
120 &IsAvailableForItemLevelRequest
123 &ToggleLowestPriority
125 @EXPORT_OK = qw( MergeHolds );
130 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
136 $branch, $borrowernumber, $biblionumber,
137 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
138 $title, $checkitem, $found
141 GetReserveFee($borrowernumber, $biblionumber, $constraint,
143 my $dbh = C4::Context->dbh;
144 my $const = lc substr( $constraint, 0, 1 );
145 $resdate = format_date_in_iso( $resdate ) if ( $resdate );
146 $resdate = C4::Dates->today( 'iso' ) unless ( $resdate );
148 $expdate = format_date_in_iso( $expdate );
150 undef $expdate; # make reserves.expirationdate default to null rather than '0000-00-00'
152 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
153 # Make room in reserves for this before those of a later reserve date
154 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
158 # If the reserv had the waiting status, we had the value of the resdate
159 if ( $found eq 'W' ) {
160 $waitingdate = $resdate;
164 # updates take place here
166 my $nextacctno = &getnextacctno( $borrowernumber );
168 INSERT INTO accountlines
169 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
171 (?,?,now(),?,?,'Res',?)
173 my $usth = $dbh->prepare($query);
174 $usth->execute( $borrowernumber, $nextacctno, $fee,
175 "Reserve Charge - $title", $fee );
181 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
182 priority,reservenotes,itemnumber,found,waitingdate,expirationdate)
187 my $sth = $dbh->prepare($query);
189 $borrowernumber, $biblionumber, $resdate, $branch,
190 $const, $priority, $notes, $checkitem,
191 $found, $waitingdate, $expdate
194 # Send e-mail to librarian if syspref is active
195 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
196 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
197 my $biblio = GetBiblioData($biblionumber);
198 my $letter = C4::Letters::getletter( 'reserves', 'HOLDPLACED');
199 my $branchcode = $borrower->{branchcode};
200 my $branch_details = C4::Branch::GetBranchDetail($branchcode);
201 my $admin_email_address =$branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
203 my %keys = (%$borrower, %$biblio);
204 foreach my $key (keys %keys) {
205 my $replacefield = "<<$key>>";
206 $letter->{content} =~ s/$replacefield/$keys{$key}/g;
207 $letter->{title} =~ s/$replacefield/$keys{$key}/g;
210 C4::Letters::EnqueueLetter(
212 borrowernumber => $borrowernumber,
213 message_transport_type => 'email',
214 from_address => $admin_email_address,
215 to_address => $admin_email_address,
224 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
226 INSERT INTO reserveconstraints
227 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
231 $sth = $dbh->prepare($query); # keep prepare outside the loop!
232 foreach (@$bibitems) {
233 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
236 return; # FIXME: why not have a useful return value?
239 =head2 GetReservesFromBiblionumber
241 ($count, $title_reserves) = &GetReserves($biblionumber);
243 This function gets the list of reservations for one C<$biblionumber>, returning a count
244 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
248 sub GetReservesFromBiblionumber {
249 my ($biblionumber) = shift or return (0, []);
250 my ($all_dates) = shift;
251 my $dbh = C4::Context->dbh;
253 # Find the desired items in the reserves
256 timestamp AS rtimestamp,
268 WHERE biblionumber = ? ";
269 unless ( $all_dates ) {
270 $query .= "AND reservedate <= CURRENT_DATE()";
272 $query .= "ORDER BY priority";
273 my $sth = $dbh->prepare($query);
274 $sth->execute($biblionumber);
277 while ( my $data = $sth->fetchrow_hashref ) {
279 # FIXME - What is this doing? How do constraints work?
280 if ($data->{constrainttype} eq 'o') {
282 SELECT biblioitemnumber
283 FROM reserveconstraints
284 WHERE biblionumber = ?
285 AND borrowernumber = ?
288 my $csth = $dbh->prepare($query);
289 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
291 while ( my $bibitemnos = $csth->fetchrow_array ) {
292 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
294 my $count = scalar @bibitemno;
296 # if we have two or more different specific itemtypes
297 # reserved by same person on same day
300 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
301 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
304 # Look up the book we just found.
305 $bdata = GetBiblioItemData( $bibitemno[0] );
307 # Add the results of this latest search to the current
309 # FIXME - An 'each' would probably be more efficient.
310 foreach my $key ( keys %$bdata ) {
311 $data->{$key} = $bdata->{$key};
314 push @results, $data;
316 return ( $#results + 1, \@results );
319 =head2 GetReservesFromItemnumber
321 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
323 TODO :: Description here
327 sub GetReservesFromItemnumber {
328 my ( $itemnumber, $all_dates ) = @_;
329 my $dbh = C4::Context->dbh;
331 SELECT reservedate,borrowernumber,branchcode
335 unless ( $all_dates ) {
336 $query .= " AND reservedate <= CURRENT_DATE()";
338 my $sth_res = $dbh->prepare($query);
339 $sth_res->execute($itemnumber);
340 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
341 return ( $reservedate, $borrowernumber, $branchcode );
344 =head2 GetReservesFromBorrowernumber
346 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
352 sub GetReservesFromBorrowernumber {
353 my ( $borrowernumber, $status ) = @_;
354 my $dbh = C4::Context->dbh;
357 $sth = $dbh->prepare("
360 WHERE borrowernumber=?
364 $sth->execute($borrowernumber,$status);
366 $sth = $dbh->prepare("
369 WHERE borrowernumber=?
372 $sth->execute($borrowernumber);
374 my $data = $sth->fetchall_arrayref({});
377 #-------------------------------------------------------------------------------------
378 =head2 CanBookBeReserved
380 $error = &CanBookBeReserved($borrowernumber, $biblionumber)
384 sub CanBookBeReserved{
385 my ($borrowernumber, $biblionumber) = @_;
387 my @items = GetItemsInfo($biblionumber);
389 #get items linked via host records
390 my $marcrecord= GetMarcBiblio($biblionumber);
391 my @hostitemInfos = GetHostItemsInfo($marcrecord);
393 push (@items,@hostitemInfos);
396 foreach my $item (@items){
397 return 1 if CanItemBeReserved($borrowernumber, $item->{itemnumber});
402 =head2 CanItemBeReserved
404 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
406 This function return 1 if an item can be issued by this borrower.
410 sub CanItemBeReserved{
411 my ($borrowernumber, $itemnumber) = @_;
413 my $dbh = C4::Context->dbh;
414 my $allowedreserves = 0;
416 my $controlbranch = C4::Context->preference('ReservesControlBranch');
417 my $itype = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
419 # we retrieve borrowers and items informations #
420 my $item = GetItem($itemnumber);
421 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
423 # we retrieve user rights on this itemtype and branchcode
424 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
426 WHERE (categorycode in (?,'*') )
427 AND (itemtype IN (?,'*'))
428 AND (branchcode IN (?,'*'))
435 my $querycount ="SELECT
438 LEFT JOIN items USING (itemnumber)
439 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
440 LEFT JOIN borrowers USING (borrowernumber)
441 WHERE borrowernumber = ?
445 my $itemtype = $item->{$itype};
446 my $categorycode = $borrower->{categorycode};
448 my $branchfield = "reserves.branchcode";
450 if( $controlbranch eq "ItemHomeLibrary" ){
451 $branchfield = "items.homebranch";
452 $branchcode = $item->{homebranch};
453 }elsif( $controlbranch eq "PatronLibrary" ){
454 $branchfield = "borrowers.branchcode";
455 $branchcode = $borrower->{branchcode};
459 $sth->execute($categorycode, $itemtype, $branchcode);
460 if(my $rights = $sth->fetchrow_hashref()){
461 $itemtype = $rights->{itemtype};
462 $allowedreserves = $rights->{reservesallowed};
469 $querycount .= "AND $branchfield = ?";
471 $querycount .= " AND $itype = ?" if ($itemtype ne "*");
472 my $sthcount = $dbh->prepare($querycount);
474 if($itemtype eq "*"){
475 $sthcount->execute($borrowernumber, $branchcode);
477 $sthcount->execute($borrowernumber, $branchcode, $itemtype);
480 my $reservecount = "0";
481 if(my $rowcount = $sthcount->fetchrow_hashref()){
482 $reservecount = $rowcount->{count};
485 # we check if it's ok or not
486 if( $reservecount < $allowedreserves ){
492 #--------------------------------------------------------------------------------
493 =head2 GetReserveCount
495 $number = &GetReserveCount($borrowernumber);
497 this function returns the number of reservation for a borrower given on input arg.
501 sub GetReserveCount {
502 my ($borrowernumber) = @_;
504 my $dbh = C4::Context->dbh;
507 SELECT COUNT(*) AS counter
509 WHERE borrowernumber = ?
511 my $sth = $dbh->prepare($query);
512 $sth->execute($borrowernumber);
513 my $row = $sth->fetchrow_hashref;
514 return $row->{counter};
517 =head2 GetOtherReserves
519 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
521 Check queued list of this document and check if this document must be transfered
525 sub GetOtherReserves {
526 my ($itemnumber) = @_;
529 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
530 if ($checkreserves) {
531 my $iteminfo = GetItem($itemnumber);
532 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
533 $messages->{'transfert'} = $checkreserves->{'branchcode'};
534 #minus priorities of others reservs
535 ModReserveMinusPriority(
537 $checkreserves->{'borrowernumber'},
538 $iteminfo->{'biblionumber'}
541 #launch the subroutine dotransfer
542 C4::Items::ModItemTransfer(
544 $iteminfo->{'holdingbranch'},
545 $checkreserves->{'branchcode'}
550 #step 2b : case of a reservation on the same branch, set the waiting status
552 $messages->{'waiting'} = 1;
553 ModReserveMinusPriority(
555 $checkreserves->{'borrowernumber'},
556 $iteminfo->{'biblionumber'}
558 ModReserveStatus($itemnumber,'W');
561 $nextreservinfo = $checkreserves->{'borrowernumber'};
564 return ( $messages, $nextreservinfo );
569 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
571 Calculate the fee for a reserve
576 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
579 my $dbh = C4::Context->dbh;
580 my $const = lc substr( $constraint, 0, 1 );
582 SELECT * FROM borrowers
583 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
584 WHERE borrowernumber = ?
586 my $sth = $dbh->prepare($query);
587 $sth->execute($borrowernumber);
588 my $data = $sth->fetchrow_hashref;
590 my $fee = $data->{'reservefee'};
591 my $cntitems = @- > $bibitems;
595 # check for items on issue
596 # first find biblioitem records
598 my $sth1 = $dbh->prepare(
599 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
600 WHERE (biblio.biblionumber = ?)"
602 $sth1->execute($biblionumber);
603 while ( my $data1 = $sth1->fetchrow_hashref ) {
604 if ( $const eq "a" ) {
605 push @biblioitems, $data1;
610 while ( $x < $cntitems ) {
611 if ( @$bibitems->{'biblioitemnumber'} ==
612 $data->{'biblioitemnumber'} )
618 if ( $const eq 'o' ) {
620 push @biblioitems, $data1;
625 push @biblioitems, $data1;
631 my $cntitemsfound = @biblioitems;
635 while ( $x < $cntitemsfound ) {
636 my $bitdata = $biblioitems[$x];
637 my $sth2 = $dbh->prepare(
639 WHERE biblioitemnumber = ?"
641 $sth2->execute( $bitdata->{'biblioitemnumber'} );
642 while ( my $itdata = $sth2->fetchrow_hashref ) {
643 my $sth3 = $dbh->prepare(
644 "SELECT * FROM issues
645 WHERE itemnumber = ?"
647 $sth3->execute( $itdata->{'itemnumber'} );
648 if ( my $isdata = $sth3->fetchrow_hashref ) {
656 if ( $allissued == 0 ) {
658 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
659 $rsth->execute($biblionumber);
660 if ( my $rdata = $rsth->fetchrow_hashref ) {
670 =head2 GetReservesToBranch
672 @transreserv = GetReservesToBranch( $frombranch );
674 Get reserve list for a given branch
678 sub GetReservesToBranch {
679 my ( $frombranch ) = @_;
680 my $dbh = C4::Context->dbh;
681 my $sth = $dbh->prepare(
682 "SELECT borrowernumber,reservedate,itemnumber,timestamp
687 $sth->execute( $frombranch );
690 while ( my $data = $sth->fetchrow_hashref ) {
691 $transreserv[$i] = $data;
694 return (@transreserv);
697 =head2 GetReservesForBranch
699 @transreserv = GetReservesForBranch($frombranch);
703 sub GetReservesForBranch {
704 my ($frombranch) = @_;
705 my $dbh = C4::Context->dbh;
706 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
711 $query .= " AND branchcode=? ";
713 $query .= "ORDER BY waitingdate" ;
714 my $sth = $dbh->prepare($query);
716 $sth->execute($frombranch);
723 while ( my $data = $sth->fetchrow_hashref ) {
724 $transreserv[$i] = $data;
727 return (@transreserv);
730 sub GetReserveStatus {
731 my ($itemnumber) = @_;
733 my $dbh = C4::Context->dbh;
735 my $itemstatus = $dbh->prepare("SELECT found FROM reserves WHERE itemnumber = ?");
737 $itemstatus->execute($itemnumber);
738 my ($found) = $itemstatus->fetchrow_array;
744 ($status, $reserve) = &CheckReserves($itemnumber);
745 ($status, $reserve) = &CheckReserves(undef, $barcode);
747 Find a book in the reserves.
749 C<$itemnumber> is the book's item number.
751 As I understand it, C<&CheckReserves> looks for the given item in the
752 reserves. If it is found, that's a match, and C<$status> is set to
755 Otherwise, it finds the most important item in the reserves with the
756 same biblio number as this book (I'm not clear on this) and returns it
757 with C<$status> set to C<Reserved>.
759 C<&CheckReserves> returns a two-element list:
761 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
763 C<$reserve> is the reserve item that matched. It is a
764 reference-to-hash whose keys are mostly the fields of the reserves
765 table in the Koha database.
770 my ( $item, $barcode ) = @_;
771 my $dbh = C4::Context->dbh;
774 if (C4::Context->preference('item-level_itypes')){
776 SELECT items.biblionumber,
777 items.biblioitemnumber,
778 itemtypes.notforloan,
779 items.notforloan AS itemnotforloan,
782 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
783 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
788 SELECT items.biblionumber,
789 items.biblioitemnumber,
790 itemtypes.notforloan,
791 items.notforloan AS itemnotforloan,
794 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
795 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
800 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
801 $sth->execute($item);
804 $sth = $dbh->prepare("$select WHERE barcode = ?");
805 $sth->execute($barcode);
807 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
808 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber ) = $sth->fetchrow_array;
810 return ( 0, 0 ) unless $itemnumber; # bail if we got nothing.
812 # if item is not for loan it cannot be reserved either.....
813 # execpt where items.notforloan < 0 : This indicates the item is holdable.
814 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
816 # Find this item in the reserves
817 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber );
819 # $priority and $highest are used to find the most important item
820 # in the list returned by &_Findgroupreserve. (The lower $priority,
821 # the more important the item.)
822 # $highest is the most important item we've seen so far.
824 if (scalar @reserves) {
825 my $priority = 10000000;
826 foreach my $res (@reserves) {
827 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
828 return ( "Waiting", $res ); # Found it
830 # See if this item is more important than what we've got so far
831 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
832 my $borrowerinfo=C4::Members::GetMemberDetails($res->{'borrowernumber'});
833 my $iteminfo=C4::Items::GetItem($itemnumber);
834 my $branch=C4::Circulation::_GetCircControlBranch($iteminfo,$borrowerinfo);
835 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
836 next if ($branchitemrule->{'holdallowed'} == 0);
837 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
838 $priority = $res->{'priority'};
845 # If we get this far, then no exact match was found.
846 # We return the most important (i.e. next) reservation.
848 $highest->{'itemnumber'} = $item;
849 return ( "Reserved", $highest );
856 =head2 CancelExpiredReserves
858 CancelExpiredReserves();
860 Cancels all reserves with an expiration date from before today.
864 sub CancelExpiredReserves {
866 my $dbh = C4::Context->dbh;
867 my $sth = $dbh->prepare( "
868 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
869 AND expirationdate IS NOT NULL
873 while ( my $res = $sth->fetchrow_hashref() ) {
874 CancelReserve( $res->{'biblionumber'}, '', $res->{'borrowernumber'} );
881 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
885 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
886 cancel, but not both: if both are given, C<&CancelReserve> does
889 C<$borrowernumber> is the borrower number of the patron on whose
890 behalf the book was reserved.
892 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
893 priorities of the other people who are waiting on the book.
898 my ( $biblio, $item, $borr ) = @_;
899 my $dbh = C4::Context->dbh;
900 if ( $item and $borr ) {
901 # removing a waiting reserve record....
902 # update the database...
905 SET cancellationdate = now(),
909 AND borrowernumber = ?
911 my $sth = $dbh->prepare($query);
912 $sth->execute( $item, $borr );
915 INSERT INTO old_reserves
916 SELECT * FROM reserves
918 AND borrowernumber = ?
920 $sth = $dbh->prepare($query);
921 $sth->execute( $item, $borr );
925 AND borrowernumber = ?
927 $sth = $dbh->prepare($query);
928 $sth->execute( $item, $borr );
931 # removing a reserve record....
932 # get the prioritiy on this record....
935 SELECT priority FROM reserves
936 WHERE biblionumber = ?
937 AND borrowernumber = ?
938 AND cancellationdate IS NULL
939 AND itemnumber IS NULL
941 my $sth = $dbh->prepare($query);
942 $sth->execute( $biblio, $borr );
943 ($priority) = $sth->fetchrow_array;
947 SET cancellationdate = now(),
950 WHERE biblionumber = ?
951 AND borrowernumber = ?
954 # update the database, removing the record...
955 $sth = $dbh->prepare($query);
956 $sth->execute( $biblio, $borr );
960 INSERT INTO old_reserves
961 SELECT * FROM reserves
962 WHERE biblionumber = ?
963 AND borrowernumber = ?
965 $sth = $dbh->prepare($query);
966 $sth->execute( $biblio, $borr );
970 WHERE biblionumber = ?
971 AND borrowernumber = ?
973 $sth = $dbh->prepare($query);
974 $sth->execute( $biblio, $borr );
976 # now fix the priority on the others....
977 _FixPriority( $biblio, $borr );
983 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
985 Change a hold request's priority or cancel it.
987 C<$rank> specifies the effect of the change. If C<$rank>
988 is 'W' or 'n', nothing happens. This corresponds to leaving a
989 request alone when changing its priority in the holds queue
992 If C<$rank> is 'del', the hold request is cancelled.
994 If C<$rank> is an integer greater than zero, the priority of
995 the request is set to that value. Since priority != 0 means
996 that the item is not waiting on the hold shelf, setting the
997 priority to a non-zero value also sets the request's found
998 status and waiting date to NULL.
1000 The optional C<$itemnumber> parameter is used only when
1001 C<$rank> is a non-zero integer; if supplied, the itemnumber
1002 of the hold request is set accordingly; if omitted, the itemnumber
1005 B<FIXME:> Note that the forgoing can have the effect of causing
1006 item-level hold requests to turn into title-level requests. This
1007 will be fixed once reserves has separate columns for requested
1008 itemnumber and supplying itemnumber.
1013 #subroutine to update a reserve
1014 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
1015 return if $rank eq "W";
1016 return if $rank eq "n";
1017 my $dbh = C4::Context->dbh;
1018 if ( $rank eq "del" ) {
1021 SET cancellationdate=now()
1022 WHERE biblionumber = ?
1023 AND borrowernumber = ?
1025 my $sth = $dbh->prepare($query);
1026 $sth->execute( $biblio, $borrower );
1029 INSERT INTO old_reserves
1032 WHERE biblionumber = ?
1033 AND borrowernumber = ?
1035 $sth = $dbh->prepare($query);
1036 $sth->execute( $biblio, $borrower );
1038 DELETE FROM reserves
1039 WHERE biblionumber = ?
1040 AND borrowernumber = ?
1042 $sth = $dbh->prepare($query);
1043 $sth->execute( $biblio, $borrower );
1046 elsif ($rank =~ /^\d+/ and $rank > 0) {
1048 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1049 WHERE biblionumber = ?
1050 AND borrowernumber = ?
1052 my $sth = $dbh->prepare($query);
1053 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
1055 _FixPriority( $biblio, $borrower, $rank);
1059 =head2 ModReserveFill
1061 &ModReserveFill($reserve);
1063 Fill a reserve. If I understand this correctly, this means that the
1064 reserved book has been found and given to the patron who reserved it.
1066 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1067 whose keys are fields from the reserves table in the Koha database.
1071 sub ModReserveFill {
1073 my $dbh = C4::Context->dbh;
1074 # fill in a reserve record....
1075 my $biblionumber = $res->{'biblionumber'};
1076 my $borrowernumber = $res->{'borrowernumber'};
1077 my $resdate = $res->{'reservedate'};
1079 # get the priority on this record....
1081 my $query = "SELECT priority
1083 WHERE biblionumber = ?
1084 AND borrowernumber = ?
1085 AND reservedate = ?";
1086 my $sth = $dbh->prepare($query);
1087 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1088 ($priority) = $sth->fetchrow_array;
1091 # update the database...
1092 $query = "UPDATE reserves
1095 WHERE biblionumber = ?
1097 AND borrowernumber = ?
1099 $sth = $dbh->prepare($query);
1100 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1103 # move to old_reserves
1104 $query = "INSERT INTO old_reserves
1105 SELECT * FROM reserves
1106 WHERE biblionumber = ?
1108 AND borrowernumber = ?
1110 $sth = $dbh->prepare($query);
1111 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1112 $query = "DELETE FROM reserves
1113 WHERE biblionumber = ?
1115 AND borrowernumber = ?
1117 $sth = $dbh->prepare($query);
1118 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1120 # now fix the priority on the others (if the priority wasn't
1121 # already sorted!)....
1122 unless ( $priority == 0 ) {
1123 _FixPriority( $biblionumber, $borrowernumber );
1127 =head2 ModReserveStatus
1129 &ModReserveStatus($itemnumber, $newstatus);
1131 Update the reserve status for the active (priority=0) reserve.
1133 $itemnumber is the itemnumber the reserve is on
1135 $newstatus is the new status.
1139 sub ModReserveStatus {
1141 #first : check if we have a reservation for this item .
1142 my ($itemnumber, $newstatus) = @_;
1143 my $dbh = C4::Context->dbh;
1144 my $query = " UPDATE reserves
1145 SET found=?,waitingdate = now()
1150 my $sth_set = $dbh->prepare($query);
1151 $sth_set->execute( $newstatus, $itemnumber );
1153 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1154 CartToShelf( $itemnumber );
1158 =head2 ModReserveAffect
1160 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1162 This function affect an item and a status for a given reserve
1163 The itemnumber parameter is used to find the biblionumber.
1164 with the biblionumber & the borrowernumber, we can affect the itemnumber
1165 to the correct reserve.
1167 if $transferToDo is not set, then the status is set to "Waiting" as well.
1168 otherwise, a transfer is on the way, and the end of the transfer will
1169 take care of the waiting status
1173 sub ModReserveAffect {
1174 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1175 my $dbh = C4::Context->dbh;
1177 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1178 # attached to $itemnumber
1179 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1180 $sth->execute($itemnumber);
1181 my ($biblionumber) = $sth->fetchrow;
1183 # get request - need to find out if item is already
1184 # waiting in order to not send duplicate hold filled notifications
1185 my $request = GetReserveInfo($borrowernumber, $biblionumber);
1186 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1188 # If we affect a reserve that has to be transfered, don't set to Waiting
1190 if ($transferToDo) {
1196 WHERE borrowernumber = ?
1197 AND biblionumber = ?
1201 # affect the reserve to Waiting as well.
1208 WHERE borrowernumber = ?
1209 AND biblionumber = ?
1212 $sth = $dbh->prepare($query);
1213 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1214 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1216 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1217 CartToShelf( $itemnumber );
1223 =head2 ModReserveCancelAll
1225 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1227 function to cancel reserv,check other reserves, and transfer document if it's necessary
1231 sub ModReserveCancelAll {
1234 my ( $itemnumber, $borrowernumber ) = @_;
1236 #step 1 : cancel the reservation
1237 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1239 #step 2 launch the subroutine of the others reserves
1240 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1242 return ( $messages, $nextreservinfo );
1245 =head2 ModReserveMinusPriority
1247 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1249 Reduce the values of queuded list
1253 sub ModReserveMinusPriority {
1254 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1256 #first step update the value of the first person on reserv
1257 my $dbh = C4::Context->dbh;
1260 SET priority = 0 , itemnumber = ?
1261 WHERE borrowernumber=?
1264 my $sth_upd = $dbh->prepare($query);
1265 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1266 # second step update all others reservs
1267 _FixPriority($biblionumber, $borrowernumber, '0');
1270 =head2 GetReserveInfo
1272 &GetReserveInfo($borrowernumber,$biblionumber);
1274 Get item and borrower details for a current hold.
1275 Current implementation this query should have a single result.
1279 sub GetReserveInfo {
1280 my ( $borrowernumber, $biblionumber ) = @_;
1281 my $dbh = C4::Context->dbh;
1285 reserves.borrowernumber,
1286 reserves.biblionumber,
1287 reserves.branchcode,
1288 reserves.waitingdate,
1304 items.holdingbranch,
1305 items.itemcallnumber,
1311 LEFT JOIN items USING(itemnumber)
1312 LEFT JOIN borrowers USING(borrowernumber)
1313 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1315 reserves.borrowernumber=?
1316 AND reserves.biblionumber=?";
1317 my $sth = $dbh->prepare($strsth);
1318 $sth->execute($borrowernumber,$biblionumber);
1320 my $data = $sth->fetchrow_hashref;
1325 =head2 IsAvailableForItemLevelRequest
1327 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1329 Checks whether a given item record is available for an
1330 item-level hold request. An item is available if
1332 * it is not lost AND
1333 * it is not damaged AND
1334 * it is not withdrawn AND
1335 * does not have a not for loan value > 0
1337 Whether or not the item is currently on loan is
1338 also checked - if the AllowOnShelfHolds system preference
1339 is ON, an item can be requested even if it is currently
1340 on loan to somebody else. If the system preference
1341 is OFF, an item that is currently checked out cannot
1342 be the target of an item-level hold request.
1344 Note that IsAvailableForItemLevelRequest() does not
1345 check if the staff operator is authorized to place
1346 a request on the item - in particular,
1347 this routine does not check IndependantBranches
1348 and canreservefromotherbranches.
1352 sub IsAvailableForItemLevelRequest {
1353 my $itemnumber = shift;
1355 my $item = GetItem($itemnumber);
1357 # must check the notforloan setting of the itemtype
1358 # FIXME - a lot of places in the code do this
1359 # or something similar - need to be
1361 my $dbh = C4::Context->dbh;
1362 my $notforloan_query;
1363 if (C4::Context->preference('item-level_itypes')) {
1364 $notforloan_query = "SELECT itemtypes.notforloan
1366 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1367 WHERE itemnumber = ?";
1369 $notforloan_query = "SELECT itemtypes.notforloan
1371 JOIN biblioitems USING (biblioitemnumber)
1372 JOIN itemtypes USING (itemtype)
1373 WHERE itemnumber = ?";
1375 my $sth = $dbh->prepare($notforloan_query);
1376 $sth->execute($itemnumber);
1377 my $notforloan_per_itemtype = 0;
1378 if (my ($notforloan) = $sth->fetchrow_array) {
1379 $notforloan_per_itemtype = 1 if $notforloan;
1382 my $available_per_item = 1;
1383 $available_per_item = 0 if $item->{itemlost} or
1384 ( $item->{notforloan} > 0 ) or
1385 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1386 $item->{wthdrawn} or
1387 $notforloan_per_itemtype;
1390 if (C4::Context->preference('AllowOnShelfHolds')) {
1391 return $available_per_item;
1393 return ($available_per_item and ($item->{onloan} or GetReserveStatus($itemnumber) eq "W"));
1397 =head2 AlterPriority
1399 AlterPriority( $where, $borrowernumber, $biblionumber, $reservedate );
1401 This function changes a reserve's priority up, down, to the top, or to the bottom.
1402 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1407 my ( $where, $borrowernumber, $biblionumber ) = @_;
1409 my $dbh = C4::Context->dbh;
1411 ## Find this reserve
1412 my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? AND cancellationdate IS NULL');
1413 $sth->execute( $biblionumber, $borrowernumber );
1414 my $reserve = $sth->fetchrow_hashref();
1417 if ( $where eq 'up' || $where eq 'down' ) {
1419 my $priority = $reserve->{'priority'};
1420 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1421 _FixPriority( $biblionumber, $borrowernumber, $priority )
1423 } elsif ( $where eq 'top' ) {
1425 _FixPriority( $biblionumber, $borrowernumber, '1' )
1427 } elsif ( $where eq 'bottom' ) {
1429 _FixPriority( $biblionumber, $borrowernumber, '999999' )
1434 =head2 ToggleLowestPriority
1436 ToggleLowestPriority( $borrowernumber, $biblionumber );
1438 This function sets the lowestPriority field to true if is false, and false if it is true.
1442 sub ToggleLowestPriority {
1443 my ( $borrowernumber, $biblionumber ) = @_;
1445 my $dbh = C4::Context->dbh;
1447 my $sth = $dbh->prepare(
1448 "UPDATE reserves SET lowestPriority = NOT lowestPriority
1449 WHERE biblionumber = ?
1450 AND borrowernumber = ?"
1458 _FixPriority( $biblionumber, $borrowernumber, '999999' );
1463 &_FixPriority($biblio,$borrowernumber,$rank,$ignoreSetLowestRank);
1465 Only used internally (so don't export it)
1466 Changed how this functions works #
1467 Now just gets an array of reserves in the rank order and updates them with
1468 the array index (+1 as array starts from 0)
1469 and if $rank is supplied will splice item from the array and splice it back in again
1470 in new priority rank
1475 my ( $biblio, $borrowernumber, $rank, $ignoreSetLowestRank ) = @_;
1476 my $dbh = C4::Context->dbh;
1477 if ( $rank eq "del" ) {
1478 CancelReserve( $biblio, undef, $borrowernumber );
1480 if ( $rank eq "W" || $rank eq "0" ) {
1482 # make sure priority for waiting or in-transit items is 0
1486 WHERE biblionumber = ?
1487 AND borrowernumber = ?
1488 AND found IN ('W', 'T')
1490 my $sth = $dbh->prepare($query);
1491 $sth->execute( $biblio, $borrowernumber );
1497 # FIXME adding a new security in returned elements for changing priority,
1498 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1499 # This is wrong a waiting reserve has W set
1500 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1502 SELECT borrowernumber, reservedate, constrainttype
1504 WHERE biblionumber = ?
1505 AND ((found <> 'W' AND found <> 'T') or found is NULL)
1506 ORDER BY priority ASC
1508 my $sth = $dbh->prepare($query);
1509 $sth->execute($biblio);
1510 while ( my $line = $sth->fetchrow_hashref ) {
1511 push( @reservedates, $line );
1512 push( @priority, $line );
1515 # To find the matching index
1517 my $key = -1; # to allow for 0 to be a valid result
1518 for ( $i = 0 ; $i < @priority ; $i++ ) {
1519 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1520 $key = $i; # save the index
1525 # if index exists in array then move it to new position
1526 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1527 my $new_rank = $rank -
1528 1; # $new_rank is what you want the new index to be in the array
1529 my $moving_item = splice( @priority, $key, 1 );
1530 splice( @priority, $new_rank, 0, $moving_item );
1533 # now fix the priority on those that are left....
1537 WHERE biblionumber = ?
1538 AND borrowernumber = ?
1542 $sth = $dbh->prepare($query);
1543 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1546 $priority[$j]->{'borrowernumber'},
1547 $priority[$j]->{'reservedate'}
1552 $sth = $dbh->prepare( "SELECT borrowernumber FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1555 unless ( $ignoreSetLowestRank ) {
1556 while ( my $res = $sth->fetchrow_hashref() ) {
1557 _FixPriority( $biblio, $res->{'borrowernumber'}, '999999', 1 );
1562 =head2 _Findgroupreserve
1564 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1566 Looks for an item-specific match first, then for a title-level match, returning the
1567 first match found. If neither, then we look for a 3rd kind of match based on
1568 reserve constraints.
1570 TODO: add more explanation about reserve constraints
1572 C<&_Findgroupreserve> returns :
1573 C<@results> is an array of references-to-hash whose keys are mostly
1574 fields from the reserves table of the Koha database, plus
1575 C<biblioitemnumber>.
1579 sub _Findgroupreserve {
1580 my ( $bibitem, $biblio, $itemnumber ) = @_;
1581 my $dbh = C4::Context->dbh;
1583 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1584 # check for exact targetted match
1585 my $item_level_target_query = qq/
1586 SELECT reserves.biblionumber AS biblionumber,
1587 reserves.borrowernumber AS borrowernumber,
1588 reserves.reservedate AS reservedate,
1589 reserves.branchcode AS branchcode,
1590 reserves.cancellationdate AS cancellationdate,
1591 reserves.found AS found,
1592 reserves.reservenotes AS reservenotes,
1593 reserves.priority AS priority,
1594 reserves.timestamp AS timestamp,
1595 biblioitems.biblioitemnumber AS biblioitemnumber,
1596 reserves.itemnumber AS itemnumber
1598 JOIN biblioitems USING (biblionumber)
1599 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1602 AND item_level_request = 1
1604 AND reservedate <= CURRENT_DATE()
1606 my $sth = $dbh->prepare($item_level_target_query);
1607 $sth->execute($itemnumber);
1609 if ( my $data = $sth->fetchrow_hashref ) {
1610 push( @results, $data );
1612 return @results if @results;
1614 # check for title-level targetted match
1615 my $title_level_target_query = qq/
1616 SELECT reserves.biblionumber AS biblionumber,
1617 reserves.borrowernumber AS borrowernumber,
1618 reserves.reservedate AS reservedate,
1619 reserves.branchcode AS branchcode,
1620 reserves.cancellationdate AS cancellationdate,
1621 reserves.found AS found,
1622 reserves.reservenotes AS reservenotes,
1623 reserves.priority AS priority,
1624 reserves.timestamp AS timestamp,
1625 biblioitems.biblioitemnumber AS biblioitemnumber,
1626 reserves.itemnumber AS itemnumber
1628 JOIN biblioitems USING (biblionumber)
1629 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1632 AND item_level_request = 0
1633 AND hold_fill_targets.itemnumber = ?
1634 AND reservedate <= CURRENT_DATE()
1636 $sth = $dbh->prepare($title_level_target_query);
1637 $sth->execute($itemnumber);
1639 if ( my $data = $sth->fetchrow_hashref ) {
1640 push( @results, $data );
1642 return @results if @results;
1645 SELECT reserves.biblionumber AS biblionumber,
1646 reserves.borrowernumber AS borrowernumber,
1647 reserves.reservedate AS reservedate,
1648 reserves.waitingdate AS waitingdate,
1649 reserves.branchcode AS branchcode,
1650 reserves.cancellationdate AS cancellationdate,
1651 reserves.found AS found,
1652 reserves.reservenotes AS reservenotes,
1653 reserves.priority AS priority,
1654 reserves.timestamp AS timestamp,
1655 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1656 reserves.itemnumber AS itemnumber
1658 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1659 WHERE reserves.biblionumber = ?
1660 AND ( ( reserveconstraints.biblioitemnumber = ?
1661 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1662 AND reserves.reservedate = reserveconstraints.reservedate )
1663 OR reserves.constrainttype='a' )
1664 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1665 AND reserves.reservedate <= CURRENT_DATE()
1667 $sth = $dbh->prepare($query);
1668 $sth->execute( $biblio, $bibitem, $itemnumber );
1670 while ( my $data = $sth->fetchrow_hashref ) {
1671 push( @results, $data );
1676 =head2 _koha_notify_reserve
1678 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1680 Sends a notification to the patron that their hold has been filled (through
1681 ModReserveAffect, _not_ ModReserveFill)
1685 sub _koha_notify_reserve {
1686 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1688 my $dbh = C4::Context->dbh;
1689 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
1691 # Try to get the borrower's email address
1693 my $which_address = C4::Context->preference('AutoEmailPrimaryAddress');
1694 # If the system preference is set to 'first valid' (value == OFF), look up email address
1695 if ($which_address eq 'OFF') {
1696 $to_address = C4::Members::GetFirstValidEmailAddress( $borrowernumber );
1698 $to_address = $borrower->{$which_address};
1704 if ( $to_address || $borrower->{'smsalertnumber'} ) {
1705 $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold_Filled' } );
1707 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1708 $letter_code = $messagingprefs->{'letter_code'};
1710 $letter_code = 'HOLD_PRINT';
1714 my $sth = $dbh->prepare("
1717 WHERE borrowernumber = ?
1718 AND biblionumber = ?
1720 $sth->execute( $borrowernumber, $biblionumber );
1721 my $reserve = $sth->fetchrow_hashref;
1722 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1724 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1726 my $letter = getletter( 'reserves', $letter_code );
1727 die "Could not find a letter called '$letter_code' in the 'reserves' module" unless( $letter );
1729 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1730 C4::Letters::parseletter( $letter, 'borrowers', $borrowernumber );
1731 C4::Letters::parseletter( $letter, 'biblio', $biblionumber );
1732 C4::Letters::parseletter( $letter, 'reserves', $borrowernumber, $biblionumber );
1734 if ( $reserve->{'itemnumber'} ) {
1735 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1737 my $today = C4::Dates->new()->output();
1738 $letter->{'title'} =~ s/<<today>>/$today/g;
1739 $letter->{'content'} =~ s/<<today>>/$today/g;
1740 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1742 if ( $print_mode ) {
1743 C4::Letters::EnqueueLetter( {
1745 borrowernumber => $borrowernumber,
1746 message_transport_type => 'print',
1752 if ( grep { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1753 # aka, 'email' in ->{'transports'}
1754 C4::Letters::EnqueueLetter(
1755 { letter => $letter,
1756 borrowernumber => $borrowernumber,
1757 message_transport_type => 'email',
1758 from_address => $admin_email_address,
1763 if ( grep { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1764 C4::Letters::EnqueueLetter(
1765 { letter => $letter,
1766 borrowernumber => $borrowernumber,
1767 message_transport_type => 'sms',
1773 =head2 _ShiftPriorityByDateAndPriority
1775 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1777 This increments the priority of all reserves after the one
1778 with either the lowest date after C<$reservedate>
1779 or the lowest priority after C<$priority>.
1781 It effectively makes room for a new reserve to be inserted with a certain
1782 priority, which is returned.
1784 This is most useful when the reservedate can be set by the user. It allows
1785 the new reserve to be placed before other reserves that have a later
1786 reservedate. Since priority also is set by the form in reserves/request.pl
1787 the sub accounts for that too.
1791 sub _ShiftPriorityByDateAndPriority {
1792 my ( $biblio, $resdate, $new_priority ) = @_;
1794 my $dbh = C4::Context->dbh;
1795 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1796 my $sth = $dbh->prepare( $query );
1797 $sth->execute( $biblio, $resdate, $new_priority );
1798 my $min_priority = $sth->fetchrow;
1799 # if no such matches are found, $new_priority remains as original value
1800 $new_priority = $min_priority if ( $min_priority );
1802 # Shift the priority up by one; works in conjunction with the next SQL statement
1803 $query = "UPDATE reserves
1804 SET priority = priority+1
1805 WHERE biblionumber = ?
1806 AND borrowernumber = ?
1809 my $sth_update = $dbh->prepare( $query );
1811 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1812 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1813 $sth = $dbh->prepare( $query );
1814 $sth->execute( $new_priority, $biblio );
1815 while ( my $row = $sth->fetchrow_hashref ) {
1816 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
1819 return $new_priority; # so the caller knows what priority they wind up receiving
1824 MergeHolds($dbh,$to_biblio, $from_biblio);
1826 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
1831 my ( $dbh, $to_biblio, $from_biblio ) = @_;
1832 my $sth = $dbh->prepare(
1833 "SELECT count(*) as reservenumber FROM reserves WHERE biblionumber = ?"
1835 $sth->execute($from_biblio);
1836 if ( my $data = $sth->fetchrow_hashref() ) {
1838 # holds exist on old record, if not we don't need to do anything
1839 $sth = $dbh->prepare(
1840 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
1841 $sth->execute( $to_biblio, $from_biblio );
1844 # don't reorder those already waiting
1846 $sth = $dbh->prepare(
1847 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
1849 my $upd_sth = $dbh->prepare(
1850 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
1851 AND reservedate = ? AND constrainttype = ? AND (itemnumber = ? or itemnumber is NULL) "
1853 $sth->execute( $to_biblio, 'W', 'T' );
1855 while ( my $reserve = $sth->fetchrow_hashref() ) {
1857 $priority, $to_biblio,
1858 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
1859 $reserve->{'constrainttype'}, $reserve->{'itemnumber'}
1869 Koha Development Team <http://koha-community.org/>