3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007-2010 BibLibre Paul POULAIN
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #use warnings; FIXME - Bug 2505
33 # for _koha_notify_reserve
34 use C4::Members::Messaging;
37 use C4::Branch qw( GetBranchDetail );
38 use C4::Dates qw( format_date_in_iso );
39 use List::MoreUtils qw( firstidx );
41 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
45 C4::Reserves - Koha functions for dealing with reservation.
53 This modules provides somes functions to deal with reservations.
55 Reserves are stored in reserves table.
56 The following columns contains important values :
57 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
58 =0 : then the reserve is being dealed
59 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
60 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
61 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
62 F(inished) : the reserve has been completed, and is done
63 - itemnumber : empty : the reserve is still unaffected to an item
64 filled: the reserve is attached to an item
65 The complete workflow is :
66 ==== 1st use case ====
67 patron request a document, 1st available : P >0, F=NULL, I=NULL
68 a library having it run "transfertodo", and clic on the list
69 if there is no transfer to do, the reserve waiting
70 patron can pick it up P =0, F=W, I=filled
71 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
72 The pickup library recieve the book, it check in P =0, F=W, I=filled
73 The patron borrow the book P =0, F=F, I=filled
75 ==== 2nd use case ====
76 patron requests a document, a given item,
77 If pickup is holding branch P =0, F=W, I=filled
78 If transfer needed, write in branchtransfer P =0, F=T, I=filled
79 The pickup library receive the book, it checks it in P =0, F=W, I=filled
80 The patron borrow the book P =0, F=F, I=filled
87 # set the version for version checking
94 &GetReservesFromItemnumber
95 &GetReservesFromBiblionumber
96 &GetReservesFromBorrowernumber
111 &ModReserveMinusPriority
118 &CancelExpiredReserves
120 &IsAvailableForItemLevelRequest
123 &ToggleLowestPriority
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 = get_itemnumbers_of($biblionumber);
388 #get items linked via host records
389 my @hostitems = get_hostitemnumbers_of($biblionumber);
391 push (@items,@hostitems);
394 foreach my $item (@items){
395 return 1 if CanItemBeReserved($borrowernumber, $item);
400 =head2 CanItemBeReserved
402 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
404 This function return 1 if an item can be issued by this borrower.
408 sub CanItemBeReserved{
409 my ($borrowernumber, $itemnumber) = @_;
411 my $dbh = C4::Context->dbh;
412 my $allowedreserves = 0;
414 my $controlbranch = C4::Context->preference('ReservesControlBranch');
415 my $itype = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
417 # we retrieve borrowers and items informations #
418 my $item = GetItem($itemnumber);
419 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
421 # we retrieve user rights on this itemtype and branchcode
422 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
424 WHERE (categorycode in (?,'*') )
425 AND (itemtype IN (?,'*'))
426 AND (branchcode IN (?,'*'))
433 my $querycount ="SELECT
436 LEFT JOIN items USING (itemnumber)
437 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
438 LEFT JOIN borrowers USING (borrowernumber)
439 WHERE borrowernumber = ?
443 my $itemtype = $item->{$itype};
444 my $categorycode = $borrower->{categorycode};
446 my $branchfield = "reserves.branchcode";
448 if( $controlbranch eq "ItemHomeLibrary" ){
449 $branchfield = "items.homebranch";
450 $branchcode = $item->{homebranch};
451 }elsif( $controlbranch eq "PatronLibrary" ){
452 $branchfield = "borrowers.branchcode";
453 $branchcode = $borrower->{branchcode};
457 $sth->execute($categorycode, $itemtype, $branchcode);
458 if(my $rights = $sth->fetchrow_hashref()){
459 $itemtype = $rights->{itemtype};
460 $allowedreserves = $rights->{reservesallowed};
467 $querycount .= "AND $branchfield = ?";
469 $querycount .= " AND $itype = ?" if ($itemtype ne "*");
470 my $sthcount = $dbh->prepare($querycount);
472 if($itemtype eq "*"){
473 $sthcount->execute($borrowernumber, $branchcode);
475 $sthcount->execute($borrowernumber, $branchcode, $itemtype);
478 my $reservecount = "0";
479 if(my $rowcount = $sthcount->fetchrow_hashref()){
480 $reservecount = $rowcount->{count};
483 # we check if it's ok or not
484 if( $reservecount < $allowedreserves ){
490 #--------------------------------------------------------------------------------
491 =head2 GetReserveCount
493 $number = &GetReserveCount($borrowernumber);
495 this function returns the number of reservation for a borrower given on input arg.
499 sub GetReserveCount {
500 my ($borrowernumber) = @_;
502 my $dbh = C4::Context->dbh;
505 SELECT COUNT(*) AS counter
507 WHERE borrowernumber = ?
509 my $sth = $dbh->prepare($query);
510 $sth->execute($borrowernumber);
511 my $row = $sth->fetchrow_hashref;
512 return $row->{counter};
515 =head2 GetOtherReserves
517 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
519 Check queued list of this document and check if this document must be transfered
523 sub GetOtherReserves {
524 my ($itemnumber) = @_;
527 my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
528 if ($checkreserves) {
529 my $iteminfo = GetItem($itemnumber);
530 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
531 $messages->{'transfert'} = $checkreserves->{'branchcode'};
532 #minus priorities of others reservs
533 ModReserveMinusPriority(
535 $checkreserves->{'borrowernumber'},
536 $iteminfo->{'biblionumber'}
539 #launch the subroutine dotransfer
540 C4::Items::ModItemTransfer(
542 $iteminfo->{'holdingbranch'},
543 $checkreserves->{'branchcode'}
548 #step 2b : case of a reservation on the same branch, set the waiting status
550 $messages->{'waiting'} = 1;
551 ModReserveMinusPriority(
553 $checkreserves->{'borrowernumber'},
554 $iteminfo->{'biblionumber'}
556 ModReserveStatus($itemnumber,'W');
559 $nextreservinfo = $checkreserves->{'borrowernumber'};
562 return ( $messages, $nextreservinfo );
567 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
569 Calculate the fee for a reserve
574 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
577 my $dbh = C4::Context->dbh;
578 my $const = lc substr( $constraint, 0, 1 );
580 SELECT * FROM borrowers
581 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
582 WHERE borrowernumber = ?
584 my $sth = $dbh->prepare($query);
585 $sth->execute($borrowernumber);
586 my $data = $sth->fetchrow_hashref;
588 my $fee = $data->{'reservefee'};
589 my $cntitems = @- > $bibitems;
593 # check for items on issue
594 # first find biblioitem records
596 my $sth1 = $dbh->prepare(
597 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
598 WHERE (biblio.biblionumber = ?)"
600 $sth1->execute($biblionumber);
601 while ( my $data1 = $sth1->fetchrow_hashref ) {
602 if ( $const eq "a" ) {
603 push @biblioitems, $data1;
608 while ( $x < $cntitems ) {
609 if ( @$bibitems->{'biblioitemnumber'} ==
610 $data->{'biblioitemnumber'} )
616 if ( $const eq 'o' ) {
618 push @biblioitems, $data1;
623 push @biblioitems, $data1;
629 my $cntitemsfound = @biblioitems;
633 while ( $x < $cntitemsfound ) {
634 my $bitdata = $biblioitems[$x];
635 my $sth2 = $dbh->prepare(
637 WHERE biblioitemnumber = ?"
639 $sth2->execute( $bitdata->{'biblioitemnumber'} );
640 while ( my $itdata = $sth2->fetchrow_hashref ) {
641 my $sth3 = $dbh->prepare(
642 "SELECT * FROM issues
643 WHERE itemnumber = ?"
645 $sth3->execute( $itdata->{'itemnumber'} );
646 if ( my $isdata = $sth3->fetchrow_hashref ) {
654 if ( $allissued == 0 ) {
656 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
657 $rsth->execute($biblionumber);
658 if ( my $rdata = $rsth->fetchrow_hashref ) {
668 =head2 GetReservesToBranch
670 @transreserv = GetReservesToBranch( $frombranch );
672 Get reserve list for a given branch
676 sub GetReservesToBranch {
677 my ( $frombranch ) = @_;
678 my $dbh = C4::Context->dbh;
679 my $sth = $dbh->prepare(
680 "SELECT borrowernumber,reservedate,itemnumber,timestamp
685 $sth->execute( $frombranch );
688 while ( my $data = $sth->fetchrow_hashref ) {
689 $transreserv[$i] = $data;
692 return (@transreserv);
695 =head2 GetReservesForBranch
697 @transreserv = GetReservesForBranch($frombranch);
701 sub GetReservesForBranch {
702 my ($frombranch) = @_;
703 my $dbh = C4::Context->dbh;
704 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
709 $query .= " AND branchcode=? ";
711 $query .= "ORDER BY waitingdate" ;
712 my $sth = $dbh->prepare($query);
714 $sth->execute($frombranch);
721 while ( my $data = $sth->fetchrow_hashref ) {
722 $transreserv[$i] = $data;
725 return (@transreserv);
728 sub GetReserveStatus {
729 my ($itemnumber) = @_;
731 my $dbh = C4::Context->dbh;
733 my $itemstatus = $dbh->prepare("SELECT found FROM reserves WHERE itemnumber = ?");
735 $itemstatus->execute($itemnumber);
736 my ($found) = $itemstatus->fetchrow_array;
742 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
743 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
745 Find a book in the reserves.
747 C<$itemnumber> is the book's item number.
749 As I understand it, C<&CheckReserves> looks for the given item in the
750 reserves. If it is found, that's a match, and C<$status> is set to
753 Otherwise, it finds the most important item in the reserves with the
754 same biblio number as this book (I'm not clear on this) and returns it
755 with C<$status> set to C<Reserved>.
757 C<&CheckReserves> returns a two-element list:
759 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
761 C<$reserve> is the reserve item that matched. It is a
762 reference-to-hash whose keys are mostly the fields of the reserves
763 table in the Koha database.
768 my ( $item, $barcode ) = @_;
769 my $dbh = C4::Context->dbh;
772 if (C4::Context->preference('item-level_itypes')){
774 SELECT items.biblionumber,
775 items.biblioitemnumber,
776 itemtypes.notforloan,
777 items.notforloan AS itemnotforloan,
780 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
781 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
786 SELECT items.biblionumber,
787 items.biblioitemnumber,
788 itemtypes.notforloan,
789 items.notforloan AS itemnotforloan,
792 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
793 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
798 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
799 $sth->execute($item);
802 $sth = $dbh->prepare("$select WHERE barcode = ?");
803 $sth->execute($barcode);
805 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
806 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber ) = $sth->fetchrow_array;
808 return ( '' ) unless $itemnumber; # bail if we got nothing.
810 # if item is not for loan it cannot be reserved either.....
811 # execpt where items.notforloan < 0 : This indicates the item is holdable.
812 return ( '' ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
814 # Find this item in the reserves
815 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber );
817 # $priority and $highest are used to find the most important item
818 # in the list returned by &_Findgroupreserve. (The lower $priority,
819 # the more important the item.)
820 # $highest is the most important item we've seen so far.
822 if (scalar @reserves) {
823 my $priority = 10000000;
824 foreach my $res (@reserves) {
825 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
826 return ( "Waiting", $res, \@reserves ); # Found it
828 # See if this item is more important than what we've got so far
829 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
830 my $borrowerinfo=C4::Members::GetMember(borrowernumber => $res->{'borrowernumber'});
831 my $iteminfo=C4::Items::GetItem($itemnumber);
832 my $branch=C4::Circulation::_GetCircControlBranch($iteminfo,$borrowerinfo);
833 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
834 next if ($branchitemrule->{'holdallowed'} == 0);
835 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
836 $priority = $res->{'priority'};
843 # If we get this far, then no exact match was found.
844 # We return the most important (i.e. next) reservation.
846 $highest->{'itemnumber'} = $item;
847 return ( "Reserved", $highest, \@reserves );
853 =head2 CancelExpiredReserves
855 CancelExpiredReserves();
857 Cancels all reserves with an expiration date from before today.
861 sub CancelExpiredReserves {
863 my $dbh = C4::Context->dbh;
864 my $sth = $dbh->prepare( "
865 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
866 AND expirationdate IS NOT NULL
870 while ( my $res = $sth->fetchrow_hashref() ) {
871 CancelReserve( $res->{'biblionumber'}, '', $res->{'borrowernumber'} );
878 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
882 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
883 cancel, but not both: if both are given, C<&CancelReserve> does
886 C<$borrowernumber> is the borrower number of the patron on whose
887 behalf the book was reserved.
889 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
890 priorities of the other people who are waiting on the book.
895 my ( $biblio, $item, $borr ) = @_;
896 my $dbh = C4::Context->dbh;
897 if ( $item and $borr ) {
898 # removing a waiting reserve record....
899 # update the database...
902 SET cancellationdate = now(),
906 AND borrowernumber = ?
908 my $sth = $dbh->prepare($query);
909 $sth->execute( $item, $borr );
912 INSERT INTO old_reserves
913 SELECT * FROM reserves
915 AND borrowernumber = ?
917 $sth = $dbh->prepare($query);
918 $sth->execute( $item, $borr );
922 AND borrowernumber = ?
924 $sth = $dbh->prepare($query);
925 $sth->execute( $item, $borr );
928 # removing a reserve record....
929 # get the prioritiy on this record....
932 SELECT priority FROM reserves
933 WHERE biblionumber = ?
934 AND borrowernumber = ?
935 AND cancellationdate IS NULL
936 AND itemnumber IS NULL
938 my $sth = $dbh->prepare($query);
939 $sth->execute( $biblio, $borr );
940 ($priority) = $sth->fetchrow_array;
944 SET cancellationdate = now(),
947 WHERE biblionumber = ?
948 AND borrowernumber = ?
951 # update the database, removing the record...
952 $sth = $dbh->prepare($query);
953 $sth->execute( $biblio, $borr );
957 INSERT INTO old_reserves
958 SELECT * FROM reserves
959 WHERE biblionumber = ?
960 AND borrowernumber = ?
962 $sth = $dbh->prepare($query);
963 $sth->execute( $biblio, $borr );
967 WHERE biblionumber = ?
968 AND borrowernumber = ?
970 $sth = $dbh->prepare($query);
971 $sth->execute( $biblio, $borr );
973 # now fix the priority on the others....
974 _FixPriority( $biblio, $borr );
980 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
982 Change a hold request's priority or cancel it.
984 C<$rank> specifies the effect of the change. If C<$rank>
985 is 'W' or 'n', nothing happens. This corresponds to leaving a
986 request alone when changing its priority in the holds queue
989 If C<$rank> is 'del', the hold request is cancelled.
991 If C<$rank> is an integer greater than zero, the priority of
992 the request is set to that value. Since priority != 0 means
993 that the item is not waiting on the hold shelf, setting the
994 priority to a non-zero value also sets the request's found
995 status and waiting date to NULL.
997 The optional C<$itemnumber> parameter is used only when
998 C<$rank> is a non-zero integer; if supplied, the itemnumber
999 of the hold request is set accordingly; if omitted, the itemnumber
1002 B<FIXME:> Note that the forgoing can have the effect of causing
1003 item-level hold requests to turn into title-level requests. This
1004 will be fixed once reserves has separate columns for requested
1005 itemnumber and supplying itemnumber.
1010 #subroutine to update a reserve
1011 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
1012 return if $rank eq "W";
1013 return if $rank eq "n";
1014 my $dbh = C4::Context->dbh;
1015 if ( $rank eq "del" ) {
1018 SET cancellationdate=now()
1019 WHERE biblionumber = ?
1020 AND borrowernumber = ?
1022 my $sth = $dbh->prepare($query);
1023 $sth->execute( $biblio, $borrower );
1026 INSERT INTO old_reserves
1029 WHERE biblionumber = ?
1030 AND borrowernumber = ?
1032 $sth = $dbh->prepare($query);
1033 $sth->execute( $biblio, $borrower );
1035 DELETE FROM reserves
1036 WHERE biblionumber = ?
1037 AND borrowernumber = ?
1039 $sth = $dbh->prepare($query);
1040 $sth->execute( $biblio, $borrower );
1043 elsif ($rank =~ /^\d+/ and $rank > 0) {
1045 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1046 WHERE biblionumber = ?
1047 AND borrowernumber = ?
1049 my $sth = $dbh->prepare($query);
1050 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
1052 _FixPriority( $biblio, $borrower, $rank);
1056 =head2 ModReserveFill
1058 &ModReserveFill($reserve);
1060 Fill a reserve. If I understand this correctly, this means that the
1061 reserved book has been found and given to the patron who reserved it.
1063 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1064 whose keys are fields from the reserves table in the Koha database.
1068 sub ModReserveFill {
1070 my $dbh = C4::Context->dbh;
1071 # fill in a reserve record....
1072 my $biblionumber = $res->{'biblionumber'};
1073 my $borrowernumber = $res->{'borrowernumber'};
1074 my $resdate = $res->{'reservedate'};
1076 # get the priority on this record....
1078 my $query = "SELECT priority
1080 WHERE biblionumber = ?
1081 AND borrowernumber = ?
1082 AND reservedate = ?";
1083 my $sth = $dbh->prepare($query);
1084 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1085 ($priority) = $sth->fetchrow_array;
1088 # update the database...
1089 $query = "UPDATE reserves
1092 WHERE biblionumber = ?
1094 AND borrowernumber = ?
1096 $sth = $dbh->prepare($query);
1097 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1100 # move to old_reserves
1101 $query = "INSERT INTO old_reserves
1102 SELECT * FROM reserves
1103 WHERE biblionumber = ?
1105 AND borrowernumber = ?
1107 $sth = $dbh->prepare($query);
1108 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1109 $query = "DELETE FROM reserves
1110 WHERE biblionumber = ?
1112 AND borrowernumber = ?
1114 $sth = $dbh->prepare($query);
1115 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1117 # now fix the priority on the others (if the priority wasn't
1118 # already sorted!)....
1119 unless ( $priority == 0 ) {
1120 _FixPriority( $biblionumber, $borrowernumber );
1124 =head2 ModReserveStatus
1126 &ModReserveStatus($itemnumber, $newstatus);
1128 Update the reserve status for the active (priority=0) reserve.
1130 $itemnumber is the itemnumber the reserve is on
1132 $newstatus is the new status.
1136 sub ModReserveStatus {
1138 #first : check if we have a reservation for this item .
1139 my ($itemnumber, $newstatus) = @_;
1140 my $dbh = C4::Context->dbh;
1141 my $query = " UPDATE reserves
1142 SET found=?,waitingdate = now()
1147 my $sth_set = $dbh->prepare($query);
1148 $sth_set->execute( $newstatus, $itemnumber );
1150 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1151 CartToShelf( $itemnumber );
1155 =head2 ModReserveAffect
1157 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1159 This function affect an item and a status for a given reserve
1160 The itemnumber parameter is used to find the biblionumber.
1161 with the biblionumber & the borrowernumber, we can affect the itemnumber
1162 to the correct reserve.
1164 if $transferToDo is not set, then the status is set to "Waiting" as well.
1165 otherwise, a transfer is on the way, and the end of the transfer will
1166 take care of the waiting status
1170 sub ModReserveAffect {
1171 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1172 my $dbh = C4::Context->dbh;
1174 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1175 # attached to $itemnumber
1176 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1177 $sth->execute($itemnumber);
1178 my ($biblionumber) = $sth->fetchrow;
1180 # get request - need to find out if item is already
1181 # waiting in order to not send duplicate hold filled notifications
1182 my $request = GetReserveInfo($borrowernumber, $biblionumber);
1183 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1185 # If we affect a reserve that has to be transfered, don't set to Waiting
1187 if ($transferToDo) {
1193 WHERE borrowernumber = ?
1194 AND biblionumber = ?
1198 # affect the reserve to Waiting as well.
1205 WHERE borrowernumber = ?
1206 AND biblionumber = ?
1209 $sth = $dbh->prepare($query);
1210 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1211 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1213 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1214 CartToShelf( $itemnumber );
1220 =head2 ModReserveCancelAll
1222 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1224 function to cancel reserv,check other reserves, and transfer document if it's necessary
1228 sub ModReserveCancelAll {
1231 my ( $itemnumber, $borrowernumber ) = @_;
1233 #step 1 : cancel the reservation
1234 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1236 #step 2 launch the subroutine of the others reserves
1237 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1239 return ( $messages, $nextreservinfo );
1242 =head2 ModReserveMinusPriority
1244 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1246 Reduce the values of queuded list
1250 sub ModReserveMinusPriority {
1251 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1253 #first step update the value of the first person on reserv
1254 my $dbh = C4::Context->dbh;
1257 SET priority = 0 , itemnumber = ?
1258 WHERE borrowernumber=?
1261 my $sth_upd = $dbh->prepare($query);
1262 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1263 # second step update all others reservs
1264 _FixPriority($biblionumber, $borrowernumber, '0');
1267 =head2 GetReserveInfo
1269 &GetReserveInfo($borrowernumber,$biblionumber);
1271 Get item and borrower details for a current hold.
1272 Current implementation this query should have a single result.
1276 sub GetReserveInfo {
1277 my ( $borrowernumber, $biblionumber ) = @_;
1278 my $dbh = C4::Context->dbh;
1282 reserves.borrowernumber,
1283 reserves.biblionumber,
1284 reserves.branchcode,
1285 reserves.waitingdate,
1301 items.holdingbranch,
1302 items.itemcallnumber,
1308 LEFT JOIN items USING(itemnumber)
1309 LEFT JOIN borrowers USING(borrowernumber)
1310 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1312 reserves.borrowernumber=?
1313 AND reserves.biblionumber=?";
1314 my $sth = $dbh->prepare($strsth);
1315 $sth->execute($borrowernumber,$biblionumber);
1317 my $data = $sth->fetchrow_hashref;
1322 =head2 IsAvailableForItemLevelRequest
1324 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1326 Checks whether a given item record is available for an
1327 item-level hold request. An item is available if
1329 * it is not lost AND
1330 * it is not damaged AND
1331 * it is not withdrawn AND
1332 * does not have a not for loan value > 0
1334 Whether or not the item is currently on loan is
1335 also checked - if the AllowOnShelfHolds system preference
1336 is ON, an item can be requested even if it is currently
1337 on loan to somebody else. If the system preference
1338 is OFF, an item that is currently checked out cannot
1339 be the target of an item-level hold request.
1341 Note that IsAvailableForItemLevelRequest() does not
1342 check if the staff operator is authorized to place
1343 a request on the item - in particular,
1344 this routine does not check IndependantBranches
1345 and canreservefromotherbranches.
1349 sub IsAvailableForItemLevelRequest {
1350 my $itemnumber = shift;
1352 my $item = GetItem($itemnumber);
1354 # must check the notforloan setting of the itemtype
1355 # FIXME - a lot of places in the code do this
1356 # or something similar - need to be
1358 my $dbh = C4::Context->dbh;
1359 my $notforloan_query;
1360 if (C4::Context->preference('item-level_itypes')) {
1361 $notforloan_query = "SELECT itemtypes.notforloan
1363 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1364 WHERE itemnumber = ?";
1366 $notforloan_query = "SELECT itemtypes.notforloan
1368 JOIN biblioitems USING (biblioitemnumber)
1369 JOIN itemtypes USING (itemtype)
1370 WHERE itemnumber = ?";
1372 my $sth = $dbh->prepare($notforloan_query);
1373 $sth->execute($itemnumber);
1374 my $notforloan_per_itemtype = 0;
1375 if (my ($notforloan) = $sth->fetchrow_array) {
1376 $notforloan_per_itemtype = 1 if $notforloan;
1379 my $available_per_item = 1;
1380 $available_per_item = 0 if $item->{itemlost} or
1381 ( $item->{notforloan} > 0 ) or
1382 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1383 $item->{wthdrawn} or
1384 $notforloan_per_itemtype;
1387 if (C4::Context->preference('AllowOnShelfHolds')) {
1388 return $available_per_item;
1390 return ($available_per_item and ($item->{onloan} or GetReserveStatus($itemnumber) eq "W"));
1394 =head2 AlterPriority
1396 AlterPriority( $where, $borrowernumber, $biblionumber, $reservedate );
1398 This function changes a reserve's priority up, down, to the top, or to the bottom.
1399 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1404 my ( $where, $borrowernumber, $biblionumber ) = @_;
1406 my $dbh = C4::Context->dbh;
1408 ## Find this reserve
1409 my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? AND cancellationdate IS NULL');
1410 $sth->execute( $biblionumber, $borrowernumber );
1411 my $reserve = $sth->fetchrow_hashref();
1414 if ( $where eq 'up' || $where eq 'down' ) {
1416 my $priority = $reserve->{'priority'};
1417 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1418 _FixPriority( $biblionumber, $borrowernumber, $priority )
1420 } elsif ( $where eq 'top' ) {
1422 _FixPriority( $biblionumber, $borrowernumber, '1' )
1424 } elsif ( $where eq 'bottom' ) {
1426 _FixPriority( $biblionumber, $borrowernumber, '999999' )
1431 =head2 ToggleLowestPriority
1433 ToggleLowestPriority( $borrowernumber, $biblionumber );
1435 This function sets the lowestPriority field to true if is false, and false if it is true.
1439 sub ToggleLowestPriority {
1440 my ( $borrowernumber, $biblionumber ) = @_;
1442 my $dbh = C4::Context->dbh;
1444 my $sth = $dbh->prepare(
1445 "UPDATE reserves SET lowestPriority = NOT lowestPriority
1446 WHERE biblionumber = ?
1447 AND borrowernumber = ?"
1455 _FixPriority( $biblionumber, $borrowernumber, '999999' );
1460 &_FixPriority($biblio,$borrowernumber,$rank,$ignoreSetLowestRank);
1462 Only used internally (so don't export it)
1463 Changed how this functions works #
1464 Now just gets an array of reserves in the rank order and updates them with
1465 the array index (+1 as array starts from 0)
1466 and if $rank is supplied will splice item from the array and splice it back in again
1467 in new priority rank
1472 my ( $biblio, $borrowernumber, $rank, $ignoreSetLowestRank ) = @_;
1473 my $dbh = C4::Context->dbh;
1474 if ( $rank eq "del" ) {
1475 CancelReserve( $biblio, undef, $borrowernumber );
1477 if ( $rank eq "W" || $rank eq "0" ) {
1479 # make sure priority for waiting or in-transit items is 0
1483 WHERE biblionumber = ?
1484 AND borrowernumber = ?
1485 AND found IN ('W', 'T')
1487 my $sth = $dbh->prepare($query);
1488 $sth->execute( $biblio, $borrowernumber );
1494 # FIXME adding a new security in returned elements for changing priority,
1495 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1496 # This is wrong a waiting reserve has W set
1497 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1499 SELECT borrowernumber, reservedate, constrainttype
1501 WHERE biblionumber = ?
1502 AND ((found <> 'W' AND found <> 'T') or found is NULL)
1503 ORDER BY priority ASC
1505 my $sth = $dbh->prepare($query);
1506 $sth->execute($biblio);
1507 while ( my $line = $sth->fetchrow_hashref ) {
1508 push( @reservedates, $line );
1509 push( @priority, $line );
1512 # To find the matching index
1514 my $key = -1; # to allow for 0 to be a valid result
1515 for ( $i = 0 ; $i < @priority ; $i++ ) {
1516 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1517 $key = $i; # save the index
1522 # if index exists in array then move it to new position
1523 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1524 my $new_rank = $rank -
1525 1; # $new_rank is what you want the new index to be in the array
1526 my $moving_item = splice( @priority, $key, 1 );
1527 splice( @priority, $new_rank, 0, $moving_item );
1530 # now fix the priority on those that are left....
1534 WHERE biblionumber = ?
1535 AND borrowernumber = ?
1539 $sth = $dbh->prepare($query);
1540 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1543 $priority[$j]->{'borrowernumber'},
1544 $priority[$j]->{'reservedate'}
1549 $sth = $dbh->prepare( "SELECT borrowernumber FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1552 unless ( $ignoreSetLowestRank ) {
1553 while ( my $res = $sth->fetchrow_hashref() ) {
1554 _FixPriority( $biblio, $res->{'borrowernumber'}, '999999', 1 );
1559 =head2 _Findgroupreserve
1561 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1563 Looks for an item-specific match first, then for a title-level match, returning the
1564 first match found. If neither, then we look for a 3rd kind of match based on
1565 reserve constraints.
1567 TODO: add more explanation about reserve constraints
1569 C<&_Findgroupreserve> returns :
1570 C<@results> is an array of references-to-hash whose keys are mostly
1571 fields from the reserves table of the Koha database, plus
1572 C<biblioitemnumber>.
1576 sub _Findgroupreserve {
1577 my ( $bibitem, $biblio, $itemnumber ) = @_;
1578 my $dbh = C4::Context->dbh;
1580 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1581 # check for exact targetted match
1582 my $item_level_target_query = qq/
1583 SELECT reserves.biblionumber AS biblionumber,
1584 reserves.borrowernumber AS borrowernumber,
1585 reserves.reservedate AS reservedate,
1586 reserves.branchcode AS branchcode,
1587 reserves.cancellationdate AS cancellationdate,
1588 reserves.found AS found,
1589 reserves.reservenotes AS reservenotes,
1590 reserves.priority AS priority,
1591 reserves.timestamp AS timestamp,
1592 biblioitems.biblioitemnumber AS biblioitemnumber,
1593 reserves.itemnumber AS itemnumber
1595 JOIN biblioitems USING (biblionumber)
1596 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1599 AND item_level_request = 1
1601 AND reservedate <= CURRENT_DATE()
1603 my $sth = $dbh->prepare($item_level_target_query);
1604 $sth->execute($itemnumber);
1606 if ( my $data = $sth->fetchrow_hashref ) {
1607 push( @results, $data );
1609 return @results if @results;
1611 # check for title-level targetted match
1612 my $title_level_target_query = qq/
1613 SELECT reserves.biblionumber AS biblionumber,
1614 reserves.borrowernumber AS borrowernumber,
1615 reserves.reservedate AS reservedate,
1616 reserves.branchcode AS branchcode,
1617 reserves.cancellationdate AS cancellationdate,
1618 reserves.found AS found,
1619 reserves.reservenotes AS reservenotes,
1620 reserves.priority AS priority,
1621 reserves.timestamp AS timestamp,
1622 biblioitems.biblioitemnumber AS biblioitemnumber,
1623 reserves.itemnumber AS itemnumber
1625 JOIN biblioitems USING (biblionumber)
1626 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1629 AND item_level_request = 0
1630 AND hold_fill_targets.itemnumber = ?
1631 AND reservedate <= CURRENT_DATE()
1633 $sth = $dbh->prepare($title_level_target_query);
1634 $sth->execute($itemnumber);
1636 if ( my $data = $sth->fetchrow_hashref ) {
1637 push( @results, $data );
1639 return @results if @results;
1642 SELECT reserves.biblionumber AS biblionumber,
1643 reserves.borrowernumber AS borrowernumber,
1644 reserves.reservedate AS reservedate,
1645 reserves.waitingdate AS waitingdate,
1646 reserves.branchcode AS branchcode,
1647 reserves.cancellationdate AS cancellationdate,
1648 reserves.found AS found,
1649 reserves.reservenotes AS reservenotes,
1650 reserves.priority AS priority,
1651 reserves.timestamp AS timestamp,
1652 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1653 reserves.itemnumber AS itemnumber
1655 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1656 WHERE reserves.biblionumber = ?
1657 AND ( ( reserveconstraints.biblioitemnumber = ?
1658 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1659 AND reserves.reservedate = reserveconstraints.reservedate )
1660 OR reserves.constrainttype='a' )
1661 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1662 AND reserves.reservedate <= CURRENT_DATE()
1664 $sth = $dbh->prepare($query);
1665 $sth->execute( $biblio, $bibitem, $itemnumber );
1667 while ( my $data = $sth->fetchrow_hashref ) {
1668 push( @results, $data );
1673 =head2 _koha_notify_reserve
1675 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1677 Sends a notification to the patron that their hold has been filled (through
1678 ModReserveAffect, _not_ ModReserveFill)
1682 sub _koha_notify_reserve {
1683 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1685 my $dbh = C4::Context->dbh;
1686 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
1688 # Try to get the borrower's email address
1690 my $which_address = C4::Context->preference('AutoEmailPrimaryAddress');
1691 # If the system preference is set to 'first valid' (value == OFF), look up email address
1692 if ($which_address eq 'OFF') {
1693 $to_address = C4::Members::GetFirstValidEmailAddress( $borrowernumber );
1695 $to_address = $borrower->{$which_address};
1701 if ( $to_address || $borrower->{'smsalertnumber'} ) {
1702 $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold_Filled' } );
1704 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1705 $letter_code = $messagingprefs->{'letter_code'};
1707 $letter_code = 'HOLD_PRINT';
1711 my $sth = $dbh->prepare("
1714 WHERE borrowernumber = ?
1715 AND biblionumber = ?
1717 $sth->execute( $borrowernumber, $biblionumber );
1718 my $reserve = $sth->fetchrow_hashref;
1719 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1721 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1723 my $letter = getletter( 'reserves', $letter_code );
1724 die "Could not find a letter called '$letter_code' in the 'reserves' module" unless( $letter );
1726 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1727 C4::Letters::parseletter( $letter, 'borrowers', $borrowernumber );
1728 C4::Letters::parseletter( $letter, 'biblio', $biblionumber );
1729 C4::Letters::parseletter( $letter, 'reserves', $borrowernumber, $biblionumber );
1731 if ( $reserve->{'itemnumber'} ) {
1732 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1734 my $today = C4::Dates->new()->output();
1735 $letter->{'title'} =~ s/<<today>>/$today/g;
1736 $letter->{'content'} =~ s/<<today>>/$today/g;
1737 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1739 if ( $print_mode ) {
1740 C4::Letters::EnqueueLetter( {
1742 borrowernumber => $borrowernumber,
1743 message_transport_type => 'print',
1749 if ( grep { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1750 # aka, 'email' in ->{'transports'}
1751 C4::Letters::EnqueueLetter(
1752 { letter => $letter,
1753 borrowernumber => $borrowernumber,
1754 message_transport_type => 'email',
1755 from_address => $admin_email_address,
1760 if ( grep { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1761 C4::Letters::EnqueueLetter(
1762 { letter => $letter,
1763 borrowernumber => $borrowernumber,
1764 message_transport_type => 'sms',
1770 =head2 _ShiftPriorityByDateAndPriority
1772 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1774 This increments the priority of all reserves after the one
1775 with either the lowest date after C<$reservedate>
1776 or the lowest priority after C<$priority>.
1778 It effectively makes room for a new reserve to be inserted with a certain
1779 priority, which is returned.
1781 This is most useful when the reservedate can be set by the user. It allows
1782 the new reserve to be placed before other reserves that have a later
1783 reservedate. Since priority also is set by the form in reserves/request.pl
1784 the sub accounts for that too.
1788 sub _ShiftPriorityByDateAndPriority {
1789 my ( $biblio, $resdate, $new_priority ) = @_;
1791 my $dbh = C4::Context->dbh;
1792 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1793 my $sth = $dbh->prepare( $query );
1794 $sth->execute( $biblio, $resdate, $new_priority );
1795 my $min_priority = $sth->fetchrow;
1796 # if no such matches are found, $new_priority remains as original value
1797 $new_priority = $min_priority if ( $min_priority );
1799 # Shift the priority up by one; works in conjunction with the next SQL statement
1800 $query = "UPDATE reserves
1801 SET priority = priority+1
1802 WHERE biblionumber = ?
1803 AND borrowernumber = ?
1806 my $sth_update = $dbh->prepare( $query );
1808 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1809 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1810 $sth = $dbh->prepare( $query );
1811 $sth->execute( $new_priority, $biblio );
1812 while ( my $row = $sth->fetchrow_hashref ) {
1813 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
1816 return $new_priority; # so the caller knows what priority they wind up receiving
1821 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
1823 Use when checking out an item to handle reserves
1824 If $cancelreserve boolean is set to true, it will remove existing reserve
1829 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
1831 my ( $restype, $res, $all_reserves ) = CheckReserves( $itemnumber );
1834 my $biblionumber = $res->{biblionumber};
1835 my $biblioitemnumber = $res->{biblioitemnumber};
1837 if ($res->{borrowernumber} == $borrowernumber) {
1838 ModReserveFill($res);
1842 # The item is reserved by someone else.
1843 # Find this item in the reserves
1846 foreach (@$all_reserves) {
1847 $_->{'borrowernumber'} == $borrowernumber or next;
1848 $_->{'biblionumber'} == $biblionumber or next;
1855 # The item is reserved by the current patron
1856 ModReserveFill($borr_res);
1859 if ($cancelreserve) { # cancel reserves on this item
1860 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
1861 CancelReserve($res->{'biblionumber'}, 0, $res->{'borrowernumber'});
1868 MergeHolds($dbh,$to_biblio, $from_biblio);
1870 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
1875 my ( $dbh, $to_biblio, $from_biblio ) = @_;
1876 my $sth = $dbh->prepare(
1877 "SELECT count(*) as reservenumber FROM reserves WHERE biblionumber = ?"
1879 $sth->execute($from_biblio);
1880 if ( my $data = $sth->fetchrow_hashref() ) {
1882 # holds exist on old record, if not we don't need to do anything
1883 $sth = $dbh->prepare(
1884 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
1885 $sth->execute( $to_biblio, $from_biblio );
1888 # don't reorder those already waiting
1890 $sth = $dbh->prepare(
1891 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
1893 my $upd_sth = $dbh->prepare(
1894 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
1895 AND reservedate = ? AND constrainttype = ? AND (itemnumber = ? or itemnumber is NULL) "
1897 $sth->execute( $to_biblio, 'W', 'T' );
1899 while ( my $reserve = $sth->fetchrow_hashref() ) {
1901 $priority, $to_biblio,
1902 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
1903 $reserve->{'constrainttype'}, $reserve->{'itemnumber'}
1913 Koha Development Team <http://koha-community.org/>