Unverified Commit 0c6ca26b authored by Monish Deb's avatar Monish Deb Committed by GitHub

Merge pull request #11646 from eileenmcnaughton/thous

 CRM-17647 fix batch form & Online Event form to do money cleaning on the form
parents 3ee8b774 c98026b8
......@@ -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');
......
......@@ -1391,9 +1391,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
) {
unset($submittedValues['tax_amount']);
}
// @todo - look to remove this line. I believe it relates to CRM-16460
// and possibly contributes to fixing the issue described there but
// would cause breakage for negative values in some cases.
$submittedValues['total_amount'] = CRM_Utils_Array::value('amount', $submittedValues);
}
......
......@@ -49,6 +49,15 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
*/
public $_totalAmount;
/**
* 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', 'tax_amount', 'amount'];
/**
* Set variables up before form is built.
*/
......@@ -205,6 +214,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
$taxAmount = 0;
foreach ($this->_params as $k => $v) {
$this->cleanMoneyFields($v);
if ($v == 'skip') {
continue;
}
......@@ -398,6 +408,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
$now = date('YmdHis');
$this->_params = $this->get('params');
$this->cleanMoneyFields($this->_params);
if (!empty($this->_params[0]['contact_id'])) {
// unclear when this would be set & whether it could be checked in getContactID.
// perhaps it relates to when cid is in the url
......@@ -1039,6 +1051,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
}
$contribParams['skipLineItem'] = 1;
$contribParams['skipCleanMoney'] = 1;
// create contribution record
$contribution = CRM_Contribute_BAO_Contribution::add($contribParams);
// CRM-11124
......@@ -1296,7 +1309,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
$_REQUEST['id'] = $form->_eventId = $params['id'];
$form->controller = new CRM_Event_Controller_Registration();
$form->_params = $params['params'];
$form->_amount = $form->_totalAmount = CRM_Utils_Array::value('totalAmount', $params);
// This happens in buildQuickForm so emulate here.
$form->_amount = $form->_totalAmount = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('totalAmount', $params));
$form->set('params', $params['params']);
$form->_values['custom_pre_id'] = array();
$form->_values['custom_post_id'] = array();
......@@ -1332,4 +1346,19 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
return array();
}
/**
* Clean money fields from the form.
*
* @param array $params
*/
protected function cleanMoneyFields(&$params) {
foreach ($this->submittableMoneyFields as $moneyField) {
foreach ($params as $index => $paramField) {
if (isset($paramField[$moneyField])) {
$params[$index][$moneyField] = CRM_Utils_Rule::cleanMoney($paramField[$moneyField]);
}
}
}
}
}
......@@ -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),
);
}
......
......@@ -81,9 +81,14 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
/**
* Initial test of submit function for paid event.
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
*
* @throws \Exception
*/
public function testPaidSubmit() {
public function testPaidSubmit($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$paymentProcessorID = $this->processorCreate();
$params = array('is_monetary' => 1, 'financial_type_id' => 1);
$event = $this->eventCreate($params);
......@@ -93,7 +98,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
'contributeMode' => 'direct',
'registerByID' => $individualID,
'paymentProcessorObj' => CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID),
'totalAmount' => 800,
'totalAmount' => $this->formatMoneyInput(8000.67),
'params' => array(
array(
'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
......@@ -133,7 +138,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
'participant_role_id' => '1',
'currencyID' => 'USD',
'amount_level' => 'Tiny-tots (ages 5-8) - 1',
'amount' => '800.00',
'amount' => $this->formatMoneyInput(8000.67),
'tax_amount' => NULL,
'year' => '2019',
'month' => '1',
......@@ -147,6 +152,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
));
$this->callAPISuccessGetCount('Participant', array(), 1);
$contribution = $this->callAPISuccessGetSingle('Contribution', array());
$this->assertEquals(8000.67, $contribution['total_amount']);
$lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
$financialTrxn = $this->callAPISuccessGetSingle(
'FinancialTrxn',
......
......@@ -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)");
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment