Bug 6755 Follow up, fixing broken call to themelanguage
[koha.git] / opac / opac-reserve.pl
blobab02afe83d1585a0f38878d5dd4fc45755b55791
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
18 use strict;
19 use warnings;
20 use CGI;
21 use C4::Auth; # checkauth, getborrowernumber.
22 use C4::Koha;
23 use C4::Circulation;
24 use C4::Reserves;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Output;
28 use C4::Dates qw/format_date/;
29 use C4::Context;
30 use C4::Members;
31 use C4::Branch; # GetBranches
32 use C4::Debug;
33 # use Data::Dumper;
35 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
37 my $query = new CGI;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "opac-reserve.tmpl",
41 query => $query,
42 type => "opac",
43 authnotrequired => 0,
44 flagsrequired => { borrow => 1 },
45 debug => 1,
48 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
49 sub get_out ($$$) {
50 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
51 exit;
54 # get borrower information ....
55 my ( $borr ) = GetMemberDetails( $borrowernumber );
57 # Pass through any reserve charge
58 if ($borr->{reservefee} > 0){
59 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
61 # get branches and itemtypes
62 my $branches = GetBranches();
63 my $itemTypes = GetItemTypes();
65 # There are two ways of calling this script, with a single biblio num
66 # or multiple biblio nums.
67 my $biblionumbers = $query->param('biblionumbers');
68 my $reserveMode = $query->param('reserve_mode');
69 if ($reserveMode && ($reserveMode eq 'single')) {
70 my $bib = $query->param('single_bib');
71 $biblionumbers = "$bib/";
73 if (! $biblionumbers) {
74 $biblionumbers = $query->param('biblionumber');
77 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
78 $template->param(message=>1, no_biblionumber=>1);
79 &get_out($query, $cookie, $template->output);
82 # Pass the numbers to the page so they can be fed back
83 # when the hold is confirmed. TODO: Not necessary?
84 $template->param( biblionumbers => $biblionumbers );
86 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
87 my @biblionumbers = split /\//, $biblionumbers;
88 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
89 # TODO: New message?
90 $template->param(message=>1, no_biblionumber=>1);
91 &get_out($query, $cookie, $template->output);
94 # pass the pickup branch along....
95 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
96 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
97 $template->param( branch => $branch );
99 # make branch selection options...
100 my $CGIbranchloop = GetBranchesLoop($branch);
101 $template->param( CGIbranch => $CGIbranchloop );
103 # Is the person allowed to choose their branch
104 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
106 $template->param( choose_branch => $OPACChooseBranch);
110 # Build hashes of the requested biblio(item)s and items.
114 # Hash of biblionumber to biblio/biblioitems record.
115 my %biblioDataHash;
117 # Hash of itemnumber to item info.
118 my %itemInfoHash;
120 foreach my $biblioNumber (@biblionumbers) {
122 my $biblioData = GetBiblioData($biblioNumber);
123 $biblioDataHash{$biblioNumber} = $biblioData;
125 my @itemInfos = GetItemsInfo($biblioNumber);
126 $biblioData->{itemInfos} = \@itemInfos;
127 foreach my $itemInfo (@itemInfos) {
128 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
131 # Compute the priority rank.
132 my ( $rank, $reserves ) = GetReservesFromBiblionumber($biblioNumber,1);
133 $biblioData->{reservecount} = $rank;
134 foreach my $res (@$reserves) {
135 my $found = $res->{'found'};
136 if ( $found && ($found eq 'W') ) {
137 $rank--;
140 $rank++;
141 $biblioData->{rank} = $rank;
146 # If this is the second time through this script, it
147 # means we are carrying out the hold request, possibly
148 # with a specific item for each biblionumber.
151 if ( $query->param('place_reserve') ) {
152 my $notes = $query->param('notes');
153 my $canreserve=0;
155 # List is composed of alternating biblio/item/branch
156 my $selectedItems = $query->param('selecteditems');
158 if ($query->param('reserve_mode') eq 'single') {
159 # This indicates non-JavaScript mode, so there was
160 # only a single biblio number selected.
161 my $bib = $query->param('single_bib');
162 my $item = $query->param("checkitem_$bib");
163 if ($item eq 'any') {
164 $item = '';
166 my $branch = $query->param('branch');
167 $selectedItems = "$bib/$item/$branch/";
170 $selectedItems =~ s!/$!!;
171 my @selectedItems = split /\//, $selectedItems, -1;
173 # Make sure there is a biblionum/itemnum/branch triplet for each item.
174 # The itemnum can be 'any', meaning next available.
175 my $selectionCount = @selectedItems;
176 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
177 $template->param(message=>1, bad_data=>1);
178 &get_out($query, $cookie, $template->output);
181 while (@selectedItems) {
182 my $biblioNum = shift(@selectedItems);
183 my $itemNum = shift(@selectedItems);
184 my $branch = shift(@selectedItems); # i.e., branch code, not name
186 my $singleBranchMode = $template->param('singleBranchMode');
187 if ($singleBranchMode || ! $OPACChooseBranch) { # single branch mode or disabled user choosing
188 $branch = $borr->{'branchcode'};
191 my $biblioData = $biblioDataHash{$biblioNum};
192 my $found;
194 # Check for user supplied reserve date
195 my $startdate;
196 if (
197 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
198 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
200 $startdate = $query->param("reserve_date_$biblioNum");
203 my $expiration_date = $query->param("expiration_date_$biblioNum");
205 # If a specific item was selected and the pickup branch is the same as the
206 # holdingbranch, force the value $rank and $found.
207 my $rank = $biblioData->{rank};
208 if ($itemNum ne ''){
209 $canreserve = 1 if CanItemBeReserved($borrowernumber,$itemNum);
210 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
211 my $item = GetItem($itemNum);
212 if ( $item->{'holdingbranch'} eq $branch ){
213 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
216 else {
217 $canreserve = 1 if CanBookBeReserved($borrowernumber,$biblioNum);
218 # Inserts a null into the 'itemnumber' field of 'reserves' table.
219 $itemNum = undef;
222 # Here we actually do the reserveration. Stage 3.
223 AddReserve($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $startdate, $expiration_date, $notes,
224 $biblioData->{'title'}, $itemNum, $found) if ($canreserve);
227 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
228 exit;
233 # Here we check that the borrower can actually make reserves Stage 1.
236 my $noreserves = 0;
237 my $maxoutstanding = C4::Context->preference("maxoutstanding");
238 $template->param( noreserve => 1 ) unless $maxoutstanding;
239 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
240 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
241 $template->param( message => 1 );
242 $noreserves = 1;
243 $template->param( too_much_oweing => $amount );
245 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} eq 1) ) {
246 $noreserves = 1;
247 $template->param(
248 message => 1,
249 GNA => 1
252 if ( $borr->{lost} && ($borr->{lost} eq 1) ) {
253 $noreserves = 1;
254 $template->param(
255 message => 1,
256 lost => 1
259 if ( $borr->{debarred} && ($borr->{debarred} eq 1) ) {
260 $noreserves = 1;
261 $template->param(
262 message => 1,
263 debarred => 1
267 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
268 $template->param( RESERVES => \@reserves );
269 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
270 $template->param( message => 1 );
271 $noreserves = 1;
272 $template->param( too_many_reserves => scalar(@reserves));
274 foreach my $res (@reserves) {
275 foreach my $biblionumber (@biblionumbers) {
276 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
277 # $template->param( message => 1 );
278 # $noreserves = 1;
279 # $template->param( already_reserved => 1 );
280 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
285 unless ($noreserves) {
286 $template->param( select_item_types => 1 );
292 # Build the template parameters that will show the info
293 # and items for each biblionumber.
296 my $notforloan_label_of = get_notforloan_label_of();
298 my $biblioLoop = [];
299 my $numBibsAvailable = 0;
300 my $itemdata_enumchron = 0;
301 my $anyholdable;
302 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
303 $template->param('item_level_itypes' => $itemLevelTypes);
305 foreach my $biblioNum (@biblionumbers) {
307 my $record = GetMarcBiblio($biblioNum);
308 # Init the bib item with the choices for branch pickup
309 my %biblioLoopIter = ( branchChoicesLoop => $CGIbranchloop );
311 # Get relevant biblio data.
312 my $biblioData = $biblioDataHash{$biblioNum};
313 if (! $biblioData) {
314 $template->param(message=>1, bad_biblionumber=>$biblioNum);
315 &get_out($query, $cookie, $template->output);
318 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
319 $biblioLoopIter{title} = $biblioData->{title};
320 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
321 $biblioLoopIter{author} = $biblioData->{author};
322 $biblioLoopIter{rank} = $biblioData->{rank};
323 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
324 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
326 if (!$itemLevelTypes && $biblioData->{itemtype}) {
327 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
328 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
331 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
332 $debug and warn $itemInfo->{'notforloan'};
334 # Get reserve fee.
335 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
336 ( $itemInfo->{'biblioitemnumber'} ) );
337 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
339 if ($itemLevelTypes && $itemInfo->{itype}) {
340 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
341 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
344 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
345 $biblioLoopIter{forloan} = 1;
349 $biblioLoopIter{itemLoop} = [];
350 my $numCopiesAvailable = 0;
351 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
352 my $itemNum = $itemInfo->{itemnumber};
353 my $itemLoopIter = {};
355 $itemLoopIter->{itemnumber} = $itemNum;
356 $itemLoopIter->{barcode} = $itemInfo->{barcode};
357 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
358 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
359 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
360 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
361 if ($itemLevelTypes) {
362 $itemLoopIter->{description} = $itemInfo->{description};
363 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
366 # If the holdingbranch is different than the homebranch, we show the
367 # holdingbranch of the document too.
368 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
369 $itemLoopIter->{holdingBranchName} =
370 $branches->{ $itemInfo->{holdingbranch} }{branchname};
373 # If the item is currently on loan, we display its return date and
374 # change the background color.
375 my $issues= GetItemIssue($itemNum);
376 if ( $issues->{'date_due'} ) {
377 $itemLoopIter->{dateDue} = format_date($issues->{'date_due'});
378 $itemLoopIter->{backgroundcolor} = 'onloan';
381 # checking reserve
382 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
383 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
385 if ( defined $reservedate ) {
386 $itemLoopIter->{backgroundcolor} = 'reserved';
387 $itemLoopIter->{reservedate} = format_date($reservedate);
388 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
389 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
390 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
391 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
394 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
395 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
397 # Management of the notforloan document
398 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
399 $itemLoopIter->{backgroundcolor} = 'other';
400 $itemLoopIter->{notforloanvalue} =
401 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
404 # Management of lost or long overdue items
405 if ( $itemInfo->{itemlost} ) {
407 # FIXME localized strings should never be in Perl code
408 $itemLoopIter->{message} =
409 $itemInfo->{itemlost} == 1 ? "(lost)"
410 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
411 : "";
412 $itemInfo->{backgroundcolor} = 'other';
415 # Check of the transfered documents
416 my ( $transfertwhen, $transfertfrom, $transfertto ) =
417 GetTransfers($itemNum);
418 if ( $transfertwhen && ($transfertwhen ne '') ) {
419 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
420 $itemLoopIter->{transfertfrom} =
421 $branches->{$transfertfrom}{branchname};
422 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
423 $itemLoopIter->{nocancel} = 1;
426 # If there is no loan, return and transfer, we show a checkbox.
427 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
429 my $branch = C4::Circulation::_GetCircControlBranch($itemLoopIter, $borr);
431 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
432 my $policy_holdallowed = 1;
434 if ( $branchitemrule->{'holdallowed'} == 0 ||
435 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
436 $policy_holdallowed = 0;
439 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum)) {
440 $itemLoopIter->{available} = 1;
441 $numCopiesAvailable++;
444 # FIXME: move this to a pm
445 my $dbh = C4::Context->dbh;
446 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
447 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
448 while (my $wait_hashref = $sth2->fetchrow_hashref) {
449 $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
451 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
453 # Show serial enumeration when needed
454 if ($itemLoopIter->{enumchron}) {
455 $itemdata_enumchron = 1;
457 $template->param( itemdata_enumchron => $itemdata_enumchron );
459 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
462 if ($numCopiesAvailable > 0) {
463 $numBibsAvailable++;
464 $biblioLoopIter{bib_available} = 1;
465 $biblioLoopIter{holdable} = 1;
466 $anyholdable = 1;
468 if ($biblioLoopIter{already_reserved}) {
469 $biblioLoopIter{holdable} = undef;
470 $anyholdable = undef;
472 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
473 $biblioLoopIter{holdable} = undef;
474 $anyholdable = undef;
477 push @$biblioLoop, \%biblioLoopIter;
480 if ( $numBibsAvailable == 0 || !$anyholdable) {
481 $template->param( none_available => 1 );
484 my $itemTableColspan = 7;
485 if (! $template->{VARS}->{'OPACItemHolds'}) {
486 $itemTableColspan--;
488 if (! $template->{VARS}->{'singleBranchMode'}) {
489 $itemTableColspan--;
491 $template->param(itemtable_colspan => $itemTableColspan);
493 # display infos
494 $template->param(bibitemloop => $biblioLoop);
495 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
496 # can set reserve date in future
497 if (
498 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
499 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
501 $template->param(
502 reserve_in_future => 1,
506 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
508 output_html_with_http_headers $query, $cookie, $template->output;