1 <% include("/elements/header.html", "$agentname Tax Report - ".
3 ? time2str('%h %o %Y ', $beginning )
7 ( $ending == 4294967295
9 : time2str('%h %o %Y', $ending )
14 Download full results<BR>
15 as <A HREF="<% $p.'search/report_tax-xls.cgi?'.$cgi->query_string%>">Excel spreadsheet</A>
18 <STYLE type="text/css">
20 background-color: #777777;
26 <% include('/elements/table-grid.html') %>
28 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3></TH>
29 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
30 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3></TH>
31 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3>Rate</TH>
32 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3></TH>
33 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3>Tax owed</TH>
34 % unless ( $cgi->param('show_taxclasses') ) {
35 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3>Tax invoiced</TH>
36 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3></TH>
37 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3>Tax credited</TH>
38 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3></TH>
39 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=3>Tax collected</TH>
44 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Total</TH>
45 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
46 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=1>Non-taxable</TH>
47 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
48 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=1>Non-taxable</TH>
49 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
50 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=1>Non-taxable</TH>
51 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
52 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Taxable</TH>
56 <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
57 <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
58 <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>(monthly exemption)</FONT></TH>
61 % foreach my $class (@pkgclasses ) {
62 % next if @{ $class->{regions} } == 0;
63 % if ( $class->{classname} ) {
65 <TD COLSPAN=19 CLASS="sectionhead"><% $class->{classname} %></TD>
69 % my $bgcolor1 = '#eeeeee';
70 % my $bgcolor2 = '#ffffff';
73 % my @regions = @{ $class->{regions} };
74 % foreach my $region ( @regions ) {
77 % if ( $with_pkgclass and length($class->{classnum}) ) {
78 % $link = ';classnum='.$class->{classnum};
79 % } # else we're not breaking down pkg class, or this is the grand total
81 % if ( $region->{'label'} eq $out ) {
83 % } elsif ( $region->{'taxnums'} ) {
84 % # might be nicer to specify this as country:state:city
85 % $link .= ';'.join(';', map { "taxnum=$_" } @{ $region->{'taxnums'} });
88 % if ( $bgcolor eq $bgcolor1 ) {
89 % $bgcolor = $bgcolor2;
91 % $bgcolor = $bgcolor1;
94 % my $hicolor = $bgcolor;
95 % unless ( $cgi->param('show_taxclasses') ) {
96 % my $diff = abs( sprintf( '%.2f', $region->{'owed'} )
97 % - sprintf( '%.2f', $region->{'tax'} )
99 % if ( $diff > 0.02 ) {
100 % $hicolor = $hicolor eq '#eeeeee' ? '#eeee99' : '#ffffcc';
105 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
106 % my $tdh = qq(TD CLASS="grid" BGCOLOR="$hicolor");
107 % my $bigmath = '<FONT FACE="sans-serif" SIZE="+1"><B>';
108 % my $bme = '</B></FONT>';
110 % if ( $region->{'is_total'} ) {
111 <TR STYLE="font-style: italic">
112 <TD STYLE="text-align: right; padding-right: 1ex; background-color:<%$bgcolor%>">Total</TD>
115 <<%$td%>><% $region->{'label'} %></TD>
117 <<%$td%> ALIGN="right">
118 <A HREF="<% $baselink. $link %>;nottax=1"
119 ><% &$money_sprintf( $region->{'sales'} ) %></A>
121 % if ( $region->{'label'} eq $out ) {
122 <<%$td%> COLSPAN=12></TD>
124 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
125 <<%$td%> ALIGN="right">
126 <A HREF="<% $baselink. $link %>;nottax=1;exempt_cust=Y"
127 ><% &$money_sprintf( $region->{'exempt_cust'} ) %></A>
129 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
130 <<%$td%> ALIGN="right">
131 <A HREF="<% $baselink. $link %>;nottax=1;exempt_pkg=Y"
132 ><% &$money_sprintf( $region->{'exempt_pkg'} ) %></A>
134 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
135 <<%$td%> ALIGN="right">
136 <A HREF="<% $exemptlink. $link %>"
137 ><% &$money_sprintf( $region->{'exempt_monthly'} ) %></A>
139 <<%$td%>><FONT SIZE="+1"><B> = </B></FONT></TD>
140 <<%$td%> ALIGN="right">
141 <A HREF="<% $baselink. $link %>;nottax=1;taxable=1"
142 ><% &$money_sprintf( $region->{'taxable'} ) %></A>
144 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath X $bme" %></TD>
145 <<%$td%> ALIGN="right"><% $region->{'rate'} %></TD>
146 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath = $bme" %></TD>
147 <<%$tdh%> ALIGN="right">
148 <% &$money_sprintf( $region->{'owed'} ) %>
151 % unless ( $cgi->param('show_taxclasses') ) {
152 % my $invlink = $region->{'url_param_inv'}
153 % ? ';'. $region->{'url_param_inv'}
156 % if ( $region->{'label'} eq $out ) {
157 <<%$td%> ALIGN="right">
158 <A HREF="<% $baselink. $invlink %>;istax=1"
159 ><% &$money_sprintf_nonzero( $region->{'tax'} ) %></A>
162 <<%$td%> ALIGN="right">
163 <A HREF="<% $creditlink. $invlink %>;istax=1"
164 ><% &$money_sprintf_nonzero( $region->{'credit'} ) %></A>
166 <<%$td%> COLSPAN=2></TD>
168 <<%$tdh%> ALIGN="right">
169 <A HREF="<% $baselink. $invlink %>;istax=1"
170 ><% &$money_sprintf( $region->{'tax'} ) %></A>
172 <<%$tdh%>><FONT SIZE="+1"><B> - </B></FONT></TD>
173 <<%$tdh%> ALIGN="right">
174 <A HREF="<% $creditlink. $invlink %>;istax=1"
175 ><% &$money_sprintf( $region->{'credit'} ) %></A>
177 <<%$tdh%>><FONT SIZE="+1"><B> = </B></FONT></TD>
178 <<%$tdh%> ALIGN="right">
179 <% &$money_sprintf( $region->{'tax'} - $region->{'credit'} ) %>
182 % } # show_taxclasses
185 % } # foreach $region
191 % if ( $cgi->param('show_taxclasses') ) {
194 <% include('/elements/table-grid.html') %>
196 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
197 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
198 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
199 <TH CLASS="grid" BGCOLOR="#cccccc">Tax credited</TH>
200 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
201 <TH CLASS="grid" BGCOLOR="#cccccc">Tax collected</TH>
204 % #some false laziness w/above
205 % foreach my $class (@pkgclasses) {
206 % if ( $class->{classname} ) {
208 <TD COLSPAN=6 CLASS="sectionhead"><% $class->{classname} %></TD>
212 % my $bgcolor1 = '#eeeeee';
213 % my $bgcolor2 = '#ffffff';
216 % foreach my $region ( @{ $class->{base_regions} } ) {
219 % if ( $with_pkgclass and length($class->{classnum}) ) {
220 % $link = ';classnum='.$class->{classnum};
223 % if ( $region->{'label'} eq $out ) {
226 % $link .= ';'. $region->{'url_param'}
227 % if $region->{'url_param'};
230 % if ( $bgcolor eq $bgcolor1 ) {
231 % $bgcolor = $bgcolor2;
233 % $bgcolor = $bgcolor1;
235 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
236 % my $tdh = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
239 % my $invlink = $region->{'url_param_inv'}
240 % ? ';'. $region->{'url_param_inv'}
244 <<%$td%>><% $region->{'label'} %></TD>
245 % if ( $region->{'label'} eq $out ) {
246 <<%$td%> ALIGN="right">
247 <A HREF="<% $baselink. $invlink %>;istax=1"
248 ><% &$money_sprintf_nonzero( $region->{'tax'} ) %></A>
251 <<%$td%> ALIGN="right">
252 <A HREF="<% $creditlink. $invlink %>;istax=1"
253 ><% &$money_sprintf_nonzero( $region->{'credit'} ) %></A>
255 <<%$td%> COLSPAN=2></TD>
257 <<%$td%> ALIGN="right">
258 <A HREF="<% $baselink. $link %>;istax=1"
259 ><% &$money_sprintf( $region->{'tax'} ) %></A>
261 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
262 <<%$tdh%> ALIGN="right">
263 <A HREF="<% $creditlink. $invlink %>;istax=1"
264 ><% &$money_sprintf( $region->{'credit'} ) %></A>
266 <<%$td%>><FONT SIZE="+1"><B> = </B></FONT></TD>
267 <<%$tdh%> ALIGN="right">
268 <% &$money_sprintf( $region->{'tax'} - $region->{'credit'} ) %>
277 % } # if show_taxclasses
279 <% include('/elements/footer.html') %>
284 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
286 my $DEBUG = $cgi->param('debug') || 0;
288 my $conf = new FS::Conf;
290 my $out = 'Out of taxable region(s)';
292 my %label_opt = ( out => 1 ); #enable 'Out of Taxable Region' label
293 $label_opt{with_city} = 1 if $cgi->param('show_cities');
294 $label_opt{with_district} = 1 if $cgi->param('show_districts');
296 $label_opt{with_taxclass} = 1 if $cgi->param('show_taxclasses');
298 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
300 my $join_cust = ' JOIN cust_bill USING ( invnum )
301 LEFT JOIN cust_main USING ( custnum ) ';
303 my $join_cust_pkg = $join_cust.
304 ' LEFT JOIN cust_pkg USING ( pkgnum )
305 LEFT JOIN part_pkg USING ( pkgpart ) ';
307 my $from_join_cust_pkg = " FROM cust_bill_pkg $join_cust_pkg ";
309 my $with_pkgclass = $cgi->param('show_pkgclasses');
311 # Either or both of these can be used to link cust_bill_pkg to
312 # cust_main_county. This one links a taxed line item (billpkgnum) to a tax rate
313 # (taxnum), and gives the amount of tax charged on that line item under that
314 # rate (as tax_amount).
315 my $pkg_tax = "SELECT SUM(amount) as tax_amount, taxnum, ".
316 "taxable_billpkgnum AS billpkgnum ".
317 "FROM cust_bill_pkg_tax_location JOIN cust_bill_pkg USING (billpkgnum) ".
318 "GROUP BY taxable_billpkgnum, taxnum";
320 # This one links a tax-exempted line item (billpkgnum) to a tax rate (taxnum),
321 # and gives the amount of the tax exemption. EXEMPT_WHERE should be replaced
322 # with a real WHERE clause to further limit the tax exemptions that will be
324 my $pkg_tax_exempt = "SELECT SUM(amount) AS exempt_charged, billpkgnum, taxnum ".
325 "FROM cust_tax_exempt_pkg EXEMPT_WHERE GROUP BY billpkgnum, taxnum";
327 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
328 # SELECT/GROUP clauses for first-level queries
329 # classnum is a placeholder; they all go in one class in this case.
330 my $select = "SELECT NULL AS classnum, cust_main_county.taxnum, ";
331 my $group = "GROUP BY cust_main_county.taxnum";
332 # SELECT/GROUP clauses for second-level (totals) queries
333 my $select_all = "SELECT NULL AS classnum, ";
336 if ( $with_pkgclass ) {
337 $select = "SELECT COALESCE(part_pkg.classnum,0), cust_main_county.taxnum, ";
338 $group = "GROUP BY part_pkg.classnum, cust_main_county.taxnum";
339 $select_all = "SELECT COALESCE(part_pkg.classnum,0), ";
340 $group_all = "GROUP BY COALESCE(part_pkg.classnum,0)";
344 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
345 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
346 die "agent not found" unless $agent;
347 $agentname = $agent->agent;
348 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
352 '(cust_bill_pkg.pkgnum != 0 OR cust_bill_pkg.feepart IS NOT NULL)';
354 # one query for each column of the report
355 # plus separate queries for the totals row
358 # SALES QUERIES (taxable sales, all types of exempt sales)
362 my $exempt = "$select SUM(exempt_charged)
363 FROM cust_main_county
364 JOIN ($pkg_tax_exempt) AS pkg_tax_exempt
366 JOIN cust_bill_pkg USING (billpkgnum)
367 $join_cust_pkg $where AND $nottax $group";
369 my $all_exempt = "$select_all SUM(exempt_charged)
370 FROM cust_main_county
371 JOIN ($pkg_tax_exempt) AS pkg_tax_exempt
373 JOIN cust_bill_pkg USING (billpkgnum)
374 $join_cust_pkg $where AND $nottax
377 # sales to tax-exempt customers
378 $sql{exempt_cust} = $exempt;
379 $sql{exempt_cust} =~ s/EXEMPT_WHERE/WHERE exempt_cust = 'Y' OR exempt_cust_taxname = 'Y'/;
380 $all_sql{exempt_cust} = $all_exempt;
381 $all_sql{exempt_cust} =~ s/EXEMPT_WHERE/WHERE exempt_cust = 'Y' OR exempt_cust_taxname = 'Y'/;
383 # sales of tax-exempt packages
384 $sql{exempt_pkg} = $exempt;
385 $sql{exempt_pkg} =~ s/EXEMPT_WHERE/WHERE exempt_setup = 'Y' OR exempt_recur = 'Y'/;
386 $all_sql{exempt_pkg} = $all_exempt;
387 $all_sql{exempt_pkg} =~ s/EXEMPT_WHERE/WHERE exempt_setup = 'Y' OR exempt_recur = 'Y'/;
389 # monthly per-customer exemptions
390 $sql{exempt_monthly} = $exempt;
391 $sql{exempt_monthly} =~ s/EXEMPT_WHERE/WHERE exempt_monthly = 'Y'/;
392 $all_sql{exempt_monthly} = $all_exempt;
393 $all_sql{exempt_monthly} =~ s/EXEMPT_WHERE/WHERE exempt_monthly = 'Y'/;
396 $sql{taxable} = "$select
397 SUM(cust_bill_pkg.setup + cust_bill_pkg.recur - COALESCE(exempt_charged, 0))
398 FROM cust_main_county
399 JOIN ($pkg_tax) AS pkg_tax USING (taxnum)
400 JOIN cust_bill_pkg USING (billpkgnum)
401 LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt
402 ON (pkg_tax_exempt.billpkgnum = cust_bill_pkg.billpkgnum
403 AND pkg_tax_exempt.taxnum = cust_main_county.taxnum)
404 $join_cust_pkg $where AND $nottax $group";
406 # Here we're going to sum all line items that are taxable _at all_,
407 # under any tax. exempt_charged is the sum of all exemptions for a
408 # particular billpkgnum + taxnum; we take the taxnum that has the
409 # smallest sum of exemptions and subtract that from the charged amount.
411 # (This isn't an exact result, since line items can be taxable under
412 # one tax and not another. Under 4.x the tax report is designed to
413 # consider only one variety of tax at a time, which should solve this.)
415 $all_sql{taxable} = "$select_all
416 SUM(cust_bill_pkg.setup + cust_bill_pkg.recur - COALESCE(min_exempt, 0))
419 SELECT billpkgnum, MIN(exempt_charged) AS min_exempt
420 FROM ($pkg_tax) AS pkg_tax
421 JOIN cust_bill_pkg USING (billpkgnum)
422 LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt USING (billpkgnum, taxnum)
426 $join_cust_pkg $where AND $nottax $group_all";
428 $sql{taxable} =~ s/EXEMPT_WHERE//; # unrestricted
429 $all_sql{taxable} =~ s/EXEMPT_WHERE//;
431 # there isn't one for 'sales', because we calculate sales by adding up
432 # the taxable and exempt columns.
434 # TAX QUERIES (billed tax, credited tax)
438 # join cust_bill_pkg to cust_main_county via cust_bill_pkg_tax_location
439 my $taxfrom = " FROM cust_bill_pkg
441 LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
442 LEFT JOIN cust_main_county USING ( taxnum )";
444 if ( $with_pkgclass ) {
445 # If we're not grouping by package class, this is unnecessary, and
446 # probably really expensive.
448 LEFT JOIN cust_bill_pkg AS taxable
449 ON (cust_bill_pkg_tax_location.taxable_billpkgnum = taxable.billpkgnum)
450 LEFT JOIN cust_pkg ON (taxable.pkgnum = cust_pkg.pkgnum)
451 LEFT JOIN part_pkg USING (pkgpart)";
454 my $istax = "cust_bill_pkg.pkgnum = 0";
456 "COALESCE(taxname,'Tax') = COALESCE(cust_bill_pkg.itemdesc,'Tax')";
458 $sql{tax} = "$select SUM(cust_bill_pkg_tax_location.amount)
460 $where AND $istax AND $named_tax
463 $all_sql{tax} = "$select_all SUM(cust_bill_pkg.setup)
469 # sum of credits applied against billed tax
470 # ($creditfrom includes join of taxable item to part_pkg if with_pkgclass
472 my $creditfrom = $taxfrom .
473 ' JOIN cust_credit_bill_pkg USING (billpkgtaxlocationnum)';
474 my $creditwhere = $where .
475 ' AND billpkgtaxratelocationnum IS NULL';
477 $sql{credit} = "$select SUM(cust_credit_bill_pkg.amount)
479 $creditwhere AND $istax AND $named_tax
482 $all_sql{credit} = "$select_all SUM(cust_credit_bill_pkg.amount)
483 FROM cust_credit_bill_pkg
484 JOIN cust_bill_pkg USING (billpkgnum)
489 if ( $with_pkgclass ) {
490 # the slightly more complicated version, with lots of joins that are
491 # unnecessary if you're not breaking down by package class
492 $all_sql{tax} = "$select_all SUM(cust_bill_pkg_tax_location.amount)
497 $all_sql{credit} = "$select_all SUM(cust_credit_bill_pkg.amount)
499 $creditwhere AND $istax
503 # "out of taxable region" sales
504 $all_sql{out_sales} =
505 "$select_all SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)
506 FROM (cust_bill_pkg $join_cust_pkg)
507 LEFT JOIN ($pkg_tax) AS pkg_tax USING (billpkgnum)
508 LEFT JOIN ($pkg_tax_exempt) AS pkg_tax_exempt USING (billpkgnum)
510 AND pkg_tax.taxnum IS NULL AND pkg_tax_exempt.taxnum IS NULL
514 $all_sql{out_sales} =~ s/EXEMPT_WHERE//;
518 foreach my $k (keys(%sql)) {
520 warn "\n".uc($k).":\n".$stmt."\n" if $DEBUG;
521 my $sth = dbh->prepare($stmt);
522 # three columns: classnum, taxnum, value
524 or die "failed to execute $k query: ".$sth->errstr;
525 while ( my $row = $sth->fetchrow_arrayref ) {
526 $data{$k}{$row->[0]}{$row->[1]} = $row->[2];
529 warn "DATA:\n".Dumper(\%data) if $DEBUG > 1;
531 foreach my $k (keys %all_sql) {
532 warn "\n".$all_sql{$k}."\n" if $DEBUG;
533 my $sth = dbh->prepare($all_sql{$k});
534 # two columns: classnum, value
536 or die "failed to execute $k totals query: ".$sth->errstr;
537 while ( my $row = $sth->fetchrow_arrayref ) {
538 $total{$k}{$row->[0]} = $row->[1];
541 warn "TOTALS:\n".Dumper(\%total);# if $DEBUG > 1;
542 # so $data{tax}, for example, is now a hash with one entry
543 # for each classnum, containing a hash with one entry for each
544 # taxnum, containing the tax billed on that taxnum.
545 # if with_pkgclass is off, then the classnum is always null.
548 # unlinked tax collected
550 "SELECT SUM(cust_bill_pkg.setup)
551 FROM (cust_bill_pkg $join_cust)
552 LEFT JOIN cust_bill_pkg_tax_location USING (billpkgnum)
553 $where AND $istax AND cust_bill_pkg_tax_location.billpkgnum IS NULL"
555 my $unlinked_tax = FS::Record->scalar_sql($out_tax_sql);
556 # unlinked tax credited
558 "SELECT SUM(cust_credit_bill_pkg.amount)
559 FROM cust_credit_bill_pkg
560 JOIN cust_bill_pkg USING (billpkgnum)
562 $where AND $istax AND cust_credit_bill_pkg.billpkgtaxlocationnum IS NULL"
564 my $unlinked_credit = FS::Record->scalar_sql($out_credit_sql);
567 my $all_sales = FS::Record->scalar_sql(
568 "SELECT SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)
569 FROM cust_bill_pkg $join_cust $where AND $nottax"
572 #tax-report_groups filtering
573 my($group_op, $group_value) = ( '', '' );
574 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ ) {
575 ( $group_op, $group_value ) = ( $1, $2 );
577 my $group_test = sub { # to be applied to a tax label
579 return 1 unless $group_op; #in case we get called inadvertantly
580 if ( $label eq $out ) { #don't display "out of taxable region" in this case
582 } elsif ( $group_op eq '=' ) {
583 $label =~ /^$group_value/;
584 } elsif ( $group_op eq '!=' ) {
585 $label !~ /^$group_value/;
587 die "guru meditation #00de: group_op $group_op\n";
592 if ($with_pkgclass) {
593 @pkgclasses = qsearch('pkg_class', {});
594 push @pkgclasses, FS::pkg_class->new({
596 classname => 'Unclassified',
599 @pkgclasses = ( FS::pkg_class->new({
606 foreach my $class (@pkgclasses) {
607 my $classnum = $class->classnum;
608 my $classname = $class->classname;
610 # if show_taxclasses is on, %base_regions will contain the same data
611 # as %regions, but with taxclasses merged together (and ignoring report_group
613 my (%regions, %base_regions);
615 my @loc_params = qw(country state county);
616 push @loc_params, 'city' if $cgi->param('show_cities');
617 push @loc_params, 'district' if $cgi->param('show_districts');
619 foreach my $r ( qsearch({ 'table' => 'cust_main_county', })) {
620 my $taxnum = $r->taxnum;
621 # set up a %regions entry for this region's tax label
622 my $label = $r->label(%label_opt);
623 next if $label eq $out;
624 $regions{$label} ||= { label => $label };
626 $regions{$label}->{$_} = $r->get($_) foreach @loc_params;
627 $regions{$label}->{taxnums} ||= [];
628 push @{ $regions{$label}->{taxnums} }, $r->taxnum;
630 my %x; # keys are data items (like 'tax', 'exempt_cust', etc.)
631 foreach my $k (keys %data) {
632 next unless exists($data{$k}{$classnum}{$taxnum});
633 $x{$k} = $data{$k}{$classnum}{$taxnum};
634 $regions{$label}{$k} += $x{$k};
635 if ( $k eq 'taxable' or $k =~ /^exempt/ ) {
636 $regions{$label}->{'sales'} += $x{$k};
640 my $owed = $data{'taxable'}{$classnum}{$taxnum} * ($r->tax/100);
641 $regions{$label}->{'owed'} += $owed;
642 $total{'owed'}{$classnum} += $owed;
644 if ( defined($regions{$label}->{'rate'})
645 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
646 $regions{$label}->{'rate'} = 'variable';
648 $regions{$label}->{'rate'} = $r->tax.'%';
651 if ( $cgi->param('show_taxclasses') ) {
652 my $base_label = $r->label(%label_opt, 'with_taxclass' => 0);
653 $base_regions{$base_label} ||=
655 label => $base_label,
659 $base_regions{$base_label}->{tax} += $x{tax};
660 $base_regions{$base_label}->{credit} += $x{credit};
665 my @regions = map { $_->{label} }
667 ($b eq $out) <=> ($a eq $out)
668 or $a->{country} cmp $b->{country}
669 or $a->{state} cmp $b->{state}
670 or $a->{county} cmp $b->{county}
671 or $a->{city} cmp $b->{city}
673 grep { $_->{sales} > 0 or $_->{tax} > 0 or $_->{credit} > 0 }
676 #tax-report_groups filtering
677 @regions = grep &{$group_test}($_), @regions
685 foreach my $label (@regions) {
686 $taxclasses{$regions{$_}->{'taxclass'}} = 1
687 if $regions{$_}->{'taxclass'};
688 $county{$regions{$_}->{'county'}} = 1;
689 $state{$regions{$_}->{'state'}} = 1;
690 $country{$regions{$_}->{'country'}} = 1;
693 my $total_url_param = '';
694 my $total_url_param_invoiced = '';
697 my @country = keys %country;
698 warn "WARNING: multiple countries on this grouped report; total links broken"
699 if scalar(@country) > 1;
700 my $country = $country[0];
702 my @state = keys %state;
703 warn "WARNING: multiple countries on this grouped report; total links broken"
704 if scalar(@state) > 1;
705 my $state = $state[0];
707 $total_url_param_invoiced =
709 'report_group='.uri_escape("$group_op $group_value").';'.
710 join(';', map 'taxclass='.uri_escape($_), keys %taxclasses );
711 $total_url_param .= ';'.
712 "country=$country;state=".uri_escape($state).';'.
713 join(';', map 'county='.uri_escape($_), keys %county ) ;
724 map $base_regions{$_},
728 #add "Out of taxable" and total lines
729 if ( $total{out_sales}{$classnum} ) {
731 'sales' => $total{out_sales}{$classnum},
735 push @regions, \%out;
736 push @base_regions, \%out;
740 my %class_total = map { $_ => $total{$_}{$classnum} } keys(%total);
741 $class_total{is_total} = 1;
742 $class_total{sales} = sum(
743 @class_total{ 'taxable',
745 grep(/^exempt/, keys %class_total) }
748 push @regions, \%class_total;
749 push @base_regions, \%class_total;
752 $pkgclass_data{$classname} = {
753 classnum => $classnum,
754 classname => $classname,
755 regions => \@regions,
756 base_regions => \@base_regions,
760 if ( $with_pkgclass ) {
761 my $class_zero = delete( $pkgclass_data{'Unclassified'} );
762 @pkgclasses = map { $pkgclass_data{$_} }
765 push @pkgclasses, $class_zero;
767 my %grand_total = map {
768 $_ => sum( values(%{ $total{$_} }) )
771 $grand_total{sales} = $all_sales;
775 classname => 'Total',
776 regions => [ \%grand_total ],
777 base_regions => [ \%grand_total ],
780 @pkgclasses = $pkgclass_data{''};
785 my $money_char = $conf->config('money_char') || '$';
786 my $money_sprintf = sub {
787 $money_char. sprintf('%.2f', shift );
789 my $money_sprintf_nonzero = sub {
790 $_[0] == 0 ? '' : &$money_sprintf($_[0])
793 my $dateagentlink = "begin=$beginning;end=$ending";
794 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
795 if length($agentname);
796 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
797 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
798 my $creditlink = $p. "search/cust_bill_pkg.cgi?$dateagentlink;credit=1";