Bug 20724: Move the ReservesNeedReturns logic to AddReserve
[koha.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2
3 # Copyright Katipo Communications 2002
4 # Copyright Koha Development team 2012
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth;    # checkauth, getborrowernumber.
25 use C4::Koha;
26 use C4::Circulation;
27 use C4::Reserves;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Output;
31 use C4::Context;
32 use C4::Members;
33 use C4::Overdues;
34 use C4::Debug;
35
36 use Koha::AuthorisedValues;
37 use Koha::Biblios;
38 use Koha::DateUtils;
39 use Koha::Items;
40 use Koha::ItemTypes;
41 use Koha::Checkouts;
42 use Koha::Libraries;
43 use Koha::Patrons;
44 use Date::Calc qw/Today Date_to_Days/;
45 use List::MoreUtils qw/uniq/;
46
47 my $maxreserves = C4::Context->preference("maxreserves");
48
49 my $query = new CGI;
50
51 # if RequestOnOpac (for placing holds) is disabled, leave immediately
52 if ( ! C4::Context->preference('RequestOnOpac') ) {
53     print $query->redirect("/cgi-bin/koha/errors/404.pl");
54     exit;
55 }
56
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58     {
59         template_name   => "opac-reserve.tt",
60         query           => $query,
61         type            => "opac",
62         authnotrequired => 0,
63         debug           => 1,
64     }
65 );
66
67 my ($show_holds_count, $show_priority);
68 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
69     m/holds/o and $show_holds_count = 1;
70     m/priority/ and $show_priority = 1;
71 }
72
73 sub get_out {
74         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
75         exit;
76 }
77
78 my $patron = Koha::Patrons->find( $borrowernumber );
79
80 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
81 unless ( $can_place_hold_if_available_at_pickup ) {
82     my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
83     if ( @patron_categories ) {
84         my $categorycode = $patron->categorycode;
85         $can_place_hold_if_available_at_pickup = grep /^$categorycode$/, @patron_categories;
86     }
87 }
88
89 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
90 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
91
92     if ( $patron->is_expired ) {
93
94         # cannot reserve, their card has expired and the rules set mean this is not allowed
95         $template->param( message => 1, expired_patron => 1 );
96         get_out( $query, $cookie, $template->output );
97     }
98 }
99
100 # Pass through any reserve charge
101 my $reservefee = $patron->category->reservefee;
102 if ( $reservefee > 0){
103     $template->param( RESERVE_CHARGE => sprintf("%.2f",$reservefee));
104 }
105
106 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
107
108 # There are two ways of calling this script, with a single biblio num
109 # or multiple biblio nums.
110 my $biblionumbers = $query->param('biblionumbers');
111 my $reserveMode = $query->param('reserve_mode');
112 if ($reserveMode && ($reserveMode eq 'single')) {
113     my $bib = $query->param('single_bib');
114     $biblionumbers = "$bib/";
115 }
116 if (! $biblionumbers) {
117     $biblionumbers = $query->param('biblionumber');
118 }
119
120 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
121     $template->param(message=>1, no_biblionumber=>1);
122     &get_out($query, $cookie, $template->output);
123 }
124
125 # Pass the numbers to the page so they can be fed back
126 # when the hold is confirmed. TODO: Not necessary?
127 $template->param( biblionumbers => $biblionumbers );
128
129 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
130 my @biblionumbers = split /\//, $biblionumbers;
131 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
132     # TODO: New message?
133     $template->param(message=>1, no_biblionumber=>1);
134     &get_out($query, $cookie, $template->output);
135 }
136
137
138 # pass the pickup branch along....
139 my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
140 $template->param( branch => $branch );
141
142 # Is the person allowed to choose their branch
143 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
144
145 $template->param( choose_branch => $OPACChooseBranch);
146
147 #
148 #
149 # Build hashes of the requested biblio(item)s and items.
150 #
151 #
152
153 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
154 my %itemInfoHash; # Hash of itemnumber to item info.
155 foreach my $biblioNumber (@biblionumbers) {
156
157     my $biblioData = GetBiblioData($biblioNumber);
158     $biblioDataHash{$biblioNumber} = $biblioData;
159
160     my @itemInfos = GetItemsInfo($biblioNumber);
161
162     my $marcrecord= GetMarcBiblio({ biblionumber => $biblioNumber });
163
164     # flag indicating existence of at least one item linked via a host record
165     my $hostitemsflag;
166     # adding items linked via host biblios
167     my @hostitemInfos = GetHostItemsInfo($marcrecord);
168     if (@hostitemInfos){
169         $hostitemsflag =1;
170         push (@itemInfos,@hostitemInfos);
171     }
172
173     $biblioData->{itemInfos} = \@itemInfos;
174     foreach my $itemInfo (@itemInfos) {
175         $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
176     }
177
178     # Compute the priority rank.
179     my $biblio = Koha::Biblios->find( $biblioNumber );
180     my $holds = $biblio->holds;
181     my $rank = $holds->count;
182     $biblioData->{reservecount} = 1;    # new reserve
183     while ( my $hold = $holds->next ) {
184         if ( $hold->is_waiting ) {
185             $rank--;
186         }
187         else {
188             $biblioData->{reservecount}++;
189         }
190     }
191     $biblioData->{rank} = $rank + 1;
192 }
193
194 #
195 #
196 # If this is the second time through this script, it
197 # means we are carrying out the hold request, possibly
198 # with a specific item for each biblionumber.
199 #
200 #
201 if ( $query->param('place_reserve') ) {
202     my $reserve_cnt = 0;
203     if ($maxreserves) {
204         $reserve_cnt = $patron->holds->count;
205     }
206
207     # List is composed of alternating biblio/item/branch
208     my $selectedItems = $query->param('selecteditems');
209
210     if ($query->param('reserve_mode') eq 'single') {
211         # This indicates non-JavaScript mode, so there was
212         # only a single biblio number selected.
213         my $bib = $query->param('single_bib');
214         my $item = $query->param("checkitem_$bib");
215         if ($item eq 'any') {
216             $item = '';
217         }
218         my $branch = $query->param('branch');
219         $selectedItems = "$bib/$item/$branch/";
220     }
221
222     $selectedItems =~ s!/$!!;
223     my @selectedItems = split /\//, $selectedItems, -1;
224
225     # Make sure there is a biblionum/itemnum/branch triplet for each item.
226     # The itemnum can be 'any', meaning next available.
227     my $selectionCount = @selectedItems;
228     if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
229         $template->param(message=>1, bad_data=>1);
230         &get_out($query, $cookie, $template->output);
231     }
232
233     my $failed_holds = 0;
234     while (@selectedItems) {
235         my $biblioNum = shift(@selectedItems);
236         my $itemNum   = shift(@selectedItems);
237         my $branch    = shift(@selectedItems);    # i.e., branch code, not name
238
239         my $canreserve = 0;
240
241         my $singleBranchMode = Koha::Libraries->search->count == 1;
242         if ( $singleBranchMode || !$OPACChooseBranch )
243         {    # single branch mode or disabled user choosing
244             $branch = $patron->branchcode;
245         }
246
247 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
248         if ( $itemNum ne '' ) {
249             my $item = Koha::Items->find( $itemNum );
250             my $hostbiblioNum = $item->biblio->biblionumber;
251             if ( $hostbiblioNum ne $biblioNum ) {
252                 $biblioNum = $hostbiblioNum;
253             }
254         }
255
256         my $biblioData = $biblioDataHash{$biblioNum};
257         my $found;
258
259         # Check for user supplied reserve date
260         my $startdate;
261         if (   C4::Context->preference('AllowHoldDateInFuture')
262             && C4::Context->preference('OPACAllowHoldDateInFuture') )
263         {
264             $startdate = $query->param("reserve_date_$biblioNum");
265         }
266
267         my $expiration_date = $query->param("expiration_date_$biblioNum");
268
269       # If a specific item was selected and the pickup branch is the same as the
270       # holdingbranch, force the value $rank and $found.
271         my $rank = $biblioData->{rank};
272         if ( $itemNum ne '' ) {
273             $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
274         }
275         else {
276             $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
277
278             # Inserts a null into the 'itemnumber' field of 'reserves' table.
279             $itemNum = undef;
280         }
281         my $notes = $query->param('notes_'.$biblioNum)||'';
282
283         if (   $maxreserves
284             && $reserve_cnt >= $maxreserves )
285         {
286             $canreserve = 0;
287         }
288
289         unless ( $can_place_hold_if_available_at_pickup ) {
290             my $items_in_this_library = Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch });
291             my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
292             my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost => { '!=' => 0 }, damaged => { '!=' => 0 }, } });
293             if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
294                 $canreserve = 0
295             }
296         }
297
298         my $itemtype = $query->param('itemtype') || undef;
299         $itemtype = undef if $itemNum;
300
301         # Here we actually do the reserveration. Stage 3.
302         if ($canreserve) {
303             my $reserve_id = AddReserve(
304                 $branch,          $borrowernumber, $biblioNum,
305                 [$biblioNum],     $rank,           $startdate,
306                 $expiration_date, $notes,          $biblioData->{title},
307                 $itemNum,         $found,          $itemtype,
308             );
309             $failed_holds++ unless $reserve_id;
310             ++$reserve_cnt;
311         }
312     }
313
314     print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
315     exit;
316 }
317
318 #
319 #
320 # Here we check that the borrower can actually make reserves Stage 1.
321 #
322 #
323 my $noreserves     = 0;
324 my $maxoutstanding = C4::Context->preference("maxoutstanding");
325 $template->param( noreserve => 1 ) unless $maxoutstanding;
326 my $amountoutstanding = $patron ? $patron->account->balance : 0;
327
328 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
329     my $amount = sprintf "%.02f", $amountoutstanding;
330     $template->param( message => 1 );
331     $noreserves = 1;
332     $template->param( too_much_oweing => $amount );
333 }
334
335 if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
336     $noreserves = 1;
337     $template->param(
338         message => 1,
339         GNA     => 1
340     );
341 }
342
343 if ( $patron->lost && ($patron->lost == 1) ) {
344     $noreserves = 1;
345     $template->param(
346         message => 1,
347         lost    => 1
348     );
349 }
350
351 if ( $patron->is_debarred ) {
352     $noreserves = 1;
353     $template->param(
354         message          => 1,
355         debarred         => 1,
356         debarred_comment => $patron->debarredcomment,
357         debarred_date    => $patron->debarred,
358     );
359 }
360
361 my $holds = $patron->holds;
362 my $reserves_count = $holds->count;
363 $template->param( RESERVES => $holds->unblessed );
364 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
365     $template->param( message => 1 );
366     $noreserves = 1;
367     $template->param( too_many_reserves => $holds->count );
368 }
369
370 unless ( $noreserves ) {
371     my $requested_reserves_count = scalar( @biblionumbers );
372     if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
373         $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
374     }
375 }
376
377 unless ($noreserves) {
378     $template->param( select_item_types => 1 );
379 }
380
381
382 #
383 #
384 # Build the template parameters that will show the info
385 # and items for each biblionumber.
386 #
387 #
388
389 my $biblioLoop = [];
390 my $numBibsAvailable = 0;
391 my $itemdata_enumchron = 0;
392 my $anyholdable = 0;
393 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
394 $template->param('item_level_itypes' => $itemLevelTypes);
395
396 foreach my $biblioNum (@biblionumbers) {
397
398     my @not_available_at = ();
399     my $record = GetMarcBiblio({ biblionumber => $biblioNum });
400     # Init the bib item with the choices for branch pickup
401     my %biblioLoopIter;
402
403     # Get relevant biblio data.
404     my $biblioData = $biblioDataHash{$biblioNum};
405     if (! $biblioData) {
406         $template->param(message=>1, bad_biblionumber=>$biblioNum);
407         &get_out($query, $cookie, $template->output);
408     }
409
410     my $frameworkcode = GetFrameworkCode( $biblioData->{biblionumber} );
411     $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
412     $biblioLoopIter{title} = $biblioData->{title};
413     $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
414     $biblioLoopIter{author} = $biblioData->{author};
415     $biblioLoopIter{rank} = $biblioData->{rank};
416     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
417     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
418     $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
419
420     if (!$itemLevelTypes && $biblioData->{itemtype}) {
421         $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
422         $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$biblioData->{itemtype}}{imageurl};
423     }
424
425     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
426         if ($itemLevelTypes && $itemInfo->{itype}) {
427             $itemInfo->{translated_description} = $itemtypes->{$itemInfo->{itype}}{translated_description};
428             $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$itemInfo->{itype}}{imageurl};
429         }
430
431         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
432             $biblioLoopIter{forloan} = 1;
433         }
434     }
435
436     my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
437     my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
438
439     $biblioLoopIter{itemLoop} = [];
440     my $numCopiesAvailable = 0;
441     my $numCopiesOPACAvailable = 0;
442     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
443         my $itemNum = $itemInfo->{itemnumber};
444         my $itemLoopIter = {};
445
446         $itemLoopIter->{itemnumber} = $itemNum;
447         $itemLoopIter->{barcode} = $itemInfo->{barcode};
448         $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
449         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
450         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
451         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
452         if ($itemLevelTypes) {
453             $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
454             $itemLoopIter->{itype} = $itemInfo->{itype};
455             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
456         }
457
458         # If the holdingbranch is different than the homebranch, we show the
459         # holdingbranch of the document too.
460         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
461             $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
462         }
463
464         # If the item is currently on loan, we display its return date and
465         # change the background color.
466         my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
467         if ( $issue ) {
468             $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
469             $itemLoopIter->{backgroundcolor} = 'onloan';
470         }
471
472         # checking reserve
473         my $item = Koha::Items->find( $itemNum );
474         my $holds = $item->current_holds;
475
476         if ( my $first_hold = $holds->next ) {
477             my $patron = Koha::Patrons->find( $first_hold->borrowernumber );
478             $itemLoopIter->{backgroundcolor} = 'reserved';
479             $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
480             $itemLoopIter->{ReservedForBorrowernumber} = $first_hold->borrowernumber;
481             $itemLoopIter->{ReservedForSurname}        = $patron->surname;
482             $itemLoopIter->{ReservedForFirstname}      = $patron->firstname;
483             $itemLoopIter->{ExpectedAtLibrary}         = $first_hold->branchcode;
484             $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
485         }
486
487         $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
488         $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
489
490         # Management of the notforloan document
491         if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
492             $itemLoopIter->{backgroundcolor} = 'other';
493             $itemLoopIter->{notforloanvalue} =
494               $notforloan_label_of->{ $itemLoopIter->{notforloan} };
495         }
496
497         # Management of lost or long overdue items
498         if ( $itemInfo->{itemlost} ) {
499
500             # FIXME localized strings should never be in Perl code
501             $itemLoopIter->{message} =
502                 $itemInfo->{itemlost} == 1 ? "(lost)"
503               : $itemInfo->{itemlost} == 2 ? "(long overdue)"
504               : "";
505             $itemInfo->{backgroundcolor} = 'other';
506         }
507
508         # Check of the transfered documents
509         my ( $transfertwhen, $transfertfrom, $transfertto ) =
510           GetTransfers($itemNum);
511         if ( $transfertwhen && ($transfertwhen ne '') ) {
512             $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
513             $itemLoopIter->{transfertfrom} = $transfertfrom;
514             $itemLoopIter->{transfertto} = $transfertto;
515             $itemLoopIter->{nocancel} = 1;
516         }
517
518         # if the items belongs to a host record, show link to host record
519         if ( $itemInfo->{biblionumber} ne $biblioNum ) {
520             $biblioLoopIter{hostitemsflag}    = 1;
521             $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
522             $itemLoopIter->{hosttitle}        = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
523         }
524
525         # If there is no loan, return and transfer, we show a checkbox.
526         $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
527
528         my $patron_unblessed = $patron->unblessed;
529         my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
530
531         my $policy_holdallowed = !$itemLoopIter->{already_reserved};
532         $policy_holdallowed &&=
533             IsAvailableForItemLevelRequest($itemInfo,$patron_unblessed) &&
534             CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
535
536         if ($policy_holdallowed) {
537             if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $patron_unblessed ) ) {
538                 $itemLoopIter->{available} = 1;
539                 $numCopiesOPACAvailable++;
540                 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
541             }
542             $numCopiesAvailable++;
543
544             unless ( $can_place_hold_if_available_at_pickup ) {
545                 my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
546                 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
547                 if ( $items_in_this_library->count > $nb_of_items_issued ) {
548                     push @not_available_at, $itemInfo->{holdingbranch};
549                 }
550             }
551         }
552
553         $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
554
555     # Show serial enumeration when needed
556         if ($itemLoopIter->{enumchron}) {
557             $itemdata_enumchron = 1;
558         }
559
560         push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
561     }
562     $template->param( itemdata_enumchron => $itemdata_enumchron );
563
564     if ($numCopiesAvailable > 0) {
565         $numBibsAvailable++;
566         $biblioLoopIter{bib_available} = 1;
567         $biblioLoopIter{holdable} = 1;
568         $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
569     }
570     if ($biblioLoopIter{already_reserved}) {
571         $biblioLoopIter{holdable} = undef;
572         $biblioLoopIter{itemholdable} = undef;
573     }
574     if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
575         $biblioLoopIter{holdable} = undef;
576         $biblioLoopIter{already_patron_possession} = 1;
577     }
578
579     if ( $biblioLoopIter{holdable} ) {
580         @not_available_at = uniq @not_available_at;
581         $biblioLoopIter{not_available_at} = \@not_available_at ;
582     }
583
584     unless ( $can_place_hold_if_available_at_pickup ) {
585         @not_available_at = uniq @not_available_at;
586         $biblioLoopIter{not_available_at} = \@not_available_at ;
587         # The record is not holdable is not available at any of the libraries
588         if ( Koha::Libraries->search->count == @not_available_at ) {
589             $biblioLoopIter{holdable} = 0;
590         }
591     }
592
593     $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
594
595     # For multiple holds per record, if a patron has previously placed a hold,
596     # the patron can only place more holds of the same type. That is, if the
597     # patron placed a record level hold, all the holds the patron places must
598     # be record level. If the patron placed an item level hold, all holds
599     # the patron places must be item level
600     my $forced_hold_level = Koha::Holds->search(
601         {
602             borrowernumber => $borrowernumber,
603             biblionumber   => $biblioNum,
604             found          => undef,
605         }
606     )->forced_hold_level();
607     if ($forced_hold_level) {
608         $biblioLoopIter{force_hold}   = 1 if $forced_hold_level eq 'item';
609         $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
610     }
611
612
613     push @$biblioLoop, \%biblioLoopIter;
614
615     $anyholdable = 1 if $biblioLoopIter{holdable};
616 }
617
618
619 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
620     $template->param( none_available => 1 );
621 }
622
623 my $show_notes=C4::Context->preference('OpacHoldNotes');
624 $template->param(OpacHoldNotes=>$show_notes);
625
626 # display infos
627 $template->param(bibitemloop => $biblioLoop);
628 # can set reserve date in future
629 if (
630     C4::Context->preference( 'AllowHoldDateInFuture' ) &&
631     C4::Context->preference( 'OPACAllowHoldDateInFuture' )
632     ) {
633     $template->param(
634             reserve_in_future         => 1,
635     );
636 }
637
638 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };