6 $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/;
8 my $beginning = str2time($1) || 0;
10 $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/;
12 my $ending = ( str2time($1) || 4294880896 ) + 86399;
14 my($total, $exempt, $taxable, $tax) = ( 0, 0, 0, 0 );
15 my $out = 'Out of taxable region(s)';
17 foreach my $r ( qsearch('cust_main_county', {}) ) {
21 } elsif ( $r->taxname ) {
25 $label = $r->state.", $label" if $r->state;
26 $label = $r->county." county, $label" if $r->county;
33 JOIN cust_bill USING ( invnum )
34 JOIN cust_main USING ( custnum )
35 WHERE _date >= $beginning AND _date <= $ending
36 AND ( county = ? OR ( ? = '' AND county IS NULL ) )
37 AND ( state = ? OR ( ? = '' AND state IS NULL ) )
38 AND ( country = ? OR ( ? = '' AND country IS NULL ) )
40 my $nottax = 'pkgnum != 0';
42 my $a = scalar_sql($r,
43 "SELECT SUM(setup+recur) $fromwhere AND $nottax"
46 $regions{$label}->{'total'} += $a;
48 foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i } qw( setup recur ) ) {
49 my $x = scalar_sql($r,
50 "SELECT SUM($e) $fromwhere AND $nottax"
53 $regions{$label}->{'exempt'} += $x;
56 foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i } qw( setup recur ) ) {
57 my $x = scalar_sql($r,
58 "SELECT SUM($e) $fromwhere AND $nottax"
61 $regions{$label}->{'taxable'} += $x;
64 if ( defined($regions{$label}->{'rate'})
65 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
66 $regions{$label}->{'rate'} = 'variable';
68 $regions{$label}->{'rate'} = $r->tax.'%';
71 #match itemdesc if necessary!
72 my $named_tax = $r->taxname ? 'AND itemdesc = '. dbh->quote($r->taxname) : '';
73 my $x = scalar_sql($r,
74 "SELECT SUM(setup+recur) $fromwhere AND pkgnum = 0 $named_tax",
77 $regions{$label}->{'tax'} += $x;
79 $regions{$label}->{'label'} = $label;
84 my @regions = map $regions{$_},
85 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
92 'taxable' => $taxable,
99 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
100 #to FS::Report or FS::Record or who the fuck knows where)
103 my $sth = dbh->prepare($sql) or die dbh->errstr;
104 $sth->execute( map $r->$_(), map { $_, $_ } qw( county state country ) )
105 or die "Unexpected error executing statement $sql: ". $sth->errstr;
106 $sth->fetchrow_arrayref->[0] || 0;
111 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
112 menubar( 'Main Menu'=>$p, ) ) %>
116 <TH COLSPAN=3>Sales</TH>
117 <TH ROWSPAN=2>Rate</TH>
118 <TH ROWSPAN=2>Tax</TH>
125 <% foreach my $region ( @regions ) { %>
127 <TD><%= $region->{'label'} %></TD>
128 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
129 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
130 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
131 <TD ALIGN="right"><%= $region->{'rate'} %></TD>
132 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>