Skip to content

Commit

Permalink
Ensure payment amount are allocated to lineitems without roundoff error
Browse files Browse the repository at this point in the history
  • Loading branch information
olayiwola-compucorp committed Mar 26, 2024
1 parent 0de1660 commit 6d5ed9a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ protected static function getPayableLineItems($params, $contribution): array {
}
}
$outstandingBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($params['contribution_id']);
$isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount'], '');
if ($outstandingBalance !== 0.0) {
$ratio = $params['total_amount'] / $outstandingBalance;
}
Expand All @@ -567,19 +568,32 @@ protected static function getPayableLineItems($params, $contribution): array {
}
else {
if (empty($lineItems[$lineItemID]['balance']) && !empty($ratio) && $params['total_amount'] < 0) {
$lineItems[$lineItemID]['allocation'] = $lineItem['subTotal'] * $ratio;
$lineItems[$lineItemID]['allocation'] = round($lineItem['subTotal'] * $ratio, 2);
}
elseif ($isPaymentCompletesContribution) {
$lineItems[$lineItemID]['allocation'] = $lineItems[$lineItemID]['balance'];
}
else {
$lineItems[$lineItemID]['allocation'] = $lineItems[$lineItemID]['balance'] * $ratio;
$lineItems[$lineItemID]['allocation'] = round($lineItems[$lineItemID]['balance'] * $ratio, 2);
}

if (!empty($lineItem['tax_amount'])) {
$lineItems[$lineItemID]['tax_allocation'] = $lineItem['tax_amount'] * ($params['total_amount'] / $contribution['total_amount']);
$lineItems[$lineItemID]['tax_allocation'] = round($lineItem['tax_amount'] * ($params['total_amount'] / $contribution['total_amount']), 2);
}

}
}

if (empty($lineItemOverrides)) {
$totalTaxAllocation = array_sum(array_column($lineItems, 'tax_allocation'));
$totalAllocation = array_sum(array_column($lineItems, 'allocation'));
$total = $totalTaxAllocation + $totalAllocation;
$leftPayment = $params['total_amount'] - $total;

// assign any leftover amount, to the last lineitem
$lineItems[$lineItemID]['allocation'] += $leftPayment;
}

return $lineItems;
}

Expand Down

0 comments on commit 6d5ed9a

Please sign in to comment.