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