hopefully fix tax report for taxclass & named tax edge cases
[freeside.git] / httemplate / search / report_tax.cgi
1 <!-- mason kludge -->
2 <%
3
4 my $user = getotaker;
5
6 $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/;
7 my $pbeginning = $1;
8 my $beginning = $1 ? str2time($1) : 0;
9
10 $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/;
11 my $pending = $1;
12 my $ending = ( $1 ? str2time($1) : 4294880896 ) + 86399;
13
14 my($total, $exempt, $taxable, $tax) = ( 0, 0, 0, 0 );
15 my $out = 'Out of taxable region(s)';
16 my %regions;
17 foreach my $r (
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 )
27                        LIMIT 1
28                      )"
29          )
30 ) {
31   #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
32   my $label;
33   if ( $r->tax == 0 ) {
34     $label = $out;
35   } elsif ( $r->taxname ) {
36     $label = $r->taxname;
37   } else {
38     $label = $r->country;
39     $label = $r->state.", $label" if $r->state;
40     $label = $r->county." county, $label" if $r->county;
41   }
42
43   my $join_pkg = "
44       JOIN cust_pkg USING ( pkgnum )
45       JOIN part_pkg USING ( pkgpart )
46   ";
47
48   my $where = "
49     WHERE _date >= $beginning AND _date <= $ending
50       AND ( county  = ? OR ? = '' )
51       AND ( state   = ? OR ? = '' )
52       AND ( country = ? )
53       AND payby != 'COMP'
54   ";
55
56   my $taxwhere = my $fromwhere = "
57     FROM cust_bill_pkg
58       JOIN cust_bill USING ( invnum ) 
59       JOIN cust_main USING ( custnum )
60   ";
61
62   $fromwhere .= $join_pkg. $where;
63   $taxwhere .= $where;
64
65   my @taxparam = my @param = qw( county county state state country );
66
67 #  my $num_others = 
68 #    scalar_sql( $r, [qw( country state state county county taxname taxname )], 
69 #      "SELECT COUNT(*) FROM cust_main_county
70 #         WHERE country = ?
71 #         AND ( state = ? OR ( state IS NULL AND ? = '' ) )
72 #         AND ( county = ? OR ( county IS NULL AND ? = '' ) )
73 #         AND ( taxname = ? OR ( taxname IS NULL AND ? = '' ) ) "
74 #    );
75 #
76 #  die "didn't even find self?" unless $num_others;
77
78   if ( $r->taxclass ) {
79     $fromwhere .= " AND ( taxclass = ?  ) ";
80     push @param, 'taxclass';
81   }
82
83   my $nottax = 'pkgnum != 0';
84
85   my $a = scalar_sql($r, \@param,
86     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
87   );
88   $total += $a;
89   $regions{$label}->{'total'} += $a;
90
91   foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i }
92                        qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
93     my $x = scalar_sql($r, \@param,
94       "SELECT SUM($e) $fromwhere AND $nottax"
95     );
96     $exempt += $x;
97     $regions{$label}->{'exempt'} += $x;
98   }
99
100   foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
101                        qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
102     my $t = scalar_sql($r, \@param, 
103       "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
104     );
105     $taxable += $t;
106     $regions{$label}->{'taxable'} += $t;
107
108     my $x = scalar_sql($r, \@param, 
109       "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
110     );
111     $exempt += $x;
112     $regions{$label}->{'exempt'} += $x;
113   }
114
115   if ( defined($regions{$label}->{'rate'})
116        && $regions{$label}->{'rate'} != $r->tax.'%' ) {
117     $regions{$label}->{'rate'} = 'variable';
118   } else {
119     $regions{$label}->{'rate'} = $r->tax.'%';
120   }
121
122   #match itemdesc if necessary!
123   my $named_tax = $r->taxname ? 'AND itemdesc = '. dbh->quote($r->taxname) : '';
124   my $x = scalar_sql($r, \@taxparam,
125     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
126     "AND pkgnum = 0 $named_tax",
127   );
128   $tax += $x;
129   $regions{$label}->{'tax'} += $x;
130
131   $regions{$label}->{'label'} = $label;
132
133 }
134
135 #ordering
136 my @regions = map $regions{$_},
137               sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
138               keys %regions;
139
140 push @regions, {
141   'label'     => 'Total',
142   'total'     => $total,
143   'exempt'    => $exempt,
144   'taxable'   => $taxable,
145   'rate'      => '',
146   'tax'       => $tax,
147 };
148
149 #-- 
150
151 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
152 #to FS::Report or FS::Record or who the fuck knows where)
153 sub scalar_sql {
154   my( $r, $param, $sql ) = @_;
155   #warn "$sql\n";
156   my $sth = dbh->prepare($sql) or die dbh->errstr;
157   $sth->execute( map $r->$_(), @$param )
158     or die "Unexpected error executing statement $sql: ". $sth->errstr;
159   $sth->fetchrow_arrayref->[0] || 0;
160 }
161
162 %>
163
164 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
165             menubar( 'Main Menu'=>$p, ) )               %>
166 <%= table() %>
167   <TR>
168     <TH ROWSPAN=2></TH>
169     <TH COLSPAN=3>Sales</TH>
170     <TH ROWSPAN=2>Rate</TH>
171     <TH ROWSPAN=2>Tax</TH>
172   </TR>
173   <TR>
174     <TH>Total</TH>
175     <TH>Non-taxable</TH>
176     <TH>Taxable</TH>
177   </TR>
178   <% foreach my $region ( @regions ) { %>
179     <TR>
180       <TD><%= $region->{'label'} %></TD>
181       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
182       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
183       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
184       <TD ALIGN="right"><%= $region->{'rate'} %></TD>
185       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>
186     </TR>
187   <% } %>
188
189 </TABLE>
190
191 </BODY>
192 </HTML>
193
194