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