Bug 10963: Simplified creation - CF framework
[koha.git] / reserve / request.pl
blob0d82f1c61577abad81042cbaf630816c4906f4a6
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 C4::Dates qw/format_date/;
43 use C4::Utils::DataTables::Members;
44 use C4::Members;
45 use C4::Search; # enabled_staff_search_views
46 use Koha::DateUtils;
48 my $dbh = C4::Context->dbh;
49 my $sth;
50 my $input = new CGI;
51 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
53 template_name => "reserve/request.tt",
54 query => $input,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => { reserveforothers => 'place_holds' },
61 my $multihold = $input->param('multi_hold');
62 $template->param(multi_hold => $multihold);
63 my $showallitems = $input->param('showallitems');
65 # get Branches and Itemtypes
66 my $branches = GetBranches();
67 my $itemtypes = GetItemTypes();
69 my $userbranch = '';
70 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
71 $userbranch = C4::Context->userenv->{'branch'};
75 # Select borrowers infos
76 my $findborrower = $input->param('findborrower');
77 $findborrower = '' unless defined $findborrower;
78 $findborrower =~ s|,| |g;
79 my $borrowernumber_hold = $input->param('borrowernumber') || '';
80 my $messageborrower;
81 my $maxreserves;
82 my $warnings;
83 my $messages;
85 my $date = C4::Dates->today('iso');
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 CancelReserve({ reserve_id => $reserve_id });
96 } elsif ( $action eq 'setLowestPriority' ) {
97 my $reserve_id = $input->param('reserve_id');
98 ToggleLowestPriority( $reserve_id );
99 } elsif ( $action eq 'toggleSuspend' ) {
100 my $reserve_id = $input->param('reserve_id');
101 my $suspend_until = $input->param('suspend_until');
102 ToggleSuspend( $reserve_id, $suspend_until );
105 if ($findborrower) {
106 my $borrower = C4::Members::GetMember( cardnumber => $findborrower );
107 if ( $borrower ) {
108 $borrowernumber_hold = $borrower->{borrowernumber};
109 } else {
110 my $dt_params = { iDisplayLength => -1 };
111 my $results = C4::Utils::DataTables::Members::search(
113 searchmember => $findborrower,
114 dt_params => $dt_params,
117 my $borrowers = $results->{patrons};
118 if ( scalar @$borrowers == 1 ) {
119 $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
120 } elsif ( @$borrowers ) {
121 $template->param( borrowers => $borrowers );
122 } else {
123 $messageborrower = "'$findborrower'";
128 # If we have the borrowernumber because we've performed an action, then we
129 # don't want to try to place another reserve.
130 if ($borrowernumber_hold && !$action) {
131 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
132 my $diffbranch;
133 my @getreservloop;
134 my $count_reserv = 0;
136 # we check the reserves of the borrower, and if he can reserv a document
137 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
139 my $number_reserves =
140 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
142 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
143 $warnings = 1;
144 $maxreserves = 1;
147 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
148 my $expiry_date = $borrowerinfo->{dateexpiry};
149 my $expiry = 0; # flag set if patron account has expired
150 if ($expiry_date and $expiry_date ne '0000-00-00' and
151 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
152 $expiry = 1;
155 # check if the borrower make the reserv in a different branch
156 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
157 $diffbranch = 1;
160 $template->param(
161 borrowernumber => $borrowerinfo->{'borrowernumber'},
162 borrowersurname => $borrowerinfo->{'surname'},
163 borrowerfirstname => $borrowerinfo->{'firstname'},
164 borrowerstreetaddress => $borrowerinfo->{'address'},
165 borrowercity => $borrowerinfo->{'city'},
166 borrowerphone => $borrowerinfo->{'phone'},
167 borrowermobile => $borrowerinfo->{'mobile'},
168 borrowerfax => $borrowerinfo->{'fax'},
169 borrowerphonepro => $borrowerinfo->{'phonepro'},
170 borroweremail => $borrowerinfo->{'email'},
171 borroweremailpro => $borrowerinfo->{'emailpro'},
172 borrowercategory => $borrowerinfo->{'category'},
173 borrowerreservs => $count_reserv,
174 cardnumber => $borrowerinfo->{'cardnumber'},
175 expiry => $expiry,
176 diffbranch => $diffbranch,
177 messages => $messages,
178 warnings => $warnings
182 $template->param( messageborrower => $messageborrower );
184 # FIXME launch another time GetMember perhaps until
185 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
187 my @biblionumbers = ();
188 my $biblionumbers = $input->param('biblionumbers');
189 if ($multihold) {
190 @biblionumbers = split '/', $biblionumbers;
191 } else {
192 push @biblionumbers, $input->param('biblionumber');
195 my $itemdata_enumchron = 0;
196 my @biblioloop = ();
197 foreach my $biblionumber (@biblionumbers) {
199 my %biblioloopiter = ();
201 my $dat = GetBiblioData($biblionumber);
203 my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
204 $canReserve //= '';
205 if ( $canReserve eq 'OK' ) {
207 #All is OK and we can continue
209 elsif ( $canReserve eq 'tooManyReserves' ) {
210 $maxreserves = 1;
212 elsif ( $canReserve eq 'ageRestricted' ) {
213 $template->param( $canReserve => 1 );
214 $biblioloopiter{$canReserve} = 1;
216 else {
217 $biblioloopiter{$canReserve} = 1;
220 my $alreadypossession;
221 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
222 $alreadypossession = 1;
225 # get existing reserves .....
226 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
227 my $count = scalar( @$reserves );
228 my $totalcount = $count;
229 my $holds_count = 0;
230 my $alreadyreserved = 0;
232 foreach my $res (@$reserves) {
233 if ( defined $res->{found} ) { # found can be 'W' or 'T'
234 $count--;
237 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
238 $holds_count++;
242 if ( $holds_count ) {
243 $alreadyreserved = 1;
244 $biblioloopiter{warn} = 1;
245 $biblioloopiter{alreadyres} = 1;
248 $template->param(
249 alreadyreserved => $alreadyreserved,
250 alreadypossession => $alreadypossession,
253 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
254 # make priorities options
256 my @optionloop;
257 for ( 1 .. $count + 1 ) {
258 push(
259 @optionloop,
261 num => $_,
262 selected => ( $_ == $count + 1 ),
266 # adding a fixed value for priority options
267 my $fixedRank = $count+1;
269 my @branchcodes;
270 my %itemnumbers_of_biblioitem;
271 my @itemnumbers;
273 ## $items is array of 'item' table numbers
274 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
275 @itemnumbers = @$items;
277 my @hostitems = get_hostitemnumbers_of($biblionumber);
278 if (@hostitems){
279 $template->param('hostitemsflag' => 1);
280 push(@itemnumbers, @hostitems);
283 if (!@itemnumbers) {
284 $template->param('noitems' => 1);
285 $biblioloopiter{noitems} = 1;
288 ## Hash of item number to 'item' table fields
289 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
291 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
292 ## when by definition all of the itemnumber have the same biblioitemnumber
293 foreach my $itemnumber (@itemnumbers) {
294 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
295 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
298 ## Should be same as biblionumber
299 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
301 my $notforloan_label_of = get_notforloan_label_of();
303 ## Hash of biblioitemnumber to 'biblioitem' table records
304 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
306 my @bibitemloop;
308 foreach my $biblioitemnumber (@biblioitemnumbers) {
309 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
310 my $num_available = 0;
311 my $num_override = 0;
312 my $hiddencount = 0;
314 $biblioitem->{description} =
315 $itemtypes->{ $biblioitem->{itemtype} }{description};
316 if($biblioitem->{biblioitemnumber} ne $biblionumber){
317 $biblioitem->{hostitemsflag}=1;
319 $biblioloopiter{description} = $biblioitem->{description};
320 $biblioloopiter{itypename} = $biblioitem->{description};
321 $biblioloopiter{imageurl} =
322 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
324 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
325 my $item = $iteminfos_of->{$itemnumber};
327 unless (C4::Context->preference('item-level_itypes')) {
328 $item->{itype} = $biblioitem->{itemtype};
331 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
332 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
333 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
335 # if the holdingbranch is different than the homebranch, we show the
336 # holdingbranch of the document too
337 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
338 $item->{holdingbranchname} =
339 $branches->{ $item->{holdingbranch} }{branchname};
342 if($item->{biblionumber} ne $biblionumber){
343 $item->{hostitemsflag}=1;
344 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
347 # if the item is currently on loan, we display its return date and
348 # change the background color
349 my $issues= GetItemIssue($itemnumber);
350 if ( $issues->{'date_due'} ) {
351 $item->{date_due} = $issues->{date_due_sql};
352 $item->{backgroundcolor} = 'onloan';
355 # checking reserve
356 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
357 if ( defined $reservedate ) {
358 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
360 $item->{backgroundcolor} = 'reserved';
361 $item->{reservedate} = format_date($reservedate);
362 $item->{ReservedForBorrowernumber} = $reservedfor;
363 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
364 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
365 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
366 $item->{waitingdate} = $wait;
369 # Management of the notforloan document
370 if ( $item->{notforloan} ) {
371 $item->{backgroundcolor} = 'other';
372 $item->{notforloanvalue} =
373 $notforloan_label_of->{ $item->{notforloan} };
376 # Management of lost or long overdue items
377 if ( $item->{itemlost} ) {
379 # FIXME localized strings should never be in Perl code
380 $item->{message} =
381 $item->{itemlost} == 1 ? "(lost)"
382 : $item->{itemlost} == 2 ? "(long overdue)"
383 : "";
384 $item->{backgroundcolor} = 'other';
385 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
386 $item->{hide} = 1;
387 $hiddencount++;
391 # Check the transit status
392 my ( $transfertwhen, $transfertfrom, $transfertto ) =
393 GetTransfers($itemnumber);
395 if ( defined $transfertwhen && $transfertwhen ne '' ) {
396 $item->{transfertwhen} = format_date($transfertwhen);
397 $item->{transfertfrom} =
398 $branches->{$transfertfrom}{branchname};
399 $item->{transfertto} = $branches->{$transfertto}{branchname};
400 $item->{nocancel} = 1;
403 # If there is no loan, return and transfer, we show a checkbox.
404 $item->{notforloan} ||= 0;
406 # if independent branches is on we need to check if the person can reserve
407 # for branches they arent logged in to
408 if ( C4::Context->preference("IndependentBranches") ) {
409 if (! C4::Context->preference("canreservefromotherbranches")){
410 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
411 my $userenv = C4::Context->userenv;
412 unless ( C4::Context->IsSuperLibrarian ) {
413 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
418 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
420 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
422 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
424 if (
425 !$item->{cantreserve}
426 && IsAvailableForItemLevelRequest($item, $borrowerinfo)
427 && CanItemBeReserved(
428 $borrowerinfo->{borrowernumber}, $itemnumber
429 ) eq 'OK'
432 $item->{available} = 1;
433 $num_available++;
435 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
436 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
437 $item->{override} = 1;
438 $num_override++;
441 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
443 # Show serial enumeration when needed
444 if ($item->{enumchron}) {
445 $itemdata_enumchron = 1;
448 push @{ $biblioitem->{itemloop} }, $item;
451 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
452 $template->param( override_required => 1 );
453 } elsif ( $num_available == 0 ) {
454 $template->param( none_available => 1 );
455 $biblioloopiter{warn} = 1;
456 $biblioloopiter{none_avail} = 1;
458 $template->param( hiddencount => $hiddencount);
460 push @bibitemloop, $biblioitem;
463 # existingreserves building
464 my @reserveloop;
465 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
466 foreach my $res ( sort {
467 my $a_found = $a->{found} || '';
468 my $b_found = $a->{found} || '';
469 $a_found cmp $b_found;
470 } @$reserves ) {
471 my %reserve;
472 my @optionloop;
473 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
474 push(
475 @optionloop,
477 num => $i,
478 selected => ( $i == $res->{priority} ),
483 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
484 my $item = $res->{'itemnumber'};
485 $item = GetBiblioFromItemNumber($item,undef);
486 $reserve{'wait'}= 1;
487 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
488 $reserve{'biblionumber'}=$item->{'biblionumber'};
489 $reserve{'barcodenumber'} = $item->{'barcode'};
490 $reserve{'wbrcode'} = $res->{'branchcode'};
491 $reserve{'itemnumber'} = $res->{'itemnumber'};
492 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
493 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
494 # Just because the holdingbranch matches the reserve branch doesn't mean the item
495 # has arrived at the destination, check for an open transfer for the item as well
496 my ( $transfertwhen, $transfertfrom, $transferto ) = C4::Circulation::GetTransfers( $res->{itemnumber} );
497 if ( not $transferto or $transferto ne $res->{branchcode} ) {
498 $reserve{'atdestination'} = 1;
501 # set found to 1 if reserve is waiting for patron pickup
502 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
503 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
504 } elsif ($res->{priority} > 0) {
505 if (defined($res->{itemnumber})) {
506 my $item = GetItem($res->{itemnumber});
507 $reserve{'itemnumber'} = $res->{'itemnumber'};
508 $reserve{'barcodenumber'} = $item->{'barcode'};
509 $reserve{'item_level_hold'} = 1;
513 # get borrowers reserve info
514 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
515 if (C4::Context->preference('HidePatronName')){
516 $reserve{'hidename'} = 1;
517 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
519 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
520 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
521 $reserve{'date'} = format_date( $res->{'reservedate'} );
522 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
523 $reserve{'biblionumber'} = $res->{'biblionumber'};
524 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
525 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
526 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
527 $reserve{'notes'} = $res->{'reservenotes'};
528 $reserve{'wait'} =
529 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
530 $reserve{'voldesc'} = $res->{'volumeddesc'};
531 $reserve{'ccode'} = $res->{'ccode'};
532 $reserve{'barcode'} = $res->{'barcode'};
533 $reserve{'priority'} = $res->{'priority'};
534 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
535 $reserve{'optionloop'} = \@optionloop;
536 $reserve{'suspend'} = $res->{'suspend'};
537 $reserve{'suspend_until'} = $res->{'suspend_until'};
538 $reserve{'reserve_id'} = $res->{'reserve_id'};
540 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
541 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
542 } else {
543 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
546 push( @reserveloop, \%reserve );
549 # get the time for the form name...
550 my $time = time();
552 $template->param(
553 branchloop => GetBranchesLoop($userbranch),
554 time => $time,
555 fixedRank => $fixedRank,
558 # display infos
559 $template->param(
560 optionloop => \@optionloop,
561 bibitemloop => \@bibitemloop,
562 itemdata_enumchron => $itemdata_enumchron,
563 date => $date,
564 biblionumber => $biblionumber,
565 findborrower => $findborrower,
566 title => $dat->{title},
567 author => $dat->{author},
568 holdsview => 1,
569 C4::Search::enabled_staff_search_views,
571 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
572 $template->param(
573 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
574 borrower_branchcode => $borrowerinfo->{'branchcode'},
578 $biblioloopiter{biblionumber} = $biblionumber;
579 $biblioloopiter{title} = $dat->{title};
580 $biblioloopiter{rank} = $fixedRank;
581 $biblioloopiter{reserveloop} = \@reserveloop;
583 if (@reserveloop) {
584 $template->param( reserveloop => \@reserveloop );
587 push @biblioloop, \%biblioloopiter;
590 $template->param( biblioloop => \@biblioloop );
591 $template->param( biblionumbers => $biblionumbers );
592 $template->param( maxreserves => $maxreserves );
594 if ($multihold) {
595 $template->param( multi_hold => 1 );
598 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
599 $template->param( reserve_in_future => 1 );
602 $template->param(
603 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
604 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
607 # printout the page
608 output_html_with_http_headers $input, $cookie, $template->output;
610 sub sort_borrowerlist {
611 my $borrowerslist = shift;
612 my $ref = [];
613 push @{$ref}, sort {
614 uc( $a->{surname} . $a->{firstname} ) cmp
615 uc( $b->{surname} . $b->{firstname} )
616 } @{$borrowerslist};
617 return $ref;