Bug 11592: MARCView and ISBD followup
[koha.git] / reserve / request.pl
1 #!/usr/bin/perl
2
3
4 #written 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 =head1 request.pl
24
25 script to place reserves/requests
26
27 =cut
28
29 use Modern::Perl;
30
31 use C4::Branch;
32 use CGI qw ( -utf8 );
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
35 use C4::Output;
36 use C4::Auth;
37 use C4::Reserves;
38 use C4::Biblio;
39 use C4::Items;
40 use C4::Koha;
41 use C4::Circulation;
42 use Koha::DateUtils;
43 use C4::Utils::DataTables::Members;
44 use C4::Members;
45 use C4::Search;         # enabled_staff_search_views
46 use Koha::DateUtils;
47 use Koha::Holds;
48 use Koha::Libraries;
49
50 my $dbh = C4::Context->dbh;
51 my $input = new CGI;
52 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
53     {
54         template_name   => "reserve/request.tt",
55         query           => $input,
56         type            => "intranet",
57         authnotrequired => 0,
58         flagsrequired   => { reserveforothers => 'place_holds' },
59     }
60 );
61
62 my $multihold = $input->param('multi_hold');
63 $template->param(multi_hold => $multihold);
64 my $showallitems = $input->param('showallitems');
65
66 # get Branches and Itemtypes
67 my $branches = GetBranches();
68 my $itemtypes = GetItemTypes();
69
70 my $userbranch = '';
71 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
72     $userbranch = C4::Context->userenv->{'branch'};
73 }
74
75
76 # Select borrowers infos
77 my $findborrower = $input->param('findborrower');
78 $findborrower = '' unless defined $findborrower;
79 $findborrower =~ s|,| |g;
80 my $borrowernumber_hold = $input->param('borrowernumber') || '';
81 my $messageborrower;
82 my $warnings;
83 my $messages;
84 my $exceeded_maxreserves;
85 my $exceeded_holds_per_record;
86
87 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
88 my $action = $input->param('action');
89 $action ||= q{};
90
91 if ( $action eq 'move' ) {
92   my $where = $input->param('where');
93   my $reserve_id = $input->param('reserve_id');
94   AlterPriority( $where, $reserve_id );
95 } elsif ( $action eq 'cancel' ) {
96   my $reserve_id = $input->param('reserve_id');
97   CancelReserve({ reserve_id => $reserve_id });
98 } elsif ( $action eq 'setLowestPriority' ) {
99   my $reserve_id = $input->param('reserve_id');
100   ToggleLowestPriority( $reserve_id );
101 } elsif ( $action eq 'toggleSuspend' ) {
102   my $reserve_id = $input->param('reserve_id');
103   my $suspend_until  = $input->param('suspend_until');
104   ToggleSuspend( $reserve_id, $suspend_until );
105 }
106
107 if ($findborrower) {
108     my $borrower = C4::Members::GetMember( cardnumber => $findborrower );
109     if ( $borrower ) {
110         $borrowernumber_hold = $borrower->{borrowernumber};
111     } else {
112         my $dt_params = { iDisplayLength => -1 };
113         my $results = C4::Utils::DataTables::Members::search(
114             {
115                 searchmember => $findborrower,
116                 dt_params => $dt_params,
117             }
118         );
119         my $borrowers = $results->{patrons};
120         if ( scalar @$borrowers == 1 ) {
121             $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
122         } elsif ( @$borrowers ) {
123             $template->param( borrowers => $borrowers );
124         } else {
125             $messageborrower = "'$findborrower'";
126         }
127     }
128 }
129
130 my @biblionumbers = ();
131 my $biblionumbers = $input->param('biblionumbers');
132 if ($multihold) {
133     @biblionumbers = split '/', $biblionumbers;
134 } else {
135     push @biblionumbers, $input->multi_param('biblionumber');
136 }
137
138
139 # If we have the borrowernumber because we've performed an action, then we
140 # don't want to try to place another reserve.
141 if ($borrowernumber_hold && !$action) {
142     my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
143     my $diffbranch;
144
145     # we check the reserves of the borrower, and if he can reserv a document
146     # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
147
148     my $reserves_count =
149       GetReserveCount( $borrowerinfo->{'borrowernumber'} );
150
151     my $new_reserves_count = scalar( @biblionumbers );
152
153     my $maxreserves = C4::Context->preference('maxreserves');
154     if ( $maxreserves
155         && ( $reserves_count + $new_reserves_count > $maxreserves ) )
156     {
157         my $new_reserves_allowed =
158             $maxreserves - $reserves_count > 0
159           ? $maxreserves - $reserves_count
160           : 0;
161         $warnings             = 1;
162         $exceeded_maxreserves = 1;
163         $template->param(
164             new_reserves_allowed => $new_reserves_allowed,
165             new_reserves_count   => $new_reserves_count,
166             reserves_count       => $reserves_count,
167             maxreserves          => $maxreserves,
168         );
169     }
170
171     # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
172     my $expiry_date = $borrowerinfo->{dateexpiry};
173     my $expiry = 0; # flag set if patron account has expired
174     if ($expiry_date and $expiry_date ne '0000-00-00' and
175         Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
176         $expiry = 1;
177     }
178
179     # check if the borrower make the reserv in a different branch
180     if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
181         $diffbranch = 1;
182     }
183
184     my $is_debarred = Koha::Patrons->find( $borrowerinfo->{borrowernumber} )->is_debarred;
185     $template->param(
186                 borrowernumber      => $borrowerinfo->{'borrowernumber'},
187                 borrowersurname     => $borrowerinfo->{'surname'},
188                 borrowerfirstname   => $borrowerinfo->{'firstname'},
189                 borrowerstreetaddress   => $borrowerinfo->{'address'},
190                 borrowercity        => $borrowerinfo->{'city'},
191                 borrowerphone       => $borrowerinfo->{'phone'},
192                 borrowermobile      => $borrowerinfo->{'mobile'},
193                 borrowerfax         => $borrowerinfo->{'fax'},
194                 borrowerphonepro    => $borrowerinfo->{'phonepro'},
195                 borroweremail       => $borrowerinfo->{'email'},
196                 borroweremailpro    => $borrowerinfo->{'emailpro'},
197                 borrowercategory    => $borrowerinfo->{'category'},
198                 cardnumber          => $borrowerinfo->{'cardnumber'},
199                 expiry              => $expiry,
200                 diffbranch          => $diffbranch,
201                 messages            => $messages,
202                 warnings            => $warnings,
203                 restricted          => $is_debarred,
204                 amount_outstanding  => GetMemberAccountRecords($borrowerinfo->{borrowernumber}),
205     );
206 }
207
208 $template->param( messageborrower => $messageborrower );
209
210 # FIXME launch another time GetMember perhaps until
211 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
212
213 my $itemdata_enumchron = 0;
214 my @biblioloop = ();
215 foreach my $biblionumber (@biblionumbers) {
216     next unless $biblionumber =~ m|^\d+$|;
217
218     my %biblioloopiter = ();
219
220     my $dat = GetBiblioData($biblionumber);
221
222     my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
223     $canReserve //= '';
224     if ( $canReserve eq 'OK' ) {
225
226         #All is OK and we can continue
227     }
228     elsif ( $canReserve eq 'tooManyReserves' ) {
229         $exceeded_maxreserves = 1;
230     }
231     elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
232         $exceeded_holds_per_record = 1;
233         $biblioloopiter{$canReserve} = 1;
234     }
235     elsif ( $canReserve eq 'ageRestricted' ) {
236         $template->param( $canReserve => 1 );
237         $biblioloopiter{$canReserve} = 1;
238     }
239     else {
240         $biblioloopiter{$canReserve} = 1;
241     }
242
243     my $force_hold_level;
244     if ( $borrowerinfo->{borrowernumber} ) {
245         # For multiple holds per record, if a patron has previously placed a hold,
246         # the patron can only place more holds of the same type. That is, if the
247         # patron placed a record level hold, all the holds the patron places must
248         # be record level. If the patron placed an item level hold, all holds
249         # the patron places must be item level
250         my $holds = Koha::Holds->search(
251             {
252                 borrowernumber => $borrowerinfo->{borrowernumber},
253                 biblionumber   => $biblionumber,
254                 found          => undef,
255             }
256         );
257         $force_hold_level = $holds->forced_hold_level();
258         $biblioloopiter{force_hold_level} = $force_hold_level;
259         $template->param( force_hold_level => $force_hold_level );
260
261         # For a librarian to be able to place multiple record holds for a patron for a record,
262         # we must find out what the maximum number of holds they can place for the patron is
263         my $max_holds_for_record = GetMaxPatronHoldsForRecord( $borrowerinfo->{borrowernumber}, $biblionumber );
264         my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
265         $biblioloopiter{remaining_holds_for_record} = $max_holds_for_record;
266         $template->param( max_holds_for_record => $max_holds_for_record );
267         $template->param( remaining_holds_for_record => $remaining_holds_for_record );
268     }
269
270     # Check to see if patron is allowed to place holds on records where the
271     # patron already has an item from that record checked out
272     my $alreadypossession;
273     if ( !C4::Context->preference('AllowHoldsOnPatronsPossessions')
274         && CheckIfIssuedToPatron( $borrowerinfo->{borrowernumber}, $biblionumber ) )
275     {
276         $template->param( alreadypossession => $alreadypossession, );
277     }
278
279
280     my $count = Koha::Holds->search( { biblionumber => $biblionumber } )->count();
281     my $totalcount = $count;
282
283     # FIXME think @optionloop, is maybe obsolete, or  must be switchable by a systeme preference fixed rank or not
284     # make priorities options
285
286     my @optionloop;
287     for ( 1 .. $count + 1 ) {
288         push(
289              @optionloop,
290              {
291               num      => $_,
292               selected => ( $_ == $count + 1 ),
293              }
294             );
295     }
296     # adding a fixed value for priority options
297     my $fixedRank = $count+1;
298
299     my %itemnumbers_of_biblioitem;
300     my @itemnumbers;
301
302     ## $items is array of 'item' table numbers
303     if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
304         @itemnumbers  = @$items;
305     }
306     my @hostitems = get_hostitemnumbers_of($biblionumber);
307     if (@hostitems){
308         $template->param('hostitemsflag' => 1);
309         push(@itemnumbers, @hostitems);
310     }
311
312     if (!@itemnumbers) {
313         $template->param('noitems' => 1);
314         $biblioloopiter{noitems} = 1;
315     }
316
317     ## Hash of item number to 'item' table fields
318     my $iteminfos_of = GetItemInfosOf(@itemnumbers);
319
320     ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
321     ## when by definition all of the itemnumber have the same biblioitemnumber
322     foreach my $itemnumber (@itemnumbers) {
323         my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
324         push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
325     }
326
327     ## Should be same as biblionumber
328     my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
329
330     my $notforloan_label_of = get_notforloan_label_of();
331
332     ## Hash of biblioitemnumber to 'biblioitem' table records
333     my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
334
335     my @bibitemloop;
336
337     my @available_itemtypes;
338     foreach my $biblioitemnumber (@biblioitemnumbers) {
339         my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
340         my $num_available = 0;
341         my $num_override  = 0;
342         my $hiddencount   = 0;
343
344         $biblioitem->{force_hold_level} = $force_hold_level;
345
346         if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
347             $biblioitem->{hostitemsflag} = 1;
348         }
349
350         $biblioloopiter{description} = $biblioitem->{description};
351         $biblioloopiter{itypename}   = $biblioitem->{description};
352         if ( $biblioitem->{itemtype} ) {
353
354             $biblioitem->{description} =
355               $itemtypes->{ $biblioitem->{itemtype} }{description};
356
357             $biblioloopiter{imageurl} =
358               getitemtypeimagelocation( 'intranet',
359                 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
360         }
361
362         foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )    {
363             my $item = $iteminfos_of->{$itemnumber};
364
365             $item->{force_hold_level} = $force_hold_level;
366
367             unless (C4::Context->preference('item-level_itypes')) {
368                 $item->{itype} = $biblioitem->{itemtype};
369             }
370
371             $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
372             $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
373             $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
374
375             # if the holdingbranch is different than the homebranch, we show the
376             # holdingbranch of the document too
377             if ( $item->{homebranch} ne $item->{holdingbranch} ) {
378                 $item->{holdingbranchname} =
379                   $branches->{ $item->{holdingbranch} }{branchname};
380             }
381
382                 if($item->{biblionumber} ne $biblionumber){
383                         $item->{hostitemsflag}=1;
384                         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
385                 }
386                 
387             # if the item is currently on loan, we display its return date and
388             # change the background color
389             my $issues= GetItemIssue($itemnumber);
390             if ( $issues->{'date_due'} ) {
391                 $item->{date_due} = $issues->{date_due_sql};
392                 $item->{backgroundcolor} = 'onloan';
393             }
394
395             # checking reserve
396             my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
397             if ( defined $reservedate ) {
398                 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
399
400                 $item->{backgroundcolor} = 'reserved';
401                 $item->{reservedate}     = output_pref({ dt => dt_from_string( $reservedate ), dateonly => 1 });
402                 $item->{ReservedForBorrowernumber}     = $reservedfor;
403                 $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
404                 $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
405                 $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
406                 $item->{waitingdate} = $wait;
407             }
408
409             # Management of the notforloan document
410             if ( $item->{notforloan} ) {
411                 $item->{backgroundcolor} = 'other';
412                 $item->{notforloanvalue} =
413                   $notforloan_label_of->{ $item->{notforloan} };
414             }
415
416             # Management of lost or long overdue items
417             if ( $item->{itemlost} ) {
418
419                 # FIXME localized strings should never be in Perl code
420                 $item->{message} =
421                   $item->{itemlost} == 1 ? "(lost)"
422                     : $item->{itemlost} == 2 ? "(long overdue)"
423                       : "";
424                 $item->{backgroundcolor} = 'other';
425                 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
426                     $item->{hide} = 1;
427                     $hiddencount++;
428                 }
429             }
430
431             # Check the transit status
432             my ( $transfertwhen, $transfertfrom, $transfertto ) =
433               GetTransfers($itemnumber);
434
435             if ( defined $transfertwhen && $transfertwhen ne '' ) {
436                 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
437                 $item->{transfertfrom} =
438                   $branches->{$transfertfrom}{branchname};
439                 $item->{transfertto} = $branches->{$transfertto}{branchname};
440                 $item->{nocancel} = 1;
441             }
442
443             # If there is no loan, return and transfer, we show a checkbox.
444             $item->{notforloan} ||= 0;
445
446             # if independent branches is on we need to check if the person can reserve
447             # for branches they arent logged in to
448             if ( C4::Context->preference("IndependentBranches") ) {
449                 if (! C4::Context->preference("canreservefromotherbranches")){
450                     # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
451                     my $userenv = C4::Context->userenv;
452                     unless ( C4::Context->IsSuperLibrarian ) {
453                         $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
454                     }
455                 }
456             }
457
458             my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
459
460             my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
461
462             $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
463
464             my $can_item_be_reserved = CanItemBeReserved( $borrowerinfo->{borrowernumber}, $itemnumber );
465             $item->{not_holdable} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
466
467             $item->{item_level_holds} = OPACItemHoldsAllowed( $item, $borrowerinfo );
468
469             if (
470                    !$item->{cantreserve}
471                 && !$exceeded_maxreserves
472                 && IsAvailableForItemLevelRequest($item, $borrowerinfo)
473                 && $can_item_be_reserved eq 'OK'
474               )
475             {
476                 $item->{available} = 1;
477                 $num_available++;
478
479                 push( @available_itemtypes, $item->{itype} );
480             }
481             elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
482                 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
483                 $item->{override} = 1;
484                 $num_override++;
485             }
486
487             # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
488
489             # Show serial enumeration when needed
490             if ($item->{enumchron}) {
491                 $itemdata_enumchron = 1;
492             }
493
494             push @{ $biblioitem->{itemloop} }, $item;
495         }
496
497         if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
498             $template->param( override_required => 1 );
499         } elsif ( $num_available == 0 ) {
500             $template->param( none_available => 1 );
501             $biblioloopiter{warn} = 1;
502             $biblioloopiter{none_avail} = 1;
503         }
504         $template->param( hiddencount => $hiddencount);
505
506         push @bibitemloop, $biblioitem;
507     }
508
509     @available_itemtypes = uniq( @available_itemtypes );
510     $template->param( available_itemtypes => \@available_itemtypes );
511
512     # existingreserves building
513     my @reserveloop;
514     my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } );
515     foreach my $res (
516         sort {
517             my $a_found = $a->found() || '';
518             my $b_found = $a->found() || '';
519             $a_found cmp $b_found;
520         } @reserves
521       )
522     {
523         my %reserve;
524         my @optionloop;
525         for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
526             push(
527                 @optionloop,
528                 {
529                     num      => $i,
530                     selected => ( $i == $res->priority() ),
531                 }
532             );
533         }
534
535         if ( $res->is_found() ) {
536             $reserve{'holdingbranch'} = $res->item()->holdingbranch();
537             $reserve{'biblionumber'}  = $res->item()->biblionumber();
538             $reserve{'barcodenumber'} = $res->item()->barcode();
539             $reserve{'wbrcode'}       = $res->branchcode();
540             $reserve{'itemnumber'}    = $res->itemnumber();
541             $reserve{'wbrname'}       = $res->branch()->branchname();
542
543             if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
544
545                 # Just because the holdingbranch matches the reserve branch doesn't mean the item
546                 # has arrived at the destination, check for an open transfer for the item as well
547                 my ( $transfertwhen, $transfertfrom, $transferto ) =
548                   C4::Circulation::GetTransfers( $res->itemnumber() );
549                 if ( not $transferto or $transferto ne $res->branchcode() ) {
550                     $reserve{'atdestination'} = 1;
551                 }
552             }
553
554             # set found to 1 if reserve is waiting for patron pickup
555             $reserve{'found'}     = $res->is_found();
556             $reserve{'intransit'} = $res->is_in_transit();
557         }
558         elsif ( $res->priority() > 0 ) {
559             if ( my $item = $res->item() )  {
560                 $reserve{'itemnumber'}      = $item->id();
561                 $reserve{'barcodenumber'}   = $item->barcode();
562                 $reserve{'item_level_hold'} = 1;
563             }
564         }
565
566         #     get borrowers reserve info
567         if ( C4::Context->preference('HidePatronName') ) {
568             $reserve{'hidename'}   = 1;
569             $reserve{'cardnumber'} = $res->borrower()->cardnumber();
570         }
571         $reserve{'expirationdate'} = output_pref( { dt => dt_from_string( $res->expirationdate ), dateonly => 1 } )
572           unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
573         $reserve{'date'}           = output_pref( { dt => dt_from_string( $res->reservedate ), dateonly => 1 } );
574         $reserve{'borrowernumber'} = $res->borrowernumber();
575         $reserve{'biblionumber'}   = $res->biblionumber();
576         $reserve{'borrowernumber'} = $res->borrowernumber();
577         $reserve{'firstname'}      = $res->borrower()->firstname();
578         $reserve{'surname'}        = $res->borrower()->surname();
579         $reserve{'notes'}          = $res->reservenotes();
580         $reserve{'waiting_date'}   = $res->waitingdate();
581         $reserve{'waiting_until'}  = $res->is_waiting() ? $res->waiting_expires_on() : undef;
582         $reserve{'ccode'}          = $res->item() ? $res->item()->ccode() : undef;
583         $reserve{'barcode'}        = $res->item() ? $res->item()->barcode() : undef;
584         $reserve{'priority'}       = $res->priority();
585         $reserve{'lowestPriority'} = $res->lowestPriority();
586         $reserve{'optionloop'}     = \@optionloop;
587         $reserve{'suspend'}        = $res->suspend();
588         $reserve{'suspend_until'}  = $res->suspend_until();
589         $reserve{'reserve_id'}     = $res->reserve_id();
590         $reserve{itemtype}         = $res->itemtype();
591
592         if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
593             $reserve{'branchloop'} = [ Koha::Libraries->find( $res->branchcode() ) ];
594         }
595         else {
596             $reserve{'branchloop'} = GetBranchesLoop( $res->branchcode() );
597         }
598
599         push( @reserveloop, \%reserve );
600     }
601
602     # get the time for the form name...
603     my $time = time();
604
605     $template->param(
606                      branchloop  => GetBranchesLoop($userbranch),
607                      time        => $time,
608                      fixedRank   => $fixedRank,
609                     );
610
611     # display infos
612     $template->param(
613                      optionloop        => \@optionloop,
614                      bibitemloop       => \@bibitemloop,
615                      itemdata_enumchron => $itemdata_enumchron,
616                      date              => $date,
617                      biblionumber      => $biblionumber,
618                      findborrower      => $findborrower,
619                      title             => $dat->{title},
620                      author            => $dat->{author},
621                      holdsview => 1,
622                      C4::Search::enabled_staff_search_views,
623                     );
624     if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
625         $template->param(
626                      borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
627                      borrower_branchcode => $borrowerinfo->{'branchcode'},
628         );
629     }
630
631     $biblioloopiter{biblionumber} = $biblionumber;
632     $biblioloopiter{title} = $dat->{title};
633     $biblioloopiter{rank} = $fixedRank;
634     $biblioloopiter{reserveloop} = \@reserveloop;
635
636     if (@reserveloop) {
637         $template->param( reserveloop => \@reserveloop );
638     }
639
640     push @biblioloop, \%biblioloopiter;
641 }
642
643 $template->param( biblioloop => \@biblioloop );
644 $template->param( biblionumbers => $biblionumbers );
645 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
646 $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
647
648 if ($multihold) {
649     $template->param( multi_hold => 1 );
650 }
651
652 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
653     $template->param( reserve_in_future => 1 );
654 }
655
656 $template->param(
657     SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
658     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
659 );
660
661 # printout the page
662 output_html_with_http_headers $input, $cookie, $template->output;
663
664 sub sort_borrowerlist {
665     my $borrowerslist = shift;
666     my $ref           = [];
667     push @{$ref}, sort {
668         uc( $a->{surname} . $a->{firstname} ) cmp
669           uc( $b->{surname} . $b->{firstname} )
670     } @{$borrowerslist};
671     return $ref;
672 }