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:57 -0800
commite3c7f3bd2297eaa064554de40e5feb03373dcf04 (patch)
tree3c3daaf1e029b2bb32bc75b5d93e0c63d0daa76d /FS/FS/cust_credit.pm
parent04acd6c7c1711d316e1e00c7ab1397a9f0205723 (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,