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