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