1 <% include("/elements/header.html", 'Prepaid Income (Unearned Revenue) Report') %>
3 <% include( '/elements/table-grid.html' ) %>
6 % if ( scalar(@agentnums) > 1 ) {
7 <TH CLASS="grid" BGCOLOR="#cccccc">Agent</TH>
9 <TH CLASS="grid" BGCOLOR="#cccccc"><% $actual_label %>Unearned Revenue</TH>
11 <TH CLASS="grid" BGCOLOR="#cccccc">Legacy Unearned Revenue</TH>
15 % my $bgcolor1 = '#eeeeee';
16 % my $bgcolor2 = '#ffffff';
19 % push @agentnums, 0 unless scalar(@agentnums) < 2;
20 % foreach my $agentnum (@agentnums) {
22 % if ( $bgcolor eq $bgcolor1 ) {
23 % $bgcolor = $bgcolor2;
25 % $bgcolor = $bgcolor1;
28 % my $alink = $agentnum ? "$link;agentnum=$agentnum" : $link;
30 % my $agent_name = 'Total';
32 % my $agent = qsearchs('agent', { 'agentnum' => $agentnum })
33 % or die "unknown agentnum $agentnum";
34 % $agent_name = $agent->agent;
39 % if ( scalar(@agentnums) > 1 ) {
40 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $agent_name |h %></TD>
43 <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>"><A HREF="<% $alink %>"><% $money_char %><% $total{$agentnum} %></A></TD>
46 <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
47 <% $now == $time ? $money_char.$total_legacy{$agentnum} : '<i>N/A</i>'%>
58 <% $actual_label %><% $actual_label ? 'u' : 'U' %>nearned revenue
59 is the as-yet-unearned portion of revenue
60 <% $actual_label ? 'Freeside has actually' : '' %>
61 invoiced for packages with
62 <% $cgi->param('include_monthly') ? 'terms extending into the future.'
63 : 'longer-than monthly terms.' %>
67 Legacy unearned revenue is the amount of unearned revenue represented by
68 customer packages. This number may be larger than actual unearned
69 revenue if you have imported longer-than monthly customer packages from
70 a previous billing system.
73 <% include('/elements/footer.html') %>
77 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
79 my $conf = new FS::Conf;
80 my $money_char = $conf->config('money_char') || '$';
82 my $legacy = $conf->exists('enable_legacy_prepaid_income');
83 my $actual_label = $legacy ? 'Actual ' : '';
85 #doesn't yet deal with daily/weekly packages
87 my $mode = $cgi->param('mode');
91 my $now = $cgi->param('date') && parse_datetime($cgi->param('date')) || $time;
92 $now =~ /^(\d+)$/ or die "unparsable date?";
95 my $dt = DateTime->from_epoch(epoch => $now, time_zone => 'local');
96 $dt->truncate(to => 'day'); # local midnight on the report day
97 $dt->add(days => 1); # the day after that
98 $dt->subtract(seconds => 1); # the last second of the report day
101 my $link = "unearned_detail.html?date=$now;mode=$mode";
103 if ( $cgi->param('include_monthly') ) {
104 $link .= ';include_monthly=1';
107 my $curuser = $FS::CurrentUser::CurrentUser;
111 $agentnum ? ($agentnum) : $curuser->agentnums;
112 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
114 #XXX#push @where, "agentnum = $agentnum";
115 #XXX#$link .= ";agentnum=$agentnum";
117 @agentnums = $curuser->agentnums;
122 #here is the agent virtualization
123 push @where, $curuser->agentnums_sql( 'table'=>'cust_main' );
126 if ( $cgi->param('status') =~ /^([a-z]+)$/ ) {
128 $link .= ";status=$status";
129 push @where, FS::cust_main->cust_status_sql . " = '$status'";
133 my %total_legacy = ();
134 foreach my $agentnum (@agentnums) {
136 my $where = join(' AND ', @where, "cust_main.agentnum = $agentnum");
137 $where = "AND $where" if $where;
139 my( $total, $total_legacy ) = ( 0, 0 );
141 my @opt = ($now, '', setuprecur => 'recur', no_usage => 1);
142 # balance owed, recurring only, not including usage charges
144 if ( $mode eq 'billed' ) {
145 $unearned_base = '( ' .
146 FS::cust_bill_pkg->charged_sql(@opt) . ' - ' .
147 FS::cust_bill_pkg->credited_sql(@opt) . ' )';
148 } elsif ( $mode eq 'paid' ) {
149 $unearned_base = FS::cust_bill_pkg->paid_sql(@opt);
152 my $edate_zero = midnight_sql('edate');
153 my $sdate_zero = midnight_sql('sdate');
154 my $period = "CAST( ($edate_zero - $sdate_zero) / 86400.0 AS DECIMAL(10,0) )";
155 my $remaining = "GREATEST(
156 CAST( ($edate_zero - $now) / 86400.0 AS DECIMAL(10,0) ),
158 my $fraction = "$remaining / $period";
160 my $unearned_sql = "CAST(
161 GREATEST( $unearned_base * $fraction, 0 )
165 my $select = "SUM( $unearned_sql )";
167 if ( !$cgi->param('include_monthly') ) {
168 # all except freq != 0; one-time charges should never be included
170 AND part_pkg.freq != '1'
171 AND part_pkg.freq NOT LIKE '%h'
172 AND part_pkg.freq NOT LIKE '%d'
173 AND part_pkg.freq NOT LIKE '%w'";
176 # $mode actually doesn't matter here, since unpaid invoices have zero
180 "SELECT $select FROM cust_bill_pkg
181 LEFT JOIN cust_pkg ON (cust_bill_pkg.pkgnum = cust_pkg.pkgnum)
182 LEFT JOIN part_pkg USING ( pkgpart )
183 LEFT JOIN cust_main ON (cust_pkg.custnum = cust_main.custnum)
185 AND cust_bill_pkg.sdate < $now
186 AND cust_bill_pkg.edate > $now
187 AND cust_bill_pkg.recur != 0
188 AND part_pkg.freq != '0'
192 my $sth = dbh->prepare($sql) or die dbh->errstr;
193 $sth->execute or die $sth->errstr;
194 my $total = $sth->fetchrow_arrayref->[0];
196 $total = sprintf('%.2f', $total);
197 $total{$agentnum} = $total;
202 #not yet rewritten in sql, but now not enabled by default
205 grep { $_->part_pkg->recur != 0
206 && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/
209 'select' => 'cust_pkg.*',
210 'table' => 'cust_pkg',
211 'addl_from' => ' LEFT JOIN cust_main USING ( custnum ) ',
212 'hashref' => { 'bill' => { op=>'>', value=>$now } },
213 'extra_sql' => $where,
216 foreach my $cust_pkg ( @cust_pkg ) {
217 my $period = $cust_pkg->bill - $cust_pkg->last_bill;
219 my $elapsed = $now - $cust_pkg->last_bill;
220 $elapsed = 0 if $elapsed < 0;
222 my $remaining = 1 - $elapsed/$period;
224 my $unearned = $remaining * $cust_pkg->part_pkg->recur; #!! only works for flat/legacy
225 $total_legacy += $unearned;
229 $total_legacy = sprintf('%.2f', $total_legacy);
230 $total_legacy{$agentnum} = $total_legacy;
231 $total_legacy{0} += $total_legacy;
237 $total{0} = sprintf('%.2f', $total{0});
238 $total_legacy{0} = sprintf('%.2f', $total_legacy{0});