Bug 7180: (follow-up) ensure that the default quantity is 1
[koha.git] / reserve / request.pl
blob1b303c7a763ecd614632f82943dd1abaa1a2a4f2
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 $reserve{'atdestination'} = 1;
481 # set found to 1 if reserve is waiting for patron pickup
482 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
483 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
484 } elsif ($res->{priority} > 0) {
485 if (defined($res->{itemnumber})) {
486 my $item = GetItem($res->{itemnumber});
487 $reserve{'itemnumber'} = $res->{'itemnumber'};
488 $reserve{'barcodenumber'} = $item->{'barcode'};
489 $reserve{'item_level_hold'} = 1;
493 # get borrowers reserve info
494 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
495 if (C4::Context->preference('HidePatronName')){
496 $reserve{'hidename'} = 1;
497 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
499 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
500 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
501 $reserve{'date'} = format_date( $res->{'reservedate'} );
502 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
503 $reserve{'biblionumber'} = $res->{'biblionumber'};
504 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
505 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
506 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
507 $reserve{'notes'} = $res->{'reservenotes'};
508 $reserve{'wait'} =
509 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
510 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
511 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
512 $reserve{'voldesc'} = $res->{'volumeddesc'};
513 $reserve{'ccode'} = $res->{'ccode'};
514 $reserve{'barcode'} = $res->{'barcode'};
515 $reserve{'priority'} = $res->{'priority'};
516 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
517 $reserve{'optionloop'} = \@optionloop;
518 $reserve{'suspend'} = $res->{'suspend'};
519 $reserve{'suspend_until'} = $res->{'suspend_until'};
520 $reserve{'reserve_id'} = $res->{'reserve_id'};
522 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
523 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
524 } else {
525 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
528 push( @reserveloop, \%reserve );
531 # get the time for the form name...
532 my $time = time();
534 $template->param(
535 branchloop => GetBranchesLoop($userbranch),
536 time => $time,
537 fixedRank => $fixedRank,
540 # display infos
541 $template->param(
542 optionloop => \@optionloop,
543 bibitemloop => \@bibitemloop,
544 itemdata_enumchron => $itemdata_enumchron,
545 date => $date,
546 biblionumber => $biblionumber,
547 findborrower => $findborrower,
548 title => $dat->{title},
549 author => $dat->{author},
550 holdsview => 1,
551 C4::Search::enabled_staff_search_views,
553 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
554 $template->param(
555 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
556 borrower_branchcode => $borrowerinfo->{'branchcode'},
560 $biblioloopiter{biblionumber} = $biblionumber;
561 $biblioloopiter{title} = $dat->{title};
562 $biblioloopiter{rank} = $fixedRank;
563 $biblioloopiter{reserveloop} = \@reserveloop;
565 if (@reserveloop) {
566 $template->param( reserveloop => \@reserveloop );
569 push @biblioloop, \%biblioloopiter;
572 $template->param( biblioloop => \@biblioloop );
573 $template->param( biblionumbers => $biblionumbers );
574 $template->param( maxreserves => $maxreserves );
576 if ($multihold) {
577 $template->param( multi_hold => 1 );
580 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
581 $template->param( reserve_in_future => 1 );
584 $template->param(
585 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
586 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
589 # printout the page
590 output_html_with_http_headers $input, $cookie, $template->output;
592 sub sort_borrowerlist {
593 my $borrowerslist = shift;
594 my $ref = [];
595 push @{$ref}, sort {
596 uc( $a->{surname} . $a->{firstname} ) cmp
597 uc( $b->{surname} . $b->{firstname} )
598 } @{$borrowerslist};
599 return $ref;