Bug 7934: generate separate PO files for help pages
[koha.git] / opac / opac-reserve.pl
blob97d136a1f339fb8e7355d6fabe91bb1ca73dcadc
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 $reserve_cnt = 0;
174 if ($MAXIMUM_NUMBER_OF_RESERVES) {
175 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
178 # List is composed of alternating biblio/item/branch
179 my $selectedItems = $query->param('selecteditems');
181 if ($query->param('reserve_mode') eq 'single') {
182 # This indicates non-JavaScript mode, so there was
183 # only a single biblio number selected.
184 my $bib = $query->param('single_bib');
185 my $item = $query->param("checkitem_$bib");
186 if ($item eq 'any') {
187 $item = '';
189 my $branch = $query->param('branch');
190 $selectedItems = "$bib/$item/$branch/";
193 $selectedItems =~ s!/$!!;
194 my @selectedItems = split /\//, $selectedItems, -1;
196 # Make sure there is a biblionum/itemnum/branch triplet for each item.
197 # The itemnum can be 'any', meaning next available.
198 my $selectionCount = @selectedItems;
199 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
200 $template->param(message=>1, bad_data=>1);
201 &get_out($query, $cookie, $template->output);
204 while (@selectedItems) {
205 my $biblioNum = shift(@selectedItems);
206 my $itemNum = shift(@selectedItems);
207 my $branch = shift(@selectedItems); # i.e., branch code, not name
209 my $canreserve = 0;
211 my $singleBranchMode = C4::Context->preference("singleBranchMode");
212 if ( $singleBranchMode || !$OPACChooseBranch )
213 { # single branch mode or disabled user choosing
214 $branch = $borr->{'branchcode'};
217 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
218 if ( $itemNum ne '' ) {
219 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
220 if ( $hostbiblioNum ne $biblioNum ) {
221 $biblioNum = $hostbiblioNum;
225 my $biblioData = $biblioDataHash{$biblioNum};
226 my $found;
228 # Check for user supplied reserve date
229 my $startdate;
230 if ( C4::Context->preference('AllowHoldDateInFuture')
231 && C4::Context->preference('OPACAllowHoldDateInFuture') )
233 $startdate = $query->param("reserve_date_$biblioNum");
236 my $expiration_date = $query->param("expiration_date_$biblioNum");
238 # If a specific item was selected and the pickup branch is the same as the
239 # holdingbranch, force the value $rank and $found.
240 my $rank = $biblioData->{rank};
241 if ( $itemNum ne '' ) {
242 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum );
243 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
244 my $item = GetItem($itemNum);
245 if ( $item->{'holdingbranch'} eq $branch ) {
246 $found = 'W'
247 unless C4::Context->preference('ReservesNeedReturns');
250 else {
251 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum );
253 # Inserts a null into the 'itemnumber' field of 'reserves' table.
254 $itemNum = undef;
256 my $notes = $query->param('notes_'.$biblioNum)||'';
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 ( $borr->{'debarred'} ) {
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 = 0;
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};
376 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
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 = GetReservesControlBranch( $itemInfo, $borr );
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;
531 if ($biblioLoopIter{already_reserved}) {
532 $biblioLoopIter{holdable} = undef;
534 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
535 $biblioLoopIter{holdable} = undef;
537 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
538 $biblioLoopIter{holdable} = undef;
539 $biblioLoopIter{already_patron_possession} = 1;
542 if( $biblioLoopIter{holdable} ){ $anyholdable++; }
544 push @$biblioLoop, \%biblioLoopIter;
547 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
548 $template->param( none_available => 1 );
551 my $itemTableColspan = 9;
552 if (! $template->{VARS}->{'OPACItemHolds'}) {
553 $itemTableColspan--;
555 if (! $template->{VARS}->{'singleBranchMode'}) {
556 $itemTableColspan--;
558 $itemTableColspan-- if !$show_holds_count && !$show_priority;
559 my $show_notes=C4::Context->preference('OpacHoldNotes');
560 $template->param(OpacHoldNotes=>$show_notes);
561 $itemTableColspan-- if !$show_notes;
562 $template->param(itemtable_colspan => $itemTableColspan);
564 # display infos
565 $template->param(bibitemloop => $biblioLoop);
566 $template->param( showholds=>$show_holds_count);
567 $template->param( showpriority=>$show_priority);
568 # can set reserve date in future
569 if (
570 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
571 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
573 $template->param(
574 reserve_in_future => 1,
578 output_html_with_http_headers $query, $cookie, $template->output;