From 4f6e08d057dd504c886f1ba60692c8f9a41b3501 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 27 Oct 2019 11:02:51 +1300 Subject: [PATCH] Extract code to calculate how much of a line item has been paid --- CRM/Financial/BAO/Payment.php | 50 ++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index fa7345f13c..281e07ce47 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -632,25 +632,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) $ratio = 0; } foreach ($lineItems as $lineItemID => $lineItem) { - $lineItems[$lineItemID]['paid'] = 0; - $financialItems = civicrm_api3('FinancialItem', 'get', [ - 'entity_id' => $lineItemID, - 'entity_table' => 'civicrm_line_item', - 'options' => ['sort' => 'id DESC', 'limit' => 0], - ])['values']; - if (!empty($financialItems)) { - $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', [ - 'entity_table' => 'civicrm_financial_item', - 'entity_id' => ['IN' => array_keys($financialItems)], - 'options' => ['limit' => 0], - 'financial_trxn_id.is_payment' => 1, - ])['values']; - foreach ($entityFinancialTrxns as $entityFinancialTrxn) { - $lineItems[$lineItemID]['paid'] += $entityFinancialTrxn['amount']; - } - // @todo determine financial_item_id in this function - but first we need to settle on the right method. - // $lineItems[$lineItemID]['financial_item_id'] = key($financialItems); - } + $lineItems[$lineItemID]['paid'] = self::getAmountOfLineItemPaid($lineItemID); $lineItems[$lineItemID]['balance'] = $lineItem['subTotal'] - $lineItems[$lineItemID]['paid']; if (!empty($lineItemOverrides)) { @@ -663,4 +645,34 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) return $lineItems; } + /** + * Get the amount paid so far against this line item. + * + * @param int $lineItemID + * + * @return float + * + * @throws \CiviCRM_API3_Exception + */ + protected static function getAmountOfLineItemPaid($lineItemID) { + $paid = 0; + $financialItems = civicrm_api3('FinancialItem', 'get', [ + 'entity_id' => $lineItemID, + 'entity_table' => 'civicrm_line_item', + 'options' => ['sort' => 'id DESC', 'limit' => 0], + ])['values']; + if (!empty($financialItems)) { + $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', [ + 'entity_table' => 'civicrm_financial_item', + 'entity_id' => ['IN' => array_keys($financialItems)], + 'options' => ['limit' => 0], + 'financial_trxn_id.is_payment' => 1, + ])['values']; + foreach ($entityFinancialTrxns as $entityFinancialTrxn) { + $paid += $entityFinancialTrxn['amount']; + } + } + return (float) $paid; + } + } -- 2.25.1