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.
35 use Koha::AuthorisedValues;
42 use Date::Calc qw/Today Date_to_Days/;
43 use List::MoreUtils qw/uniq/;
45 my $maxreserves = C4::Context->preference("maxreserves");
49 # if RequestOnOpac (for placing holds) is disabled, leave immediately
50 if ( ! C4::Context->preference('RequestOnOpac') ) {
51 print $query->redirect("/cgi-bin/koha/errors/404.pl");
55 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57 template_name => "opac-reserve.tt",
65 my ($show_holds_count, $show_priority);
66 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
67 m/holds/o and $show_holds_count = 1;
68 m/priority/ and $show_priority = 1;
72 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
76 # get borrower information ....
77 my ( $borr ) = GetMember( borrowernumber => $borrowernumber );
78 my $patron = Koha::Patrons->find( $borrowernumber );
80 my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
81 unless ( $can_place_hold_if_available_at_pickup ) {
82 my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
83 if ( @patron_categories ) {
84 $can_place_hold_if_available_at_pickup = grep /$borr->{categorycode}/, @patron_categories;
88 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
89 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
91 if ( $patron->is_expired ) {
93 # cannot reserve, their card has expired and the rules set mean this is not allowed
94 $template->param( message => 1, expired_patron => 1 );
95 get_out( $query, $cookie, $template->output );
99 # Pass through any reserve charge
100 my $reservefee = $patron->category->reservefee;
101 if ( $reservefee > 0){
102 $template->param( RESERVE_CHARGE => sprintf("%.2f",$reservefee));
105 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
107 # There are two ways of calling this script, with a single biblio num
108 # or multiple biblio nums.
109 my $biblionumbers = $query->param('biblionumbers');
110 my $reserveMode = $query->param('reserve_mode');
111 if ($reserveMode && ($reserveMode eq 'single')) {
112 my $bib = $query->param('single_bib');
113 $biblionumbers = "$bib/";
115 if (! $biblionumbers) {
116 $biblionumbers = $query->param('biblionumber');
119 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
120 $template->param(message=>1, no_biblionumber=>1);
121 &get_out($query, $cookie, $template->output);
124 # Pass the numbers to the page so they can be fed back
125 # when the hold is confirmed. TODO: Not necessary?
126 $template->param( biblionumbers => $biblionumbers );
128 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
129 my @biblionumbers = split /\//, $biblionumbers;
130 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
132 $template->param(message=>1, no_biblionumber=>1);
133 &get_out($query, $cookie, $template->output);
137 # pass the pickup branch along....
138 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
139 $template->param( branch => $branch );
141 # Is the person allowed to choose their branch
142 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
144 $template->param( choose_branch => $OPACChooseBranch);
148 # Build hashes of the requested biblio(item)s and items.
152 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
153 my %itemInfoHash; # Hash of itemnumber to item info.
154 foreach my $biblioNumber (@biblionumbers) {
156 my $biblioData = GetBiblioData($biblioNumber);
157 $biblioDataHash{$biblioNumber} = $biblioData;
159 my @itemInfos = GetItemsInfo($biblioNumber);
161 my $marcrecord= GetMarcBiblio($biblioNumber);
163 # flag indicating existence of at least one item linked via a host record
165 # adding items linked via host biblios
166 my @hostitemInfos = GetHostItemsInfo($marcrecord);
169 push (@itemInfos,@hostitemInfos);
172 $biblioData->{itemInfos} = \@itemInfos;
173 foreach my $itemInfo (@itemInfos) {
174 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
177 # Compute the priority rank.
178 my $biblio = Koha::Biblios->find( $biblioNumber );
179 my $holds = $biblio->holds;
180 my $rank = $holds->count;
181 $biblioData->{reservecount} = 1; # new reserve
182 while ( my $hold = $holds->next ) {
183 if ( $hold->is_waiting ) {
187 $biblioData->{reservecount}++;
190 $biblioData->{rank} = $rank + 1;
195 # If this is the second time through this script, it
196 # means we are carrying out the hold request, possibly
197 # with a specific item for each biblionumber.
200 if ( $query->param('place_reserve') ) {
203 $reserve_cnt = $patron->holds->count;
206 # List is composed of alternating biblio/item/branch
207 my $selectedItems = $query->param('selecteditems');
209 if ($query->param('reserve_mode') eq 'single') {
210 # This indicates non-JavaScript mode, so there was
211 # only a single biblio number selected.
212 my $bib = $query->param('single_bib');
213 my $item = $query->param("checkitem_$bib");
214 if ($item eq 'any') {
217 my $branch = $query->param('branch');
218 $selectedItems = "$bib/$item/$branch/";
221 $selectedItems =~ s!/$!!;
222 my @selectedItems = split /\//, $selectedItems, -1;
224 # Make sure there is a biblionum/itemnum/branch triplet for each item.
225 # The itemnum can be 'any', meaning next available.
226 my $selectionCount = @selectedItems;
227 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
228 $template->param(message=>1, bad_data=>1);
229 &get_out($query, $cookie, $template->output);
232 my $failed_holds = 0;
233 while (@selectedItems) {
234 my $biblioNum = shift(@selectedItems);
235 my $itemNum = shift(@selectedItems);
236 my $branch = shift(@selectedItems); # i.e., branch code, not name
240 my $singleBranchMode = Koha::Libraries->search->count == 1;
241 if ( $singleBranchMode || !$OPACChooseBranch )
242 { # single branch mode or disabled user choosing
243 $branch = $borr->{'branchcode'};
246 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
247 if ( $itemNum ne '' ) {
248 my $item = Koha::Items->find( $itemNum );
249 my $hostbiblioNum = $item->biblio->biblionumber;
250 if ( $hostbiblioNum ne $biblioNum ) {
251 $biblioNum = $hostbiblioNum;
255 my $biblioData = $biblioDataHash{$biblioNum};
258 # Check for user supplied reserve date
260 if ( C4::Context->preference('AllowHoldDateInFuture')
261 && C4::Context->preference('OPACAllowHoldDateInFuture') )
263 $startdate = $query->param("reserve_date_$biblioNum");
266 my $expiration_date = $query->param("expiration_date_$biblioNum");
268 # If a specific item was selected and the pickup branch is the same as the
269 # holdingbranch, force the value $rank and $found.
270 my $rank = $biblioData->{rank};
271 if ( $itemNum ne '' ) {
272 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
273 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
274 my $item = GetItem($itemNum);
275 if ( $item->{'holdingbranch'} eq $branch ) {
277 unless C4::Context->preference('ReservesNeedReturns');
281 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
283 # Inserts a null into the 'itemnumber' field of 'reserves' table.
286 my $notes = $query->param('notes_'.$biblioNum)||'';
289 && $reserve_cnt >= $maxreserves )
294 unless ( $can_place_hold_if_available_at_pickup ) {
295 my $items_in_this_library = Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch });
296 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
297 my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost => { '!=' => 0 }, damaged => { '!=' => 0 }, } });
298 if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
303 my $itemtype = $query->param('itemtype') || undef;
304 $itemtype = undef if $itemNum;
306 # Here we actually do the reserveration. Stage 3.
308 my $reserve_id = AddReserve(
309 $branch, $borrowernumber, $biblioNum,
310 [$biblioNum], $rank, $startdate,
311 $expiration_date, $notes, $biblioData->{title},
312 $itemNum, $found, $itemtype,
314 $failed_holds++ unless $reserve_id;
319 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
325 # Here we check that the borrower can actually make reserves Stage 1.
329 my $maxoutstanding = C4::Context->preference("maxoutstanding");
330 $template->param( noreserve => 1 ) unless $maxoutstanding;
331 my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
332 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
333 my $amount = sprintf "%.02f", $amountoutstanding;
334 $template->param( message => 1 );
336 $template->param( too_much_oweing => $amount );
339 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
347 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
355 if ( $patron->is_debarred ) {
360 debarred_comment => $borr->{debarredcomment},
361 debarred_date => $borr->{debarred},
365 my $holds = $patron->holds;
366 my $reserves_count = $holds->count;
367 $template->param( RESERVES => $holds->unblessed );
368 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
369 $template->param( message => 1 );
371 $template->param( too_many_reserves => $holds->count );
374 unless ( $noreserves ) {
375 my $requested_reserves_count = scalar( @biblionumbers );
376 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
377 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
381 unless ($noreserves) {
382 $template->param( select_item_types => 1 );
388 # Build the template parameters that will show the info
389 # and items for each biblionumber.
394 my $numBibsAvailable = 0;
395 my $itemdata_enumchron = 0;
397 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
398 $template->param('item_level_itypes' => $itemLevelTypes);
400 foreach my $biblioNum (@biblionumbers) {
402 my @not_available_at = ();
403 my $record = GetMarcBiblio($biblioNum);
404 # Init the bib item with the choices for branch pickup
407 # Get relevant biblio data.
408 my $biblioData = $biblioDataHash{$biblioNum};
410 $template->param(message=>1, bad_biblionumber=>$biblioNum);
411 &get_out($query, $cookie, $template->output);
414 my $frameworkcode = GetFrameworkCode( $biblioData->{biblionumber} );
415 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
416 $biblioLoopIter{title} = $biblioData->{title};
417 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
418 $biblioLoopIter{author} = $biblioData->{author};
419 $biblioLoopIter{rank} = $biblioData->{rank};
420 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
421 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
422 $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
424 if (!$itemLevelTypes && $biblioData->{itemtype}) {
425 $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
426 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$biblioData->{itemtype}}{imageurl};
429 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
430 if ($itemLevelTypes && $itemInfo->{itype}) {
431 $itemInfo->{translated_description} = $itemtypes->{$itemInfo->{itype}}{translated_description};
432 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemtypes->{$itemInfo->{itype}}{imageurl};
435 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
436 $biblioLoopIter{forloan} = 1;
440 my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
441 my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
443 $biblioLoopIter{itemLoop} = [];
444 my $numCopiesAvailable = 0;
445 my $numCopiesOPACAvailable = 0;
446 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
447 my $itemNum = $itemInfo->{itemnumber};
448 my $itemLoopIter = {};
450 $itemLoopIter->{itemnumber} = $itemNum;
451 $itemLoopIter->{barcode} = $itemInfo->{barcode};
452 $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
453 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
454 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
455 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
456 if ($itemLevelTypes) {
457 $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
458 $itemLoopIter->{itype} = $itemInfo->{itype};
459 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
462 # If the holdingbranch is different than the homebranch, we show the
463 # holdingbranch of the document too.
464 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
465 $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
468 # If the item is currently on loan, we display its return date and
469 # change the background color.
470 my $issue = Koha::Checkouts->find( { itemnumber => $itemNum } );
472 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issue->date_due, 'sql'), as_due_date => 1 });
473 $itemLoopIter->{backgroundcolor} = 'onloan';
477 my $item = Koha::Items->find( $itemNum );
478 my $holds = $item->current_holds;
480 if ( my $first_hold = $holds->next ) {
481 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $first_hold->borrowernumber );
482 $itemLoopIter->{backgroundcolor} = 'reserved';
483 $itemLoopIter->{reservedate} = output_pref({ dt => dt_from_string($first_hold->reservedate), dateonly => 1 }); # FIXME Should be formatted in the template
484 $itemLoopIter->{ReservedForBorrowernumber} = $first_hold->borrowernumber;
485 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
486 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
487 $itemLoopIter->{ExpectedAtLibrary} = $first_hold->branchcode;
488 $itemLoopIter->{waitingdate} = $first_hold->waitingdate;
491 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
492 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
494 # Management of the notforloan document
495 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
496 $itemLoopIter->{backgroundcolor} = 'other';
497 $itemLoopIter->{notforloanvalue} =
498 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
501 # Management of lost or long overdue items
502 if ( $itemInfo->{itemlost} ) {
504 # FIXME localized strings should never be in Perl code
505 $itemLoopIter->{message} =
506 $itemInfo->{itemlost} == 1 ? "(lost)"
507 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
509 $itemInfo->{backgroundcolor} = 'other';
512 # Check of the transfered documents
513 my ( $transfertwhen, $transfertfrom, $transfertto ) =
514 GetTransfers($itemNum);
515 if ( $transfertwhen && ($transfertwhen ne '') ) {
516 $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
517 $itemLoopIter->{transfertfrom} = $transfertfrom;
518 $itemLoopIter->{transfertto} = $transfertto;
519 $itemLoopIter->{nocancel} = 1;
522 # if the items belongs to a host record, show link to host record
523 if ( $itemInfo->{biblionumber} ne $biblioNum ) {
524 $biblioLoopIter{hostitemsflag} = 1;
525 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
526 $itemLoopIter->{hosttitle} = GetBiblioData( $itemInfo->{biblionumber} )->{title};
529 # If there is no loan, return and transfer, we show a checkbox.
530 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
532 my $branch = GetReservesControlBranch( $itemInfo, $borr );
534 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
535 $policy_holdallowed &&=
536 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
537 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
539 if ($policy_holdallowed) {
540 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
541 $itemLoopIter->{available} = 1;
542 $numCopiesOPACAvailable++;
543 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
545 $numCopiesAvailable++;
547 unless ( $can_place_hold_if_available_at_pickup ) {
548 my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
549 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
550 if ( $items_in_this_library->count > $nb_of_items_issued ) {
551 push @not_available_at, $itemInfo->{holdingbranch};
556 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itemInfo->{itype} }{imageurl} );
558 # Show serial enumeration when needed
559 if ($itemLoopIter->{enumchron}) {
560 $itemdata_enumchron = 1;
563 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
565 $template->param( itemdata_enumchron => $itemdata_enumchron );
567 if ($numCopiesAvailable > 0) {
569 $biblioLoopIter{bib_available} = 1;
570 $biblioLoopIter{holdable} = 1;
571 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
573 if ($biblioLoopIter{already_reserved}) {
574 $biblioLoopIter{holdable} = undef;
575 $biblioLoopIter{itemholdable} = undef;
577 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
578 $biblioLoopIter{holdable} = undef;
579 $biblioLoopIter{already_patron_possession} = 1;
582 if ( $biblioLoopIter{holdable} ) {
583 @not_available_at = uniq @not_available_at;
584 $biblioLoopIter{not_available_at} = \@not_available_at ;
587 unless ( $can_place_hold_if_available_at_pickup ) {
588 @not_available_at = uniq @not_available_at;
589 $biblioLoopIter{not_available_at} = \@not_available_at ;
590 # The record is not holdable is not available at any of the libraries
591 if ( Koha::Libraries->search->count == @not_available_at ) {
592 $biblioLoopIter{holdable} = 0;
596 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
598 # For multiple holds per record, if a patron has previously placed a hold,
599 # the patron can only place more holds of the same type. That is, if the
600 # patron placed a record level hold, all the holds the patron places must
601 # be record level. If the patron placed an item level hold, all holds
602 # the patron places must be item level
603 my $forced_hold_level = Koha::Holds->search(
605 borrowernumber => $borrowernumber,
606 biblionumber => $biblioNum,
609 )->forced_hold_level();
610 if ($forced_hold_level) {
611 $biblioLoopIter{force_hold} = 1 if $forced_hold_level eq 'item';
612 $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
616 push @$biblioLoop, \%biblioLoopIter;
618 $anyholdable = 1 if $biblioLoopIter{holdable};
622 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
623 $template->param( none_available => 1 );
626 my $show_notes=C4::Context->preference('OpacHoldNotes');
627 $template->param(OpacHoldNotes=>$show_notes);
630 $template->param(bibitemloop => $biblioLoop);
631 # can set reserve date in future
633 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
634 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
637 reserve_in_future => 1,
641 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };