1 <% include("/elements/header.html", "$agentname Tax Report - ".
3 ? time2str('%h %o %Y ', $beginning )
7 ( $ending == 4294967295
9 : time2str('%h %o %Y', $ending )
14 <% include('/elements/table-grid.html') %>
17 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
18 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
19 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
20 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Rate</TH>
21 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
22 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax owed</TH>
23 % unless ( $cgi->param('show_taxclasses') ) {
24 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
29 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
30 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
31 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
32 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
33 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
34 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
35 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
36 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
37 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
40 % my $bgcolor1 = '#eeeeee';
41 % my $bgcolor2 = '#ffffff';
44 % foreach my $region ( @regions ) {
47 % if ( $region->{'label'} eq $out ) {
50 % $link = ';'. $region->{'url_param'}
51 % if $region->{'url_param'};
54 % if ( $bgcolor eq $bgcolor1 ) {
55 % $bgcolor = $bgcolor2;
57 % $bgcolor = $bgcolor1;
61 % my $hicolor = $bgcolor;
62 % unless ( $cgi->param('show_taxclasses') ) {
63 % my $diff = abs( sprintf( '%.2f', $region->{'owed'} )
64 % - sprintf( '%.2f', $region->{'tax'} )
66 % if ( $diff > 0.02 ) {
67 % # $hicolor = $hicolor eq '#eeeeee' ? '#eeee66' : '#ffff99';
68 % #} elsif ( $diff ) {
69 % $hicolor = $hicolor eq '#eeeeee' ? '#eeee99' : '#ffffcc';
74 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
75 % my $tdh = qq(TD CLASS="grid" BGCOLOR="$hicolor");
76 % my $bigmath = '<FONT FACE="sans-serif" SIZE="+1"><B>';
77 % my $bme = '</B></FONT>';
80 <<%$td%>><% $region->{'label'} %></TD>
81 <<%$td%> ALIGN="right">
82 <A HREF="<% $baselink. $link %>;nottax=1"
83 ><% &$money_sprintf( $region->{'total'} ) %></A>
85 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
86 <<%$td%> ALIGN="right">
87 <A HREF="<% $baselink. $link %>;nottax=1;cust_tax=Y"
88 ><% &$money_sprintf( $region->{'exempt_cust'} ) %></A>
90 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
91 <<%$td%> ALIGN="right">
92 <A HREF="<% $baselink. $link %>;nottax=1;pkg_tax=Y"
93 ><% &$money_sprintf( $region->{'exempt_pkg'} ) %></A>
95 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
96 <<%$td%> ALIGN="right">
97 <A HREF="<% $exemptlink. $link %>"
98 ><% &$money_sprintf( $region->{'exempt_monthly'} ) %></A>
100 <<%$td%>><FONT SIZE="+1"><B> = </B></FONT></TD>
101 <<%$td%> ALIGN="right">
102 <A HREF="<% $baselink. $link %>;nottax=1;taxable=1"
103 ><% &$money_sprintf( $region->{'taxable'} ) %></A>
105 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath X $bme" %></TD>
106 <<%$td%> ALIGN="right"><% $region->{'rate'} %></TD>
107 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath = $bme" %></TD>
108 <<%$tdh%> ALIGN="right">
109 <% &$money_sprintf( $region->{'owed'} ) %>
112 % unless ( $cgi->param('show_taxclasses') ) {
113 <<%$tdh%> ALIGN="right">
114 <A HREF="<% $baselink. $link %>;istax=1"
115 ><% &$money_sprintf( $region->{'tax'} ) %></A>
124 % if ( $cgi->param('show_taxclasses') ) {
127 <% include('/elements/table-grid.html') %>
129 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
130 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
133 % #some false laziness w/above
134 % $bgcolor1 = '#eeeeee';
135 % $bgcolor2 = '#ffffff';
137 % foreach my $region ( @base_regions ) {
140 % if ( $region->{'label'} eq $out ) {
143 % $link = ';'. $region->{'url_param'}
144 % if $region->{'url_param'};
147 % if ( $bgcolor eq $bgcolor1 ) {
148 % $bgcolor = $bgcolor2;
150 % $bgcolor = $bgcolor1;
152 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
155 <<%$td%>><% $region->{'label'} %></TD>
156 <<%$td%> ALIGN="right">
157 <A HREF="<% $baselink. $link %>;istax=1"
158 ><% &$money_sprintf( $region->{'tax'} ) %></A>
164 % if ( $bgcolor eq $bgcolor1 ) {
165 % $bgcolor = $bgcolor2;
167 % $bgcolor = $bgcolor1;
169 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
173 <<%$td%> ALIGN="right">
174 <A HREF="<% $baselink %>;istax=1"
175 ><% &$money_sprintf( $tot_tax ) %></A>
183 <% include('/elements/footer.html') %>
188 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
190 my $conf = new FS::Conf;
192 my $user = getotaker;
194 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
196 my $join_cust = ' JOIN cust_bill USING ( invnum )
197 LEFT JOIN cust_main USING ( custnum ) ';
198 my $join_cust_pkg = $join_cust.
199 ' LEFT JOIN cust_pkg USING ( pkgnum )
200 LEFT JOIN part_pkg USING ( pkgpart ) ';
201 $join_cust_pkg .= ' LEFT JOIN cust_location USING ( locationnum )'
202 if $conf->exists('tax-pkg_address');
204 my $from_join_cust_pkg = " FROM cust_bill_pkg $join_cust_pkg ";
206 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
208 my( $location_sql, @base_param ) = FS::cust_pkg->location_sql;
209 $where .= " AND $location_sql ";
212 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
213 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
214 die "agent not found" unless $agent;
215 $agentname = $agent->agent;
216 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
221 my $prefix = @_ ? shift : '';
223 ( $table.${prefix}county = cust_main_county.county
224 OR cust_main_county.county = ''
225 OR cust_main_county.county IS NULL )
226 AND ( $table.${prefix}state = cust_main_county.state
227 OR cust_main_county.state = ''
228 OR cust_main_county.state IS NULL )
229 AND ( $table.${prefix}country = cust_main_county.country )
234 if ( $conf->exists('tax-ship_address') ) {
237 ( cust_main_county.country = cust_main.country
238 OR cust_main_county.country = cust_main.ship_country
244 ( ( ship_last IS NULL OR ship_last = '' )
245 AND ". gotcust('cust_main'). "
248 ( ship_last IS NOT NULL AND ship_last != ''
249 AND ". gotcust('cust_main', 'ship_'). "
256 $gotcust = gotcust('cust_main');
259 if ( $conf->exists('tax-pkg_address') ) {
261 ( cust_pkg.locationnum IS NULL AND $gotcust)
262 OR ( cust_pkg.locationnum IS NOT NULL AND ". gotcust('cust_location'). " )";
264 "WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
265 LEFT JOIN cust_main USING ( custnum )
266 LEFT JOIN cust_location USING ( locationnum )
273 "WHERE 0 < ( SELECT COUNT(*) FROM cust_main WHERE $gotcust LIMIT 1 )";
276 my $out = 'Out of taxable region(s)';
279 foreach my $r ( qsearch({ 'table' => 'cust_main_county',
280 'extra_sql' => $gotcust,
284 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
286 my $label = getlabel($r);
287 $regions{$label}->{'label'} = $label;
289 $regions{$label}->{$_} = $r->$_() for (qw( county state country )); #taxname?
291 $regions{$label}->{'url_param'} =
292 join(';', map "$_=".uri_escape($r->$_()),
293 qw( county state country taxname )
296 my @param = @base_param;
297 my $mywhere = $where;
299 if ( $r->taxclass ) {
301 $mywhere .= " AND taxclass = ? ";
302 push @param, 'taxclass';
303 $regions{$label}->{'url_param'} .= ';taxclass='. uri_escape($r->taxclass);
304 #no, always# if $cgi->param('show_taxclasses');
306 $regions{$label}->{'taxclass'} = $r->taxclass;
310 $regions{$label}->{'url_param'} .= ';taxclassNULL=1'
311 if $cgi->param('show_taxclasses');
313 my $same_sql = $r->sql_taxclass_sameregion;
314 $mywhere .= " AND $same_sql" if $same_sql;
318 my $fromwhere = "$from_join_cust_pkg $mywhere"; # AND payby != 'COMP' ";
320 # my $label = getlabel($r);
321 # $regions{$label}->{'label'} = $label;
323 my $nottax = 'pkgnum != 0';
325 ## calculate total for this region
328 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax";
329 my $t = scalar_sql($r, \@param, $t_sql);
330 $regions{$label}->{'total'} += $t;
332 #if ( $label eq $out ) # && $t ) {
333 # warn "adding $t for ".
334 # join('/', map $r->$_, qw( taxclass county state country ) ). "\n";
335 # #warn $t_sql if $r->state eq 'FL';
338 ## calculate customer-exemption for this region
342 # my($taxable, $x_cust) = (0, 0);
343 # foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
344 # qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
345 # $taxable += scalar_sql($r, \@param,
346 # "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
349 # $x_cust += scalar_sql($r, \@param,
350 # "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
354 my $x_cust = scalar_sql($r, \@param,
355 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
356 $fromwhere AND $nottax AND tax = 'Y' "
359 $regions{$label}->{'exempt_cust'} += $x_cust;
361 ## calculate package-exemption for this region
363 my $x_pkg = scalar_sql($r, \@param,
365 ( CASE WHEN part_pkg.setuptax = 'Y'
366 THEN cust_bill_pkg.setup
371 ( CASE WHEN part_pkg.recurtax = 'Y'
372 THEN cust_bill_pkg.recur
380 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
381 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
383 AND ( tax != 'Y' OR tax IS NULL )
386 $regions{$label}->{'exempt_pkg'} += $x_pkg;
388 ## calculate monthly exemption (texas tax) for this region
390 # count up all the cust_tax_exempt_pkg records associated with
391 # the actual line items.
393 my $x_monthly = scalar_sql($r, \@param,
395 FROM cust_tax_exempt_pkg
396 JOIN cust_bill_pkg USING ( billpkgnum )
400 $regions{$label}->{'exempt_monthly'} += $x_monthly;
402 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
403 $regions{$label}->{'taxable'} += $taxable;
405 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
407 if ( defined($regions{$label}->{'rate'})
408 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
409 $regions{$label}->{'rate'} = 'variable';
411 $regions{$label}->{'rate'} = $r->tax.'%';
416 my $distinct = "country, state, county,
417 CASE WHEN taxname IS NULL THEN '' ELSE taxname END AS taxname";
418 my $taxclass_distinct =
419 #a little bit unsure of this part... test?
420 #ah, it looks like it winds up being irrelevant as ->{'tax'}
421 # from $regions is not displayed when show_taxclasses is on
422 ( $cgi->param('show_taxclasses')
423 ? " CASE WHEN taxclass IS NULL THEN '' ELSE taxclass END "
429 'select' => "DISTINCT $distinct, $taxclass_distinct",
430 'table' => 'cust_main_county',
432 'extra_sql' => $gotcust,
435 my $taxfromwhere = " FROM cust_bill_pkg $join_cust ";
436 my $taxwhere = $where;
437 if ( $conf->exists('tax-pkg_address') ) {
439 $taxfromwhere .= 'LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
440 LEFT JOIN cust_location USING ( locationnum ) ';
443 $taxwhere =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
446 $taxfromwhere .= " $taxwhere "; #AND payby != 'COMP' ";
447 my @taxparam = @base_param;
449 #should i be a cust_main_county method or something
450 #need to pass in $taxfromwhere & @taxparam???
451 my $_taxamount_sub = sub {
454 #match itemdesc if necessary!
457 ? 'AND itemdesc = '. dbh->quote($r->taxname)
458 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
460 my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
461 " $taxfromwhere AND cust_bill_pkg.pkgnum = 0 $named_tax";
463 scalar_sql($r, \@taxparam, $sql );
466 #tax-report_groups filtering
467 my($group_op, $group_value) = ( '', '' );
468 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ ) {
469 ( $group_op, $group_value ) = ( $1, $2 );
471 my $group_test = sub {
473 return 1 unless $group_op; #in case we get called inadvertantly
474 if ( $label eq $out ) { #don't display "out of taxable region" in this case
476 } elsif ( $group_op eq '=' ) {
477 $label =~ /^$group_value/;
478 } elsif ( $group_op eq '!=' ) {
479 $label !~ /^$group_value/;
481 die "guru meditation #00de: group_op $group_op\n";
486 #foreach my $label ( keys %regions ) {
487 foreach my $r ( qsearch(\%qsearch) ) {
489 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
491 my $label = getlabel($r);
493 next unless &{$group_test}($label);
496 #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
497 #my @param = @base_param;
499 my $x = &{$_taxamount_sub}($r);
501 $regions{$label}->{'tax'} += $x;
502 $tot_tax += $x unless $cgi->param('show_taxclasses');
506 my %base_regions = ();
507 if ( $cgi->param('show_taxclasses') ) {
509 $qsearch{'select'} = "DISTINCT $distinct";
510 foreach my $r ( qsearch(\%qsearch) ) {
512 my $x = &{$_taxamount_sub}($r);
514 my $base_label = getlabel($r, 'no_taxclass'=>1 );
515 $base_regions{$base_label}->{'label'} = $base_label;
517 $base_regions{$base_label}->{'url_param'} =
518 join(';', map "$_=". uri_escape($r->$_()),
519 qw( county state country taxname )
522 $base_regions{$base_label}->{'tax'} += $x;
528 my @regions = keys %regions;
530 #tax-report_groups filtering
531 @regions = grep &{$group_test}($_), @regions
535 my( $total, $tot_taxable, $tot_owed ) = ( 0, 0, 0 );
536 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0, 0 );
542 $total += $regions{$_}->{'total'};
543 $tot_taxable += $regions{$_}->{'taxable'};
544 $tot_owed += $regions{$_}->{'owed'};
545 $exempt_cust += $regions{$_}->{'exempt_cust'};
546 $exempt_pkg += $regions{$_}->{'exempt_pkg'};
547 $exempt_monthly += $regions{$_}->{'exempt_monthly'};
548 $taxclasses{$regions{$_}->{'taxclass'}} = 1
549 if $regions{$_}->{'taxclass'};
550 $county{$regions{$_}->{'county'}} = 1;
551 $state{$regions{$_}->{'state'}} = 1;
552 $country{$regions{$_}->{'country'}} = 1;
555 my $total_url_param = '';
558 my @country = keys %country;
559 warn "WARNING: multiple countries on this grouped report; total links broken"
560 if scalar(@country) > 1;
561 my $country = $country[0];
563 my @state = keys %state;
564 warn "WARNING: multiple countries on this grouped report; total links broken"
565 if scalar(@state) > 1;
566 my $state = $state[0];
569 'report_group='.uri_escape("$group_op $group_value").';'.
570 join(';', map 'taxclass='.uri_escape($_), keys %taxclasses ).';'.
571 "country=$country;state=".uri_escape($state).';'.
572 join(';', map 'county='.uri_escape($_), keys %county )
581 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
585 map $base_regions{$_},
586 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
592 'url_param' => $total_url_param,
594 'exempt_cust' => $exempt_cust,
595 'exempt_pkg' => $exempt_pkg,
596 'exempt_monthly' => $exempt_monthly,
597 'taxable' => $tot_taxable,
605 my $money_char = $conf->config('money_char') || '$';
606 my $money_sprintf = sub {
607 $money_char. sprintf('%.2f', shift );
617 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
618 'county' => $r->county,
619 'country' => $r->country,
620 'tax' => { op=>'>', value=>0 },
626 #kludge to avoid "will not stay shared" warning
627 my $out = 'Out of taxable region(s)';
629 # } elsif ( $r->taxname && count_taxname($r->taxname) == 1 ) {
630 # $label = $r->taxname;
631 ## $regions{$label}->{'taxname'} = $label;
632 ## push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
634 $label = $r->country;
635 $label = $r->state.", $label" if $r->state;
636 $label = $r->county." county, $label" if $r->county;
637 $label = "$label (". $r->taxclass. ")"
639 && $cgi->param('show_taxclasses')
640 && ! $opt{'no_taxclass'};
641 $label = $r->taxname. " ($label)" if $r->taxname;
646 #my %count_taxname = (); #cache
648 # my $taxname = shift;
649 # return $count_taxname{$taxname} if exists $count_taxname{$taxname};
650 # my $sql = 'SELECT COUNT(*) FROM cust_main_county WHERE taxname = ?';
651 # my $sth = dbh->prepare($sql) or die dbh->errstr;
652 # $sth->execute( $taxname )
653 # or die "Unexpected error executing statement $sql: ". $sth->errstr;
654 # $count_taxname{$taxname} = $sth->fetchrow_arrayref->[0];
657 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
658 #to FS::Report or FS::Record or who the fuck knows where)
660 my( $r, $param, $sql ) = @_;
662 my $sth = dbh->prepare($sql) or die dbh->errstr;
663 $sth->execute( map $r->$_(), @$param )
664 or die "Unexpected error executing statement $sql: ". $sth->errstr;
665 $sth->fetchrow_arrayref->[0] || 0;
668 my $dateagentlink = "begin=$beginning;end=$ending";
669 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
670 if length($agentname);
671 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
672 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";