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
;
46 use Koha
::IssuingRules
;
51 use List
::MoreUtils
qw( firstidx any );
55 use vars
qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
59 C4::Reserves - Koha functions for dealing with reservation.
67 This modules provides somes functions to deal with reservations.
69 Reserves are stored in reserves table.
70 The following columns contains important values :
71 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
72 =0 : then the reserve is being dealed
73 - found : NULL : means the patron requested the 1st available, and we haven't chosen the item
74 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
75 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
76 F(inished) : the reserve has been completed, and is done
77 - itemnumber : empty : the reserve is still unaffected to an item
78 filled: the reserve is attached to an item
79 The complete workflow is :
80 ==== 1st use case ====
81 patron request a document, 1st available : P >0, F=NULL, I=NULL
82 a library having it run "transfertodo", and clic on the list
83 if there is no transfer to do, the reserve waiting
84 patron can pick it up P =0, F=W, I=filled
85 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
86 The pickup library receive the book, it check in P =0, F=W, I=filled
87 The patron borrow the book P =0, F=F, I=filled
89 ==== 2nd use case ====
90 patron requests a document, a given item,
91 If pickup is holding branch P =0, F=W, I=filled
92 If transfer needed, write in branchtransfer P =0, F=T, I=filled
93 The pickup library receive the book, it checks it in P =0, F=W, I=filled
94 The patron borrow the book P =0, F=F, I=filled
107 &GetReservesFromBorrowernumber
108 &GetReservesForBranch
121 &ModReserveMinusPriority
127 &CanReserveBeCanceledFromOpac
129 &CancelExpiredReserves
131 &AutoUnsuspendReserves
133 &IsAvailableForItemLevelRequest
135 &OPACItemHoldsAllowed
138 &ToggleLowestPriority
144 &GetReservesControlBranch
148 GetMaxPatronHoldsForRecord
150 @EXPORT_OK = qw( MergeHolds );
155 AddReserve($branch,$borrowernumber,$biblionumber,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
157 Adds reserve and generates HOLDPLACED message.
159 The following tables are available witin the HOLDPLACED message:
172 $branch, $borrowernumber, $biblionumber, $bibitems,
173 $priority, $resdate, $expdate, $notes,
174 $title, $checkitem, $found, $itemtype
177 $resdate = output_pref
( { str
=> dt_from_string
( $resdate ), dateonly
=> 1, dateformat
=> 'iso' })
178 or output_pref
({ dt
=> dt_from_string
, dateonly
=> 1, dateformat
=> 'iso' });
180 $expdate = output_pref
({ str
=> $expdate, dateonly
=> 1, dateformat
=> 'iso' });
182 if ( C4
::Context
->preference('AllowHoldDateInFuture') ) {
184 # Make room in reserves for this before those of a later reserve date
185 $priority = _ShiftPriorityByDateAndPriority
( $biblionumber, $resdate, $priority );
190 # If the reserv had the waiting status, we had the value of the resdate
191 if ( $found eq 'W' ) {
192 $waitingdate = $resdate;
195 # Don't add itemtype limit if specific item is selected
196 $itemtype = undef if $checkitem;
198 # updates take place here
199 my $hold = Koha
::Hold
->new(
201 borrowernumber
=> $borrowernumber,
202 biblionumber
=> $biblionumber,
203 reservedate
=> $resdate,
204 branchcode
=> $branch,
205 priority
=> $priority,
206 reservenotes
=> $notes,
207 itemnumber
=> $checkitem,
209 waitingdate
=> $waitingdate,
210 expirationdate
=> $expdate,
211 itemtype
=> $itemtype,
215 logaction
( 'HOLDS', 'CREATE', $hold->id, Dumper
($hold->unblessed) )
216 if C4
::Context
->preference('HoldsLog');
218 my $reserve_id = $hold->id();
220 # add a reserve fee if needed
221 if ( C4
::Context
->preference('HoldFeeMode') ne 'any_time_is_collected' ) {
222 my $reserve_fee = GetReserveFee
( $borrowernumber, $biblionumber );
223 ChargeReserveFee
( $borrowernumber, $reserve_fee, $title );
226 _FixPriority
({ biblionumber
=> $biblionumber});
228 # Send e-mail to librarian if syspref is active
229 if(C4
::Context
->preference("emailLibrarianWhenHoldIsPlaced")){
230 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
231 my $library = Koha
::Libraries
->find($borrower->{branchcode
})->unblessed;
232 if ( my $letter = C4
::Letters
::GetPreparedLetter
(
233 module
=> 'reserves',
234 letter_code
=> 'HOLDPLACED',
235 branchcode
=> $branch,
237 'branches' => $library,
238 'borrowers' => $borrower,
239 'biblio' => $biblionumber,
240 'biblioitems' => $biblionumber,
241 'items' => $checkitem,
242 'reserves' => $hold->unblessed,
246 my $admin_email_address = $library->{'branchemail'} || C4
::Context
->preference('KohaAdminEmailAddress');
248 C4
::Letters
::EnqueueLetter
(
250 borrowernumber
=> $borrowernumber,
251 message_transport_type
=> 'email',
252 from_address
=> $admin_email_address,
253 to_address
=> $admin_email_address,
264 $res = GetReserve( $reserve_id );
266 Return the current reserve.
271 my ($reserve_id) = @_;
273 my $dbh = C4
::Context
->dbh;
275 my $query = "SELECT * FROM reserves WHERE reserve_id = ?";
276 my $sth = $dbh->prepare( $query );
277 $sth->execute( $reserve_id );
278 return $sth->fetchrow_hashref();
281 =head2 GetReservesFromBorrowernumber
283 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
289 sub GetReservesFromBorrowernumber
{
290 my ( $borrowernumber, $status ) = @_;
291 my $dbh = C4
::Context
->dbh;
294 $sth = $dbh->prepare("
297 WHERE borrowernumber=?
301 $sth->execute($borrowernumber,$status);
303 $sth = $dbh->prepare("
306 WHERE borrowernumber=?
309 $sth->execute($borrowernumber);
311 my $data = $sth->fetchall_arrayref({});
315 =head2 CanBookBeReserved
317 $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
318 if ($canReserve eq 'OK') { #We can reserve this Item! }
320 See CanItemBeReserved() for possible return values.
324 sub CanBookBeReserved
{
325 my ($borrowernumber, $biblionumber) = @_;
327 my $items = GetItemnumbersForBiblio
($biblionumber);
328 #get items linked via host records
329 my @hostitems = get_hostitemnumbers_of
($biblionumber);
331 push (@
$items,@hostitems);
335 foreach my $item (@
$items) {
336 $canReserve = CanItemBeReserved
( $borrowernumber, $item );
337 return 'OK' if $canReserve eq 'OK';
342 =head2 CanItemBeReserved
344 $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber)
345 if ($canReserve eq 'OK') { #We can reserve this Item! }
347 @RETURNS OK, if the Item can be reserved.
348 ageRestricted, if the Item is age restricted for this borrower.
349 damaged, if the Item is damaged.
350 cannotReserveFromOtherBranches, if syspref 'canreservefromotherbranches' is OK.
351 tooManyReserves, if the borrower has exceeded his maximum reserve amount.
352 notReservable, if holds on this item are not allowed
356 sub CanItemBeReserved
{
357 my ( $borrowernumber, $itemnumber ) = @_;
359 my $dbh = C4
::Context
->dbh;
360 my $ruleitemtype; # itemtype of the matching issuing rule
361 my $allowedreserves = 0; # Total number of holds allowed across all records
362 my $holds_per_record = 1; # Total number of holds allowed for this one given record
364 # we retrieve borrowers and items informations #
365 # item->{itype} will come for biblioitems if necessery
366 my $item = GetItem
($itemnumber);
367 my $biblioData = C4
::Biblio
::GetBiblioData
( $item->{biblionumber
} );
368 my $borrower = C4
::Members
::GetMember
( 'borrowernumber' => $borrowernumber );
370 # If an item is damaged and we don't allow holds on damaged items, we can stop right here
372 if ( $item->{damaged
}
373 && !C4
::Context
->preference('AllowHoldsOnDamagedItems') );
375 # Check for the age restriction
376 my ( $ageRestriction, $daysToAgeRestriction ) =
377 C4
::Circulation
::GetAgeRestriction
( $biblioData->{agerestriction
}, $borrower );
378 return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
380 # Check that the patron doesn't have an item level hold on this item already
381 return 'itemAlreadyOnHold'
382 if Koha
::Holds
->search( { borrowernumber
=> $borrowernumber, itemnumber
=> $itemnumber } )->count();
384 my $controlbranch = C4
::Context
->preference('ReservesControlBranch');
387 SELECT count(*) AS count
389 LEFT JOIN items USING (itemnumber)
390 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
391 LEFT JOIN borrowers USING (borrowernumber)
392 WHERE borrowernumber = ?
396 my $branchfield = "reserves.branchcode";
398 if ( $controlbranch eq "ItemHomeLibrary" ) {
399 $branchfield = "items.homebranch";
400 $branchcode = $item->{homebranch
};
402 elsif ( $controlbranch eq "PatronLibrary" ) {
403 $branchfield = "borrowers.branchcode";
404 $branchcode = $borrower->{branchcode
};
408 if ( my $rights = GetHoldRule
( $borrower->{'categorycode'}, $item->{'itype'}, $branchcode ) ) {
409 $ruleitemtype = $rights->{itemtype
};
410 $allowedreserves = $rights->{reservesallowed
};
411 $holds_per_record = $rights->{holds_per_record
};
417 $item = Koha
::Items
->find( $itemnumber );
418 my $holds = Koha
::Holds
->search(
420 borrowernumber
=> $borrowernumber,
421 biblionumber
=> $item->biblionumber,
422 found
=> undef, # Found holds don't count against a patron's holds limit
425 if ( $holds->count() >= $holds_per_record ) {
426 return "tooManyHoldsForThisRecord";
431 $querycount .= "AND $branchfield = ?";
433 # If using item-level itypes, fall back to the record
434 # level itemtype if the hold has no associated item
436 C4
::Context
->preference('item-level_itypes')
437 ?
" AND COALESCE( items.itype, biblioitems.itemtype ) = ?"
438 : " AND biblioitems.itemtype = ?"
439 if ( $ruleitemtype ne "*" );
441 my $sthcount = $dbh->prepare($querycount);
443 if ( $ruleitemtype eq "*" ) {
444 $sthcount->execute( $borrowernumber, $branchcode );
447 $sthcount->execute( $borrowernumber, $branchcode, $ruleitemtype );
450 my $reservecount = "0";
451 if ( my $rowcount = $sthcount->fetchrow_hashref() ) {
452 $reservecount = $rowcount->{count
};
455 # we check if it's ok or not
456 if ( $reservecount >= $allowedreserves ) {
457 return 'tooManyReserves';
460 my $circ_control_branch =
461 C4
::Circulation
::_GetCircControlBranch
( $item->unblessed(), $borrower );
463 C4
::Circulation
::GetBranchItemRule
( $circ_control_branch, $item->itype );
465 if ( $branchitemrule->{holdallowed
} == 0 ) {
466 return 'notReservable';
469 if ( $branchitemrule->{holdallowed
} == 1
470 && $borrower->{branchcode
} ne $item->homebranch )
472 return 'cannotReserveFromOtherBranches';
475 # If reservecount is ok, we check item branch if IndependentBranches is ON
476 # and canreservefromotherbranches is OFF
477 if ( C4
::Context
->preference('IndependentBranches')
478 and !C4
::Context
->preference('canreservefromotherbranches') )
480 my $itembranch = $item->homebranch;
481 if ( $itembranch ne $borrower->{branchcode
} ) {
482 return 'cannotReserveFromOtherBranches';
489 =head2 CanReserveBeCanceledFromOpac
491 $number = CanReserveBeCanceledFromOpac($reserve_id, $borrowernumber);
493 returns 1 if reserve can be cancelled by user from OPAC.
494 First check if reserve belongs to user, next checks if reserve is not in
495 transfer or waiting status
499 sub CanReserveBeCanceledFromOpac
{
500 my ($reserve_id, $borrowernumber) = @_;
502 return unless $reserve_id and $borrowernumber;
503 my $reserve = GetReserve
($reserve_id);
505 return 0 unless $reserve->{borrowernumber
} == $borrowernumber;
506 return 0 if ( $reserve->{found
} eq 'W' ) or ( $reserve->{found
} eq 'T' );
512 =head2 GetReserveCount
514 $number = &GetReserveCount($borrowernumber);
516 this function returns the number of reservation for a borrower given on input arg.
520 sub GetReserveCount
{
521 my ($borrowernumber) = @_;
523 my $dbh = C4
::Context
->dbh;
526 SELECT COUNT(*) AS counter
528 WHERE borrowernumber = ?
530 my $sth = $dbh->prepare($query);
531 $sth->execute($borrowernumber);
532 my $row = $sth->fetchrow_hashref;
533 return $row->{counter
};
536 =head2 GetOtherReserves
538 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
540 Check queued list of this document and check if this document must be transferred
544 sub GetOtherReserves
{
545 my ($itemnumber) = @_;
548 my ( undef, $checkreserves, undef ) = CheckReserves
($itemnumber);
549 if ($checkreserves) {
550 my $iteminfo = GetItem
($itemnumber);
551 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
552 $messages->{'transfert'} = $checkreserves->{'branchcode'};
553 #minus priorities of others reservs
554 ModReserveMinusPriority
(
556 $checkreserves->{'reserve_id'},
559 #launch the subroutine dotransfer
560 C4
::Items
::ModItemTransfer
(
562 $iteminfo->{'holdingbranch'},
563 $checkreserves->{'branchcode'}
568 #step 2b : case of a reservation on the same branch, set the waiting status
570 $messages->{'waiting'} = 1;
571 ModReserveMinusPriority
(
573 $checkreserves->{'reserve_id'},
575 ModReserveStatus
($itemnumber,'W');
578 $nextreservinfo = $checkreserves->{'borrowernumber'};
581 return ( $messages, $nextreservinfo );
584 =head2 ChargeReserveFee
586 $fee = ChargeReserveFee( $borrowernumber, $fee, $title );
588 Charge the fee for a reserve (if $fee > 0)
592 sub ChargeReserveFee
{
593 my ( $borrowernumber, $fee, $title ) = @_;
594 return if !$fee || $fee==0; # the last test is needed to include 0.00
596 INSERT INTO accountlines
( borrowernumber
, accountno
, date
, amount
, description
, accounttype
, amountoutstanding
) VALUES
(?
, ?
, NOW
(), ?
, ?
, 'Res', ?
)
598 my $dbh = C4
::Context
->dbh;
599 my $nextacctno = &getnextacctno
( $borrowernumber );
600 $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
605 $fee = GetReserveFee( $borrowernumber, $biblionumber );
607 Calculate the fee for a reserve (if applicable).
612 my ( $borrowernumber, $biblionumber ) = @_;
614 SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers
.categorycode
= categories
.categorycode WHERE borrowernumber
= ?
617 SELECT COUNT
(*) FROM items
618 LEFT JOIN issues USING
(itemnumber
)
619 WHERE items
.biblionumber
=? AND issues
.issue_id IS NULL
622 SELECT COUNT
(*) FROM reserves WHERE biblionumber
=? AND borrowernumber
<>?
625 my $dbh = C4
::Context
->dbh;
626 my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
627 my $hold_fee_mode = C4
::Context
->preference('HoldFeeMode') || 'not_always';
628 if( $fee and $fee > 0 and $hold_fee_mode eq 'not_always' ) {
629 # This is a reconstruction of the old code:
630 # Compare number of items with items issued, and optionally check holds
631 # If not all items are issued and there are no holds: charge no fee
632 # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
633 my ( $notissued, $reserved );
634 ( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
637 ( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
638 ( $biblionumber, $borrowernumber ) );
639 $fee = 0 if $reserved == 0;
645 =head2 GetReservesToBranch
647 @transreserv = GetReservesToBranch( $frombranch );
649 Get reserve list for a given branch
653 sub GetReservesToBranch
{
654 my ( $frombranch ) = @_;
655 my $dbh = C4
::Context
->dbh;
656 my $sth = $dbh->prepare(
657 "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
662 $sth->execute( $frombranch );
665 while ( my $data = $sth->fetchrow_hashref ) {
666 $transreserv[$i] = $data;
669 return (@transreserv);
672 =head2 GetReservesForBranch
674 @transreserv = GetReservesForBranch($frombranch);
678 sub GetReservesForBranch
{
679 my ($frombranch) = @_;
680 my $dbh = C4
::Context
->dbh;
683 SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
688 $query .= " AND branchcode=? " if ( $frombranch );
689 $query .= "ORDER BY waitingdate" ;
691 my $sth = $dbh->prepare($query);
693 $sth->execute($frombranch);
700 while ( my $data = $sth->fetchrow_hashref ) {
701 $transreserv[$i] = $data;
704 return (@transreserv);
707 =head2 GetReserveStatus
709 $reservestatus = GetReserveStatus($itemnumber);
711 Takes an itemnumber and returns the status of the reserve placed on it.
712 If several reserves exist, the reserve with the lower priority is given.
716 ## FIXME: I don't think this does what it thinks it does.
717 ## It only ever checks the first reserve result, even though
718 ## multiple reserves for that bib can have the itemnumber set
719 ## the sub is only used once in the codebase.
720 sub GetReserveStatus
{
721 my ($itemnumber) = @_;
723 my $dbh = C4
::Context
->dbh;
725 my ($sth, $found, $priority);
727 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
728 $sth->execute($itemnumber);
729 ($found, $priority) = $sth->fetchrow_array;
733 return 'Waiting' if $found eq 'W' and $priority == 0;
734 return 'Finished' if $found eq 'F';
737 return 'Reserved' if $priority > 0;
739 return ''; # empty string here will remove need for checking undef, or less log lines
744 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
745 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
746 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
748 Find a book in the reserves.
750 C<$itemnumber> is the book's item number.
751 C<$lookahead> is the number of days to look in advance for future reserves.
753 As I understand it, C<&CheckReserves> looks for the given item in the
754 reserves. If it is found, that's a match, and C<$status> is set to
757 Otherwise, it finds the most important item in the reserves with the
758 same biblio number as this book (I'm not clear on this) and returns it
759 with C<$status> set to C<Reserved>.
761 C<&CheckReserves> returns a two-element list:
763 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
765 C<$reserve> is the reserve item that matched. It is a
766 reference-to-hash whose keys are mostly the fields of the reserves
767 table in the Koha database.
772 my ( $item, $barcode, $lookahead_days, $ignore_borrowers) = @_;
773 my $dbh = C4
::Context
->dbh;
776 if (C4
::Context
->preference('item-level_itypes')){
778 SELECT items.biblionumber,
779 items.biblioitemnumber,
780 itemtypes.notforloan,
781 items.notforloan AS itemnotforloan,
787 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
788 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
793 SELECT items.biblionumber,
794 items.biblioitemnumber,
795 itemtypes.notforloan,
796 items.notforloan AS itemnotforloan,
802 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
803 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
808 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
809 $sth->execute($item);
812 $sth = $dbh->prepare("$select WHERE barcode = ?");
813 $sth->execute($barcode);
815 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
816 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged, $item_homebranch, $item_holdingbranch ) = $sth->fetchrow_array;
818 return if ( $damaged && !C4
::Context
->preference('AllowHoldsOnDamagedItems') );
820 return unless $itemnumber; # bail if we got nothing.
822 # if item is not for loan it cannot be reserved either.....
823 # except where items.notforloan < 0 : This indicates the item is holdable.
824 return if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
826 # Find this item in the reserves
827 my @reserves = _Findgroupreserve
( $bibitem, $biblio, $itemnumber, $lookahead_days, $ignore_borrowers);
829 # $priority and $highest are used to find the most important item
830 # in the list returned by &_Findgroupreserve. (The lower $priority,
831 # the more important the item.)
832 # $highest is the most important item we've seen so far.
834 if (scalar @reserves) {
835 my $LocalHoldsPriority = C4
::Context
->preference('LocalHoldsPriority');
836 my $LocalHoldsPriorityPatronControl = C4
::Context
->preference('LocalHoldsPriorityPatronControl');
837 my $LocalHoldsPriorityItemControl = C4
::Context
->preference('LocalHoldsPriorityItemControl');
839 my $priority = 10000000;
840 foreach my $res (@reserves) {
841 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
842 return ( "Waiting", $res, \
@reserves ); # Found it
846 my $local_hold_match;
848 if ($LocalHoldsPriority) {
849 $borrowerinfo = C4
::Members
::GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
850 $iteminfo = C4
::Items
::GetItem
($itemnumber);
852 my $local_holds_priority_item_branchcode =
853 $iteminfo->{$LocalHoldsPriorityItemControl};
854 my $local_holds_priority_patron_branchcode =
855 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
857 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
858 ?
$borrowerinfo->{branchcode
}
861 $local_holds_priority_item_branchcode eq
862 $local_holds_priority_patron_branchcode;
865 # See if this item is more important than what we've got so far
866 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
867 $iteminfo ||= C4
::Items
::GetItem
($itemnumber);
868 next if $res->{itemtype
} && $res->{itemtype
} ne _get_itype
( $iteminfo );
869 $borrowerinfo ||= C4
::Members
::GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
870 my $branch = GetReservesControlBranch
( $iteminfo, $borrowerinfo );
871 my $branchitemrule = C4
::Circulation
::GetBranchItemRule
($branch,$iteminfo->{'itype'});
872 next if ($branchitemrule->{'holdallowed'} == 0);
873 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
874 next if ( ($branchitemrule->{hold_fulfillment_policy
} ne 'any') && ($res->{branchcode
} ne $iteminfo->{ $branchitemrule->{hold_fulfillment_policy
} }) );
875 $priority = $res->{'priority'};
877 last if $local_hold_match;
883 # If we get this far, then no exact match was found.
884 # We return the most important (i.e. next) reservation.
886 $highest->{'itemnumber'} = $item;
887 return ( "Reserved", $highest, \
@reserves );
893 =head2 CancelExpiredReserves
895 CancelExpiredReserves();
897 Cancels all reserves with an expiration date from before today.
901 sub CancelExpiredReserves
{
903 # Cancel reserves that have passed their expiration date.
904 my $dbh = C4
::Context
->dbh;
905 my $sth = $dbh->prepare( "
906 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
907 AND expirationdate IS NOT NULL
912 while ( my $res = $sth->fetchrow_hashref() ) {
913 CancelReserve
({ reserve_id
=> $res->{'reserve_id'} });
916 # Cancel reserves that have been waiting too long
917 if ( C4
::Context
->preference("ExpireReservesMaxPickUpDelay") ) {
918 my $max_pickup_delay = C4
::Context
->preference("ReservesMaxPickUpDelay");
919 my $cancel_on_holidays = C4
::Context
->preference('ExpireReservesOnHolidays');
921 my $today = dt_from_string
();
923 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
924 $sth = $dbh->prepare( $query );
925 $sth->execute( $max_pickup_delay );
927 while ( my $res = $sth->fetchrow_hashref ) {
929 unless ( $cancel_on_holidays ) {
930 my $calendar = Koha
::Calendar
->new( branchcode
=> $res->{'branchcode'} );
931 my $is_holiday = $calendar->is_holiday( $today );
939 CancelReserve
({ reserve_id
=> $res->{'reserve_id'}, charge_cancel_fee
=> 1 });
946 =head2 AutoUnsuspendReserves
948 AutoUnsuspendReserves();
950 Unsuspends all suspended reserves with a suspend_until date from before today.
954 sub AutoUnsuspendReserves
{
955 my $today = dt_from_string
();
957 my @holds = Koha
::Holds
->search( { suspend_until
=> { '<' => $today->ymd() } } );
959 map { $_->suspend(0)->suspend_until(undef)->store() } @holds;
964 CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber, ] [ charge_cancel_fee => 1 ] });
966 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.
973 my $reserve_id = $params->{'reserve_id'};
974 # Filter out only the desired keys; this will insert undefined values for elements missing in
975 # \%params, but GetReserveId filters them out anyway.
976 $reserve_id = GetReserveId
( { biblionumber
=> $params->{'biblionumber'}, borrowernumber
=> $params->{'borrowernumber'}, itemnumber
=> $params->{'itemnumber'} } ) unless ( $reserve_id );
978 return unless ( $reserve_id );
980 my $dbh = C4
::Context
->dbh;
982 my $reserve = GetReserve
( $reserve_id );
985 my $hold = Koha
::Holds
->find( $reserve_id );
986 logaction
( 'HOLDS', 'CANCEL', $hold->reserve_id, Dumper
($hold->unblessed) )
987 if C4
::Context
->preference('HoldsLog');
991 SET cancellationdate = now(),
995 my $sth = $dbh->prepare($query);
996 $sth->execute( $reserve_id );
999 INSERT INTO old_reserves
1000 SELECT * FROM reserves
1001 WHERE reserve_id = ?
1003 $sth = $dbh->prepare($query);
1004 $sth->execute( $reserve_id );
1007 DELETE FROM reserves
1008 WHERE reserve_id = ?
1010 $sth = $dbh->prepare($query);
1011 $sth->execute( $reserve_id );
1013 # now fix the priority on the others....
1014 _FixPriority
({ biblionumber
=> $reserve->{biblionumber
} });
1016 # and, if desired, charge a cancel fee
1017 my $charge = C4
::Context
->preference("ExpireReservesMaxPickUpDelayCharge");
1018 if ( $charge && $params->{'charge_cancel_fee'} ) {
1019 manualinvoice
($reserve->{'borrowernumber'}, $reserve->{'itemnumber'}, '', 'HE', $charge);
1028 ModReserve({ rank => $rank,
1029 reserve_id => $reserve_id,
1030 branchcode => $branchcode
1031 [, itemnumber => $itemnumber ]
1032 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
1035 Change a hold request's priority or cancel it.
1037 C<$rank> specifies the effect of the change. If C<$rank>
1038 is 'W' or 'n', nothing happens. This corresponds to leaving a
1039 request alone when changing its priority in the holds queue
1042 If C<$rank> is 'del', the hold request is cancelled.
1044 If C<$rank> is an integer greater than zero, the priority of
1045 the request is set to that value. Since priority != 0 means
1046 that the item is not waiting on the hold shelf, setting the
1047 priority to a non-zero value also sets the request's found
1048 status and waiting date to NULL.
1050 The optional C<$itemnumber> parameter is used only when
1051 C<$rank> is a non-zero integer; if supplied, the itemnumber
1052 of the hold request is set accordingly; if omitted, the itemnumber
1055 B<FIXME:> Note that the forgoing can have the effect of causing
1056 item-level hold requests to turn into title-level requests. This
1057 will be fixed once reserves has separate columns for requested
1058 itemnumber and supplying itemnumber.
1063 my ( $params ) = @_;
1065 my $rank = $params->{'rank'};
1066 my $reserve_id = $params->{'reserve_id'};
1067 my $branchcode = $params->{'branchcode'};
1068 my $itemnumber = $params->{'itemnumber'};
1069 my $suspend_until = $params->{'suspend_until'};
1070 my $borrowernumber = $params->{'borrowernumber'};
1071 my $biblionumber = $params->{'biblionumber'};
1073 return if $rank eq "W";
1074 return if $rank eq "n";
1076 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
1077 $reserve_id = GetReserveId
({ biblionumber
=> $biblionumber, borrowernumber
=> $borrowernumber, itemnumber
=> $itemnumber }) unless ( $reserve_id );
1079 if ( $rank eq "del" ) {
1080 CancelReserve
({ reserve_id
=> $reserve_id });
1082 elsif ($rank =~ /^\d+/ and $rank > 0) {
1083 my $hold = Koha
::Holds
->find($reserve_id);
1084 logaction
( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper
($hold->unblessed) )
1085 if C4
::Context
->preference('HoldsLog');
1090 branchcode
=> $branchcode,
1091 itemnumber
=> $itemnumber,
1093 waitingdate
=> undef
1097 if ( defined( $suspend_until ) ) {
1098 if ( $suspend_until ) {
1099 $suspend_until = eval { dt_from_string
( $suspend_until ) };
1100 $hold->suspend_hold( $suspend_until );
1102 # If the hold is suspended leave the hold suspended, but convert it to an indefinite hold.
1103 # If the hold is not suspended, this does nothing.
1104 $hold->set( { suspend_until
=> undef } )->store();
1108 _FixPriority
({ reserve_id
=> $reserve_id, rank
=>$rank });
1112 =head2 ModReserveFill
1114 &ModReserveFill($reserve);
1116 Fill a reserve. If I understand this correctly, this means that the
1117 reserved book has been found and given to the patron who reserved it.
1119 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1120 whose keys are fields from the reserves table in the Koha database.
1124 sub ModReserveFill
{
1126 my $reserve_id = $res->{'reserve_id'};
1128 my $hold = Koha
::Holds
->find($reserve_id);
1130 # get the priority on this record....
1131 my $priority = $hold->priority;
1133 # update the hold statuses, no need to store it though, we will be deleting it anyway
1141 Koha
::Old
::Hold
->new( $hold->unblessed() )->store();
1145 if ( C4
::Context
->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
1146 my $reserve_fee = GetReserveFee
( $hold->borrowernumber, $hold->biblionumber );
1147 ChargeReserveFee
( $hold->borrowernumber, $reserve_fee, $hold->biblio->title );
1150 # now fix the priority on the others (if the priority wasn't
1151 # already sorted!)....
1152 unless ( $priority == 0 ) {
1153 _FixPriority
( { reserve_id
=> $reserve_id, biblionumber
=> $hold->biblionumber } );
1157 =head2 ModReserveStatus
1159 &ModReserveStatus($itemnumber, $newstatus);
1161 Update the reserve status for the active (priority=0) reserve.
1163 $itemnumber is the itemnumber the reserve is on
1165 $newstatus is the new status.
1169 sub ModReserveStatus
{
1171 #first : check if we have a reservation for this item .
1172 my ($itemnumber, $newstatus) = @_;
1173 my $dbh = C4
::Context
->dbh;
1175 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1176 my $sth_set = $dbh->prepare($query);
1177 $sth_set->execute( $newstatus, $itemnumber );
1179 if ( C4
::Context
->preference("ReturnToShelvingCart") && $newstatus ) {
1180 CartToShelf
( $itemnumber );
1184 =head2 ModReserveAffect
1186 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id);
1188 This function affect an item and a status for a given reserve, either fetched directly
1189 by record_id, or by borrowernumber and itemnumber or biblionumber. If only biblionumber
1190 is given, only first reserve returned is affected, which is ok for anything but
1193 if $transferToDo is not set, then the status is set to "Waiting" as well.
1194 otherwise, a transfer is on the way, and the end of the transfer will
1195 take care of the waiting status
1199 sub ModReserveAffect
{
1200 my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id ) = @_;
1201 my $dbh = C4
::Context
->dbh;
1203 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1204 # attached to $itemnumber
1205 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1206 $sth->execute($itemnumber);
1207 my ($biblionumber) = $sth->fetchrow;
1209 # get request - need to find out if item is already
1210 # waiting in order to not send duplicate hold filled notifications
1213 # Find hold by id if we have it
1214 $hold = Koha
::Holds
->find( $reserve_id ) if $reserve_id;
1215 # Find item level hold for this item if there is one
1216 $hold ||= Koha
::Holds
->search( { borrowernumber
=> $borrowernumber, itemnumber
=> $itemnumber } )->next();
1217 # Find record level hold if there is no item level hold
1218 $hold ||= Koha
::Holds
->search( { borrowernumber
=> $borrowernumber, biblionumber
=> $biblionumber } )->next();
1220 return unless $hold;
1222 $reserve_id = $hold->id();
1224 my $already_on_shelf = $hold->found && $hold->found eq 'W';
1226 # If we affect a reserve that has to be transferred, don't set to Waiting
1228 if ($transferToDo) {
1232 itemnumber
=> $itemnumber,
1238 # affect the reserve to Waiting as well.
1242 itemnumber
=> $itemnumber,
1244 waitingdate
=> dt_from_string
(),
1250 _koha_notify_reserve
( $hold->reserve_id )
1251 if ( !$transferToDo && !$already_on_shelf );
1253 _FixPriority
( { biblionumber
=> $biblionumber } );
1255 if ( C4
::Context
->preference("ReturnToShelvingCart") ) {
1256 CartToShelf
($itemnumber);
1262 =head2 ModReserveCancelAll
1264 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1266 function to cancel reserv,check other reserves, and transfer document if it's necessary
1270 sub ModReserveCancelAll
{
1273 my ( $itemnumber, $borrowernumber ) = @_;
1275 #step 1 : cancel the reservation
1276 my $CancelReserve = CancelReserve
({ itemnumber
=> $itemnumber, borrowernumber
=> $borrowernumber });
1278 #step 2 launch the subroutine of the others reserves
1279 ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
1281 return ( $messages, $nextreservinfo );
1284 =head2 ModReserveMinusPriority
1286 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1288 Reduce the values of queued list
1292 sub ModReserveMinusPriority
{
1293 my ( $itemnumber, $reserve_id ) = @_;
1295 #first step update the value of the first person on reserv
1296 my $dbh = C4
::Context
->dbh;
1299 SET priority = 0 , itemnumber = ?
1300 WHERE reserve_id = ?
1302 my $sth_upd = $dbh->prepare($query);
1303 $sth_upd->execute( $itemnumber, $reserve_id );
1304 # second step update all others reserves
1305 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '0' });
1308 =head2 GetReserveInfo
1310 &GetReserveInfo($reserve_id);
1312 Get item and borrower details for a current hold.
1313 Current implementation this query should have a single result.
1317 sub GetReserveInfo
{
1318 my ( $reserve_id ) = @_;
1319 my $dbh = C4
::Context
->dbh;
1324 reserves.borrowernumber,
1325 reserves.biblionumber,
1326 reserves.branchcode,
1327 reserves.waitingdate,
1343 items.holdingbranch,
1344 items.itemcallnumber,
1350 LEFT JOIN items USING(itemnumber)
1351 LEFT JOIN borrowers USING(borrowernumber)
1352 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1353 WHERE reserves.reserve_id = ?";
1354 my $sth = $dbh->prepare($strsth);
1355 $sth->execute($reserve_id);
1357 my $data = $sth->fetchrow_hashref;
1361 =head2 IsAvailableForItemLevelRequest
1363 my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
1365 Checks whether a given item record is available for an
1366 item-level hold request. An item is available if
1368 * it is not lost AND
1369 * it is not damaged AND
1370 * it is not withdrawn AND
1371 * does not have a not for loan value > 0
1373 Need to check the issuingrules onshelfholds column,
1374 if this is set items on the shelf can be placed on hold
1376 Note that IsAvailableForItemLevelRequest() does not
1377 check if the staff operator is authorized to place
1378 a request on the item - in particular,
1379 this routine does not check IndependentBranches
1380 and canreservefromotherbranches.
1384 sub IsAvailableForItemLevelRequest
{
1386 my $borrower = shift;
1388 my $dbh = C4
::Context
->dbh;
1389 # must check the notforloan setting of the itemtype
1390 # FIXME - a lot of places in the code do this
1391 # or something similar - need to be
1393 my $itype = _get_itype
($item);
1394 my $notforloan_per_itemtype
1395 = $dbh->selectrow_array("SELECT notforloan FROM itemtypes WHERE itemtype = ?",
1399 $notforloan_per_itemtype ||
1400 $item->{itemlost
} ||
1401 $item->{notforloan
} > 0 ||
1402 $item->{withdrawn
} ||
1403 ($item->{damaged
} && !C4
::Context
->preference('AllowHoldsOnDamagedItems'));
1405 my $on_shelf_holds = _OnShelfHoldsAllowed
($itype,$borrower->{categorycode
},$item->{holdingbranch
});
1407 if ( $on_shelf_holds == 1 ) {
1409 } elsif ( $on_shelf_holds == 2 ) {
1411 Koha
::Items
->search( { biblionumber
=> $item->{biblionumber
} } );
1413 my $any_available = 0;
1415 foreach my $i (@items) {
1418 || $i->notforloan > 0
1421 || IsItemOnHoldAndFound
( $i->id )
1423 && !C4
::Context
->preference('AllowHoldsOnDamagedItems') )
1424 || Koha
::ItemTypes
->find( $i->effective_itemtype() )->notforloan;
1427 return $any_available ?
0 : 1;
1430 return $item->{onloan
} || GetReserveStatus
($item->{itemnumber
}) eq "Waiting";
1433 =head2 OnShelfHoldsAllowed
1435 OnShelfHoldsAllowed($itemtype,$borrowercategory,$branchcode);
1437 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see if onshelf
1438 holds are allowed, returns true if so.
1442 sub OnShelfHoldsAllowed
{
1443 my ($item, $borrower) = @_;
1445 my $itype = _get_itype
($item);
1446 return _OnShelfHoldsAllowed
($itype,$borrower->{categorycode
},$item->{holdingbranch
});
1453 if (C4
::Context
->preference('item-level_itypes')) {
1454 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
1455 # When GetItem is fixed, we can remove this
1456 $itype = $item->{itype
};
1459 # XXX This is a bit dodgy. It relies on biblio itemtype column having different name.
1460 # So if we already have a biblioitems join when calling this function,
1461 # we don't need to access the database again
1462 $itype = $item->{itemtype
};
1465 my $dbh = C4
::Context
->dbh;
1466 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
1467 my $sth = $dbh->prepare($query);
1468 $sth->execute($item->{biblioitemnumber
});
1469 if (my $data = $sth->fetchrow_hashref()){
1470 $itype = $data->{itemtype
};
1476 sub _OnShelfHoldsAllowed
{
1477 my ($itype,$borrowercategory,$branchcode) = @_;
1479 my $issuing_rule = Koha
::IssuingRules
->get_effective_issuing_rule({ categorycode
=> $borrowercategory, itemtype
=> $itype, branchcode
=> $branchcode });
1480 return $issuing_rule ?
$issuing_rule->onshelfholds : undef;
1483 =head2 AlterPriority
1485 AlterPriority( $where, $reserve_id );
1487 This function changes a reserve's priority up, down, to the top, or to the bottom.
1488 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1493 my ( $where, $reserve_id ) = @_;
1495 my $reserve = GetReserve
( $reserve_id );
1497 if ( $reserve->{cancellationdate
} ) {
1498 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (".$reserve->{cancellationdate
}.')';
1502 if ( $where eq 'up' || $where eq 'down' ) {
1504 my $priority = $reserve->{'priority'};
1505 $priority = $where eq 'up' ?
$priority - 1 : $priority + 1;
1506 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> $priority })
1508 } elsif ( $where eq 'top' ) {
1510 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '1' })
1512 } elsif ( $where eq 'bottom' ) {
1514 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '999999' });
1519 =head2 ToggleLowestPriority
1521 ToggleLowestPriority( $borrowernumber, $biblionumber );
1523 This function sets the lowestPriority field to true if is false, and false if it is true.
1527 sub ToggleLowestPriority
{
1528 my ( $reserve_id ) = @_;
1530 my $dbh = C4
::Context
->dbh;
1532 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1533 $sth->execute( $reserve_id );
1535 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '999999' });
1538 =head2 ToggleSuspend
1540 ToggleSuspend( $reserve_id );
1542 This function sets the suspend field to true if is false, and false if it is true.
1543 If the reserve is currently suspended with a suspend_until date, that date will
1544 be cleared when it is unsuspended.
1549 my ( $reserve_id, $suspend_until ) = @_;
1551 $suspend_until = dt_from_string
($suspend_until) if ($suspend_until);
1553 my $hold = Koha
::Holds
->find( $reserve_id );
1555 if ( $hold->is_suspended ) {
1558 $hold->suspend_hold( $suspend_until );
1565 borrowernumber => $borrowernumber,
1566 [ biblionumber => $biblionumber, ]
1567 [ suspend_until => $suspend_until, ]
1568 [ suspend => $suspend ]
1571 This function accepts a set of hash keys as its parameters.
1572 It requires either borrowernumber or biblionumber, or both.
1574 suspend_until is wholly optional.
1581 my $borrowernumber = $params{'borrowernumber'} || undef;
1582 my $biblionumber = $params{'biblionumber'} || undef;
1583 my $suspend_until = $params{'suspend_until'} || undef;
1584 my $suspend = defined( $params{'suspend'} ) ?
$params{'suspend'} : 1;
1586 $suspend_until = eval { dt_from_string
($suspend_until) }
1587 if ( defined($suspend_until) );
1589 return unless ( $borrowernumber || $biblionumber );
1592 $params->{found
} = undef;
1593 $params->{borrowernumber
} = $borrowernumber if $borrowernumber;
1594 $params->{biblionumber
} = $biblionumber if $biblionumber;
1596 my @holds = Koha
::Holds
->search($params);
1599 map { $_->suspend_hold($suspend_until) } @holds;
1602 map { $_->resume() } @holds;
1610 reserve_id => $reserve_id,
1612 [ignoreSetLowestRank => $ignoreSetLowestRank]
1617 _FixPriority({ biblionumber => $biblionumber});
1619 This routine adjusts the priority of a hold request and holds
1622 In the first form, where a reserve_id is passed, the priority of the
1623 hold is set to supplied rank, and other holds for that bib are adjusted
1624 accordingly. If the rank is "del", the hold is cancelled. If no rank
1625 is supplied, all of the holds on that bib have their priority adjusted
1626 as if the second form had been used.
1628 In the second form, where a biblionumber is passed, the holds on that
1629 bib (that are not captured) are sorted in order of increasing priority,
1630 then have reserves.priority set so that the first non-captured hold
1631 has its priority set to 1, the second non-captured hold has its priority
1632 set to 2, and so forth.
1634 In both cases, holds that have the lowestPriority flag on are have their
1635 priority adjusted to ensure that they remain at the end of the line.
1637 Note that the ignoreSetLowestRank parameter is meant to be used only
1638 when _FixPriority calls itself.
1643 my ( $params ) = @_;
1644 my $reserve_id = $params->{reserve_id
};
1645 my $rank = $params->{rank
} // '';
1646 my $ignoreSetLowestRank = $params->{ignoreSetLowestRank
};
1647 my $biblionumber = $params->{biblionumber
};
1649 my $dbh = C4
::Context
->dbh;
1651 unless ( $biblionumber ) {
1652 my $res = GetReserve
( $reserve_id );
1653 $biblionumber = $res->{biblionumber
};
1656 if ( $rank eq "del" ) {
1657 CancelReserve
({ reserve_id
=> $reserve_id });
1659 elsif ( $rank eq "W" || $rank eq "0" ) {
1661 # make sure priority for waiting or in-transit items is 0
1665 WHERE reserve_id = ?
1666 AND found IN ('W', 'T')
1668 my $sth = $dbh->prepare($query);
1669 $sth->execute( $reserve_id );
1675 SELECT reserve_id, borrowernumber, reservedate
1677 WHERE biblionumber = ?
1678 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1679 ORDER BY priority ASC
1681 my $sth = $dbh->prepare($query);
1682 $sth->execute( $biblionumber );
1683 while ( my $line = $sth->fetchrow_hashref ) {
1684 push( @priority, $line );
1687 # To find the matching index
1689 my $key = -1; # to allow for 0 to be a valid result
1690 for ( $i = 0 ; $i < @priority ; $i++ ) {
1691 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1692 $key = $i; # save the index
1697 # if index exists in array then move it to new position
1698 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1699 my $new_rank = $rank -
1700 1; # $new_rank is what you want the new index to be in the array
1701 my $moving_item = splice( @priority, $key, 1 );
1702 splice( @priority, $new_rank, 0, $moving_item );
1705 # now fix the priority on those that are left....
1709 WHERE reserve_id = ?
1711 $sth = $dbh->prepare($query);
1712 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1715 $priority[$j]->{'reserve_id'}
1719 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1722 unless ( $ignoreSetLowestRank ) {
1723 while ( my $res = $sth->fetchrow_hashref() ) {
1725 reserve_id
=> $res->{'reserve_id'},
1727 ignoreSetLowestRank
=> 1
1733 =head2 _Findgroupreserve
1735 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead, $ignore_borrowers);
1737 Looks for a holds-queue based item-specific match first, then for a holds-queue title-level match, returning the
1738 first match found. If neither, then we look for non-holds-queue based holds.
1739 Lookahead is the number of days to look in advance.
1741 C<&_Findgroupreserve> returns :
1742 C<@results> is an array of references-to-hash whose keys are mostly
1743 fields from the reserves table of the Koha database, plus
1744 C<biblioitemnumber>.
1748 sub _Findgroupreserve
{
1749 my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_;
1750 my $dbh = C4
::Context
->dbh;
1752 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1753 # check for exact targeted match
1754 my $item_level_target_query = qq{
1755 SELECT reserves
.biblionumber AS biblionumber
,
1756 reserves
.borrowernumber AS borrowernumber
,
1757 reserves
.reservedate AS reservedate
,
1758 reserves
.branchcode AS branchcode
,
1759 reserves
.cancellationdate AS cancellationdate
,
1760 reserves
.found AS found
,
1761 reserves
.reservenotes AS reservenotes
,
1762 reserves
.priority AS priority
,
1763 reserves
.timestamp AS timestamp
,
1764 biblioitems
.biblioitemnumber AS biblioitemnumber
,
1765 reserves
.itemnumber AS itemnumber
,
1766 reserves
.reserve_id AS reserve_id
,
1767 reserves
.itemtype AS itemtype
1769 JOIN biblioitems USING
(biblionumber
)
1770 JOIN hold_fill_targets USING
(biblionumber
, borrowernumber
, itemnumber
)
1773 AND item_level_request
= 1
1775 AND reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1779 my $sth = $dbh->prepare($item_level_target_query);
1780 $sth->execute($itemnumber, $lookahead||0);
1782 if ( my $data = $sth->fetchrow_hashref ) {
1783 push( @results, $data )
1784 unless any
{ $data->{borrowernumber
} eq $_ } @
$ignore_borrowers ;
1786 return @results if @results;
1788 # check for title-level targeted match
1789 my $title_level_target_query = qq{
1790 SELECT reserves
.biblionumber AS biblionumber
,
1791 reserves
.borrowernumber AS borrowernumber
,
1792 reserves
.reservedate AS reservedate
,
1793 reserves
.branchcode AS branchcode
,
1794 reserves
.cancellationdate AS cancellationdate
,
1795 reserves
.found AS found
,
1796 reserves
.reservenotes AS reservenotes
,
1797 reserves
.priority AS priority
,
1798 reserves
.timestamp AS timestamp
,
1799 biblioitems
.biblioitemnumber AS biblioitemnumber
,
1800 reserves
.itemnumber AS itemnumber
,
1801 reserves
.reserve_id AS reserve_id
,
1802 reserves
.itemtype AS itemtype
1804 JOIN biblioitems USING
(biblionumber
)
1805 JOIN hold_fill_targets USING
(biblionumber
, borrowernumber
)
1808 AND item_level_request
= 0
1809 AND hold_fill_targets
.itemnumber
= ?
1810 AND reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1814 $sth = $dbh->prepare($title_level_target_query);
1815 $sth->execute($itemnumber, $lookahead||0);
1817 if ( my $data = $sth->fetchrow_hashref ) {
1818 push( @results, $data )
1819 unless any
{ $data->{borrowernumber
} eq $_ } @
$ignore_borrowers ;
1821 return @results if @results;
1824 SELECT reserves
.biblionumber AS biblionumber
,
1825 reserves
.borrowernumber AS borrowernumber
,
1826 reserves
.reservedate AS reservedate
,
1827 reserves
.waitingdate AS waitingdate
,
1828 reserves
.branchcode AS branchcode
,
1829 reserves
.cancellationdate AS cancellationdate
,
1830 reserves
.found AS found
,
1831 reserves
.reservenotes AS reservenotes
,
1832 reserves
.priority AS priority
,
1833 reserves
.timestamp AS timestamp
,
1834 reserves
.itemnumber AS itemnumber
,
1835 reserves
.reserve_id AS reserve_id
,
1836 reserves
.itemtype AS itemtype
1838 WHERE reserves
.biblionumber
= ?
1839 AND
(reserves
.itemnumber IS NULL OR reserves
.itemnumber
= ?
)
1840 AND reserves
.reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1844 $sth = $dbh->prepare($query);
1845 $sth->execute( $biblio, $itemnumber, $lookahead||0);
1847 while ( my $data = $sth->fetchrow_hashref ) {
1848 push( @results, $data )
1849 unless any
{ $data->{borrowernumber
} eq $_ } @
$ignore_borrowers ;
1854 =head2 _koha_notify_reserve
1856 _koha_notify_reserve( $hold->reserve_id );
1858 Sends a notification to the patron that their hold has been filled (through
1859 ModReserveAffect, _not_ ModReserveFill)
1861 The letter code for this notice may be found using the following query:
1863 select distinct letter_code
1864 from message_transports
1865 inner join message_attributes using (message_attribute_id)
1866 where message_name = 'Hold_Filled'
1868 This will probably sipmly be 'HOLD', but because it is defined in the database,
1869 it is subject to addition or change.
1871 The following tables are availalbe witin the notice:
1882 sub _koha_notify_reserve
{
1883 my $reserve_id = shift;
1884 my $hold = Koha
::Holds
->find($reserve_id);
1885 my $borrowernumber = $hold->borrowernumber;
1887 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
1889 # Try to get the borrower's email address
1890 my $to_address = C4
::Members
::GetNoticeEmailAddress
($borrowernumber);
1892 my $messagingprefs = C4
::Members
::Messaging
::GetMessagingPreferences
( {
1893 borrowernumber
=> $borrowernumber,
1894 message_name
=> 'Hold_Filled'
1897 my $library = Koha
::Libraries
->find( $hold->branchcode )->unblessed;
1899 my $admin_email_address = $library->{branchemail
} || C4
::Context
->preference('KohaAdminEmailAddress');
1901 my %letter_params = (
1902 module
=> 'reserves',
1903 branchcode
=> $hold->branchcode,
1905 'branches' => $library,
1906 'borrowers' => $borrower,
1907 'biblio' => $hold->biblionumber,
1908 'biblioitems' => $hold->biblionumber,
1909 'reserves' => $hold->unblessed,
1910 'items' => $hold->itemnumber,
1912 substitute
=> { today
=> output_pref
( { dt
=> dt_from_string
, dateonly
=> 1 } ) },
1915 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.
1916 my $send_notification = sub {
1917 my ( $mtt, $letter_code ) = (@_);
1918 return unless defined $letter_code;
1919 $letter_params{letter_code
} = $letter_code;
1920 $letter_params{message_transport_type
} = $mtt;
1921 my $letter = C4
::Letters
::GetPreparedLetter
( %letter_params );
1923 warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
1927 C4
::Letters
::EnqueueLetter
( {
1929 borrowernumber
=> $borrowernumber,
1930 from_address
=> $admin_email_address,
1931 message_transport_type
=> $mtt,
1935 while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports
} } ) {
1937 ( $mtt eq 'email' and not $to_address ) # No email address
1938 or ( $mtt eq 'sms' and not $borrower->{smsalertnumber
} ) # No SMS number
1939 or ( $mtt eq 'phone' and C4
::Context
->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
1942 &$send_notification($mtt, $letter_code);
1943 $notification_sent++;
1945 #Making sure that a print notification is sent if no other transport types can be utilized.
1946 if (! $notification_sent) {
1947 &$send_notification('print', 'HOLD');
1952 =head2 _ShiftPriorityByDateAndPriority
1954 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1956 This increments the priority of all reserves after the one
1957 with either the lowest date after C<$reservedate>
1958 or the lowest priority after C<$priority>.
1960 It effectively makes room for a new reserve to be inserted with a certain
1961 priority, which is returned.
1963 This is most useful when the reservedate can be set by the user. It allows
1964 the new reserve to be placed before other reserves that have a later
1965 reservedate. Since priority also is set by the form in reserves/request.pl
1966 the sub accounts for that too.
1970 sub _ShiftPriorityByDateAndPriority
{
1971 my ( $biblio, $resdate, $new_priority ) = @_;
1973 my $dbh = C4
::Context
->dbh;
1974 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1975 my $sth = $dbh->prepare( $query );
1976 $sth->execute( $biblio, $resdate, $new_priority );
1977 my $min_priority = $sth->fetchrow;
1978 # if no such matches are found, $new_priority remains as original value
1979 $new_priority = $min_priority if ( $min_priority );
1981 # Shift the priority up by one; works in conjunction with the next SQL statement
1982 $query = "UPDATE reserves
1983 SET priority = priority+1
1984 WHERE biblionumber = ?
1985 AND borrowernumber = ?
1988 my $sth_update = $dbh->prepare( $query );
1990 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1991 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1992 $sth = $dbh->prepare( $query );
1993 $sth->execute( $new_priority, $biblio );
1994 while ( my $row = $sth->fetchrow_hashref ) {
1995 $sth_update->execute( $biblio, $row->{borrowernumber
}, $row->{reservedate
} );
1998 return $new_priority; # so the caller knows what priority they wind up receiving
2001 =head2 OPACItemHoldsAllowed
2003 OPACItemHoldsAllowed($item_record,$borrower_record);
2005 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see
2006 if specific item holds are allowed, returns true if so.
2010 sub OPACItemHoldsAllowed
{
2011 my ($item,$borrower) = @_;
2013 my $branchcode = $item->{homebranch
} or die "No homebranch";
2015 my $dbh = C4
::Context
->dbh;
2016 if (C4
::Context
->preference('item-level_itypes')) {
2017 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
2018 # When GetItem is fixed, we can remove this
2019 $itype = $item->{itype
};
2022 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
2023 my $sth = $dbh->prepare($query);
2024 $sth->execute($item->{biblioitemnumber
});
2025 if (my $data = $sth->fetchrow_hashref()){
2026 $itype = $data->{itemtype
};
2030 my $query = "SELECT opacitemholds,categorycode,itemtype,branchcode FROM issuingrules WHERE
2031 (issuingrules.categorycode = ? OR issuingrules.categorycode = '*')
2033 (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2035 (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')
2037 issuingrules.categorycode desc,
2038 issuingrules.itemtype desc,
2039 issuingrules.branchcode desc
2041 my $sth = $dbh->prepare($query);
2042 $sth->execute($borrower->{categorycode
},$itype,$branchcode);
2043 my $data = $sth->fetchrow_hashref;
2044 my $opacitemholds = uc substr ($data->{opacitemholds
}, 0, 1);
2045 return '' if $opacitemholds eq 'N';
2046 return $opacitemholds;
2051 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
2053 Use when checking out an item to handle reserves
2054 If $cancelreserve boolean is set to true, it will remove existing reserve
2059 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
2061 my $lookahead = C4
::Context
->preference('ConfirmFutureHolds'); #number of days to look for future holds
2062 my ( $restype, $res, $all_reserves ) = CheckReserves
( $itemnumber, undef, $lookahead );
2065 my $biblionumber = $res->{biblionumber
};
2067 if ($res->{borrowernumber
} == $borrowernumber) {
2068 ModReserveFill
($res);
2072 # The item is reserved by someone else.
2073 # Find this item in the reserves
2076 foreach (@
$all_reserves) {
2077 $_->{'borrowernumber'} == $borrowernumber or next;
2078 $_->{'biblionumber'} == $biblionumber or next;
2085 # The item is reserved by the current patron
2086 ModReserveFill
($borr_res);
2089 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
2090 RevertWaitingStatus
({ itemnumber
=> $itemnumber });
2092 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
2093 CancelReserve
( { reserve_id
=> $res->{'reserve_id'} } );
2100 MergeHolds($dbh,$to_biblio, $from_biblio);
2102 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
2107 my ( $dbh, $to_biblio, $from_biblio ) = @_;
2108 my $sth = $dbh->prepare(
2109 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
2111 $sth->execute($from_biblio);
2112 if ( my $data = $sth->fetchrow_hashref() ) {
2114 # holds exist on old record, if not we don't need to do anything
2115 $sth = $dbh->prepare(
2116 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
2117 $sth->execute( $to_biblio, $from_biblio );
2120 # don't reorder those already waiting
2122 $sth = $dbh->prepare(
2123 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
2125 my $upd_sth = $dbh->prepare(
2126 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
2127 AND reservedate = ? AND (itemnumber = ? or itemnumber is NULL) "
2129 $sth->execute( $to_biblio, 'W', 'T' );
2131 while ( my $reserve = $sth->fetchrow_hashref() ) {
2133 $priority, $to_biblio,
2134 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
2135 $reserve->{'itemnumber'}
2142 =head2 RevertWaitingStatus
2144 RevertWaitingStatus({ itemnumber => $itemnumber });
2146 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
2148 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
2149 item level hold, even if it was only a bibliolevel hold to
2150 begin with. This is because we can no longer know if a hold
2151 was item-level or bib-level after a hold has been set to
2156 sub RevertWaitingStatus
{
2157 my ( $params ) = @_;
2158 my $itemnumber = $params->{'itemnumber'};
2160 return unless ( $itemnumber );
2162 my $dbh = C4
::Context
->dbh;
2164 ## Get the waiting reserve we want to revert
2166 SELECT * FROM reserves
2167 WHERE itemnumber = ?
2168 AND found IS NOT NULL
2170 my $sth = $dbh->prepare( $query );
2171 $sth->execute( $itemnumber );
2172 my $reserve = $sth->fetchrow_hashref();
2174 ## Increment the priority of all other non-waiting
2175 ## reserves for this bib record
2179 priority = priority + 1
2185 $sth = $dbh->prepare( $query );
2186 $sth->execute( $reserve->{'biblionumber'} );
2188 ## Fix up the currently waiting reserve
2198 $sth = $dbh->prepare( $query );
2199 $sth->execute( $reserve->{'reserve_id'} );
2200 _FixPriority
( { biblionumber
=> $reserve->{biblionumber
} } );
2205 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
2207 Returnes the first reserve id that matches the given criteria
2212 my ( $params ) = @_;
2214 return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
2216 foreach my $key ( keys %$params ) {
2217 delete $params->{$key} unless defined( $params->{$key} );
2220 my $hold = Koha
::Holds
->search( $params )->next();
2222 return unless $hold;
2229 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
2231 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2233 The letter code will be HOLD_SLIP, and the following tables are
2234 available within the slip:
2246 my ($branch, $borrowernumber, $biblionumber) = @_;
2248 # return unless ( C4::Context->boolean_preference('printreserveslips') );
2250 my $reserve_id = GetReserveId
({
2251 biblionumber
=> $biblionumber,
2252 borrowernumber
=> $borrowernumber
2254 my $reserve = GetReserveInfo
($reserve_id) or return;
2256 return C4
::Letters
::GetPreparedLetter
(
2257 module
=> 'circulation',
2258 letter_code
=> 'HOLD_SLIP',
2259 branchcode
=> $branch,
2261 'reserves' => $reserve,
2262 'branches' => $reserve->{branchcode
},
2263 'borrowers' => $reserve->{borrowernumber
},
2264 'biblio' => $reserve->{biblionumber
},
2265 'biblioitems' => $reserve->{biblionumber
},
2266 'items' => $reserve->{itemnumber
},
2271 =head2 GetReservesControlBranch
2273 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2275 Return the branchcode to be used to determine which reserves
2276 policy applies to a transaction.
2278 C<$item> is a hashref for an item. Only 'homebranch' is used.
2280 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2284 sub GetReservesControlBranch
{
2285 my ( $item, $borrower ) = @_;
2287 my $reserves_control = C4
::Context
->preference('ReservesControlBranch');
2290 ( $reserves_control eq 'ItemHomeLibrary' ) ?
$item->{'homebranch'}
2291 : ( $reserves_control eq 'PatronLibrary' ) ?
$borrower->{'branchcode'}
2297 =head2 CalculatePriority
2299 my $p = CalculatePriority($biblionumber, $resdate);
2301 Calculate priority for a new reserve on biblionumber, placing it at
2302 the end of the line of all holds whose start date falls before
2303 the current system time and that are neither on the hold shelf
2306 The reserve date parameter is optional; if it is supplied, the
2307 priority is based on the set of holds whose start date falls before
2308 the parameter value.
2310 After calculation of this priority, it is recommended to call
2311 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2316 sub CalculatePriority
{
2317 my ( $biblionumber, $resdate ) = @_;
2320 SELECT COUNT(*) FROM reserves
2321 WHERE biblionumber = ?
2323 AND (found IS NULL OR found = '')
2325 #skip found==W or found==T (waiting or transit holds)
2327 $sql.= ' AND ( reservedate <= ? )';
2330 $sql.= ' AND ( reservedate < NOW() )';
2332 my $dbh = C4
::Context
->dbh();
2333 my @row = $dbh->selectrow_array(
2336 $resdate ?
($biblionumber, $resdate) : ($biblionumber)
2339 return @row ?
$row[0]+1 : 1;
2342 =head2 IsItemOnHoldAndFound
2344 my $bool = IsItemFoundHold( $itemnumber );
2346 Returns true if the item is currently on hold
2347 and that hold has a non-null found status ( W, T, etc. )
2351 sub IsItemOnHoldAndFound
{
2352 my ($itemnumber) = @_;
2354 my $rs = Koha
::Database
->new()->schema()->resultset('Reserve');
2356 my $found = $rs->count(
2358 itemnumber
=> $itemnumber,
2359 found
=> { '!=' => undef }
2366 =head2 GetMaxPatronHoldsForRecord
2368 my $holds_per_record = ReservesControlBranch( $borrowernumber, $biblionumber );
2370 For multiple holds on a given record for a given patron, the max
2371 number of record level holds that a patron can be placed is the highest
2372 value of the holds_per_record rule for each item if the record for that
2373 patron. This subroutine finds and returns the highest holds_per_record
2374 rule value for a given patron id and record id.
2378 sub GetMaxPatronHoldsForRecord
{
2379 my ( $borrowernumber, $biblionumber ) = @_;
2381 my $patron = Koha
::Patrons
->find($borrowernumber);
2382 my @items = Koha
::Items
->search( { biblionumber
=> $biblionumber } );
2384 my $controlbranch = C4
::Context
->preference('ReservesControlBranch');
2386 my $categorycode = $patron->categorycode;
2388 $branchcode = $patron->branchcode if ( $controlbranch eq "PatronLibrary" );
2391 foreach my $item (@items) {
2392 my $itemtype = $item->effective_itemtype();
2394 $branchcode = $item->homebranch if ( $controlbranch eq "ItemHomeLibrary" );
2396 my $rule = GetHoldRule
( $categorycode, $itemtype, $branchcode );
2397 my $holds_per_record = $rule ?
$rule->{holds_per_record
} : 0;
2398 $max = $holds_per_record if $holds_per_record > $max;
2406 my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
2408 Returns the matching hold related issuingrule fields for a given
2409 patron category, itemtype, and library.
2414 my ( $categorycode, $itemtype, $branchcode ) = @_;
2416 my $dbh = C4
::Context
->dbh;
2418 my $sth = $dbh->prepare(
2420 SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record
2422 WHERE (categorycode in (?,'*') )
2423 AND (itemtype IN (?,'*'))
2424 AND (branchcode IN (?,'*'))
2425 ORDER BY categorycode DESC,
2431 $sth->execute( $categorycode, $itemtype, $branchcode );
2433 return $sth->fetchrow_hashref();
2438 Koha Development Team <http://koha-community.org/>