From: Mark Wells <mark@freeside.biz>
Date: Mon, 17 Oct 2016 23:03:13 +0000 (-0700)
Subject: limit unused-time credit if the invoice already has credits applied, #72873, from... 
X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=76aa1ddd3ef1c63972fa4e940cbdc9a84fde35a1;hp=b25a5e64054fed0ba59842442a5921ba5a3d1071

limit unused-time credit if the invoice already has credits applied, #72873, from #42729
---

diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm
index 2c7550633..714d29259 100644
--- a/FS/FS/cust_pkg.pm
+++ b/FS/FS/cust_pkg.pm
@@ -1810,6 +1810,8 @@ sub credit_remaining {
         # the cancellation date (can happen with advance billing). in that
         # case, use the entire recurring charge:
         my $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
+        my $max_credit = $amount
+            - $cust_bill_pkg->credited('', '', setuprecur => 'recur') || 0;
 
         # but if the cancellation happens during the interval, prorate it:
         # (XXX obey prorate_round_day here?)
@@ -1818,14 +1820,23 @@ sub credit_remaining {
                       ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
         }
 
+        # if there are existing credits, don't let the sum of credits exceed
+        # the recurring charge
+        $amount = $max_credit if $amount > $max_credit;
+
         $amount = sprintf('%.2f', $amount);
 
-        push @billpkgnums, $cust_bill_pkg->billpkgnum;
-        push @amounts,     $amount;
-        push @setuprecurs, 'recur';
+        # if no time has been used and/or there are existing line item
+        # credits, we may end up not needing to credit anything.
+        if ( $amount > 0 ) {
 
-        warn "Crediting for $amount on package ".$remain_pkg->pkgnum."\n"
-          if $DEBUG;
+          push @billpkgnums, $cust_bill_pkg->billpkgnum;
+          push @amounts,     $amount;
+          push @setuprecurs, 'recur';
+
+          warn "Crediting for $amount on package ".$remain_pkg->pkgnum."\n"
+            if $DEBUG;
+        }
 
       }