bug 7178 follow-up: typo fix
[koha.git] / reserve / request.pl
blobb96447f8cc52a99287c54a13dd2c947534ae3972
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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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::Members;
44 use C4::Search; # enabled_staff_search_views
45 use Koha::DateUtils;
47 my $dbh = C4::Context->dbh;
48 my $sth;
49 my $input = new CGI;
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52 template_name => "reserve/request.tmpl",
53 query => $input,
54 type => "intranet",
55 authnotrequired => 0,
56 flagsrequired => { reserveforothers => 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold => $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches();
66 my $itemtypes = GetItemTypes();
68 my $default = C4::Context->userenv->{branch};
69 my @values;
70 my %label_of;
72 foreach my $branchcode (sort keys %{$branches} ) {
73 push @values, $branchcode;
74 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
76 my $CGIbranch = CGI::scrolling_list(
77 -name => 'pickup',
78 -id => 'pickup',
79 -values => \@values,
80 -default => $default,
81 -labels => \%label_of,
82 -size => 1,
83 -multiple => 0,
86 # Select borrowers infos
87 my $findborrower = $input->param('findborrower');
88 $findborrower = '' unless defined $findborrower;
89 $findborrower =~ s|,| |g;
90 my $borrowernumber_hold = $input->param('borrowernumber') || '';
91 my $borrowerslist;
92 my $messageborrower;
93 my $warnings;
94 my $messages;
96 my $date = C4::Dates->today('iso');
97 my $action = $input->param('action');
98 $action ||= q{};
100 if ( $action eq 'move' ) {
101 my $where = $input->param('where');
102 my $borrowernumber = $input->param('borrowernumber');
103 my $biblionumber = $input->param('biblionumber');
104 AlterPriority( $where, $borrowernumber, $biblionumber );
105 } elsif ( $action eq 'cancel' ) {
106 my $borrowernumber = $input->param('borrowernumber');
107 my $biblionumber = $input->param('biblionumber');
108 CancelReserve( $biblionumber, '', $borrowernumber );
109 } elsif ( $action eq 'setLowestPriority' ) {
110 my $borrowernumber = $input->param('borrowernumber');
111 my $biblionumber = $input->param('biblionumber');
112 ToggleLowestPriority( $borrowernumber, $biblionumber );
115 if ($findborrower) {
116 my $borrowers = Search($findborrower, 'cardnumber');
118 if ($borrowers && @$borrowers) {
119 if ( @$borrowers == 1 ) {
120 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
122 else {
123 $borrowerslist = $borrowers;
125 } else {
126 $messageborrower = "'$findborrower'";
130 # If we have the borrowernumber because we've performed an action, then we
131 # don't want to try to place another reserve.
132 if ($borrowernumber_hold && !$action) {
133 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
134 my $diffbranch;
135 my @getreservloop;
136 my $count_reserv = 0;
137 my $maxreserves;
139 # we check the reserves of the borrower, and if he can reserv a document
140 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
142 my $number_reserves =
143 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
145 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
146 $warnings = 1;
147 $maxreserves = 1;
150 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
151 my $expiry_date = $borrowerinfo->{dateexpiry};
152 my $expiry = 0; # flag set if patron account has expired
153 if ($expiry_date and $expiry_date ne '0000-00-00' and
154 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
155 $messages = $expiry = 1;
158 # check if the borrower make the reserv in a different branch
159 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
160 $messages = 1;
161 $diffbranch = 1;
164 $template->param(
165 borrowernumber => $borrowerinfo->{'borrowernumber'},
166 borrowersurname => $borrowerinfo->{'surname'},
167 borrowerfirstname => $borrowerinfo->{'firstname'},
168 borrowerstreetaddress => $borrowerinfo->{'address'},
169 borrowercity => $borrowerinfo->{'city'},
170 borrowerphone => $borrowerinfo->{'phone'},
171 borrowermobile => $borrowerinfo->{'mobile'},
172 borrowerfax => $borrowerinfo->{'fax'},
173 borrowerphonepro => $borrowerinfo->{'phonepro'},
174 borroweremail => $borrowerinfo->{'email'},
175 borroweremailpro => $borrowerinfo->{'emailpro'},
176 borrowercategory => $borrowerinfo->{'category'},
177 borrowerreservs => $count_reserv,
178 cardnumber => $borrowerinfo->{'cardnumber'},
179 maxreserves => $maxreserves,
180 expiry => $expiry,
181 diffbranch => $diffbranch,
182 messages => $messages,
183 warnings => $warnings
187 $template->param( messageborrower => $messageborrower );
189 my $CGIselectborrower;
190 if ($borrowerslist) {
191 my @values;
192 my %labels;
194 foreach my $borrower (
195 sort {
196 uc($a->{surname}
197 . $a->{firstname}) cmp uc($b->{surname}
198 . $b->{firstname})
199 } @{$borrowerslist}
202 push @values, $borrower->{borrowernumber};
204 $labels{ $borrower->{borrowernumber} } = sprintf(
205 '%s, %s ... (%s - %s) ... %s',
206 $borrower->{surname} ||'', $borrower->{firstname} || '',
207 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
208 $borrower->{address} || '',
212 $CGIselectborrower = CGI::scrolling_list(
213 -name => 'borrowernumber',
214 -values => \@values,
215 -labels => \%labels,
216 -size => 7,
217 -multiple => 0,
221 # FIXME launch another time GetMember perhaps until
222 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
224 my @biblionumbers = ();
225 my $biblionumbers = $input->param('biblionumbers');
226 if ($multihold) {
227 @biblionumbers = split '/', $biblionumbers;
228 } else {
229 push @biblionumbers, $input->param('biblionumber');
232 my $itemdata_enumchron = 0;
233 my @biblioloop = ();
234 foreach my $biblionumber (@biblionumbers) {
236 my %biblioloopiter = ();
237 my $maxreserves;
239 my $dat = GetBiblioData($biblionumber);
241 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
242 $warnings = 1;
243 $maxreserves = 1;
245 # get existing reserves .....
246 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
247 my $totalcount = $count;
248 my $alreadyreserved;
250 foreach my $res (@$reserves) {
251 if ( defined $res->{found} && $res->{found} eq 'W' ) {
252 $count--;
255 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
256 $warnings = 1;
257 $alreadyreserved = 1;
258 $biblioloopiter{warn} = 1;
259 $biblioloopiter{alreadyres} = 1;
263 $template->param( alreadyreserved => $alreadyreserved,
264 messages => $messages,
265 warnings => $warnings,
266 maxreserves=>$maxreserves
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 $biblioitem->{description} =
332 $itemtypes->{ $biblioitem->{itemtype} }{description};
333 if($biblioitem->{biblioitemnumber} ne $biblionumber){
334 $biblioitem->{hostitemsflag}=1;
336 $biblioloopiter{description} = $biblioitem->{description};
337 $biblioloopiter{itypename} = $biblioitem->{description};
338 $biblioloopiter{imageurl} =
339 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
341 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
342 my $item = $iteminfos_of->{$itemnumber};
344 unless (C4::Context->preference('item-level_itypes')) {
345 $item->{itype} = $biblioitem->{itemtype};
348 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
349 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
350 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
352 # if the holdingbranch is different than the homebranch, we show the
353 # holdingbranch of the document too
354 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
355 $item->{holdingbranchname} =
356 $branches->{ $item->{holdingbranch} }{branchname};
359 if($item->{biblionumber} ne $biblionumber){
360 $item->{hostitemsflag}=1;
361 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
364 # add information
365 $item->{itemcallnumber} = $item->{itemcallnumber};
367 # if the item is currently on loan, we display its return date and
368 # change the background color
369 my $issues= GetItemIssue($itemnumber);
370 if ( $issues->{'date_due'} ) {
371 $item->{date_due} = format_sqldatetime($issues->{date_due});
372 $item->{backgroundcolor} = 'onloan';
375 # checking reserve
376 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
377 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
379 if ( defined $reservedate ) {
380 $item->{backgroundcolor} = 'reserved';
381 $item->{reservedate} = format_date($reservedate);
382 $item->{ReservedForBorrowernumber} = $reservedfor;
383 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
384 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
385 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
389 # Management of the notforloan document
390 if ( $item->{notforloan} ) {
391 $item->{backgroundcolor} = 'other';
392 $item->{notforloanvalue} =
393 $notforloan_label_of->{ $item->{notforloan} };
396 # Management of lost or long overdue items
397 if ( $item->{itemlost} ) {
399 # FIXME localized strings should never be in Perl code
400 $item->{message} =
401 $item->{itemlost} == 1 ? "(lost)"
402 : $item->{itemlost} == 2 ? "(long overdue)"
403 : "";
404 $item->{backgroundcolor} = 'other';
405 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
406 $item->{hide} = 1;
407 $hiddencount++;
411 # Check the transit status
412 my ( $transfertwhen, $transfertfrom, $transfertto ) =
413 GetTransfers($itemnumber);
415 if ( defined $transfertwhen && $transfertwhen ne '' ) {
416 $item->{transfertwhen} = format_date($transfertwhen);
417 $item->{transfertfrom} =
418 $branches->{$transfertfrom}{branchname};
419 $item->{transfertto} = $branches->{$transfertto}{branchname};
420 $item->{nocancel} = 1;
423 # If there is no loan, return and transfer, we show a checkbox.
424 $item->{notforloan} = $item->{notforloan} || 0;
426 # if independent branches is on we need to check if the person can reserve
427 # for branches they arent logged in to
428 if ( C4::Context->preference("IndependantBranches") ) {
429 if (! C4::Context->preference("canreservefromotherbranches")){
430 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
431 my $userenv = C4::Context->userenv;
432 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
433 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
438 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
440 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
441 my $policy_holdallowed = 1;
443 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
445 if ( $branchitemrule->{'holdallowed'} == 0 ||
446 ( $branchitemrule->{'holdallowed'} == 1 &&
447 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
448 $policy_holdallowed = 0;
451 if (
452 $policy_holdallowed
453 && !$item->{cantreserve}
454 && IsAvailableForItemLevelRequest($itemnumber)
455 && CanItemBeReserved(
456 $borrowerinfo->{borrowernumber}, $itemnumber
460 $item->{available} = 1;
461 $num_available++;
463 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
465 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
466 $item->{override} = 1;
467 $num_override++;
470 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
472 # FIXME: move this to a pm
473 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
474 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
475 while (my $wait_hashref = $sth2->fetchrow_hashref) {
476 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
479 # Show serial enumeration when needed
480 if ($item->{enumchron}) {
481 $itemdata_enumchron = 1;
484 push @{ $biblioitem->{itemloop} }, $item;
487 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
488 $template->param( override_required => 1 );
489 } elsif ( $num_available == 0 ) {
490 $template->param( none_available => 1 );
491 $template->param( warnings => 1 );
492 $biblioloopiter{warn} = 1;
493 $biblioloopiter{none_avail} = 1;
495 $template->param( hiddencount => $hiddencount);
497 push @bibitemloop, $biblioitem;
500 # existingreserves building
501 my @reserveloop;
502 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
503 foreach my $res ( sort {
504 my $a_found = $a->{found} || '';
505 my $b_found = $a->{found} || '';
506 $a_found cmp $b_found;
507 } @$reserves ) {
508 my %reserve;
509 my @optionloop;
510 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
511 push(
512 @optionloop,
514 num => $i,
515 selected => ( $i == $res->{priority} ),
520 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
521 my $item = $res->{'itemnumber'};
522 $item = GetBiblioFromItemNumber($item,undef);
523 $reserve{'wait'}= 1;
524 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
525 $reserve{'biblionumber'}=$item->{'biblionumber'};
526 $reserve{'barcodenumber'} = $item->{'barcode'};
527 $reserve{'wbrcode'} = $res->{'branchcode'};
528 $reserve{'itemnumber'} = $res->{'itemnumber'};
529 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
530 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
531 $reserve{'atdestination'} = 1;
533 # set found to 1 if reserve is waiting for patron pickup
534 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
535 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
536 } elsif ($res->{priority} > 0) {
537 if (defined($res->{itemnumber})) {
538 my $item = GetItem($res->{itemnumber});
539 $reserve{'itemnumber'} = $res->{'itemnumber'};
540 $reserve{'barcodenumber'} = $item->{'barcode'};
541 $reserve{'item_level_hold'} = 1;
545 # get borrowers reserve info
546 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
547 if (C4::Context->preference('HidePatronName')){
548 $reserve{'hidename'} = 1;
549 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
551 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
552 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
553 $reserve{'date'} = format_date( $res->{'reservedate'} );
554 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
555 $reserve{'biblionumber'} = $res->{'biblionumber'};
556 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
557 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
558 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
559 $reserve{'notes'} = $res->{'reservenotes'};
560 $reserve{'wait'} =
561 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
562 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
563 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
564 $reserve{'voldesc'} = $res->{'volumeddesc'};
565 $reserve{'ccode'} = $res->{'ccode'};
566 $reserve{'barcode'} = $res->{'barcode'};
567 $reserve{'priority'} = $res->{'priority'};
568 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
569 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
570 $reserve{'optionloop'} = \@optionloop;
572 push( @reserveloop, \%reserve );
575 # get the time for the form name...
576 my $time = time();
578 $template->param(
579 CGIbranch => $CGIbranch,
581 time => $time,
582 fixedRank => $fixedRank,
585 # display infos
586 $template->param(
587 optionloop => \@optionloop,
588 bibitemloop => \@bibitemloop,
589 itemdata_enumchron => $itemdata_enumchron,
590 date => $date,
591 biblionumber => $biblionumber,
592 findborrower => $findborrower,
593 title => $dat->{title},
594 author => $dat->{author},
595 holdsview => 1,
596 C4::Search::enabled_staff_search_views,
598 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
599 $template->param(
600 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
601 borrower_branchcode => $borrowerinfo->{'branchcode'},
604 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
606 $biblioloopiter{biblionumber} = $biblionumber;
607 $biblioloopiter{title} = $dat->{title};
608 $biblioloopiter{rank} = $fixedRank;
609 $biblioloopiter{reserveloop} = \@reserveloop;
611 if (@reserveloop) {
612 $template->param( reserveloop => \@reserveloop );
615 push @biblioloop, \%biblioloopiter;
618 $template->param( biblioloop => \@biblioloop );
619 $template->param( biblionumbers => $biblionumbers );
620 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
622 if ($multihold) {
623 $template->param( multi_hold => 1 );
626 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
627 $template->param( reserve_in_future => 1 );
630 # printout the page
631 output_html_with_http_headers $input, $cookie, $template->output;