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
;
39 use Date
::Calc qw
/Today Date_to_Days/;
40 use List
::MoreUtils qw
/uniq/;
42 my $maxreserves = C4
::Context
->preference("maxreserves");
46 # if RequestOnOpac (for placing holds) is disabled, leave immediately
47 if ( ! C4
::Context
->preference('RequestOnOpac') ) {
48 print $query->redirect("/cgi-bin/koha/errors/404.pl");
52 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
54 template_name
=> "opac-reserve.tt",
62 my ($show_holds_count, $show_priority);
63 for ( C4
::Context
->preference("OPACShowHoldQueueDetails") ) {
64 m/holds/o and $show_holds_count = 1;
65 m/priority/ and $show_priority = 1;
69 output_html_with_http_headers
(shift,shift,shift); # $query, $cookie, $template->output;
73 # get borrower information ....
74 my ( $borr ) = GetMember
( borrowernumber
=> $borrowernumber );
75 my $patron = Koha
::Patrons
->find( $borrowernumber );
77 my $can_place_hold_if_available_at_pickup = C4
::Context
->preference('OPACHoldsIfAvailableAtPickup');
78 unless ( $can_place_hold_if_available_at_pickup ) {
79 my @patron_categories = split '\|', C4
::Context
->preference('OPACHoldsIfAvailableAtPickupExceptions');
80 if ( @patron_categories ) {
81 $can_place_hold_if_available_at_pickup = grep /$borr->{categorycode}/, @patron_categories;
85 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
86 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
88 if ( $patron->is_expired ) {
90 # cannot reserve, their card has expired and the rules set mean this is not allowed
91 $template->param( message
=> 1, expired_patron
=> 1 );
92 get_out
( $query, $cookie, $template->output );
96 # Pass through any reserve charge
97 my $reservefee = $patron->category->reservefee;
98 if ( $reservefee > 0){
99 $template->param( RESERVE_CHARGE
=> sprintf("%.2f",$reservefee));
102 my $itemTypes = GetItemTypes
();
104 # There are two ways of calling this script, with a single biblio num
105 # or multiple biblio nums.
106 my $biblionumbers = $query->param('biblionumbers');
107 my $reserveMode = $query->param('reserve_mode');
108 if ($reserveMode && ($reserveMode eq 'single')) {
109 my $bib = $query->param('single_bib');
110 $biblionumbers = "$bib/";
112 if (! $biblionumbers) {
113 $biblionumbers = $query->param('biblionumber');
116 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
117 $template->param(message
=>1, no_biblionumber
=>1);
118 &get_out
($query, $cookie, $template->output);
121 # Pass the numbers to the page so they can be fed back
122 # when the hold is confirmed. TODO: Not necessary?
123 $template->param( biblionumbers
=> $biblionumbers );
125 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
126 my @biblionumbers = split /\//, $biblionumbers;
127 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
129 $template->param(message
=>1, no_biblionumber
=>1);
130 &get_out
($query, $cookie, $template->output);
134 # pass the pickup branch along....
135 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4
::Context
->userenv->{branch
} || '' ;
136 $template->param( branch
=> $branch );
138 # Is the person allowed to choose their branch
139 my $OPACChooseBranch = (C4
::Context
->preference("OPACAllowUserToChooseBranch")) ?
1 : 0;
141 $template->param( choose_branch
=> $OPACChooseBranch);
145 # Build hashes of the requested biblio(item)s and items.
149 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
150 my %itemInfoHash; # Hash of itemnumber to item info.
151 foreach my $biblioNumber (@biblionumbers) {
153 my $biblioData = GetBiblioData
($biblioNumber);
154 $biblioDataHash{$biblioNumber} = $biblioData;
156 my @itemInfos = GetItemsInfo
($biblioNumber);
158 my $marcrecord= GetMarcBiblio
($biblioNumber);
160 # flag indicating existence of at least one item linked via a host record
162 # adding items linked via host biblios
163 my @hostitemInfos = GetHostItemsInfo
($marcrecord);
166 push (@itemInfos,@hostitemInfos);
169 $biblioData->{itemInfos
} = \
@itemInfos;
170 foreach my $itemInfo (@itemInfos) {
171 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
174 # Compute the priority rank.
175 my $biblio = Koha
::Biblios
->find( $biblioNumber );
176 my $holds = $biblio->holds;
177 my $rank = $holds->count;
178 $biblioData->{reservecount
} = 1; # new reserve
179 while ( my $hold = $holds->next ) {
180 if ( $hold->is_waiting ) {
184 $biblioData->{reservecount
}++;
187 $biblioData->{rank
} = $rank + 1;
192 # If this is the second time through this script, it
193 # means we are carrying out the hold request, possibly
194 # with a specific item for each biblionumber.
197 if ( $query->param('place_reserve') ) {
200 $reserve_cnt = GetReservesFromBorrowernumber
( $borrowernumber );
203 # List is composed of alternating biblio/item/branch
204 my $selectedItems = $query->param('selecteditems');
206 if ($query->param('reserve_mode') eq 'single') {
207 # This indicates non-JavaScript mode, so there was
208 # only a single biblio number selected.
209 my $bib = $query->param('single_bib');
210 my $item = $query->param("checkitem_$bib");
211 if ($item eq 'any') {
214 my $branch = $query->param('branch');
215 $selectedItems = "$bib/$item/$branch/";
218 $selectedItems =~ s!/$!!;
219 my @selectedItems = split /\//, $selectedItems, -1;
221 # Make sure there is a biblionum/itemnum/branch triplet for each item.
222 # The itemnum can be 'any', meaning next available.
223 my $selectionCount = @selectedItems;
224 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
225 $template->param(message
=>1, bad_data
=>1);
226 &get_out
($query, $cookie, $template->output);
229 my $failed_holds = 0;
230 while (@selectedItems) {
231 my $biblioNum = shift(@selectedItems);
232 my $itemNum = shift(@selectedItems);
233 my $branch = shift(@selectedItems); # i.e., branch code, not name
237 my $singleBranchMode = Koha
::Libraries
->search->count == 1;
238 if ( $singleBranchMode || !$OPACChooseBranch )
239 { # single branch mode or disabled user choosing
240 $branch = $borr->{'branchcode'};
243 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
244 if ( $itemNum ne '' ) {
245 my $hostbiblioNum = GetBiblionumberFromItemnumber
($itemNum);
246 if ( $hostbiblioNum ne $biblioNum ) {
247 $biblioNum = $hostbiblioNum;
251 my $biblioData = $biblioDataHash{$biblioNum};
254 # Check for user supplied reserve date
256 if ( C4
::Context
->preference('AllowHoldDateInFuture')
257 && C4
::Context
->preference('OPACAllowHoldDateInFuture') )
259 $startdate = $query->param("reserve_date_$biblioNum");
262 my $expiration_date = $query->param("expiration_date_$biblioNum");
264 # If a specific item was selected and the pickup branch is the same as the
265 # holdingbranch, force the value $rank and $found.
266 my $rank = $biblioData->{rank
};
267 if ( $itemNum ne '' ) {
268 $canreserve = 1 if CanItemBeReserved
( $borrowernumber, $itemNum ) eq 'OK';
269 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
270 my $item = GetItem
($itemNum);
271 if ( $item->{'holdingbranch'} eq $branch ) {
273 unless C4
::Context
->preference('ReservesNeedReturns');
277 $canreserve = 1 if CanBookBeReserved
( $borrowernumber, $biblioNum ) eq 'OK';
279 # Inserts a null into the 'itemnumber' field of 'reserves' table.
282 my $notes = $query->param('notes_'.$biblioNum)||'';
285 && $reserve_cnt >= $maxreserves )
290 unless ( $can_place_hold_if_available_at_pickup ) {
291 my $items_in_this_library = Koha
::Items
->search({ biblionumber
=> $biblioNum, holdingbranch
=> $branch });
292 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
293 my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost
=> { '!=' => 0 }, damaged
=> { '!=' => 0 }, } });
294 if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
299 my $itemtype = $query->param('itemtype') || undef;
300 $itemtype = undef if $itemNum;
302 # Here we actually do the reserveration. Stage 3.
304 my $reserve_id = AddReserve
(
305 $branch, $borrowernumber, $biblioNum,
306 [$biblioNum], $rank, $startdate,
307 $expiration_date, $notes, $biblioData->{title
},
308 $itemNum, $found, $itemtype,
310 $failed_holds++ unless $reserve_id;
315 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ?
"failed_holds=$failed_holds" : q
|| ) . "#opac-user-holds");
321 # Here we check that the borrower can actually make reserves Stage 1.
325 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
326 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
327 my ( $amountoutstanding ) = GetMemberAccountRecords
($borrowernumber);
328 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
329 my $amount = sprintf "%.02f", $amountoutstanding;
330 $template->param( message
=> 1 );
332 $template->param( too_much_oweing
=> $amount );
335 if ( $borr->{gonenoaddress
} && ($borr->{gonenoaddress
} == 1) ) {
343 if ( $borr->{lost
} && ($borr->{lost
} == 1) ) {
351 if ( Koha
::Patrons
->find( $borrowernumber )->is_debarred ) {
356 debarred_comment
=> $borr->{debarredcomment
},
357 debarred_date
=> $borr->{debarred
},
361 my @reserves = GetReservesFromBorrowernumber
( $borrowernumber );
362 my $reserves_count = scalar(@reserves);
363 $template->param( RESERVES
=> \
@reserves );
364 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
365 $template->param( message
=> 1 );
367 $template->param( too_many_reserves
=> scalar(@reserves));
370 unless ( $noreserves ) {
371 my $requested_reserves_count = scalar( @biblionumbers );
372 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
373 $template->param( new_reserves_allowed
=> $maxreserves - $reserves_count );
377 unless ($noreserves) {
378 $template->param( select_item_types
=> 1 );
384 # Build the template parameters that will show the info
385 # and items for each biblionumber.
390 my $numBibsAvailable = 0;
391 my $itemdata_enumchron = 0;
393 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
394 $template->param('item_level_itypes' => $itemLevelTypes);
396 foreach my $biblioNum (@biblionumbers) {
398 my @not_available_at = ();
399 my $record = GetMarcBiblio
($biblioNum);
400 # Init the bib item with the choices for branch pickup
403 # Get relevant biblio data.
404 my $biblioData = $biblioDataHash{$biblioNum};
406 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
407 &get_out
($query, $cookie, $template->output);
410 my $frameworkcode = GetFrameworkCode
( $biblioData->{biblionumber
} );
411 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
412 $biblioLoopIter{title
} = $biblioData->{title
};
413 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, $frameworkcode);
414 $biblioLoopIter{author
} = $biblioData->{author
};
415 $biblioLoopIter{rank
} = $biblioData->{rank
};
416 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
417 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
418 $biblioLoopIter{reqholdnotes
}=0; #TODO: For future use
420 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
421 $biblioLoopIter{translated_description
} = $itemTypes->{$biblioData->{itemtype
}}{translated_description
};
422 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$biblioData->{itemtype
}}{imageurl
};
425 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
426 if ($itemLevelTypes && $itemInfo->{itype
}) {
427 $itemInfo->{translated_description
} = $itemTypes->{$itemInfo->{itype
}}{translated_description
};
428 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemTypes->{$itemInfo->{itype
}}{imageurl
};
431 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
432 $biblioLoopIter{forloan
} = 1;
436 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
437 my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
439 $biblioLoopIter{itemLoop
} = [];
440 my $numCopiesAvailable = 0;
441 my $numCopiesOPACAvailable = 0;
442 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
443 my $itemNum = $itemInfo->{itemnumber
};
444 my $itemLoopIter = {};
446 $itemLoopIter->{itemnumber
} = $itemNum;
447 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
448 $itemLoopIter->{homeBranchName
} = $itemInfo->{homebranch
};
449 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
450 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
451 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
452 if ($itemLevelTypes) {
453 $itemLoopIter->{translated_description
} = $itemInfo->{translated_description
};
454 $itemLoopIter->{itype
} = $itemInfo->{itype
};
455 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
458 # If the holdingbranch is different than the homebranch, we show the
459 # holdingbranch of the document too.
460 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
461 $itemLoopIter->{holdingBranchName
} = $itemInfo->{holdingbranch
};
464 # If the item is currently on loan, we display its return date and
465 # change the background color.
466 my $issues= GetItemIssue
($itemNum);
467 if ( $issues->{'date_due'} ) {
468 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issues->{date_due
}, 'sql'), as_due_date
=> 1 });
469 $itemLoopIter->{backgroundcolor
} = 'onloan';
473 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber
($itemNum);
474 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
476 # the item could be reserved for this borrower vi a host record, flag this
479 if ( defined $reservedate ) {
480 $itemLoopIter->{backgroundcolor
} = 'reserved';
481 $itemLoopIter->{reservedate
} = output_pref
({ dt
=> dt_from_string
($reservedate), dateonly
=> 1 });
482 $itemLoopIter->{ReservedForBorrowernumber
} = $reservedfor;
483 $itemLoopIter->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
484 $itemLoopIter->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
485 $itemLoopIter->{ExpectedAtLibrary
} = $expectedAt;
487 $itemLoopIter->{waitingdate
} = $wait;
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 $branch = GetReservesControlBranch
( $itemInfo, $borr );
533 my $policy_holdallowed = !$itemLoopIter->{already_reserved
};
534 $policy_holdallowed &&=
535 IsAvailableForItemLevelRequest
($itemInfo,$borr) &&
536 CanItemBeReserved
($borrowernumber,$itemNum) eq 'OK';
538 if ($policy_holdallowed) {
539 if ( my $hold_allowed = OPACItemHoldsAllowed
( $itemInfo, $borr ) ) {
540 $itemLoopIter->{available
} = 1;
541 $numCopiesOPACAvailable++;
542 $biblioLoopIter{force_hold
} = 1 if $hold_allowed eq 'F';
544 $numCopiesAvailable++;
546 unless ( $can_place_hold_if_available_at_pickup ) {
547 my $items_in_this_library = Koha
::Items
->search({ biblionumber
=> $itemInfo->{biblionumber
}, holdingbranch
=> $itemInfo->{holdingbranch
} });
548 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
549 if ( $items_in_this_library->count > $nb_of_items_issued ) {
550 push @not_available_at, $itemInfo->{holdingbranch
};
555 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemTypes->{ $itemInfo->{itype
} }{imageurl
} );
557 # Show serial enumeration when needed
558 if ($itemLoopIter->{enumchron
}) {
559 $itemdata_enumchron = 1;
562 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
564 $template->param( itemdata_enumchron
=> $itemdata_enumchron );
566 if ($numCopiesAvailable > 0) {
568 $biblioLoopIter{bib_available
} = 1;
569 $biblioLoopIter{holdable
} = 1;
570 $biblioLoopIter{itemholdable
} = 1 if $numCopiesOPACAvailable;
572 if ($biblioLoopIter{already_reserved
}) {
573 $biblioLoopIter{holdable
} = undef;
574 $biblioLoopIter{itemholdable
} = undef;
576 if(not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowernumber,$biblioNum)) {
577 $biblioLoopIter{holdable
} = undef;
578 $biblioLoopIter{already_patron_possession
} = 1;
581 if ( $biblioLoopIter{holdable
} ) {
582 @not_available_at = uniq
@not_available_at;
583 $biblioLoopIter{not_available_at
} = \
@not_available_at ;
586 unless ( $can_place_hold_if_available_at_pickup ) {
587 @not_available_at = uniq
@not_available_at;
588 $biblioLoopIter{not_available_at
} = \
@not_available_at ;
589 # The record is not holdable is not available at any of the libraries
590 if ( Koha
::Libraries
->search->count == @not_available_at ) {
591 $biblioLoopIter{holdable
} = 0;
595 $biblioLoopIter{holdable
} &&= CanBookBeReserved
($borrowernumber,$biblioNum) eq 'OK';
597 # For multiple holds per record, if a patron has previously placed a hold,
598 # the patron can only place more holds of the same type. That is, if the
599 # patron placed a record level hold, all the holds the patron places must
600 # be record level. If the patron placed an item level hold, all holds
601 # the patron places must be item level
602 my $forced_hold_level = Koha
::Holds
->search(
604 borrowernumber
=> $borrowernumber,
605 biblionumber
=> $biblioNum,
608 )->forced_hold_level();
609 if ($forced_hold_level) {
610 $biblioLoopIter{force_hold
} = 1 if $forced_hold_level eq 'item';
611 $biblioLoopIter{itemholdable
} = 0 if $forced_hold_level eq 'record';
615 push @
$biblioLoop, \
%biblioLoopIter;
617 $anyholdable = 1 if $biblioLoopIter{holdable
};
621 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
622 $template->param( none_available
=> 1 );
625 my $show_notes=C4
::Context
->preference('OpacHoldNotes');
626 $template->param(OpacHoldNotes
=>$show_notes);
629 $template->param(bibitemloop
=> $biblioLoop);
630 # can set reserve date in future
632 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
633 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
636 reserve_in_future
=> 1,
640 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };