Revert "Bug 10606: Remove MySQLism in GetUpcomingDueIssues"
[koha.git] / opac / opac-reserve.pl
blobc30ee08ccc5e3344f876f94301417f5a2123f09b
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 my $numCopiesOPACAvailable = 0;
427 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
428 my $itemNum = $itemInfo->{itemnumber};
429 my $itemLoopIter = {};
431 $itemLoopIter->{itemnumber} = $itemNum;
432 $itemLoopIter->{barcode} = $itemInfo->{barcode};
433 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
434 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
435 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
436 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
437 if ($itemLevelTypes) {
438 $itemLoopIter->{description} = $itemInfo->{description};
439 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
442 # If the holdingbranch is different than the homebranch, we show the
443 # holdingbranch of the document too.
444 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
445 $itemLoopIter->{holdingBranchName} =
446 $branches->{ $itemInfo->{holdingbranch} }{branchname};
449 # If the item is currently on loan, we display its return date and
450 # change the background color.
451 my $issues= GetItemIssue($itemNum);
452 if ( $issues->{'date_due'} ) {
453 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
454 $itemLoopIter->{backgroundcolor} = 'onloan';
457 # checking reserve
458 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
459 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
461 # the item could be reserved for this borrower vi a host record, flag this
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 if ($policy_holdallowed) {
523 if (my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} )) {
524 $policy_holdallowed =
525 ($branchitemrule->{'holdallowed'} == 2) ||
526 ($branchitemrule->{'holdallowed'} == 1
527 && $borr->{'branchcode'} eq $itemInfo->{'homebranch'});
528 } else {
529 $policy_holdallowed = 0; # No rule - not allowed
532 $policy_holdallowed &&=
533 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
534 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
536 if ($policy_holdallowed) {
537 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
538 $itemLoopIter->{available} = 1;
539 $numCopiesOPACAvailable++;
540 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
542 $numCopiesAvailable++;
545 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
547 # Show serial enumeration when needed
548 if ($itemLoopIter->{enumchron}) {
549 $itemdata_enumchron = 1;
552 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
554 $template->param( itemdata_enumchron => $itemdata_enumchron );
556 if ($numCopiesAvailable > 0) {
557 $numBibsAvailable++;
558 $biblioLoopIter{bib_available} = 1;
559 $biblioLoopIter{holdable} = 1;
560 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
562 if ($biblioLoopIter{already_reserved}) {
563 $biblioLoopIter{holdable} = undef;
564 $biblioLoopIter{itemholdable} = undef;
566 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
567 $biblioLoopIter{holdable} = undef;
568 $biblioLoopIter{already_patron_possession} = 1;
571 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
573 push @$biblioLoop, \%biblioLoopIter;
575 $anyholdable = 1 if $biblioLoopIter{holdable};
578 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
579 $template->param( none_available => 1 );
582 my $show_notes=C4::Context->preference('OpacHoldNotes');
583 $template->param(OpacHoldNotes=>$show_notes);
585 # display infos
586 $template->param(bibitemloop => $biblioLoop);
587 # can set reserve date in future
588 if (
589 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
590 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
592 $template->param(
593 reserve_in_future => 1,
597 output_html_with_http_headers $query, $cookie, $template->output;