From cdd71d6b5f6de7d03b21a8d0168bc659fd38b60f Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 7 Feb 2018 21:08:28 +1300 Subject: [PATCH] CRM-17647 fix another instance of mishandling thousand separators --- CRM/Batch/Form/Entry.php | 18 ++++++++++++++++++ tests/phpunit/CRM/Batch/Form/EntryTest.php | 17 ++++++++++++----- tests/phpunit/CiviTest/CiviUnitTestCase.php | 3 ++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index 62ea793b1e..1934bcc60f 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -82,6 +82,15 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { */ public $_fields = array(); + /** + * Monetary fields that may be submitted. + * + * These should get a standardised format in the beginPostProcess function. + * + * These fields are common to many forms. Some may override this. + */ + protected $submittableMoneyFields = ['total_amount', 'net_amount', 'non_deductible_amount', 'fee_amount']; + /** * Build all the data structures needed to build the form. */ @@ -443,6 +452,14 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { */ private function processContribution(&$params) { + foreach ($this->submittableMoneyFields as $moneyField) { + foreach ($params['field'] as $index => $fieldValues) { + if (isset($fieldValues[$moneyField])) { + $params['field'][$index][$moneyField] = CRM_Utils_Rule::cleanMoney($params['field'][$index][$moneyField]); + } + } + } + $params['actualBatchTotal'] = CRM_Utils_Rule::cleanMoney($params['actualBatchTotal']); // get the price set associated with offline contribution record. $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); @@ -540,6 +557,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { } } $value['line_item'] = $lineItem; + $value['skipCleanMoney'] = TRUE; //finally call contribution create for all the magic $contribution = CRM_Contribute_BAO_Contribution::create($value); $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate'); diff --git a/tests/phpunit/CRM/Batch/Form/EntryTest.php b/tests/phpunit/CRM/Batch/Form/EntryTest.php index 816bc8af38..fbb7a4389b 100644 --- a/tests/phpunit/CRM/Batch/Form/EntryTest.php +++ b/tests/phpunit/CRM/Batch/Form/EntryTest.php @@ -200,8 +200,13 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { /** * Test Contribution Import. + * + * @param $thousandSeparator + * + * @dataProvider getThousandSeparators */ - public function testProcessContribution() { + public function testProcessContribution($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); $this->offsetDefaultPriceSet(); $form = new CRM_Batch_Form_Entry(); $params = $this->getContributionData(); @@ -330,9 +335,11 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { } /** + * @param $thousandSeparator + * * @return array */ - public function getContributionData() { + public function getContributionData($thousandSeparator = '.') { return array( //'batch_id' => 4, 'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL), @@ -344,7 +351,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { 'field' => array( 1 => array( 'financial_type' => 1, - 'total_amount' => 15, + 'total_amount' => $this->formatMoneyInput(1500.15), 'receive_date' => '2013-07-24', 'receive_date_time' => NULL, 'payment_instrument' => 1, @@ -353,7 +360,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { ), 2 => array( 'financial_type' => 1, - 'total_amount' => 15, + 'total_amount' => $this->formatMoneyInput(1500.15), 'receive_date' => '2013-07-24', 'receive_date_time' => NULL, 'payment_instrument' => 1, @@ -361,7 +368,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { 'contribution_status_id' => 1, ), ), - 'actualBatchTotal' => 30, + 'actualBatchTotal' => $this->formatMoneyInput(3000.30), ); } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index adaba859a6..e391913537 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -2594,7 +2594,8 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) } public function restoreDefaultPriceSetConfig() { - CRM_Core_DAO::executeQuery('DELETE FROM civicrm_price_set WHERE id > 2'); + CRM_Core_DAO::executeQuery("DELETE FROM civicrm_price_set WHERE name NOT IN('default_contribution_amount', 'default_membership_type_amount')"); + CRM_Core_DAO::executeQuery("UPDATE civicrm_price_set SET id = 1 WHERE name ='default_contribution_amount'"); CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field` (`id`, `price_set_id`, `name`, `label`, `html_type`, `is_enter_qty`, `help_pre`, `help_post`, `weight`, `is_display_amounts`, `options_per_line`, `is_active`, `is_required`, `active_on`, `expire_on`, `javascript`, `visibility_id`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', 'Text', 0, NULL, NULL, 1, 1, 1, 1, 1, NULL, NULL, NULL, 1)"); CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field_value` (`id`, `price_field_id`, `name`, `label`, `description`, `amount`, `count`, `max_value`, `weight`, `membership_type_id`, `membership_num_terms`, `is_default`, `is_active`, `financial_type_id`, `non_deductible_amount`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', NULL, '1', NULL, NULL, 1, NULL, NULL, 0, 1, 1, 0.00)"); } -- 2.25.1