Bug 16281: Remove the use of 'onclick' from Reports module
[koha.git] / C4 / Reserves.pm
blob58cfdbfaf81fb7d43d5a45c7cfd507bb4d7e9320
1 package C4::Reserves;
3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007-2010 BibLibre Paul POULAIN
6 # 2011 Catalyst IT
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>.
24 use strict;
25 #use warnings; FIXME - Bug 2505
26 use C4::Context;
27 use C4::Biblio;
28 use C4::Members;
29 use C4::Items;
30 use C4::Circulation;
31 use C4::Accounts;
33 # for _koha_notify_reserve
34 use C4::Members::Messaging;
35 use C4::Members qw();
36 use C4::Letters;
38 use Koha::DateUtils;
39 use Koha::Calendar;
40 use Koha::Database;
41 use Koha::Hold;
42 use Koha::Holds;
43 use Koha::Libraries;
44 use Koha::Items;
45 use Koha::ItemTypes;
47 use List::MoreUtils qw( firstidx any );
48 use Carp;
50 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
52 =head1 NAME
54 C4::Reserves - Koha functions for dealing with reservation.
56 =head1 SYNOPSIS
58 use C4::Reserves;
60 =head1 DESCRIPTION
62 This modules provides somes functions to deal with reservations.
64 Reserves are stored in reserves table.
65 The following columns contains important values :
66 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
67 =0 : then the reserve is being dealed
68 - found : NULL : means the patron requested the 1st available, and we haven't chosen the item
69 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
70 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
71 F(inished) : the reserve has been completed, and is done
72 - itemnumber : empty : the reserve is still unaffected to an item
73 filled: the reserve is attached to an item
74 The complete workflow is :
75 ==== 1st use case ====
76 patron request a document, 1st available : P >0, F=NULL, I=NULL
77 a library having it run "transfertodo", and clic on the list
78 if there is no transfer to do, the reserve waiting
79 patron can pick it up P =0, F=W, I=filled
80 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
81 The pickup library receive the book, it check in P =0, F=W, I=filled
82 The patron borrow the book P =0, F=F, I=filled
84 ==== 2nd use case ====
85 patron requests a document, a given item,
86 If pickup is holding branch P =0, F=W, I=filled
87 If transfer needed, write in branchtransfer P =0, F=T, I=filled
88 The pickup library receive the book, it checks it in P =0, F=W, I=filled
89 The patron borrow the book P =0, F=F, I=filled
91 =head1 FUNCTIONS
93 =cut
95 BEGIN {
96 require Exporter;
97 @ISA = qw(Exporter);
98 @EXPORT = qw(
99 &AddReserve
101 &GetReserve
102 &GetReservesFromItemnumber
103 &GetReservesFromBiblionumber
104 &GetReservesFromBorrowernumber
105 &GetReservesForBranch
106 &GetReservesToBranch
107 &GetReserveCount
108 &GetReserveInfo
109 &GetReserveStatus
111 &GetOtherReserves
113 &ModReserveFill
114 &ModReserveAffect
115 &ModReserve
116 &ModReserveStatus
117 &ModReserveCancelAll
118 &ModReserveMinusPriority
119 &MoveReserve
121 &CheckReserves
122 &CanBookBeReserved
123 &CanItemBeReserved
124 &CanReserveBeCanceledFromOpac
125 &CancelReserve
126 &CancelExpiredReserves
128 &AutoUnsuspendReserves
130 &IsAvailableForItemLevelRequest
132 &OPACItemHoldsAllowed
134 &AlterPriority
135 &ToggleLowestPriority
137 &ReserveSlip
138 &ToggleSuspend
139 &SuspendAll
141 &GetReservesControlBranch
143 IsItemOnHoldAndFound
145 @EXPORT_OK = qw( MergeHolds );
148 =head2 AddReserve
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:
156 branches
157 borrowers
158 biblio
159 biblioitems
160 items
162 =cut
164 sub AddReserve {
165 my (
166 $branch, $borrowernumber, $biblionumber, $bibitems,
167 $priority, $resdate, $expdate, $notes,
168 $title, $checkitem, $found, $itemtype
169 ) = @_;
171 if ( Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->count() > 0 ) {
172 carp("AddReserve: borrower $borrowernumber already has a hold for biblionumber $biblionumber");
173 return;
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 );
189 my $waitingdate;
191 # If the reserv had the waiting status, we had the value of the resdate
192 if ( $found eq 'W' ) {
193 $waitingdate = $resdate;
196 # Don't add itemtype limit if specific item is selected
197 $itemtype = undef if $checkitem;
199 # updates take place here
200 my $hold = Koha::Hold->new(
202 borrowernumber => $borrowernumber,
203 biblionumber => $biblionumber,
204 reservedate => $resdate,
205 branchcode => $branch,
206 priority => $priority,
207 reservenotes => $notes,
208 itemnumber => $checkitem,
209 found => $found,
210 waitingdate => $waitingdate,
211 expirationdate => $expdate,
212 itemtype => $itemtype,
214 )->store();
215 my $reserve_id = $hold->id();
217 # add a reserve fee if needed
218 my $fee = GetReserveFee( $borrowernumber, $biblionumber );
219 ChargeReserveFee( $borrowernumber, $fee, $title );
221 _FixPriority({ biblionumber => $biblionumber});
223 # Send e-mail to librarian if syspref is active
224 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
225 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
226 my $library = Koha::Libraries->find($borrower->{branchcode})->unblessed;
227 if ( my $letter = C4::Letters::GetPreparedLetter (
228 module => 'reserves',
229 letter_code => 'HOLDPLACED',
230 branchcode => $branch,
231 tables => {
232 'branches' => $library,
233 'borrowers' => $borrower,
234 'biblio' => $biblionumber,
235 'biblioitems' => $biblionumber,
236 'items' => $checkitem,
238 ) ) {
240 my $admin_email_address = $library->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
242 C4::Letters::EnqueueLetter(
243 { letter => $letter,
244 borrowernumber => $borrowernumber,
245 message_transport_type => 'email',
246 from_address => $admin_email_address,
247 to_address => $admin_email_address,
253 return $reserve_id;
256 =head2 GetReserve
258 $res = GetReserve( $reserve_id );
260 Return the current reserve.
262 =cut
264 sub GetReserve {
265 my ($reserve_id) = @_;
267 my $dbh = C4::Context->dbh;
269 my $query = "SELECT * FROM reserves WHERE reserve_id = ?";
270 my $sth = $dbh->prepare( $query );
271 $sth->execute( $reserve_id );
272 return $sth->fetchrow_hashref();
275 =head2 GetReservesFromBiblionumber
277 my $reserves = GetReservesFromBiblionumber({
278 biblionumber => $biblionumber,
279 [ itemnumber => $itemnumber, ]
280 [ all_dates => 1|0 ]
283 This function gets the list of reservations for one C<$biblionumber>,
284 returning an arrayref pointing to the reserves for C<$biblionumber>.
286 By default, only reserves whose start date falls before the current
287 time are returned. To return all reserves, including future ones,
288 the C<all_dates> parameter can be included and set to a true value.
290 If the C<itemnumber> parameter is supplied, reserves must be targeted
291 to that item or not targeted to any item at all; otherwise, they
292 are excluded from the list.
294 =cut
296 sub GetReservesFromBiblionumber {
297 my ( $params ) = @_;
298 my $biblionumber = $params->{biblionumber} or return [];
299 my $itemnumber = $params->{itemnumber};
300 my $all_dates = $params->{all_dates} // 0;
301 my $dbh = C4::Context->dbh;
303 # Find the desired items in the reserves
304 my @params;
305 my $query = "
306 SELECT reserve_id,
307 branchcode,
308 timestamp AS rtimestamp,
309 priority,
310 biblionumber,
311 borrowernumber,
312 reservedate,
313 found,
314 itemnumber,
315 reservenotes,
316 expirationdate,
317 lowestPriority,
318 suspend,
319 suspend_until,
320 itemtype
321 FROM reserves
322 WHERE biblionumber = ? ";
323 push( @params, $biblionumber );
324 unless ( $all_dates ) {
325 $query .= " AND reservedate <= CAST(NOW() AS DATE) ";
327 if ( $itemnumber ) {
328 $query .= " AND ( itemnumber IS NULL OR itemnumber = ? )";
329 push( @params, $itemnumber );
331 $query .= "ORDER BY priority";
332 my $sth = $dbh->prepare($query);
333 $sth->execute( @params );
334 my @results;
335 while ( my $data = $sth->fetchrow_hashref ) {
336 push @results, $data;
338 return \@results;
341 =head2 GetReservesFromItemnumber
343 ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $waitingdate ) = GetReservesFromItemnumber($itemnumber);
345 Get the first reserve for a specific item number (based on priority). Returns the abovementioned values for that reserve.
347 The routine does not look at future reserves (read: item level holds), but DOES include future waits (a confirmed future hold).
349 =cut
351 sub GetReservesFromItemnumber {
352 my ($itemnumber) = @_;
354 my $schema = Koha::Database->new()->schema();
356 my $r = $schema->resultset('Reserve')->search(
358 itemnumber => $itemnumber,
359 suspend => 0,
360 -or => [
361 reservedate => \'<= CAST( NOW() AS DATE )',
362 waitingdate => { '!=', undef }
366 order_by => 'priority',
368 )->first();
370 return unless $r;
372 return (
373 $r->reservedate(),
374 $r->get_column('borrowernumber'),
375 $r->get_column('branchcode'),
376 $r->reserve_id(),
377 $r->waitingdate(),
381 =head2 GetReservesFromBorrowernumber
383 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
385 TODO :: Descritpion
387 =cut
389 sub GetReservesFromBorrowernumber {
390 my ( $borrowernumber, $status ) = @_;
391 my $dbh = C4::Context->dbh;
392 my $sth;
393 if ($status) {
394 $sth = $dbh->prepare("
395 SELECT *
396 FROM reserves
397 WHERE borrowernumber=?
398 AND found =?
399 ORDER BY reservedate
401 $sth->execute($borrowernumber,$status);
402 } else {
403 $sth = $dbh->prepare("
404 SELECT *
405 FROM reserves
406 WHERE borrowernumber=?
407 ORDER BY reservedate
409 $sth->execute($borrowernumber);
411 my $data = $sth->fetchall_arrayref({});
412 return @$data;
415 =head2 CanBookBeReserved
417 $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
418 if ($canReserve eq 'OK') { #We can reserve this Item! }
420 See CanItemBeReserved() for possible return values.
422 =cut
424 sub CanBookBeReserved{
425 my ($borrowernumber, $biblionumber) = @_;
427 my $items = GetItemnumbersForBiblio($biblionumber);
428 #get items linked via host records
429 my @hostitems = get_hostitemnumbers_of($biblionumber);
430 if (@hostitems){
431 push (@$items,@hostitems);
434 my $canReserve;
435 foreach my $item (@$items) {
436 $canReserve = CanItemBeReserved( $borrowernumber, $item );
437 return 'OK' if $canReserve eq 'OK';
439 return $canReserve;
442 =head2 CanItemBeReserved
444 $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber)
445 if ($canReserve eq 'OK') { #We can reserve this Item! }
447 @RETURNS OK, if the Item can be reserved.
448 ageRestricted, if the Item is age restricted for this borrower.
449 damaged, if the Item is damaged.
450 cannotReserveFromOtherBranches, if syspref 'canreservefromotherbranches' is OK.
451 tooManyReserves, if the borrower has exceeded his maximum reserve amount.
452 notReservable, if holds on this item are not allowed
454 =cut
456 sub CanItemBeReserved{
457 my ($borrowernumber, $itemnumber) = @_;
459 my $dbh = C4::Context->dbh;
460 my $ruleitemtype; # itemtype of the matching issuing rule
461 my $allowedreserves = 0;
463 # we retrieve borrowers and items informations #
464 # item->{itype} will come for biblioitems if necessery
465 my $item = GetItem($itemnumber);
466 my $biblioData = C4::Biblio::GetBiblioData( $item->{biblionumber} );
467 my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
469 # If an item is damaged and we don't allow holds on damaged items, we can stop right here
470 return 'damaged' if ( $item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems') );
472 #Check for the age restriction
473 my ($ageRestriction, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction( $biblioData->{agerestriction}, $borrower );
474 return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
476 my $controlbranch = C4::Context->preference('ReservesControlBranch');
478 # we retrieve user rights on this itemtype and branchcode
479 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
480 FROM issuingrules
481 WHERE (categorycode in (?,'*') )
482 AND (itemtype IN (?,'*'))
483 AND (branchcode IN (?,'*'))
484 ORDER BY
485 categorycode DESC,
486 itemtype DESC,
487 branchcode DESC;"
490 my $querycount ="SELECT
491 count(*) as count
492 FROM reserves
493 LEFT JOIN items USING (itemnumber)
494 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
495 LEFT JOIN borrowers USING (borrowernumber)
496 WHERE borrowernumber = ?
500 my $branchcode = "";
501 my $branchfield = "reserves.branchcode";
503 if( $controlbranch eq "ItemHomeLibrary" ){
504 $branchfield = "items.homebranch";
505 $branchcode = $item->{homebranch};
506 }elsif( $controlbranch eq "PatronLibrary" ){
507 $branchfield = "borrowers.branchcode";
508 $branchcode = $borrower->{branchcode};
511 # we retrieve rights
512 $sth->execute($borrower->{'categorycode'}, $item->{'itype'}, $branchcode);
513 if(my $rights = $sth->fetchrow_hashref()){
514 $ruleitemtype = $rights->{itemtype};
515 $allowedreserves = $rights->{reservesallowed};
516 }else{
517 $ruleitemtype = '*';
520 # we retrieve count
522 $querycount .= "AND $branchfield = ?";
524 # If using item-level itypes, fall back to the record
525 # level itemtype if the hold has no associated item
526 $querycount .=
527 C4::Context->preference('item-level_itypes')
528 ? " AND COALESCE( items.itype, biblioitems.itemtype ) = ?"
529 : " AND biblioitems.itemtype = ?"
530 if ( $ruleitemtype ne "*" );
532 my $sthcount = $dbh->prepare($querycount);
534 if($ruleitemtype eq "*"){
535 $sthcount->execute($borrowernumber, $branchcode);
536 }else{
537 $sthcount->execute($borrowernumber, $branchcode, $ruleitemtype);
540 my $reservecount = "0";
541 if(my $rowcount = $sthcount->fetchrow_hashref()){
542 $reservecount = $rowcount->{count};
544 # we check if it's ok or not
545 if( $reservecount >= $allowedreserves ){
546 return 'tooManyReserves';
549 my $circ_control_branch = C4::Circulation::_GetCircControlBranch($item,
550 $borrower);
551 my $branchitemrule = C4::Circulation::GetBranchItemRule($circ_control_branch,
552 $item->{itype});
554 if ( $branchitemrule->{holdallowed} == 0 ) {
555 return 'notReservable';
558 if ( $branchitemrule->{holdallowed} == 1
559 && $borrower->{branchcode} ne $item->{homebranch} )
561 return 'cannotReserveFromOtherBranches';
564 # If reservecount is ok, we check item branch if IndependentBranches is ON
565 # and canreservefromotherbranches is OFF
566 if ( C4::Context->preference('IndependentBranches')
567 and !C4::Context->preference('canreservefromotherbranches') )
569 my $itembranch = $item->{homebranch};
570 if ($itembranch ne $borrower->{branchcode}) {
571 return 'cannotReserveFromOtherBranches';
575 return 'OK';
578 =head2 CanReserveBeCanceledFromOpac
580 $number = CanReserveBeCanceledFromOpac($reserve_id, $borrowernumber);
582 returns 1 if reserve can be cancelled by user from OPAC.
583 First check if reserve belongs to user, next checks if reserve is not in
584 transfer or waiting status
586 =cut
588 sub CanReserveBeCanceledFromOpac {
589 my ($reserve_id, $borrowernumber) = @_;
591 return unless $reserve_id and $borrowernumber;
592 my $reserve = GetReserve($reserve_id);
594 return 0 unless $reserve->{borrowernumber} == $borrowernumber;
595 return 0 if ( $reserve->{found} eq 'W' ) or ( $reserve->{found} eq 'T' );
597 return 1;
601 =head2 GetReserveCount
603 $number = &GetReserveCount($borrowernumber);
605 this function returns the number of reservation for a borrower given on input arg.
607 =cut
609 sub GetReserveCount {
610 my ($borrowernumber) = @_;
612 my $dbh = C4::Context->dbh;
614 my $query = "
615 SELECT COUNT(*) AS counter
616 FROM reserves
617 WHERE borrowernumber = ?
619 my $sth = $dbh->prepare($query);
620 $sth->execute($borrowernumber);
621 my $row = $sth->fetchrow_hashref;
622 return $row->{counter};
625 =head2 GetOtherReserves
627 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
629 Check queued list of this document and check if this document must be transferred
631 =cut
633 sub GetOtherReserves {
634 my ($itemnumber) = @_;
635 my $messages;
636 my $nextreservinfo;
637 my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
638 if ($checkreserves) {
639 my $iteminfo = GetItem($itemnumber);
640 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
641 $messages->{'transfert'} = $checkreserves->{'branchcode'};
642 #minus priorities of others reservs
643 ModReserveMinusPriority(
644 $itemnumber,
645 $checkreserves->{'reserve_id'},
648 #launch the subroutine dotransfer
649 C4::Items::ModItemTransfer(
650 $itemnumber,
651 $iteminfo->{'holdingbranch'},
652 $checkreserves->{'branchcode'}
657 #step 2b : case of a reservation on the same branch, set the waiting status
658 else {
659 $messages->{'waiting'} = 1;
660 ModReserveMinusPriority(
661 $itemnumber,
662 $checkreserves->{'reserve_id'},
664 ModReserveStatus($itemnumber,'W');
667 $nextreservinfo = $checkreserves->{'borrowernumber'};
670 return ( $messages, $nextreservinfo );
673 =head2 ChargeReserveFee
675 $fee = ChargeReserveFee( $borrowernumber, $fee, $title );
677 Charge the fee for a reserve (if $fee > 0)
679 =cut
681 sub ChargeReserveFee {
682 my ( $borrowernumber, $fee, $title ) = @_;
683 return if !$fee || $fee==0; # the last test is needed to include 0.00
684 my $accquery = qq{
685 INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
687 my $dbh = C4::Context->dbh;
688 my $nextacctno = &getnextacctno( $borrowernumber );
689 $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
692 =head2 GetReserveFee
694 $fee = GetReserveFee( $borrowernumber, $biblionumber );
696 Calculate the fee for a reserve (if applicable).
698 =cut
700 sub GetReserveFee {
701 my ( $borrowernumber, $biblionumber ) = @_;
702 my $borquery = qq{
703 SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE borrowernumber = ?
705 my $issue_qry = qq{
706 SELECT COUNT(*) FROM items
707 LEFT JOIN issues USING (itemnumber)
708 WHERE items.biblionumber=? AND issues.issue_id IS NULL
710 my $holds_qry = qq{
711 SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?
714 my $dbh = C4::Context->dbh;
715 my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
716 my $hold_fee_mode = C4::Context->preference('HoldFeeMode') || 'not_always';
717 if( $fee and $fee > 0 and $hold_fee_mode ne 'always' ) {
718 # This is a reconstruction of the old code:
719 # Compare number of items with items issued, and optionally check holds
720 # If not all items are issued and there are no holds: charge no fee
721 # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
722 my ( $notissued, $reserved );
723 ( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
724 ( $biblionumber ) );
725 if( $notissued ) {
726 ( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
727 ( $biblionumber, $borrowernumber ) );
728 $fee = 0 if $reserved == 0;
731 return $fee;
734 =head2 GetReservesToBranch
736 @transreserv = GetReservesToBranch( $frombranch );
738 Get reserve list for a given branch
740 =cut
742 sub GetReservesToBranch {
743 my ( $frombranch ) = @_;
744 my $dbh = C4::Context->dbh;
745 my $sth = $dbh->prepare(
746 "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
747 FROM reserves
748 WHERE priority='0'
749 AND branchcode=?"
751 $sth->execute( $frombranch );
752 my @transreserv;
753 my $i = 0;
754 while ( my $data = $sth->fetchrow_hashref ) {
755 $transreserv[$i] = $data;
756 $i++;
758 return (@transreserv);
761 =head2 GetReservesForBranch
763 @transreserv = GetReservesForBranch($frombranch);
765 =cut
767 sub GetReservesForBranch {
768 my ($frombranch) = @_;
769 my $dbh = C4::Context->dbh;
771 my $query = "
772 SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
773 FROM reserves
774 WHERE priority='0'
775 AND found='W'
777 $query .= " AND branchcode=? " if ( $frombranch );
778 $query .= "ORDER BY waitingdate" ;
780 my $sth = $dbh->prepare($query);
781 if ($frombranch){
782 $sth->execute($frombranch);
783 } else {
784 $sth->execute();
787 my @transreserv;
788 my $i = 0;
789 while ( my $data = $sth->fetchrow_hashref ) {
790 $transreserv[$i] = $data;
791 $i++;
793 return (@transreserv);
796 =head2 GetReserveStatus
798 $reservestatus = GetReserveStatus($itemnumber);
800 Takes an itemnumber and returns the status of the reserve placed on it.
801 If several reserves exist, the reserve with the lower priority is given.
803 =cut
805 ## FIXME: I don't think this does what it thinks it does.
806 ## It only ever checks the first reserve result, even though
807 ## multiple reserves for that bib can have the itemnumber set
808 ## the sub is only used once in the codebase.
809 sub GetReserveStatus {
810 my ($itemnumber) = @_;
812 my $dbh = C4::Context->dbh;
814 my ($sth, $found, $priority);
815 if ( $itemnumber ) {
816 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
817 $sth->execute($itemnumber);
818 ($found, $priority) = $sth->fetchrow_array;
821 if(defined $found) {
822 return 'Waiting' if $found eq 'W' and $priority == 0;
823 return 'Finished' if $found eq 'F';
826 return 'Reserved' if $priority > 0;
828 return ''; # empty string here will remove need for checking undef, or less log lines
831 =head2 CheckReserves
833 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
834 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
835 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
837 Find a book in the reserves.
839 C<$itemnumber> is the book's item number.
840 C<$lookahead> is the number of days to look in advance for future reserves.
842 As I understand it, C<&CheckReserves> looks for the given item in the
843 reserves. If it is found, that's a match, and C<$status> is set to
844 C<Waiting>.
846 Otherwise, it finds the most important item in the reserves with the
847 same biblio number as this book (I'm not clear on this) and returns it
848 with C<$status> set to C<Reserved>.
850 C<&CheckReserves> returns a two-element list:
852 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
854 C<$reserve> is the reserve item that matched. It is a
855 reference-to-hash whose keys are mostly the fields of the reserves
856 table in the Koha database.
858 =cut
860 sub CheckReserves {
861 my ( $item, $barcode, $lookahead_days, $ignore_borrowers) = @_;
862 my $dbh = C4::Context->dbh;
863 my $sth;
864 my $select;
865 if (C4::Context->preference('item-level_itypes')){
866 $select = "
867 SELECT items.biblionumber,
868 items.biblioitemnumber,
869 itemtypes.notforloan,
870 items.notforloan AS itemnotforloan,
871 items.itemnumber,
872 items.damaged,
873 items.homebranch,
874 items.holdingbranch
875 FROM items
876 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
877 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
880 else {
881 $select = "
882 SELECT items.biblionumber,
883 items.biblioitemnumber,
884 itemtypes.notforloan,
885 items.notforloan AS itemnotforloan,
886 items.itemnumber,
887 items.damaged,
888 items.homebranch,
889 items.holdingbranch
890 FROM items
891 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
892 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
896 if ($item) {
897 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
898 $sth->execute($item);
900 else {
901 $sth = $dbh->prepare("$select WHERE barcode = ?");
902 $sth->execute($barcode);
904 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
905 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged, $item_homebranch, $item_holdingbranch ) = $sth->fetchrow_array;
907 return if ( $damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') );
909 return unless $itemnumber; # bail if we got nothing.
911 # if item is not for loan it cannot be reserved either.....
912 # except where items.notforloan < 0 : This indicates the item is holdable.
913 return if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
915 # Find this item in the reserves
916 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber, $lookahead_days, $ignore_borrowers);
918 # $priority and $highest are used to find the most important item
919 # in the list returned by &_Findgroupreserve. (The lower $priority,
920 # the more important the item.)
921 # $highest is the most important item we've seen so far.
922 my $highest;
923 if (scalar @reserves) {
924 my $LocalHoldsPriority = C4::Context->preference('LocalHoldsPriority');
925 my $LocalHoldsPriorityPatronControl = C4::Context->preference('LocalHoldsPriorityPatronControl');
926 my $LocalHoldsPriorityItemControl = C4::Context->preference('LocalHoldsPriorityItemControl');
928 my $priority = 10000000;
929 foreach my $res (@reserves) {
930 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
931 return ( "Waiting", $res, \@reserves ); # Found it
932 } else {
933 my $borrowerinfo;
934 my $iteminfo;
935 my $local_hold_match;
937 if ($LocalHoldsPriority) {
938 $borrowerinfo = C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
939 $iteminfo = C4::Items::GetItem($itemnumber);
941 my $local_holds_priority_item_branchcode =
942 $iteminfo->{$LocalHoldsPriorityItemControl};
943 my $local_holds_priority_patron_branchcode =
944 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
945 ? $res->{branchcode}
946 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
947 ? $borrowerinfo->{branchcode}
948 : undef;
949 $local_hold_match =
950 $local_holds_priority_item_branchcode eq
951 $local_holds_priority_patron_branchcode;
954 # See if this item is more important than what we've got so far
955 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
956 $iteminfo ||= C4::Items::GetItem($itemnumber);
957 next if $res->{itemtype} && $res->{itemtype} ne _get_itype( $iteminfo );
958 $borrowerinfo ||= C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
959 my $branch = GetReservesControlBranch( $iteminfo, $borrowerinfo );
960 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
961 next if ($branchitemrule->{'holdallowed'} == 0);
962 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
963 next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $iteminfo->{ $branchitemrule->{hold_fulfillment_policy} }) );
964 $priority = $res->{'priority'};
965 $highest = $res;
966 last if $local_hold_match;
972 # If we get this far, then no exact match was found.
973 # We return the most important (i.e. next) reservation.
974 if ($highest) {
975 $highest->{'itemnumber'} = $item;
976 return ( "Reserved", $highest, \@reserves );
979 return ( '' );
982 =head2 CancelExpiredReserves
984 CancelExpiredReserves();
986 Cancels all reserves with an expiration date from before today.
988 =cut
990 sub CancelExpiredReserves {
992 # Cancel reserves that have passed their expiration date.
993 my $dbh = C4::Context->dbh;
994 my $sth = $dbh->prepare( "
995 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
996 AND expirationdate IS NOT NULL
997 AND found IS NULL
998 " );
999 $sth->execute();
1001 while ( my $res = $sth->fetchrow_hashref() ) {
1002 CancelReserve({ reserve_id => $res->{'reserve_id'} });
1005 # Cancel reserves that have been waiting too long
1006 if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
1007 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
1008 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
1010 my $today = dt_from_string();
1012 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
1013 $sth = $dbh->prepare( $query );
1014 $sth->execute( $max_pickup_delay );
1016 while ( my $res = $sth->fetchrow_hashref ) {
1017 my $do_cancel = 1;
1018 unless ( $cancel_on_holidays ) {
1019 my $calendar = Koha::Calendar->new( branchcode => $res->{'branchcode'} );
1020 my $is_holiday = $calendar->is_holiday( $today );
1022 if ( $is_holiday ) {
1023 $do_cancel = 0;
1027 if ( $do_cancel ) {
1028 CancelReserve({ reserve_id => $res->{'reserve_id'}, charge_cancel_fee => 1 });
1035 =head2 AutoUnsuspendReserves
1037 AutoUnsuspendReserves();
1039 Unsuspends all suspended reserves with a suspend_until date from before today.
1041 =cut
1043 sub AutoUnsuspendReserves {
1044 my $today = dt_from_string();
1046 my @holds = Koha::Holds->search( { suspend_until => { '<' => $today->ymd() } } );
1048 map { $_->suspend(0)->suspend_until(undef)->store() } @holds;
1051 =head2 CancelReserve
1053 CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber, ] [ charge_cancel_fee => 1 ] });
1055 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.
1057 =cut
1059 sub CancelReserve {
1060 my ( $params ) = @_;
1062 my $reserve_id = $params->{'reserve_id'};
1063 # Filter out only the desired keys; this will insert undefined values for elements missing in
1064 # \%params, but GetReserveId filters them out anyway.
1065 $reserve_id = GetReserveId( { biblionumber => $params->{'biblionumber'}, borrowernumber => $params->{'borrowernumber'}, itemnumber => $params->{'itemnumber'} } ) unless ( $reserve_id );
1067 return unless ( $reserve_id );
1069 my $dbh = C4::Context->dbh;
1071 my $reserve = GetReserve( $reserve_id );
1072 if ($reserve) {
1073 my $query = "
1074 UPDATE reserves
1075 SET cancellationdate = now(),
1076 found = Null,
1077 priority = 0
1078 WHERE reserve_id = ?
1080 my $sth = $dbh->prepare($query);
1081 $sth->execute( $reserve_id );
1083 $query = "
1084 INSERT INTO old_reserves
1085 SELECT * FROM reserves
1086 WHERE reserve_id = ?
1088 $sth = $dbh->prepare($query);
1089 $sth->execute( $reserve_id );
1091 $query = "
1092 DELETE FROM reserves
1093 WHERE reserve_id = ?
1095 $sth = $dbh->prepare($query);
1096 $sth->execute( $reserve_id );
1098 # now fix the priority on the others....
1099 _FixPriority({ biblionumber => $reserve->{biblionumber} });
1101 # and, if desired, charge a cancel fee
1102 my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
1103 if ( $charge && $params->{'charge_cancel_fee'} ) {
1104 manualinvoice($reserve->{'borrowernumber'}, $reserve->{'itemnumber'}, 'Hold waiting too long', 'F', $charge);
1108 return $reserve;
1111 =head2 ModReserve
1113 ModReserve({ rank => $rank,
1114 reserve_id => $reserve_id,
1115 branchcode => $branchcode
1116 [, itemnumber => $itemnumber ]
1117 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
1120 Change a hold request's priority or cancel it.
1122 C<$rank> specifies the effect of the change. If C<$rank>
1123 is 'W' or 'n', nothing happens. This corresponds to leaving a
1124 request alone when changing its priority in the holds queue
1125 for a bib.
1127 If C<$rank> is 'del', the hold request is cancelled.
1129 If C<$rank> is an integer greater than zero, the priority of
1130 the request is set to that value. Since priority != 0 means
1131 that the item is not waiting on the hold shelf, setting the
1132 priority to a non-zero value also sets the request's found
1133 status and waiting date to NULL.
1135 The optional C<$itemnumber> parameter is used only when
1136 C<$rank> is a non-zero integer; if supplied, the itemnumber
1137 of the hold request is set accordingly; if omitted, the itemnumber
1138 is cleared.
1140 B<FIXME:> Note that the forgoing can have the effect of causing
1141 item-level hold requests to turn into title-level requests. This
1142 will be fixed once reserves has separate columns for requested
1143 itemnumber and supplying itemnumber.
1145 =cut
1147 sub ModReserve {
1148 my ( $params ) = @_;
1150 my $rank = $params->{'rank'};
1151 my $reserve_id = $params->{'reserve_id'};
1152 my $branchcode = $params->{'branchcode'};
1153 my $itemnumber = $params->{'itemnumber'};
1154 my $suspend_until = $params->{'suspend_until'};
1155 my $borrowernumber = $params->{'borrowernumber'};
1156 my $biblionumber = $params->{'biblionumber'};
1158 return if $rank eq "W";
1159 return if $rank eq "n";
1161 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
1162 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber }) unless ( $reserve_id );
1164 my $dbh = C4::Context->dbh;
1165 if ( $rank eq "del" ) {
1166 CancelReserve({ reserve_id => $reserve_id });
1168 elsif ($rank =~ /^\d+/ and $rank > 0) {
1169 my $hold = Koha::Holds->find($reserve_id);
1171 $hold->set(
1173 priority => $rank,
1174 branchcode => $branchcode,
1175 itemnumber => $itemnumber,
1176 found => undef,
1177 waitingdate => undef
1179 )->store();
1181 if ( defined( $suspend_until ) ) {
1182 if ( $suspend_until ) {
1183 $suspend_until = eval { dt_from_string( $suspend_until ) };
1184 $hold->suspend_hold( $suspend_until );
1185 } else {
1186 # If the hold is suspended leave the hold suspended, but convert it to an indefinite hold.
1187 # If the hold is not suspended, this does nothing.
1188 $hold->set( { suspend_until => undef } )->store();
1192 _FixPriority({ reserve_id => $reserve_id, rank =>$rank });
1196 =head2 ModReserveFill
1198 &ModReserveFill($reserve);
1200 Fill a reserve. If I understand this correctly, this means that the
1201 reserved book has been found and given to the patron who reserved it.
1203 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1204 whose keys are fields from the reserves table in the Koha database.
1206 =cut
1208 sub ModReserveFill {
1209 my ($res) = @_;
1210 my $dbh = C4::Context->dbh;
1211 # fill in a reserve record....
1212 my $reserve_id = $res->{'reserve_id'};
1213 my $biblionumber = $res->{'biblionumber'};
1214 my $borrowernumber = $res->{'borrowernumber'};
1215 my $resdate = $res->{'reservedate'};
1217 # get the priority on this record....
1218 my $priority;
1219 my $query = "SELECT priority
1220 FROM reserves
1221 WHERE biblionumber = ?
1222 AND borrowernumber = ?
1223 AND reservedate = ?";
1224 my $sth = $dbh->prepare($query);
1225 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1226 ($priority) = $sth->fetchrow_array;
1228 # update the database...
1229 $query = "UPDATE reserves
1230 SET found = 'F',
1231 priority = 0
1232 WHERE biblionumber = ?
1233 AND reservedate = ?
1234 AND borrowernumber = ?
1236 $sth = $dbh->prepare($query);
1237 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1239 # move to old_reserves
1240 $query = "INSERT INTO old_reserves
1241 SELECT * FROM reserves
1242 WHERE biblionumber = ?
1243 AND reservedate = ?
1244 AND borrowernumber = ?
1246 $sth = $dbh->prepare($query);
1247 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1248 $query = "DELETE FROM reserves
1249 WHERE biblionumber = ?
1250 AND reservedate = ?
1251 AND borrowernumber = ?
1253 $sth = $dbh->prepare($query);
1254 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1256 # now fix the priority on the others (if the priority wasn't
1257 # already sorted!)....
1258 unless ( $priority == 0 ) {
1259 _FixPriority({ reserve_id => $reserve_id, biblionumber => $biblionumber });
1263 =head2 ModReserveStatus
1265 &ModReserveStatus($itemnumber, $newstatus);
1267 Update the reserve status for the active (priority=0) reserve.
1269 $itemnumber is the itemnumber the reserve is on
1271 $newstatus is the new status.
1273 =cut
1275 sub ModReserveStatus {
1277 #first : check if we have a reservation for this item .
1278 my ($itemnumber, $newstatus) = @_;
1279 my $dbh = C4::Context->dbh;
1281 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1282 my $sth_set = $dbh->prepare($query);
1283 $sth_set->execute( $newstatus, $itemnumber );
1285 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1286 CartToShelf( $itemnumber );
1290 =head2 ModReserveAffect
1292 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1294 This function affect an item and a status for a given reserve
1295 The itemnumber parameter is used to find the biblionumber.
1296 with the biblionumber & the borrowernumber, we can affect the itemnumber
1297 to the correct reserve.
1299 if $transferToDo is not set, then the status is set to "Waiting" as well.
1300 otherwise, a transfer is on the way, and the end of the transfer will
1301 take care of the waiting status
1303 =cut
1305 sub ModReserveAffect {
1306 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1307 my $dbh = C4::Context->dbh;
1309 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1310 # attached to $itemnumber
1311 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1312 $sth->execute($itemnumber);
1313 my ($biblionumber) = $sth->fetchrow;
1315 # get request - need to find out if item is already
1316 # waiting in order to not send duplicate hold filled notifications
1317 my $reserve_id = GetReserveId({
1318 borrowernumber => $borrowernumber,
1319 biblionumber => $biblionumber,
1321 return unless defined $reserve_id;
1322 my $request = GetReserveInfo($reserve_id);
1323 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
1325 # If we affect a reserve that has to be transferred, don't set to Waiting
1326 my $query;
1327 if ($transferToDo) {
1328 $query = "
1329 UPDATE reserves
1330 SET priority = 0,
1331 itemnumber = ?,
1332 found = 'T'
1333 WHERE borrowernumber = ?
1334 AND biblionumber = ?
1337 else {
1338 # affect the reserve to Waiting as well.
1339 $query = "
1340 UPDATE reserves
1341 SET priority = 0,
1342 found = 'W',
1343 waitingdate = NOW(),
1344 itemnumber = ?
1345 WHERE borrowernumber = ?
1346 AND biblionumber = ?
1349 $sth = $dbh->prepare($query);
1350 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1351 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1352 _FixPriority( { biblionumber => $biblionumber } );
1353 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1354 CartToShelf( $itemnumber );
1357 return;
1360 =head2 ModReserveCancelAll
1362 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1364 function to cancel reserv,check other reserves, and transfer document if it's necessary
1366 =cut
1368 sub ModReserveCancelAll {
1369 my $messages;
1370 my $nextreservinfo;
1371 my ( $itemnumber, $borrowernumber ) = @_;
1373 #step 1 : cancel the reservation
1374 my $CancelReserve = CancelReserve({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
1376 #step 2 launch the subroutine of the others reserves
1377 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1379 return ( $messages, $nextreservinfo );
1382 =head2 ModReserveMinusPriority
1384 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1386 Reduce the values of queued list
1388 =cut
1390 sub ModReserveMinusPriority {
1391 my ( $itemnumber, $reserve_id ) = @_;
1393 #first step update the value of the first person on reserv
1394 my $dbh = C4::Context->dbh;
1395 my $query = "
1396 UPDATE reserves
1397 SET priority = 0 , itemnumber = ?
1398 WHERE reserve_id = ?
1400 my $sth_upd = $dbh->prepare($query);
1401 $sth_upd->execute( $itemnumber, $reserve_id );
1402 # second step update all others reserves
1403 _FixPriority({ reserve_id => $reserve_id, rank => '0' });
1406 =head2 GetReserveInfo
1408 &GetReserveInfo($reserve_id);
1410 Get item and borrower details for a current hold.
1411 Current implementation this query should have a single result.
1413 =cut
1415 sub GetReserveInfo {
1416 my ( $reserve_id ) = @_;
1417 my $dbh = C4::Context->dbh;
1418 my $strsth="SELECT
1419 reserve_id,
1420 reservedate,
1421 reservenotes,
1422 reserves.borrowernumber,
1423 reserves.biblionumber,
1424 reserves.branchcode,
1425 reserves.waitingdate,
1426 notificationdate,
1427 reminderdate,
1428 priority,
1429 found,
1430 firstname,
1431 surname,
1432 phone,
1433 email,
1434 address,
1435 address2,
1436 cardnumber,
1437 city,
1438 zipcode,
1439 biblio.title,
1440 biblio.author,
1441 items.holdingbranch,
1442 items.itemcallnumber,
1443 items.itemnumber,
1444 items.location,
1445 barcode,
1446 notes
1447 FROM reserves
1448 LEFT JOIN items USING(itemnumber)
1449 LEFT JOIN borrowers USING(borrowernumber)
1450 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1451 WHERE reserves.reserve_id = ?";
1452 my $sth = $dbh->prepare($strsth);
1453 $sth->execute($reserve_id);
1455 my $data = $sth->fetchrow_hashref;
1456 return $data;
1459 =head2 IsAvailableForItemLevelRequest
1461 my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
1463 Checks whether a given item record is available for an
1464 item-level hold request. An item is available if
1466 * it is not lost AND
1467 * it is not damaged AND
1468 * it is not withdrawn AND
1469 * does not have a not for loan value > 0
1471 Need to check the issuingrules onshelfholds column,
1472 if this is set items on the shelf can be placed on hold
1474 Note that IsAvailableForItemLevelRequest() does not
1475 check if the staff operator is authorized to place
1476 a request on the item - in particular,
1477 this routine does not check IndependentBranches
1478 and canreservefromotherbranches.
1480 =cut
1482 sub IsAvailableForItemLevelRequest {
1483 my $item = shift;
1484 my $borrower = shift;
1486 my $dbh = C4::Context->dbh;
1487 # must check the notforloan setting of the itemtype
1488 # FIXME - a lot of places in the code do this
1489 # or something similar - need to be
1490 # consolidated
1491 my $itype = _get_itype($item);
1492 my $notforloan_per_itemtype
1493 = $dbh->selectrow_array("SELECT notforloan FROM itemtypes WHERE itemtype = ?",
1494 undef, $itype);
1496 return 0 if
1497 $notforloan_per_itemtype ||
1498 $item->{itemlost} ||
1499 $item->{notforloan} > 0 ||
1500 $item->{withdrawn} ||
1501 ($item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems'));
1503 my $on_shelf_holds = _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
1505 if ( $on_shelf_holds == 1 ) {
1506 return 1;
1507 } elsif ( $on_shelf_holds == 2 ) {
1508 my @items =
1509 Koha::Items->search( { biblionumber => $item->{biblionumber} } );
1511 my $any_available = 0;
1513 foreach my $i (@items) {
1514 $any_available = 1
1515 unless $i->itemlost
1516 || $i->{notforloan} > 0
1517 || $i->withdrawn
1518 || $i->onloan
1519 || IsItemOnHoldAndFound( $i->id )
1520 || ( $i->damaged
1521 && !C4::Context->preference('AllowHoldsOnDamagedItems') )
1522 || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan;
1525 return $any_available ? 0 : 1;
1528 return $item->{onloan} || GetReserveStatus($item->{itemnumber}) eq "Waiting";
1531 =head2 OnShelfHoldsAllowed
1533 OnShelfHoldsAllowed($itemtype,$borrowercategory,$branchcode);
1535 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see if onshelf
1536 holds are allowed, returns true if so.
1538 =cut
1540 sub OnShelfHoldsAllowed {
1541 my ($item, $borrower) = @_;
1543 my $itype = _get_itype($item);
1544 return _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
1547 sub _get_itype {
1548 my $item = shift;
1550 my $itype;
1551 if (C4::Context->preference('item-level_itypes')) {
1552 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
1553 # When GetItem is fixed, we can remove this
1554 $itype = $item->{itype};
1556 else {
1557 # XXX This is a bit dodgy. It relies on biblio itemtype column having different name.
1558 # So if we already have a biblioitems join when calling this function,
1559 # we don't need to access the database again
1560 $itype = $item->{itemtype};
1562 unless ($itype) {
1563 my $dbh = C4::Context->dbh;
1564 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
1565 my $sth = $dbh->prepare($query);
1566 $sth->execute($item->{biblioitemnumber});
1567 if (my $data = $sth->fetchrow_hashref()){
1568 $itype = $data->{itemtype};
1571 return $itype;
1574 sub _OnShelfHoldsAllowed {
1575 my ($itype,$borrowercategory,$branchcode) = @_;
1577 my $rule = C4::Circulation::GetIssuingRule($borrowercategory, $itype, $branchcode);
1578 return $rule->{onshelfholds};
1581 =head2 AlterPriority
1583 AlterPriority( $where, $reserve_id );
1585 This function changes a reserve's priority up, down, to the top, or to the bottom.
1586 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1588 =cut
1590 sub AlterPriority {
1591 my ( $where, $reserve_id ) = @_;
1593 my $dbh = C4::Context->dbh;
1595 my $reserve = GetReserve( $reserve_id );
1597 if ( $reserve->{cancellationdate} ) {
1598 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (".$reserve->{cancellationdate}.')';
1599 return;
1602 if ( $where eq 'up' || $where eq 'down' ) {
1604 my $priority = $reserve->{'priority'};
1605 $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
1606 _FixPriority({ reserve_id => $reserve_id, rank => $priority })
1608 } elsif ( $where eq 'top' ) {
1610 _FixPriority({ reserve_id => $reserve_id, rank => '1' })
1612 } elsif ( $where eq 'bottom' ) {
1614 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1619 =head2 ToggleLowestPriority
1621 ToggleLowestPriority( $borrowernumber, $biblionumber );
1623 This function sets the lowestPriority field to true if is false, and false if it is true.
1625 =cut
1627 sub ToggleLowestPriority {
1628 my ( $reserve_id ) = @_;
1630 my $dbh = C4::Context->dbh;
1632 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1633 $sth->execute( $reserve_id );
1635 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1638 =head2 ToggleSuspend
1640 ToggleSuspend( $reserve_id );
1642 This function sets the suspend field to true if is false, and false if it is true.
1643 If the reserve is currently suspended with a suspend_until date, that date will
1644 be cleared when it is unsuspended.
1646 =cut
1648 sub ToggleSuspend {
1649 my ( $reserve_id, $suspend_until ) = @_;
1651 $suspend_until = dt_from_string($suspend_until) if ($suspend_until);
1653 my $hold = Koha::Holds->find( $reserve_id );
1655 if ( $hold->is_suspended ) {
1656 $hold->resume()
1657 } else {
1658 $hold->suspend_hold( $suspend_until );
1662 =head2 SuspendAll
1664 SuspendAll(
1665 borrowernumber => $borrowernumber,
1666 [ biblionumber => $biblionumber, ]
1667 [ suspend_until => $suspend_until, ]
1668 [ suspend => $suspend ]
1671 This function accepts a set of hash keys as its parameters.
1672 It requires either borrowernumber or biblionumber, or both.
1674 suspend_until is wholly optional.
1676 =cut
1678 sub SuspendAll {
1679 my %params = @_;
1681 my $borrowernumber = $params{'borrowernumber'} || undef;
1682 my $biblionumber = $params{'biblionumber'} || undef;
1683 my $suspend_until = $params{'suspend_until'} || undef;
1684 my $suspend = defined( $params{'suspend'} ) ? $params{'suspend'} : 1;
1686 $suspend_until = eval { dt_from_string($suspend_until) }
1687 if ( defined($suspend_until) );
1689 return unless ( $borrowernumber || $biblionumber );
1691 my $params;
1692 $params->{found} = undef;
1693 $params->{borrowernumber} = $borrowernumber if $borrowernumber;
1694 $params->{biblionumber} = $biblionumber if $biblionumber;
1696 my @holds = Koha::Holds->search($params);
1698 if ($suspend) {
1699 map { $_->suspend_hold($suspend_until) } @holds;
1701 else {
1702 map { $_->resume() } @holds;
1707 =head2 _FixPriority
1709 _FixPriority({
1710 reserve_id => $reserve_id,
1711 [rank => $rank,]
1712 [ignoreSetLowestRank => $ignoreSetLowestRank]
1717 _FixPriority({ biblionumber => $biblionumber});
1719 This routine adjusts the priority of a hold request and holds
1720 on the same bib.
1722 In the first form, where a reserve_id is passed, the priority of the
1723 hold is set to supplied rank, and other holds for that bib are adjusted
1724 accordingly. If the rank is "del", the hold is cancelled. If no rank
1725 is supplied, all of the holds on that bib have their priority adjusted
1726 as if the second form had been used.
1728 In the second form, where a biblionumber is passed, the holds on that
1729 bib (that are not captured) are sorted in order of increasing priority,
1730 then have reserves.priority set so that the first non-captured hold
1731 has its priority set to 1, the second non-captured hold has its priority
1732 set to 2, and so forth.
1734 In both cases, holds that have the lowestPriority flag on are have their
1735 priority adjusted to ensure that they remain at the end of the line.
1737 Note that the ignoreSetLowestRank parameter is meant to be used only
1738 when _FixPriority calls itself.
1740 =cut
1742 sub _FixPriority {
1743 my ( $params ) = @_;
1744 my $reserve_id = $params->{reserve_id};
1745 my $rank = $params->{rank} // '';
1746 my $ignoreSetLowestRank = $params->{ignoreSetLowestRank};
1747 my $biblionumber = $params->{biblionumber};
1749 my $dbh = C4::Context->dbh;
1751 unless ( $biblionumber ) {
1752 my $res = GetReserve( $reserve_id );
1753 $biblionumber = $res->{biblionumber};
1756 if ( $rank eq "del" ) {
1757 CancelReserve({ reserve_id => $reserve_id });
1759 elsif ( $rank eq "W" || $rank eq "0" ) {
1761 # make sure priority for waiting or in-transit items is 0
1762 my $query = "
1763 UPDATE reserves
1764 SET priority = 0
1765 WHERE reserve_id = ?
1766 AND found IN ('W', 'T')
1768 my $sth = $dbh->prepare($query);
1769 $sth->execute( $reserve_id );
1771 my @priority;
1773 # get whats left
1774 my $query = "
1775 SELECT reserve_id, borrowernumber, reservedate
1776 FROM reserves
1777 WHERE biblionumber = ?
1778 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1779 ORDER BY priority ASC
1781 my $sth = $dbh->prepare($query);
1782 $sth->execute( $biblionumber );
1783 while ( my $line = $sth->fetchrow_hashref ) {
1784 push( @priority, $line );
1787 # To find the matching index
1788 my $i;
1789 my $key = -1; # to allow for 0 to be a valid result
1790 for ( $i = 0 ; $i < @priority ; $i++ ) {
1791 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1792 $key = $i; # save the index
1793 last;
1797 # if index exists in array then move it to new position
1798 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1799 my $new_rank = $rank -
1800 1; # $new_rank is what you want the new index to be in the array
1801 my $moving_item = splice( @priority, $key, 1 );
1802 splice( @priority, $new_rank, 0, $moving_item );
1805 # now fix the priority on those that are left....
1806 $query = "
1807 UPDATE reserves
1808 SET priority = ?
1809 WHERE reserve_id = ?
1811 $sth = $dbh->prepare($query);
1812 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1813 $sth->execute(
1814 $j + 1,
1815 $priority[$j]->{'reserve_id'}
1819 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1820 $sth->execute();
1822 unless ( $ignoreSetLowestRank ) {
1823 while ( my $res = $sth->fetchrow_hashref() ) {
1824 _FixPriority({
1825 reserve_id => $res->{'reserve_id'},
1826 rank => '999999',
1827 ignoreSetLowestRank => 1
1833 =head2 _Findgroupreserve
1835 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead, $ignore_borrowers);
1837 Looks for a holds-queue based item-specific match first, then for a holds-queue title-level match, returning the
1838 first match found. If neither, then we look for non-holds-queue based holds.
1839 Lookahead is the number of days to look in advance.
1841 C<&_Findgroupreserve> returns :
1842 C<@results> is an array of references-to-hash whose keys are mostly
1843 fields from the reserves table of the Koha database, plus
1844 C<biblioitemnumber>.
1846 =cut
1848 sub _Findgroupreserve {
1849 my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_;
1850 my $dbh = C4::Context->dbh;
1852 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1853 # check for exact targeted match
1854 my $item_level_target_query = qq{
1855 SELECT reserves.biblionumber AS biblionumber,
1856 reserves.borrowernumber AS borrowernumber,
1857 reserves.reservedate AS reservedate,
1858 reserves.branchcode AS branchcode,
1859 reserves.cancellationdate AS cancellationdate,
1860 reserves.found AS found,
1861 reserves.reservenotes AS reservenotes,
1862 reserves.priority AS priority,
1863 reserves.timestamp AS timestamp,
1864 biblioitems.biblioitemnumber AS biblioitemnumber,
1865 reserves.itemnumber AS itemnumber,
1866 reserves.reserve_id AS reserve_id,
1867 reserves.itemtype AS itemtype
1868 FROM reserves
1869 JOIN biblioitems USING (biblionumber)
1870 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1871 WHERE found IS NULL
1872 AND priority > 0
1873 AND item_level_request = 1
1874 AND itemnumber = ?
1875 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1876 AND suspend = 0
1877 ORDER BY priority
1879 my $sth = $dbh->prepare($item_level_target_query);
1880 $sth->execute($itemnumber, $lookahead||0);
1881 my @results;
1882 if ( my $data = $sth->fetchrow_hashref ) {
1883 push( @results, $data )
1884 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1886 return @results if @results;
1888 # check for title-level targeted match
1889 my $title_level_target_query = qq{
1890 SELECT reserves.biblionumber AS biblionumber,
1891 reserves.borrowernumber AS borrowernumber,
1892 reserves.reservedate AS reservedate,
1893 reserves.branchcode AS branchcode,
1894 reserves.cancellationdate AS cancellationdate,
1895 reserves.found AS found,
1896 reserves.reservenotes AS reservenotes,
1897 reserves.priority AS priority,
1898 reserves.timestamp AS timestamp,
1899 biblioitems.biblioitemnumber AS biblioitemnumber,
1900 reserves.itemnumber AS itemnumber,
1901 reserves.reserve_id AS reserve_id,
1902 reserves.itemtype AS itemtype
1903 FROM reserves
1904 JOIN biblioitems USING (biblionumber)
1905 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1906 WHERE found IS NULL
1907 AND priority > 0
1908 AND item_level_request = 0
1909 AND hold_fill_targets.itemnumber = ?
1910 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1911 AND suspend = 0
1912 ORDER BY priority
1914 $sth = $dbh->prepare($title_level_target_query);
1915 $sth->execute($itemnumber, $lookahead||0);
1916 @results = ();
1917 if ( my $data = $sth->fetchrow_hashref ) {
1918 push( @results, $data )
1919 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1921 return @results if @results;
1923 my $query = qq{
1924 SELECT reserves.biblionumber AS biblionumber,
1925 reserves.borrowernumber AS borrowernumber,
1926 reserves.reservedate AS reservedate,
1927 reserves.waitingdate AS waitingdate,
1928 reserves.branchcode AS branchcode,
1929 reserves.cancellationdate AS cancellationdate,
1930 reserves.found AS found,
1931 reserves.reservenotes AS reservenotes,
1932 reserves.priority AS priority,
1933 reserves.timestamp AS timestamp,
1934 reserves.itemnumber AS itemnumber,
1935 reserves.reserve_id AS reserve_id,
1936 reserves.itemtype AS itemtype
1937 FROM reserves
1938 WHERE reserves.biblionumber = ?
1939 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1940 AND reserves.reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1941 AND suspend = 0
1942 ORDER BY priority
1944 $sth = $dbh->prepare($query);
1945 $sth->execute( $biblio, $itemnumber, $lookahead||0);
1946 @results = ();
1947 while ( my $data = $sth->fetchrow_hashref ) {
1948 push( @results, $data )
1949 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1951 return @results;
1954 =head2 _koha_notify_reserve
1956 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1958 Sends a notification to the patron that their hold has been filled (through
1959 ModReserveAffect, _not_ ModReserveFill)
1961 The letter code for this notice may be found using the following query:
1963 select distinct letter_code
1964 from message_transports
1965 inner join message_attributes using (message_attribute_id)
1966 where message_name = 'Hold_Filled'
1968 This will probably sipmly be 'HOLD', but because it is defined in the database,
1969 it is subject to addition or change.
1971 The following tables are availalbe witin the notice:
1973 branches
1974 borrowers
1975 biblio
1976 biblioitems
1977 reserves
1978 items
1980 =cut
1982 sub _koha_notify_reserve {
1983 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1985 my $dbh = C4::Context->dbh;
1986 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
1988 # Try to get the borrower's email address
1989 my $to_address = C4::Members::GetNoticeEmailAddress($borrowernumber);
1991 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( {
1992 borrowernumber => $borrowernumber,
1993 message_name => 'Hold_Filled'
1994 } );
1996 my $sth = $dbh->prepare("
1997 SELECT *
1998 FROM reserves
1999 WHERE borrowernumber = ?
2000 AND biblionumber = ?
2002 $sth->execute( $borrowernumber, $biblionumber );
2003 my $reserve = $sth->fetchrow_hashref;
2004 my $library = Koha::Libraries->find( $reserve->{branchcode} )->unblessed;
2006 my $admin_email_address = $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress');
2008 my %letter_params = (
2009 module => 'reserves',
2010 branchcode => $reserve->{branchcode},
2011 tables => {
2012 'branches' => $library,
2013 'borrowers' => $borrower,
2014 'biblio' => $biblionumber,
2015 'biblioitems' => $biblionumber,
2016 'reserves' => $reserve,
2017 'items', $reserve->{'itemnumber'},
2019 substitute => { today => output_pref( { dt => dt_from_string, dateonly => 1 } ) },
2022 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.
2023 my $send_notification = sub {
2024 my ( $mtt, $letter_code ) = (@_);
2025 return unless defined $letter_code;
2026 $letter_params{letter_code} = $letter_code;
2027 $letter_params{message_transport_type} = $mtt;
2028 my $letter = C4::Letters::GetPreparedLetter ( %letter_params );
2029 unless ($letter) {
2030 warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
2031 return;
2034 C4::Letters::EnqueueLetter( {
2035 letter => $letter,
2036 borrowernumber => $borrowernumber,
2037 from_address => $admin_email_address,
2038 message_transport_type => $mtt,
2039 } );
2042 while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) {
2043 next if (
2044 ( $mtt eq 'email' and not $to_address ) # No email address
2045 or ( $mtt eq 'sms' and not $borrower->{smsalertnumber} ) # No SMS number
2046 or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
2049 &$send_notification($mtt, $letter_code);
2050 $notification_sent++;
2052 #Making sure that a print notification is sent if no other transport types can be utilized.
2053 if (! $notification_sent) {
2054 &$send_notification('print', 'HOLD');
2059 =head2 _ShiftPriorityByDateAndPriority
2061 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
2063 This increments the priority of all reserves after the one
2064 with either the lowest date after C<$reservedate>
2065 or the lowest priority after C<$priority>.
2067 It effectively makes room for a new reserve to be inserted with a certain
2068 priority, which is returned.
2070 This is most useful when the reservedate can be set by the user. It allows
2071 the new reserve to be placed before other reserves that have a later
2072 reservedate. Since priority also is set by the form in reserves/request.pl
2073 the sub accounts for that too.
2075 =cut
2077 sub _ShiftPriorityByDateAndPriority {
2078 my ( $biblio, $resdate, $new_priority ) = @_;
2080 my $dbh = C4::Context->dbh;
2081 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
2082 my $sth = $dbh->prepare( $query );
2083 $sth->execute( $biblio, $resdate, $new_priority );
2084 my $min_priority = $sth->fetchrow;
2085 # if no such matches are found, $new_priority remains as original value
2086 $new_priority = $min_priority if ( $min_priority );
2088 # Shift the priority up by one; works in conjunction with the next SQL statement
2089 $query = "UPDATE reserves
2090 SET priority = priority+1
2091 WHERE biblionumber = ?
2092 AND borrowernumber = ?
2093 AND reservedate = ?
2094 AND found IS NULL";
2095 my $sth_update = $dbh->prepare( $query );
2097 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
2098 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
2099 $sth = $dbh->prepare( $query );
2100 $sth->execute( $new_priority, $biblio );
2101 while ( my $row = $sth->fetchrow_hashref ) {
2102 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
2105 return $new_priority; # so the caller knows what priority they wind up receiving
2108 =head2 OPACItemHoldsAllowed
2110 OPACItemHoldsAllowed($item_record,$borrower_record);
2112 Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see
2113 if specific item holds are allowed, returns true if so.
2115 =cut
2117 sub OPACItemHoldsAllowed {
2118 my ($item,$borrower) = @_;
2120 my $branchcode = $item->{homebranch} or die "No homebranch";
2121 my $itype;
2122 my $dbh = C4::Context->dbh;
2123 if (C4::Context->preference('item-level_itypes')) {
2124 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
2125 # When GetItem is fixed, we can remove this
2126 $itype = $item->{itype};
2128 else {
2129 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
2130 my $sth = $dbh->prepare($query);
2131 $sth->execute($item->{biblioitemnumber});
2132 if (my $data = $sth->fetchrow_hashref()){
2133 $itype = $data->{itemtype};
2137 my $query = "SELECT opacitemholds,categorycode,itemtype,branchcode FROM issuingrules WHERE
2138 (issuingrules.categorycode = ? OR issuingrules.categorycode = '*')
2140 (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2142 (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')
2143 ORDER BY
2144 issuingrules.categorycode desc,
2145 issuingrules.itemtype desc,
2146 issuingrules.branchcode desc
2147 LIMIT 1";
2148 my $sth = $dbh->prepare($query);
2149 $sth->execute($borrower->{categorycode},$itype,$branchcode);
2150 my $data = $sth->fetchrow_hashref;
2151 my $opacitemholds = uc substr ($data->{opacitemholds}, 0, 1);
2152 return '' if $opacitemholds eq 'N';
2153 return $opacitemholds;
2156 =head2 MoveReserve
2158 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
2160 Use when checking out an item to handle reserves
2161 If $cancelreserve boolean is set to true, it will remove existing reserve
2163 =cut
2165 sub MoveReserve {
2166 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
2168 my $lookahead = C4::Context->preference('ConfirmFutureHolds'); #number of days to look for future holds
2169 my ( $restype, $res, $all_reserves ) = CheckReserves( $itemnumber, undef, $lookahead );
2170 return unless $res;
2172 my $biblionumber = $res->{biblionumber};
2173 my $biblioitemnumber = $res->{biblioitemnumber};
2175 if ($res->{borrowernumber} == $borrowernumber) {
2176 ModReserveFill($res);
2178 else {
2179 # warn "Reserved";
2180 # The item is reserved by someone else.
2181 # Find this item in the reserves
2183 my $borr_res;
2184 foreach (@$all_reserves) {
2185 $_->{'borrowernumber'} == $borrowernumber or next;
2186 $_->{'biblionumber'} == $biblionumber or next;
2188 $borr_res = $_;
2189 last;
2192 if ( $borr_res ) {
2193 # The item is reserved by the current patron
2194 ModReserveFill($borr_res);
2197 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
2198 RevertWaitingStatus({ itemnumber => $itemnumber });
2200 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
2201 CancelReserve( { reserve_id => $res->{'reserve_id'} } );
2206 =head2 MergeHolds
2208 MergeHolds($dbh,$to_biblio, $from_biblio);
2210 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
2212 =cut
2214 sub MergeHolds {
2215 my ( $dbh, $to_biblio, $from_biblio ) = @_;
2216 my $sth = $dbh->prepare(
2217 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
2219 $sth->execute($from_biblio);
2220 if ( my $data = $sth->fetchrow_hashref() ) {
2222 # holds exist on old record, if not we don't need to do anything
2223 $sth = $dbh->prepare(
2224 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
2225 $sth->execute( $to_biblio, $from_biblio );
2227 # Reorder by date
2228 # don't reorder those already waiting
2230 $sth = $dbh->prepare(
2231 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
2233 my $upd_sth = $dbh->prepare(
2234 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
2235 AND reservedate = ? AND (itemnumber = ? or itemnumber is NULL) "
2237 $sth->execute( $to_biblio, 'W', 'T' );
2238 my $priority = 1;
2239 while ( my $reserve = $sth->fetchrow_hashref() ) {
2240 $upd_sth->execute(
2241 $priority, $to_biblio,
2242 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
2243 $reserve->{'itemnumber'}
2245 $priority++;
2250 =head2 RevertWaitingStatus
2252 RevertWaitingStatus({ itemnumber => $itemnumber });
2254 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
2256 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
2257 item level hold, even if it was only a bibliolevel hold to
2258 begin with. This is because we can no longer know if a hold
2259 was item-level or bib-level after a hold has been set to
2260 waiting status.
2262 =cut
2264 sub RevertWaitingStatus {
2265 my ( $params ) = @_;
2266 my $itemnumber = $params->{'itemnumber'};
2268 return unless ( $itemnumber );
2270 my $dbh = C4::Context->dbh;
2272 ## Get the waiting reserve we want to revert
2273 my $query = "
2274 SELECT * FROM reserves
2275 WHERE itemnumber = ?
2276 AND found IS NOT NULL
2278 my $sth = $dbh->prepare( $query );
2279 $sth->execute( $itemnumber );
2280 my $reserve = $sth->fetchrow_hashref();
2282 ## Increment the priority of all other non-waiting
2283 ## reserves for this bib record
2284 $query = "
2285 UPDATE reserves
2287 priority = priority + 1
2288 WHERE
2289 biblionumber = ?
2291 priority > 0
2293 $sth = $dbh->prepare( $query );
2294 $sth->execute( $reserve->{'biblionumber'} );
2296 ## Fix up the currently waiting reserve
2297 $query = "
2298 UPDATE reserves
2300 priority = 1,
2301 found = NULL,
2302 waitingdate = NULL
2303 WHERE
2304 reserve_id = ?
2306 $sth = $dbh->prepare( $query );
2307 $sth->execute( $reserve->{'reserve_id'} );
2308 _FixPriority( { biblionumber => $reserve->{biblionumber} } );
2311 =head2 GetReserveId
2313 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
2315 Returnes the first reserve id that matches the given criteria
2317 =cut
2319 sub GetReserveId {
2320 my ( $params ) = @_;
2322 return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
2324 my $dbh = C4::Context->dbh();
2326 my $sql = "SELECT reserve_id FROM reserves WHERE ";
2328 my @params;
2329 my @limits;
2330 foreach my $key ( keys %$params ) {
2331 if ( defined( $params->{$key} ) ) {
2332 push( @limits, "$key = ?" );
2333 push( @params, $params->{$key} );
2337 $sql .= join( " AND ", @limits );
2339 my $sth = $dbh->prepare( $sql );
2340 $sth->execute( @params );
2341 my $row = $sth->fetchrow_hashref();
2343 return $row->{'reserve_id'};
2346 =head2 ReserveSlip
2348 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
2350 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2352 The letter code will be HOLD_SLIP, and the following tables are
2353 available within the slip:
2355 reserves
2356 branches
2357 borrowers
2358 biblio
2359 biblioitems
2360 items
2362 =cut
2364 sub ReserveSlip {
2365 my ($branch, $borrowernumber, $biblionumber) = @_;
2367 # return unless ( C4::Context->boolean_preference('printreserveslips') );
2369 my $reserve_id = GetReserveId({
2370 biblionumber => $biblionumber,
2371 borrowernumber => $borrowernumber
2372 }) or return;
2373 my $reserve = GetReserveInfo($reserve_id) or return;
2375 return C4::Letters::GetPreparedLetter (
2376 module => 'circulation',
2377 letter_code => 'HOLD_SLIP',
2378 branchcode => $branch,
2379 tables => {
2380 'reserves' => $reserve,
2381 'branches' => $reserve->{branchcode},
2382 'borrowers' => $reserve->{borrowernumber},
2383 'biblio' => $reserve->{biblionumber},
2384 'biblioitems' => $reserve->{biblionumber},
2385 'items' => $reserve->{itemnumber},
2390 =head2 GetReservesControlBranch
2392 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2394 Return the branchcode to be used to determine which reserves
2395 policy applies to a transaction.
2397 C<$item> is a hashref for an item. Only 'homebranch' is used.
2399 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2401 =cut
2403 sub GetReservesControlBranch {
2404 my ( $item, $borrower ) = @_;
2406 my $reserves_control = C4::Context->preference('ReservesControlBranch');
2408 my $branchcode =
2409 ( $reserves_control eq 'ItemHomeLibrary' ) ? $item->{'homebranch'}
2410 : ( $reserves_control eq 'PatronLibrary' ) ? $borrower->{'branchcode'}
2411 : undef;
2413 return $branchcode;
2416 =head2 CalculatePriority
2418 my $p = CalculatePriority($biblionumber, $resdate);
2420 Calculate priority for a new reserve on biblionumber, placing it at
2421 the end of the line of all holds whose start date falls before
2422 the current system time and that are neither on the hold shelf
2423 or in transit.
2425 The reserve date parameter is optional; if it is supplied, the
2426 priority is based on the set of holds whose start date falls before
2427 the parameter value.
2429 After calculation of this priority, it is recommended to call
2430 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2431 AddReserves.
2433 =cut
2435 sub CalculatePriority {
2436 my ( $biblionumber, $resdate ) = @_;
2438 my $sql = q{
2439 SELECT COUNT(*) FROM reserves
2440 WHERE biblionumber = ?
2441 AND priority > 0
2442 AND (found IS NULL OR found = '')
2444 #skip found==W or found==T (waiting or transit holds)
2445 if( $resdate ) {
2446 $sql.= ' AND ( reservedate <= ? )';
2448 else {
2449 $sql.= ' AND ( reservedate < NOW() )';
2451 my $dbh = C4::Context->dbh();
2452 my @row = $dbh->selectrow_array(
2453 $sql,
2454 undef,
2455 $resdate ? ($biblionumber, $resdate) : ($biblionumber)
2458 return @row ? $row[0]+1 : 1;
2461 =head2 IsItemOnHoldAndFound
2463 my $bool = IsItemFoundHold( $itemnumber );
2465 Returns true if the item is currently on hold
2466 and that hold has a non-null found status ( W, T, etc. )
2468 =cut
2470 sub IsItemOnHoldAndFound {
2471 my ($itemnumber) = @_;
2473 my $rs = Koha::Database->new()->schema()->resultset('Reserve');
2475 my $found = $rs->count(
2477 itemnumber => $itemnumber,
2478 found => { '!=' => undef }
2482 return $found;
2485 =head1 AUTHOR
2487 Koha Development Team <http://koha-community.org/>
2489 =cut