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') || 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 );
100 #output_html_with_http_headers($query,$cookie,"<html><head></head><body> @biblionumbers </body></html>\n");
111 # Build hashes of the requested biblio(item)s and items.
115 # Hash of biblionumber to biblio/biblioitems record.
118 # Hash of itemnumber to item info.
121 foreach my $biblioNumber (@biblionumbers) {
123 my $biblioData = GetBiblioData
($biblioNumber);
124 $biblioDataHash{$biblioNumber} = $biblioData;
126 my @itemInfos = GetItemsInfo
($biblioNumber);
127 $biblioData->{itemInfos
} = \
@itemInfos;
128 foreach my $itemInfo (@itemInfos) {
129 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
132 # Compute the priority rank.
133 my ( $rank, $reserves ) = GetReservesFromBiblionumber
($biblioNumber,1);
134 $biblioData->{reservecount
} = $rank;
135 foreach my $res (@
$reserves) {
136 my $found = $res->{'found'};
137 if ( $found && ($found eq 'W') ) {
142 $biblioData->{rank
} = $rank;
147 # If this is the second time through this script, it
148 # means we are carrying out the hold request, possibly
149 # with a specific item for each biblionumber.
152 if ( $query->param('place_reserve') ) {
153 my $notes = $query->param('notes');
156 # List is composed of alternating biblio/item/branch
157 my $selectedItems = $query->param('selecteditems');
159 if ($query->param('reserve_mode') eq 'single') {
160 # This indicates non-JavaScript mode, so there was
161 # only a single biblio number selected.
162 my $bib = $query->param('single_bib');
163 my $item = $query->param("checkitem_$bib");
164 if ($item eq 'any') {
167 my $branch = $query->param('branch');
168 $selectedItems = "$bib/$item/$branch/";
171 my @selectedItems = split /\//, $selectedItems;
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) {
188 $branch = $borr->{'branchcode'};
191 my $biblioData = $biblioDataHash{$biblioNum};
194 # Check for user supplied reserve date
197 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
198 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
200 $startdate = $query->param("reserve_date_$biblioNum");
203 # If a specific item was selected and the pickup branch is the same as the
204 # holdingbranch, force the value $rank and $found.
205 my $rank = $biblioData->{rank
};
207 $canreserve = 1 if CanItemBeReserved
($borrowernumber,$itemNum);
208 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
209 my $item = GetItem
($itemNum);
210 if ( $item->{'holdingbranch'} eq $branch ){
211 $found = 'W' unless C4
::Context
->preference('ReservesNeedReturns');
215 $canreserve = 1 if CanBookBeReserved
($borrowernumber,$biblioNum);
216 # Inserts a null into the 'itemnumber' field of 'reserves' table.
220 # Here we actually do the reserveration. Stage 3.
221 AddReserve
($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $startdate, $notes,
222 $biblioData->{'title'}, $itemNum, $found) if ($canreserve);
225 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
231 # Here we check that the borrower can actually make reserves Stage 1.
235 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
236 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
237 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
238 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
239 $template->param( message
=> 1 );
241 $template->param( too_much_oweing
=> $amount );
243 if ( $borr->{gonenoaddress
} && ($borr->{gonenoaddress
} eq 1) ) {
250 if ( $borr->{lost
} && ($borr->{lost
} eq 1) ) {
257 if ( $borr->{debarred
} && ($borr->{debarred
} eq 1) ) {
265 my @reserves = GetReservesFromBorrowernumber
( $borrowernumber );
266 $template->param( RESERVES
=> \
@reserves );
267 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
268 $template->param( message
=> 1 );
270 $template->param( too_many_reserves
=> scalar(@reserves));
272 foreach my $res (@reserves) {
273 foreach my $biblionumber (@biblionumbers) {
274 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
275 # $template->param( message => 1 );
277 # $template->param( already_reserved => 1 );
278 $biblioDataHash{$biblionumber}->{already_reserved
} = 1;
283 unless ($noreserves) {
284 $template->param( select_item_types
=> 1 );
290 # Build the template parameters that will show the info
291 # and items for each biblionumber.
294 my $notforloan_label_of = get_notforloan_label_of
();
297 my $numBibsAvailable = 0;
298 my $itemdata_enumchron = 0;
299 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
300 $template->param('item-level_itypes' => $itemLevelTypes);
302 foreach my $biblioNum (@biblionumbers) {
304 my $record = GetMarcBiblio
($biblioNum);
305 my $subtitle = C4
::Biblio
::get_koha_field_from_marc
('bibliosubtitle', 'subtitle', $record, '');
306 # Init the bib item with the choices for branch pickup
307 my %biblioLoopIter = ( branchChoicesLoop
=> $CGIbranchloop );
309 # Get relevant biblio data.
310 my $biblioData = $biblioDataHash{$biblioNum};
312 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
313 &get_out
($query, $cookie, $template->output);
316 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
317 $biblioLoopIter{title
} = $biblioData->{title
};
318 $biblioLoopIter{subtitle
} = $subtitle;
319 $biblioLoopIter{author
} = $biblioData->{author
};
320 $biblioLoopIter{rank
} = $biblioData->{rank
};
321 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
322 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
324 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
325 $biblioLoopIter{description
} = $itemTypes->{$biblioData->{itemtype
}}{description
};
326 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$biblioData->{itemtype
}}{imageurl
};
329 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
330 $debug and warn $itemInfo->{'notforloan'};
333 my $fee = GetReserveFee
(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
334 ( $itemInfo->{'biblioitemnumber'} ) );
335 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ?
$fee : 0.0);
337 if ($itemLevelTypes && $itemInfo->{itype
}) {
338 $itemInfo->{description
} = $itemTypes->{$itemInfo->{itype
}}{description
};
339 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$itemInfo->{itype
}}{imageurl
};
342 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
343 $biblioLoopIter{forloan
} = 1;
347 $biblioLoopIter{itemTypeDescription
} = $itemTypes->{$biblioData->{itemtype
}}{description
};
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';
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)"
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 $branchitemrule = GetBranchItemRule
( $borr->{'branchcode'}, $itemInfo->{'itype'} );
430 my $policy_holdallowed = 1;
432 if ( $branchitemrule->{'holdallowed'} == 0 ||
433 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
434 $policy_holdallowed = 0;
437 if (IsAvailableForItemLevelRequest
($itemNum) and $policy_holdallowed and CanItemBeReserved
($borrowernumber,$itemNum)) {
438 $itemLoopIter->{available
} = 1;
439 $numCopiesAvailable++;
442 # FIXME: move this to a pm
443 my $dbh = C4
::Context
->dbh;
444 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
445 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber
}, $itemNum);
446 while (my $wait_hashref = $sth2->fetchrow_hashref) {
447 $itemLoopIter->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
449 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemTypes->{ $itemInfo->{itype
} }{imageurl
} );
451 # Show serial enumeration when needed
452 if ($itemLoopIter->{enumchron
}) {
453 $itemdata_enumchron = 1;
455 $template->param( itemdata_enumchron
=> $itemdata_enumchron );
457 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
460 if ($numCopiesAvailable > 0) {
462 $biblioLoopIter{bib_available
} = 1;
463 $biblioLoopIter{holdable
} = 1;
465 if ($biblioLoopIter{already_reserved
}) {
466 $biblioLoopIter{holdable
} = undef;
468 if(not CanBookBeReserved
($borrowernumber,$biblioNum)){
469 $biblioLoopIter{holdable
} = undef;
472 push @
$biblioLoop, \
%biblioLoopIter;
475 if ( $numBibsAvailable == 0 ) {
476 $template->param( none_available
=> 1, message
=> 1 );
479 my $itemTableColspan = 5;
480 if (!$template->param('OPACItemHolds')) {
483 if ($template->param('singleBranchMode')) {
486 $template->param(itemtable_colspan
=> $itemTableColspan);
489 $template->param(bibitemloop
=> $biblioLoop);
491 # can set reserve date in future
493 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
494 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
497 reserve_in_future
=> 1,
498 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
502 output_html_with_http_headers
$query, $cookie, $template->output;