5 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
9 JOIN cust_bill USING ( invnum )
10 JOIN cust_main USING ( custnum )
13 JOIN cust_pkg USING ( pkgnum )
14 JOIN part_pkg USING ( pkgpart )
17 WHERE _date >= $beginning AND _date <= $ending
18 AND ( county = ? OR ? = '' )
19 AND ( state = ? OR ? = '' )
23 my @base_param = qw( county county state state country );
26 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
27 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
28 die "agent not found" unless $agent;
29 $agentname = $agent->agent;
30 $where .= ' AND agentnum = '. $agent->agentnum;
34 WHERE 0 < ( SELECT COUNT(*) FROM cust_main
35 WHERE ( cust_main.county = cust_main_county.county
36 OR cust_main_county.county = ''
37 OR cust_main_county.county IS NULL )
38 AND ( cust_main.state = cust_main_county.state
39 OR cust_main_county.state = ''
40 OR cust_main_county.state IS NULL )
41 AND ( cust_main.country = cust_main_county.country )
46 my $monthly_exempt_warning = 0;
47 my($total, $taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
48 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0 );
49 my $out = 'Out of taxable region(s)';
51 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
52 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
54 my $label = getlabel($r);
55 $regions{$label}->{'label'} = $label;
57 $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
59 my $fromwhere = $from_join_cust. $join_pkg. $where;
60 my @param = @base_param;
63 $fromwhere .= " AND taxclass = ? ";
64 push @param, 'taxclass';
65 $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass;
68 # my $label = getlabel($r);
69 # $regions{$label}->{'label'} = $label;
71 my $nottax = 'pkgnum != 0';
73 ## calculate total for this region
75 my $t = scalar_sql($r, \@param,
76 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
79 $regions{$label}->{'total'} += $t;
81 ## calculate package-exemption for this region
83 foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i }
84 qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
85 my $x = scalar_sql($r, \@param,
86 "SELECT SUM($e) $fromwhere AND $nottax"
89 $regions{$label}->{'exempt_pkg'} += $x;
92 ## calculate customer-exemption for this region
94 my($t, $x_cust) = (0, 0);
95 foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
96 qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
97 $t += scalar_sql($r, \@param,
98 "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
101 $x_cust += scalar_sql($r, \@param,
102 "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
106 $exempt_cust += $x_cust;
107 $regions{$label}->{'exempt_cust'} += $x_cust;
109 ## calculate monthly exemption (texas tax) for this region
111 my($sday,$smon,$syear) = (localtime($beginning) )[ 3, 4, 5 ];
112 $monthly_exempt_warning=1 if $sday != 1 && $beginning;
113 $smon++; $syear+=1900;
115 my $eending = ( $ending == 4294967295 ) ? time : $ending;
116 my($eday,$emon,$eyear) = (localtime($eending) )[ 3, 4, 5 ];
117 $emon++; $eyear+=1900;
119 my $x_monthly = scalar_sql($r, [ 'taxnum' ],
120 "SELECT SUM(amount) FROM cust_tax_exempt where taxnum = ? ".
121 " AND ( year > $syear OR ( year = $syear and month >= $smon ) )".
122 " AND ( year < $eyear OR ( year = $eyear and month <= $emon ) )"
125 warn $r->taxnum(). ": $x_monthly\n";
129 $exempt_monthly += $x_monthly;
130 $regions{$label}->{'exempt_monthly'} += $x_monthly;
133 $regions{$label}->{'taxable'} += $t;
135 $owed += $t * ($r->tax/100);
136 $regions{$label}->{'owed'} += $t * ($r->tax/100);
138 if ( defined($regions{$label}->{'rate'})
139 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
140 $regions{$label}->{'rate'} = 'variable';
142 $regions{$label}->{'rate'} = $r->tax.'%';
147 my $taxwhere = "$from_join_cust $where";
148 my @taxparam = @base_param;
150 #foreach my $label ( keys %regions ) {
152 qsearch( 'cust_main_county',
154 'DISTINCT ON (country, state, county, taxname) *',
159 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
161 my $label = getlabel($r);
163 my $fromwhere = $join_pkg. $where;
164 my @param = @base_param;
166 #match itemdesc if necessary!
169 ? 'AND itemdesc = '. dbh->quote($r->taxname)
170 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
171 my $x = scalar_sql($r, \@taxparam,
172 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
173 "AND pkgnum = 0 $named_tax",
176 $regions{$label}->{'tax'} += $x;
181 my @regions = map $regions{$_},
182 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
189 'exempt_cust' => $exempt_cust,
190 'exempt_pkg' => $exempt_pkg,
191 'exempt_monthly' => $exempt_monthly,
192 'taxable' => $taxable,
206 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
207 'county' => $r->county,
208 'country' => $r->country,
209 'tax' => { op=>'>', value=>0 },
215 #kludge to avoid "will not stay shared" warning
216 my $out = 'Out of taxable region(s)';
218 } elsif ( $r->taxname ) {
219 $label = $r->taxname;
220 # $regions{$label}->{'taxname'} = $label;
221 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
223 $label = $r->country;
224 $label = $r->state.", $label" if $r->state;
225 $label = $r->county." county, $label" if $r->county;
226 $label = "$label (". $r->taxclass. ")" if $r->taxclass;
227 #$label = $r->taxname. " ($label)" if $r->taxname;
232 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
233 #to FS::Report or FS::Record or who the fuck knows where)
235 my( $r, $param, $sql ) = @_;
237 my $sth = dbh->prepare($sql) or die dbh->errstr;
238 $sth->execute( map $r->$_(), @$param )
239 or die "Unexpected error executing statement $sql: ". $sth->errstr;
240 $sth->fetchrow_arrayref->[0] || 0;
247 my $baselink = $p. "search/cust_bill_pkg.cgi?begin=$beginning;end=$ending";
252 <%= header( "$agentname Sales Tax Report - ".
253 time2str('%h %o %Y through ', $beginning ).
254 ( $cgi->param('ending') == 4294967295
256 : time2str('%h %o %Y', $ending )
258 menubar( 'Main Menu'=>$p, )
262 <%= include('/elements/table-grid.html') %>
265 <TH CLASS="grid" BGCOLOR="#cccccc"ROWSPAN=2></TH>
266 <TH CLASS="grid" BGCOLOR="#cccccc"COLSPAN=5>Sales</TH>
267 <TH CLASS="grid" BGCOLOR="#cccccc"ROWSPAN=2>Rate</TH>
268 <TH CLASS="grid" BGCOLOR="#cccccc"ROWSPAN=2>Tax owed</TH>
269 <TH CLASS="grid" BGCOLOR="#cccccc"ROWSPAN=2>Tax invoiced</TH>
272 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
273 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
274 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
275 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
276 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
279 <% my $bgcolor1 = '#eeeeee';
280 my $bgcolor2 = '#ffffff';
284 <% foreach my $region ( @regions ) {
286 if ( $bgcolor eq $bgcolor1 ) {
287 $bgcolor = $bgcolor2;
289 $bgcolor = $bgcolor1;
292 my $link = $baselink;
293 if ( $region->{'label'} ne 'Total' ) {
294 if ( $region->{'label'} eq $out ) {
297 $link .= ';'. $region->{'url_param'};
302 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $region->{'label'} %></TD>
303 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
304 <A HREF="<%= $link %>;nottax=1">$<%= sprintf('%.2f', $region->{'total'} ) %></A>
306 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
307 <A HREF="<%= $link %>;nottax=1;cust_tax=Y">$<%= sprintf('%.2f', $region->{'exempt_cust'} ) %></A>
309 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
310 <A HREF="<%= $link %>;nottax=1;pkg_tax=Y">$<%= sprintf('%.2f', $region->{'exempt_pkg'} ) %></A>
312 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
313 $<%= sprintf('%.2f', $region->{'exempt_monthly'} ) %></A>
315 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
316 $<%= sprintf('%.2f', $region->{'taxable'} ) %></A>
318 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right"><%= $region->{'rate'} %></TD>
319 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
320 $<%= sprintf('%.2f', $region->{'owed'} ) %>
322 <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>" ALIGN="right">
323 <A HREF="<%= $link %>;istax=1">$<%= sprintf('%.2f', $region->{'tax'} ) %></A>
330 <% if ( $monthly_exempt_warning ) { %>
332 Partial-month tax reports (except for current month) may not be correct due
333 to month-granularity tax exemption (usually "texas tax"). For an accurate
334 report, start on the first of a month and end on the last day of a month (or
335 leave blank for to now).