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') ) {
25 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
30 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
31 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
32 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
33 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
34 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
35 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
36 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
37 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
38 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
40 % my $bgcolor1 = '#eeeeee';
41 % my $bgcolor2 = '#ffffff';
44 % foreach my $region ( @regions ) {
46 % if ( $bgcolor eq $bgcolor1 ) {
47 % $bgcolor = $bgcolor2;
49 % $bgcolor = $bgcolor1;
53 % if ( $region->{'label'} ne 'Total' ) {
54 % if ( $region->{'label'} eq $out ) {
57 % $link = ';'. $region->{'url_param'};
68 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} %></TD>
69 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
70 <A HREF="<% $baselink. $link %>;nottax=1"><% $money_char %><% sprintf('%.2f', $region->{'total'} ) %></A>
72 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
73 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
74 <A HREF="<% $baselink. $link %>;nottax=1;cust_tax=Y"><% $money_char %><% sprintf('%.2f', $region->{'exempt_cust'} ) %></A>
76 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
77 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
78 <A HREF="<% $baselink. $link %>;nottax=1;pkg_tax=Y"><% $money_char %><% sprintf('%.2f', $region->{'exempt_pkg'} ) %></A>
80 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
81 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
82 <A HREF="<% $exemptlink. $link %>"><% $money_char %><% sprintf('%.2f', $region->{'exempt_monthly'} ) %></A>
84 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><FONT SIZE="+1"><B> = </B></FONT></TD>
85 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
86 <% $money_char %><% sprintf('%.2f', $region->{'taxable'} ) %></A>
88 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> X </B></FONT>' %></TD>
89 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right"><% $region->{'rate'} %></TD>
90 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> = </B></FONT>' %></TD>
91 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
92 <% $money_char %><% sprintf('%.2f', $region->{'owed'} ) %>
94 % unless ( $cgi->param('show_taxclasses') ) {
96 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
97 <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $region->{'tax'} ) %></A>
106 % if ( $cgi->param('show_taxclasses') ) {
110 <% include('/elements/table-grid.html') %>
112 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
113 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
115 % #some false laziness w/above
116 % $bgcolor1 = '#eeeeee';
117 % $bgcolor2 = '#ffffff';
118 % foreach my $region ( @base_regions ) {
120 % if ( $bgcolor eq $bgcolor1 ) {
121 % $bgcolor = $bgcolor2;
123 % $bgcolor = $bgcolor1;
127 % #if ( $region->{'label'} ne 'Total' ) {
128 % if ( $region->{'label'} eq $out ) {
131 % $link = ';'. $region->{'url_param'};
138 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $region->{'label'} %></TD>
139 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
140 <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $region->{'tax'} ) %></A>
145 % if ( $bgcolor eq $bgcolor1 ) {
146 % $bgcolor = $bgcolor2;
148 % $bgcolor = $bgcolor1;
154 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">Total</TD>
155 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
156 <A HREF="<% $baselink %>;istax=1"><% $money_char %><% sprintf('%.2f', $tax ) %></A>
169 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
171 my $conf = new FS::Conf;
172 my $money_char = $conf->config('money_char') || '$';
174 my $user = getotaker;
176 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
179 JOIN cust_bill USING ( invnum )
180 LEFT JOIN cust_main USING ( custnum )
182 my $from_join_cust = "
187 LEFT JOIN cust_pkg USING ( pkgnum )
188 LEFT JOIN part_pkg USING ( pkgpart )
191 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
192 my @base_param = qw( county county state state country );
193 if ( $conf->exists('tax-ship_address') ) {
196 AND ( ( ( ship_last IS NULL OR ship_last = '' )
197 AND ( county = ? OR ? = '' )
198 AND ( state = ? OR ? = '' )
201 OR ( ship_last IS NOT NULL AND ship_last != ''
202 AND ( ship_county = ? OR ? = '' )
203 AND ( ship_state = ? OR ? = '' )
208 # AND payby != 'COMP'
210 push @base_param, @base_param;
215 AND ( county = ? OR ? = '' )
216 AND ( state = ? OR ? = '' )
219 # AND payby != 'COMP'
224 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
225 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
226 die "agent not found" unless $agent;
227 $agentname = $agent->agent;
228 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
232 WHERE 0 < ( SELECT COUNT(*) FROM cust_main
234 if ( $conf->exists('tax-ship_address') ) {
239 ( cust_main_county.country = cust_main.country
240 OR cust_main_county.country = cust_main.ship_country
247 ( ( ship_last IS NULL OR ship_last = '' )
248 AND ( cust_main_county.country = cust_main.country )
249 AND ( cust_main_county.state = cust_main.state
250 OR cust_main_county.state = ''
251 OR cust_main_county.state IS NULL )
252 AND ( cust_main_county.county = cust_main.county
253 OR cust_main_county.county = ''
254 OR cust_main_county.county IS NULL )
259 ( ship_last IS NOT NULL AND ship_last != ''
260 AND ( cust_main_county.country = cust_main.ship_country )
261 AND ( cust_main_county.state = cust_main.ship_state
262 OR cust_main_county.state = ''
263 OR cust_main_county.state IS NULL )
264 AND ( cust_main_county.county = cust_main.ship_county
265 OR cust_main_county.county = ''
266 OR cust_main_county.county IS NULL )
278 WHERE ( cust_main.county = cust_main_county.county
279 OR cust_main_county.county = ''
280 OR cust_main_county.county IS NULL )
281 AND ( cust_main.state = cust_main_county.state
282 OR cust_main_county.state = ''
283 OR cust_main_county.state IS NULL )
284 AND ( cust_main.country = cust_main_county.country )
291 my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0 );
292 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0, 0 );
293 my $out = 'Out of taxable region(s)';
295 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
296 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
298 my $label = getlabel($r);
299 $regions{$label}->{'label'} = $label;
300 $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
302 my @param = @base_param;
303 my $mywhere = $where;
305 if ( $r->taxclass ) {
307 $mywhere .= " AND taxclass = ? ";
308 push @param, 'taxclass';
309 $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass
310 if $cgi->param('show_taxclasses');
314 $regions{$label}->{'url_param'} .= ';taxclassNULL=1'
315 if $cgi->param('show_taxclasses');
317 my $same_sql = $r->sql_taxclass_sameregion;
318 $mywhere .= " AND $same_sql" if $same_sql;
322 my $fromwhere = $from_join_cust. $join_pkg. $mywhere. " AND payby != 'COMP' ";
324 # my $label = getlabel($r);
325 # $regions{$label}->{'label'} = $label;
327 my $nottax = 'pkgnum != 0';
329 ## calculate total for this region
331 my $t = scalar_sql($r, \@param,
332 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
335 $regions{$label}->{'total'} += $t;
337 ## calculate customer-exemption for this region
341 # my($taxable, $x_cust) = (0, 0);
342 # foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
343 # qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
344 # $taxable += scalar_sql($r, \@param,
345 # "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
348 # $x_cust += scalar_sql($r, \@param,
349 # "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
353 my $x_cust = scalar_sql($r, \@param,
354 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
355 $fromwhere AND $nottax AND tax = 'Y' "
358 $exempt_cust += $x_cust;
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 $exempt_pkg += $x_pkg;
387 $regions{$label}->{'exempt_pkg'} += $x_pkg;
389 ## calculate monthly exemption (texas tax) for this region
391 # count up all the cust_tax_exempt_pkg records associated with
392 # the actual line items.
394 my $x_monthly = scalar_sql($r, \@param,
396 FROM cust_tax_exempt_pkg
397 JOIN cust_bill_pkg USING ( billpkgnum )
401 # if ( $x_monthly ) {
402 # #warn $r->taxnum(). ": $x_monthly\n";
403 # $taxable -= $x_monthly;
406 $exempt_monthly += $x_monthly;
407 $regions{$label}->{'exempt_monthly'} += $x_monthly;
409 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
411 $tot_taxable += $taxable;
412 $regions{$label}->{'taxable'} += $taxable;
414 $owed += $taxable * ($r->tax/100);
415 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
417 if ( defined($regions{$label}->{'rate'})
418 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
419 $regions{$label}->{'rate'} = 'variable';
421 $regions{$label}->{'rate'} = $r->tax.'%';
426 my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ";
427 my @taxparam = @base_param;
428 my %base_regions = ();
429 #foreach my $label ( keys %regions ) {
431 qsearch( 'cust_main_county',
437 CASE WHEN taxname IS NULL THEN '' ELSE taxname END AS taxname,".
439 #a little bit unsure of this part... test?
440 #ah, it looks like it winds up being irrelevant as ->{'tax'}
441 # from $regions is not displayed when show_taxclasses is on
442 ( $cgi->param('show_taxclasses')
443 ? " CASE WHEN taxclass IS NULL THEN '' ELSE taxclass END "
451 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
453 my $label = getlabel($r);
455 #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
456 #my @param = @base_param;
458 #match itemdesc if necessary!
461 ? 'AND itemdesc = '. dbh->quote($r->taxname)
462 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
464 my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
465 " $taxwhere AND pkgnum = 0 $named_tax";
467 my $x = scalar_sql($r, \@taxparam, $sql );
469 $regions{$label}->{'tax'} += $x;
471 if ( $cgi->param('show_taxclasses') ) {
472 my $base_label = getlabel($r, 'no_taxclass'=>1 );
473 $base_regions{$base_label}->{'label'} = $base_label;
474 $base_regions{$base_label}->{'url_param'} =
475 join(';', map "$_=".$r->$_(), qw( county state country ) );
476 $base_regions{$base_label}->{'tax'} += $x;
484 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
488 map $base_regions{$_},
489 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
496 'exempt_cust' => $exempt_cust,
497 'exempt_pkg' => $exempt_pkg,
498 'exempt_monthly' => $exempt_monthly,
499 'taxable' => $tot_taxable,
514 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
515 'county' => $r->county,
516 'country' => $r->country,
517 'tax' => { op=>'>', value=>0 },
523 #kludge to avoid "will not stay shared" warning
524 my $out = 'Out of taxable region(s)';
526 } elsif ( $r->taxname ) {
527 $label = $r->taxname;
528 # $regions{$label}->{'taxname'} = $label;
529 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
531 $label = $r->country;
532 $label = $r->state.", $label" if $r->state;
533 $label = $r->county." county, $label" if $r->county;
534 $label = "$label (". $r->taxclass. ")"
536 && $cgi->param('show_taxclasses')
537 && ! $opt{'no_taxclass'};
538 #$label = $r->taxname. " ($label)" if $r->taxname;
543 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
544 #to FS::Report or FS::Record or who the fuck knows where)
546 my( $r, $param, $sql ) = @_;
548 my $sth = dbh->prepare($sql) or die dbh->errstr;
549 $sth->execute( map $r->$_(), @$param )
550 or die "Unexpected error executing statement $sql: ". $sth->errstr;
551 $sth->fetchrow_arrayref->[0] || 0;
554 my $dateagentlink = "begin=$beginning;end=$ending";
555 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
556 if length($agentname);
557 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
558 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";