-<!-- mason kludge -->
-<%
+<% include("/elements/header.html", 'Prepaid Income (Unearned Revenue) Report',
+ menubar( 'Main Menu'=>$p, ) ) %>
+<% table() %>
+ <TR>
+ <TH>Actual Unearned Revenue</TH>
+ <TH>Legacy Unearned Revenue</TH>
+ </TR>
+ <TR>
+ <TD ALIGN="right">$<% $total %>
+ <TD ALIGN="right">
+ <% $now == $time ? "\$$total_legacy" : '<i>N/A</i>'%>
+ </TD>
+ </TR>
- #doesn't yet deal with daily/weekly packages
+</TABLE>
+<BR>
+Actual unearned revenue is the amount of unearned revenue Freeside has
+actually invoiced for packages with longer-than monthly terms.
+<BR><BR>
+Legacy unearned revenue is the amount of unearned revenue represented by
+customer packages. This number may be larger than actual unearned
+revenue if you have imported longer-than monthly customer packages from
+a previous billing system.
+</BODY>
+</HTML>
+<%init>
- #needs to be re-written in sql for efficiency
+die "access denied"
+ unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
- my $now = time;
+#doesn't yet deal with daily/weekly packages
- my %prepaid;
+#needs to be re-written in sql for efficiency
- my @cust_bill_pkg =
- grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
- qsearch( 'cust_bill_pkg', {
- 'recur' => { op=>'!=', value=>0 },
- 'edate' => { op=>'>', value=>$now },
- }, );
+my $time = time;
- foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
+my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time;
+$now =~ /^(\d+)$/ or die "unparsable date?";
+$now = $1;
- #conceptual false laziness w/texas tax exempt_amount stuff in
- #FS::cust_main::bill
+my( $total, $total_legacy ) = ( 0, 0 );
- my $freq = $cust_bill_pkg->cust_pkg->part_pkg->freq;
- my $per_month = sprintf("%.2f", $cust_bill_pkg->recur / $freq);
+my @cust_bill_pkg =
+ grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
+ qsearch( 'cust_bill_pkg', {
+ 'recur' => { op=>'!=', value=>0 },
+ 'edate' => { op=>'>', value=>$now },
+ }, );
- my($mon, $year) = (localtime($cust_bill_pkg->sdate) )[4,5];
- $mon+=2; $year+=1900;
+my @cust_pkg =
+ grep { $_->part_pkg->recur != 0
+ && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/
+ }
+ qsearch ( 'cust_pkg', {
+ 'bill' => { op=>'>', value=>$now }
+ } );
- foreach my $which_month ( 2 .. $freq ) {
- until ( $mon < 13 ) { $mon -= 12; $year++; }
- $prepaid{"$year-$mon"} += $per_month;
- }
+foreach my $cust_bill_pkg ( @cust_bill_pkg) {
+ my $period = $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
- }
+ my $elapsed = $now - $cust_bill_pkg->sdate;
+ $elapsed = 0 if $elapsed < 0;
-%>
+ my $remaining = 1 - $elapsed/$period;
-<%= header('Prepaid Income Report', menubar( 'Main Menu'=>$p, ) ) %>
-<%= table() %>
-<%
+ my $unearned = $remaining * $cust_bill_pkg->recur;
+ $total += $unearned;
- my $total = 0;
+}
- my ($now_mon, $now_year) = (localtime($now))[4,5];
- $now_mon+=2; $now_year+=1900;
- until ( $now_mon < 13 ) { $now_mon -= 12; $now_year++; }
+foreach my $cust_pkg ( @cust_pkg ) {
+ my $period = $cust_pkg->bill - $cust_pkg->last_bill;
- my $subseq = 0;
- for my $year ( $now_year .. 2037 ) {
- for my $mon ( ( $subseq++ ? 1 : $now_mon ) .. 12 ) {
- if ( $prepaid{"$year-$mon"} ) {
- $total += $prepaid{"$year-$mon"};
- %> <TR><TD><%= "$year-$mon" %></TD>
- <TD><%= sprintf("%.2f", $prepaid{"$year-$mon"} ) %></TD>
- </TR>
- <%
- }
- }
+ my $elapsed = $now - $cust_pkg->last_bill;
+ $elapsed = 0 if $elapsed < 0;
- }
+ my $remaining = 1 - $elapsed/$period;
-%>
-<TR><TH>Total</TH><TD><%= sprintf("%.2f", $total) %></TD></TR>
-</TABLE>
-</BODY>
-</HTML>
+ my $unearned = $remaining * $cust_pkg->part_pkg->recur; #!! only works for flat/legacy
+ $total_legacy += $unearned;
+
+}
+
+$total = sprintf('%.2f', $total);
+$total_legacy = sprintf('%.2f', $total_legacy);
+
+</%init>