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.
38 use Date
::Calc qw
/Today Date_to_Days/;
39 use List
::MoreUtils qw
/uniq/;
41 my $maxreserves = C4
::Context
->preference("maxreserves");
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");
51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
53 template_name
=> "opac-reserve.tt",
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;
68 output_html_with_http_headers
(shift,shift,shift); # $query, $cookie, $template->output;
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
}));
91 my $itemTypes = GetItemTypes
();
93 # There are two ways of calling this script, with a single biblio num
94 # or multiple biblio nums.
95 my $biblionumbers = $query->param('biblionumbers');
96 my $reserveMode = $query->param('reserve_mode');
97 if ($reserveMode && ($reserveMode eq 'single')) {
98 my $bib = $query->param('single_bib');
99 $biblionumbers = "$bib/";
101 if (! $biblionumbers) {
102 $biblionumbers = $query->param('biblionumber');
105 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
106 $template->param(message
=>1, no_biblionumber
=>1);
107 &get_out
($query, $cookie, $template->output);
110 # Pass the numbers to the page so they can be fed back
111 # when the hold is confirmed. TODO: Not necessary?
112 $template->param( biblionumbers
=> $biblionumbers );
114 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
115 my @biblionumbers = split /\//, $biblionumbers;
116 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
118 $template->param(message
=>1, no_biblionumber
=>1);
119 &get_out
($query, $cookie, $template->output);
123 # pass the pickup branch along....
124 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4
::Context
->userenv->{branch
} || '' ;
125 $template->param( branch
=> $branch );
127 # Is the person allowed to choose their branch
128 my $OPACChooseBranch = (C4
::Context
->preference("OPACAllowUserToChooseBranch")) ?
1 : 0;
130 $template->param( choose_branch
=> $OPACChooseBranch);
134 # Build hashes of the requested biblio(item)s and items.
138 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
139 my %itemInfoHash; # Hash of itemnumber to item info.
140 foreach my $biblioNumber (@biblionumbers) {
142 my $biblioData = GetBiblioData
($biblioNumber);
143 $biblioDataHash{$biblioNumber} = $biblioData;
145 my @itemInfos = GetItemsInfo
($biblioNumber);
147 my $marcrecord= GetMarcBiblio
($biblioNumber);
149 # flag indicating existence of at least one item linked via a host record
151 # adding items linked via host biblios
152 my @hostitemInfos = GetHostItemsInfo
($marcrecord);
155 push (@itemInfos,@hostitemInfos);
158 $biblioData->{itemInfos
} = \
@itemInfos;
159 foreach my $itemInfo (@itemInfos) {
160 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
163 # Compute the priority rank.
164 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblioNumber, all_dates
=> 1 });
165 my $rank = scalar( @
$reserves );
166 $biblioData->{reservecount
} = 1; # new reserve
167 foreach my $res (@
{$reserves}) {
168 my $found = $res->{found
};
169 if ( $found && $found eq 'W' ) {
173 $biblioData->{reservecount
}++;
176 $biblioData->{rank
} = $rank + 1;
181 # If this is the second time through this script, it
182 # means we are carrying out the hold request, possibly
183 # with a specific item for each biblionumber.
186 if ( $query->param('place_reserve') ) {
189 $reserve_cnt = GetReservesFromBorrowernumber
( $borrowernumber );
192 # List is composed of alternating biblio/item/branch
193 my $selectedItems = $query->param('selecteditems');
195 if ($query->param('reserve_mode') eq 'single') {
196 # This indicates non-JavaScript mode, so there was
197 # only a single biblio number selected.
198 my $bib = $query->param('single_bib');
199 my $item = $query->param("checkitem_$bib");
200 if ($item eq 'any') {
203 my $branch = $query->param('branch');
204 $selectedItems = "$bib/$item/$branch/";
207 $selectedItems =~ s!/$!!;
208 my @selectedItems = split /\//, $selectedItems, -1;
210 # Make sure there is a biblionum/itemnum/branch triplet for each item.
211 # The itemnum can be 'any', meaning next available.
212 my $selectionCount = @selectedItems;
213 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
214 $template->param(message
=>1, bad_data
=>1);
215 &get_out
($query, $cookie, $template->output);
218 my $failed_holds = 0;
219 while (@selectedItems) {
220 my $biblioNum = shift(@selectedItems);
221 my $itemNum = shift(@selectedItems);
222 my $branch = shift(@selectedItems); # i.e., branch code, not name
226 my $singleBranchMode = Koha
::Libraries
->search->count == 1;
227 if ( $singleBranchMode || !$OPACChooseBranch )
228 { # single branch mode or disabled user choosing
229 $branch = $borr->{'branchcode'};
232 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
233 if ( $itemNum ne '' ) {
234 my $hostbiblioNum = GetBiblionumberFromItemnumber
($itemNum);
235 if ( $hostbiblioNum ne $biblioNum ) {
236 $biblioNum = $hostbiblioNum;
240 my $biblioData = $biblioDataHash{$biblioNum};
243 # Check for user supplied reserve date
245 if ( C4
::Context
->preference('AllowHoldDateInFuture')
246 && C4
::Context
->preference('OPACAllowHoldDateInFuture') )
248 $startdate = $query->param("reserve_date_$biblioNum");
251 my $expiration_date = $query->param("expiration_date_$biblioNum");
253 # If a specific item was selected and the pickup branch is the same as the
254 # holdingbranch, force the value $rank and $found.
255 my $rank = $biblioData->{rank
};
256 if ( $itemNum ne '' ) {
257 $canreserve = 1 if CanItemBeReserved
( $borrowernumber, $itemNum ) eq 'OK';
258 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
259 my $item = GetItem
($itemNum);
260 if ( $item->{'holdingbranch'} eq $branch ) {
262 unless C4
::Context
->preference('ReservesNeedReturns');
266 $canreserve = 1 if CanBookBeReserved
( $borrowernumber, $biblioNum ) eq 'OK';
268 # Inserts a null into the 'itemnumber' field of 'reserves' table.
271 my $notes = $query->param('notes_'.$biblioNum)||'';
274 && $reserve_cnt >= $maxreserves )
279 my $itemtype = $query->param('itemtype') || undef;
280 $itemtype = undef if $itemNum;
282 # Here we actually do the reserveration. Stage 3.
284 my $reserve_id = AddReserve
(
285 $branch, $borrowernumber, $biblioNum,
286 [$biblioNum], $rank, $startdate,
287 $expiration_date, $notes, $biblioData->{title
},
288 $itemNum, $found, $itemtype,
290 $failed_holds++ unless $reserve_id;
295 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ?
"failed_holds=$failed_holds" : q
|| ) . "#opac-user-holds");
301 # Here we check that the borrower can actually make reserves Stage 1.
305 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
306 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
307 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
308 my $amount = sprintf "%.02f", $borr->{'amountoutstanding'};
309 $template->param( message
=> 1 );
311 $template->param( too_much_oweing
=> $amount );
314 if ( $borr->{gonenoaddress
} && ($borr->{gonenoaddress
} == 1) ) {
322 if ( $borr->{lost
} && ($borr->{lost
} == 1) ) {
330 if ( Koha
::Patrons
->find( $borrowernumber )->is_debarred ) {
335 debarred_comment
=> $borr->{debarredcomment
},
336 debarred_date
=> $borr->{debarred
},
340 my @reserves = GetReservesFromBorrowernumber
( $borrowernumber );
341 my $reserves_count = scalar(@reserves);
342 $template->param( RESERVES
=> \
@reserves );
343 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
344 $template->param( message
=> 1 );
346 $template->param( too_many_reserves
=> scalar(@reserves));
349 unless ( $noreserves ) {
350 my $requested_reserves_count = scalar( @biblionumbers );
351 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
352 $template->param( new_reserves_allowed
=> $maxreserves - $reserves_count );
356 unless ($noreserves) {
357 $template->param( select_item_types
=> 1 );
363 # Build the template parameters that will show the info
364 # and items for each biblionumber.
367 my $notforloan_label_of = get_notforloan_label_of
();
370 my $numBibsAvailable = 0;
371 my $itemdata_enumchron = 0;
373 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
374 $template->param('item_level_itypes' => $itemLevelTypes);
376 foreach my $biblioNum (@biblionumbers) {
378 my $record = GetMarcBiblio
($biblioNum);
379 # Init the bib item with the choices for branch pickup
382 # Get relevant biblio data.
383 my $biblioData = $biblioDataHash{$biblioNum};
385 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
386 &get_out
($query, $cookie, $template->output);
389 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
390 $biblioLoopIter{title
} = $biblioData->{title
};
391 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, GetFrameworkCode
($biblioData->{biblionumber
}));
392 $biblioLoopIter{author
} = $biblioData->{author
};
393 $biblioLoopIter{rank
} = $biblioData->{rank
};
394 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
395 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
396 $biblioLoopIter{mandatorynotes
}=0; #FIXME: For future use
398 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
399 $biblioLoopIter{translated_description
} = $itemTypes->{$biblioData->{itemtype
}}{translated_description
};
400 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$biblioData->{itemtype
}}{imageurl
};
403 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
404 if ($itemLevelTypes && $itemInfo->{itype
}) {
405 $itemInfo->{translated_description
} = $itemTypes->{$itemInfo->{itype
}}{translated_description
};
406 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$itemInfo->{itype
}}{imageurl
};
409 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
410 $biblioLoopIter{forloan
} = 1;
414 $biblioLoopIter{itemLoop
} = [];
415 my $numCopiesAvailable = 0;
416 my $numCopiesOPACAvailable = 0;
417 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
418 my $itemNum = $itemInfo->{itemnumber
};
419 my $itemLoopIter = {};
421 $itemLoopIter->{itemnumber
} = $itemNum;
422 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
423 $itemLoopIter->{homeBranchName
} = $itemInfo->{homebranch
};
424 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
425 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
426 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
427 if ($itemLevelTypes) {
428 $itemLoopIter->{translated_description
} = $itemInfo->{translated_description
};
429 $itemLoopIter->{itype
} = $itemInfo->{itype
};
430 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
433 # If the holdingbranch is different than the homebranch, we show the
434 # holdingbranch of the document too.
435 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
436 $itemLoopIter->{holdingBranchName
} = $itemInfo->{holdingbranch
};
439 # If the item is currently on loan, we display its return date and
440 # change the background color.
441 my $issues= GetItemIssue
($itemNum);
442 if ( $issues->{'date_due'} ) {
443 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issues->{date_due
}, 'sql'), as_due_date
=> 1 });
444 $itemLoopIter->{backgroundcolor
} = 'onloan';
448 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber
($itemNum);
449 my $ItemBorrowerReserveInfo = GetMemberDetails
( $reservedfor, 0);
451 # the item could be reserved for this borrower vi a host record, flag this
454 if ( defined $reservedate ) {
455 $itemLoopIter->{backgroundcolor
} = 'reserved';
456 $itemLoopIter->{reservedate
} = output_pref
({ dt
=> dt_from_string
($reservedate), dateonly
=> 1 });
457 $itemLoopIter->{ReservedForBorrowernumber
} = $reservedfor;
458 $itemLoopIter->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
459 $itemLoopIter->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
460 $itemLoopIter->{ExpectedAtLibrary
} = $expectedAt;
462 $itemLoopIter->{waitingdate
} = $wait;
465 $itemLoopIter->{notforloan
} = $itemInfo->{notforloan
};
466 $itemLoopIter->{itemnotforloan
} = $itemInfo->{itemnotforloan
};
468 # Management of the notforloan document
469 if ( $itemLoopIter->{notforloan
} || $itemLoopIter->{itemnotforloan
}) {
470 $itemLoopIter->{backgroundcolor
} = 'other';
471 $itemLoopIter->{notforloanvalue
} =
472 $notforloan_label_of->{ $itemLoopIter->{notforloan
} };
475 # Management of lost or long overdue items
476 if ( $itemInfo->{itemlost
} ) {
478 # FIXME localized strings should never be in Perl code
479 $itemLoopIter->{message
} =
480 $itemInfo->{itemlost
} == 1 ?
"(lost)"
481 : $itemInfo->{itemlost
} == 2 ?
"(long overdue)"
483 $itemInfo->{backgroundcolor
} = 'other';
486 # Check of the transfered documents
487 my ( $transfertwhen, $transfertfrom, $transfertto ) =
488 GetTransfers
($itemNum);
489 if ( $transfertwhen && ($transfertwhen ne '') ) {
490 $itemLoopIter->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
($transfertwhen), dateonly
=> 1 });
491 $itemLoopIter->{transfertfrom
} = $transfertfrom;
492 $itemLoopIter->{transfertto
} = $transfertto;
493 $itemLoopIter->{nocancel
} = 1;
496 # if the items belongs to a host record, show link to host record
497 if ( $itemInfo->{biblionumber
} ne $biblioNum ) {
498 $biblioLoopIter{hostitemsflag
} = 1;
499 $itemLoopIter->{hostbiblionumber
} = $itemInfo->{biblionumber
};
500 $itemLoopIter->{hosttitle
} = GetBiblioData
( $itemInfo->{biblionumber
} )->{title
};
503 # If there is no loan, return and transfer, we show a checkbox.
504 $itemLoopIter->{notforloan
} = $itemLoopIter->{notforloan
} || 0;
506 my $branch = GetReservesControlBranch
( $itemInfo, $borr );
508 my $policy_holdallowed = !$itemLoopIter->{already_reserved
};
509 $policy_holdallowed &&=
510 IsAvailableForItemLevelRequest
($itemInfo,$borr) &&
511 CanItemBeReserved
($borrowernumber,$itemNum) eq 'OK';
513 if ($policy_holdallowed) {
514 if ( my $hold_allowed = OPACItemHoldsAllowed
( $itemInfo, $borr ) ) {
515 $itemLoopIter->{available
} = 1;
516 $numCopiesOPACAvailable++;
517 $biblioLoopIter{force_hold
} = 1 if $hold_allowed eq 'F';
519 $numCopiesAvailable++;
522 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemTypes->{ $itemInfo->{itype
} }{imageurl
} );
524 # Show serial enumeration when needed
525 if ($itemLoopIter->{enumchron
}) {
526 $itemdata_enumchron = 1;
529 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
531 $template->param( itemdata_enumchron
=> $itemdata_enumchron );
533 if ($numCopiesAvailable > 0) {
535 $biblioLoopIter{bib_available
} = 1;
536 $biblioLoopIter{holdable
} = 1;
537 $biblioLoopIter{itemholdable
} = 1 if $numCopiesOPACAvailable;
539 if ($biblioLoopIter{already_reserved
}) {
540 $biblioLoopIter{holdable
} = undef;
541 $biblioLoopIter{itemholdable
} = undef;
543 if(not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowernumber,$biblioNum)) {
544 $biblioLoopIter{holdable
} = undef;
545 $biblioLoopIter{already_patron_possession
} = 1;
548 $biblioLoopIter{holdable
} &&= CanBookBeReserved
($borrowernumber,$biblioNum) eq 'OK';
550 # For multiple holds per record, if a patron has previously placed a hold,
551 # the patron can only place more holds of the same type. That is, if the
552 # patron placed a record level hold, all the holds the patron places must
553 # be record level. If the patron placed an item level hold, all holds
554 # the patron places must be item level
555 my $forced_hold_level = Koha
::Holds
->search(
557 borrowernumber
=> $borrowernumber,
558 biblionumber
=> $biblioNum,
561 )->forced_hold_level();
562 if ($forced_hold_level) {
563 $biblioLoopIter{force_hold
} = 1 if $forced_hold_level eq 'item';
564 $biblioLoopIter{itemholdable
} = 0 if $forced_hold_level eq 'record';
568 push @
$biblioLoop, \
%biblioLoopIter;
570 $anyholdable = 1 if $biblioLoopIter{holdable
};
574 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
575 $template->param( none_available
=> 1 );
578 my $show_notes=C4
::Context
->preference('OpacHoldNotes');
579 $template->param(OpacHoldNotes
=>$show_notes);
582 $template->param(bibitemloop
=> $biblioLoop);
583 # can set reserve date in future
585 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
586 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
589 reserve_in_future
=> 1,
593 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };