summaryrefslogtreecommitdiff
path: root/FS/FS/cust_credit.pm
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2016-02-04 16:52:35 -0800
committerMark Wells <mark@freeside.biz>2016-02-04 16:52:43 -0800
commit899740c29e1db6c2baef42a7bec0430a7c9d967a (patch)
tree4477983e4d325ce25756f1a7a137fd6d932d5e31 /FS/FS/cust_credit.pm
parent91e5e9cab28d10b19a80681e2d89632a991b04d7 (diff)
correctly divide tax into setup and recur portions when calculating credit amounts, #26925
Diffstat (limited to 'FS/FS/cust_credit.pm')
-rw-r--r--FS/FS/cust_credit.pm41
1 files changed, 31 insertions, 10 deletions
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,