From cb50fa5d5617b601fca001e3a63a7fc06a0379a5 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 23 Jan 2020 02:37:19 +0000
Subject: [PATCH] Bug 24476: Change CanBookBeRenewed and adjust tests

Signed-off-by: Andrew <andrew@bywatersolutions.com>
---
 C4/Circulation.pm            |  8 ++++----
 t/db_dependent/Circulation.t | 28 ++++++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index ad196ab843..03012d1c71 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2771,7 +2771,7 @@ sub CanBookBeRenewed {
         return ( 0, 'overdue');
     }
 
-    if ( $issue->auto_renew ) {
+    if ( $issue->auto_renew && $patron->autorenewal ) {
 
         if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) {
             return ( 0, 'auto_account_expired' );
@@ -2827,17 +2827,17 @@ sub CanBookBeRenewed {
 
         if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) )
         {
-            return ( 0, "auto_too_soon" ) if $issue->auto_renew;
+            return ( 0, "auto_too_soon" ) if $issue->auto_renew && $patron->autorenewal;
             return ( 0, "too_soon" );
         }
-        elsif ( $issue->auto_renew ) {
+        elsif ( $issue->auto_renew && $patron->autorenewal ) {
             return ( 0, "auto_renew" );
         }
     }
 
     # Fallback for automatic renewals:
     # If norenewalbefore is undef, don't renew before due date.
-    if ( $issue->auto_renew ) {
+    if ( $issue->auto_renew && $patron->autorenewal ) {
         my $now = dt_from_string;
         return ( 0, "auto_renew" )
           if $now >= dt_from_string( $issue->date_due, 'sql' );
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index c0e2dacece..6b1fbc3bc6 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -277,7 +277,7 @@ $dbh->do(
 
 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
 subtest "CanBookBeRenewed tests" => sub {
-    plan tests => 71;
+    plan tests => 77;
 
     C4::Context->set_preference('ItemsDeniedRenewal','');
     # Generate test biblio
@@ -358,7 +358,8 @@ subtest "CanBookBeRenewed tests" => sub {
     my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
     my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
 
-    my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
+    my $renewing_borrower_obj = Koha::Patrons->find( $renewing_borrowernumber );
+    my $renewing_borrower = $renewing_borrower_obj->unblessed;
     my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
     my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
 
@@ -607,6 +608,12 @@ subtest "CanBookBeRenewed tests" => sub {
     is( $error, 'auto_too_soon',
         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 1, 'No renewal before is undef, but patron opted out of auto_renewal' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
+
     # Bug 7413
     # Test premature manual renewal
     $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
@@ -642,6 +649,12 @@ subtest "CanBookBeRenewed tests" => sub {
         'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
     );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 0, 'No renewal before is 7, patron opted out of auto_renewal still cannot renew early' );
+    is( $error, 'too_soon', 'Error is too_soon, no auto' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
     # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
     # and test automatic renewal again
     $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
@@ -652,6 +665,12 @@ subtest "CanBookBeRenewed tests" => sub {
         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
     );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 0, 'No renewal before is 0, patron opted out of auto_renewal still cannot renew early' );
+    is( $error, 'too_soon', 'Error is too_soon, no auto' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
     # Change policy so that loans can be renewed 99 days prior to the due date
     # and test automatic renewal again
     $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
@@ -662,6 +681,11 @@ subtest "CanBookBeRenewed tests" => sub {
         'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
     );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 1, 'No renewal before is 99, patron opted out of auto_renewal so can renew' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
     subtest "too_late_renewal / no_auto_renewal_after" => sub {
         plan tests => 14;
         my $item_to_auto_renew = $builder->build(
-- 
2.11.0