Bug 12692 - Markup improvements to holds awaiting pickup report
[koha.git] / reserve / request.pl
blob14340c24b362d59887278adcff954aeab22c0c35
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, $flags ) = get_template_and_user(
52 template_name => "reserve/request.tt",
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 $userbranch = '';
69 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
70 $userbranch = C4::Context->userenv->{'branch'};
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s|,| |g;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
79 my $messageborrower;
80 my $maxreserves;
81 my $warnings;
82 my $messages;
84 my $date = C4::Dates->today('iso');
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 AlterPriority( $where, $reserve_id );
92 } elsif ( $action eq 'cancel' ) {
93 my $reserve_id = $input->param('reserve_id');
94 CancelReserve({ reserve_id => $reserve_id });
95 } elsif ( $action eq 'setLowestPriority' ) {
96 my $reserve_id = $input->param('reserve_id');
97 ToggleLowestPriority( $reserve_id );
98 } elsif ( $action eq 'toggleSuspend' ) {
99 my $reserve_id = $input->param('reserve_id');
100 my $suspend_until = $input->param('suspend_until');
101 ToggleSuspend( $reserve_id, $suspend_until );
104 if ($findborrower) {
105 my $borrowers = Search($findborrower, 'cardnumber');
107 if ($borrowers && @$borrowers) {
108 if ( @$borrowers == 1 ) {
109 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
111 else {
112 $template->param( borrower_list => sort_borrowerlist($borrowers));
114 } else {
115 $messageborrower = "'$findborrower'";
119 # If we have the borrowernumber because we've performed an action, then we
120 # don't want to try to place another reserve.
121 if ($borrowernumber_hold && !$action) {
122 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
123 my $diffbranch;
124 my @getreservloop;
125 my $count_reserv = 0;
127 # we check the reserves of the borrower, and if he can reserv a document
128 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
130 my $number_reserves =
131 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
133 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
134 $warnings = 1;
135 $maxreserves = 1;
138 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
139 my $expiry_date = $borrowerinfo->{dateexpiry};
140 my $expiry = 0; # flag set if patron account has expired
141 if ($expiry_date and $expiry_date ne '0000-00-00' and
142 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
143 $expiry = 1;
146 # check if the borrower make the reserv in a different branch
147 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
148 $diffbranch = 1;
151 $template->param(
152 borrowernumber => $borrowerinfo->{'borrowernumber'},
153 borrowersurname => $borrowerinfo->{'surname'},
154 borrowerfirstname => $borrowerinfo->{'firstname'},
155 borrowerstreetaddress => $borrowerinfo->{'address'},
156 borrowercity => $borrowerinfo->{'city'},
157 borrowerphone => $borrowerinfo->{'phone'},
158 borrowermobile => $borrowerinfo->{'mobile'},
159 borrowerfax => $borrowerinfo->{'fax'},
160 borrowerphonepro => $borrowerinfo->{'phonepro'},
161 borroweremail => $borrowerinfo->{'email'},
162 borroweremailpro => $borrowerinfo->{'emailpro'},
163 borrowercategory => $borrowerinfo->{'category'},
164 borrowerreservs => $count_reserv,
165 cardnumber => $borrowerinfo->{'cardnumber'},
166 expiry => $expiry,
167 diffbranch => $diffbranch,
168 messages => $messages,
169 warnings => $warnings
173 $template->param( messageborrower => $messageborrower );
175 # FIXME launch another time GetMember perhaps until
176 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
178 my @biblionumbers = ();
179 my $biblionumbers = $input->param('biblionumbers');
180 if ($multihold) {
181 @biblionumbers = split '/', $biblionumbers;
182 } else {
183 push @biblionumbers, $input->param('biblionumber');
186 my $itemdata_enumchron = 0;
187 my @biblioloop = ();
188 foreach my $biblionumber (@biblionumbers) {
190 my %biblioloopiter = ();
192 my $dat = GetBiblioData($biblionumber);
194 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
195 $maxreserves = 1;
198 my $alreadypossession;
199 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
200 $alreadypossession = 1;
203 # get existing reserves .....
204 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
205 my $count = scalar( @$reserves );
206 my $totalcount = $count;
207 my $holds_count = 0;
208 my $alreadyreserved = 0;
210 foreach my $res (@$reserves) {
211 if ( defined $res->{found} ) { # found can be 'W' or 'T'
212 $count--;
215 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
216 $holds_count++;
220 if ( $holds_count ) {
221 $alreadyreserved = 1;
222 $biblioloopiter{warn} = 1;
223 $biblioloopiter{alreadyres} = 1;
226 $template->param(
227 alreadyreserved => $alreadyreserved,
228 alreadypossession => $alreadypossession,
231 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
232 # make priorities options
234 my @optionloop;
235 for ( 1 .. $count + 1 ) {
236 push(
237 @optionloop,
239 num => $_,
240 selected => ( $_ == $count + 1 ),
244 # adding a fixed value for priority options
245 my $fixedRank = $count+1;
247 my @branchcodes;
248 my %itemnumbers_of_biblioitem;
249 my @itemnumbers;
251 ## $items is array of 'item' table numbers
252 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
253 @itemnumbers = @$items;
255 my @hostitems = get_hostitemnumbers_of($biblionumber);
256 if (@hostitems){
257 $template->param('hostitemsflag' => 1);
258 push(@itemnumbers, @hostitems);
261 if (!@itemnumbers) {
262 $template->param('noitems' => 1);
263 $biblioloopiter{noitems} = 1;
266 ## Hash of item number to 'item' table fields
267 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
269 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
270 ## when by definition all of the itemnumber have the same biblioitemnumber
271 foreach my $itemnumber (@itemnumbers) {
272 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
273 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
276 ## Should be same as biblionumber
277 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
279 my $notforloan_label_of = get_notforloan_label_of();
281 ## Hash of biblioitemnumber to 'biblioitem' table records
282 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
284 my @bibitemloop;
286 foreach my $biblioitemnumber (@biblioitemnumbers) {
287 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
288 my $num_available = 0;
289 my $num_override = 0;
290 my $hiddencount = 0;
292 $biblioitem->{description} =
293 $itemtypes->{ $biblioitem->{itemtype} }{description};
294 if($biblioitem->{biblioitemnumber} ne $biblionumber){
295 $biblioitem->{hostitemsflag}=1;
297 $biblioloopiter{description} = $biblioitem->{description};
298 $biblioloopiter{itypename} = $biblioitem->{description};
299 $biblioloopiter{imageurl} =
300 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
302 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
303 my $item = $iteminfos_of->{$itemnumber};
305 unless (C4::Context->preference('item-level_itypes')) {
306 $item->{itype} = $biblioitem->{itemtype};
309 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
310 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
311 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
313 # if the holdingbranch is different than the homebranch, we show the
314 # holdingbranch of the document too
315 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
316 $item->{holdingbranchname} =
317 $branches->{ $item->{holdingbranch} }{branchname};
320 if($item->{biblionumber} ne $biblionumber){
321 $item->{hostitemsflag}=1;
322 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
325 # add information
326 $item->{itemcallnumber} = $item->{itemcallnumber};
328 # if the item is currently on loan, we display its return date and
329 # change the background color
330 my $issues= GetItemIssue($itemnumber);
331 if ( $issues->{'date_due'} ) {
332 $item->{date_due} = format_sqldatetime($issues->{date_due});
333 $item->{backgroundcolor} = 'onloan';
336 # checking reserve
337 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
338 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
340 if ( defined $reservedate ) {
341 $item->{backgroundcolor} = 'reserved';
342 $item->{reservedate} = format_date($reservedate);
343 $item->{ReservedForBorrowernumber} = $reservedfor;
344 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
345 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
346 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
347 $item->{waitingdate} = $wait;
350 # Management of the notforloan document
351 if ( $item->{notforloan} ) {
352 $item->{backgroundcolor} = 'other';
353 $item->{notforloanvalue} =
354 $notforloan_label_of->{ $item->{notforloan} };
357 # Management of lost or long overdue items
358 if ( $item->{itemlost} ) {
360 # FIXME localized strings should never be in Perl code
361 $item->{message} =
362 $item->{itemlost} == 1 ? "(lost)"
363 : $item->{itemlost} == 2 ? "(long overdue)"
364 : "";
365 $item->{backgroundcolor} = 'other';
366 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
367 $item->{hide} = 1;
368 $hiddencount++;
372 # Check the transit status
373 my ( $transfertwhen, $transfertfrom, $transfertto ) =
374 GetTransfers($itemnumber);
376 if ( defined $transfertwhen && $transfertwhen ne '' ) {
377 $item->{transfertwhen} = format_date($transfertwhen);
378 $item->{transfertfrom} =
379 $branches->{$transfertfrom}{branchname};
380 $item->{transfertto} = $branches->{$transfertto}{branchname};
381 $item->{nocancel} = 1;
384 # If there is no loan, return and transfer, we show a checkbox.
385 $item->{notforloan} = $item->{notforloan} || 0;
387 # if independent branches is on we need to check if the person can reserve
388 # for branches they arent logged in to
389 if ( C4::Context->preference("IndependentBranches") ) {
390 if (! C4::Context->preference("canreservefromotherbranches")){
391 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
392 my $userenv = C4::Context->userenv;
393 unless ( C4::Context->IsSuperLibrarian ) {
394 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
399 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
401 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
402 my $policy_holdallowed = 1;
404 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
406 if ( $branchitemrule->{'holdallowed'} == 0 ||
407 ( $branchitemrule->{'holdallowed'} == 1 &&
408 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
409 $policy_holdallowed = 0;
412 if (
413 $policy_holdallowed
414 && !$item->{cantreserve}
415 && IsAvailableForItemLevelRequest($itemnumber)
416 && CanItemBeReserved(
417 $borrowerinfo->{borrowernumber}, $itemnumber
421 $item->{available} = 1;
422 $num_available++;
424 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
425 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
426 $item->{override} = 1;
427 $num_override++;
430 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
432 # Show serial enumeration when needed
433 if ($item->{enumchron}) {
434 $itemdata_enumchron = 1;
437 push @{ $biblioitem->{itemloop} }, $item;
440 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
441 $template->param( override_required => 1 );
442 } elsif ( $num_available == 0 ) {
443 $template->param( none_available => 1 );
444 $biblioloopiter{warn} = 1;
445 $biblioloopiter{none_avail} = 1;
447 $template->param( hiddencount => $hiddencount);
449 push @bibitemloop, $biblioitem;
452 # existingreserves building
453 my @reserveloop;
454 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
455 foreach my $res ( sort {
456 my $a_found = $a->{found} || '';
457 my $b_found = $a->{found} || '';
458 $a_found cmp $b_found;
459 } @$reserves ) {
460 my %reserve;
461 my @optionloop;
462 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
463 push(
464 @optionloop,
466 num => $i,
467 selected => ( $i == $res->{priority} ),
472 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
473 my $item = $res->{'itemnumber'};
474 $item = GetBiblioFromItemNumber($item,undef);
475 $reserve{'wait'}= 1;
476 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
477 $reserve{'biblionumber'}=$item->{'biblionumber'};
478 $reserve{'barcodenumber'} = $item->{'barcode'};
479 $reserve{'wbrcode'} = $res->{'branchcode'};
480 $reserve{'itemnumber'} = $res->{'itemnumber'};
481 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
482 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
483 # Just because the holdingbranch matches the reserve branch doesn't mean the item
484 # has arrived at the destination, check for an open transfer for the item as well
485 my ( $transfertwhen, $transfertfrom, $transferto ) = C4::Circulation::GetTransfers( $res->{itemnumber} );
486 if ( not $transferto or $transferto ne $res->{branchcode} ) {
487 $reserve{'atdestination'} = 1;
490 # set found to 1 if reserve is waiting for patron pickup
491 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
492 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
493 } elsif ($res->{priority} > 0) {
494 if (defined($res->{itemnumber})) {
495 my $item = GetItem($res->{itemnumber});
496 $reserve{'itemnumber'} = $res->{'itemnumber'};
497 $reserve{'barcodenumber'} = $item->{'barcode'};
498 $reserve{'item_level_hold'} = 1;
502 # get borrowers reserve info
503 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
504 if (C4::Context->preference('HidePatronName')){
505 $reserve{'hidename'} = 1;
506 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
508 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
509 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
510 $reserve{'date'} = format_date( $res->{'reservedate'} );
511 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
512 $reserve{'biblionumber'} = $res->{'biblionumber'};
513 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
514 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
515 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
516 $reserve{'notes'} = $res->{'reservenotes'};
517 $reserve{'wait'} =
518 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
519 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
520 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
521 $reserve{'voldesc'} = $res->{'volumeddesc'};
522 $reserve{'ccode'} = $res->{'ccode'};
523 $reserve{'barcode'} = $res->{'barcode'};
524 $reserve{'priority'} = $res->{'priority'};
525 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
526 $reserve{'optionloop'} = \@optionloop;
527 $reserve{'suspend'} = $res->{'suspend'};
528 $reserve{'suspend_until'} = $res->{'suspend_until'};
529 $reserve{'reserve_id'} = $res->{'reserve_id'};
531 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
532 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
533 } else {
534 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
537 push( @reserveloop, \%reserve );
540 # get the time for the form name...
541 my $time = time();
543 $template->param(
544 branchloop => GetBranchesLoop($userbranch),
545 time => $time,
546 fixedRank => $fixedRank,
549 # display infos
550 $template->param(
551 optionloop => \@optionloop,
552 bibitemloop => \@bibitemloop,
553 itemdata_enumchron => $itemdata_enumchron,
554 date => $date,
555 biblionumber => $biblionumber,
556 findborrower => $findborrower,
557 title => $dat->{title},
558 author => $dat->{author},
559 holdsview => 1,
560 C4::Search::enabled_staff_search_views,
562 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
563 $template->param(
564 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
565 borrower_branchcode => $borrowerinfo->{'branchcode'},
569 $biblioloopiter{biblionumber} = $biblionumber;
570 $biblioloopiter{title} = $dat->{title};
571 $biblioloopiter{rank} = $fixedRank;
572 $biblioloopiter{reserveloop} = \@reserveloop;
574 if (@reserveloop) {
575 $template->param( reserveloop => \@reserveloop );
578 push @biblioloop, \%biblioloopiter;
581 $template->param( biblioloop => \@biblioloop );
582 $template->param( biblionumbers => $biblionumbers );
583 $template->param( maxreserves => $maxreserves );
585 if ($multihold) {
586 $template->param( multi_hold => 1 );
589 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
590 $template->param( reserve_in_future => 1 );
593 $template->param(
594 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
595 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
598 # printout the page
599 output_html_with_http_headers $input, $cookie, $template->output;
601 sub sort_borrowerlist {
602 my $borrowerslist = shift;
603 my $ref = [];
604 push @{$ref}, sort {
605 uc( $a->{surname} . $a->{firstname} ) cmp
606 uc( $b->{surname} . $b->{firstname} )
607 } @{$borrowerslist};
608 return $ref;