Bug 24688: Check the priority of the fulfilled hold when fixing priorities
[koha.git] / C4 / Reserves.pm
1 package C4::Reserves;
2
3 # Copyright 2000-2002 Katipo Communications
4 #           2006 SAN Ouest Provence
5 #           2007-2010 BibLibre Paul POULAIN
6 #           2011 Catalyst IT
7 #
8 # This file is part of Koha.
9 #
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.
14 #
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.
19 #
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>.
22
23
24 use Modern::Perl;
25
26 use C4::Accounts;
27 use C4::Biblio;
28 use C4::Circulation;
29 use C4::Context;
30 use C4::Items;
31 use C4::Letters;
32 use C4::Log;
33 use C4::Members::Messaging;
34 use C4::Members;
35 use Koha::Account::Lines;
36 use Koha::Biblios;
37 use Koha::Calendar;
38 use Koha::CirculationRules;
39 use Koha::Database;
40 use Koha::DateUtils;
41 use Koha::Hold;
42 use Koha::Holds;
43 use Koha::ItemTypes;
44 use Koha::Items;
45 use Koha::Libraries;
46 use Koha::Old::Hold;
47 use Koha::Patrons;
48
49 use Carp;
50 use Data::Dumper;
51 use List::MoreUtils qw( firstidx any );
52
53 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
54
55 =head1 NAME
56
57 C4::Reserves - Koha functions for dealing with reservation.
58
59 =head1 SYNOPSIS
60
61   use C4::Reserves;
62
63 =head1 DESCRIPTION
64
65 This modules provides somes functions to deal with reservations.
66
67   Reserves are stored in reserves table.
68   The following columns contains important values :
69   - priority >0      : then the reserve is at 1st stage, and not yet affected to any item.
70              =0      : then the reserve is being dealed
71   - found : NULL       : means the patron requested the 1st available, and we haven't chosen the item
72             T(ransit)  : the reserve is linked to an item but is in transit to the pickup branch
73             W(aiting)  : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
74             F(inished) : the reserve has been completed, and is done
75   - itemnumber : empty : the reserve is still unaffected to an item
76                  filled: the reserve is attached to an item
77   The complete workflow is :
78   ==== 1st use case ====
79   patron request a document, 1st available :                      P >0, F=NULL, I=NULL
80   a library having it run "transfertodo", and clic on the list
81          if there is no transfer to do, the reserve waiting
82          patron can pick it up                                    P =0, F=W,    I=filled
83          if there is a transfer to do, write in branchtransfer    P =0, F=T,    I=filled
84            The pickup library receive the book, it check in       P =0, F=W,    I=filled
85   The patron borrow the book                                      P =0, F=F,    I=filled
86
87   ==== 2nd use case ====
88   patron requests a document, a given item,
89     If pickup is holding branch                                   P =0, F=W,   I=filled
90     If transfer needed, write in branchtransfer                   P =0, F=T,    I=filled
91         The pickup library receive the book, it checks it in      P =0, F=W,    I=filled
92   The patron borrow the book                                      P =0, F=F,    I=filled
93
94 =head1 FUNCTIONS
95
96 =cut
97
98 BEGIN {
99     require Exporter;
100     @ISA = qw(Exporter);
101     @EXPORT = qw(
102         &AddReserve
103
104         &GetReserveStatus
105
106         &GetOtherReserves
107
108         &ModReserveFill
109         &ModReserveAffect
110         &ModReserve
111         &ModReserveStatus
112         &ModReserveCancelAll
113         &ModReserveMinusPriority
114         &MoveReserve
115
116         &CheckReserves
117         &CanBookBeReserved
118         &CanItemBeReserved
119         &CanReserveBeCanceledFromOpac
120         &CancelExpiredReserves
121
122         &AutoUnsuspendReserves
123
124         &IsAvailableForItemLevelRequest
125
126         &AlterPriority
127         &ToggleLowestPriority
128
129         &ReserveSlip
130         &ToggleSuspend
131         &SuspendAll
132
133         &GetReservesControlBranch
134
135         IsItemOnHoldAndFound
136
137         GetMaxPatronHoldsForRecord
138     );
139     @EXPORT_OK = qw( MergeHolds );
140 }
141
142 =head2 AddReserve
143
144     AddReserve(
145         {
146             branch           => $branchcode,
147             borrowernumber   => $borrowernumber,
148             biblionumber     => $biblionumber,
149             priority         => $priority,
150             reservation_date => $reservation_date,
151             expiration_date  => $expiration_date,
152             notes            => $notes,
153             title            => $title,
154             itemnumber       => $itemnumber,
155             found            => $found,
156             itemtype         => $itemtype,
157         }
158     );
159
160 Adds reserve and generates HOLDPLACED message.
161
162 The following tables are available witin the HOLDPLACED message:
163
164     branches
165     borrowers
166     biblio
167     biblioitems
168     items
169     reserves
170
171 =cut
172
173 sub AddReserve {
174     my ($params)       = @_;
175     my $branch         = $params->{branchcode};
176     my $borrowernumber = $params->{borrowernumber};
177     my $biblionumber   = $params->{biblionumber};
178     my $priority       = $params->{priority};
179     my $resdate        = $params->{reservation_date};
180     my $expdate        = $params->{expiration_date};
181     my $notes          = $params->{notes};
182     my $title          = $params->{title};
183     my $checkitem      = $params->{itemnumber};
184     my $found          = $params->{found};
185     my $itemtype       = $params->{itemtype};
186
187     $resdate = output_pref( { str => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' })
188         or output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
189
190     $expdate = output_pref({ str => $expdate, dateonly => 1, dateformat => 'iso' });
191
192     # if we have an item selectionned, and the pickup branch is the same as the holdingbranch
193     # of the document, we force the value $priority and $found .
194     if ( $checkitem and not C4::Context->preference('ReservesNeedReturns') ) {
195         my $item = Koha::Items->find( $checkitem ); # FIXME Prevent bad calls
196
197         if (
198             # If item is already checked out, it cannot be set waiting
199             !$item->onloan
200
201             # The item can't be waiting if it needs a transfer
202             && $item->holdingbranch eq $branch
203
204             # Similarly, if in transit it can't be waiting
205             && !$item->get_transfer
206
207             # If we can't hold damaged items, and it is damaged, it can't be waiting
208             && ( $item->damaged && C4::Context->preference('AllowHoldsOnDamagedItems') || !$item->damaged )
209
210             # Lastly, if this already has holds, we shouldn't make it waiting for the new hold
211             && !$item->current_holds->count )
212         {
213             $priority = 0;
214             $found = 'W';
215         }
216     }
217
218     if ( C4::Context->preference('AllowHoldDateInFuture') ) {
219
220         # Make room in reserves for this before those of a later reserve date
221         $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
222     }
223
224     my $waitingdate;
225
226     # If the reserv had the waiting status, we had the value of the resdate
227     if ( $found && $found eq 'W' ) {
228         $waitingdate = $resdate;
229     }
230
231     # Don't add itemtype limit if specific item is selected
232     $itemtype = undef if $checkitem;
233
234     # updates take place here
235     my $hold = Koha::Hold->new(
236         {
237             borrowernumber => $borrowernumber,
238             biblionumber   => $biblionumber,
239             reservedate    => $resdate,
240             branchcode     => $branch,
241             priority       => $priority,
242             reservenotes   => $notes,
243             itemnumber     => $checkitem,
244             found          => $found,
245             waitingdate    => $waitingdate,
246             expirationdate => $expdate,
247             itemtype       => $itemtype,
248             item_level_hold => $checkitem ? 1 : 0,
249         }
250     )->store();
251     $hold->set_waiting() if $found && $found eq 'W';
252
253     logaction( 'HOLDS', 'CREATE', $hold->id, Dumper($hold->unblessed) )
254         if C4::Context->preference('HoldsLog');
255
256     my $reserve_id = $hold->id();
257
258     # add a reserve fee if needed
259     if ( C4::Context->preference('HoldFeeMode') ne 'any_time_is_collected' ) {
260         my $reserve_fee = GetReserveFee( $borrowernumber, $biblionumber );
261         ChargeReserveFee( $borrowernumber, $reserve_fee, $title );
262     }
263
264     _FixPriority({ biblionumber => $biblionumber});
265
266     # Send e-mail to librarian if syspref is active
267     if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
268         my $patron = Koha::Patrons->find( $borrowernumber );
269         my $library = $patron->library;
270         if ( my $letter =  C4::Letters::GetPreparedLetter (
271             module => 'reserves',
272             letter_code => 'HOLDPLACED',
273             branchcode => $branch,
274             lang => $patron->lang,
275             tables => {
276                 'branches'    => $library->unblessed,
277                 'borrowers'   => $patron->unblessed,
278                 'biblio'      => $biblionumber,
279                 'biblioitems' => $biblionumber,
280                 'items'       => $checkitem,
281                 'reserves'    => $hold->unblessed,
282             },
283         ) ) {
284
285             my $branch_email_address = $library->inbound_email_address;
286
287             C4::Letters::EnqueueLetter(
288                 {
289                     letter                 => $letter,
290                     borrowernumber         => $borrowernumber,
291                     message_transport_type => 'email',
292                     to_address             => $branch_email_address,
293                 }
294             );
295         }
296     }
297
298     return $reserve_id;
299 }
300
301 =head2 CanBookBeReserved
302
303   $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber, $branchcode)
304   if ($canReserve eq 'OK') { #We can reserve this Item! }
305
306 See CanItemBeReserved() for possible return values.
307
308 =cut
309
310 sub CanBookBeReserved{
311     my ($borrowernumber, $biblionumber, $pickup_branchcode) = @_;
312
313     my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber})->get_column("itemnumber");
314     #get items linked via host records
315     my @hostitems = get_hostitemnumbers_of($biblionumber);
316     if (@hostitems){
317         push (@itemnumbers, @hostitems);
318     }
319
320     my $canReserve = { status => '' };
321     foreach my $itemnumber (@itemnumbers) {
322         $canReserve = CanItemBeReserved( $borrowernumber, $itemnumber, $pickup_branchcode );
323         return { status => 'OK' } if $canReserve->{status} eq 'OK';
324     }
325     return $canReserve;
326 }
327
328 =head2 CanItemBeReserved
329
330   $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber, $branchcode)
331   if ($canReserve->{status} eq 'OK') { #We can reserve this Item! }
332
333 @RETURNS { status => OK },              if the Item can be reserved.
334          { status => ageRestricted },   if the Item is age restricted for this borrower.
335          { status => damaged },         if the Item is damaged.
336          { status => cannotReserveFromOtherBranches }, if syspref 'canreservefromotherbranches' is OK.
337          { status => branchNotInHoldGroup }, if borrower home library is not in hold group, and holds are only allowed from hold groups.
338          { status => tooManyReserves, limit => $limit }, if the borrower has exceeded their maximum reserve amount.
339          { status => notReservable },   if holds on this item are not allowed
340          { status => libraryNotFound },   if given branchcode is not an existing library
341          { status => libraryNotPickupLocation },   if given branchcode is not configured to be a pickup location
342          { status => cannotBeTransferred }, if branch transfer limit applies on given item and branchcode
343          { status => pickupNotInHoldGroup }, pickup location is not in hold group, and pickup locations are only allowed from hold groups.
344
345 =cut
346
347 sub CanItemBeReserved {
348     my ( $borrowernumber, $itemnumber, $pickup_branchcode ) = @_;
349
350     my $dbh = C4::Context->dbh;
351     my $ruleitemtype;    # itemtype of the matching issuing rule
352     my $allowedreserves  = 0; # Total number of holds allowed across all records
353     my $holds_per_record = 1; # Total number of holds allowed for this one given record
354     my $holds_per_day;        # Default to unlimited
355
356     # we retrieve borrowers and items informations #
357     # item->{itype} will come for biblioitems if necessery
358     my $item       = Koha::Items->find($itemnumber);
359     my $biblio     = $item->biblio;
360     my $patron = Koha::Patrons->find( $borrowernumber );
361     my $borrower = $patron->unblessed;
362
363     # If an item is damaged and we don't allow holds on damaged items, we can stop right here
364     return { status =>'damaged' }
365       if ( $item->damaged
366         && !C4::Context->preference('AllowHoldsOnDamagedItems') );
367
368     # Check for the age restriction
369     my ( $ageRestriction, $daysToAgeRestriction ) =
370       C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower );
371     return { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0;
372
373     # Check that the patron doesn't have an item level hold on this item already
374     return { status =>'itemAlreadyOnHold' }
375       if Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->count();
376
377     my $controlbranch = C4::Context->preference('ReservesControlBranch');
378
379     my $querycount = q{
380         SELECT count(*) AS count
381           FROM reserves
382      LEFT JOIN items USING (itemnumber)
383      LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
384      LEFT JOIN borrowers USING (borrowernumber)
385          WHERE borrowernumber = ?
386     };
387
388     my $branchcode  = "";
389     my $branchfield = "reserves.branchcode";
390
391     if ( $controlbranch eq "ItemHomeLibrary" ) {
392         $branchfield = "items.homebranch";
393         $branchcode  = $item->homebranch;
394     }
395     elsif ( $controlbranch eq "PatronLibrary" ) {
396         $branchfield = "borrowers.branchcode";
397         $branchcode  = $borrower->{branchcode};
398     }
399
400     # we retrieve rights
401     if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->effective_itemtype, $branchcode ) ) {
402         $ruleitemtype     = $rights->{itemtype};
403         $allowedreserves  = $rights->{reservesallowed} // $allowedreserves;
404         $holds_per_record = $rights->{holds_per_record} // $holds_per_record;
405         $holds_per_day    = $rights->{holds_per_day};
406     }
407     else {
408         $ruleitemtype = undef;
409     }
410
411     my $holds = Koha::Holds->search(
412         {
413             borrowernumber => $borrowernumber,
414             biblionumber   => $item->biblionumber,
415         }
416     );
417     if (   defined $holds_per_record && $holds_per_record ne ''
418         && $holds->count() >= $holds_per_record ) {
419         return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record };
420     }
421
422     my $today_holds = Koha::Holds->search({
423         borrowernumber => $borrowernumber,
424         reservedate    => dt_from_string->date
425     });
426
427     if (   defined $holds_per_day && $holds_per_day ne ''
428         && $today_holds->count() >= $holds_per_day )
429     {
430         return { status => 'tooManyReservesToday', limit => $holds_per_day };
431     }
432
433     # we retrieve count
434
435     $querycount .= "AND ( $branchfield = ? OR $branchfield IS NULL )";
436
437     # If using item-level itypes, fall back to the record
438     # level itemtype if the hold has no associated item
439     $querycount .=
440       C4::Context->preference('item-level_itypes')
441       ? " AND COALESCE( items.itype, biblioitems.itemtype ) = ?"
442       : " AND biblioitems.itemtype = ?"
443       if defined $ruleitemtype;
444
445     my $sthcount = $dbh->prepare($querycount);
446
447     if ( defined $ruleitemtype ) {
448         $sthcount->execute( $borrowernumber, $branchcode, $ruleitemtype );
449     }
450     else {
451         $sthcount->execute( $borrowernumber, $branchcode );
452     }
453
454     my $reservecount = "0";
455     if ( my $rowcount = $sthcount->fetchrow_hashref() ) {
456         $reservecount = $rowcount->{count};
457     }
458
459     # we check if it's ok or not
460     if (   defined  $allowedreserves && $allowedreserves ne ''
461         && $reservecount >= $allowedreserves ) {
462         return { status => 'tooManyReserves', limit => $allowedreserves };
463     }
464
465     # Now we need to check hold limits by patron category
466     my $rule = Koha::CirculationRules->get_effective_rule(
467         {
468             categorycode => $borrower->{categorycode},
469             branchcode   => $branchcode,
470             rule_name    => 'max_holds',
471         }
472     );
473     if ( $rule && defined( $rule->rule_value ) && $rule->rule_value ne '' ) {
474         my $total_holds_count = Koha::Holds->search(
475             {
476                 borrowernumber => $borrower->{borrowernumber}
477             }
478         )->count();
479
480         return { status => 'tooManyReserves', limit => $rule->rule_value} if $total_holds_count >= $rule->rule_value;
481     }
482
483     my $reserves_control_branch =
484       GetReservesControlBranch( $item->unblessed(), $borrower );
485     my $branchitemrule =
486       C4::Circulation::GetBranchItemRule( $reserves_control_branch, $item->itype ); # FIXME Should not be item->effective_itemtype?
487
488     if ( $branchitemrule->{holdallowed} == 0 ) {
489         return { status => 'notReservable' };
490     }
491
492     if (   $branchitemrule->{holdallowed} == 1
493         && $borrower->{branchcode} ne $item->homebranch )
494     {
495         return { status => 'cannotReserveFromOtherBranches' };
496     }
497
498     my $item_library = Koha::Libraries->find( {branchcode => $item->homebranch} );
499     if ( $branchitemrule->{holdallowed} == 3) {
500         if($borrower->{branchcode} ne $item->homebranch && !$item_library->validate_hold_sibling( {branchcode => $borrower->{branchcode}} )) {
501             return { status => 'branchNotInHoldGroup' };
502         }
503     }
504
505     # If reservecount is ok, we check item branch if IndependentBranches is ON
506     # and canreservefromotherbranches is OFF
507     if ( C4::Context->preference('IndependentBranches')
508         and !C4::Context->preference('canreservefromotherbranches') )
509     {
510         if ( $item->homebranch ne $borrower->{branchcode} ) {
511             return { status => 'cannotReserveFromOtherBranches' };
512         }
513     }
514
515     if ($pickup_branchcode) {
516         my $destination = Koha::Libraries->find({
517             branchcode => $pickup_branchcode,
518         });
519
520         unless ($destination) {
521             return { status => 'libraryNotFound' };
522         }
523         unless ($destination->pickup_location) {
524             return { status => 'libraryNotPickupLocation' };
525         }
526         unless ($item->can_be_transferred({ to => $destination })) {
527             return { status => 'cannotBeTransferred' };
528         }
529         unless ($branchitemrule->{hold_fulfillment_policy} ne 'holdgroup' || $item_library->validate_hold_sibling( {branchcode => $pickup_branchcode} )) {
530             return { status => 'pickupNotInHoldGroup' };
531         }
532         unless ($branchitemrule->{hold_fulfillment_policy} ne 'patrongroup' || Koha::Libraries->find({branchcode => $borrower->{branchcode}})->validate_hold_sibling({branchcode => $pickup_branchcode})) {
533             return { status => 'pickupNotInHoldGroup' };
534         }
535     }
536
537     return { status => 'OK' };
538 }
539
540 =head2 CanReserveBeCanceledFromOpac
541
542     $number = CanReserveBeCanceledFromOpac($reserve_id, $borrowernumber);
543
544     returns 1 if reserve can be cancelled by user from OPAC.
545     First check if reserve belongs to user, next checks if reserve is not in
546     transfer or waiting status
547
548 =cut
549
550 sub CanReserveBeCanceledFromOpac {
551     my ($reserve_id, $borrowernumber) = @_;
552
553     return unless $reserve_id and $borrowernumber;
554     my $reserve = Koha::Holds->find($reserve_id);
555
556     return 0 unless $reserve->borrowernumber == $borrowernumber;
557     return 0 if ( $reserve->found eq 'W' ) or ( $reserve->found eq 'T' );
558
559     return 1;
560
561 }
562
563 =head2 GetOtherReserves
564
565   ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
566
567 Check queued list of this document and check if this document must be transferred
568
569 =cut
570
571 sub GetOtherReserves {
572     my ($itemnumber) = @_;
573     my $messages;
574     my $nextreservinfo;
575     my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
576     if ($checkreserves) {
577         my $item = Koha::Items->find($itemnumber);
578         if ( $item->holdingbranch ne $checkreserves->{'branchcode'} ) {
579             $messages->{'transfert'} = $checkreserves->{'branchcode'};
580             #minus priorities of others reservs
581             ModReserveMinusPriority(
582                 $itemnumber,
583                 $checkreserves->{'reserve_id'},
584             );
585
586             #launch the subroutine dotransfer
587             C4::Items::ModItemTransfer(
588                 $itemnumber,
589                 $item->holdingbranch,
590                 $checkreserves->{'branchcode'}
591               ),
592               ;
593         }
594
595      #step 2b : case of a reservation on the same branch, set the waiting status
596         else {
597             $messages->{'waiting'} = 1;
598             ModReserveMinusPriority(
599                 $itemnumber,
600                 $checkreserves->{'reserve_id'},
601             );
602             ModReserveStatus($itemnumber,'W');
603         }
604
605         $nextreservinfo = $checkreserves->{'borrowernumber'};
606     }
607
608     return ( $messages, $nextreservinfo );
609 }
610
611 =head2 ChargeReserveFee
612
613     $fee = ChargeReserveFee( $borrowernumber, $fee, $title );
614
615     Charge the fee for a reserve (if $fee > 0)
616
617 =cut
618
619 sub ChargeReserveFee {
620     my ( $borrowernumber, $fee, $title ) = @_;
621     return if !$fee || $fee == 0;    # the last test is needed to include 0.00
622     Koha::Account->new( { patron_id => $borrowernumber } )->add_debit(
623         {
624             amount       => $fee,
625             description  => $title,
626             note         => undef,
627             user_id      => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
628             library_id   => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
629             interface    => C4::Context->interface,
630             invoice_type => undef,
631             type         => 'RESERVE',
632             item_id      => undef
633         }
634     );
635 }
636
637 =head2 GetReserveFee
638
639     $fee = GetReserveFee( $borrowernumber, $biblionumber );
640
641     Calculate the fee for a reserve (if applicable).
642
643 =cut
644
645 sub GetReserveFee {
646     my ( $borrowernumber, $biblionumber ) = @_;
647     my $borquery = qq{
648 SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE borrowernumber = ?
649     };
650     my $issue_qry = qq{
651 SELECT COUNT(*) FROM items
652 LEFT JOIN issues USING (itemnumber)
653 WHERE items.biblionumber=? AND issues.issue_id IS NULL
654     };
655     my $holds_qry = qq{
656 SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?
657     };
658
659     my $dbh = C4::Context->dbh;
660     my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
661     my $hold_fee_mode = C4::Context->preference('HoldFeeMode') || 'not_always';
662     if( $fee and $fee > 0 and $hold_fee_mode eq 'not_always' ) {
663         # This is a reconstruction of the old code:
664         # Compare number of items with items issued, and optionally check holds
665         # If not all items are issued and there are no holds: charge no fee
666         # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
667         my ( $notissued, $reserved );
668         ( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
669             ( $biblionumber ) );
670         if( $notissued ) {
671             ( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
672                 ( $biblionumber, $borrowernumber ) );
673             $fee = 0 if $reserved == 0;
674         }
675     }
676     return $fee;
677 }
678
679 =head2 GetReserveStatus
680
681   $reservestatus = GetReserveStatus($itemnumber);
682
683 Takes an itemnumber and returns the status of the reserve placed on it.
684 If several reserves exist, the reserve with the lower priority is given.
685
686 =cut
687
688 ## FIXME: I don't think this does what it thinks it does.
689 ## It only ever checks the first reserve result, even though
690 ## multiple reserves for that bib can have the itemnumber set
691 ## the sub is only used once in the codebase.
692 sub GetReserveStatus {
693     my ($itemnumber) = @_;
694
695     my $dbh = C4::Context->dbh;
696
697     my ($sth, $found, $priority);
698     if ( $itemnumber ) {
699         $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
700         $sth->execute($itemnumber);
701         ($found, $priority) = $sth->fetchrow_array;
702     }
703
704     if(defined $found) {
705         return 'Waiting'  if $found eq 'W' and $priority == 0;
706         return 'Finished' if $found eq 'F';
707     }
708
709     return 'Reserved' if defined $priority && $priority > 0;
710
711     return ''; # empty string here will remove need for checking undef, or less log lines
712 }
713
714 =head2 CheckReserves
715
716   ($status, $matched_reserve, $possible_reserves) = &CheckReserves($itemnumber);
717   ($status, $matched_reserve, $possible_reserves) = &CheckReserves(undef, $barcode);
718   ($status, $matched_reserve, $possible_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
719
720 Find a book in the reserves.
721
722 C<$itemnumber> is the book's item number.
723 C<$lookahead> is the number of days to look in advance for future reserves.
724
725 As I understand it, C<&CheckReserves> looks for the given item in the
726 reserves. If it is found, that's a match, and C<$status> is set to
727 C<Waiting>.
728
729 Otherwise, it finds the most important item in the reserves with the
730 same biblio number as this book (I'm not clear on this) and returns it
731 with C<$status> set to C<Reserved>.
732
733 C<&CheckReserves> returns a two-element list:
734
735 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
736
737 C<$reserve> is the reserve item that matched. It is a
738 reference-to-hash whose keys are mostly the fields of the reserves
739 table in the Koha database.
740
741 =cut
742
743 sub CheckReserves {
744     my ( $item, $barcode, $lookahead_days, $ignore_borrowers) = @_;
745     my $dbh = C4::Context->dbh;
746     my $sth;
747     my $select;
748     if (C4::Context->preference('item-level_itypes')){
749         $select = "
750            SELECT items.biblionumber,
751            items.biblioitemnumber,
752            itemtypes.notforloan,
753            items.notforloan AS itemnotforloan,
754            items.itemnumber,
755            items.damaged,
756            items.homebranch,
757            items.holdingbranch
758            FROM   items
759            LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
760            LEFT JOIN itemtypes   ON items.itype   = itemtypes.itemtype
761         ";
762     }
763     else {
764         $select = "
765            SELECT items.biblionumber,
766            items.biblioitemnumber,
767            itemtypes.notforloan,
768            items.notforloan AS itemnotforloan,
769            items.itemnumber,
770            items.damaged,
771            items.homebranch,
772            items.holdingbranch
773            FROM   items
774            LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
775            LEFT JOIN itemtypes   ON biblioitems.itemtype   = itemtypes.itemtype
776         ";
777     }
778
779     if ($item) {
780         $sth = $dbh->prepare("$select WHERE itemnumber = ?");
781         $sth->execute($item);
782     }
783     else {
784         $sth = $dbh->prepare("$select WHERE barcode = ?");
785         $sth->execute($barcode);
786     }
787     # note: we get the itemnumber because we might have started w/ just the barcode.  Now we know for sure we have it.
788     my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged, $item_homebranch, $item_holdingbranch ) = $sth->fetchrow_array;
789     return if ( $damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') );
790
791     return unless $itemnumber; # bail if we got nothing.
792     # if item is not for loan it cannot be reserved either.....
793     # except where items.notforloan < 0 :  This indicates the item is holdable.
794     return if  ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
795
796     # Find this item in the reserves
797     my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber, $lookahead_days, $ignore_borrowers);
798
799     # $priority and $highest are used to find the most important item
800     # in the list returned by &_Findgroupreserve. (The lower $priority,
801     # the more important the item.)
802     # $highest is the most important item we've seen so far.
803     my $highest;
804     if (scalar @reserves) {
805         my $LocalHoldsPriority = C4::Context->preference('LocalHoldsPriority');
806         my $LocalHoldsPriorityPatronControl = C4::Context->preference('LocalHoldsPriorityPatronControl');
807         my $LocalHoldsPriorityItemControl = C4::Context->preference('LocalHoldsPriorityItemControl');
808
809         my $priority = 10000000;
810         foreach my $res (@reserves) {
811             if ( $res->{'itemnumber'} && $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
812                 if ($res->{'found'} eq 'W') {
813                     return ( "Waiting", $res, \@reserves ); # Found it, it is waiting
814                 } else {
815                     return ( "Reserved", $res, \@reserves ); # Found determinated hold, e. g. the tranferred one
816                 }
817             } else {
818                 my $patron;
819                 my $item;
820                 my $local_hold_match;
821
822                 if ($LocalHoldsPriority) {
823                     $patron = Koha::Patrons->find( $res->{borrowernumber} );
824                     $item = Koha::Items->find($itemnumber);
825
826                     my $local_holds_priority_item_branchcode =
827                       $item->$LocalHoldsPriorityItemControl;
828                     my $local_holds_priority_patron_branchcode =
829                       ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
830                       ? $res->{branchcode}
831                       : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
832                       ? $patron->branchcode
833                       : undef;
834                     $local_hold_match =
835                       $local_holds_priority_item_branchcode eq
836                       $local_holds_priority_patron_branchcode;
837                 }
838
839                 # See if this item is more important than what we've got so far
840                 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
841                     $item ||= Koha::Items->find($itemnumber);
842                     next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype;
843                     $patron ||= Koha::Patrons->find( $res->{borrowernumber} );
844                     my $branch = GetReservesControlBranch( $item->unblessed, $patron->unblessed );
845                     my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype);
846                     next if ($branchitemrule->{'holdallowed'} == 0);
847                     next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $patron->branchcode));
848                     my $library = Koha::Libraries->find({branchcode=>$item->homebranch});
849                     next if (($branchitemrule->{'holdallowed'} == 3) && (!$library->validate_hold_sibling({branchcode => $patron->branchcode}) ));
850                     my $hold_fulfillment_policy = $branchitemrule->{hold_fulfillment_policy};
851                     next if ( ($hold_fulfillment_policy eq 'holdgroup') && (!$library->validate_hold_sibling({branchcode => $res->{branchcode}})) );
852                     next if ( ($hold_fulfillment_policy eq 'homebranch') && ($res->{branchcode} ne $item->$hold_fulfillment_policy) );
853                     next if ( ($hold_fulfillment_policy eq 'holdingbranch') && ($res->{branchcode} ne $item->$hold_fulfillment_policy) );
854                     next unless $item->can_be_transferred( { to => Koha::Libraries->find( $res->{branchcode} ) } );
855                     $priority = $res->{'priority'};
856                     $highest  = $res;
857                     last if $local_hold_match;
858                 }
859             }
860         }
861     }
862
863     # If we get this far, then no exact match was found.
864     # We return the most important (i.e. next) reservation.
865     if ($highest) {
866         $highest->{'itemnumber'} = $item;
867         return ( "Reserved", $highest, \@reserves );
868     }
869
870     return ( '' );
871 }
872
873 =head2 CancelExpiredReserves
874
875   CancelExpiredReserves();
876
877 Cancels all reserves with an expiration date from before today.
878
879 =cut
880
881 sub CancelExpiredReserves {
882     my $today = dt_from_string();
883     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
884     my $expireWaiting = C4::Context->preference('ExpireReservesMaxPickUpDelay');
885
886     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
887     my $params = { expirationdate => { '<', $dtf->format_date($today) } };
888     $params->{found} = [ { '!=', 'W' }, undef ]  unless $expireWaiting;
889
890     # FIXME To move to Koha::Holds->search_expired (?)
891     my $holds = Koha::Holds->search( $params );
892
893     while ( my $hold = $holds->next ) {
894         my $calendar = Koha::Calendar->new( branchcode => $hold->branchcode );
895
896         next if !$cancel_on_holidays && $calendar->is_holiday( $today );
897
898         my $cancel_params = {};
899         if ( $hold->found eq 'W' ) {
900             $cancel_params->{charge_cancel_fee} = 1;
901         }
902         $hold->cancel( $cancel_params );
903     }
904 }
905
906 =head2 AutoUnsuspendReserves
907
908   AutoUnsuspendReserves();
909
910 Unsuspends all suspended reserves with a suspend_until date from before today.
911
912 =cut
913
914 sub AutoUnsuspendReserves {
915     my $today = dt_from_string();
916
917     my @holds = Koha::Holds->search( { suspend_until => { '<=' => $today->ymd() } } );
918
919     map { $_->resume() } @holds;
920 }
921
922 =head2 ModReserve
923
924   ModReserve({ rank => $rank,
925                reserve_id => $reserve_id,
926                branchcode => $branchcode
927                [, itemnumber => $itemnumber ]
928                [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
929               });
930
931 Change a hold request's priority or cancel it.
932
933 C<$rank> specifies the effect of the change.  If C<$rank>
934 is 'W' or 'n', nothing happens.  This corresponds to leaving a
935 request alone when changing its priority in the holds queue
936 for a bib.
937
938 If C<$rank> is 'del', the hold request is cancelled.
939
940 If C<$rank> is an integer greater than zero, the priority of
941 the request is set to that value.  Since priority != 0 means
942 that the item is not waiting on the hold shelf, setting the
943 priority to a non-zero value also sets the request's found
944 status and waiting date to NULL.
945
946 The optional C<$itemnumber> parameter is used only when
947 C<$rank> is a non-zero integer; if supplied, the itemnumber
948 of the hold request is set accordingly; if omitted, the itemnumber
949 is cleared.
950
951 B<FIXME:> Note that the forgoing can have the effect of causing
952 item-level hold requests to turn into title-level requests.  This
953 will be fixed once reserves has separate columns for requested
954 itemnumber and supplying itemnumber.
955
956 =cut
957
958 sub ModReserve {
959     my ( $params ) = @_;
960
961     my $rank = $params->{'rank'};
962     my $reserve_id = $params->{'reserve_id'};
963     my $branchcode = $params->{'branchcode'};
964     my $itemnumber = $params->{'itemnumber'};
965     my $suspend_until = $params->{'suspend_until'};
966     my $borrowernumber = $params->{'borrowernumber'};
967     my $biblionumber = $params->{'biblionumber'};
968
969     return if $rank eq "W";
970     return if $rank eq "n";
971
972     return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
973
974     my $hold;
975     unless ( $reserve_id ) {
976         my $holds = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber });
977         return unless $holds->count; # FIXME Should raise an exception
978         $hold = $holds->next;
979         $reserve_id = $hold->reserve_id;
980     }
981
982     $hold ||= Koha::Holds->find($reserve_id);
983
984     if ( $rank eq "del" ) {
985         $hold->cancel;
986     }
987     elsif ($rank =~ /^\d+/ and $rank > 0) {
988         logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->unblessed) )
989             if C4::Context->preference('HoldsLog');
990
991         my $properties = {
992             priority    => $rank,
993             branchcode  => $branchcode,
994             itemnumber  => $itemnumber,
995             found       => undef,
996             waitingdate => undef
997         };
998         if (exists $params->{reservedate}) {
999             $properties->{reservedate} = $params->{reservedate} || undef;
1000         }
1001         if (exists $params->{expirationdate}) {
1002             $properties->{expirationdate} = $params->{expirationdate} || undef;
1003         }
1004
1005         $hold->set($properties)->store();
1006
1007         if ( defined( $suspend_until ) ) {
1008             if ( $suspend_until ) {
1009                 $suspend_until = eval { dt_from_string( $suspend_until ) };
1010                 $hold->suspend_hold( $suspend_until );
1011             } else {
1012                 # If the hold is suspended leave the hold suspended, but convert it to an indefinite hold.
1013                 # If the hold is not suspended, this does nothing.
1014                 $hold->set( { suspend_until => undef } )->store();
1015             }
1016         }
1017
1018         _FixPriority({ reserve_id => $reserve_id, rank =>$rank });
1019     }
1020 }
1021
1022 =head2 ModReserveFill
1023
1024   &ModReserveFill($reserve);
1025
1026 Fill a reserve. If I understand this correctly, this means that the
1027 reserved book has been found and given to the patron who reserved it.
1028
1029 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1030 whose keys are fields from the reserves table in the Koha database.
1031
1032 =cut
1033
1034 sub ModReserveFill {
1035     my ($res) = @_;
1036     my $reserve_id = $res->{'reserve_id'};
1037
1038     my $hold = Koha::Holds->find($reserve_id);
1039     # get the priority on this record....
1040     my $priority = $hold->priority;
1041
1042     # update the hold statuses, no need to store it though, we will be deleting it anyway
1043     $hold->set(
1044         {
1045             found    => 'F',
1046             priority => 0,
1047         }
1048     );
1049
1050     # FIXME Must call Koha::Hold->cancel ? => No, should call ->filled and add the correct log
1051     Koha::Old::Hold->new( $hold->unblessed() )->store();
1052
1053     $hold->delete();
1054
1055     if ( C4::Context->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
1056         my $reserve_fee = GetReserveFee( $hold->borrowernumber, $hold->biblionumber );
1057         ChargeReserveFee( $hold->borrowernumber, $reserve_fee, $hold->biblio->title );
1058     }
1059
1060     # now fix the priority on the others (if the priority wasn't
1061     # already sorted!)....
1062     unless ( $priority == 0 ) {
1063         _FixPriority( { reserve_id => $reserve_id, biblionumber => $hold->biblionumber } );
1064     }
1065 }
1066
1067 =head2 ModReserveStatus
1068
1069   &ModReserveStatus($itemnumber, $newstatus);
1070
1071 Update the reserve status for the active (priority=0) reserve.
1072
1073 $itemnumber is the itemnumber the reserve is on
1074
1075 $newstatus is the new status.
1076
1077 =cut
1078
1079 sub ModReserveStatus {
1080
1081     #first : check if we have a reservation for this item .
1082     my ($itemnumber, $newstatus) = @_;
1083     my $dbh = C4::Context->dbh;
1084
1085     my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1086     my $sth_set = $dbh->prepare($query);
1087     $sth_set->execute( $newstatus, $itemnumber );
1088
1089     my $item = Koha::Items->find($itemnumber);
1090     if ( $item->location && $item->location eq 'CART'
1091         && ( !$item->permanent_location || $item->permanent_location ne 'CART' )
1092         && $newstatus ) {
1093       CartToShelf( $itemnumber );
1094     }
1095 }
1096
1097 =head2 ModReserveAffect
1098
1099   &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id);
1100
1101 This function affect an item and a status for a given reserve, either fetched directly
1102 by record_id, or by borrowernumber and itemnumber or biblionumber. If only biblionumber
1103 is given, only first reserve returned is affected, which is ok for anything but
1104 multi-item holds.
1105
1106 if $transferToDo is not set, then the status is set to "Waiting" as well.
1107 otherwise, a transfer is on the way, and the end of the transfer will
1108 take care of the waiting status
1109
1110 =cut
1111
1112 sub ModReserveAffect {
1113     my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id ) = @_;
1114     my $dbh = C4::Context->dbh;
1115
1116     # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1117     # attached to $itemnumber
1118     my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1119     $sth->execute($itemnumber);
1120     my ($biblionumber) = $sth->fetchrow;
1121
1122     # get request - need to find out if item is already
1123     # waiting in order to not send duplicate hold filled notifications
1124
1125     my $hold;
1126     # Find hold by id if we have it
1127     $hold = Koha::Holds->find( $reserve_id ) if $reserve_id;
1128     # Find item level hold for this item if there is one
1129     $hold ||= Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->next();
1130     # Find record level hold if there is no item level hold
1131     $hold ||= Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->next();
1132
1133     return unless $hold;
1134
1135     my $already_on_shelf = $hold->found && $hold->found eq 'W';
1136
1137     $hold->itemnumber($itemnumber);
1138     $hold->set_waiting($transferToDo);
1139
1140     _koha_notify_reserve( $hold->reserve_id )
1141       if ( !$transferToDo && !$already_on_shelf );
1142
1143     _FixPriority( { biblionumber => $biblionumber } );
1144     my $item = Koha::Items->find($itemnumber);
1145     if ( $item->location && $item->location eq 'CART'
1146         && ( !$item->permanent_location || $item->permanent_location ne 'CART' ) ) {
1147       CartToShelf( $itemnumber );
1148     }
1149
1150     return;
1151 }
1152
1153 =head2 ModReserveCancelAll
1154
1155   ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1156
1157 function to cancel reserv,check other reserves, and transfer document if it's necessary
1158
1159 =cut
1160
1161 sub ModReserveCancelAll {
1162     my $messages;
1163     my $nextreservinfo;
1164     my ( $itemnumber, $borrowernumber ) = @_;
1165
1166     #step 1 : cancel the reservation
1167     my $holds = Koha::Holds->search({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
1168     return unless $holds->count;
1169     $holds->next->cancel;
1170
1171     #step 2 launch the subroutine of the others reserves
1172     ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1173
1174     return ( $messages, $nextreservinfo );
1175 }
1176
1177 =head2 ModReserveMinusPriority
1178
1179   &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1180
1181 Reduce the values of queued list
1182
1183 =cut
1184
1185 sub ModReserveMinusPriority {
1186     my ( $itemnumber, $reserve_id ) = @_;
1187
1188     #first step update the value of the first person on reserv
1189     my $dbh   = C4::Context->dbh;
1190     my $query = "
1191         UPDATE reserves
1192         SET    priority = 0 , itemnumber = ?
1193         WHERE  reserve_id = ?
1194     ";
1195     my $sth_upd = $dbh->prepare($query);
1196     $sth_upd->execute( $itemnumber, $reserve_id );
1197     # second step update all others reserves
1198     _FixPriority({ reserve_id => $reserve_id, rank => '0' });
1199 }
1200
1201 =head2 IsAvailableForItemLevelRequest
1202
1203   my $is_available = IsAvailableForItemLevelRequest( $item_record, $borrower_record, $pickup_branchcode );
1204
1205 Checks whether a given item record is available for an
1206 item-level hold request.  An item is available if
1207
1208 * it is not lost AND
1209 * it is not damaged AND
1210 * it is not withdrawn AND
1211 * a waiting or in transit reserve is placed on
1212 * does not have a not for loan value > 0
1213
1214 Need to check the issuingrules onshelfholds column,
1215 if this is set items on the shelf can be placed on hold
1216
1217 Note that IsAvailableForItemLevelRequest() does not
1218 check if the staff operator is authorized to place
1219 a request on the item - in particular,
1220 this routine does not check IndependentBranches
1221 and canreservefromotherbranches.
1222
1223 =cut
1224
1225 sub IsAvailableForItemLevelRequest {
1226     my ( $item, $patron, $pickup_branchcode ) = @_;
1227
1228     my $dbh = C4::Context->dbh;
1229     # must check the notforloan setting of the itemtype
1230     # FIXME - a lot of places in the code do this
1231     #         or something similar - need to be
1232     #         consolidated
1233     my $itemtype = $item->effective_itemtype;
1234     my $notforloan_per_itemtype = Koha::ItemTypes->find($itemtype)->notforloan;
1235
1236     return 0 if
1237         $notforloan_per_itemtype ||
1238         $item->itemlost        ||
1239         $item->notforloan > 0  ||
1240         $item->withdrawn        ||
1241         ($item->damaged && !C4::Context->preference('AllowHoldsOnDamagedItems'));
1242
1243     my $on_shelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
1244
1245     if ($pickup_branchcode) {
1246         my $destination = Koha::Libraries->find($pickup_branchcode);
1247         return 0 unless $destination;
1248         return 0 unless $destination->pickup_location;
1249         return 0 unless $item->can_be_transferred( { to => $destination } );
1250         my $reserves_control_branch =
1251             GetReservesControlBranch( $item->unblessed(), $patron->unblessed() );
1252         my $branchitemrule =
1253             C4::Circulation::GetBranchItemRule( $reserves_control_branch, $item->itype );
1254         my $home_library = Koka::Libraries->find( {branchcode => $item->homebranch} );
1255         return 0 unless $branchitemrule->{hold_fulfillment_policy} ne 'holdgroup' || $home_library->validate_hold_sibling( {branchcode => $pickup_branchcode} );
1256     }
1257
1258     if ( $on_shelf_holds == 1 ) {
1259         return 1;
1260     } elsif ( $on_shelf_holds == 2 ) {
1261         my @items =
1262           Koha::Items->search( { biblionumber => $item->biblionumber } );
1263
1264         my $any_available = 0;
1265
1266         foreach my $i (@items) {
1267             my $reserves_control_branch = GetReservesControlBranch( $i->unblessed(), $patron->unblessed );
1268             my $branchitemrule = C4::Circulation::GetBranchItemRule( $reserves_control_branch, $i->itype );
1269             my $item_library = Koha::Libraries->find( {branchcode => $i->homebranch} );
1270
1271
1272             $any_available = 1
1273               unless $i->itemlost
1274               || $i->notforloan > 0
1275               || $i->withdrawn
1276               || $i->onloan
1277               || IsItemOnHoldAndFound( $i->id )
1278               || ( $i->damaged
1279                 && !C4::Context->preference('AllowHoldsOnDamagedItems') )
1280               || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan
1281               || $branchitemrule->{holdallowed} == 1 && $patron->branchcode ne $i->homebranch
1282               || $branchitemrule->{holdallowed} == 3 && !$item_library->validate_hold_sibling( {branchcode => $patron->branchcode} );
1283         }
1284
1285         return $any_available ? 0 : 1;
1286     } else { # on_shelf_holds == 0 "If any unavailable" (the description is rather cryptic and could still be improved)
1287         return $item->onloan || IsItemOnHoldAndFound( $item->itemnumber );
1288     }
1289 }
1290
1291 sub _get_itype {
1292     my $item = shift;
1293
1294     my $itype;
1295     if (C4::Context->preference('item-level_itypes')) {
1296         # We can't trust GetItem to honour the syspref, so safest to do it ourselves
1297         # When GetItem is fixed, we can remove this
1298         $itype = $item->{itype};
1299     }
1300     else {
1301         # XXX This is a bit dodgy. It relies on biblio itemtype column having different name.
1302         # So if we already have a biblioitems join when calling this function,
1303         # we don't need to access the database again
1304         $itype = $item->{itemtype};
1305     }
1306     unless ($itype) {
1307         my $dbh = C4::Context->dbh;
1308         my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
1309         my $sth = $dbh->prepare($query);
1310         $sth->execute($item->{biblioitemnumber});
1311         if (my $data = $sth->fetchrow_hashref()){
1312             $itype = $data->{itemtype};
1313         }
1314     }
1315     return $itype;
1316 }
1317
1318 =head2 AlterPriority
1319
1320   AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
1321
1322 This function changes a reserve's priority up, down, to the top, or to the bottom.
1323 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1324
1325 =cut
1326
1327 sub AlterPriority {
1328     my ( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority ) = @_;
1329
1330     my $hold = Koha::Holds->find( $reserve_id );
1331     return unless $hold;
1332
1333     if ( $hold->cancellationdate ) {
1334         warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (" . $hold->cancellationdate . ')';
1335         return;
1336     }
1337
1338     if ( $where eq 'up' ) {
1339       return unless $prev_priority;
1340       _FixPriority({ reserve_id => $reserve_id, rank => $prev_priority })
1341     } elsif ( $where eq 'down' ) {
1342       return unless $next_priority;
1343       _FixPriority({ reserve_id => $reserve_id, rank => $next_priority })
1344     } elsif ( $where eq 'top' ) {
1345       _FixPriority({ reserve_id => $reserve_id, rank => $first_priority })
1346     } elsif ( $where eq 'bottom' ) {
1347       _FixPriority({ reserve_id => $reserve_id, rank => $last_priority });
1348     }
1349
1350     # FIXME Should return the new priority
1351 }
1352
1353 =head2 ToggleLowestPriority
1354
1355   ToggleLowestPriority( $borrowernumber, $biblionumber );
1356
1357 This function sets the lowestPriority field to true if is false, and false if it is true.
1358
1359 =cut
1360
1361 sub ToggleLowestPriority {
1362     my ( $reserve_id ) = @_;
1363
1364     my $dbh = C4::Context->dbh;
1365
1366     my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1367     $sth->execute( $reserve_id );
1368
1369     _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1370 }
1371
1372 =head2 ToggleSuspend
1373
1374   ToggleSuspend( $reserve_id );
1375
1376 This function sets the suspend field to true if is false, and false if it is true.
1377 If the reserve is currently suspended with a suspend_until date, that date will
1378 be cleared when it is unsuspended.
1379
1380 =cut
1381
1382 sub ToggleSuspend {
1383     my ( $reserve_id, $suspend_until ) = @_;
1384
1385     $suspend_until = dt_from_string($suspend_until) if ($suspend_until);
1386
1387     my $hold = Koha::Holds->find( $reserve_id );
1388
1389     if ( $hold->is_suspended ) {
1390         $hold->resume()
1391     } else {
1392         $hold->suspend_hold( $suspend_until );
1393     }
1394 }
1395
1396 =head2 SuspendAll
1397
1398   SuspendAll(
1399       borrowernumber   => $borrowernumber,
1400       [ biblionumber   => $biblionumber, ]
1401       [ suspend_until  => $suspend_until, ]
1402       [ suspend        => $suspend ]
1403   );
1404
1405   This function accepts a set of hash keys as its parameters.
1406   It requires either borrowernumber or biblionumber, or both.
1407
1408   suspend_until is wholly optional.
1409
1410 =cut
1411
1412 sub SuspendAll {
1413     my %params = @_;
1414
1415     my $borrowernumber = $params{'borrowernumber'} || undef;
1416     my $biblionumber   = $params{'biblionumber'}   || undef;
1417     my $suspend_until  = $params{'suspend_until'}  || undef;
1418     my $suspend = defined( $params{'suspend'} ) ? $params{'suspend'} : 1;
1419
1420     $suspend_until = eval { dt_from_string($suspend_until) }
1421       if ( defined($suspend_until) );
1422
1423     return unless ( $borrowernumber || $biblionumber );
1424
1425     my $params;
1426     $params->{found}          = undef;
1427     $params->{borrowernumber} = $borrowernumber if $borrowernumber;
1428     $params->{biblionumber}   = $biblionumber if $biblionumber;
1429
1430     my @holds = Koha::Holds->search($params);
1431
1432     if ($suspend) {
1433         map { $_->suspend_hold($suspend_until) } @holds;
1434     }
1435     else {
1436         map { $_->resume() } @holds;
1437     }
1438 }
1439
1440
1441 =head2 _FixPriority
1442
1443   _FixPriority({
1444     reserve_id => $reserve_id,
1445     [rank => $rank,]
1446     [ignoreSetLowestRank => $ignoreSetLowestRank]
1447   });
1448
1449   or
1450
1451   _FixPriority({ biblionumber => $biblionumber});
1452
1453 This routine adjusts the priority of a hold request and holds
1454 on the same bib.
1455
1456 In the first form, where a reserve_id is passed, the priority of the
1457 hold is set to supplied rank, and other holds for that bib are adjusted
1458 accordingly.  If the rank is "del", the hold is cancelled.  If no rank
1459 is supplied, all of the holds on that bib have their priority adjusted
1460 as if the second form had been used.
1461
1462 In the second form, where a biblionumber is passed, the holds on that
1463 bib (that are not captured) are sorted in order of increasing priority,
1464 then have reserves.priority set so that the first non-captured hold
1465 has its priority set to 1, the second non-captured hold has its priority
1466 set to 2, and so forth.
1467
1468 In both cases, holds that have the lowestPriority flag on are have their
1469 priority adjusted to ensure that they remain at the end of the line.
1470
1471 Note that the ignoreSetLowestRank parameter is meant to be used only
1472 when _FixPriority calls itself.
1473
1474 =cut
1475
1476 sub _FixPriority {
1477     my ( $params ) = @_;
1478     my $reserve_id = $params->{reserve_id};
1479     my $rank = $params->{rank} // '';
1480     my $ignoreSetLowestRank = $params->{ignoreSetLowestRank};
1481     my $biblionumber = $params->{biblionumber};
1482
1483     my $dbh = C4::Context->dbh;
1484
1485     my $hold;
1486     if ( $reserve_id ) {
1487         $hold = Koha::Holds->find( $reserve_id );
1488         if (!defined $hold){
1489             # may have already been checked out and hold fulfilled
1490             $hold = Koha::Old::Holds->find( $reserve_id );
1491         }
1492         return unless $hold;
1493     }
1494
1495     unless ( $biblionumber ) { # FIXME This is a very weird API
1496         $biblionumber = $hold->biblionumber;
1497     }
1498
1499     if ( $rank eq "del" ) { # FIXME will crash if called without $hold
1500         $hold->cancel;
1501     }
1502     elsif ( $reserve_id && ( $rank eq "W" || $rank eq "0" ) ) {
1503
1504         # make sure priority for waiting or in-transit items is 0
1505         my $query = "
1506             UPDATE reserves
1507             SET    priority = 0
1508             WHERE reserve_id = ?
1509             AND found IN ('W', 'T')
1510         ";
1511         my $sth = $dbh->prepare($query);
1512         $sth->execute( $reserve_id );
1513     }
1514     my @priority;
1515
1516     # get whats left
1517     my $query = "
1518         SELECT reserve_id, borrowernumber, reservedate
1519         FROM   reserves
1520         WHERE  biblionumber   = ?
1521           AND  ((found <> 'W' AND found <> 'T') OR found IS NULL)
1522         ORDER BY priority ASC
1523     ";
1524     my $sth = $dbh->prepare($query);
1525     $sth->execute( $biblionumber );
1526     while ( my $line = $sth->fetchrow_hashref ) {
1527         push( @priority,     $line );
1528     }
1529
1530     # FIXME This whole sub must be rewritten, especially to highlight what is done when reserve_id is not given
1531     # To find the matching index
1532     my $i;
1533     my $key = -1;    # to allow for 0 to be a valid result
1534     for ( $i = 0 ; $i < @priority ; $i++ ) {
1535         if ( $reserve_id && $reserve_id == $priority[$i]->{'reserve_id'} ) {
1536             $key = $i;    # save the index
1537             last;
1538         }
1539     }
1540
1541     # if index exists in array then move it to new position
1542     if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1543         my $new_rank = $rank -
1544           1;    # $new_rank is what you want the new index to be in the array
1545         my $moving_item = splice( @priority, $key, 1 );
1546         splice( @priority, $new_rank, 0, $moving_item );
1547     }
1548
1549     # now fix the priority on those that are left....
1550     $query = "
1551         UPDATE reserves
1552         SET    priority = ?
1553         WHERE  reserve_id = ?
1554     ";
1555     $sth = $dbh->prepare($query);
1556     for ( my $j = 0 ; $j < @priority ; $j++ ) {
1557         $sth->execute(
1558             $j + 1,
1559             $priority[$j]->{'reserve_id'}
1560         );
1561     }
1562
1563     $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1564     $sth->execute();
1565
1566     unless ( $ignoreSetLowestRank ) {
1567       while ( my $res = $sth->fetchrow_hashref() ) {
1568         _FixPriority({
1569             reserve_id => $res->{'reserve_id'},
1570             rank => '999999',
1571             ignoreSetLowestRank => 1
1572         });
1573       }
1574     }
1575 }
1576
1577 =head2 _Findgroupreserve
1578
1579   @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead, $ignore_borrowers);
1580
1581 Looks for a holds-queue based item-specific match first, then for a holds-queue title-level match, returning the
1582 first match found.  If neither, then we look for non-holds-queue based holds.
1583 Lookahead is the number of days to look in advance.
1584
1585 C<&_Findgroupreserve> returns :
1586 C<@results> is an array of references-to-hash whose keys are mostly
1587 fields from the reserves table of the Koha database, plus
1588 C<biblioitemnumber>.
1589
1590 This routine with either return:
1591 1 - Item specific holds from the holds queue
1592 2 - Title level holds from the holds queue
1593 3 - All holds for this biblionumber
1594
1595 All return values will respect any borrowernumbers passed as arrayref in $ignore_borrowers
1596
1597 =cut
1598
1599 sub _Findgroupreserve {
1600     my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_;
1601     my $dbh   = C4::Context->dbh;
1602
1603     # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1604     # check for exact targeted match
1605     my $item_level_target_query = qq{
1606         SELECT reserves.biblionumber        AS biblionumber,
1607                reserves.borrowernumber      AS borrowernumber,
1608                reserves.reservedate         AS reservedate,
1609                reserves.branchcode          AS branchcode,
1610                reserves.cancellationdate    AS cancellationdate,
1611                reserves.found               AS found,
1612                reserves.reservenotes        AS reservenotes,
1613                reserves.priority            AS priority,
1614                reserves.timestamp           AS timestamp,
1615                biblioitems.biblioitemnumber AS biblioitemnumber,
1616                reserves.itemnumber          AS itemnumber,
1617                reserves.reserve_id          AS reserve_id,
1618                reserves.itemtype            AS itemtype
1619         FROM reserves
1620         JOIN biblioitems USING (biblionumber)
1621         JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1622         WHERE found IS NULL
1623         AND priority > 0
1624         AND item_level_request = 1
1625         AND itemnumber = ?
1626         AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1627         AND suspend = 0
1628         ORDER BY priority
1629     };
1630     my $sth = $dbh->prepare($item_level_target_query);
1631     $sth->execute($itemnumber, $lookahead||0);
1632     my @results;
1633     if ( my $data = $sth->fetchrow_hashref ) {
1634         push( @results, $data )
1635           unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1636     }
1637     return @results if @results;
1638
1639     # check for title-level targeted match
1640     my $title_level_target_query = qq{
1641         SELECT reserves.biblionumber        AS biblionumber,
1642                reserves.borrowernumber      AS borrowernumber,
1643                reserves.reservedate         AS reservedate,
1644                reserves.branchcode          AS branchcode,
1645                reserves.cancellationdate    AS cancellationdate,
1646                reserves.found               AS found,
1647                reserves.reservenotes        AS reservenotes,
1648                reserves.priority            AS priority,
1649                reserves.timestamp           AS timestamp,
1650                biblioitems.biblioitemnumber AS biblioitemnumber,
1651                reserves.itemnumber          AS itemnumber,
1652                reserves.reserve_id          AS reserve_id,
1653                reserves.itemtype            AS itemtype
1654         FROM reserves
1655         JOIN biblioitems USING (biblionumber)
1656         JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1657         WHERE found IS NULL
1658         AND priority > 0
1659         AND item_level_request = 0
1660         AND hold_fill_targets.itemnumber = ?
1661         AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1662         AND suspend = 0
1663         ORDER BY priority
1664     };
1665     $sth = $dbh->prepare($title_level_target_query);
1666     $sth->execute($itemnumber, $lookahead||0);
1667     @results = ();
1668     if ( my $data = $sth->fetchrow_hashref ) {
1669         push( @results, $data )
1670           unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1671     }
1672     return @results if @results;
1673
1674     my $query = qq{
1675         SELECT reserves.biblionumber               AS biblionumber,
1676                reserves.borrowernumber             AS borrowernumber,
1677                reserves.reservedate                AS reservedate,
1678                reserves.waitingdate                AS waitingdate,
1679                reserves.branchcode                 AS branchcode,
1680                reserves.cancellationdate           AS cancellationdate,
1681                reserves.found                      AS found,
1682                reserves.reservenotes               AS reservenotes,
1683                reserves.priority                   AS priority,
1684                reserves.timestamp                  AS timestamp,
1685                reserves.itemnumber                 AS itemnumber,
1686                reserves.reserve_id                 AS reserve_id,
1687                reserves.itemtype                   AS itemtype
1688         FROM reserves
1689         WHERE reserves.biblionumber = ?
1690           AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1691           AND reserves.reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1692           AND suspend = 0
1693           ORDER BY priority
1694     };
1695     $sth = $dbh->prepare($query);
1696     $sth->execute( $biblio, $itemnumber, $lookahead||0);
1697     @results = ();
1698     while ( my $data = $sth->fetchrow_hashref ) {
1699         push( @results, $data )
1700           unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1701     }
1702     return @results;
1703 }
1704
1705 =head2 _koha_notify_reserve
1706
1707   _koha_notify_reserve( $hold->reserve_id );
1708
1709 Sends a notification to the patron that their hold has been filled (through
1710 ModReserveAffect, _not_ ModReserveFill)
1711
1712 The letter code for this notice may be found using the following query:
1713
1714     select distinct letter_code
1715     from message_transports
1716     inner join message_attributes using (message_attribute_id)
1717     where message_name = 'Hold_Filled'
1718
1719 This will probably sipmly be 'HOLD', but because it is defined in the database,
1720 it is subject to addition or change.
1721
1722 The following tables are availalbe witin the notice:
1723
1724     branches
1725     borrowers
1726     biblio
1727     biblioitems
1728     reserves
1729     items
1730
1731 =cut
1732
1733 sub _koha_notify_reserve {
1734     my $reserve_id = shift;
1735     my $hold = Koha::Holds->find($reserve_id);
1736     my $borrowernumber = $hold->borrowernumber;
1737
1738     my $patron = Koha::Patrons->find( $borrowernumber );
1739
1740     # Try to get the borrower's email address
1741     my $to_address = $patron->notice_email_address;
1742
1743     my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( {
1744             borrowernumber => $borrowernumber,
1745             message_name => 'Hold_Filled'
1746     } );
1747
1748     my $library = Koha::Libraries->find( $hold->branchcode )->unblessed;
1749
1750     my $admin_email_address = $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress');
1751
1752     my %letter_params = (
1753         module => 'reserves',
1754         branchcode => $hold->branchcode,
1755         lang => $patron->lang,
1756         tables => {
1757             'branches'       => $library,
1758             'borrowers'      => $patron->unblessed,
1759             'biblio'         => $hold->biblionumber,
1760             'biblioitems'    => $hold->biblionumber,
1761             'reserves'       => $hold->unblessed,
1762             'items'          => $hold->itemnumber,
1763         },
1764     );
1765
1766     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.
1767     my $send_notification = sub {
1768         my ( $mtt, $letter_code ) = (@_);
1769         return unless defined $letter_code;
1770         $letter_params{letter_code} = $letter_code;
1771         $letter_params{message_transport_type} = $mtt;
1772         my $letter =  C4::Letters::GetPreparedLetter ( %letter_params );
1773         unless ($letter) {
1774             warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
1775             return;
1776         }
1777
1778         C4::Letters::EnqueueLetter( {
1779             letter => $letter,
1780             borrowernumber => $borrowernumber,
1781             from_address => $admin_email_address,
1782             message_transport_type => $mtt,
1783         } );
1784     };
1785
1786     while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) {
1787         next if (
1788                ( $mtt eq 'email' and not $to_address ) # No email address
1789             or ( $mtt eq 'sms'   and not $patron->smsalertnumber ) # No SMS number
1790             or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
1791         );
1792
1793         &$send_notification($mtt, $letter_code);
1794         $notification_sent++;
1795     }
1796     #Making sure that a print notification is sent if no other transport types can be utilized.
1797     if (! $notification_sent) {
1798         &$send_notification('print', 'HOLD');
1799     }
1800
1801 }
1802
1803 =head2 _ShiftPriorityByDateAndPriority
1804
1805   $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1806
1807 This increments the priority of all reserves after the one
1808 with either the lowest date after C<$reservedate>
1809 or the lowest priority after C<$priority>.
1810
1811 It effectively makes room for a new reserve to be inserted with a certain
1812 priority, which is returned.
1813
1814 This is most useful when the reservedate can be set by the user.  It allows
1815 the new reserve to be placed before other reserves that have a later
1816 reservedate.  Since priority also is set by the form in reserves/request.pl
1817 the sub accounts for that too.
1818
1819 =cut
1820
1821 sub _ShiftPriorityByDateAndPriority {
1822     my ( $biblio, $resdate, $new_priority ) = @_;
1823
1824     my $dbh = C4::Context->dbh;
1825     my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1826     my $sth = $dbh->prepare( $query );
1827     $sth->execute( $biblio, $resdate, $new_priority );
1828     my $min_priority = $sth->fetchrow;
1829     # if no such matches are found, $new_priority remains as original value
1830     $new_priority = $min_priority if ( $min_priority );
1831
1832     # Shift the priority up by one; works in conjunction with the next SQL statement
1833     $query = "UPDATE reserves
1834               SET priority = priority+1
1835               WHERE biblionumber = ?
1836               AND borrowernumber = ?
1837               AND reservedate = ?
1838               AND found IS NULL";
1839     my $sth_update = $dbh->prepare( $query );
1840
1841     # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1842     $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1843     $sth = $dbh->prepare( $query );
1844     $sth->execute( $new_priority, $biblio );
1845     while ( my $row = $sth->fetchrow_hashref ) {
1846         $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
1847     }
1848
1849     return $new_priority;  # so the caller knows what priority they wind up receiving
1850 }
1851
1852 =head2 MoveReserve
1853
1854   MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
1855
1856 Use when checking out an item to handle reserves
1857 If $cancelreserve boolean is set to true, it will remove existing reserve
1858
1859 =cut
1860
1861 sub MoveReserve {
1862     my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
1863
1864     $cancelreserve //= 0;
1865
1866     my $lookahead = C4::Context->preference('ConfirmFutureHolds'); #number of days to look for future holds
1867     my ( $restype, $res, undef ) = CheckReserves( $itemnumber, undef, $lookahead );
1868     return unless $res;
1869
1870     my $biblionumber     =  $res->{biblionumber};
1871
1872     if ($res->{borrowernumber} == $borrowernumber) {
1873         ModReserveFill($res);
1874     }
1875     else {
1876         # warn "Reserved";
1877         # The item is reserved by someone else.
1878         # Find this item in the reserves
1879
1880         my $borr_res  = Koha::Holds->search({
1881             borrowernumber => $borrowernumber,
1882             biblionumber   => $biblionumber,
1883         },{
1884             order_by       => 'priority'
1885         })->next();
1886
1887         if ( $borr_res ) {
1888             # The item is reserved by the current patron
1889             ModReserveFill($borr_res->unblessed);
1890         }
1891
1892         if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
1893             RevertWaitingStatus({ itemnumber => $itemnumber });
1894         }
1895         elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
1896             my $hold = Koha::Holds->find( $res->{reserve_id} );
1897             $hold->cancel;
1898         }
1899     }
1900 }
1901
1902 =head2 MergeHolds
1903
1904   MergeHolds($dbh,$to_biblio, $from_biblio);
1905
1906 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
1907
1908 =cut
1909
1910 sub MergeHolds {
1911     my ( $dbh, $to_biblio, $from_biblio ) = @_;
1912     my $sth = $dbh->prepare(
1913         "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
1914     );
1915     $sth->execute($from_biblio);
1916     if ( my $data = $sth->fetchrow_hashref() ) {
1917
1918         # holds exist on old record, if not we don't need to do anything
1919         $sth = $dbh->prepare(
1920             "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
1921         $sth->execute( $to_biblio, $from_biblio );
1922
1923         # Reorder by date
1924         # don't reorder those already waiting
1925
1926         $sth = $dbh->prepare(
1927 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
1928         );
1929         my $upd_sth = $dbh->prepare(
1930 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
1931         AND reservedate = ? AND (itemnumber = ? or itemnumber is NULL) "
1932         );
1933         $sth->execute( $to_biblio, 'W', 'T' );
1934         my $priority = 1;
1935         while ( my $reserve = $sth->fetchrow_hashref() ) {
1936             $upd_sth->execute(
1937                 $priority,                    $to_biblio,
1938                 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
1939                 $reserve->{'itemnumber'}
1940             );
1941             $priority++;
1942         }
1943     }
1944 }
1945
1946 =head2 RevertWaitingStatus
1947
1948   RevertWaitingStatus({ itemnumber => $itemnumber });
1949
1950   Reverts a 'waiting' hold back to a regular hold with a priority of 1.
1951
1952   Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
1953           item level hold, even if it was only a bibliolevel hold to
1954           begin with. This is because we can no longer know if a hold
1955           was item-level or bib-level after a hold has been set to
1956           waiting status.
1957
1958 =cut
1959
1960 sub RevertWaitingStatus {
1961     my ( $params ) = @_;
1962     my $itemnumber = $params->{'itemnumber'};
1963
1964     return unless ( $itemnumber );
1965
1966     my $dbh = C4::Context->dbh;
1967
1968     ## Get the waiting reserve we want to revert
1969     my $query = "
1970         SELECT * FROM reserves
1971         WHERE itemnumber = ?
1972         AND found IS NOT NULL
1973     ";
1974     my $sth = $dbh->prepare( $query );
1975     $sth->execute( $itemnumber );
1976     my $reserve = $sth->fetchrow_hashref();
1977
1978     my $hold = Koha::Holds->find( $reserve->{reserve_id} ); # TODO Remove the next raw SQL statements and use this instead
1979
1980     ## Increment the priority of all other non-waiting
1981     ## reserves for this bib record
1982     $query = "
1983         UPDATE reserves
1984         SET
1985           priority = priority + 1
1986         WHERE
1987           biblionumber =  ?
1988         AND
1989           priority > 0
1990     ";
1991     $sth = $dbh->prepare( $query );
1992     $sth->execute( $reserve->{'biblionumber'} );
1993
1994     $hold->set(
1995         {
1996             priority    => 1,
1997             found       => undef,
1998             waitingdate => undef,
1999             itemnumber  => $hold->item_level_hold ? $hold->itemnumber : undef,
2000         }
2001     )->store();
2002
2003     _FixPriority( { biblionumber => $reserve->{biblionumber} } );
2004
2005     return $hold;
2006 }
2007
2008 =head2 ReserveSlip
2009
2010 ReserveSlip(
2011     {
2012         branchcode     => $branchcode,
2013         borrowernumber => $borrowernumber,
2014         biblionumber   => $biblionumber,
2015         [ itemnumber   => $itemnumber, ]
2016         [ barcode      => $barcode, ]
2017     }
2018   )
2019
2020 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2021
2022 The letter code will be HOLD_SLIP, and the following tables are
2023 available within the slip:
2024
2025     reserves
2026     branches
2027     borrowers
2028     biblio
2029     biblioitems
2030     items
2031
2032 =cut
2033
2034 sub ReserveSlip {
2035     my ($args) = @_;
2036     my $branchcode     = $args->{branchcode};
2037     my $borrowernumber = $args->{borrowernumber};
2038     my $biblionumber   = $args->{biblionumber};
2039     my $itemnumber     = $args->{itemnumber};
2040     my $barcode        = $args->{barcode};
2041
2042
2043     my $patron = Koha::Patrons->find($borrowernumber);
2044
2045     my $hold;
2046     if ($itemnumber || $barcode ) {
2047         $itemnumber ||= Koha::Items->find( { barcode => $barcode } )->itemnumber;
2048
2049         $hold = Koha::Holds->search(
2050             {
2051                 biblionumber   => $biblionumber,
2052                 borrowernumber => $borrowernumber,
2053                 itemnumber     => $itemnumber
2054             }
2055         )->next;
2056     }
2057     else {
2058         $hold = Koha::Holds->search(
2059             {
2060                 biblionumber   => $biblionumber,
2061                 borrowernumber => $borrowernumber
2062             }
2063         )->next;
2064     }
2065
2066     return unless $hold;
2067     my $reserve = $hold->unblessed;
2068
2069     return  C4::Letters::GetPreparedLetter (
2070         module => 'circulation',
2071         letter_code => 'HOLD_SLIP',
2072         branchcode => $branchcode,
2073         lang => $patron->lang,
2074         tables => {
2075             'reserves'    => $reserve,
2076             'branches'    => $reserve->{branchcode},
2077             'borrowers'   => $reserve->{borrowernumber},
2078             'biblio'      => $reserve->{biblionumber},
2079             'biblioitems' => $reserve->{biblionumber},
2080             'items'       => $reserve->{itemnumber},
2081         },
2082     );
2083 }
2084
2085 =head2 GetReservesControlBranch
2086
2087   my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2088
2089   Return the branchcode to be used to determine which reserves
2090   policy applies to a transaction.
2091
2092   C<$item> is a hashref for an item. Only 'homebranch' is used.
2093
2094   C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2095
2096 =cut
2097
2098 sub GetReservesControlBranch {
2099     my ( $item, $borrower ) = @_;
2100
2101     my $reserves_control = C4::Context->preference('ReservesControlBranch');
2102
2103     my $branchcode =
2104         ( $reserves_control eq 'ItemHomeLibrary' ) ? $item->{'homebranch'}
2105       : ( $reserves_control eq 'PatronLibrary' )   ? $borrower->{'branchcode'}
2106       :                                              undef;
2107
2108     return $branchcode;
2109 }
2110
2111 =head2 CalculatePriority
2112
2113     my $p = CalculatePriority($biblionumber, $resdate);
2114
2115 Calculate priority for a new reserve on biblionumber, placing it at
2116 the end of the line of all holds whose start date falls before
2117 the current system time and that are neither on the hold shelf
2118 or in transit.
2119
2120 The reserve date parameter is optional; if it is supplied, the
2121 priority is based on the set of holds whose start date falls before
2122 the parameter value.
2123
2124 After calculation of this priority, it is recommended to call
2125 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2126 AddReserves.
2127
2128 =cut
2129
2130 sub CalculatePriority {
2131     my ( $biblionumber, $resdate ) = @_;
2132
2133     my $sql = q{
2134         SELECT COUNT(*) FROM reserves
2135         WHERE biblionumber = ?
2136         AND   priority > 0
2137         AND   (found IS NULL OR found = '')
2138     };
2139     #skip found==W or found==T (waiting or transit holds)
2140     if( $resdate ) {
2141         $sql.= ' AND ( reservedate <= ? )';
2142     }
2143     else {
2144         $sql.= ' AND ( reservedate < NOW() )';
2145     }
2146     my $dbh = C4::Context->dbh();
2147     my @row = $dbh->selectrow_array(
2148         $sql,
2149         undef,
2150         $resdate ? ($biblionumber, $resdate) : ($biblionumber)
2151     );
2152
2153     return @row ? $row[0]+1 : 1;
2154 }
2155
2156 =head2 IsItemOnHoldAndFound
2157
2158     my $bool = IsItemFoundHold( $itemnumber );
2159
2160     Returns true if the item is currently on hold
2161     and that hold has a non-null found status ( W, T, etc. )
2162
2163 =cut
2164
2165 sub IsItemOnHoldAndFound {
2166     my ($itemnumber) = @_;
2167
2168     my $rs = Koha::Database->new()->schema()->resultset('Reserve');
2169
2170     my $found = $rs->count(
2171         {
2172             itemnumber => $itemnumber,
2173             found      => { '!=' => undef }
2174         }
2175     );
2176
2177     return $found;
2178 }
2179
2180 =head2 GetMaxPatronHoldsForRecord
2181
2182 my $holds_per_record = ReservesControlBranch( $borrowernumber, $biblionumber );
2183
2184 For multiple holds on a given record for a given patron, the max
2185 number of record level holds that a patron can be placed is the highest
2186 value of the holds_per_record rule for each item if the record for that
2187 patron. This subroutine finds and returns the highest holds_per_record
2188 rule value for a given patron id and record id.
2189
2190 =cut
2191
2192 sub GetMaxPatronHoldsForRecord {
2193     my ( $borrowernumber, $biblionumber ) = @_;
2194
2195     my $patron = Koha::Patrons->find($borrowernumber);
2196     my @items = Koha::Items->search( { biblionumber => $biblionumber } );
2197
2198     my $controlbranch = C4::Context->preference('ReservesControlBranch');
2199
2200     my $categorycode = $patron->categorycode;
2201     my $branchcode;
2202     $branchcode = $patron->branchcode if ( $controlbranch eq "PatronLibrary" );
2203
2204     my $max = 0;
2205     foreach my $item (@items) {
2206         my $itemtype = $item->effective_itemtype();
2207
2208         $branchcode = $item->homebranch if ( $controlbranch eq "ItemHomeLibrary" );
2209
2210         my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
2211         my $holds_per_record = $rule ? $rule->{holds_per_record} : 0;
2212         $max = $holds_per_record if $holds_per_record > $max;
2213     }
2214
2215     return $max;
2216 }
2217
2218 =head2 GetHoldRule
2219
2220 my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
2221
2222 Returns the matching hold related issuingrule fields for a given
2223 patron category, itemtype, and library.
2224
2225 =cut
2226
2227 sub GetHoldRule {
2228     my ( $categorycode, $itemtype, $branchcode ) = @_;
2229
2230     my $reservesallowed = Koha::CirculationRules->get_effective_rule(
2231         {
2232             itemtype     => $itemtype,
2233             categorycode => $categorycode,
2234             branchcode   => $branchcode,
2235             rule_name    => 'reservesallowed',
2236             order_by     => {
2237                 -desc => [ 'categorycode', 'itemtype', 'branchcode' ]
2238             }
2239         }
2240     );
2241
2242     my $rules;
2243     if ( $reservesallowed ) {
2244         $rules->{reservesallowed} = $reservesallowed->rule_value;
2245         $rules->{itemtype}        = $reservesallowed->itemtype;
2246         $rules->{categorycode}    = $reservesallowed->categorycode;
2247         $rules->{branchcode}      = $reservesallowed->branchcode;
2248     }
2249
2250     my $holds_per_x_rules = Koha::CirculationRules->get_effective_rules(
2251         {
2252             itemtype     => $itemtype,
2253             categorycode => $categorycode,
2254             branchcode   => $branchcode,
2255             rules        => ['holds_per_record', 'holds_per_day'],
2256             order_by     => {
2257                 -desc => [ 'categorycode', 'itemtype', 'branchcode' ]
2258             }
2259         }
2260     );
2261     $rules->{holds_per_record} = $holds_per_x_rules->{holds_per_record};
2262     $rules->{holds_per_day} = $holds_per_x_rules->{holds_per_day};
2263
2264     return $rules;
2265 }
2266
2267 =head1 AUTHOR
2268
2269 Koha Development Team <http://koha-community.org/>
2270
2271 =cut
2272
2273 1;