Skip to content

Commit

Permalink
[REF] permit negative payments to be made against completed payments
Browse files Browse the repository at this point in the history
  • Loading branch information
seamuslee001 committed Aug 3, 2020
1 parent 0e3d61b commit d55ff32
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/api/v3/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public function testGetPaymentWithTrxnID() {
],
];
$this->checkPaymentResult($payment, $expectedResult);
$this->callAPISuccess('Payment', 'create', ['total_amount' => '-20', 'contribution_id' => $contributionID2]);
$this->validateAllPayments();
}

Expand Down

0 comments on commit d55ff32

Please sign in to comment.