650326f7d3ca37c401bef35bd1f21526a24ee24c
[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::Libraries;
38 use Koha::Patrons;
39 use Date::Calc qw/Today Date_to_Days/;
40 use List::MoreUtils qw/uniq/;
41
42 my $maxreserves = C4::Context->preference("maxreserves");
43
44 my $query = new CGI;
45
46 # if RequestOnOpac (for placing holds) is disabled, leave immediately
47 if ( ! C4::Context->preference('RequestOnOpac') ) {
48     print $query->redirect("/cgi-bin/koha/errors/404.pl");
49     exit;
50 }
51
52 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
53     {
54         template_name   => "opac-reserve.tt",
55         query           => $query,
56         type            => "opac",
57         authnotrequired => 0,
58         debug           => 1,
59     }
60 );
61
62 my ($show_holds_count, $show_priority);
63 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
64     m/holds/o and $show_holds_count = 1;
65     m/priority/ and $show_priority = 1;
66 }
67
68 sub get_out {
69         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
70         exit;
71 }
72
73 # get borrower information ....
74 my ( $borr ) = GetMember( borrowernumber => $borrowernumber );
75 my $patron = Koha::Patrons->find( $borrowernumber );
76
77 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
78 unless ( $can_place_hold_if_available_at_pickup ) {
79     my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
80     if ( @patron_categories ) {
81         $can_place_hold_if_available_at_pickup = grep /$borr->{categorycode}/, @patron_categories;
82     }
83 }
84
85 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
86 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
87
88     if ( $patron->is_expired ) {
89
90         # cannot reserve, their card has expired and the rules set mean this is not allowed
91         $template->param( message => 1, expired_patron => 1 );
92         get_out( $query, $cookie, $template->output );
93     }
94 }
95
96 # Pass through any reserve charge
97 my $reservefee = $patron->category->reservefee;
98 if ( $reservefee > 0){
99     $template->param( RESERVE_CHARGE => sprintf("%.2f",$reservefee));
100 }
101
102 my $itemTypes = GetItemTypes();
103
104 # There are two ways of calling this script, with a single biblio num
105 # or multiple biblio nums.
106 my $biblionumbers = $query->param('biblionumbers');
107 my $reserveMode = $query->param('reserve_mode');
108 if ($reserveMode && ($reserveMode eq 'single')) {
109     my $bib = $query->param('single_bib');
110     $biblionumbers = "$bib/";
111 }
112 if (! $biblionumbers) {
113     $biblionumbers = $query->param('biblionumber');
114 }
115
116 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
117     $template->param(message=>1, no_biblionumber=>1);
118     &get_out($query, $cookie, $template->output);
119 }
120
121 # Pass the numbers to the page so they can be fed back
122 # when the hold is confirmed. TODO: Not necessary?
123 $template->param( biblionumbers => $biblionumbers );
124
125 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
126 my @biblionumbers = split /\//, $biblionumbers;
127 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
128     # TODO: New message?
129     $template->param(message=>1, no_biblionumber=>1);
130     &get_out($query, $cookie, $template->output);
131 }
132
133
134 # pass the pickup branch along....
135 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
136 $template->param( branch => $branch );
137
138 # Is the person allowed to choose their branch
139 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
140
141 $template->param( choose_branch => $OPACChooseBranch);
142
143 #
144 #
145 # Build hashes of the requested biblio(item)s and items.
146 #
147 #
148
149 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
150 my %itemInfoHash; # Hash of itemnumber to item info.
151 foreach my $biblioNumber (@biblionumbers) {
152
153     my $biblioData = GetBiblioData($biblioNumber);
154     $biblioDataHash{$biblioNumber} = $biblioData;
155
156     my @itemInfos = GetItemsInfo($biblioNumber);
157
158     my $marcrecord= GetMarcBiblio($biblioNumber);
159
160     # flag indicating existence of at least one item linked via a host record
161     my $hostitemsflag;
162     # adding items linked via host biblios
163     my @hostitemInfos = GetHostItemsInfo($marcrecord);
164     if (@hostitemInfos){
165         $hostitemsflag =1;
166         push (@itemInfos,@hostitemInfos);
167     }
168
169     $biblioData->{itemInfos} = \@itemInfos;
170     foreach my $itemInfo (@itemInfos) {
171         $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
172     }
173
174     # Compute the priority rank.
175     my $biblio = Koha::Biblios->find( $biblioNumber );
176     my $holds = $biblio->holds;
177     my $rank = $holds->count;
178     $biblioData->{reservecount} = 1;    # new reserve
179     while ( my $hold = $holds->next ) {
180         if ( $hold->is_waiting ) {
181             $rank--;
182         }
183         else {
184             $biblioData->{reservecount}++;
185         }
186     }
187     $biblioData->{rank} = $rank + 1;
188 }
189
190 #
191 #
192 # If this is the second time through this script, it
193 # means we are carrying out the hold request, possibly
194 # with a specific item for each biblionumber.
195 #
196 #
197 if ( $query->param('place_reserve') ) {
198     my $reserve_cnt = 0;
199     if ($maxreserves) {
200         $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
201     }
202
203     # List is composed of alternating biblio/item/branch
204     my $selectedItems = $query->param('selecteditems');
205
206     if ($query->param('reserve_mode') eq 'single') {
207         # This indicates non-JavaScript mode, so there was
208         # only a single biblio number selected.
209         my $bib = $query->param('single_bib');
210         my $item = $query->param("checkitem_$bib");
211         if ($item eq 'any') {
212             $item = '';
213         }
214         my $branch = $query->param('branch');
215         $selectedItems = "$bib/$item/$branch/";
216     }
217
218     $selectedItems =~ s!/$!!;
219     my @selectedItems = split /\//, $selectedItems, -1;
220
221     # Make sure there is a biblionum/itemnum/branch triplet for each item.
222     # The itemnum can be 'any', meaning next available.
223     my $selectionCount = @selectedItems;
224     if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
225         $template->param(message=>1, bad_data=>1);
226         &get_out($query, $cookie, $template->output);
227     }
228
229     my $failed_holds = 0;
230     while (@selectedItems) {
231         my $biblioNum = shift(@selectedItems);
232         my $itemNum   = shift(@selectedItems);
233         my $branch    = shift(@selectedItems);    # i.e., branch code, not name
234
235         my $canreserve = 0;
236
237         my $singleBranchMode = Koha::Libraries->search->count == 1;
238         if ( $singleBranchMode || !$OPACChooseBranch )
239         {    # single branch mode or disabled user choosing
240             $branch = $borr->{'branchcode'};
241         }
242
243 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
244         if ( $itemNum ne '' ) {
245             my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
246             if ( $hostbiblioNum ne $biblioNum ) {
247                 $biblioNum = $hostbiblioNum;
248             }
249         }
250
251         my $biblioData = $biblioDataHash{$biblioNum};
252         my $found;
253
254         # Check for user supplied reserve date
255         my $startdate;
256         if (   C4::Context->preference('AllowHoldDateInFuture')
257             && C4::Context->preference('OPACAllowHoldDateInFuture') )
258         {
259             $startdate = $query->param("reserve_date_$biblioNum");
260         }
261
262         my $expiration_date = $query->param("expiration_date_$biblioNum");
263
264       # If a specific item was selected and the pickup branch is the same as the
265       # holdingbranch, force the value $rank and $found.
266         my $rank = $biblioData->{rank};
267         if ( $itemNum ne '' ) {
268             $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
269             $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
270             my $item = GetItem($itemNum);
271             if ( $item->{'holdingbranch'} eq $branch ) {
272                 $found = 'W'
273                   unless C4::Context->preference('ReservesNeedReturns');
274             }
275         }
276         else {
277             $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
278
279             # Inserts a null into the 'itemnumber' field of 'reserves' table.
280             $itemNum = undef;
281         }
282         my $notes = $query->param('notes_'.$biblioNum)||'';
283
284         if (   $maxreserves
285             && $reserve_cnt >= $maxreserves )
286         {
287             $canreserve = 0;
288         }
289
290         unless ( $can_place_hold_if_available_at_pickup ) {
291             my $items_in_this_library = Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch });
292             my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
293             my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost => { '!=' => 0 }, damaged => { '!=' => 0 }, } });
294             if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
295                 $canreserve = 0
296             }
297         }
298
299         my $itemtype = $query->param('itemtype') || undef;
300         $itemtype = undef if $itemNum;
301
302         # Here we actually do the reserveration. Stage 3.
303         if ($canreserve) {
304             my $reserve_id = AddReserve(
305                 $branch,          $borrowernumber, $biblioNum,
306                 [$biblioNum],     $rank,           $startdate,
307                 $expiration_date, $notes,          $biblioData->{title},
308                 $itemNum,         $found,          $itemtype,
309             );
310             $failed_holds++ unless $reserve_id;
311             ++$reserve_cnt;
312         }
313     }
314
315     print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
316     exit;
317 }
318
319 #
320 #
321 # Here we check that the borrower can actually make reserves Stage 1.
322 #
323 #
324 my $noreserves     = 0;
325 my $maxoutstanding = C4::Context->preference("maxoutstanding");
326 $template->param( noreserve => 1 ) unless $maxoutstanding;
327 my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
328 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
329     my $amount = sprintf "%.02f", $amountoutstanding;
330     $template->param( message => 1 );
331     $noreserves = 1;
332     $template->param( too_much_oweing => $amount );
333 }
334
335 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
336     $noreserves = 1;
337     $template->param(
338         message => 1,
339         GNA     => 1
340     );
341 }
342
343 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
344     $noreserves = 1;
345     $template->param(
346         message => 1,
347         lost    => 1
348     );
349 }
350
351 if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
352     $noreserves = 1;
353     $template->param(
354         message          => 1,
355         debarred         => 1,
356         debarred_comment => $borr->{debarredcomment},
357         debarred_date    => $borr->{debarred},
358     );
359 }
360
361 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
362 my $reserves_count = scalar(@reserves);
363 $template->param( RESERVES => \@reserves );
364 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
365     $template->param( message => 1 );
366     $noreserves = 1;
367     $template->param( too_many_reserves => scalar(@reserves));
368 }
369
370 unless ( $noreserves ) {
371     my $requested_reserves_count = scalar( @biblionumbers );
372     if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
373         $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
374     }
375 }
376
377 unless ($noreserves) {
378     $template->param( select_item_types => 1 );
379 }
380
381
382 #
383 #
384 # Build the template parameters that will show the info
385 # and items for each biblionumber.
386 #
387 #
388
389 my $biblioLoop = [];
390 my $numBibsAvailable = 0;
391 my $itemdata_enumchron = 0;
392 my $anyholdable = 0;
393 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
394 $template->param('item_level_itypes' => $itemLevelTypes);
395
396 foreach my $biblioNum (@biblionumbers) {
397
398     my @not_available_at = ();
399     my $record = GetMarcBiblio($biblioNum);
400     # Init the bib item with the choices for branch pickup
401     my %biblioLoopIter;
402
403     # Get relevant biblio data.
404     my $biblioData = $biblioDataHash{$biblioNum};
405     if (! $biblioData) {
406         $template->param(message=>1, bad_biblionumber=>$biblioNum);
407         &get_out($query, $cookie, $template->output);
408     }
409
410     my $frameworkcode = GetFrameworkCode( $biblioData->{biblionumber} );
411     $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
412     $biblioLoopIter{title} = $biblioData->{title};
413     $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
414     $biblioLoopIter{author} = $biblioData->{author};
415     $biblioLoopIter{rank} = $biblioData->{rank};
416     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
417     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
418     $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
419
420     if (!$itemLevelTypes && $biblioData->{itemtype}) {
421         $biblioLoopIter{translated_description} = $itemTypes->{$biblioData->{itemtype}}{translated_description};
422         $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
423     }
424
425     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
426         if ($itemLevelTypes && $itemInfo->{itype}) {
427             $itemInfo->{translated_description} = $itemTypes->{$itemInfo->{itype}}{translated_description};
428             $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
429         }
430
431         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
432             $biblioLoopIter{forloan} = 1;
433         }
434     }
435
436     my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
437     my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
438
439     $biblioLoopIter{itemLoop} = [];
440     my $numCopiesAvailable = 0;
441     my $numCopiesOPACAvailable = 0;
442     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
443         my $itemNum = $itemInfo->{itemnumber};
444         my $itemLoopIter = {};
445
446         $itemLoopIter->{itemnumber} = $itemNum;
447         $itemLoopIter->{barcode} = $itemInfo->{barcode};
448         $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
449         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
450         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
451         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
452         if ($itemLevelTypes) {
453             $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
454             $itemLoopIter->{itype} = $itemInfo->{itype};
455             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
456         }
457
458         # If the holdingbranch is different than the homebranch, we show the
459         # holdingbranch of the document too.
460         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
461             $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
462         }
463
464         # If the item is currently on loan, we display its return date and
465         # change the background color.
466         my $issues= GetItemIssue($itemNum);
467         if ( $issues->{'date_due'} ) {
468             $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
469             $itemLoopIter->{backgroundcolor} = 'onloan';
470         }
471
472         # checking reserve
473         my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
474         my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
475
476         # the item could be reserved for this borrower vi a host record, flag this
477         $reservedfor //= '';
478
479         if ( defined $reservedate ) {
480             $itemLoopIter->{backgroundcolor} = 'reserved';
481             $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($reservedate), dateonly => 1 });
482             $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
483             $itemLoopIter->{ReservedForSurname}        = $ItemBorrowerReserveInfo->{'surname'};
484             $itemLoopIter->{ReservedForFirstname}      = $ItemBorrowerReserveInfo->{'firstname'};
485             $itemLoopIter->{ExpectedAtLibrary}         = $expectedAt;
486             #waiting status
487             $itemLoopIter->{waitingdate} = $wait;
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 };