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.
36 use Koha
::AuthorisedValues
;
44 use Date
::Calc qw
/Today Date_to_Days/;
45 use List
::MoreUtils qw
/uniq/;
47 my $maxreserves = C4
::Context
->preference("maxreserves");
51 # if RequestOnOpac (for placing holds) is disabled, leave immediately
52 if ( ! C4
::Context
->preference('RequestOnOpac') ) {
53 print $query->redirect("/cgi-bin/koha/errors/404.pl");
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
59 template_name
=> "opac-reserve.tt",
67 my ($show_holds_count, $show_priority);
68 for ( C4
::Context
->preference("OPACShowHoldQueueDetails") ) {
69 m/holds/o and $show_holds_count = 1;
70 m/priority/ and $show_priority = 1;
74 output_html_with_http_headers
(shift,shift,shift); # $query, $cookie, $template->output;
78 my $patron = Koha
::Patrons
->find( $borrowernumber );
80 my $can_place_hold_if_available_at_pickup = C4
::Context
->preference('OPACHoldsIfAvailableAtPickup');
81 unless ( $can_place_hold_if_available_at_pickup ) {
82 my @patron_categories = split '\|', C4
::Context
->preference('OPACHoldsIfAvailableAtPickupExceptions');
83 if ( @patron_categories ) {
84 my $categorycode = $patron->categorycode;
85 $can_place_hold_if_available_at_pickup = grep /^$categorycode$/, @patron_categories;
89 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
90 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
92 if ( $patron->is_expired ) {
94 # cannot reserve, their card has expired and the rules set mean this is not allowed
95 $template->param( message
=> 1, expired_patron
=> 1 );
96 get_out
( $query, $cookie, $template->output );
100 # Pass through any reserve charge
101 my $reservefee = $patron->category->reservefee;
102 if ( $reservefee > 0){
103 $template->param( RESERVE_CHARGE
=> sprintf("%.2f",$reservefee));
106 my $itemtypes = { map { $_->{itemtype
} => $_ } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
108 # There are two ways of calling this script, with a single biblio num
109 # or multiple biblio nums.
110 my $biblionumbers = $query->param('biblionumbers');
111 my $reserveMode = $query->param('reserve_mode');
112 if ($reserveMode && ($reserveMode eq 'single')) {
113 my $bib = $query->param('single_bib');
114 $biblionumbers = "$bib/";
116 if (! $biblionumbers) {
117 $biblionumbers = $query->param('biblionumber');
120 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
121 $template->param(message
=>1, no_biblionumber
=>1);
122 &get_out
($query, $cookie, $template->output);
125 # Pass the numbers to the page so they can be fed back
126 # when the hold is confirmed. TODO: Not necessary?
127 $template->param( biblionumbers
=> $biblionumbers );
129 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
130 my @biblionumbers = split /\//, $biblionumbers;
131 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
133 $template->param(message
=>1, no_biblionumber
=>1);
134 &get_out
($query, $cookie, $template->output);
138 # pass the pickup branch along....
139 my $branch = $query->param('branch') || $patron->branchcode || C4
::Context
->userenv->{branch
} || '' ;
140 $template->param( branch
=> $branch );
142 # Is the person allowed to choose their branch
143 my $OPACChooseBranch = (C4
::Context
->preference("OPACAllowUserToChooseBranch")) ?
1 : 0;
145 $template->param( choose_branch
=> $OPACChooseBranch);
149 # Build hashes of the requested biblio(item)s and items.
153 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
154 my %itemInfoHash; # Hash of itemnumber to item info.
155 foreach my $biblioNumber (@biblionumbers) {
157 my $biblioData = GetBiblioData
($biblioNumber);
158 $biblioDataHash{$biblioNumber} = $biblioData;
160 my @itemInfos = GetItemsInfo
($biblioNumber);
162 my $marcrecord= GetMarcBiblio
({ biblionumber
=> $biblioNumber });
164 # flag indicating existence of at least one item linked via a host record
166 # adding items linked via host biblios
167 my @hostitemInfos = GetHostItemsInfo
($marcrecord);
170 push (@itemInfos,@hostitemInfos);
173 $biblioData->{itemInfos
} = \
@itemInfos;
174 foreach my $itemInfo (@itemInfos) {
175 $itemInfoHash{$itemInfo->{itemnumber
}} = $itemInfo;
178 # Compute the priority rank.
179 my $biblio = Koha
::Biblios
->find( $biblioNumber );
180 my $holds = $biblio->holds;
181 my $rank = $holds->count;
182 $biblioData->{reservecount
} = 1; # new reserve
183 while ( my $hold = $holds->next ) {
184 if ( $hold->is_waiting ) {
188 $biblioData->{reservecount
}++;
191 $biblioData->{rank
} = $rank + 1;
196 # If this is the second time through this script, it
197 # means we are carrying out the hold request, possibly
198 # with a specific item for each biblionumber.
201 if ( $query->param('place_reserve') ) {
204 $reserve_cnt = $patron->holds->count;
207 # List is composed of alternating biblio/item/branch
208 my $selectedItems = $query->param('selecteditems');
210 if ($query->param('reserve_mode') eq 'single') {
211 # This indicates non-JavaScript mode, so there was
212 # only a single biblio number selected.
213 my $bib = $query->param('single_bib');
214 my $item = $query->param("checkitem_$bib");
215 if ($item eq 'any') {
218 my $branch = $query->param('branch');
219 $selectedItems = "$bib/$item/$branch/";
222 $selectedItems =~ s!/$!!;
223 my @selectedItems = split /\//, $selectedItems, -1;
225 # Make sure there is a biblionum/itemnum/branch triplet for each item.
226 # The itemnum can be 'any', meaning next available.
227 my $selectionCount = @selectedItems;
228 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
229 $template->param(message
=>1, bad_data
=>1);
230 &get_out
($query, $cookie, $template->output);
233 my $failed_holds = 0;
234 while (@selectedItems) {
235 my $biblioNum = shift(@selectedItems);
236 my $itemNum = shift(@selectedItems);
237 my $branch = shift(@selectedItems); # i.e., branch code, not name
241 my $singleBranchMode = Koha
::Libraries
->search->count == 1;
242 if ( $singleBranchMode || !$OPACChooseBranch )
243 { # single branch mode or disabled user choosing
244 $branch = $patron->branchcode;
247 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
248 if ( $itemNum ne '' ) {
249 my $item = Koha
::Items
->find( $itemNum );
250 my $hostbiblioNum = $item->biblio->biblionumber;
251 if ( $hostbiblioNum ne $biblioNum ) {
252 $biblioNum = $hostbiblioNum;
256 my $biblioData = $biblioDataHash{$biblioNum};
259 # Check for user supplied reserve date
261 if ( C4
::Context
->preference('AllowHoldDateInFuture')
262 && C4
::Context
->preference('OPACAllowHoldDateInFuture') )
264 $startdate = $query->param("reserve_date_$biblioNum");
267 my $expiration_date = $query->param("expiration_date_$biblioNum");
269 # If a specific item was selected and the pickup branch is the same as the
270 # holdingbranch, force the value $rank and $found.
271 my $rank = $biblioData->{rank
};
272 if ( $itemNum ne '' ) {
273 $canreserve = 1 if CanItemBeReserved
( $borrowernumber, $itemNum ) eq 'OK';
274 $rank = '0' unless C4
::Context
->preference('ReservesNeedReturns');
275 my $item = GetItem
($itemNum);
276 if ( $item->{'holdingbranch'} eq $branch ) {
278 unless C4
::Context
->preference('ReservesNeedReturns');
282 $canreserve = 1 if CanBookBeReserved
( $borrowernumber, $biblioNum ) eq 'OK';
284 # Inserts a null into the 'itemnumber' field of 'reserves' table.
287 my $notes = $query->param('notes_'.$biblioNum)||'';
290 && $reserve_cnt >= $maxreserves )
295 unless ( $can_place_hold_if_available_at_pickup ) {
296 my $items_in_this_library = Koha
::Items
->search({ biblionumber
=> $biblioNum, holdingbranch
=> $branch });
297 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
298 my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost
=> { '!=' => 0 }, damaged
=> { '!=' => 0 }, } });
299 if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
304 my $itemtype = $query->param('itemtype') || undef;
305 $itemtype = undef if $itemNum;
307 # Here we actually do the reserveration. Stage 3.
309 my $reserve_id = AddReserve
(
310 $branch, $borrowernumber, $biblioNum,
311 [$biblioNum], $rank, $startdate,
312 $expiration_date, $notes, $biblioData->{title
},
313 $itemNum, $found, $itemtype,
315 $failed_holds++ unless $reserve_id;
320 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ?
"failed_holds=$failed_holds" : q
|| ) . "#opac-user-holds");
326 # Here we check that the borrower can actually make reserves Stage 1.
330 my $maxoutstanding = C4
::Context
->preference("maxoutstanding");
331 $template->param( noreserve
=> 1 ) unless $maxoutstanding;
332 my ( $amountoutstanding ) = GetMemberAccountRecords
($borrowernumber);
333 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
334 my $amount = sprintf "%.02f", $amountoutstanding;
335 $template->param( message
=> 1 );
337 $template->param( too_much_oweing
=> $amount );
340 if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
348 if ( $patron->lost && ($patron->lost == 1) ) {
356 if ( $patron->is_debarred ) {
361 debarred_comment
=> $patron->debarredcomment,
362 debarred_date
=> $patron->debarred,
366 my $holds = $patron->holds;
367 my $reserves_count = $holds->count;
368 $template->param( RESERVES
=> $holds->unblessed );
369 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
370 $template->param( message
=> 1 );
372 $template->param( too_many_reserves
=> $holds->count );
375 unless ( $noreserves ) {
376 my $requested_reserves_count = scalar( @biblionumbers );
377 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
378 $template->param( new_reserves_allowed
=> $maxreserves - $reserves_count );
382 unless ($noreserves) {
383 $template->param( select_item_types
=> 1 );
389 # Build the template parameters that will show the info
390 # and items for each biblionumber.
395 my $numBibsAvailable = 0;
396 my $itemdata_enumchron = 0;
398 my $itemLevelTypes = C4
::Context
->preference('item-level_itypes');
399 $template->param('item_level_itypes' => $itemLevelTypes);
401 foreach my $biblioNum (@biblionumbers) {
403 my @not_available_at = ();
404 my $record = GetMarcBiblio
({ biblionumber
=> $biblioNum });
405 # Init the bib item with the choices for branch pickup
408 # Get relevant biblio data.
409 my $biblioData = $biblioDataHash{$biblioNum};
411 $template->param(message
=>1, bad_biblionumber
=>$biblioNum);
412 &get_out
($query, $cookie, $template->output);
415 my $frameworkcode = GetFrameworkCode
( $biblioData->{biblionumber
} );
416 $biblioLoopIter{biblionumber
} = $biblioData->{biblionumber
};
417 $biblioLoopIter{title
} = $biblioData->{title
};
418 $biblioLoopIter{subtitle
} = GetRecordValue
('subtitle', $record, $frameworkcode);
419 $biblioLoopIter{author
} = $biblioData->{author
};
420 $biblioLoopIter{rank
} = $biblioData->{rank
};
421 $biblioLoopIter{reservecount
} = $biblioData->{reservecount
};
422 $biblioLoopIter{already_reserved
} = $biblioData->{already_reserved
};
423 $biblioLoopIter{reqholdnotes
}=0; #TODO: For future use
425 if (!$itemLevelTypes && $biblioData->{itemtype
}) {
426 $biblioLoopIter{translated_description
} = $itemtypes->{$biblioData->{itemtype
}}{translated_description
};
427 $biblioLoopIter{imageurl
} = getitemtypeimagesrc
() . "/". $itemtypes->{$biblioData->{itemtype
}}{imageurl
};
430 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
431 if ($itemLevelTypes && $itemInfo->{itype
}) {
432 $itemInfo->{translated_description
} = $itemtypes->{$itemInfo->{itype
}}{translated_description
};
433 $itemInfo->{imageurl
} = getitemtypeimagesrc
() . "/". $itemtypes->{$itemInfo->{itype
}}{imageurl
};
436 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
437 $biblioLoopIter{forloan
} = 1;
441 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
442 my $notforloan_label_of = { map { $_->authorised_value => $_->opac_description } @notforloan_avs };
444 $biblioLoopIter{itemLoop
} = [];
445 my $numCopiesAvailable = 0;
446 my $numCopiesOPACAvailable = 0;
447 foreach my $itemInfo (@
{$biblioData->{itemInfos
}}) {
448 my $itemNum = $itemInfo->{itemnumber
};
449 my $itemLoopIter = {};
451 $itemLoopIter->{itemnumber
} = $itemNum;
452 $itemLoopIter->{barcode
} = $itemInfo->{barcode
};
453 $itemLoopIter->{homeBranchName
} = $itemInfo->{homebranch
};
454 $itemLoopIter->{callNumber
} = $itemInfo->{itemcallnumber
};
455 $itemLoopIter->{enumchron
} = $itemInfo->{enumchron
};
456 $itemLoopIter->{copynumber
} = $itemInfo->{copynumber
};
457 if ($itemLevelTypes) {
458 $itemLoopIter->{translated_description
} = $itemInfo->{translated_description
};
459 $itemLoopIter->{itype
} = $itemInfo->{itype
};
460 $itemLoopIter->{imageurl
} = $itemInfo->{imageurl
};
463 # If the holdingbranch is different than the homebranch, we show the
464 # holdingbranch of the document too.
465 if ( $itemInfo->{homebranch
} ne $itemInfo->{holdingbranch
} ) {
466 $itemLoopIter->{holdingBranchName
} = $itemInfo->{holdingbranch
};
469 # If the item is currently on loan, we display its return date and
470 # change the background color.
471 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemNum } );
473 $itemLoopIter->{dateDue
} = output_pref
({ dt
=> dt_from_string
($issue->date_due, 'sql'), as_due_date
=> 1 });
474 $itemLoopIter->{backgroundcolor
} = 'onloan';
478 my $item = Koha
::Items
->find( $itemNum );
479 my $holds = $item->current_holds;
481 if ( my $first_hold = $holds->next ) {
482 my $patron = Koha
::Patrons
->find( $first_hold->borrowernumber );
483 $itemLoopIter->{backgroundcolor
} = 'reserved';
484 $itemLoopIter->{reservedate
} = output_pref
({ dt
=> dt_from_string
($first_hold->reservedate), dateonly
=> 1 }); # FIXME Should be formatted in the template
485 $itemLoopIter->{ReservedForBorrowernumber
} = $first_hold->borrowernumber;
486 $itemLoopIter->{ReservedForSurname
} = $patron->surname;
487 $itemLoopIter->{ReservedForFirstname
} = $patron->firstname;
488 $itemLoopIter->{ExpectedAtLibrary
} = $first_hold->branchcode;
489 $itemLoopIter->{waitingdate
} = $first_hold->waitingdate;
492 $itemLoopIter->{notforloan
} = $itemInfo->{notforloan
};
493 $itemLoopIter->{itemnotforloan
} = $itemInfo->{itemnotforloan
};
495 # Management of the notforloan document
496 if ( $itemLoopIter->{notforloan
} || $itemLoopIter->{itemnotforloan
}) {
497 $itemLoopIter->{backgroundcolor
} = 'other';
498 $itemLoopIter->{notforloanvalue
} =
499 $notforloan_label_of->{ $itemLoopIter->{notforloan
} };
502 # Management of lost or long overdue items
503 if ( $itemInfo->{itemlost
} ) {
505 # FIXME localized strings should never be in Perl code
506 $itemLoopIter->{message
} =
507 $itemInfo->{itemlost
} == 1 ?
"(lost)"
508 : $itemInfo->{itemlost
} == 2 ?
"(long overdue)"
510 $itemInfo->{backgroundcolor
} = 'other';
513 # Check of the transfered documents
514 my ( $transfertwhen, $transfertfrom, $transfertto ) =
515 GetTransfers
($itemNum);
516 if ( $transfertwhen && ($transfertwhen ne '') ) {
517 $itemLoopIter->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
($transfertwhen), dateonly
=> 1 });
518 $itemLoopIter->{transfertfrom
} = $transfertfrom;
519 $itemLoopIter->{transfertto
} = $transfertto;
520 $itemLoopIter->{nocancel
} = 1;
523 # if the items belongs to a host record, show link to host record
524 if ( $itemInfo->{biblionumber
} ne $biblioNum ) {
525 $biblioLoopIter{hostitemsflag
} = 1;
526 $itemLoopIter->{hostbiblionumber
} = $itemInfo->{biblionumber
};
527 $itemLoopIter->{hosttitle
} = Koha
::Biblios
->find( $itemInfo->{biblionumber
} )->title;
530 # If there is no loan, return and transfer, we show a checkbox.
531 $itemLoopIter->{notforloan
} = $itemLoopIter->{notforloan
} || 0;
533 my $patron_unblessed = $patron->unblessed;
534 my $branch = GetReservesControlBranch
( $itemInfo, $patron_unblessed );
536 my $policy_holdallowed = !$itemLoopIter->{already_reserved
};
537 $policy_holdallowed &&=
538 IsAvailableForItemLevelRequest
($itemInfo,$patron_unblessed) &&
539 CanItemBeReserved
($borrowernumber,$itemNum) eq 'OK';
541 if ($policy_holdallowed) {
542 if ( my $hold_allowed = OPACItemHoldsAllowed
( $itemInfo, $patron_unblessed ) ) {
543 $itemLoopIter->{available
} = 1;
544 $numCopiesOPACAvailable++;
545 $biblioLoopIter{force_hold
} = 1 if $hold_allowed eq 'F';
547 $numCopiesAvailable++;
549 unless ( $can_place_hold_if_available_at_pickup ) {
550 my $items_in_this_library = Koha
::Items
->search({ biblionumber
=> $itemInfo->{biblionumber
}, holdingbranch
=> $itemInfo->{holdingbranch
} });
551 my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
552 if ( $items_in_this_library->count > $nb_of_items_issued ) {
553 push @not_available_at, $itemInfo->{holdingbranch
};
558 $itemLoopIter->{imageurl
} = getitemtypeimagelocation
( 'opac', $itemtypes->{ $itemInfo->{itype
} }{imageurl
} );
560 # Show serial enumeration when needed
561 if ($itemLoopIter->{enumchron
}) {
562 $itemdata_enumchron = 1;
565 push @
{$biblioLoopIter{itemLoop
}}, $itemLoopIter;
567 $template->param( itemdata_enumchron
=> $itemdata_enumchron );
569 if ($numCopiesAvailable > 0) {
571 $biblioLoopIter{bib_available
} = 1;
572 $biblioLoopIter{holdable
} = 1;
573 $biblioLoopIter{itemholdable
} = 1 if $numCopiesOPACAvailable;
575 if ($biblioLoopIter{already_reserved
}) {
576 $biblioLoopIter{holdable
} = undef;
577 $biblioLoopIter{itemholdable
} = undef;
579 if(not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowernumber,$biblioNum)) {
580 $biblioLoopIter{holdable
} = undef;
581 $biblioLoopIter{already_patron_possession
} = 1;
584 if ( $biblioLoopIter{holdable
} ) {
585 @not_available_at = uniq
@not_available_at;
586 $biblioLoopIter{not_available_at
} = \
@not_available_at ;
589 unless ( $can_place_hold_if_available_at_pickup ) {
590 @not_available_at = uniq
@not_available_at;
591 $biblioLoopIter{not_available_at
} = \
@not_available_at ;
592 # The record is not holdable is not available at any of the libraries
593 if ( Koha
::Libraries
->search->count == @not_available_at ) {
594 $biblioLoopIter{holdable
} = 0;
598 $biblioLoopIter{holdable
} &&= CanBookBeReserved
($borrowernumber,$biblioNum) eq 'OK';
600 # For multiple holds per record, if a patron has previously placed a hold,
601 # the patron can only place more holds of the same type. That is, if the
602 # patron placed a record level hold, all the holds the patron places must
603 # be record level. If the patron placed an item level hold, all holds
604 # the patron places must be item level
605 my $forced_hold_level = Koha
::Holds
->search(
607 borrowernumber
=> $borrowernumber,
608 biblionumber
=> $biblioNum,
611 )->forced_hold_level();
612 if ($forced_hold_level) {
613 $biblioLoopIter{force_hold
} = 1 if $forced_hold_level eq 'item';
614 $biblioLoopIter{itemholdable
} = 0 if $forced_hold_level eq 'record';
618 push @
$biblioLoop, \
%biblioLoopIter;
620 $anyholdable = 1 if $biblioLoopIter{holdable
};
624 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
625 $template->param( none_available
=> 1 );
628 my $show_notes=C4
::Context
->preference('OpacHoldNotes');
629 $template->param(OpacHoldNotes
=>$show_notes);
632 $template->param(bibitemloop
=> $biblioLoop);
633 # can set reserve date in future
635 C4
::Context
->preference( 'AllowHoldDateInFuture' ) &&
636 C4
::Context
->preference( 'OPACAllowHoldDateInFuture' )
639 reserve_in_future
=> 1,
643 output_html_with_http_headers
$query, $cookie, $template->output, undef, { force_no_caching
=> 1 };