Bug 12265: [QA Follow-up] - Improve usage of find
[koha.git] / reserve / request.pl
bloba86e0e78b41dac6089f718e4a4e9a266e7243e18
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.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 $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;
82 my $date = C4::Dates->today('iso');
83 my $action = $input->param('action');
84 $action ||= q{};
86 if ( $action eq 'move' ) {
87 my $where = $input->param('where');
88 my $reserve_id = $input->param('reserve_id');
89 AlterPriority( $where, $reserve_id );
90 } elsif ( $action eq 'cancel' ) {
91 my $reserve_id = $input->param('reserve_id');
92 CancelReserve({ reserve_id => $reserve_id });
93 } elsif ( $action eq 'setLowestPriority' ) {
94 my $reserve_id = $input->param('reserve_id');
95 ToggleLowestPriority( $reserve_id );
96 } elsif ( $action eq 'toggleSuspend' ) {
97 my $reserve_id = $input->param('reserve_id');
98 my $suspend_until = $input->param('suspend_until');
99 ToggleSuspend( $reserve_id, $suspend_until );
102 if ($findborrower) {
103 my $borrowers = Search($findborrower, 'cardnumber');
105 if ($borrowers && @$borrowers) {
106 if ( @$borrowers == 1 ) {
107 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
109 else {
110 $template->param( borrower_list => sort_borrowerlist($borrowers));
112 } else {
113 $messageborrower = "'$findborrower'";
117 # If we have the borrowernumber because we've performed an action, then we
118 # don't want to try to place another reserve.
119 if ($borrowernumber_hold && !$action) {
120 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
121 my $diffbranch;
122 my @getreservloop;
123 my $count_reserv = 0;
125 # we check the reserves of the borrower, and if he can reserv a document
126 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
128 my $number_reserves =
129 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
131 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
132 $maxreserves = 1;
135 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
136 my $expiry_date = $borrowerinfo->{dateexpiry};
137 my $expiry = 0; # flag set if patron account has expired
138 if ($expiry_date and $expiry_date ne '0000-00-00' and
139 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
140 $expiry = 1;
143 # check if the borrower make the reserv in a different branch
144 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
145 $diffbranch = 1;
148 $template->param(
149 borrowernumber => $borrowerinfo->{'borrowernumber'},
150 borrowersurname => $borrowerinfo->{'surname'},
151 borrowerfirstname => $borrowerinfo->{'firstname'},
152 borrowerstreetaddress => $borrowerinfo->{'address'},
153 borrowercity => $borrowerinfo->{'city'},
154 borrowerphone => $borrowerinfo->{'phone'},
155 borrowermobile => $borrowerinfo->{'mobile'},
156 borrowerfax => $borrowerinfo->{'fax'},
157 borrowerphonepro => $borrowerinfo->{'phonepro'},
158 borroweremail => $borrowerinfo->{'email'},
159 borroweremailpro => $borrowerinfo->{'emailpro'},
160 borrowercategory => $borrowerinfo->{'category'},
161 borrowerreservs => $count_reserv,
162 cardnumber => $borrowerinfo->{'cardnumber'},
163 expiry => $expiry,
164 diffbranch => $diffbranch,
168 $template->param( messageborrower => $messageborrower );
170 # FIXME launch another time GetMember perhaps until
171 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
173 my @biblionumbers = ();
174 my $biblionumbers = $input->param('biblionumbers');
175 if ($multihold) {
176 @biblionumbers = split '/', $biblionumbers;
177 } else {
178 push @biblionumbers, $input->param('biblionumber');
181 my $itemdata_enumchron = 0;
182 my @biblioloop = ();
183 foreach my $biblionumber (@biblionumbers) {
185 my %biblioloopiter = ();
187 my $dat = GetBiblioData($biblionumber);
189 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
190 $maxreserves = 1;
193 my $alreadypossession;
194 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
195 $alreadypossession = 1;
198 # get existing reserves .....
199 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
200 my $count = scalar( @$reserves );
201 my $totalcount = $count;
202 my $holds_count = 0;
203 my $alreadyreserved = 0;
205 foreach my $res (@$reserves) {
206 if ( defined $res->{found} && $res->{found} eq 'W' ) {
207 $count--;
210 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
211 $holds_count++;
215 if ( $holds_count ) {
216 $alreadyreserved = 1;
217 $biblioloopiter{warn} = 1;
218 $biblioloopiter{alreadyres} = 1;
221 $template->param(
222 alreadyreserved => $alreadyreserved,
223 alreadypossession => $alreadypossession,
226 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
227 # make priorities options
229 my @optionloop;
230 for ( 1 .. $count + 1 ) {
231 push(
232 @optionloop,
234 num => $_,
235 selected => ( $_ == $count + 1 ),
239 # adding a fixed value for priority options
240 my $fixedRank = $count+1;
242 my @branchcodes;
243 my %itemnumbers_of_biblioitem;
244 my @itemnumbers;
246 ## $items is array of 'item' table numbers
247 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
248 @itemnumbers = @$items;
250 my @hostitems = get_hostitemnumbers_of($biblionumber);
251 if (@hostitems){
252 $template->param('hostitemsflag' => 1);
253 push(@itemnumbers, @hostitems);
256 if (!@itemnumbers) {
257 $template->param('noitems' => 1);
258 $biblioloopiter{noitems} = 1;
261 ## Hash of item number to 'item' table fields
262 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
264 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
265 ## when by definition all of the itemnumber have the same biblioitemnumber
266 foreach my $itemnumber (@itemnumbers) {
267 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
268 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
271 ## Should be same as biblionumber
272 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
274 my $notforloan_label_of = get_notforloan_label_of();
276 ## Hash of biblioitemnumber to 'biblioitem' table records
277 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
279 my @bibitemloop;
281 foreach my $biblioitemnumber (@biblioitemnumbers) {
282 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
283 my $num_available = 0;
284 my $num_override = 0;
285 my $hiddencount = 0;
287 $biblioitem->{description} =
288 $itemtypes->{ $biblioitem->{itemtype} }{description};
289 if($biblioitem->{biblioitemnumber} ne $biblionumber){
290 $biblioitem->{hostitemsflag}=1;
292 $biblioloopiter{description} = $biblioitem->{description};
293 $biblioloopiter{itypename} = $biblioitem->{description};
294 $biblioloopiter{imageurl} =
295 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
297 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
298 my $item = $iteminfos_of->{$itemnumber};
300 unless (C4::Context->preference('item-level_itypes')) {
301 $item->{itype} = $biblioitem->{itemtype};
304 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
305 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
306 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
308 # if the holdingbranch is different than the homebranch, we show the
309 # holdingbranch of the document too
310 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
311 $item->{holdingbranchname} =
312 $branches->{ $item->{holdingbranch} }{branchname};
315 if($item->{biblionumber} ne $biblionumber){
316 $item->{hostitemsflag}=1;
317 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
320 # add information
321 $item->{itemcallnumber} = $item->{itemcallnumber};
323 # if the item is currently on loan, we display its return date and
324 # change the background color
325 my $issues= GetItemIssue($itemnumber);
326 if ( $issues->{'date_due'} ) {
327 $item->{date_due} = format_sqldatetime($issues->{date_due});
328 $item->{backgroundcolor} = 'onloan';
331 # checking reserve
332 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
333 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
335 if ( defined $reservedate ) {
336 $item->{backgroundcolor} = 'reserved';
337 $item->{reservedate} = format_date($reservedate);
338 $item->{ReservedForBorrowernumber} = $reservedfor;
339 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
340 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
341 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
342 $item->{waitingdate} = $wait;
345 # Management of the notforloan document
346 if ( $item->{notforloan} ) {
347 $item->{backgroundcolor} = 'other';
348 $item->{notforloanvalue} =
349 $notforloan_label_of->{ $item->{notforloan} };
352 # Management of lost or long overdue items
353 if ( $item->{itemlost} ) {
355 # FIXME localized strings should never be in Perl code
356 $item->{message} =
357 $item->{itemlost} == 1 ? "(lost)"
358 : $item->{itemlost} == 2 ? "(long overdue)"
359 : "";
360 $item->{backgroundcolor} = 'other';
361 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
362 $item->{hide} = 1;
363 $hiddencount++;
367 # Check the transit status
368 my ( $transfertwhen, $transfertfrom, $transfertto ) =
369 GetTransfers($itemnumber);
371 if ( defined $transfertwhen && $transfertwhen ne '' ) {
372 $item->{transfertwhen} = format_date($transfertwhen);
373 $item->{transfertfrom} =
374 $branches->{$transfertfrom}{branchname};
375 $item->{transfertto} = $branches->{$transfertto}{branchname};
376 $item->{nocancel} = 1;
379 # If there is no loan, return and transfer, we show a checkbox.
380 $item->{notforloan} = $item->{notforloan} || 0;
382 # if independent branches is on we need to check if the person can reserve
383 # for branches they arent logged in to
384 if ( C4::Context->preference("IndependentBranches") ) {
385 if (! C4::Context->preference("canreservefromotherbranches")){
386 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
387 my $userenv = C4::Context->userenv;
388 unless ( C4::Context->IsSuperLibrarian ) {
389 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
394 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
396 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
397 my $policy_holdallowed = 1;
399 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
401 if ( $branchitemrule->{'holdallowed'} == 0 ||
402 ( $branchitemrule->{'holdallowed'} == 1 &&
403 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
404 $policy_holdallowed = 0;
407 if (
408 $policy_holdallowed
409 && !$item->{cantreserve}
410 && IsAvailableForItemLevelRequest($itemnumber)
411 && CanItemBeReserved(
412 $borrowerinfo->{borrowernumber}, $itemnumber
416 $item->{available} = 1;
417 $num_available++;
419 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
421 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
422 $item->{override} = 1;
423 $num_override++;
426 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
428 # Show serial enumeration when needed
429 if ($item->{enumchron}) {
430 $itemdata_enumchron = 1;
433 push @{ $biblioitem->{itemloop} }, $item;
436 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
437 $template->param( override_required => 1 );
438 } elsif ( $num_available == 0 ) {
439 $template->param( none_available => 1 );
440 $biblioloopiter{warn} = 1;
441 $biblioloopiter{none_avail} = 1;
443 $template->param( hiddencount => $hiddencount);
445 push @bibitemloop, $biblioitem;
448 # existingreserves building
449 my @reserveloop;
450 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
451 foreach my $res ( sort {
452 my $a_found = $a->{found} || '';
453 my $b_found = $a->{found} || '';
454 $a_found cmp $b_found;
455 } @$reserves ) {
456 my %reserve;
457 my @optionloop;
458 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
459 push(
460 @optionloop,
462 num => $i,
463 selected => ( $i == $res->{priority} ),
468 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
469 my $item = $res->{'itemnumber'};
470 $item = GetBiblioFromItemNumber($item,undef);
471 $reserve{'wait'}= 1;
472 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
473 $reserve{'biblionumber'}=$item->{'biblionumber'};
474 $reserve{'barcodenumber'} = $item->{'barcode'};
475 $reserve{'wbrcode'} = $res->{'branchcode'};
476 $reserve{'itemnumber'} = $res->{'itemnumber'};
477 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
478 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
479 # Just because the holdingbranch matches the reserve branch doesn't mean the item
480 # has arrived at the destination, check for an open transfer for the item as well
481 my ( $transfertwhen, $transfertfrom, $transferto ) = C4::Circulation::GetTransfers( $res->{itemnumber} );
482 if ( not $transferto or $transferto ne $res->{branchcode} ) {
483 $reserve{'atdestination'} = 1;
486 # set found to 1 if reserve is waiting for patron pickup
487 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
488 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
489 } elsif ($res->{priority} > 0) {
490 if (defined($res->{itemnumber})) {
491 my $item = GetItem($res->{itemnumber});
492 $reserve{'itemnumber'} = $res->{'itemnumber'};
493 $reserve{'barcodenumber'} = $item->{'barcode'};
494 $reserve{'item_level_hold'} = 1;
498 # get borrowers reserve info
499 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
500 if (C4::Context->preference('HidePatronName')){
501 $reserve{'hidename'} = 1;
502 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
504 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
505 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
506 $reserve{'date'} = format_date( $res->{'reservedate'} );
507 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
508 $reserve{'biblionumber'} = $res->{'biblionumber'};
509 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
510 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
511 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
512 $reserve{'notes'} = $res->{'reservenotes'};
513 $reserve{'wait'} =
514 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
515 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
516 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
517 $reserve{'voldesc'} = $res->{'volumeddesc'};
518 $reserve{'ccode'} = $res->{'ccode'};
519 $reserve{'barcode'} = $res->{'barcode'};
520 $reserve{'priority'} = $res->{'priority'};
521 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
522 $reserve{'optionloop'} = \@optionloop;
523 $reserve{'suspend'} = $res->{'suspend'};
524 $reserve{'suspend_until'} = $res->{'suspend_until'};
525 $reserve{'reserve_id'} = $res->{'reserve_id'};
527 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
528 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
529 } else {
530 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
533 push( @reserveloop, \%reserve );
536 # get the time for the form name...
537 my $time = time();
539 $template->param(
540 branchloop => GetBranchesLoop($userbranch),
541 time => $time,
542 fixedRank => $fixedRank,
545 # display infos
546 $template->param(
547 optionloop => \@optionloop,
548 bibitemloop => \@bibitemloop,
549 itemdata_enumchron => $itemdata_enumchron,
550 date => $date,
551 biblionumber => $biblionumber,
552 findborrower => $findborrower,
553 title => $dat->{title},
554 author => $dat->{author},
555 holdsview => 1,
556 C4::Search::enabled_staff_search_views,
558 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
559 $template->param(
560 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
561 borrower_branchcode => $borrowerinfo->{'branchcode'},
565 $biblioloopiter{biblionumber} = $biblionumber;
566 $biblioloopiter{title} = $dat->{title};
567 $biblioloopiter{rank} = $fixedRank;
568 $biblioloopiter{reserveloop} = \@reserveloop;
570 if (@reserveloop) {
571 $template->param( reserveloop => \@reserveloop );
574 push @biblioloop, \%biblioloopiter;
577 $template->param( biblioloop => \@biblioloop );
578 $template->param( biblionumbers => $biblionumbers );
579 $template->param( maxreserves => $maxreserves );
581 if ($multihold) {
582 $template->param( multi_hold => 1 );
585 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
586 $template->param( reserve_in_future => 1 );
589 $template->param(
590 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
591 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
594 # printout the page
595 output_html_with_http_headers $input, $cookie, $template->output;
597 sub sort_borrowerlist {
598 my $borrowerslist = shift;
599 my $ref = [];
600 push @{$ref}, sort {
601 uc( $a->{surname} . $a->{firstname} ) cmp
602 uc( $b->{surname} . $b->{firstname} )
603 } @{$borrowerslist};
604 return $ref;