5 my $user = $FS::UID::user; #dumb 1.4 8-char workaround
7 $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/;
9 my $beginning = str2time($1) || 0;
11 $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/;
13 my $ending = ( str2time($1) || 4294880896 ) + 86399;
15 my($total, $exempt, $taxable, $tax) = ( 0, 0, 0, 0 );
16 my $out = 'Out of taxable region(s)';
18 foreach my $r ( qsearch('cust_main_county', {}) ) {
22 } elsif ( $r->taxname ) {
26 $label = $r->state.", $label" if $r->state;
27 $label = $r->county." county, $label" if $r->county;
34 JOIN cust_bill USING ( invnum )
35 JOIN cust_main USING ( custnum )
36 WHERE _date >= $beginning AND _date <= $ending
37 AND ( county = ? OR ( ? = '' AND county IS NULL ) )
38 AND ( state = ? OR ( ? = '' AND state IS NULL ) )
39 AND ( country = ? OR ( ? = '' AND country IS NULL ) )
41 my $nottax = 'pkgnum != 0';
43 my $a = scalar_sql($r,
44 "SELECT SUM(setup+recur) $fromwhere AND $nottax"
47 $regions{$label}->{'total'} += $a;
49 foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i } qw( setup recur ) ) {
50 my $x = scalar_sql($r,
51 "SELECT SUM($e) $fromwhere AND $nottax"
54 $regions{$label}->{'exempt'} += $x;
57 foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i } qw( setup recur ) ) {
58 my $x = scalar_sql($r,
59 "SELECT SUM($e) $fromwhere AND $nottax"
62 $regions{$label}->{'taxable'} += $x;
65 if ( defined($regions{$label}->{'rate'})
66 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
67 $regions{$label}->{'rate'} = 'variable';
69 $regions{$label}->{'rate'} = $r->tax.'%';
72 #match itemdesc if necessary!
73 my $named_tax = $r->taxname ? 'AND itemdesc = '. dbh->quote($r->taxname) : '';
74 my $x = scalar_sql($r,
75 "SELECT SUM(setup+recur) $fromwhere AND pkgnum = 0 $named_tax",
78 $regions{$label}->{'tax'} += $x;
80 $regions{$label}->{'label'} = $label;
85 my @regions = map $regions{$_},
86 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
93 'taxable' => $taxable,
100 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
101 #to FS::Report or FS::Record or who the fuck knows where)
104 my $sth = dbh->prepare($sql) or die dbh->errstr;
105 $sth->execute( map $r->$_(), map { $_, $_ } qw( county state country ) )
106 or die "Unexpected error executing statement $sql: ". $sth->errstr;
107 $sth->fetchrow_arrayref->[0] || 0;
112 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
113 menubar( 'Main Menu'=>$p, ) ) %>
117 <TH COLSPAN=3>Sales</TH>
118 <TH ROWSPAN=2>Rate</TH>
119 <TH ROWSPAN=2>Tax</TH>
126 <% foreach my $region ( @regions ) { %>
128 <TD><%= $region->{'label'} %></TD>
129 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
130 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
131 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
132 <TD ALIGN="right"><%= $region->{'rate'} %></TD>
133 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>