From 8e5d6bd48c22d74daff9e577c8caa2b8a80a109f Mon Sep 17 00:00:00 2001 From: Muhammad Shahrukh <> Date: Wed, 10 Jul 2024 14:10:53 +0500 Subject: [PATCH] PROD-180: Resolve Issue with Payment Allocation to Contribution Line Item Applying PR https://github.com/civicrm/civicrm-core/pull/29823 Partially Included in 5.74 --- CRM/Financial/BAO/Payment.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 9144f4805bc0..b1f9279259ad 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -536,6 +536,7 @@ protected static function getPayableItems(array $params, array $contribution): a } } + $isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount'], ''); $items = LineItem::get(FALSE) ->addSelect('*', 'financial_item.status_id:name', 'financial_item.id', 'financial_item.financial_account_id', 'financial_item_id.currency', 'financial_item.financial_account_id.is_tax', 'financial_item.entity_id', 'financial_item.amount', 'allocated.amount') ->addJoin( @@ -579,15 +580,32 @@ protected static function getPayableItems(array $params, array $contribution): a } else { if (empty($item['balance']) && !empty($ratio) && $params['total_amount'] < 0) { - $item['allocation'] = $item['item_total'] * $ratio; + $item['allocation'] = round($item['item_total'] * $ratio, 2); + } + elseif ($isPaymentCompletesContribution) { + $item['allocation'] = $item['balance']; } else { - $item['allocation'] = $item['balance'] * $ratio; + $item['allocation'] = round($item['balance'] * $ratio, 2); + } + + if (!empty($item['tax_amount'])) { + $item['tax_allocation'] = round($item['tax_amount'] * ($params['total_amount'] / $contribution['total_amount']), 2); } } $payableItems[$payableItemIndex] = $item; } + if (empty($lineItemOverrides) && !empty($ratio)) { + $totalTaxAllocation = array_sum(array_column($payableItems, 'tax_allocation')); + $totalAllocation = array_sum(array_column($payableItems, 'allocation')); + $total = $totalTaxAllocation + $totalAllocation; + $leftPayment = $params['total_amount'] - $total; + + // assign any leftover amount, to the last lineitem + $payableItems[$payableItemIndex]['allocation'] += $leftPayment; + } + return $payableItems; }