From e3a78cbac3442f7fc3dca570655f97e3f5f56acb Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 3 Aug 2017 17:20:11 +0530 Subject: [PATCH] added unit test and other changes --- CRM/Contribute/BAO/Contribution.php | 4 +- CRM/Core/BAO/FinancialTrxn.php | 4 + CRM/Financial/Form/PaymentEdit.php | 74 +++++++---- api/v3/FinancialTrxn.php | 5 +- .../CRM/Financial/Form/PaymentEditTest.php | 121 ++++++++++++++++++ 5 files changed, 183 insertions(+), 25 deletions(-) create mode 100644 tests/phpunit/CRM/Financial/Form/PaymentEditTest.php diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index ae02b059fc..2c4256c218 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4177,7 +4177,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) 'name' => "", 'url' => 'civicrm/payment/edit', 'class' => 'medium-popup', - 'qs' => "reset=1&id=%%id%%", + 'qs' => "reset=1&id=%%id%%&contri_id=%%contri_id%%", 'title' => ts('Edit Payment'), ), ); @@ -4186,11 +4186,13 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) CRM_Core_Action::mask(array(CRM_Core_Permission::EDIT)), array( 'id' => $resultDAO->id, + 'contri_id' => $contributionId, ) ); } $val = array( + 'id' => $resultDAO->id, 'total_amount' => $resultDAO->total_amount, 'financial_type' => $resultDAO->financial_account, 'payment_instrument' => $paidByLabel, diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index 93300a1b89..b2db2603bd 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -77,6 +77,10 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn { $entityFinancialTrxnParams['entity_table'] = $trxnEntityTable['entity_table']; $entityFinancialTrxnParams['entity_id'] = $trxnEntityTable['entity_id']; } + elseif (!empty($params['entity_id']) && !empty($params['entity_table'])) { + $entityFinancialTrxnParams['entity_table'] = $params['entity_table']; + $entityFinancialTrxnParams['entity_id'] = $params['entity_id']; + } else { $entityFinancialTrxnParams['entity_id'] = $params['contribution_id']; } diff --git a/CRM/Financial/Form/PaymentEdit.php b/CRM/Financial/Form/PaymentEdit.php index e7948729c1..9a5b21c297 100644 --- a/CRM/Financial/Form/PaymentEdit.php +++ b/CRM/Financial/Form/PaymentEdit.php @@ -39,6 +39,13 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { */ protected $_id; + /** + * The id of the related contribution ID + * + * @var int + */ + protected $_contributionID; + /** * The variable which holds the information of a financial transaction * @@ -60,6 +67,7 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { parent::preProcess(); $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); $this->assign('id', $this->_id); + $this->_contributionID = CRM_Utils_Request::retrieve('contri_id', 'Positive', $this); $this->_values = civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $this->_id)); if (!empty($this->_values['payment_processor_id'])) { @@ -164,45 +172,65 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { $params['check_number'] = CRM_Utils_Array::value('check_number', $this->_submitValues); } - if ($this->_submitValues['payment_instrument_id'] != $this->_values['payment_instrument_id']) { - //first reverse previous transaction + $this->submit($params); + + CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath())); + } + + + /** + * Wrapper function to process form submission + * + * @param array $submittedValues + * + */ + protected function submit($submittedValues) { + // if payment instrument is changed then + // 1. Record a new reverse financial transaction with old payment instrument + // 2. Record a new financial transaction with new payment instrument + // 3. Add EntityFinancialTrxn records to relate with corresponding financial item and contribution + if ($submittedValues['payment_instrument_id'] != $this->_values['payment_instrument_id']) { $previousFinanciaTrxn = $this->_values; - unset($previousFinanciaTrxn['id'], $params['id']); - $previousFinanciaTrxn['trxn_date'] = CRM_Utils_Array::value('trxn_date', $params, date('YmdHis')); + $newFinancialTrxn = $submittedValues; + unset($previousFinanciaTrxn['id'], $newFinancialTrxn['id']); + $previousFinanciaTrxn['trxn_date'] = CRM_Utils_Array::value('trxn_date', $submittedValues, date('YmdHis')); $previousFinanciaTrxn['total_amount'] = -$previousFinanciaTrxn['total_amount']; $previousFinanciaTrxn['net_amount'] = -$previousFinanciaTrxn['net_amount']; $previousFinanciaTrxn['fee_amount'] = -$previousFinanciaTrxn['fee_amount']; - $previousFinanciaTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($params['payment_instrument_id']); - $previousFinanciaTrxn['contribution_id'] = self::getContributionIDbyTrxn($this->_id); - CRM_Core_BAO_FinancialTrxn::create($previousFinanciaTrxn); + $previousFinanciaTrxn['contribution_id'] = $newFinancialTrxn['contribution_id'] = $this->_contributionID; - $params['to_financial_account_id'] = $previousFinanciaTrxn['to_financial_account_id']; - $params['contribution_id'] = $previousFinanciaTrxn['contribution_id']; + $newFinancialTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($submittedValues['payment_instrument_id']); foreach (array('total_amount', 'fee_amount', 'net_amount', 'currency', 'is_payment', 'status_id') as $fieldName) { - $params[$fieldName] = $this->_values[$fieldName]; + $newFinancialTrxn[$fieldName] = $this->_values[$fieldName]; + } + + foreach (array($previousFinanciaTrxn, $newFinancialTrxn) as $financialTrxnParams) { + civicrm_api3('FinancialTrxn', 'create', $financialTrxnParams); + $trxnParams = array( + 'total_amount' => $financialTrxnParams['total_amount'], + 'contribution_id' => $this->_contributionID, + ); + $contributionTotalAmount = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionID, 'total_amount'); + CRM_Contribute_BAO_Contribution::assignProportionalLineItems($trxnParams, $submittedValues['id'], $contributionTotalAmount); } - CRM_Core_BAO_FinancialTrxn::create($params); } else { // simply update the financial trxn - civicrm_api3('FinancialTrxn', 'create', $params); + civicrm_api3('FinancialTrxn', 'create', $submittedValues); } - CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath())); } /** - * Get contribution ID from financial trx ID - * @param int $financialTrxnID + * Wrapper for unit testing the post process submit function. * - * @return int contribution ID + * @param array $params */ - public static function getContributionIDbyTrxn($financialTrxnID) { - return CRM_Core_DAO::singleValueQuery(" - SELECT entity_id - FROM civicrm_entity_financial_trxn - WHERE entity_table = 'civicrm_contribution' AND financial_trxn_id = %1 - LIMIT 1 - ", array(1 => array($financialTrxnID, 'Integer'))); + public function testSubmit($params) { + $this->_id = $params['id']; + $this->_contributionID = $params['contribution_id']; + $this->_values = civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $params['id'])); + + $this->submit($params); } /** diff --git a/api/v3/FinancialTrxn.php b/api/v3/FinancialTrxn.php index 6a5c3f1dd5..5e5958b1f9 100644 --- a/api/v3/FinancialTrxn.php +++ b/api/v3/FinancialTrxn.php @@ -39,7 +39,10 @@ * @return array */ function civicrm_api3_financial_trxn_create($params) { - return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); + if (empty($params['contribution_id']) && (empty($params['entity_table']) && empty($params['entity_id']))) { + return civicrm_api3_create_error('Please provide finanical trxn entity details.'); + } + return _civicrm_api3_basic_create('CRM_Core_BAO_FinancialTrxn', $params); } /** diff --git a/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php b/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php new file mode 100644 index 0000000000..cea667b581 --- /dev/null +++ b/tests/phpunit/CRM/Financial/Form/PaymentEditTest.php @@ -0,0 +1,121 @@ +createLoggedInUser(); + + $this->_individualID = $this->individualCreate(); + } + + /** + * Clean up after each test. + */ + public function tearDown() { + $this->quickCleanUpFinancialEntities(); + $this->quickCleanup(array('civicrm_note', 'civicrm_uf_match', 'civicrm_address')); + } + + /** + * Test the submit function of payment edit form. + */ + public function testSubmitOnPaymentInstrumentChange() { + // First create a contribution using 'Check' as payment instrument + $form = new CRM_Contribute_Form_Contribution(); + $form->testSubmit(array( + 'total_amount' => 50, + 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), + 'receive_date' => '04/21/2015', + 'receive_date_time' => '11:27PM', + 'contact_id' => $this->_individualID, + 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Check'), + 'check_number' => '123XA', + 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), + ), + CRM_Core_Action::ADD); + // fetch the financial trxn record later used in setting default values of payment edit form + $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualID)); + $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE); + $financialTrxnInfo = $payments['transaction'][0]; + + // build parameters which changed payment instrument and tran date values + $params = array( + 'id' => $financialTrxnInfo['id'], + 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit Card'), + 'card_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', 'Visa'), + 'pan_truncation' => 1111, + 'trnx_id' => 'txn_12AAAA', + 'trxn_date' => date('Y-m-d H:i:s'), + 'contribution_id' => $contribution['id'], + ); + $form = new CRM_Financial_Form_PaymentEdit(); + $form->testSubmit($params); + $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE); + $expectedPaymentParams = array( + array( + 'total_amount' => 50.00, + 'financial_type' => 'Donation,Donation,Donation', + 'payment_instrument' => 'Check', + 'status' => 'Completed', + 'receive_date' => '2015-04-21 23:27:00', + 'check_number' => '123XA', + ), + array( + 'total_amount' => -50.00, + 'financial_type' => NULL, + 'payment_instrument' => 'Check', + 'status' => 'Completed', + 'receive_date' => $params['trxn_date'], + 'check_number' => '123XA', + ), + array( + 'total_amount' => 50.00, + 'financial_type' => NULL, + 'payment_instrument' => sprintf('Credit Card (Visa: %s)', $params['pan_truncation']), + 'status' => 'Completed', + 'receive_date' => $params['trxn_date'], + ), + ); + $this->assertEquals(3, count($payments['transaction'])); + foreach ($expectedPaymentParams as $key => $paymentParams) { + foreach ($paymentParams as $fieldName => $expectedValue) { + $this->assertEquals($expectedPaymentParams[$key][$fieldName], $payments['transaction'][$key][$fieldName]); + } + } + } + +} -- 2.25.1