From: Mark Wells Date: Mon, 17 Oct 2016 23:15:54 +0000 (-0700) Subject: Merge branch 'FREESIDE_3_BRANCH' of git.freeside.biz:/home/git/freeside into 3.x X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=7334227ea36b450e71c028e7ba564f91e7d4d1de;hp=eade44a93547afab49b043ac9b66ca432cffab21 Merge branch 'FREESIDE_3_BRANCH' of git.freeside.biz:/home/git/freeside into 3.x --- diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index 18f4a3246..ca410757b 100644 --- a/FS/FS/cust_credit.pm +++ b/FS/FS/cust_credit.pm @@ -743,10 +743,12 @@ sub calculate_tax_adjustment { if ($recur) { $recur -= $cust_bill_pkg->credited('', '', setuprecur => 'recur') || 0; } + # Skip line items that have been completely credited. + next if ($setup + $recur) == 0; my $setup_ratio = $setup / ($setup + $recur); - # Calculate the fraction of tax to credit: it's the fraction of this charge - # (either setup or recur) that's being credited. + # Calculate the fraction of tax to credit: it's the fraction of this + # charge (either setup or recur) that's being credited. my $charged = ($setuprecur eq 'setup') ? $setup : $recur; next if $charged == 0; # shouldn't happen, but still... @@ -911,7 +913,7 @@ sub credit_lineitems { my $invnum = $cust_bill_pkg->invnum; $need_to_unapply -= $cust_bill_pkg->owed($setuprecur); - next if $need_to_unapply < 0.005; + return if $need_to_unapply < 0.005; my $error; # then unapply payments one at a time (partially if need be) until the diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index 467b54d71..b5506f63e 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; + } }