From 44dcd4a1ff335a85a6babf0e007be57e6ec4f525 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Wed, 21 Sep 2016 14:30:01 -0700 Subject: [PATCH] and still create credit source records on 4.x+, #42729 --- FS/FS/cust_credit.pm | 28 ++++++++++++++++++++++++++++ FS/FS/cust_pkg.pm | 1 + FS/FS/part_pkg/flat.pm | 2 ++ 3 files changed, 31 insertions(+) diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index 3fea56142..aebf25922 100644 --- a/FS/FS/cust_credit.pm +++ b/FS/FS/cust_credit.pm @@ -798,6 +798,7 @@ Example: 'setuprecurs' => \@setuprecurs, 'amounts' => \@amounts, 'apply' => 1, #0 leaves the credit unapplied + 'set_source' => 1, #creates credit source records for the line items #the credit map { $_ => scalar($cgi->param($_)) } @@ -862,6 +863,8 @@ sub credit_lineitems { $arg{amount} = sprintf('%.2f', $tax_adjust{subtotal} + $tax_adjust{taxtotal}); } + my $set_source = $arg{set_source}; + # create the credit my $cust_credit = new FS::cust_credit ( { map { $_ => $arg{$_} } @@ -885,6 +888,7 @@ sub credit_lineitems { my %cust_bill_pkg = (); my %cust_credit_bill_pkg = (); my %unapplied_payments = (); #invoice numbers, and then billpaynums + my %currency; # little private function to unapply payments from a cust_bill_pkg until # there's a specified amount of unpaid balance on it. @@ -957,6 +961,17 @@ sub credit_lineitems { # unapply payments if necessary $error = &{$unapply_sub}($cust_bill_pkg, $setuprecur, $amount); + if ( $set_source ) { + $currency{$invnum} ||= $cust_bill_pkg->cust_bill->currency; + my $source = FS::cust_credit_source_bill_pkg->new({ + 'crednum' => $cust_credit->crednum, + 'billpkgnum' => $billpkgnum, + 'amount' => $amount, + 'currency' => $currency{invnum}, + }); + $error ||= $source->insert; + } + if ( $error ) { $dbh->rollback if $oldAutoCommit; return "Error unapplying payment: $error"; @@ -994,6 +1009,19 @@ sub credit_lineitems { }; $error = &{$unapply_sub}($cust_bill_pkg, 'setup', $amount); + + # I guess it's correct to do this for taxes also? + if ( $set_source ) { + $currency{$invnum} ||= $cust_bill_pkg->cust_bill->currency; + my $source = FS::cust_credit_source_bill_pkg->new({ + 'crednum' => $cust_credit->crednum, + 'billpkgnum' => $billpkgnum, + 'amount' => $amount, + 'currency' => $currency{invnum}, + }); + $error ||= $source->insert; + } + if ( $error ) { $dbh->rollback if $oldAutoCommit; return "Error unapplying payment: $error"; diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index e9d80da0b..2c7550633 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -1859,6 +1859,7 @@ sub credit_remaining { 'date' => time, 'reasonnum' => $reason->reasonnum, 'apply' => 1, + 'set_source' => 1, ); ''; diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 84599ea8a..97d4363da 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -225,6 +225,8 @@ sub calc_cancel { } } +# no longer used; see credit_remaining in FS::cust_pkg + sub calc_remain { my ($self, $cust_pkg, %options) = @_; -- 2.11.0