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
;
41 use Date
::Calc qw
/Today Date_to_Days/;
42 use List
::MoreUtils qw
/uniq/;
44 my $maxreserves = C4
::Context
->preference("maxreserves");
48 # if RequestOnOpac (for placing holds) is disabled, leave immediately
49 if ( ! C4
::Context
->preference('RequestOnOpac') ) {
50 print $query->redirect("/cgi-bin/koha/errors/404.pl");
54 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
56 template_name
=> "opac-reserve.tt",
64 my ($show_holds_count, $show_priority);
65 for ( C4
::Context
->preference("OPACShowHoldQueueDetails") ) {
66 m/holds/o and $show_holds_count = 1;
67 m/priority/ and $show_priority = 1;
71 output_html_with_http_headers
(shift,shift,shift); # $query, $cookie, $template->output;
75 # get borrower information ....
76 my ( $borr ) = GetMember
( borrowernumber
=> $borrowernumber );
77 my $patron = Koha
::Patrons
->find( $borrowernumber );
79 my $can_place_hold_if_available_at_pickup = C4
::Context
->preference('OPACHoldsIfAvailableAtPickup');
80 unless ( $can_place_hold_if_available_at_pickup ) {
81 my @patron_categories = split '\|', C4
::Context
->preference('OPACHoldsIfAvailableAtPickupExceptions');
82 if ( @patron_categories ) {
83 $can_place_hold_if_available_at_pickup = grep /$borr->{categorycode}/, @patron_categories;
87 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
88 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
90 if ( $patron->is_expired ) {
92 # cannot reserve, their card has expired and the rules set mean this is not allowed
93 $template->param( message
=> 1, expired_patron
=> 1 );
94 get_out
( $query, $cookie, $template->output );
98 # Pass through any reserve charge
99 my $reservefee = $patron->category->reservefee;
100 if ( $reservefee > 0){
101 $template->param( RESERVE_CHARGE
=> sprintf("%.2f",$reservefee));
104 my $itemtypes = { map { $_->{itemtype
} => $_ } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
106 # There are two ways of calling this script, with a single biblio num
107 # or multiple biblio nums.
108 my $biblionumbers = $query->param('biblionumbers');
109 my $reserveMode = $query->param('reserve_mode');
110 if ($reserveMode && ($reserveMode eq 'single')) {
111 my $bib = $query->param('single_bib');
112 $biblionumbers = "$bib/";
114 if (! $biblionumbers) {
115 $biblionumbers = $query->param('biblionumber');
118 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
119 $template->param(message
=>1, no_biblionumber
=>1);
120 &get_out
($query, $cookie, $template->output);
123 # Pass the numbers to the page so they can be fed back
124 # when the hold is confirmed. TODO: Not necessary?
125 $template->param( biblionumbers
=> $biblionumbers );
127 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
128 my @biblionumbers = split /\//, $biblionumbers;
129 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
131 $template->param(message
=>1, no_biblionumber
=>1);
132 &get_out
($query, $cookie, $template->output);
136 # pass the pickup branch along....
137 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4
::Context
->userenv->{branch
} || '' ;
138 $template->param( branch
=> $branch );
140 # Is the person allowed to choose their branch
141 my $OPACChooseBranch = (C4
::Context
->preference("OPACAllowUserToChooseBranch")) ?
1 : 0;
143 $template->param( choose_branch
=> $OPACChooseBranch);
147 # Build hashes of the requested biblio(item)s and items.
151 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
152 my %itemInfoHash; # Hash of itemnumber to item info.
153 foreach my $biblioNumber (@biblionumbers) {
155 my $biblioData = GetBiblioData
($biblioNumber);
156 $biblioDataHash{$biblioNumber} = $biblioData;
158 my @itemInfos = GetItemsInfo
($biblioNumber);
160 my $marcrecord= GetMarcBiblio
($biblioNumber);
162 # flag indicating existence of at least one item linked via a host record
164 # adding items linked via host biblios
165 my @hostitemInfos = GetHostItemsInfo
($marcrecord);
168 push (@itemInfos,@hostitemInfos);
171 $biblioData->{itemInfos
} = \
@itemInfos;
172 foreach my $itemInfo (@itemInfos) {
173 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
176 # Compute the priority rank.
177 my $biblio = Koha
::Biblios
->find( $biblioNumber );
178 my $holds = $biblio->holds;
179 my $rank = $holds->count;
180 $biblioData->{reservecount
} = 1; # new reserve
181 while ( my $hold = $holds->next ) {
182 if ( $hold->is_waiting ) {
186 $biblioData->{reservecount
}++;
189 $biblioData->{rank
} = $rank + 1;
194 # If this is the second time through this script, it
195 # means we are carrying out the hold request, possibly
196 # with a specific item for each biblionumber.
199 if ( $query->param('place_reserve') ) {
202 my $patron = Koha
::Patrons
->find( $borrowernumber );
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 $hostbiblioNum = GetBiblionumberFromItemnumber
($itemNum);
249 if ( $hostbiblioNum ne $biblioNum ) {
250 $biblioNum = $hostbiblioNum;
254 my $biblioData = $biblioDataHash{$biblioNum};
257 # Check for user supplied reserve date
259 if ( C4
::Context
->preference('AllowHoldDateInFuture')
260 && C4
::Context
->preference('OPACAllowHoldDateInFuture') )
262 $startdate = $query->param("reserve_date_$biblioNum");
265 my $expiration_date = $query->param("expiration_date_$biblioNum");
267 # If a specific item was selected and the pickup branch is the same as the
268 # holdingbranch, force the value $rank and $found.
269 my $rank = $biblioData->{rank
};
270 if ( $itemNum ne '' ) {
271 $canreserve = 1 if CanItemBeReserved
( $borrowernumber, $itemNum ) eq 'OK';
272 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
273 my $item = GetItem
($itemNum);
274 if ( $item->{'holdingbranch'} eq $branch ) {
276 unless C4
::Context
->preference('ReservesNeedReturns');
280 $canreserve = 1 if CanBookBeReserved
( $borrowernumber, $biblioNum ) 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 ) = GetMemberAccountRecords
($borrowernumber);
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 ( $borr->{gonenoaddress
} && ($borr->{gonenoaddress
} == 1) ) {
346 if ( $borr->{lost
} && ($borr->{lost
} == 1) ) {
354 my $patron = Koha
::Patrons
->find( $borrowernumber );
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 $issues= GetItemIssue
($itemNum);
471 if ( $issues->{'date_due'} ) {
472 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issues->{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 };