fix top subtotals on refund reports
[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 %   if ( $params{breakdown}->{pkgclass} ) {
82 %     $rowlink .= ';classnum=' . ($row->{pkgclass} || 0);
83 % #    $rowregion .= ';classnum=' . ($row->{pkgclass} || 0);
84 %   }
85 %
86 %   if ( $row->{total} ) {
87   </TBODY><TBODY CLASS="total">
88 %   }
89   <TR CLASS="row<% $rownum % 2 %>">
90 %   # Row label
91 %   # Special: If this report is showing all taxes, link the row label to
92 %   # the detailed tax report for that taxname/country.
93     <TD CLASS="rowhead">
94 %   if ( $all ) {
95 %     my $newcgi = CGI->new($cgi);
96 %     $newcgi->delete('all');
97 %     $newcgi->param('country', $row->{country});
98 %     $newcgi->param('taxname', $row->{taxname});
99 %     $newcgi->param('breakdown', qw(city district));
100
101       <A HREF="<% encode_entities( $newcgi->self_url ) %>">
102         <% $row->{label} |h %>
103       </A>
104
105 %   } else { # on the per-taxname report, just show the label with no link
106
107         <% $row->{label} |h %>
108
109 %   }
110     </TD>
111     <TD>
112 %   # Total sales
113       <A HREF="<% $saleslink . $rowlink %>">
114         <% $money_sprintf->( $row->{sales} ) %>
115       </A>
116     </TD>
117 %   # Exemptions: customer
118     <TD>
119       <A HREF="<% $saleslink . $rowlink . ';exempt_cust=Y' %>">
120         <% $money_sprintf->( $row->{exempt_cust} ) %>
121       </A>
122     </TD>
123 %   # package
124     <TD>
125       <A HREF="<% $saleslink . $rowlink . ';exempt_pkg=Y' %>">
126         <% $money_sprintf->( $row->{exempt_pkg} ) %>
127       </A>
128     </TD>
129 %   # monthly (note this uses $exemptlink; it's a completely separate report)
130     <TD>
131       <A HREF="<% $exemptlink . $rowlink %>">
132         <% $money_sprintf->( $row->{exempt_monthly} ) %>
133       </A>
134     </TD>
135 %   # credited sales
136     <TD>
137       <A HREF="<% $salescreditlink . $rowlink %>">
138         <% $money_sprintf->( $row->{sales_credited} ) %>
139       </A>
140     </TD>
141 %   # taxable sales
142     <TD>
143       <A HREF="<% $saleslink . $rowlink . ";taxable=1" %>">
144         <% $money_sprintf->( $row->{taxable} ) %>
145       </A>
146     </TD>
147     <TD CLASS="bigmath"> &times; </TD>
148     <TD><% $row->{rate} %></TD>
149 %   # estimated tax
150     <TD CLASS="bigmath"> = </TD>
151     <TD>
152 %   if ( $row->{estimated} ) {
153       <% $money_sprintf->( $row->{estimated} ) %>
154 %   }
155     </TD>
156 %   # invoiced tax
157     <TD>
158       <A HREF="<% $taxlink . $rowlink %>">
159         <% $money_sprintf->( $row->{tax} ) %>
160       </A>
161     </TD>
162 %   # credited tax
163     <TD CLASS="bigmath"> &minus; </TD>
164     <TD>
165 %#      <A HREF="<% $creditlink . $rowlink %>"> currently broken
166         <% $money_sprintf->( $row->{tax_credited} ) %>
167 %#      </A>
168     </TD>
169 %   # net tax due
170     <TD CLASS="bigmath"> = </TD>
171     <TD><% $money_sprintf->( $row->{tax} - $row->{tax_credited} ) %></TD>
172 %   # tax collected
173     <TD>&nbsp;</TD>
174     <TD><% $money_sprintf->( $row->{tax_paid} ) %></TD>
175   </TR>
176 %   $rownum++;
177 %   $prev_row = $row;
178 % } # foreach my $row
179 % # at the end of everything
180   </TBODY>
181 % # the all-taxes report doesn't have "out of region"
182 % if ( !$all and $report->{out_sales} > 0 ) {
183   <TBODY CLASS="total" STYLE="background-color: #cccccc; line-height: 3">
184     <TR>
185       <TD CLASS="rowhead">
186         <% emt('Out of taxable region') %>
187       </TD>
188       <TD STYLE="text-align: right">
189         <A HREF="<% $saleslink %>;out=1;taxname=<% encode_entities($params{'taxname'}) %>">
190           <% $money_sprintf->( $report->{out_sales} ) %>
191         </A>
192       </TD>
193       <TD COLSPAN=0></TD>
194     </TR>
195   </TBODY>
196 % }
197 </TABLE>
198
199 <BR>
200 <& /elements/table-grid.html &>
201   <THEAD>
202   <TR>
203     <TH ROwSPAN=2></TH>
204     <TH ROWSPAN=2>Total credits</TH>
205     <TH COLSPAN=3>Applied to</TH>
206   </TR>
207   <TR STYLE="font-size: small">
208     <TH>Taxable sales</TH>
209     <TH>Tax-exempt sales</TH>
210     <TH>Taxes</TH>
211   </TR>
212   </THEAD>
213
214 % $rownum = 0;
215 % $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
216
217   <TBODY>
218 % # mostly duplicates the stuff above...
219 % # but putting it all in one giant table is no good
220 % foreach my $row (@rows) {
221 %   if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
222 %     if ( $rownum > 0 ) { # start a new section
223 %       $rownum = 0;
224   </TBODY><TBODY>
225 %     }
226 %     if ( $params{breakdown}->{pkgclass} ) { # and caption the new section
227   <TR>
228     <TD COLSPAN=5 CLASS="sectionhead">
229       <% $pkgclass_name{$row->{pkgclass}} %>
230     </TD>
231   </TR>
232 %     }
233 %   } # if $row->{pkgclass} ne ...
234
235 %   my $rowlink = ';taxnum=' . $row->{taxnums};
236 %
237 %   if ( $row->{total} ) {
238   </TBODY><TBODY CLASS="total">
239 %   }
240   <TR CLASS="row<% $rownum % 2 %>">
241     <TD CLASS="rowhead"><% $row->{label} |h %></TD>
242     <TD>
243 %   # Total credits
244       <% $money_sprintf->( $row->{credits} ) %>
245     </TD>
246 %   # Credits to taxable sales
247     <TD>
248       <A HREF="<% $salescreditlink . $rowlink %>">
249         <% $money_sprintf->( $row->{sales_credited} ) %>
250       </A>
251     </TD>
252 %   # ... to exempt sales (link is the same, it shows both exempt and taxable)
253     <TD>
254       <A HREF="<% $salescreditlink . $rowlink %>">
255         <% $money_sprintf->( $row->{exempt_credited} ) %>
256       </A>
257     </TD>
258 %   # ... to taxes
259     <TD>
260 %#      <A HREF="<% $creditlink . $rowlink %>"> currently broken
261         <% $money_sprintf->( $row->{tax_credited} ) %>
262 %#      </A>
263     </TD>
264   </TR>
265 %   $rownum++;
266 %   $prev_row = $row;
267 % } # foreach my $row
268 % # "out of taxable region" for credits (there is a need for it)
269 % if ( !$all and $report->{out_credit} > 0 ) {
270 %   my $creditlink = "cust_credit_bill_pkg.html?out=1;$dateagentlink";
271 %   if ( $params{'credit_date'} eq 'cust_credit_bill' ) {
272 %     $creditlink =~ s/begin/credit_begin/;
273 %     $creditlink =~ s/end/credit_end/;
274 %   }
275 %   $creditlink .= ";taxname=" . encode_entities($params{'taxname'});
276   <TBODY CLASS="total" STYLE="background-color: #cccccc; line-height: 3">
277     <TR>
278       <TD CLASS="rowhead">
279         <% emt('Out of taxable region') %>
280       </TD>
281       <TD STYLE="text-align: right">
282         <A HREF="<% $creditlink %>">
283           <% $money_sprintf->( $report->{out_credit} ) %>
284         </A>
285       </TD>
286       <TD COLSPAN=0></TD>
287     </TR>
288   </TBODY>
289 % }
290   </TBODY>
291 </TABLE>
292
293
294 <& /elements/footer.html &>
295 <%init>
296
297 die "access denied"
298   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
299
300 my $DEBUG = $cgi->param('debug') || 0;
301
302 my $conf = new FS::Conf;
303
304 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
305
306 my %params = (
307   beginning => $beginning,
308   ending    => $ending,
309 );
310 $params{debug}   = $DEBUG;
311 my $agentname;
312
313 # filter by agentnum
314 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
315   my $agent = FS::agent->by_key($1) or die "unknown agentnum $1";
316   $params{agentnum} = $1;
317   $agentname = $agent->agentname;
318 }
319
320 # credit date behavior: limit by the date of the credit application, or
321 # the invoice?
322 if ( $cgi->param('credit_date') eq 'cust_credit_bill' ) {
323   $params{credit_date} = 'cust_credit_bill';
324 } else {
325   $params{credit_date} = 'cust_bill';
326 }
327
328 my $all = $cgi->param('all');
329 my $report_class;
330
331 if ( $all ) {
332   # then show the master report, no country, no taxname, no breakdown
333   $report_class = 'FS::Report::Tax::All';
334 } else {
335   $report_class = 'FS::Report::Tax::ByName';
336   $params{country} = $cgi->param('country');
337   $params{breakdown} = { map { $_ => 1 } $cgi->param('breakdown') };
338
339   # allow anything in here; FS::Report::Tax will treat it as unsafe
340   if ( length($cgi->param('taxname')) ) {
341     $params{taxname} = $cgi->param('taxname');
342   } else {
343     die "taxname required";
344   }
345 }
346
347 if ($DEBUG) {
348   warn "REPORT: $report_class\nPARAMS:\n".Dumper(\%params)."\n\n";
349 }
350
351 my $report = $report_class->report(%params);
352 my @rows = $report->table; # array of hashrefs
353
354 my $money_char = $conf->config('money_char') || '$';
355 my $money_sprintf = sub {
356   $money_char. sprintf('%.2f', shift);
357 };
358
359 my $dateagentlink = "begin=$beginning;end=$ending";
360 if ( $params{agentnum} ) {
361   $dateagentlink .= ';agentnum=' . $params{agentnum};
362 }
363 my $saleslink  = $p. "search/cust_bill_pkg.cgi?$dateagentlink;nottax=1";
364 my $taxlink    = $p. "search/cust_bill_pkg.cgi?$dateagentlink;istax=1";
365 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
366 my $salescreditlink = $p. "search/cust_bill_pkg.cgi?$dateagentlink;nottax=1;credit=1";
367 if ( $params{'credit_date'} eq 'cust_credit_bill' ) {
368   $salescreditlink =~ s/begin/credit_begin/;
369   $salescreditlink =~ s/end/credit_end/;
370   $saleslink .= ";credit_begin=$beginning;credit_end=$ending";
371 }
372 #my $creditlink = $p. "search/cust_bill_pkg.cgi?$dateagentlink;credit=1;istax=1";
373 #if ( $params{'credit_date'} eq 'cust_credit_bill' ) {
374 #  $creditlink =~ s/begin/credit_begin/;
375 #  $creditlink =~ s/end/credit_end/;
376 #}
377 my $creditlink = ''; # disabled until we find a sane way to do this
378
379 my %pkgclass_name = map { $_->classnum, $_->classname } qsearch('pkg_class');
380 $pkgclass_name{''} = 'Unclassified';
381
382 </%init>