Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2
3 # Copyright Katipo Communications 2002
4 # Copyright Koha Development team 2012
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth;    # checkauth, getborrowernumber.
25 use C4::Koha;
26 use C4::Circulation;
27 use C4::Reserves;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Output;
31 use C4::Context;
32 use C4::Members;
33 use C4::Overdues;
34 use C4::Debug;
35
36 use Koha::AuthorisedValues;
37 use Koha::Biblios;
38 use Koha::DateUtils;
39 use Koha::IssuingRules;
40 use Koha::Items;
41 use Koha::ItemTypes;
42 use Koha::Checkouts;
43 use Koha::Libraries;
44 use Koha::Patrons;
45 use Date::Calc qw/Today Date_to_Days/;
46 use List::MoreUtils qw/uniq/;
47
48 my $maxreserves = C4::Context->preference("maxreserves");
49
50 my $query = new CGI;
51
52 # if RequestOnOpac (for placing holds) is disabled, leave immediately
53 if ( ! C4::Context->preference('RequestOnOpac') ) {
54     print $query->redirect("/cgi-bin/koha/errors/404.pl");
55     exit;
56 }
57
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
59     {
60         template_name   => "opac-reserve.tt",
61         query           => $query,
62         type            => "opac",
63         authnotrequired => 0,
64         debug           => 1,
65     }
66 );
67
68 my ($show_holds_count, $show_priority);
69 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
70     m/holds/o and $show_holds_count = 1;
71     m/priority/ and $show_priority = 1;
72 }
73
74 my $patron = Koha::Patrons->find( $borrowernumber );
75
76 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
77 unless ( $can_place_hold_if_available_at_pickup ) {
78     my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
79     if ( @patron_categories ) {
80         my $categorycode = $patron->categorycode;
81         $can_place_hold_if_available_at_pickup = grep /^$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         output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
93     }
94 }
95
96 # Pass through any reserve charge
97 my $reservefee = $patron->category->reservefee;
98 if ( $reservefee > 0){
99     $template->param( RESERVE_CHARGE => $reservefee);
100 }
101
102 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
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     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
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     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
131 }
132
133
134 # pass the pickup branch along....
135 my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
136 $template->param( branch => $branch );
137
138 #
139 #
140 # Build hashes of the requested biblio(item)s and items.
141 #
142 #
143
144 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
145 my %itemInfoHash; # Hash of itemnumber to item info.
146 foreach my $biblioNumber (@biblionumbers) {
147
148     my $biblioData = GetBiblioData($biblioNumber);
149     $biblioDataHash{$biblioNumber} = $biblioData;
150
151     my @itemInfos = GetItemsInfo($biblioNumber);
152
153     my $marcrecord= GetMarcBiblio({ biblionumber => $biblioNumber });
154
155     # flag indicating existence of at least one item linked via a host record
156     my $hostitemsflag;
157     # adding items linked via host biblios
158     my @hostitemInfos = GetHostItemsInfo($marcrecord);
159     if (@hostitemInfos){
160         $hostitemsflag =1;
161         push (@itemInfos,@hostitemInfos);
162     }
163
164     $biblioData->{itemInfos} = \@itemInfos;
165     foreach my $itemInfo (@itemInfos) {
166         $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
167     }
168
169     # Compute the priority rank.
170     my $biblio = Koha::Biblios->find( $biblioNumber );
171     $biblioData->{object} = $biblio;
172     my $holds = $biblio->holds;
173     my $rank = $holds->count;
174     $biblioData->{reservecount} = 1;    # new reserve
175     while ( my $hold = $holds->next ) {
176         if ( $hold->is_waiting ) {
177             $rank--;
178         }
179         else {
180             $biblioData->{reservecount}++;
181         }
182     }
183     $biblioData->{rank} = $rank + 1;
184 }
185
186 #
187 #
188 # If this is the second time through this script, it
189 # means we are carrying out the hold request, possibly
190 # with a specific item for each biblionumber.
191 #
192 #
193 if ( $query->param('place_reserve') ) {
194     my $reserve_cnt = 0;
195     if ($maxreserves) {
196         $reserve_cnt = $patron->holds->count;
197     }
198
199     # List is composed of alternating biblio/item/branch
200     my $selectedItems = $query->param('selecteditems');
201
202     if ($query->param('reserve_mode') eq 'single') {
203         # This indicates non-JavaScript mode, so there was
204         # only a single biblio number selected.
205         my $bib = $query->param('single_bib');
206         my $item = $query->param("checkitem_$bib");
207         if ($item eq 'any') {
208             $item = '';
209         }
210         my $branch = $query->param('branch');
211         $selectedItems = "$bib/$item/$branch/";
212     }
213
214     $selectedItems =~ s!/$!!;
215     my @selectedItems = split /\//, $selectedItems, -1;
216
217     # Make sure there is a biblionum/itemnum/branch triplet for each item.
218     # The itemnum can be 'any', meaning next available.
219     my $selectionCount = @selectedItems;
220     if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
221         $template->param(message=>1, bad_data=>1);
222         output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
223     }
224
225     my $failed_holds = 0;
226     while (@selectedItems) {
227         my $biblioNum = shift(@selectedItems);
228         my $itemNum   = shift(@selectedItems);
229         my $branch    = shift(@selectedItems);    # i.e., branch code, not name
230
231         my $canreserve = 0;
232
233         my $singleBranchMode = Koha::Libraries->search->count == 1;
234         if ( $singleBranchMode || ! C4::Context->preference("OPACAllowUserToChooseBranch") )
235         {    # single branch mode or disabled user choosing
236             $branch = $patron->branchcode;
237         }
238
239         # When choosing a specific item, the default pickup library should be dictated by the default hold policy
240         if ( ! C4::Context->preference("OPACAllowUserToChooseBranch") && $itemNum ) {
241             my $item = Koha::Items->find( $itemNum );
242             my $type = $item->effective_itemtype;
243             my $rule = GetBranchItemRule( $patron->branchcode, $type );
244
245             if ( $rule->{hold_fulfillment_policy} eq 'any' ) {
246                 $branch = $patron->branchcode;
247             } else {
248                 my $policy = $rule->{hold_fulfillment_policy};
249                 $branch = $item->$policy;
250             }
251         }
252
253 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
254         if ( $itemNum ne '' ) {
255             my $item = Koha::Items->find( $itemNum );
256             my $hostbiblioNum = $item->biblio->biblionumber;
257             if ( $hostbiblioNum ne $biblioNum ) {
258                 $biblioNum = $hostbiblioNum;
259             }
260         }
261
262         my $biblioData = $biblioDataHash{$biblioNum};
263         my $found;
264
265         # Check for user supplied reserve date
266         my $startdate;
267         if (   C4::Context->preference('AllowHoldDateInFuture')
268             && C4::Context->preference('OPACAllowHoldDateInFuture') )
269         {
270             $startdate = $query->param("reserve_date_$biblioNum");
271         }
272
273         my $expiration_date = $query->param("expiration_date_$biblioNum");
274
275         my $rank = $biblioData->{rank};
276         if ( $itemNum ne '' ) {
277             $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
278         }
279         else {
280             $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum )->{status} 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 = $patron->account->balance;
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 ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
339     $noreserves = 1;
340     $template->param(
341         message => 1,
342         GNA     => 1
343     );
344 }
345
346 if ( $patron->lost && ($patron->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 => $patron->debarredcomment,
360         debarred_date    => $patron->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 $itemdata_ccode = 0;
396 my $anyholdable = 0;
397 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
398 my $pickup_locations = Koha::Libraries->search({ pickup_location => 1 });
399 $template->param('item_level_itypes' => $itemLevelTypes);
400
401 foreach my $biblioNum (@biblionumbers) {
402
403     my $record = GetMarcBiblio({ biblionumber => $biblioNum });
404     # Init the bib item with the choices for branch pickup
405     my %biblioLoopIter;
406
407     # Get relevant biblio data.
408     my $biblioData = $biblioDataHash{$biblioNum};
409     if (! $biblioData) {
410         $template->param(message=>1, bad_biblionumber=>$biblioNum);
411         output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
412     }
413
414     my @not_available_at = ();
415     my $biblio = $biblioData->{object};
416     foreach my $library ( $pickup_locations->as_list ) {
417         push( @not_available_at, $library->branchcode ) unless $biblio->can_be_transferred({ to => $library });
418     }
419
420     my $frameworkcode = GetFrameworkCode( $biblioData->{biblionumber} );
421     $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
422     $biblioLoopIter{title} = $biblioData->{title};
423     $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
424     $biblioLoopIter{author} = $biblioData->{author};
425     $biblioLoopIter{rank} = $biblioData->{rank};
426     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
427     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
428     $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
429
430     if (!$itemLevelTypes && $biblioData->{itemtype}) {
431         $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
432         $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$biblioData->{itemtype}}{imageurl};
433     }
434
435     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
436         if ($itemLevelTypes && $itemInfo->{itype}) {
437             $itemInfo->{translated_description} = $itemtypes->{$itemInfo->{itype}}{translated_description};
438             $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$itemInfo->{itype}}{imageurl};
439         }
440
441         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
442             $biblioLoopIter{forloan} = 1;
443         }
444     }
445
446     my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
447     my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
448
449     $biblioLoopIter{itemLoop} = [];
450     my $numCopiesAvailable = 0;
451     my $numCopiesOPACAvailable = 0;
452     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
453         my $itemNum = $itemInfo->{itemnumber};
454         my $item = Koha::Items->find( $itemNum );
455         my $itemLoopIter = {};
456
457         $itemLoopIter->{itemnumber} = $itemNum;
458         $itemLoopIter->{barcode} = $itemInfo->{barcode};
459         $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
460         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
461         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
462         $itemLoopIter->{ccode} = $itemInfo->{ccode};
463         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
464         if ($itemLevelTypes) {
465             $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
466             $itemLoopIter->{itype} = $itemInfo->{itype};
467             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
468         }
469
470         # If the holdingbranch is different than the homebranch, we show the
471         # holdingbranch of the document too.
472         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
473             $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
474         }
475
476         # If the item is currently on loan, we display its return date and
477         # change the background color.
478         my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
479         if ( $issue ) {
480             $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
481             $itemLoopIter->{backgroundcolor} = 'onloan';
482         }
483
484         # checking reserve
485         my $holds = $item->current_holds;
486
487         if ( my $first_hold = $holds->next ) {
488             $itemLoopIter->{backgroundcolor} = 'reserved';
489             $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
490             $itemLoopIter->{ExpectedAtLibrary}         = $first_hold->branchcode;
491             $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
492         }
493
494         $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
495         $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
496
497         # Management of the notforloan document
498         if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
499             $itemLoopIter->{backgroundcolor} = 'other';
500             $itemLoopIter->{notforloanvalue} =
501               $notforloan_label_of->{ $itemLoopIter->{notforloan} };
502         }
503
504         # Management of lost or long overdue items
505         if ( $itemInfo->{itemlost} ) {
506
507             # FIXME localized strings should never be in Perl code
508             $itemLoopIter->{message} =
509                 $itemInfo->{itemlost} == 1 ? "(lost)"
510               : $itemInfo->{itemlost} == 2 ? "(long overdue)"
511               : "";
512             $itemInfo->{backgroundcolor} = 'other';
513         }
514
515         # Check of the transferred documents
516         my ( $transfertwhen, $transfertfrom, $transfertto ) =
517           GetTransfers($itemNum);
518         if ( $transfertwhen && ($transfertwhen ne '') ) {
519             $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
520             $itemLoopIter->{transfertfrom} = $transfertfrom;
521             $itemLoopIter->{transfertto} = $transfertto;
522             $itemLoopIter->{nocancel} = 1;
523         }
524
525         # if the items belongs to a host record, show link to host record
526         if ( $itemInfo->{biblionumber} ne $biblioNum ) {
527             $biblioLoopIter{hostitemsflag}    = 1;
528             $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
529             $itemLoopIter->{hosttitle}        = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
530         }
531
532         # If there is no loan, return and transfer, we show a checkbox.
533         $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
534
535         my $patron_unblessed = $patron->unblessed;
536         my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
537
538         my $policy_holdallowed = !$itemLoopIter->{already_reserved};
539         $policy_holdallowed &&=
540             IsAvailableForItemLevelRequest($item, $patron) &&
541             CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
542
543         if ($policy_holdallowed) {
544             my $opac_hold_policy = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
545             if ( $opac_hold_policy ne 'N' ) { # If Y or F
546                 $itemLoopIter->{available} = 1;
547                 $numCopiesOPACAvailable++;
548                 $biblioLoopIter{force_hold} = 1 if $opac_hold_policy eq 'F';
549             }
550             $numCopiesAvailable++;
551
552             unless ( $can_place_hold_if_available_at_pickup ) {
553                 my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
554                 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
555                 if ( $items_in_this_library->count > $nb_of_items_issued ) {
556                     push @not_available_at, $itemInfo->{holdingbranch};
557                 }
558             }
559         }
560
561         $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
562
563     # Show serial enumeration when needed
564         if ($itemLoopIter->{enumchron}) {
565             $itemdata_enumchron = 1;
566         }
567     # Show collection when needed
568         if ($itemLoopIter->{ccode}) {
569             $itemdata_ccode = 1;
570         }
571
572         push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
573     }
574     $template->param(
575         itemdata_enumchron => $itemdata_enumchron,
576         itemdata_ccode     => $itemdata_ccode,
577     );
578
579     if ($numCopiesAvailable > 0) {
580         $numBibsAvailable++;
581         $biblioLoopIter{bib_available} = 1;
582         $biblioLoopIter{holdable} = 1;
583         $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
584     }
585     if ($biblioLoopIter{already_reserved}) {
586         $biblioLoopIter{holdable} = undef;
587         $biblioLoopIter{itemholdable} = undef;
588     }
589     if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
590         $biblioLoopIter{holdable} = undef;
591         $biblioLoopIter{already_patron_possession} = 1;
592     }
593
594     if ( $biblioLoopIter{holdable} ) {
595         @not_available_at = uniq @not_available_at;
596         $biblioLoopIter{not_available_at} = \@not_available_at ;
597     }
598
599     unless ( $can_place_hold_if_available_at_pickup ) {
600         @not_available_at = uniq @not_available_at;
601         $biblioLoopIter{not_available_at} = \@not_available_at ;
602         # The record is not holdable is not available at any of the libraries
603         if ( Koha::Libraries->search->count == @not_available_at ) {
604             $biblioLoopIter{holdable} = 0;
605         }
606     }
607
608     $biblioLoopIter{holdable} &&= CanBookBeReserved( $borrowernumber, $biblioNum )->{status} eq 'OK';
609
610     # For multiple holds per record, if a patron has previously placed a hold,
611     # the patron can only place more holds of the same type. That is, if the
612     # patron placed a record level hold, all the holds the patron places must
613     # be record level. If the patron placed an item level hold, all holds
614     # the patron places must be item level
615     my $forced_hold_level = Koha::Holds->search(
616         {
617             borrowernumber => $borrowernumber,
618             biblionumber   => $biblioNum,
619             found          => undef,
620         }
621     )->forced_hold_level();
622     if ($forced_hold_level) {
623         $biblioLoopIter{force_hold}   = 1 if $forced_hold_level eq 'item';
624         $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
625     }
626
627
628     push @$biblioLoop, \%biblioLoopIter;
629
630     $anyholdable = 1 if $biblioLoopIter{holdable};
631 }
632
633 unless ($pickup_locations->count) {
634     $numBibsAvailable = 0;
635     $anyholdable = 0;
636     $template->param(
637         message => 1,
638         no_pickup_locations => 1
639     );
640 }
641
642 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
643     $template->param( none_available => 1 );
644 }
645
646 if (scalar @biblionumbers > 1) {
647     $template->param( multi_hold => 1);
648 }
649
650 my $show_notes=C4::Context->preference('OpacHoldNotes');
651 $template->param(OpacHoldNotes=>$show_notes);
652
653 # display infos
654 $template->param(bibitemloop => $biblioLoop);
655 # can set reserve date in future
656 if (
657     C4::Context->preference( 'AllowHoldDateInFuture' ) &&
658     C4::Context->preference( 'OPACAllowHoldDateInFuture' )
659     ) {
660     $template->param(
661             reserve_in_future         => 1,
662     );
663 }
664
665 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };