From 00b4d571e978d786ddb83778355a257db77a24cf Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 6 Jul 2016 03:16:32 +0530 Subject: [PATCH] [ready-for-core-team-review]CRM-16189, added function to get previous financial item for line item (#8654) * CRM-16189, added function to get previous financial item for line item ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Added unit test for getPreviousFinancialItem ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- CRM/Financial/BAO/FinancialItem.php | 22 ++++++++++++ .../CRM/Financial/BAO/FinancialItemTest.php | 35 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/CRM/Financial/BAO/FinancialItem.php b/CRM/Financial/BAO/FinancialItem.php index 60786b6b42..244e5e9c53 100644 --- a/CRM/Financial/BAO/FinancialItem.php +++ b/CRM/Financial/BAO/FinancialItem.php @@ -275,4 +275,26 @@ WHERE cc.id IN (' . implode(',', $contactIds) . ') AND con.is_test = 0'; return FALSE; } + /** + * Get last financial item data. + * + * @param int $entityId + * + * @param string $entityTable + * + * @return object CRM_Core_DAO + */ + public static function getPreviousFinancialItem($entityId, $entityTable = 'civicrm_line_item') { + $queryParams = array( + 1 => array($entityId, 'Integer'), + 2 => array($entityTable, 'String'), + ); + $query = 'SELECT id, description, status_id, financial_account_id + FROM civicrm_financial_item + WHERE entity_id = %1 AND entity_table = %2 ORDER BY id DESC LIMIT 1'; + $prevFinancialItem = CRM_Core_DAO::executeQuery($query, $queryParams); + $prevFinancialItem->fetch(); + return $prevFinancialItem; + } + } diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialItemTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialItemTest.php index ca66fa63e1..a6d581dbb4 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialItemTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialItemTest.php @@ -266,4 +266,39 @@ class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase { $this->assertEquals($entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item'); } + /** + * Check method getPreviousFinancialItem(). + */ + public function testGetPreviousFinancialItem() { + $contactId = $this->individualCreate(); + + $params = array( + 'contact_id' => $contactId, + 'currency' => 'USD', + 'financial_type_id' => 1, + 'contribution_status_id' => 1, + 'payment_instrument_id' => 1, + 'source' => 'STUDENT', + 'receive_date' => '20160522000000', + 'receipt_date' => '20160522000000', + 'non_deductible_amount' => 0.00, + 'total_amount' => 100.00, + 'trxn_id' => '22ereerwww444444', + 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81', + ); + + $contribution = CRM_Contribute_BAO_Contribution::create($params); + + $params = array( + 'id' => $contribution->id, + 'total_amount' => 300.00, + ); + + $contribution = CRM_Contribute_BAO_Contribution::create($params); + $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution->id); + $params = array('id' => $financialItem->id); + $financialItem = $this->callAPISuccess('FinancialItem', 'get', $params); + $this->assertEquals($financialItem['values'][$financialItem['id']]['amount'], 200.00, "The amounts do not match."); + } + } -- 2.25.1