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;
74 my $patron = Koha
::Patrons
->find( $borrowernumber );
76 my $can_place_hold_if_available_at_pickup = C4
::Context
->preference('OPACHoldsIfAvailableAtPickup');
77 unless ( $can_place_hold_if_available_at_pickup ) {
78 my @patron_categories = split '\|', C4
::Context
->preference('OPACHoldsIfAvailableAtPickupExceptions');
79 if ( @patron_categories ) {
80 my $categorycode = $patron->categorycode;
81 $can_place_hold_if_available_at_pickup = grep /^$categorycode$/, @patron_categories;
85 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
86 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
88 if ( $patron->is_expired ) {
90 # cannot reserve, their card has expired and the rules set mean this is not allowed
91 $template->param( message
=> 1, expired_patron
=> 1 );
92 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };
96 # Pass through any reserve charge
97 my $reservefee = $patron->category->reservefee;
98 if ( $reservefee > 0){
99 $template->param( RESERVE_CHARGE
=> $reservefee);
102 my $itemtypes = { map { $_->{itemtype
} => $_ } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
104 # There are two ways of calling this script, with a single biblio num
105 # or multiple biblio nums.
106 my $biblionumbers = $query->param('biblionumbers');
107 my $reserveMode = $query->param('reserve_mode');
108 if ($reserveMode && ($reserveMode eq 'single')) {
109 my $bib = $query->param('single_bib');
110 $biblionumbers = "$bib/";
112 if (! $biblionumbers) {
113 $biblionumbers = $query->param('biblionumber');
116 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
117 $template->param(message
=>1, no_biblionumber
=>1);
118 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };
121 # Pass the numbers to the page so they can be fed back
122 # when the hold is confirmed. TODO: Not necessary?
123 $template->param( biblionumbers
=> $biblionumbers );
125 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
126 my @biblionumbers = split /\//, $biblionumbers;
127 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
129 $template->param(message
=>1, no_biblionumber
=>1);
130 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };
134 # pass the pickup branch along....
135 my $branch = $query->param('branch') || $patron->branchcode || C4
::Context
->userenv->{branch
} || '' ;
136 $template->param( branch
=> $branch );
140 # Build hashes of the requested biblio(item)s and items.
144 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
145 my %itemInfoHash; # Hash of itemnumber to item info.
146 foreach my $biblioNumber (@biblionumbers) {
148 my $biblioData = GetBiblioData
($biblioNumber);
149 $biblioDataHash{$biblioNumber} = $biblioData;
151 my @itemInfos = GetItemsInfo
($biblioNumber);
153 my $marcrecord= GetMarcBiblio
({ biblionumber
=> $biblioNumber });
155 # flag indicating existence of at least one item linked via a host record
157 # adding items linked via host biblios
158 my @hostitemInfos = GetHostItemsInfo
($marcrecord);
161 push (@itemInfos,@hostitemInfos);
164 $biblioData->{itemInfos
} = \
@itemInfos;
165 foreach my $itemInfo (@itemInfos) {
166 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
169 # Compute the priority rank.
170 my $biblio = Koha
::Biblios
->find( $biblioNumber );
171 $biblioData->{object
} = $biblio;
172 my $holds = $biblio->holds;
173 my $rank = $holds->count;
174 $biblioData->{reservecount
} = 1; # new reserve
175 while ( my $hold = $holds->next ) {
176 if ( $hold->is_waiting ) {
180 $biblioData->{reservecount
}++;
183 $biblioData->{rank
} = $rank + 1;
188 # If this is the second time through this script, it
189 # means we are carrying out the hold request, possibly
190 # with a specific item for each biblionumber.
193 if ( $query->param('place_reserve') ) {
196 $reserve_cnt = $patron->holds->count;
199 # List is composed of alternating biblio/item/branch
200 my $selectedItems = $query->param('selecteditems');
202 if ($query->param('reserve_mode') eq 'single') {
203 # This indicates non-JavaScript mode, so there was
204 # only a single biblio number selected.
205 my $bib = $query->param('single_bib');
206 my $item = $query->param("checkitem_$bib");
207 if ($item eq 'any') {
210 my $branch = $query->param('branch');
211 $selectedItems = "$bib/$item/$branch/";
214 $selectedItems =~ s!/$!!;
215 my @selectedItems = split /\//, $selectedItems, -1;
217 # Make sure there is a biblionum/itemnum/branch triplet for each item.
218 # The itemnum can be 'any', meaning next available.
219 my $selectionCount = @selectedItems;
220 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
221 $template->param(message
=>1, bad_data
=>1);
222 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };
225 my $failed_holds = 0;
226 while (@selectedItems) {
227 my $biblioNum = shift(@selectedItems);
228 my $itemNum = shift(@selectedItems);
229 my $branch = shift(@selectedItems); # i.e., branch code, not name
233 my $singleBranchMode = Koha
::Libraries
->search->count == 1;
234 if ( $singleBranchMode || ! C4
::Context
->preference("OPACAllowUserToChooseBranch") )
235 { # single branch mode or disabled user choosing
236 $branch = $patron->branchcode;
239 # When choosing a specific item, the default pickup library should be dictated by the default hold policy
240 if ( ! C4
::Context
->preference("OPACAllowUserToChooseBranch") && $itemNum ) {
241 my $item = Koha
::Items
->find( $itemNum );
242 my $type = $item->effective_itemtype;
243 my $rule = GetBranchItemRule
( $patron->branchcode, $type );
245 if ( $rule->{hold_fulfillment_policy
} eq 'any' ) {
246 $branch = $patron->branchcode;
248 my $policy = $rule->{hold_fulfillment_policy
};
249 $branch = $item->$policy;
253 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
254 if ( $itemNum ne '' ) {
255 my $item = Koha
::Items
->find( $itemNum );
256 my $hostbiblioNum = $item->biblio->biblionumber;
257 if ( $hostbiblioNum ne $biblioNum ) {
258 $biblioNum = $hostbiblioNum;
262 my $biblioData = $biblioDataHash{$biblioNum};
265 # Check for user supplied reserve date
267 if ( C4
::Context
->preference('AllowHoldDateInFuture')
268 && C4
::Context
->preference('OPACAllowHoldDateInFuture') )
270 $startdate = $query->param("reserve_date_$biblioNum");
273 my $expiration_date = $query->param("expiration_date_$biblioNum");
275 my $rank = $biblioData->{rank
};
276 if ( $itemNum ne '' ) {
277 $canreserve = 1 if CanItemBeReserved
( $borrowernumber, $itemNum )->{status
} eq 'OK';
280 $canreserve = 1 if CanBookBeReserved
( $borrowernumber, $biblioNum )->{status
} eq 'OK';
282 # Inserts a null into the 'itemnumber' field of 'reserves' table.
285 my $notes = $query->param('notes_'.$biblioNum)||'';
288 && $reserve_cnt >= $maxreserves )
293 unless ( $can_place_hold_if_available_at_pickup ) {
294 my $items_in_this_library = Koha
::Items
->search({ biblionumber
=> $biblioNum, holdingbranch
=> $branch });
295 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
296 my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost
=> { '!=' => 0 }, damaged
=> { '!=' => 0 }, } });
297 if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
302 my $itemtype = $query->param('itemtype') || undef;
303 $itemtype = undef if $itemNum;
305 # Here we actually do the reserveration. Stage 3.
307 my $reserve_id = AddReserve
(
308 $branch, $borrowernumber, $biblioNum,
309 [$biblioNum], $rank, $startdate,
310 $expiration_date, $notes, $biblioData->{title
},
311 $itemNum, $found, $itemtype,
313 $failed_holds++ unless $reserve_id;
318 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ?
"failed_holds=$failed_holds" : q
|| ) . "#opac-user-holds");
324 # Here we check that the borrower can actually make reserves Stage 1.
328 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
329 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
330 my $amountoutstanding = $patron->account->balance;
331 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
332 my $amount = sprintf "%.02f", $amountoutstanding;
333 $template->param( message
=> 1 );
335 $template->param( too_much_oweing
=> $amount );
338 if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
346 if ( $patron->lost && ($patron->lost == 1) ) {
354 if ( $patron->is_debarred ) {
359 debarred_comment
=> $patron->debarredcomment,
360 debarred_date
=> $patron->debarred,
364 my $holds = $patron->holds;
365 my $reserves_count = $holds->count;
366 $template->param( RESERVES
=> $holds->unblessed );
367 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
368 $template->param( message
=> 1 );
370 $template->param( too_many_reserves
=> $holds->count );
373 unless ( $noreserves ) {
374 my $requested_reserves_count = scalar( @biblionumbers );
375 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
376 $template->param( new_reserves_allowed
=> $maxreserves - $reserves_count );
380 unless ($noreserves) {
381 $template->param( select_item_types
=> 1 );
387 # Build the template parameters that will show the info
388 # and items for each biblionumber.
393 my $numBibsAvailable = 0;
394 my $itemdata_enumchron = 0;
395 my $itemdata_ccode = 0;
397 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
398 my $pickup_locations = Koha
::Libraries
->search({ pickup_location
=> 1 });
399 $template->param('item_level_itypes' => $itemLevelTypes);
401 foreach my $biblioNum (@biblionumbers) {
403 my $record = GetMarcBiblio
({ biblionumber
=> $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 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };
414 my @not_available_at = ();
415 my $biblio = $biblioData->{object
};
416 foreach my $library ( $pickup_locations->as_list ) {
417 push( @not_available_at, $library->branchcode ) unless $biblio->can_be_transferred({ to
=> $library });
420 my $frameworkcode = GetFrameworkCode
( $biblioData->{biblionumber
} );
421 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
422 $biblioLoopIter{title
} = $biblioData->{title
};
423 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, $frameworkcode);
424 $biblioLoopIter{author
} = $biblioData->{author
};
425 $biblioLoopIter{rank
} = $biblioData->{rank
};
426 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
427 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
428 $biblioLoopIter{reqholdnotes
}=0; #TODO: For future use
430 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
431 $biblioLoopIter{translated_description
} = $itemtypes->{$biblioData->{itemtype
}}{translated_description
};
432 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemtypes->{$biblioData->{itemtype
}}{imageurl
};
435 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
436 if ($itemLevelTypes && $itemInfo->{itype
}) {
437 $itemInfo->{translated_description
} = $itemtypes->{$itemInfo->{itype
}}{translated_description
};
438 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemtypes->{$itemInfo->{itype
}}{imageurl
};
441 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
442 $biblioLoopIter{forloan
} = 1;
446 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
447 my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
449 $biblioLoopIter{itemLoop
} = [];
450 my $numCopiesAvailable = 0;
451 my $numCopiesOPACAvailable = 0;
452 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
453 my $itemNum = $itemInfo->{itemnumber
};
454 my $item = Koha
::Items
->find( $itemNum );
455 my $itemLoopIter = {};
457 $itemLoopIter->{itemnumber
} = $itemNum;
458 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
459 $itemLoopIter->{homeBranchName
} = $itemInfo->{homebranch
};
460 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
461 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
462 $itemLoopIter->{ccode
} = $itemInfo->{ccode
};
463 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
464 if ($itemLevelTypes) {
465 $itemLoopIter->{translated_description
} = $itemInfo->{translated_description
};
466 $itemLoopIter->{itype
} = $itemInfo->{itype
};
467 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
470 # If the holdingbranch is different than the homebranch, we show the
471 # holdingbranch of the document too.
472 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
473 $itemLoopIter->{holdingBranchName
} = $itemInfo->{holdingbranch
};
476 # If the item is currently on loan, we display its return date and
477 # change the background color.
478 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemNum } );
480 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issue->date_due, 'sql'), as_due_date
=> 1 });
481 $itemLoopIter->{backgroundcolor
} = 'onloan';
485 my $holds = $item->current_holds;
487 if ( my $first_hold = $holds->next ) {
488 $itemLoopIter->{backgroundcolor
} = 'reserved';
489 $itemLoopIter->{reservedate
} = output_pref
({ dt
=> dt_from_string
($first_hold->reservedate), dateonly
=> 1 }); # FIXME Should be formatted in the template
490 $itemLoopIter->{ExpectedAtLibrary
} = $first_hold->branchcode;
491 $itemLoopIter->{waitingdate
} = $first_hold->waitingdate;
494 $itemLoopIter->{notforloan
} = $itemInfo->{notforloan
};
495 $itemLoopIter->{itemnotforloan
} = $itemInfo->{itemnotforloan
};
497 # Management of the notforloan document
498 if ( $itemLoopIter->{notforloan
} || $itemLoopIter->{itemnotforloan
}) {
499 $itemLoopIter->{backgroundcolor
} = 'other';
500 $itemLoopIter->{notforloanvalue
} =
501 $notforloan_label_of->{ $itemLoopIter->{notforloan
} };
504 # Management of lost or long overdue items
505 if ( $itemInfo->{itemlost
} ) {
507 # FIXME localized strings should never be in Perl code
508 $itemLoopIter->{message
} =
509 $itemInfo->{itemlost
} == 1 ?
"(lost)"
510 : $itemInfo->{itemlost
} == 2 ?
"(long overdue)"
512 $itemInfo->{backgroundcolor
} = 'other';
515 # Check of the transferred documents
516 my ( $transfertwhen, $transfertfrom, $transfertto ) =
517 GetTransfers
($itemNum);
518 if ( $transfertwhen && ($transfertwhen ne '') ) {
519 $itemLoopIter->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
($transfertwhen), dateonly
=> 1 });
520 $itemLoopIter->{transfertfrom
} = $transfertfrom;
521 $itemLoopIter->{transfertto
} = $transfertto;
522 $itemLoopIter->{nocancel
} = 1;
525 # if the items belongs to a host record, show link to host record
526 if ( $itemInfo->{biblionumber
} ne $biblioNum ) {
527 $biblioLoopIter{hostitemsflag
} = 1;
528 $itemLoopIter->{hostbiblionumber
} = $itemInfo->{biblionumber
};
529 $itemLoopIter->{hosttitle
} = Koha
::Biblios
->find( $itemInfo->{biblionumber
} )->title;
532 # If there is no loan, return and transfer, we show a checkbox.
533 $itemLoopIter->{notforloan
} = $itemLoopIter->{notforloan
} || 0;
535 my $patron_unblessed = $patron->unblessed;
536 my $branch = GetReservesControlBranch
( $itemInfo, $patron_unblessed );
538 my $policy_holdallowed = !$itemLoopIter->{already_reserved
};
539 $policy_holdallowed &&=
540 IsAvailableForItemLevelRequest
($itemInfo,$patron_unblessed) &&
541 CanItemBeReserved
( $borrowernumber, $itemNum )->{status
} eq 'OK';
543 if ($policy_holdallowed) {
544 my $opac_hold_policy = Koha
::IssuingRules
->get_opacitemholds_policy( { item
=> $item, patron
=> $patron } );
545 if ( $opac_hold_policy ne 'N' ) { # If Y or F
546 $itemLoopIter->{available
} = 1;
547 $numCopiesOPACAvailable++;
548 $biblioLoopIter{force_hold
} = 1 if $opac_hold_policy eq 'F';
550 $numCopiesAvailable++;
552 unless ( $can_place_hold_if_available_at_pickup ) {
553 my $items_in_this_library = Koha
::Items
->search({ biblionumber
=> $itemInfo->{biblionumber
}, holdingbranch
=> $itemInfo->{holdingbranch
} });
554 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
555 if ( $items_in_this_library->count > $nb_of_items_issued ) {
556 push @not_available_at, $itemInfo->{holdingbranch
};
561 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemtypes->{ $itemInfo->{itype
} }{imageurl
} );
563 # Show serial enumeration when needed
564 if ($itemLoopIter->{enumchron
}) {
565 $itemdata_enumchron = 1;
567 # Show collection when needed
568 if ($itemLoopIter->{ccode
}) {
572 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
575 itemdata_enumchron
=> $itemdata_enumchron,
576 itemdata_ccode
=> $itemdata_ccode,
579 if ($numCopiesAvailable > 0) {
581 $biblioLoopIter{bib_available
} = 1;
582 $biblioLoopIter{holdable
} = 1;
583 $biblioLoopIter{itemholdable
} = 1 if $numCopiesOPACAvailable;
585 if ($biblioLoopIter{already_reserved
}) {
586 $biblioLoopIter{holdable
} = undef;
587 $biblioLoopIter{itemholdable
} = undef;
589 if(not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowernumber,$biblioNum)) {
590 $biblioLoopIter{holdable
} = undef;
591 $biblioLoopIter{already_patron_possession
} = 1;
594 if ( $biblioLoopIter{holdable
} ) {
595 @not_available_at = uniq
@not_available_at;
596 $biblioLoopIter{not_available_at
} = \
@not_available_at ;
599 unless ( $can_place_hold_if_available_at_pickup ) {
600 @not_available_at = uniq
@not_available_at;
601 $biblioLoopIter{not_available_at
} = \
@not_available_at ;
602 # The record is not holdable is not available at any of the libraries
603 if ( Koha
::Libraries
->search->count == @not_available_at ) {
604 $biblioLoopIter{holdable
} = 0;
608 $biblioLoopIter{holdable
} &&= CanBookBeReserved
( $borrowernumber, $biblioNum )->{status
} eq 'OK';
610 # For multiple holds per record, if a patron has previously placed a hold,
611 # the patron can only place more holds of the same type. That is, if the
612 # patron placed a record level hold, all the holds the patron places must
613 # be record level. If the patron placed an item level hold, all holds
614 # the patron places must be item level
615 my $forced_hold_level = Koha
::Holds
->search(
617 borrowernumber
=> $borrowernumber,
618 biblionumber
=> $biblioNum,
621 )->forced_hold_level();
622 if ($forced_hold_level) {
623 $biblioLoopIter{force_hold
} = 1 if $forced_hold_level eq 'item';
624 $biblioLoopIter{itemholdable
} = 0 if $forced_hold_level eq 'record';
628 push @
$biblioLoop, \
%biblioLoopIter;
630 $anyholdable = 1 if $biblioLoopIter{holdable
};
633 unless ($pickup_locations->count) {
634 $numBibsAvailable = 0;
638 no_pickup_locations
=> 1
642 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
643 $template->param( none_available
=> 1 );
646 if (scalar @biblionumbers > 1) {
647 $template->param( multi_hold
=> 1);
650 my $show_notes=C4
::Context
->preference('OpacHoldNotes');
651 $template->param(OpacHoldNotes
=>$show_notes);
654 $template->param(bibitemloop
=> $biblioLoop);
655 # can set reserve date in future
657 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
658 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
661 reserve_in_future
=> 1,
665 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };