6 $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/;
8 my $beginning = $1 ? str2time($1) : 0;
10 $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/;
12 my $ending = ( $1 ? str2time($1) : 4294880896 ) + 86399;
14 my($total, $exempt, $taxable, $tax) = ( 0, 0, 0, 0 );
15 my $out = 'Out of taxable region(s)';
18 qsearch('cust_main_county', {}, '',
19 "WHERE 0 < ( SELECT COUNT(*) FROM cust_main
20 WHERE ( cust_main.county = cust_main_county.county
21 OR cust_main_county.county = ''
22 OR cust_main_county.county IS NULL )
23 AND ( cust_main.state = cust_main_county.state
24 OR cust_main_county.state = ''
25 OR cust_main_county.state IS NULL )
26 AND ( cust_main.country = cust_main_county.country )
31 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
35 } elsif ( $r->taxname ) {
39 $label = $r->state.", $label" if $r->state;
40 $label = $r->county." county, $label" if $r->county;
47 JOIN cust_bill USING ( invnum )
48 JOIN cust_main USING ( custnum )
49 WHERE _date >= $beginning AND _date <= $ending
50 AND ( county = ? OR ? = '' )
51 AND ( state = ? OR ? = '' )
55 my $nottax = 'pkgnum != 0';
57 my $a = scalar_sql($r,
58 "SELECT SUM(setup+recur) $fromwhere AND $nottax"
61 $regions{$label}->{'total'} += $a;
63 foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i } qw( setup recur ) ) {
64 my $x = scalar_sql($r,
65 "SELECT SUM($e) $fromwhere AND $nottax"
68 $regions{$label}->{'exempt'} += $x;
71 foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i } qw( setup recur ) ) {
72 my $t = scalar_sql($r,
73 "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
76 $regions{$label}->{'taxable'} += $t;
78 my $x = scalar_sql($r,
79 "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
82 $regions{$label}->{'exempt'} += $x;
85 if ( defined($regions{$label}->{'rate'})
86 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
87 $regions{$label}->{'rate'} = 'variable';
89 $regions{$label}->{'rate'} = $r->tax.'%';
92 #match itemdesc if necessary!
93 my $named_tax = $r->taxname ? 'AND itemdesc = '. dbh->quote($r->taxname) : '';
94 my $x = scalar_sql($r,
95 "SELECT SUM(setup+recur) $fromwhere AND pkgnum = 0 $named_tax",
98 $regions{$label}->{'tax'} += $x;
100 $regions{$label}->{'label'} = $label;
105 my @regions = map $regions{$_},
106 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
113 'taxable' => $taxable,
120 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
121 #to FS::Report or FS::Record or who the fuck knows where)
125 my $sth = dbh->prepare($sql) or die dbh->errstr;
126 $sth->execute( map $r->$_(), qw( county county state state country ) )
127 or die "Unexpected error executing statement $sql: ". $sth->errstr;
128 $sth->fetchrow_arrayref->[0] || 0;
133 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
134 menubar( 'Main Menu'=>$p, ) ) %>
138 <TH COLSPAN=3>Sales</TH>
139 <TH ROWSPAN=2>Rate</TH>
140 <TH ROWSPAN=2>Tax</TH>
147 <% foreach my $region ( @regions ) { %>
149 <TD><%= $region->{'label'} %></TD>
150 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
151 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
152 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
153 <TD ALIGN="right"><%= $region->{'rate'} %></TD>
154 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>