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 $monthly_exempt_warning = 0;
46 my($total, $exempt, $taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
47 my $out = 'Out of taxable region(s)';
49 foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
50 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
52 my $label = getlabel($r);
53 $regions{$label}->{'label'} = $label;
55 my $fromwhere = $from_join_cust. $join_pkg. $where;
56 my @param = @base_param;
59 $fromwhere .= " AND ( taxclass = ? ) ";
60 push @param, 'taxclass';
63 my $nottax = 'pkgnum != 0';
65 my $a = scalar_sql($r, \@param,
66 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
69 $regions{$label}->{'total'} += $a;
71 foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i }
72 qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
73 my $x = scalar_sql($r, \@param,
74 "SELECT SUM($e) $fromwhere AND $nottax"
77 $regions{$label}->{'exempt'} += $x;
80 foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
81 qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
82 my $t = scalar_sql($r, \@param,
83 "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
86 my $x = scalar_sql($r, \@param,
87 "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
90 my($sday,$smon,$syear) = (localtime($beginning) )[ 3, 4, 5 ];
91 $monthly_exempt_warning=1 if $sday != 1 && $beginning;
92 $smon++; $syear+=1900;
94 my($eday,$emon,$eyear) = (localtime($ending) )[ 3, 4, 5 ];
95 $emon++; $eyear+=1900;
97 my $monthly_exemption = scalar_sql($r, [ 'taxnum' ],
98 "SELECT SUM(amount) FROM cust_tax_exempt where taxnum = ? ".
99 " AND ( year > $syear OR ( year = $syear and month >= $smon ) )".
100 " AND ( year < $eyear OR ( year = $eyear and month <= $emon ) )"
102 if ( $monthly_exemption ) {
103 $t -= $monthly_exemption;
104 $x += $monthly_exemption;
108 $regions{$label}->{'taxable'} += $t;
111 $regions{$label}->{'exempt'} += $x;
113 $owed += $t * ($r->tax/100);
114 $regions{$label}->{'owed'} += $t * ($r->tax/100);
118 if ( defined($regions{$label}->{'rate'})
119 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
120 $regions{$label}->{'rate'} = 'variable';
122 $regions{$label}->{'rate'} = $r->tax.'%';
127 my $taxwhere = "$from_join_cust $where";
128 my @taxparam = @base_param;
130 #foreach my $label ( keys %regions ) {
132 qsearch( 'cust_main_county',
134 'DISTINCT ON (country, state, county, taxname) *',
139 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
141 my $label = getlabel($r);
143 my $fromwhere = $join_pkg. $where;
144 my @param = @base_param;
146 #match itemdesc if necessary!
149 ? 'AND itemdesc = '. dbh->quote($r->taxname)
150 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
151 my $x = scalar_sql($r, \@taxparam,
152 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
153 "AND pkgnum = 0 $named_tax",
156 $regions{$label}->{'tax'} += $x;
161 my @regions = map $regions{$_},
162 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
169 'taxable' => $taxable,
180 if ( $r->tax == 0 ) {
181 #kludge to avoid "will not stay shared" warning
182 my $out = 'Out of taxable region(s)';
184 } elsif ( $r->taxname ) {
185 $label = $r->taxname;
186 # $regions{$label}->{'taxname'} = $label;
187 # push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
189 $label = $r->country;
190 $label = $r->state.", $label" if $r->state;
191 $label = $r->county." county, $label" if $r->county;
192 #$label = $r->taxname. " ($label)" if $r->taxname;
197 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
198 #to FS::Report or FS::Record or who the fuck knows where)
200 my( $r, $param, $sql ) = @_;
202 my $sth = dbh->prepare($sql) or die dbh->errstr;
203 $sth->execute( map $r->$_(), @$param )
204 or die "Unexpected error executing statement $sql: ". $sth->errstr;
205 $sth->fetchrow_arrayref->[0] || 0;
210 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
211 menubar( 'Main Menu'=>$p, ) ) %>
215 <TH COLSPAN=3>Sales</TH>
216 <TH ROWSPAN=2>Rate</TH>
217 <TH ROWSPAN=2>Tax owed</TH>
218 <TH ROWSPAN=2>Tax invoiced</TH>
225 <% foreach my $region ( @regions ) { %>
227 <TD><%= $region->{'label'} %></TD>
228 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
229 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
230 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
231 <TD ALIGN="right"><%= $region->{'rate'} %></TD>
232 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'owed'} ) %></TD>
233 <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>
239 <% if ( $monthly_exempt_warning ) { %>
241 Partial-month tax reports (except for current month) may not be correct due
242 to month-granularity tax exemption (usually "texas tax"). For an accurate
243 report, start on the first of a month and end on the last day of a month (or
244 leave blank for to now).