From d55ff327b09875a213821b2ccdc6860cd4323812 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 13 Jul 2020 14:38:20 +1000 Subject: [PATCH] [REF] permit negative payments to be made against completed payments --- CRM/Financial/BAO/Payment.php | 11 +++++++++-- tests/phpunit/api/v3/PaymentTest.php | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index f841363f141f..1dabc550e504 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -486,6 +486,9 @@ protected static function getPayableLineItems($params): array { if ($outstandingBalance !== 0.0) { $ratio = $params['total_amount'] / $outstandingBalance; } + elseif ($params['total_amount'] < 0) { + $ratio = $params['total_amount'] / (float) CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['contribution_id'], TRUE); + } else { // Help we are making a payment but no money is owed. We won't allocate the overpayment to any line item. $ratio = 0; @@ -495,12 +498,16 @@ protected static function getPayableLineItems($params): array { $lineItems[$lineItemID]['id'] = $lineItemID; $lineItems[$lineItemID]['paid'] = self::getAmountOfLineItemPaid($lineItemID); $lineItems[$lineItemID]['balance'] = $lineItem['subTotal'] - $lineItems[$lineItemID]['paid']; - if (!empty($lineItemOverrides)) { $lineItems[$lineItemID]['allocation'] = $lineItemOverrides[$lineItemID] ?? NULL; } else { - $lineItems[$lineItemID]['allocation'] = $lineItems[$lineItemID]['balance'] * $ratio; + if (empty($lineItems[$lineItemID]['balance']) && !empty($ratio) && $params['total_amount'] < 0) { + $lineItems[$lineItemID]['allocation'] = $lineItem['subTotal'] * $ratio; + } + else { + $lineItems[$lineItemID]['allocation'] = $lineItems[$lineItemID]['balance'] * $ratio; + } } } return $lineItems; diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 046f1f6058df..2d3da04872e1 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -183,6 +183,7 @@ public function testGetPaymentWithTrxnID() { ], ]; $this->checkPaymentResult($payment, $expectedResult); + $this->callAPISuccess('Payment', 'create', ['total_amount' => '-20', 'contribution_id' => $contributionID2]); $this->validateAllPayments(); }