From: Pradeep Nayak Date: Thu, 7 Jul 2016 19:58:14 +0000 (+0530) Subject: [ready-for-core-team-review]CRM-16189, added function to create deferred transactions... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6419695fe5228422acdb38f4030f3a6509f14365;p=civicrm-core.git [ready-for-core-team-review]CRM-16189, added function to create deferred transactions (#8579) * CRM-16189, added function to create deferred transactions ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 Conflicts: CRM/Core/BAO/FinancialTrxn.php * CRM-16189, updated function ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 Conflicts: CRM/Core/BAO/FinancialTrxn.php * CRM-16189, fixed Jenkin errors ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Added unit test for createDeferredTrxn function ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Resolved jenkins errors ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Removed count since individual test passes but multiple tests do not clear the db ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Added params to unit test to avoid clashing with results from other test ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Removed unintentional rebase code ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index 6f53352283..cfc66df199 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -597,4 +597,104 @@ WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFina return $revenueAmount; } + /** + * Create transaction for deferred revenue. + * + * @param array $lineItems + * + * @param array $contributionDetails + * + * @param bool $update + * + * @param string $context + * + */ + public static function createDeferredTrxn($lineItems, $contributionDetails, $update = FALSE, $context = NULL) { + if (empty($lineItems)) { + return FALSE; + } + $revenueRecognitionDate = $contributionDetails->revenue_recognition_date; + if (!CRM_Utils_System::isNull($revenueRecognitionDate)) { + $statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + if (!$update + && (CRM_Utils_Array::value($contributionDetails->contribution_status_id, $statuses) != 'Completed' + || (CRM_Utils_Array::value($contributionDetails->contribution_status_id, $statuses) != 'Pending' + && $contributionDetails->is_pay_later) + ) + ) { + return; + } + $trxnParams = array( + 'contribution_id' => $contributionDetails->id, + 'fee_amount' => '0.00', + 'currency' => $contributionDetails->currency, + 'trxn_id' => $contributionDetails->trxn_id, + 'status_id' => $contributionDetails->contribution_status_id, + 'payment_instrument_id' => $contributionDetails->payment_instrument_id, + 'check_number' => $contributionDetails->check_number, + 'is_payment' => 1, + ); + + $deferredRevenues = array(); + foreach ($lineItems as $priceSetID => $lineItem) { + if (!$priceSetID) { + continue; + } + foreach ($lineItem as $key => $item) { + if ($item['line_total'] <= 0 && !$update) { + continue; + } + $deferredRevenues[$key] = $item; + if ($context == 'changeFinancialType') { + $deferredRevenues[$key]['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_LineItem', $item['id'], 'financial_type_id'); + } + if (in_array($item['entity_table'], + array('civicrm_participant', 'civicrm_contribution')) + ) { + $deferredRevenues[$key]['revenue'][] = array( + 'amount' => $item['line_total'], + 'revenue_date' => $revenueRecognitionDate, + ); + } + else { + // for membership + $deferredRevenues[$key]['revenue'] = self::getMembershipRevenueAmount($item); + } + } + } + $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); + // TODO: Call hook to alter $deferredRevenues + foreach ($deferredRevenues as $key => $deferredRevenue) { + $results = civicrm_api3('EntityFinancialAccount', 'get', array( + 'entity_table' => 'civicrm_financial_type', + 'entity_id' => $deferredRevenue['financial_type_id'], + 'account_relationship' => array('IN' => array('Income Account is', 'Deferred Revenue Account is')), + )); + if ($results['count'] != 2) { + continue; + } + foreach ($results['values'] as $result) { + if ($result['account_relationship'] == $accountRel) { + $trxnParams['to_financial_account_id'] = $result['financial_account_id']; + } + else { + $trxnParams['from_financial_account_id'] = $result['financial_account_id']; + } + } + foreach ($deferredRevenue['revenue'] as $revenue) { + $trxnParams['total_amount'] = $trxnParams['net_amount'] = $revenue['amount']; + $trxnParams['trxn_date'] = CRM_Utils_Date::isoToMysql($revenue['revenue_date']); + $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams); + $entityParams = array( + 'entity_id' => $deferredRevenue['financial_item_id'], + 'entity_table' => 'civicrm_financial_item', + 'amount' => $revenue['amount'], + 'financial_trxn_id' => $financialTxn->id, + ); + civicrm_api3('EntityFinancialTrxn', 'create', $entityParams); + } + } + } + } + } diff --git a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php index f7c2226d51..adfaa78a0c 100644 --- a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php +++ b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php @@ -129,4 +129,46 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase { $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.'); } + /** + * Test for createDeferredTrxn(). + */ + public function testCreateDeferredTrxn() { + Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1')); + $cid = $this->individualCreate(); + $params = array( + 'contact_id' => $cid, + 'receive_date' => '2016-01-20', + 'total_amount' => 622, + 'financial_type_id' => 4, + 'revenue_recognition_date' => date('Ymd', strtotime("+1 month")), + 'line_items' => array( + array( + 'line_item' => array( + array( + 'entity_table' => 'civicrm_contribution', + 'price_field_id' => 8, + 'price_field_value_id' => 16, + 'label' => 'test 1', + 'qty' => 1, + 'unit_price' => 100, + 'line_total' => 100, + 'financial_type_id' => 4, + ), + ), + 'params' => array(), + ), + ), + ); + $contribution = CRM_Contribute_BAO_Contribution::create($params); + $lineItems[1] = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution->id); + $lineItemId = key($lineItems[1]); + $lineItems[1][$lineItemId]['financial_item_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_item WHERE entity_table = 'civicrm_line_item' AND entity_id = {$lineItemId}"); + // Get financial trxns for contribution + $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622)); + $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('2016-01-20'))); + CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($lineItems, $contribution); + $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622, 'id' => array("NOT IN" => array($trxn['id'])))); + $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month"))); + } + }