add credited sales column to tax report, #37088
[freeside.git] / FS / FS / Report / Tax.pm
1 package FS::Report::Tax;
2
3 use strict;
4 use vars qw($DEBUG);
5 use FS::Record qw(dbh qsearch qsearchs group_concat_sql);
6 use Date::Format qw( time2str );
7
8 use Data::Dumper;
9
10 $DEBUG = 0;
11
12 =item report_internal OPTIONS
13
14 Constructor.  Generates a tax report using the internal tax rate system 
15 (L<FS::cust_main_county>).
16
17 Required parameters:
18
19 - beginning, ending: the date range as Unix timestamps.
20 - taxname: the name of the tax (corresponds to C<cust_bill_pkg.itemdesc>).
21 - country: the country code.
22
23 Optional parameters:
24 - agentnum: limit to this agentnum.num.
25 - breakdown: hashref of the fields to group by.  Keys can be 'city', 'district',
26   'pkgclass', or 'taxclass'; values should be true.
27 - debug: sets the debug level.  1 will warn the data collected for the report;
28   2 will also warn all of the SQL statements.
29
30 =cut
31
32 sub report_internal {
33   my $class = shift;
34   my %opt = @_;
35
36   $DEBUG ||= $opt{debug};
37
38   my $conf = new FS::Conf;
39
40   my($beginning, $ending) = @opt{'beginning', 'ending'};
41
42   my ($taxname, $country, %breakdown);
43
44   # taxname can contain arbitrary punctuation; escape it properly and 
45   # include $taxname unquoted elsewhere
46   $taxname = dbh->quote($opt{'taxname'});
47
48   if ( $opt{country} =~ /^(\w\w)$/ ) {
49     $country = $1;
50   } else {
51     die "country required";
52   }
53
54   # %breakdown: short name => field identifier
55   # null classnum should remain null, not be converted to zero
56   %breakdown = (
57     'taxclass'  => 'cust_main_county.taxclass',
58     'pkgclass'  => 'COALESCE(part_fee.classnum,part_pkg.classnum)',
59     'city'      => 'cust_main_county.city',
60     'district'  => 'cust_main_county.district',
61     'state'     => 'cust_main_county.state',
62     'county'    => 'cust_main_county.county',
63   );
64   foreach (qw(taxclass pkgclass city district)) {
65     delete $breakdown{$_} unless $opt{breakdown}->{$_};
66   }
67
68   my $join_cust =     '      JOIN cust_bill     USING ( invnum  )
69                         LEFT JOIN cust_main     USING ( custnum ) ';
70
71   my $join_cust_pkg = $join_cust.
72                       ' LEFT JOIN cust_pkg      USING ( pkgnum  )
73                         LEFT JOIN part_pkg      USING ( pkgpart )
74                         LEFT JOIN part_fee      USING ( feepart ) ';
75
76   my $from_join_cust_pkg = " FROM cust_bill_pkg $join_cust_pkg "; 
77
78   # all queries MUST be linked to both cust_bill and cust_main_county
79
80   # Either or both of these can be used to link cust_bill_pkg to 
81   # cust_main_county. This one links a taxed line item (billpkgnum) to a tax rate
82   # (taxnum), and gives the amount of tax charged on that line item under that
83   # rate (as tax_amount).
84   my $pkg_tax = "SELECT SUM(amount) as tax_amount, taxnum, ".
85     "taxable_billpkgnum AS billpkgnum ".
86     "FROM cust_bill_pkg_tax_location JOIN cust_bill_pkg USING (billpkgnum) ".
87     "GROUP BY taxable_billpkgnum, taxnum";
88
89   # This one links a tax-exempted line item (billpkgnum) to a tax rate
90   # (taxnum), and gives the amount of the tax exemption.  EXEMPT_WHERE must 
91   # be replaced with an expression to further limit the tax exemptions
92   # that will be included, or "TRUE" to not limit them.
93   #
94   # Note that tax exemptions with non-null creditbillpkgnum are always
95   # excluded. Those are "negative exemptions" created by crediting a sale 
96   # that had received an exemption.
97   my $pkg_tax_exempt = "SELECT SUM(amount) AS exempt_charged, billpkgnum, taxnum ".
98     "FROM cust_tax_exempt_pkg WHERE
99       ( EXEMPT_WHERE )
100       AND cust_tax_exempt_pkg.creditbillpkgnum IS NULL
101      GROUP BY billpkgnum, taxnum";
102
103   my $where = "WHERE cust_bill._date >= $beginning AND cust_bill._date <= $ending ".
104               "AND COALESCE(cust_main_county.taxname,'Tax') = $taxname ".
105               "AND cust_main_county.country = '$country'";
106   # SELECT/GROUP clauses for first-level queries
107   my $select = "SELECT ";
108   my $group = "GROUP BY ";
109   foreach (qw(pkgclass taxclass state county city district)) {
110     if ( $breakdown{$_} ) {
111       $select .= "$breakdown{$_} AS $_, ";
112       $group  .= "$breakdown{$_}, ";
113     } else {
114       $select .= "NULL AS $_, ";
115     }
116   }
117   $select .= group_concat_sql('DISTINCT(cust_main_county.taxnum)', ',') .
118              ' AS taxnums, ';
119   $group =~ s/, $//;
120
121   # SELECT/GROUP clauses for second-level (totals) queries
122   # breakdown by package class only, if anything
123   my $select_all = "SELECT NULL AS pkgclass, ";
124   my $group_all = "";
125   if ( $breakdown{pkgclass} ) {
126     $select_all = "SELECT $breakdown{pkgclass} AS pkgclass, ";
127     $group_all = "GROUP BY $breakdown{pkgclass}";
128   }
129   $select_all .= group_concat_sql('DISTINCT(cust_main_county.taxnum)', ',') .
130                  ' AS taxnums, ';
131
132   my $agentnum;
133   if ( $opt{agentnum} and $opt{agentnum} =~ /^(\d+)$/ ) {
134     $agentnum = $1;
135     my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
136     die "agent not found" unless $agent;
137     $where .= " AND cust_main.agentnum = $agentnum";
138   }
139
140   my $nottax = 
141     '(cust_bill_pkg.pkgnum != 0 OR cust_bill_pkg.feepart IS NOT NULL)';
142
143   # one query for each column of the report
144   # plus separate queries for the totals row
145   my (%sql, %all_sql);
146
147   # SALES QUERIES (taxable sales, all types of exempt sales)
148   # -------------
149
150   # general form
151   my $exempt = "$select SUM(exempt_charged)
152     FROM cust_main_county
153     JOIN ($pkg_tax_exempt) AS pkg_tax_exempt
154     USING (taxnum)
155     JOIN cust_bill_pkg USING (billpkgnum)
156     $join_cust_pkg $where AND $nottax
157     $group";
158
159   my $all_exempt = "$select_all SUM(exempt_charged)
160     FROM cust_main_county
161     JOIN ($pkg_tax_exempt) AS pkg_tax_exempt
162     USING (taxnum)
163     JOIN cust_bill_pkg USING (billpkgnum)
164     $join_cust_pkg $where AND $nottax
165     $group_all";
166
167   # sales to tax-exempt customers
168   $sql{exempt_cust} = $exempt;
169   $sql{exempt_cust} =~ s/EXEMPT_WHERE/exempt_cust = 'Y' OR exempt_cust_taxname = 'Y'/;
170   $all_sql{exempt_cust} = $all_exempt;
171   $all_sql{exempt_cust} =~ s/EXEMPT_WHERE/exempt_cust = 'Y' OR exempt_cust_taxname = 'Y'/;
172
173   # sales of tax-exempt packages
174   $sql{exempt_pkg} = $exempt;
175   $sql{exempt_pkg} =~ s/EXEMPT_WHERE/exempt_setup = 'Y' OR exempt_recur = 'Y'/;
176   $all_sql{exempt_pkg} = $all_exempt;
177   $all_sql{exempt_pkg} =~ s/EXEMPT_WHERE/exempt_setup = 'Y' OR exempt_recur = 'Y'/;
178
179   # monthly per-customer exemptions
180   $sql{exempt_monthly} = $exempt;
181   $sql{exempt_monthly} =~ s/EXEMPT_WHERE/exempt_monthly = 'Y'/;
182   $all_sql{exempt_monthly} = $all_exempt;
183   $all_sql{exempt_monthly} =~ s/EXEMPT_WHERE/exempt_monthly = 'Y'/;
184
185   # credits applied to taxable sales
186   # Note that negative exemptions (from exempt sales being credited) are NOT
187   # counted when calculating the exempt amount. (See above.) Therefore we need
188   # to NOT include any credits against exempt sales in this amount, either.
189   # These two subqueries implement that. They have joins to cust_credit_bill
190   # and cust_bill so that credits can be filtered by application date if
191   # requested.
192
193   # Each row here is the sum of credits applied to a line item.
194   my $sales_credit =
195     "SELECT billpkgnum, SUM(cust_credit_bill_pkg.amount) AS credited
196     FROM cust_credit_bill_pkg
197     JOIN cust_credit_bill USING (creditbillnum)
198     JOIN cust_bill USING (invnum)
199     WHERE cust_bill._date >= $beginning AND cust_bill._date <= $ending
200     GROUP BY billpkgnum
201     ";
202
203   # Each row here is the sum of negative exemptions applied to a combination
204   # of line item and tax definition.
205   my $exempt_credit =
206     "SELECT cust_credit_bill_pkg.billpkgnum, taxnum,
207       0 - SUM(cust_tax_exempt_pkg.amount) AS exempt_credited
208     FROM cust_credit_bill_pkg
209     LEFT JOIN cust_tax_exempt_pkg USING (creditbillpkgnum)
210     JOIN cust_credit_bill USING (creditbillnum)
211     JOIN cust_bill USING (invnum)
212     WHERE cust_bill._date >= $beginning AND cust_bill._date <= $ending
213     GROUP BY cust_credit_bill_pkg.billpkgnum, taxnum
214     ";
215   
216   if ( $opt{credit_date} eq 'cust_credit_bill' ) {
217     $sales_credit =~ s/cust_bill._date/cust_credit_bill._date/g;
218     $exempt_credit =~ s/cust_bill._date/cust_credit_bill._date/g;
219   }
220
221   $sql{sales_credited} = "$select
222     SUM(COALESCE(credited, 0) - COALESCE(exempt_credited, 0))
223     FROM cust_main_county
224     JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
225     JOIN cust_bill_pkg USING (billpkgnum)
226     LEFT JOIN ($sales_credit) AS sales_credit USING (billpkgnum)
227     LEFT JOIN ($exempt_credit) AS exempt_credit USING (billpkgnum, taxnum)
228     $join_cust_pkg $where AND $nottax
229     $group
230     ";
231
232   $all_sql{sales_credited} = "$select_all
233     SUM(COALESCE(credited, 0) - COALESCE(exempt_credited, 0))
234     FROM cust_main_county
235     JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
236     JOIN cust_bill_pkg USING (billpkgnum)
237     LEFT JOIN ($sales_credit) AS sales_credit USING (billpkgnum)
238     LEFT JOIN ($exempt_credit) AS exempt_credit USING (billpkgnum, taxnum)
239     $join_cust_pkg $where AND $nottax
240     $group
241     ";
242
243   # taxable sales
244   $sql{taxable} = "$select
245     SUM(cust_bill_pkg.setup + cust_bill_pkg.recur
246       - COALESCE(exempt_charged, 0)
247       - COALESCE(credited, 0)
248       + COALESCE(exempt_credited, 0)
249     )
250     FROM cust_main_county
251     JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
252     JOIN cust_bill_pkg USING (billpkgnum)
253     LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt USING (billpkgnum, taxnum)
254     LEFT JOIN ($sales_credit) AS sales_credit USING (billpkgnum)
255     LEFT JOIN ($exempt_credit) AS exempt_credit USING (billpkgnum, taxnum)
256     $join_cust_pkg $where AND $nottax 
257     $group";
258
259   $all_sql{taxable} = "$select_all
260     SUM(cust_bill_pkg.setup + cust_bill_pkg.recur
261       - COALESCE(exempt_charged, 0)
262       - COALESCE(credited, 0)
263       + COALESCE(exempt_credited, 0)
264     )
265     FROM cust_main_county
266     JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
267     JOIN cust_bill_pkg USING (billpkgnum)
268     LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt USING (billpkgnum, taxnum)
269     LEFT JOIN ($sales_credit) AS sales_credit USING (billpkgnum)
270     LEFT JOIN ($exempt_credit) AS exempt_credit USING (billpkgnum, taxnum)
271     $join_cust_pkg $where AND $nottax 
272     $group_all";
273
274   $sql{taxable} =~ s/EXEMPT_WHERE/TRUE/; # unrestricted
275   $all_sql{taxable} =~ s/EXEMPT_WHERE/TRUE/;
276
277   # estimated tax (taxable * rate)
278   $sql{estimated} = "$select
279     SUM(cust_main_county.tax / 100 * 
280       (cust_bill_pkg.setup + cust_bill_pkg.recur
281       - COALESCE(exempt_charged, 0)
282       - COALESCE(credited, 0)
283       + COALESCE(exempt_credited, 0)
284       )
285     )
286     FROM cust_main_county
287     JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
288     JOIN cust_bill_pkg USING (billpkgnum)
289     LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt USING (billpkgnum, taxnum)
290     LEFT JOIN ($sales_credit) AS sales_credit USING (billpkgnum)
291     LEFT JOIN ($exempt_credit) AS exempt_credit USING (billpkgnum, taxnum)
292     $join_cust_pkg $where AND $nottax 
293     $group";
294
295   $all_sql{estimated} = "$select_all
296     SUM(cust_main_county.tax / 100 * 
297       (cust_bill_pkg.setup + cust_bill_pkg.recur
298       - COALESCE(exempt_charged, 0)
299       - COALESCE(credited, 0)
300       + COALESCE(exempt_credited, 0)
301       )
302     )
303     FROM cust_main_county
304     JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
305     JOIN cust_bill_pkg USING (billpkgnum)
306     LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt USING (billpkgnum, taxnum)
307     LEFT JOIN ($sales_credit) AS sales_credit USING (billpkgnum)
308     LEFT JOIN ($exempt_credit) AS exempt_credit USING (billpkgnum, taxnum)
309     $join_cust_pkg $where AND $nottax 
310     $group_all";
311
312   $sql{estimated} =~ s/EXEMPT_WHERE/TRUE/; # unrestricted
313   $all_sql{estimated} =~ s/EXEMPT_WHERE/TRUE/;
314
315   # there isn't one for 'sales', because we calculate sales by adding up 
316   # the taxable and exempt columns.
317   
318   # TAX QUERIES (billed tax, credited tax, collected tax)
319   # -----------
320
321   # sum of billed tax:
322   # join cust_bill_pkg to cust_main_county via cust_bill_pkg_tax_location
323   my $taxfrom = " FROM cust_bill_pkg 
324                   $join_cust 
325                   LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
326                   LEFT JOIN cust_main_county USING ( taxnum )";
327
328   if ( $breakdown{pkgclass} ) {
329     # If we're not grouping by package class, this is unnecessary, and
330     # probably really expensive.
331     # Remember that fees also have package classes.
332     $taxfrom .= "
333                   LEFT JOIN cust_bill_pkg AS taxable
334                     ON (cust_bill_pkg_tax_location.taxable_billpkgnum = taxable.billpkgnum)
335                   LEFT JOIN cust_pkg ON (taxable.pkgnum = cust_pkg.pkgnum)
336                   LEFT JOIN part_pkg USING (pkgpart)
337                   LEFT JOIN part_fee ON (taxable.feepart = part_fee.feepart) ";
338   }
339
340   my $istax = "cust_bill_pkg.pkgnum = 0 and cust_bill_pkg.feepart is null";
341
342   $sql{tax} = "$select SUM(cust_bill_pkg_tax_location.amount)
343                $taxfrom
344                $where AND $istax
345                $group";
346
347   $all_sql{tax} = "$select_all SUM(cust_bill_pkg_tax_location.amount)
348                $taxfrom
349                $where AND $istax
350                $group_all";
351
352   # sum of credits applied against billed tax
353   # ($creditfrom includes join of taxable item to part_pkg/part_fee if 
354   # with_pkgclass is on)
355   my $creditfrom = $taxfrom .
356     ' JOIN cust_credit_bill_pkg USING (billpkgtaxlocationnum)' .
357     ' JOIN cust_credit_bill     USING (creditbillnum)';
358   my $creditwhere = $where . 
359     ' AND billpkgtaxratelocationnum IS NULL';
360
361   # if the credit_date option is set to application date, change
362   # $creditwhere accordingly
363   if ( $opt{credit_date} eq 'cust_credit_bill' ) {
364     $creditwhere     =~ s/cust_bill._date/cust_credit_bill._date/g;
365   }
366
367   $sql{tax_credited} = "$select SUM(cust_credit_bill_pkg.amount)
368                   $creditfrom
369                   $creditwhere AND $istax
370                   $group";
371
372   $all_sql{tax_credited} = "$select_all SUM(cust_credit_bill_pkg.amount)
373                   $creditfrom
374                   $creditwhere AND $istax
375                   $group_all";
376
377   # sum of tax paid
378   # this suffers from the same ambiguity as anything else that applies 
379   # received payments to specific packages, but in reality the discrepancy
380   # should be minimal since people either pay their bill or don't.
381   # the join is on billpkgtaxlocationnum to avoid cross-producting.
382  
383   my $paidfrom = $taxfrom .
384     ' JOIN cust_bill_pay_pkg'.
385     ' ON (cust_bill_pay_pkg.billpkgtaxlocationnum ='.
386     ' cust_bill_pkg_tax_location.billpkgtaxlocationnum)';
387
388   $sql{tax_paid} = "$select SUM(cust_bill_pay_pkg.amount)
389                     $paidfrom
390                     $where AND $istax
391                     $group";
392
393   $all_sql{tax_paid} = "$select_all SUM(cust_bill_pay_pkg.amount)
394                     $paidfrom
395                     $where AND $istax
396                     $group_all";
397
398   my %data;
399   my %total;
400   # note that we use keys(%sql) here and keys(%all_sql) later. nothing
401   # obligates us to use the same set of variables for the total query 
402   # as for the individual category queries
403   foreach my $k (keys(%sql)) {
404     my $stmt = $sql{$k};
405     warn "\n".uc($k).":\n".$stmt."\n" if $DEBUG > 1;
406     my $sth = dbh->prepare($stmt);
407     # eight columns: pkgclass, taxclass, state, county, city, district
408     # taxnums (comma separated), value
409     $sth->execute 
410       or die "failed to execute $k query: ".$sth->errstr;
411     while ( my $row = $sth->fetchrow_arrayref ) {
412       my $bin = $data
413                 {$row->[0]} # pkgclass
414                 {$row->[1]  # taxclass
415                   || ($breakdown{taxclass} ? 'Unclassified' : '')}
416                 {$row->[2]} # state
417                 {$row->[3] ? $row->[3] . ' County' : ''} # county
418                 {$row->[4]} # city
419                 {$row->[5]} # district
420               ||= [];
421       push @$bin, [ $k, $row->[6], $row->[7] ];
422     }
423   }
424   warn "DATA:\n".Dumper(\%data) if $DEBUG;
425
426   foreach my $k (keys %all_sql) {
427     warn "\nTOTAL ".uc($k).":\n".$all_sql{$k}."\n" if $DEBUG;
428     my $sth = dbh->prepare($all_sql{$k});
429     # three columns: pkgclass, taxnums (comma separated), value
430     $sth->execute 
431       or die "failed to execute $k totals query: ".$sth->errstr;
432     while ( my $row = $sth->fetchrow_arrayref ) {
433       my $bin = $total{$row->[0]} ||= [];
434       push @$bin, [ $k, $row->[1], $row->[2] ];
435     }
436   }
437   warn "TOTALS:\n".Dumper(\%total) if $DEBUG > 1;
438
439   # $data{$pkgclass}{$taxclass}{$state}{$county}{$city}{$district} = [
440   #   [ 'taxable',     taxnums, amount ],
441   #   [ 'exempt_cust', taxnums, amount ],
442   #   ...
443   # ]
444   # non-requested grouping levels simply collapse into key = ''
445
446   # the much-maligned "out of taxable region"...
447   # find sales that are not linked to any tax with this name
448   # but are still inside the date range/agent criteria.
449   #
450   # This doesn't use $select_all/$group_all because we want a single number,
451   # not a breakdown by pkgclass. Unless someone needs that eventually, 
452   # in which case we'll turn it into an %all_sql query.
453   
454   my $outside_where =
455     "WHERE cust_bill._date >= $beginning AND cust_bill._date <= $ending";
456   if ( $agentnum ) {
457     $outside_where .= " AND cust_main.agentnum = $agentnum";
458   }
459   my $sql_outside = "SELECT SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)
460     FROM cust_bill_pkg
461     $join_cust_pkg
462     $outside_where
463     AND $nottax
464     AND NOT EXISTS(
465       SELECT 1 FROM cust_tax_exempt_pkg
466         JOIN cust_main_county USING (taxnum)
467         WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
468           AND COALESCE(cust_main_county.taxname,'Tax') = $taxname
469           AND cust_tax_exempt_pkg.creditbillpkgnum IS NULL
470     )
471     AND NOT EXISTS(
472       SELECT 1 FROM cust_bill_pkg_tax_location
473         JOIN cust_main_county USING (taxnum)
474         WHERE cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum
475           AND COALESCE(cust_main_county.taxname,'Tax') = $taxname
476     )
477   ";
478   warn "\nOUTSIDE:\n$sql_outside\n" if $DEBUG;
479   my $total_outside = FS::Record->scalar_sql($sql_outside);
480
481   my %taxrates;
482   foreach my $tax (
483     qsearch('cust_main_county', {
484               country => $country,
485               tax => { op => '>', value => 0 }
486             }) )
487     {
488     $taxrates{$tax->taxnum} = $tax->tax;
489   }
490
491   # return the data
492   bless {
493     'opt'       => \%opt,
494     'data'      => \%data,
495     'total'     => \%total,
496     'taxrates'  => \%taxrates,
497     'outside'   => $total_outside,
498   }, $class;
499 }
500
501 sub opt {
502   my $self = shift;
503   $self->{opt};
504 }
505
506 sub data {
507   my $self = shift;
508   $self->{data};
509 }
510
511 # sub fetchall_array...
512
513 sub table {
514   my $self = shift;
515   my @columns = (qw(pkgclass taxclass state county city district));
516   # taxnums, field headings, and amounts
517   my @rows;
518   my %row_template;
519
520   # de-treeify this thing
521   my $descend;
522   $descend = sub {
523     my ($tree, $level) = @_;
524     if ( ref($tree) eq 'HASH' ) {
525       foreach my $k ( sort {
526            -1*($b eq '')    # sort '' to the end
527           or  ($a eq '')    # sort '' to the end
528           or  ($a <=> $b)   # sort numbers as numbers
529           or  ($a cmp $b)   # sort alphabetics as alphabetics
530         } keys %$tree )
531       {
532         $row_template{ $columns[$level] } = $k;
533         &{ $descend }($tree->{$k}, $level + 1);
534         if ( $level == 0 ) {
535           # then insert the total row for the pkgclass
536           $row_template{'total'} = 1; # flag it as a total
537           &{ $descend }($self->{total}->{$k}, 1);
538           $row_template{'total'} = 0;
539         }
540       }
541     } elsif ( ref($tree) eq 'ARRAY' ) {
542       # then we've reached the bottom; elements of this array are arrayrefs
543       # of [ field, taxnums, amount ].
544       # start with the inherited location-element fields
545       my %this_row = %row_template;
546       my %taxnums;
547       foreach my $x (@$tree) {
548         # accumulate taxnums
549         foreach (split(',', $x->[1])) {
550           $taxnums{$_} = 1;
551         }
552         # and money values
553         $this_row{ $x->[0] } = $x->[2];
554       }
555       # store combined taxnums
556       $this_row{taxnums} = join(',', sort { $a cmp $b } keys %taxnums);
557       # and calculate row totals
558       $this_row{sales} = sprintf('%.2f',
559                           $this_row{taxable} +
560                           $this_row{exempt_cust} +
561                           $this_row{exempt_pkg} + 
562                           $this_row{exempt_monthly}
563                         );
564       # and give it a label
565       if ( $this_row{total} ) {
566         $this_row{label} = 'Total';
567       } else {
568         $this_row{label} = join(', ', grep $_,
569                             $this_row{taxclass},
570                             $this_row{state},
571                             $this_row{county}, # already has ' County' suffix
572                             $this_row{city},
573                             $this_row{district}
574                            );
575       }
576       # and indicate the tax rate, if any
577       my $rate;
578       foreach (keys %taxnums) {
579         $rate ||= $self->{taxrates}->{$_};
580         if ( $rate != $self->{taxrates}->{$_} ) {
581           $rate = 'variable';
582           last;
583         }
584       }
585       if ( $rate eq 'variable' ) {
586         $this_row{rate} = 'variable';
587       } elsif ( $rate > 0 ) {
588         $this_row{rate} = sprintf('%.2f', $rate);
589       }
590       push @rows, \%this_row;
591     }
592   };
593
594   &{ $descend }($self->{data}, 0);
595
596   warn "TABLE:\n".Dumper(\@rows) if $self->{opt}->{debug};
597   return @rows;
598 }
599
600 sub taxrates {
601   my $self = shift;
602   $self->{taxrates}
603 }
604
605 sub title {
606   my $self = shift;
607   my $string = '';
608   if ( $self->{opt}->{agentnum} ) {
609     my $agent = qsearchs('agent', { agentnum => $self->{opt}->{agentnum} });
610     $string .= $agent->agent . ' ';
611   }
612   $string .= 'Tax Report: '; # XXX localization
613   if ( $self->{opt}->{beginning} ) {
614     $string .= time2str('%h %o %Y ', $self->{opt}->{beginning});
615   }
616   $string .= 'through ';
617   if ( $self->{opt}->{ending} and $self->{opt}->{ending} < 4294967295 ) {
618     $string .= time2str('%h %o %Y', $self->{opt}->{ending});
619   } else {
620     $string .= 'now';
621   }
622   $string .= ' - ' . $self->{opt}->{taxname};
623   return $string;
624 }
625
626 1;