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