Merge branch 'FREESIDE_3_BRANCH' of git.freeside.biz:/home/git/freeside into 3.x
authorMark Wells <mark@freeside.biz>
Mon, 17 Oct 2016 23:15:54 +0000 (16:15 -0700)
committerMark Wells <mark@freeside.biz>
Mon, 17 Oct 2016 23:15:54 +0000 (16:15 -0700)
FS/FS/cust_credit.pm
FS/FS/cust_pkg.pm

index 18f4a32..ca41075 100644 (file)
@@ -743,10 +743,12 @@ sub calculate_tax_adjustment {
     if ($recur) {
       $recur -= $cust_bill_pkg->credited('', '', setuprecur => 'recur') || 0;
     }
+    # Skip line items that have been completely credited.
+    next if ($setup + $recur) == 0;
     my $setup_ratio = $setup / ($setup + $recur);
 
-    # Calculate the fraction of tax to credit: it's the fraction of this charge
-    # (either setup or recur) that's being credited.
+    # Calculate the fraction of tax to credit: it's the fraction of this
+    # charge (either setup or recur) that's being credited.
     my $charged = ($setuprecur eq 'setup') ? $setup : $recur;
     next if $charged == 0; # shouldn't happen, but still...
 
@@ -911,7 +913,7 @@ sub credit_lineitems {
     my $invnum = $cust_bill_pkg->invnum;
 
     $need_to_unapply -= $cust_bill_pkg->owed($setuprecur);
-    next if $need_to_unapply < 0.005;
+    return if $need_to_unapply < 0.005;
 
     my $error;
     # then unapply payments one at a time (partially if need be) until the
index 467b54d..b5506f6 100644 (file)
@@ -1792,6 +1792,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?)
@@ -1800,14 +1802,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;
+        }
 
       }