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.
33 use C4
::Branch
; # GetBranches
37 use Koha
::Borrower
::Debarments
qw(IsDebarred);
38 use Date
::Calc qw
/Today Date_to_Days/;
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 );
75 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
76 if ( $borr->{'BlockExpiredPatronOpacActions'} ) {
78 if ( $borr->{'is_expired'} ) {
80 # cannot reserve, their card has expired and the rules set mean this is not allowed
81 $template->param( message
=> 1, expired_patron
=> 1 );
82 get_out
( $query, $cookie, $template->output );
86 # Pass through any reserve charge
87 if ($borr->{reservefee
} > 0){
88 $template->param( RESERVE_CHARGE
=> sprintf("%.2f",$borr->{reservefee
}));
90 # get branches and itemtypes
91 my $branches = GetBranches
();
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 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
127 $template->param( branch
=> $branch );
129 # make branch selection options...
130 my $branchloop = GetBranchesLoop
($branch);
132 # Is the person allowed to choose their branch
133 my $OPACChooseBranch = (C4
::Context
->preference("OPACAllowUserToChooseBranch")) ?
1 : 0;
135 $template->param( choose_branch
=> $OPACChooseBranch);
139 # Build hashes of the requested biblio(item)s and items.
143 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
144 my %itemInfoHash; # Hash of itemnumber to item info.
145 foreach my $biblioNumber (@biblionumbers) {
147 my $biblioData = GetBiblioData
($biblioNumber);
148 $biblioDataHash{$biblioNumber} = $biblioData;
150 my @itemInfos = GetItemsInfo
($biblioNumber);
152 my $marcrecord= GetMarcBiblio
($biblioNumber);
154 # flag indicating existence of at least one item linked via a host record
156 # adding items linked via host biblios
157 my @hostitemInfos = GetHostItemsInfo
($marcrecord);
160 push (@itemInfos,@hostitemInfos);
163 $biblioData->{itemInfos
} = \
@itemInfos;
164 foreach my $itemInfo (@itemInfos) {
165 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
168 # Compute the priority rank.
169 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblioNumber, all_dates
=> 1 });
170 my $rank = scalar( @
$reserves );
171 $biblioData->{reservecount
} = 1; # new reserve
172 foreach my $res (@
{$reserves}) {
173 my $found = $res->{found
};
174 if ( $found && $found eq 'W' ) {
178 $biblioData->{reservecount
}++;
181 $biblioData->{rank
} = $rank + 1;
186 # If this is the second time through this script, it
187 # means we are carrying out the hold request, possibly
188 # with a specific item for each biblionumber.
191 if ( $query->param('place_reserve') ) {
194 $reserve_cnt = GetReservesFromBorrowernumber
( $borrowernumber );
197 # List is composed of alternating biblio/item/branch
198 my $selectedItems = $query->param('selecteditems');
200 if ($query->param('reserve_mode') eq 'single') {
201 # This indicates non-JavaScript mode, so there was
202 # only a single biblio number selected.
203 my $bib = $query->param('single_bib');
204 my $item = $query->param("checkitem_$bib");
205 if ($item eq 'any') {
208 my $branch = $query->param('branch');
209 $selectedItems = "$bib/$item/$branch/";
212 $selectedItems =~ s!/$!!;
213 my @selectedItems = split /\//, $selectedItems, -1;
215 # Make sure there is a biblionum/itemnum/branch triplet for each item.
216 # The itemnum can be 'any', meaning next available.
217 my $selectionCount = @selectedItems;
218 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
219 $template->param(message
=>1, bad_data
=>1);
220 &get_out
($query, $cookie, $template->output);
223 my $failed_holds = 0;
224 while (@selectedItems) {
225 my $biblioNum = shift(@selectedItems);
226 my $itemNum = shift(@selectedItems);
227 my $branch = shift(@selectedItems); # i.e., branch code, not name
231 my $singleBranchMode = C4
::Context
->preference("singleBranchMode");
232 if ( $singleBranchMode || !$OPACChooseBranch )
233 { # single branch mode or disabled user choosing
234 $branch = $borr->{'branchcode'};
237 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
238 if ( $itemNum ne '' ) {
239 my $hostbiblioNum = GetBiblionumberFromItemnumber
($itemNum);
240 if ( $hostbiblioNum ne $biblioNum ) {
241 $biblioNum = $hostbiblioNum;
245 my $biblioData = $biblioDataHash{$biblioNum};
248 # Check for user supplied reserve date
250 if ( C4
::Context
->preference('AllowHoldDateInFuture')
251 && C4
::Context
->preference('OPACAllowHoldDateInFuture') )
253 $startdate = $query->param("reserve_date_$biblioNum");
256 my $expiration_date = $query->param("expiration_date_$biblioNum");
258 # If a specific item was selected and the pickup branch is the same as the
259 # holdingbranch, force the value $rank and $found.
260 my $rank = $biblioData->{rank
};
261 if ( $itemNum ne '' ) {
262 $canreserve = 1 if CanItemBeReserved
( $borrowernumber, $itemNum ) eq 'OK';
263 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
264 my $item = GetItem
($itemNum);
265 if ( $item->{'holdingbranch'} eq $branch ) {
267 unless C4
::Context
->preference('ReservesNeedReturns');
271 $canreserve = 1 if CanBookBeReserved
( $borrowernumber, $biblioNum ) eq 'OK';
273 # Inserts a null into the 'itemnumber' field of 'reserves' table.
276 my $notes = $query->param('notes_'.$biblioNum)||'';
279 && $reserve_cnt >= $maxreserves )
284 # Here we actually do the reserveration. Stage 3.
286 my $reserve_id = AddReserve
(
287 $branch, $borrowernumber,
290 $startdate, $expiration_date,
291 $notes, $biblioData->{title
},
294 $failed_holds++ unless $reserve_id;
299 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ?
"failed_holds=$failed_holds" : q
|| ) . "#opac-user-holds");
305 # Here we check that the borrower can actually make reserves Stage 1.
309 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
310 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
311 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
312 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
313 $template->param( message
=> 1 );
315 $template->param( too_much_oweing
=> $amount );
318 if ( $borr->{gonenoaddress
} && ($borr->{gonenoaddress
} == 1) ) {
326 if ( $borr->{lost
} && ($borr->{lost
} == 1) ) {
334 if ( IsDebarred
($borrowernumber) ) {
339 debarred_comment
=> $borr->{debarredcomment
},
340 debarred_date
=> $borr->{debarred
},
344 my @reserves = GetReservesFromBorrowernumber
( $borrowernumber );
345 my $reserves_count = scalar(@reserves);
346 $template->param( RESERVES
=> \
@reserves );
347 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
348 $template->param( message
=> 1 );
350 $template->param( too_many_reserves
=> scalar(@reserves));
353 unless ( $noreserves ) {
354 my $requested_reserves_count = scalar( @biblionumbers );
355 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
356 $template->param( new_reserves_allowed
=> $maxreserves - $reserves_count );
360 foreach my $res (@reserves) {
361 foreach my $biblionumber (@biblionumbers) {
362 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
363 # $template->param( message => 1 );
365 # $template->param( already_reserved => 1 );
366 $biblioDataHash{$biblionumber}->{already_reserved
} = 1;
371 unless ($noreserves) {
372 $template->param( select_item_types
=> 1 );
378 # Build the template parameters that will show the info
379 # and items for each biblionumber.
382 my $notforloan_label_of = get_notforloan_label_of
();
385 my $numBibsAvailable = 0;
386 my $itemdata_enumchron = 0;
388 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
389 $template->param('item_level_itypes' => $itemLevelTypes);
391 foreach my $biblioNum (@biblionumbers) {
393 my $record = GetMarcBiblio
($biblioNum);
394 # Init the bib item with the choices for branch pickup
395 my %biblioLoopIter = ( branchloop
=> $branchloop );
397 # Get relevant biblio data.
398 my $biblioData = $biblioDataHash{$biblioNum};
400 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
401 &get_out
($query, $cookie, $template->output);
404 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
405 $biblioLoopIter{title
} = $biblioData->{title
};
406 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, GetFrameworkCode
($biblioData->{biblionumber
}));
407 $biblioLoopIter{author
} = $biblioData->{author
};
408 $biblioLoopIter{rank
} = $biblioData->{rank
};
409 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
410 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
411 $biblioLoopIter{mandatorynotes
}=0; #FIXME: For future use
413 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
414 $biblioLoopIter{translated_description
} = $itemTypes->{$biblioData->{itemtype
}}{translated_description
};
415 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$biblioData->{itemtype
}}{imageurl
};
418 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
419 if ($itemLevelTypes && $itemInfo->{itype
}) {
420 $itemInfo->{translated_description
} = $itemTypes->{$itemInfo->{itype
}}{translated_description
};
421 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$itemInfo->{itype
}}{imageurl
};
424 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
425 $biblioLoopIter{forloan
} = 1;
429 $biblioLoopIter{itemLoop
} = [];
430 my $numCopiesAvailable = 0;
431 my $numCopiesOPACAvailable = 0;
432 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
433 my $itemNum = $itemInfo->{itemnumber
};
434 my $itemLoopIter = {};
436 $itemLoopIter->{itemnumber
} = $itemNum;
437 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
438 $itemLoopIter->{homeBranchName
} = $branches->{$itemInfo->{homebranch
}}{branchname
};
439 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
440 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
441 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
442 if ($itemLevelTypes) {
443 $itemLoopIter->{translated_description
} = $itemInfo->{translated_description
};
444 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
447 # If the holdingbranch is different than the homebranch, we show the
448 # holdingbranch of the document too.
449 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
450 $itemLoopIter->{holdingBranchName
} =
451 $branches->{ $itemInfo->{holdingbranch
} }{branchname
};
454 # If the item is currently on loan, we display its return date and
455 # change the background color.
456 my $issues= GetItemIssue
($itemNum);
457 if ( $issues->{'date_due'} ) {
458 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issues->{date_due
}, 'sql'), as_due_date
=> 1 });
459 $itemLoopIter->{backgroundcolor
} = 'onloan';
463 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber
($itemNum);
464 my $ItemBorrowerReserveInfo = GetMemberDetails
( $reservedfor, 0);
466 # the item could be reserved for this borrower vi a host record, flag this
468 if ($reservedfor eq $borrowernumber){
469 $itemLoopIter->{already_reserved
} = 1;
472 if ( defined $reservedate ) {
473 $itemLoopIter->{backgroundcolor
} = 'reserved';
474 $itemLoopIter->{reservedate
} = output_pref
({ dt
=> dt_from_string
($reservedate), dateonly
=> 1 });
475 $itemLoopIter->{ReservedForBorrowernumber
} = $reservedfor;
476 $itemLoopIter->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
477 $itemLoopIter->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
478 $itemLoopIter->{ExpectedAtLibrary
} = $expectedAt;
480 $itemLoopIter->{waitingdate
} = $wait;
483 $itemLoopIter->{notforloan
} = $itemInfo->{notforloan
};
484 $itemLoopIter->{itemnotforloan
} = $itemInfo->{itemnotforloan
};
486 # Management of the notforloan document
487 if ( $itemLoopIter->{notforloan
} || $itemLoopIter->{itemnotforloan
}) {
488 $itemLoopIter->{backgroundcolor
} = 'other';
489 $itemLoopIter->{notforloanvalue
} =
490 $notforloan_label_of->{ $itemLoopIter->{notforloan
} };
493 # Management of lost or long overdue items
494 if ( $itemInfo->{itemlost
} ) {
496 # FIXME localized strings should never be in Perl code
497 $itemLoopIter->{message
} =
498 $itemInfo->{itemlost
} == 1 ?
"(lost)"
499 : $itemInfo->{itemlost
} == 2 ?
"(long overdue)"
501 $itemInfo->{backgroundcolor
} = 'other';
504 # Check of the transfered documents
505 my ( $transfertwhen, $transfertfrom, $transfertto ) =
506 GetTransfers
($itemNum);
507 if ( $transfertwhen && ($transfertwhen ne '') ) {
508 $itemLoopIter->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
($transfertwhen), dateonly
=> 1 });
509 $itemLoopIter->{transfertfrom
} =
510 $branches->{$transfertfrom}{branchname
};
511 $itemLoopIter->{transfertto
} = $branches->{$transfertto}{branchname
};
512 $itemLoopIter->{nocancel
} = 1;
515 # if the items belongs to a host record, show link to host record
516 if ($itemInfo->{biblionumber
} ne $biblioNum){
517 $biblioLoopIter{hostitemsflag
} = 1;
518 $itemLoopIter->{hostbiblionumber
} = $itemInfo->{biblionumber
};
519 $itemLoopIter->{hosttitle
} = GetBiblioData
($itemInfo->{biblionumber
})->{title
};
522 # If there is no loan, return and transfer, we show a checkbox.
523 $itemLoopIter->{notforloan
} = $itemLoopIter->{notforloan
} || 0;
525 my $branch = GetReservesControlBranch
( $itemInfo, $borr );
527 my $policy_holdallowed = !$itemLoopIter->{already_reserved
};
528 $policy_holdallowed &&=
529 IsAvailableForItemLevelRequest
($itemInfo,$borr) &&
530 CanItemBeReserved
($borrowernumber,$itemNum) eq 'OK';
532 if ($policy_holdallowed) {
533 if ( my $hold_allowed = OPACItemHoldsAllowed
( $itemInfo, $borr ) ) {
534 $itemLoopIter->{available
} = 1;
535 $numCopiesOPACAvailable++;
536 $biblioLoopIter{force_hold
} = 1 if $hold_allowed eq 'F';
538 $numCopiesAvailable++;
541 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemTypes->{ $itemInfo->{itype
} }{imageurl
} );
543 # Show serial enumeration when needed
544 if ($itemLoopIter->{enumchron
}) {
545 $itemdata_enumchron = 1;
548 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
550 $template->param( itemdata_enumchron
=> $itemdata_enumchron );
552 if ($numCopiesAvailable > 0) {
554 $biblioLoopIter{bib_available
} = 1;
555 $biblioLoopIter{holdable
} = 1;
556 $biblioLoopIter{itemholdable
} = 1 if $numCopiesOPACAvailable;
558 if ($biblioLoopIter{already_reserved
}) {
559 $biblioLoopIter{holdable
} = undef;
560 $biblioLoopIter{itemholdable
} = undef;
562 if(not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowernumber,$biblioNum)) {
563 $biblioLoopIter{holdable
} = undef;
564 $biblioLoopIter{already_patron_possession
} = 1;
567 $biblioLoopIter{holdable
} &&= CanBookBeReserved
($borrowernumber,$biblioNum) eq 'OK';
569 push @
$biblioLoop, \
%biblioLoopIter;
571 $anyholdable = 1 if $biblioLoopIter{holdable
};
574 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
575 $template->param( none_available
=> 1 );
578 my $show_notes=C4
::Context
->preference('OpacHoldNotes');
579 $template->param(OpacHoldNotes
=>$show_notes);
582 $template->param(bibitemloop
=> $biblioLoop);
583 # can set reserve date in future
585 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
586 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
589 reserve_in_future
=> 1,
593 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };