summaryrefslogtreecommitdiff
path: root/httemplate
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2012-06-09 20:26:01 -0700
committerMark Wells <mark@freeside.biz>2012-06-09 20:26:01 -0700
commit10d114817c3d17c778444cc96a799d9d2b042224 (patch)
tree8b4f5a2dd68f050c7413e72083467200eb42c6ee /httemplate
parenta1eb54c2661dad29992603e78682d9be2d372625 (diff)
further changes to unearned income report, #13289
Diffstat (limited to 'httemplate')
-rw-r--r--httemplate/search/cust_bill_pkg.cgi46
-rw-r--r--httemplate/search/prepaid_income.html21
2 files changed, 45 insertions, 22 deletions
diff --git a/httemplate/search/cust_bill_pkg.cgi b/httemplate/search/cust_bill_pkg.cgi
index a84f981e3..f9dd4a20e 100644
--- a/httemplate/search/cust_bill_pkg.cgi
+++ b/httemplate/search/cust_bill_pkg.cgi
@@ -367,28 +367,44 @@ if ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
my $usage_sql = FS::cust_bill_pkg->usage_sql;
push @select, "($usage_sql) AS usage"; # we need this
- my $owed_sql = FS::cust_bill_pkg->owed_sql($unearned, '',
- setuprecur => 'recur', no_usage => 1);
- push @select, "($owed_sql) AS owed"; # also this
+ my $paid_sql = 'GREATEST(' .
+ FS::cust_bill_pkg->paid_sql($unearned, '', setuprecur => 'recur') .
+ " - $usage_sql, 0)";
+
+ push @select, "$paid_sql AS paid_no_usage"; # need this either way
+
+ if ( $unearned_mode eq 'paid' ) {
+ # then use the amount paid, minus usage charges
+ $unearned_base = $paid_sql;
+ }
+ else {
+ # use the amount billed, minus usage charges and credits
+ $unearned_base = "GREATEST( cust_bill_pkg.recur - ".
+ FS::cust_bill_pkg->credited_sql($unearned, '', setuprecur => 'recur') .
+ " - $usage_sql, 0)";
+ # include only rows that have some non-usage, non-credited portion
+ }
+ # whatever we're using as the base, only show rows where it's positive
+ push @where, "$unearned_base > 0";
- $unearned_base = "(cust_bill_pkg.recur - $usage_sql)";
my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS REAL)";
my $elapsed = "GREATEST( $unearned - cust_bill_pkg.sdate, 0 )";
my $remaining = "(1 - $elapsed/$period)";
- $unearned_sql = "CAST(
- GREATEST( ( $unearned_base * $remaining ) - $owed_sql, 0 )
- AS DECIMAL(10,2)
- )";
+ $unearned_sql = "CAST( $unearned_base * $remaining AS DECIMAL(10,2) )";
push @select, "$unearned_sql AS unearned_revenue";
- if ( $unearned_mode eq 'billed' ) {
- # include only rows that have some unearned portion
- push @where, "$unearned_base > 0";
- }
- elsif ( $unearned_mode eq 'paid' ) {
- # include only those that have some _paid_ unearned portion
- push @where, "$unearned_sql > 0";
+ # last payment/credit date
+ my %t = (pay => 'cust_bill_pay', credit => 'cust_credit_bill');
+ foreach my $x (qw(pay credit)) {
+ my $table = $t{$x};
+ my $link = $table.'_pkg';
+ my $pkey = dbdef->table($table)->primary_key;
+ my $last_date_sql = "SELECT MAX(_date)
+ FROM $table JOIN $link USING ($pkey)
+ WHERE $link.billpkgnum = cust_bill_pkg.billpkgnum
+ AND $table._date <= $unearned";
+ push @select, "($last_date_sql) AS last_$x";
}
}
diff --git a/httemplate/search/prepaid_income.html b/httemplate/search/prepaid_income.html
index deed33a33..d02345edc 100644
--- a/httemplate/search/prepaid_income.html
+++ b/httemplate/search/prepaid_income.html
@@ -98,7 +98,7 @@ $dt->add(days => 1); # the day after that
$dt->subtract(seconds => 1); # the last second of the report day
$now = $dt->epoch;
-my $link = "cust_bill_pkg.cgi?nottax=1;unearned_now=$now;mode=$mode";
+my $link = "unearned_detail.html?date=$now;mode=$mode";
if ( $cgi->param('include_monthly') ) {
$link .= ';include_monthly=1';
@@ -138,17 +138,22 @@ foreach my $agentnum (@agentnums) {
my( $total, $total_legacy ) = ( 0, 0 );
- # false laziness
- my $usage_sql = FS::cust_bill_pkg->usage_sql;
- my $owed_sql = FS::cust_bill_pkg->owed_sql($now, '',
- setuprecur => 'recur', no_usage => 1);
- my $unearned_base = "(cust_bill_pkg.recur - $usage_sql)";
+ my @opt = ($now, '', setuprecur => 'recur', no_usage => 1);
+ # balance owed, recurring only, not including usage charges
+ my $unearned_base;
+ if ( $mode eq 'billed' ) {
+ $unearned_base = '( ' .
+ FS::cust_bill_pkg->charged_sql(@opt) . ' - ' .
+ FS::cust_bill_pkg->credited_sql(@opt) . ' )';
+ } elsif ( $mode eq 'paid' ) {
+ $unearned_base = FS::cust_bill_pkg->paid_sql(@opt);
+ }
my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS REAL)";
my $elapsed = "GREATEST( $now - cust_bill_pkg.sdate, 0 )";
my $remaining = "(1 - $elapsed/$period)";
my $unearned_sql = "CAST(
- GREATEST( $unearned_base * $remaining - $owed_sql, 0 )
+ GREATEST( $unearned_base * $remaining, 0 )
AS DECIMAL(10,2)
)";
@@ -180,6 +185,8 @@ foreach my $agentnum (@agentnums) {
AND part_pkg.freq != '0'
$where
";
+
+ warn $sql;
my $sth = dbh->prepare($sql) or die dbh->errstr;
$sth->execute or die $sth->errstr;
my $total = $sth->fetchrow_arrayref->[0];