ed7f9ffb853fdc39d44b359a2bc71ab0176accff
[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 = str2time($1) || 0;
9
10 $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/;
11 my $pending = $1;
12 my $ending = ( 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 ( qsearch('cust_main_county', {}) ) {
18   my $label;
19   if ( $r->tax == 0 ) {
20     $label = $out;
21   } elsif ( $r->taxname ) {
22     $label = $r->taxname;
23   } else {
24     $label = $r->country;
25     $label = $r->state.", $label" if $r->state;
26     $label = $r->county." county, $label" if $r->county;
27   }
28
29   #match taxclass too?
30
31   my $fromwhere = "
32     FROM cust_bill_pkg
33       JOIN cust_bill USING ( invnum ) 
34       JOIN cust_main USING ( custnum )
35     WHERE _date >= $beginning AND _date <= $ending
36       AND county = ? AND state = ? AND country = ?
37   ";
38   my $nottax = 'pkgnum != 0';
39
40   my $a = scalar_sql($r,
41     "SELECT SUM(setup+recur) $fromwhere AND $nottax"
42   );
43   $total += $a;
44   $regions{$label}->{'total'} += $a;
45
46   foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i } qw( setup recur ) ) {
47     my $x = scalar_sql($r,
48       "SELECT SUM($e) $fromwhere AND $nottax"
49     );
50     $exempt += $x;
51     $regions{$label}->{'exempt'} += $x;
52   }
53
54   foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i } qw( setup recur ) ) {
55     my $x = scalar_sql($r,
56       "SELECT SUM($e) $fromwhere AND $nottax"
57     );
58     $taxable += $x;
59     $regions{$label}->{'taxable'} += $x;
60   }
61
62   if ( defined($regions{$label}->{'rate'})
63        && $regions{$label}->{'rate'} != $r->tax.'%' ) {
64     $regions{$label}->{'rate'} = 'variable';
65   } else {
66     $regions{$label}->{'rate'} = $r->tax.'%';
67   }
68
69   #match itemdesc if necessary!
70   my $named_tax = $r->taxname ? 'AND itemdesc = '. dbh->quote($r->taxname) : '';
71   my $x = scalar_sql($r,
72     "SELECT SUM(setup+recur) $fromwhere AND pkgnum = 0 $named_tax",
73   );
74   $tax += $x;
75   $regions{$label}->{'tax'} += $x;
76
77   $regions{$label}->{'label'} = $label;
78
79 }
80
81 #ordering
82 my @regions = map $regions{$_},
83               sort { ( ($b eq $out) cmp ($a eq $out) ) || ($a cmp $b) }
84               keys %regions;
85
86 push @regions, {
87   'label'     => 'Total',
88   'total'     => $total,
89   'exempt'    => $exempt,
90   'taxable'   => $taxable,
91   'rate'      => '',
92   'tax'       => $tax,
93 };
94
95 #-- 
96
97 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
98 #to FS::Report or FS::Record or who the fuck knows where)
99 sub scalar_sql {
100   my( $r, $sql ) = @_;
101   my $sth = dbh->prepare($sql) or die dbh->errstr;
102   $sth->execute( map $r->$_(), qw( county state country ) )
103     or die "Unexpected error executing statement $sql: ". $sth->errstr;
104   $sth->fetchrow_arrayref->[0] || 0;
105 }
106
107 %>
108
109 <%= header( "Sales Tax Report - $pbeginning through ".($pending||'now'),
110             menubar( 'Main Menu'=>$p, ) )               %>
111 <%= table() %>
112   <TR>
113     <TH ROWSPAN=2></TH>
114     <TH COLSPAN=3>Sales</TH>
115     <TH ROWSPAN=2>Rate</TH>
116     <TH ROWSPAN=2>Tax</TH>
117   </TR>
118   <TR>
119     <TH>Total</TH>
120     <TH>Non-taxable</TH>
121     <TH>Taxable</TH>
122   </TR>
123   <% foreach my $region ( @regions ) { %>
124     <TR>
125       <TD><%= $region->{'label'} %></TD>
126       <TD ALIGN="right">$<%= $region->{'total'} %></TD>
127       <TD ALIGN="right">$<%= $region->{'exempt'} %></TD>
128       <TD ALIGN="right">$<%= $region->{'taxable'} %></TD>
129       <TD ALIGN="right"><%= $region->{'rate'} %></TD>
130       <TD ALIGN="right">$<%= $region->{'tax'} %></TD>
131     </TR>
132   <% } %>
133
134 </TABLE>
135
136 </BODY>
137 </HTML>
138
139