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;
37 use C4::Branch qw( GetBranchDetail );
45 use List::MoreUtils qw( firstidx any );
48 use vars qw($VERSION @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
94 # set the version for version checking
95 $VERSION = 3.07.00.049;
102 &GetReservesFromItemnumber
103 &GetReservesFromBiblionumber
104 &GetReservesFromBorrowernumber
105 &GetReservesForBranch
118 &ModReserveMinusPriority
124 &CanReserveBeCanceledFromOpac
126 &CancelExpiredReserves
128 &AutoUnsuspendReserves
130 &IsAvailableForItemLevelRequest
132 &OPACItemHoldsAllowed
135 &ToggleLowestPriority
141 &GetReservesControlBranch
145 @EXPORT_OK = qw( MergeHolds );
150 AddReserve($branch,$borrowernumber,$biblionumber,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
152 Adds reserve and generates HOLDPLACED message.
154 The following tables are available witin the HOLDPLACED message:
166 $branch, $borrowernumber, $biblionumber,
167 $bibitems, $priority, $resdate, $expdate, $notes,
168 $title, $checkitem, $found
171 if ( Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->count() > 0 ) {
172 carp("AddReserve: borrower $borrowernumber already has a hold for biblionumber $biblionumber");
176 my $dbh = C4::Context->dbh;
178 $resdate = output_pref( { str => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' })
179 or output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
181 $expdate = output_pref({ str => $expdate, dateonly => 1, dateformat => 'iso' });
183 if ( C4::Context->preference('AllowHoldDateInFuture') ) {
185 # Make room in reserves for this before those of a later reserve date
186 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
191 # If the reserv had the waiting status, we had the value of the resdate
192 if ( $found eq 'W' ) {
193 $waitingdate = $resdate;
196 # updates take place here
197 my $hold = Koha::Hold->new(
199 borrowernumber => $borrowernumber,
200 biblionumber => $biblionumber,
201 reservedate => $resdate,
202 branchcode => $branch,
203 priority => $priority,
204 reservenotes => $notes,
205 itemnumber => $checkitem,
207 waitingdate => $waitingdate,
208 expirationdate => $expdate
211 my $reserve_id = $hold->id();
213 # add a reserve fee if needed
214 my $fee = GetReserveFee( $borrowernumber, $biblionumber );
215 ChargeReserveFee( $borrowernumber, $fee, $title );
217 _FixPriority({ biblionumber => $biblionumber});
219 # Send e-mail to librarian if syspref is active
220 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
221 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
222 my $branch_details = C4::Branch::GetBranchDetail($borrower->{branchcode});
223 if ( my $letter = C4::Letters::GetPreparedLetter (
224 module => 'reserves',
225 letter_code => 'HOLDPLACED',
226 branchcode => $branch,
228 'branches' => $branch_details,
229 'borrowers' => $borrower,
230 'biblio' => $biblionumber,
231 'biblioitems' => $biblionumber,
232 'items' => $checkitem,
236 my $admin_email_address =$branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
238 C4::Letters::EnqueueLetter(
240 borrowernumber => $borrowernumber,
241 message_transport_type => 'email',
242 from_address => $admin_email_address,
243 to_address => $admin_email_address,
254 $res = GetReserve( $reserve_id );
256 Return the current reserve.
261 my ($reserve_id) = @_;
263 my $dbh = C4::Context->dbh;
265 my $query = "SELECT * FROM reserves WHERE reserve_id = ?";
266 my $sth = $dbh->prepare( $query );
267 $sth->execute( $reserve_id );
268 return $sth->fetchrow_hashref();
271 =head2 GetReservesFromBiblionumber
273 my $reserves = GetReservesFromBiblionumber({
274 biblionumber => $biblionumber,
275 [ itemnumber => $itemnumber, ]
279 This function gets the list of reservations for one C<$biblionumber>,
280 returning an arrayref pointing to the reserves for C<$biblionumber>.
282 By default, only reserves whose start date falls before the current
283 time are returned. To return all reserves, including future ones,
284 the C<all_dates> parameter can be included and set to a true value.
286 If the C<itemnumber> parameter is supplied, reserves must be targeted
287 to that item or not targeted to any item at all; otherwise, they
288 are excluded from the list.
292 sub GetReservesFromBiblionumber {
294 my $biblionumber = $params->{biblionumber} or return [];
295 my $itemnumber = $params->{itemnumber};
296 my $all_dates = $params->{all_dates} // 0;
297 my $dbh = C4::Context->dbh;
299 # Find the desired items in the reserves
304 timestamp AS rtimestamp,
317 WHERE biblionumber = ? ";
318 push( @params, $biblionumber );
319 unless ( $all_dates ) {
320 $query .= " AND reservedate <= CAST(NOW() AS DATE) ";
323 $query .= " AND ( itemnumber IS NULL OR itemnumber = ? )";
324 push( @params, $itemnumber );
326 $query .= "ORDER BY priority";
327 my $sth = $dbh->prepare($query);
328 $sth->execute( @params );
330 while ( my $data = $sth->fetchrow_hashref ) {
331 push @results, $data;
336 =head2 GetReservesFromItemnumber
338 ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $waitingdate ) = GetReservesFromItemnumber($itemnumber);
340 Get the first reserve for a specific item number (based on priority). Returns the abovementioned values for that reserve.
342 The routine does not look at future reserves (read: item level holds), but DOES include future waits (a confirmed future hold).
346 sub GetReservesFromItemnumber {
347 my ($itemnumber) = @_;
349 my $schema = Koha::Database->new()->schema();
351 my $r = $schema->resultset('Reserve')->search(
353 itemnumber => $itemnumber,
356 reservedate => \'<= CAST( NOW() AS DATE )',
357 waitingdate => { '!=', undef }
361 order_by => 'priority',
369 $r->get_column('borrowernumber'),
370 $r->get_column('branchcode'),
376 =head2 GetReservesFromBorrowernumber
378 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
384 sub GetReservesFromBorrowernumber {
385 my ( $borrowernumber, $status ) = @_;
386 my $dbh = C4::Context->dbh;
389 $sth = $dbh->prepare("
392 WHERE borrowernumber=?
396 $sth->execute($borrowernumber,$status);
398 $sth = $dbh->prepare("
401 WHERE borrowernumber=?
404 $sth->execute($borrowernumber);
406 my $data = $sth->fetchall_arrayref({});
410 =head2 CanBookBeReserved
412 $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
413 if ($canReserve eq 'OK') { #We can reserve this Item! }
415 See CanItemBeReserved() for possible return values.
419 sub CanBookBeReserved{
420 my ($borrowernumber, $biblionumber) = @_;
422 my $items = GetItemnumbersForBiblio($biblionumber);
423 #get items linked via host records
424 my @hostitems = get_hostitemnumbers_of($biblionumber);
426 push (@$items,@hostitems);
430 foreach my $item (@$items) {
431 $canReserve = CanItemBeReserved( $borrowernumber, $item );
432 return 'OK' if $canReserve eq 'OK';
437 =head2 CanItemBeReserved
439 $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber)
440 if ($canReserve eq 'OK') { #We can reserve this Item! }
442 @RETURNS OK, if the Item can be reserved.
443 ageRestricted, if the Item is age restricted for this borrower.
444 damaged, if the Item is damaged.
445 cannotReserveFromOtherBranches, if syspref 'canreservefromotherbranches' is OK.
446 tooManyReserves, if the borrower has exceeded his maximum reserve amount.
447 notReservable, if holds on this item are not allowed
451 sub CanItemBeReserved{
452 my ($borrowernumber, $itemnumber) = @_;
454 my $dbh = C4::Context->dbh;
455 my $ruleitemtype; # itemtype of the matching issuing rule
456 my $allowedreserves = 0;
458 # we retrieve borrowers and items informations #
459 # item->{itype} will come for biblioitems if necessery
460 my $item = GetItem($itemnumber);
461 my $biblioData = C4::Biblio::GetBiblioData( $item->{biblionumber} );
462 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
464 # If an item is damaged and we don't allow holds on damaged items, we can stop right here
465 return 'damaged' if ( $item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems') );
467 #Check for the age restriction
468 my ($ageRestriction, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction( $biblioData->{agerestriction}, $borrower );
469 return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
471 my $controlbranch = C4::Context->preference('ReservesControlBranch');
473 # we retrieve user rights on this itemtype and branchcode
474 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
476 WHERE (categorycode in (?,'*') )
477 AND (itemtype IN (?,'*'))
478 AND (branchcode IN (?,'*'))
485 my $querycount ="SELECT
488 LEFT JOIN items USING (itemnumber)
489 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
490 LEFT JOIN borrowers USING (borrowernumber)
491 WHERE borrowernumber = ?
496 my $branchfield = "reserves.branchcode";
498 if( $controlbranch eq "ItemHomeLibrary" ){
499 $branchfield = "items.homebranch";
500 $branchcode = $item->{homebranch};
501 }elsif( $controlbranch eq "PatronLibrary" ){
502 $branchfield = "borrowers.branchcode";
503 $branchcode = $borrower->{branchcode};
507 $sth->execute($borrower->{'categorycode'}, $item->{'itype'}, $branchcode);
508 if(my $rights = $sth->fetchrow_hashref()){
509 $ruleitemtype = $rights->{itemtype};
510 $allowedreserves = $rights->{reservesallowed};
517 $querycount .= "AND $branchfield = ?";
519 # If using item-level itypes, fall back to the record
520 # level itemtype if the hold has no associated item
522 C4::Context->preference('item-level_itypes')
523 ? " AND COALESCE( itype, itemtype ) = ?"
524 : " AND itemtype = ?"
525 if ( $ruleitemtype ne "*" );
527 my $sthcount = $dbh->prepare($querycount);
529 if($ruleitemtype eq "*"){
530 $sthcount->execute($borrowernumber, $branchcode);
532 $sthcount->execute($borrowernumber, $branchcode, $ruleitemtype);
535 my $reservecount = "0";
536 if(my $rowcount = $sthcount->fetchrow_hashref()){
537 $reservecount = $rowcount->{count};
539 # we check if it's ok or not
540 if( $reservecount >= $allowedreserves ){
541 return 'tooManyReserves';
544 my $circ_control_branch = C4::Circulation::_GetCircControlBranch($item,
546 my $branchitemrule = C4::Circulation::GetBranchItemRule($circ_control_branch,
549 if ( $branchitemrule->{holdallowed} == 0 ) {
550 return 'notReservable';
553 if ( $branchitemrule->{holdallowed} == 1
554 && $borrower->{branchcode} ne $item->{homebranch} )
556 return 'cannotReserveFromOtherBranches';
559 # If reservecount is ok, we check item branch if IndependentBranches is ON
560 # and canreservefromotherbranches is OFF
561 if ( C4::Context->preference('IndependentBranches')
562 and !C4::Context->preference('canreservefromotherbranches') )
564 my $itembranch = $item->{homebranch};
565 if ($itembranch ne $borrower->{branchcode}) {
566 return 'cannotReserveFromOtherBranches';
573 =head2 CanReserveBeCanceledFromOpac
575 $number = CanReserveBeCanceledFromOpac($reserve_id, $borrowernumber);
577 returns 1 if reserve can be cancelled by user from OPAC.
578 First check if reserve belongs to user, next checks if reserve is not in
579 transfer or waiting status
583 sub CanReserveBeCanceledFromOpac {
584 my ($reserve_id, $borrowernumber) = @_;
586 return unless $reserve_id and $borrowernumber;
587 my $reserve = GetReserve($reserve_id);
589 return 0 unless $reserve->{borrowernumber} == $borrowernumber;
590 return 0 if ( $reserve->{found} eq 'W' ) or ( $reserve->{found} eq 'T' );
596 =head2 GetReserveCount
598 $number = &GetReserveCount($borrowernumber);
600 this function returns the number of reservation for a borrower given on input arg.
604 sub GetReserveCount {
605 my ($borrowernumber) = @_;
607 my $dbh = C4::Context->dbh;
610 SELECT COUNT(*) AS counter
612 WHERE borrowernumber = ?
614 my $sth = $dbh->prepare($query);
615 $sth->execute($borrowernumber);
616 my $row = $sth->fetchrow_hashref;
617 return $row->{counter};
620 =head2 GetOtherReserves
622 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
624 Check queued list of this document and check if this document must be transferred
628 sub GetOtherReserves {
629 my ($itemnumber) = @_;
632 my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
633 if ($checkreserves) {
634 my $iteminfo = GetItem($itemnumber);
635 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
636 $messages->{'transfert'} = $checkreserves->{'branchcode'};
637 #minus priorities of others reservs
638 ModReserveMinusPriority(
640 $checkreserves->{'reserve_id'},
643 #launch the subroutine dotransfer
644 C4::Items::ModItemTransfer(
646 $iteminfo->{'holdingbranch'},
647 $checkreserves->{'branchcode'}
652 #step 2b : case of a reservation on the same branch, set the waiting status
654 $messages->{'waiting'} = 1;
655 ModReserveMinusPriority(
657 $checkreserves->{'reserve_id'},
659 ModReserveStatus($itemnumber,'W');
662 $nextreservinfo = $checkreserves->{'borrowernumber'};
665 return ( $messages, $nextreservinfo );
668 =head2 ChargeReserveFee
670 $fee = ChargeReserveFee( $borrowernumber, $fee, $title );
672 Charge the fee for a reserve (if $fee > 0)
676 sub ChargeReserveFee {
677 my ( $borrowernumber, $fee, $title ) = @_;
678 return if !$fee || $fee==0; # the last test is needed to include 0.00
680 INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
682 my $dbh = C4::Context->dbh;
683 my $nextacctno = &getnextacctno( $borrowernumber );
684 $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
689 $fee = GetReserveFee( $borrowernumber, $biblionumber );
691 Calculate the fee for a reserve (if applicable).
696 my ( $borrowernumber, $biblionumber ) = @_;
698 SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE borrowernumber = ?
701 SELECT COUNT(*) FROM items
702 LEFT JOIN issues USING (itemnumber)
703 WHERE items.biblionumber=? AND issues.issue_id IS NULL
706 SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?
709 my $dbh = C4::Context->dbh;
710 my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
711 if( $fee && $fee > 0 ) {
712 # This is a reconstruction of the old code:
713 # Compare number of items with items issued, and optionally check holds
714 # If not all items are issued and there are no holds: charge no fee
715 # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
716 my ( $notissued, $reserved );
717 ( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
720 ( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
721 ( $biblionumber, $borrowernumber ) );
722 $fee = 0 if $reserved == 0;
728 =head2 GetReservesToBranch
730 @transreserv = GetReservesToBranch( $frombranch );
732 Get reserve list for a given branch
736 sub GetReservesToBranch {
737 my ( $frombranch ) = @_;
738 my $dbh = C4::Context->dbh;
739 my $sth = $dbh->prepare(
740 "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
745 $sth->execute( $frombranch );
748 while ( my $data = $sth->fetchrow_hashref ) {
749 $transreserv[$i] = $data;
752 return (@transreserv);
755 =head2 GetReservesForBranch
757 @transreserv = GetReservesForBranch($frombranch);
761 sub GetReservesForBranch {
762 my ($frombranch) = @_;
763 my $dbh = C4::Context->dbh;
766 SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
771 $query .= " AND branchcode=? " if ( $frombranch );
772 $query .= "ORDER BY waitingdate" ;
774 my $sth = $dbh->prepare($query);
776 $sth->execute($frombranch);
783 while ( my $data = $sth->fetchrow_hashref ) {
784 $transreserv[$i] = $data;
787 return (@transreserv);
790 =head2 GetReserveStatus
792 $reservestatus = GetReserveStatus($itemnumber);
794 Takes an itemnumber and returns the status of the reserve placed on it.
795 If several reserves exist, the reserve with the lower priority is given.
799 ## FIXME: I don't think this does what it thinks it does.
800 ## It only ever checks the first reserve result, even though
801 ## multiple reserves for that bib can have the itemnumber set
802 ## the sub is only used once in the codebase.
803 sub GetReserveStatus {
804 my ($itemnumber) = @_;
806 my $dbh = C4::Context->dbh;
808 my ($sth, $found, $priority);
810 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
811 $sth->execute($itemnumber);
812 ($found, $priority) = $sth->fetchrow_array;
816 return 'Waiting' if $found eq 'W' and $priority == 0;
817 return 'Finished' if $found eq 'F';
820 return 'Reserved' if $priority > 0;
822 return ''; # empty string here will remove need for checking undef, or less log lines
827 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
828 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
829 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
831 Find a book in the reserves.
833 C<$itemnumber> is the book's item number.
834 C<$lookahead> is the number of days to look in advance for future reserves.
836 As I understand it, C<&CheckReserves> looks for the given item in the
837 reserves. If it is found, that's a match, and C<$status> is set to
840 Otherwise, it finds the most important item in the reserves with the
841 same biblio number as this book (I'm not clear on this) and returns it
842 with C<$status> set to C<Reserved>.
844 C<&CheckReserves> returns a two-element list:
846 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
848 C<$reserve> is the reserve item that matched. It is a
849 reference-to-hash whose keys are mostly the fields of the reserves
850 table in the Koha database.
855 my ( $item, $barcode, $lookahead_days, $ignore_borrowers) = @_;
856 my $dbh = C4::Context->dbh;
859 if (C4::Context->preference('item-level_itypes')){
861 SELECT items.biblionumber,
862 items.biblioitemnumber,
863 itemtypes.notforloan,
864 items.notforloan AS itemnotforloan,
870 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
871 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
876 SELECT items.biblionumber,
877 items.biblioitemnumber,
878 itemtypes.notforloan,
879 items.notforloan AS itemnotforloan,
885 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
886 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
891 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
892 $sth->execute($item);
895 $sth = $dbh->prepare("$select WHERE barcode = ?");
896 $sth->execute($barcode);
898 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
899 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged, $item_homebranch, $item_holdingbranch ) = $sth->fetchrow_array;
901 return if ( $damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') );
903 return unless $itemnumber; # bail if we got nothing.
905 # if item is not for loan it cannot be reserved either.....
906 # except where items.notforloan < 0 : This indicates the item is holdable.
907 return if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
909 # Find this item in the reserves
910 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber, $lookahead_days, $ignore_borrowers);
912 # $priority and $highest are used to find the most important item
913 # in the list returned by &_Findgroupreserve. (The lower $priority,
914 # the more important the item.)
915 # $highest is the most important item we've seen so far.
917 if (scalar @reserves) {
918 my $LocalHoldsPriority = C4::Context->preference('LocalHoldsPriority');
919 my $LocalHoldsPriorityPatronControl = C4::Context->preference('LocalHoldsPriorityPatronControl');
920 my $LocalHoldsPriorityItemControl = C4::Context->preference('LocalHoldsPriorityItemControl');
922 my $priority = 10000000;
923 foreach my $res (@reserves) {
924 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
925 return ( "Waiting", $res, \@reserves ); # Found it
929 my $local_hold_match;
931 if ($LocalHoldsPriority) {
932 $borrowerinfo = C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
933 $iteminfo = C4::Items::GetItem($itemnumber);
935 my $local_holds_priority_item_branchcode =
936 $iteminfo->{$LocalHoldsPriorityItemControl};
937 my $local_holds_priority_patron_branchcode =
938 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
940 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
941 ? $borrowerinfo->{branchcode}
944 $local_holds_priority_item_branchcode eq
945 $local_holds_priority_patron_branchcode;
948 # See if this item is more important than what we've got so far
949 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
950 $borrowerinfo ||= C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
951 $iteminfo ||= C4::Items::GetItem($itemnumber);
952 my $branch = GetReservesControlBranch( $iteminfo, $borrowerinfo );
953 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
954 next if ($branchitemrule->{'holdallowed'} == 0);
955 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
956 $priority = $res->{'priority'};
958 last if $local_hold_match;
964 # If we get this far, then no exact match was found.
965 # We return the most important (i.e. next) reservation.
967 $highest->{'itemnumber'} = $item;
968 return ( "Reserved", $highest, \@reserves );
974 =head2 CancelExpiredReserves
976 CancelExpiredReserves();
978 Cancels all reserves with an expiration date from before today.
982 sub CancelExpiredReserves {
984 # Cancel reserves that have passed their expiration date.
985 my $dbh = C4::Context->dbh;
986 my $sth = $dbh->prepare( "
987 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
988 AND expirationdate IS NOT NULL
993 while ( my $res = $sth->fetchrow_hashref() ) {
994 CancelReserve({ reserve_id => $res->{'reserve_id'} });
997 # Cancel reserves that have been waiting too long
998 if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
999 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
1000 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
1002 my $today = dt_from_string();
1004 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
1005 $sth = $dbh->prepare( $query );
1006 $sth->execute( $max_pickup_delay );
1008 while ( my $res = $sth->fetchrow_hashref ) {
1010 unless ( $cancel_on_holidays ) {
1011 my $calendar = Koha::Calendar->new( branchcode => $res->{'branchcode'} );
1012 my $is_holiday = $calendar->is_holiday( $today );
1014 if ( $is_holiday ) {
1020 CancelReserve({ reserve_id => $res->{'reserve_id'}, charge_cancel_fee => 1 });
1027 =head2 AutoUnsuspendReserves
1029 AutoUnsuspendReserves();
1031 Unsuspends all suspended reserves with a suspend_until date from before today.
1035 sub AutoUnsuspendReserves {
1037 my $dbh = C4::Context->dbh;
1039 my $query = "UPDATE reserves SET suspend = 0, suspend_until = NULL WHERE DATE( suspend_until ) < DATE( CURDATE() )";
1040 my $sth = $dbh->prepare( $query );
1045 =head2 CancelReserve
1047 CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber, ] [ charge_cancel_fee => 1 ] });
1049 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.
1054 my ( $params ) = @_;
1056 my $reserve_id = $params->{'reserve_id'};
1057 # Filter out only the desired keys; this will insert undefined values for elements missing in
1058 # \%params, but GetReserveId filters them out anyway.
1059 $reserve_id = GetReserveId( { biblionumber => $params->{'biblionumber'}, borrowernumber => $params->{'borrowernumber'}, itemnumber => $params->{'itemnumber'} } ) unless ( $reserve_id );
1061 return unless ( $reserve_id );
1063 my $dbh = C4::Context->dbh;
1065 my $reserve = GetReserve( $reserve_id );
1069 SET cancellationdate = now(),
1072 WHERE reserve_id = ?
1074 my $sth = $dbh->prepare($query);
1075 $sth->execute( $reserve_id );
1078 INSERT INTO old_reserves
1079 SELECT * FROM reserves
1080 WHERE reserve_id = ?
1082 $sth = $dbh->prepare($query);
1083 $sth->execute( $reserve_id );
1086 DELETE FROM reserves
1087 WHERE reserve_id = ?
1089 $sth = $dbh->prepare($query);
1090 $sth->execute( $reserve_id );
1092 # now fix the priority on the others....
1093 _FixPriority({ biblionumber => $reserve->{biblionumber} });
1095 # and, if desired, charge a cancel fee
1096 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
1097 if ( $charge && $params->{'charge_cancel_fee'} ) {
1098 manualinvoice($reserve->{'borrowernumber'}, $reserve->{'itemnumber'}, 'Hold waiting too long', 'F', $charge);
1107 ModReserve({ rank => $rank,
1108 reserve_id => $reserve_id,
1109 branchcode => $branchcode
1110 [, itemnumber => $itemnumber ]
1111 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
1114 Change a hold request's priority or cancel it.
1116 C<$rank> specifies the effect of the change. If C<$rank>
1117 is 'W' or 'n', nothing happens. This corresponds to leaving a
1118 request alone when changing its priority in the holds queue
1121 If C<$rank> is 'del', the hold request is cancelled.
1123 If C<$rank> is an integer greater than zero, the priority of
1124 the request is set to that value. Since priority != 0 means
1125 that the item is not waiting on the hold shelf, setting the
1126 priority to a non-zero value also sets the request's found
1127 status and waiting date to NULL.
1129 The optional C<$itemnumber> parameter is used only when
1130 C<$rank> is a non-zero integer; if supplied, the itemnumber
1131 of the hold request is set accordingly; if omitted, the itemnumber
1134 B<FIXME:> Note that the forgoing can have the effect of causing
1135 item-level hold requests to turn into title-level requests. This
1136 will be fixed once reserves has separate columns for requested
1137 itemnumber and supplying itemnumber.
1142 my ( $params ) = @_;
1144 my $rank = $params->{'rank'};
1145 my $reserve_id = $params->{'reserve_id'};
1146 my $branchcode = $params->{'branchcode'};
1147 my $itemnumber = $params->{'itemnumber'};
1148 my $suspend_until = $params->{'suspend_until'};
1149 my $borrowernumber = $params->{'borrowernumber'};
1150 my $biblionumber = $params->{'biblionumber'};
1152 return if $rank eq "W";
1153 return if $rank eq "n";
1155 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
1156 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber }) unless ( $reserve_id );
1158 my $dbh = C4::Context->dbh;
1159 if ( $rank eq "del" ) {
1160 CancelReserve({ reserve_id => $reserve_id });
1162 elsif ($rank =~ /^\d+/ and $rank > 0) {
1164 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1165 WHERE reserve_id = ?
1167 my $sth = $dbh->prepare($query);
1168 $sth->execute( $rank, $branchcode, $itemnumber, $reserve_id );
1170 if ( defined( $suspend_until ) ) {
1171 if ( $suspend_until ) {
1172 $suspend_until = eval { output_pref( { dt => dt_from_string( $suspend_until ), dateonly => 1, dateformat => 'iso' }); };
1173 $dbh->do("UPDATE reserves SET suspend = 1, suspend_until = ? WHERE reserve_id = ?", undef, ( $suspend_until, $reserve_id ) );
1175 $dbh->do("UPDATE reserves SET suspend_until = NULL WHERE reserve_id = ?", undef, ( $reserve_id ) );
1179 _FixPriority({ reserve_id => $reserve_id, rank =>$rank });
1183 =head2 ModReserveFill
1185 &ModReserveFill($reserve);
1187 Fill a reserve. If I understand this correctly, this means that the
1188 reserved book has been found and given to the patron who reserved it.
1190 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1191 whose keys are fields from the reserves table in the Koha database.
1195 sub ModReserveFill {
1197 my $dbh = C4::Context->dbh;
1198 # fill in a reserve record....
1199 my $reserve_id = $res->{'reserve_id'};
1200 my $biblionumber = $res->{'biblionumber'};
1201 my $borrowernumber = $res->{'borrowernumber'};
1202 my $resdate = $res->{'reservedate'};
1204 # get the priority on this record....
1206 my $query = "SELECT priority
1208 WHERE biblionumber = ?
1209 AND borrowernumber = ?
1210 AND reservedate = ?";
1211 my $sth = $dbh->prepare($query);
1212 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1213 ($priority) = $sth->fetchrow_array;
1215 # update the database...
1216 $query = "UPDATE reserves
1219 WHERE biblionumber = ?
1221 AND borrowernumber = ?
1223 $sth = $dbh->prepare($query);
1224 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1226 # move to old_reserves
1227 $query = "INSERT INTO old_reserves
1228 SELECT * FROM reserves
1229 WHERE biblionumber = ?
1231 AND borrowernumber = ?
1233 $sth = $dbh->prepare($query);
1234 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1235 $query = "DELETE FROM reserves
1236 WHERE biblionumber = ?
1238 AND borrowernumber = ?
1240 $sth = $dbh->prepare($query);
1241 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1243 # now fix the priority on the others (if the priority wasn't
1244 # already sorted!)....
1245 unless ( $priority == 0 ) {
1246 _FixPriority({ reserve_id => $reserve_id, biblionumber => $biblionumber });
1250 =head2 ModReserveStatus
1252 &ModReserveStatus($itemnumber, $newstatus);
1254 Update the reserve status for the active (priority=0) reserve.
1256 $itemnumber is the itemnumber the reserve is on
1258 $newstatus is the new status.
1262 sub ModReserveStatus {
1264 #first : check if we have a reservation for this item .
1265 my ($itemnumber, $newstatus) = @_;
1266 my $dbh = C4::Context->dbh;
1268 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1269 my $sth_set = $dbh->prepare($query);
1270 $sth_set->execute( $newstatus, $itemnumber );
1272 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1273 CartToShelf( $itemnumber );
1277 =head2 ModReserveAffect
1279 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1281 This function affect an item and a status for a given reserve
1282 The itemnumber parameter is used to find the biblionumber.
1283 with the biblionumber & the borrowernumber, we can affect the itemnumber
1284 to the correct reserve.
1286 if $transferToDo is not set, then the status is set to "Waiting" as well.
1287 otherwise, a transfer is on the way, and the end of the transfer will
1288 take care of the waiting status
1292 sub ModReserveAffect {
1293 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1294 my $dbh = C4::Context->dbh;
1296 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1297 # attached to $itemnumber
1298 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1299 $sth->execute($itemnumber);
1300 my ($biblionumber) = $sth->fetchrow;
1302 # get request - need to find out if item is already
1303 # waiting in order to not send duplicate hold filled notifications
1304 my $reserve_id = GetReserveId({
1305 borrowernumber => $borrowernumber,
1306 biblionumber => $biblionumber,
1308 return unless defined $reserve_id;
1309 my $request = GetReserveInfo($reserve_id);
1310 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1312 # If we affect a reserve that has to be transferred, don't set to Waiting
1314 if ($transferToDo) {
1320 WHERE borrowernumber = ?
1321 AND biblionumber = ?
1325 # affect the reserve to Waiting as well.
1330 waitingdate = NOW(),
1332 WHERE borrowernumber = ?
1333 AND biblionumber = ?
1336 $sth = $dbh->prepare($query);
1337 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1338 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1339 _FixPriority( { biblionumber => $biblionumber } );
1340 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1341 CartToShelf( $itemnumber );
1347 =head2 ModReserveCancelAll
1349 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1351 function to cancel reserv,check other reserves, and transfer document if it's necessary
1355 sub ModReserveCancelAll {
1358 my ( $itemnumber, $borrowernumber ) = @_;
1360 #step 1 : cancel the reservation
1361 my $CancelReserve = CancelReserve({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
1363 #step 2 launch the subroutine of the others reserves
1364 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1366 return ( $messages, $nextreservinfo );
1369 =head2 ModReserveMinusPriority
1371 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1373 Reduce the values of queued list
1377 sub ModReserveMinusPriority {
1378 my ( $itemnumber, $reserve_id ) = @_;
1380 #first step update the value of the first person on reserv
1381 my $dbh = C4::Context->dbh;
1384 SET priority = 0 , itemnumber = ?
1385 WHERE reserve_id = ?
1387 my $sth_upd = $dbh->prepare($query);
1388 $sth_upd->execute( $itemnumber, $reserve_id );
1389 # second step update all others reserves
1390 _FixPriority({ reserve_id => $reserve_id, rank => '0' });
1393 =head2 GetReserveInfo
1395 &GetReserveInfo($reserve_id);
1397 Get item and borrower details for a current hold.
1398 Current implementation this query should have a single result.
1402 sub GetReserveInfo {
1403 my ( $reserve_id ) = @_;
1404 my $dbh = C4::Context->dbh;
1409 reserves.borrowernumber,
1410 reserves.biblionumber,
1411 reserves.branchcode,
1412 reserves.waitingdate,
1428 items.holdingbranch,
1429 items.itemcallnumber,
1435 LEFT JOIN items USING(itemnumber)
1436 LEFT JOIN borrowers USING(borrowernumber)
1437 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1438 WHERE reserves.reserve_id = ?";
1439 my $sth = $dbh->prepare($strsth);
1440 $sth->execute($reserve_id);
1442 my $data = $sth->fetchrow_hashref;
1446 =head2 IsAvailableForItemLevelRequest
1448 my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
1450 Checks whether a given item record is available for an
1451 item-level hold request. An item is available if
1453 * it is not lost AND
1454 * it is not damaged AND
1455 * it is not withdrawn AND
1456 * does not have a not for loan value > 0
1458 Need to check the issuingrules onshelfholds column,
1459 if this is set items on the shelf can be placed on hold
1461 Note that IsAvailableForItemLevelRequest() does not
1462 check if the staff operator is authorized to place
1463 a request on the item - in particular,
1464 this routine does not check IndependentBranches
1465 and canreservefromotherbranches.
1469 sub IsAvailableForItemLevelRequest {
1471 my $borrower = shift;
1473 my $dbh = C4::Context->dbh;
1474 # must check the notforloan setting of the itemtype
1475 # FIXME - a lot of places in the code do this
1476 # or something similar - need to be
1478 my $itype = _get_itype($item);
1479 my $notforloan_per_itemtype
1480 = $dbh->selectrow_array("SELECT notforloan FROM itemtypes WHERE itemtype = ?",
1484 $notforloan_per_itemtype ||
1485 $item->{itemlost} ||
1486 $item->{notforloan} > 0 ||
1487 $item->{withdrawn} ||
1488 ($item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems'));
1491 return 1 if _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
1493 return $item->{onloan} || GetReserveStatus($item->{itemnumber}) eq "Waiting";
1496 =head2 OnShelfHoldsAllowed
1498 OnShelfHoldsAllowed($itemtype,$borrowercategory,$branchcode);
1500 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see if onshelf
1501 holds are allowed, returns true if so.
1505 sub OnShelfHoldsAllowed {
1506 my ($item, $borrower) = @_;
1508 my $itype = _get_itype($item);
1509 return _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
1516 if (C4::Context->preference('item-level_itypes')) {
1517 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
1518 # When GetItem is fixed, we can remove this
1519 $itype = $item->{itype};
1522 # XXX This is a bit dodgy. It relies on biblio itemtype column having different name.
1523 # So if we already have a biblioitems join when calling this function,
1524 # we don't need to access the database again
1525 $itype = $item->{itemtype};
1528 my $dbh = C4::Context->dbh;
1529 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
1530 my $sth = $dbh->prepare($query);
1531 $sth->execute($item->{biblioitemnumber});
1532 if (my $data = $sth->fetchrow_hashref()){
1533 $itype = $data->{itemtype};
1539 sub _OnShelfHoldsAllowed {
1540 my ($itype,$borrowercategory,$branchcode) = @_;
1542 my $rule = C4::Circulation::GetIssuingRule($borrowercategory, $itype, $branchcode);
1543 return $rule->{onshelfholds};
1546 =head2 AlterPriority
1548 AlterPriority( $where, $reserve_id );
1550 This function changes a reserve's priority up, down, to the top, or to the bottom.
1551 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1556 my ( $where, $reserve_id ) = @_;
1558 my $dbh = C4::Context->dbh;
1560 my $reserve = GetReserve( $reserve_id );
1562 if ( $reserve->{cancellationdate} ) {
1563 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (".$reserve->{cancellationdate}.')';
1567 if ( $where eq 'up' || $where eq 'down' ) {
1569 my $priority = $reserve->{'priority'};
1570 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1571 _FixPriority({ reserve_id => $reserve_id, rank => $priority })
1573 } elsif ( $where eq 'top' ) {
1575 _FixPriority({ reserve_id => $reserve_id, rank => '1' })
1577 } elsif ( $where eq 'bottom' ) {
1579 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1584 =head2 ToggleLowestPriority
1586 ToggleLowestPriority( $borrowernumber, $biblionumber );
1588 This function sets the lowestPriority field to true if is false, and false if it is true.
1592 sub ToggleLowestPriority {
1593 my ( $reserve_id ) = @_;
1595 my $dbh = C4::Context->dbh;
1597 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1598 $sth->execute( $reserve_id );
1600 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1603 =head2 ToggleSuspend
1605 ToggleSuspend( $reserve_id );
1607 This function sets the suspend field to true if is false, and false if it is true.
1608 If the reserve is currently suspended with a suspend_until date, that date will
1609 be cleared when it is unsuspended.
1614 my ( $reserve_id, $suspend_until ) = @_;
1616 $suspend_until = output_pref(
1618 dt => dt_from_string($suspend_until),
1619 dateformat => 'iso',
1622 ) if ($suspend_until);
1624 my $do_until = ( $suspend_until ) ? '?' : 'NULL';
1626 my $dbh = C4::Context->dbh;
1628 my $sth = $dbh->prepare(
1629 "UPDATE reserves SET suspend = NOT suspend,
1630 suspend_until = CASE WHEN suspend = 0 THEN NULL ELSE $do_until END
1631 WHERE reserve_id = ?
1635 push( @params, $suspend_until ) if ( $suspend_until );
1636 push( @params, $reserve_id );
1638 $sth->execute( @params );
1644 borrowernumber => $borrowernumber,
1645 [ biblionumber => $biblionumber, ]
1646 [ suspend_until => $suspend_until, ]
1647 [ suspend => $suspend ]
1650 This function accepts a set of hash keys as its parameters.
1651 It requires either borrowernumber or biblionumber, or both.
1653 suspend_until is wholly optional.
1660 my $borrowernumber = $params{'borrowernumber'} || undef;
1661 my $biblionumber = $params{'biblionumber'} || undef;
1662 my $suspend_until = $params{'suspend_until'} || undef;
1663 my $suspend = defined( $params{'suspend'} ) ? $params{'suspend'} : 1;
1665 $suspend_until = eval { output_pref( { dt => dt_from_string( $suspend_until), dateonly => 1, dateformat => 'iso' } ); }
1666 if ( defined( $suspend_until ) );
1668 return unless ( $borrowernumber || $biblionumber );
1670 my ( $query, $sth, $dbh, @query_params );
1672 $query = "UPDATE reserves SET suspend = ? ";
1673 push( @query_params, $suspend );
1675 $query .= ", suspend_until = NULL ";
1676 } elsif ( $suspend_until ) {
1677 $query .= ", suspend_until = ? ";
1678 push( @query_params, $suspend_until );
1680 $query .= " WHERE ";
1681 if ( $borrowernumber ) {
1682 $query .= " borrowernumber = ? ";
1683 push( @query_params, $borrowernumber );
1685 $query .= " AND " if ( $borrowernumber && $biblionumber );
1686 if ( $biblionumber ) {
1687 $query .= " biblionumber = ? ";
1688 push( @query_params, $biblionumber );
1690 $query .= " AND found IS NULL ";
1692 $dbh = C4::Context->dbh;
1693 $sth = $dbh->prepare( $query );
1694 $sth->execute( @query_params );
1701 reserve_id => $reserve_id,
1703 [ignoreSetLowestRank => $ignoreSetLowestRank]
1708 _FixPriority({ biblionumber => $biblionumber});
1710 This routine adjusts the priority of a hold request and holds
1713 In the first form, where a reserve_id is passed, the priority of the
1714 hold is set to supplied rank, and other holds for that bib are adjusted
1715 accordingly. If the rank is "del", the hold is cancelled. If no rank
1716 is supplied, all of the holds on that bib have their priority adjusted
1717 as if the second form had been used.
1719 In the second form, where a biblionumber is passed, the holds on that
1720 bib (that are not captured) are sorted in order of increasing priority,
1721 then have reserves.priority set so that the first non-captured hold
1722 has its priority set to 1, the second non-captured hold has its priority
1723 set to 2, and so forth.
1725 In both cases, holds that have the lowestPriority flag on are have their
1726 priority adjusted to ensure that they remain at the end of the line.
1728 Note that the ignoreSetLowestRank parameter is meant to be used only
1729 when _FixPriority calls itself.
1734 my ( $params ) = @_;
1735 my $reserve_id = $params->{reserve_id};
1736 my $rank = $params->{rank} // '';
1737 my $ignoreSetLowestRank = $params->{ignoreSetLowestRank};
1738 my $biblionumber = $params->{biblionumber};
1740 my $dbh = C4::Context->dbh;
1742 unless ( $biblionumber ) {
1743 my $res = GetReserve( $reserve_id );
1744 $biblionumber = $res->{biblionumber};
1747 if ( $rank eq "del" ) {
1748 CancelReserve({ reserve_id => $reserve_id });
1750 elsif ( $rank eq "W" || $rank eq "0" ) {
1752 # make sure priority for waiting or in-transit items is 0
1756 WHERE reserve_id = ?
1757 AND found IN ('W', 'T')
1759 my $sth = $dbh->prepare($query);
1760 $sth->execute( $reserve_id );
1766 SELECT reserve_id, borrowernumber, reservedate
1768 WHERE biblionumber = ?
1769 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1770 ORDER BY priority ASC
1772 my $sth = $dbh->prepare($query);
1773 $sth->execute( $biblionumber );
1774 while ( my $line = $sth->fetchrow_hashref ) {
1775 push( @priority, $line );
1778 # To find the matching index
1780 my $key = -1; # to allow for 0 to be a valid result
1781 for ( $i = 0 ; $i < @priority ; $i++ ) {
1782 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1783 $key = $i; # save the index
1788 # if index exists in array then move it to new position
1789 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1790 my $new_rank = $rank -
1791 1; # $new_rank is what you want the new index to be in the array
1792 my $moving_item = splice( @priority, $key, 1 );
1793 splice( @priority, $new_rank, 0, $moving_item );
1796 # now fix the priority on those that are left....
1800 WHERE reserve_id = ?
1802 $sth = $dbh->prepare($query);
1803 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1806 $priority[$j]->{'reserve_id'}
1810 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1813 unless ( $ignoreSetLowestRank ) {
1814 while ( my $res = $sth->fetchrow_hashref() ) {
1816 reserve_id => $res->{'reserve_id'},
1818 ignoreSetLowestRank => 1
1824 =head2 _Findgroupreserve
1826 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead, $ignore_borrowers);
1828 Looks for a holds-queue based item-specific match first, then for a holds-queue title-level match, returning the
1829 first match found. If neither, then we look for non-holds-queue based holds.
1830 Lookahead is the number of days to look in advance.
1832 C<&_Findgroupreserve> returns :
1833 C<@results> is an array of references-to-hash whose keys are mostly
1834 fields from the reserves table of the Koha database, plus
1835 C<biblioitemnumber>.
1839 sub _Findgroupreserve {
1840 my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_;
1841 my $dbh = C4::Context->dbh;
1843 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1844 # check for exact targeted match
1845 my $item_level_target_query = qq{
1846 SELECT reserves.biblionumber AS biblionumber,
1847 reserves.borrowernumber AS borrowernumber,
1848 reserves.reservedate AS reservedate,
1849 reserves.branchcode AS branchcode,
1850 reserves.cancellationdate AS cancellationdate,
1851 reserves.found AS found,
1852 reserves.reservenotes AS reservenotes,
1853 reserves.priority AS priority,
1854 reserves.timestamp AS timestamp,
1855 biblioitems.biblioitemnumber AS biblioitemnumber,
1856 reserves.itemnumber AS itemnumber,
1857 reserves.reserve_id AS reserve_id
1859 JOIN biblioitems USING (biblionumber)
1860 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1863 AND item_level_request = 1
1865 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1869 my $sth = $dbh->prepare($item_level_target_query);
1870 $sth->execute($itemnumber, $lookahead||0);
1872 if ( my $data = $sth->fetchrow_hashref ) {
1873 push( @results, $data )
1874 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1876 return @results if @results;
1878 # check for title-level targeted match
1879 my $title_level_target_query = qq{
1880 SELECT reserves.biblionumber AS biblionumber,
1881 reserves.borrowernumber AS borrowernumber,
1882 reserves.reservedate AS reservedate,
1883 reserves.branchcode AS branchcode,
1884 reserves.cancellationdate AS cancellationdate,
1885 reserves.found AS found,
1886 reserves.reservenotes AS reservenotes,
1887 reserves.priority AS priority,
1888 reserves.timestamp AS timestamp,
1889 biblioitems.biblioitemnumber AS biblioitemnumber,
1890 reserves.itemnumber AS itemnumber,
1891 reserves.reserve_id AS reserve_id
1893 JOIN biblioitems USING (biblionumber)
1894 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1897 AND item_level_request = 0
1898 AND hold_fill_targets.itemnumber = ?
1899 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1903 $sth = $dbh->prepare($title_level_target_query);
1904 $sth->execute($itemnumber, $lookahead||0);
1906 if ( my $data = $sth->fetchrow_hashref ) {
1907 push( @results, $data )
1908 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1910 return @results if @results;
1913 SELECT reserves.biblionumber AS biblionumber,
1914 reserves.borrowernumber AS borrowernumber,
1915 reserves.reservedate AS reservedate,
1916 reserves.waitingdate AS waitingdate,
1917 reserves.branchcode AS branchcode,
1918 reserves.cancellationdate AS cancellationdate,
1919 reserves.found AS found,
1920 reserves.reservenotes AS reservenotes,
1921 reserves.priority AS priority,
1922 reserves.timestamp AS timestamp,
1923 reserves.itemnumber AS itemnumber,
1924 reserves.reserve_id AS reserve_id
1926 WHERE reserves.biblionumber = ?
1927 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1928 AND reserves.reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1932 $sth = $dbh->prepare($query);
1933 $sth->execute( $biblio, $itemnumber, $lookahead||0);
1935 while ( my $data = $sth->fetchrow_hashref ) {
1936 push( @results, $data )
1937 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1942 =head2 _koha_notify_reserve
1944 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1946 Sends a notification to the patron that their hold has been filled (through
1947 ModReserveAffect, _not_ ModReserveFill)
1949 The letter code for this notice may be found using the following query:
1951 select distinct letter_code
1952 from message_transports
1953 inner join message_attributes using (message_attribute_id)
1954 where message_name = 'Hold_Filled'
1956 This will probably sipmly be 'HOLD', but because it is defined in the database,
1957 it is subject to addition or change.
1959 The following tables are availalbe witin the notice:
1970 sub _koha_notify_reserve {
1971 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1973 my $dbh = C4::Context->dbh;
1974 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
1976 # Try to get the borrower's email address
1977 my $to_address = C4::Members::GetNoticeEmailAddress($borrowernumber);
1979 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( {
1980 borrowernumber => $borrowernumber,
1981 message_name => 'Hold_Filled'
1984 my $sth = $dbh->prepare("
1987 WHERE borrowernumber = ?
1988 AND biblionumber = ?
1990 $sth->execute( $borrowernumber, $biblionumber );
1991 my $reserve = $sth->fetchrow_hashref;
1992 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1994 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1996 my %letter_params = (
1997 module => 'reserves',
1998 branchcode => $reserve->{branchcode},
2000 'branches' => $branch_details,
2001 'borrowers' => $borrower,
2002 'biblio' => $biblionumber,
2003 'biblioitems' => $biblionumber,
2004 'reserves' => $reserve,
2005 'items', $reserve->{'itemnumber'},
2007 substitute => { today => output_pref( { dt => dt_from_string, dateonly => 1 } ) },
2010 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.
2011 my $send_notification = sub {
2012 my ( $mtt, $letter_code ) = (@_);
2013 return unless defined $letter_code;
2014 $letter_params{letter_code} = $letter_code;
2015 $letter_params{message_transport_type} = $mtt;
2016 my $letter = C4::Letters::GetPreparedLetter ( %letter_params );
2018 warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
2022 C4::Letters::EnqueueLetter( {
2024 borrowernumber => $borrowernumber,
2025 from_address => $admin_email_address,
2026 message_transport_type => $mtt,
2030 while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) {
2032 ( $mtt eq 'email' and not $to_address ) # No email address
2033 or ( $mtt eq 'sms' and not $borrower->{smsalertnumber} ) # No SMS number
2034 or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
2037 &$send_notification($mtt, $letter_code);
2038 $notification_sent++;
2040 #Making sure that a print notification is sent if no other transport types can be utilized.
2041 if (! $notification_sent) {
2042 &$send_notification('print', 'HOLD');
2047 =head2 _ShiftPriorityByDateAndPriority
2049 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
2051 This increments the priority of all reserves after the one
2052 with either the lowest date after C<$reservedate>
2053 or the lowest priority after C<$priority>.
2055 It effectively makes room for a new reserve to be inserted with a certain
2056 priority, which is returned.
2058 This is most useful when the reservedate can be set by the user. It allows
2059 the new reserve to be placed before other reserves that have a later
2060 reservedate. Since priority also is set by the form in reserves/request.pl
2061 the sub accounts for that too.
2065 sub _ShiftPriorityByDateAndPriority {
2066 my ( $biblio, $resdate, $new_priority ) = @_;
2068 my $dbh = C4::Context->dbh;
2069 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
2070 my $sth = $dbh->prepare( $query );
2071 $sth->execute( $biblio, $resdate, $new_priority );
2072 my $min_priority = $sth->fetchrow;
2073 # if no such matches are found, $new_priority remains as original value
2074 $new_priority = $min_priority if ( $min_priority );
2076 # Shift the priority up by one; works in conjunction with the next SQL statement
2077 $query = "UPDATE reserves
2078 SET priority = priority+1
2079 WHERE biblionumber = ?
2080 AND borrowernumber = ?
2083 my $sth_update = $dbh->prepare( $query );
2085 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
2086 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
2087 $sth = $dbh->prepare( $query );
2088 $sth->execute( $new_priority, $biblio );
2089 while ( my $row = $sth->fetchrow_hashref ) {
2090 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
2093 return $new_priority; # so the caller knows what priority they wind up receiving
2096 =head2 OPACItemHoldsAllowed
2098 OPACItemHoldsAllowed($item_record,$borrower_record);
2100 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see
2101 if specific item holds are allowed, returns true if so.
2105 sub OPACItemHoldsAllowed {
2106 my ($item,$borrower) = @_;
2108 my $branchcode = $item->{homebranch} or die "No homebranch";
2110 my $dbh = C4::Context->dbh;
2111 if (C4::Context->preference('item-level_itypes')) {
2112 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
2113 # When GetItem is fixed, we can remove this
2114 $itype = $item->{itype};
2117 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
2118 my $sth = $dbh->prepare($query);
2119 $sth->execute($item->{biblioitemnumber});
2120 if (my $data = $sth->fetchrow_hashref()){
2121 $itype = $data->{itemtype};
2125 my $query = "SELECT opacitemholds,categorycode,itemtype,branchcode FROM issuingrules WHERE
2126 (issuingrules.categorycode = ? OR issuingrules.categorycode = '*')
2128 (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2130 (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')
2132 issuingrules.categorycode desc,
2133 issuingrules.itemtype desc,
2134 issuingrules.branchcode desc
2136 my $sth = $dbh->prepare($query);
2137 $sth->execute($borrower->{categorycode},$itype,$branchcode);
2138 my $data = $sth->fetchrow_hashref;
2139 my $opacitemholds = uc substr ($data->{opacitemholds}, 0, 1);
2140 return '' if $opacitemholds eq 'N';
2141 return $opacitemholds;
2146 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
2148 Use when checking out an item to handle reserves
2149 If $cancelreserve boolean is set to true, it will remove existing reserve
2154 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
2156 my $lookahead = C4::Context->preference('ConfirmFutureHolds'); #number of days to look for future holds
2157 my ( $restype, $res, $all_reserves ) = CheckReserves( $itemnumber, undef, $lookahead );
2160 my $biblionumber = $res->{biblionumber};
2161 my $biblioitemnumber = $res->{biblioitemnumber};
2163 if ($res->{borrowernumber} == $borrowernumber) {
2164 ModReserveFill($res);
2168 # The item is reserved by someone else.
2169 # Find this item in the reserves
2172 foreach (@$all_reserves) {
2173 $_->{'borrowernumber'} == $borrowernumber or next;
2174 $_->{'biblionumber'} == $biblionumber or next;
2181 # The item is reserved by the current patron
2182 ModReserveFill($borr_res);
2185 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
2186 RevertWaitingStatus({ itemnumber => $itemnumber });
2188 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
2189 CancelReserve( { reserve_id => $res->{'reserve_id'} } );
2196 MergeHolds($dbh,$to_biblio, $from_biblio);
2198 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
2203 my ( $dbh, $to_biblio, $from_biblio ) = @_;
2204 my $sth = $dbh->prepare(
2205 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
2207 $sth->execute($from_biblio);
2208 if ( my $data = $sth->fetchrow_hashref() ) {
2210 # holds exist on old record, if not we don't need to do anything
2211 $sth = $dbh->prepare(
2212 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
2213 $sth->execute( $to_biblio, $from_biblio );
2216 # don't reorder those already waiting
2218 $sth = $dbh->prepare(
2219 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
2221 my $upd_sth = $dbh->prepare(
2222 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
2223 AND reservedate = ? AND (itemnumber = ? or itemnumber is NULL) "
2225 $sth->execute( $to_biblio, 'W', 'T' );
2227 while ( my $reserve = $sth->fetchrow_hashref() ) {
2229 $priority, $to_biblio,
2230 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
2231 $reserve->{'itemnumber'}
2238 =head2 RevertWaitingStatus
2240 RevertWaitingStatus({ itemnumber => $itemnumber });
2242 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
2244 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
2245 item level hold, even if it was only a bibliolevel hold to
2246 begin with. This is because we can no longer know if a hold
2247 was item-level or bib-level after a hold has been set to
2252 sub RevertWaitingStatus {
2253 my ( $params ) = @_;
2254 my $itemnumber = $params->{'itemnumber'};
2256 return unless ( $itemnumber );
2258 my $dbh = C4::Context->dbh;
2260 ## Get the waiting reserve we want to revert
2262 SELECT * FROM reserves
2263 WHERE itemnumber = ?
2264 AND found IS NOT NULL
2266 my $sth = $dbh->prepare( $query );
2267 $sth->execute( $itemnumber );
2268 my $reserve = $sth->fetchrow_hashref();
2270 ## Increment the priority of all other non-waiting
2271 ## reserves for this bib record
2275 priority = priority + 1
2281 $sth = $dbh->prepare( $query );
2282 $sth->execute( $reserve->{'biblionumber'} );
2284 ## Fix up the currently waiting reserve
2294 $sth = $dbh->prepare( $query );
2295 $sth->execute( $reserve->{'reserve_id'} );
2296 _FixPriority( { biblionumber => $reserve->{biblionumber} } );
2301 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
2303 Returnes the first reserve id that matches the given criteria
2308 my ( $params ) = @_;
2310 return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
2312 my $dbh = C4::Context->dbh();
2314 my $sql = "SELECT reserve_id FROM reserves WHERE ";
2318 foreach my $key ( keys %$params ) {
2319 if ( defined( $params->{$key} ) ) {
2320 push( @limits, "$key = ?" );
2321 push( @params, $params->{$key} );
2325 $sql .= join( " AND ", @limits );
2327 my $sth = $dbh->prepare( $sql );
2328 $sth->execute( @params );
2329 my $row = $sth->fetchrow_hashref();
2331 return $row->{'reserve_id'};
2336 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
2338 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2340 The letter code will be RESERVESLIP, and the following tables are
2341 available within the slip:
2353 my ($branch, $borrowernumber, $biblionumber) = @_;
2355 # return unless ( C4::Context->boolean_preference('printreserveslips') );
2357 my $reserve_id = GetReserveId({
2358 biblionumber => $biblionumber,
2359 borrowernumber => $borrowernumber
2361 my $reserve = GetReserveInfo($reserve_id) or return;
2363 return C4::Letters::GetPreparedLetter (
2364 module => 'circulation',
2365 letter_code => 'RESERVESLIP',
2366 branchcode => $branch,
2368 'reserves' => $reserve,
2369 'branches' => $reserve->{branchcode},
2370 'borrowers' => $reserve->{borrowernumber},
2371 'biblio' => $reserve->{biblionumber},
2372 'biblioitems' => $reserve->{biblionumber},
2373 'items' => $reserve->{itemnumber},
2378 =head2 GetReservesControlBranch
2380 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2382 Return the branchcode to be used to determine which reserves
2383 policy applies to a transaction.
2385 C<$item> is a hashref for an item. Only 'homebranch' is used.
2387 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2391 sub GetReservesControlBranch {
2392 my ( $item, $borrower ) = @_;
2394 my $reserves_control = C4::Context->preference('ReservesControlBranch');
2397 ( $reserves_control eq 'ItemHomeLibrary' ) ? $item->{'homebranch'}
2398 : ( $reserves_control eq 'PatronLibrary' ) ? $borrower->{'branchcode'}
2404 =head2 CalculatePriority
2406 my $p = CalculatePriority($biblionumber, $resdate);
2408 Calculate priority for a new reserve on biblionumber, placing it at
2409 the end of the line of all holds whose start date falls before
2410 the current system time and that are neither on the hold shelf
2413 The reserve date parameter is optional; if it is supplied, the
2414 priority is based on the set of holds whose start date falls before
2415 the parameter value.
2417 After calculation of this priority, it is recommended to call
2418 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2423 sub CalculatePriority {
2424 my ( $biblionumber, $resdate ) = @_;
2427 SELECT COUNT(*) FROM reserves
2428 WHERE biblionumber = ?
2430 AND (found IS NULL OR found = '')
2432 #skip found==W or found==T (waiting or transit holds)
2434 $sql.= ' AND ( reservedate <= ? )';
2437 $sql.= ' AND ( reservedate < NOW() )';
2439 my $dbh = C4::Context->dbh();
2440 my @row = $dbh->selectrow_array(
2443 $resdate ? ($biblionumber, $resdate) : ($biblionumber)
2446 return @row ? $row[0]+1 : 1;
2449 =head2 IsItemOnHoldAndFound
2451 my $bool = IsItemFoundHold( $itemnumber );
2453 Returns true if the item is currently on hold
2454 and that hold has a non-null found status ( W, T, etc. )
2458 sub IsItemOnHoldAndFound {
2459 my ($itemnumber) = @_;
2461 my $rs = Koha::Database->new()->schema()->resultset('Reserve');
2463 my $found = $rs->count(
2465 itemnumber => $itemnumber,
2466 found => { '!=' => undef }
2475 Koha Development Team <http://koha-community.org/>