Bug 28572: Remove C4::Debug
[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
35 use Koha::AuthorisedValues;
36 use Koha::Biblios;
37 use Koha::DateUtils;
38 use Koha::CirculationRules;
39 use Koha::Items;
40 use Koha::ItemTypes;
41 use Koha::Checkouts;
42 use Koha::Libraries;
43 use Koha::Patrons;
44 use Date::Calc qw/Today Date_to_Days/;
45 use List::MoreUtils qw/uniq/;
46
47 my $maxreserves = C4::Context->preference("maxreserves");
48
49 my $query = CGI->new;
50
51 # if RequestOnOpac (for placing holds) is disabled, leave immediately
52 if ( ! C4::Context->preference('RequestOnOpac') ) {
53     print $query->redirect("/cgi-bin/koha/errors/404.pl");
54     exit;
55 }
56
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58     {
59         template_name   => "opac-reserve.tt",
60         query           => $query,
61         type            => "opac",
62     }
63 );
64
65 my ($show_holds_count, $show_priority);
66 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
67     m/holds/o and $show_holds_count = 1;
68     m/priority/ and $show_priority = 1;
69 }
70
71 my $patron = Koha::Patrons->find( $borrowernumber, { prefetch => ['categorycode'] } );
72 my $category = $patron->category;
73
74 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
75 unless ( $can_place_hold_if_available_at_pickup ) {
76     my @patron_categories = split ',', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
77     if ( @patron_categories ) {
78         my $categorycode = $patron->categorycode;
79         $can_place_hold_if_available_at_pickup = grep { $_ eq $categorycode } @patron_categories;
80     }
81 }
82
83 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
84 if ( $category->effective_BlockExpiredPatronOpacActions ) {
85
86     if ( $patron->is_expired ) {
87
88         # cannot reserve, their card has expired and the rules set mean this is not allowed
89         $template->param( message => 1, expired_patron => 1 );
90         output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
91         exit;
92     }
93 }
94
95 # Pass through any reserve charge
96 my $reservefee = $category->reservefee;
97 if ( $reservefee > 0){
98     $template->param( RESERVE_CHARGE => $reservefee);
99 }
100
101 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
102
103 # There are two ways of calling this script, with a single biblio num
104 # or multiple biblio nums.
105 my $biblionumbers = $query->param('biblionumbers');
106 my $reserveMode = $query->param('reserve_mode');
107 if ($reserveMode && ($reserveMode eq 'single')) {
108     my $bib = $query->param('single_bib');
109     $biblionumbers = "$bib/";
110 }
111 if (! $biblionumbers) {
112     $biblionumbers = $query->param('biblionumber');
113 }
114
115 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
116     $template->param(message=>1, no_biblionumber=>1);
117     output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
118     exit;
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     exit;
132 }
133
134
135 # pass the pickup branch along....
136 my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
137 $template->param( branch => $branch );
138
139 #
140 #
141 # Build hashes of the requested biblio(item)s and items.
142 #
143 #
144
145 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
146 my %itemInfoHash; # Hash of itemnumber to item info.
147 foreach my $biblioNumber (@biblionumbers) {
148
149     my $biblioData = GetBiblioData($biblioNumber);
150     $biblioDataHash{$biblioNumber} = $biblioData;
151
152     my @itemInfos = GetItemsInfo($biblioNumber);
153
154     my $marcrecord= GetMarcBiblio({ biblionumber => $biblioNumber });
155
156     # flag indicating existence of at least one item linked via a host record
157     my $hostitemsflag;
158     # adding items linked via host biblios
159     my @hostitemInfos = GetHostItemsInfo($marcrecord);
160     if (@hostitemInfos){
161         $hostitemsflag =1;
162         push (@itemInfos,@hostitemInfos);
163     }
164
165     $biblioData->{itemInfos} = \@itemInfos;
166     foreach my $itemInfo (@itemInfos) {
167         $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
168     }
169
170     # Compute the priority rank.
171     my $biblio = Koha::Biblios->find( $biblioNumber );
172     $biblioData->{object} = $biblio;
173     my $holds = $biblio->holds;
174     my $rank = $holds->count;
175     $biblioData->{reservecount} = 1;    # new reserve
176     while ( my $hold = $holds->next ) {
177         if ( $hold->is_waiting ) {
178             $rank--;
179         }
180         else {
181             $biblioData->{reservecount}++;
182         }
183     }
184     $biblioData->{rank} = $rank + 1;
185 }
186
187 #
188 #
189 # If this is the second time through this script, it
190 # means we are carrying out the hold request, possibly
191 # with a specific item for each biblionumber.
192 #
193 #
194 if ( $query->param('place_reserve') ) {
195     my $reserve_cnt = 0;
196     if ($maxreserves) {
197         $reserve_cnt = $patron->holds->count;
198     }
199
200     # List is composed of alternating biblio/item/branch
201     my $selectedItems = $query->param('selecteditems');
202
203     if ($query->param('reserve_mode') eq 'single') {
204         # This indicates non-JavaScript mode, so there was
205         # only a single biblio number selected.
206         my $bib = $query->param('single_bib');
207         my $item = $query->param("checkitem_$bib");
208         if ($item eq 'any') {
209             $item = '';
210         }
211         my $branch = $query->param('branch');
212         $selectedItems = "$bib/$item/$branch/";
213     }
214
215     $selectedItems =~ s!/$!!;
216     my @selectedItems = split /\//, $selectedItems, -1;
217
218     # Make sure there is a biblionum/itemnum/branch triplet for each item.
219     # The itemnum can be 'any', meaning next available.
220     my $selectionCount = @selectedItems;
221     if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
222         $template->param(message=>1, bad_data=>1);
223         output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
224         exit;
225     }
226
227     my $failed_holds = 0;
228     while (@selectedItems) {
229         my $biblioNum = shift(@selectedItems);
230         my $itemNum   = shift(@selectedItems);
231         my $branch    = shift(@selectedItems);    # i.e., branch code, not name
232
233         my $canreserve = 0;
234
235         my $singleBranchMode = Koha::Libraries->search->count == 1;
236         if ( $singleBranchMode || ! C4::Context->preference("OPACAllowUserToChooseBranch") )
237         {    # single branch mode or disabled user choosing
238             $branch = $patron->branchcode;
239         }
240
241         # When choosing a specific item, the default pickup library should be dictated by the default hold policy
242         if ( ! C4::Context->preference("OPACAllowUserToChooseBranch") && $itemNum ) {
243             my $item = Koha::Items->find( $itemNum );
244             my $type = $item->effective_itemtype;
245             my $rule = GetBranchItemRule( $patron->branchcode, $type );
246
247             if ( $rule->{hold_fulfillment_policy} eq 'any' || $rule->{hold_fulfillment_policy} eq 'patrongroup' ) {
248                 $branch = $patron->branchcode;
249             } elsif ( $rule->{hold_fulfillment_policy} eq 'holdgroup' ){
250                 $branch = $item->homebranch;
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     my $visible_items = { map { $_->itemnumber => $_ } $biblio->items->filter_by_visible_in_opac( { patron => $patron } )->as_list };
466
467     # Only keep the items that are visible in the opac (i.e. those in %visible_items)
468     # FIXME: We should get rid of itemInfos altogether and use $visible_items
469     $biblioData->{itemInfos} = [ grep { $visible_items->{ $_->{itemnumber} } } @{ $biblioData->{itemInfos} } ];
470
471     $biblioLoopIter{itemLoop} = [];
472     my $numCopiesAvailable = 0;
473     my $numCopiesOPACAvailable = 0;
474     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
475         my $itemNum = $itemInfo->{itemnumber};
476         my $item = $visible_items->{$itemNum};
477         my $itemLoopIter = {};
478
479         $itemLoopIter->{itemnumber} = $itemNum;
480         $itemLoopIter->{barcode} = $itemInfo->{barcode};
481         $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
482         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
483         $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
484         $itemLoopIter->{ccode} = $itemInfo->{ccode};
485         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
486         if ($itemLevelTypes) {
487             $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
488             $itemLoopIter->{itype} = $itemInfo->{itype};
489             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
490         }
491
492         # If the holdingbranch is different than the homebranch, we show the
493         # holdingbranch of the document too.
494         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
495             $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
496         }
497
498         # If the item is currently on loan, we display its return date and
499         # change the background color.
500         my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
501         if ( $issue ) {
502             $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
503             $itemLoopIter->{backgroundcolor} = 'onloan';
504         }
505
506         # checking reserve
507         my $holds = $item->current_holds;
508
509         if ( my $first_hold = $holds->next ) {
510             $itemLoopIter->{backgroundcolor} = 'reserved';
511             $itemLoopIter->{reservedate}     = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
512             $itemLoopIter->{ExpectedAtLibrary}         = $first_hold->branchcode;
513             $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
514         }
515
516         $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
517         $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
518
519         # Management of the notforloan document
520         if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
521             $itemLoopIter->{backgroundcolor} = 'other';
522             $itemLoopIter->{notforloanvalue} =
523               $notforloan_label_of->{ $itemLoopIter->{notforloan} };
524         }
525
526         # Management of lost or long overdue items
527         if ( $itemInfo->{itemlost} ) {
528
529             # FIXME localized strings should never be in Perl code
530             $itemLoopIter->{message} =
531                 $itemInfo->{itemlost} == 1 ? "(lost)"
532               : $itemInfo->{itemlost} == 2 ? "(long overdue)"
533               : "";
534             $itemInfo->{backgroundcolor} = 'other';
535         }
536
537         # Check of the transferred documents
538         my ( $transfertwhen, $transfertfrom, $transfertto ) =
539           GetTransfers($itemNum);
540         if ( $transfertwhen && ($transfertwhen ne '') ) {
541             $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
542             $itemLoopIter->{transfertfrom} = $transfertfrom;
543             $itemLoopIter->{transfertto} = $transfertto;
544             $itemLoopIter->{nocancel} = 1;
545         }
546
547         # if the items belongs to a host record, show link to host record
548         if ( $itemInfo->{biblionumber} ne $biblioNum ) {
549             $biblioLoopIter{hostitemsflag}    = 1;
550             $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
551             $itemLoopIter->{hosttitle}        = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
552         }
553
554         # If there is no loan, return and transfer, we show a checkbox.
555         $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
556
557         my $patron_unblessed = $patron->unblessed;
558         my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
559
560         my $policy_holdallowed = !$itemLoopIter->{already_reserved};
561         $policy_holdallowed &&=
562             IsAvailableForItemLevelRequest($item, $patron) &&
563             CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
564
565         if ($policy_holdallowed) {
566             my $opac_hold_policy = Koha::CirculationRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
567             if ( $opac_hold_policy ne 'N' ) { # If Y or F
568                 $itemLoopIter->{available} = 1;
569                 $numCopiesOPACAvailable++;
570                 $biblioLoopIter{force_hold} = 1 if $opac_hold_policy eq 'F';
571             }
572             $numCopiesAvailable++;
573
574             unless ( $can_place_hold_if_available_at_pickup ) {
575                 my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
576                 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
577                 if ( $items_in_this_library->count > $nb_of_items_issued ) {
578                     push @not_available_at, $itemInfo->{holdingbranch};
579                 }
580             }
581         }
582
583         $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
584
585     # Show serial enumeration when needed
586         if ($itemLoopIter->{enumchron}) {
587             $itemdata_enumchron = 1;
588         }
589     # Show collection when needed
590         if ($itemLoopIter->{ccode}) {
591             $itemdata_ccode = 1;
592         }
593
594         push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
595     }
596     $template->param(
597         itemdata_enumchron => $itemdata_enumchron,
598         itemdata_ccode     => $itemdata_ccode,
599     );
600
601     if ($numCopiesAvailable > 0) {
602         $numBibsAvailable++;
603         $biblioLoopIter{bib_available} = 1;
604         $biblioLoopIter{holdable} = 1;
605         $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
606     }
607     if ($biblioLoopIter{already_reserved}) {
608         $biblioLoopIter{holdable} = undef;
609         $biblioLoopIter{itemholdable} = undef;
610     }
611     if ( $biblioLoopIter{holdable} ) {
612         @not_available_at = uniq @not_available_at;
613         $biblioLoopIter{not_available_at} = \@not_available_at ;
614     }
615
616     unless ( $can_place_hold_if_available_at_pickup ) {
617         @not_available_at = uniq @not_available_at;
618         $biblioLoopIter{not_available_at} = \@not_available_at ;
619         # The record is not holdable is not available at any of the libraries
620         if ( Koha::Libraries->search->count == @not_available_at ) {
621             $biblioLoopIter{holdable} = 0;
622         }
623     }
624
625     my $status = CanBookBeReserved( $borrowernumber, $biblioNum )->{status};
626     $biblioLoopIter{holdable} &&= $status eq 'OK';
627     $biblioLoopIter{already_patron_possession} = $status eq 'alreadypossession';
628
629     # For multiple holds per record, if a patron has previously placed a hold,
630     # the patron can only place more holds of the same type. That is, if the
631     # patron placed a record level hold, all the holds the patron places must
632     # be record level. If the patron placed an item level hold, all holds
633     # the patron places must be item level
634     my $forced_hold_level = Koha::Holds->search(
635         {
636             borrowernumber => $borrowernumber,
637             biblionumber   => $biblioNum,
638             found          => undef,
639         }
640     )->forced_hold_level();
641     if ($forced_hold_level) {
642         $biblioLoopIter{force_hold}   = 1 if $forced_hold_level eq 'item';
643         $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
644         $biblioLoopIter{forced_hold_level} = $forced_hold_level;
645     }
646
647
648     push @$biblioLoop, \%biblioLoopIter;
649
650     $anyholdable = 1 if $biblioLoopIter{holdable};
651 }
652
653 unless ($pickup_locations->count) {
654     $numBibsAvailable = 0;
655     $anyholdable = 0;
656     $template->param(
657         message => 1,
658         no_pickup_locations => 1
659     );
660 }
661
662 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
663     $template->param( none_available => 1 );
664 }
665
666 if (scalar @biblionumbers > 1) {
667     $template->param( multi_hold => 1);
668 }
669
670 my $show_notes=C4::Context->preference('OpacHoldNotes');
671 $template->param(OpacHoldNotes=>$show_notes);
672
673 # display infos
674 $template->param(bibitemloop => $biblioLoop);
675 # can set reserve date in future
676 if (
677     C4::Context->preference( 'AllowHoldDateInFuture' ) &&
678     C4::Context->preference( 'OPACAllowHoldDateInFuture' )
679     ) {
680     $template->param(
681             reserve_in_future         => 1,
682     );
683 }
684
685 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };