Bug 20479: Ease readability - do not enter the block if not logged in
[koha.git] / reserve / request.pl
blob5ff0cb7d41cb8c2375776d2f6596d8daeae18ce5
1 #!/usr/bin/perl
4 #written 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
8 # This file is part of Koha.
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.
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.
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>.
23 =head1 request.pl
25 script to place reserves/requests
27 =cut
29 use Modern::Perl;
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::Circulation;
41 use Koha::DateUtils;
42 use C4::Utils::DataTables::Members;
43 use C4::Members;
44 use C4::Search; # enabled_staff_search_views
46 use Koha::Biblios;
47 use Koha::DateUtils;
48 use Koha::Checkouts;
49 use Koha::Holds;
50 use Koha::IssuingRules;
51 use Koha::Items;
52 use Koha::ItemTypes;
53 use Koha::Libraries;
54 use Koha::Patrons;
56 my $dbh = C4::Context->dbh;
57 my $input = new CGI;
58 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
60 template_name => "reserve/request.tt",
61 query => $input,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => { reserveforothers => 'place_holds' },
68 my $multihold = $input->param('multi_hold');
69 $template->param(multi_hold => $multihold);
70 my $showallitems = $input->param('showallitems');
72 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s|,| |g;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
79 my $messageborrower;
80 my $warnings;
81 my $messages;
82 my $exceeded_maxreserves;
83 my $exceeded_holds_per_record;
85 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
86 my $action = $input->param('action');
87 $action ||= q{};
89 if ( $action eq 'move' ) {
90 my $where = $input->param('where');
91 my $reserve_id = $input->param('reserve_id');
92 AlterPriority( $where, $reserve_id );
93 } elsif ( $action eq 'cancel' ) {
94 my $reserve_id = $input->param('reserve_id');
95 my $hold = Koha::Holds->find( $reserve_id );
96 $hold->cancel if $hold;
97 } elsif ( $action eq 'setLowestPriority' ) {
98 my $reserve_id = $input->param('reserve_id');
99 ToggleLowestPriority( $reserve_id );
100 } elsif ( $action eq 'toggleSuspend' ) {
101 my $reserve_id = $input->param('reserve_id');
102 my $suspend_until = $input->param('suspend_until');
103 ToggleSuspend( $reserve_id, $suspend_until );
106 if ($findborrower) {
107 my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
108 if ( $patron ) {
109 $borrowernumber_hold = $patron->borrowernumber;
110 } else {
111 my $dt_params = { iDisplayLength => -1 };
112 my $results = C4::Utils::DataTables::Members::search(
114 searchmember => $findborrower,
115 dt_params => $dt_params,
118 my $borrowers = $results->{patrons};
119 if ( scalar @$borrowers == 1 ) {
120 $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
121 } elsif ( @$borrowers ) {
122 $template->param( borrowers => $borrowers );
123 } else {
124 $messageborrower = "'$findborrower'";
129 my @biblionumbers = ();
130 my $biblionumbers = $input->param('biblionumbers');
131 if ($multihold) {
132 @biblionumbers = split '/', $biblionumbers;
133 } else {
134 push @biblionumbers, $input->multi_param('biblionumber');
138 # If we have the borrowernumber because we've performed an action, then we
139 # don't want to try to place another reserve.
140 if ($borrowernumber_hold && !$action) {
141 my $patron = Koha::Patrons->find( $borrowernumber_hold );
142 my $diffbranch;
144 # we check the reserves of the user, and if they can reserve a document
145 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
147 my $reserves_count = $patron->holds->count;
149 my $new_reserves_count = scalar( @biblionumbers );
151 my $maxreserves = C4::Context->preference('maxreserves');
152 if ( $maxreserves
153 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
155 my $new_reserves_allowed =
156 $maxreserves - $reserves_count > 0
157 ? $maxreserves - $reserves_count
158 : 0;
159 $warnings = 1;
160 $exceeded_maxreserves = 1;
161 $template->param(
162 new_reserves_allowed => $new_reserves_allowed,
163 new_reserves_count => $new_reserves_count,
164 reserves_count => $reserves_count,
165 maxreserves => $maxreserves,
169 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
170 my $expiry_date = $patron->dateexpiry;
171 my $expiry = 0; # flag set if patron account has expired
172 if ($expiry_date and $expiry_date ne '0000-00-00' and
173 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
174 $expiry = 1;
177 # check if the borrower make the reserv in a different branch
178 if ( $patron->branchcode ne C4::Context->userenv->{'branch'} ) {
179 $diffbranch = 1;
182 my $amount_outstanding = $patron->account->balance;
183 $template->param(
184 patron => $patron,
185 expiry => $expiry,
186 diffbranch => $diffbranch,
187 messages => $messages,
188 warnings => $warnings,
189 amount_outstanding => $amount_outstanding,
193 $template->param( messageborrower => $messageborrower );
195 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
196 my $patron = Koha::Patrons->find( $borrowernumber_hold );
198 my $logged_in_patron = Koha::Patrons->find( $borrowernumber );
200 my $itemdata_enumchron = 0;
201 my @biblioloop = ();
202 foreach my $biblionumber (@biblionumbers) {
203 next unless $biblionumber =~ m|^\d+$|;
205 my %biblioloopiter = ();
207 my $biblio = Koha::Biblios->find( $biblionumber );
209 my $force_hold_level;
210 if ( $patron ) {
211 { # CanBookBeReserved
212 my $canReserve = CanBookBeReserved( $patron->borrowernumber, $biblionumber );
213 $canReserve //= '';
214 if ( $canReserve eq 'OK' ) {
216 #All is OK and we can continue
218 elsif ( $canReserve eq 'tooManyReserves' ) {
219 $exceeded_maxreserves = 1;
221 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
222 $exceeded_holds_per_record = 1;
223 $biblioloopiter{$canReserve} = 1;
225 elsif ( $canReserve eq 'ageRestricted' ) {
226 $template->param( $canReserve => 1 );
227 $biblioloopiter{$canReserve} = 1;
229 else {
230 $biblioloopiter{$canReserve} = 1;
234 # For multiple holds per record, if a patron has previously placed a hold,
235 # the patron can only place more holds of the same type. That is, if the
236 # patron placed a record level hold, all the holds the patron places must
237 # be record level. If the patron placed an item level hold, all holds
238 # the patron places must be item level
239 my $holds = Koha::Holds->search(
241 borrowernumber => $patron->borrowernumber,
242 biblionumber => $biblionumber,
243 found => undef,
246 $force_hold_level = $holds->forced_hold_level();
247 $biblioloopiter{force_hold_level} = $force_hold_level;
248 $template->param( force_hold_level => $force_hold_level );
250 # For a librarian to be able to place multiple record holds for a patron for a record,
251 # we must find out what the maximum number of holds they can place for the patron is
252 my $max_holds_for_record = GetMaxPatronHoldsForRecord( $patron->borrowernumber, $biblionumber );
253 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
254 $biblioloopiter{remaining_holds_for_record} = $max_holds_for_record;
255 $template->param( max_holds_for_record => $max_holds_for_record );
256 $template->param( remaining_holds_for_record => $remaining_holds_for_record );
258 { # alreadypossession
259 # Check to see if patron is allowed to place holds on records where the
260 # patron already has an item from that record checked out
261 if ( !C4::Context->preference('AllowHoldsOnPatronsPossessions')
262 && CheckIfIssuedToPatron( $patron->borrowernumber, $biblionumber ) )
264 $template->param( alreadypossession => 1, );
270 my $count = Koha::Holds->search( { biblionumber => $biblionumber } )->count();
271 my $totalcount = $count;
273 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
274 # make priorities options
276 my @optionloop;
277 for ( 1 .. $count + 1 ) {
278 push(
279 @optionloop,
281 num => $_,
282 selected => ( $_ == $count + 1 ),
286 # adding a fixed value for priority options
287 my $fixedRank = $count+1;
289 my %itemnumbers_of_biblioitem;
291 my @hostitems = get_hostitemnumbers_of($biblionumber);
292 my @itemnumbers;
293 if (@hostitems){
294 $template->param('hostitemsflag' => 1);
295 push(@itemnumbers, @hostitems);
298 my $items = Koha::Items->search({ -or => { biblionumber => $biblionumber, itemnumber => { in => \@itemnumbers } } });
300 unless ( $items->count ) {
301 # FIXME Then why do we continue?
302 $template->param('noitems' => 1);
303 $biblioloopiter{noitems} = 1;
306 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
307 ## when by definition all of the itemnumber have the same biblioitemnumber
308 my ( $iteminfos_of );
309 while ( my $item = $items->next ) {
310 $item = $item->unblessed;
311 my $biblioitemnumber = $item->{biblioitemnumber};
312 my $itemnumber = $item->{itemnumber};
313 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
314 $iteminfos_of->{$itemnumber} = $item;
317 ## Should be same as biblionumber
318 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
320 my $biblioiteminfos_of = {
321 map {
322 my $biblioitem = $_;
323 ( $biblioitem->{biblioitemnumber} => $biblioitem )
324 } @{ Koha::Biblioitems->search(
325 { biblioitemnumber => { -in => \@biblioitemnumbers } },
326 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
327 )->unblessed
331 my $frameworkcode = GetFrameworkCode( $biblionumber );
332 my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
333 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
335 my @bibitemloop;
337 my @available_itemtypes;
338 foreach my $biblioitemnumber (@biblioitemnumbers) {
339 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
340 my $num_available = 0;
341 my $num_override = 0;
342 my $hiddencount = 0;
344 $biblioitem->{force_hold_level} = $force_hold_level;
346 if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
347 $biblioitem->{hostitemsflag} = 1;
350 $biblioloopiter{description} = $biblioitem->{description};
351 $biblioloopiter{itypename} = $biblioitem->{description};
352 if ( $biblioitem->{itemtype} ) {
354 $biblioitem->{description} =
355 $itemtypes->{ $biblioitem->{itemtype} }{description};
357 $biblioloopiter{imageurl} =
358 getitemtypeimagelocation( 'intranet',
359 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
362 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
363 my $item = $iteminfos_of->{$itemnumber};
365 $item->{force_hold_level} = $force_hold_level;
367 unless (C4::Context->preference('item-level_itypes')) {
368 $item->{itype} = $biblioitem->{itemtype};
371 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
372 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
373 $item->{homebranch} = $item->{homebranch};
375 # if the holdingbranch is different than the homebranch, we show the
376 # holdingbranch of the document too
377 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
378 $item->{holdingbranch} = $item->{holdingbranch};
381 if($item->{biblionumber} ne $biblionumber){
382 $item->{hostitemsflag} = 1;
383 $item->{hosttitle} = Koha::Biblios->find( $item->{biblionumber} )->title;
386 # if the item is currently on loan, we display its return date and
387 # change the background color
388 my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } );
389 if ( $issue ) {
390 $item->{date_due} = $issue->date_due;
391 $item->{backgroundcolor} = 'onloan';
394 # checking reserve
395 my $item_object = Koha::Items->find( $itemnumber );
396 my $holds = $item_object->current_holds;
397 if ( my $first_hold = $holds->next ) {
398 my $p = Koha::Patrons->find( $first_hold->borrowernumber );
400 $item->{backgroundcolor} = 'reserved';
401 $item->{reservedate} = output_pref({ dt => dt_from_string( $first_hold->reservedate ), dateonly => 1 }); # FIXME Should be formatted in the template
402 $item->{ReservedFor} = $p;
403 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
404 $item->{waitingdate} = $first_hold->waitingdate;
407 # Management of the notforloan document
408 if ( $item->{notforloan} ) {
409 $item->{backgroundcolor} = 'other';
410 $item->{notforloanvalue} =
411 $notforloan_label_of->{ $item->{notforloan} };
414 # Management of lost or long overdue items
415 if ( $item->{itemlost} ) {
417 # FIXME localized strings should never be in Perl code
418 $item->{message} =
419 $item->{itemlost} == 1 ? "(lost)"
420 : $item->{itemlost} == 2 ? "(long overdue)"
421 : "";
422 $item->{backgroundcolor} = 'other';
423 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
424 $item->{hide} = 1;
425 $hiddencount++;
429 # Check the transit status
430 my ( $transfertwhen, $transfertfrom, $transfertto ) =
431 GetTransfers($itemnumber);
433 if ( defined $transfertwhen && $transfertwhen ne '' ) {
434 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
435 $item->{transfertfrom} = $transfertfrom;
436 $item->{transfertto} = $transfertto;
437 $item->{nocancel} = 1;
440 # If there is no loan, return and transfer, we show a checkbox.
441 $item->{notforloan} ||= 0;
443 # if independent branches is on we need to check if the person can reserve
444 # for branches they arent logged in to
445 if ( C4::Context->preference("IndependentBranches") ) {
446 if (! C4::Context->preference("canreservefromotherbranches")){
447 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
448 my $userenv = C4::Context->userenv;
449 unless ( C4::Context->IsSuperLibrarian ) {
450 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
455 if ( $patron ) {
456 my $patron_unblessed = $patron->unblessed;
457 my $branch = C4::Circulation::_GetCircControlBranch($item, $patron_unblessed);
459 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
461 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
463 my $can_item_be_reserved = CanItemBeReserved( $patron->borrowernumber, $itemnumber );
464 $item->{not_holdable} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
466 $item->{item_level_holds} = Koha::IssuingRules->get_opacitemholds_policy( { item => $item_object, patron => $patron } );
468 if (
469 !$item->{cantreserve}
470 && !$exceeded_maxreserves
471 && IsAvailableForItemLevelRequest($item, $patron_unblessed)
472 && $can_item_be_reserved eq 'OK'
475 $item->{available} = 1;
476 $num_available++;
478 push( @available_itemtypes, $item->{itype} );
480 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
481 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
482 $item->{override} = 1;
483 $num_override++;
485 push( @available_itemtypes, $item->{itype} );
488 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
490 # Show serial enumeration when needed
491 if ($item->{enumchron}) {
492 $itemdata_enumchron = 1;
496 push @{ $biblioitem->{itemloop} }, $item;
499 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
500 $template->param( override_required => 1 );
501 } elsif ( $num_available == 0 ) {
502 $template->param( none_available => 1 );
503 $biblioloopiter{warn} = 1;
504 $biblioloopiter{none_avail} = 1;
506 $template->param( hiddencount => $hiddencount);
508 push @bibitemloop, $biblioitem;
511 @available_itemtypes = uniq( @available_itemtypes );
512 $template->param( available_itemtypes => \@available_itemtypes );
514 # existingreserves building
515 my @reserveloop;
516 my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } );
517 foreach my $res (
518 sort {
519 my $a_found = $a->found() || '';
520 my $b_found = $a->found() || '';
521 $a_found cmp $b_found;
522 } @reserves
525 my $priority = $res->priority();
526 my %reserve;
527 my @optionloop;
528 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
529 push(
530 @optionloop,
532 num => $i,
533 selected => ( $i == $priority ),
538 if ( $res->is_found() ) {
539 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
540 $reserve{'biblionumber'} = $res->item()->biblionumber();
541 $reserve{'barcodenumber'} = $res->item()->barcode();
542 $reserve{'wbrcode'} = $res->branchcode();
543 $reserve{'itemnumber'} = $res->itemnumber();
544 $reserve{'wbrname'} = $res->branch()->branchname();
546 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
548 # Just because the holdingbranch matches the reserve branch doesn't mean the item
549 # has arrived at the destination, check for an open transfer for the item as well
550 my ( $transfertwhen, $transfertfrom, $transferto ) =
551 C4::Circulation::GetTransfers( $res->itemnumber() );
552 if ( not $transferto or $transferto ne $res->branchcode() ) {
553 $reserve{'atdestination'} = 1;
557 # set found to 1 if reserve is waiting for patron pickup
558 $reserve{'found'} = $res->is_found();
559 $reserve{'intransit'} = $res->is_in_transit();
561 elsif ( $res->priority() > 0 ) {
562 if ( my $item = $res->item() ) {
563 $reserve{'itemnumber'} = $item->id();
564 $reserve{'barcodenumber'} = $item->barcode();
565 $reserve{'item_level_hold'} = 1;
569 $reserve{'expirationdate'} = output_pref( { dt => dt_from_string( $res->expirationdate ), dateonly => 1 } )
570 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
571 $reserve{'date'} = output_pref( { dt => dt_from_string( $res->reservedate ), dateonly => 1 } );
572 $reserve{'borrowernumber'} = $res->borrowernumber();
573 $reserve{'biblionumber'} = $res->biblionumber();
574 $reserve{'patron'} = $res->borrower;
575 $reserve{'notes'} = $res->reservenotes();
576 $reserve{'waiting_date'} = $res->waitingdate();
577 $reserve{'ccode'} = $res->item() ? $res->item()->ccode() : undef;
578 $reserve{'barcode'} = $res->item() ? $res->item()->barcode() : undef;
579 $reserve{'priority'} = $res->priority();
580 $reserve{'lowestPriority'} = $res->lowestPriority();
581 $reserve{'optionloop'} = \@optionloop;
582 $reserve{'suspend'} = $res->suspend();
583 $reserve{'suspend_until'} = $res->suspend_until();
584 $reserve{'reserve_id'} = $res->reserve_id();
585 $reserve{itemtype} = $res->itemtype();
586 $reserve{branchcode} = $res->branchcode();
588 push( @reserveloop, \%reserve );
591 # get the time for the form name...
592 my $time = time();
594 $template->param(
595 time => $time,
596 fixedRank => $fixedRank,
599 # display infos
600 $template->param(
601 optionloop => \@optionloop,
602 bibitemloop => \@bibitemloop,
603 itemdata_enumchron => $itemdata_enumchron,
604 date => $date,
605 biblionumber => $biblionumber,
606 findborrower => $findborrower,
607 title => $biblio->title,
608 author => $biblio->author,
609 holdsview => 1,
610 C4::Search::enabled_staff_search_views,
613 $biblioloopiter{biblionumber} = $biblionumber;
614 $biblioloopiter{title} = $biblio->title;
615 $biblioloopiter{rank} = $fixedRank;
616 $biblioloopiter{reserveloop} = \@reserveloop;
618 if (@reserveloop) {
619 $template->param( reserveloop => \@reserveloop );
622 push @biblioloop, \%biblioloopiter;
625 $template->param( biblioloop => \@biblioloop );
626 $template->param( biblionumbers => $biblionumbers );
627 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
628 $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
630 if ($multihold) {
631 $template->param( multi_hold => 1 );
634 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
635 $template->param( reserve_in_future => 1 );
638 $template->param(
639 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
640 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
643 # printout the page
644 output_html_with_http_headers $input, $cookie, $template->output;
646 sub sort_borrowerlist {
647 my $borrowerslist = shift;
648 my $ref = [];
649 push @{$ref}, sort {
650 uc( $a->{surname} . $a->{firstname} ) cmp
651 uc( $b->{surname} . $b->{firstname} )
652 } @{$borrowerslist};
653 return $ref;