Bug 13177: Help page for Rotating Collections
[koha.git] / opac / opac-reserve.pl
blob60232cc6ee191b7b79b2d92425385f1695957cf0
1 #!/usr/bin/perl
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>.
21 use strict;
22 use warnings;
23 use CGI qw ( -utf8 );
24 use C4::Auth; # checkauth, getborrowernumber.
25 use C4::Koha;
26 use C4::Circulation;
27 use C4::Reserves;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Output;
31 use C4::Context;
32 use C4::Members;
33 use C4::Branch; # GetBranches
34 use C4::Overdues;
35 use C4::Debug;
36 use Koha::DateUtils;
37 use Koha::Borrower::Debarments qw(IsDebarred);
38 use Date::Calc qw/Today Date_to_Days/;
39 # use Data::Dumper;
41 my $maxreserves = C4::Context->preference("maxreserves");
43 my $query = new CGI;
45 # if RequestOnOpac (for placing holds) is disabled, leave immediately
46 if ( ! C4::Context->preference('RequestOnOpac') ) {
47 print $query->redirect("/cgi-bin/koha/errors/404.pl");
48 exit;
51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
53 template_name => "opac-reserve.tt",
54 query => $query,
55 type => "opac",
56 authnotrequired => 0,
57 debug => 1,
61 my ($show_holds_count, $show_priority);
62 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
63 m/holds/o and $show_holds_count = 1;
64 m/priority/ and $show_priority = 1;
67 sub get_out {
68 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
69 exit;
72 # get borrower information ....
73 my ( $borr ) = GetMemberDetails( $borrowernumber );
75 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
76 if ( $borr->{'BlockExpiredPatronOpacActions'} ) {
78 if ( $borr->{'is_expired'} ) {
80 # cannot reserve, their card has expired and the rules set mean this is not allowed
81 $template->param( message => 1, expired_patron => 1 );
82 get_out( $query, $cookie, $template->output );
86 # Pass through any reserve charge
87 if ($borr->{reservefee} > 0){
88 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
90 # get branches and itemtypes
91 my $branches = GetBranches();
92 my $itemTypes = GetItemTypes();
94 # There are two ways of calling this script, with a single biblio num
95 # or multiple biblio nums.
96 my $biblionumbers = $query->param('biblionumbers');
97 my $reserveMode = $query->param('reserve_mode');
98 if ($reserveMode && ($reserveMode eq 'single')) {
99 my $bib = $query->param('single_bib');
100 $biblionumbers = "$bib/";
102 if (! $biblionumbers) {
103 $biblionumbers = $query->param('biblionumber');
106 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
107 $template->param(message=>1, no_biblionumber=>1);
108 &get_out($query, $cookie, $template->output);
111 # Pass the numbers to the page so they can be fed back
112 # when the hold is confirmed. TODO: Not necessary?
113 $template->param( biblionumbers => $biblionumbers );
115 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
116 my @biblionumbers = split /\//, $biblionumbers;
117 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
118 # TODO: New message?
119 $template->param(message=>1, no_biblionumber=>1);
120 &get_out($query, $cookie, $template->output);
124 # pass the pickup branch along....
125 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
126 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
127 $template->param( branch => $branch );
129 # make branch selection options...
130 my $branchloop = GetBranchesLoop($branch);
132 # Is the person allowed to choose their branch
133 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
135 $template->param( choose_branch => $OPACChooseBranch);
139 # Build hashes of the requested biblio(item)s and items.
143 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
144 my %itemInfoHash; # Hash of itemnumber to item info.
145 foreach my $biblioNumber (@biblionumbers) {
147 my $biblioData = GetBiblioData($biblioNumber);
148 $biblioDataHash{$biblioNumber} = $biblioData;
150 my @itemInfos = GetItemsInfo($biblioNumber);
152 my $marcrecord= GetMarcBiblio($biblioNumber);
154 # flag indicating existence of at least one item linked via a host record
155 my $hostitemsflag;
156 # adding items linked via host biblios
157 my @hostitemInfos = GetHostItemsInfo($marcrecord);
158 if (@hostitemInfos){
159 $hostitemsflag =1;
160 push (@itemInfos,@hostitemInfos);
163 $biblioData->{itemInfos} = \@itemInfos;
164 foreach my $itemInfo (@itemInfos) {
165 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
168 # Compute the priority rank.
169 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
170 my $rank = scalar( @$reserves );
171 $biblioData->{reservecount} = 1; # new reserve
172 foreach my $res (@{$reserves}) {
173 my $found = $res->{found};
174 if ( $found && $found eq 'W' ) {
175 $rank--;
177 else {
178 $biblioData->{reservecount}++;
181 $biblioData->{rank} = $rank + 1;
186 # If this is the second time through this script, it
187 # means we are carrying out the hold request, possibly
188 # with a specific item for each biblionumber.
191 if ( $query->param('place_reserve') ) {
192 my $reserve_cnt = 0;
193 if ($maxreserves) {
194 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
197 # List is composed of alternating biblio/item/branch
198 my $selectedItems = $query->param('selecteditems');
200 if ($query->param('reserve_mode') eq 'single') {
201 # This indicates non-JavaScript mode, so there was
202 # only a single biblio number selected.
203 my $bib = $query->param('single_bib');
204 my $item = $query->param("checkitem_$bib");
205 if ($item eq 'any') {
206 $item = '';
208 my $branch = $query->param('branch');
209 $selectedItems = "$bib/$item/$branch/";
212 $selectedItems =~ s!/$!!;
213 my @selectedItems = split /\//, $selectedItems, -1;
215 # Make sure there is a biblionum/itemnum/branch triplet for each item.
216 # The itemnum can be 'any', meaning next available.
217 my $selectionCount = @selectedItems;
218 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
219 $template->param(message=>1, bad_data=>1);
220 &get_out($query, $cookie, $template->output);
223 my $failed_holds = 0;
224 while (@selectedItems) {
225 my $biblioNum = shift(@selectedItems);
226 my $itemNum = shift(@selectedItems);
227 my $branch = shift(@selectedItems); # i.e., branch code, not name
229 my $canreserve = 0;
231 my $singleBranchMode = C4::Context->preference("singleBranchMode");
232 if ( $singleBranchMode || !$OPACChooseBranch )
233 { # single branch mode or disabled user choosing
234 $branch = $borr->{'branchcode'};
237 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
238 if ( $itemNum ne '' ) {
239 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
240 if ( $hostbiblioNum ne $biblioNum ) {
241 $biblioNum = $hostbiblioNum;
245 my $biblioData = $biblioDataHash{$biblioNum};
246 my $found;
248 # Check for user supplied reserve date
249 my $startdate;
250 if ( C4::Context->preference('AllowHoldDateInFuture')
251 && C4::Context->preference('OPACAllowHoldDateInFuture') )
253 $startdate = $query->param("reserve_date_$biblioNum");
256 my $expiration_date = $query->param("expiration_date_$biblioNum");
258 # If a specific item was selected and the pickup branch is the same as the
259 # holdingbranch, force the value $rank and $found.
260 my $rank = $biblioData->{rank};
261 if ( $itemNum ne '' ) {
262 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
263 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
264 my $item = GetItem($itemNum);
265 if ( $item->{'holdingbranch'} eq $branch ) {
266 $found = 'W'
267 unless C4::Context->preference('ReservesNeedReturns');
270 else {
271 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
273 # Inserts a null into the 'itemnumber' field of 'reserves' table.
274 $itemNum = undef;
276 my $notes = $query->param('notes_'.$biblioNum)||'';
278 if ( $maxreserves
279 && $reserve_cnt >= $maxreserves )
281 $canreserve = 0;
284 # Here we actually do the reserveration. Stage 3.
285 if ($canreserve) {
286 my $reserve_id = AddReserve(
287 $branch, $borrowernumber,
288 $biblioNum,
289 [$biblioNum], $rank,
290 $startdate, $expiration_date,
291 $notes, $biblioData->{title},
292 $itemNum, $found
294 $failed_holds++ unless $reserve_id;
295 ++$reserve_cnt;
299 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
300 exit;
305 # Here we check that the borrower can actually make reserves Stage 1.
308 my $noreserves = 0;
309 my $maxoutstanding = C4::Context->preference("maxoutstanding");
310 $template->param( noreserve => 1 ) unless $maxoutstanding;
311 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
312 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
313 $template->param( message => 1 );
314 $noreserves = 1;
315 $template->param( too_much_oweing => $amount );
317 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
318 $noreserves = 1;
319 $template->param(
320 message => 1,
321 GNA => 1
324 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
325 $noreserves = 1;
326 $template->param(
327 message => 1,
328 lost => 1
331 if ( IsDebarred($borrowernumber) ) {
332 $noreserves = 1;
333 $template->param(
334 message => 1,
335 debarred => 1
339 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
340 my $reserves_count = scalar(@reserves);
341 $template->param( RESERVES => \@reserves );
342 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
343 $template->param( message => 1 );
344 $noreserves = 1;
345 $template->param( too_many_reserves => scalar(@reserves));
348 unless ( $noreserves ) {
349 my $requested_reserves_count = scalar( @biblionumbers );
350 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
351 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
355 foreach my $res (@reserves) {
356 foreach my $biblionumber (@biblionumbers) {
357 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
358 # $template->param( message => 1 );
359 # $noreserves = 1;
360 # $template->param( already_reserved => 1 );
361 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
366 unless ($noreserves) {
367 $template->param( select_item_types => 1 );
373 # Build the template parameters that will show the info
374 # and items for each biblionumber.
377 my $notforloan_label_of = get_notforloan_label_of();
379 my $biblioLoop = [];
380 my $numBibsAvailable = 0;
381 my $itemdata_enumchron = 0;
382 my $anyholdable = 0;
383 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
384 $template->param('item_level_itypes' => $itemLevelTypes);
386 foreach my $biblioNum (@biblionumbers) {
388 my $record = GetMarcBiblio($biblioNum);
389 # Init the bib item with the choices for branch pickup
390 my %biblioLoopIter = ( branchloop => $branchloop );
392 # Get relevant biblio data.
393 my $biblioData = $biblioDataHash{$biblioNum};
394 if (! $biblioData) {
395 $template->param(message=>1, bad_biblionumber=>$biblioNum);
396 &get_out($query, $cookie, $template->output);
399 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
400 $biblioLoopIter{title} = $biblioData->{title};
401 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
402 $biblioLoopIter{author} = $biblioData->{author};
403 $biblioLoopIter{rank} = $biblioData->{rank};
404 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
405 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
406 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
408 if (!$itemLevelTypes && $biblioData->{itemtype}) {
409 $biblioLoopIter{translated_description} = $itemTypes->{$biblioData->{itemtype}}{translated_description};
410 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
413 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
414 if ($itemLevelTypes && $itemInfo->{itype}) {
415 $itemInfo->{translated_description} = $itemTypes->{$itemInfo->{itype}}{translated_description};
416 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
419 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
420 $biblioLoopIter{forloan} = 1;
424 $biblioLoopIter{itemLoop} = [];
425 my $numCopiesAvailable = 0;
426 my $numCopiesOPACAvailable = 0;
427 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
428 my $itemNum = $itemInfo->{itemnumber};
429 my $itemLoopIter = {};
431 $itemLoopIter->{itemnumber} = $itemNum;
432 $itemLoopIter->{barcode} = $itemInfo->{barcode};
433 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
434 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
435 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
436 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
437 if ($itemLevelTypes) {
438 $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
439 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
442 # If the holdingbranch is different than the homebranch, we show the
443 # holdingbranch of the document too.
444 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
445 $itemLoopIter->{holdingBranchName} =
446 $branches->{ $itemInfo->{holdingbranch} }{branchname};
449 # If the item is currently on loan, we display its return date and
450 # change the background color.
451 my $issues= GetItemIssue($itemNum);
452 if ( $issues->{'date_due'} ) {
453 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
454 $itemLoopIter->{backgroundcolor} = 'onloan';
457 # checking reserve
458 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
459 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
461 # the item could be reserved for this borrower vi a host record, flag this
462 $reservedfor //= '';
463 if ($reservedfor eq $borrowernumber){
464 $itemLoopIter->{already_reserved} = 1;
467 if ( defined $reservedate ) {
468 $itemLoopIter->{backgroundcolor} = 'reserved';
469 $itemLoopIter->{reservedate} = output_pref({ dt => dt_from_string($reservedate), dateonly => 1 });
470 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
471 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
472 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
473 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
474 #waiting status
475 $itemLoopIter->{waitingdate} = $wait;
478 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
479 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
481 # Management of the notforloan document
482 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
483 $itemLoopIter->{backgroundcolor} = 'other';
484 $itemLoopIter->{notforloanvalue} =
485 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
488 # Management of lost or long overdue items
489 if ( $itemInfo->{itemlost} ) {
491 # FIXME localized strings should never be in Perl code
492 $itemLoopIter->{message} =
493 $itemInfo->{itemlost} == 1 ? "(lost)"
494 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
495 : "";
496 $itemInfo->{backgroundcolor} = 'other';
499 # Check of the transfered documents
500 my ( $transfertwhen, $transfertfrom, $transfertto ) =
501 GetTransfers($itemNum);
502 if ( $transfertwhen && ($transfertwhen ne '') ) {
503 $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
504 $itemLoopIter->{transfertfrom} =
505 $branches->{$transfertfrom}{branchname};
506 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
507 $itemLoopIter->{nocancel} = 1;
510 # if the items belongs to a host record, show link to host record
511 if ($itemInfo->{biblionumber} ne $biblioNum){
512 $biblioLoopIter{hostitemsflag} = 1;
513 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
514 $itemLoopIter->{hosttitle} = GetBiblioData($itemInfo->{biblionumber})->{title};
517 # If there is no loan, return and transfer, we show a checkbox.
518 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
520 my $branch = GetReservesControlBranch( $itemInfo, $borr );
522 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
523 $policy_holdallowed &&=
524 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
525 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
527 if ($policy_holdallowed) {
528 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
529 $itemLoopIter->{available} = 1;
530 $numCopiesOPACAvailable++;
531 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
533 $numCopiesAvailable++;
536 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
538 # Show serial enumeration when needed
539 if ($itemLoopIter->{enumchron}) {
540 $itemdata_enumchron = 1;
543 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
545 $template->param( itemdata_enumchron => $itemdata_enumchron );
547 if ($numCopiesAvailable > 0) {
548 $numBibsAvailable++;
549 $biblioLoopIter{bib_available} = 1;
550 $biblioLoopIter{holdable} = 1;
551 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
553 if ($biblioLoopIter{already_reserved}) {
554 $biblioLoopIter{holdable} = undef;
555 $biblioLoopIter{itemholdable} = undef;
557 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
558 $biblioLoopIter{holdable} = undef;
559 $biblioLoopIter{already_patron_possession} = 1;
562 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
564 push @$biblioLoop, \%biblioLoopIter;
566 $anyholdable = 1 if $biblioLoopIter{holdable};
569 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
570 $template->param( none_available => 1 );
573 my $show_notes=C4::Context->preference('OpacHoldNotes');
574 $template->param(OpacHoldNotes=>$show_notes);
576 # display infos
577 $template->param(bibitemloop => $biblioLoop);
578 # can set reserve date in future
579 if (
580 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
581 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
583 $template->param(
584 reserve_in_future => 1,
588 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };