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