Bug 14927: Returns item info when resumptionToken is used
[koha.git] / opac / opac-reserve.pl
blob4dff9a2d23ecbe66d76fdc492bbb300dd584b1ea
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 debug => 1,
62 my ($show_holds_count, $show_priority);
63 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
64 m/holds/o and $show_holds_count = 1;
65 m/priority/ and $show_priority = 1;
68 sub get_out {
69 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
70 exit;
73 # get borrower information ....
74 my ( $borr ) = GetMemberDetails( $borrowernumber );
76 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
77 if ( $borr->{'BlockExpiredPatronOpacActions'} ) {
79 if ( $borr->{'is_expired'} ) {
81 # cannot reserve, their card has expired and the rules set mean this is not allowed
82 $template->param( message => 1, expired_patron => 1 );
83 get_out( $query, $cookie, $template->output );
87 # Pass through any reserve charge
88 if ($borr->{reservefee} > 0){
89 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
91 # get branches and itemtypes
92 my $branches = GetBranches();
93 my $itemTypes = GetItemTypes();
95 # There are two ways of calling this script, with a single biblio num
96 # or multiple biblio nums.
97 my $biblionumbers = $query->param('biblionumbers');
98 my $reserveMode = $query->param('reserve_mode');
99 if ($reserveMode && ($reserveMode eq 'single')) {
100 my $bib = $query->param('single_bib');
101 $biblionumbers = "$bib/";
103 if (! $biblionumbers) {
104 $biblionumbers = $query->param('biblionumber');
107 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
108 $template->param(message=>1, no_biblionumber=>1);
109 &get_out($query, $cookie, $template->output);
112 # Pass the numbers to the page so they can be fed back
113 # when the hold is confirmed. TODO: Not necessary?
114 $template->param( biblionumbers => $biblionumbers );
116 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
117 my @biblionumbers = split /\//, $biblionumbers;
118 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
119 # TODO: New message?
120 $template->param(message=>1, no_biblionumber=>1);
121 &get_out($query, $cookie, $template->output);
125 # pass the pickup branch along....
126 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
127 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
128 $template->param( branch => $branch );
130 # make branch selection options...
131 my $branchloop = GetBranchesLoop($branch);
133 # Is the person allowed to choose their branch
134 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
136 $template->param( choose_branch => $OPACChooseBranch);
140 # Build hashes of the requested biblio(item)s and items.
144 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
145 my %itemInfoHash; # Hash of itemnumber to item info.
146 foreach my $biblioNumber (@biblionumbers) {
148 my $biblioData = GetBiblioData($biblioNumber);
149 $biblioDataHash{$biblioNumber} = $biblioData;
151 my @itemInfos = GetItemsInfo($biblioNumber);
153 my $marcrecord= GetMarcBiblio($biblioNumber);
155 # flag indicating existence of at least one item linked via a host record
156 my $hostitemsflag;
157 # adding items linked via host biblios
158 my @hostitemInfos = GetHostItemsInfo($marcrecord);
159 if (@hostitemInfos){
160 $hostitemsflag =1;
161 push (@itemInfos,@hostitemInfos);
164 $biblioData->{itemInfos} = \@itemInfos;
165 foreach my $itemInfo (@itemInfos) {
166 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
169 # Compute the priority rank.
170 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
171 my $rank = scalar( @$reserves );
172 $biblioData->{reservecount} = 1; # new reserve
173 foreach my $res (@{$reserves}) {
174 my $found = $res->{found};
175 if ( $found && $found eq 'W' ) {
176 $rank--;
178 else {
179 $biblioData->{reservecount}++;
182 $biblioData->{rank} = $rank + 1;
187 # If this is the second time through this script, it
188 # means we are carrying out the hold request, possibly
189 # with a specific item for each biblionumber.
192 if ( $query->param('place_reserve') ) {
193 my $reserve_cnt = 0;
194 if ($maxreserves) {
195 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
198 # List is composed of alternating biblio/item/branch
199 my $selectedItems = $query->param('selecteditems');
201 if ($query->param('reserve_mode') eq 'single') {
202 # This indicates non-JavaScript mode, so there was
203 # only a single biblio number selected.
204 my $bib = $query->param('single_bib');
205 my $item = $query->param("checkitem_$bib");
206 if ($item eq 'any') {
207 $item = '';
209 my $branch = $query->param('branch');
210 $selectedItems = "$bib/$item/$branch/";
213 $selectedItems =~ s!/$!!;
214 my @selectedItems = split /\//, $selectedItems, -1;
216 # Make sure there is a biblionum/itemnum/branch triplet for each item.
217 # The itemnum can be 'any', meaning next available.
218 my $selectionCount = @selectedItems;
219 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
220 $template->param(message=>1, bad_data=>1);
221 &get_out($query, $cookie, $template->output);
224 while (@selectedItems) {
225 my $biblioNum = shift(@selectedItems);
226 my $itemNum = shift(@selectedItems);
227 my $branch = shift(@selectedItems); # i.e., branch code, not name
229 my $canreserve = 0;
231 my $singleBranchMode = C4::Context->preference("singleBranchMode");
232 if ( $singleBranchMode || !$OPACChooseBranch )
233 { # single branch mode or disabled user choosing
234 $branch = $borr->{'branchcode'};
237 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
238 if ( $itemNum ne '' ) {
239 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
240 if ( $hostbiblioNum ne $biblioNum ) {
241 $biblioNum = $hostbiblioNum;
245 my $biblioData = $biblioDataHash{$biblioNum};
246 my $found;
248 # Check for user supplied reserve date
249 my $startdate;
250 if ( C4::Context->preference('AllowHoldDateInFuture')
251 && C4::Context->preference('OPACAllowHoldDateInFuture') )
253 $startdate = $query->param("reserve_date_$biblioNum");
256 my $expiration_date = $query->param("expiration_date_$biblioNum");
258 # If a specific item was selected and the pickup branch is the same as the
259 # holdingbranch, force the value $rank and $found.
260 my $rank = $biblioData->{rank};
261 if ( $itemNum ne '' ) {
262 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
263 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
264 my $item = GetItem($itemNum);
265 if ( $item->{'holdingbranch'} eq $branch ) {
266 $found = 'W'
267 unless C4::Context->preference('ReservesNeedReturns');
270 else {
271 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
273 # Inserts a null into the 'itemnumber' field of 'reserves' table.
274 $itemNum = undef;
276 my $notes = $query->param('notes_'.$biblioNum)||'';
278 if ( $maxreserves
279 && $reserve_cnt >= $maxreserves )
281 $canreserve = 0;
284 # Here we actually do the reserveration. Stage 3.
285 if ($canreserve) {
286 AddReserve(
287 $branch, $borrowernumber,
288 $biblioNum,
289 [$biblioNum], $rank,
290 $startdate, $expiration_date,
291 $notes, $biblioData->{title},
292 $itemNum, $found
294 ++$reserve_cnt;
298 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
299 exit;
304 # Here we check that the borrower can actually make reserves Stage 1.
307 my $noreserves = 0;
308 my $maxoutstanding = C4::Context->preference("maxoutstanding");
309 $template->param( noreserve => 1 ) unless $maxoutstanding;
310 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
311 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
312 $template->param( message => 1 );
313 $noreserves = 1;
314 $template->param( too_much_oweing => $amount );
316 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
317 $noreserves = 1;
318 $template->param(
319 message => 1,
320 GNA => 1
323 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
324 $noreserves = 1;
325 $template->param(
326 message => 1,
327 lost => 1
330 if ( IsDebarred($borrowernumber) ) {
331 $noreserves = 1;
332 $template->param(
333 message => 1,
334 debarred => 1
338 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
339 my $reserves_count = scalar(@reserves);
340 $template->param( RESERVES => \@reserves );
341 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
342 $template->param( message => 1 );
343 $noreserves = 1;
344 $template->param( too_many_reserves => scalar(@reserves));
347 unless ( $noreserves ) {
348 my $requested_reserves_count = scalar( @biblionumbers );
349 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
350 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
354 foreach my $res (@reserves) {
355 foreach my $biblionumber (@biblionumbers) {
356 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
357 # $template->param( message => 1 );
358 # $noreserves = 1;
359 # $template->param( already_reserved => 1 );
360 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
365 unless ($noreserves) {
366 $template->param( select_item_types => 1 );
372 # Build the template parameters that will show the info
373 # and items for each biblionumber.
376 my $notforloan_label_of = get_notforloan_label_of();
378 my $biblioLoop = [];
379 my $numBibsAvailable = 0;
380 my $itemdata_enumchron = 0;
381 my $anyholdable = 0;
382 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
383 $template->param('item_level_itypes' => $itemLevelTypes);
385 foreach my $biblioNum (@biblionumbers) {
387 my $record = GetMarcBiblio($biblioNum);
388 # Init the bib item with the choices for branch pickup
389 my %biblioLoopIter = ( branchloop => $branchloop );
391 # Get relevant biblio data.
392 my $biblioData = $biblioDataHash{$biblioNum};
393 if (! $biblioData) {
394 $template->param(message=>1, bad_biblionumber=>$biblioNum);
395 &get_out($query, $cookie, $template->output);
398 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
399 $biblioLoopIter{title} = $biblioData->{title};
400 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
401 $biblioLoopIter{author} = $biblioData->{author};
402 $biblioLoopIter{rank} = $biblioData->{rank};
403 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
404 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
405 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
407 if (!$itemLevelTypes && $biblioData->{itemtype}) {
408 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
409 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
412 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
413 if ($itemLevelTypes && $itemInfo->{itype}) {
414 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
415 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
418 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
419 $biblioLoopIter{forloan} = 1;
423 $biblioLoopIter{itemLoop} = [];
424 my $numCopiesAvailable = 0;
425 my $numCopiesOPACAvailable = 0;
426 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
427 my $itemNum = $itemInfo->{itemnumber};
428 my $itemLoopIter = {};
430 $itemLoopIter->{itemnumber} = $itemNum;
431 $itemLoopIter->{barcode} = $itemInfo->{barcode};
432 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
433 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
434 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
435 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
436 if ($itemLevelTypes) {
437 $itemLoopIter->{description} = $itemInfo->{description};
438 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
441 # If the holdingbranch is different than the homebranch, we show the
442 # holdingbranch of the document too.
443 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
444 $itemLoopIter->{holdingBranchName} =
445 $branches->{ $itemInfo->{holdingbranch} }{branchname};
448 # If the item is currently on loan, we display its return date and
449 # change the background color.
450 my $issues= GetItemIssue($itemNum);
451 if ( $issues->{'date_due'} ) {
452 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
453 $itemLoopIter->{backgroundcolor} = 'onloan';
456 # checking reserve
457 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
458 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
460 # the item could be reserved for this borrower vi a host record, flag this
461 $reservedfor //= '';
462 if ($reservedfor eq $borrowernumber){
463 $itemLoopIter->{already_reserved} = 1;
466 if ( defined $reservedate ) {
467 $itemLoopIter->{backgroundcolor} = 'reserved';
468 $itemLoopIter->{reservedate} = format_date($reservedate);
469 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
470 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
471 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
472 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
473 #waiting status
474 $itemLoopIter->{waitingdate} = $wait;
477 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
478 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
480 # Management of the notforloan document
481 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
482 $itemLoopIter->{backgroundcolor} = 'other';
483 $itemLoopIter->{notforloanvalue} =
484 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
487 # Management of lost or long overdue items
488 if ( $itemInfo->{itemlost} ) {
490 # FIXME localized strings should never be in Perl code
491 $itemLoopIter->{message} =
492 $itemInfo->{itemlost} == 1 ? "(lost)"
493 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
494 : "";
495 $itemInfo->{backgroundcolor} = 'other';
498 # Check of the transfered documents
499 my ( $transfertwhen, $transfertfrom, $transfertto ) =
500 GetTransfers($itemNum);
501 if ( $transfertwhen && ($transfertwhen ne '') ) {
502 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
503 $itemLoopIter->{transfertfrom} =
504 $branches->{$transfertfrom}{branchname};
505 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
506 $itemLoopIter->{nocancel} = 1;
509 # if the items belongs to a host record, show link to host record
510 if ($itemInfo->{biblionumber} ne $biblioNum){
511 $biblioLoopIter{hostitemsflag} = 1;
512 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
513 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
516 # If there is no loan, return and transfer, we show a checkbox.
517 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
519 my $branch = GetReservesControlBranch( $itemInfo, $borr );
521 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
522 $policy_holdallowed &&=
523 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
524 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
526 if ($policy_holdallowed) {
527 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
528 $itemLoopIter->{available} = 1;
529 $numCopiesOPACAvailable++;
530 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
532 $numCopiesAvailable++;
535 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
537 # Show serial enumeration when needed
538 if ($itemLoopIter->{enumchron}) {
539 $itemdata_enumchron = 1;
542 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
544 $template->param( itemdata_enumchron => $itemdata_enumchron );
546 if ($numCopiesAvailable > 0) {
547 $numBibsAvailable++;
548 $biblioLoopIter{bib_available} = 1;
549 $biblioLoopIter{holdable} = 1;
550 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
552 if ($biblioLoopIter{already_reserved}) {
553 $biblioLoopIter{holdable} = undef;
554 $biblioLoopIter{itemholdable} = undef;
556 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
557 $biblioLoopIter{holdable} = undef;
558 $biblioLoopIter{already_patron_possession} = 1;
561 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
563 push @$biblioLoop, \%biblioLoopIter;
565 $anyholdable = 1 if $biblioLoopIter{holdable};
568 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
569 $template->param( none_available => 1 );
572 my $show_notes=C4::Context->preference('OpacHoldNotes');
573 $template->param(OpacHoldNotes=>$show_notes);
575 # display infos
576 $template->param(bibitemloop => $biblioLoop);
577 # can set reserve date in future
578 if (
579 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
580 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
582 $template->param(
583 reserve_in_future => 1,
587 output_html_with_http_headers $query, $cookie, $template->output;