Bug 6554 Fix error caused by modifying $_ in a map
[koha.git] / opac / opac-reserve.pl
blobe725d306e7f58e8004ababd317fe389b9ce9fd0b
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;
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 Data::Dumper;
40 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
42 my $query = new CGI;
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 template_name => "opac-reserve.tmpl",
46 query => $query,
47 type => "opac",
48 authnotrequired => 0,
49 flagsrequired => { borrow => 1 },
50 debug => 1,
54 my ($show_holds_count, $show_priority);
55 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
56 m/holds/o and $show_holds_count = 1;
57 m/priority/ and $show_priority = 1;
60 sub get_out {
61 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
62 exit;
65 # get borrower information ....
66 my ( $borr ) = GetMemberDetails( $borrowernumber );
68 # Pass through any reserve charge
69 if ($borr->{reservefee} > 0){
70 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
72 # get branches and itemtypes
73 my $branches = GetBranches();
74 my $itemTypes = GetItemTypes();
76 # There are two ways of calling this script, with a single biblio num
77 # or multiple biblio nums.
78 my $biblionumbers = $query->param('biblionumbers');
79 my $reserveMode = $query->param('reserve_mode');
80 if ($reserveMode && ($reserveMode eq 'single')) {
81 my $bib = $query->param('single_bib');
82 $biblionumbers = "$bib/";
84 if (! $biblionumbers) {
85 $biblionumbers = $query->param('biblionumber');
88 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
89 $template->param(message=>1, no_biblionumber=>1);
90 &get_out($query, $cookie, $template->output);
93 # Pass the numbers to the page so they can be fed back
94 # when the hold is confirmed. TODO: Not necessary?
95 $template->param( biblionumbers => $biblionumbers );
97 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
98 my @biblionumbers = split /\//, $biblionumbers;
99 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
100 # TODO: New message?
101 $template->param(message=>1, no_biblionumber=>1);
102 &get_out($query, $cookie, $template->output);
105 # pass the pickup branch along....
106 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
107 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
108 $template->param( branch => $branch );
110 # make branch selection options...
111 my $branchloop = GetBranchesLoop($branch);
113 # Is the person allowed to choose their branch
114 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
116 $template->param( choose_branch => $OPACChooseBranch);
120 # Build hashes of the requested biblio(item)s and items.
124 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
125 my %itemInfoHash; # Hash of itemnumber to item info.
126 foreach my $biblioNumber (@biblionumbers) {
128 my $biblioData = GetBiblioData($biblioNumber);
129 $biblioDataHash{$biblioNumber} = $biblioData;
131 my @itemInfos = GetItemsInfo($biblioNumber);
133 my $marcrecord= GetMarcBiblio($biblioNumber);
135 # flag indicating existence of at least one item linked via a host record
136 my $hostitemsflag;
137 # adding items linked via host biblios
138 my @hostitemInfos = GetHostItemsInfo($marcrecord);
139 if (@hostitemInfos){
140 $hostitemsflag =1;
141 push (@itemInfos,@hostitemInfos);
144 $biblioData->{itemInfos} = \@itemInfos;
145 foreach my $itemInfo (@itemInfos) {
146 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
149 # Compute the priority rank.
150 my ( $rank, $reserves ) =
151 GetReservesFromBiblionumber( $biblioNumber, 1 );
152 $biblioData->{reservecount} = 1; # new reserve
153 foreach my $res (@{$reserves}) {
154 my $found = $res->{found};
155 if ( $found && $found eq 'W' ) {
156 $rank--;
158 else {
159 $biblioData->{reservecount}++;
162 $biblioData->{rank} = $rank + 1;
167 # If this is the second time through this script, it
168 # means we are carrying out the hold request, possibly
169 # with a specific item for each biblionumber.
172 if ( $query->param('place_reserve') ) {
173 my $notes = $query->param('notes');
174 my $reserve_cnt = 0;
175 if ($MAXIMUM_NUMBER_OF_RESERVES) {
176 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
179 # List is composed of alternating biblio/item/branch
180 my $selectedItems = $query->param('selecteditems');
182 if ($query->param('reserve_mode') eq 'single') {
183 # This indicates non-JavaScript mode, so there was
184 # only a single biblio number selected.
185 my $bib = $query->param('single_bib');
186 my $item = $query->param("checkitem_$bib");
187 if ($item eq 'any') {
188 $item = '';
190 my $branch = $query->param('branch');
191 $selectedItems = "$bib/$item/$branch/";
194 $selectedItems =~ s!/$!!;
195 my @selectedItems = split /\//, $selectedItems, -1;
197 # Make sure there is a biblionum/itemnum/branch triplet for each item.
198 # The itemnum can be 'any', meaning next available.
199 my $selectionCount = @selectedItems;
200 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
201 $template->param(message=>1, bad_data=>1);
202 &get_out($query, $cookie, $template->output);
205 while (@selectedItems) {
206 my $biblioNum = shift(@selectedItems);
207 my $itemNum = shift(@selectedItems);
208 my $branch = shift(@selectedItems); # i.e., branch code, not name
210 my $canreserve = 0;
212 my $singleBranchMode = C4::Context->preference("singleBranchMode");
213 if ( $singleBranchMode || !$OPACChooseBranch )
214 { # single branch mode or disabled user choosing
215 $branch = $borr->{'branchcode'};
218 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
219 if ( $itemNum ne '' ) {
220 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
221 if ( $hostbiblioNum ne $biblioNum ) {
222 $biblioNum = $hostbiblioNum;
226 my $biblioData = $biblioDataHash{$biblioNum};
227 my $found;
229 # Check for user supplied reserve date
230 my $startdate;
231 if ( C4::Context->preference('AllowHoldDateInFuture')
232 && C4::Context->preference('OPACAllowHoldDateInFuture') )
234 $startdate = $query->param("reserve_date_$biblioNum");
237 my $expiration_date = $query->param("expiration_date_$biblioNum");
239 # If a specific item was selected and the pickup branch is the same as the
240 # holdingbranch, force the value $rank and $found.
241 my $rank = $biblioData->{rank};
242 if ( $itemNum ne '' ) {
243 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum );
244 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
245 my $item = GetItem($itemNum);
246 if ( $item->{'holdingbranch'} eq $branch ) {
247 $found = 'W'
248 unless C4::Context->preference('ReservesNeedReturns');
251 else {
252 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum );
254 # Inserts a null into the 'itemnumber' field of 'reserves' table.
255 $itemNum = undef;
258 if ( $MAXIMUM_NUMBER_OF_RESERVES
259 && $reserve_cnt >= $MAXIMUM_NUMBER_OF_RESERVES )
261 $canreserve = 0;
264 # Here we actually do the reserveration. Stage 3.
265 if ($canreserve) {
266 AddReserve(
267 $branch, $borrowernumber,
268 $biblioNum, 'a',
269 [$biblioNum], $rank,
270 $startdate, $expiration_date,
271 $notes, $biblioData->{title},
272 $itemNum, $found
274 ++$reserve_cnt;
278 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
279 exit;
284 # Here we check that the borrower can actually make reserves Stage 1.
287 my $noreserves = 0;
288 my $maxoutstanding = C4::Context->preference("maxoutstanding");
289 $template->param( noreserve => 1 ) unless $maxoutstanding;
290 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
291 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
292 $template->param( message => 1 );
293 $noreserves = 1;
294 $template->param( too_much_oweing => $amount );
296 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
297 $noreserves = 1;
298 $template->param(
299 message => 1,
300 GNA => 1
303 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
304 $noreserves = 1;
305 $template->param(
306 message => 1,
307 lost => 1
310 if ( CheckBorrowerDebarred($borrowernumber) ) {
311 $noreserves = 1;
312 $template->param(
313 message => 1,
314 debarred => 1
318 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
319 $template->param( RESERVES => \@reserves );
320 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
321 $template->param( message => 1 );
322 $noreserves = 1;
323 $template->param( too_many_reserves => scalar(@reserves));
325 foreach my $res (@reserves) {
326 foreach my $biblionumber (@biblionumbers) {
327 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
328 # $template->param( message => 1 );
329 # $noreserves = 1;
330 # $template->param( already_reserved => 1 );
331 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
336 unless ($noreserves) {
337 $template->param( select_item_types => 1 );
343 # Build the template parameters that will show the info
344 # and items for each biblionumber.
347 my $notforloan_label_of = get_notforloan_label_of();
349 my $biblioLoop = [];
350 my $numBibsAvailable = 0;
351 my $itemdata_enumchron = 0;
352 my $anyholdable;
353 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
354 $template->param('item_level_itypes' => $itemLevelTypes);
356 foreach my $biblioNum (@biblionumbers) {
358 my $record = GetMarcBiblio($biblioNum);
359 # Init the bib item with the choices for branch pickup
360 my %biblioLoopIter = ( branchloop => $branchloop );
362 # Get relevant biblio data.
363 my $biblioData = $biblioDataHash{$biblioNum};
364 if (! $biblioData) {
365 $template->param(message=>1, bad_biblionumber=>$biblioNum);
366 &get_out($query, $cookie, $template->output);
369 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
370 $biblioLoopIter{title} = $biblioData->{title};
371 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
372 $biblioLoopIter{author} = $biblioData->{author};
373 $biblioLoopIter{rank} = $biblioData->{rank};
374 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
375 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
377 if (!$itemLevelTypes && $biblioData->{itemtype}) {
378 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
379 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
382 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
383 $debug and warn $itemInfo->{'notforloan'};
385 # Get reserve fee.
386 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
387 ( $itemInfo->{'biblioitemnumber'} ) );
388 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
390 if ($itemLevelTypes && $itemInfo->{itype}) {
391 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
392 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
395 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
396 $biblioLoopIter{forloan} = 1;
400 $biblioLoopIter{itemLoop} = [];
401 my $numCopiesAvailable = 0;
402 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
403 my $itemNum = $itemInfo->{itemnumber};
404 my $itemLoopIter = {};
406 $itemLoopIter->{itemnumber} = $itemNum;
407 $itemLoopIter->{barcode} = $itemInfo->{barcode};
408 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
409 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
410 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
411 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
412 if ($itemLevelTypes) {
413 $itemLoopIter->{description} = $itemInfo->{description};
414 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
417 # If the holdingbranch is different than the homebranch, we show the
418 # holdingbranch of the document too.
419 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
420 $itemLoopIter->{holdingBranchName} =
421 $branches->{ $itemInfo->{holdingbranch} }{branchname};
424 # If the item is currently on loan, we display its return date and
425 # change the background color.
426 my $issues= GetItemIssue($itemNum);
427 if ( $issues->{'date_due'} ) {
428 $itemLoopIter->{dateDue} = format_sqlduedatetime($issues->{date_due});
429 $itemLoopIter->{backgroundcolor} = 'onloan';
432 # checking reserve
433 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
434 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
436 # the item could be reserved for this borrower vi a host record, flag this
437 if ($reservedfor eq $borrowernumber){
438 $itemLoopIter->{already_reserved} = 1;
441 if ( defined $reservedate ) {
442 $itemLoopIter->{backgroundcolor} = 'reserved';
443 $itemLoopIter->{reservedate} = format_date($reservedate);
444 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
445 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
446 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
447 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
450 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
451 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
453 # Management of the notforloan document
454 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
455 $itemLoopIter->{backgroundcolor} = 'other';
456 $itemLoopIter->{notforloanvalue} =
457 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
460 # Management of lost or long overdue items
461 if ( $itemInfo->{itemlost} ) {
463 # FIXME localized strings should never be in Perl code
464 $itemLoopIter->{message} =
465 $itemInfo->{itemlost} == 1 ? "(lost)"
466 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
467 : "";
468 $itemInfo->{backgroundcolor} = 'other';
471 # Check of the transfered documents
472 my ( $transfertwhen, $transfertfrom, $transfertto ) =
473 GetTransfers($itemNum);
474 if ( $transfertwhen && ($transfertwhen ne '') ) {
475 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
476 $itemLoopIter->{transfertfrom} =
477 $branches->{$transfertfrom}{branchname};
478 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
479 $itemLoopIter->{nocancel} = 1;
482 # if the items belongs to a host record, show link to host record
483 if ($itemInfo->{biblionumber} ne $biblioNum){
484 $biblioLoopIter{hostitemsflag} = 1;
485 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
486 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
489 # If there is no loan, return and transfer, we show a checkbox.
490 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
492 my $branch = ( C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' ) ? $itemInfo->{'homebranch'} : $borr->{'branchcode'};
494 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
495 my $policy_holdallowed = 1;
497 if ( $branchitemrule->{'holdallowed'} == 0 ||
498 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
499 $policy_holdallowed = 0;
502 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum) and ($itemLoopIter->{already_reserved} ne 1)) {
503 $itemLoopIter->{available} = 1;
504 $numCopiesAvailable++;
507 # FIXME: move this to a pm
508 my $dbh = C4::Context->dbh;
509 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
510 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
511 while (my $wait_hashref = $sth2->fetchrow_hashref) {
512 $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
514 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
516 # Show serial enumeration when needed
517 if ($itemLoopIter->{enumchron}) {
518 $itemdata_enumchron = 1;
521 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
523 $template->param( itemdata_enumchron => $itemdata_enumchron );
525 if ($numCopiesAvailable > 0) {
526 $numBibsAvailable++;
527 $biblioLoopIter{bib_available} = 1;
528 $biblioLoopIter{holdable} = 1;
529 $anyholdable = 1;
531 if ($biblioLoopIter{already_reserved}) {
532 $biblioLoopIter{holdable} = undef;
533 $anyholdable = undef;
535 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
536 $biblioLoopIter{holdable} = undef;
537 $anyholdable = undef;
539 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
540 $biblioLoopIter{holdable} = undef;
541 $biblioLoopIter{already_patron_possession} = 1;
542 $anyholdable = undef;
545 push @$biblioLoop, \%biblioLoopIter;
548 if ( $numBibsAvailable == 0 || !$anyholdable) {
549 $template->param( none_available => 1 );
552 my $itemTableColspan = 7;
553 if (! $template->{VARS}->{'OPACItemHolds'}) {
554 $itemTableColspan--;
556 if (! $template->{VARS}->{'singleBranchMode'}) {
557 $itemTableColspan--;
559 $template->param(itemtable_colspan => $itemTableColspan);
561 # display infos
562 $template->param(bibitemloop => $biblioLoop);
563 $template->param( showholds=>$show_holds_count);
564 $template->param( showpriority=>$show_priority);
565 # can set reserve date in future
566 if (
567 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
568 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
570 $template->param(
571 reserve_in_future => 1,
575 output_html_with_http_headers $query, $cookie, $template->output;