2b531ea466e2342ae5b82561c38f92b2df9cb46d
[freeside.git] / httemplate / search / report_tax.cgi
1 <& /elements/header.html, $report->title &>
2 <TD ALIGN="right">
3 Download full results<BR>
4 as <A HREF="<% $p.'search/report_tax-xls.cgi?'.$cgi->query_string%>">Excel spreadsheet</A>
5 </TD>
6
7 <STYLE type="text/css">
8 TD.sectionhead {
9   background-color: #777777;
10   color: #ffffff;
11   font-weight: bold;
12   text-align: left;
13 }
14 .grid TH { background-color: #cccccc; padding: 0px 3px 2px }
15 .row0 TD { background-color: #eeeeee; padding: 0px 3px 2px; text-align: right}
16 .row1 TD { background-color: #ffffff; padding: 0px 3px 2px; text-align: right}
17 TD.rowhead { font-weight: bold; text-align: left; padding: 0px 3px }
18 .bigmath { font-size: large; font-weight: bold; font: sans-serif; text-align: center }
19 .total { font-style: italic }
20 </STYLE>
21
22 <& /elements/table-grid.html &>
23   <THEAD>
24   <TR>
25     <TH ROWSPAN=3></TH>
26     <TH COLSPAN=6>Sales</TH>
27     <TH ROWSPAN=3></TH>
28     <TH ROWSPAN=3>Rate</TH>
29     <TH ROWSPAN=3></TH>
30     <TH ROWSPAN=3>Estimated tax</TH>
31     <TH ROWSPAN=3>Tax invoiced</TH>
32     <TH ROWSPAN=3></TH>
33     <TH ROWSPAN=3>Tax credited</TH>
34     <TH ROWSPAN=3></TH>
35     <TH ROWSPAN=3>Net tax due</TH>
36     <TH ROWSPAN=3></TH>
37     <TH ROWSPAN=3>Tax collected</TH>
38   </TR>
39
40   <TR>
41     <TH ROWSPAN=2>Total</TH>
42     <TH ROWSPAN=1>Non-taxable</TH>
43     <TH ROWSPAN=1>Non-taxable</TH>
44     <TH ROWSPAN=1>Non-taxable</TH>
45     <TH ROWSPAN=2>Credited</TH>
46     <TH ROWSPAN=2>Taxable</TH>
47   </TR>
48
49   <TR STYLE="font-size:small">
50     <TH>(tax-exempt customer)</TH>
51     <TH>(tax-exempt package)</TH>
52     <TH>(monthly exemption)</TH>
53   </TR>
54   </THEAD>
55
56 % my $rownum = 0;
57 % my $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
58
59   <TBODY>
60 % foreach my $row (@rows) {
61 %   # before anything else: if this row's pkgclass is not the same as the 
62 %   # previous row's, then:
63 %   if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
64 %     if ( $rownum > 0 ) { # start a new section
65 %       $rownum = 0;
66   </TBODY><TBODY>
67 %     }
68 %     if ( $params{breakdown}->{pkgclass} ) { # and caption the new section
69   <TR>
70     <TD COLSPAN=19 CLASS="sectionhead">
71       <% $pkgclass_name{$row->{pkgclass}} %>
72     </TD>
73   </TR>
74 %     }
75 %   } # if $row->{pkgclass} ne ...
76
77 %   # construct base links that limit to the tax rates described by this row
78 %   # cust_bill_pkg.cgi wants a list of specific taxnums (and package class)
79 %   # cust_credit_bill_pkg.html wants a geographic scope (and package class)
80 %   my $rowlink = ';taxnum=' . $row->{taxnums};
81 %   my $rowregion = ';country=' . $cgi->param('country');
82 %   foreach my $loc (qw(state county city district)) {
83 %     if ( $row->{$loc} ) {
84 %       $rowregion .= ";$loc=" . uri_escape($row->{$loc});
85 %     }
86 %   }
87 %   # and also the package class, if we're limiting package class
88 %   if ( $params{breakdown}->{pkgclass} ) {
89 %     $rowlink .= ';classnum=' . ($row->{pkgclass} || 0);
90 %     $rowregion .= ';classnum=' . ($row->{pkgclass} || 0);
91 %   }
92 %
93 %   if ( $row->{total} ) {
94   </TBODY><TBODY CLASS="total">
95 %   }
96   <TR CLASS="row<% $rownum % 2 %>">
97 %   # Row label
98     <TD CLASS="rowhead"><% $row->{label} |h %></TD>
99     <TD>
100 %   # Total sales
101       <A HREF="<% $saleslink . $rowlink %>">
102         <% $money_sprintf->( $row->{sales} ) %>
103       </A>
104     </TD>
105 %   # Exemptions: customer
106     <TD>
107       <A HREF="<% $saleslink . $rowlink . ';exempt_cust=Y' %>">
108         <% $money_sprintf->( $row->{exempt_cust} ) %>
109       </A>
110     </TD>
111 %   # package
112     <TD>
113       <A HREF="<% $saleslink . $rowlink . ';exempt_pkg=Y' %>">
114         <% $money_sprintf->( $row->{exempt_pkg} ) %>
115       </A>
116     </TD>
117 %   # monthly (note this uses $exemptlink; it's a completely separate report)
118     <TD>
119       <A HREF="<% $exemptlink . $rowlink %>">
120         <% $money_sprintf->( $row->{exempt_monthly} ) %>
121       </A>
122     </TD>
123 %   # credited sales
124     <TD>
125       <A HREF="<% $salescreditlink . $rowregion %>">
126         <% $money_sprintf->( $row->{sales_credited} ) %>
127       </A>
128     </TD>
129 %   # taxable sales
130     <TD>
131       <A HREF="<% $saleslink . $rowlink . ";taxable=1" %>">
132         <% $money_sprintf->( $row->{taxable} ) %>
133       </A>
134     </TD>
135     <TD CLASS="bigmath"> &times; </TD>
136     <TD><% $row->{rate} %></TD>
137 %   # estimated tax
138     <TD CLASS="bigmath"> = </TD>
139     <TD>
140 %   if ( $row->{estimated} ) {
141       <% $money_sprintf->( $row->{estimated} ) %>
142 %   }
143     </TD>
144 %   # invoiced tax
145     <TD>
146       <A HREF="<% $taxlink . $rowlink %>">
147         <% $money_sprintf->( $row->{tax} ) %>
148       </A>
149     </TD>
150 %   # credited tax
151     <TD CLASS="bigmath"> &minus; </TD>
152     <TD>
153 %#      <A HREF="<% $creditlink . $rowlink %>"> currently broken
154         <% $money_sprintf->( $row->{tax_credited} ) %>
155 %#      </A>
156     </TD>
157 %   # net tax due
158     <TD CLASS="bigmath"> = </TD>
159     <TD><% $money_sprintf->( $row->{tax} - $row->{tax_credited} ) %></TD>
160 %   # tax collected
161     <TD>&nbsp;</TD>
162     <TD><% $money_sprintf->( $row->{tax_paid} ) %></TD>
163   </TR>
164 %   $rownum++;
165 %   $prev_row = $row;
166 % } # foreach my $row
167 % # at the end of everything
168   </TBODY>
169 % if ( $report->{outside} > 0 ) {
170   <TBODY CLASS="total" STYLE="background-color: #cccccc; line-height: 3">
171     <TR>
172       <TD CLASS="rowhead">
173         <% emt('Out of taxable region') %>
174       </TD>
175       <TD STYLE="text-align: right">
176         <A HREF="<% $saleslink %>;out=1;taxname=<% encode_entities($params{'taxname'}) %>">
177           <% $money_sprintf->( $report->{outside } ) %>
178         </A>
179       </TD>
180       <TD COLSPAN=0></TD>
181     </TR>
182   </TBODY>
183 % }
184 </TABLE>
185
186 <BR>
187 <& /elements/table-grid.html &>
188   <THEAD>
189   <TR>
190     <TH ROwSPAN=2></TH>
191     <TH ROWSPAN=2>Total credits</TH>
192     <TH COLSPAN=3>Applied to</TH>
193   </TR>
194   <TR STYLE="font-size: small">
195     <TH>Taxable sales</TH>
196     <TH>Tax-exempt sales</TH>
197     <TH>Taxes</TH>
198   </TR>
199   </THEAD>
200
201 % $rownum = 0;
202 % $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
203
204   <TBODY>
205 % # mostly duplicates the stuff above...
206 % # but putting it all in one giant table is no good
207 % foreach my $row (@rows) {
208 %   if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
209 %     if ( $rownum > 0 ) { # start a new section
210 %       $rownum = 0;
211   </TBODY><TBODY>
212 %     }
213 %     if ( $params{breakdown}->{pkgclass} ) { # and caption the new section
214   <TR>
215     <TD COLSPAN=5 CLASS="sectionhead">
216       <% $pkgclass_name{$row->{pkgclass}} %>
217     </TD>
218   </TR>
219 %     }
220 %   } # if $row->{pkgclass} ne ...
221
222 %   my $rowlink = ';taxnum=' . $row->{taxnums};
223 %   my $rowregion = ';country=' . $cgi->param('country');
224 %   foreach my $loc (qw(state county city district)) {
225 %     if ( $row->{$loc} ) {
226 %       $rowregion .= ";$loc=" . uri_escape($row->{$loc});
227 %     }
228 %   }
229 %   if ( $params{breakdown}->{pkgclass} ) {
230 %     $rowlink .= ';classnum=' . ($row->{pkgclass} || 0);
231 %     $rowregion .= ';classnum=' . ($row->{pkgclass} || 0);
232 %   }
233 %
234 %   if ( $row->{total} ) {
235   </TBODY><TBODY CLASS="total">
236 %   }
237   <TR CLASS="row<% $rownum % 2 %>">
238     <TD CLASS="rowhead"><% $row->{label} |h %></TD>
239     <TD>
240 %   # Total credits
241       <% $money_sprintf->( $row->{credits} ) %>
242     </TD>
243 %   # Credits to taxable sales
244     <TD>
245       <A HREF="<% $salescreditlink . $rowregion %>">
246         <% $money_sprintf->( $row->{sales_credited} ) %>
247       </A>
248     </TD>
249 %   # ... to exempt sales (link is the same, it shows both exempt and taxable)
250     <TD>
251       <A HREF="<% $salescreditlink . $rowregion %>">
252         <% $money_sprintf->( $row->{exempt_credited} ) %>
253       </A>
254     </TD>
255 %   # ... to taxes
256     <TD>
257 %#      <A HREF="<% $creditlink . $rowlink %>"> currently broken
258         <% $money_sprintf->( $row->{tax_credited} ) %>
259 %#      </A>
260     </TD>
261   </TR>
262 %   $rownum++;
263 %   $prev_row = $row;
264 % } # foreach my $row
265 % # no "out of taxable region" for credits (yet)
266   </TBODY>
267 </TABLE>
268
269
270 <& /elements/footer.html &>
271 <%init>
272
273 die "access denied"
274   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
275
276 my $DEBUG = $cgi->param('debug') || 0;
277
278 my $conf = new FS::Conf;
279
280 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
281
282 my %params = (
283   beginning => $beginning,
284   ending    => $ending,
285 );
286 $params{country} = $cgi->param('country');
287 $params{debug}   = $DEBUG;
288 $params{breakdown} = { map { $_ => 1 } $cgi->param('breakdown') };
289
290 my $agentname;
291 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
292   my $agent = FS::agent->by_key($1) or die "unknown agentnum $1";
293   $params{agentnum} = $1;
294   $agentname = $agent->agentname;
295 }
296
297 # allow anything in here; FS::Report::Tax will treat it as unsafe
298 if ( length($cgi->param('taxname')) ) {
299   $params{taxname} = $cgi->param('taxname');
300 } else {
301   die "taxname required";
302 }
303
304 if ( $cgi->param('credit_date') eq 'cust_credit_bill' ) {
305   $params{credit_date} = 'cust_credit_bill';
306 } else {
307   $params{credit_date} = 'cust_bill';
308 }
309
310 warn "PARAMS:\n".Dumper(\%params)."\n\n" if $DEBUG;
311
312 my $report = FS::Report::Tax->report_internal(%params);
313 my @rows = $report->table; # array of hashrefs
314
315 my $money_char = $conf->config('money_char') || '$';
316 my $money_sprintf = sub {
317   $money_char. sprintf('%.2f', shift);
318 };
319
320 my $dateagentlink = "begin=$beginning;end=$ending";
321 if ( $params{agentnum} ) {
322   $dateagentlink .= ';agentnum=' . $params{agentnum};
323 }
324 my $saleslink  = $p. "search/cust_bill_pkg.cgi?$dateagentlink;nottax=1";
325 my $taxlink    = $p. "search/cust_bill_pkg.cgi?$dateagentlink;istax=1";
326 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
327 my $salescreditlink = $p. "search/cust_credit_bill_pkg.html?$dateagentlink;nottax=1";
328 if ( $params{'credit_date'} eq 'cust_credit_bill' ) {
329   $salescreditlink =~ s/begin/credit_begin/;
330   $salescreditlink =~ s/end/credit_end/;
331 }
332 #my $creditlink = $p. "search/cust_bill_pkg.cgi?$dateagentlink;credit=1;istax=1";
333 #if ( $params{'credit_date'} eq 'cust_credit_bill' ) {
334 #  $creditlink =~ s/begin/credit_begin/;
335 #  $creditlink =~ s/end/credit_end/;
336 #}
337 my $creditlink = ''; # disabled until we find a sane way to do this
338
339 my %pkgclass_name = map { $_->classnum, $_->classname } qsearch('pkg_class');
340 $pkgclass_name{''} = 'Unclassified';
341
342 </%init>