Bug 8007: Compatibility with bug 11944
[koha.git] / opac / opac-reserve.pl
blob5ab4c34470b7174d9ece8eb98ca4b6a472a8ec7d
1 #!/usr/bin/perl
3 # Copyright Katipo Communications 2002
4 # Copyright Koha Development team 2012
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 use warnings;
23 use CGI qw ( -utf8 );
24 use C4::Auth; # checkauth, getborrowernumber.
25 use C4::Koha;
26 use C4::Circulation;
27 use C4::Reserves;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Output;
31 use C4::Dates qw/format_date/;
32 use C4::Context;
33 use C4::Members;
34 use C4::Branch; # GetBranches
35 use C4::Overdues;
36 use C4::Debug;
37 use Koha::DateUtils;
38 use Koha::Borrower::Debarments qw(IsDebarred);
39 use Date::Calc qw/Today Date_to_Days/;
40 # use Data::Dumper;
42 my $maxreserves = C4::Context->preference("maxreserves");
44 my $query = new CGI;
46 # if RequestOnOpac (for placing holds) is disabled, leave immediately
47 if ( ! C4::Context->preference('RequestOnOpac') ) {
48 print $query->redirect("/cgi-bin/koha/errors/404.pl");
49 exit;
52 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
54 template_name => "opac-reserve.tt",
55 query => $query,
56 type => "opac",
57 authnotrequired => 0,
58 flagsrequired => { borrow => 1 },
59 debug => 1,
63 my ($show_holds_count, $show_priority);
64 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
65 m/holds/o and $show_holds_count = 1;
66 m/priority/ and $show_priority = 1;
69 sub get_out {
70 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
71 exit;
74 # get borrower information ....
75 my ( $borr ) = GetMemberDetails( $borrowernumber );
77 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
78 if ( $borr->{'BlockExpiredPatronOpacActions'} ) {
80 if ( $borr->{'is_expired'} ) {
82 # cannot reserve, their card has expired and the rules set mean this is not allowed
83 $template->param( message => 1, expired_patron => 1 );
84 get_out( $query, $cookie, $template->output );
88 # Pass through any reserve charge
89 if ($borr->{reservefee} > 0){
90 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
92 # get branches and itemtypes
93 my $branches = GetBranches();
94 my $itemTypes = GetItemTypes();
96 # There are two ways of calling this script, with a single biblio num
97 # or multiple biblio nums.
98 my $biblionumbers = $query->param('biblionumbers');
99 my $reserveMode = $query->param('reserve_mode');
100 if ($reserveMode && ($reserveMode eq 'single')) {
101 my $bib = $query->param('single_bib');
102 $biblionumbers = "$bib/";
104 if (! $biblionumbers) {
105 $biblionumbers = $query->param('biblionumber');
108 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
109 $template->param(message=>1, no_biblionumber=>1);
110 &get_out($query, $cookie, $template->output);
113 # Pass the numbers to the page so they can be fed back
114 # when the hold is confirmed. TODO: Not necessary?
115 $template->param( biblionumbers => $biblionumbers );
117 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
118 my @biblionumbers = split /\//, $biblionumbers;
119 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
120 # TODO: New message?
121 $template->param(message=>1, no_biblionumber=>1);
122 &get_out($query, $cookie, $template->output);
126 # pass the pickup branch along....
127 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
128 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
129 $template->param( branch => $branch );
131 # make branch selection options...
132 my $branchloop = GetBranchesLoop($branch);
134 # Is the person allowed to choose their branch
135 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
137 $template->param( choose_branch => $OPACChooseBranch);
141 # Build hashes of the requested biblio(item)s and items.
145 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
146 my %itemInfoHash; # Hash of itemnumber to item info.
147 foreach my $biblioNumber (@biblionumbers) {
149 my $biblioData = GetBiblioData($biblioNumber);
150 $biblioDataHash{$biblioNumber} = $biblioData;
152 my @itemInfos = GetItemsInfo($biblioNumber);
154 my $marcrecord= GetMarcBiblio($biblioNumber);
156 # flag indicating existence of at least one item linked via a host record
157 my $hostitemsflag;
158 # adding items linked via host biblios
159 my @hostitemInfos = GetHostItemsInfo($marcrecord);
160 if (@hostitemInfos){
161 $hostitemsflag =1;
162 push (@itemInfos,@hostitemInfos);
165 $biblioData->{itemInfos} = \@itemInfos;
166 foreach my $itemInfo (@itemInfos) {
167 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
170 # Compute the priority rank.
171 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
172 my $rank = scalar( @$reserves );
173 $biblioData->{reservecount} = 1; # new reserve
174 foreach my $res (@{$reserves}) {
175 my $found = $res->{found};
176 if ( $found && $found eq 'W' ) {
177 $rank--;
179 else {
180 $biblioData->{reservecount}++;
183 $biblioData->{rank} = $rank + 1;
188 # If this is the second time through this script, it
189 # means we are carrying out the hold request, possibly
190 # with a specific item for each biblionumber.
193 if ( $query->param('place_reserve') ) {
194 my $reserve_cnt = 0;
195 if ($maxreserves) {
196 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
199 # List is composed of alternating biblio/item/branch
200 my $selectedItems = $query->param('selecteditems');
202 if ($query->param('reserve_mode') eq 'single') {
203 # This indicates non-JavaScript mode, so there was
204 # only a single biblio number selected.
205 my $bib = $query->param('single_bib');
206 my $item = $query->param("checkitem_$bib");
207 if ($item eq 'any') {
208 $item = '';
210 my $branch = $query->param('branch');
211 $selectedItems = "$bib/$item/$branch/";
214 $selectedItems =~ s!/$!!;
215 my @selectedItems = split /\//, $selectedItems, -1;
217 # Make sure there is a biblionum/itemnum/branch triplet for each item.
218 # The itemnum can be 'any', meaning next available.
219 my $selectionCount = @selectedItems;
220 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
221 $template->param(message=>1, bad_data=>1);
222 &get_out($query, $cookie, $template->output);
225 while (@selectedItems) {
226 my $biblioNum = shift(@selectedItems);
227 my $itemNum = shift(@selectedItems);
228 my $branch = shift(@selectedItems); # i.e., branch code, not name
230 my $canreserve = 0;
232 my $singleBranchMode = C4::Context->preference("singleBranchMode");
233 if ( $singleBranchMode || !$OPACChooseBranch )
234 { # single branch mode or disabled user choosing
235 $branch = $borr->{'branchcode'};
238 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
239 if ( $itemNum ne '' ) {
240 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
241 if ( $hostbiblioNum ne $biblioNum ) {
242 $biblioNum = $hostbiblioNum;
246 my $biblioData = $biblioDataHash{$biblioNum};
247 my $found;
249 # Check for user supplied reserve date
250 my $startdate;
251 if ( C4::Context->preference('AllowHoldDateInFuture')
252 && C4::Context->preference('OPACAllowHoldDateInFuture') )
254 $startdate = $query->param("reserve_date_$biblioNum");
257 my $expiration_date = $query->param("expiration_date_$biblioNum");
259 # If a specific item was selected and the pickup branch is the same as the
260 # holdingbranch, force the value $rank and $found.
261 my $rank = $biblioData->{rank};
262 if ( $itemNum ne '' ) {
263 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
264 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
265 my $item = GetItem($itemNum);
266 if ( $item->{'holdingbranch'} eq $branch ) {
267 $found = 'W'
268 unless C4::Context->preference('ReservesNeedReturns');
271 else {
272 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
274 # Inserts a null into the 'itemnumber' field of 'reserves' table.
275 $itemNum = undef;
277 my $notes = $query->param('notes_'.$biblioNum)||'';
279 if ( $maxreserves
280 && $reserve_cnt >= $maxreserves )
282 $canreserve = 0;
285 # Here we actually do the reserveration. Stage 3.
286 if ($canreserve) {
287 AddReserve(
288 $branch, $borrowernumber,
289 $biblioNum, 'a',
290 [$biblioNum], $rank,
291 $startdate, $expiration_date,
292 $notes, $biblioData->{title},
293 $itemNum, $found
295 ++$reserve_cnt;
299 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
300 exit;
305 # Here we check that the borrower can actually make reserves Stage 1.
308 my $noreserves = 0;
309 my $maxoutstanding = C4::Context->preference("maxoutstanding");
310 $template->param( noreserve => 1 ) unless $maxoutstanding;
311 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
312 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
313 $template->param( message => 1 );
314 $noreserves = 1;
315 $template->param( too_much_oweing => $amount );
317 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
318 $noreserves = 1;
319 $template->param(
320 message => 1,
321 GNA => 1
324 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
325 $noreserves = 1;
326 $template->param(
327 message => 1,
328 lost => 1
331 if ( IsDebarred($borrowernumber) ) {
332 $noreserves = 1;
333 $template->param(
334 message => 1,
335 debarred => 1
339 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
340 my $reserves_count = scalar(@reserves);
341 $template->param( RESERVES => \@reserves );
342 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
343 $template->param( message => 1 );
344 $noreserves = 1;
345 $template->param( too_many_reserves => scalar(@reserves));
348 unless ( $noreserves ) {
349 my $requested_reserves_count = scalar( @biblionumbers );
350 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
351 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
355 foreach my $res (@reserves) {
356 foreach my $biblionumber (@biblionumbers) {
357 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
358 # $template->param( message => 1 );
359 # $noreserves = 1;
360 # $template->param( already_reserved => 1 );
361 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
366 unless ($noreserves) {
367 $template->param( select_item_types => 1 );
373 # Build the template parameters that will show the info
374 # and items for each biblionumber.
377 my $notforloan_label_of = get_notforloan_label_of();
379 my $biblioLoop = [];
380 my $numBibsAvailable = 0;
381 my $itemdata_enumchron = 0;
382 my $anyholdable = 0;
383 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
384 $template->param('item_level_itypes' => $itemLevelTypes);
386 foreach my $biblioNum (@biblionumbers) {
388 my $record = GetMarcBiblio($biblioNum);
389 # Init the bib item with the choices for branch pickup
390 my %biblioLoopIter = ( branchloop => $branchloop );
392 # Get relevant biblio data.
393 my $biblioData = $biblioDataHash{$biblioNum};
394 if (! $biblioData) {
395 $template->param(message=>1, bad_biblionumber=>$biblioNum);
396 &get_out($query, $cookie, $template->output);
399 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
400 $biblioLoopIter{title} = $biblioData->{title};
401 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
402 $biblioLoopIter{author} = $biblioData->{author};
403 $biblioLoopIter{rank} = $biblioData->{rank};
404 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
405 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
406 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
408 if (!$itemLevelTypes && $biblioData->{itemtype}) {
409 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
410 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
413 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
414 $debug and warn $itemInfo->{'notforloan'};
416 # Get reserve fee.
417 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
418 ( $itemInfo->{'biblioitemnumber'} ) );
419 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
421 if ($itemLevelTypes && $itemInfo->{itype}) {
422 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
423 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
426 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
427 $biblioLoopIter{forloan} = 1;
431 $biblioLoopIter{itemLoop} = [];
432 my $numCopiesAvailable = 0;
433 my $numCopiesOPACAvailable = 0;
434 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
435 my $itemNum = $itemInfo->{itemnumber};
436 my $itemLoopIter = {};
438 $itemLoopIter->{itemnumber} = $itemNum;
439 $itemLoopIter->{barcode} = $itemInfo->{barcode};
440 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
441 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
442 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
443 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
444 if ($itemLevelTypes) {
445 $itemLoopIter->{description} = $itemInfo->{description};
446 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
449 # If the holdingbranch is different than the homebranch, we show the
450 # holdingbranch of the document too.
451 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
452 $itemLoopIter->{holdingBranchName} =
453 $branches->{ $itemInfo->{holdingbranch} }{branchname};
456 # If the item is currently on loan, we display its return date and
457 # change the background color.
458 my $issues= GetItemIssue($itemNum);
459 if ( $issues->{'date_due'} ) {
460 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
461 $itemLoopIter->{backgroundcolor} = 'onloan';
464 # checking reserve
465 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
466 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
468 # the item could be reserved for this borrower vi a host record, flag this
469 if ($reservedfor eq $borrowernumber){
470 $itemLoopIter->{already_reserved} = 1;
473 if ( defined $reservedate ) {
474 $itemLoopIter->{backgroundcolor} = 'reserved';
475 $itemLoopIter->{reservedate} = format_date($reservedate);
476 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
477 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
478 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
479 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
480 #waiting status
481 $itemLoopIter->{waitingdate} = $wait;
484 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
485 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
487 # Management of the notforloan document
488 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
489 $itemLoopIter->{backgroundcolor} = 'other';
490 $itemLoopIter->{notforloanvalue} =
491 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
494 # Management of lost or long overdue items
495 if ( $itemInfo->{itemlost} ) {
497 # FIXME localized strings should never be in Perl code
498 $itemLoopIter->{message} =
499 $itemInfo->{itemlost} == 1 ? "(lost)"
500 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
501 : "";
502 $itemInfo->{backgroundcolor} = 'other';
505 # Check of the transfered documents
506 my ( $transfertwhen, $transfertfrom, $transfertto ) =
507 GetTransfers($itemNum);
508 if ( $transfertwhen && ($transfertwhen ne '') ) {
509 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
510 $itemLoopIter->{transfertfrom} =
511 $branches->{$transfertfrom}{branchname};
512 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
513 $itemLoopIter->{nocancel} = 1;
516 # if the items belongs to a host record, show link to host record
517 if ($itemInfo->{biblionumber} ne $biblioNum){
518 $biblioLoopIter{hostitemsflag} = 1;
519 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
520 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
523 # If there is no loan, return and transfer, we show a checkbox.
524 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
526 my $branch = GetReservesControlBranch( $itemInfo, $borr );
528 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
529 if ($policy_holdallowed) {
530 if (my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} )) {
531 $policy_holdallowed =
532 ($branchitemrule->{'holdallowed'} == 2) ||
533 ($branchitemrule->{'holdallowed'} == 1
534 && $borr->{'branchcode'} eq $itemInfo->{'homebranch'});
535 } else {
536 $policy_holdallowed = 0; # No rule - not allowed
539 $policy_holdallowed &&=
540 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
541 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
543 if ($policy_holdallowed) {
544 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
545 $itemLoopIter->{available} = 1;
546 $numCopiesOPACAvailable++;
547 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
549 $numCopiesAvailable++;
552 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
554 # Show serial enumeration when needed
555 if ($itemLoopIter->{enumchron}) {
556 $itemdata_enumchron = 1;
559 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
561 $template->param( itemdata_enumchron => $itemdata_enumchron );
563 if ($numCopiesAvailable > 0) {
564 $numBibsAvailable++;
565 $biblioLoopIter{bib_available} = 1;
566 $biblioLoopIter{holdable} = 1;
567 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
569 if ($biblioLoopIter{already_reserved}) {
570 $biblioLoopIter{holdable} = undef;
571 $biblioLoopIter{itemholdable} = undef;
573 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
574 $biblioLoopIter{holdable} = undef;
575 $biblioLoopIter{already_patron_possession} = 1;
578 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
580 push @$biblioLoop, \%biblioLoopIter;
582 $anyholdable = 1 if $biblioLoopIter{holdable};
585 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
586 $template->param( none_available => 1 );
589 my $show_notes=C4::Context->preference('OpacHoldNotes');
590 $template->param(OpacHoldNotes=>$show_notes);
592 # display infos
593 $template->param(bibitemloop => $biblioLoop);
594 # can set reserve date in future
595 if (
596 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
597 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
599 $template->param(
600 reserve_in_future => 1,
604 output_html_with_http_headers $query, $cookie, $template->output;