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