From 075e958764d8453e00fadd2fd34140af59ce10be Mon Sep 17 00:00:00 2001
From: Lari Taskula <larit@student.uef.fi>
Date: Fri, 8 Apr 2016 13:26:26 +0000
Subject: [PATCH] Bug 16223: Enable regex match for rules of debarment removals

Rebased-by: Emmi Takkinen <emmi.takkinen@koha-suomi.fi>

Signed-off-by: Katariina Hanhisalo <katariina.hanhisalo@xamk.fi>

Signed-off-by: Katariina Hanhisalo <katariina.hanhisalo@xamk.fi>
---
 Koha/Patron/Debarments.pm                          | 36 +++++++++++++---------
 .../prog/en/modules/admin/preferences/patrons.pref |  9 +++---
 2 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/Koha/Patron/Debarments.pm b/Koha/Patron/Debarments.pm
index 36cd70eb5b..144f01ac97 100644
--- a/Koha/Patron/Debarments.pm
+++ b/Koha/Patron/Debarments.pm
@@ -326,22 +326,28 @@ sub DelDebarmentsAfterPayment {
     my $total_due = $lines->total_outstanding;
 
     foreach my $debarment (@{ $debarments }){
-        if (exists $liftDebarmentRules->{$debarment->{'comment'}}) {
-            # Delete debarment IF:
-            # 1. there is no maximum outstanding fines defined for the liftDebarmentRule
-            #    and there is no outstanding fines.
-            # 2. there is a maximum outstanding fines amount defined
-            #    and total_due is smaller or equal than the defined maximum outstanding amount
-            # Otherwise, do not lift the debarment.
-            if (not defined $liftDebarmentRules->{$debarment->{'comment'}}->{'outstanding'}){
-                if ($total_due <= 0) {
-                    DelDebarment($debarment->{'borrower_debarment_id'});
-                }
+        my $rule;
+
+        foreach my $liftRule (keys %{ $liftDebarmentRules }){
+            my $comment = $debarment->{'comment'};
+            $rule = $liftRule if $comment =~ $liftRule;
+        }
+        next unless $rule;
+
+        # Delete debarment IF:
+        # 1. there is no maximum outstanding fines defined for the liftDebarmentRule
+        #    and there is no outstanding fines.
+        # 2. there is a maximum outstanding fines amount defined
+        #    and total_due is smaller or equal than the defined maximum outstanding amount
+        # Otherwise, do not lift the debarment.
+        if (not defined $liftDebarmentRules->{$rule}->{'outstanding'}){
+            if ($total_due <= 0) {
+                DelDebarment($debarment->{'borrower_debarment_id'});
             }
-            else {
-                if ($total_due <= $liftDebarmentRules->{$debarment->{'comment'}}->{'outstanding'}) {
-                    DelDebarment($debarment->{'borrower_debarment_id'});
-                }
+        }
+        else {
+            if ($total_due <= $liftDebarmentRules->{$rule}->{'outstanding'}) {
+                DelDebarment($debarment->{'borrower_debarment_id'});
             }
         }
     }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref
index a4b7780bc5..0db6f1b69b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref
@@ -104,13 +104,14 @@ Patrons:
          - pref: DebarmentsToLiftAfterPayment
            type: textarea
            class: code
-         - Lift these debarments after Borrower has paid his/her fees
-         - "<p>Example, debarment is lifted after all fees are paid:</p>"
+         - Lift these debarments after Borrower has paid his/her fees. Matches dynamic content in debarment comment.
+         - "<p>Example, debarments with comment (either exact match or dynamic content, e.g. 'Debarment message for Patron 123') that match 'Debarment message' are lifted after all fees are paid:</p>"
          - "<pre>Debarment message:</pre>
             <pre>  outstanding: 0</pre>"
-         - "<p>Example, debarment is lifted after payment with outstanding fees less or equal than 5:</p>"
-         - "<pre>Another debarment:</pre>
+         - "<p>Example, debarment with message of exactly 'Debarment message' is lifted after payment with outstanding fees less or equal than 5:</p>"
+         - "<pre>^Debarment message$:</pre>
             <pre>  outstanding: 5.00</pre>"
+         - You can use regex in the definitions to match your needs.
     Membership expiry:
      -
          - When renewing borrowers, base the new expiry date on
-- 
2.11.0