Bug 9552 - BIB1 Relation "Greater Than" Attribute Not Mapped Properly in CCL.Properties
[koha.git] / opac / opac-reserve.pl
blob07b4f76520a456cafe867aa60115aa1831deeab8
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 $CGIbranchloop = GetBranchesLoop($branch);
112 $template->param( CGIbranch => $CGIbranchloop );
114 # Is the person allowed to choose their branch
115 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
117 $template->param( choose_branch => $OPACChooseBranch);
121 # Build hashes of the requested biblio(item)s and items.
125 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
126 my %itemInfoHash; # Hash of itemnumber to item info.
127 foreach my $biblioNumber (@biblionumbers) {
129 my $biblioData = GetBiblioData($biblioNumber);
130 $biblioDataHash{$biblioNumber} = $biblioData;
132 my @itemInfos = GetItemsInfo($biblioNumber);
134 my $marcrecord= GetMarcBiblio($biblioNumber);
136 # flag indicating existence of at least one item linked via a host record
137 my $hostitemsflag;
138 # adding items linked via host biblios
139 my @hostitemInfos = GetHostItemsInfo($marcrecord);
140 if (@hostitemInfos){
141 $hostitemsflag =1;
142 push (@itemInfos,@hostitemInfos);
145 $biblioData->{itemInfos} = \@itemInfos;
146 foreach my $itemInfo (@itemInfos) {
147 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
150 # Compute the priority rank.
151 my ( $rank, $reserves ) =
152 GetReservesFromBiblionumber( $biblioNumber, 1 );
153 $biblioData->{reservecount} = 1; # new reserve
154 foreach my $res (@{$reserves}) {
155 my $found = $res->{found};
156 if ( $found && $found eq 'W' ) {
157 $rank--;
159 else {
160 $biblioData->{reservecount}++;
163 $biblioData->{rank} = $rank + 1;
168 # If this is the second time through this script, it
169 # means we are carrying out the hold request, possibly
170 # with a specific item for each biblionumber.
173 if ( $query->param('place_reserve') ) {
174 my $notes = $query->param('notes');
175 my $reserve_cnt = 0;
176 if ($MAXIMUM_NUMBER_OF_RESERVES) {
177 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
180 # List is composed of alternating biblio/item/branch
181 my $selectedItems = $query->param('selecteditems');
183 if ($query->param('reserve_mode') eq 'single') {
184 # This indicates non-JavaScript mode, so there was
185 # only a single biblio number selected.
186 my $bib = $query->param('single_bib');
187 my $item = $query->param("checkitem_$bib");
188 if ($item eq 'any') {
189 $item = '';
191 my $branch = $query->param('branch');
192 $selectedItems = "$bib/$item/$branch/";
195 $selectedItems =~ s!/$!!;
196 my @selectedItems = split /\//, $selectedItems, -1;
198 # Make sure there is a biblionum/itemnum/branch triplet for each item.
199 # The itemnum can be 'any', meaning next available.
200 my $selectionCount = @selectedItems;
201 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
202 $template->param(message=>1, bad_data=>1);
203 &get_out($query, $cookie, $template->output);
206 while (@selectedItems) {
207 my $biblioNum = shift(@selectedItems);
208 my $itemNum = shift(@selectedItems);
209 my $branch = shift(@selectedItems); # i.e., branch code, not name
211 my $canreserve = 0;
213 my $singleBranchMode = C4::Context->preference("singleBranchMode");
214 if ( $singleBranchMode || !$OPACChooseBranch )
215 { # single branch mode or disabled user choosing
216 $branch = $borr->{'branchcode'};
219 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
220 if ( $itemNum ne '' ) {
221 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
222 if ( $hostbiblioNum ne $biblioNum ) {
223 $biblioNum = $hostbiblioNum;
227 my $biblioData = $biblioDataHash{$biblioNum};
228 my $found;
230 # Check for user supplied reserve date
231 my $startdate;
232 if ( C4::Context->preference('AllowHoldDateInFuture')
233 && C4::Context->preference('OPACAllowHoldDateInFuture') )
235 $startdate = $query->param("reserve_date_$biblioNum");
238 my $expiration_date = $query->param("expiration_date_$biblioNum");
240 # If a specific item was selected and the pickup branch is the same as the
241 # holdingbranch, force the value $rank and $found.
242 my $rank = $biblioData->{rank};
243 if ( $itemNum ne '' ) {
244 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum );
245 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
246 my $item = GetItem($itemNum);
247 if ( $item->{'holdingbranch'} eq $branch ) {
248 $found = 'W'
249 unless C4::Context->preference('ReservesNeedReturns');
252 else {
253 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum );
255 # Inserts a null into the 'itemnumber' field of 'reserves' table.
256 $itemNum = undef;
259 if ( $MAXIMUM_NUMBER_OF_RESERVES
260 && $reserve_cnt >= $MAXIMUM_NUMBER_OF_RESERVES )
262 $canreserve = 0;
265 # Here we actually do the reserveration. Stage 3.
266 if ($canreserve) {
267 AddReserve(
268 $branch, $borrowernumber,
269 $biblioNum, 'a',
270 [$biblioNum], $rank,
271 $startdate, $expiration_date,
272 $notes, $biblioData->{title},
273 $itemNum, $found
275 ++$reserve_cnt;
279 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
280 exit;
285 # Here we check that the borrower can actually make reserves Stage 1.
288 my $noreserves = 0;
289 my $maxoutstanding = C4::Context->preference("maxoutstanding");
290 $template->param( noreserve => 1 ) unless $maxoutstanding;
291 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
292 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
293 $template->param( message => 1 );
294 $noreserves = 1;
295 $template->param( too_much_oweing => $amount );
297 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
298 $noreserves = 1;
299 $template->param(
300 message => 1,
301 GNA => 1
304 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
305 $noreserves = 1;
306 $template->param(
307 message => 1,
308 lost => 1
311 if ( CheckBorrowerDebarred($borrowernumber) ) {
312 $noreserves = 1;
313 $template->param(
314 message => 1,
315 debarred => 1
319 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
320 $template->param( RESERVES => \@reserves );
321 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
322 $template->param( message => 1 );
323 $noreserves = 1;
324 $template->param( too_many_reserves => scalar(@reserves));
326 foreach my $res (@reserves) {
327 foreach my $biblionumber (@biblionumbers) {
328 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
329 # $template->param( message => 1 );
330 # $noreserves = 1;
331 # $template->param( already_reserved => 1 );
332 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
337 unless ($noreserves) {
338 $template->param( select_item_types => 1 );
344 # Build the template parameters that will show the info
345 # and items for each biblionumber.
348 my $notforloan_label_of = get_notforloan_label_of();
350 my $biblioLoop = [];
351 my $numBibsAvailable = 0;
352 my $itemdata_enumchron = 0;
353 my $anyholdable;
354 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
355 $template->param('item_level_itypes' => $itemLevelTypes);
357 foreach my $biblioNum (@biblionumbers) {
359 my $record = GetMarcBiblio($biblioNum);
360 # Init the bib item with the choices for branch pickup
361 my %biblioLoopIter = ( branchChoicesLoop => $CGIbranchloop );
363 # Get relevant biblio data.
364 my $biblioData = $biblioDataHash{$biblioNum};
365 if (! $biblioData) {
366 $template->param(message=>1, bad_biblionumber=>$biblioNum);
367 &get_out($query, $cookie, $template->output);
370 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
371 $biblioLoopIter{title} = $biblioData->{title};
372 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
373 $biblioLoopIter{author} = $biblioData->{author};
374 $biblioLoopIter{rank} = $biblioData->{rank};
375 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
376 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
378 if (!$itemLevelTypes && $biblioData->{itemtype}) {
379 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
380 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
383 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
384 $debug and warn $itemInfo->{'notforloan'};
386 # Get reserve fee.
387 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
388 ( $itemInfo->{'biblioitemnumber'} ) );
389 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
391 if ($itemLevelTypes && $itemInfo->{itype}) {
392 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
393 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
396 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
397 $biblioLoopIter{forloan} = 1;
401 $biblioLoopIter{itemLoop} = [];
402 my $numCopiesAvailable = 0;
403 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
404 my $itemNum = $itemInfo->{itemnumber};
405 my $itemLoopIter = {};
407 $itemLoopIter->{itemnumber} = $itemNum;
408 $itemLoopIter->{barcode} = $itemInfo->{barcode};
409 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
410 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
411 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
412 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
413 if ($itemLevelTypes) {
414 $itemLoopIter->{description} = $itemInfo->{description};
415 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
418 # If the holdingbranch is different than the homebranch, we show the
419 # holdingbranch of the document too.
420 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
421 $itemLoopIter->{holdingBranchName} =
422 $branches->{ $itemInfo->{holdingbranch} }{branchname};
425 # If the item is currently on loan, we display its return date and
426 # change the background color.
427 my $issues= GetItemIssue($itemNum);
428 if ( $issues->{'date_due'} ) {
429 $itemLoopIter->{dateDue} = format_sqlduedatetime($issues->{date_due});
430 $itemLoopIter->{backgroundcolor} = 'onloan';
433 # checking reserve
434 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
435 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
437 # the item could be reserved for this borrower vi a host record, flag this
438 if ($reservedfor eq $borrowernumber){
439 $itemLoopIter->{already_reserved} = 1;
442 if ( defined $reservedate ) {
443 $itemLoopIter->{backgroundcolor} = 'reserved';
444 $itemLoopIter->{reservedate} = format_date($reservedate);
445 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
446 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
447 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
448 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
451 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
452 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
454 # Management of the notforloan document
455 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
456 $itemLoopIter->{backgroundcolor} = 'other';
457 $itemLoopIter->{notforloanvalue} =
458 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
461 # Management of lost or long overdue items
462 if ( $itemInfo->{itemlost} ) {
464 # FIXME localized strings should never be in Perl code
465 $itemLoopIter->{message} =
466 $itemInfo->{itemlost} == 1 ? "(lost)"
467 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
468 : "";
469 $itemInfo->{backgroundcolor} = 'other';
472 # Check of the transfered documents
473 my ( $transfertwhen, $transfertfrom, $transfertto ) =
474 GetTransfers($itemNum);
475 if ( $transfertwhen && ($transfertwhen ne '') ) {
476 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
477 $itemLoopIter->{transfertfrom} =
478 $branches->{$transfertfrom}{branchname};
479 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
480 $itemLoopIter->{nocancel} = 1;
483 # if the items belongs to a host record, show link to host record
484 if ($itemInfo->{biblionumber} ne $biblioNum){
485 $biblioLoopIter{hostitemsflag} = 1;
486 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
487 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
490 # If there is no loan, return and transfer, we show a checkbox.
491 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
493 my $branch = ( C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' ) ? $itemInfo->{'homebranch'} : $borr->{'branchcode'};
495 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
496 my $policy_holdallowed = 1;
498 if ( $branchitemrule->{'holdallowed'} == 0 ||
499 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
500 $policy_holdallowed = 0;
503 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum) and ($itemLoopIter->{already_reserved} ne 1)) {
504 $itemLoopIter->{available} = 1;
505 $numCopiesAvailable++;
508 # FIXME: move this to a pm
509 my $dbh = C4::Context->dbh;
510 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
511 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
512 while (my $wait_hashref = $sth2->fetchrow_hashref) {
513 $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
515 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
517 # Show serial enumeration when needed
518 if ($itemLoopIter->{enumchron}) {
519 $itemdata_enumchron = 1;
522 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
524 $template->param( itemdata_enumchron => $itemdata_enumchron );
526 if ($numCopiesAvailable > 0) {
527 $numBibsAvailable++;
528 $biblioLoopIter{bib_available} = 1;
529 $biblioLoopIter{holdable} = 1;
530 $anyholdable = 1;
532 if ($biblioLoopIter{already_reserved}) {
533 $biblioLoopIter{holdable} = undef;
534 $anyholdable = undef;
536 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
537 $biblioLoopIter{holdable} = undef;
538 $anyholdable = undef;
540 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
541 $biblioLoopIter{holdable} = undef;
542 $biblioLoopIter{already_patron_possession} = 1;
543 $anyholdable = undef;
546 push @$biblioLoop, \%biblioLoopIter;
549 if ( $numBibsAvailable == 0 || !$anyholdable) {
550 $template->param( none_available => 1 );
553 my $itemTableColspan = 7;
554 if (! $template->{VARS}->{'OPACItemHolds'}) {
555 $itemTableColspan--;
557 if (! $template->{VARS}->{'singleBranchMode'}) {
558 $itemTableColspan--;
560 $template->param(itemtable_colspan => $itemTableColspan);
562 # display infos
563 $template->param(bibitemloop => $biblioLoop);
564 $template->param( showholds=>$show_holds_count);
565 $template->param( showpriority=>$show_priority);
566 # can set reserve date in future
567 if (
568 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
569 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
571 $template->param(
572 reserve_in_future => 1,
576 output_html_with_http_headers $query, $cookie, $template->output;