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 my $patron = Koha
::Patrons
->find( $borrowernumber );
78 my $can_place_hold_if_available_at_pickup = C4
::Context
->preference('OPACHoldsIfAvailableAtPickup');
79 unless ( $can_place_hold_if_available_at_pickup ) {
80 my @patron_categories = split '\|', C4
::Context
->preference('OPACHoldsIfAvailableAtPickupExceptions');
81 if ( @patron_categories ) {
82 my $categorycode = $patron->categorycode;
83 $can_place_hold_if_available_at_pickup = grep /^$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') || $patron->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 $reserve_cnt = $patron->holds->count;
205 # List is composed of alternating biblio/item/branch
206 my $selectedItems = $query->param('selecteditems');
208 if ($query->param('reserve_mode') eq 'single') {
209 # This indicates non-JavaScript mode, so there was
210 # only a single biblio number selected.
211 my $bib = $query->param('single_bib');
212 my $item = $query->param("checkitem_$bib");
213 if ($item eq 'any') {
216 my $branch = $query->param('branch');
217 $selectedItems = "$bib/$item/$branch/";
220 $selectedItems =~ s!/$!!;
221 my @selectedItems = split /\//, $selectedItems, -1;
223 # Make sure there is a biblionum/itemnum/branch triplet for each item.
224 # The itemnum can be 'any', meaning next available.
225 my $selectionCount = @selectedItems;
226 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
227 $template->param(message
=>1, bad_data
=>1);
228 &get_out
($query, $cookie, $template->output);
231 my $failed_holds = 0;
232 while (@selectedItems) {
233 my $biblioNum = shift(@selectedItems);
234 my $itemNum = shift(@selectedItems);
235 my $branch = shift(@selectedItems); # i.e., branch code, not name
239 my $singleBranchMode = Koha
::Libraries
->search->count == 1;
240 if ( $singleBranchMode || !$OPACChooseBranch )
241 { # single branch mode or disabled user choosing
242 $branch = $patron->branchcode;
245 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
246 if ( $itemNum ne '' ) {
247 my $item = Koha
::Items
->find( $itemNum );
248 my $hostbiblioNum = $item->biblio->biblionumber;
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 ( $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;
396 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
397 $template->param('item_level_itypes' => $itemLevelTypes);
399 foreach my $biblioNum (@biblionumbers) {
401 my @not_available_at = ();
402 my $record = GetMarcBiblio
($biblioNum);
403 # Init the bib item with the choices for branch pickup
406 # Get relevant biblio data.
407 my $biblioData = $biblioDataHash{$biblioNum};
409 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
410 &get_out
($query, $cookie, $template->output);
413 my $frameworkcode = GetFrameworkCode
( $biblioData->{biblionumber
} );
414 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
415 $biblioLoopIter{title
} = $biblioData->{title
};
416 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, $frameworkcode);
417 $biblioLoopIter{author
} = $biblioData->{author
};
418 $biblioLoopIter{rank
} = $biblioData->{rank
};
419 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
420 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
421 $biblioLoopIter{reqholdnotes
}=0; #TODO: For future use
423 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
424 $biblioLoopIter{translated_description
} = $itemtypes->{$biblioData->{itemtype
}}{translated_description
};
425 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemtypes->{$biblioData->{itemtype
}}{imageurl
};
428 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
429 if ($itemLevelTypes && $itemInfo->{itype
}) {
430 $itemInfo->{translated_description
} = $itemtypes->{$itemInfo->{itype
}}{translated_description
};
431 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemtypes->{$itemInfo->{itype
}}{imageurl
};
434 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
435 $biblioLoopIter{forloan
} = 1;
439 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
440 my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
442 $biblioLoopIter{itemLoop
} = [];
443 my $numCopiesAvailable = 0;
444 my $numCopiesOPACAvailable = 0;
445 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
446 my $itemNum = $itemInfo->{itemnumber
};
447 my $itemLoopIter = {};
449 $itemLoopIter->{itemnumber
} = $itemNum;
450 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
451 $itemLoopIter->{homeBranchName
} = $itemInfo->{homebranch
};
452 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
453 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
454 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
455 if ($itemLevelTypes) {
456 $itemLoopIter->{translated_description
} = $itemInfo->{translated_description
};
457 $itemLoopIter->{itype
} = $itemInfo->{itype
};
458 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
461 # If the holdingbranch is different than the homebranch, we show the
462 # holdingbranch of the document too.
463 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
464 $itemLoopIter->{holdingBranchName
} = $itemInfo->{holdingbranch
};
467 # If the item is currently on loan, we display its return date and
468 # change the background color.
469 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemNum } );
471 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issue->date_due, 'sql'), as_due_date
=> 1 });
472 $itemLoopIter->{backgroundcolor
} = 'onloan';
476 my $item = Koha
::Items
->find( $itemNum );
477 my $holds = $item->current_holds;
479 if ( my $first_hold = $holds->next ) {
480 my $patron = Koha
::Patrons
->find( $first_hold->borrowernumber );
481 $itemLoopIter->{backgroundcolor
} = 'reserved';
482 $itemLoopIter->{reservedate
} = output_pref
({ dt
=> dt_from_string
($first_hold->reservedate), dateonly
=> 1 }); # FIXME Should be formatted in the template
483 $itemLoopIter->{ReservedForBorrowernumber
} = $first_hold->borrowernumber;
484 $itemLoopIter->{ReservedForSurname
} = $patron->surname;
485 $itemLoopIter->{ReservedForFirstname
} = $patron->firstname;
486 $itemLoopIter->{ExpectedAtLibrary
} = $first_hold->branchcode;
487 $itemLoopIter->{waitingdate
} = $first_hold->waitingdate;
490 $itemLoopIter->{notforloan
} = $itemInfo->{notforloan
};
491 $itemLoopIter->{itemnotforloan
} = $itemInfo->{itemnotforloan
};
493 # Management of the notforloan document
494 if ( $itemLoopIter->{notforloan
} || $itemLoopIter->{itemnotforloan
}) {
495 $itemLoopIter->{backgroundcolor
} = 'other';
496 $itemLoopIter->{notforloanvalue
} =
497 $notforloan_label_of->{ $itemLoopIter->{notforloan
} };
500 # Management of lost or long overdue items
501 if ( $itemInfo->{itemlost
} ) {
503 # FIXME localized strings should never be in Perl code
504 $itemLoopIter->{message
} =
505 $itemInfo->{itemlost
} == 1 ?
"(lost)"
506 : $itemInfo->{itemlost
} == 2 ?
"(long overdue)"
508 $itemInfo->{backgroundcolor
} = 'other';
511 # Check of the transfered documents
512 my ( $transfertwhen, $transfertfrom, $transfertto ) =
513 GetTransfers
($itemNum);
514 if ( $transfertwhen && ($transfertwhen ne '') ) {
515 $itemLoopIter->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
($transfertwhen), dateonly
=> 1 });
516 $itemLoopIter->{transfertfrom
} = $transfertfrom;
517 $itemLoopIter->{transfertto
} = $transfertto;
518 $itemLoopIter->{nocancel
} = 1;
521 # if the items belongs to a host record, show link to host record
522 if ( $itemInfo->{biblionumber
} ne $biblioNum ) {
523 $biblioLoopIter{hostitemsflag
} = 1;
524 $itemLoopIter->{hostbiblionumber
} = $itemInfo->{biblionumber
};
525 $itemLoopIter->{hosttitle
} = GetBiblioData
( $itemInfo->{biblionumber
} )->{title
};
528 # If there is no loan, return and transfer, we show a checkbox.
529 $itemLoopIter->{notforloan
} = $itemLoopIter->{notforloan
} || 0;
531 my $patron_unblessed = $patron->unblessed;
532 my $branch = GetReservesControlBranch
( $itemInfo, $patron_unblessed );
534 my $policy_holdallowed = !$itemLoopIter->{already_reserved
};
535 $policy_holdallowed &&=
536 IsAvailableForItemLevelRequest
($itemInfo,$patron_unblessed) &&
537 CanItemBeReserved
($borrowernumber,$itemNum) eq 'OK';
539 if ($policy_holdallowed) {
540 if ( my $hold_allowed = OPACItemHoldsAllowed
( $itemInfo, $patron_unblessed ) ) {
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 };