Bug 31963: Only show hold fee msg on OPAC if patron will be charged
[koha-ffzg.git] / opac / opac-reserve.pl
index df0582d..1200ef0 100755 (executable)
@@ -25,7 +25,7 @@ use CGI qw ( -utf8 );
 use C4::Auth qw( get_template_and_user );
 use C4::Koha qw( getitemtypeimagelocation getitemtypeimagesrc );
 use C4::Circulation qw( GetBranchItemRule );
-use C4::Reserves qw( CanItemBeReserved CanBookBeReserved AddReserve GetReservesControlBranch ItemsAnyAvailableAndNotRestricted IsAvailableForItemLevelRequest );
+use C4::Reserves qw( CanItemBeReserved CanBookBeReserved AddReserve GetReservesControlBranch ItemsAnyAvailableAndNotRestricted IsAvailableForItemLevelRequest GetReserveFee );
 use C4::Biblio qw( GetBiblioData GetFrameworkCode );
 use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
@@ -72,24 +72,6 @@ unless ( $can_place_hold_if_available_at_pickup ) {
     }
 }
 
-# check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
-if ( $category->effective_BlockExpiredPatronOpacActions ) {
-
-    if ( $patron->is_expired ) {
-
-        # cannot reserve, their card has expired and the rules set mean this is not allowed
-        $template->param( message => 1, expired_patron => 1 );
-        output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
-        exit;
-    }
-}
-
-# Pass through any reserve charge
-my $reservefee = $category->reservefee;
-if ( $reservefee > 0){
-    $template->param( RESERVE_CHARGE => $reservefee);
-}
-
 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
 
 # There are two ways of calling this script, with a single biblio num
@@ -123,53 +105,77 @@ if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
     exit;
 }
 
-
-# pass the pickup branch along....
-my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
-$template->param( branch => $branch );
-
 #
 #
-# Build hashes of the requested biblio(item)s and items.
+# Here we check that the borrower can actually make reserves Stage 1.
 #
 #
+my $noreserves     = 0;
+if ( $category->effective_BlockExpiredPatronOpacActions ) {
+    if ( $patron->is_expired ) {
+        # cannot reserve, their card has expired and the rules set mean this is not allowed
+        $noreserves = 1;
+        $template->param( message => 1, expired_patron => 1 );
+    }
+}
 
-my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
-foreach my $biblioNumber (@biblionumbers) {
-
-    my $biblioData = GetBiblioData($biblioNumber);
-    $biblioDataHash{$biblioNumber} = $biblioData;
+my $maxoutstanding = C4::Context->preference("maxoutstanding");
+my $amountoutstanding = $patron->account->balance;
+if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
+    my $amount = sprintf "%.02f", $amountoutstanding;
+    $template->param( message => 1 );
+    $noreserves = 1;
+    $template->param( too_much_oweing => $amount );
+}
 
-    my $biblio = Koha::Biblios->find( $biblioNumber );
-    next unless $biblio;
+if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
+    $noreserves = 1;
+    $template->param(
+        message => 1,
+        GNA     => 1
+    );
+}
 
-    my $marcrecord = $biblio->metadata->record;
+if ( $patron->lost && ($patron->lost == 1) ) {
+    $noreserves = 1;
+    $template->param(
+        message => 1,
+        lost    => 1
+    );
+}
 
-    my $items = Koha::Items->search_ordered(
-        [
-            biblionumber => $biblioNumber,
-            'me.itemnumber' => {
-                -in => [
-                    $biblio->host_items->get_column('itemnumber')
-                ]
-            }
-        ],
-        { prefetch => [ 'issue', 'homebranch', 'holdingbranch' ] }
-    )->filter_by_visible_in_opac({ patron => $patron });
+if ( $patron->is_debarred ) {
+    $noreserves = 1;
+    $template->param(
+        message          => 1,
+        debarred         => 1,
+        debarred_comment => $patron->debarredcomment,
+        debarred_date    => $patron->debarred,
+    );
+}
 
-    $biblioData->{items} = [$items->as_list]; # FIXME Potentially a lot in memory here!
+my $holds = $patron->holds;
+my $reserves_count = $holds->count;
+$template->param( RESERVES => $holds->unblessed );
+if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
+    $template->param( message => 1 );
+    $noreserves = 1;
+    $template->param( too_many_reserves => $holds->count );
+}
 
-    # Compute the priority rank.
-    $biblioData->{object} = $biblio;
-    my $reservecount = $biblio->holds->search({ found => [ {"!=" => "W"},undef] })->count;
-    $biblioData->{reservecount} = $reservecount;
-    $biblioData->{rank} = $reservecount + 1;
+if( $noreserves ){
+    output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
+    exit;
 }
 
+
+# pass the pickup branch along....
+my $branch = $query->param('branch') || $patron->branchcode || C4::Context->userenv->{branch} || '' ;
+$template->param( branch => $branch );
+
 #
 #
-# If this is the second time through this script, it
-# means we are carrying out the hold request, possibly
+# Here we are carrying out the hold request, possibly
 # with a specific item for each biblionumber.
 #
 #
@@ -237,17 +243,11 @@ if ( $query->param('place_reserve') ) {
             }
         }
 
-#item may belong to a host biblio, if yes change biblioNum to hosts bilbionumber
+        # if we have an item, we are placing the hold on the item's bib, in case of analytics
         if ( $item ) {
-            my $hostbiblioNum = $item->biblio->biblionumber;
-            if ( $hostbiblioNum ne $biblioNum ) {
-                $biblioNum = $hostbiblioNum;
-            }
+            $biblioNum = $item->biblionumber;
         }
 
-        my $biblioData = $biblioDataHash{$biblioNum};
-        my $found;
-
         # Check for user supplied reserve date
         my $startdate;
         if (   C4::Context->preference('AllowHoldDateInFuture')
@@ -261,7 +261,8 @@ if ( $query->param('place_reserve') ) {
         my $itemtype = $query->param('itemtype') || undef;
         $itemtype = undef if $itemNum;
 
-        my $rank = $biblioData->{rank};
+        my $biblio = Koha::Biblios->find($biblioNum);
+        my $rank = $biblio->holds->search( { found => [ { "!=" => "W" }, undef ] } )->count + 1;
         if ( $item ) {
             $canreserve = 1 if CanItemBeReserved( $patron, $item, $branch )->{status} eq 'OK';
         }
@@ -300,9 +301,9 @@ if ( $query->param('place_reserve') ) {
                     reservation_date => $startdate,
                     expiration_date  => $patron_expiration_date,
                     notes            => $notes,
-                    title            => $biblioData->{title},
+                    title            => $biblio->title,
                     itemnumber       => $itemNum,
-                    found            => $found,
+                    found            => undef,
                     itemtype         => $itemtype,
                 }
             );
@@ -317,65 +318,48 @@ if ( $query->param('place_reserve') ) {
 
 #
 #
-# Here we check that the borrower can actually make reserves Stage 1.
+# Build hashes of the requested biblio(item)s and items.
 #
 #
-my $noreserves     = 0;
-my $maxoutstanding = C4::Context->preference("maxoutstanding");
-my $amountoutstanding = $patron->account->balance;
-if ( $amountoutstanding && ($amountoutstanding > $maxoutstanding) ) {
-    my $amount = sprintf "%.02f", $amountoutstanding;
-    $template->param( message => 1 );
-    $noreserves = 1;
-    $template->param( too_much_oweing => $amount );
-}
 
-if ( $patron->gonenoaddress && ($patron->gonenoaddress == 1) ) {
-    $noreserves = 1;
-    $template->param(
-        message => 1,
-        GNA     => 1
-    );
-}
+my %biblioDataHash; # Hash of biblionumber to biblio/biblioitems record.
+foreach my $biblioNumber (@biblionumbers) {
 
-if ( $patron->lost && ($patron->lost == 1) ) {
-    $noreserves = 1;
-    $template->param(
-        message => 1,
-        lost    => 1
-    );
-}
+    my $biblioData = GetBiblioData($biblioNumber);
+    $biblioDataHash{$biblioNumber} = $biblioData;
 
-if ( $patron->is_debarred ) {
-    $noreserves = 1;
-    $template->param(
-        message          => 1,
-        debarred         => 1,
-        debarred_comment => $patron->debarredcomment,
-        debarred_date    => $patron->debarred,
-    );
-}
+    my $biblio = Koha::Biblios->find( $biblioNumber );
+    next unless $biblio;
 
-my $holds = $patron->holds;
-my $reserves_count = $holds->count;
-$template->param( RESERVES => $holds->unblessed );
-if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) {
-    $template->param( message => 1 );
-    $noreserves = 1;
-    $template->param( too_many_reserves => $holds->count );
-}
+    my $items = Koha::Items->search_ordered(
+        [
+            biblionumber => $biblioNumber,
+            'me.itemnumber' => {
+                -in => [
+                    $biblio->host_items->get_column('itemnumber')
+                ]
+            }
+        ],
+        { prefetch => [ 'issue', 'homebranch', 'holdingbranch' ] }
+    )->filter_by_visible_in_opac({ patron => $patron });
 
-unless ( $noreserves ) {
-    my $requested_reserves_count = scalar( @biblionumbers );
-    if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
-        $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
-    }
+    $biblioData->{items} = [$items->as_list]; # FIXME Potentially a lot in memory here!
+
+    # Compute the priority rank.
+    $biblioData->{object} = $biblio;
+    my $reservecount = $biblio->holds->search({ found => [ {"!=" => "W"},undef] })->count;
+    $biblioData->{reservecount} = $reservecount;
+    $biblioData->{rank} = $reservecount + 1;
 }
 
-unless ($noreserves) {
-    $template->param( select_item_types => 1 );
+
+my $requested_reserves_count = scalar( @biblionumbers );
+if ( $maxreserves && ( $reserves_count + $requested_reserves_count > $maxreserves ) ) {
+    $template->param( new_reserves_allowed => $maxreserves - $reserves_count );
 }
 
+$template->param( select_item_types => 1 );
+
 
 #
 #
@@ -424,7 +408,6 @@ foreach my $biblioNum (@biblionumbers) {
     $biblioLoopIter{rank} = $biblioData->{rank};
     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
-    $biblioLoopIter{reqholdnotes}=0; #TODO: For future use
 
     if (!$itemLevelTypes && $biblioData->{itemtype}) {
         $biblioLoopIter{translated_description} = $itemtypes->{$biblioData->{itemtype}}{translated_description};
@@ -586,6 +569,8 @@ foreach my $biblioNum (@biblionumbers) {
         $biblioLoopIter{forced_hold_level} = $forced_hold_level;
     }
 
+    # Pass through any reserve charge
+    $biblioLoopIter{reserve_charge} = GetReserveFee( $patron->id, $biblioNum );
 
     push @$biblioLoop, \%biblioLoopIter;