From aff0beb6c02cea2c9d903edb945089c414fede8f Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Mon, 16 Sep 2013 12:27:07 -0700 Subject: [PATCH] allow for taxes when using "fee" event to negate credit balance, #24991 --- FS/FS/cust_main/Billing.pm | 10 ++++------ FS/FS/part_event/Action/fee.pm | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index e35128557..aeb3feced 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -1160,6 +1160,10 @@ sub _make_lines { return $error if $error; } + $cust_bill_pkg->set_display( part_pkg => $part_pkg, + real_pkgpart => $real_pkgpart, + ); + push @$cust_bill_pkgs, $cust_bill_pkg; } #if $setup != 0 || $recur != 0 @@ -1277,12 +1281,6 @@ sub _handle_taxes { } - #what's this doing in the middle of _handle_taxes? probably should split - #this into three parts above in _make_lines - $cust_bill_pkg->set_display( part_pkg => $part_pkg, - real_pkgpart => $real_pkgpart, - ); - my %tax_cust_bill_pkg = $cust_bill_pkg->disintegrate; foreach my $key (keys %tax_cust_bill_pkg) { my @taxes = @{ $taxes{$key} || [] }; diff --git a/FS/FS/part_event/Action/fee.pm b/FS/FS/part_event/Action/fee.pm index cd9e200c8..c2b4673fa 100644 --- a/FS/FS/part_event/Action/fee.pm +++ b/FS/FS/part_event/Action/fee.pm @@ -32,7 +32,48 @@ sub _calc_fee { if ( $balance >= 0 ) { return 0; } elsif ( (-1 * $balance) < $self->option('charge') ) { - return -1 * $balance; + my $total = -1 * $balance; + # if it's tax exempt, then we're done + # XXX we also bail out if you're using external tax tables, because + # they're definitely NOT linear and we haven't yet had a reason to + # make that case work. + return $total if $self->option('setuptax') eq 'Y' + or FS::Conf->new->exists('enable_taxproducts'); + + # estimate tax rate + # false laziness with xmlhttp-calculate_taxes, cust_main::Billing, etc. + # XXX not accurate with monthly exemptions + my $cust_main = $cust_object->cust_main; + my $taxlisthash = {}; + my $charge = FS::cust_bill_pkg->new({ + setup => $total, + recur => 0, + details => [] + }); + my $part_pkg = FS::part_pkg->new({ + taxclass => $self->option('taxclass') + }); + my $error = $cust_main->_handle_taxes( + FS::part_pkg->new({ taxclass => ($self->option('taxclass') || '') }), + $taxlisthash, + $charge, + FS::cust_pkg->new({custnum => $cust_main->custnum}), + ); + if ( $error ) { + warn "error estimating taxes for breakage charge: custnum ".$cust_main->custnum."\n"; + return $total; + } + # $taxlisthash: tax identifier => [ cust_main_county, cust_bill_pkg... ] + my $total_rate = 0; + my @taxes = map { $_->[0] } values %$taxlisthash; + foreach (@taxes) { + $total_rate += $_->tax; + } + return $total if $total_rate == 0; # no taxes apply + + my $total_cents = $total * 100; + my $charge_cents = sprintf('%.0f', $total_cents * 100/(100 + $total_rate)); + return ($charge_cents / 100); } } -- 2.11.0