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