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.
36 use Koha::AuthorisedValues;
39 use Koha::IssuingRules;
45 use Date::Calc qw/Today Date_to_Days/;
46 use List::MoreUtils qw/uniq/;
48 my $maxreserves = C4::Context->preference("maxreserves");
52 # if RequestOnOpac (for placing holds) is disabled, leave immediately
53 if ( ! C4::Context->preference('RequestOnOpac') ) {
54 print $query->redirect("/cgi-bin/koha/errors/404.pl");
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60 template_name => "opac-reserve.tt",
68 my ($show_holds_count, $show_priority);
69 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
70 m/holds/o and $show_holds_count = 1;
71 m/priority/ and $show_priority = 1;
75 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
79 my $patron = Koha::Patrons->find( $borrowernumber );
81 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
82 unless ( $can_place_hold_if_available_at_pickup ) {
83 my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
84 if ( @patron_categories ) {
85 my $categorycode = $patron->categorycode;
86 $can_place_hold_if_available_at_pickup = grep /^$categorycode$/, @patron_categories;
90 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
91 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
93 if ( $patron->is_expired ) {
95 # cannot reserve, their card has expired and the rules set mean this is not allowed
96 $template->param( message => 1, expired_patron => 1 );
97 get_out( $query, $cookie, $template->output );
101 # Pass through any reserve charge
102 my $reservefee = $patron->category->reservefee;
103 if ( $reservefee > 0){
104 $template->param( RESERVE_CHARGE => sprintf("%.2f",$reservefee));
107 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
109 # There are two ways of calling this script, with a single biblio num
110 # or multiple biblio nums.
111 my $biblionumbers = $query->param('biblionumbers');
112 my $reserveMode = $query->param('reserve_mode');
113 if ($reserveMode && ($reserveMode eq 'single')) {
114 my $bib = $query->param('single_bib');
115 $biblionumbers = "$bib/";
117 if (! $biblionumbers) {
118 $biblionumbers = $query->param('biblionumber');
121 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
122 $template->param(message=>1, no_biblionumber=>1);
123 &get_out($query, $cookie, $template->output);
126 # Pass the numbers to the page so they can be fed back
127 # when the hold is confirmed. TODO: Not necessary?
128 $template->param( biblionumbers => $biblionumbers );
130 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
131 my @biblionumbers = split /\//, $biblionumbers;
132 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
134 $template->param(message=>1, no_biblionumber=>1);
135 &get_out($query, $cookie, $template->output);
139 # pass the pickup branch along....
140 my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
141 $template->param( branch => $branch );
145 # Build hashes of the requested biblio(item)s and items.
149 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
150 my %itemInfoHash; # Hash of itemnumber to item info.
151 foreach my $biblioNumber (@biblionumbers) {
153 my $biblioData = GetBiblioData($biblioNumber);
154 $biblioDataHash{$biblioNumber} = $biblioData;
156 my @itemInfos = GetItemsInfo($biblioNumber);
158 my $marcrecord= GetMarcBiblio({ biblionumber => $biblioNumber });
160 # flag indicating existence of at least one item linked via a host record
162 # adding items linked via host biblios
163 my @hostitemInfos = GetHostItemsInfo($marcrecord);
166 push (@itemInfos,@hostitemInfos);
169 $biblioData->{itemInfos} = \@itemInfos;
170 foreach my $itemInfo (@itemInfos) {
171 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
174 # Compute the priority rank.
175 my $biblio = Koha::Biblios->find( $biblioNumber );
176 my $holds = $biblio->holds;
177 my $rank = $holds->count;
178 $biblioData->{reservecount} = 1; # new reserve
179 while ( my $hold = $holds->next ) {
180 if ( $hold->is_waiting ) {
184 $biblioData->{reservecount}++;
187 $biblioData->{rank} = $rank + 1;
192 # If this is the second time through this script, it
193 # means we are carrying out the hold request, possibly
194 # with a specific item for each biblionumber.
197 if ( $query->param('place_reserve') ) {
200 $reserve_cnt = $patron->holds->count;
203 # List is composed of alternating biblio/item/branch
204 my $selectedItems = $query->param('selecteditems');
206 if ($query->param('reserve_mode') eq 'single') {
207 # This indicates non-JavaScript mode, so there was
208 # only a single biblio number selected.
209 my $bib = $query->param('single_bib');
210 my $item = $query->param("checkitem_$bib");
211 if ($item eq 'any') {
214 my $branch = $query->param('branch');
215 $selectedItems = "$bib/$item/$branch/";
218 $selectedItems =~ s!/$!!;
219 my @selectedItems = split /\//, $selectedItems, -1;
221 # Make sure there is a biblionum/itemnum/branch triplet for each item.
222 # The itemnum can be 'any', meaning next available.
223 my $selectionCount = @selectedItems;
224 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
225 $template->param(message=>1, bad_data=>1);
226 &get_out($query, $cookie, $template->output);
229 my $failed_holds = 0;
230 while (@selectedItems) {
231 my $biblioNum = shift(@selectedItems);
232 my $itemNum = shift(@selectedItems);
233 my $branch = shift(@selectedItems); # i.e., branch code, not name
237 my $singleBranchMode = Koha::Libraries->search->count == 1;
238 if ( $singleBranchMode || ! C4::Context->preference("OPACAllowUserToChooseBranch") )
239 { # single branch mode or disabled user choosing
240 $branch = $patron->branchcode;
243 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
244 if ( $itemNum ne '' ) {
245 my $item = Koha::Items->find( $itemNum );
246 my $hostbiblioNum = $item->biblio->biblionumber;
247 if ( $hostbiblioNum ne $biblioNum ) {
248 $biblioNum = $hostbiblioNum;
252 my $biblioData = $biblioDataHash{$biblioNum};
255 # Check for user supplied reserve date
257 if ( C4::Context->preference('AllowHoldDateInFuture')
258 && C4::Context->preference('OPACAllowHoldDateInFuture') )
260 $startdate = $query->param("reserve_date_$biblioNum");
263 my $expiration_date = $query->param("expiration_date_$biblioNum");
265 my $rank = $biblioData->{rank};
266 if ( $itemNum ne '' ) {
267 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
270 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum )->{status} eq 'OK';
272 # Inserts a null into the 'itemnumber' field of 'reserves' table.
275 my $notes = $query->param('notes_'.$biblioNum)||'';
278 && $reserve_cnt >= $maxreserves )
283 unless ( $can_place_hold_if_available_at_pickup ) {
284 my $items_in_this_library = Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch });
285 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
286 my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost => { '!=' => 0 }, damaged => { '!=' => 0 }, } });
287 if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
292 my $itemtype = $query->param('itemtype') || undef;
293 $itemtype = undef if $itemNum;
295 # Here we actually do the reserveration. Stage 3.
297 my $reserve_id = AddReserve(
298 $branch, $borrowernumber, $biblioNum,
299 [$biblioNum], $rank, $startdate,
300 $expiration_date, $notes, $biblioData->{title},
301 $itemNum, $found, $itemtype,
303 $failed_holds++ unless $reserve_id;
308 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
314 # Here we check that the borrower can actually make reserves Stage 1.
318 my $maxoutstanding = C4::Context->preference("maxoutstanding");
319 $template->param( noreserve => 1 ) unless $maxoutstanding;
320 my $amountoutstanding = $patron->account->balance;
321 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
322 my $amount = sprintf "%.02f", $amountoutstanding;
323 $template->param( message => 1 );
325 $template->param( too_much_oweing => $amount );
328 if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
336 if ( $patron->lost && ($patron->lost == 1) ) {
344 if ( $patron->is_debarred ) {
349 debarred_comment => $patron->debarredcomment,
350 debarred_date => $patron->debarred,
354 my $holds = $patron->holds;
355 my $reserves_count = $holds->count;
356 $template->param( RESERVES => $holds->unblessed );
357 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
358 $template->param( message => 1 );
360 $template->param( too_many_reserves => $holds->count );
363 unless ( $noreserves ) {
364 my $requested_reserves_count = scalar( @biblionumbers );
365 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
366 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
370 unless ($noreserves) {
371 $template->param( select_item_types => 1 );
377 # Build the template parameters that will show the info
378 # and items for each biblionumber.
383 my $numBibsAvailable = 0;
384 my $itemdata_enumchron = 0;
386 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
387 $template->param('item_level_itypes' => $itemLevelTypes);
389 foreach my $biblioNum (@biblionumbers) {
391 my @not_available_at = ();
392 my $record = GetMarcBiblio({ biblionumber => $biblioNum });
393 # Init the bib item with the choices for branch pickup
396 # Get relevant biblio data.
397 my $biblioData = $biblioDataHash{$biblioNum};
399 $template->param(message=>1, bad_biblionumber=>$biblioNum);
400 &get_out($query, $cookie, $template->output);
403 my $frameworkcode = GetFrameworkCode( $biblioData->{biblionumber} );
404 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
405 $biblioLoopIter{title} = $biblioData->{title};
406 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
407 $biblioLoopIter{author} = $biblioData->{author};
408 $biblioLoopIter{rank} = $biblioData->{rank};
409 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
410 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
411 $biblioLoopIter{reqholdnotes}=0; #TODO: 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 my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
430 my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
432 $biblioLoopIter{itemLoop} = [];
433 my $numCopiesAvailable = 0;
434 my $numCopiesOPACAvailable = 0;
435 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
436 my $itemNum = $itemInfo->{itemnumber};
437 my $item = Koha::Items->find( $itemNum );
438 my $itemLoopIter = {};
440 $itemLoopIter->{itemnumber} = $itemNum;
441 $itemLoopIter->{barcode} = $itemInfo->{barcode};
442 $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
443 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
444 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
445 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
446 if ($itemLevelTypes) {
447 $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
448 $itemLoopIter->{itype} = $itemInfo->{itype};
449 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
452 # If the holdingbranch is different than the homebranch, we show the
453 # holdingbranch of the document too.
454 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
455 $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
458 # If the item is currently on loan, we display its return date and
459 # change the background color.
460 my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
462 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
463 $itemLoopIter->{backgroundcolor} = 'onloan';
467 my $holds = $item->current_holds;
469 if ( my $first_hold = $holds->next ) {
470 $itemLoopIter->{backgroundcolor} = 'reserved';
471 $itemLoopIter->{reservedate} = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
472 $itemLoopIter->{ExpectedAtLibrary} = $first_hold->branchcode;
473 $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
476 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
477 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
479 # Management of the notforloan document
480 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
481 $itemLoopIter->{backgroundcolor} = 'other';
482 $itemLoopIter->{notforloanvalue} =
483 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
486 # Management of lost or long overdue items
487 if ( $itemInfo->{itemlost} ) {
489 # FIXME localized strings should never be in Perl code
490 $itemLoopIter->{message} =
491 $itemInfo->{itemlost} == 1 ? "(lost)"
492 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
494 $itemInfo->{backgroundcolor} = 'other';
497 # Check of the transfered documents
498 my ( $transfertwhen, $transfertfrom, $transfertto ) =
499 GetTransfers($itemNum);
500 if ( $transfertwhen && ($transfertwhen ne '') ) {
501 $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
502 $itemLoopIter->{transfertfrom} = $transfertfrom;
503 $itemLoopIter->{transfertto} = $transfertto;
504 $itemLoopIter->{nocancel} = 1;
507 # if the items belongs to a host record, show link to host record
508 if ( $itemInfo->{biblionumber} ne $biblioNum ) {
509 $biblioLoopIter{hostitemsflag} = 1;
510 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
511 $itemLoopIter->{hosttitle} = Koha::Biblios->find( $itemInfo->{biblionumber} )->title;
514 # If there is no loan, return and transfer, we show a checkbox.
515 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
517 my $patron_unblessed = $patron->unblessed;
518 my $branch = GetReservesControlBranch( $itemInfo, $patron_unblessed );
520 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
521 $policy_holdallowed &&=
522 IsAvailableForItemLevelRequest($itemInfo,$patron_unblessed) &&
523 CanItemBeReserved( $borrowernumber, $itemNum )->{status} eq 'OK';
525 if ($policy_holdallowed) {
526 my $opac_hold_policy = Koha::IssuingRules->get_opacitemholds_policy( { item => $item, patron => $patron } );
527 if ( $opac_hold_policy ne 'N' ) { # If Y or F
528 $itemLoopIter->{available} = 1;
529 $numCopiesOPACAvailable++;
530 $biblioLoopIter{force_hold} = 1 if $opac_hold_policy eq 'F';
532 $numCopiesAvailable++;
534 unless ( $can_place_hold_if_available_at_pickup ) {
535 my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
536 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
537 if ( $items_in_this_library->count > $nb_of_items_issued ) {
538 push @not_available_at, $itemInfo->{holdingbranch};
543 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
545 # Show serial enumeration when needed
546 if ($itemLoopIter->{enumchron}) {
547 $itemdata_enumchron = 1;
550 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
552 $template->param( itemdata_enumchron => $itemdata_enumchron );
554 if ($numCopiesAvailable > 0) {
556 $biblioLoopIter{bib_available} = 1;
557 $biblioLoopIter{holdable} = 1;
558 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
560 if ($biblioLoopIter{already_reserved}) {
561 $biblioLoopIter{holdable} = undef;
562 $biblioLoopIter{itemholdable} = undef;
564 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
565 $biblioLoopIter{holdable} = undef;
566 $biblioLoopIter{already_patron_possession} = 1;
569 if ( $biblioLoopIter{holdable} ) {
570 @not_available_at = uniq @not_available_at;
571 $biblioLoopIter{not_available_at} = \@not_available_at ;
574 unless ( $can_place_hold_if_available_at_pickup ) {
575 @not_available_at = uniq @not_available_at;
576 $biblioLoopIter{not_available_at} = \@not_available_at ;
577 # The record is not holdable is not available at any of the libraries
578 if ( Koha::Libraries->search->count == @not_available_at ) {
579 $biblioLoopIter{holdable} = 0;
583 $biblioLoopIter{holdable} &&= CanBookBeReserved( $borrowernumber, $biblioNum )->{status} eq 'OK';
585 # For multiple holds per record, if a patron has previously placed a hold,
586 # the patron can only place more holds of the same type. That is, if the
587 # patron placed a record level hold, all the holds the patron places must
588 # be record level. If the patron placed an item level hold, all holds
589 # the patron places must be item level
590 my $forced_hold_level = Koha::Holds->search(
592 borrowernumber => $borrowernumber,
593 biblionumber => $biblioNum,
596 )->forced_hold_level();
597 if ($forced_hold_level) {
598 $biblioLoopIter{force_hold} = 1 if $forced_hold_level eq 'item';
599 $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
603 push @$biblioLoop, \%biblioLoopIter;
605 $anyholdable = 1 if $biblioLoopIter{holdable};
609 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
610 $template->param( none_available => 1 );
613 if (scalar @biblionumbers > 1) {
614 $template->param( multi_hold => 1);
617 my $show_notes=C4::Context->preference('OpacHoldNotes');
618 $template->param(OpacHoldNotes=>$show_notes);
621 $template->param(bibitemloop => $biblioLoop);
622 # can set reserve date in future
624 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
625 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
628 reserve_in_future => 1,
632 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };