Bug 29136: Ajaxify the patron search when placing a hold
[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 CGI qw ( -utf8 );
32 use List::MoreUtils qw( uniq );
33 use Date::Calc qw( Date_to_Days );
34 use C4::Output qw( output_html_with_http_headers );
35 use C4::Auth qw( get_template_and_user );
36 use C4::Reserves qw( RevertWaitingStatus AlterPriority ToggleLowestPriority ToggleSuspend CanBookBeReserved GetMaxPatronHoldsForRecord ItemsAnyAvailableAndNotRestricted CanItemBeReserved IsAvailableForItemLevelRequest );
37 use C4::Items qw( get_hostitemnumbers_of );
38 use C4::Koha qw( getitemtypeimagelocation );
39 use C4::Serials qw( CountSubscriptionFromBiblionumber );
40 use C4::Circulation qw( GetTransfers _GetCircControlBranch GetBranchItemRule );
41 use Koha::DateUtils qw( dt_from_string output_pref );
42 use C4::Utils::DataTables::Members;
43 use C4::Search qw( enabled_staff_search_views );
44
45 use Koha::Biblios;
46 use Koha::DateUtils qw( dt_from_string output_pref );
47 use Koha::Checkouts;
48 use Koha::Holds;
49 use Koha::CirculationRules;
50 use Koha::Items;
51 use Koha::ItemTypes;
52 use Koha::Libraries;
53 use Koha::Patrons;
54 use Koha::Clubs;
55 use Koha::BackgroundJob::BatchCancelHold;
56
57 my $dbh = C4::Context->dbh;
58 my $input = CGI->new;
59 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
60     {
61         template_name   => "reserve/request.tt",
62         query           => $input,
63         type            => "intranet",
64         flagsrequired   => { reserveforothers => 'place_holds' },
65     }
66 );
67
68 my $showallitems = $input->param('showallitems');
69 my $pickup = $input->param('pickup');
70
71 my $itemtypes = {
72     map {
73         $_->itemtype =>
74           { %{ $_->unblessed }, image_location => $_->image_location }
75     } Koha::ItemTypes->search_with_localization->as_list
76 };
77
78 # Select borrowers infos
79 my $findborrower = $input->param('findborrower');
80 $findborrower = '' unless defined $findborrower;
81 $findborrower =~ s|,| |g;
82 my $findclub = $input->param('findclub');
83 $findclub = '' unless defined $findclub && !$findborrower;
84 my $borrowernumber_hold = $input->param('borrowernumber') || '';
85 my $club_hold = $input->param('club')||'';
86 my $messageborrower;
87 my $messageclub;
88 my $warnings;
89 my $messages;
90 my $exceeded_maxreserves;
91 my $exceeded_holds_per_record;
92
93 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
94 my $action = $input->param('action');
95 $action ||= q{};
96
97 if ( $action eq 'move' ) {
98     my $where           = $input->param('where');
99     my $reserve_id      = $input->param('reserve_id');
100     my $prev_priority   = $input->param('prev_priority');
101     my $next_priority   = $input->param('next_priority');
102     my $first_priority  = $input->param('first_priority');
103     my $last_priority   = $input->param('last_priority');
104     my $hold_itemnumber = $input->param('itemnumber');
105     if ( $prev_priority == 0 && $next_priority == 1 ) {
106         C4::Reserves::RevertWaitingStatus( { itemnumber => $hold_itemnumber } );
107     }
108     else {
109         AlterPriority(
110             $where,         $reserve_id,     $prev_priority,
111             $next_priority, $first_priority, $last_priority
112         );
113     }
114 }
115 elsif ( $action eq 'cancel' ) {
116     my $reserve_id          = $input->param('reserve_id');
117     my $cancellation_reason = $input->param("cancellation-reason");
118     my $hold                = Koha::Holds->find($reserve_id);
119     $hold->cancel( { cancellation_reason => $cancellation_reason } ) if $hold;
120 }
121 elsif ( $action eq 'setLowestPriority' ) {
122     my $reserve_id = $input->param('reserve_id');
123     ToggleLowestPriority($reserve_id);
124 }
125 elsif ( $action eq 'toggleSuspend' ) {
126     my $reserve_id    = $input->param('reserve_id');
127     my $suspend_until = $input->param('suspend_until');
128     ToggleSuspend( $reserve_id, $suspend_until );
129 }
130 elsif ( $action eq 'cancelBulk' ) {
131     my $cancellation_reason = $input->param("cancellation-reason");
132     my @hold_ids            = split( ',', scalar $input->param("ids"));
133     my $params              = {
134         reason   => $cancellation_reason,
135         hold_ids => \@hold_ids,
136     };
137     my $job_id = Koha::BackgroundJob::BatchCancelHold->new->enqueue($params);
138
139     $template->param(
140         enqueued => 1,
141         job_id   => $job_id
142     );
143 }
144
145 if ($findborrower) {
146     my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
147     $borrowernumber_hold = $patron->borrowernumber if $patron;
148 }
149
150 if($findclub) {
151     my $club = Koha::Clubs->find( { name => $findclub } );
152     if( $club ) {
153         $club_hold = $club->id;
154     } else {
155         my @clubs = Koha::Clubs->search( [
156             { name => { like => '%'.$findclub.'%' } },
157             { description => { like => '%'.$findclub.'%' } }
158         ] )->as_list;
159         if( scalar @clubs == 1 ) {
160             $club_hold = $clubs[0]->id;
161         } elsif ( @clubs ) {
162             $template->param( clubs => \@clubs );
163         } else {
164             $messageclub = "'$findclub'";
165         }
166     }
167 }
168
169 my @biblionumbers = $input->multi_param('biblionumber');
170
171 my $multi_hold = @biblionumbers > 1;
172 $template->param(
173     multi_hold => $multi_hold,
174 );
175
176 # If we have the borrowernumber because we've performed an action, then we
177 # don't want to try to place another reserve.
178 if ($borrowernumber_hold && !$action) {
179     my $patron = Koha::Patrons->find( $borrowernumber_hold );
180     my $diffbranch;
181
182     # we check the reserves of the user, and if they can reserve a document
183     # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
184
185     my $reserves_count = $patron->holds->count;
186
187     my $new_reserves_count = scalar( @biblionumbers );
188
189     my $maxreserves = C4::Context->preference('maxreserves');
190     $template->param( maxreserves => $maxreserves );
191
192     if ( $maxreserves
193         && ( $reserves_count + $new_reserves_count > $maxreserves ) )
194     {
195         my $new_reserves_allowed =
196             $maxreserves - $reserves_count > 0
197           ? $maxreserves - $reserves_count
198           : 0;
199         $warnings             = 1;
200         $exceeded_maxreserves = 1;
201         $template->param(
202             new_reserves_allowed => $new_reserves_allowed,
203             new_reserves_count   => $new_reserves_count,
204             reserves_count       => $reserves_count,
205             maxreserves          => $maxreserves,
206         );
207     }
208
209     # check if the borrower make the reserv in a different branch
210     if ( $patron->branchcode ne C4::Context->userenv->{'branch'} ) {
211         $diffbranch = 1;
212     }
213
214     my $amount_outstanding = $patron->account->balance;
215     $template->param(
216                 patron              => $patron,
217                 diffbranch          => $diffbranch,
218                 messages            => $messages,
219                 warnings            => $warnings,
220                 amount_outstanding  => $amount_outstanding,
221     );
222 }
223
224 if ($club_hold && !$borrowernumber_hold && !$action) {
225     my $club = Koha::Clubs->find($club_hold);
226
227     my $enrollments = $club->club_enrollments;
228
229     my $maxreserves = C4::Context->preference('maxreserves');
230     my $new_reserves_count = scalar( @biblionumbers );
231
232     my @members;
233
234     while(my $enrollment = $enrollments->next) {
235         next if $enrollment->is_canceled;
236         my $member = { patron => $enrollment->patron };
237         my $reserves_count = $enrollment->patron->holds->count;
238         if ( $maxreserves
239             && ( $reserves_count + $new_reserves_count > $maxreserves ) )
240         {
241             $member->{new_reserves_allowed} = $maxreserves - $reserves_count > 0
242                 ? $maxreserves - $reserves_count
243                 : 0;
244             $member->{exceeded_maxreserves} = 1;
245         }
246         $member->{amount_outstanding} = $enrollment->patron->account->balance;
247         if ( $enrollment->patron->branchcode ne C4::Context->userenv->{'branch'} ) {
248             $member->{diffbranch} = 1;
249         }
250
251         push @members, $member;
252     }
253
254     $template->param(
255         club                => $club,
256         members             => \@members,
257         maxreserves         => $maxreserves,
258         new_reserves_count  => $new_reserves_count
259     );
260 }
261
262 unless ( $club_hold or $borrowernumber_hold ) {
263     $template->param( clubcount => Koha::Clubs->search->count );
264 }
265
266 $template->param(
267     messageborrower => $messageborrower,
268     messageclub     => $messageclub
269 );
270
271 # Load the hold list if
272 #  - we are searching for a patron or club and found one
273 #  - we are not searching for anything
274 if (   ( $findborrower && $borrowernumber_hold || $findclub && $club_hold )
275     || ( !$findborrower && !$findclub ) )
276 {
277     # FIXME launch another time GetMember perhaps until (Joubu: Why?)
278     my $patron = Koha::Patrons->find( $borrowernumber_hold );
279
280     if ( $patron && $multi_hold ) {
281         my @multi_pickup_locations =
282           Koha::Biblios->search( { biblionumber => \@biblionumbers } )
283           ->pickup_locations( { patron => $patron } )->as_list;
284         $template->param( multi_pickup_locations => \@multi_pickup_locations );
285     }
286
287     my $logged_in_patron = Koha::Patrons->find( $borrowernumber );
288
289     my $wants_check;
290     if ($patron) {
291         $wants_check = $patron->wants_check_for_previous_checkout;
292     }
293     my $itemdata_enumchron = 0;
294     my $itemdata_ccode = 0;
295     my @biblioloop = ();
296     my $no_reserves_allowed = 0;
297     foreach my $biblionumber (@biblionumbers) {
298         next unless $biblionumber =~ m|^\d+$|;
299
300         my %biblioloopiter = ();
301
302         my $biblio = Koha::Biblios->find( $biblionumber );
303         unless ($biblio) {
304             $biblioloopiter{noitems} = 1;
305             $template->param('nobiblio' => 1);
306             last;
307         }
308
309         if ( $patron ) {
310             { # CanBookBeReserved
311                 my $canReserve = CanBookBeReserved( $patron->borrowernumber, $biblionumber );
312                 if ( $canReserve->{status} eq 'OK' ) {
313
314                     #All is OK and we can continue
315                 }
316                 elsif ( $canReserve->{status} eq 'noReservesAllowed' || $canReserve->{status} eq 'notReservable' ) {
317                     $no_reserves_allowed = 1;
318                 }
319                 elsif ( $canReserve->{status} eq 'tooManyReserves' ) {
320                     $exceeded_maxreserves = 1;
321                     $template->param( maxreserves => $canReserve->{limit} );
322                 }
323                 elsif ( $canReserve->{status} eq 'tooManyHoldsForThisRecord' ) {
324                     $exceeded_holds_per_record = 1;
325                     $biblioloopiter{ $canReserve->{status} } = 1;
326                 }
327                 elsif ( $canReserve->{status} eq 'ageRestricted' ) {
328                     $template->param( $canReserve->{status} => 1 );
329                     $biblioloopiter{ $canReserve->{status} } = 1;
330                 }
331                 elsif ( $canReserve->{status} eq 'alreadypossession' ) {
332                     $template->param( $canReserve->{status} => 1);
333                     $biblioloopiter{ $canReserve->{status} } = 1;
334                 }
335                 else {
336                     $biblioloopiter{ $canReserve->{status} } = 1;
337                 }
338             }
339
340             # For multiple holds per record, if a patron has previously placed a hold,
341             # the patron can only place more holds of the same type. That is, if the
342             # patron placed a record level hold, all the holds the patron places must
343             # be record level. If the patron placed an item level hold, all holds
344             # the patron places must be item level
345             my $holds = Koha::Holds->search(
346                 {
347                     borrowernumber => $patron->borrowernumber,
348                     biblionumber   => $biblionumber,
349                     found          => undef,
350                 }
351             );
352             $template->param( force_hold_level => $holds->forced_hold_level() );
353
354             # For a librarian to be able to place multiple record holds for a patron for a record,
355             # we must find out what the maximum number of holds they can place for the patron is
356             my $max_holds_for_record = GetMaxPatronHoldsForRecord( $patron->borrowernumber, $biblionumber );
357             my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
358             $biblioloopiter{remaining_holds_for_record} = $max_holds_for_record;
359             $template->param( max_holds_for_record => $max_holds_for_record );
360             $template->param( remaining_holds_for_record => $remaining_holds_for_record );
361         }
362
363         # adding a fixed value for priority options
364         my $fixedRank = $biblio->holds->count + 1;
365
366         my @items = $biblio->items->as_list;
367
368         my @host_items = $biblio->host_items->as_list;
369         if (@host_items) {
370             push @items, @host_items;
371         }
372
373         unless ( @items ) {
374             # FIXME Then why do we continue?
375             $template->param('noitems' => 1) unless ( $multi_hold );
376             $biblioloopiter{noitems} = 1;
377         }
378
379         if ( $club_hold or $borrowernumber_hold ) {
380             my @available_itemtypes;
381             my $num_available = 0;
382             my $num_override  = 0;
383             my $hiddencount   = 0;
384             my $num_alreadyheld = 0;
385
386             # iterating through all items first to check if any of them available
387             # to pass this value further inside down to IsAvailableForItemLevelRequest to
388             # it's complicated logic to analyse.
389             # (before this loop was inside that sub loop so it was O(n^2) )
390             my $items_any_available;
391             $items_any_available = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio->biblionumber, patron => $patron })
392                 if $patron;
393
394             for my $item_object ( @items ) {
395                 my $do_check;
396                 my $item = $item_object->unblessed;
397                 if ( $patron ) {
398                     $do_check = $patron->do_check_for_previous_checkout($item) if $wants_check;
399                     if ( $do_check && $wants_check ) {
400                         $item->{checked_previously} = $do_check;
401                         if ( $multi_hold ) {
402                             $biblioloopiter{checked_previously} = $do_check;
403                         } else {
404                             $template->param( checked_previously => $do_check );
405                         }
406                     }
407                 }
408
409                 $item->{itemtype} = $itemtypes->{ $item_object->effective_itemtype };
410
411                 if($item->{biblionumber} ne $biblio->biblionumber){
412                     $item->{hosttitle} = Koha::Biblios->find( $item->{biblionumber} )->title;
413                 }
414
415                 # if the item is currently on loan, we display its return date and
416                 # change the background color
417                 my $issue = $item_object->checkout;
418                 if ( $issue ) { # FIXME must be moved to the template
419                     $item->{date_due} = $issue->date_due;
420                     $item->{backgroundcolor} = 'onloan';
421                 }
422
423                 # checking reserve
424                 my $holds = $item_object->current_holds;
425                 if ( my $first_hold = $holds->next ) {
426                     my $p = Koha::Patrons->find( $first_hold->borrowernumber );
427
428                     $item->{backgroundcolor} = 'reserved';
429                     $item->{reservedate}     = output_pref({ dt => dt_from_string( $first_hold->reservedate ), dateonly => 1 }); # FIXME Should be formatted in the template
430                     $item->{ReservedFor}     = $p;
431                     $item->{ExpectedAtLibrary}     = $first_hold->branchcode;
432                     $item->{waitingdate} = $first_hold->waitingdate;
433                 }
434
435                 # Management of the notforloan document
436                 if ( $item->{notforloan} ) {
437                     $item->{backgroundcolor} = 'other';
438                 }
439
440                 # Management of lost or long overdue items
441                 if ( $item->{itemlost} ) {
442                     $item->{backgroundcolor} = 'other';
443                     if ($logged_in_patron->category->hidelostitems && !$showallitems) {
444                         $item->{hide} = 1;
445                         $hiddencount++;
446                     }
447                 }
448
449                 # Check the transit status
450                 my ( $transfertwhen, $transfertfrom, $transfertto ) =
451                   GetTransfers($item_object->itemnumber); # FIXME replace with get_transfer
452
453                 if ( defined $transfertwhen && $transfertwhen ne '' ) {
454                     $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
455                     $item->{transfertfrom} = $transfertfrom;
456                     $item->{transfertto} = $transfertto;
457                     $item->{nocancel} = 1;
458                 }
459
460                 # If there is no loan, return and transfer, we show a checkbox.
461                 $item->{notforloan} ||= 0;
462
463                 # if independent branches is on we need to check if the person can reserve
464                 # for branches they arent logged in to
465                 if ( C4::Context->preference("IndependentBranches") ) {
466                     if (! C4::Context->preference("canreservefromotherbranches")){
467                         # can't reserve items so need to check if item homebranch and userenv branch match if not we can't reserve
468                         my $userenv = C4::Context->userenv;
469                         unless ( C4::Context->IsSuperLibrarian ) {
470                             $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
471                         }
472                     }
473                 }
474
475                 if ( $patron ) {
476                     my $patron_unblessed = $patron->unblessed;
477                     my $branch = C4::Circulation::_GetCircControlBranch($item, $patron_unblessed);
478
479                     my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
480
481                     $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
482
483                     my $can_item_be_reserved = CanItemBeReserved( $patron, $item_object )->{status};
484                     $item->{not_holdable} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
485
486                     $item->{item_level_holds} = Koha::CirculationRules->get_opacitemholds_policy( { item => $item_object, patron => $patron } );
487
488                     if (
489                            !$item->{cantreserve}
490                         && !$exceeded_maxreserves
491                         && $can_item_be_reserved eq 'OK'
492                         # items_any_available defined outside of the current loop,
493                         # so we avoiding loop inside IsAvailableForItemLevelRequest:
494                         && IsAvailableForItemLevelRequest($item_object, $patron, undef, $items_any_available)
495                       )
496                     {
497                         # Send the pickup locations count to the UI, the pickup locations will be pulled using the API
498                         my $pickup_locations = $item_object->pickup_locations({ patron => $patron });
499                         $item->{pickup_locations_count} = $pickup_locations->count;
500                         if ( $item->{pickup_locations_count} > 0 ) {
501                             $num_available++;
502                             $item->{available} = 1;
503                             # pass the holding branch for use as default
504                             my $default_pickup_location = $pickup_locations->search({ branchcode => $item->{holdingbranch} })->next;
505                             $item->{default_pickup_location} = $default_pickup_location;
506                         }
507                         else {
508                             $item->{available} = 0;
509                             $item->{not_holdable} = "no_valid_pickup_location";
510                         }
511
512                         push( @available_itemtypes, $item->{itype} );
513                     }
514                     elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
515                         # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
516                         # with the exception of itemAlreadyOnHold because, you know, the item is already on hold
517                         if ( $can_item_be_reserved ne 'itemAlreadyOnHold' ) {
518                             # Send the pickup locations count to the UI, the pickup locations will be pulled using the API
519                             my @pickup_locations = $item_object->pickup_locations({ patron => $patron })->as_list;
520                             $item->{pickup_locations_count} = scalar @pickup_locations;
521
522                             if ( @pickup_locations ) {
523                                 $num_available++;
524                                 $item->{available} = 1;
525
526                                 my $default_pickup_location;
527
528                                 # Default to logged-in, if valid
529                                 if ( C4::Context->userenv->{branch} ) {
530                                     ($default_pickup_location) = grep { $_->branchcode eq C4::Context->userenv->{branch} } @pickup_locations;
531                                 }
532
533                                 $item->{default_pickup_location} = $default_pickup_location;
534                             }
535                             else {
536                                 $item->{available} = 0;
537                                 $item->{not_holdable} = "no_valid_pickup_location";
538                             }
539                         } else { $num_alreadyheld++ }
540
541                         push( @available_itemtypes, $item->{itype} );
542                     }
543
544                     # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
545
546                     # Show serial enumeration when needed
547                     if ($item->{enumchron}) {
548                         $itemdata_enumchron = 1;
549                     }
550                     # Show collection when needed
551                     if ($item->{ccode}) {
552                         $itemdata_ccode = 1;
553                     }
554                 }
555
556                 push @{ $biblioloopiter{itemloop} }, $item;
557             }
558
559             $biblioloopiter{biblioitem} = $biblio->biblioitem;
560
561             # While we can't override an alreay held item, we should be able to override the others
562             # Unless all items are already held
563             if ( $num_override > 0 && ($num_override + $num_alreadyheld) == scalar( @{ $biblioloopiter{itemloop} } ) ) {
564             # That is, if all items require an override
565                 $template->param( override_required => 1 );
566             } elsif ( $num_available == 0 ) {
567                 $template->param( none_available => 1 );
568                 $biblioloopiter{warn} = 1;
569                 $biblioloopiter{none_avail} = 1;
570             }
571             $template->param( hiddencount => $hiddencount);
572
573             @available_itemtypes = uniq( @available_itemtypes );
574             $template->param( available_itemtypes => \@available_itemtypes );
575         }
576
577         # existingreserves building
578         my @reserveloop;
579         my $always_show_holds = $input->cookie('always_show_holds');
580         $template->param( always_show_holds => $always_show_holds );
581         my $show_holds_now = $input->param('show_holds_now');
582         unless( (defined $always_show_holds && $always_show_holds eq 'DONT') && !$show_holds_now ){
583             my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } )->as_list;
584             foreach my $res (
585                 sort {
586                     my $a_found = $a->found() || '';
587                     my $b_found = $a->found() || '';
588                     $a_found cmp $b_found;
589                 } @reserves
590               )
591             {
592                 my %reserve;
593                 if ( $res->is_found() ) {
594                     $reserve{'holdingbranch'} = $res->item()->holdingbranch();
595                     $reserve{'biblionumber'}  = $res->item()->biblionumber();
596                     $reserve{'barcodenumber'} = $res->item()->barcode();
597                     $reserve{'wbrcode'}       = $res->branchcode();
598                     $reserve{'itemnumber'}    = $res->itemnumber();
599                     $reserve{'wbrname'}       = $res->branch()->branchname();
600                     $reserve{'atdestination'} = $res->is_at_destination();
601                     $reserve{'desk_name'}     = ( $res->desk() ) ? $res->desk()->desk_name() : '' ;
602                     $reserve{'found'}     = $res->is_found();
603                     $reserve{'inprocessing'} = $res->is_in_processing();
604                     $reserve{'intransit'} = $res->is_in_transit();
605                 }
606                 elsif ( $res->priority() > 0 ) {
607                     if ( my $item = $res->item() )  {
608                         $reserve{'itemnumber'}      = $item->id();
609                         $reserve{'barcodenumber'}   = $item->barcode();
610                         $reserve{'item_level_hold'} = 1;
611                     }
612                 }
613
614                 $reserve{'expirationdate'} = $res->expirationdate;
615                 $reserve{'date'}           = $res->reservedate;
616                 $reserve{'borrowernumber'} = $res->borrowernumber();
617                 $reserve{'biblionumber'}   = $res->biblionumber();
618                 $reserve{'patron'}         = $res->borrower;
619                 $reserve{'notes'}          = $res->reservenotes();
620                 $reserve{'waiting_date'}   = $res->waitingdate();
621                 $reserve{'ccode'}          = $res->item() ? $res->item()->ccode() : undef;
622                 $reserve{'barcode'}        = $res->item() ? $res->item()->barcode() : undef;
623                 $reserve{'priority'}       = $res->priority();
624                 $reserve{'lowestPriority'} = $res->lowestPriority();
625                 $reserve{'suspend'}        = $res->suspend();
626                 $reserve{'suspend_until'}  = $res->suspend_until();
627                 $reserve{'reserve_id'}     = $res->reserve_id();
628                 $reserve{itemtype}         = $res->itemtype();
629                 $reserve{branchcode}       = $res->branchcode();
630                 $reserve{non_priority}     = $res->non_priority();
631                 $reserve{object}           = $res;
632
633                 push( @reserveloop, \%reserve );
634             }
635         }
636
637         # get the time for the form name...
638         my $time = time();
639
640         $template->param(
641                          time        => $time,
642                          fixedRank   => $fixedRank,
643                         );
644
645         # display infos
646         $template->param(
647                          itemdata_enumchron => $itemdata_enumchron,
648                          itemdata_ccode    => $itemdata_ccode,
649                          date              => $date,
650                          biblionumber      => $biblionumber,
651                          findborrower      => $findborrower,
652                          biblio            => $biblio,
653                          holdsview         => 1,
654                          C4::Search::enabled_staff_search_views,
655                         );
656
657         $biblioloopiter{biblionumber} = $biblionumber;
658         $biblioloopiter{title}  = $biblio->title;
659         $biblioloopiter{author} = $biblio->author;
660         $biblioloopiter{rank} = $fixedRank;
661         $biblioloopiter{reserveloop} = \@reserveloop;
662
663         if (@reserveloop) {
664             $template->param( reserveloop => \@reserveloop );
665         }
666
667         if ( $patron ) {
668             # Add the valid pickup locations
669             my @pickup_locations = $biblio->pickup_locations({ patron => $patron })->as_list;
670             $biblioloopiter{pickup_locations} = \@pickup_locations;
671             $biblioloopiter{pickup_locations_codes} = [ map { $_->branchcode } @pickup_locations ];
672         }
673
674         push @biblioloop, \%biblioloopiter;
675     }
676
677     $template->param( biblioloop => \@biblioloop );
678     $template->param( no_reserves_allowed => $no_reserves_allowed );
679     $template->param( exceeded_maxreserves => $exceeded_maxreserves );
680     $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
681     # FIXME: getting just the first bib's result doesn't seem right
682     $template->param( subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumbers[0]));
683 } elsif ( ! $multi_hold ) {
684     my $biblio = Koha::Biblios->find( $biblionumbers[0] );
685     $template->param( biblio => $biblio );
686 }
687 $template->param( biblionumbers => \@biblionumbers );
688
689 # pass the userenv branch if no pickup location selected
690 $template->param( pickup => $pickup || C4::Context->userenv->{branch} );
691
692 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
693     $template->param( reserve_in_future => 1 );
694 }
695
696 $template->param(
697     SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
698     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
699     borrowernumber => $borrowernumber_hold,
700 );
701
702 # printout the page
703 output_html_with_http_headers $input, $cookie, $template->output;
704
705 sub sort_borrowerlist {
706     my $borrowerslist = shift;
707     my $ref           = [];
708     push @{$ref}, sort {
709         uc( $a->{surname} . $a->{firstname} ) cmp
710           uc( $b->{surname} . $b->{firstname} )
711     } @{$borrowerslist};
712     return $ref;
713 }