enable CardFortress in test database, #71513
[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 invoiced</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="<% $creditlink. $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 #shit, all sorts of false laxiness w/tax_rate::geneate_liability_report
68
69 my $conf = new FS::Conf;
70 my $money_char = $conf->config('money_char') || '$';
71
72 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
73
74 my $join_cust = "
75     JOIN cust_bill USING ( invnum ) 
76     LEFT JOIN cust_main USING ( custnum )
77 ";
78
79 my $join_loc = "LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum )";
80 my $join_tax_loc = "LEFT JOIN tax_rate_location USING ( taxratelocationnum )";
81
82 my $addl_from = " $join_cust $join_loc $join_tax_loc "; 
83
84 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
85
86 my $agentname = '';
87 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
88   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
89   die "agent not found" unless $agent;
90   $agentname = $agent->agent;
91   $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
92 }
93
94 #my @taxparam = ( 'itemdesc', 'tax_rate_location.state', 'tax_rate_location.county', 'tax_rate_location.city', 'cust_bill_pkg_tax_rate_location.locationtaxid' );
95 my @taxparams = qw( city county state locationtaxid );
96 my @params = ('itemdesc', @taxparams);
97
98 my $select = 'DISTINCT itemdesc,locationtaxid,tax_rate_location.state,tax_rate_location.county,tax_rate_location.city';
99
100 my $tax = 0;
101 my $credit = 0;
102 my %taxes = ();
103 my %basetaxes = ();
104 foreach my $t (qsearch({ table     => 'cust_bill_pkg',
105                          select    => $select,
106                          hashref   => { pkgpart => 0 },
107                          addl_from => $addl_from,
108                          extra_sql => $where,
109                       })
110               )
111 {
112   #my @params = map { my $f = $_; $f =~ s/.*\.//; $f } @taxparam;
113   my $label = join('~', map { $t->$_ } @params);
114   $label = 'Tax'. $label if $label =~ /^~/;
115   unless ( exists( $taxes{$label} ) ) {
116     my ($baselabel, @trash) = split /~/, $label;
117
118     $taxes{$label}->{'label'} = join(', ', split(/~/, $label) );
119     $taxes{$label}->{'url_param'} =
120       join(';', map { "$_=". uri_escape($t->$_) } @params);
121
122     my $payby_itemdesc_loc = 
123       "    payby != 'COMP' ".
124       "AND ( itemdesc = ? OR ? = '' AND itemdesc IS NULL ) ".
125       "AND ". FS::tax_rate_location->location_sql( map { $_ => $t->$_ }
126                                                        @taxparams
127                                                  );
128
129     my $taxwhere =
130       "FROM cust_bill_pkg $addl_from $where AND $payby_itemdesc_loc";
131
132     my $sql = "SELECT SUM(amount) $taxwhere AND cust_bill_pkg.pkgnum = 0";
133
134     my $x = scalar_sql($t, [ 'itemdesc', 'itemdesc' ], $sql );
135     $tax += $x;
136     $taxes{$label}->{'tax'} += $x;
137
138     my $creditfrom =
139       "JOIN cust_credit_bill_pkg USING (billpkgnum,billpkgtaxratelocationnum)";
140     my $creditwhere =
141      "FROM cust_bill_pkg $addl_from $creditfrom $where AND $payby_itemdesc_loc";
142
143     $sql = "SELECT SUM(cust_credit_bill_pkg.amount) ".
144            " $creditwhere AND cust_bill_pkg.pkgnum = 0";
145
146     my $y = scalar_sql($t, [ 'itemdesc', 'itemdesc' ], $sql );
147     $credit += $y;
148     $taxes{$label}->{'credit'} += $y;
149
150     unless ( exists( $taxes{$baselabel} ) ) {
151
152       $basetaxes{$baselabel}->{'label'} = $baselabel;
153       $basetaxes{$baselabel}->{'url_param'} = "itemdesc=$baselabel";
154       $basetaxes{$baselabel}->{'base'} = 1;
155
156     }
157
158     $basetaxes{$baselabel}->{'tax'} += $x;
159     $basetaxes{$baselabel}->{'credit'} += $y;
160       
161   }
162
163   # calculate customer-exemption for this tax
164   # calculate package-exemption for this tax
165   # calculate monthly exemption (texas tax) for this tax
166   # count up all the cust_tax_exempt_pkg records associated with
167   # the actual line items.
168 }
169
170
171 #ordering
172 my @taxes = ();
173
174 foreach my $tax ( sort { $a cmp $b } keys %taxes ) {
175   my ($base, @trash) = split '~', $tax;
176   my $basetax = delete( $basetaxes{$base} );
177   if ($basetax) {
178     if ( $basetax->{tax} == $taxes{$tax}->{tax} ) {
179       $taxes{$tax}->{base} = 1;
180     } else {
181       push @taxes, $basetax;
182     }
183   }
184   push @taxes, $taxes{$tax};
185 }
186
187 push @taxes, {
188   'label'          => 'Total',
189   'url_param'      => '',
190   'tax'            => $tax,
191   'credit'         => $credit,
192   'base'           => 1,
193 };
194
195 #-- 
196
197 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
198 #to FS::Report or FS::Record or who the fuck knows where)
199 sub scalar_sql {
200   my( $r, $param, $sql ) = @_;
201   my $sth = dbh->prepare($sql) or die dbh->errstr;
202   $sth->execute( map $r->$_(), @$param )
203     or die "Unexpected error executing statement $sql: ". $sth->errstr;
204   $sth->fetchrow_arrayref->[0] || 0;
205 }
206
207 my $dateagentlink = "begin=$beginning;end=$ending";
208 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
209   if length($agentname);
210 my $baselink   = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
211 my $creditlink = $p. "search/cust_credit_bill_pkg.html?$dateagentlink";
212
213 </%init>