X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_pkg.pm;h=ee9afa3e9f8f093f06b89ca5f50d6c97dac19595;hb=a36d5bbb21a67515e6560547669bd80fc2b3a8d9;hp=467b54d718a92f1a5ae4ba58b7f6409df71025a3;hpb=6d2db824c90c29789852fb1a7645400c90032091;p=freeside.git diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index 467b54d71..ee9afa3e9 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -1792,6 +1792,8 @@ sub credit_remaining { # the cancellation date (can happen with advance billing). in that # case, use the entire recurring charge: my $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage; + my $max_credit = $amount + - $cust_bill_pkg->credited('', '', setuprecur => 'recur') || 0; # but if the cancellation happens during the interval, prorate it: # (XXX obey prorate_round_day here?) @@ -1800,14 +1802,23 @@ sub credit_remaining { ($edate - $time) / ($edate - $cust_bill_pkg->sdate); } + # if there are existing credits, don't let the sum of credits exceed + # the recurring charge + $amount = $max_credit if $amount > $max_credit; + $amount = sprintf('%.2f', $amount); - push @billpkgnums, $cust_bill_pkg->billpkgnum; - push @amounts, $amount; - push @setuprecurs, 'recur'; + # if no time has been used and/or there are existing line item + # credits, we may end up not needing to credit anything. + if ( $amount > 0 ) { - warn "Crediting for $amount on package ".$remain_pkg->pkgnum."\n" - if $DEBUG; + push @billpkgnums, $cust_bill_pkg->billpkgnum; + push @amounts, $amount; + push @setuprecurs, 'recur'; + + warn "Crediting for $amount on package ".$remain_pkg->pkgnum."\n" + if $DEBUG; + } } @@ -5031,6 +5042,17 @@ sub cancel_sql { "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0"; } +=item ncancelled_recurring_sql + +Returns an SQL expression identifying un-cancelled, recurring packages. + +=cut + +sub ncancelled_recurring_sql { + $_[0]->recurring_sql(). + " AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 ) "; +} + =item status_sql Returns an SQL expression to give the package status as a string.