Skip to content

Commit

Permalink
CRM-21187 Fix handling of currency when updating a contribution to be…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
eileenmcnaughton committed Sep 15, 2017
1 parent db3a540 commit 664e4ae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions CRM/Core/BAO/FinancialTrxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
42 changes: 41 additions & 1 deletion tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 664e4ae

Please sign in to comment.