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;
11 <TR><TH COLSPAN=2><% $legend %></TH></TR>
13 <TD>E911 access lines:</TD>
14 <TD><% $row->{quantity} || 0 %></TD>
17 <TD>Total fees charged: </TD>
18 <TD><% $money_char.sprintf('%.2f', $row->{charged_amount}) %></TD>
21 <TD>Fee payments collected: </TD>
22 <TD><% $money_char.sprintf('%.2f', $row->{paid_amount}) %></TD>
25 <TD>Administrative fee (1%): </TD>
26 <TD><% $money_char.sprintf('%.2f', $row->{paid_amount} * $admin_fee) %></TD>
30 <TD><% $money_char.sprintf('%.2f', $row->{paid_amount} * (1-$admin_fee) ) %>
34 <& /elements/footer.html &>
36 % $cgi->param('error' => 'No paid E911 fees found.');
37 <& /elements/errorpage.html &>
42 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
44 my $money_char = FS::Conf->new->config('money_char') || '$';
46 my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
48 $cgi->param('e911pkgpart') =~ /^(\d+)$/;
49 my $pkgpart = $1 or die 'bad e911pkgpart';
51 $cgi->param('agentnum') =~ /^(\d*)$/;
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.
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';
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)
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 )";
75 $from .= ' JOIN cust_main ON (cust_pkg.custnum = cust_main.custnum)';
76 $where .= "\n AND cust_main.agentnum = $agentnum";
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).
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
89 my $sth = dbh->prepare($sql);
91 my $row = $sth->fetchrow_hashref;
93 my $admin_fee = 0.01; # 1% admin fee, allowed in Texas
95 $end = '' if $end == 4294967295;
98 $legend = FS::agent->by_key($agentnum)->agent . ', ';
100 if ( $begin and $end ) {
101 $legend .= time2str('%h %o %Y', $begin) . '—' .
102 time2str('%h %o %Y', $end);
104 $legend .= time2str('after %h %o %Y', $begin);
106 $legend .= time2str('before %h %o %Y', $end);
108 $legend .= 'any time';
110 $legend = ucfirst($legend);