Bug 6687 - allow people to be moved in the holds queue
[koha.git] / reserve / request.pl
blobb34584541f398ec071f2bd9e1f19d515493fda02
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; # GetBranches get_branchinfos_of
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
46 my $dbh = C4::Context->dbh;
47 my $sth;
48 my $input = new CGI;
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51 template_name => "reserve/request.tmpl",
52 query => $input,
53 type => "intranet",
54 authnotrequired => 0,
55 flagsrequired => { reserveforothers => 'place_holds' },
59 my $multihold = $input->param('multi_hold');
60 $template->param(multi_hold => $multihold);
61 my $showallitems = $input->param('showallitems');
63 # get Branches and Itemtypes
64 my $branches = GetBranches();
65 my $itemtypes = GetItemTypes();
67 my $default = C4::Context->userenv->{branch};
68 my @values;
69 my %label_of;
71 foreach my $branchcode (sort keys %{$branches} ) {
72 push @values, $branchcode;
73 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
75 my $CGIbranch = CGI::scrolling_list(
76 -name => 'pickup',
77 -id => 'pickup',
78 -values => \@values,
79 -default => $default,
80 -labels => \%label_of,
81 -size => 1,
82 -multiple => 0,
85 # Select borrowers infos
86 my $findborrower = $input->param('findborrower');
87 $findborrower = '' unless defined $findborrower;
88 $findborrower =~ s|,| |g;
89 my $borrowernumber_hold = $input->param('borrowernumber') || '';
90 my $borrowerslist;
91 my $messageborrower;
92 my $warnings;
93 my $messages;
95 my $date = C4::Dates->today('iso');
96 my $action = $input->param('action');
97 $action ||= q{};
99 if ( $action eq 'move' ) {
100 my $where = $input->param('where');
101 my $borrowernumber = $input->param('borrowernumber');
102 my $biblionumber = $input->param('biblionumber');
103 AlterPriority( $where, $borrowernumber, $biblionumber );
104 } elsif ( $action eq 'cancel' ) {
105 my $borrowernumber = $input->param('borrowernumber');
106 my $biblionumber = $input->param('biblionumber');
107 CancelReserve( $biblionumber, '', $borrowernumber );
108 } elsif ( $action eq 'setLowestPriority' ) {
109 my $borrowernumber = $input->param('borrowernumber');
110 my $biblionumber = $input->param('biblionumber');
111 ToggleLowestPriority( $borrowernumber, $biblionumber );
114 if ($findborrower) {
115 my ( $count, $borrowers ) =
116 SearchMember($findborrower, 'cardnumber', 'web' );
118 my @borrowers = @$borrowers;
120 if ( !@borrowers ) {
121 $messageborrower = "'$findborrower'";
123 elsif ( @borrowers == 1 ) {
124 $borrowernumber_hold = $borrowers[0]->{'borrowernumber'};
126 else {
127 $borrowerslist = \@borrowers;
131 # If we have the borrowernumber because we've performed an action, then we
132 # don't want to try to place another reserve.
133 if ($borrowernumber_hold && !$action) {
134 my $borrowerinfo = GetMemberDetails( $borrowernumber_hold );
135 my $diffbranch;
136 my @getreservloop;
137 my $count_reserv = 0;
138 my $maxreserves;
140 # we check the reserves of the borrower, and if he can reserv a document
141 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
143 my $number_reserves =
144 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
146 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
147 $warnings = 1;
148 $maxreserves = 1;
151 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
152 my $expiry_date = $borrowerinfo->{dateexpiry};
153 my $expiry = 0; # flag set if patron account has expired
154 if ($expiry_date and $expiry_date ne '0000-00-00' and
155 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
156 $messages = $expiry = 1;
159 # check if the borrower make the reserv in a different branch
160 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
161 $messages = 1;
162 $diffbranch = 1;
165 $template->param(
166 borrowernumber => $borrowerinfo->{'borrowernumber'},
167 borrowersurname => $borrowerinfo->{'surname'},
168 borrowerfirstname => $borrowerinfo->{'firstname'},
169 borrowerstreetaddress => $borrowerinfo->{'address'},
170 borrowercity => $borrowerinfo->{'city'},
171 borrowerphone => $borrowerinfo->{'phone'},
172 borrowermobile => $borrowerinfo->{'mobile'},
173 borrowerfax => $borrowerinfo->{'fax'},
174 borrowerphonepro => $borrowerinfo->{'phonepro'},
175 borroweremail => $borrowerinfo->{'email'},
176 borroweremailpro => $borrowerinfo->{'emailpro'},
177 borrowercategory => $borrowerinfo->{'category'},
178 borrowerreservs => $count_reserv,
179 cardnumber => $borrowerinfo->{'cardnumber'},
180 maxreserves => $maxreserves,
181 expiry => $expiry,
182 diffbranch => $diffbranch,
183 messages => $messages,
184 warnings => $warnings
188 $template->param( messageborrower => $messageborrower );
190 my $CGIselectborrower;
191 if ($borrowerslist) {
192 my @values;
193 my %labels;
195 foreach my $borrower (
196 sort {
197 $a->{surname}
198 . $a->{firstname} cmp $b->{surname}
199 . $b->{firstname}
200 } @{$borrowerslist}
203 push @values, $borrower->{borrowernumber};
205 $labels{ $borrower->{borrowernumber} } = sprintf(
206 '%s, %s ... (%s - %s) ... %s',
207 $borrower->{surname} ||'', $borrower->{firstname} || '',
208 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
209 $borrower->{address} || '',
213 $CGIselectborrower = CGI::scrolling_list(
214 -name => 'borrowernumber',
215 -values => \@values,
216 -labels => \%labels,
217 -size => 7,
218 -multiple => 0,
222 # FIXME launch another time GetMemberDetails perhaps until
223 my $borrowerinfo = GetMemberDetails( $borrowernumber_hold );
225 my @biblionumbers = ();
226 my $biblionumbers = $input->param('biblionumbers');
227 if ($multihold) {
228 @biblionumbers = split '/', $biblionumbers;
229 } else {
230 push @biblionumbers, $input->param('biblionumber');
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 else {
295 $template->param('noitems' => 1);
296 $biblioloopiter{noitems} = 1;
299 ## Hash of item number to 'item' table fields
300 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
302 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
303 ## when by definition all of the itemnumber have the same biblioitemnumber
304 foreach my $itemnumber (@itemnumbers) {
305 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
306 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
309 ## Should be same as biblionumber
310 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
312 my $notforloan_label_of = get_notforloan_label_of();
314 ## Hash of biblioitemnumber to 'biblioitem' table records
315 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
317 my @bibitemloop;
319 foreach my $biblioitemnumber (@biblioitemnumbers) {
320 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
321 my $num_available = 0;
322 my $num_override = 0;
323 my $hiddencount = 0;
325 $biblioitem->{description} =
326 $itemtypes->{ $biblioitem->{itemtype} }{description};
327 $biblioloopiter{description} = $biblioitem->{description};
328 $biblioloopiter{itypename} = $biblioitem->{description};
329 $biblioloopiter{imageurl} =
330 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
332 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
333 my $item = $iteminfos_of->{$itemnumber};
335 unless (C4::Context->preference('item-level_itypes')) {
336 $item->{itype} = $biblioitem->{itemtype};
339 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
340 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
341 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
343 # if the holdingbranch is different than the homebranch, we show the
344 # holdingbranch of the document too
345 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
346 $item->{holdingbranchname} =
347 $branches->{ $item->{holdingbranch} }{branchname};
350 # add information
351 $item->{itemcallnumber} = $item->{itemcallnumber};
353 # if the item is currently on loan, we display its return date and
354 # change the background color
355 my $issues= GetItemIssue($itemnumber);
356 if ( $issues->{'date_due'} ) {
357 $item->{date_due} = format_date($issues->{'date_due'});
358 $item->{backgroundcolor} = 'onloan';
361 # checking reserve
362 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
363 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
365 if ( defined $reservedate ) {
366 $item->{backgroundcolor} = 'reserved';
367 $item->{reservedate} = format_date($reservedate);
368 $item->{ReservedForBorrowernumber} = $reservedfor;
369 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
370 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
371 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
375 # Management of the notforloan document
376 if ( $item->{notforloan} ) {
377 $item->{backgroundcolor} = 'other';
378 $item->{notforloanvalue} =
379 $notforloan_label_of->{ $item->{notforloan} };
382 # Management of lost or long overdue items
383 if ( $item->{itemlost} ) {
385 # FIXME localized strings should never be in Perl code
386 $item->{message} =
387 $item->{itemlost} == 1 ? "(lost)"
388 : $item->{itemlost} == 2 ? "(long overdue)"
389 : "";
390 $item->{backgroundcolor} = 'other';
391 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
392 $item->{hide} = 1;
393 $hiddencount++;
397 # Check the transit status
398 my ( $transfertwhen, $transfertfrom, $transfertto ) =
399 GetTransfers($itemnumber);
401 if ( defined $transfertwhen && $transfertwhen ne '' ) {
402 $item->{transfertwhen} = format_date($transfertwhen);
403 $item->{transfertfrom} =
404 $branches->{$transfertfrom}{branchname};
405 $item->{transfertto} = $branches->{$transfertto}{branchname};
406 $item->{nocancel} = 1;
409 # If there is no loan, return and transfer, we show a checkbox.
410 $item->{notforloan} = $item->{notforloan} || 0;
412 # if independent branches is on we need to check if the person can reserve
413 # for branches they arent logged in to
414 if ( C4::Context->preference("IndependantBranches") ) {
415 if (! C4::Context->preference("canreservefromotherbranches")){
416 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
417 my $userenv = C4::Context->userenv;
418 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
419 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
424 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
426 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
427 my $policy_holdallowed = 1;
429 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
431 if ( $branchitemrule->{'holdallowed'} == 0 ||
432 ( $branchitemrule->{'holdallowed'} == 1 &&
433 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
434 $policy_holdallowed = 0;
437 if (IsAvailableForItemLevelRequest($itemnumber) and
438 not $item->{cantreserve} and
439 CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber) ) {
440 if ( $policy_holdallowed ) {
441 $item->{available} = 1;
442 $num_available++;
444 } elsif (C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
445 $item->{override} = 1;
446 $num_override++;
448 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
449 if (C4::Context->preference( 'AllowHoldPolicyOverride' ) && !$item->{available} ) {
450 $item->{override} = 1;
451 $num_override++;
454 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
456 # FIXME: move this to a pm
457 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
458 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
459 while (my $wait_hashref = $sth2->fetchrow_hashref) {
460 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
462 push @{ $biblioitem->{itemloop} }, $item;
465 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
466 $template->param( override_required => 1 );
467 } elsif ( $num_available == 0 ) {
468 $template->param( none_available => 1 );
469 $template->param( warnings => 1 );
470 $biblioloopiter{warn} = 1;
471 $biblioloopiter{none_avail} = 1;
473 $template->param( hiddencount => $hiddencount);
475 push @bibitemloop, $biblioitem;
478 # existingreserves building
479 my @reserveloop;
480 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
481 foreach my $res ( sort {
482 my $a_found = $a->{found} || '';
483 my $b_found = $a->{found} || '';
484 $a_found cmp $b_found;
485 } @$reserves ) {
486 my %reserve;
487 my @optionloop;
488 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
489 push(
490 @optionloop,
492 num => $i,
493 selected => ( $i == $res->{priority} ),
498 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
499 my $item = $res->{'itemnumber'};
500 $item = GetBiblioFromItemNumber($item,undef);
501 $reserve{'wait'}= 1;
502 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
503 $reserve{'biblionumber'}=$item->{'biblionumber'};
504 $reserve{'barcodenumber'} = $item->{'barcode'};
505 $reserve{'wbrcode'} = $res->{'branchcode'};
506 $reserve{'itemnumber'} = $res->{'itemnumber'};
507 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
508 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
509 $reserve{'atdestination'} = 1;
511 # set found to 1 if reserve is waiting for patron pickup
512 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
513 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
514 } elsif ($res->{priority} > 0) {
515 if (defined($res->{itemnumber})) {
516 my $item = GetItem($res->{itemnumber});
517 $reserve{'itemnumber'} = $res->{'itemnumber'};
518 $reserve{'barcodenumber'} = $item->{'barcode'};
519 $reserve{'item_level_hold'} = 1;
523 # get borrowers reserve info
524 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
525 if (C4::Context->preference('HidePatronName')){
526 $reserve{'hidename'} = 1;
527 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
529 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
530 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
531 $reserve{'date'} = format_date( $res->{'reservedate'} );
532 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
533 $reserve{'biblionumber'} = $res->{'biblionumber'};
534 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
535 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
536 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
537 $reserve{'notes'} = $res->{'reservenotes'};
538 $reserve{'wait'} =
539 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
540 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
541 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
542 $reserve{'voldesc'} = $res->{'volumeddesc'};
543 $reserve{'ccode'} = $res->{'ccode'};
544 $reserve{'barcode'} = $res->{'barcode'};
545 $reserve{'priority'} = $res->{'priority'};
546 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
547 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
548 $reserve{'optionloop'} = \@optionloop;
550 push( @reserveloop, \%reserve );
553 # get the time for the form name...
554 my $time = time();
556 $template->param(
557 CGIbranch => $CGIbranch,
559 time => $time,
560 fixedRank => $fixedRank,
563 # display infos
564 $template->param(
565 optionloop => \@optionloop,
566 bibitemloop => \@bibitemloop,
567 date => $date,
568 biblionumber => $biblionumber,
569 findborrower => $findborrower,
570 title => $dat->{title},
571 author => $dat->{author},
572 holdsview => 1,
573 C4::Search::enabled_staff_search_views,
575 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
576 $template->param(
577 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
578 borrower_branchcode => $borrowerinfo->{'branchcode'},
581 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
583 $biblioloopiter{biblionumber} = $biblionumber;
584 $biblioloopiter{title} = $dat->{title};
585 $biblioloopiter{rank} = $fixedRank;
586 $biblioloopiter{reserveloop} = \@reserveloop;
588 if (@reserveloop) {
589 $template->param( reserveloop => \@reserveloop );
592 push @biblioloop, \%biblioloopiter;
595 $template->param( biblioloop => \@biblioloop );
596 $template->param( biblionumbers => $biblionumbers );
597 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
599 if ($multihold) {
600 $template->param( multi_hold => 1 );
603 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
604 $template->param( reserve_in_future => 1 );
607 # printout the page
608 output_html_with_http_headers $input, $cookie, $template->output;