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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use C4::Auth; # checkauth, getborrowernumber.
31 use C4::Dates qw/format_date/;
34 use C4::Branch; # GetBranches
38 use Date::Calc qw/Today Date_to_Days/;
41 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
46 template_name => "opac-reserve.tmpl",
50 flagsrequired => { borrow => 1 },
55 my ($show_holds_count, $show_priority);
56 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
57 m/holds/o and $show_holds_count = 1;
58 m/priority/ and $show_priority = 1;
62 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
66 # get borrower information ....
67 my ( $borr ) = GetMemberDetails( $borrowernumber );
69 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
70 if( $borr->{'BlockExpiredPatronOpacActions'} == -1 ? C4::Context->preference("BlockExpiredPatronOpacActions") : $borr->{'BlockExpiredPatronOpacActions'} ) {
72 if( Date_to_Days( Today() ) > Date_to_Days( split /-/, $borr->{'dateexpiry'} ) ){
73 # cannot reserve, their card has expired and the rules set mean this is not allowed
74 $template->param( message=>1, expired_patron=>1 );
75 get_out($query, $cookie, $template->output);
79 # Pass through any reserve charge
80 if ($borr->{reservefee} > 0){
81 $template->param( RESERVE_CHARGE => sprintf("%.2f",$borr->{reservefee}));
83 # get branches and itemtypes
84 my $branches = GetBranches();
85 my $itemTypes = GetItemTypes();
87 # There are two ways of calling this script, with a single biblio num
88 # or multiple biblio nums.
89 my $biblionumbers = $query->param('biblionumbers');
90 my $reserveMode = $query->param('reserve_mode');
91 if ($reserveMode && ($reserveMode eq 'single')) {
92 my $bib = $query->param('single_bib');
93 $biblionumbers = "$bib/";
95 if (! $biblionumbers) {
96 $biblionumbers = $query->param('biblionumber');
99 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
100 $template->param(message=>1, no_biblionumber=>1);
101 &get_out($query, $cookie, $template->output);
104 # Pass the numbers to the page so they can be fed back
105 # when the hold is confirmed. TODO: Not necessary?
106 $template->param( biblionumbers => $biblionumbers );
108 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
109 my @biblionumbers = split /\//, $biblionumbers;
110 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
112 $template->param(message=>1, no_biblionumber=>1);
113 &get_out($query, $cookie, $template->output);
116 # pass the pickup branch along....
117 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
118 ($branches->{$branch}) or $branch = ""; # Confirm branch is real
119 $template->param( branch => $branch );
121 # make branch selection options...
122 my $branchloop = GetBranchesLoop($branch);
124 # Is the person allowed to choose their branch
125 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
127 $template->param( choose_branch => $OPACChooseBranch);
131 # Build hashes of the requested biblio(item)s and items.
135 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
136 my %itemInfoHash; # Hash of itemnumber to item info.
137 foreach my $biblioNumber (@biblionumbers) {
139 my $biblioData = GetBiblioData($biblioNumber);
140 $biblioDataHash{$biblioNumber} = $biblioData;
142 my @itemInfos = GetItemsInfo($biblioNumber);
144 my $marcrecord= GetMarcBiblio($biblioNumber);
146 # flag indicating existence of at least one item linked via a host record
148 # adding items linked via host biblios
149 my @hostitemInfos = GetHostItemsInfo($marcrecord);
152 push (@itemInfos,@hostitemInfos);
155 $biblioData->{itemInfos} = \@itemInfos;
156 foreach my $itemInfo (@itemInfos) {
157 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
160 # Compute the priority rank.
161 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
162 my $rank = scalar( @$reserves );
163 $biblioData->{reservecount} = 1; # new reserve
164 foreach my $res (@{$reserves}) {
165 my $found = $res->{found};
166 if ( $found && $found eq 'W' ) {
170 $biblioData->{reservecount}++;
173 $biblioData->{rank} = $rank + 1;
178 # If this is the second time through this script, it
179 # means we are carrying out the hold request, possibly
180 # with a specific item for each biblionumber.
183 if ( $query->param('place_reserve') ) {
185 if ($MAXIMUM_NUMBER_OF_RESERVES) {
186 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
189 # List is composed of alternating biblio/item/branch
190 my $selectedItems = $query->param('selecteditems');
192 if ($query->param('reserve_mode') eq 'single') {
193 # This indicates non-JavaScript mode, so there was
194 # only a single biblio number selected.
195 my $bib = $query->param('single_bib');
196 my $item = $query->param("checkitem_$bib");
197 if ($item eq 'any') {
200 my $branch = $query->param('branch');
201 $selectedItems = "$bib/$item/$branch/";
204 $selectedItems =~ s!/$!!;
205 my @selectedItems = split /\//, $selectedItems, -1;
207 # Make sure there is a biblionum/itemnum/branch triplet for each item.
208 # The itemnum can be 'any', meaning next available.
209 my $selectionCount = @selectedItems;
210 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
211 $template->param(message=>1, bad_data=>1);
212 &get_out($query, $cookie, $template->output);
215 while (@selectedItems) {
216 my $biblioNum = shift(@selectedItems);
217 my $itemNum = shift(@selectedItems);
218 my $branch = shift(@selectedItems); # i.e., branch code, not name
222 my $singleBranchMode = C4::Context->preference("singleBranchMode");
223 if ( $singleBranchMode || !$OPACChooseBranch )
224 { # single branch mode or disabled user choosing
225 $branch = $borr->{'branchcode'};
228 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
229 if ( $itemNum ne '' ) {
230 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
231 if ( $hostbiblioNum ne $biblioNum ) {
232 $biblioNum = $hostbiblioNum;
236 my $biblioData = $biblioDataHash{$biblioNum};
239 # Check for user supplied reserve date
241 if ( C4::Context->preference('AllowHoldDateInFuture')
242 && C4::Context->preference('OPACAllowHoldDateInFuture') )
244 $startdate = $query->param("reserve_date_$biblioNum");
247 my $expiration_date = $query->param("expiration_date_$biblioNum");
249 # If a specific item was selected and the pickup branch is the same as the
250 # holdingbranch, force the value $rank and $found.
251 my $rank = $biblioData->{rank};
252 if ( $itemNum ne '' ) {
253 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum );
254 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
255 my $item = GetItem($itemNum);
256 if ( $item->{'holdingbranch'} eq $branch ) {
258 unless C4::Context->preference('ReservesNeedReturns');
262 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum );
264 # Inserts a null into the 'itemnumber' field of 'reserves' table.
267 my $notes = $query->param('notes_'.$biblioNum)||'';
269 if ( $MAXIMUM_NUMBER_OF_RESERVES
270 && $reserve_cnt >= $MAXIMUM_NUMBER_OF_RESERVES )
275 # Here we actually do the reserveration. Stage 3.
278 $branch, $borrowernumber,
281 $startdate, $expiration_date,
282 $notes, $biblioData->{title},
289 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
295 # Here we check that the borrower can actually make reserves Stage 1.
299 my $maxoutstanding = C4::Context->preference("maxoutstanding");
300 $template->param( noreserve => 1 ) unless $maxoutstanding;
301 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
302 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
303 $template->param( message => 1 );
305 $template->param( too_much_oweing => $amount );
307 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} == 1) ) {
314 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
321 if ( $borr->{'debarred'} ) {
329 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
330 $template->param( RESERVES => \@reserves );
331 if ( $MAXIMUM_NUMBER_OF_RESERVES && (scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES) ) {
332 $template->param( message => 1 );
334 $template->param( too_many_reserves => scalar(@reserves));
336 foreach my $res (@reserves) {
337 foreach my $biblionumber (@biblionumbers) {
338 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
339 # $template->param( message => 1 );
341 # $template->param( already_reserved => 1 );
342 $biblioDataHash{$biblionumber}->{already_reserved} = 1;
347 unless ($noreserves) {
348 $template->param( select_item_types => 1 );
354 # Build the template parameters that will show the info
355 # and items for each biblionumber.
358 my $notforloan_label_of = get_notforloan_label_of();
361 my $numBibsAvailable = 0;
362 my $itemdata_enumchron = 0;
364 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
365 $template->param('item_level_itypes' => $itemLevelTypes);
367 foreach my $biblioNum (@biblionumbers) {
369 my $record = GetMarcBiblio($biblioNum);
370 # Init the bib item with the choices for branch pickup
371 my %biblioLoopIter = ( branchloop => $branchloop );
373 # Get relevant biblio data.
374 my $biblioData = $biblioDataHash{$biblioNum};
376 $template->param(message=>1, bad_biblionumber=>$biblioNum);
377 &get_out($query, $cookie, $template->output);
380 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
381 $biblioLoopIter{title} = $biblioData->{title};
382 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
383 $biblioLoopIter{author} = $biblioData->{author};
384 $biblioLoopIter{rank} = $biblioData->{rank};
385 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
386 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
387 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
389 if (!$itemLevelTypes && $biblioData->{itemtype}) {
390 $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
391 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
394 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
395 $debug and warn $itemInfo->{'notforloan'};
398 my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
399 ( $itemInfo->{'biblioitemnumber'} ) );
400 $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
402 if ($itemLevelTypes && $itemInfo->{itype}) {
403 $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
404 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
407 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
408 $biblioLoopIter{forloan} = 1;
412 $biblioLoopIter{itemLoop} = [];
413 my $numCopiesAvailable = 0;
414 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
415 my $itemNum = $itemInfo->{itemnumber};
416 my $itemLoopIter = {};
418 $itemLoopIter->{itemnumber} = $itemNum;
419 $itemLoopIter->{barcode} = $itemInfo->{barcode};
420 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
421 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
422 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
423 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
424 if ($itemLevelTypes) {
425 $itemLoopIter->{description} = $itemInfo->{description};
426 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
429 # If the holdingbranch is different than the homebranch, we show the
430 # holdingbranch of the document too.
431 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
432 $itemLoopIter->{holdingBranchName} =
433 $branches->{ $itemInfo->{holdingbranch} }{branchname};
436 # If the item is currently on loan, we display its return date and
437 # change the background color.
438 my $issues= GetItemIssue($itemNum);
439 if ( $issues->{'date_due'} ) {
440 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
441 $itemLoopIter->{backgroundcolor} = 'onloan';
445 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
446 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
448 # the item could be reserved for this borrower vi a host record, flag this
449 if ($reservedfor eq $borrowernumber){
450 $itemLoopIter->{already_reserved} = 1;
453 if ( defined $reservedate ) {
454 $itemLoopIter->{backgroundcolor} = 'reserved';
455 $itemLoopIter->{reservedate} = format_date($reservedate);
456 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
457 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
458 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
459 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
461 $itemLoopIter->{waitingdate} = $wait;
464 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
465 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
467 # Management of the notforloan document
468 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
469 $itemLoopIter->{backgroundcolor} = 'other';
470 $itemLoopIter->{notforloanvalue} =
471 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
474 # Management of lost or long overdue items
475 if ( $itemInfo->{itemlost} ) {
477 # FIXME localized strings should never be in Perl code
478 $itemLoopIter->{message} =
479 $itemInfo->{itemlost} == 1 ? "(lost)"
480 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
482 $itemInfo->{backgroundcolor} = 'other';
485 # Check of the transfered documents
486 my ( $transfertwhen, $transfertfrom, $transfertto ) =
487 GetTransfers($itemNum);
488 if ( $transfertwhen && ($transfertwhen ne '') ) {
489 $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
490 $itemLoopIter->{transfertfrom} =
491 $branches->{$transfertfrom}{branchname};
492 $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
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 $branchitemrule = GetBranchItemRule( $branch, $itemInfo->{'itype'} );
509 my $policy_holdallowed = 1;
511 if ( $branchitemrule->{'holdallowed'} == 0 ||
512 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
513 $policy_holdallowed = 0;
516 if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemNum) and ($itemLoopIter->{already_reserved} ne 1)) {
517 $itemLoopIter->{available} = 1;
518 $numCopiesAvailable++;
521 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
523 # Show serial enumeration when needed
524 if ($itemLoopIter->{enumchron}) {
525 $itemdata_enumchron = 1;
528 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
530 $template->param( itemdata_enumchron => $itemdata_enumchron );
532 if ($numCopiesAvailable > 0) {
534 $biblioLoopIter{bib_available} = 1;
535 $biblioLoopIter{holdable} = 1;
537 if ($biblioLoopIter{already_reserved}) {
538 $biblioLoopIter{holdable} = undef;
540 if(not CanBookBeReserved($borrowernumber,$biblioNum)){
541 $biblioLoopIter{holdable} = undef;
543 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
544 $biblioLoopIter{holdable} = undef;
545 $biblioLoopIter{already_patron_possession} = 1;
548 if( $biblioLoopIter{holdable} ){ $anyholdable++; }
550 push @$biblioLoop, \%biblioLoopIter;
553 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
554 $template->param( none_available => 1 );
557 my $itemTableColspan = 9;
558 if (! $template->{VARS}->{'OPACItemHolds'}) {
561 if (! $template->{VARS}->{'singleBranchMode'}) {
564 $itemTableColspan-- if !$show_holds_count && !$show_priority;
565 my $show_notes=C4::Context->preference('OpacHoldNotes');
566 $template->param(OpacHoldNotes=>$show_notes);
567 $itemTableColspan-- if !$show_notes;
568 $template->param(itemtable_colspan => $itemTableColspan);
571 $template->param(bibitemloop => $biblioLoop);
572 $template->param( showholds=>$show_holds_count);
573 $template->param( showpriority=>$show_priority);
574 # can set reserve date in future
576 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
577 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
580 reserve_in_future => 1,
584 output_html_with_http_headers $query, $cookie, $template->output;