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') || 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 );
101 # Build hashes of the requested biblio(item)s and items.
105 # Hash of biblionumber to biblio/biblioitems record.
108 # Hash of itemnumber to item info.
111 foreach my $biblioNumber (@biblionumbers) {
113 my $biblioData = GetBiblioData
($biblioNumber);
114 $biblioDataHash{$biblioNumber} = $biblioData;
116 my @itemInfos = GetItemsInfo
($biblioNumber);
117 $biblioData->{itemInfos
} = \
@itemInfos;
118 foreach my $itemInfo (@itemInfos) {
119 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
122 # Compute the priority rank.
123 my ( $rank, $reserves ) = GetReservesFromBiblionumber
($biblioNumber,1);
124 $biblioData->{reservecount
} = $rank;
125 foreach my $res (@
$reserves) {
126 my $found = $res->{'found'};
127 if ( $found && ($found eq 'W') ) {
132 $biblioData->{rank
} = $rank;
137 # If this is the second time through this script, it
138 # means we are carrying out the hold request, possibly
139 # with a specific item for each biblionumber.
142 if ( $query->param('place_reserve') ) {
143 my $notes = $query->param('notes');
146 # List is composed of alternating biblio/item/branch
147 my $selectedItems = $query->param('selecteditems');
149 if ($query->param('reserve_mode') eq 'single') {
150 # This indicates non-JavaScript mode, so there was
151 # only a single biblio number selected.
152 my $bib = $query->param('single_bib');
153 my $item = $query->param("checkitem_$bib");
154 if ($item eq 'any') {
157 my $branch = $query->param('branch');
158 $selectedItems = "$bib/$item/$branch/";
161 $selectedItems =~ s!/$!!;
162 my @selectedItems = split /\//, $selectedItems, -1;
164 # Make sure there is a biblionum/itemnum/branch triplet for each item.
165 # The itemnum can be 'any', meaning next available.
166 my $selectionCount = @selectedItems;
167 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
168 $template->param(message
=>1, bad_data
=>1);
169 &get_out
($query, $cookie, $template->output);
172 while (@selectedItems) {
173 my $biblioNum = shift(@selectedItems);
174 my $itemNum = shift(@selectedItems);
175 my $branch = shift(@selectedItems); # i.e., branch code, not name
177 my $singleBranchMode = $template->param('singleBranchMode');
178 if ($singleBranchMode) {
179 $branch = $borr->{'branchcode'};
182 my $biblioData = $biblioDataHash{$biblioNum};
185 # Check for user supplied reserve date
188 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
189 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
191 $startdate = $query->param("reserve_date_$biblioNum");
194 my $expiration_date = $query->param("expiration_date_$biblioNum");
196 # If a specific item was selected and the pickup branch is the same as the
197 # holdingbranch, force the value $rank and $found.
198 my $rank = $biblioData->{rank
};
200 $canreserve = 1 if CanItemBeReserved
($borrowernumber,$itemNum);
201 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
202 my $item = GetItem
($itemNum);
203 if ( $item->{'holdingbranch'} eq $branch ){
204 $found = 'W' unless C4
::Context
->preference('ReservesNeedReturns');
208 $canreserve = 1 if CanBookBeReserved
($borrowernumber,$biblioNum);
209 # Inserts a null into the 'itemnumber' field of 'reserves' table.
213 # Here we actually do the reserveration. Stage 3.
214 AddReserve
($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $startdate, $expiration_date, $notes,
215 $biblioData->{'title'}, $itemNum, $found) if ($canreserve);
218 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
224 # Here we check that the borrower can actually make reserves Stage 1.
228 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
229 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
230 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
231 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
232 $template->param( message
=> 1 );
234 $template->param( too_much_oweing
=> $amount );
236 if ( $borr->{gonenoaddress
} && ($borr->{gonenoaddress
} eq 1) ) {
243 if ( $borr->{lost
} && ($borr->{lost
} eq 1) ) {
250 if ( $borr->{debarred
} && ($borr->{debarred
} eq 1) ) {
258 my @reserves = GetReservesFromBorrowernumber
( $borrowernumber );
259 $template->param( RESERVES
=> \
@reserves );
260 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
261 $template->param( message
=> 1 );
263 $template->param( too_many_reserves
=> scalar(@reserves));
265 foreach my $res (@reserves) {
266 foreach my $biblionumber (@biblionumbers) {
267 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
268 # $template->param( message => 1 );
270 # $template->param( already_reserved => 1 );
271 $biblioDataHash{$biblionumber}->{already_reserved
} = 1;
276 unless ($noreserves) {
277 $template->param( select_item_types
=> 1 );
283 # Build the template parameters that will show the info
284 # and items for each biblionumber.
287 my $notforloan_label_of = get_notforloan_label_of
();
290 my $numBibsAvailable = 0;
291 my $itemdata_enumchron = 0;
292 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
293 $template->param('item-level_itypes' => $itemLevelTypes);
295 foreach my $biblioNum (@biblionumbers) {
297 my $record = GetMarcBiblio
($biblioNum);
298 # Init the bib item with the choices for branch pickup
299 my %biblioLoopIter = ( branchChoicesLoop
=> $CGIbranchloop );
301 # Get relevant biblio data.
302 my $biblioData = $biblioDataHash{$biblioNum};
304 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
305 &get_out
($query, $cookie, $template->output);
308 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
309 $biblioLoopIter{title
} = $biblioData->{title
};
310 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, GetFrameworkCode
($biblioData->{biblionumber
}));
311 $biblioLoopIter{author
} = $biblioData->{author
};
312 $biblioLoopIter{rank
} = $biblioData->{rank
};
313 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
314 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
316 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
317 $biblioLoopIter{description
} = $itemTypes->{$biblioData->{itemtype
}}{description
};
318 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$biblioData->{itemtype
}}{imageurl
};
321 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
322 $debug and warn $itemInfo->{'notforloan'};
325 my $fee = GetReserveFee
(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
326 ( $itemInfo->{'biblioitemnumber'} ) );
327 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ?
$fee : 0.0);
329 if ($itemLevelTypes && $itemInfo->{itype
}) {
330 $itemInfo->{description
} = $itemTypes->{$itemInfo->{itype
}}{description
};
331 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$itemInfo->{itype
}}{imageurl
};
334 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
335 $biblioLoopIter{forloan
} = 1;
339 $biblioLoopIter{itemTypeDescription
} = $itemTypes->{$biblioData->{itemtype
}}{description
};
341 $biblioLoopIter{itemLoop
} = [];
342 my $numCopiesAvailable = 0;
343 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
344 my $itemNum = $itemInfo->{itemnumber
};
345 my $itemLoopIter = {};
347 $itemLoopIter->{itemnumber
} = $itemNum;
348 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
349 $itemLoopIter->{homeBranchName
} = $branches->{$itemInfo->{homebranch
}}{branchname
};
350 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
351 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
352 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
353 if ($itemLevelTypes) {
354 $itemLoopIter->{description
} = $itemInfo->{description
};
355 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
358 # If the holdingbranch is different than the homebranch, we show the
359 # holdingbranch of the document too.
360 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
361 $itemLoopIter->{holdingBranchName
} =
362 $branches->{ $itemInfo->{holdingbranch
} }{branchname
};
365 # If the item is currently on loan, we display its return date and
366 # change the background color.
367 my $issues= GetItemIssue
($itemNum);
368 if ( $issues->{'date_due'} ) {
369 $itemLoopIter->{dateDue
} = format_date
($issues->{'date_due'});
370 $itemLoopIter->{backgroundcolor
} = 'onloan';
374 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemNum);
375 my $ItemBorrowerReserveInfo = GetMemberDetails
( $reservedfor, 0);
377 if ( defined $reservedate ) {
378 $itemLoopIter->{backgroundcolor
} = 'reserved';
379 $itemLoopIter->{reservedate
} = format_date
($reservedate);
380 $itemLoopIter->{ReservedForBorrowernumber
} = $reservedfor;
381 $itemLoopIter->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
382 $itemLoopIter->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
383 $itemLoopIter->{ExpectedAtLibrary
} = $expectedAt;
386 $itemLoopIter->{notforloan
} = $itemInfo->{notforloan
};
387 $itemLoopIter->{itemnotforloan
} = $itemInfo->{itemnotforloan
};
389 # Management of the notforloan document
390 if ( $itemLoopIter->{notforloan
} || $itemLoopIter->{itemnotforloan
}) {
391 $itemLoopIter->{backgroundcolor
} = 'other';
392 $itemLoopIter->{notforloanvalue
} =
393 $notforloan_label_of->{ $itemLoopIter->{notforloan
} };
396 # Management of lost or long overdue items
397 if ( $itemInfo->{itemlost
} ) {
399 # FIXME localized strings should never be in Perl code
400 $itemLoopIter->{message
} =
401 $itemInfo->{itemlost
} == 1 ?
"(lost)"
402 : $itemInfo->{itemlost
} == 2 ?
"(long overdue)"
404 $itemInfo->{backgroundcolor
} = 'other';
407 # Check of the transfered documents
408 my ( $transfertwhen, $transfertfrom, $transfertto ) =
409 GetTransfers
($itemNum);
410 if ( $transfertwhen && ($transfertwhen ne '') ) {
411 $itemLoopIter->{transfertwhen
} = format_date
($transfertwhen);
412 $itemLoopIter->{transfertfrom
} =
413 $branches->{$transfertfrom}{branchname
};
414 $itemLoopIter->{transfertto
} = $branches->{$transfertto}{branchname
};
415 $itemLoopIter->{nocancel
} = 1;
418 # If there is no loan, return and transfer, we show a checkbox.
419 $itemLoopIter->{notforloan
} = $itemLoopIter->{notforloan
} || 0;
421 my $branch = C4
::Circulation
::_GetCircControlBranch
($itemLoopIter, $borr);
423 my $branchitemrule = GetBranchItemRule
( $branch, $itemInfo->{'itype'} );
424 my $policy_holdallowed = 1;
426 if ( $branchitemrule->{'holdallowed'} == 0 ||
427 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
428 $policy_holdallowed = 0;
431 if (IsAvailableForItemLevelRequest
($itemNum) and $policy_holdallowed and CanItemBeReserved
($borrowernumber,$itemNum)) {
432 $itemLoopIter->{available
} = 1;
433 $numCopiesAvailable++;
436 # FIXME: move this to a pm
437 my $dbh = C4
::Context
->dbh;
438 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
439 $sth2->execute($itemLoopIter->{ReservedForBorrowernumber
}, $itemNum);
440 while (my $wait_hashref = $sth2->fetchrow_hashref) {
441 $itemLoopIter->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
443 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemTypes->{ $itemInfo->{itype
} }{imageurl
} );
445 # Show serial enumeration when needed
446 if ($itemLoopIter->{enumchron
}) {
447 $itemdata_enumchron = 1;
449 $template->param( itemdata_enumchron
=> $itemdata_enumchron );
451 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
454 if ($numCopiesAvailable > 0) {
456 $biblioLoopIter{bib_available
} = 1;
457 $biblioLoopIter{holdable
} = 1;
459 if ($biblioLoopIter{already_reserved
}) {
460 $biblioLoopIter{holdable
} = undef;
462 if(not CanBookBeReserved
($borrowernumber,$biblioNum)){
463 $biblioLoopIter{holdable
} = undef;
466 push @
$biblioLoop, \
%biblioLoopIter;
469 if ( $numBibsAvailable == 0 ) {
470 $template->param( none_available
=> 1, message
=> 1 );
473 my $itemTableColspan = 5;
474 if (!$template->param('OPACItemHolds')) {
477 if ($template->param('singleBranchMode')) {
480 $template->param(itemtable_colspan
=> $itemTableColspan);
483 $template->param(bibitemloop
=> $biblioLoop);
484 $template->param( showpriority
=>1 ) if $OPACDisplayRequestPriority;
485 # can set reserve date in future
487 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
488 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
491 reserve_in_future
=> 1,
495 $template->param( DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar() );
497 output_html_with_http_headers
$query, $cookie, $template->output;