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
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 #use warnings; FIXME - Bug 2505
33 # for _koha_notify_reserve
34 use C4::Members::Messaging;
45 use List::MoreUtils qw( firstidx any );
48 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
52 C4::Reserves - Koha functions for dealing with reservation.
60 This modules provides somes functions to deal with reservations.
62 Reserves are stored in reserves table.
63 The following columns contains important values :
64 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
65 =0 : then the reserve is being dealed
66 - found : NULL : means the patron requested the 1st available, and we haven't chosen the item
67 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
68 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
69 F(inished) : the reserve has been completed, and is done
70 - itemnumber : empty : the reserve is still unaffected to an item
71 filled: the reserve is attached to an item
72 The complete workflow is :
73 ==== 1st use case ====
74 patron request a document, 1st available : P >0, F=NULL, I=NULL
75 a library having it run "transfertodo", and clic on the list
76 if there is no transfer to do, the reserve waiting
77 patron can pick it up P =0, F=W, I=filled
78 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
79 The pickup library receive the book, it check in P =0, F=W, I=filled
80 The patron borrow the book P =0, F=F, I=filled
82 ==== 2nd use case ====
83 patron requests a document, a given item,
84 If pickup is holding branch P =0, F=W, I=filled
85 If transfer needed, write in branchtransfer P =0, F=T, I=filled
86 The pickup library receive the book, it checks it in P =0, F=W, I=filled
87 The patron borrow the book P =0, F=F, I=filled
100 &GetReservesFromItemnumber
101 &GetReservesFromBiblionumber
102 &GetReservesFromBorrowernumber
103 &GetReservesForBranch
116 &ModReserveMinusPriority
122 &CanReserveBeCanceledFromOpac
124 &CancelExpiredReserves
126 &AutoUnsuspendReserves
128 &IsAvailableForItemLevelRequest
130 &OPACItemHoldsAllowed
133 &ToggleLowestPriority
139 &GetReservesControlBranch
143 @EXPORT_OK = qw( MergeHolds );
148 AddReserve($branch,$borrowernumber,$biblionumber,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
150 Adds reserve and generates HOLDPLACED message.
152 The following tables are available witin the HOLDPLACED message:
164 $branch, $borrowernumber, $biblionumber, $bibitems,
165 $priority, $resdate, $expdate, $notes,
166 $title, $checkitem, $found, $itemtype
169 if ( Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->count() > 0 ) {
170 carp("AddReserve: borrower $borrowernumber already has a hold for biblionumber $biblionumber");
174 my $dbh = C4::Context->dbh;
176 $resdate = output_pref( { str => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' })
177 or output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
179 $expdate = output_pref({ str => $expdate, dateonly => 1, dateformat => 'iso' });
181 if ( C4::Context->preference('AllowHoldDateInFuture') ) {
183 # Make room in reserves for this before those of a later reserve date
184 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
189 # If the reserv had the waiting status, we had the value of the resdate
190 if ( $found eq 'W' ) {
191 $waitingdate = $resdate;
194 # Don't add itemtype limit if specific item is selected
195 $itemtype = undef if $checkitem;
197 # updates take place here
198 my $hold = Koha::Hold->new(
200 borrowernumber => $borrowernumber,
201 biblionumber => $biblionumber,
202 reservedate => $resdate,
203 branchcode => $branch,
204 priority => $priority,
205 reservenotes => $notes,
206 itemnumber => $checkitem,
208 waitingdate => $waitingdate,
209 expirationdate => $expdate,
210 itemtype => $itemtype,
213 my $reserve_id = $hold->id();
215 # add a reserve fee if needed
216 my $fee = GetReserveFee( $borrowernumber, $biblionumber );
217 ChargeReserveFee( $borrowernumber, $fee, $title );
219 _FixPriority({ biblionumber => $biblionumber});
221 # Send e-mail to librarian if syspref is active
222 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
223 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
224 my $library = Koha::Libraries->find($borrower->{branchcode})->unblessed;
225 if ( my $letter = C4::Letters::GetPreparedLetter (
226 module => 'reserves',
227 letter_code => 'HOLDPLACED',
228 branchcode => $branch,
230 'branches' => $library,
231 'borrowers' => $borrower,
232 'biblio' => $biblionumber,
233 'biblioitems' => $biblionumber,
234 'items' => $checkitem,
238 my $admin_email_address = $library->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
240 C4::Letters::EnqueueLetter(
242 borrowernumber => $borrowernumber,
243 message_transport_type => 'email',
244 from_address => $admin_email_address,
245 to_address => $admin_email_address,
256 $res = GetReserve( $reserve_id );
258 Return the current reserve.
263 my ($reserve_id) = @_;
265 my $dbh = C4::Context->dbh;
267 my $query = "SELECT * FROM reserves WHERE reserve_id = ?";
268 my $sth = $dbh->prepare( $query );
269 $sth->execute( $reserve_id );
270 return $sth->fetchrow_hashref();
273 =head2 GetReservesFromBiblionumber
275 my $reserves = GetReservesFromBiblionumber({
276 biblionumber => $biblionumber,
277 [ itemnumber => $itemnumber, ]
281 This function gets the list of reservations for one C<$biblionumber>,
282 returning an arrayref pointing to the reserves for C<$biblionumber>.
284 By default, only reserves whose start date falls before the current
285 time are returned. To return all reserves, including future ones,
286 the C<all_dates> parameter can be included and set to a true value.
288 If the C<itemnumber> parameter is supplied, reserves must be targeted
289 to that item or not targeted to any item at all; otherwise, they
290 are excluded from the list.
294 sub GetReservesFromBiblionumber {
296 my $biblionumber = $params->{biblionumber} or return [];
297 my $itemnumber = $params->{itemnumber};
298 my $all_dates = $params->{all_dates} // 0;
299 my $dbh = C4::Context->dbh;
301 # Find the desired items in the reserves
306 timestamp AS rtimestamp,
320 WHERE biblionumber = ? ";
321 push( @params, $biblionumber );
322 unless ( $all_dates ) {
323 $query .= " AND reservedate <= CAST(NOW() AS DATE) ";
326 $query .= " AND ( itemnumber IS NULL OR itemnumber = ? )";
327 push( @params, $itemnumber );
329 $query .= "ORDER BY priority";
330 my $sth = $dbh->prepare($query);
331 $sth->execute( @params );
333 while ( my $data = $sth->fetchrow_hashref ) {
334 push @results, $data;
339 =head2 GetReservesFromItemnumber
341 ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $waitingdate ) = GetReservesFromItemnumber($itemnumber);
343 Get the first reserve for a specific item number (based on priority). Returns the abovementioned values for that reserve.
345 The routine does not look at future reserves (read: item level holds), but DOES include future waits (a confirmed future hold).
349 sub GetReservesFromItemnumber {
350 my ($itemnumber) = @_;
352 my $schema = Koha::Database->new()->schema();
354 my $r = $schema->resultset('Reserve')->search(
356 itemnumber => $itemnumber,
359 reservedate => \'<= CAST( NOW() AS DATE )',
360 waitingdate => { '!=', undef }
364 order_by => 'priority',
372 $r->get_column('borrowernumber'),
373 $r->get_column('branchcode'),
379 =head2 GetReservesFromBorrowernumber
381 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
387 sub GetReservesFromBorrowernumber {
388 my ( $borrowernumber, $status ) = @_;
389 my $dbh = C4::Context->dbh;
392 $sth = $dbh->prepare("
395 WHERE borrowernumber=?
399 $sth->execute($borrowernumber,$status);
401 $sth = $dbh->prepare("
404 WHERE borrowernumber=?
407 $sth->execute($borrowernumber);
409 my $data = $sth->fetchall_arrayref({});
413 =head2 CanBookBeReserved
415 $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
416 if ($canReserve eq 'OK') { #We can reserve this Item! }
418 See CanItemBeReserved() for possible return values.
422 sub CanBookBeReserved{
423 my ($borrowernumber, $biblionumber) = @_;
425 my $items = GetItemnumbersForBiblio($biblionumber);
426 #get items linked via host records
427 my @hostitems = get_hostitemnumbers_of($biblionumber);
429 push (@$items,@hostitems);
433 foreach my $item (@$items) {
434 $canReserve = CanItemBeReserved( $borrowernumber, $item );
435 return 'OK' if $canReserve eq 'OK';
440 =head2 CanItemBeReserved
442 $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber)
443 if ($canReserve eq 'OK') { #We can reserve this Item! }
445 @RETURNS OK, if the Item can be reserved.
446 ageRestricted, if the Item is age restricted for this borrower.
447 damaged, if the Item is damaged.
448 cannotReserveFromOtherBranches, if syspref 'canreservefromotherbranches' is OK.
449 tooManyReserves, if the borrower has exceeded his maximum reserve amount.
450 notReservable, if holds on this item are not allowed
454 sub CanItemBeReserved{
455 my ($borrowernumber, $itemnumber) = @_;
457 my $dbh = C4::Context->dbh;
458 my $ruleitemtype; # itemtype of the matching issuing rule
459 my $allowedreserves = 0;
461 # we retrieve borrowers and items informations #
462 # item->{itype} will come for biblioitems if necessery
463 my $item = GetItem($itemnumber);
464 my $biblioData = C4::Biblio::GetBiblioData( $item->{biblionumber} );
465 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
467 # If an item is damaged and we don't allow holds on damaged items, we can stop right here
468 return 'damaged' if ( $item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems') );
470 #Check for the age restriction
471 my ($ageRestriction, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction( $biblioData->{agerestriction}, $borrower );
472 return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
474 my $controlbranch = C4::Context->preference('ReservesControlBranch');
476 # we retrieve user rights on this itemtype and branchcode
477 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
479 WHERE (categorycode in (?,'*') )
480 AND (itemtype IN (?,'*'))
481 AND (branchcode IN (?,'*'))
488 my $querycount ="SELECT
491 LEFT JOIN items USING (itemnumber)
492 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
493 LEFT JOIN borrowers USING (borrowernumber)
494 WHERE borrowernumber = ?
499 my $branchfield = "reserves.branchcode";
501 if( $controlbranch eq "ItemHomeLibrary" ){
502 $branchfield = "items.homebranch";
503 $branchcode = $item->{homebranch};
504 }elsif( $controlbranch eq "PatronLibrary" ){
505 $branchfield = "borrowers.branchcode";
506 $branchcode = $borrower->{branchcode};
510 $sth->execute($borrower->{'categorycode'}, $item->{'itype'}, $branchcode);
511 if(my $rights = $sth->fetchrow_hashref()){
512 $ruleitemtype = $rights->{itemtype};
513 $allowedreserves = $rights->{reservesallowed};
520 $querycount .= "AND $branchfield = ?";
522 # If using item-level itypes, fall back to the record
523 # level itemtype if the hold has no associated item
525 C4::Context->preference('item-level_itypes')
526 ? " AND COALESCE( items.itype, biblioitems.itemtype ) = ?"
527 : " AND biblioitems.itemtype = ?"
528 if ( $ruleitemtype ne "*" );
530 my $sthcount = $dbh->prepare($querycount);
532 if($ruleitemtype eq "*"){
533 $sthcount->execute($borrowernumber, $branchcode);
535 $sthcount->execute($borrowernumber, $branchcode, $ruleitemtype);
538 my $reservecount = "0";
539 if(my $rowcount = $sthcount->fetchrow_hashref()){
540 $reservecount = $rowcount->{count};
542 # we check if it's ok or not
543 if( $reservecount >= $allowedreserves ){
544 return 'tooManyReserves';
547 my $circ_control_branch = C4::Circulation::_GetCircControlBranch($item,
549 my $branchitemrule = C4::Circulation::GetBranchItemRule($circ_control_branch,
552 if ( $branchitemrule->{holdallowed} == 0 ) {
553 return 'notReservable';
556 if ( $branchitemrule->{holdallowed} == 1
557 && $borrower->{branchcode} ne $item->{homebranch} )
559 return 'cannotReserveFromOtherBranches';
562 # If reservecount is ok, we check item branch if IndependentBranches is ON
563 # and canreservefromotherbranches is OFF
564 if ( C4::Context->preference('IndependentBranches')
565 and !C4::Context->preference('canreservefromotherbranches') )
567 my $itembranch = $item->{homebranch};
568 if ($itembranch ne $borrower->{branchcode}) {
569 return 'cannotReserveFromOtherBranches';
576 =head2 CanReserveBeCanceledFromOpac
578 $number = CanReserveBeCanceledFromOpac($reserve_id, $borrowernumber);
580 returns 1 if reserve can be cancelled by user from OPAC.
581 First check if reserve belongs to user, next checks if reserve is not in
582 transfer or waiting status
586 sub CanReserveBeCanceledFromOpac {
587 my ($reserve_id, $borrowernumber) = @_;
589 return unless $reserve_id and $borrowernumber;
590 my $reserve = GetReserve($reserve_id);
592 return 0 unless $reserve->{borrowernumber} == $borrowernumber;
593 return 0 if ( $reserve->{found} eq 'W' ) or ( $reserve->{found} eq 'T' );
599 =head2 GetReserveCount
601 $number = &GetReserveCount($borrowernumber);
603 this function returns the number of reservation for a borrower given on input arg.
607 sub GetReserveCount {
608 my ($borrowernumber) = @_;
610 my $dbh = C4::Context->dbh;
613 SELECT COUNT(*) AS counter
615 WHERE borrowernumber = ?
617 my $sth = $dbh->prepare($query);
618 $sth->execute($borrowernumber);
619 my $row = $sth->fetchrow_hashref;
620 return $row->{counter};
623 =head2 GetOtherReserves
625 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
627 Check queued list of this document and check if this document must be transferred
631 sub GetOtherReserves {
632 my ($itemnumber) = @_;
635 my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
636 if ($checkreserves) {
637 my $iteminfo = GetItem($itemnumber);
638 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
639 $messages->{'transfert'} = $checkreserves->{'branchcode'};
640 #minus priorities of others reservs
641 ModReserveMinusPriority(
643 $checkreserves->{'reserve_id'},
646 #launch the subroutine dotransfer
647 C4::Items::ModItemTransfer(
649 $iteminfo->{'holdingbranch'},
650 $checkreserves->{'branchcode'}
655 #step 2b : case of a reservation on the same branch, set the waiting status
657 $messages->{'waiting'} = 1;
658 ModReserveMinusPriority(
660 $checkreserves->{'reserve_id'},
662 ModReserveStatus($itemnumber,'W');
665 $nextreservinfo = $checkreserves->{'borrowernumber'};
668 return ( $messages, $nextreservinfo );
671 =head2 ChargeReserveFee
673 $fee = ChargeReserveFee( $borrowernumber, $fee, $title );
675 Charge the fee for a reserve (if $fee > 0)
679 sub ChargeReserveFee {
680 my ( $borrowernumber, $fee, $title ) = @_;
681 return if !$fee || $fee==0; # the last test is needed to include 0.00
683 INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
685 my $dbh = C4::Context->dbh;
686 my $nextacctno = &getnextacctno( $borrowernumber );
687 $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
692 $fee = GetReserveFee( $borrowernumber, $biblionumber );
694 Calculate the fee for a reserve (if applicable).
699 my ( $borrowernumber, $biblionumber ) = @_;
701 SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE borrowernumber = ?
704 SELECT COUNT(*) FROM items
705 LEFT JOIN issues USING (itemnumber)
706 WHERE items.biblionumber=? AND issues.issue_id IS NULL
709 SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?
712 my $dbh = C4::Context->dbh;
713 my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
714 my $hold_fee_mode = C4::Context->preference('HoldFeeMode') || 'not_always';
715 if( $fee and $fee > 0 and $hold_fee_mode ne 'always' ) {
716 # This is a reconstruction of the old code:
717 # Compare number of items with items issued, and optionally check holds
718 # If not all items are issued and there are no holds: charge no fee
719 # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
720 my ( $notissued, $reserved );
721 ( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
724 ( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
725 ( $biblionumber, $borrowernumber ) );
726 $fee = 0 if $reserved == 0;
732 =head2 GetReservesToBranch
734 @transreserv = GetReservesToBranch( $frombranch );
736 Get reserve list for a given branch
740 sub GetReservesToBranch {
741 my ( $frombranch ) = @_;
742 my $dbh = C4::Context->dbh;
743 my $sth = $dbh->prepare(
744 "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
749 $sth->execute( $frombranch );
752 while ( my $data = $sth->fetchrow_hashref ) {
753 $transreserv[$i] = $data;
756 return (@transreserv);
759 =head2 GetReservesForBranch
761 @transreserv = GetReservesForBranch($frombranch);
765 sub GetReservesForBranch {
766 my ($frombranch) = @_;
767 my $dbh = C4::Context->dbh;
770 SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
775 $query .= " AND branchcode=? " if ( $frombranch );
776 $query .= "ORDER BY waitingdate" ;
778 my $sth = $dbh->prepare($query);
780 $sth->execute($frombranch);
787 while ( my $data = $sth->fetchrow_hashref ) {
788 $transreserv[$i] = $data;
791 return (@transreserv);
794 =head2 GetReserveStatus
796 $reservestatus = GetReserveStatus($itemnumber);
798 Takes an itemnumber and returns the status of the reserve placed on it.
799 If several reserves exist, the reserve with the lower priority is given.
803 ## FIXME: I don't think this does what it thinks it does.
804 ## It only ever checks the first reserve result, even though
805 ## multiple reserves for that bib can have the itemnumber set
806 ## the sub is only used once in the codebase.
807 sub GetReserveStatus {
808 my ($itemnumber) = @_;
810 my $dbh = C4::Context->dbh;
812 my ($sth, $found, $priority);
814 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
815 $sth->execute($itemnumber);
816 ($found, $priority) = $sth->fetchrow_array;
820 return 'Waiting' if $found eq 'W' and $priority == 0;
821 return 'Finished' if $found eq 'F';
824 return 'Reserved' if $priority > 0;
826 return ''; # empty string here will remove need for checking undef, or less log lines
831 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
832 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
833 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
835 Find a book in the reserves.
837 C<$itemnumber> is the book's item number.
838 C<$lookahead> is the number of days to look in advance for future reserves.
840 As I understand it, C<&CheckReserves> looks for the given item in the
841 reserves. If it is found, that's a match, and C<$status> is set to
844 Otherwise, it finds the most important item in the reserves with the
845 same biblio number as this book (I'm not clear on this) and returns it
846 with C<$status> set to C<Reserved>.
848 C<&CheckReserves> returns a two-element list:
850 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
852 C<$reserve> is the reserve item that matched. It is a
853 reference-to-hash whose keys are mostly the fields of the reserves
854 table in the Koha database.
859 my ( $item, $barcode, $lookahead_days, $ignore_borrowers) = @_;
860 my $dbh = C4::Context->dbh;
863 if (C4::Context->preference('item-level_itypes')){
865 SELECT items.biblionumber,
866 items.biblioitemnumber,
867 itemtypes.notforloan,
868 items.notforloan AS itemnotforloan,
874 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
875 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
880 SELECT items.biblionumber,
881 items.biblioitemnumber,
882 itemtypes.notforloan,
883 items.notforloan AS itemnotforloan,
889 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
890 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
895 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
896 $sth->execute($item);
899 $sth = $dbh->prepare("$select WHERE barcode = ?");
900 $sth->execute($barcode);
902 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
903 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged, $item_homebranch, $item_holdingbranch ) = $sth->fetchrow_array;
905 return if ( $damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') );
907 return unless $itemnumber; # bail if we got nothing.
909 # if item is not for loan it cannot be reserved either.....
910 # except where items.notforloan < 0 : This indicates the item is holdable.
911 return if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
913 # Find this item in the reserves
914 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber, $lookahead_days, $ignore_borrowers);
916 # $priority and $highest are used to find the most important item
917 # in the list returned by &_Findgroupreserve. (The lower $priority,
918 # the more important the item.)
919 # $highest is the most important item we've seen so far.
921 if (scalar @reserves) {
922 my $LocalHoldsPriority = C4::Context->preference('LocalHoldsPriority');
923 my $LocalHoldsPriorityPatronControl = C4::Context->preference('LocalHoldsPriorityPatronControl');
924 my $LocalHoldsPriorityItemControl = C4::Context->preference('LocalHoldsPriorityItemControl');
926 my $priority = 10000000;
927 foreach my $res (@reserves) {
928 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
929 return ( "Waiting", $res, \@reserves ); # Found it
933 my $local_hold_match;
935 if ($LocalHoldsPriority) {
936 $borrowerinfo = C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
937 $iteminfo = C4::Items::GetItem($itemnumber);
939 my $local_holds_priority_item_branchcode =
940 $iteminfo->{$LocalHoldsPriorityItemControl};
941 my $local_holds_priority_patron_branchcode =
942 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
944 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
945 ? $borrowerinfo->{branchcode}
948 $local_holds_priority_item_branchcode eq
949 $local_holds_priority_patron_branchcode;
952 # See if this item is more important than what we've got so far
953 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
954 $iteminfo ||= C4::Items::GetItem($itemnumber);
955 next if $res->{itemtype} && $res->{itemtype} ne _get_itype( $iteminfo );
956 $borrowerinfo ||= C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
957 my $branch = GetReservesControlBranch( $iteminfo, $borrowerinfo );
958 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
959 next if ($branchitemrule->{'holdallowed'} == 0);
960 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
961 next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $iteminfo->{ $branchitemrule->{hold_fulfillment_policy} }) );
962 $priority = $res->{'priority'};
964 last if $local_hold_match;
970 # If we get this far, then no exact match was found.
971 # We return the most important (i.e. next) reservation.
973 $highest->{'itemnumber'} = $item;
974 return ( "Reserved", $highest, \@reserves );
980 =head2 CancelExpiredReserves
982 CancelExpiredReserves();
984 Cancels all reserves with an expiration date from before today.
988 sub CancelExpiredReserves {
990 # Cancel reserves that have passed their expiration date.
991 my $dbh = C4::Context->dbh;
992 my $sth = $dbh->prepare( "
993 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
994 AND expirationdate IS NOT NULL
999 while ( my $res = $sth->fetchrow_hashref() ) {
1000 CancelReserve({ reserve_id => $res->{'reserve_id'} });
1003 # Cancel reserves that have been waiting too long
1004 if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
1005 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
1006 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
1008 my $today = dt_from_string();
1010 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
1011 $sth = $dbh->prepare( $query );
1012 $sth->execute( $max_pickup_delay );
1014 while ( my $res = $sth->fetchrow_hashref ) {
1016 unless ( $cancel_on_holidays ) {
1017 my $calendar = Koha::Calendar->new( branchcode => $res->{'branchcode'} );
1018 my $is_holiday = $calendar->is_holiday( $today );
1020 if ( $is_holiday ) {
1026 CancelReserve({ reserve_id => $res->{'reserve_id'}, charge_cancel_fee => 1 });
1033 =head2 AutoUnsuspendReserves
1035 AutoUnsuspendReserves();
1037 Unsuspends all suspended reserves with a suspend_until date from before today.
1041 sub AutoUnsuspendReserves {
1042 my $today = dt_from_string();
1044 my @holds = Koha::Holds->search( { suspend_until => { '<' => $today->ymd() } } );
1046 map { $_->suspend(0)->suspend_until(undef)->store() } @holds;
1049 =head2 CancelReserve
1051 CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber, ] [ charge_cancel_fee => 1 ] });
1053 Cancels a reserve. If C<charge_cancel_fee> is passed and the C<ExpireReservesMaxPickUpDelayCharge> syspref is set, charge that fee to the patron's account.
1058 my ( $params ) = @_;
1060 my $reserve_id = $params->{'reserve_id'};
1061 # Filter out only the desired keys; this will insert undefined values for elements missing in
1062 # \%params, but GetReserveId filters them out anyway.
1063 $reserve_id = GetReserveId( { biblionumber => $params->{'biblionumber'}, borrowernumber => $params->{'borrowernumber'}, itemnumber => $params->{'itemnumber'} } ) unless ( $reserve_id );
1065 return unless ( $reserve_id );
1067 my $dbh = C4::Context->dbh;
1069 my $reserve = GetReserve( $reserve_id );
1073 SET cancellationdate = now(),
1076 WHERE reserve_id = ?
1078 my $sth = $dbh->prepare($query);
1079 $sth->execute( $reserve_id );
1082 INSERT INTO old_reserves
1083 SELECT * FROM reserves
1084 WHERE reserve_id = ?
1086 $sth = $dbh->prepare($query);
1087 $sth->execute( $reserve_id );
1090 DELETE FROM reserves
1091 WHERE reserve_id = ?
1093 $sth = $dbh->prepare($query);
1094 $sth->execute( $reserve_id );
1096 # now fix the priority on the others....
1097 _FixPriority({ biblionumber => $reserve->{biblionumber} });
1099 # and, if desired, charge a cancel fee
1100 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
1101 if ( $charge && $params->{'charge_cancel_fee'} ) {
1102 manualinvoice($reserve->{'borrowernumber'}, $reserve->{'itemnumber'}, 'Hold waiting too long', 'F', $charge);
1111 ModReserve({ rank => $rank,
1112 reserve_id => $reserve_id,
1113 branchcode => $branchcode
1114 [, itemnumber => $itemnumber ]
1115 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
1118 Change a hold request's priority or cancel it.
1120 C<$rank> specifies the effect of the change. If C<$rank>
1121 is 'W' or 'n', nothing happens. This corresponds to leaving a
1122 request alone when changing its priority in the holds queue
1125 If C<$rank> is 'del', the hold request is cancelled.
1127 If C<$rank> is an integer greater than zero, the priority of
1128 the request is set to that value. Since priority != 0 means
1129 that the item is not waiting on the hold shelf, setting the
1130 priority to a non-zero value also sets the request's found
1131 status and waiting date to NULL.
1133 The optional C<$itemnumber> parameter is used only when
1134 C<$rank> is a non-zero integer; if supplied, the itemnumber
1135 of the hold request is set accordingly; if omitted, the itemnumber
1138 B<FIXME:> Note that the forgoing can have the effect of causing
1139 item-level hold requests to turn into title-level requests. This
1140 will be fixed once reserves has separate columns for requested
1141 itemnumber and supplying itemnumber.
1146 my ( $params ) = @_;
1148 my $rank = $params->{'rank'};
1149 my $reserve_id = $params->{'reserve_id'};
1150 my $branchcode = $params->{'branchcode'};
1151 my $itemnumber = $params->{'itemnumber'};
1152 my $suspend_until = $params->{'suspend_until'};
1153 my $borrowernumber = $params->{'borrowernumber'};
1154 my $biblionumber = $params->{'biblionumber'};
1156 return if $rank eq "W";
1157 return if $rank eq "n";
1159 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
1160 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber }) unless ( $reserve_id );
1162 my $dbh = C4::Context->dbh;
1163 if ( $rank eq "del" ) {
1164 CancelReserve({ reserve_id => $reserve_id });
1166 elsif ($rank =~ /^\d+/ and $rank > 0) {
1167 my $hold = Koha::Holds->find($reserve_id);
1172 branchcode => $branchcode,
1173 itemnumber => $itemnumber,
1175 waitingdate => undef
1179 if ( defined( $suspend_until ) ) {
1180 if ( $suspend_until ) {
1181 $suspend_until = eval { dt_from_string( $suspend_until ) };
1182 $hold->suspend_hold( $suspend_until );
1184 # If the hold is suspended leave the hold suspended, but convert it to an indefinite hold.
1185 # If the hold is not suspended, this does nothing.
1186 $hold->set( { suspend_until => undef } )->store();
1190 _FixPriority({ reserve_id => $reserve_id, rank =>$rank });
1194 =head2 ModReserveFill
1196 &ModReserveFill($reserve);
1198 Fill a reserve. If I understand this correctly, this means that the
1199 reserved book has been found and given to the patron who reserved it.
1201 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1202 whose keys are fields from the reserves table in the Koha database.
1206 sub ModReserveFill {
1208 my $dbh = C4::Context->dbh;
1209 # fill in a reserve record....
1210 my $reserve_id = $res->{'reserve_id'};
1211 my $biblionumber = $res->{'biblionumber'};
1212 my $borrowernumber = $res->{'borrowernumber'};
1213 my $resdate = $res->{'reservedate'};
1215 # get the priority on this record....
1217 my $query = "SELECT priority
1219 WHERE biblionumber = ?
1220 AND borrowernumber = ?
1221 AND reservedate = ?";
1222 my $sth = $dbh->prepare($query);
1223 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1224 ($priority) = $sth->fetchrow_array;
1226 # update the database...
1227 $query = "UPDATE reserves
1230 WHERE biblionumber = ?
1232 AND borrowernumber = ?
1234 $sth = $dbh->prepare($query);
1235 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1237 # move to old_reserves
1238 $query = "INSERT INTO old_reserves
1239 SELECT * FROM reserves
1240 WHERE biblionumber = ?
1242 AND borrowernumber = ?
1244 $sth = $dbh->prepare($query);
1245 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1246 $query = "DELETE FROM reserves
1247 WHERE biblionumber = ?
1249 AND borrowernumber = ?
1251 $sth = $dbh->prepare($query);
1252 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1254 # now fix the priority on the others (if the priority wasn't
1255 # already sorted!)....
1256 unless ( $priority == 0 ) {
1257 _FixPriority({ reserve_id => $reserve_id, biblionumber => $biblionumber });
1261 =head2 ModReserveStatus
1263 &ModReserveStatus($itemnumber, $newstatus);
1265 Update the reserve status for the active (priority=0) reserve.
1267 $itemnumber is the itemnumber the reserve is on
1269 $newstatus is the new status.
1273 sub ModReserveStatus {
1275 #first : check if we have a reservation for this item .
1276 my ($itemnumber, $newstatus) = @_;
1277 my $dbh = C4::Context->dbh;
1279 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1280 my $sth_set = $dbh->prepare($query);
1281 $sth_set->execute( $newstatus, $itemnumber );
1283 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1284 CartToShelf( $itemnumber );
1288 =head2 ModReserveAffect
1290 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1292 This function affect an item and a status for a given reserve
1293 The itemnumber parameter is used to find the biblionumber.
1294 with the biblionumber & the borrowernumber, we can affect the itemnumber
1295 to the correct reserve.
1297 if $transferToDo is not set, then the status is set to "Waiting" as well.
1298 otherwise, a transfer is on the way, and the end of the transfer will
1299 take care of the waiting status
1303 sub ModReserveAffect {
1304 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1305 my $dbh = C4::Context->dbh;
1307 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1308 # attached to $itemnumber
1309 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1310 $sth->execute($itemnumber);
1311 my ($biblionumber) = $sth->fetchrow;
1313 # get request - need to find out if item is already
1314 # waiting in order to not send duplicate hold filled notifications
1315 my $reserve_id = GetReserveId({
1316 borrowernumber => $borrowernumber,
1317 biblionumber => $biblionumber,
1319 return unless defined $reserve_id;
1320 my $request = GetReserveInfo($reserve_id);
1321 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1323 # If we affect a reserve that has to be transferred, don't set to Waiting
1325 if ($transferToDo) {
1331 WHERE borrowernumber = ?
1332 AND biblionumber = ?
1336 # affect the reserve to Waiting as well.
1341 waitingdate = NOW(),
1343 WHERE borrowernumber = ?
1344 AND biblionumber = ?
1347 $sth = $dbh->prepare($query);
1348 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1349 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1350 _FixPriority( { biblionumber => $biblionumber } );
1351 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1352 CartToShelf( $itemnumber );
1358 =head2 ModReserveCancelAll
1360 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1362 function to cancel reserv,check other reserves, and transfer document if it's necessary
1366 sub ModReserveCancelAll {
1369 my ( $itemnumber, $borrowernumber ) = @_;
1371 #step 1 : cancel the reservation
1372 my $CancelReserve = CancelReserve({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
1374 #step 2 launch the subroutine of the others reserves
1375 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1377 return ( $messages, $nextreservinfo );
1380 =head2 ModReserveMinusPriority
1382 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1384 Reduce the values of queued list
1388 sub ModReserveMinusPriority {
1389 my ( $itemnumber, $reserve_id ) = @_;
1391 #first step update the value of the first person on reserv
1392 my $dbh = C4::Context->dbh;
1395 SET priority = 0 , itemnumber = ?
1396 WHERE reserve_id = ?
1398 my $sth_upd = $dbh->prepare($query);
1399 $sth_upd->execute( $itemnumber, $reserve_id );
1400 # second step update all others reserves
1401 _FixPriority({ reserve_id => $reserve_id, rank => '0' });
1404 =head2 GetReserveInfo
1406 &GetReserveInfo($reserve_id);
1408 Get item and borrower details for a current hold.
1409 Current implementation this query should have a single result.
1413 sub GetReserveInfo {
1414 my ( $reserve_id ) = @_;
1415 my $dbh = C4::Context->dbh;
1420 reserves.borrowernumber,
1421 reserves.biblionumber,
1422 reserves.branchcode,
1423 reserves.waitingdate,
1439 items.holdingbranch,
1440 items.itemcallnumber,
1446 LEFT JOIN items USING(itemnumber)
1447 LEFT JOIN borrowers USING(borrowernumber)
1448 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1449 WHERE reserves.reserve_id = ?";
1450 my $sth = $dbh->prepare($strsth);
1451 $sth->execute($reserve_id);
1453 my $data = $sth->fetchrow_hashref;
1457 =head2 IsAvailableForItemLevelRequest
1459 my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
1461 Checks whether a given item record is available for an
1462 item-level hold request. An item is available if
1464 * it is not lost AND
1465 * it is not damaged AND
1466 * it is not withdrawn AND
1467 * does not have a not for loan value > 0
1469 Need to check the issuingrules onshelfholds column,
1470 if this is set items on the shelf can be placed on hold
1472 Note that IsAvailableForItemLevelRequest() does not
1473 check if the staff operator is authorized to place
1474 a request on the item - in particular,
1475 this routine does not check IndependentBranches
1476 and canreservefromotherbranches.
1480 sub IsAvailableForItemLevelRequest {
1482 my $borrower = shift;
1484 my $dbh = C4::Context->dbh;
1485 # must check the notforloan setting of the itemtype
1486 # FIXME - a lot of places in the code do this
1487 # or something similar - need to be
1489 my $itype = _get_itype($item);
1490 my $notforloan_per_itemtype
1491 = $dbh->selectrow_array("SELECT notforloan FROM itemtypes WHERE itemtype = ?",
1495 $notforloan_per_itemtype ||
1496 $item->{itemlost} ||
1497 $item->{notforloan} > 0 ||
1498 $item->{withdrawn} ||
1499 ($item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems'));
1502 return 1 if _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
1504 return $item->{onloan} || GetReserveStatus($item->{itemnumber}) eq "Waiting";
1507 =head2 OnShelfHoldsAllowed
1509 OnShelfHoldsAllowed($itemtype,$borrowercategory,$branchcode);
1511 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see if onshelf
1512 holds are allowed, returns true if so.
1516 sub OnShelfHoldsAllowed {
1517 my ($item, $borrower) = @_;
1519 my $itype = _get_itype($item);
1520 return _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
1527 if (C4::Context->preference('item-level_itypes')) {
1528 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
1529 # When GetItem is fixed, we can remove this
1530 $itype = $item->{itype};
1533 # XXX This is a bit dodgy. It relies on biblio itemtype column having different name.
1534 # So if we already have a biblioitems join when calling this function,
1535 # we don't need to access the database again
1536 $itype = $item->{itemtype};
1539 my $dbh = C4::Context->dbh;
1540 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
1541 my $sth = $dbh->prepare($query);
1542 $sth->execute($item->{biblioitemnumber});
1543 if (my $data = $sth->fetchrow_hashref()){
1544 $itype = $data->{itemtype};
1550 sub _OnShelfHoldsAllowed {
1551 my ($itype,$borrowercategory,$branchcode) = @_;
1553 my $rule = C4::Circulation::GetIssuingRule($borrowercategory, $itype, $branchcode);
1554 return $rule->{onshelfholds};
1557 =head2 AlterPriority
1559 AlterPriority( $where, $reserve_id );
1561 This function changes a reserve's priority up, down, to the top, or to the bottom.
1562 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1567 my ( $where, $reserve_id ) = @_;
1569 my $dbh = C4::Context->dbh;
1571 my $reserve = GetReserve( $reserve_id );
1573 if ( $reserve->{cancellationdate} ) {
1574 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (".$reserve->{cancellationdate}.')';
1578 if ( $where eq 'up' || $where eq 'down' ) {
1580 my $priority = $reserve->{'priority'};
1581 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1582 _FixPriority({ reserve_id => $reserve_id, rank => $priority })
1584 } elsif ( $where eq 'top' ) {
1586 _FixPriority({ reserve_id => $reserve_id, rank => '1' })
1588 } elsif ( $where eq 'bottom' ) {
1590 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1595 =head2 ToggleLowestPriority
1597 ToggleLowestPriority( $borrowernumber, $biblionumber );
1599 This function sets the lowestPriority field to true if is false, and false if it is true.
1603 sub ToggleLowestPriority {
1604 my ( $reserve_id ) = @_;
1606 my $dbh = C4::Context->dbh;
1608 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1609 $sth->execute( $reserve_id );
1611 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1614 =head2 ToggleSuspend
1616 ToggleSuspend( $reserve_id );
1618 This function sets the suspend field to true if is false, and false if it is true.
1619 If the reserve is currently suspended with a suspend_until date, that date will
1620 be cleared when it is unsuspended.
1625 my ( $reserve_id, $suspend_until ) = @_;
1627 $suspend_until = dt_from_string($suspend_until) if ($suspend_until);
1629 my $hold = Koha::Holds->find( $reserve_id );
1631 if ( $hold->is_suspended ) {
1634 $hold->suspend_hold( $suspend_until );
1641 borrowernumber => $borrowernumber,
1642 [ biblionumber => $biblionumber, ]
1643 [ suspend_until => $suspend_until, ]
1644 [ suspend => $suspend ]
1647 This function accepts a set of hash keys as its parameters.
1648 It requires either borrowernumber or biblionumber, or both.
1650 suspend_until is wholly optional.
1657 my $borrowernumber = $params{'borrowernumber'} || undef;
1658 my $biblionumber = $params{'biblionumber'} || undef;
1659 my $suspend_until = $params{'suspend_until'} || undef;
1660 my $suspend = defined( $params{'suspend'} ) ? $params{'suspend'} : 1;
1662 $suspend_until = eval { dt_from_string($suspend_until) }
1663 if ( defined($suspend_until) );
1665 return unless ( $borrowernumber || $biblionumber );
1668 $params->{found} = undef;
1669 $params->{borrowernumber} = $borrowernumber if $borrowernumber;
1670 $params->{biblionumber} = $biblionumber if $biblionumber;
1672 my @holds = Koha::Holds->search($params);
1675 map { $_->suspend_hold($suspend_until) } @holds;
1678 map { $_->resume() } @holds;
1686 reserve_id => $reserve_id,
1688 [ignoreSetLowestRank => $ignoreSetLowestRank]
1693 _FixPriority({ biblionumber => $biblionumber});
1695 This routine adjusts the priority of a hold request and holds
1698 In the first form, where a reserve_id is passed, the priority of the
1699 hold is set to supplied rank, and other holds for that bib are adjusted
1700 accordingly. If the rank is "del", the hold is cancelled. If no rank
1701 is supplied, all of the holds on that bib have their priority adjusted
1702 as if the second form had been used.
1704 In the second form, where a biblionumber is passed, the holds on that
1705 bib (that are not captured) are sorted in order of increasing priority,
1706 then have reserves.priority set so that the first non-captured hold
1707 has its priority set to 1, the second non-captured hold has its priority
1708 set to 2, and so forth.
1710 In both cases, holds that have the lowestPriority flag on are have their
1711 priority adjusted to ensure that they remain at the end of the line.
1713 Note that the ignoreSetLowestRank parameter is meant to be used only
1714 when _FixPriority calls itself.
1719 my ( $params ) = @_;
1720 my $reserve_id = $params->{reserve_id};
1721 my $rank = $params->{rank} // '';
1722 my $ignoreSetLowestRank = $params->{ignoreSetLowestRank};
1723 my $biblionumber = $params->{biblionumber};
1725 my $dbh = C4::Context->dbh;
1727 unless ( $biblionumber ) {
1728 my $res = GetReserve( $reserve_id );
1729 $biblionumber = $res->{biblionumber};
1732 if ( $rank eq "del" ) {
1733 CancelReserve({ reserve_id => $reserve_id });
1735 elsif ( $rank eq "W" || $rank eq "0" ) {
1737 # make sure priority for waiting or in-transit items is 0
1741 WHERE reserve_id = ?
1742 AND found IN ('W', 'T')
1744 my $sth = $dbh->prepare($query);
1745 $sth->execute( $reserve_id );
1751 SELECT reserve_id, borrowernumber, reservedate
1753 WHERE biblionumber = ?
1754 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1755 ORDER BY priority ASC
1757 my $sth = $dbh->prepare($query);
1758 $sth->execute( $biblionumber );
1759 while ( my $line = $sth->fetchrow_hashref ) {
1760 push( @priority, $line );
1763 # To find the matching index
1765 my $key = -1; # to allow for 0 to be a valid result
1766 for ( $i = 0 ; $i < @priority ; $i++ ) {
1767 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1768 $key = $i; # save the index
1773 # if index exists in array then move it to new position
1774 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1775 my $new_rank = $rank -
1776 1; # $new_rank is what you want the new index to be in the array
1777 my $moving_item = splice( @priority, $key, 1 );
1778 splice( @priority, $new_rank, 0, $moving_item );
1781 # now fix the priority on those that are left....
1785 WHERE reserve_id = ?
1787 $sth = $dbh->prepare($query);
1788 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1791 $priority[$j]->{'reserve_id'}
1795 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1798 unless ( $ignoreSetLowestRank ) {
1799 while ( my $res = $sth->fetchrow_hashref() ) {
1801 reserve_id => $res->{'reserve_id'},
1803 ignoreSetLowestRank => 1
1809 =head2 _Findgroupreserve
1811 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead, $ignore_borrowers);
1813 Looks for a holds-queue based item-specific match first, then for a holds-queue title-level match, returning the
1814 first match found. If neither, then we look for non-holds-queue based holds.
1815 Lookahead is the number of days to look in advance.
1817 C<&_Findgroupreserve> returns :
1818 C<@results> is an array of references-to-hash whose keys are mostly
1819 fields from the reserves table of the Koha database, plus
1820 C<biblioitemnumber>.
1824 sub _Findgroupreserve {
1825 my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_;
1826 my $dbh = C4::Context->dbh;
1828 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1829 # check for exact targeted match
1830 my $item_level_target_query = qq{
1831 SELECT reserves.biblionumber AS biblionumber,
1832 reserves.borrowernumber AS borrowernumber,
1833 reserves.reservedate AS reservedate,
1834 reserves.branchcode AS branchcode,
1835 reserves.cancellationdate AS cancellationdate,
1836 reserves.found AS found,
1837 reserves.reservenotes AS reservenotes,
1838 reserves.priority AS priority,
1839 reserves.timestamp AS timestamp,
1840 biblioitems.biblioitemnumber AS biblioitemnumber,
1841 reserves.itemnumber AS itemnumber,
1842 reserves.reserve_id AS reserve_id,
1843 reserves.itemtype AS itemtype
1845 JOIN biblioitems USING (biblionumber)
1846 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1849 AND item_level_request = 1
1851 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1855 my $sth = $dbh->prepare($item_level_target_query);
1856 $sth->execute($itemnumber, $lookahead||0);
1858 if ( my $data = $sth->fetchrow_hashref ) {
1859 push( @results, $data )
1860 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1862 return @results if @results;
1864 # check for title-level targeted match
1865 my $title_level_target_query = qq{
1866 SELECT reserves.biblionumber AS biblionumber,
1867 reserves.borrowernumber AS borrowernumber,
1868 reserves.reservedate AS reservedate,
1869 reserves.branchcode AS branchcode,
1870 reserves.cancellationdate AS cancellationdate,
1871 reserves.found AS found,
1872 reserves.reservenotes AS reservenotes,
1873 reserves.priority AS priority,
1874 reserves.timestamp AS timestamp,
1875 biblioitems.biblioitemnumber AS biblioitemnumber,
1876 reserves.itemnumber AS itemnumber,
1877 reserves.reserve_id AS reserve_id,
1878 reserves.itemtype AS itemtype
1880 JOIN biblioitems USING (biblionumber)
1881 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1884 AND item_level_request = 0
1885 AND hold_fill_targets.itemnumber = ?
1886 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1890 $sth = $dbh->prepare($title_level_target_query);
1891 $sth->execute($itemnumber, $lookahead||0);
1893 if ( my $data = $sth->fetchrow_hashref ) {
1894 push( @results, $data )
1895 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1897 return @results if @results;
1900 SELECT reserves.biblionumber AS biblionumber,
1901 reserves.borrowernumber AS borrowernumber,
1902 reserves.reservedate AS reservedate,
1903 reserves.waitingdate AS waitingdate,
1904 reserves.branchcode AS branchcode,
1905 reserves.cancellationdate AS cancellationdate,
1906 reserves.found AS found,
1907 reserves.reservenotes AS reservenotes,
1908 reserves.priority AS priority,
1909 reserves.timestamp AS timestamp,
1910 reserves.itemnumber AS itemnumber,
1911 reserves.reserve_id AS reserve_id,
1912 reserves.itemtype AS itemtype
1914 WHERE reserves.biblionumber = ?
1915 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1916 AND reserves.reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1920 $sth = $dbh->prepare($query);
1921 $sth->execute( $biblio, $itemnumber, $lookahead||0);
1923 while ( my $data = $sth->fetchrow_hashref ) {
1924 push( @results, $data )
1925 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1930 =head2 _koha_notify_reserve
1932 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1934 Sends a notification to the patron that their hold has been filled (through
1935 ModReserveAffect, _not_ ModReserveFill)
1937 The letter code for this notice may be found using the following query:
1939 select distinct letter_code
1940 from message_transports
1941 inner join message_attributes using (message_attribute_id)
1942 where message_name = 'Hold_Filled'
1944 This will probably sipmly be 'HOLD', but because it is defined in the database,
1945 it is subject to addition or change.
1947 The following tables are availalbe witin the notice:
1958 sub _koha_notify_reserve {
1959 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1961 my $dbh = C4::Context->dbh;
1962 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
1964 # Try to get the borrower's email address
1965 my $to_address = C4::Members::GetNoticeEmailAddress($borrowernumber);
1967 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( {
1968 borrowernumber => $borrowernumber,
1969 message_name => 'Hold_Filled'
1972 my $sth = $dbh->prepare("
1975 WHERE borrowernumber = ?
1976 AND biblionumber = ?
1978 $sth->execute( $borrowernumber, $biblionumber );
1979 my $reserve = $sth->fetchrow_hashref;
1980 my $library = Koha::Libraries->find( $reserve->{branchcode} )->unblessed;
1982 my $admin_email_address = $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress');
1984 my %letter_params = (
1985 module => 'reserves',
1986 branchcode => $reserve->{branchcode},
1988 'branches' => $library,
1989 'borrowers' => $borrower,
1990 'biblio' => $biblionumber,
1991 'biblioitems' => $biblionumber,
1992 'reserves' => $reserve,
1993 'items', $reserve->{'itemnumber'},
1995 substitute => { today => output_pref( { dt => dt_from_string, dateonly => 1 } ) },
1998 my $notification_sent = 0; #Keeping track if a Hold_filled message is sent. If no message can be sent, then default to a print message.
1999 my $send_notification = sub {
2000 my ( $mtt, $letter_code ) = (@_);
2001 return unless defined $letter_code;
2002 $letter_params{letter_code} = $letter_code;
2003 $letter_params{message_transport_type} = $mtt;
2004 my $letter = C4::Letters::GetPreparedLetter ( %letter_params );
2006 warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
2010 C4::Letters::EnqueueLetter( {
2012 borrowernumber => $borrowernumber,
2013 from_address => $admin_email_address,
2014 message_transport_type => $mtt,
2018 while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) {
2020 ( $mtt eq 'email' and not $to_address ) # No email address
2021 or ( $mtt eq 'sms' and not $borrower->{smsalertnumber} ) # No SMS number
2022 or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
2025 &$send_notification($mtt, $letter_code);
2026 $notification_sent++;
2028 #Making sure that a print notification is sent if no other transport types can be utilized.
2029 if (! $notification_sent) {
2030 &$send_notification('print', 'HOLD');
2035 =head2 _ShiftPriorityByDateAndPriority
2037 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
2039 This increments the priority of all reserves after the one
2040 with either the lowest date after C<$reservedate>
2041 or the lowest priority after C<$priority>.
2043 It effectively makes room for a new reserve to be inserted with a certain
2044 priority, which is returned.
2046 This is most useful when the reservedate can be set by the user. It allows
2047 the new reserve to be placed before other reserves that have a later
2048 reservedate. Since priority also is set by the form in reserves/request.pl
2049 the sub accounts for that too.
2053 sub _ShiftPriorityByDateAndPriority {
2054 my ( $biblio, $resdate, $new_priority ) = @_;
2056 my $dbh = C4::Context->dbh;
2057 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
2058 my $sth = $dbh->prepare( $query );
2059 $sth->execute( $biblio, $resdate, $new_priority );
2060 my $min_priority = $sth->fetchrow;
2061 # if no such matches are found, $new_priority remains as original value
2062 $new_priority = $min_priority if ( $min_priority );
2064 # Shift the priority up by one; works in conjunction with the next SQL statement
2065 $query = "UPDATE reserves
2066 SET priority = priority+1
2067 WHERE biblionumber = ?
2068 AND borrowernumber = ?
2071 my $sth_update = $dbh->prepare( $query );
2073 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
2074 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
2075 $sth = $dbh->prepare( $query );
2076 $sth->execute( $new_priority, $biblio );
2077 while ( my $row = $sth->fetchrow_hashref ) {
2078 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
2081 return $new_priority; # so the caller knows what priority they wind up receiving
2084 =head2 OPACItemHoldsAllowed
2086 OPACItemHoldsAllowed($item_record,$borrower_record);
2088 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see
2089 if specific item holds are allowed, returns true if so.
2093 sub OPACItemHoldsAllowed {
2094 my ($item,$borrower) = @_;
2096 my $branchcode = $item->{homebranch} or die "No homebranch";
2098 my $dbh = C4::Context->dbh;
2099 if (C4::Context->preference('item-level_itypes')) {
2100 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
2101 # When GetItem is fixed, we can remove this
2102 $itype = $item->{itype};
2105 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
2106 my $sth = $dbh->prepare($query);
2107 $sth->execute($item->{biblioitemnumber});
2108 if (my $data = $sth->fetchrow_hashref()){
2109 $itype = $data->{itemtype};
2113 my $query = "SELECT opacitemholds,categorycode,itemtype,branchcode FROM issuingrules WHERE
2114 (issuingrules.categorycode = ? OR issuingrules.categorycode = '*')
2116 (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2118 (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')
2120 issuingrules.categorycode desc,
2121 issuingrules.itemtype desc,
2122 issuingrules.branchcode desc
2124 my $sth = $dbh->prepare($query);
2125 $sth->execute($borrower->{categorycode},$itype,$branchcode);
2126 my $data = $sth->fetchrow_hashref;
2127 my $opacitemholds = uc substr ($data->{opacitemholds}, 0, 1);
2128 return '' if $opacitemholds eq 'N';
2129 return $opacitemholds;
2134 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
2136 Use when checking out an item to handle reserves
2137 If $cancelreserve boolean is set to true, it will remove existing reserve
2142 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
2144 my $lookahead = C4::Context->preference('ConfirmFutureHolds'); #number of days to look for future holds
2145 my ( $restype, $res, $all_reserves ) = CheckReserves( $itemnumber, undef, $lookahead );
2148 my $biblionumber = $res->{biblionumber};
2149 my $biblioitemnumber = $res->{biblioitemnumber};
2151 if ($res->{borrowernumber} == $borrowernumber) {
2152 ModReserveFill($res);
2156 # The item is reserved by someone else.
2157 # Find this item in the reserves
2160 foreach (@$all_reserves) {
2161 $_->{'borrowernumber'} == $borrowernumber or next;
2162 $_->{'biblionumber'} == $biblionumber or next;
2169 # The item is reserved by the current patron
2170 ModReserveFill($borr_res);
2173 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
2174 RevertWaitingStatus({ itemnumber => $itemnumber });
2176 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
2177 CancelReserve( { reserve_id => $res->{'reserve_id'} } );
2184 MergeHolds($dbh,$to_biblio, $from_biblio);
2186 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
2191 my ( $dbh, $to_biblio, $from_biblio ) = @_;
2192 my $sth = $dbh->prepare(
2193 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
2195 $sth->execute($from_biblio);
2196 if ( my $data = $sth->fetchrow_hashref() ) {
2198 # holds exist on old record, if not we don't need to do anything
2199 $sth = $dbh->prepare(
2200 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
2201 $sth->execute( $to_biblio, $from_biblio );
2204 # don't reorder those already waiting
2206 $sth = $dbh->prepare(
2207 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
2209 my $upd_sth = $dbh->prepare(
2210 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
2211 AND reservedate = ? AND (itemnumber = ? or itemnumber is NULL) "
2213 $sth->execute( $to_biblio, 'W', 'T' );
2215 while ( my $reserve = $sth->fetchrow_hashref() ) {
2217 $priority, $to_biblio,
2218 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
2219 $reserve->{'itemnumber'}
2226 =head2 RevertWaitingStatus
2228 RevertWaitingStatus({ itemnumber => $itemnumber });
2230 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
2232 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
2233 item level hold, even if it was only a bibliolevel hold to
2234 begin with. This is because we can no longer know if a hold
2235 was item-level or bib-level after a hold has been set to
2240 sub RevertWaitingStatus {
2241 my ( $params ) = @_;
2242 my $itemnumber = $params->{'itemnumber'};
2244 return unless ( $itemnumber );
2246 my $dbh = C4::Context->dbh;
2248 ## Get the waiting reserve we want to revert
2250 SELECT * FROM reserves
2251 WHERE itemnumber = ?
2252 AND found IS NOT NULL
2254 my $sth = $dbh->prepare( $query );
2255 $sth->execute( $itemnumber );
2256 my $reserve = $sth->fetchrow_hashref();
2258 ## Increment the priority of all other non-waiting
2259 ## reserves for this bib record
2263 priority = priority + 1
2269 $sth = $dbh->prepare( $query );
2270 $sth->execute( $reserve->{'biblionumber'} );
2272 ## Fix up the currently waiting reserve
2282 $sth = $dbh->prepare( $query );
2283 $sth->execute( $reserve->{'reserve_id'} );
2284 _FixPriority( { biblionumber => $reserve->{biblionumber} } );
2289 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
2291 Returnes the first reserve id that matches the given criteria
2296 my ( $params ) = @_;
2298 return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
2300 my $dbh = C4::Context->dbh();
2302 my $sql = "SELECT reserve_id FROM reserves WHERE ";
2306 foreach my $key ( keys %$params ) {
2307 if ( defined( $params->{$key} ) ) {
2308 push( @limits, "$key = ?" );
2309 push( @params, $params->{$key} );
2313 $sql .= join( " AND ", @limits );
2315 my $sth = $dbh->prepare( $sql );
2316 $sth->execute( @params );
2317 my $row = $sth->fetchrow_hashref();
2319 return $row->{'reserve_id'};
2324 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
2326 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2328 The letter code will be HOLD_SLIP, and the following tables are
2329 available within the slip:
2341 my ($branch, $borrowernumber, $biblionumber) = @_;
2343 # return unless ( C4::Context->boolean_preference('printreserveslips') );
2345 my $reserve_id = GetReserveId({
2346 biblionumber => $biblionumber,
2347 borrowernumber => $borrowernumber
2349 my $reserve = GetReserveInfo($reserve_id) or return;
2351 return C4::Letters::GetPreparedLetter (
2352 module => 'circulation',
2353 letter_code => 'HOLD_SLIP',
2354 branchcode => $branch,
2356 'reserves' => $reserve,
2357 'branches' => $reserve->{branchcode},
2358 'borrowers' => $reserve->{borrowernumber},
2359 'biblio' => $reserve->{biblionumber},
2360 'biblioitems' => $reserve->{biblionumber},
2361 'items' => $reserve->{itemnumber},
2366 =head2 GetReservesControlBranch
2368 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2370 Return the branchcode to be used to determine which reserves
2371 policy applies to a transaction.
2373 C<$item> is a hashref for an item. Only 'homebranch' is used.
2375 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2379 sub GetReservesControlBranch {
2380 my ( $item, $borrower ) = @_;
2382 my $reserves_control = C4::Context->preference('ReservesControlBranch');
2385 ( $reserves_control eq 'ItemHomeLibrary' ) ? $item->{'homebranch'}
2386 : ( $reserves_control eq 'PatronLibrary' ) ? $borrower->{'branchcode'}
2392 =head2 CalculatePriority
2394 my $p = CalculatePriority($biblionumber, $resdate);
2396 Calculate priority for a new reserve on biblionumber, placing it at
2397 the end of the line of all holds whose start date falls before
2398 the current system time and that are neither on the hold shelf
2401 The reserve date parameter is optional; if it is supplied, the
2402 priority is based on the set of holds whose start date falls before
2403 the parameter value.
2405 After calculation of this priority, it is recommended to call
2406 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2411 sub CalculatePriority {
2412 my ( $biblionumber, $resdate ) = @_;
2415 SELECT COUNT(*) FROM reserves
2416 WHERE biblionumber = ?
2418 AND (found IS NULL OR found = '')
2420 #skip found==W or found==T (waiting or transit holds)
2422 $sql.= ' AND ( reservedate <= ? )';
2425 $sql.= ' AND ( reservedate < NOW() )';
2427 my $dbh = C4::Context->dbh();
2428 my @row = $dbh->selectrow_array(
2431 $resdate ? ($biblionumber, $resdate) : ($biblionumber)
2434 return @row ? $row[0]+1 : 1;
2437 =head2 IsItemOnHoldAndFound
2439 my $bool = IsItemFoundHold( $itemnumber );
2441 Returns true if the item is currently on hold
2442 and that hold has a non-null found status ( W, T, etc. )
2446 sub IsItemOnHoldAndFound {
2447 my ($itemnumber) = @_;
2449 my $rs = Koha::Database->new()->schema()->resultset('Reserve');
2451 my $found = $rs->count(
2453 itemnumber => $itemnumber,
2454 found => { '!=' => undef }
2463 Koha Development Team <http://koha-community.org/>