Bug 12728: Add StatisticsFields to sysprefs.sql
[koha.git] / opac / opac-reserve.pl
blob45d03bd65e38a6c7df542d7f6c68ad93ca7ec306
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 Date::Calc qw/Today Date_to_Days/;
39 # use Data::Dumper;
41 my $maxreserves = C4::Context->preference("maxreserves");
43 my $query = new CGI;
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
46 template_name => "opac-reserve.tt",
47 query => $query,
48 type => "opac",
49 authnotrequired => 0,
50 flagsrequired => { borrow => 1 },
51 debug => 1,
55 my ($show_holds_count, $show_priority);
56 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
57 m/holds/o and $show_holds_count = 1;
58 m/priority/ and $show_priority = 1;
61 sub get_out {
62 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
63 exit;
66 # get borrower information ....
67 my ( $borr ) = GetMemberDetails( $borrowernumber );
69 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
70 if ( $borr->{'BlockExpiredPatronOpacActions'} ) {
72 if ( $borr->{'is_expired'} ) {
74 # cannot reserve, their card has expired and the rules set mean this is not allowed
75 $template->param( message => 1, expired_patron => 1 );
76 get_out( $query, $cookie, $template->output );
80 # Pass through any reserve charge
81 if ($borr->{reservefee} > 0){
82 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
84 # get branches and itemtypes
85 my $branches = GetBranches();
86 my $itemTypes = GetItemTypes();
88 # There are two ways of calling this script, with a single biblio num
89 # or multiple biblio nums.
90 my $biblionumbers = $query->param('biblionumbers');
91 my $reserveMode = $query->param('reserve_mode');
92 if ($reserveMode && ($reserveMode eq 'single')) {
93 my $bib = $query->param('single_bib');
94 $biblionumbers = "$bib/";
96 if (! $biblionumbers) {
97 $biblionumbers = $query->param('biblionumber');
100 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
101 $template->param(message=>1, no_biblionumber=>1);
102 &get_out($query, $cookie, $template->output);
105 # Pass the numbers to the page so they can be fed back
106 # when the hold is confirmed. TODO: Not necessary?
107 $template->param( biblionumbers => $biblionumbers );
109 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
110 my @biblionumbers = split /\//, $biblionumbers;
111 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
112 # TODO: New message?
113 $template->param(message=>1, no_biblionumber=>1);
114 &get_out($query, $cookie, $template->output);
118 # pass the pickup branch along....
119 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
120 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
121 $template->param( branch => $branch );
123 # make branch selection options...
124 my $branchloop = GetBranchesLoop($branch);
126 # Is the person allowed to choose their branch
127 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
129 $template->param( choose_branch => $OPACChooseBranch);
133 # Build hashes of the requested biblio(item)s and items.
137 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
138 my %itemInfoHash; # Hash of itemnumber to item info.
139 foreach my $biblioNumber (@biblionumbers) {
141 my $biblioData = GetBiblioData($biblioNumber);
142 $biblioDataHash{$biblioNumber} = $biblioData;
144 my @itemInfos = GetItemsInfo($biblioNumber);
146 my $marcrecord= GetMarcBiblio($biblioNumber);
148 # flag indicating existence of at least one item linked via a host record
149 my $hostitemsflag;
150 # adding items linked via host biblios
151 my @hostitemInfos = GetHostItemsInfo($marcrecord);
152 if (@hostitemInfos){
153 $hostitemsflag =1;
154 push (@itemInfos,@hostitemInfos);
157 $biblioData->{itemInfos} = \@itemInfos;
158 foreach my $itemInfo (@itemInfos) {
159 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
162 # Compute the priority rank.
163 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
164 my $rank = scalar( @$reserves );
165 $biblioData->{reservecount} = 1; # new reserve
166 foreach my $res (@{$reserves}) {
167 my $found = $res->{found};
168 if ( $found && $found eq 'W' ) {
169 $rank--;
171 else {
172 $biblioData->{reservecount}++;
175 $biblioData->{rank} = $rank + 1;
180 # If this is the second time through this script, it
181 # means we are carrying out the hold request, possibly
182 # with a specific item for each biblionumber.
185 if ( $query->param('place_reserve') ) {
186 my $reserve_cnt = 0;
187 if ($maxreserves) {
188 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
191 # List is composed of alternating biblio/item/branch
192 my $selectedItems = $query->param('selecteditems');
194 if ($query->param('reserve_mode') eq 'single') {
195 # This indicates non-JavaScript mode, so there was
196 # only a single biblio number selected.
197 my $bib = $query->param('single_bib');
198 my $item = $query->param("checkitem_$bib");
199 if ($item eq 'any') {
200 $item = '';
202 my $branch = $query->param('branch');
203 $selectedItems = "$bib/$item/$branch/";
206 $selectedItems =~ s!/$!!;
207 my @selectedItems = split /\//, $selectedItems, -1;
209 # Make sure there is a biblionum/itemnum/branch triplet for each item.
210 # The itemnum can be 'any', meaning next available.
211 my $selectionCount = @selectedItems;
212 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
213 $template->param(message=>1, bad_data=>1);
214 &get_out($query, $cookie, $template->output);
217 while (@selectedItems) {
218 my $biblioNum = shift(@selectedItems);
219 my $itemNum = shift(@selectedItems);
220 my $branch = shift(@selectedItems); # i.e., branch code, not name
222 my $canreserve = 0;
224 my $singleBranchMode = C4::Context->preference("singleBranchMode");
225 if ( $singleBranchMode || !$OPACChooseBranch )
226 { # single branch mode or disabled user choosing
227 $branch = $borr->{'branchcode'};
230 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
231 if ( $itemNum ne '' ) {
232 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
233 if ( $hostbiblioNum ne $biblioNum ) {
234 $biblioNum = $hostbiblioNum;
238 my $biblioData = $biblioDataHash{$biblioNum};
239 my $found;
241 # Check for user supplied reserve date
242 my $startdate;
243 if ( C4::Context->preference('AllowHoldDateInFuture')
244 && C4::Context->preference('OPACAllowHoldDateInFuture') )
246 $startdate = $query->param("reserve_date_$biblioNum");
249 my $expiration_date = $query->param("expiration_date_$biblioNum");
251 # If a specific item was selected and the pickup branch is the same as the
252 # holdingbranch, force the value $rank and $found.
253 my $rank = $biblioData->{rank};
254 if ( $itemNum ne '' ) {
255 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum );
256 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
257 my $item = GetItem($itemNum);
258 if ( $item->{'holdingbranch'} eq $branch ) {
259 $found = 'W'
260 unless C4::Context->preference('ReservesNeedReturns');
263 else {
264 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum );
266 # Inserts a null into the 'itemnumber' field of 'reserves' table.
267 $itemNum = undef;
269 my $notes = $query->param('notes_'.$biblioNum)||'';
271 if ( $maxreserves
272 && $reserve_cnt >= $maxreserves )
274 $canreserve = 0;
277 # Here we actually do the reserveration. Stage 3.
278 if ($canreserve) {
279 AddReserve(
280 $branch, $borrowernumber,
281 $biblioNum, 'a',
282 [$biblioNum], $rank,
283 $startdate, $expiration_date,
284 $notes, $biblioData->{title},
285 $itemNum, $found
287 ++$reserve_cnt;
291 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
292 exit;
297 # Here we check that the borrower can actually make reserves Stage 1.
300 my $noreserves = 0;
301 my $maxoutstanding = C4::Context->preference("maxoutstanding");
302 $template->param( noreserve => 1 ) unless $maxoutstanding;
303 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
304 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
305 $template->param( message => 1 );
306 $noreserves = 1;
307 $template->param( too_much_oweing => $amount );
309 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
310 $noreserves = 1;
311 $template->param(
312 message => 1,
313 GNA => 1
316 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
317 $noreserves = 1;
318 $template->param(
319 message => 1,
320 lost => 1
323 if ( $borr->{'debarred'} ) {
324 $noreserves = 1;
325 $template->param(
326 message => 1,
327 debarred => 1
331 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
332 my $reserves_count = scalar(@reserves);
333 $template->param( RESERVES => \@reserves );
334 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
335 $template->param( message => 1 );
336 $noreserves = 1;
337 $template->param( too_many_reserves => scalar(@reserves));
340 unless ( $noreserves ) {
341 my $requested_reserves_count = scalar( @biblionumbers );
342 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
343 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
347 foreach my $res (@reserves) {
348 foreach my $biblionumber (@biblionumbers) {
349 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
350 # $template->param( message => 1 );
351 # $noreserves = 1;
352 # $template->param( already_reserved => 1 );
353 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
358 unless ($noreserves) {
359 $template->param( select_item_types => 1 );
365 # Build the template parameters that will show the info
366 # and items for each biblionumber.
369 my $notforloan_label_of = get_notforloan_label_of();
371 my $biblioLoop = [];
372 my $numBibsAvailable = 0;
373 my $itemdata_enumchron = 0;
374 my $anyholdable = 0;
375 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
376 $template->param('item_level_itypes' => $itemLevelTypes);
378 foreach my $biblioNum (@biblionumbers) {
380 my $record = GetMarcBiblio($biblioNum);
381 # Init the bib item with the choices for branch pickup
382 my %biblioLoopIter = ( branchloop => $branchloop );
384 # Get relevant biblio data.
385 my $biblioData = $biblioDataHash{$biblioNum};
386 if (! $biblioData) {
387 $template->param(message=>1, bad_biblionumber=>$biblioNum);
388 &get_out($query, $cookie, $template->output);
391 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
392 $biblioLoopIter{title} = $biblioData->{title};
393 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
394 $biblioLoopIter{author} = $biblioData->{author};
395 $biblioLoopIter{rank} = $biblioData->{rank};
396 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
397 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
398 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
400 if (!$itemLevelTypes && $biblioData->{itemtype}) {
401 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
402 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
405 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
406 $debug and warn $itemInfo->{'notforloan'};
408 # Get reserve fee.
409 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
410 ( $itemInfo->{'biblioitemnumber'} ) );
411 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
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 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
426 my $itemNum = $itemInfo->{itemnumber};
427 my $itemLoopIter = {};
429 $itemLoopIter->{itemnumber} = $itemNum;
430 $itemLoopIter->{barcode} = $itemInfo->{barcode};
431 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
432 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
433 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
434 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
435 if ($itemLevelTypes) {
436 $itemLoopIter->{description} = $itemInfo->{description};
437 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
440 # If the holdingbranch is different than the homebranch, we show the
441 # holdingbranch of the document too.
442 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
443 $itemLoopIter->{holdingBranchName} =
444 $branches->{ $itemInfo->{holdingbranch} }{branchname};
447 # If the item is currently on loan, we display its return date and
448 # change the background color.
449 my $issues= GetItemIssue($itemNum);
450 if ( $issues->{'date_due'} ) {
451 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
452 $itemLoopIter->{backgroundcolor} = 'onloan';
455 # checking reserve
456 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
457 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
459 # the item could be reserved for this borrower vi a host record, flag this
460 if ($reservedfor eq $borrowernumber){
461 $itemLoopIter->{already_reserved} = 1;
464 if ( defined $reservedate ) {
465 $itemLoopIter->{backgroundcolor} = 'reserved';
466 $itemLoopIter->{reservedate} = format_date($reservedate);
467 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
468 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
469 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
470 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
471 #waiting status
472 $itemLoopIter->{waitingdate} = $wait;
475 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
476 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
478 # Management of the notforloan document
479 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
480 $itemLoopIter->{backgroundcolor} = 'other';
481 $itemLoopIter->{notforloanvalue} =
482 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
485 # Management of lost or long overdue items
486 if ( $itemInfo->{itemlost} ) {
488 # FIXME localized strings should never be in Perl code
489 $itemLoopIter->{message} =
490 $itemInfo->{itemlost} == 1 ? "(lost)"
491 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
492 : "";
493 $itemInfo->{backgroundcolor} = 'other';
496 # Check of the transfered documents
497 my ( $transfertwhen, $transfertfrom, $transfertto ) =
498 GetTransfers($itemNum);
499 if ( $transfertwhen && ($transfertwhen ne '') ) {
500 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
501 $itemLoopIter->{transfertfrom} =
502 $branches->{$transfertfrom}{branchname};
503 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
504 $itemLoopIter->{nocancel} = 1;
507 # if the items belongs to a host record, show link to host record
508 if ($itemInfo->{biblionumber} ne $biblioNum){
509 $biblioLoopIter{hostitemsflag} = 1;
510 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
511 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
514 # If there is no loan, return and transfer, we show a checkbox.
515 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
517 my $branch = GetReservesControlBranch( $itemInfo, $borr );
519 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
520 my $policy_holdallowed = 1;
522 if ( $branchitemrule->{'holdallowed'} == 0 ||
523 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
524 $policy_holdallowed = 0;
527 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum) and ($itemLoopIter->{already_reserved} ne 1)) {
528 $itemLoopIter->{available} = 1;
529 $numCopiesAvailable++;
532 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
534 # Show serial enumeration when needed
535 if ($itemLoopIter->{enumchron}) {
536 $itemdata_enumchron = 1;
539 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
541 $template->param( itemdata_enumchron => $itemdata_enumchron );
543 if ($numCopiesAvailable > 0) {
544 $numBibsAvailable++;
545 $biblioLoopIter{bib_available} = 1;
546 $biblioLoopIter{holdable} = 1;
548 if ($biblioLoopIter{already_reserved}) {
549 $biblioLoopIter{holdable} = undef;
551 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
552 $biblioLoopIter{holdable} = undef;
554 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
555 $biblioLoopIter{holdable} = undef;
556 $biblioLoopIter{already_patron_possession} = 1;
559 if( $biblioLoopIter{holdable} ){ $anyholdable++; }
561 push @$biblioLoop, \%biblioLoopIter;
564 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
565 $template->param( none_available => 1 );
568 my $itemTableColspan = 9;
569 if (! $template->{VARS}->{'OPACItemHolds'}) {
570 $itemTableColspan--;
572 if (! $template->{VARS}->{'singleBranchMode'}) {
573 $itemTableColspan--;
575 $itemTableColspan-- if !$show_holds_count && !$show_priority;
576 my $show_notes=C4::Context->preference('OpacHoldNotes');
577 $template->param(OpacHoldNotes=>$show_notes);
578 $itemTableColspan-- if !$show_notes;
579 $template->param(itemtable_colspan => $itemTableColspan);
581 # display infos
582 $template->param(bibitemloop => $biblioLoop);
583 $template->param( showholds=>$show_holds_count);
584 $template->param( showpriority=>$show_priority);
585 # can set reserve date in future
586 if (
587 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
588 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
590 $template->param(
591 reserve_in_future => 1,
595 output_html_with_http_headers $query, $cookie, $template->output;