Bug 13578: Make sure the 'public' parameter is passed to the last step
[koha.git] / opac / opac-reserve.pl
blobb7143687e175504bf02d8984339079a92d79cea5
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
45 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
47 template_name => "opac-reserve.tt",
48 query => $query,
49 type => "opac",
50 authnotrequired => 0,
51 flagsrequired => { borrow => 1 },
52 debug => 1,
56 my ($show_holds_count, $show_priority);
57 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
58 m/holds/o and $show_holds_count = 1;
59 m/priority/ and $show_priority = 1;
62 sub get_out {
63 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
64 exit;
67 # get borrower information ....
68 my ( $borr ) = GetMemberDetails( $borrowernumber );
70 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
71 if ( $borr->{'BlockExpiredPatronOpacActions'} ) {
73 if ( $borr->{'is_expired'} ) {
75 # cannot reserve, their card has expired and the rules set mean this is not allowed
76 $template->param( message => 1, expired_patron => 1 );
77 get_out( $query, $cookie, $template->output );
81 # Pass through any reserve charge
82 if ($borr->{reservefee} > 0){
83 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
85 # get branches and itemtypes
86 my $branches = GetBranches();
87 my $itemTypes = GetItemTypes();
89 # There are two ways of calling this script, with a single biblio num
90 # or multiple biblio nums.
91 my $biblionumbers = $query->param('biblionumbers');
92 my $reserveMode = $query->param('reserve_mode');
93 if ($reserveMode && ($reserveMode eq 'single')) {
94 my $bib = $query->param('single_bib');
95 $biblionumbers = "$bib/";
97 if (! $biblionumbers) {
98 $biblionumbers = $query->param('biblionumber');
101 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
102 $template->param(message=>1, no_biblionumber=>1);
103 &get_out($query, $cookie, $template->output);
106 # Pass the numbers to the page so they can be fed back
107 # when the hold is confirmed. TODO: Not necessary?
108 $template->param( biblionumbers => $biblionumbers );
110 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
111 my @biblionumbers = split /\//, $biblionumbers;
112 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
113 # TODO: New message?
114 $template->param(message=>1, no_biblionumber=>1);
115 &get_out($query, $cookie, $template->output);
119 # pass the pickup branch along....
120 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
121 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
122 $template->param( branch => $branch );
124 # make branch selection options...
125 my $branchloop = GetBranchesLoop($branch);
127 # Is the person allowed to choose their branch
128 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
130 $template->param( choose_branch => $OPACChooseBranch);
134 # Build hashes of the requested biblio(item)s and items.
138 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
139 my %itemInfoHash; # Hash of itemnumber to item info.
140 foreach my $biblioNumber (@biblionumbers) {
142 my $biblioData = GetBiblioData($biblioNumber);
143 $biblioDataHash{$biblioNumber} = $biblioData;
145 my @itemInfos = GetItemsInfo($biblioNumber);
147 my $marcrecord= GetMarcBiblio($biblioNumber);
149 # flag indicating existence of at least one item linked via a host record
150 my $hostitemsflag;
151 # adding items linked via host biblios
152 my @hostitemInfos = GetHostItemsInfo($marcrecord);
153 if (@hostitemInfos){
154 $hostitemsflag =1;
155 push (@itemInfos,@hostitemInfos);
158 $biblioData->{itemInfos} = \@itemInfos;
159 foreach my $itemInfo (@itemInfos) {
160 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
163 # Compute the priority rank.
164 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
165 my $rank = scalar( @$reserves );
166 $biblioData->{reservecount} = 1; # new reserve
167 foreach my $res (@{$reserves}) {
168 my $found = $res->{found};
169 if ( $found && $found eq 'W' ) {
170 $rank--;
172 else {
173 $biblioData->{reservecount}++;
176 $biblioData->{rank} = $rank + 1;
181 # If this is the second time through this script, it
182 # means we are carrying out the hold request, possibly
183 # with a specific item for each biblionumber.
186 if ( $query->param('place_reserve') ) {
187 my $reserve_cnt = 0;
188 if ($maxreserves) {
189 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
192 # List is composed of alternating biblio/item/branch
193 my $selectedItems = $query->param('selecteditems');
195 if ($query->param('reserve_mode') eq 'single') {
196 # This indicates non-JavaScript mode, so there was
197 # only a single biblio number selected.
198 my $bib = $query->param('single_bib');
199 my $item = $query->param("checkitem_$bib");
200 if ($item eq 'any') {
201 $item = '';
203 my $branch = $query->param('branch');
204 $selectedItems = "$bib/$item/$branch/";
207 $selectedItems =~ s!/$!!;
208 my @selectedItems = split /\//, $selectedItems, -1;
210 # Make sure there is a biblionum/itemnum/branch triplet for each item.
211 # The itemnum can be 'any', meaning next available.
212 my $selectionCount = @selectedItems;
213 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
214 $template->param(message=>1, bad_data=>1);
215 &get_out($query, $cookie, $template->output);
218 while (@selectedItems) {
219 my $biblioNum = shift(@selectedItems);
220 my $itemNum = shift(@selectedItems);
221 my $branch = shift(@selectedItems); # i.e., branch code, not name
223 my $canreserve = 0;
225 my $singleBranchMode = C4::Context->preference("singleBranchMode");
226 if ( $singleBranchMode || !$OPACChooseBranch )
227 { # single branch mode or disabled user choosing
228 $branch = $borr->{'branchcode'};
231 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
232 if ( $itemNum ne '' ) {
233 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
234 if ( $hostbiblioNum ne $biblioNum ) {
235 $biblioNum = $hostbiblioNum;
239 my $biblioData = $biblioDataHash{$biblioNum};
240 my $found;
242 # Check for user supplied reserve date
243 my $startdate;
244 if ( C4::Context->preference('AllowHoldDateInFuture')
245 && C4::Context->preference('OPACAllowHoldDateInFuture') )
247 $startdate = $query->param("reserve_date_$biblioNum");
250 my $expiration_date = $query->param("expiration_date_$biblioNum");
252 # If a specific item was selected and the pickup branch is the same as the
253 # holdingbranch, force the value $rank and $found.
254 my $rank = $biblioData->{rank};
255 if ( $itemNum ne '' ) {
256 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
257 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
258 my $item = GetItem($itemNum);
259 if ( $item->{'holdingbranch'} eq $branch ) {
260 $found = 'W'
261 unless C4::Context->preference('ReservesNeedReturns');
264 else {
265 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
267 # Inserts a null into the 'itemnumber' field of 'reserves' table.
268 $itemNum = undef;
270 my $notes = $query->param('notes_'.$biblioNum)||'';
272 if ( $maxreserves
273 && $reserve_cnt >= $maxreserves )
275 $canreserve = 0;
278 # Here we actually do the reserveration. Stage 3.
279 if ($canreserve) {
280 AddReserve(
281 $branch, $borrowernumber,
282 $biblioNum, 'a',
283 [$biblioNum], $rank,
284 $startdate, $expiration_date,
285 $notes, $biblioData->{title},
286 $itemNum, $found
288 ++$reserve_cnt;
292 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
293 exit;
298 # Here we check that the borrower can actually make reserves Stage 1.
301 my $noreserves = 0;
302 my $maxoutstanding = C4::Context->preference("maxoutstanding");
303 $template->param( noreserve => 1 ) unless $maxoutstanding;
304 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
305 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
306 $template->param( message => 1 );
307 $noreserves = 1;
308 $template->param( too_much_oweing => $amount );
310 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
311 $noreserves = 1;
312 $template->param(
313 message => 1,
314 GNA => 1
317 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
318 $noreserves = 1;
319 $template->param(
320 message => 1,
321 lost => 1
324 if ( IsDebarred($borrowernumber) ) {
325 $noreserves = 1;
326 $template->param(
327 message => 1,
328 debarred => 1
332 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
333 my $reserves_count = scalar(@reserves);
334 $template->param( RESERVES => \@reserves );
335 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
336 $template->param( message => 1 );
337 $noreserves = 1;
338 $template->param( too_many_reserves => scalar(@reserves));
341 unless ( $noreserves ) {
342 my $requested_reserves_count = scalar( @biblionumbers );
343 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
344 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
348 foreach my $res (@reserves) {
349 foreach my $biblionumber (@biblionumbers) {
350 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
351 # $template->param( message => 1 );
352 # $noreserves = 1;
353 # $template->param( already_reserved => 1 );
354 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
359 unless ($noreserves) {
360 $template->param( select_item_types => 1 );
366 # Build the template parameters that will show the info
367 # and items for each biblionumber.
370 my $notforloan_label_of = get_notforloan_label_of();
372 my $biblioLoop = [];
373 my $numBibsAvailable = 0;
374 my $itemdata_enumchron = 0;
375 my $anyholdable = 0;
376 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
377 $template->param('item_level_itypes' => $itemLevelTypes);
379 foreach my $biblioNum (@biblionumbers) {
381 my $record = GetMarcBiblio($biblioNum);
382 # Init the bib item with the choices for branch pickup
383 my %biblioLoopIter = ( branchloop => $branchloop );
385 # Get relevant biblio data.
386 my $biblioData = $biblioDataHash{$biblioNum};
387 if (! $biblioData) {
388 $template->param(message=>1, bad_biblionumber=>$biblioNum);
389 &get_out($query, $cookie, $template->output);
392 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
393 $biblioLoopIter{title} = $biblioData->{title};
394 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
395 $biblioLoopIter{author} = $biblioData->{author};
396 $biblioLoopIter{rank} = $biblioData->{rank};
397 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
398 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
399 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
401 if (!$itemLevelTypes && $biblioData->{itemtype}) {
402 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
403 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
406 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
407 $debug and warn $itemInfo->{'notforloan'};
409 # Get reserve fee.
410 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
411 ( $itemInfo->{'biblioitemnumber'} ) );
412 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
414 if ($itemLevelTypes && $itemInfo->{itype}) {
415 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
416 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
419 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
420 $biblioLoopIter{forloan} = 1;
424 $biblioLoopIter{itemLoop} = [];
425 my $numCopiesAvailable = 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 if ($reservedfor eq $borrowernumber){
462 $itemLoopIter->{already_reserved} = 1;
465 if ( defined $reservedate ) {
466 $itemLoopIter->{backgroundcolor} = 'reserved';
467 $itemLoopIter->{reservedate} = format_date($reservedate);
468 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
469 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
470 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
471 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
472 #waiting status
473 $itemLoopIter->{waitingdate} = $wait;
476 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
477 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
479 # Management of the notforloan document
480 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
481 $itemLoopIter->{backgroundcolor} = 'other';
482 $itemLoopIter->{notforloanvalue} =
483 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
486 # Management of lost or long overdue items
487 if ( $itemInfo->{itemlost} ) {
489 # FIXME localized strings should never be in Perl code
490 $itemLoopIter->{message} =
491 $itemInfo->{itemlost} == 1 ? "(lost)"
492 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
493 : "";
494 $itemInfo->{backgroundcolor} = 'other';
497 # Check of the transfered documents
498 my ( $transfertwhen, $transfertfrom, $transfertto ) =
499 GetTransfers($itemNum);
500 if ( $transfertwhen && ($transfertwhen ne '') ) {
501 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
502 $itemLoopIter->{transfertfrom} =
503 $branches->{$transfertfrom}{branchname};
504 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
505 $itemLoopIter->{nocancel} = 1;
508 # if the items belongs to a host record, show link to host record
509 if ($itemInfo->{biblionumber} ne $biblioNum){
510 $biblioLoopIter{hostitemsflag} = 1;
511 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
512 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
515 # If there is no loan, return and transfer, we show a checkbox.
516 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
518 my $branch = GetReservesControlBranch( $itemInfo, $borr );
520 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
521 my $policy_holdallowed = 1;
523 if ( $branchitemrule->{'holdallowed'} == 0 ||
524 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
525 $policy_holdallowed = 0;
528 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum) eq 'OK' and ($itemLoopIter->{already_reserved} ne 1)) {
529 $itemLoopIter->{available} = 1;
530 $numCopiesAvailable++;
533 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
535 # Show serial enumeration when needed
536 if ($itemLoopIter->{enumchron}) {
537 $itemdata_enumchron = 1;
540 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
542 $template->param( itemdata_enumchron => $itemdata_enumchron );
544 if ($numCopiesAvailable > 0) {
545 $numBibsAvailable++;
546 $biblioLoopIter{bib_available} = 1;
547 $biblioLoopIter{holdable} = 1;
549 if ($biblioLoopIter{already_reserved}) {
550 $biblioLoopIter{holdable} = undef;
552 my $canReserve = CanBookBeReserved($borrowernumber,$biblioNum);
553 unless ($canReserve eq 'OK') {
554 $biblioLoopIter{holdable} = undef;
555 $biblioLoopIter{ $canReserve } = 1;
557 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
558 $biblioLoopIter{holdable} = undef;
559 $biblioLoopIter{already_patron_possession} = 1;
562 if( $biblioLoopIter{holdable} ){ $anyholdable++; }
564 push @$biblioLoop, \%biblioLoopIter;
567 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
568 $template->param( none_available => 1 );
571 my $show_notes=C4::Context->preference('OpacHoldNotes');
572 $template->param(OpacHoldNotes=>$show_notes);
574 # display infos
575 $template->param(bibitemloop => $biblioLoop);
576 # can set reserve date in future
577 if (
578 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
579 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
581 $template->param(
582 reserve_in_future => 1,
586 output_html_with_http_headers $query, $cookie, $template->output;