From 664e4ae2d7630085411e9a5e8abbf6afb3556179 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 15 Sep 2017 11:51:47 +1200 Subject: [PATCH] CRM-21187 Fix handling of currency when updating a contribution to be completed. This is replicable via the api & potentially other flows. The currency id of NULL is not accepted in financialTrxn BAO without it replacing with a default, which is not appropriate on an update. --- CRM/Contribute/BAO/Contribution.php | 4 +++ CRM/Core/BAO/FinancialTrxn.php | 6 ++-- tests/phpunit/api/v3/ContributionTest.php | 42 ++++++++++++++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 53b6448fb09b..c709b47ce8bd 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3539,6 +3539,10 @@ public static function updateFinancialAccounts(&$params, $context = NULL) { // & this can be removed return; } + if (empty($params['trxnParams']['currency'])) { + // This is an update so original currency if none passed in. + $params['trxnParams']['currency'] = isset($params['currency']) ? $params['currency'] : $params['prevContribution']->currency; + } self::recordAlwaysAccountsReceivable($params['trxnParams'], $params); $trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']); $params['entity_id'] = self::$_trxnIDs[] = $trxn->id; diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index 4ffd028a8e3f..06f1021d38b2 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -50,18 +50,18 @@ public function __construct() { * * @return CRM_Core_BAO_FinancialTrxn */ - public static function create(&$params) { + public static function create($params) { $trxn = new CRM_Financial_DAO_FinancialTrxn(); $trxn->copyValues($params); - if (!CRM_Utils_Rule::currencyCode($trxn->currency)) { + if (empty($params['id']) && !CRM_Utils_Rule::currencyCode($trxn->currency)) { $trxn->currency = CRM_Core_Config::singleton()->defaultCurrency; } $trxn->save(); - // We shoudn't proceed further to record related entity financial trxns if it's update. if (!empty($params['id'])) { + // For an update entity financial transaction record will already exist. Return early. return $trxn; } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index ffc8271cddc9..3166a5a9ff56 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1018,7 +1018,7 @@ public function testCreateContributionPayLaterOnline() { * Function tests that additional financial records are created for online contribution with pending option. */ public function testCreateContributionPendingOnline() { - $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); + CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); $this->assertAPISuccess($contributionPage); $params = array( @@ -1747,6 +1747,46 @@ public function testCompleteTransaction() { $this->revertTemplateToReservedTemplate(); } + /** + * Test completing a transaction via the API with a non-USD transaction. + */ + public function testCompleteTransactionEuro() { + $mut = new CiviMailUtils($this, TRUE); + $this->swapMessageTemplateForTestTemplate(); + $this->createLoggedInUser(); + $params = array_merge($this->_params, array('contribution_status_id' => 2, 'currency' => 'EUR')); + $contribution = $this->callAPISuccess('contribution', 'create', $params); + + $this->callAPISuccess('contribution', 'completetransaction', array( + 'id' => $contribution['id'], + )); + + $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'])); + $this->assertEquals('SSF', $contribution['contribution_source']); + $this->assertEquals('Completed', $contribution['contribution_status']); + $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date']))); + + $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']); + $entityFinancialTransaction = reset($entityFinancialTransactions); + $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id'])); + $this->assertEquals('EUR', $financialTrxn['currency']); + + $mut->checkMailLog(array( + 'email:::anthony_anderson@civicrm.org', + 'is_monetary:::1', + 'amount:::100.00', + 'currency:::EUR', + 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])), + "receipt_date:::\n", + 'contributeMode:::notify', + 'title:::Contribution', + 'displayName:::Mr. Anthony Anderson II', + 'contributionStatus:::Completed', + )); + $mut->stop(); + $this->revertTemplateToReservedTemplate(); + } + /** * Test to ensure mail is sent on chosing pay later */