fix one-time card charging not pulling in exp date?
[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 $fromwhere = "
44     FROM cust_bill_pkg
45       JOIN cust_bill USING ( invnum ) 
46       JOIN cust_main USING ( custnum )
47       JOIN cust_pkg USING ( pkgnum )
48       JOIN part_pkg USING ( pkgpart )
49     WHERE _date >= $beginning AND _date <= $ending
50       AND ( county  = ? OR ? = '' )
51       AND ( state   = ? OR ? = '' )
52       AND ( country = ? )
53       AND ( taxclass = ? OR ? = '' )
54       AND payby != 'COMP'
55   ";
56   my $nottax = 'pkgnum != 0';
57
58   my $a = scalar_sql($r,
59     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
60   );
61   $total += $a;
62   $regions{$label}->{'total'} += $a;
63
64   foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i }
65                        qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
66     my $x = scalar_sql($r,
67       "SELECT SUM($e) $fromwhere AND $nottax"
68     );
69     $exempt += $x;
70     $regions{$label}->{'exempt'} += $x;
71   }
72
73   foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
74                        qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
75     my $t = scalar_sql($r,
76       "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
77     );
78     $taxable += $t;
79     $regions{$label}->{'taxable'} += $t;
80
81     my $x = scalar_sql($r,
82       "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
83     );
84     $exempt += $x;
85     $regions{$label}->{'exempt'} += $x;
86   }
87
88   if ( defined($regions{$label}->{'rate'})
89        && $regions{$label}->{'rate'} != $r->tax.'%' ) {
90     $regions{$label}->{'rate'} = 'variable';
91   } else {
92     $regions{$label}->{'rate'} = $r->tax.'%';
93   }
94
95   #match itemdesc if necessary!
96   my $named_tax = $r->taxname ? 'AND itemdesc = '. dbh->quote($r->taxname) : '';
97   my $x = scalar_sql($r,
98     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere ".
99     "AND pkgnum = 0 $named_tax",
100   );
101   $tax += $x;
102   $regions{$label}->{'tax'} += $x;
103
104   $regions{$label}->{'label'} = $label;
105
106 }
107
108 #ordering
109 my @regions = map $regions{$_},
110               sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
111               keys %regions;
112
113 push @regions, {
114   'label'     => 'Total',
115   'total'     => $total,
116   'exempt'    => $exempt,
117   'taxable'   => $taxable,
118   'rate'      => '',
119   'tax'       => $tax,
120 };
121
122 #-- 
123
124 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
125 #to FS::Report or FS::Record or who the fuck knows where)
126 sub scalar_sql {
127   my( $r, $sql ) = @_;
128   #warn "$sql\n";
129   my $sth = dbh->prepare($sql) or die dbh->errstr;
130   $sth->execute( map $r->$_(),
131                      qw( county county state state country taxclass taxclass ) )
132     or die "Unexpected error executing statement $sql: ". $sth->errstr;
133   $sth->fetchrow_arrayref->[0] || 0;
134 }
135
136 %>
137
138 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
139             menubar( 'Main Menu'=>$p, ) )               %>
140 <%= table() %>
141   <TR>
142     <TH ROWSPAN=2></TH>
143     <TH COLSPAN=3>Sales</TH>
144     <TH ROWSPAN=2>Rate</TH>
145     <TH ROWSPAN=2>Tax</TH>
146   </TR>
147   <TR>
148     <TH>Total</TH>
149     <TH>Non-taxable</TH>
150     <TH>Taxable</TH>
151   </TR>
152   <% foreach my $region ( @regions ) { %>
153     <TR>
154       <TD><%= $region->{'label'} %></TD>
155       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'total'} ) %></TD>
156       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'exempt'} ) %></TD>
157       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'taxable'} ) %></TD>
158       <TD ALIGN="right"><%= $region->{'rate'} %></TD>
159       <TD ALIGN="right">$<%= sprintf('%.2f', $region->{'tax'} ) %></TD>
160     </TR>
161   <% } %>
162
163 </TABLE>
164
165 </BODY>
166 </HTML>
167
168