From 4750ab016d04483cb152df393d09dad3dcf1cc4a Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 13 Mar 2018 16:24:23 +1300 Subject: [PATCH] Add test for api money, fix net_amount calc --- CRM/Contribute/BAO/Contribution.php | 2 +- api/v3/Contribution.php | 7 +++++++ tests/phpunit/api/v3/ContributionTest.php | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index ff7d05b3baf2..000242ccdb3e 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -122,7 +122,7 @@ public static function add(&$params, $ids = array()) { //if priceset is used, no need to cleanup money if (!empty($params['skipCleanMoney'])) { - unset($moneyFields[0]); + $moneyFields = []; } else { // @todo put a deprecated here - this should be done in the form layer. diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 027725ff5874..451e302d5832 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -47,6 +47,13 @@ function civicrm_api3_contribution_create(&$params) { $params = array_merge($params, $values); // The BAO should not clean money - it should be done in the form layer & api wrapper // (although arguably the api should expect pre-cleaned it seems to do some cleaning.) + if (empty($params['skipCleanMoney'])) { + foreach (['total_amount', 'net_amount', 'fee_amount'] as $field) { + if (isset($params[$field])) { + $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); + } + } + } $params['skipCleanMoney'] = TRUE; if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index ff112c9dfbca..6dd0d2b705a9 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -682,15 +682,26 @@ public function testCreateContributionSource() { /** * Create test with unique field name on source. + * + * @param string $thousandSeparator + * punctuation used to refer to thousands. + * + * @dataProvider getThousandSeparators */ - public function testCreateDefaultNow() { - + public function testCreateDefaultNow($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); $params = $this->_params; - unset($params['receive_date']); + unset($params['receive_date'], $params['net_amount']); + + $params['total_amount'] = $this->formatMoneyInput(5000.77); + $params['fee_amount'] = $this->formatMoneyInput(.77); $contribution = $this->callAPISuccess('contribution', 'create', $params); $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id'])); $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date']))); + $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77)); + $this->assertEquals(.77, $contribution['fee_amount']); + $this->assertEquals(5000, $contribution['net_amount']); } /**