simple reporting for new tax system
[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">Tax collected</TH>
19   </TR>
20 % my $bgcolor1 = '#eeeeee';
21 % my $bgcolor2 = '#ffffff';
22 % my $bgcolor;
23 %
24 % foreach my $tax ( @taxes ) {
25 %
26 %   if ( $bgcolor eq $bgcolor1 ) {
27 %     $bgcolor = $bgcolor2;
28 %   } else {
29 %     $bgcolor = $bgcolor1;
30 %   }
31 %
32 %   my $link = '';
33 %   if ( $tax->{'label'} ne 'Total' ) {
34 %     $link = ';'. $tax->{'url_param'};
35 %   }
36 %
37
38     <TR>
39       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $tax->{'label'} %></TD>
40       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
41         <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $tax->{'tax'} ) %></A>
42       </TD>
43     </TR>
44 % } 
45
46 </TABLE>
47
48 </BODY>
49 </HTML>
50 <%init>
51
52 die "access denied"
53   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
54
55 my $conf = new FS::Conf;
56 my $money_char = $conf->config('money_char') || '$';
57
58 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
59
60 my $join_cust = "
61     JOIN cust_bill USING ( invnum ) 
62     LEFT JOIN cust_main USING ( custnum )
63 ";
64 my $from_join_cust = "
65     FROM cust_bill_pkg
66     $join_cust
67 "; 
68
69 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
70
71 my $agentname = '';
72 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
73   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
74   die "agent not found" unless $agent;
75   $agentname = $agent->agent;
76   $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
77 }
78
79 my $tax = 0;
80 my %taxes = ();
81 foreach my $t (qsearch({ table     => 'cust_bill_pkg',
82                          hashref   => { pkgpart => 0 },
83                          addl_from => $join_cust,
84                          extra_sql => $where,
85                       })
86               )
87 {
88   #warn $t->itemdesc. "\n";
89
90   my $label = $t->itemdesc;
91   $label ||= 'Tax';
92   $taxes{$label}->{'label'} = $label;
93   $taxes{$label}->{'url_param'} = "itemdesc=$label";
94
95   # calculate total for this tax 
96   # calculate customer-exemption for this tax
97   # calculate package-exemption for this tax
98   # calculate monthly exemption (texas tax) for this tax
99   # count up all the cust_tax_exempt_pkg records associated with
100   # the actual line items.
101 }
102
103
104 foreach my $t (qsearch({ table     => 'cust_bill_pkg',
105                          select    => 'DISTINCT itemdesc',
106                          hashref   => { pkgpart => 0 },
107                          addl_from => $join_cust,
108                          extra_sql => $where,
109                       })
110               )
111 {
112
113   my $label = $t->itemdesc;
114   $label ||= 'Tax';
115   my @taxparam = ( 'itemdesc' );
116   my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ".
117     "AND itemdesc = ?" ;
118
119   my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
120             " $taxwhere AND pkgnum = 0";
121
122   my $x = scalar_sql($t, \@taxparam, $sql );
123   $tax += $x;
124   $taxes{$label}->{'tax'} += $x;
125
126 }
127
128 #ordering
129 my @taxes =
130   map $taxes{$_},
131   sort { ($b cmp $a) }
132   keys %taxes;
133
134 push @taxes, {
135   'label'          => 'Total',
136   'url_param'      => '',
137   'tax'            => $tax,
138 };
139
140 #-- 
141
142 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
143 #to FS::Report or FS::Record or who the fuck knows where)
144 sub scalar_sql {
145   my( $r, $param, $sql ) = @_;
146   #warn "$sql\n";
147   my $sth = dbh->prepare($sql) or die dbh->errstr;
148   $sth->execute( map $r->$_(), @$param )
149     or die "Unexpected error executing statement $sql: ". $sth->errstr;
150   $sth->fetchrow_arrayref->[0] || 0;
151 }
152
153 my $dateagentlink = "begin=$beginning;end=$ending";
154 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
155   if length($agentname);
156 my $baselink   = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
157
158 </%init>