Bug 21654: Remove refund_lost_item_fee_rules.sql from the installer process
[koha.git] / reserve / request.pl
blob9e21963939d3c7f6cac14fd09a3354d06a017073
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::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
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;
57 my $dbh = C4::Context->dbh;
58 my $input = new CGI;
59 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
61 template_name => "reserve/request.tt",
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { reserveforothers => 'place_holds' },
69 my $showallitems = $input->param('showallitems');
71 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
73 # Select borrowers infos
74 my $findborrower = $input->param('findborrower');
75 $findborrower = '' unless defined $findborrower;
76 $findborrower =~ s|,| |g;
77 my $borrowernumber_hold = $input->param('borrowernumber') || '';
78 my $messageborrower;
79 my $warnings;
80 my $messages;
81 my $exceeded_maxreserves;
82 my $exceeded_holds_per_record;
84 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
85 my $action = $input->param('action');
86 $action ||= q{};
88 if ( $action eq 'move' ) {
89 my $where = $input->param('where');
90 my $reserve_id = $input->param('reserve_id');
91 my $prev_priority = $input->param('prev_priority');
92 my $next_priority = $input->param('next_priority');
93 my $first_priority = $input->param('first_priority');
94 my $last_priority = $input->param('last_priority');
95 AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
96 } elsif ( $action eq 'cancel' ) {
97 my $reserve_id = $input->param('reserve_id');
98 my $hold = Koha::Holds->find( $reserve_id );
99 $hold->cancel if $hold;
100 } elsif ( $action eq 'setLowestPriority' ) {
101 my $reserve_id = $input->param('reserve_id');
102 ToggleLowestPriority( $reserve_id );
103 } elsif ( $action eq 'toggleSuspend' ) {
104 my $reserve_id = $input->param('reserve_id');
105 my $suspend_until = $input->param('suspend_until');
106 ToggleSuspend( $reserve_id, $suspend_until );
109 if ($findborrower) {
110 my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
111 if ( $patron ) {
112 $borrowernumber_hold = $patron->borrowernumber;
113 } else {
114 my $dt_params = { iDisplayLength => -1 };
115 my $results = C4::Utils::DataTables::Members::search(
117 searchmember => $findborrower,
118 dt_params => $dt_params,
121 my $borrowers = $results->{patrons};
122 if ( scalar @$borrowers == 1 ) {
123 $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
124 } elsif ( @$borrowers ) {
125 $template->param( borrowers => $borrowers );
126 } else {
127 $messageborrower = "'$findborrower'";
132 my @biblionumbers = ();
133 my $biblionumber = $input->param('biblionumber');
134 my $biblionumbers = $input->param('biblionumbers');
135 if ( $biblionumbers ) {
136 @biblionumbers = split '/', $biblionumbers;
137 } else {
138 push @biblionumbers, $input->multi_param('biblionumber');
141 # FIXME multi_hold should not be a variable but depends on the number of elements in @biblionumbers
142 $template->param(multi_hold => scalar $input->param('multi_hold'));
144 # If we have the borrowernumber because we've performed an action, then we
145 # don't want to try to place another reserve.
146 if ($borrowernumber_hold && !$action) {
147 my $patron = Koha::Patrons->find( $borrowernumber_hold );
148 my $diffbranch;
150 # we check the reserves of the user, and if they can reserve a document
151 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
153 my $reserves_count = $patron->holds->count;
155 my $new_reserves_count = scalar( @biblionumbers );
157 my $maxreserves = C4::Context->preference('maxreserves');
158 if ( $maxreserves
159 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
161 my $new_reserves_allowed =
162 $maxreserves - $reserves_count > 0
163 ? $maxreserves - $reserves_count
164 : 0;
165 $warnings = 1;
166 $exceeded_maxreserves = 1;
167 $template->param(
168 new_reserves_allowed => $new_reserves_allowed,
169 new_reserves_count => $new_reserves_count,
170 reserves_count => $reserves_count,
171 maxreserves => $maxreserves,
175 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
176 my $expiry_date = $patron->dateexpiry;
177 my $expiry = 0; # flag set if patron account has expired
178 if ($expiry_date and $expiry_date ne '0000-00-00' and
179 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
180 $expiry = 1;
183 # check if the borrower make the reserv in a different branch
184 if ( $patron->branchcode ne C4::Context->userenv->{'branch'} ) {
185 $diffbranch = 1;
188 my $amount_outstanding = $patron->account->balance;
189 $template->param(
190 patron => $patron,
191 expiry => $expiry,
192 diffbranch => $diffbranch,
193 messages => $messages,
194 warnings => $warnings,
195 amount_outstanding => $amount_outstanding,
199 $template->param( messageborrower => $messageborrower );
201 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
202 my $patron = Koha::Patrons->find( $borrowernumber_hold );
204 my $logged_in_patron = Koha::Patrons->find( $borrowernumber );
206 my $itemdata_enumchron = 0;
207 my @biblioloop = ();
208 foreach my $biblionumber (@biblionumbers) {
209 next unless $biblionumber =~ m|^\d+$|;
211 my %biblioloopiter = ();
213 my $biblio = Koha::Biblios->find( $biblionumber );
215 my $force_hold_level;
216 if ( $patron ) {
217 { # CanBookBeReserved
218 my $canReserve = CanBookBeReserved( $patron->borrowernumber, $biblionumber );
219 $canReserve->{status} //= '';
220 if ( $canReserve->{status} eq 'OK' ) {
222 #All is OK and we can continue
224 elsif ( $canReserve->{status} eq 'tooManyReserves' ) {
225 $exceeded_maxreserves = 1;
226 $template->param( maxreserves => $canReserve->{limit} );
228 elsif ( $canReserve->{status} eq 'tooManyHoldsForThisRecord' ) {
229 $exceeded_holds_per_record = 1;
230 $biblioloopiter{ $canReserve->{status} } = 1;
232 elsif ( $canReserve->{status} eq 'ageRestricted' ) {
233 $template->param( $canReserve->{status} => 1 );
234 $biblioloopiter{ $canReserve->{status} } = 1;
236 else {
237 $biblioloopiter{ $canReserve->{status} } = 1;
241 # For multiple holds per record, if a patron has previously placed a hold,
242 # the patron can only place more holds of the same type. That is, if the
243 # patron placed a record level hold, all the holds the patron places must
244 # be record level. If the patron placed an item level hold, all holds
245 # the patron places must be item level
246 my $holds = Koha::Holds->search(
248 borrowernumber => $patron->borrowernumber,
249 biblionumber => $biblionumber,
250 found => undef,
253 $force_hold_level = $holds->forced_hold_level();
254 $biblioloopiter{force_hold_level} = $force_hold_level;
255 $template->param( force_hold_level => $force_hold_level );
257 # For a librarian to be able to place multiple record holds for a patron for a record,
258 # we must find out what the maximum number of holds they can place for the patron is
259 my $max_holds_for_record = GetMaxPatronHoldsForRecord( $patron->borrowernumber, $biblionumber );
260 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
261 $biblioloopiter{remaining_holds_for_record} = $max_holds_for_record;
262 $template->param( max_holds_for_record => $max_holds_for_record );
263 $template->param( remaining_holds_for_record => $remaining_holds_for_record );
265 { # alreadypossession
266 # Check to see if patron is allowed to place holds on records where the
267 # patron already has an item from that record checked out
268 if ( !C4::Context->preference('AllowHoldsOnPatronsPossessions')
269 && CheckIfIssuedToPatron( $patron->borrowernumber, $biblionumber ) )
271 $template->param( alreadypossession => 1, );
277 my $count = Koha::Holds->search( { biblionumber => $biblionumber } )->count();
278 my $totalcount = $count;
280 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
281 # make priorities options
283 my @optionloop;
284 for ( 1 .. $count + 1 ) {
285 push(
286 @optionloop,
288 num => $_,
289 selected => ( $_ == $count + 1 ),
293 # adding a fixed value for priority options
294 my $fixedRank = $count+1;
296 my %itemnumbers_of_biblioitem;
298 my @hostitems = get_hostitemnumbers_of($biblionumber);
299 my @itemnumbers;
300 if (@hostitems){
301 $template->param('hostitemsflag' => 1);
302 push(@itemnumbers, @hostitems);
305 my $items = Koha::Items->search({ -or => { biblionumber => $biblionumber, itemnumber => { in => \@itemnumbers } } });
307 unless ( $items->count ) {
308 # FIXME Then why do we continue?
309 $template->param('noitems' => 1);
310 $biblioloopiter{noitems} = 1;
313 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
314 ## when by definition all of the itemnumber have the same biblioitemnumber
315 my ( $iteminfos_of );
316 while ( my $item = $items->next ) {
317 $item = $item->unblessed;
318 my $biblioitemnumber = $item->{biblioitemnumber};
319 my $itemnumber = $item->{itemnumber};
320 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
321 $iteminfos_of->{$itemnumber} = $item;
324 ## Should be same as biblionumber
325 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
327 my $biblioiteminfos_of = {
328 map {
329 my $biblioitem = $_;
330 ( $biblioitem->{biblioitemnumber} => $biblioitem )
331 } @{ Koha::Biblioitems->search(
332 { biblioitemnumber => { -in => \@biblioitemnumbers } },
333 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
334 )->unblessed
338 my $frameworkcode = GetFrameworkCode( $biblionumber );
339 my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
340 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
342 my @bibitemloop;
344 my @available_itemtypes;
345 foreach my $biblioitemnumber (@biblioitemnumbers) {
346 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
347 my $num_available = 0;
348 my $num_override = 0;
349 my $hiddencount = 0;
351 $biblioitem->{force_hold_level} = $force_hold_level;
353 if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
354 $biblioitem->{hostitemsflag} = 1;
357 $biblioloopiter{description} = $biblioitem->{description};
358 $biblioloopiter{itypename} = $biblioitem->{description};
359 if ( $biblioitem->{itemtype} ) {
361 $biblioitem->{description} =
362 $itemtypes->{ $biblioitem->{itemtype} }{description};
364 $biblioloopiter{imageurl} =
365 getitemtypeimagelocation( 'intranet',
366 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
369 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
370 my $item = $iteminfos_of->{$itemnumber};
372 $item->{force_hold_level} = $force_hold_level;
374 unless (C4::Context->preference('item-level_itypes')) {
375 $item->{itype} = $biblioitem->{itemtype};
378 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
379 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
380 $item->{homebranch} = $item->{homebranch};
382 # if the holdingbranch is different than the homebranch, we show the
383 # holdingbranch of the document too
384 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
385 $item->{holdingbranch} = $item->{holdingbranch};
388 if($item->{biblionumber} ne $biblionumber){
389 $item->{hostitemsflag} = 1;
390 $item->{hosttitle} = Koha::Biblios->find( $item->{biblionumber} )->title;
393 # if the item is currently on loan, we display its return date and
394 # change the background color
395 my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } );
396 if ( $issue ) {
397 $item->{date_due} = $issue->date_due;
398 $item->{backgroundcolor} = 'onloan';
401 # checking reserve
402 my $item_object = Koha::Items->find( $itemnumber );
403 my $holds = $item_object->current_holds;
404 if ( my $first_hold = $holds->next ) {
405 my $p = Koha::Patrons->find( $first_hold->borrowernumber );
407 $item->{backgroundcolor} = 'reserved';
408 $item->{reservedate} = output_pref({ dt => dt_from_string( $first_hold->reservedate ), dateonly => 1 }); # FIXME Should be formatted in the template
409 $item->{ReservedFor} = $p;
410 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
411 $item->{waitingdate} = $first_hold->waitingdate;
414 # Management of the notforloan document
415 if ( $item->{notforloan} ) {
416 $item->{backgroundcolor} = 'other';
417 $item->{notforloanvalue} =
418 $notforloan_label_of->{ $item->{notforloan} };
421 # Management of lost or long overdue items
422 if ( $item->{itemlost} ) {
424 # FIXME localized strings should never be in Perl code
425 $item->{message} =
426 $item->{itemlost} == 1 ? "(lost)"
427 : $item->{itemlost} == 2 ? "(long overdue)"
428 : "";
429 $item->{backgroundcolor} = 'other';
430 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
431 $item->{hide} = 1;
432 $hiddencount++;
436 # Check the transit status
437 my ( $transfertwhen, $transfertfrom, $transfertto ) =
438 GetTransfers($itemnumber);
440 if ( defined $transfertwhen && $transfertwhen ne '' ) {
441 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
442 $item->{transfertfrom} = $transfertfrom;
443 $item->{transfertto} = $transfertto;
444 $item->{nocancel} = 1;
447 # If there is no loan, return and transfer, we show a checkbox.
448 $item->{notforloan} ||= 0;
450 # if independent branches is on we need to check if the person can reserve
451 # for branches they arent logged in to
452 if ( C4::Context->preference("IndependentBranches") ) {
453 if (! C4::Context->preference("canreservefromotherbranches")){
454 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
455 my $userenv = C4::Context->userenv;
456 unless ( C4::Context->IsSuperLibrarian ) {
457 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
462 if ( $patron ) {
463 my $patron_unblessed = $patron->unblessed;
464 my $branch = C4::Circulation::_GetCircControlBranch($item, $patron_unblessed);
466 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
468 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
470 my $can_item_be_reserved = CanItemBeReserved( $patron->borrowernumber, $itemnumber );
471 $item->{not_holdable} = $can_item_be_reserved->{status} unless ( $can_item_be_reserved->{status} eq 'OK' );
473 $item->{item_level_holds} = Koha::IssuingRules->get_opacitemholds_policy( { item => $item_object, patron => $patron } );
475 if (
476 !$item->{cantreserve}
477 && !$exceeded_maxreserves
478 && IsAvailableForItemLevelRequest($item, $patron_unblessed)
479 && $can_item_be_reserved->{status} eq 'OK'
482 $item->{available} = 1;
483 $num_available++;
485 push( @available_itemtypes, $item->{itype} );
487 elsif ( $can_item_be_reserved->{status} eq 'tooManyReserves' && C4::Context->preference('AllowHoldPolicyOverride') ) {
488 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
489 $item->{override} = 1;
490 $num_override++;
492 push( @available_itemtypes, $item->{itype} );
495 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
497 # Show serial enumeration when needed
498 if ($item->{enumchron}) {
499 $itemdata_enumchron = 1;
503 push @{ $biblioitem->{itemloop} }, $item;
506 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
507 $template->param( override_required => 1 );
508 } elsif ( $num_available == 0 ) {
509 $template->param( none_available => 1 );
510 $biblioloopiter{warn} = 1;
511 $biblioloopiter{none_avail} = 1;
513 $template->param( hiddencount => $hiddencount);
515 push @bibitemloop, $biblioitem;
518 @available_itemtypes = uniq( @available_itemtypes );
519 $template->param( available_itemtypes => \@available_itemtypes );
521 # existingreserves building
522 my @reserveloop;
523 my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } );
524 foreach my $res (
525 sort {
526 my $a_found = $a->found() || '';
527 my $b_found = $a->found() || '';
528 $a_found cmp $b_found;
529 } @reserves
532 my $priority = $res->priority();
533 my %reserve;
534 my @optionloop;
535 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
536 push(
537 @optionloop,
539 num => $i,
540 selected => ( $i == $priority ),
545 if ( $res->is_found() ) {
546 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
547 $reserve{'biblionumber'} = $res->item()->biblionumber();
548 $reserve{'barcodenumber'} = $res->item()->barcode();
549 $reserve{'wbrcode'} = $res->branchcode();
550 $reserve{'itemnumber'} = $res->itemnumber();
551 $reserve{'wbrname'} = $res->branch()->branchname();
553 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
555 # Just because the holdingbranch matches the reserve branch doesn't mean the item
556 # has arrived at the destination, check for an open transfer for the item as well
557 my ( $transfertwhen, $transfertfrom, $transferto ) =
558 C4::Circulation::GetTransfers( $res->itemnumber() );
559 if ( not $transferto or $transferto ne $res->branchcode() ) {
560 $reserve{'atdestination'} = 1;
564 # set found to 1 if reserve is waiting for patron pickup
565 $reserve{'found'} = $res->is_found();
566 $reserve{'intransit'} = $res->is_in_transit();
568 elsif ( $res->priority() > 0 ) {
569 if ( my $item = $res->item() ) {
570 $reserve{'itemnumber'} = $item->id();
571 $reserve{'barcodenumber'} = $item->barcode();
572 $reserve{'item_level_hold'} = 1;
576 $reserve{'expirationdate'} = output_pref( { dt => dt_from_string( $res->expirationdate ), dateonly => 1 } )
577 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
578 $reserve{'date'} = output_pref( { dt => dt_from_string( $res->reservedate ), dateonly => 1 } );
579 $reserve{'borrowernumber'} = $res->borrowernumber();
580 $reserve{'biblionumber'} = $res->biblionumber();
581 $reserve{'patron'} = $res->borrower;
582 $reserve{'notes'} = $res->reservenotes();
583 $reserve{'waiting_date'} = $res->waitingdate();
584 $reserve{'ccode'} = $res->item() ? $res->item()->ccode() : undef;
585 $reserve{'barcode'} = $res->item() ? $res->item()->barcode() : undef;
586 $reserve{'priority'} = $res->priority();
587 $reserve{'lowestPriority'} = $res->lowestPriority();
588 $reserve{'optionloop'} = \@optionloop;
589 $reserve{'suspend'} = $res->suspend();
590 $reserve{'suspend_until'} = $res->suspend_until();
591 $reserve{'reserve_id'} = $res->reserve_id();
592 $reserve{itemtype} = $res->itemtype();
593 $reserve{branchcode} = $res->branchcode();
594 $reserve{object} = $res;
596 push( @reserveloop, \%reserve );
599 # get the time for the form name...
600 my $time = time();
602 $template->param(
603 time => $time,
604 fixedRank => $fixedRank,
607 # display infos
608 $template->param(
609 optionloop => \@optionloop,
610 bibitemloop => \@bibitemloop,
611 itemdata_enumchron => $itemdata_enumchron,
612 date => $date,
613 biblionumber => $biblionumber,
614 findborrower => $findborrower,
615 title => $biblio->title,
616 author => $biblio->author,
617 holdsview => 1,
618 C4::Search::enabled_staff_search_views,
621 $biblioloopiter{biblionumber} = $biblionumber;
622 $biblioloopiter{title} = $biblio->title;
623 $biblioloopiter{rank} = $fixedRank;
624 $biblioloopiter{reserveloop} = \@reserveloop;
626 if (@reserveloop) {
627 $template->param( reserveloop => \@reserveloop );
630 push @biblioloop, \%biblioloopiter;
633 $template->param( biblioloop => \@biblioloop );
634 $template->param( biblionumbers => $biblionumbers );
635 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
636 $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
637 $template->param( subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber));
639 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
640 $template->param( reserve_in_future => 1 );
643 $template->param(
644 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
645 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
648 # printout the page
649 output_html_with_http_headers $input, $cookie, $template->output;
651 sub sort_borrowerlist {
652 my $borrowerslist = shift;
653 my $ref = [];
654 push @{$ref}, sort {
655 uc( $a->{surname} . $a->{firstname} ) cmp
656 uc( $b->{surname} . $b->{firstname} )
657 } @{$borrowerslist};
658 return $ref;