[freeside-commits] branch FREESIDE_4_BRANCH updated. 899740c29e1db6c2baef42a7bec0430a7c9d967a

Mark Wells mark at 420.am
Thu Feb 4 16:54:14 PST 2016


The branch, FREESIDE_4_BRANCH has been updated
       via  899740c29e1db6c2baef42a7bec0430a7c9d967a (commit)
      from  91e5e9cab28d10b19a80681e2d89632a991b04d7 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 899740c29e1db6c2baef42a7bec0430a7c9d967a
Author: Mark Wells <mark at freeside.biz>
Date:   Thu Feb 4 16:52:35 2016 -0800

    correctly divide tax into setup and recur portions when calculating credit amounts, #26925

diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm
index 0329535..6b21d69 100644
--- a/FS/FS/cust_credit.pm
+++ b/FS/FS/cust_credit.pm
@@ -723,19 +723,31 @@ sub calculate_tax_adjustment {
     $cust_bill{ $invnum}->custnum == $arg{custnum}
       or die "lineitem #$billpkgnum not found\n";
 
-    # calculate credit ratio.
-    # (First deduct any existing credits applied to this line item, to avoid
-    # rounding errors.)
-    my $charged = $cust_bill_pkg->get($setuprecur);
-    my $previously_credited =
-      $cust_bill_pkg->credited( '', '', setuprecur => $setuprecur) || 0;
-
-    $charged -= $previously_credited;
+    # tax_Xlocation records don't distinguish setup and recur, so calculate
+    # the fraction of setup+recur (after deducting credits) that's setup. This
+    # will also be the fraction of tax (after deducting credits) that's tax on
+    # setup.
+    my ($setup, $recur);
+    $setup = $cust_bill_pkg->get('setup') || 0;
+    if ($setup) {
+      $setup -= $cust_bill_pkg->credited('', '', setuprecur => 'setup') || 0;
+    }
+    $recur = $cust_bill_pkg->get('recur') || 0;
+    if ($recur) {
+      $recur -= $cust_bill_pkg->credited('', '', setuprecur => '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.
+    my $charged = ($setuprecur eq 'setup') ? $setup : $recur;
+    next if $charged == 0; # shouldn't happen, but still...
+
     if ($charged < $amount) {
       $error = "invoice #$invnum: tried to credit $amount, but only $charged was charged";
       last;
     }
-    my $ratio = $amount / $charged;
+    my $credit_ratio = $amount / $charged;
 
     # gather taxes that apply to the selected item
     foreach my $table (
@@ -750,7 +762,16 @@ sub calculate_tax_adjustment {
         foreach ($tax_link->cust_credit_bill_pkg) {
           $tax_amount -= $_->amount;
         }
-        my $tax_credit = sprintf('%.2f', $tax_amount * $ratio);
+        # split tax amount based on setuprecur
+        # (this method ensures that, if you credit both setup and recur tax,
+        # it always equals the entire tax despite any rounding)
+        my $setup_tax = sprintf('%.2f', $tax_amount * $setup_ratio);
+        if ( $setuprecur eq 'setup' ) {
+          $tax_amount = $setup_tax;
+        } else {
+          $tax_amount = $tax_amount - $setup_tax;
+        }
+        my $tax_credit = sprintf('%.2f', $tax_amount * $credit_ratio);
         my $pkey = $tax_link->get($tax_link->primary_key);
         push @taxlines, {
           table   => $table,

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/cust_credit.pm |   41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)




More information about the freeside-commits mailing list