Bug 929 : Last follow up, implementing the last of Katrins suggestions
[koha.git] / opac / opac-reserve.pl
blobb2144a9192a6fb038da3423e940ef9be520f8e00
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::Overdues;
33 use C4::Debug;
34 # use Data::Dumper;
36 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
38 my $query = new CGI;
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41 template_name => "opac-reserve.tmpl",
42 query => $query,
43 type => "opac",
44 authnotrequired => 0,
45 flagsrequired => { borrow => 1 },
46 debug => 1,
49 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
50 sub get_out ($$$) {
51 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
52 exit;
55 # get borrower information ....
56 my ( $borr ) = GetMemberDetails( $borrowernumber );
58 # Pass through any reserve charge
59 if ($borr->{reservefee} > 0){
60 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
62 # get branches and itemtypes
63 my $branches = GetBranches();
64 my $itemTypes = GetItemTypes();
66 # There are two ways of calling this script, with a single biblio num
67 # or multiple biblio nums.
68 my $biblionumbers = $query->param('biblionumbers');
69 my $reserveMode = $query->param('reserve_mode');
70 if ($reserveMode && ($reserveMode eq 'single')) {
71 my $bib = $query->param('single_bib');
72 $biblionumbers = "$bib/";
74 if (! $biblionumbers) {
75 $biblionumbers = $query->param('biblionumber');
78 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
79 $template->param(message=>1, no_biblionumber=>1);
80 &get_out($query, $cookie, $template->output);
83 # Pass the numbers to the page so they can be fed back
84 # when the hold is confirmed. TODO: Not necessary?
85 $template->param( biblionumbers => $biblionumbers );
87 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
88 my @biblionumbers = split /\//, $biblionumbers;
89 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
90 # TODO: New message?
91 $template->param(message=>1, no_biblionumber=>1);
92 &get_out($query, $cookie, $template->output);
95 # pass the pickup branch along....
96 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
97 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
98 $template->param( branch => $branch );
100 # make branch selection options...
101 my $CGIbranchloop = GetBranchesLoop($branch);
102 $template->param( CGIbranch => $CGIbranchloop );
104 # Is the person allowed to choose their branch
105 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
107 $template->param( choose_branch => $OPACChooseBranch);
111 # Build hashes of the requested biblio(item)s and items.
115 # Hash of biblionumber to biblio/biblioitems record.
116 my %biblioDataHash;
118 # Hash of itemnumber to item info.
119 my %itemInfoHash;
121 foreach my $biblioNumber (@biblionumbers) {
123 my $biblioData = GetBiblioData($biblioNumber);
124 $biblioDataHash{$biblioNumber} = $biblioData;
126 my @itemInfos = GetItemsInfo($biblioNumber);
128 my $marcrecord= GetMarcBiblio($biblioNumber);
130 # flag indicating existence of at least one item linked via a host record
131 my $hostitemsflag;
132 # adding items linked via host biblios
133 my @hostitemInfos = GetHostItemsInfo($marcrecord);
134 if (@hostitemInfos){
135 $hostitemsflag =1;
136 push (@itemInfos,@hostitemInfos);
141 $biblioData->{itemInfos} = \@itemInfos;
142 foreach my $itemInfo (@itemInfos) {
143 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
146 # Compute the priority rank.
147 my ( $rank, $reserves ) = GetReservesFromBiblionumber($biblioNumber,1);
148 $biblioData->{reservecount} = $rank;
149 foreach my $res (@$reserves) {
150 my $found = $res->{'found'};
151 if ( $found && ($found eq 'W') ) {
152 $rank--;
155 $rank++;
156 $biblioData->{rank} = $rank;
161 # If this is the second time through this script, it
162 # means we are carrying out the hold request, possibly
163 # with a specific item for each biblionumber.
166 if ( $query->param('place_reserve') ) {
167 my $notes = $query->param('notes');
168 my $canreserve=0;
170 # List is composed of alternating biblio/item/branch
171 my $selectedItems = $query->param('selecteditems');
173 if ($query->param('reserve_mode') eq 'single') {
174 # This indicates non-JavaScript mode, so there was
175 # only a single biblio number selected.
176 my $bib = $query->param('single_bib');
177 my $item = $query->param("checkitem_$bib");
178 if ($item eq 'any') {
179 $item = '';
181 my $branch = $query->param('branch');
182 $selectedItems = "$bib/$item/$branch/";
185 $selectedItems =~ s!/$!!;
186 my @selectedItems = split /\//, $selectedItems, -1;
188 # Make sure there is a biblionum/itemnum/branch triplet for each item.
189 # The itemnum can be 'any', meaning next available.
190 my $selectionCount = @selectedItems;
191 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
192 $template->param(message=>1, bad_data=>1);
193 &get_out($query, $cookie, $template->output);
196 while (@selectedItems) {
197 my $biblioNum = shift(@selectedItems);
198 my $itemNum = shift(@selectedItems);
199 my $branch = shift(@selectedItems); # i.e., branch code, not name
201 my $singleBranchMode = $template->param('singleBranchMode');
202 if ($singleBranchMode || ! $OPACChooseBranch) { # single branch mode or disabled user choosing
203 $branch = $borr->{'branchcode'};
206 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
207 if ($itemNum ne '') {
208 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
209 if ($hostbiblioNum ne $biblioNum) {
210 $biblioNum = $hostbiblioNum;
214 my $biblioData = $biblioDataHash{$biblioNum};
215 my $found;
217 # Check for user supplied reserve date
218 my $startdate;
219 if (
220 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
221 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
223 $startdate = $query->param("reserve_date_$biblioNum");
226 my $expiration_date = $query->param("expiration_date_$biblioNum");
228 # If a specific item was selected and the pickup branch is the same as the
229 # holdingbranch, force the value $rank and $found.
230 my $rank = $biblioData->{rank};
231 if ($itemNum ne ''){
232 $canreserve = 1 if CanItemBeReserved($borrowernumber,$itemNum);
233 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
234 my $item = GetItem($itemNum);
235 if ( $item->{'holdingbranch'} eq $branch ){
236 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
239 else {
240 $canreserve = 1 if CanBookBeReserved($borrowernumber,$biblioNum);
241 # Inserts a null into the 'itemnumber' field of 'reserves' table.
242 $itemNum = undef;
245 # Here we actually do the reserveration. Stage 3.
246 AddReserve($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $startdate, $expiration_date, $notes,
247 $biblioData->{'title'}, $itemNum, $found) if ($canreserve);
250 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
251 exit;
256 # Here we check that the borrower can actually make reserves Stage 1.
259 my $noreserves = 0;
260 my $maxoutstanding = C4::Context->preference("maxoutstanding");
261 $template->param( noreserve => 1 ) unless $maxoutstanding;
262 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
263 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
264 $template->param( message => 1 );
265 $noreserves = 1;
266 $template->param( too_much_oweing => $amount );
268 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} eq 1) ) {
269 $noreserves = 1;
270 $template->param(
271 message => 1,
272 GNA => 1
275 if ( $borr->{lost} && ($borr->{lost} eq 1) ) {
276 $noreserves = 1;
277 $template->param(
278 message => 1,
279 lost => 1
282 if ( CheckBorrowerDebarred($borrowernumber) ) {
283 $noreserves = 1;
284 $template->param(
285 message => 1,
286 debarred => 1
290 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
291 $template->param( RESERVES => \@reserves );
292 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
293 $template->param( message => 1 );
294 $noreserves = 1;
295 $template->param( too_many_reserves => scalar(@reserves));
297 foreach my $res (@reserves) {
298 foreach my $biblionumber (@biblionumbers) {
299 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
300 # $template->param( message => 1 );
301 # $noreserves = 1;
302 # $template->param( already_reserved => 1 );
303 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
308 unless ($noreserves) {
309 $template->param( select_item_types => 1 );
315 # Build the template parameters that will show the info
316 # and items for each biblionumber.
319 my $notforloan_label_of = get_notforloan_label_of();
321 my $biblioLoop = [];
322 my $numBibsAvailable = 0;
323 my $itemdata_enumchron = 0;
324 my $anyholdable;
325 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
326 $template->param('item_level_itypes' => $itemLevelTypes);
328 foreach my $biblioNum (@biblionumbers) {
330 my $record = GetMarcBiblio($biblioNum);
331 # Init the bib item with the choices for branch pickup
332 my %biblioLoopIter = ( branchChoicesLoop => $CGIbranchloop );
334 # Get relevant biblio data.
335 my $biblioData = $biblioDataHash{$biblioNum};
336 if (! $biblioData) {
337 $template->param(message=>1, bad_biblionumber=>$biblioNum);
338 &get_out($query, $cookie, $template->output);
341 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
342 $biblioLoopIter{title} = $biblioData->{title};
343 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
344 $biblioLoopIter{author} = $biblioData->{author};
345 $biblioLoopIter{rank} = $biblioData->{rank};
346 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
347 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
349 if (!$itemLevelTypes && $biblioData->{itemtype}) {
350 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
351 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
354 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
355 $debug and warn $itemInfo->{'notforloan'};
357 # Get reserve fee.
358 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
359 ( $itemInfo->{'biblioitemnumber'} ) );
360 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
362 if ($itemLevelTypes && $itemInfo->{itype}) {
363 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
364 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
367 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
368 $biblioLoopIter{forloan} = 1;
372 $biblioLoopIter{itemLoop} = [];
373 my $numCopiesAvailable = 0;
374 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
375 my $itemNum = $itemInfo->{itemnumber};
376 my $itemLoopIter = {};
378 $itemLoopIter->{itemnumber} = $itemNum;
379 $itemLoopIter->{barcode} = $itemInfo->{barcode};
380 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
381 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
382 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
383 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
384 if ($itemLevelTypes) {
385 $itemLoopIter->{description} = $itemInfo->{description};
386 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
389 # If the holdingbranch is different than the homebranch, we show the
390 # holdingbranch of the document too.
391 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
392 $itemLoopIter->{holdingBranchName} =
393 $branches->{ $itemInfo->{holdingbranch} }{branchname};
396 # If the item is currently on loan, we display its return date and
397 # change the background color.
398 my $issues= GetItemIssue($itemNum);
399 if ( $issues->{'date_due'} ) {
400 $itemLoopIter->{dateDue} = format_date($issues->{'date_due'});
401 $itemLoopIter->{backgroundcolor} = 'onloan';
404 # checking reserve
405 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
406 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
408 # the item could be reserved for this borrower vi a host record, flag this
409 if ($reservedfor eq $borrowernumber){
410 $itemLoopIter->{already_reserved} = 1;
413 if ( defined $reservedate ) {
414 $itemLoopIter->{backgroundcolor} = 'reserved';
415 $itemLoopIter->{reservedate} = format_date($reservedate);
416 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
417 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
418 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
419 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
422 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
423 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
425 # Management of the notforloan document
426 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
427 $itemLoopIter->{backgroundcolor} = 'other';
428 $itemLoopIter->{notforloanvalue} =
429 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
432 # Management of lost or long overdue items
433 if ( $itemInfo->{itemlost} ) {
435 # FIXME localized strings should never be in Perl code
436 $itemLoopIter->{message} =
437 $itemInfo->{itemlost} == 1 ? "(lost)"
438 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
439 : "";
440 $itemInfo->{backgroundcolor} = 'other';
443 # Check of the transfered documents
444 my ( $transfertwhen, $transfertfrom, $transfertto ) =
445 GetTransfers($itemNum);
446 if ( $transfertwhen && ($transfertwhen ne '') ) {
447 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
448 $itemLoopIter->{transfertfrom} =
449 $branches->{$transfertfrom}{branchname};
450 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
451 $itemLoopIter->{nocancel} = 1;
454 # if the items belongs to a host record, show link to host record
455 if ($itemInfo->{biblionumber} ne $biblioNum){
456 $biblioLoopIter{hostitemsflag} = 1;
457 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
458 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
461 # If there is no loan, return and transfer, we show a checkbox.
462 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
464 my $branch = C4::Circulation::_GetCircControlBranch($itemLoopIter, $borr);
466 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
467 my $policy_holdallowed = 1;
469 if ( $branchitemrule->{'holdallowed'} == 0 ||
470 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
471 $policy_holdallowed = 0;
474 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum) and ($itemLoopIter->{already_reserved} ne 1)) {
475 $itemLoopIter->{available} = 1;
476 $numCopiesAvailable++;
479 # FIXME: move this to a pm
480 my $dbh = C4::Context->dbh;
481 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
482 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
483 while (my $wait_hashref = $sth2->fetchrow_hashref) {
484 $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
486 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
488 # Show serial enumeration when needed
489 if ($itemLoopIter->{enumchron}) {
490 $itemdata_enumchron = 1;
493 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
495 $template->param( itemdata_enumchron => $itemdata_enumchron );
497 if ($numCopiesAvailable > 0) {
498 $numBibsAvailable++;
499 $biblioLoopIter{bib_available} = 1;
500 $biblioLoopIter{holdable} = 1;
501 $anyholdable = 1;
503 if ($biblioLoopIter{already_reserved}) {
504 $biblioLoopIter{holdable} = undef;
505 $anyholdable = undef;
507 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
508 $biblioLoopIter{holdable} = undef;
509 $anyholdable = undef;
512 push @$biblioLoop, \%biblioLoopIter;
515 if ( $numBibsAvailable == 0 || !$anyholdable) {
516 $template->param( none_available => 1 );
519 my $itemTableColspan = 7;
520 if (! $template->{VARS}->{'OPACItemHolds'}) {
521 $itemTableColspan--;
523 if (! $template->{VARS}->{'singleBranchMode'}) {
524 $itemTableColspan--;
526 $template->param(itemtable_colspan => $itemTableColspan);
528 # display infos
529 $template->param(bibitemloop => $biblioLoop);
530 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
531 # can set reserve date in future
532 if (
533 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
534 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
536 $template->param(
537 reserve_in_future => 1,
541 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
543 output_html_with_http_headers $query, $cookie, $template->output;