Bug 15774: Use Koha::Object(s) for additional fields
[koha.git] / C4 / Reserves.pm
blob36175495821272f07b0c154ab8f34ac906ad02a9
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;
37 use C4::Log;
39 use Koha::Biblios;
40 use Koha::DateUtils;
41 use Koha::Calendar;
42 use Koha::Database;
43 use Koha::Hold;
44 use Koha::Old::Hold;
45 use Koha::Holds;
46 use Koha::Libraries;
47 use Koha::IssuingRules;
48 use Koha::Items;
49 use Koha::ItemTypes;
50 use Koha::Patrons;
51 use Koha::CirculationRules;
52 use Koha::Account::Lines;
54 use List::MoreUtils qw( firstidx any );
55 use Carp;
56 use Data::Dumper;
58 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
60 =head1 NAME
62 C4::Reserves - Koha functions for dealing with reservation.
64 =head1 SYNOPSIS
66 use C4::Reserves;
68 =head1 DESCRIPTION
70 This modules provides somes functions to deal with reservations.
72 Reserves are stored in reserves table.
73 The following columns contains important values :
74 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
75 =0 : then the reserve is being dealed
76 - found : NULL : means the patron requested the 1st available, and we haven't chosen the item
77 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
78 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
79 F(inished) : the reserve has been completed, and is done
80 - itemnumber : empty : the reserve is still unaffected to an item
81 filled: the reserve is attached to an item
82 The complete workflow is :
83 ==== 1st use case ====
84 patron request a document, 1st available : P >0, F=NULL, I=NULL
85 a library having it run "transfertodo", and clic on the list
86 if there is no transfer to do, the reserve waiting
87 patron can pick it up P =0, F=W, I=filled
88 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
89 The pickup library receive the book, it check in P =0, F=W, I=filled
90 The patron borrow the book P =0, F=F, I=filled
92 ==== 2nd use case ====
93 patron requests a document, a given item,
94 If pickup is holding branch P =0, F=W, I=filled
95 If transfer needed, write in branchtransfer P =0, F=T, I=filled
96 The pickup library receive the book, it checks it in P =0, F=W, I=filled
97 The patron borrow the book P =0, F=F, I=filled
99 =head1 FUNCTIONS
101 =cut
103 BEGIN {
104 require Exporter;
105 @ISA = qw(Exporter);
106 @EXPORT = qw(
107 &AddReserve
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 &CancelExpiredReserves
127 &AutoUnsuspendReserves
129 &IsAvailableForItemLevelRequest
131 &AlterPriority
132 &ToggleLowestPriority
134 &ReserveSlip
135 &ToggleSuspend
136 &SuspendAll
138 &GetReservesControlBranch
140 IsItemOnHoldAndFound
142 GetMaxPatronHoldsForRecord
144 @EXPORT_OK = qw( MergeHolds );
147 =head2 AddReserve
149 AddReserve($branch,$borrowernumber,$biblionumber,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
151 Adds reserve and generates HOLDPLACED message.
153 The following tables are available witin the HOLDPLACED message:
155 branches
156 borrowers
157 biblio
158 biblioitems
159 items
160 reserves
162 =cut
164 sub AddReserve {
165 my (
166 $branch, $borrowernumber, $biblionumber, $bibitems,
167 $priority, $resdate, $expdate, $notes,
168 $title, $checkitem, $found, $itemtype
169 ) = @_;
171 $resdate = output_pref( { str => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' })
172 or output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
174 $expdate = output_pref({ str => $expdate, dateonly => 1, dateformat => 'iso' });
176 # if we have an item selectionned, and the pickup branch is the same as the holdingbranch
177 # of the document, we force the value $priority and $found .
178 if ( $checkitem and not C4::Context->preference('ReservesNeedReturns') ) {
179 $priority = 0;
180 my $item = Koha::Items->find( $checkitem ); # FIXME Prevent bad calls
181 if ( $item->holdingbranch eq $branch ) {
182 $found = 'W';
186 if ( C4::Context->preference('AllowHoldDateInFuture') ) {
188 # Make room in reserves for this before those of a later reserve date
189 $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
192 my $waitingdate;
194 # If the reserv had the waiting status, we had the value of the resdate
195 if ( $found eq 'W' ) {
196 $waitingdate = $resdate;
199 # Don't add itemtype limit if specific item is selected
200 $itemtype = undef if $checkitem;
202 # updates take place here
203 my $hold = Koha::Hold->new(
205 borrowernumber => $borrowernumber,
206 biblionumber => $biblionumber,
207 reservedate => $resdate,
208 branchcode => $branch,
209 priority => $priority,
210 reservenotes => $notes,
211 itemnumber => $checkitem,
212 found => $found,
213 waitingdate => $waitingdate,
214 expirationdate => $expdate,
215 itemtype => $itemtype,
217 )->store();
218 $hold->set_waiting() if $found eq 'W';
220 logaction( 'HOLDS', 'CREATE', $hold->id, Dumper($hold->unblessed) )
221 if C4::Context->preference('HoldsLog');
223 my $reserve_id = $hold->id();
225 # add a reserve fee if needed
226 if ( C4::Context->preference('HoldFeeMode') ne 'any_time_is_collected' ) {
227 my $reserve_fee = GetReserveFee( $borrowernumber, $biblionumber );
228 ChargeReserveFee( $borrowernumber, $reserve_fee, $title );
231 _FixPriority({ biblionumber => $biblionumber});
233 # Send e-mail to librarian if syspref is active
234 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
235 my $patron = Koha::Patrons->find( $borrowernumber );
236 my $library = $patron->library;
237 if ( my $letter = C4::Letters::GetPreparedLetter (
238 module => 'reserves',
239 letter_code => 'HOLDPLACED',
240 branchcode => $branch,
241 lang => $patron->lang,
242 tables => {
243 'branches' => $library->unblessed,
244 'borrowers' => $patron->unblessed,
245 'biblio' => $biblionumber,
246 'biblioitems' => $biblionumber,
247 'items' => $checkitem,
248 'reserves' => $hold->unblessed,
250 ) ) {
252 my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
254 C4::Letters::EnqueueLetter(
255 { letter => $letter,
256 borrowernumber => $borrowernumber,
257 message_transport_type => 'email',
258 from_address => $admin_email_address,
259 to_address => $admin_email_address,
265 return $reserve_id;
268 =head2 CanBookBeReserved
270 $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber, $branchcode)
271 if ($canReserve eq 'OK') { #We can reserve this Item! }
273 See CanItemBeReserved() for possible return values.
275 =cut
277 sub CanBookBeReserved{
278 my ($borrowernumber, $biblionumber, $pickup_branchcode) = @_;
280 my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber})->get_column("itemnumber");
281 #get items linked via host records
282 my @hostitems = get_hostitemnumbers_of($biblionumber);
283 if (@hostitems){
284 push (@itemnumbers, @hostitems);
287 my $canReserve = { status => '' };
288 foreach my $itemnumber (@itemnumbers) {
289 $canReserve = CanItemBeReserved( $borrowernumber, $itemnumber, $pickup_branchcode );
290 return { status => 'OK' } if $canReserve->{status} eq 'OK';
292 return $canReserve;
295 =head2 CanItemBeReserved
297 $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber, $branchcode)
298 if ($canReserve->{status} eq 'OK') { #We can reserve this Item! }
300 @RETURNS { status => OK }, if the Item can be reserved.
301 { status => ageRestricted }, if the Item is age restricted for this borrower.
302 { status => damaged }, if the Item is damaged.
303 { status => cannotReserveFromOtherBranches }, if syspref 'canreservefromotherbranches' is OK.
304 { status => tooManyReserves, limit => $limit }, if the borrower has exceeded their maximum reserve amount.
305 { status => notReservable }, if holds on this item are not allowed
306 { status => libraryNotFound }, if given branchcode is not an existing library
307 { status => libraryNotPickupLocation }, if given branchcode is not configured to be a pickup location
308 { status => cannotBeTransferred }, if branch transfer limit applies on given item and branchcode
310 =cut
312 sub CanItemBeReserved {
313 my ( $borrowernumber, $itemnumber, $pickup_branchcode ) = @_;
315 my $dbh = C4::Context->dbh;
316 my $ruleitemtype; # itemtype of the matching issuing rule
317 my $allowedreserves = 0; # Total number of holds allowed across all records
318 my $holds_per_record = 1; # Total number of holds allowed for this one given record
319 my $holds_per_day; # Default to unlimited
321 # we retrieve borrowers and items informations #
322 # item->{itype} will come for biblioitems if necessery
323 my $item = Koha::Items->find($itemnumber);
324 my $biblio = $item->biblio;
325 my $patron = Koha::Patrons->find( $borrowernumber );
326 my $borrower = $patron->unblessed;
328 # If an item is damaged and we don't allow holds on damaged items, we can stop right here
329 return { status =>'damaged' }
330 if ( $item->damaged
331 && !C4::Context->preference('AllowHoldsOnDamagedItems') );
333 # Check for the age restriction
334 my ( $ageRestriction, $daysToAgeRestriction ) =
335 C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower );
336 return { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0;
338 # Check that the patron doesn't have an item level hold on this item already
339 return { status =>'itemAlreadyOnHold' }
340 if Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->count();
342 my $controlbranch = C4::Context->preference('ReservesControlBranch');
344 my $querycount = q{
345 SELECT count(*) AS count
346 FROM reserves
347 LEFT JOIN items USING (itemnumber)
348 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
349 LEFT JOIN borrowers USING (borrowernumber)
350 WHERE borrowernumber = ?
353 my $branchcode = "";
354 my $branchfield = "reserves.branchcode";
356 if ( $controlbranch eq "ItemHomeLibrary" ) {
357 $branchfield = "items.homebranch";
358 $branchcode = $item->homebranch;
360 elsif ( $controlbranch eq "PatronLibrary" ) {
361 $branchfield = "borrowers.branchcode";
362 $branchcode = $borrower->{branchcode};
365 # we retrieve rights
366 if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->effective_itemtype, $branchcode ) ) {
367 $ruleitemtype = $rights->{itemtype};
368 $allowedreserves = $rights->{reservesallowed};
369 $holds_per_record = $rights->{holds_per_record};
370 $holds_per_day = $rights->{holds_per_day};
372 else {
373 $ruleitemtype = '*';
376 my $holds = Koha::Holds->search(
378 borrowernumber => $borrowernumber,
379 biblionumber => $item->biblionumber,
380 found => undef, # Found holds don't count against a patron's holds limit
383 if ( $holds->count() >= $holds_per_record ) {
384 return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record };
387 my $today_holds = Koha::Holds->search({
388 borrowernumber => $borrowernumber,
389 reservedate => dt_from_string->date
392 if ( defined $holds_per_day &&
393 ( ( $holds_per_day > 0 && $today_holds->count() >= $holds_per_day )
394 or ( $holds_per_day == 0 ) )
396 return { status => 'tooManyReservesToday', limit => $holds_per_day };
399 # we retrieve count
401 $querycount .= "AND $branchfield = ?";
403 # If using item-level itypes, fall back to the record
404 # level itemtype if the hold has no associated item
405 $querycount .=
406 C4::Context->preference('item-level_itypes')
407 ? " AND COALESCE( items.itype, biblioitems.itemtype ) = ?"
408 : " AND biblioitems.itemtype = ?"
409 if ( $ruleitemtype ne "*" );
411 my $sthcount = $dbh->prepare($querycount);
413 if ( $ruleitemtype eq "*" ) {
414 $sthcount->execute( $borrowernumber, $branchcode );
416 else {
417 $sthcount->execute( $borrowernumber, $branchcode, $ruleitemtype );
420 my $reservecount = "0";
421 if ( my $rowcount = $sthcount->fetchrow_hashref() ) {
422 $reservecount = $rowcount->{count};
425 # we check if it's ok or not
426 if ( $reservecount >= $allowedreserves ) {
427 return { status => 'tooManyReserves', limit => $allowedreserves };
430 # Now we need to check hold limits by patron category
431 my $rule = Koha::CirculationRules->get_effective_rule(
433 categorycode => $borrower->{categorycode},
434 branchcode => $branchcode,
435 rule_name => 'max_holds',
438 if ( $rule && defined( $rule->rule_value ) && $rule->rule_value ne '' ) {
439 my $total_holds_count = Koha::Holds->search(
441 borrowernumber => $borrower->{borrowernumber}
443 )->count();
445 return { status => 'tooManyReserves', limit => $rule->rule_value} if $total_holds_count >= $rule->rule_value;
448 my $circ_control_branch =
449 C4::Circulation::_GetCircControlBranch( $item->unblessed(), $borrower );
450 my $branchitemrule =
451 C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->itype ); # FIXME Should not be item->effective_itemtype?
453 if ( $branchitemrule->{holdallowed} == 0 ) {
454 return { status => 'notReservable' };
457 if ( $branchitemrule->{holdallowed} == 1
458 && $borrower->{branchcode} ne $item->homebranch )
460 return { status => 'cannotReserveFromOtherBranches' };
463 # If reservecount is ok, we check item branch if IndependentBranches is ON
464 # and canreservefromotherbranches is OFF
465 if ( C4::Context->preference('IndependentBranches')
466 and !C4::Context->preference('canreservefromotherbranches') )
468 if ( $item->homebranch ne $borrower->{branchcode} ) {
469 return { status => 'cannotReserveFromOtherBranches' };
473 if ($pickup_branchcode) {
474 my $destination = Koha::Libraries->find({
475 branchcode => $pickup_branchcode,
478 unless ($destination) {
479 return { status => 'libraryNotFound' };
481 unless ($destination->pickup_location) {
482 return { status => 'libraryNotPickupLocation' };
484 unless ($item->can_be_transferred({ to => $destination })) {
485 return 'cannotBeTransferred';
489 return { status => 'OK' };
492 =head2 CanReserveBeCanceledFromOpac
494 $number = CanReserveBeCanceledFromOpac($reserve_id, $borrowernumber);
496 returns 1 if reserve can be cancelled by user from OPAC.
497 First check if reserve belongs to user, next checks if reserve is not in
498 transfer or waiting status
500 =cut
502 sub CanReserveBeCanceledFromOpac {
503 my ($reserve_id, $borrowernumber) = @_;
505 return unless $reserve_id and $borrowernumber;
506 my $reserve = Koha::Holds->find($reserve_id);
508 return 0 unless $reserve->borrowernumber == $borrowernumber;
509 return 0 if ( $reserve->found eq 'W' ) or ( $reserve->found eq 'T' );
511 return 1;
515 =head2 GetOtherReserves
517 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
519 Check queued list of this document and check if this document must be transferred
521 =cut
523 sub GetOtherReserves {
524 my ($itemnumber) = @_;
525 my $messages;
526 my $nextreservinfo;
527 my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
528 if ($checkreserves) {
529 my $item = Koha::Items->find($itemnumber);
530 if ( $item->holdingbranch ne $checkreserves->{'branchcode'} ) {
531 $messages->{'transfert'} = $checkreserves->{'branchcode'};
532 #minus priorities of others reservs
533 ModReserveMinusPriority(
534 $itemnumber,
535 $checkreserves->{'reserve_id'},
538 #launch the subroutine dotransfer
539 C4::Items::ModItemTransfer(
540 $itemnumber,
541 $item->holdingbranch,
542 $checkreserves->{'branchcode'}
547 #step 2b : case of a reservation on the same branch, set the waiting status
548 else {
549 $messages->{'waiting'} = 1;
550 ModReserveMinusPriority(
551 $itemnumber,
552 $checkreserves->{'reserve_id'},
554 ModReserveStatus($itemnumber,'W');
557 $nextreservinfo = $checkreserves->{'borrowernumber'};
560 return ( $messages, $nextreservinfo );
563 =head2 ChargeReserveFee
565 $fee = ChargeReserveFee( $borrowernumber, $fee, $title );
567 Charge the fee for a reserve (if $fee > 0)
569 =cut
571 sub ChargeReserveFee {
572 my ( $borrowernumber, $fee, $title ) = @_;
573 return if !$fee || $fee == 0; # the last test is needed to include 0.00
574 Koha::Account->new( { patron_id => $borrowernumber } )->add_debit(
576 amount => $fee,
577 description => "Reserve Charge - " . $title,
578 note => undef,
579 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : 0,
580 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
581 sip => undef,
582 invoice_type => undef,
583 type => 'reserve',
584 item_id => undef
589 =head2 GetReserveFee
591 $fee = GetReserveFee( $borrowernumber, $biblionumber );
593 Calculate the fee for a reserve (if applicable).
595 =cut
597 sub GetReserveFee {
598 my ( $borrowernumber, $biblionumber ) = @_;
599 my $borquery = qq{
600 SELECT reservefee FROM borrowers LEFT JOIN categories ON borrowers.categorycode = categories.categorycode WHERE borrowernumber = ?
602 my $issue_qry = qq{
603 SELECT COUNT(*) FROM items
604 LEFT JOIN issues USING (itemnumber)
605 WHERE items.biblionumber=? AND issues.issue_id IS NULL
607 my $holds_qry = qq{
608 SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?
611 my $dbh = C4::Context->dbh;
612 my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
613 my $hold_fee_mode = C4::Context->preference('HoldFeeMode') || 'not_always';
614 if( $fee and $fee > 0 and $hold_fee_mode eq 'not_always' ) {
615 # This is a reconstruction of the old code:
616 # Compare number of items with items issued, and optionally check holds
617 # If not all items are issued and there are no holds: charge no fee
618 # NOTE: Lost, damaged, not-for-loan, etc. are just ignored here
619 my ( $notissued, $reserved );
620 ( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
621 ( $biblionumber ) );
622 if( $notissued ) {
623 ( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
624 ( $biblionumber, $borrowernumber ) );
625 $fee = 0 if $reserved == 0;
628 return $fee;
631 =head2 GetReserveStatus
633 $reservestatus = GetReserveStatus($itemnumber);
635 Takes an itemnumber and returns the status of the reserve placed on it.
636 If several reserves exist, the reserve with the lower priority is given.
638 =cut
640 ## FIXME: I don't think this does what it thinks it does.
641 ## It only ever checks the first reserve result, even though
642 ## multiple reserves for that bib can have the itemnumber set
643 ## the sub is only used once in the codebase.
644 sub GetReserveStatus {
645 my ($itemnumber) = @_;
647 my $dbh = C4::Context->dbh;
649 my ($sth, $found, $priority);
650 if ( $itemnumber ) {
651 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
652 $sth->execute($itemnumber);
653 ($found, $priority) = $sth->fetchrow_array;
656 if(defined $found) {
657 return 'Waiting' if $found eq 'W' and $priority == 0;
658 return 'Finished' if $found eq 'F';
661 return 'Reserved' if $priority > 0;
663 return ''; # empty string here will remove need for checking undef, or less log lines
666 =head2 CheckReserves
668 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
669 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
670 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
672 Find a book in the reserves.
674 C<$itemnumber> is the book's item number.
675 C<$lookahead> is the number of days to look in advance for future reserves.
677 As I understand it, C<&CheckReserves> looks for the given item in the
678 reserves. If it is found, that's a match, and C<$status> is set to
679 C<Waiting>.
681 Otherwise, it finds the most important item in the reserves with the
682 same biblio number as this book (I'm not clear on this) and returns it
683 with C<$status> set to C<Reserved>.
685 C<&CheckReserves> returns a two-element list:
687 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
689 C<$reserve> is the reserve item that matched. It is a
690 reference-to-hash whose keys are mostly the fields of the reserves
691 table in the Koha database.
693 =cut
695 sub CheckReserves {
696 my ( $item, $barcode, $lookahead_days, $ignore_borrowers) = @_;
697 my $dbh = C4::Context->dbh;
698 my $sth;
699 my $select;
700 if (C4::Context->preference('item-level_itypes')){
701 $select = "
702 SELECT items.biblionumber,
703 items.biblioitemnumber,
704 itemtypes.notforloan,
705 items.notforloan AS itemnotforloan,
706 items.itemnumber,
707 items.damaged,
708 items.homebranch,
709 items.holdingbranch
710 FROM items
711 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
712 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
715 else {
716 $select = "
717 SELECT items.biblionumber,
718 items.biblioitemnumber,
719 itemtypes.notforloan,
720 items.notforloan AS itemnotforloan,
721 items.itemnumber,
722 items.damaged,
723 items.homebranch,
724 items.holdingbranch
725 FROM items
726 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
727 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
731 if ($item) {
732 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
733 $sth->execute($item);
735 else {
736 $sth = $dbh->prepare("$select WHERE barcode = ?");
737 $sth->execute($barcode);
739 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
740 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged, $item_homebranch, $item_holdingbranch ) = $sth->fetchrow_array;
742 return if ( $damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') );
744 return unless $itemnumber; # bail if we got nothing.
746 # if item is not for loan it cannot be reserved either.....
747 # except where items.notforloan < 0 : This indicates the item is holdable.
748 return if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
750 # Find this item in the reserves
751 my @reserves = _Findgroupreserve( $bibitem, $biblio, $itemnumber, $lookahead_days, $ignore_borrowers);
753 # $priority and $highest are used to find the most important item
754 # in the list returned by &_Findgroupreserve. (The lower $priority,
755 # the more important the item.)
756 # $highest is the most important item we've seen so far.
757 my $highest;
758 if (scalar @reserves) {
759 my $LocalHoldsPriority = C4::Context->preference('LocalHoldsPriority');
760 my $LocalHoldsPriorityPatronControl = C4::Context->preference('LocalHoldsPriorityPatronControl');
761 my $LocalHoldsPriorityItemControl = C4::Context->preference('LocalHoldsPriorityItemControl');
763 my $priority = 10000000;
764 foreach my $res (@reserves) {
765 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
766 if ($res->{'found'} eq 'W') {
767 return ( "Waiting", $res, \@reserves ); # Found it, it is waiting
768 } else {
769 return ( "Reserved", $res, \@reserves ); # Found determinated hold, e. g. the tranferred one
771 } else {
772 my $patron;
773 my $item;
774 my $local_hold_match;
776 if ($LocalHoldsPriority) {
777 $patron = Koha::Patrons->find( $res->{borrowernumber} );
778 $item = Koha::Items->find($itemnumber);
780 my $local_holds_priority_item_branchcode =
781 $item->$LocalHoldsPriorityItemControl;
782 my $local_holds_priority_patron_branchcode =
783 ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
784 ? $res->{branchcode}
785 : ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
786 ? $patron->branchcode
787 : undef;
788 $local_hold_match =
789 $local_holds_priority_item_branchcode eq
790 $local_holds_priority_patron_branchcode;
793 # See if this item is more important than what we've got so far
794 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
795 $item ||= Koha::Items->find($itemnumber);
796 next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype;
797 $patron ||= Koha::Patrons->find( $res->{borrowernumber} );
798 my $branch = GetReservesControlBranch( $item->unblessed, $patron->unblessed );
799 my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype);
800 next if ($branchitemrule->{'holdallowed'} == 0);
801 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $patron->branchcode));
802 my $hold_fulfillment_policy = $branchitemrule->{hold_fulfillment_policy};
803 next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $item->$hold_fulfillment_policy) );
804 $priority = $res->{'priority'};
805 $highest = $res;
806 last if $local_hold_match;
812 # If we get this far, then no exact match was found.
813 # We return the most important (i.e. next) reservation.
814 if ($highest) {
815 $highest->{'itemnumber'} = $item;
816 return ( "Reserved", $highest, \@reserves );
819 return ( '' );
822 =head2 CancelExpiredReserves
824 CancelExpiredReserves();
826 Cancels all reserves with an expiration date from before today.
828 =cut
830 sub CancelExpiredReserves {
831 my $today = dt_from_string();
832 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
833 my $expireWaiting = C4::Context->preference('ExpireReservesMaxPickUpDelay');
835 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
836 my $params = { expirationdate => { '<', $dtf->format_date($today) } };
837 $params->{found} = undef unless $expireWaiting;
839 # FIXME To move to Koha::Holds->search_expired (?)
840 my $holds = Koha::Holds->search( $params );
842 while ( my $hold = $holds->next ) {
843 my $calendar = Koha::Calendar->new( branchcode => $hold->branchcode );
845 next if !$cancel_on_holidays && $calendar->is_holiday( $today );
847 my $cancel_params = {};
848 if ( $hold->found eq 'W' ) {
849 $cancel_params->{charge_cancel_fee} = 1;
851 $hold->cancel( $cancel_params );
855 =head2 AutoUnsuspendReserves
857 AutoUnsuspendReserves();
859 Unsuspends all suspended reserves with a suspend_until date from before today.
861 =cut
863 sub AutoUnsuspendReserves {
864 my $today = dt_from_string();
866 my @holds = Koha::Holds->search( { suspend_until => { '<=' => $today->ymd() } } );
868 map { $_->resume() } @holds;
871 =head2 ModReserve
873 ModReserve({ rank => $rank,
874 reserve_id => $reserve_id,
875 branchcode => $branchcode
876 [, itemnumber => $itemnumber ]
877 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
880 Change a hold request's priority or cancel it.
882 C<$rank> specifies the effect of the change. If C<$rank>
883 is 'W' or 'n', nothing happens. This corresponds to leaving a
884 request alone when changing its priority in the holds queue
885 for a bib.
887 If C<$rank> is 'del', the hold request is cancelled.
889 If C<$rank> is an integer greater than zero, the priority of
890 the request is set to that value. Since priority != 0 means
891 that the item is not waiting on the hold shelf, setting the
892 priority to a non-zero value also sets the request's found
893 status and waiting date to NULL.
895 The optional C<$itemnumber> parameter is used only when
896 C<$rank> is a non-zero integer; if supplied, the itemnumber
897 of the hold request is set accordingly; if omitted, the itemnumber
898 is cleared.
900 B<FIXME:> Note that the forgoing can have the effect of causing
901 item-level hold requests to turn into title-level requests. This
902 will be fixed once reserves has separate columns for requested
903 itemnumber and supplying itemnumber.
905 =cut
907 sub ModReserve {
908 my ( $params ) = @_;
910 my $rank = $params->{'rank'};
911 my $reserve_id = $params->{'reserve_id'};
912 my $branchcode = $params->{'branchcode'};
913 my $itemnumber = $params->{'itemnumber'};
914 my $suspend_until = $params->{'suspend_until'};
915 my $borrowernumber = $params->{'borrowernumber'};
916 my $biblionumber = $params->{'biblionumber'};
918 return if $rank eq "W";
919 return if $rank eq "n";
921 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
923 my $hold;
924 unless ( $reserve_id ) {
925 my $holds = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber });
926 return unless $holds->count; # FIXME Should raise an exception
927 $hold = $holds->next;
928 $reserve_id = $hold->reserve_id;
931 $hold ||= Koha::Holds->find($reserve_id);
933 if ( $rank eq "del" ) {
934 $hold->cancel;
936 elsif ($rank =~ /^\d+/ and $rank > 0) {
937 logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->unblessed) )
938 if C4::Context->preference('HoldsLog');
940 $hold->set(
942 priority => $rank,
943 branchcode => $branchcode,
944 itemnumber => $itemnumber,
945 found => undef,
946 waitingdate => undef
948 )->store();
950 if ( defined( $suspend_until ) ) {
951 if ( $suspend_until ) {
952 $suspend_until = eval { dt_from_string( $suspend_until ) };
953 $hold->suspend_hold( $suspend_until );
954 } else {
955 # If the hold is suspended leave the hold suspended, but convert it to an indefinite hold.
956 # If the hold is not suspended, this does nothing.
957 $hold->set( { suspend_until => undef } )->store();
961 _FixPriority({ reserve_id => $reserve_id, rank =>$rank });
965 =head2 ModReserveFill
967 &ModReserveFill($reserve);
969 Fill a reserve. If I understand this correctly, this means that the
970 reserved book has been found and given to the patron who reserved it.
972 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
973 whose keys are fields from the reserves table in the Koha database.
975 =cut
977 sub ModReserveFill {
978 my ($res) = @_;
979 my $reserve_id = $res->{'reserve_id'};
981 my $hold = Koha::Holds->find($reserve_id);
983 # get the priority on this record....
984 my $priority = $hold->priority;
986 # update the hold statuses, no need to store it though, we will be deleting it anyway
987 $hold->set(
989 found => 'F',
990 priority => 0,
994 # FIXME Must call Koha::Hold->cancel ? => No, should call ->filled and add the correct log
995 Koha::Old::Hold->new( $hold->unblessed() )->store();
997 $hold->delete();
999 if ( C4::Context->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
1000 my $reserve_fee = GetReserveFee( $hold->borrowernumber, $hold->biblionumber );
1001 ChargeReserveFee( $hold->borrowernumber, $reserve_fee, $hold->biblio->title );
1004 # now fix the priority on the others (if the priority wasn't
1005 # already sorted!)....
1006 unless ( $priority == 0 ) {
1007 _FixPriority( { reserve_id => $reserve_id, biblionumber => $hold->biblionumber } );
1011 =head2 ModReserveStatus
1013 &ModReserveStatus($itemnumber, $newstatus);
1015 Update the reserve status for the active (priority=0) reserve.
1017 $itemnumber is the itemnumber the reserve is on
1019 $newstatus is the new status.
1021 =cut
1023 sub ModReserveStatus {
1025 #first : check if we have a reservation for this item .
1026 my ($itemnumber, $newstatus) = @_;
1027 my $dbh = C4::Context->dbh;
1029 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1030 my $sth_set = $dbh->prepare($query);
1031 $sth_set->execute( $newstatus, $itemnumber );
1033 if ( C4::Context->preference("ReturnToShelvingCart") && $newstatus ) {
1034 CartToShelf( $itemnumber );
1038 =head2 ModReserveAffect
1040 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id);
1042 This function affect an item and a status for a given reserve, either fetched directly
1043 by record_id, or by borrowernumber and itemnumber or biblionumber. If only biblionumber
1044 is given, only first reserve returned is affected, which is ok for anything but
1045 multi-item holds.
1047 if $transferToDo is not set, then the status is set to "Waiting" as well.
1048 otherwise, a transfer is on the way, and the end of the transfer will
1049 take care of the waiting status
1051 =cut
1053 sub ModReserveAffect {
1054 my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id ) = @_;
1055 my $dbh = C4::Context->dbh;
1057 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1058 # attached to $itemnumber
1059 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1060 $sth->execute($itemnumber);
1061 my ($biblionumber) = $sth->fetchrow;
1063 # get request - need to find out if item is already
1064 # waiting in order to not send duplicate hold filled notifications
1066 my $hold;
1067 # Find hold by id if we have it
1068 $hold = Koha::Holds->find( $reserve_id ) if $reserve_id;
1069 # Find item level hold for this item if there is one
1070 $hold ||= Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->next();
1071 # Find record level hold if there is no item level hold
1072 $hold ||= Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->next();
1074 return unless $hold;
1076 my $already_on_shelf = $hold->found && $hold->found eq 'W';
1078 $hold->itemnumber($itemnumber);
1079 $hold->set_waiting($transferToDo);
1081 _koha_notify_reserve( $hold->reserve_id )
1082 if ( !$transferToDo && !$already_on_shelf );
1084 _FixPriority( { biblionumber => $biblionumber } );
1086 if ( C4::Context->preference("ReturnToShelvingCart") ) {
1087 CartToShelf($itemnumber);
1090 return;
1093 =head2 ModReserveCancelAll
1095 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1097 function to cancel reserv,check other reserves, and transfer document if it's necessary
1099 =cut
1101 sub ModReserveCancelAll {
1102 my $messages;
1103 my $nextreservinfo;
1104 my ( $itemnumber, $borrowernumber ) = @_;
1106 #step 1 : cancel the reservation
1107 my $holds = Koha::Holds->search({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
1108 return unless $holds->count;
1109 $holds->next->cancel;
1111 #step 2 launch the subroutine of the others reserves
1112 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1114 return ( $messages, $nextreservinfo );
1117 =head2 ModReserveMinusPriority
1119 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1121 Reduce the values of queued list
1123 =cut
1125 sub ModReserveMinusPriority {
1126 my ( $itemnumber, $reserve_id ) = @_;
1128 #first step update the value of the first person on reserv
1129 my $dbh = C4::Context->dbh;
1130 my $query = "
1131 UPDATE reserves
1132 SET priority = 0 , itemnumber = ?
1133 WHERE reserve_id = ?
1135 my $sth_upd = $dbh->prepare($query);
1136 $sth_upd->execute( $itemnumber, $reserve_id );
1137 # second step update all others reserves
1138 _FixPriority({ reserve_id => $reserve_id, rank => '0' });
1141 =head2 IsAvailableForItemLevelRequest
1143 my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
1145 Checks whether a given item record is available for an
1146 item-level hold request. An item is available if
1148 * it is not lost AND
1149 * it is not damaged AND
1150 * it is not withdrawn AND
1151 * a waiting or in transit reserve is placed on
1152 * does not have a not for loan value > 0
1154 Need to check the issuingrules onshelfholds column,
1155 if this is set items on the shelf can be placed on hold
1157 Note that IsAvailableForItemLevelRequest() does not
1158 check if the staff operator is authorized to place
1159 a request on the item - in particular,
1160 this routine does not check IndependentBranches
1161 and canreservefromotherbranches.
1163 =cut
1165 sub IsAvailableForItemLevelRequest {
1166 my $item = shift;
1167 my $borrower = shift;
1169 my $dbh = C4::Context->dbh;
1170 # must check the notforloan setting of the itemtype
1171 # FIXME - a lot of places in the code do this
1172 # or something similar - need to be
1173 # consolidated
1174 my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
1175 my $item_object = Koha::Items->find( $item->{itemnumber } );
1176 my $itemtype = $item_object->effective_itemtype;
1177 my $notforloan_per_itemtype
1178 = $dbh->selectrow_array("SELECT notforloan FROM itemtypes WHERE itemtype = ?",
1179 undef, $itemtype);
1181 return 0 if
1182 $notforloan_per_itemtype ||
1183 $item->{itemlost} ||
1184 $item->{notforloan} > 0 ||
1185 $item->{withdrawn} ||
1186 ($item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems'));
1188 my $on_shelf_holds = Koha::IssuingRules->get_onshelfholds_policy( { item => $item_object, patron => $patron } );
1190 if ( $on_shelf_holds == 1 ) {
1191 return 1;
1192 } elsif ( $on_shelf_holds == 2 ) {
1193 my @items =
1194 Koha::Items->search( { biblionumber => $item->{biblionumber} } );
1196 my $any_available = 0;
1198 foreach my $i (@items) {
1200 my $circ_control_branch = C4::Circulation::_GetCircControlBranch( $i->unblessed(), $borrower );
1201 my $branchitemrule = C4::Circulation::GetBranchItemRule( $circ_control_branch, $i->itype );
1203 $any_available = 1
1204 unless $i->itemlost
1205 || $i->notforloan > 0
1206 || $i->withdrawn
1207 || $i->onloan
1208 || IsItemOnHoldAndFound( $i->id )
1209 || ( $i->damaged
1210 && !C4::Context->preference('AllowHoldsOnDamagedItems') )
1211 || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan
1212 || $branchitemrule->{holdallowed} == 1 && $borrower->{branchcode} ne $i->homebranch;
1215 return $any_available ? 0 : 1;
1216 } else { # on_shelf_holds == 0 "If any unavailable" (the description is rather cryptic and could still be improved)
1217 return $item->{onloan} || IsItemOnHoldAndFound( $item->{itemnumber} );
1221 sub _get_itype {
1222 my $item = shift;
1224 my $itype;
1225 if (C4::Context->preference('item-level_itypes')) {
1226 # We can't trust GetItem to honour the syspref, so safest to do it ourselves
1227 # When GetItem is fixed, we can remove this
1228 $itype = $item->{itype};
1230 else {
1231 # XXX This is a bit dodgy. It relies on biblio itemtype column having different name.
1232 # So if we already have a biblioitems join when calling this function,
1233 # we don't need to access the database again
1234 $itype = $item->{itemtype};
1236 unless ($itype) {
1237 my $dbh = C4::Context->dbh;
1238 my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
1239 my $sth = $dbh->prepare($query);
1240 $sth->execute($item->{biblioitemnumber});
1241 if (my $data = $sth->fetchrow_hashref()){
1242 $itype = $data->{itemtype};
1245 return $itype;
1248 =head2 AlterPriority
1250 AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
1252 This function changes a reserve's priority up, down, to the top, or to the bottom.
1253 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1255 =cut
1257 sub AlterPriority {
1258 my ( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority ) = @_;
1260 my $hold = Koha::Holds->find( $reserve_id );
1261 return unless $hold;
1263 if ( $hold->cancellationdate ) {
1264 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (" . $hold->cancellationdate . ')';
1265 return;
1268 if ( $where eq 'up' ) {
1269 return unless $prev_priority;
1270 _FixPriority({ reserve_id => $reserve_id, rank => $prev_priority })
1271 } elsif ( $where eq 'down' ) {
1272 return unless $next_priority;
1273 _FixPriority({ reserve_id => $reserve_id, rank => $next_priority })
1274 } elsif ( $where eq 'top' ) {
1275 _FixPriority({ reserve_id => $reserve_id, rank => $first_priority })
1276 } elsif ( $where eq 'bottom' ) {
1277 _FixPriority({ reserve_id => $reserve_id, rank => $last_priority });
1280 # FIXME Should return the new priority
1283 =head2 ToggleLowestPriority
1285 ToggleLowestPriority( $borrowernumber, $biblionumber );
1287 This function sets the lowestPriority field to true if is false, and false if it is true.
1289 =cut
1291 sub ToggleLowestPriority {
1292 my ( $reserve_id ) = @_;
1294 my $dbh = C4::Context->dbh;
1296 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1297 $sth->execute( $reserve_id );
1299 _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
1302 =head2 ToggleSuspend
1304 ToggleSuspend( $reserve_id );
1306 This function sets the suspend field to true if is false, and false if it is true.
1307 If the reserve is currently suspended with a suspend_until date, that date will
1308 be cleared when it is unsuspended.
1310 =cut
1312 sub ToggleSuspend {
1313 my ( $reserve_id, $suspend_until ) = @_;
1315 $suspend_until = dt_from_string($suspend_until) if ($suspend_until);
1317 my $hold = Koha::Holds->find( $reserve_id );
1319 if ( $hold->is_suspended ) {
1320 $hold->resume()
1321 } else {
1322 $hold->suspend_hold( $suspend_until );
1326 =head2 SuspendAll
1328 SuspendAll(
1329 borrowernumber => $borrowernumber,
1330 [ biblionumber => $biblionumber, ]
1331 [ suspend_until => $suspend_until, ]
1332 [ suspend => $suspend ]
1335 This function accepts a set of hash keys as its parameters.
1336 It requires either borrowernumber or biblionumber, or both.
1338 suspend_until is wholly optional.
1340 =cut
1342 sub SuspendAll {
1343 my %params = @_;
1345 my $borrowernumber = $params{'borrowernumber'} || undef;
1346 my $biblionumber = $params{'biblionumber'} || undef;
1347 my $suspend_until = $params{'suspend_until'} || undef;
1348 my $suspend = defined( $params{'suspend'} ) ? $params{'suspend'} : 1;
1350 $suspend_until = eval { dt_from_string($suspend_until) }
1351 if ( defined($suspend_until) );
1353 return unless ( $borrowernumber || $biblionumber );
1355 my $params;
1356 $params->{found} = undef;
1357 $params->{borrowernumber} = $borrowernumber if $borrowernumber;
1358 $params->{biblionumber} = $biblionumber if $biblionumber;
1360 my @holds = Koha::Holds->search($params);
1362 if ($suspend) {
1363 map { $_->suspend_hold($suspend_until) } @holds;
1365 else {
1366 map { $_->resume() } @holds;
1371 =head2 _FixPriority
1373 _FixPriority({
1374 reserve_id => $reserve_id,
1375 [rank => $rank,]
1376 [ignoreSetLowestRank => $ignoreSetLowestRank]
1381 _FixPriority({ biblionumber => $biblionumber});
1383 This routine adjusts the priority of a hold request and holds
1384 on the same bib.
1386 In the first form, where a reserve_id is passed, the priority of the
1387 hold is set to supplied rank, and other holds for that bib are adjusted
1388 accordingly. If the rank is "del", the hold is cancelled. If no rank
1389 is supplied, all of the holds on that bib have their priority adjusted
1390 as if the second form had been used.
1392 In the second form, where a biblionumber is passed, the holds on that
1393 bib (that are not captured) are sorted in order of increasing priority,
1394 then have reserves.priority set so that the first non-captured hold
1395 has its priority set to 1, the second non-captured hold has its priority
1396 set to 2, and so forth.
1398 In both cases, holds that have the lowestPriority flag on are have their
1399 priority adjusted to ensure that they remain at the end of the line.
1401 Note that the ignoreSetLowestRank parameter is meant to be used only
1402 when _FixPriority calls itself.
1404 =cut
1406 sub _FixPriority {
1407 my ( $params ) = @_;
1408 my $reserve_id = $params->{reserve_id};
1409 my $rank = $params->{rank} // '';
1410 my $ignoreSetLowestRank = $params->{ignoreSetLowestRank};
1411 my $biblionumber = $params->{biblionumber};
1413 my $dbh = C4::Context->dbh;
1415 my $hold;
1416 if ( $reserve_id ) {
1417 $hold = Koha::Holds->find( $reserve_id );
1418 return unless $hold;
1421 unless ( $biblionumber ) { # FIXME This is a very weird API
1422 $biblionumber = $hold->biblionumber;
1425 if ( $rank eq "del" ) { # FIXME will crash if called without $hold
1426 $hold->cancel;
1428 elsif ( $rank eq "W" || $rank eq "0" ) {
1430 # make sure priority for waiting or in-transit items is 0
1431 my $query = "
1432 UPDATE reserves
1433 SET priority = 0
1434 WHERE reserve_id = ?
1435 AND found IN ('W', 'T')
1437 my $sth = $dbh->prepare($query);
1438 $sth->execute( $reserve_id );
1440 my @priority;
1442 # get whats left
1443 my $query = "
1444 SELECT reserve_id, borrowernumber, reservedate
1445 FROM reserves
1446 WHERE biblionumber = ?
1447 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1448 ORDER BY priority ASC
1450 my $sth = $dbh->prepare($query);
1451 $sth->execute( $biblionumber );
1452 while ( my $line = $sth->fetchrow_hashref ) {
1453 push( @priority, $line );
1456 # To find the matching index
1457 my $i;
1458 my $key = -1; # to allow for 0 to be a valid result
1459 for ( $i = 0 ; $i < @priority ; $i++ ) {
1460 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1461 $key = $i; # save the index
1462 last;
1466 # if index exists in array then move it to new position
1467 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1468 my $new_rank = $rank -
1469 1; # $new_rank is what you want the new index to be in the array
1470 my $moving_item = splice( @priority, $key, 1 );
1471 splice( @priority, $new_rank, 0, $moving_item );
1474 # now fix the priority on those that are left....
1475 $query = "
1476 UPDATE reserves
1477 SET priority = ?
1478 WHERE reserve_id = ?
1480 $sth = $dbh->prepare($query);
1481 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1482 $sth->execute(
1483 $j + 1,
1484 $priority[$j]->{'reserve_id'}
1488 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1489 $sth->execute();
1491 unless ( $ignoreSetLowestRank ) {
1492 while ( my $res = $sth->fetchrow_hashref() ) {
1493 _FixPriority({
1494 reserve_id => $res->{'reserve_id'},
1495 rank => '999999',
1496 ignoreSetLowestRank => 1
1502 =head2 _Findgroupreserve
1504 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead, $ignore_borrowers);
1506 Looks for a holds-queue based item-specific match first, then for a holds-queue title-level match, returning the
1507 first match found. If neither, then we look for non-holds-queue based holds.
1508 Lookahead is the number of days to look in advance.
1510 C<&_Findgroupreserve> returns :
1511 C<@results> is an array of references-to-hash whose keys are mostly
1512 fields from the reserves table of the Koha database, plus
1513 C<biblioitemnumber>.
1515 =cut
1517 sub _Findgroupreserve {
1518 my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_;
1519 my $dbh = C4::Context->dbh;
1521 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1522 # check for exact targeted match
1523 my $item_level_target_query = qq{
1524 SELECT reserves.biblionumber AS biblionumber,
1525 reserves.borrowernumber AS borrowernumber,
1526 reserves.reservedate AS reservedate,
1527 reserves.branchcode AS branchcode,
1528 reserves.cancellationdate AS cancellationdate,
1529 reserves.found AS found,
1530 reserves.reservenotes AS reservenotes,
1531 reserves.priority AS priority,
1532 reserves.timestamp AS timestamp,
1533 biblioitems.biblioitemnumber AS biblioitemnumber,
1534 reserves.itemnumber AS itemnumber,
1535 reserves.reserve_id AS reserve_id,
1536 reserves.itemtype AS itemtype
1537 FROM reserves
1538 JOIN biblioitems USING (biblionumber)
1539 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1540 WHERE found IS NULL
1541 AND priority > 0
1542 AND item_level_request = 1
1543 AND itemnumber = ?
1544 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1545 AND suspend = 0
1546 ORDER BY priority
1548 my $sth = $dbh->prepare($item_level_target_query);
1549 $sth->execute($itemnumber, $lookahead||0);
1550 my @results;
1551 if ( my $data = $sth->fetchrow_hashref ) {
1552 push( @results, $data )
1553 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1555 return @results if @results;
1557 # check for title-level targeted match
1558 my $title_level_target_query = qq{
1559 SELECT reserves.biblionumber AS biblionumber,
1560 reserves.borrowernumber AS borrowernumber,
1561 reserves.reservedate AS reservedate,
1562 reserves.branchcode AS branchcode,
1563 reserves.cancellationdate AS cancellationdate,
1564 reserves.found AS found,
1565 reserves.reservenotes AS reservenotes,
1566 reserves.priority AS priority,
1567 reserves.timestamp AS timestamp,
1568 biblioitems.biblioitemnumber AS biblioitemnumber,
1569 reserves.itemnumber AS itemnumber,
1570 reserves.reserve_id AS reserve_id,
1571 reserves.itemtype AS itemtype
1572 FROM reserves
1573 JOIN biblioitems USING (biblionumber)
1574 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1575 WHERE found IS NULL
1576 AND priority > 0
1577 AND item_level_request = 0
1578 AND hold_fill_targets.itemnumber = ?
1579 AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1580 AND suspend = 0
1581 ORDER BY priority
1583 $sth = $dbh->prepare($title_level_target_query);
1584 $sth->execute($itemnumber, $lookahead||0);
1585 @results = ();
1586 if ( my $data = $sth->fetchrow_hashref ) {
1587 push( @results, $data )
1588 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1590 return @results if @results;
1592 my $query = qq{
1593 SELECT reserves.biblionumber AS biblionumber,
1594 reserves.borrowernumber AS borrowernumber,
1595 reserves.reservedate AS reservedate,
1596 reserves.waitingdate AS waitingdate,
1597 reserves.branchcode AS branchcode,
1598 reserves.cancellationdate AS cancellationdate,
1599 reserves.found AS found,
1600 reserves.reservenotes AS reservenotes,
1601 reserves.priority AS priority,
1602 reserves.timestamp AS timestamp,
1603 reserves.itemnumber AS itemnumber,
1604 reserves.reserve_id AS reserve_id,
1605 reserves.itemtype AS itemtype
1606 FROM reserves
1607 WHERE reserves.biblionumber = ?
1608 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1609 AND reserves.reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY)
1610 AND suspend = 0
1611 ORDER BY priority
1613 $sth = $dbh->prepare($query);
1614 $sth->execute( $biblio, $itemnumber, $lookahead||0);
1615 @results = ();
1616 while ( my $data = $sth->fetchrow_hashref ) {
1617 push( @results, $data )
1618 unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ;
1620 return @results;
1623 =head2 _koha_notify_reserve
1625 _koha_notify_reserve( $hold->reserve_id );
1627 Sends a notification to the patron that their hold has been filled (through
1628 ModReserveAffect, _not_ ModReserveFill)
1630 The letter code for this notice may be found using the following query:
1632 select distinct letter_code
1633 from message_transports
1634 inner join message_attributes using (message_attribute_id)
1635 where message_name = 'Hold_Filled'
1637 This will probably sipmly be 'HOLD', but because it is defined in the database,
1638 it is subject to addition or change.
1640 The following tables are availalbe witin the notice:
1642 branches
1643 borrowers
1644 biblio
1645 biblioitems
1646 reserves
1647 items
1649 =cut
1651 sub _koha_notify_reserve {
1652 my $reserve_id = shift;
1653 my $hold = Koha::Holds->find($reserve_id);
1654 my $borrowernumber = $hold->borrowernumber;
1656 my $patron = Koha::Patrons->find( $borrowernumber );
1658 # Try to get the borrower's email address
1659 my $to_address = $patron->notice_email_address;
1661 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( {
1662 borrowernumber => $borrowernumber,
1663 message_name => 'Hold_Filled'
1664 } );
1666 my $library = Koha::Libraries->find( $hold->branchcode )->unblessed;
1668 my $admin_email_address = $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress');
1670 my %letter_params = (
1671 module => 'reserves',
1672 branchcode => $hold->branchcode,
1673 lang => $patron->lang,
1674 tables => {
1675 'branches' => $library,
1676 'borrowers' => $patron->unblessed,
1677 'biblio' => $hold->biblionumber,
1678 'biblioitems' => $hold->biblionumber,
1679 'reserves' => $hold->unblessed,
1680 'items' => $hold->itemnumber,
1684 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.
1685 my $send_notification = sub {
1686 my ( $mtt, $letter_code ) = (@_);
1687 return unless defined $letter_code;
1688 $letter_params{letter_code} = $letter_code;
1689 $letter_params{message_transport_type} = $mtt;
1690 my $letter = C4::Letters::GetPreparedLetter ( %letter_params );
1691 unless ($letter) {
1692 warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
1693 return;
1696 C4::Letters::EnqueueLetter( {
1697 letter => $letter,
1698 borrowernumber => $borrowernumber,
1699 from_address => $admin_email_address,
1700 message_transport_type => $mtt,
1701 } );
1704 while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) {
1705 next if (
1706 ( $mtt eq 'email' and not $to_address ) # No email address
1707 or ( $mtt eq 'sms' and not $patron->smsalertnumber ) # No SMS number
1708 or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
1711 &$send_notification($mtt, $letter_code);
1712 $notification_sent++;
1714 #Making sure that a print notification is sent if no other transport types can be utilized.
1715 if (! $notification_sent) {
1716 &$send_notification('print', 'HOLD');
1721 =head2 _ShiftPriorityByDateAndPriority
1723 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1725 This increments the priority of all reserves after the one
1726 with either the lowest date after C<$reservedate>
1727 or the lowest priority after C<$priority>.
1729 It effectively makes room for a new reserve to be inserted with a certain
1730 priority, which is returned.
1732 This is most useful when the reservedate can be set by the user. It allows
1733 the new reserve to be placed before other reserves that have a later
1734 reservedate. Since priority also is set by the form in reserves/request.pl
1735 the sub accounts for that too.
1737 =cut
1739 sub _ShiftPriorityByDateAndPriority {
1740 my ( $biblio, $resdate, $new_priority ) = @_;
1742 my $dbh = C4::Context->dbh;
1743 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1744 my $sth = $dbh->prepare( $query );
1745 $sth->execute( $biblio, $resdate, $new_priority );
1746 my $min_priority = $sth->fetchrow;
1747 # if no such matches are found, $new_priority remains as original value
1748 $new_priority = $min_priority if ( $min_priority );
1750 # Shift the priority up by one; works in conjunction with the next SQL statement
1751 $query = "UPDATE reserves
1752 SET priority = priority+1
1753 WHERE biblionumber = ?
1754 AND borrowernumber = ?
1755 AND reservedate = ?
1756 AND found IS NULL";
1757 my $sth_update = $dbh->prepare( $query );
1759 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1760 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1761 $sth = $dbh->prepare( $query );
1762 $sth->execute( $new_priority, $biblio );
1763 while ( my $row = $sth->fetchrow_hashref ) {
1764 $sth_update->execute( $biblio, $row->{borrowernumber}, $row->{reservedate} );
1767 return $new_priority; # so the caller knows what priority they wind up receiving
1770 =head2 MoveReserve
1772 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
1774 Use when checking out an item to handle reserves
1775 If $cancelreserve boolean is set to true, it will remove existing reserve
1777 =cut
1779 sub MoveReserve {
1780 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
1782 my $lookahead = C4::Context->preference('ConfirmFutureHolds'); #number of days to look for future holds
1783 my ( $restype, $res, $all_reserves ) = CheckReserves( $itemnumber, undef, $lookahead );
1784 return unless $res;
1786 my $biblionumber = $res->{biblionumber};
1788 if ($res->{borrowernumber} == $borrowernumber) {
1789 ModReserveFill($res);
1791 else {
1792 # warn "Reserved";
1793 # The item is reserved by someone else.
1794 # Find this item in the reserves
1796 my $borr_res;
1797 foreach (@$all_reserves) {
1798 $_->{'borrowernumber'} == $borrowernumber or next;
1799 $_->{'biblionumber'} == $biblionumber or next;
1801 $borr_res = $_;
1802 last;
1805 if ( $borr_res ) {
1806 # The item is reserved by the current patron
1807 ModReserveFill($borr_res);
1810 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
1811 RevertWaitingStatus({ itemnumber => $itemnumber });
1813 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
1814 my $hold = Koha::Holds->find( $res->{reserve_id} );
1815 $hold->cancel;
1820 =head2 MergeHolds
1822 MergeHolds($dbh,$to_biblio, $from_biblio);
1824 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
1826 =cut
1828 sub MergeHolds {
1829 my ( $dbh, $to_biblio, $from_biblio ) = @_;
1830 my $sth = $dbh->prepare(
1831 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
1833 $sth->execute($from_biblio);
1834 if ( my $data = $sth->fetchrow_hashref() ) {
1836 # holds exist on old record, if not we don't need to do anything
1837 $sth = $dbh->prepare(
1838 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
1839 $sth->execute( $to_biblio, $from_biblio );
1841 # Reorder by date
1842 # don't reorder those already waiting
1844 $sth = $dbh->prepare(
1845 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
1847 my $upd_sth = $dbh->prepare(
1848 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
1849 AND reservedate = ? AND (itemnumber = ? or itemnumber is NULL) "
1851 $sth->execute( $to_biblio, 'W', 'T' );
1852 my $priority = 1;
1853 while ( my $reserve = $sth->fetchrow_hashref() ) {
1854 $upd_sth->execute(
1855 $priority, $to_biblio,
1856 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
1857 $reserve->{'itemnumber'}
1859 $priority++;
1864 =head2 RevertWaitingStatus
1866 RevertWaitingStatus({ itemnumber => $itemnumber });
1868 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
1870 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
1871 item level hold, even if it was only a bibliolevel hold to
1872 begin with. This is because we can no longer know if a hold
1873 was item-level or bib-level after a hold has been set to
1874 waiting status.
1876 =cut
1878 sub RevertWaitingStatus {
1879 my ( $params ) = @_;
1880 my $itemnumber = $params->{'itemnumber'};
1882 return unless ( $itemnumber );
1884 my $dbh = C4::Context->dbh;
1886 ## Get the waiting reserve we want to revert
1887 my $query = "
1888 SELECT * FROM reserves
1889 WHERE itemnumber = ?
1890 AND found IS NOT NULL
1892 my $sth = $dbh->prepare( $query );
1893 $sth->execute( $itemnumber );
1894 my $reserve = $sth->fetchrow_hashref();
1896 ## Increment the priority of all other non-waiting
1897 ## reserves for this bib record
1898 $query = "
1899 UPDATE reserves
1901 priority = priority + 1
1902 WHERE
1903 biblionumber = ?
1905 priority > 0
1907 $sth = $dbh->prepare( $query );
1908 $sth->execute( $reserve->{'biblionumber'} );
1910 ## Fix up the currently waiting reserve
1911 $query = "
1912 UPDATE reserves
1914 priority = 1,
1915 found = NULL,
1916 waitingdate = NULL
1917 WHERE
1918 reserve_id = ?
1920 $sth = $dbh->prepare( $query );
1921 $sth->execute( $reserve->{'reserve_id'} );
1922 _FixPriority( { biblionumber => $reserve->{biblionumber} } );
1925 =head2 ReserveSlip
1927 ReserveSlip(
1929 branchcode => $branchcode,
1930 borrowernumber => $borrowernumber,
1931 biblionumber => $biblionumber,
1932 [ itemnumber => $itemnumber, ]
1933 [ barcode => $barcode, ]
1937 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
1939 The letter code will be HOLD_SLIP, and the following tables are
1940 available within the slip:
1942 reserves
1943 branches
1944 borrowers
1945 biblio
1946 biblioitems
1947 items
1949 =cut
1951 sub ReserveSlip {
1952 my ($args) = @_;
1953 my $branchcode = $args->{branchcode};
1954 my $borrowernumber = $args->{borrowernumber};
1955 my $biblionumber = $args->{biblionumber};
1956 my $itemnumber = $args->{itemnumber};
1957 my $barcode = $args->{barcode};
1960 my $patron = Koha::Patrons->find($borrowernumber);
1962 my $hold;
1963 if ($itemnumber || $barcode ) {
1964 $itemnumber ||= Koha::Items->find( { barcode => $barcode } )->itemnumber;
1966 $hold = Koha::Holds->search(
1968 biblionumber => $biblionumber,
1969 borrowernumber => $borrowernumber,
1970 itemnumber => $itemnumber
1972 )->next;
1974 else {
1975 $hold = Koha::Holds->search(
1977 biblionumber => $biblionumber,
1978 borrowernumber => $borrowernumber
1980 )->next;
1983 return unless $hold;
1984 my $reserve = $hold->unblessed;
1986 return C4::Letters::GetPreparedLetter (
1987 module => 'circulation',
1988 letter_code => 'HOLD_SLIP',
1989 branchcode => $branchcode,
1990 lang => $patron->lang,
1991 tables => {
1992 'reserves' => $reserve,
1993 'branches' => $reserve->{branchcode},
1994 'borrowers' => $reserve->{borrowernumber},
1995 'biblio' => $reserve->{biblionumber},
1996 'biblioitems' => $reserve->{biblionumber},
1997 'items' => $reserve->{itemnumber},
2002 =head2 GetReservesControlBranch
2004 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2006 Return the branchcode to be used to determine which reserves
2007 policy applies to a transaction.
2009 C<$item> is a hashref for an item. Only 'homebranch' is used.
2011 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2013 =cut
2015 sub GetReservesControlBranch {
2016 my ( $item, $borrower ) = @_;
2018 my $reserves_control = C4::Context->preference('ReservesControlBranch');
2020 my $branchcode =
2021 ( $reserves_control eq 'ItemHomeLibrary' ) ? $item->{'homebranch'}
2022 : ( $reserves_control eq 'PatronLibrary' ) ? $borrower->{'branchcode'}
2023 : undef;
2025 return $branchcode;
2028 =head2 CalculatePriority
2030 my $p = CalculatePriority($biblionumber, $resdate);
2032 Calculate priority for a new reserve on biblionumber, placing it at
2033 the end of the line of all holds whose start date falls before
2034 the current system time and that are neither on the hold shelf
2035 or in transit.
2037 The reserve date parameter is optional; if it is supplied, the
2038 priority is based on the set of holds whose start date falls before
2039 the parameter value.
2041 After calculation of this priority, it is recommended to call
2042 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2043 AddReserves.
2045 =cut
2047 sub CalculatePriority {
2048 my ( $biblionumber, $resdate ) = @_;
2050 my $sql = q{
2051 SELECT COUNT(*) FROM reserves
2052 WHERE biblionumber = ?
2053 AND priority > 0
2054 AND (found IS NULL OR found = '')
2056 #skip found==W or found==T (waiting or transit holds)
2057 if( $resdate ) {
2058 $sql.= ' AND ( reservedate <= ? )';
2060 else {
2061 $sql.= ' AND ( reservedate < NOW() )';
2063 my $dbh = C4::Context->dbh();
2064 my @row = $dbh->selectrow_array(
2065 $sql,
2066 undef,
2067 $resdate ? ($biblionumber, $resdate) : ($biblionumber)
2070 return @row ? $row[0]+1 : 1;
2073 =head2 IsItemOnHoldAndFound
2075 my $bool = IsItemFoundHold( $itemnumber );
2077 Returns true if the item is currently on hold
2078 and that hold has a non-null found status ( W, T, etc. )
2080 =cut
2082 sub IsItemOnHoldAndFound {
2083 my ($itemnumber) = @_;
2085 my $rs = Koha::Database->new()->schema()->resultset('Reserve');
2087 my $found = $rs->count(
2089 itemnumber => $itemnumber,
2090 found => { '!=' => undef }
2094 return $found;
2097 =head2 GetMaxPatronHoldsForRecord
2099 my $holds_per_record = ReservesControlBranch( $borrowernumber, $biblionumber );
2101 For multiple holds on a given record for a given patron, the max
2102 number of record level holds that a patron can be placed is the highest
2103 value of the holds_per_record rule for each item if the record for that
2104 patron. This subroutine finds and returns the highest holds_per_record
2105 rule value for a given patron id and record id.
2107 =cut
2109 sub GetMaxPatronHoldsForRecord {
2110 my ( $borrowernumber, $biblionumber ) = @_;
2112 my $patron = Koha::Patrons->find($borrowernumber);
2113 my @items = Koha::Items->search( { biblionumber => $biblionumber } );
2115 my $controlbranch = C4::Context->preference('ReservesControlBranch');
2117 my $categorycode = $patron->categorycode;
2118 my $branchcode;
2119 $branchcode = $patron->branchcode if ( $controlbranch eq "PatronLibrary" );
2121 my $max = 0;
2122 foreach my $item (@items) {
2123 my $itemtype = $item->effective_itemtype();
2125 $branchcode = $item->homebranch if ( $controlbranch eq "ItemHomeLibrary" );
2127 my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
2128 my $holds_per_record = $rule ? $rule->{holds_per_record} : 0;
2129 $max = $holds_per_record if $holds_per_record > $max;
2132 return $max;
2135 =head2 GetHoldRule
2137 my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
2139 Returns the matching hold related issuingrule fields for a given
2140 patron category, itemtype, and library.
2142 =cut
2144 sub GetHoldRule {
2145 my ( $categorycode, $itemtype, $branchcode ) = @_;
2147 my $dbh = C4::Context->dbh;
2149 my $sth = $dbh->prepare(
2151 SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record, holds_per_day
2152 FROM issuingrules
2153 WHERE (categorycode in (?,'*') )
2154 AND (itemtype IN (?,'*'))
2155 AND (branchcode IN (?,'*'))
2156 ORDER BY categorycode DESC,
2157 itemtype DESC,
2158 branchcode DESC
2162 $sth->execute( $categorycode, $itemtype, $branchcode );
2164 return $sth->fetchrow_hashref();
2167 =head1 AUTHOR
2169 Koha Development Team <http://koha-community.org/>
2171 =cut