3 # Copyright Katipo Communications 2002
4 # Copyright Koha Development team 2012
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Auth; # checkauth, getborrowernumber.
38 use Date::Calc qw/Today Date_to_Days/;
39 use List::MoreUtils qw/uniq/;
41 my $maxreserves = C4::Context->preference("maxreserves");
45 # if RequestOnOpac (for placing holds) is disabled, leave immediately
46 if ( ! C4::Context->preference('RequestOnOpac') ) {
47 print $query->redirect("/cgi-bin/koha/errors/404.pl");
51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
53 template_name => "opac-reserve.tt",
61 my ($show_holds_count, $show_priority);
62 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
63 m/holds/o and $show_holds_count = 1;
64 m/priority/ and $show_priority = 1;
68 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
72 # get borrower information ....
73 my ( $borr ) = GetMemberDetails( $borrowernumber );
74 my $patron = Koha::Patrons->find( $borrowernumber );
76 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
77 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
79 if ( $patron->is_expired ) {
81 # cannot reserve, their card has expired and the rules set mean this is not allowed
82 $template->param( message => 1, expired_patron => 1 );
83 get_out( $query, $cookie, $template->output );
87 # Pass through any reserve charge
88 if ($borr->{reservefee} > 0){
89 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
92 my $itemTypes = GetItemTypes();
94 # There are two ways of calling this script, with a single biblio num
95 # or multiple biblio nums.
96 my $biblionumbers = $query->param('biblionumbers');
97 my $reserveMode = $query->param('reserve_mode');
98 if ($reserveMode && ($reserveMode eq 'single')) {
99 my $bib = $query->param('single_bib');
100 $biblionumbers = "$bib/";
102 if (! $biblionumbers) {
103 $biblionumbers = $query->param('biblionumber');
106 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
107 $template->param(message=>1, no_biblionumber=>1);
108 &get_out($query, $cookie, $template->output);
111 # Pass the numbers to the page so they can be fed back
112 # when the hold is confirmed. TODO: Not necessary?
113 $template->param( biblionumbers => $biblionumbers );
115 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
116 my @biblionumbers = split /\//, $biblionumbers;
117 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
119 $template->param(message=>1, no_biblionumber=>1);
120 &get_out($query, $cookie, $template->output);
124 # pass the pickup branch along....
125 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
126 $template->param( branch => $branch );
128 # Is the person allowed to choose their branch
129 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
131 $template->param( choose_branch => $OPACChooseBranch);
135 # Build hashes of the requested biblio(item)s and items.
139 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
140 my %itemInfoHash; # Hash of itemnumber to item info.
141 foreach my $biblioNumber (@biblionumbers) {
143 my $biblioData = GetBiblioData($biblioNumber);
144 $biblioDataHash{$biblioNumber} = $biblioData;
146 my @itemInfos = GetItemsInfo($biblioNumber);
148 my $marcrecord= GetMarcBiblio($biblioNumber);
150 # flag indicating existence of at least one item linked via a host record
152 # adding items linked via host biblios
153 my @hostitemInfos = GetHostItemsInfo($marcrecord);
156 push (@itemInfos,@hostitemInfos);
159 $biblioData->{itemInfos} = \@itemInfos;
160 foreach my $itemInfo (@itemInfos) {
161 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
164 # Compute the priority rank.
165 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
166 my $rank = scalar( @$reserves );
167 $biblioData->{reservecount} = 1; # new reserve
168 foreach my $res (@{$reserves}) {
169 my $found = $res->{found};
170 if ( $found && $found eq 'W' ) {
174 $biblioData->{reservecount}++;
177 $biblioData->{rank} = $rank + 1;
182 # If this is the second time through this script, it
183 # means we are carrying out the hold request, possibly
184 # with a specific item for each biblionumber.
187 if ( $query->param('place_reserve') ) {
190 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
193 # List is composed of alternating biblio/item/branch
194 my $selectedItems = $query->param('selecteditems');
196 if ($query->param('reserve_mode') eq 'single') {
197 # This indicates non-JavaScript mode, so there was
198 # only a single biblio number selected.
199 my $bib = $query->param('single_bib');
200 my $item = $query->param("checkitem_$bib");
201 if ($item eq 'any') {
204 my $branch = $query->param('branch');
205 $selectedItems = "$bib/$item/$branch/";
208 $selectedItems =~ s!/$!!;
209 my @selectedItems = split /\//, $selectedItems, -1;
211 # Make sure there is a biblionum/itemnum/branch triplet for each item.
212 # The itemnum can be 'any', meaning next available.
213 my $selectionCount = @selectedItems;
214 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
215 $template->param(message=>1, bad_data=>1);
216 &get_out($query, $cookie, $template->output);
219 my $failed_holds = 0;
220 while (@selectedItems) {
221 my $biblioNum = shift(@selectedItems);
222 my $itemNum = shift(@selectedItems);
223 my $branch = shift(@selectedItems); # i.e., branch code, not name
227 my $singleBranchMode = Koha::Libraries->search->count == 1;
228 if ( $singleBranchMode || !$OPACChooseBranch )
229 { # single branch mode or disabled user choosing
230 $branch = $borr->{'branchcode'};
233 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
234 if ( $itemNum ne '' ) {
235 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
236 if ( $hostbiblioNum ne $biblioNum ) {
237 $biblioNum = $hostbiblioNum;
241 my $biblioData = $biblioDataHash{$biblioNum};
244 # Check for user supplied reserve date
246 if ( C4::Context->preference('AllowHoldDateInFuture')
247 && C4::Context->preference('OPACAllowHoldDateInFuture') )
249 $startdate = $query->param("reserve_date_$biblioNum");
252 my $expiration_date = $query->param("expiration_date_$biblioNum");
254 # If a specific item was selected and the pickup branch is the same as the
255 # holdingbranch, force the value $rank and $found.
256 my $rank = $biblioData->{rank};
257 if ( $itemNum ne '' ) {
258 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
259 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
260 my $item = GetItem($itemNum);
261 if ( $item->{'holdingbranch'} eq $branch ) {
263 unless C4::Context->preference('ReservesNeedReturns');
267 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
269 # Inserts a null into the 'itemnumber' field of 'reserves' table.
272 my $notes = $query->param('notes_'.$biblioNum)||'';
275 && $reserve_cnt >= $maxreserves )
280 my $itemtype = $query->param('itemtype') || undef;
281 $itemtype = undef if $itemNum;
283 # Here we actually do the reserveration. Stage 3.
285 my $reserve_id = AddReserve(
286 $branch, $borrowernumber, $biblioNum,
287 [$biblioNum], $rank, $startdate,
288 $expiration_date, $notes, $biblioData->{title},
289 $itemNum, $found, $itemtype,
291 $failed_holds++ unless $reserve_id;
296 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
302 # Here we check that the borrower can actually make reserves Stage 1.
306 my $maxoutstanding = C4::Context->preference("maxoutstanding");
307 $template->param( noreserve => 1 ) unless $maxoutstanding;
308 my $amountoutstanding = GetMemberAccountRecords($borrowernumber);
309 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
310 my $amount = sprintf "%.02f", $amountoutstanding;
311 $template->param( message => 1 );
313 $template->param( too_much_oweing => $amount );
316 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
324 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
332 if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
337 debarred_comment => $borr->{debarredcomment},
338 debarred_date => $borr->{debarred},
342 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
343 my $reserves_count = scalar(@reserves);
344 $template->param( RESERVES => \@reserves );
345 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
346 $template->param( message => 1 );
348 $template->param( too_many_reserves => scalar(@reserves));
351 unless ( $noreserves ) {
352 my $requested_reserves_count = scalar( @biblionumbers );
353 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
354 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
358 unless ($noreserves) {
359 $template->param( select_item_types => 1 );
365 # Build the template parameters that will show the info
366 # and items for each biblionumber.
369 my $notforloan_label_of = get_notforloan_label_of();
372 my $numBibsAvailable = 0;
373 my $itemdata_enumchron = 0;
375 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
376 $template->param('item_level_itypes' => $itemLevelTypes);
378 foreach my $biblioNum (@biblionumbers) {
380 my $record = GetMarcBiblio($biblioNum);
381 # Init the bib item with the choices for branch pickup
384 # Get relevant biblio data.
385 my $biblioData = $biblioDataHash{$biblioNum};
387 $template->param(message=>1, bad_biblionumber=>$biblioNum);
388 &get_out($query, $cookie, $template->output);
391 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
392 $biblioLoopIter{title} = $biblioData->{title};
393 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
394 $biblioLoopIter{author} = $biblioData->{author};
395 $biblioLoopIter{rank} = $biblioData->{rank};
396 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
397 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
398 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
400 if (!$itemLevelTypes && $biblioData->{itemtype}) {
401 $biblioLoopIter{translated_description} = $itemTypes->{$biblioData->{itemtype}}{translated_description};
402 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
405 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
406 if ($itemLevelTypes && $itemInfo->{itype}) {
407 $itemInfo->{translated_description} = $itemTypes->{$itemInfo->{itype}}{translated_description};
408 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
411 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
412 $biblioLoopIter{forloan} = 1;
416 $biblioLoopIter{itemLoop} = [];
417 my $numCopiesAvailable = 0;
418 my $numCopiesOPACAvailable = 0;
419 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
420 my $itemNum = $itemInfo->{itemnumber};
421 my $itemLoopIter = {};
423 $itemLoopIter->{itemnumber} = $itemNum;
424 $itemLoopIter->{barcode} = $itemInfo->{barcode};
425 $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
426 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
427 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
428 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
429 if ($itemLevelTypes) {
430 $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
431 $itemLoopIter->{itype} = $itemInfo->{itype};
432 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
435 # If the holdingbranch is different than the homebranch, we show the
436 # holdingbranch of the document too.
437 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
438 $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
441 # If the item is currently on loan, we display its return date and
442 # change the background color.
443 my $issues= GetItemIssue($itemNum);
444 if ( $issues->{'date_due'} ) {
445 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
446 $itemLoopIter->{backgroundcolor} = 'onloan';
450 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
451 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
453 # the item could be reserved for this borrower vi a host record, flag this
456 if ( defined $reservedate ) {
457 $itemLoopIter->{backgroundcolor} = 'reserved';
458 $itemLoopIter->{reservedate} = output_pref({ dt => dt_from_string($reservedate), dateonly => 1 });
459 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
460 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
461 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
462 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
464 $itemLoopIter->{waitingdate} = $wait;
467 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
468 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
470 # Management of the notforloan document
471 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
472 $itemLoopIter->{backgroundcolor} = 'other';
473 $itemLoopIter->{notforloanvalue} =
474 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
477 # Management of lost or long overdue items
478 if ( $itemInfo->{itemlost} ) {
480 # FIXME localized strings should never be in Perl code
481 $itemLoopIter->{message} =
482 $itemInfo->{itemlost} == 1 ? "(lost)"
483 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
485 $itemInfo->{backgroundcolor} = 'other';
488 # Check of the transfered documents
489 my ( $transfertwhen, $transfertfrom, $transfertto ) =
490 GetTransfers($itemNum);
491 if ( $transfertwhen && ($transfertwhen ne '') ) {
492 $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
493 $itemLoopIter->{transfertfrom} = $transfertfrom;
494 $itemLoopIter->{transfertto} = $transfertto;
495 $itemLoopIter->{nocancel} = 1;
498 # if the items belongs to a host record, show link to host record
499 if ( $itemInfo->{biblionumber} ne $biblioNum ) {
500 $biblioLoopIter{hostitemsflag} = 1;
501 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
502 $itemLoopIter->{hosttitle} = GetBiblioData( $itemInfo->{biblionumber} )->{title};
505 # If there is no loan, return and transfer, we show a checkbox.
506 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
508 my $branch = GetReservesControlBranch( $itemInfo, $borr );
510 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
511 $policy_holdallowed &&=
512 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
513 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
515 if ($policy_holdallowed) {
516 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
517 $itemLoopIter->{available} = 1;
518 $numCopiesOPACAvailable++;
519 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
521 $numCopiesAvailable++;
524 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
526 # Show serial enumeration when needed
527 if ($itemLoopIter->{enumchron}) {
528 $itemdata_enumchron = 1;
531 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
533 $template->param( itemdata_enumchron => $itemdata_enumchron );
535 if ($numCopiesAvailable > 0) {
537 $biblioLoopIter{bib_available} = 1;
538 $biblioLoopIter{holdable} = 1;
539 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
541 if ($biblioLoopIter{already_reserved}) {
542 $biblioLoopIter{holdable} = undef;
543 $biblioLoopIter{itemholdable} = undef;
545 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
546 $biblioLoopIter{holdable} = undef;
547 $biblioLoopIter{already_patron_possession} = 1;
550 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
552 # For multiple holds per record, if a patron has previously placed a hold,
553 # the patron can only place more holds of the same type. That is, if the
554 # patron placed a record level hold, all the holds the patron places must
555 # be record level. If the patron placed an item level hold, all holds
556 # the patron places must be item level
557 my $forced_hold_level = Koha::Holds->search(
559 borrowernumber => $borrowernumber,
560 biblionumber => $biblioNum,
563 )->forced_hold_level();
564 if ($forced_hold_level) {
565 $biblioLoopIter{force_hold} = 1 if $forced_hold_level eq 'item';
566 $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
570 push @$biblioLoop, \%biblioLoopIter;
572 $anyholdable = 1 if $biblioLoopIter{holdable};
576 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
577 $template->param( none_available => 1 );
580 my $show_notes=C4::Context->preference('OpacHoldNotes');
581 $template->param(OpacHoldNotes=>$show_notes);
584 $template->param(bibitemloop => $biblioLoop);
585 # can set reserve date in future
587 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
588 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
591 reserve_in_future => 1,
595 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };