summaryrefslogtreecommitdiff
path: root/httemplate/search/e911.html
blob: 6d387d563dfbee87ec4162efe3c52c2bbfff96de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<%doc>

  E911 Fee Report

  Finds billing totals for a given pkgpart where the bill item matches
  cust_pkg.pkgpart or cust_bill_pkg.pkgpart_override columns.

  Given date range, filter by when the invoice was paid.

  * E911 access lines - SUM(cust_bill_pkg.quantity)
  * Total fees charged - SUM(cust_bill_pay_pkg.amount)
  * Fee payments collected - SUM(cust_bill_pkg.setup) + SUM(cust_bill_pkg.recur)

  * Administrative fee (1%) - 1% of Fee Payments Collected
  * Amount due - 99% of Fee Payments Collected

</%doc>
% if ( $row ) {
<& /elements/header.html, 'E911 Fee Report' &>

<& /elements/table-grid.html &>
<STYLE TYPE="text/css">
table.grid TD:first-child { font-weight: normal }
table.grid TD { font-weight: bold;
                text-align: right;
                padding: 1px 2px }
</STYLE>

  <TR><TH COLSPAN=2><% $legend %></TH></TR>
  <TR>
    <TD><% mt('E911 access lines') %>:</TD>
    <TD><% $report{e911_access_lines} %></TD>
  </TR>
  <TR>
    <TD><% mt('Total fees charged') %>: </TD>
    <TD><% $money_char.$report{fees_charged} %></TD>
  </TD>
  <TR>
    <TD><% mt('Fee payments collected') %>: </TD>
    <TD><% $money_char.$report{fees_collected} %></TD>
  </TR>
  <TR>
    <TD><% mt('Administrative fee') %> (1%): </TD>
    <TD><% $money_char.$report{admin_fee} %></TD>
  </TR>
  <TR>
    <TD><% mt('Amount due') %>: </TD>
    <TD><% $money_char.$report{e911_amount_due} %></TD>
  </TR>
</TABLE>
<& /elements/footer.html &>
% } else { # no data
%   $cgi->param('error' => 'No paid E911 fees found.');
<& /elements/errorpage.html &>
% }
<%init>

our $DEBUG;

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');

my $money_char = FS::Conf->new->config('money_char') || '$';

my($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);

$cgi->param('e911pkgpart') =~ /^(\d+)$/;
my $pkgpart = $1 or die 'bad e911pkgpart';

$cgi->param('agentnum') =~ /^(\d*)$/;
my $agentnum = $1;

# This has the potential to become as nightmarish as the old tax report.
# If we end up doing multiple rows for some reason (date intervals, 
# package classes, etc.), do NOT simply loop through this and do a 
# bazillion scalar_sql queries.  Use a properly grouped aggregate query.

my $sql_statement = "
  SELECT
    sum(cust_bill_pkg.quantity) as quantity,
    sum(cust_bill_pay_pkg.amount) as amount,
    sum(cust_bill_pkg.setup) as setup,
    sum(cust_bill_pkg.recur) as recur

  FROM cust_pkg
    LEFT JOIN cust_bill_pkg      USING (pkgnum)
    LEFT JOIN cust_bill_pay_pkg  USING (billpkgnum)
    LEFT JOIN cust_bill_pay      USING (billpaynum)
";
if ( $agentnum ) {
  $sql_statement .= "
      LEFT JOIN cust_main          USING (custnum)
    WHERE
      cust_main.agentnum = ?
      AND ";
} else {
  $sql_statement .= "
    WHERE
  "
}
$sql_statement .= "
    ( cust_bill_pkg.pkgpart_override = ? OR cust_pkg.pkgpart = ? )
    AND (
      ( cust_bill_pay._date >= ? AND cust_bill_pay._date < ? )
      OR cust_bill_pay.paynum IS NULL
    );
";

# Preserving this oddball, unexplained epoch substitution
$end = '' if $end == 4294967295;

my @bind_values = (
    $agentnum ? $agentnum : (),
    $pkgpart,
    $pkgpart,
    $begin || 0,
    $end || time(),
);

if ( $DEBUG ) {
  warn "\$sql_statement: $sql_statement\n";
  warn "\@bind_values: ".join(', ',@bind_values)."\n";
}

my $sth = dbh->prepare( $sql_statement );
$sth->execute( @bind_values ) || die $sth->errstr;
my $row = $sth->fetchrow_hashref;

my %report = (
  e911_access_lines => $row->{quantity} || 0,

  fees_charged => sprintf(
    "%.2f",
    ( $row->{setup} + $row->{recur} ) || 0,
  ),

  fees_collected => sprintf(
    "%.2f",
    ( $row->{amount} || 0 ),
  ),
);

# Does everybody use this 1% admin fee?  Should this be configurable?
$report{admin_fee} = sprintf( "%.2f", $report{fees_collected} * 0.01 );
$report{e911_amount_due} = $report{fees_collected} - $report{admin_fee};

my $begin_text =
  $begin
    ? DateTime->from_epoch(epoch => $begin)->mdy('/')
    : mt('Anytime');

my $end_text =  DateTime->from_epoch(epoch => ( $end || time ))->mdy('/');

my $legend = FS::agent->by_key($agentnum)->agent . ', ' if $agentnum;
if ( $begin && $end ) {
  $legend .= "$begin_text &#x2194; $end_text";
} elsif ( $begin ) {
  $legend .= mt('After')." $begin_text";
} else {
  $legend .= mt('Through')." $end_text"
}

</%init>