From e3c7f3bd2297eaa064554de40e5feb03373dcf04 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Thu, 4 Feb 2016 16:52:35 -0800 Subject: [PATCH] correctly divide tax into setup and recur portions when calculating credit amounts, #26925 --- FS/FS/cust_credit.pm | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index 032953532..6b21d69b9 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, -- 2.11.0