6 use FS::Record qw(qsearch qsearchs);
12 FS::TaxEngine - Base class for tax calculation engines.
16 1. At the start of creating an invoice, create an FS::TaxEngine object.
17 2. Each time a sale item is added to the invoice, call L</add_sale> on the
19 3. Set the "pending" flag on the invoice.
20 4. Insert the invoice and its line items.
22 - If the TaxEngine is "batch" style (Billsoft):
23 5. After creating all invoices for the day, call
24 FS::TaxEngine::process_tax_batch. This will create the tax items for
25 all of the pending invoices, clear the "pending" flag, and call
26 L<FS::cust_main::Billing/collect> on each of the billed customers.
28 - If not (the internal tax system, CCH):
29 5. After adding all sale items, call L</calculate_taxes> on the TaxEngine to
30 produce a list of tax line items.
31 6. Append the tax line items to the invoice.
32 7. Update the invoice with the new charged amount and clear the pending flag.
38 =item new 'cust_main' => CUST_MAIN, 'invoice_time' => TIME, OPTIONS...
40 Creates an L<FS::TaxEngine> object. The subclass will be chosen by the
41 'enable_taxproducts' configuration setting.
43 CUST_MAIN and TIME are required. OPTIONS can include:
45 "cancel" => 1 to indicate that the package is being billed on cancellation.
47 "estimate" => 1 to indicate that this calculation is for tax estimation,
48 and isn't an actual sale invoice, in case that matters.
55 my $conf = FS::Conf->new;
56 if ($class eq 'FS::TaxEngine') {
57 my $subclass = $conf->config('enable_taxproducts') || 'internal';
58 $class .= "::$subclass";
61 die "couldn't load $class: $@\n" if $@;
63 my $self = { items => [], taxes => {}, conf => $conf, %opt };
69 Returns a hashref of metadata about this tax method, including:
70 - batch: whether this is a batch-style engine (requires different usage)
71 - override: whether this engine uses tax overrides
72 - manual_tax_location: whether this engine requires the user to select a "tax
73 location" separate from the address/city/state/zip fields
74 - rate_table: the table that stores the tax rates
75 (the 'taxline' method of that class will be used to calculate line-item
77 - link_table: the table that links L<FS::cust_bill_pkg> records for taxes
78 to the C<rate_table> entry that generated them, and to the item they
87 =item add_sale CUST_BILL_PKG
89 Adds the CUST_BILL_PKG object as a taxable sale on this invoice.
91 =item calculate_taxes INVOICE
93 Calculates the taxes on the taxable sales and returns a list of
94 L<FS::cust_bill_pkg> objects to add to the invoice. The base implementation
95 is to call L</make_taxlines> to produce a list of "raw" tax line items,
96 then L</consolidate_taxlines> to combine those with the same itemdesc.
98 If this fails, it will throw an exception. (Accordingly it should not trap
99 exceptions from internal methods that it calls, except to translate error
100 messages into a more meaningful form.) If it succeeds, it MUST return an
101 arrayref (even if the arrayref is empty).
105 sub calculate_taxes {
107 my $cust_bill = shift;
109 my @raw_taxlines = $self->make_taxlines($cust_bill);
111 my @real_taxlines = $self->consolidate_taxlines(@raw_taxlines);
113 if ( $cust_bill and $cust_bill->get('invnum') ) {
114 $_->set('invnum', $cust_bill->get('invnum')) foreach @real_taxlines;
116 return \@real_taxlines;
121 my $conf = $self->{conf};
123 my $cust_bill = shift;
127 # For each distinct tax rate definition, calculate the tax and exemptions.
128 foreach my $taxnum ( keys %{ $self->{taxes} } ) {
130 my $taxables = $self->{taxes}{$taxnum};
131 my $tax_object = shift @$taxables;
132 # $tax_object is a cust_main_county or tax_rate
133 # (with billpkgnum, pkgnum, locationnum set)
134 # the rest of @{ $taxlisthash->{$tax} } is cust_bill_pkg component objects
135 # (setup, recurring, usage classes)
137 my $taxline = $self->taxline('tax' => $tax_object, 'sales' => $taxables);
138 # taxline methods are now required to return real line items
139 # with their link records
140 die $taxline unless ref($taxline);
142 push @taxlines, $taxline;
149 sub consolidate_taxlines {
152 my $conf = $self->{conf};
154 my @raw_taxlines = @_;
157 # keys are tax names (as printed on invoices / itemdesc )
158 # values are arrayrefs of taxlines
160 # collate these by itemdesc
161 foreach my $taxline (@raw_taxlines) {
162 my $taxname = $taxline->itemdesc;
163 $taxname{$taxname} ||= [];
164 push @{ $taxname{$taxname} }, $taxline;
168 # values are (cumulative) amounts
171 my $link_table = $self->info->{link_table};
173 # Preconstruct cust_bill_pkg objects that will become the "final"
174 # taxlines for each name, so that we can reference them.
175 # (keys are taxnames)
176 my %real_taxline_named = map {
177 $_ => FS::cust_bill_pkg->new({
186 # For each distinct tax name (the values set as $taxline->itemdesc),
187 # create a consolidated tax item with the total amount and all the links
188 # of all tax items that share that name.
189 foreach my $taxname ( keys %taxname ) {
191 my $tax_cust_bill_pkg = $real_taxline_named{$taxname};
192 $tax_cust_bill_pkg->set( $link_table => \@tax_links );
195 warn "adding $taxname\n" if $DEBUG > 1;
197 foreach my $taxitem ( @{ $taxname{$taxname} } ) {
198 # then we need to transfer the amount and the links from the
199 # line item to the new one we're creating.
200 $tax_total += $taxitem->setup;
201 foreach my $link ( @{ $taxitem->get($link_table) } ) {
202 $link->set('tax_cust_bill_pkg', $tax_cust_bill_pkg);
204 # if the link represents tax on tax, also fix its taxable pointer
205 # to point to the "final" taxline
206 my $taxable_cust_bill_pkg = $link->get('taxable_cust_bill_pkg');
207 if (my $other_taxname = $taxable_cust_bill_pkg->itemdesc) {
208 $link->set('taxable_cust_bill_pkg',
209 $real_taxline_named{$other_taxname}
213 push @tax_links, $link;
216 next unless $tax_total;
218 # we should really neverround this up...I guess it's okay if taxline
219 # already returns amounts with 2 decimal places
220 $tax_total = sprintf('%.2f', $tax_total );
221 $tax_cust_bill_pkg->set('setup', $tax_total);
223 my $pkg_category = qsearchs( 'pkg_category', { 'categoryname' => $taxname,
229 if ( $pkg_category and
230 $conf->config('invoice_latexsummary') ||
231 $conf->config('invoice_htmlsummary')
234 my %hash = ( 'section' => $pkg_category->categoryname );
235 push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
237 $tax_cust_bill_pkg->set('display', \@display);
239 push @tax_line_items, $tax_cust_bill_pkg;
247 =item cust_tax_locations LOCATION
249 Given an L<FS::cust_location> object (or a hash of location fields),
250 returns a list of all tax jurisdiction locations that could possibly
251 match it. This is meant for interactive use: the location editing UI
252 displays the candidate locations to the user so they can choose the
257 sub cust_tax_locations {
259 } # shouldn't even get called unless info->{manual_tax_location} is true
261 =item add_taxproduct DESCRIPTION
263 If the module allows manually adding tax products (categories of taxable
264 items/services), this method will be called to do it. (If not, the UI in
265 browse/part_pkg_taxproduct/* should prevent adding an unlisted tax product.
266 That is the default behavior, so by default this method simply fails.)
268 DESCRIPTION is the contents of the taxproduct_description form input, which
269 will normally be filled in by browse/part_pkg_taxproduct/*.
271 Must return the newly inserted part_pkg_taxproduct object on success, or
278 "$class does not allow manually adding taxproducts";
281 =item transfer_batch (batch-style only)
283 Submits the pending transaction batch for processing, receives the
284 results, and appends the calculated taxes to all invoices that were
285 included in the batch. Then clears their pending flags, and queues
286 a job to run C<FS::cust_main::Billing::collect> on each affected