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