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 WHERE _date >= $beginning AND _date <= $ending
24 AND ( county = ? OR ? = '' )
25 AND ( state = ? OR ? = '' )
29 my @base_param = qw( county county state state country );
32 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
33 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
34 die "agent not found" unless $agent;
35 $agentname = $agent->agent;
36 $where .= ' AND agentnum = '. $agent->agentnum;
40 WHERE 0 < ( SELECT COUNT(*) FROM cust_main
41 WHERE ( cust_main.county = cust_main_county.county
42 OR cust_main_county.county = ''
43 OR cust_main_county.county IS NULL )
44 AND ( cust_main.state = cust_main_county.state
45 OR cust_main_county.state = ''
46 OR cust_main_county.state IS NULL )
47 AND ( cust_main.country = cust_main_county.country )
52 my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
53 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0 );
54 my $out = 'Out of taxable region(s)';
56 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
57 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
59 my $label = getlabel($r);
60 $regions{$label}->{'label'} = $label;
61 $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
63 my @param = @base_param;
67 $mywhere .= " AND taxclass = ? ";
68 push @param, 'taxclass';
69 $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass
70 if $cgi->param('show_taxclasses');
73 my $fromwhere = $from_join_cust. $join_pkg. $mywhere. " AND payby != 'COMP' ";
75 # my $label = getlabel($r);
76 # $regions{$label}->{'label'} = $label;
78 my $nottax = 'pkgnum != 0';
80 ## calculate total for this region
82 my $t = scalar_sql($r, \@param,
83 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
86 $regions{$label}->{'total'} += $t;
88 ## calculate customer-exemption for this region
92 # my($taxable, $x_cust) = (0, 0);
93 # foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
94 # qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
95 # $taxable += scalar_sql($r, \@param,
96 # "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
99 # $x_cust += scalar_sql($r, \@param,
100 # "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
104 my $x_cust = scalar_sql($r, \@param,
105 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
106 $fromwhere AND $nottax AND tax = 'Y' "
109 $exempt_cust += $x_cust;
110 $regions{$label}->{'exempt_cust'} += $x_cust;
112 ## calculate package-exemption for this region
114 my $x_pkg = scalar_sql($r, \@param,
116 ( CASE WHEN part_pkg.setuptax = 'Y'
117 THEN cust_bill_pkg.setup
122 ( CASE WHEN part_pkg.recurtax = 'Y'
123 THEN cust_bill_pkg.recur
131 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
132 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
134 AND ( tax != 'Y' OR tax IS NULL )
137 $exempt_pkg += $x_pkg;
138 $regions{$label}->{'exempt_pkg'} += $x_pkg;
140 ## calculate monthly exemption (texas tax) for this region
142 # count up all the cust_tax_exempt_pkg records associated with
143 # the actual line items.
145 my $x_monthly = scalar_sql($r, \@param,
147 FROM cust_tax_exempt_pkg
148 JOIN cust_bill_pkg USING ( billpkgnum )
152 # if ( $x_monthly ) {
153 # #warn $r->taxnum(). ": $x_monthly\n";
154 # $taxable -= $x_monthly;
157 $exempt_monthly += $x_monthly;
158 $regions{$label}->{'exempt_monthly'} += $x_monthly;
160 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
162 $tot_taxable += $taxable;
163 $regions{$label}->{'taxable'} += $taxable;
165 $owed += $taxable * ($r->tax/100);
166 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
168 if ( defined($regions{$label}->{'rate'})
169 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
170 $regions{$label}->{'rate'} = 'variable';
172 $regions{$label}->{'rate'} = $r->tax.'%';
177 my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ";
178 my @taxparam = @base_param;
179 my %base_regions = ();
180 #foreach my $label ( keys %regions ) {
182 qsearch( 'cust_main_county',
184 'DISTINCT ON (country, state, county, taxname) *',
189 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
191 my $label = getlabel($r);
193 my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
194 my @param = @base_param;
196 #match itemdesc if necessary!
199 ? 'AND itemdesc = '. dbh->quote($r->taxname)
200 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
201 my $x = scalar_sql($r, \@taxparam,
202 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
203 "AND pkgnum = 0 $named_tax",
206 $regions{$label}->{'tax'} += $x;
208 if ( $cgi->param('show_taxclasses') ) {
209 my $base_label = getlabel($r, 'no_taxclass'=>1 );
210 $base_regions{$base_label}->{'label'} = $base_label;
211 $base_regions{$base_label}->{'url_param'} =
212 join(';', map "$_=".$r->$_(), qw( county state country ) );
213 $base_regions{$base_label}->{'tax'} += $x;
221 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
225 map $base_regions{$_},
226 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
233 'exempt_cust' => $exempt_cust,
234 'exempt_pkg' => $exempt_pkg,
235 'exempt_monthly' => $exempt_monthly,
236 'taxable' => $tot_taxable,
251 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
252 'county' => $r->county,
253 'country' => $r->country,
254 'tax' => { op=>'>', value=>0 },
260 #kludge to avoid "will not stay shared" warning
261 my $out = 'Out of taxable region(s)';
263 } elsif ( $r->taxname ) {
264 $label = $r->taxname;
265 # $regions{$label}->{'taxname'} = $label;
266 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
268 $label = $r->country;
269 $label = $r->state.", $label" if $r->state;
270 $label = $r->county." county, $label" if $r->county;
271 $label = "$label (". $r->taxclass. ")"
273 && $cgi->param('show_taxclasses')
274 && ! $opt{'no_taxclass'};
275 #$label = $r->taxname. " ($label)" if $r->taxname;
280 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
281 #to FS::Report or FS::Record or who the fuck knows where)
283 my( $r, $param, $sql ) = @_;
285 my $sth = dbh->prepare($sql) or die dbh->errstr;
286 $sth->execute( map $r->$_(), @$param )
287 or die "Unexpected error executing statement $sql: ". $sth->errstr;
288 $sth->fetchrow_arrayref->[0] || 0;
294 my $dateagentlink = "begin=$beginning;end=$ending";
295 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
296 if length($agentname);
297 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
298 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
301 <%= header( "$agentname Sales Tax Report - ".
303 ? time2str('%h %o %Y ', $beginning )
307 ( $ending == 4294967295
309 : time2str('%h %o %Y', $ending )
311 menubar( 'Main Menu'=>$p, )
315 <%= include('/elements/table-grid.html') %>
318 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
319 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
320 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
321 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Rate</TH>
322 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
323 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax owed</TH>
324 <% unless ( $cgi->param('show_taxclasses') ) { %>
325 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
329 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
330 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
331 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
332 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
333 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
334 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
335 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
336 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
337 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
340 <% my $bgcolor1 = '#eeeeee';
341 my $bgcolor2 = '#ffffff';
345 <% foreach my $region ( @regions ) {
347 if ( $bgcolor eq $bgcolor1 ) {
348 $bgcolor = $bgcolor2;
350 $bgcolor = $bgcolor1;
354 if ( $region->{'label'} ne 'Total' ) {
355 if ( $region->{'label'} eq $out ) {
358 $link = ';'. $region->{'url_param'};
368 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} %></TD>
369 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
370 <A HREF="<%= $baselink. $link %>;nottax=1"><%= $money_char %><%= sprintf('%.2f', $region->{'total'} ) %></A>
372 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
373 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
374 <A HREF="<%= $baselink. $link %>;nottax=1;cust_tax=Y"><%= $money_char %><%= sprintf('%.2f', $region->{'exempt_cust'} ) %></A>
376 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
377 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
378 <A HREF="<%= $baselink. $link %>;nottax=1;pkg_tax=Y"><%= $money_char %><%= sprintf('%.2f', $region->{'exempt_pkg'} ) %></A>
380 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> - </B></FONT></TD>
381 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
382 <A HREF="<%= $exemptlink. $link %>"><%= $money_char %><%= sprintf('%.2f', $region->{'exempt_monthly'} ) %></A>
384 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><FONT SIZE="+1"><B> = </B></FONT></TD>
385 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
386 <%= $money_char %><%= sprintf('%.2f', $region->{'taxable'} ) %></A>
388 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> X </B></FONT>' %></TD>
389 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right"><%= $region->{'rate'} %></TD>
390 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} eq 'Total' ? '' : '<FONT FACE="sans-serif" SIZE="+1"><B> = </B></FONT>' %></TD>
391 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
392 <%= $money_char %><%= sprintf('%.2f', $region->{'owed'} ) %>
394 <% unless ( $cgi->param('show_taxclasses') ) { %>
395 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
396 <A HREF="<%= $baselink. $link %>;istax=1"><%= $money_char %><%= sprintf('%.2f', $region->{'tax'} ) %></A>
406 <% if ( $cgi->param('show_taxclasses') ) { %>
409 <%= include('/elements/table-grid.html') %>
411 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
412 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
415 <% #some false laziness w/above
416 $bgcolor1 = '#eeeeee';
417 $bgcolor2 = '#ffffff';
418 foreach my $region ( @base_regions ) {
420 if ( $bgcolor eq $bgcolor1 ) {
421 $bgcolor = $bgcolor2;
423 $bgcolor = $bgcolor1;
427 #if ( $region->{'label'} ne 'Total' ) {
428 if ( $region->{'label'} eq $out ) {
431 $link = ';'. $region->{'url_param'};
437 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} %></TD>
438 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
439 <A HREF="<%= $baselink. $link %>;istax=1"><%= $money_char %><%= sprintf('%.2f', $region->{'tax'} ) %></A>
446 if ( $bgcolor eq $bgcolor1 ) {
447 $bgcolor = $bgcolor2;
449 $bgcolor = $bgcolor1;
454 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>">Total</TD>
455 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
456 <A HREF="<%= $baselink %>;istax=1"><%= $money_char %><%= sprintf('%.2f', $tax ) %></A>