X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Freport_prepaid_income.cgi;h=ce928b81cb72e1ef0aa869772d1e09933cdc8eff;hb=d99758f77f8d2f839934498af839109596e216b8;hp=9f045738a33da51feae64a76aa5b9cf20d4ea3cb;hpb=c9544281f45155587a54aa435500887d74e5c9f5;p=freeside.git diff --git a/httemplate/search/report_prepaid_income.cgi b/httemplate/search/report_prepaid_income.cgi index 9f045738a..ce928b81c 100644 --- a/httemplate/search/report_prepaid_income.cgi +++ b/httemplate/search/report_prepaid_income.cgi @@ -1,72 +1,111 @@ - -<% +<% include("/elements/header.html", 'Prepaid Income (Unearned Revenue) Report') %> + +<% table() %> + + Actual Unearned Revenue + Legacy Unearned Revenue + + + $<% $total %> + + <% $now == $time ? "\$$total_legacy" : 'N/A'%> + + - #doesn't yet deal with daily/weekly packages + +
+Actual unearned revenue is the amount of unearned revenue Freeside has +actually invoiced for packages with longer-than monthly terms. +

+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. + + +<%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 @where = (); - my $freq = $cust_bill_pkg->cust_pkg->part_pkg->freq; - my $per_month = sprintf("%.2f", $cust_bill_pkg->recur / $freq); +if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) { + my $agentnum = $1; + push @where, "agentnum = $agentnum"; +} - my($mon, $year) = (localtime($cust_bill_pkg->sdate) )[4,5]; - $mon+=2; $year+=1900; +#here is the agent virtualization +push @where, $FS::CurrentUser::CurrentUser->agentnums_sql; - foreach my $which_month ( 2 .. $freq ) { - until ( $mon < 13 ) { $mon -= 12; $year++; } - $prepaid{"$year-$mon"} += $per_month; - $mon++; - } +my $where = join(' AND ', @where); +$where = "AND $where" if $where; - } +my( $total, $total_legacy ) = ( 0, 0 ); - my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); +my @cust_bill_pkg = + grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ } + qsearch({ + 'select' => 'cust_bill_pkg.*', + 'table' => 'cust_bill_pkg', + 'addl_from' => ' LEFT JOIN cust_bill USING ( invnum ) '. + ' LEFT JOIN cust_main USING ( custnum ) ', + 'hashref' => { + 'recur' => { op=>'!=', value=>0 }, + 'edate' => { op=>'>', value=>$now }, + }, + 'extra_sql' => $where, + }); -%> +my @cust_pkg = + grep { $_->part_pkg->recur != 0 + && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/ + } + qsearch({ + 'select' => 'cust_pkg.*', + 'table' => 'cust_pkg', + 'addl_from' => ' LEFT JOIN cust_main USING ( custnum ) ', + 'hashref' => { 'bill' => { op=>'>', value=>$now } }, + 'extra_sql' => $where, + }); -<%= header('Prepaid Income Report', menubar( 'Main Menu'=>$p, ) ) %> -<%= table() %> -<% +foreach my $cust_bill_pkg ( @cust_bill_pkg) { + my $period = $cust_bill_pkg->edate - $cust_bill_pkg->sdate; - my $total = 0; + my $elapsed = $now - $cust_bill_pkg->sdate; + $elapsed = 0 if $elapsed < 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++; } + my $remaining = 1 - $elapsed/$period; - 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"}; - %> <%= $mon[$mon-1]. ' '. $year %> - - <%= sprintf("%.2f", $prepaid{"$year-$mon"} ) %> - - - <% - } - } + my $unearned = $remaining * $cust_bill_pkg->recur; + $total += $unearned; - } +} -%> -Total<%= sprintf("%.2f", $total) %> - - - +foreach my $cust_pkg ( @cust_pkg ) { + my $period = $cust_pkg->bill - $cust_pkg->last_bill; + + my $elapsed = $now - $cust_pkg->last_bill; + $elapsed = 0 if $elapsed < 0; + + my $remaining = 1 - $elapsed/$period; + + 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); + +