1 <% include("/elements/header.html", "$agentname Sales 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'} ne 'Total' ) {
48 % if ( $region->{'label'} eq $out ) {
51 % $link = ';'. $region->{'url_param'};
55 % if ( $bgcolor eq $bgcolor1 ) {
56 % $bgcolor = $bgcolor2;
58 % $bgcolor = $bgcolor1;
62 % my $hicolor = $bgcolor;
63 % unless ( $cgi->param('show_taxclasses') ) {
64 % my $diff = abs( sprintf( '%.2f', $region->{'owed'} )
65 % - sprintf( '%.2f', $region->{'tax'} )
67 % if ( $diff > 0.02 ) {
68 % # $hicolor = $hicolor eq '#eeeeee' ? '#eeee66' : '#ffff99';
69 % #} elsif ( $diff ) {
70 % $hicolor = $hicolor eq '#eeeeee' ? '#eeee99' : '#ffffcc';
75 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
76 % my $tdh = qq(TD CLASS="grid" BGCOLOR="$hicolor");
77 % my $bigmath = '<FONT FACE="sans-serif" SIZE="+1"><B>';
78 % my $bme = '</B></FONT>';
81 <<%$td%>><% $region->{'label'} %></TD>
82 <<%$td%> ALIGN="right">
83 <A HREF="<% $baselink. $link %>;nottax=1"
84 ><% &$money_sprintf( $region->{'total'} ) %></A>
86 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
87 <<%$td%> ALIGN="right">
88 <A HREF="<% $baselink. $link %>;nottax=1;cust_tax=Y"
89 ><% &$money_sprintf( $region->{'exempt_cust'} ) %></A>
91 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
92 <<%$td%> ALIGN="right">
93 <A HREF="<% $baselink. $link %>;nottax=1;pkg_tax=Y"
94 ><% &$money_sprintf( $region->{'exempt_pkg'} ) %></A>
96 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
97 <<%$td%> ALIGN="right">
98 <A HREF="<% $exemptlink. $link %>"
99 ><% &$money_sprintf( $region->{'exempt_monthly'} ) %></A>
101 <<%$td%>><FONT SIZE="+1"><B> = </B></FONT></TD>
102 <<%$td%> ALIGN="right">
103 <A HREF="<% $baselink. $link %>;nottax=1;taxable=1"
104 ><% &$money_sprintf( $region->{'taxable'} ) %></A>
106 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath X $bme" %></TD>
107 <<%$td%> ALIGN="right"><% $region->{'rate'} %></TD>
108 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath = $bme" %></TD>
109 <<%$tdh%> ALIGN="right">
110 <% &$money_sprintf( $region->{'owed'} ) %>
113 % unless ( $cgi->param('show_taxclasses') ) {
114 <<%$tdh%> ALIGN="right">
115 <A HREF="<% $baselink. $link %>;istax=1"
116 ><% &$money_sprintf( $region->{'tax'} ) %></A>
125 % if ( $cgi->param('show_taxclasses') ) {
128 <% include('/elements/table-grid.html') %>
130 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
131 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
134 % #some false laziness w/above
135 % $bgcolor1 = '#eeeeee';
136 % $bgcolor2 = '#ffffff';
138 % foreach my $region ( @base_regions ) {
141 % #if ( $region->{'label'} ne 'Total' ) {
142 % if ( $region->{'label'} eq $out ) {
145 % $link = ';'. $region->{'url_param'};
149 % if ( $bgcolor eq $bgcolor1 ) {
150 % $bgcolor = $bgcolor2;
152 % $bgcolor = $bgcolor1;
154 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
157 <<%$td%>><% $region->{'label'} %></TD>
158 <<%$td%> ALIGN="right">
159 <A HREF="<% $baselink. $link %>;istax=1"
160 ><% &$money_sprintf( $region->{'tax'} ) %></A>
166 % if ( $bgcolor eq $bgcolor1 ) {
167 % $bgcolor = $bgcolor2;
169 % $bgcolor = $bgcolor1;
171 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
175 <<%$td%> ALIGN="right">
176 <A HREF="<% $baselink %>;istax=1"
177 ><% &$money_sprintf( $tax ) %></A>
185 <% include('/elements/footer.html') %>
190 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
192 my $conf = new FS::Conf;
194 my $user = getotaker;
196 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
198 my $join_cust = ' JOIN cust_bill USING ( invnum )
199 LEFT JOIN cust_main USING ( custnum ) ';
200 my $join_cust_pkg = $join_cust.
201 ' LEFT JOIN cust_pkg USING ( pkgnum )
202 LEFT JOIN part_pkg USING ( pkgpart ) ';
203 $join_cust_pkg .= ' LEFT JOIN cust_location USING ( locationnum )'
204 if $conf->exists('tax-pkg_address');
206 my $from_join_cust_pkg = " FROM cust_bill_pkg $join_cust_pkg ";
208 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
210 my( $location_sql, @base_param ) = FS::cust_pkg->location_sql;
211 $where .= " AND $location_sql ";
214 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
215 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
216 die "agent not found" unless $agent;
217 $agentname = $agent->agent;
218 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
223 my $prefix = @_ ? shift : '';
225 ( $table.${prefix}county = cust_main_county.county
226 OR cust_main_county.county = ''
227 OR cust_main_county.county IS NULL )
228 AND ( $table.${prefix}state = cust_main_county.state
229 OR cust_main_county.state = ''
230 OR cust_main_county.state IS NULL )
231 AND ( $table.${prefix}country = cust_main_county.country )
236 if ( $conf->exists('tax-ship_address') ) {
239 ( cust_main_county.country = cust_main.country
240 OR cust_main_county.country = cust_main.ship_country
246 ( ( ship_last IS NULL OR ship_last = '' )
247 AND ". gotcust('cust_main'). "
250 ( ship_last IS NOT NULL AND ship_last != ''
251 AND ". gotcust('cust_main', 'ship_'). "
258 $gotcust = gotcust('cust_main');
261 if ( $conf->exists('tax-pkg_address') ) {
263 ( cust_pkg.locationnum IS NULL AND $gotcust)
264 OR ( cust_pkg.locationnum IS NOT NULL AND ". gotcust('cust_location'). " )";
266 "WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
267 LEFT JOIN cust_main USING ( custnum )
268 LEFT JOIN cust_location USING ( locationnum )
275 "WHERE 0 < ( SELECT COUNT(*) FROM cust_main WHERE $gotcust LIMIT 1 )";
278 my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0 );
279 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0, 0 );
280 my $out = 'Out of taxable region(s)';
283 foreach my $r ( qsearch({ 'table' => 'cust_main_county',
284 'extra_sql' => $gotcust,
288 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
290 my $label = getlabel($r);
291 $regions{$label}->{'label'} = $label;
292 $regions{$label}->{'url_param'} =
293 join(';', map "$_=".uri_escape($r->$_()),
294 qw( county state country taxname )
297 my @param = @base_param;
298 my $mywhere = $where;
300 if ( $r->taxclass ) {
302 $mywhere .= " AND taxclass = ? ";
303 push @param, 'taxclass';
304 $regions{$label}->{'url_param'} .= ';taxclass='. uri_escape($r->taxclass);
305 #no, always# if $cgi->param('show_taxclasses');
309 $regions{$label}->{'url_param'} .= ';taxclassNULL=1'
310 if $cgi->param('show_taxclasses');
312 my $same_sql = $r->sql_taxclass_sameregion;
313 $mywhere .= " AND $same_sql" if $same_sql;
317 my $fromwhere = "$from_join_cust_pkg $mywhere"; # AND payby != 'COMP' ";
319 # my $label = getlabel($r);
320 # $regions{$label}->{'label'} = $label;
322 my $nottax = 'pkgnum != 0';
324 ## calculate total for this region
327 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax";
328 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 $exempt_cust += $x_cust;
360 $regions{$label}->{'exempt_cust'} += $x_cust;
362 ## calculate package-exemption for this region
364 my $x_pkg = scalar_sql($r, \@param,
366 ( CASE WHEN part_pkg.setuptax = 'Y'
367 THEN cust_bill_pkg.setup
372 ( CASE WHEN part_pkg.recurtax = 'Y'
373 THEN cust_bill_pkg.recur
381 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
382 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
384 AND ( tax != 'Y' OR tax IS NULL )
387 $exempt_pkg += $x_pkg;
388 $regions{$label}->{'exempt_pkg'} += $x_pkg;
390 ## calculate monthly exemption (texas tax) for this region
392 # count up all the cust_tax_exempt_pkg records associated with
393 # the actual line items.
395 my $x_monthly = scalar_sql($r, \@param,
397 FROM cust_tax_exempt_pkg
398 JOIN cust_bill_pkg USING ( billpkgnum )
402 # if ( $x_monthly ) {
403 # #warn $r->taxnum(). ": $x_monthly\n";
404 # $taxable -= $x_monthly;
407 $exempt_monthly += $x_monthly;
408 $regions{$label}->{'exempt_monthly'} += $x_monthly;
410 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
412 $tot_taxable += $taxable;
413 $regions{$label}->{'taxable'} += $taxable;
415 $owed += $taxable * ($r->tax/100);
416 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
418 if ( defined($regions{$label}->{'rate'})
419 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
420 $regions{$label}->{'rate'} = 'variable';
422 $regions{$label}->{'rate'} = $r->tax.'%';
427 my $distinct = "country, state, county,
428 CASE WHEN taxname IS NULL THEN '' ELSE taxname END AS taxname";
429 my $taxclass_distinct =
430 #a little bit unsure of this part... test?
431 #ah, it looks like it winds up being irrelevant as ->{'tax'}
432 # from $regions is not displayed when show_taxclasses is on
433 ( $cgi->param('show_taxclasses')
434 ? " CASE WHEN taxclass IS NULL THEN '' ELSE taxclass END "
440 'select' => "DISTINCT $distinct, $taxclass_distinct",
441 'table' => 'cust_main_county',
443 'extra_sql' => $gotcust,
446 my $taxfromwhere = " FROM cust_bill_pkg $join_cust ";
447 my $taxwhere = $where;
448 if ( $conf->exists('tax-pkg_address') ) {
450 $taxfromwhere .= 'LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
451 LEFT JOIN cust_location USING ( locationnum ) ';
454 $taxwhere =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
457 $taxfromwhere .= " $taxwhere "; #AND payby != 'COMP' ";
458 my @taxparam = @base_param;
460 #should i be a cust_main_county method or something
461 #need to pass in $taxfromwhere & @taxparam???
462 my $_taxamount_sub = sub {
465 #match itemdesc if necessary!
468 ? 'AND itemdesc = '. dbh->quote($r->taxname)
469 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
471 my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
472 " $taxfromwhere AND cust_bill_pkg.pkgnum = 0 $named_tax";
474 scalar_sql($r, \@taxparam, $sql );
477 #foreach my $label ( keys %regions ) {
478 foreach my $r ( qsearch(\%qsearch) ) {
480 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
482 my $label = getlabel($r);
484 #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
485 #my @param = @base_param;
487 my $x = &{$_taxamount_sub}($r);
489 $tax += $x unless $cgi->param('show_taxclasses');
490 $regions{$label}->{'tax'} += $x;
494 my %base_regions = ();
495 if ( $cgi->param('show_taxclasses') ) {
497 $qsearch{'select'} = "DISTINCT $distinct";
498 foreach my $r ( qsearch(\%qsearch) ) {
500 my $x = &{$_taxamount_sub}($r);
502 my $base_label = getlabel($r, 'no_taxclass'=>1 );
503 $base_regions{$base_label}->{'label'} = $base_label;
505 $base_regions{$base_label}->{'url_param'} =
506 join(';', map "$_=". uri_escape($r->$_()),
507 qw( county state country taxname )
510 $base_regions{$base_label}->{'tax'} += $x;
520 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
524 map $base_regions{$_},
525 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
532 'exempt_cust' => $exempt_cust,
533 'exempt_pkg' => $exempt_pkg,
534 'exempt_monthly' => $exempt_monthly,
535 'taxable' => $tot_taxable,
543 my $money_char = $conf->config('money_char') || '$';
544 my $money_sprintf = sub {
545 $money_char. sprintf('%.2f', shift );
555 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
556 'county' => $r->county,
557 'country' => $r->country,
558 'tax' => { op=>'>', value=>0 },
564 #kludge to avoid "will not stay shared" warning
565 my $out = 'Out of taxable region(s)';
567 # } elsif ( $r->taxname && count_taxname($r->taxname) == 1 ) {
568 # $label = $r->taxname;
569 ## $regions{$label}->{'taxname'} = $label;
570 ## push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
572 $label = $r->country;
573 $label = $r->state.", $label" if $r->state;
574 $label = $r->county." county, $label" if $r->county;
575 $label = "$label (". $r->taxclass. ")"
577 && $cgi->param('show_taxclasses')
578 && ! $opt{'no_taxclass'};
579 $label = $r->taxname. " ($label)" if $r->taxname;
584 #my %count_taxname = (); #cache
586 # my $taxname = shift;
587 # return $count_taxname{$taxname} if exists $count_taxname{$taxname};
588 # my $sql = 'SELECT COUNT(*) FROM cust_main_county WHERE taxname = ?';
589 # my $sth = dbh->prepare($sql) or die dbh->errstr;
590 # $sth->execute( $taxname )
591 # or die "Unexpected error executing statement $sql: ". $sth->errstr;
592 # $count_taxname{$taxname} = $sth->fetchrow_arrayref->[0];
595 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
596 #to FS::Report or FS::Record or who the fuck knows where)
598 my( $r, $param, $sql ) = @_;
600 my $sth = dbh->prepare($sql) or die dbh->errstr;
601 $sth->execute( map $r->$_(), @$param )
602 or die "Unexpected error executing statement $sql: ". $sth->errstr;
603 $sth->fetchrow_arrayref->[0] || 0;
606 my $dateagentlink = "begin=$beginning;end=$ending";
607 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
608 if length($agentname);
609 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
610 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";