This commit was generated by cvs2svn to compensate for changes in r8690,
[freeside.git] / httemplate / search / report_newtax.cgi
1 <% include("/elements/header.html", "$agentname Tax Report - ".
2               ( $beginning
3                   ? time2str('%h %o %Y ', $beginning )
4                   : ''
5               ).
6               'through '.
7               ( $ending == 4294967295
8                   ? 'now'
9                   : time2str('%h %o %Y', $ending )
10               )
11           )
12 %>
13
14 <% include('/elements/table-grid.html') %>
15
16   <TR>
17     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
18     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
19     <TH CLASS="grid" BGCOLOR="#cccccc">Tax collected</TH>
20     <TH CLASS="grid" BGCOLOR="#cccccc">&nbsp;&nbsp;&nbsp;&nbsp;</TH>
21     <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
22     <TH CLASS="grid" BGCOLOR="#cccccc">Tax credited</TH>
23   </TR>
24 % my $bgcolor1 = '#eeeeee';
25 % my $bgcolor2 = '#ffffff';
26 % my $bgcolor;
27 %
28 % foreach my $tax ( @taxes ) {
29 %
30 %   if ( $bgcolor eq $bgcolor1 ) {
31 %     $bgcolor = $bgcolor2;
32 %   } else {
33 %     $bgcolor = $bgcolor1;
34 %   }
35 %
36 %   my $link = '';
37 %   if ( $tax->{'label'} ne 'Total' ) {
38 %     $link = ';'. $tax->{'url_param'};
39 %   }
40 %
41
42     <TR>
43       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $tax->{'label'} %></TD>
44       <% $tax->{base} ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
45       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
46         <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $tax->{'tax'} ) %></A>
47       </TD>
48       <% !($tax->{base}) ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
49       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"></TD>
50       <% $tax->{base} ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
51       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
52         <A HREF="<% $baselink. $link %>;istax=1;iscredit=rate"><% $money_char %><% sprintf('%.2f', $tax->{'credit'} ) %></A>
53       </TD>
54       <% !($tax->{base}) ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
55     </TR>
56 % } 
57
58 </TABLE>
59
60 </BODY>
61 </HTML>
62 <%init>
63
64 die "access denied"
65   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
66
67 my $conf = new FS::Conf;
68 my $money_char = $conf->config('money_char') || '$';
69
70 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
71
72 my $join_cust = "
73     JOIN cust_bill USING ( invnum ) 
74     LEFT JOIN cust_main USING ( custnum )
75 ";
76
77 my $join_loc = "LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum )";
78 my $join_tax_loc = "LEFT JOIN tax_rate_location USING ( taxratelocationnum )";
79
80 my $addl_from = " $join_cust $join_loc $join_tax_loc "; 
81
82 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
83
84 my $agentname = '';
85 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
86   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
87   die "agent not found" unless $agent;
88   $agentname = $agent->agent;
89   $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
90 }
91
92 # my ( $location_sql, @location_param ) = FS::cust_pkg->location_sql;
93 # $where .= " AND $location_sql";
94 #my @taxparam = ( 'itemdesc', @location_param );
95 # now something along the lines of geocode matching ?
96 #$where .= FS::cust_pkg->_location_sql_where('cust_tax_location');;
97 my @taxparam = ( 'itemdesc', 'tax_rate_location.state', 'tax_rate_location.county', 'tax_rate_location.city', 'cust_bill_pkg_tax_rate_location.locationtaxid' );
98
99 my $select = 'DISTINCT itemdesc,locationtaxid,tax_rate_location.state,tax_rate_location.county,tax_rate_location.city';
100
101 my $tax = 0;
102 my $credit = 0;
103 my %taxes = ();
104 my %basetaxes = ();
105 foreach my $t (qsearch({ table     => 'cust_bill_pkg',
106                          select    => $select,
107                          hashref   => { pkgpart => 0 },
108                          addl_from => $addl_from,
109                          extra_sql => $where,
110                       })
111               )
112 {
113   my @params = map { my $f = $_; $f =~ s/.*\.//; $f } @taxparam;
114   my $label = join('~', map { $t->$_ } @params);
115   $label = 'Tax'. $label if $label =~ /^~/;
116   unless ( exists( $taxes{$label} ) ) {
117     my ($baselabel, @trash) = split /~/, $label;
118
119     $taxes{$label}->{'label'} = join(', ', split(/~/, $label) );
120     $taxes{$label}->{'url_param'} =
121       join(';', map { "$_=". uri_escape($t->$_) } @params);
122
123     my $taxwhere = "FROM cust_bill_pkg $addl_from $where AND payby != 'COMP' ".
124       "AND ". join( ' AND ', map { "( $_ = ? OR ? = '' AND $_ IS NULL)" } @taxparam );
125
126     my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
127               " $taxwhere AND cust_bill_pkg.pkgnum = 0";
128
129     my $x = scalar_sql($t, [ map { $_, $_ } @params ], $sql );
130     $tax += $x;
131     $taxes{$label}->{'tax'} += $x;
132
133     my $creditfrom = " JOIN cust_credit_bill_pkg USING (billpkgnum,billpkgtaxratelocationnum) ";
134     my $creditwhere = "FROM cust_bill_pkg $addl_from $creditfrom $where ".
135       "AND payby != 'COMP' ".
136       "AND ". join( ' AND ', map { "( $_ = ? OR ? = '' AND $_ IS NULL)" } @taxparam );
137
138     $sql = "SELECT SUM(cust_credit_bill_pkg.amount) ".
139            " $creditwhere AND cust_bill_pkg.pkgnum = 0";
140
141     my $y = scalar_sql($t, [ map { $_, $_ } @params ], $sql );
142     $credit += $y;
143     $taxes{$label}->{'credit'} += $y;
144
145     unless ( exists( $taxes{$baselabel} ) ) {
146
147       $basetaxes{$baselabel}->{'label'} = $baselabel;
148       $basetaxes{$baselabel}->{'url_param'} = "itemdesc=$baselabel";
149       $basetaxes{$baselabel}->{'base'} = 1;
150
151     }
152
153     $basetaxes{$baselabel}->{'tax'} += $x;
154     $basetaxes{$baselabel}->{'credit'} += $y;
155       
156   }
157
158   # calculate customer-exemption for this tax
159   # calculate package-exemption for this tax
160   # calculate monthly exemption (texas tax) for this tax
161   # count up all the cust_tax_exempt_pkg records associated with
162   # the actual line items.
163 }
164
165
166 #ordering
167 my @taxes = ();
168
169 foreach my $tax ( sort { $a cmp $b } keys %taxes ) {
170   my ($base, @trash) = split '~', $tax;
171   my $basetax = delete( $basetaxes{$base} );
172   if ($basetax) {
173     if ( $basetax->{tax} == $taxes{$tax}->{tax} ) {
174       $taxes{$tax}->{base} = 1;
175     } else {
176       push @taxes, $basetax;
177     }
178   }
179   push @taxes, $taxes{$tax};
180 }
181
182 push @taxes, {
183   'label'          => 'Total',
184   'url_param'      => '',
185   'tax'            => $tax,
186   'credit'         => $credit,
187   'base'           => 1,
188 };
189
190 #-- 
191
192 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
193 #to FS::Report or FS::Record or who the fuck knows where)
194 sub scalar_sql {
195   my( $r, $param, $sql ) = @_;
196   my $sth = dbh->prepare($sql) or die dbh->errstr;
197   $sth->execute( map $r->$_(), @$param )
198     or die "Unexpected error executing statement $sql: ". $sth->errstr;
199   $sth->fetchrow_arrayref->[0] || 0;
200 }
201
202 my $dateagentlink = "begin=$beginning;end=$ending";
203 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
204   if length($agentname);
205 my $baselink   = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
206
207 </%init>