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
23 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 },
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');
92 $template->param( branch => $branch );
94 # make sure it's a real branch
95 if ( !$branch || !$branches->{$branch} ) {
98 $template->param( branchname => $branches->{$branch}->{'branchname'} );
100 # make branch selection options...
105 my @CGIbranchlooparray;
107 foreach my $branch ( keys %$branches ) {
110 $line{branch} = $branches->{$branch}->{'branchname'};
111 $line{value} = $branch;
112 if ($line{value} eq C4::Context->userenv->{'branch'}) {
115 push @CGIbranchlooparray, \%line;
118 @CGIbranchlooparray =
119 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
120 my $CGIbranchloop = \@CGIbranchlooparray;
121 $template->param( CGIbranch => $CGIbranchloop );
125 #output_html_with_http_headers($query,$cookie,"<html><head></head><body> @biblionumbers </body></html>\n");
136 # Build hashes of the requested biblio(item)s and items.
140 # Hash of biblionumber to biblio/biblioitems record.
143 # Hash of itemnumber to item info.
146 foreach my $biblioNumber (@biblionumbers) {
148 my $biblioData = GetBiblioData($biblioNumber);
149 $biblioDataHash{$biblioNumber} = $biblioData;
151 my @itemInfos = GetItemsInfo($biblioNumber);
152 $biblioData->{itemInfos} = \@itemInfos;
153 foreach my $itemInfo (@itemInfos) {
154 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
157 # Compute the priority rank.
158 my ( $rank, $reserves ) = GetReservesFromBiblionumber($biblioNumber);
159 $biblioData->{reservecount} = $rank;
160 foreach my $res (@$reserves) {
161 my $found = $res->{'found'};
162 if ( $found && ($found eq 'W') ) {
167 $biblioData->{rank} = $rank;
172 # If this is the second time through this script, it
173 # means we are carrying out the hold request, possibly
174 # with a specific item for each biblionumber.
177 if ( $query->param('place_reserve') ) {
179 my $notes = $query->param('notes');
181 # List is composed of alternating biblio/item/branch
182 my $selectedItems = $query->param('selecteditems');
184 if ($query->param('reserve_mode') eq 'single') {
185 # This indicates non-JavaScript mode, so there was
186 # only a single biblio number selected.
187 my $bib = $query->param('single_bib');
188 my $item = $query->param("checkitem_$bib");
189 if ($item eq 'any') {
192 my $branch = $query->param('branch');
193 $selectedItems = "$bib/$item/$branch/";
196 my @selectedItems = split /\//, $selectedItems;
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 $singleBranchMode = $template->param('singleBranchMode');
212 if ($singleBranchMode) {
213 $branch = $borr->{'branchcode'};
216 my $biblioData = $biblioDataHash{$biblioNum};
219 # If a specific item was selected and the pickup branch is the same as the
220 # holdingbranch, force the value $rank and $found.
221 my $rank = $biblioData->{rank};
223 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
224 my $item = GetItem($itemNum);
225 if ( $item->{'holdingbranch'} eq $branch ){
226 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
230 # Inserts a null into the 'itemnumber' field of 'reserves' table.
234 # Here we actually do the reserveration. Stage 3.
235 AddReserve($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $notes,
236 $biblioData->{'title'}, $itemNum, $found);
239 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
245 # Here we check that the borrower can actually make reserves Stage 1.
249 my $maxoutstanding = C4::Context->preference("maxoutstanding");
250 $template->param( noreserve => 1 ) unless $maxoutstanding;
251 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
252 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
253 $template->param( message => 1 );
255 $template->param( too_much_oweing => $amount );
257 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} eq 1) ) {
264 if ( $borr->{lost} && ($borr->{lost} eq 1) ) {
271 if ( $borr->{debarred} && ($borr->{debarred} eq 1) ) {
279 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
280 $template->param( RESERVES => \@reserves );
281 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
282 $template->param( message => 1 );
284 $template->param( too_many_reserves => scalar(@reserves));
286 foreach my $res (@reserves) {
287 foreach my $biblionumber (@biblionumbers) {
288 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
289 # $template->param( message => 1 );
291 # $template->param( already_reserved => 1 );
292 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
297 unless ($noreserves) {
298 $template->param( select_item_types => 1 );
304 # Build the template parameters that will show the info
305 # and items for each biblionumber.
308 my $notforloan_label_of = get_notforloan_label_of();
311 my $numBibsAvailable = 0;
312 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
313 $template->param('item-level_itypes' => $itemLevelTypes);
315 foreach my $biblioNum (@biblionumbers) {
318 # Init the bib item with the choices for branch pickup
319 my %biblioLoopIter = ( branchChoicesLoop => $CGIbranchloop );
321 # Get relevant biblio data.
322 my $biblioData = $biblioDataHash{$biblioNum};
324 $template->param(message=>1, bad_biblionumber=>$biblioNum);
325 &get_out($query, $cookie, $template->output);
328 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
329 $biblioLoopIter{title} = $biblioData->{title};
330 $biblioLoopIter{author} = $biblioData->{author};
331 $biblioLoopIter{rank} = $biblioData->{rank};
332 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
333 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
335 if (!$itemLevelTypes && $biblioData->{itemtype}) {
336 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
337 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
340 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
341 $debug and warn $itemInfo->{'notforloan'};
344 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
345 ( $itemInfo->{'biblioitemnumber'} ) );
346 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
348 if ($itemLevelTypes && $itemInfo->{itype}) {
349 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
350 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
353 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
354 $biblioLoopIter{forloan} = 1;
358 $biblioLoopIter{itemTypeDescription} =
359 $itemTypes->{$biblioData->{itemtype}}{description};
362 $biblioLoopIter{itemLoop} = [];
363 my $numCopiesAvailable = 0;
364 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
365 my $itemNum = $itemInfo->{itemnumber};
366 my $itemLoopIter = {};
368 $itemLoopIter->{itemnumber} = $itemNum;
369 $itemLoopIter->{barcode} = $itemInfo->{barcode};
370 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
371 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
373 if ($itemLevelTypes) {
374 $itemLoopIter->{description} = $itemInfo->{description};
375 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
378 # If the holdingbranch is different than the homebranch, we show the
379 # holdingbranch of the document too.
380 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
381 $itemLoopIter->{holdingBranchName} =
382 $branches->{ $itemInfo->{holdingbranch} }{branchname};
385 # If the item is currently on loan, we display its return date and
386 # change the background color.
387 my $issues= GetItemIssue($itemNum);
388 if ( $issues->{'date_due'} ) {
389 $itemLoopIter->{dateDue} = format_date($issues->{'date_due'});
390 $itemLoopIter->{backgroundcolor} = 'onloan';
394 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
395 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
397 if ( defined $reservedate ) {
398 $itemLoopIter->{backgroundcolor} = 'reserved';
399 $itemLoopIter->{reservedate} = format_date($reservedate);
400 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
401 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
402 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
403 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
406 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
407 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
409 # Management of the notforloan document
410 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
411 $itemLoopIter->{backgroundcolor} = 'other';
412 $itemLoopIter->{notforloanvalue} =
413 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
416 # Management of lost or long overdue items
417 if ( $itemInfo->{itemlost} ) {
419 # FIXME localized strings should never be in Perl code
420 $itemLoopIter->{message} =
421 $itemInfo->{itemlost} == 1 ? "(lost)"
422 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
424 $itemInfo->{backgroundcolor} = 'other';
427 # Check of the transfered documents
428 my ( $transfertwhen, $transfertfrom, $transfertto ) =
429 GetTransfers($itemNum);
430 if ( $transfertwhen && ($transfertwhen ne '') ) {
431 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
432 $itemLoopIter->{transfertfrom} =
433 $branches->{$transfertfrom}{branchname};
434 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
435 $itemLoopIter->{nocancel} = 1;
438 # If there is no loan, return and transfer, we show a checkbox.
439 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
441 my $branchitemrule = GetBranchItemRule( $borr->{'branchcode'}, $itemInfo->{'itype'} );
442 my $policy_holdallowed = 1;
444 if ( $branchitemrule->{'holdallowed'} == 0 ||
445 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
446 $policy_holdallowed = 0;
449 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed) {
450 $itemLoopIter->{available} = 1;
451 $numCopiesAvailable++;
454 # FIXME: move this to a pm
455 my $dbh = C4::Context->dbh;
456 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
457 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
458 while (my $wait_hashref = $sth2->fetchrow_hashref) {
459 $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
461 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
464 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
467 if ($numCopiesAvailable > 0) {
469 $biblioLoopIter{bib_available} = 1;
470 $biblioLoopIter{holdable} = 1;
472 if ($biblioLoopIter{already_reserved}) {
473 $biblioLoopIter{holdable} = undef;
476 push @$biblioLoop, \%biblioLoopIter;
480 if ( $numBibsAvailable == 0 ) {
481 $template->param( none_available => 1, message => 1 );
485 my $itemTableColspan = 5;
486 if (!$template->param('OPACItemHolds')) {
489 if ($template->param('singleBranchMode')) {
492 $template->param(itemtable_colspan => $itemTableColspan);
497 $template->param(bibitemloop => $biblioLoop);
498 output_html_with_http_headers $query, $cookie, $template->output;