Bug 17785: Fix OAI-PMH's XSLT-generated URLs under Plack
[koha.git] / opac / opac-reserve.pl
blobf1fe2ffbb68c709a517a32145ab9c36f213112e2
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 Modern::Perl;
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::Overdues;
34 use C4::Debug;
35 use Koha::DateUtils;
36 use Koha::Libraries;
37 use Koha::Patrons;
38 use Date::Calc qw/Today Date_to_Days/;
39 use List::MoreUtils qw/uniq/;
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 ) = GetMember( borrowernumber => $borrowernumber );
74 my $patron = Koha::Patrons->find( $borrowernumber );
76 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
77 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
79 if ( $patron->is_expired ) {
81 # cannot reserve, their card has expired and the rules set mean this is not allowed
82 $template->param( message => 1, expired_patron => 1 );
83 get_out( $query, $cookie, $template->output );
87 # Pass through any reserve charge
88 my $reservefee = $patron->category->reservefee;
89 if ( $reservefee > 0){
90 $template->param( RESERVE_CHARGE => sprintf("%.2f",$reservefee));
93 my $itemTypes = GetItemTypes();
95 # There are two ways of calling this script, with a single biblio num
96 # or multiple biblio nums.
97 my $biblionumbers = $query->param('biblionumbers');
98 my $reserveMode = $query->param('reserve_mode');
99 if ($reserveMode && ($reserveMode eq 'single')) {
100 my $bib = $query->param('single_bib');
101 $biblionumbers = "$bib/";
103 if (! $biblionumbers) {
104 $biblionumbers = $query->param('biblionumber');
107 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
108 $template->param(message=>1, no_biblionumber=>1);
109 &get_out($query, $cookie, $template->output);
112 # Pass the numbers to the page so they can be fed back
113 # when the hold is confirmed. TODO: Not necessary?
114 $template->param( biblionumbers => $biblionumbers );
116 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
117 my @biblionumbers = split /\//, $biblionumbers;
118 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
119 # TODO: New message?
120 $template->param(message=>1, no_biblionumber=>1);
121 &get_out($query, $cookie, $template->output);
125 # pass the pickup branch along....
126 my $branch = $query->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
127 $template->param( branch => $branch );
129 # Is the person allowed to choose their branch
130 my $OPACChooseBranch = (C4::Context->preference("OPACAllowUserToChooseBranch")) ? 1 : 0;
132 $template->param( choose_branch => $OPACChooseBranch);
136 # Build hashes of the requested biblio(item)s and items.
140 my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
141 my %itemInfoHash; # Hash of itemnumber to item info.
142 foreach my $biblioNumber (@biblionumbers) {
144 my $biblioData = GetBiblioData($biblioNumber);
145 $biblioDataHash{$biblioNumber} = $biblioData;
147 my @itemInfos = GetItemsInfo($biblioNumber);
149 my $marcrecord= GetMarcBiblio($biblioNumber);
151 # flag indicating existence of at least one item linked via a host record
152 my $hostitemsflag;
153 # adding items linked via host biblios
154 my @hostitemInfos = GetHostItemsInfo($marcrecord);
155 if (@hostitemInfos){
156 $hostitemsflag =1;
157 push (@itemInfos,@hostitemInfos);
160 $biblioData->{itemInfos} = \@itemInfos;
161 foreach my $itemInfo (@itemInfos) {
162 $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
165 # Compute the priority rank.
166 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 });
167 my $rank = scalar( @$reserves );
168 $biblioData->{reservecount} = 1; # new reserve
169 foreach my $res (@{$reserves}) {
170 my $found = $res->{found};
171 if ( $found && $found eq 'W' ) {
172 $rank--;
174 else {
175 $biblioData->{reservecount}++;
178 $biblioData->{rank} = $rank + 1;
183 # If this is the second time through this script, it
184 # means we are carrying out the hold request, possibly
185 # with a specific item for each biblionumber.
188 if ( $query->param('place_reserve') ) {
189 my $reserve_cnt = 0;
190 if ($maxreserves) {
191 $reserve_cnt = GetReservesFromBorrowernumber( $borrowernumber );
194 # List is composed of alternating biblio/item/branch
195 my $selectedItems = $query->param('selecteditems');
197 if ($query->param('reserve_mode') eq 'single') {
198 # This indicates non-JavaScript mode, so there was
199 # only a single biblio number selected.
200 my $bib = $query->param('single_bib');
201 my $item = $query->param("checkitem_$bib");
202 if ($item eq 'any') {
203 $item = '';
205 my $branch = $query->param('branch');
206 $selectedItems = "$bib/$item/$branch/";
209 $selectedItems =~ s!/$!!;
210 my @selectedItems = split /\//, $selectedItems, -1;
212 # Make sure there is a biblionum/itemnum/branch triplet for each item.
213 # The itemnum can be 'any', meaning next available.
214 my $selectionCount = @selectedItems;
215 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
216 $template->param(message=>1, bad_data=>1);
217 &get_out($query, $cookie, $template->output);
220 my $failed_holds = 0;
221 while (@selectedItems) {
222 my $biblioNum = shift(@selectedItems);
223 my $itemNum = shift(@selectedItems);
224 my $branch = shift(@selectedItems); # i.e., branch code, not name
226 my $canreserve = 0;
228 my $singleBranchMode = Koha::Libraries->search->count == 1;
229 if ( $singleBranchMode || !$OPACChooseBranch )
230 { # single branch mode or disabled user choosing
231 $branch = $borr->{'branchcode'};
234 #item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
235 if ( $itemNum ne '' ) {
236 my $hostbiblioNum = GetBiblionumberFromItemnumber($itemNum);
237 if ( $hostbiblioNum ne $biblioNum ) {
238 $biblioNum = $hostbiblioNum;
242 my $biblioData = $biblioDataHash{$biblioNum};
243 my $found;
245 # Check for user supplied reserve date
246 my $startdate;
247 if ( C4::Context->preference('AllowHoldDateInFuture')
248 && C4::Context->preference('OPACAllowHoldDateInFuture') )
250 $startdate = $query->param("reserve_date_$biblioNum");
253 my $expiration_date = $query->param("expiration_date_$biblioNum");
255 # If a specific item was selected and the pickup branch is the same as the
256 # holdingbranch, force the value $rank and $found.
257 my $rank = $biblioData->{rank};
258 if ( $itemNum ne '' ) {
259 $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum ) eq 'OK';
260 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
261 my $item = GetItem($itemNum);
262 if ( $item->{'holdingbranch'} eq $branch ) {
263 $found = 'W'
264 unless C4::Context->preference('ReservesNeedReturns');
267 else {
268 $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum ) eq 'OK';
270 # Inserts a null into the 'itemnumber' field of 'reserves' table.
271 $itemNum = undef;
273 my $notes = $query->param('notes_'.$biblioNum)||'';
275 if ( $maxreserves
276 && $reserve_cnt >= $maxreserves )
278 $canreserve = 0;
281 my $itemtype = $query->param('itemtype') || undef;
282 $itemtype = undef if $itemNum;
284 # Here we actually do the reserveration. Stage 3.
285 if ($canreserve) {
286 my $reserve_id = AddReserve(
287 $branch, $borrowernumber, $biblioNum,
288 [$biblioNum], $rank, $startdate,
289 $expiration_date, $notes, $biblioData->{title},
290 $itemNum, $found, $itemtype,
292 $failed_holds++ unless $reserve_id;
293 ++$reserve_cnt;
297 print $query->redirect("/cgi-bin/koha/opac-user.pl?" . ( $failed_holds ? "failed_holds=$failed_holds" : q|| ) . "#opac-user-holds");
298 exit;
303 # Here we check that the borrower can actually make reserves Stage 1.
306 my $noreserves = 0;
307 my $maxoutstanding = C4::Context->preference("maxoutstanding");
308 $template->param( noreserve => 1 ) unless $maxoutstanding;
309 my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
310 if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
311 my $amount = sprintf "%.02f", $amountoutstanding;
312 $template->param( message => 1 );
313 $noreserves = 1;
314 $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
325 if ( $borr->{lost} && ($borr->{lost} == 1) ) {
326 $noreserves = 1;
327 $template->param(
328 message => 1,
329 lost => 1
333 if ( Koha::Patrons->find( $borrowernumber )->is_debarred ) {
334 $noreserves = 1;
335 $template->param(
336 message => 1,
337 debarred => 1,
338 debarred_comment => $borr->{debarredcomment},
339 debarred_date => $borr->{debarred},
343 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
344 my $reserves_count = scalar(@reserves);
345 $template->param( RESERVES => \@reserves );
346 if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
347 $template->param( message => 1 );
348 $noreserves = 1;
349 $template->param( too_many_reserves => scalar(@reserves));
352 unless ( $noreserves ) {
353 my $requested_reserves_count = scalar( @biblionumbers );
354 if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
355 $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
359 unless ($noreserves) {
360 $template->param( select_item_types => 1 );
366 # Build the template parameters that will show the info
367 # and items for each biblionumber.
370 my $notforloan_label_of = get_notforloan_label_of();
372 my $biblioLoop = [];
373 my $numBibsAvailable = 0;
374 my $itemdata_enumchron = 0;
375 my $anyholdable = 0;
376 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
377 $template->param('item_level_itypes' => $itemLevelTypes);
379 foreach my $biblioNum (@biblionumbers) {
381 my $record = GetMarcBiblio($biblioNum);
382 # Init the bib item with the choices for branch pickup
383 my %biblioLoopIter;
385 # Get relevant biblio data.
386 my $biblioData = $biblioDataHash{$biblioNum};
387 if (! $biblioData) {
388 $template->param(message=>1, bad_biblionumber=>$biblioNum);
389 &get_out($query, $cookie, $template->output);
392 $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
393 $biblioLoopIter{title} = $biblioData->{title};
394 $biblioLoopIter{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($biblioData->{biblionumber}));
395 $biblioLoopIter{author} = $biblioData->{author};
396 $biblioLoopIter{rank} = $biblioData->{rank};
397 $biblioLoopIter{reservecount} = $biblioData->{reservecount};
398 $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
399 $biblioLoopIter{mandatorynotes}=0; #FIXME: For future use
401 if (!$itemLevelTypes && $biblioData->{itemtype}) {
402 $biblioLoopIter{translated_description} = $itemTypes->{$biblioData->{itemtype}}{translated_description};
403 $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
406 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
407 if ($itemLevelTypes && $itemInfo->{itype}) {
408 $itemInfo->{translated_description} = $itemTypes->{$itemInfo->{itype}}{translated_description};
409 $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
412 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
413 $biblioLoopIter{forloan} = 1;
417 $biblioLoopIter{itemLoop} = [];
418 my $numCopiesAvailable = 0;
419 my $numCopiesOPACAvailable = 0;
420 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
421 my $itemNum = $itemInfo->{itemnumber};
422 my $itemLoopIter = {};
424 $itemLoopIter->{itemnumber} = $itemNum;
425 $itemLoopIter->{barcode} = $itemInfo->{barcode};
426 $itemLoopIter->{homeBranchName} = $itemInfo->{homebranch};
427 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
428 $itemLoopIter->{enumchron} = $itemInfo->{enumchron};
429 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
430 if ($itemLevelTypes) {
431 $itemLoopIter->{translated_description} = $itemInfo->{translated_description};
432 $itemLoopIter->{itype} = $itemInfo->{itype};
433 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
436 # If the holdingbranch is different than the homebranch, we show the
437 # holdingbranch of the document too.
438 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
439 $itemLoopIter->{holdingBranchName} = $itemInfo->{holdingbranch};
442 # If the item is currently on loan, we display its return date and
443 # change the background color.
444 my $issues= GetItemIssue($itemNum);
445 if ( $issues->{'date_due'} ) {
446 $itemLoopIter->{dateDue} = output_pref({ dt => dt_from_string($issues->{date_due}, 'sql'), as_due_date => 1 });
447 $itemLoopIter->{backgroundcolor} = 'onloan';
450 # checking reserve
451 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($itemNum);
452 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
454 # the item could be reserved for this borrower vi a host record, flag this
455 $reservedfor //= '';
457 if ( defined $reservedate ) {
458 $itemLoopIter->{backgroundcolor} = 'reserved';
459 $itemLoopIter->{reservedate} = output_pref({ dt => dt_from_string($reservedate), dateonly => 1 });
460 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
461 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
462 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
463 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
464 #waiting status
465 $itemLoopIter->{waitingdate} = $wait;
468 $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
469 $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
471 # Management of the notforloan document
472 if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
473 $itemLoopIter->{backgroundcolor} = 'other';
474 $itemLoopIter->{notforloanvalue} =
475 $notforloan_label_of->{ $itemLoopIter->{notforloan} };
478 # Management of lost or long overdue items
479 if ( $itemInfo->{itemlost} ) {
481 # FIXME localized strings should never be in Perl code
482 $itemLoopIter->{message} =
483 $itemInfo->{itemlost} == 1 ? "(lost)"
484 : $itemInfo->{itemlost} == 2 ? "(long overdue)"
485 : "";
486 $itemInfo->{backgroundcolor} = 'other';
489 # Check of the transfered documents
490 my ( $transfertwhen, $transfertfrom, $transfertto ) =
491 GetTransfers($itemNum);
492 if ( $transfertwhen && ($transfertwhen ne '') ) {
493 $itemLoopIter->{transfertwhen} = output_pref({ dt => dt_from_string($transfertwhen), dateonly => 1 });
494 $itemLoopIter->{transfertfrom} = $transfertfrom;
495 $itemLoopIter->{transfertto} = $transfertto;
496 $itemLoopIter->{nocancel} = 1;
499 # if the items belongs to a host record, show link to host record
500 if ( $itemInfo->{biblionumber} ne $biblioNum ) {
501 $biblioLoopIter{hostitemsflag} = 1;
502 $itemLoopIter->{hostbiblionumber} = $itemInfo->{biblionumber};
503 $itemLoopIter->{hosttitle} = GetBiblioData( $itemInfo->{biblionumber} )->{title};
506 # If there is no loan, return and transfer, we show a checkbox.
507 $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
509 my $branch = GetReservesControlBranch( $itemInfo, $borr );
511 my $policy_holdallowed = !$itemLoopIter->{already_reserved};
512 $policy_holdallowed &&=
513 IsAvailableForItemLevelRequest($itemInfo,$borr) &&
514 CanItemBeReserved($borrowernumber,$itemNum) eq 'OK';
516 if ($policy_holdallowed) {
517 if ( my $hold_allowed = OPACItemHoldsAllowed( $itemInfo, $borr ) ) {
518 $itemLoopIter->{available} = 1;
519 $numCopiesOPACAvailable++;
520 $biblioLoopIter{force_hold} = 1 if $hold_allowed eq 'F';
522 $numCopiesAvailable++;
525 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
527 # Show serial enumeration when needed
528 if ($itemLoopIter->{enumchron}) {
529 $itemdata_enumchron = 1;
532 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
534 $template->param( itemdata_enumchron => $itemdata_enumchron );
536 if ($numCopiesAvailable > 0) {
537 $numBibsAvailable++;
538 $biblioLoopIter{bib_available} = 1;
539 $biblioLoopIter{holdable} = 1;
540 $biblioLoopIter{itemholdable} = 1 if $numCopiesOPACAvailable;
542 if ($biblioLoopIter{already_reserved}) {
543 $biblioLoopIter{holdable} = undef;
544 $biblioLoopIter{itemholdable} = undef;
546 if(not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowernumber,$biblioNum)) {
547 $biblioLoopIter{holdable} = undef;
548 $biblioLoopIter{already_patron_possession} = 1;
551 $biblioLoopIter{holdable} &&= CanBookBeReserved($borrowernumber,$biblioNum) eq 'OK';
553 # For multiple holds per record, if a patron has previously placed a hold,
554 # the patron can only place more holds of the same type. That is, if the
555 # patron placed a record level hold, all the holds the patron places must
556 # be record level. If the patron placed an item level hold, all holds
557 # the patron places must be item level
558 my $forced_hold_level = Koha::Holds->search(
560 borrowernumber => $borrowernumber,
561 biblionumber => $biblioNum,
562 found => undef,
564 )->forced_hold_level();
565 if ($forced_hold_level) {
566 $biblioLoopIter{force_hold} = 1 if $forced_hold_level eq 'item';
567 $biblioLoopIter{itemholdable} = 0 if $forced_hold_level eq 'record';
571 push @$biblioLoop, \%biblioLoopIter;
573 $anyholdable = 1 if $biblioLoopIter{holdable};
577 if ( $numBibsAvailable == 0 || $anyholdable == 0) {
578 $template->param( none_available => 1 );
581 my $show_notes=C4::Context->preference('OpacHoldNotes');
582 $template->param(OpacHoldNotes=>$show_notes);
584 # display infos
585 $template->param(bibitemloop => $biblioLoop);
586 # can set reserve date in future
587 if (
588 C4::Context->preference( 'AllowHoldDateInFuture' ) &&
589 C4::Context->preference( 'OPACAllowHoldDateInFuture' )
591 $template->param(
592 reserve_in_future => 1,
596 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };