From c536978ff94fce6e3f81c37a83c226196cd9d2d2 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 14 Jan 2016 14:52:06 +0000
Subject: [PATCH] Bug 15582: Ability to block auto renewals if
 OPACFineNoRenewals is reached

If a patron owes more than the OPACFineNoRenewals value, the issue won't
be auto renewed anymore (driven by the new pref OPACFineNoRenewalsBlockAutoRenew).

Test plan:
Note: You will have to manually change data in your DB, make sure you
have access to the sql cli.
1/ Set the OPACFineNoRenewals to 5 (for instance)
2/ Set OPACFineNoRenewalsBlockAutoRenew to block
3/ Check an item out to a patron and mark is as an auto renewal
4/ Make sure the patron does not have any fees or charges.
5/ Execute the automatic renewals cronjob script (misc/cronjobs/automatic_renewals.pl)
Confirm that the issue has been renewed
6/ Create an invoice for this patron with a amount > OPACFineNoRenewals (6
for instance)
7/ Execute the automatic renewals cronjob script (misc/cronjobs/automatic_renewals.pl)
Confirm that the issue has not been renewed.
8/ Set OPACFineNoRenewalsBlockAutoRenew to allow
9/ Execute the automatic renewals cronjob script (misc/cronjobs/automatic_renewals.pl)
Confirm that the issue has been renewed

Sponsored-by: University of the Arts London
Signed-off-by: Jonathan Field <jonathan.field@ptfs-europe.com>
Signed-off-by: Janet McGowan <janet.mcgowan@ptfs-europe.com>
---
 C4/Circulation.pm                                  |  8 +++++
 .../intranet-tmpl/prog/en/modules/circ/renew.tt    |  2 +-
 t/db_dependent/Circulation.t                       | 42 +++++++++++++++++++++-
 3 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 074706f..eb7c893 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2742,6 +2742,14 @@ sub CanBookBeRenewed {
                 return ( 0, "auto_too_late" );
             }
         }
+
+        if ( C4::Context->preference('OPACFineNoRenewalsBlockAutoRenew') ) {
+            my $fine_no_renewals = C4::Context->preference("OPACFineNoRenewals");
+            my ( $amountoutstanding ) = C4::Members::GetMemberAccountRecords($borrower->{borrowernumber});
+            if ( $amountoutstanding and $amountoutstanding > $fine_no_renewals ) {
+                return ( 0, "auto_too_much_oweing" );
+            }
+        }
     }
 
     if ( defined $issuing_rule->norenewalbefore
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt
index b63d7a4..dbcc096 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt
@@ -90,7 +90,7 @@
                                     </form>
                                 [% END %]
 
-                            [% ELSIF error == "auto_renew" %]
+                            [% ELSIF error == "auto_renew" or error == "auto_too_much_oweing" %]
 
                                 <p>[% item.biblio.title %] [% item.biblioitem.subtitle %] ( [% item.barcode %] ) has been scheduled for automatic renewal. </p>
 
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index 52aa087..78c0ad7 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 92;
+use Test::More tests => 93;
 
 BEGIN {
     require_ok('C4::Circulation');
@@ -624,6 +624,46 @@ C4::Context->dbh->do("DELETE FROM accountlines");
         is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
     };
 
+    subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
+        plan tests => 6;
+        my $item_to_auto_renew = $builder->build({
+            source => 'Item',
+            value => {
+                biblionumber => $biblionumber,
+                homebranch       => $branch,
+                holdingbranch    => $branch,
+            }
+        });
+
+        my $ten_days_before = dt_from_string->add( days => -10 );
+        my $ten_days_ahead = dt_from_string->add( days => 10 );
+        AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
+
+        $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
+        C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
+        C4::Context->set_preference('OPACFineNoRenewals','10');
+        my $fines_amount = 5;
+        C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
+        ( $renewokay, $error ) =
+          CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
+        is( $renewokay, 0, 'Do not renew, renewal is automatic' );
+        is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
+
+        C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
+        ( $renewokay, $error ) =
+          CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
+        is( $renewokay, 0, 'Do not renew, renewal is automatic' );
+        is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
+
+        C4::Accounts::manualinvoice( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber}, "Some fines", 'F', $fines_amount );
+        ( $renewokay, $error ) =
+          CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
+        is( $renewokay, 0, 'Do not renew, renewal is automatic' );
+        is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
+
+        $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
+    };
+
     subtest "GetLatestAutoRenewDate" => sub {
         plan tests => 5;
         my $item_to_auto_renew = $builder->build(
-- 
2.1.4