X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill_pkg.cgi;h=f9dd4a20e52c1288e5631a26f0f57ba656306bde;hp=a84f981e3aea37851d54853dd5facc18121bf0ec;hb=10d114817c3d17c778444cc96a799d9d2b042224;hpb=a1eb54c2661dad29992603e78682d9be2d372625 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"; } }