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 $from_join_cust = "
16 JOIN cust_bill USING ( invnum )
17 JOIN cust_main USING ( custnum )
20 JOIN cust_pkg USING ( pkgnum )
21 JOIN part_pkg USING ( pkgpart )
24 WHERE _date >= $beginning AND _date <= $ending
25 AND ( county = ? OR ? = '' )
26 AND ( state = ? OR ? = '' )
30 my @base_param = qw( county county state state country );
33 WHERE 0 < ( SELECT COUNT(*) FROM cust_main
34 WHERE ( cust_main.county = cust_main_county.county
35 OR cust_main_county.county = ''
36 OR cust_main_county.county IS NULL )
37 AND ( cust_main.state = cust_main_county.state
38 OR cust_main_county.state = ''
39 OR cust_main_county.state IS NULL )
40 AND ( cust_main.country = cust_main_county.country )
45 my($total, $exempt, $taxable, $tax) = ( 0, 0, 0, 0 );
46 my $out = 'Out of taxable region(s)';
48 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
49 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
51 my $label = getlabel($r);
52 $regions{$label}->{'label'} = $label;
54 my $fromwhere = $from_join_cust. $join_pkg. $where;
55 my @param = @base_param;
58 $fromwhere .= " AND ( taxclass = ? ) ";
59 push @param, 'taxclass';
62 my $nottax = 'pkgnum != 0';
64 my $a = scalar_sql($r, \@param,
65 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
68 $regions{$label}->{'total'} += $a;
70 foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i }
71 qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
72 my $x = scalar_sql($r, \@param,
73 "SELECT SUM($e) $fromwhere AND $nottax"
76 $regions{$label}->{'exempt'} += $x;
79 foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
80 qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
81 my $t = scalar_sql($r, \@param,
82 "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
85 $regions{$label}->{'taxable'} += $t;
86 $owed += $t * ($r->tax/100);
87 $regions{$label}->{'owed'} += $t * ($r->tax/100);
89 my $x = scalar_sql($r, \@param,
90 "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
93 $regions{$label}->{'exempt'} += $x;
96 if ( defined($regions{$label}->{'rate'})
97 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
98 $regions{$label}->{'rate'} = 'variable';
100 $regions{$label}->{'rate'} = $r->tax.'%';
105 my $taxwhere = "$from_join_cust $where";
106 my @taxparam = @base_param;
108 #foreach my $label ( keys %regions ) {
110 qsearch( 'cust_main_county',
112 'DISTINCT ON (country, state, county, taxname) *',
117 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
119 my $label = getlabel($r);
121 my $fromwhere = $join_pkg. $where;
122 my @param = @base_param;
124 #match itemdesc if necessary!
127 ? 'AND itemdesc = '. dbh->quote($r->taxname)
128 : "AND itemdesc IS NULL or itemdesc = ''";
129 my $x = scalar_sql($r, \@taxparam,
130 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
131 "AND pkgnum = 0 $named_tax",
134 $regions{$label}->{'tax'} += $x;
139 my @regions = map $regions{$_},
140 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
147 'taxable' => $taxable,
156 if ( $r->tax == 0 ) {
158 } elsif ( $r->taxname ) {
159 $label = $r->taxname;
160 # $regions{$label}->{'taxname'} = $label;
161 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
163 $label = $r->country;
164 $label = $r->state.", $label" if $r->state;
165 $label = $r->county." county, $label" if $r->county;
166 #$label = $r->taxname. " ($label)" if $r->taxname;
171 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
172 #to FS::Report or FS::Record or who the fuck knows where)
174 my( $r, $param, $sql ) = @_;
176 my $sth = dbh->prepare($sql) or die dbh->errstr;
177 $sth->execute( map $r->$_(), @$param )
178 or die "Unexpected error executing statement $sql: ". $sth->errstr;
179 $sth->fetchrow_arrayref->[0] || 0;
184 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
185 menubar( 'Main Menu'=>$p, ) ) %>
189 <TH COLSPAN=3>Sales</TH>
190 <TH ROWSPAN=2>Rate</TH>
191 <TH ROWSPAN=2>Tax owed</TH>
192 <TH ROWSPAN=2>Tax invoiced</TH>
199 <% foreach my $region ( @regions ) { %>
201 <TD><%= $region->{'label'} %></TD>
202 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
203 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
204 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
205 <TD ALIGN="right"><%= $region->{'rate'} %></TD>
206 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'owed'} ) %></TD>
207 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>