3 my $conf = new FS::Conf;
4 my $money_char = $conf->config('money_char') || '$';
8 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
11 JOIN cust_bill USING ( invnum )
12 LEFT JOIN cust_main USING ( custnum )
14 my $from_join_cust = "
19 LEFT JOIN cust_pkg USING ( pkgnum )
20 LEFT JOIN part_pkg USING ( pkgpart )
23 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
24 my @base_param = qw( county county state state country );
25 if ( $conf->exists('tax-ship_address') ) {
28 AND ( ( ( ship_last IS NULL OR ship_last = '' )
29 AND ( county = ? OR ? = '' )
30 AND ( state = ? OR ? = '' )
33 OR ( ship_last IS NOT NULL AND ship_last != ''
34 AND ( ship_county = ? OR ? = '' )
35 AND ( ship_state = ? OR ? = '' )
42 push @base_param, @base_param;
47 AND ( county = ? OR ? = '' )
48 AND ( state = ? OR ? = '' )
56 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
57 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
58 die "agent not found" unless $agent;
59 $agentname = $agent->agent;
60 $where .= ' AND agentnum = '. $agent->agentnum;
64 WHERE 0 < ( SELECT COUNT(*) FROM cust_main
66 if ( $conf->exists('tax-ship_address') ) {
71 ( cust_main_county.country = cust_main.country
72 OR cust_main_county.country = cust_main.ship_country
79 ( ( ship_last IS NULL OR ship_last = '' )
80 AND ( cust_main_county.country = cust_main.country )
81 AND ( cust_main_county.state = cust_main.state
82 OR cust_main_county.state = ''
83 OR cust_main_county.state IS NULL )
84 AND ( cust_main_county.county = cust_main.county
85 OR cust_main_county.county = ''
86 OR cust_main_county.county IS NULL )
91 ( ship_last IS NOT NULL AND ship_last != ''
92 AND ( cust_main_county.country = cust_main.ship_country )
93 AND ( cust_main_county.state = cust_main.ship_state
94 OR cust_main_county.state = ''
95 OR cust_main_county.state IS NULL )
96 AND ( cust_main_county.county = cust_main.ship_county
97 OR cust_main_county.county = ''
98 OR cust_main_county.county IS NULL )
110 WHERE ( cust_main.county = cust_main_county.county
111 OR cust_main_county.county = ''
112 OR cust_main_county.county IS NULL )
113 AND ( cust_main.state = cust_main_county.state
114 OR cust_main_county.state = ''
115 OR cust_main_county.state IS NULL )
116 AND ( cust_main.country = cust_main_county.country )
123 my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
124 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0 );
125 my $out = 'Out of taxable region(s)';
127 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
128 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
130 my $label = getlabel($r);
131 $regions{$label}->{'label'} = $label;
132 $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
134 my @param = @base_param;
135 my $mywhere = $where;
137 if ( $r->taxclass ) {
138 $mywhere .= " AND taxclass = ? ";
139 push @param, 'taxclass';
140 $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass
141 if $cgi->param('show_taxclasses');
144 my $fromwhere = $from_join_cust. $join_pkg. $mywhere. " AND payby != 'COMP' ";
146 # my $label = getlabel($r);
147 # $regions{$label}->{'label'} = $label;
149 my $nottax = 'pkgnum != 0';
151 ## calculate total for this region
153 my $t = scalar_sql($r, \@param,
154 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
157 $regions{$label}->{'total'} += $t;
159 ## calculate customer-exemption for this region
163 # my($taxable, $x_cust) = (0, 0);
164 # foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
165 # qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
166 # $taxable += scalar_sql($r, \@param,
167 # "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
170 # $x_cust += scalar_sql($r, \@param,
171 # "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
175 my $x_cust = scalar_sql($r, \@param,
176 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
177 $fromwhere AND $nottax AND tax = 'Y' "
180 $exempt_cust += $x_cust;
181 $regions{$label}->{'exempt_cust'} += $x_cust;
183 ## calculate package-exemption for this region
185 my $x_pkg = scalar_sql($r, \@param,
187 ( CASE WHEN part_pkg.setuptax = 'Y'
188 THEN cust_bill_pkg.setup
193 ( CASE WHEN part_pkg.recurtax = 'Y'
194 THEN cust_bill_pkg.recur
202 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
203 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
205 AND ( tax != 'Y' OR tax IS NULL )
208 $exempt_pkg += $x_pkg;
209 $regions{$label}->{'exempt_pkg'} += $x_pkg;
211 ## calculate monthly exemption (texas tax) for this region
213 # count up all the cust_tax_exempt_pkg records associated with
214 # the actual line items.
216 my $x_monthly = scalar_sql($r, \@param,
218 FROM cust_tax_exempt_pkg
219 JOIN cust_bill_pkg USING ( billpkgnum )
223 # if ( $x_monthly ) {
224 # #warn $r->taxnum(). ": $x_monthly\n";
225 # $taxable -= $x_monthly;
228 $exempt_monthly += $x_monthly;
229 $regions{$label}->{'exempt_monthly'} += $x_monthly;
231 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
233 $tot_taxable += $taxable;
234 $regions{$label}->{'taxable'} += $taxable;
236 $owed += $taxable * ($r->tax/100);
237 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
239 if ( defined($regions{$label}->{'rate'})
240 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
241 $regions{$label}->{'rate'} = 'variable';
243 $regions{$label}->{'rate'} = $r->tax.'%';
248 my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ";
249 my @taxparam = @base_param;
250 my %base_regions = ();
251 #foreach my $label ( keys %regions ) {
253 qsearch( 'cust_main_county',
255 'DISTINCT ON (country, state, county, taxname) *',
260 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
262 my $label = getlabel($r);
264 #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
265 #my @param = @base_param;
267 #match itemdesc if necessary!
270 ? 'AND itemdesc = '. dbh->quote($r->taxname)
271 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
272 my $x = scalar_sql($r, \@taxparam,
273 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
274 "AND pkgnum = 0 $named_tax",
277 $regions{$label}->{'tax'} += $x;
279 if ( $cgi->param('show_taxclasses') ) {
280 my $base_label = getlabel($r, 'no_taxclass'=>1 );
281 $base_regions{$base_label}->{'label'} = $base_label;
282 $base_regions{$base_label}->{'url_param'} =
283 join(';', map "$_=".$r->$_(), qw( county state country ) );
284 $base_regions{$base_label}->{'tax'} += $x;
292 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
296 map $base_regions{$_},
297 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
304 'exempt_cust' => $exempt_cust,
305 'exempt_pkg' => $exempt_pkg,
306 'exempt_monthly' => $exempt_monthly,
307 'taxable' => $tot_taxable,
322 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
323 'county' => $r->county,
324 'country' => $r->country,
325 'tax' => { op=>'>', value=>0 },
331 #kludge to avoid "will not stay shared" warning
332 my $out = 'Out of taxable region(s)';
334 } elsif ( $r->taxname ) {
335 $label = $r->taxname;
336 # $regions{$label}->{'taxname'} = $label;
337 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
339 $label = $r->country;
340 $label = $r->state.", $label" if $r->state;
341 $label = $r->county." county, $label" if $r->county;
342 $label = "$label (". $r->taxclass. ")"
344 && $cgi->param('show_taxclasses')
345 && ! $opt{'no_taxclass'};
346 #$label = $r->taxname. " ($label)" if $r->taxname;
351 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
352 #to FS::Report or FS::Record or who the fuck knows where)
354 my( $r, $param, $sql ) = @_;
356 my $sth = dbh->prepare($sql) or die dbh->errstr;
357 $sth->execute( map $r->$_(), @$param )
358 or die "Unexpected error executing statement $sql: ". $sth->errstr;
359 $sth->fetchrow_arrayref->[0] || 0;
365 my $dateagentlink = "begin=$beginning;end=$ending";
366 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
367 if length($agentname);
368 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
369 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
372 <%= include("/elements/header.html", "$agentname Sales Tax Report - ".
374 ? time2str('%h %o %Y ', $beginning )
378 ( $ending == 4294967295
380 : time2str('%h %o %Y', $ending )
382 menubar( 'Main Menu'=>$p, )
386 <%= include('/elements/table-grid.html') %>
389 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
390 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
391 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
392 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Rate</TH>
393 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
394 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax owed</TH>
395 <% unless ( $cgi->param('show_taxclasses') ) { %>
396 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
400 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
401 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
402 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
403 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
404 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
405 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
406 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
407 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
408 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
411 <% my $bgcolor1 = '#eeeeee';
412 my $bgcolor2 = '#ffffff';
416 <% foreach my $region ( @regions ) {
418 if ( $bgcolor eq $bgcolor1 ) {
419 $bgcolor = $bgcolor2;
421 $bgcolor = $bgcolor1;
425 if ( $region->{'label'} ne 'Total' ) {
426 if ( $region->{'label'} eq $out ) {
429 $link = ';'. $region->{'url_param'};
439 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} %></TD>
440 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
441 <A HREF="<%= $baselink. $link %>;nottax=1"><%= $money_char %><%= sprintf('%.2f', $region->{'total'} ) %></A>
443 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
444 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
445 <A HREF="<%= $baselink. $link %>;nottax=1;cust_tax=Y"><%= $money_char %><%= sprintf('%.2f', $region->{'exempt_cust'} ) %></A>
447 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
448 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
449 <A HREF="<%= $baselink. $link %>;nottax=1;pkg_tax=Y"><%= $money_char %><%= sprintf('%.2f', $region->{'exempt_pkg'} ) %></A>
451 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
452 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
453 <A HREF="<%= $exemptlink. $link %>"><%= $money_char %><%= sprintf('%.2f', $region->{'exempt_monthly'} ) %></A>
455 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> = </B></FONT></TD>
456 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
457 <%= $money_char %><%= sprintf('%.2f', $region->{'taxable'} ) %></A>
459 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> X </B></FONT>' %></TD>
460 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right"><%= $region->{'rate'} %></TD>
461 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> = </B></FONT>' %></TD>
462 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
463 <%= $money_char %><%= sprintf('%.2f', $region->{'owed'} ) %>
465 <% unless ( $cgi->param('show_taxclasses') ) { %>
466 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
467 <A HREF="<%= $baselink. $link %>;istax=1"><%= $money_char %><%= sprintf('%.2f', $region->{'tax'} ) %></A>
477 <% if ( $cgi->param('show_taxclasses') ) { %>
480 <%= include('/elements/table-grid.html') %>
482 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
483 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
486 <% #some false laziness w/above
487 $bgcolor1 = '#eeeeee';
488 $bgcolor2 = '#ffffff';
489 foreach my $region ( @base_regions ) {
491 if ( $bgcolor eq $bgcolor1 ) {
492 $bgcolor = $bgcolor2;
494 $bgcolor = $bgcolor1;
498 #if ( $region->{'label'} ne 'Total' ) {
499 if ( $region->{'label'} eq $out ) {
502 $link = ';'. $region->{'url_param'};
508 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} %></TD>
509 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
510 <A HREF="<%= $baselink. $link %>;istax=1"><%= $money_char %><%= sprintf('%.2f', $region->{'tax'} ) %></A>
517 if ( $bgcolor eq $bgcolor1 ) {
518 $bgcolor = $bgcolor2;
520 $bgcolor = $bgcolor1;
525 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>">Total</TD>
526 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
527 <A HREF="<%= $baselink %>;istax=1"><%= $money_char %><%= sprintf('%.2f', $tax ) %></A>