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
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
21 use C4::Auth; # checkauth, getborrowernumber.
28 use C4::Dates qw/format_date/;
31 use C4::Branch; # GetBranches
35 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "opac-reserve.tmpl",
44 flagsrequired => { borrow => 1 },
48 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
50 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
54 # get borrower information ....
55 my ( $borr ) = GetMemberDetails( $borrowernumber );
57 # get branches and itemtypes
58 my $branches = GetBranches();
59 my $itemTypes = GetItemTypes();
61 # There are two ways of calling this script, with a single biblio num
62 # or multiple biblio nums.
63 my $biblionumbers = $query->param('biblionumbers');
64 my $reserveMode = $query->param('reserve_mode');
65 if ($reserveMode && ($reserveMode eq 'single')) {
66 my $bib = $query->param('single_bib');
67 $biblionumbers = "$bib/";
69 if (! $biblionumbers) {
70 $biblionumbers = $query->param('biblionumber');
73 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
74 $template->param(message=>1, no_biblionumber=>1);
75 &get_out($query, $cookie, $template->output);
78 # Pass the numbers to the page so they can be fed back
79 # when the hold is confirmed. TODO: Not necessary?
80 $template->param( biblionumbers => $biblionumbers );
82 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
83 my @biblionumbers = split /\//, $biblionumbers;
84 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
86 $template->param(message=>1, no_biblionumber=>1);
87 &get_out($query, $cookie, $template->output);
90 # pass the pickup branch along....
91 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
92 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
93 $template->param( branch => $branch );
95 # make branch selection options...
96 my $CGIbranchloop = GetBranchesLoop($branch);
97 $template->param( CGIbranch => $CGIbranchloop );
99 # Is the person allowed to choose their branch
100 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
102 $template->param( choose_branch => $OPACChooseBranch);
106 # Build hashes of the requested biblio(item)s and items.
110 # Hash of biblionumber to biblio/biblioitems record.
113 # Hash of itemnumber to item info.
116 foreach my $biblioNumber (@biblionumbers) {
118 my $biblioData = GetBiblioData($biblioNumber);
119 $biblioDataHash{$biblioNumber} = $biblioData;
121 my @itemInfos = GetItemsInfo($biblioNumber);
122 $biblioData->{itemInfos} = \@itemInfos;
123 foreach my $itemInfo (@itemInfos) {
124 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
127 # Compute the priority rank.
128 my ( $rank, $reserves ) = GetReservesFromBiblionumber($biblioNumber,1);
129 $biblioData->{reservecount} = $rank;
130 foreach my $res (@$reserves) {
131 my $found = $res->{'found'};
132 if ( $found && ($found eq 'W') ) {
137 $biblioData->{rank} = $rank;
142 # If this is the second time through this script, it
143 # means we are carrying out the hold request, possibly
144 # with a specific item for each biblionumber.
147 if ( $query->param('place_reserve') ) {
148 my $notes = $query->param('notes');
151 # List is composed of alternating biblio/item/branch
152 my $selectedItems = $query->param('selecteditems');
154 if ($query->param('reserve_mode') eq 'single') {
155 # This indicates non-JavaScript mode, so there was
156 # only a single biblio number selected.
157 my $bib = $query->param('single_bib');
158 my $item = $query->param("checkitem_$bib");
159 if ($item eq 'any') {
162 my $branch = $query->param('branch');
163 $selectedItems = "$bib/$item/$branch/";
166 $selectedItems =~ s!/$!!;
167 my @selectedItems = split /\//, $selectedItems, -1;
169 # Make sure there is a biblionum/itemnum/branch triplet for each item.
170 # The itemnum can be 'any', meaning next available.
171 my $selectionCount = @selectedItems;
172 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
173 $template->param(message=>1, bad_data=>1);
174 &get_out($query, $cookie, $template->output);
177 while (@selectedItems) {
178 my $biblioNum = shift(@selectedItems);
179 my $itemNum = shift(@selectedItems);
180 my $branch = shift(@selectedItems); # i.e., branch code, not name
182 my $singleBranchMode = $template->param('singleBranchMode');
183 if ($singleBranchMode || ! $OPACChooseBranch) { # single branch mode or disabled user choosing
184 $branch = $borr->{'branchcode'};
187 my $biblioData = $biblioDataHash{$biblioNum};
190 # Check for user supplied reserve date
193 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
194 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
196 $startdate = $query->param("reserve_date_$biblioNum");
199 my $expiration_date = $query->param("expiration_date_$biblioNum");
201 # If a specific item was selected and the pickup branch is the same as the
202 # holdingbranch, force the value $rank and $found.
203 my $rank = $biblioData->{rank};
205 $canreserve = 1 if CanItemBeReserved($borrowernumber,$itemNum);
206 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
207 my $item = GetItem($itemNum);
208 if ( $item->{'holdingbranch'} eq $branch ){
209 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
213 $canreserve = 1 if CanBookBeReserved($borrowernumber,$biblioNum);
214 # Inserts a null into the 'itemnumber' field of 'reserves' table.
218 # Here we actually do the reserveration. Stage 3.
219 AddReserve($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $startdate, $expiration_date, $notes,
220 $biblioData->{'title'}, $itemNum, $found) if ($canreserve);
223 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
229 # Here we check that the borrower can actually make reserves Stage 1.
233 my $maxoutstanding = C4::Context->preference("maxoutstanding");
234 $template->param( noreserve => 1 ) unless $maxoutstanding;
235 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
236 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
237 $template->param( message => 1 );
239 $template->param( too_much_oweing => $amount );
241 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} eq 1) ) {
248 if ( $borr->{lost} && ($borr->{lost} eq 1) ) {
255 if ( $borr->{debarred} && ($borr->{debarred} eq 1) ) {
263 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
264 $template->param( RESERVES => \@reserves );
265 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
266 $template->param( message => 1 );
268 $template->param( too_many_reserves => scalar(@reserves));
270 foreach my $res (@reserves) {
271 foreach my $biblionumber (@biblionumbers) {
272 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
273 # $template->param( message => 1 );
275 # $template->param( already_reserved => 1 );
276 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
281 unless ($noreserves) {
282 $template->param( select_item_types => 1 );
288 # Build the template parameters that will show the info
289 # and items for each biblionumber.
292 my $notforloan_label_of = get_notforloan_label_of();
295 my $numBibsAvailable = 0;
296 my $itemdata_enumchron = 0;
298 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
299 $template->param('item_level_itypes' => $itemLevelTypes);
301 foreach my $biblioNum (@biblionumbers) {
303 my $record = GetMarcBiblio($biblioNum);
304 # Init the bib item with the choices for branch pickup
305 my %biblioLoopIter = ( branchChoicesLoop => $CGIbranchloop );
307 # Get relevant biblio data.
308 my $biblioData = $biblioDataHash{$biblioNum};
310 $template->param(message=>1, bad_biblionumber=>$biblioNum);
311 &get_out($query, $cookie, $template->output);
314 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
315 $biblioLoopIter{title} = $biblioData->{title};
316 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
317 $biblioLoopIter{author} = $biblioData->{author};
318 $biblioLoopIter{rank} = $biblioData->{rank};
319 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
320 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
322 if (!$itemLevelTypes && $biblioData->{itemtype}) {
323 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
324 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
327 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
328 $debug and warn $itemInfo->{'notforloan'};
331 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
332 ( $itemInfo->{'biblioitemnumber'} ) );
333 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
335 if ($itemLevelTypes && $itemInfo->{itype}) {
336 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
337 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
340 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
341 $biblioLoopIter{forloan} = 1;
345 $biblioLoopIter{itemLoop} = [];
346 my $numCopiesAvailable = 0;
347 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
348 my $itemNum = $itemInfo->{itemnumber};
349 my $itemLoopIter = {};
351 $itemLoopIter->{itemnumber} = $itemNum;
352 $itemLoopIter->{barcode} = $itemInfo->{barcode};
353 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
354 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
355 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
356 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
357 if ($itemLevelTypes) {
358 $itemLoopIter->{description} = $itemInfo->{description};
359 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
362 # If the holdingbranch is different than the homebranch, we show the
363 # holdingbranch of the document too.
364 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
365 $itemLoopIter->{holdingBranchName} =
366 $branches->{ $itemInfo->{holdingbranch} }{branchname};
369 # If the item is currently on loan, we display its return date and
370 # change the background color.
371 my $issues= GetItemIssue($itemNum);
372 if ( $issues->{'date_due'} ) {
373 $itemLoopIter->{dateDue} = format_date($issues->{'date_due'});
374 $itemLoopIter->{backgroundcolor} = 'onloan';
378 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
379 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
381 if ( defined $reservedate ) {
382 $itemLoopIter->{backgroundcolor} = 'reserved';
383 $itemLoopIter->{reservedate} = format_date($reservedate);
384 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
385 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
386 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
387 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
390 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
391 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
393 # Management of the notforloan document
394 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
395 $itemLoopIter->{backgroundcolor} = 'other';
396 $itemLoopIter->{notforloanvalue} =
397 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
400 # Management of lost or long overdue items
401 if ( $itemInfo->{itemlost} ) {
403 # FIXME localized strings should never be in Perl code
404 $itemLoopIter->{message} =
405 $itemInfo->{itemlost} == 1 ? "(lost)"
406 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
408 $itemInfo->{backgroundcolor} = 'other';
411 # Check of the transfered documents
412 my ( $transfertwhen, $transfertfrom, $transfertto ) =
413 GetTransfers($itemNum);
414 if ( $transfertwhen && ($transfertwhen ne '') ) {
415 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
416 $itemLoopIter->{transfertfrom} =
417 $branches->{$transfertfrom}{branchname};
418 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
419 $itemLoopIter->{nocancel} = 1;
422 # If there is no loan, return and transfer, we show a checkbox.
423 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
425 my $branch = C4::Circulation::_GetCircControlBranch($itemLoopIter, $borr);
427 my $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
428 my $policy_holdallowed = 1;
430 if ( $branchitemrule->{'holdallowed'} == 0 ||
431 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
432 $policy_holdallowed = 0;
435 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum)) {
436 $itemLoopIter->{available} = 1;
437 $numCopiesAvailable++;
440 # FIXME: move this to a pm
441 my $dbh = C4::Context->dbh;
442 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
443 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
444 while (my $wait_hashref = $sth2->fetchrow_hashref) {
445 $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
447 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
449 # Show serial enumeration when needed
450 if ($itemLoopIter->{enumchron}) {
451 $itemdata_enumchron = 1;
453 $template->param( itemdata_enumchron => $itemdata_enumchron );
455 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
458 if ($numCopiesAvailable > 0) {
460 $biblioLoopIter{bib_available} = 1;
461 $biblioLoopIter{holdable} = 1;
464 if ($biblioLoopIter{already_reserved}) {
465 $biblioLoopIter{holdable} = undef;
466 $anyholdable = undef;
468 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
469 $biblioLoopIter{holdable} = undef;
470 $anyholdable = undef;
473 push @$biblioLoop, \%biblioLoopIter;
476 if ( $numBibsAvailable == 0 || !$anyholdable) {
477 $template->param( none_available => 1 );
480 my $itemTableColspan = 5;
481 if (!$template->param('OPACItemHolds')) {
484 if ($template->param('singleBranchMode')) {
487 $template->param(itemtable_colspan => $itemTableColspan);
490 $template->param(bibitemloop => $biblioLoop);
491 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
492 # can set reserve date in future
494 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
495 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
498 reserve_in_future => 1,
502 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
504 output_html_with_http_headers $query, $cookie, $template->output;