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