1 <% include("/elements/header.html", "$agentname Sales Tax Report - ".
3 ? time2str('%h %o %Y ', $beginning )
7 ( $ending == 4294967295
9 : time2str('%h %o %Y', $ending )
11 menubar( 'Main Menu'=>$p, )
15 <% include('/elements/table-grid.html') %>
18 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
19 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
20 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
21 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Rate</TH>
22 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
23 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax owed</TH>
24 % unless ( $cgi->param('show_taxclasses') ) {
26 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
31 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
32 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
33 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
34 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
35 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
36 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
37 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
38 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
39 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
41 % my $bgcolor1 = '#eeeeee';
42 % my $bgcolor2 = '#ffffff';
45 % foreach my $region ( @regions ) {
47 % if ( $bgcolor eq $bgcolor1 ) {
48 % $bgcolor = $bgcolor2;
50 % $bgcolor = $bgcolor1;
54 % if ( $region->{'label'} ne 'Total' ) {
55 % if ( $region->{'label'} eq $out ) {
58 % $link = ';'. $region->{'url_param'};
69 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} %></TD>
70 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
71 <A HREF="<% $baselink. $link %>;nottax=1"><% $money_char %><% sprintf('%.2f', $region->{'total'} ) %></A>
73 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
74 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
75 <A HREF="<% $baselink. $link %>;nottax=1;cust_tax=Y"><% $money_char %><% sprintf('%.2f', $region->{'exempt_cust'} ) %></A>
77 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
78 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
79 <A HREF="<% $baselink. $link %>;nottax=1;pkg_tax=Y"><% $money_char %><% sprintf('%.2f', $region->{'exempt_pkg'} ) %></A>
81 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
82 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
83 <A HREF="<% $exemptlink. $link %>"><% $money_char %><% sprintf('%.2f', $region->{'exempt_monthly'} ) %></A>
85 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> = </B></FONT></TD>
86 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
87 <% $money_char %><% sprintf('%.2f', $region->{'taxable'} ) %></A>
89 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> X </B></FONT>' %></TD>
90 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right"><% $region->{'rate'} %></TD>
91 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> = </B></FONT>' %></TD>
92 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
93 <% $money_char %><% sprintf('%.2f', $region->{'owed'} ) %>
95 % unless ( $cgi->param('show_taxclasses') ) {
97 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
98 <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $region->{'tax'} ) %></A>
107 % if ( $cgi->param('show_taxclasses') ) {
111 <% include('/elements/table-grid.html') %>
113 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
114 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
116 % #some false laziness w/above
117 % $bgcolor1 = '#eeeeee';
118 % $bgcolor2 = '#ffffff';
119 % foreach my $region ( @base_regions ) {
121 % if ( $bgcolor eq $bgcolor1 ) {
122 % $bgcolor = $bgcolor2;
124 % $bgcolor = $bgcolor1;
128 % #if ( $region->{'label'} ne 'Total' ) {
129 % if ( $region->{'label'} eq $out ) {
132 % $link = ';'. $region->{'url_param'};
139 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} %></TD>
140 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
141 <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $region->{'tax'} ) %></A>
146 % if ( $bgcolor eq $bgcolor1 ) {
147 % $bgcolor = $bgcolor2;
149 % $bgcolor = $bgcolor1;
155 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">Total</TD>
156 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
157 <A HREF="<% $baselink %>;istax=1"><% $money_char %><% sprintf('%.2f', $tax ) %></A>
170 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
172 my $conf = new FS::Conf;
173 my $money_char = $conf->config('money_char') || '$';
175 my $user = getotaker;
177 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
180 JOIN cust_bill USING ( invnum )
181 LEFT JOIN cust_main USING ( custnum )
183 my $from_join_cust = "
188 LEFT JOIN cust_pkg USING ( pkgnum )
189 LEFT JOIN part_pkg USING ( pkgpart )
192 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
193 my @base_param = qw( county county state state country );
194 if ( $conf->exists('tax-ship_address') ) {
197 AND ( ( ( ship_last IS NULL OR ship_last = '' )
198 AND ( county = ? OR ? = '' )
199 AND ( state = ? OR ? = '' )
202 OR ( ship_last IS NOT NULL AND ship_last != ''
203 AND ( ship_county = ? OR ? = '' )
204 AND ( ship_state = ? OR ? = '' )
209 # AND payby != 'COMP'
211 push @base_param, @base_param;
216 AND ( county = ? OR ? = '' )
217 AND ( state = ? OR ? = '' )
220 # AND payby != 'COMP'
225 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
226 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
227 die "agent not found" unless $agent;
228 $agentname = $agent->agent;
229 $where .= ' AND agentnum = '. $agent->agentnum;
233 WHERE 0 < ( SELECT COUNT(*) FROM cust_main
235 if ( $conf->exists('tax-ship_address') ) {
240 ( cust_main_county.country = cust_main.country
241 OR cust_main_county.country = cust_main.ship_country
248 ( ( ship_last IS NULL OR ship_last = '' )
249 AND ( cust_main_county.country = cust_main.country )
250 AND ( cust_main_county.state = cust_main.state
251 OR cust_main_county.state = ''
252 OR cust_main_county.state IS NULL )
253 AND ( cust_main_county.county = cust_main.county
254 OR cust_main_county.county = ''
255 OR cust_main_county.county IS NULL )
260 ( ship_last IS NOT NULL AND ship_last != ''
261 AND ( cust_main_county.country = cust_main.ship_country )
262 AND ( cust_main_county.state = cust_main.ship_state
263 OR cust_main_county.state = ''
264 OR cust_main_county.state IS NULL )
265 AND ( cust_main_county.county = cust_main.ship_county
266 OR cust_main_county.county = ''
267 OR cust_main_county.county IS NULL )
279 WHERE ( cust_main.county = cust_main_county.county
280 OR cust_main_county.county = ''
281 OR cust_main_county.county IS NULL )
282 AND ( cust_main.state = cust_main_county.state
283 OR cust_main_county.state = ''
284 OR cust_main_county.state IS NULL )
285 AND ( cust_main.country = cust_main_county.country )
292 my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
293 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0 );
294 my $out = 'Out of taxable region(s)';
296 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
297 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
299 my $label = getlabel($r);
300 $regions{$label}->{'label'} = $label;
301 $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
303 my @param = @base_param;
304 my $mywhere = $where;
306 if ( $r->taxclass ) {
308 $mywhere .= " AND taxclass = ? ";
309 push @param, 'taxclass';
310 $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass
311 if $cgi->param('show_taxclasses');
315 my $same_query = "SELECT COUNT(*) FROM cust_main_county WHERE country = ?";
316 my @same_param = ( 'country' );
317 foreach my $opt_field (qw( state county )) {
318 if ( $r->$opt_field() ) {
319 $same_query .= " AND $opt_field = ?";
320 push @same_param, $opt_field;
322 $same_query .= " AND $opt_field IS NULL";
326 my $num_same_region = scalar_sql( $r, \@same_param, $same_query );
328 if ( $num_same_region > 1 ) {
330 $mywhere .= " AND taxclass IS NULL";
336 my $fromwhere = $from_join_cust. $join_pkg. $mywhere. " AND payby != 'COMP' ";
338 # my $label = getlabel($r);
339 # $regions{$label}->{'label'} = $label;
341 my $nottax = 'pkgnum != 0';
343 ## calculate total for this region
345 my $t = scalar_sql($r, \@param,
346 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
349 $regions{$label}->{'total'} += $t;
351 ## calculate customer-exemption for this region
355 # my($taxable, $x_cust) = (0, 0);
356 # foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
357 # qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
358 # $taxable += scalar_sql($r, \@param,
359 # "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
362 # $x_cust += scalar_sql($r, \@param,
363 # "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
367 my $x_cust = scalar_sql($r, \@param,
368 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
369 $fromwhere AND $nottax AND tax = 'Y' "
372 $exempt_cust += $x_cust;
373 $regions{$label}->{'exempt_cust'} += $x_cust;
375 ## calculate package-exemption for this region
377 my $x_pkg = scalar_sql($r, \@param,
379 ( CASE WHEN part_pkg.setuptax = 'Y'
380 THEN cust_bill_pkg.setup
385 ( CASE WHEN part_pkg.recurtax = 'Y'
386 THEN cust_bill_pkg.recur
394 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
395 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
397 AND ( tax != 'Y' OR tax IS NULL )
400 $exempt_pkg += $x_pkg;
401 $regions{$label}->{'exempt_pkg'} += $x_pkg;
403 ## calculate monthly exemption (texas tax) for this region
405 # count up all the cust_tax_exempt_pkg records associated with
406 # the actual line items.
408 my $x_monthly = scalar_sql($r, \@param,
410 FROM cust_tax_exempt_pkg
411 JOIN cust_bill_pkg USING ( billpkgnum )
415 # if ( $x_monthly ) {
416 # #warn $r->taxnum(). ": $x_monthly\n";
417 # $taxable -= $x_monthly;
420 $exempt_monthly += $x_monthly;
421 $regions{$label}->{'exempt_monthly'} += $x_monthly;
423 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
425 $tot_taxable += $taxable;
426 $regions{$label}->{'taxable'} += $taxable;
428 $owed += $taxable * ($r->tax/100);
429 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
431 if ( defined($regions{$label}->{'rate'})
432 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
433 $regions{$label}->{'rate'} = 'variable';
435 $regions{$label}->{'rate'} = $r->tax.'%';
440 my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ";
441 my @taxparam = @base_param;
442 my %base_regions = ();
443 #foreach my $label ( keys %regions ) {
445 qsearch( 'cust_main_county',
447 "DISTINCT ON ( country, state, county, CASE WHEN taxname IS NULL THEN '' ELSE taxname END ) *",
452 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
454 my $label = getlabel($r);
456 #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
457 #my @param = @base_param;
459 #match itemdesc if necessary!
462 ? 'AND itemdesc = '. dbh->quote($r->taxname)
463 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
465 my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
466 " $taxwhere AND pkgnum = 0 $named_tax";
468 my $x = scalar_sql($r, \@taxparam, $sql );
470 $regions{$label}->{'tax'} += $x;
472 if ( $cgi->param('show_taxclasses') ) {
473 my $base_label = getlabel($r, 'no_taxclass'=>1 );
474 $base_regions{$base_label}->{'label'} = $base_label;
475 $base_regions{$base_label}->{'url_param'} =
476 join(';', map "$_=".$r->$_(), qw( county state country ) );
477 $base_regions{$base_label}->{'tax'} += $x;
485 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
489 map $base_regions{$_},
490 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
497 'exempt_cust' => $exempt_cust,
498 'exempt_pkg' => $exempt_pkg,
499 'exempt_monthly' => $exempt_monthly,
500 'taxable' => $tot_taxable,
515 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
516 'county' => $r->county,
517 'country' => $r->country,
518 'tax' => { op=>'>', value=>0 },
524 #kludge to avoid "will not stay shared" warning
525 my $out = 'Out of taxable region(s)';
527 } elsif ( $r->taxname ) {
528 $label = $r->taxname;
529 # $regions{$label}->{'taxname'} = $label;
530 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
532 $label = $r->country;
533 $label = $r->state.", $label" if $r->state;
534 $label = $r->county." county, $label" if $r->county;
535 $label = "$label (". $r->taxclass. ")"
537 && $cgi->param('show_taxclasses')
538 && ! $opt{'no_taxclass'};
539 #$label = $r->taxname. " ($label)" if $r->taxname;
544 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
545 #to FS::Report or FS::Record or who the fuck knows where)
547 my( $r, $param, $sql ) = @_;
549 my $sth = dbh->prepare($sql) or die dbh->errstr;
550 $sth->execute( map $r->$_(), @$param )
551 or die "Unexpected error executing statement $sql: ". $sth->errstr;
552 $sth->fetchrow_arrayref->[0] || 0;
557 my $dateagentlink = "begin=$beginning;end=$ending";
558 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
559 if length($agentname);
560 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
561 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";