stray closing /TABLE in the no-ticket case
[freeside.git] / httemplate / search / e911.html
1 % if ( $row ) {
2 %# pretty minimal report
3 <& /elements/header.html, 'E911 Fee Report' &>
4 <& /elements/table-grid.html &>
5 <STYLE TYPE="text/css">
6 table.grid TD:first-child { font-weight: normal }
7 table.grid TD { font-weight: bold;
8                 text-align: right;
9                 padding: 1px 2px }
10 </STYLE>
11   <TR><TH COLSPAN=2><% $legend %></TH></TR>
12   <TR>
13     <TD>E911 access lines:</TD>
14     <TD><% $row->{quantity} || 0 %></TD>
15   </TR>
16   <TR>
17     <TD>Total fees charged: </TD>
18     <TD><% $money_char.sprintf('%.2f', $row->{charged_amount}) %></TD>
19   </TD>
20   <TR>
21     <TD>Fee payments collected: </TD>
22     <TD><% $money_char.sprintf('%.2f', $row->{paid_amount}) %></TD>
23   </TR>
24   <TR>
25     <TD>Administrative fee (1%): </TD>
26     <TD><% $money_char.sprintf('%.2f', $row->{paid_amount} * $admin_fee) %></TD>
27   </TR>
28   <TR>
29     <TD>Amount due: </TD>
30     <TD><% $money_char.sprintf('%.2f', $row->{paid_amount} * (1-$admin_fee) ) %>
31     </TD>
32   </TR>
33 </TABLE>
34 <& /elements/footer.html &>
35 % } else { # no data
36 %   $cgi->param('error' => 'No paid E911 fees found.');
37 <& /elements/errorpage.html &>
38 % }
39 <%init>
40
41 die "access denied"
42   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
43
44 my $money_char = FS::Conf->new->config('money_char') || '$';
45
46 my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
47
48 $cgi->param('e911pkgpart') =~ /^(\d+)$/;
49 my $pkgpart = $1 or die 'bad e911pkgpart';
50
51 $cgi->param('agentnum') =~ /^(\d*)$/;
52 my $agentnum = $1;
53
54 # This has the potential to become as nightmarish as the old tax report.
55 # If we end up doing multiple rows for some reason (date intervals, 
56 # package classes, etc.), do NOT simply loop through this and do a 
57 # bazillion scalar_sql queries.  Use a properly grouped aggregate query.
58
59 my $select = 'SELECT cust_bill_pkg.billpkgnum, cust_bill_pkg.quantity, '.
60 'cust_bill_pkg.setup, SUM(cust_bill_pay_pkg.amount) AS paid_amount';
61
62 my $from = 'FROM cust_pkg
63   JOIN cust_bill_pkg      USING (pkgnum)
64   JOIN cust_bill          USING (invnum)
65   LEFT JOIN cust_bill_pay_pkg  USING (billpkgnum)
66   LEFT JOIN cust_bill_pay      USING (billpaynum)
67 ';
68 # going by payment application date here, which should be
69 # max(invoice date, payment date)
70 my $where = "WHERE cust_pkg.pkgpart = $pkgpart
71 AND ( (cust_bill_pay._date >= $begin AND cust_bill_pay._date < $end)
72       OR cust_bill_pay.paynum IS NULL )";
73
74 if ( $agentnum ) {
75   $from .= '  JOIN cust_main ON (cust_pkg.custnum = cust_main.custnum)';
76   $where .= "\n AND cust_main.agentnum = $agentnum";
77 }
78
79 my $subquery = "$select $from $where
80 GROUP BY cust_bill_pkg.billpkgnum, cust_bill_pkg.quantity";
81 # This has one row for each E911 line item that has any payments applied.
82 # Fields are the billpkgnum of the item (currently unused), the number of
83 # E911 charges, and the total amount paid (always > 0).
84
85 # now sum those rows.
86 my $sql = "SELECT SUM(quantity) AS quantity, SUM(setup) AS charged_amount,
87 SUM(paid_amount) AS paid_amount FROM ($subquery) AS paid_fees"; # no grouping
88
89 my $sth = dbh->prepare($sql);
90 $sth->execute;
91 my $row = $sth->fetchrow_hashref;
92
93 my $admin_fee = 0.01; # 1% admin fee, allowed in Texas
94
95 $end = '' if $end == 4294967295;
96 my $legend = '';
97 if ( $agentnum ) {
98   $legend = FS::agent->by_key($agentnum)->agent . ', ';
99 }
100 if ( $begin and $end ) {
101   $legend .= time2str('%h %o %Y', $begin) . '&mdash;' .
102              time2str('%h %o %Y', $end);
103 } elsif ( $begin ) {
104   $legend .= time2str('after %h %o %Y', $begin);
105 } elsif ( $end ) {
106   $legend .= time2str('before %h %o %Y', $end);
107 } else {
108   $legend .= 'any time';
109 }
110 $legend = ucfirst($legend);
111 </%init>