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