1 package FS::TaxEngine::internal;
4 use base 'FS::TaxEngine';
5 use FS::Record qw(dbh qsearch qsearchs);
9 FS::TaxEngine::internal: the classic Freeside "internal tax engine".
10 Uses tax rates and localities defined in L<FS::cust_main_county>.
17 my ($self, $cust_bill_pkg) = @_;
18 my $cust_pkg = $cust_bill_pkg->cust_pkg;
19 my $pkgpart = $cust_bill_pkg->pkgpart_override || $cust_pkg->pkgpart;
20 my $part_pkg = $part_pkg_cache{$pkgpart} ||= FS::part_pkg->by_key($pkgpart)
21 or die "pkgpart $pkgpart not found";
22 push @{ $self->{items} }, $cust_bill_pkg;
24 my $location = $cust_pkg->tax_location; # cacheable?
26 my @loc_keys = qw( district city county state country );
27 my %taxhash = map { $_ => $location->get($_) } @loc_keys;
29 $taxhash{'taxclass'} = $part_pkg->taxclass;
31 my @taxes = (); # entries are cust_main_county objects
32 my %taxhash_elim = %taxhash;
33 my @elim = qw( district city county state );
36 #first try a match with taxclass
37 @taxes = qsearch( 'cust_main_county', \%taxhash_elim );
39 if ( !scalar(@taxes) && $taxhash_elim{'taxclass'} ) {
40 #then try a match without taxclass
41 my %no_taxclass = %taxhash_elim;
42 $no_taxclass{ 'taxclass' } = '';
43 @taxes = qsearch( 'cust_main_county', \%no_taxclass );
46 $taxhash_elim{ shift(@elim) } = '';
47 } while ( !scalar(@taxes) && scalar(@elim) );
50 my $taxnum = $_->taxnum;
51 $self->{taxes}->{$taxnum} ||= [ $_ ];
52 push @{ $self->{taxes}->{$taxnum} }, $cust_bill_pkg;
57 my ($self, %opt) = @_;
58 my $tax_object = $opt{tax};
59 my $taxables = $opt{sales};
60 my $taxnum = $tax_object->taxnum;
61 my $exemptions = $self->{exemptions}->{$taxnum} ||= [];
63 my $name = $tax_object->taxname || 'Tax';
64 my $taxable_cents = 0;
67 my $cust_main = $self->{cust_main};
68 my $custnum = $cust_main->custnum;
69 my $invoice_time = $self->{invoice_time};
71 # set a flag if the customer is tax-exempt
73 my $conf = FS::Conf->new;
74 if ( $conf->exists('cust_class-tax_exempt') ) {
75 my $cust_class = $cust_main->cust_class;
76 $exempt_cust = $cust_class->tax if $cust_class;
78 $exempt_cust = $cust_main->tax;
80 # set a flag if the customer is exempt from this tax here
81 my $exempt_cust_taxname = $cust_main->tax_exemption($tax_object->taxname)
82 if $tax_object->taxname;
84 # Gather any exemptions that are already attached to these cust_bill_pkgs
85 # so that we can deduct them from the customer's monthly limit.
86 my @existing_exemptions = @{ $exemptions };
87 push @existing_exemptions, @{ $_->cust_tax_exempt_pkg }
90 my $tax_item = FS::cust_bill_pkg->new({
99 foreach my $cust_bill_pkg (@$taxables) {
101 my $cust_pkg = $cust_bill_pkg->cust_pkg;
102 my $part_pkg = $cust_bill_pkg->part_pkg;
104 my $taxable_charged = $cust_bill_pkg->setup + $cust_bill_pkg->recur
105 or next; # don't create zero-amount exemptions
107 # XXX the following procedure should probably be in cust_bill_pkg
109 if ( $exempt_cust ) {
111 push @new_exemptions, FS::cust_tax_exempt_pkg->new({
112 amount => $taxable_charged,
115 $taxable_charged = 0;
117 } elsif ( $exempt_cust_taxname ) {
119 push @new_exemptions, FS::cust_tax_exempt_pkg->new({
120 amount => $taxable_charged,
121 exempt_cust_taxname => 'Y',
123 $taxable_charged = 0;
127 if ( ($part_pkg->setuptax eq 'Y' or $tax_object->setuptax eq 'Y')
128 and $cust_bill_pkg->setup > 0 and $taxable_charged > 0 ) {
130 push @new_exemptions, FS::cust_tax_exempt_pkg->new({
131 amount => $cust_bill_pkg->setup,
134 $taxable_charged -= $cust_bill_pkg->setup;
137 if ( ($part_pkg->recurtax eq 'Y' or $tax_object->recurtax eq 'Y')
138 and $cust_bill_pkg->recur > 0 and $taxable_charged > 0 ) {
140 push @new_exemptions, FS::cust_tax_exempt_pkg->new({
141 amount => $cust_bill_pkg->recur,
144 $taxable_charged -= $cust_bill_pkg->recur;
148 if ( $tax_object->exempt_amount && $tax_object->exempt_amount > 0
149 and $taxable_charged > 0 ) {
150 # If the billing period extends across multiple calendar months,
151 # there may be several months of exemption available.
152 my $sdate = $cust_bill_pkg->sdate || $invoice_time;
153 my $start_month = (localtime($sdate))[4] + 1;
154 my $start_year = (localtime($sdate))[5] + 1900;
155 my $edate = $cust_bill_pkg->edate || $invoice_time;
156 my $end_month = (localtime($edate))[4] + 1;
157 my $end_year = (localtime($edate))[5] + 1900;
159 # If the partial last month + partial first month <= one month,
160 # don't use the exemption in the last month
161 # (unless the last month is also the first month, e.g. one-time
163 if ( (localtime($sdate))[3] >= (localtime($edate))[3]
164 and ($start_month != $end_month or $start_year != $end_year)
167 if ( $end_month == 0 ) {
173 # number of months of exemption available
174 my $freq = ($end_month - $start_month) +
175 ($end_year - $start_year) * 12 +
178 # divide equally among all of them
179 my $permonth = sprintf('%.2f', $taxable_charged / $freq);
181 #call the whole thing off if this customer has any old
182 #exemption records...
183 my @cust_tax_exempt =
184 qsearch( 'cust_tax_exempt' => { custnum=> $custnum } );
185 if ( @cust_tax_exempt ) {
187 'this customer still has old-style tax exemption records; '.
188 'run bin/fs-migrate-cust_tax_exempt?';
191 my ($mon, $year) = ($start_month, $start_year);
192 while ($taxable_charged > 0.005 and
193 ($year < $end_year or
194 ($year == $end_year and $mon <= $end_month)
198 # find the sum of the exemption used by this customer, for this tax,
202 FROM cust_tax_exempt_pkg
203 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
204 LEFT JOIN cust_bill USING ( invnum )
209 AND exempt_monthly = 'Y'
211 my $sth = dbh->prepare($sql) or
212 return "fatal: can't lookup existing exemption: ". dbh->errstr;
219 return "fatal: can't lookup existing exemption: ". dbh->errstr;
220 my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0;
222 # add any exemption we're already using for another line item
223 foreach ( grep { $_->taxnum == $tax_object->taxnum &&
224 $_->exempt_monthly eq 'Y' &&
227 } @existing_exemptions
230 $existing_exemption += $_->amount;
233 my $remaining_exemption =
234 $tax_object->exempt_amount - $existing_exemption;
235 if ( $remaining_exemption > 0 ) {
236 my $addl = $remaining_exemption > $permonth
238 : $remaining_exemption;
239 $addl = $taxable_charged if $addl > $taxable_charged;
241 push @new_exemptions, FS::cust_tax_exempt_pkg->new({
242 amount => sprintf('%.2f', $addl),
243 exempt_monthly => 'Y',
247 $taxable_charged -= $addl;
249 # if they're using multiple months of exemption for a multi-month
250 # package, then record the exemptions in separate months
260 $_->taxnum($tax_object->taxnum) foreach @new_exemptions;
262 # attach them to the line item
263 push @{ $cust_bill_pkg->cust_tax_exempt_pkg }, @new_exemptions;
264 push @existing_exemptions, @new_exemptions;
266 $taxable_charged = sprintf( "%.2f", $taxable_charged);
267 next if $taxable_charged == 0;
269 my $this_tax_cents = int($taxable_charged * $tax_object->tax);
270 my $location = FS::cust_bill_pkg_tax_location->new({
271 'taxnum' => $tax_object->taxnum,
272 'taxtype' => ref($tax_object),
273 'cents' => $this_tax_cents,
274 'pkgnum' => $cust_bill_pkg->pkgnum,
275 'locationnum' => $cust_bill_pkg->cust_pkg->tax_locationnum,
276 'taxable_cust_bill_pkg' => $cust_bill_pkg,
277 'tax_cust_bill_pkg' => $tax_item,
279 push @tax_location, $location;
281 $taxable_cents += $taxable_charged;
282 $tax_cents += $this_tax_cents;
283 } #foreach $cust_bill_pkg
285 # now round and distribute
286 my $extra_cents = sprintf('%.2f', $taxable_cents * $tax_object->tax / 100)
288 # make sure we have an integer
289 $extra_cents = sprintf('%.0f', $extra_cents);
290 if ( $extra_cents < 0 ) {
291 die "nonsense extra_cents value $extra_cents";
293 $tax_cents += $extra_cents;
295 foreach (@tax_location) { # can never require more than a single pass, yes?
296 my $cents = $_->get('cents');
297 if ( $extra_cents > 0 ) {
301 $_->set('amount', sprintf('%.2f', $cents/100));
303 $tax_item->set('setup' => sprintf('%.2f', $tax_cents / 100));
304 $tax_item->set('cust_bill_pkg_tax_location', \@tax_location);
313 rate_table => 'cust_main_county',
314 link_table => 'cust_bill_pkg_tax_location',