Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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
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 sub get_out {
75         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
76         exit;
77 }
78
79 my $patron = Koha::Patrons->find( $borrowernumber );
80
81 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
82 unless ( $can_place_hold_if_available_at_pickup ) {
83     my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
84     if ( @patron_categories ) {
85         my $categorycode = $patron->categorycode;
86         $can_place_hold_if_available_at_pickup = grep /^$categorycode$/, @patron_categories;
87     }
88 }
89
90 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
91 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
92
93     if ( $patron->is_expired ) {
94
95         # cannot reserve, their card has expired and the rules set mean this is not allowed
96         $template->param( message => 1, expired_patron => 1 );
97         get_out( $query, $cookie, $template->output );
98     }
99 }
100
101 # Pass through any reserve charge
102 my $reservefee = $patron->category->reservefee;
103 if ( $reservefee > 0){
104     $template->param( RESERVE_CHARGE => $reservefee);
105 }
106
107 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
108
109 # There are two ways of calling this script, with a single biblio num
110 # or multiple biblio nums.
111 my $biblionumbers = $query->param('biblionumbers');
112 my $reserveMode = $query->param('reserve_mode');
113 if ($reserveMode && ($reserveMode eq 'single')) {
114     my $bib = $query->param('single_bib');
115     $biblionumbers = "$bib/";
116 }
117 if (! $biblionumbers) {
118     $biblionumbers = $query->param('biblionumber');
119 }
120
121 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
122     $template->param(message=>1, no_biblionumber=>1);
123     &get_out($query, $cookie, $template->output);
124 }
125
126 # Pass the numbers to the page so they can be fed back
127 # when the hold is confirmed. TODO: Not necessary?
128 $template->param( biblionumbers => $biblionumbers );
129
130 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
131 my @biblionumbers = split /\//, $biblionumbers;
132 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
133     # TODO: New message?
134     $template->param(message=>1, no_biblionumber=>1);
135     &get_out($query, $cookie, $template->output);
136 }
137
138
139 # pass the pickup branch along....
140 my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
141 $template->param( branch => $branch );
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 => $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     $biblioData->{object} = $biblio;
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 = $patron->holds->count;
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 || ! C4::Context->preference("OPACAllowUserToChooseBranch") )
240         {    # single branch mode or disabled user choosing
241             $branch = $patron->branchcode;
242         }
243
244         # When choosing a specific item, the default pickup library should be dictated by the default hold policy
245         if ( ! C4::Context->preference("OPACAllowUserToChooseBranch") && $itemNum ) {
246             my $item = Koha::Items->find( $itemNum );
247             my $type = $item->effective_itemtype;
248             my $rule = GetBranchItemRule( $patron->branchcode, $type );
249
250             if ( $rule->{hold_fulfillment_policy} eq 'any' ) {
251                 $branch = $patron->branchcode;
252             } else {
253                 my $policy = $rule->{hold_fulfillment_policy};
254                 $branch = $item->$policy;
255             }
256         }
257
258 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
259         if ( $itemNum ne '' ) {
260             my $item = Koha::Items->find( $itemNum );
261             my $hostbiblioNum = $item->biblio->biblionumber;
262             if ( $hostbiblioNum ne $biblioNum ) {
263                 $biblioNum = $hostbiblioNum;
264             }
265         }
266
267         my $biblioData = $biblioDataHash{$biblioNum};
268         my $found;
269
270         # Check for user supplied reserve date
271         my $startdate;
272         if (   C4::Context->preference('AllowHoldDateInFuture')
273             && C4::Context->preference('OPACAllowHoldDateInFuture') )
274         {
275             $startdate = $query->param("reserve_date_$biblioNum");
276         }
277
278         my $expiration_date = $query->param("expiration_date_$biblioNum");
279
280         my $rank = $biblioData->{rank};
281         if ( $itemNum ne '' ) {
282             $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
283         }
284         else {
285             $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum )->{status} eq 'OK';
286
287             # Inserts a null into the 'itemnumber' field of 'reserves' table.
288             $itemNum = undef;
289         }
290         my $notes = $query->param('notes_'.$biblioNum)||'';
291
292         if (   $maxreserves
293             && $reserve_cnt >= $maxreserves )
294         {
295             $canreserve = 0;
296         }
297
298         unless ( $can_place_hold_if_available_at_pickup ) {
299             my $items_in_this_library = Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch });
300             my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
301             my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost => { '!=' => 0 }, damaged => { '!=' => 0 }, } });
302             if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
303                 $canreserve = 0
304             }
305         }
306
307         my $itemtype = $query->param('itemtype') || undef;
308         $itemtype = undef if $itemNum;
309
310         # Here we actually do the reserveration. Stage 3.
311         if ($canreserve) {
312             my $reserve_id = AddReserve(
313                 $branch,          $borrowernumber, $biblioNum,
314                 [$biblioNum],     $rank,           $startdate,
315                 $expiration_date, $notes,          $biblioData->{title},
316                 $itemNum,         $found,          $itemtype,
317             );
318             $failed_holds++ unless $reserve_id;
319             ++$reserve_cnt;
320         }
321     }
322
323     print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
324     exit;
325 }
326
327 #
328 #
329 # Here we check that the borrower can actually make reserves Stage 1.
330 #
331 #
332 my $noreserves     = 0;
333 my $maxoutstanding = C4::Context->preference("maxoutstanding");
334 $template->param( noreserve => 1 ) unless $maxoutstanding;
335 my $amountoutstanding = $patron->account->balance;
336 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
337     my $amount = sprintf "%.02f", $amountoutstanding;
338     $template->param( message => 1 );
339     $noreserves = 1;
340     $template->param( too_much_oweing => $amount );
341 }
342
343 if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
344     $noreserves = 1;
345     $template->param(
346         message => 1,
347         GNA     => 1
348     );
349 }
350
351 if ( $patron->lost && ($patron->lost == 1) ) {
352     $noreserves = 1;
353     $template->param(
354         message => 1,
355         lost    => 1
356     );
357 }
358
359 if ( $patron->is_debarred ) {
360     $noreserves = 1;
361     $template->param(
362         message          => 1,
363         debarred         => 1,
364         debarred_comment => $patron->debarredcomment,
365         debarred_date    => $patron->debarred,
366     );
367 }
368
369 my $holds = $patron->holds;
370 my $reserves_count = $holds->count;
371 $template->param( RESERVES => $holds->unblessed );
372 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
373     $template->param( message => 1 );
374     $noreserves = 1;
375     $template->param( too_many_reserves => $holds->count );
376 }
377
378 unless ( $noreserves ) {
379     my $requested_reserves_count = scalar( @biblionumbers );
380     if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
381         $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
382     }
383 }
384
385 unless ($noreserves) {
386     $template->param( select_item_types => 1 );
387 }
388
389
390 #
391 #
392 # Build the template parameters that will show the info
393 # and items for each biblionumber.
394 #
395 #
396
397 my $biblioLoop = [];
398 my $numBibsAvailable = 0;
399 my $itemdata_enumchron = 0;
400 my $itemdata_ccode = 0;
401 my $anyholdable = 0;
402 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
403 my $pickup_locations = Koha::Libraries->search({ pickup_location => 1 });
404 $template->param('item_level_itypes' => $itemLevelTypes);
405
406 foreach my $biblioNum (@biblionumbers) {
407
408     my $record = GetMarcBiblio({ biblionumber => $biblioNum });
409     # Init the bib item with the choices for branch pickup
410     my %biblioLoopIter;
411
412     # Get relevant biblio data.
413     my $biblioData = $biblioDataHash{$biblioNum};
414     if (! $biblioData) {
415         $template->param(message=>1, bad_biblionumber=>$biblioNum);
416         &get_out($query, $cookie, $template->output);
417     }
418
419     my @not_available_at = ();
420     my $biblio = $biblioData->{object};
421     foreach my $library ( $pickup_locations->as_list ) {
422         push( @not_available_at, $library->branchcode ) unless $biblio->can_be_transferred({ to => $library });
423     }
424
425     my $frameworkcode = GetFrameworkCode( $biblioData->{biblionumber} );
426     $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
427     $biblioLoopIter{title} = $biblioData->{title};
428     $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
429     $biblioLoopIter{author} = $biblioData->{author};
430     $biblioLoopIter{rank} = $biblioData->{rank};
431     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
432     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
433     $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
434
435     if (!$itemLevelTypes && $biblioData->{itemtype}) {
436         $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
437         $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$biblioData->{itemtype}}{imageurl};
438     }
439
440     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
441         if ($itemLevelTypes && $itemInfo->{itype}) {
442             $itemInfo->{translated_description} = $itemtypes->{$itemInfo->{itype}}{translated_description};
443             $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$itemInfo->{itype}}{imageurl};
444         }
445
446         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
447             $biblioLoopIter{forloan} = 1;
448         }
449     }
450
451     my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
452     my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
453
454     $biblioLoopIter{itemLoop} = [];
455     my $numCopiesAvailable = 0;
456     my $numCopiesOPACAvailable = 0;
457     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
458         my $itemNum = $itemInfo->{itemnumber};
459         my $item = Koha::Items->find( $itemNum );
460         my $itemLoopIter = {};
461
462         $itemLoopIter->{itemnumber} = $itemNum;
463         $itemLoopIter->{barcode} = $itemInfo->{barcode};
464         $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
465         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
466         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
467         $itemLoopIter->{ccode} = $itemInfo->{ccode};
468         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
469         if ($itemLevelTypes) {
470             $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
471             $itemLoopIter->{itype} = $itemInfo->{itype};
472             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
473         }
474
475         # If the holdingbranch is different than the homebranch, we show the
476         # holdingbranch of the document too.
477         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
478             $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
479         }
480
481         # If the item is currently on loan, we display its return date and
482         # change the background color.
483         my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
484         if ( $issue ) {
485             $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
486             $itemLoopIter->{backgroundcolor} = 'onloan';
487         }
488
489         # checking reserve
490         my $holds = $item->current_holds;
491
492         if ( my $first_hold = $holds->next ) {
493             $itemLoopIter->{backgroundcolor} = 'reserved';
494             $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
495             $itemLoopIter->{ExpectedAtLibrary}         = $first_hold->branchcode;
496             $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
497         }
498
499         $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
500         $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
501
502         # Management of the notforloan document
503         if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
504             $itemLoopIter->{backgroundcolor} = 'other';
505             $itemLoopIter->{notforloanvalue} =
506               $notforloan_label_of->{ $itemLoopIter->{notforloan} };
507         }
508
509         # Management of lost or long overdue items
510         if ( $itemInfo->{itemlost} ) {
511
512             # FIXME localized strings should never be in Perl code
513             $itemLoopIter->{message} =
514                 $itemInfo->{itemlost} == 1 ? "(lost)"
515               : $itemInfo->{itemlost} == 2 ? "(long overdue)"
516               : "";
517             $itemInfo->{backgroundcolor} = 'other';
518         }
519
520         # Check of the transferred documents
521         my ( $transfertwhen, $transfertfrom, $transfertto ) =
522           GetTransfers($itemNum);
523         if ( $transfertwhen && ($transfertwhen ne '') ) {
524             $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
525             $itemLoopIter->{transfertfrom} = $transfertfrom;
526             $itemLoopIter->{transfertto} = $transfertto;
527             $itemLoopIter->{nocancel} = 1;
528         }
529
530         # if the items belongs to a host record, show link to host record
531         if ( $itemInfo->{biblionumber} ne $biblioNum ) {
532             $biblioLoopIter{hostitemsflag}    = 1;
533             $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
534             $itemLoopIter->{hosttitle}        = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
535         }
536
537         # If there is no loan, return and transfer, we show a checkbox.
538         $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
539
540         my $patron_unblessed = $patron->unblessed;
541         my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
542
543         my $policy_holdallowed = !$itemLoopIter->{already_reserved};
544         $policy_holdallowed &&=
545             IsAvailableForItemLevelRequest($itemInfo,$patron_unblessed) &&
546             CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
547
548         if ($policy_holdallowed) {
549             my $opac_hold_policy = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
550             if ( $opac_hold_policy ne 'N' ) { # If Y or F
551                 $itemLoopIter->{available} = 1;
552                 $numCopiesOPACAvailable++;
553                 $biblioLoopIter{force_hold} = 1 if $opac_hold_policy eq 'F';
554             }
555             $numCopiesAvailable++;
556
557             unless ( $can_place_hold_if_available_at_pickup ) {
558                 my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
559                 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
560                 if ( $items_in_this_library->count > $nb_of_items_issued ) {
561                     push @not_available_at, $itemInfo->{holdingbranch};
562                 }
563             }
564         }
565
566         $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
567
568     # Show serial enumeration when needed
569         if ($itemLoopIter->{enumchron}) {
570             $itemdata_enumchron = 1;
571         }
572     # Show collection when needed
573         if ($itemLoopIter->{ccode}) {
574             $itemdata_ccode = 1;
575         }
576
577         push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
578     }
579     $template->param(
580         itemdata_enumchron => $itemdata_enumchron,
581         itemdata_ccode     => $itemdata_ccode,
582     );
583
584     if ($numCopiesAvailable > 0) {
585         $numBibsAvailable++;
586         $biblioLoopIter{bib_available} = 1;
587         $biblioLoopIter{holdable} = 1;
588         $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
589     }
590     if ($biblioLoopIter{already_reserved}) {
591         $biblioLoopIter{holdable} = undef;
592         $biblioLoopIter{itemholdable} = undef;
593     }
594     if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
595         $biblioLoopIter{holdable} = undef;
596         $biblioLoopIter{already_patron_possession} = 1;
597     }
598
599     if ( $biblioLoopIter{holdable} ) {
600         @not_available_at = uniq @not_available_at;
601         $biblioLoopIter{not_available_at} = \@not_available_at ;
602     }
603
604     unless ( $can_place_hold_if_available_at_pickup ) {
605         @not_available_at = uniq @not_available_at;
606         $biblioLoopIter{not_available_at} = \@not_available_at ;
607         # The record is not holdable is not available at any of the libraries
608         if ( Koha::Libraries->search->count == @not_available_at ) {
609             $biblioLoopIter{holdable} = 0;
610         }
611     }
612
613     $biblioLoopIter{holdable} &&= CanBookBeReserved( $borrowernumber, $biblioNum )->{status} eq 'OK';
614
615     # For multiple holds per record, if a patron has previously placed a hold,
616     # the patron can only place more holds of the same type. That is, if the
617     # patron placed a record level hold, all the holds the patron places must
618     # be record level. If the patron placed an item level hold, all holds
619     # the patron places must be item level
620     my $forced_hold_level = Koha::Holds->search(
621         {
622             borrowernumber => $borrowernumber,
623             biblionumber   => $biblioNum,
624             found          => undef,
625         }
626     )->forced_hold_level();
627     if ($forced_hold_level) {
628         $biblioLoopIter{force_hold}   = 1 if $forced_hold_level eq 'item';
629         $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
630     }
631
632
633     push @$biblioLoop, \%biblioLoopIter;
634
635     $anyholdable = 1 if $biblioLoopIter{holdable};
636 }
637
638 unless ($pickup_locations->count) {
639     $numBibsAvailable = 0;
640     $anyholdable = 0;
641     $template->param(
642         message => 1,
643         no_pickup_locations => 1
644     );
645 }
646
647 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
648     $template->param( none_available => 1 );
649 }
650
651 if (scalar @biblionumbers > 1) {
652     $template->param( multi_hold => 1);
653 }
654
655 my $show_notes=C4::Context->preference('OpacHoldNotes');
656 $template->param(OpacHoldNotes=>$show_notes);
657
658 # display infos
659 $template->param(bibitemloop => $biblioLoop);
660 # can set reserve date in future
661 if (
662     C4::Context->preference( 'AllowHoldDateInFuture' ) &&
663     C4::Context->preference( 'OPACAllowHoldDateInFuture' )
664     ) {
665     $template->param(
666             reserve_in_future         => 1,
667     );
668 }
669
670 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };