Translation updates for Koha 3.22.0 release
[koha.git] / reserve / request.pl
blob80cf0d6118e5243c9be735afecb8ab581b98ad2b
1 #!/usr/bin/perl
4 #writen 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 strict;
30 use warnings;
31 use C4::Branch;
32 use CGI qw ( -utf8 );
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
35 use C4::Output;
36 use C4::Auth;
37 use C4::Reserves;
38 use C4::Biblio;
39 use C4::Items;
40 use C4::Koha;
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 use Koha::DateUtils;
47 use Koha::Borrower::Debarments qw(IsDebarred);
49 my $dbh = C4::Context->dbh;
50 my $sth;
51 my $input = new CGI;
52 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
54 template_name => "reserve/request.tt",
55 query => $input,
56 type => "intranet",
57 authnotrequired => 0,
58 flagsrequired => { reserveforothers => 'place_holds' },
62 my $multihold = $input->param('multi_hold');
63 $template->param(multi_hold => $multihold);
64 my $showallitems = $input->param('showallitems');
66 # get Branches and Itemtypes
67 my $branches = GetBranches();
68 my $itemtypes = GetItemTypes();
70 my $userbranch = '';
71 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
72 $userbranch = C4::Context->userenv->{'branch'};
76 # Select borrowers infos
77 my $findborrower = $input->param('findborrower');
78 $findborrower = '' unless defined $findborrower;
79 $findborrower =~ s|,| |g;
80 my $borrowernumber_hold = $input->param('borrowernumber') || '';
81 my $messageborrower;
82 my $warnings;
83 my $messages;
84 my $exceeded_maxreserves;
86 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
87 my $action = $input->param('action');
88 $action ||= q{};
90 if ( $action eq 'move' ) {
91 my $where = $input->param('where');
92 my $reserve_id = $input->param('reserve_id');
93 AlterPriority( $where, $reserve_id );
94 } elsif ( $action eq 'cancel' ) {
95 my $reserve_id = $input->param('reserve_id');
96 CancelReserve({ reserve_id => $reserve_id });
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 $borrower = C4::Members::GetMember( cardnumber => $findborrower );
108 if ( $borrower ) {
109 $borrowernumber_hold = $borrower->{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->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 $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
142 my $diffbranch;
143 my @getreservloop;
145 # we check the reserves of the borrower, and if he can reserv a document
146 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
148 my $reserves_count =
149 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
151 my $new_reserves_count = scalar( @biblionumbers );
153 my $maxreserves = C4::Context->preference('maxreserves');
154 if ( $maxreserves
155 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
157 my $new_reserves_allowed =
158 $maxreserves - $reserves_count > 0
159 ? $maxreserves - $reserves_count
160 : 0;
161 $warnings = 1;
162 $exceeded_maxreserves = 1;
163 $template->param(
164 new_reserves_allowed => $new_reserves_allowed,
165 new_reserves_count => $new_reserves_count,
166 reserves_count => $reserves_count,
167 maxreserves => $maxreserves,
171 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
172 my $expiry_date = $borrowerinfo->{dateexpiry};
173 my $expiry = 0; # flag set if patron account has expired
174 if ($expiry_date and $expiry_date ne '0000-00-00' and
175 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
176 $expiry = 1;
179 # check if the borrower make the reserv in a different branch
180 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
181 $diffbranch = 1;
184 $template->param(
185 borrowernumber => $borrowerinfo->{'borrowernumber'},
186 borrowersurname => $borrowerinfo->{'surname'},
187 borrowerfirstname => $borrowerinfo->{'firstname'},
188 borrowerstreetaddress => $borrowerinfo->{'address'},
189 borrowercity => $borrowerinfo->{'city'},
190 borrowerphone => $borrowerinfo->{'phone'},
191 borrowermobile => $borrowerinfo->{'mobile'},
192 borrowerfax => $borrowerinfo->{'fax'},
193 borrowerphonepro => $borrowerinfo->{'phonepro'},
194 borroweremail => $borrowerinfo->{'email'},
195 borroweremailpro => $borrowerinfo->{'emailpro'},
196 borrowercategory => $borrowerinfo->{'category'},
197 cardnumber => $borrowerinfo->{'cardnumber'},
198 expiry => $expiry,
199 diffbranch => $diffbranch,
200 messages => $messages,
201 warnings => $warnings,
202 restricted => IsDebarred($borrowerinfo->{'borrowernumber'}),
203 amount_outstanding => GetMemberAccountRecords($borrowerinfo->{borrowernumber}),
207 $template->param( messageborrower => $messageborrower );
209 # FIXME launch another time GetMember perhaps until
210 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
212 my $itemdata_enumchron = 0;
213 my @biblioloop = ();
214 foreach my $biblionumber (@biblionumbers) {
216 my %biblioloopiter = ();
218 my $dat = GetBiblioData($biblionumber);
220 my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
221 $canReserve //= '';
222 if ( $canReserve eq 'OK' ) {
224 #All is OK and we can continue
226 elsif ( $canReserve eq 'tooManyReserves' ) {
227 $exceeded_maxreserves = 1;
229 elsif ( $canReserve eq 'ageRestricted' ) {
230 $template->param( $canReserve => 1 );
231 $biblioloopiter{$canReserve} = 1;
233 else {
234 $biblioloopiter{$canReserve} = 1;
237 my $alreadypossession;
238 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
239 $alreadypossession = 1;
242 # get existing reserves .....
243 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
244 my $count = scalar( @$reserves );
245 my $totalcount = $count;
246 my $holds_count = 0;
247 my $alreadyreserved = 0;
249 foreach my $res (@$reserves) {
250 if ( defined $res->{found} ) { # found can be 'W' or 'T'
251 $count--;
254 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
255 $holds_count++;
259 if ( $holds_count ) {
260 $alreadyreserved = 1;
261 $biblioloopiter{warn} = 1;
262 $biblioloopiter{alreadyres} = 1;
265 $template->param(
266 alreadyreserved => $alreadyreserved,
267 alreadypossession => $alreadypossession,
270 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
271 # make priorities options
273 my @optionloop;
274 for ( 1 .. $count + 1 ) {
275 push(
276 @optionloop,
278 num => $_,
279 selected => ( $_ == $count + 1 ),
283 # adding a fixed value for priority options
284 my $fixedRank = $count+1;
286 my @branchcodes;
287 my %itemnumbers_of_biblioitem;
288 my @itemnumbers;
290 ## $items is array of 'item' table numbers
291 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
292 @itemnumbers = @$items;
294 my @hostitems = get_hostitemnumbers_of($biblionumber);
295 if (@hostitems){
296 $template->param('hostitemsflag' => 1);
297 push(@itemnumbers, @hostitems);
300 if (!@itemnumbers) {
301 $template->param('noitems' => 1);
302 $biblioloopiter{noitems} = 1;
305 ## Hash of item number to 'item' table fields
306 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
308 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
309 ## when by definition all of the itemnumber have the same biblioitemnumber
310 foreach my $itemnumber (@itemnumbers) {
311 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
312 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
315 ## Should be same as biblionumber
316 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
318 my $notforloan_label_of = get_notforloan_label_of();
320 ## Hash of biblioitemnumber to 'biblioitem' table records
321 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
323 my @bibitemloop;
325 foreach my $biblioitemnumber (@biblioitemnumbers) {
326 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
327 my $num_available = 0;
328 my $num_override = 0;
329 my $hiddencount = 0;
331 if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
332 $biblioitem->{hostitemsflag} = 1;
335 $biblioloopiter{description} = $biblioitem->{description};
336 $biblioloopiter{itypename} = $biblioitem->{description};
337 if ( $biblioitem->{itemtype} ) {
339 $biblioitem->{description} =
340 $itemtypes->{ $biblioitem->{itemtype} }{description};
342 $biblioloopiter{imageurl} =
343 getitemtypeimagelocation( 'intranet',
344 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
347 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
348 my $item = $iteminfos_of->{$itemnumber};
350 unless (C4::Context->preference('item-level_itypes')) {
351 $item->{itype} = $biblioitem->{itemtype};
354 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
355 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
356 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
358 # if the holdingbranch is different than the homebranch, we show the
359 # holdingbranch of the document too
360 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
361 $item->{holdingbranchname} =
362 $branches->{ $item->{holdingbranch} }{branchname};
365 if($item->{biblionumber} ne $biblionumber){
366 $item->{hostitemsflag}=1;
367 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
370 # if the item is currently on loan, we display its return date and
371 # change the background color
372 my $issues= GetItemIssue($itemnumber);
373 if ( $issues->{'date_due'} ) {
374 $item->{date_due} = $issues->{date_due_sql};
375 $item->{backgroundcolor} = 'onloan';
378 # checking reserve
379 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
380 if ( defined $reservedate ) {
381 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
383 $item->{backgroundcolor} = 'reserved';
384 $item->{reservedate} = output_pref({ dt => dt_from_string( $reservedate ), dateonly => 1 });
385 $item->{ReservedForBorrowernumber} = $reservedfor;
386 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
387 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
388 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
389 $item->{waitingdate} = $wait;
392 # Management of the notforloan document
393 if ( $item->{notforloan} ) {
394 $item->{backgroundcolor} = 'other';
395 $item->{notforloanvalue} =
396 $notforloan_label_of->{ $item->{notforloan} };
399 # Management of lost or long overdue items
400 if ( $item->{itemlost} ) {
402 # FIXME localized strings should never be in Perl code
403 $item->{message} =
404 $item->{itemlost} == 1 ? "(lost)"
405 : $item->{itemlost} == 2 ? "(long overdue)"
406 : "";
407 $item->{backgroundcolor} = 'other';
408 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
409 $item->{hide} = 1;
410 $hiddencount++;
414 # Check the transit status
415 my ( $transfertwhen, $transfertfrom, $transfertto ) =
416 GetTransfers($itemnumber);
418 if ( defined $transfertwhen && $transfertwhen ne '' ) {
419 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
420 $item->{transfertfrom} =
421 $branches->{$transfertfrom}{branchname};
422 $item->{transfertto} = $branches->{$transfertto}{branchname};
423 $item->{nocancel} = 1;
426 # If there is no loan, return and transfer, we show a checkbox.
427 $item->{notforloan} ||= 0;
429 # if independent branches is on we need to check if the person can reserve
430 # for branches they arent logged in to
431 if ( C4::Context->preference("IndependentBranches") ) {
432 if (! C4::Context->preference("canreservefromotherbranches")){
433 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
434 my $userenv = C4::Context->userenv;
435 unless ( C4::Context->IsSuperLibrarian ) {
436 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
441 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
443 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
445 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
447 if (
448 !$item->{cantreserve}
449 && !$exceeded_maxreserves
450 && IsAvailableForItemLevelRequest($item, $borrowerinfo)
451 && CanItemBeReserved(
452 $borrowerinfo->{borrowernumber}, $itemnumber
453 ) eq 'OK'
456 $item->{available} = 1;
457 $num_available++;
459 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
460 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
461 $item->{override} = 1;
462 $num_override++;
465 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
467 # Show serial enumeration when needed
468 if ($item->{enumchron}) {
469 $itemdata_enumchron = 1;
472 push @{ $biblioitem->{itemloop} }, $item;
475 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
476 $template->param( override_required => 1 );
477 } elsif ( $num_available == 0 ) {
478 $template->param( none_available => 1 );
479 $biblioloopiter{warn} = 1;
480 $biblioloopiter{none_avail} = 1;
482 $template->param( hiddencount => $hiddencount);
484 push @bibitemloop, $biblioitem;
487 # existingreserves building
488 my @reserveloop;
489 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
490 foreach my $res ( sort {
491 my $a_found = $a->{found} || '';
492 my $b_found = $a->{found} || '';
493 $a_found cmp $b_found;
494 } @$reserves ) {
495 my %reserve;
496 my @optionloop;
497 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
498 push(
499 @optionloop,
501 num => $i,
502 selected => ( $i == $res->{priority} ),
507 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
508 my $item = $res->{'itemnumber'};
509 $item = GetBiblioFromItemNumber($item,undef);
510 $reserve{'wait'}= 1;
511 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
512 $reserve{'biblionumber'}=$item->{'biblionumber'};
513 $reserve{'barcodenumber'} = $item->{'barcode'};
514 $reserve{'wbrcode'} = $res->{'branchcode'};
515 $reserve{'itemnumber'} = $res->{'itemnumber'};
516 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
517 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
518 # Just because the holdingbranch matches the reserve branch doesn't mean the item
519 # has arrived at the destination, check for an open transfer for the item as well
520 my ( $transfertwhen, $transfertfrom, $transferto ) = C4::Circulation::GetTransfers( $res->{itemnumber} );
521 if ( not $transferto or $transferto ne $res->{branchcode} ) {
522 $reserve{'atdestination'} = 1;
525 # set found to 1 if reserve is waiting for patron pickup
526 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
527 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
528 } elsif ($res->{priority} > 0) {
529 if (defined($res->{itemnumber})) {
530 my $item = GetItem($res->{itemnumber});
531 $reserve{'itemnumber'} = $res->{'itemnumber'};
532 $reserve{'barcodenumber'} = $item->{'barcode'};
533 $reserve{'item_level_hold'} = 1;
537 # get borrowers reserve info
538 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
539 if (C4::Context->preference('HidePatronName')){
540 $reserve{'hidename'} = 1;
541 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
543 $reserve{'expirationdate'} = output_pref({ dt => dt_from_string( $res->{'expirationdate'} ), dateonly => 1 })
544 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
545 $reserve{'date'} = output_pref({ dt => dt_from_string( $res->{'reservedate'} ), dateonly => 1 });
546 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
547 $reserve{'biblionumber'} = $res->{'biblionumber'};
548 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
549 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
550 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
551 $reserve{'notes'} = $res->{'reservenotes'};
552 $reserve{'wait'} =
553 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
554 $reserve{'voldesc'} = $res->{'volumeddesc'};
555 $reserve{'ccode'} = $res->{'ccode'};
556 $reserve{'barcode'} = $res->{'barcode'};
557 $reserve{'priority'} = $res->{'priority'};
558 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
559 $reserve{'optionloop'} = \@optionloop;
560 $reserve{'suspend'} = $res->{'suspend'};
561 $reserve{'suspend_until'} = $res->{'suspend_until'};
562 $reserve{'reserve_id'} = $res->{'reserve_id'};
564 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
565 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
566 } else {
567 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
570 push( @reserveloop, \%reserve );
573 # get the time for the form name...
574 my $time = time();
576 $template->param(
577 branchloop => GetBranchesLoop($userbranch),
578 time => $time,
579 fixedRank => $fixedRank,
582 # display infos
583 $template->param(
584 optionloop => \@optionloop,
585 bibitemloop => \@bibitemloop,
586 itemdata_enumchron => $itemdata_enumchron,
587 date => $date,
588 biblionumber => $biblionumber,
589 findborrower => $findborrower,
590 title => $dat->{title},
591 author => $dat->{author},
592 holdsview => 1,
593 C4::Search::enabled_staff_search_views,
595 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
596 $template->param(
597 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
598 borrower_branchcode => $borrowerinfo->{'branchcode'},
602 $biblioloopiter{biblionumber} = $biblionumber;
603 $biblioloopiter{title} = $dat->{title};
604 $biblioloopiter{rank} = $fixedRank;
605 $biblioloopiter{reserveloop} = \@reserveloop;
607 if (@reserveloop) {
608 $template->param( reserveloop => \@reserveloop );
611 push @biblioloop, \%biblioloopiter;
614 $template->param( biblioloop => \@biblioloop );
615 $template->param( biblionumbers => $biblionumbers );
616 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
618 if ($multihold) {
619 $template->param( multi_hold => 1 );
622 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
623 $template->param( reserve_in_future => 1 );
626 $template->param(
627 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
628 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
631 # printout the page
632 output_html_with_http_headers $input, $cookie, $template->output;
634 sub sort_borrowerlist {
635 my $borrowerslist = shift;
636 my $ref = [];
637 push @{$ref}, sort {
638 uc( $a->{surname} . $a->{firstname} ) cmp
639 uc( $b->{surname} . $b->{firstname} )
640 } @{$borrowerslist};
641 return $ref;