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