From 2b11b180fca025304df07c90464929a1ea46ea68 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 11 Feb 2020 13:18:10 +0000
Subject: [PATCH] Bug 19014: on_reserve blocks auto_renew

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 C4/Circulation.pm            | 14 +++++++++-----
 t/db_dependent/Circulation.t |  7 ++++++-
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index bda8815e30..379f1f46b5 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2693,6 +2693,7 @@ sub CanBookBeRenewed {
 
     my $dbh    = C4::Context->dbh;
     my $renews = 1;
+    my $auto_renew = 0;
 
     my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
     my $issue = $item->checkout or return ( 0, 'no_checkout' );
@@ -2794,17 +2795,19 @@ sub CanBookBeRenewed {
                 return ( 0, "too_soon" );
             }
             elsif ( $issue->auto_renew ) {
-                return ( 0, "auto_renew" );
+                $auto_renew = 1;
             }
         }
 
         # Fallback for automatic renewals:
         # If norenewalbefore is undef, don't renew before due date.
-        if ( $issue->auto_renew ) {
+        if ( $issue->auto_renew && !$auto_renew ) {
             my $now = dt_from_string;
-            return ( 0, "auto_renew" )
-              if $now >= dt_from_string( $issue->date_due, 'sql' );
-            return ( 0, "auto_too_soon" );
+            if ( $now >= dt_from_string( $issue->date_due, 'sql' ) ){
+                $auto_renew = 1;
+            } else {
+                return ( 0, "auto_too_soon" );
+            }
         }
     }
 
@@ -2874,6 +2877,7 @@ sub CanBookBeRenewed {
         }
     }
     return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
+    return ( 0, "auto_renew" ) if $auto_renew && !$override_limit; # 0 if auto-renewal should not succeed
 
     return ( 1, undef );
 }
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index 825dbd7b12..41ed041b9f 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -274,7 +274,7 @@ Koha::CirculationRules->set_rules(
 
 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
 subtest "CanBookBeRenewed tests" => sub {
-    plan tests => 75;
+    plan tests => 77;
 
     C4::Context->set_preference('ItemsDeniedRenewal','');
     # Generate test biblio
@@ -638,6 +638,11 @@ subtest "CanBookBeRenewed tests" => sub {
     ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber, 1 );
     is( $renewokay, 0, 'Still should not be able to renew' );
     is( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
+    $dbh->do('UPDATE circulation_rules SET rule_value = 0 where rule_name = "norenewalbefore"');
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber, 1 );
+    is( $renewokay, 0, 'Still should not be able to renew' );
+    is( $error, 'on_reserve', 'returned code is on_reserve, auto_renew only happens if not on reserve' );
+    ModReserveCancelAll($item_4->itemnumber, $reserving_borrowernumber);
 
 
 
-- 
2.20.1