From 601c7a24b426dc50f2a8496aafdb870688a98f65 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Thu, 8 Dec 2016 17:12:11 +0530 Subject: [PATCH] CRM-19626: Add min-amount handling to priceset --- CRM/Contribute/Form/Contribution/Main.php | 7 ++ CRM/Event/Form/Registration/Register.php | 7 ++ CRM/Price/BAO/PriceSet.php | 3 +- CRM/Price/DAO/PriceSet.php | 17 +++- CRM/Price/Form/Set.php | 2 + CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl | 5 +- templates/CRM/Price/Form/Set.tpl | 8 +- .../Event/Form/Registration/RegisterTest.php | 77 +++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 4 +- xml/schema/Price/PriceSet.xml | 11 +++ 10 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/CRM/Event/Form/Registration/RegisterTest.php diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 363ca31890..74e8216889 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -861,9 +861,16 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $fields, $lineItem ); + $minAmt = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount'); if ($fields['amount'] < 0) { $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly'); } + elseif (!empty($minAmt) && $fields['amount'] < $minAmt) { + $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Contribution(s).', array( + 1 => CRM_Utils_Money::format($minAmt), + )); + } + $amount = $fields['amount']; } diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index 9ec3fb30fc..0c8a4790b8 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -803,9 +803,16 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { $lineItem = array(); CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], $fields, $lineItem); + + $minAmt = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount'); if ($fields['amount'] < 0) { $errors['_qf_default'] = ts('Event Fee(s) can not be less than zero. Please select the options accordingly'); } + elseif (!empty($minAmt) && $fields['amount'] < $minAmt) { + $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Event Fee(s).', array( + 1 => CRM_Utils_Money::format($minAmt), + )); + } } // @todo - can we remove the 'is_monetary' concept? diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index 36dc1dcd16..83f7c51467 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -592,7 +592,7 @@ AND ( expire_on IS NULL OR expire_on >= {$currentTime} ) // also get the pre and post help from this price set $sql = " -SELECT extends, financial_type_id, help_pre, help_post, is_quick_config +SELECT extends, financial_type_id, help_pre, help_post, is_quick_config, min_amount FROM civicrm_price_set WHERE id = %1"; $dao = CRM_Core_DAO::executeQuery($sql, $params); @@ -602,6 +602,7 @@ WHERE id = %1"; $setTree[$setID]['help_pre'] = $dao->help_pre; $setTree[$setID]['help_post'] = $dao->help_post; $setTree[$setID]['is_quick_config'] = $dao->is_quick_config; + $setTree[$setID]['min_amount'] = $dao->min_amount; } return $setTree; } diff --git a/CRM/Price/DAO/PriceSet.php b/CRM/Price/DAO/PriceSet.php index bda7ba9467..f07500a2bd 100644 --- a/CRM/Price/DAO/PriceSet.php +++ b/CRM/Price/DAO/PriceSet.php @@ -30,7 +30,7 @@ * * Generated from xml/schema/CRM/Price/PriceSet.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:cf1125145d52414335549f50501ff5fc) + * (GenCodeChecksum:1642f2b38411573c40848c0d7c83c74c) */ require_once 'CRM/Core/DAO.php'; require_once 'CRM/Utils/Type.php'; @@ -120,6 +120,12 @@ class CRM_Price_DAO_PriceSet extends CRM_Core_DAO { * @var boolean */ public $is_reserved; + /** + * Minimum Amount required for this set. + * + * @var int unsigned + */ + public $min_amount; /** * class constructor * @@ -292,6 +298,15 @@ class CRM_Price_DAO_PriceSet extends CRM_Core_DAO { 'type' => 'CheckBox', ) , ) , + 'min_amount' => array( + 'name' => 'min_amount', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('Minimum Amount') , + 'description' => 'Minimum Amount required for this set.', + 'html' => array( + 'type' => 'Text', + ) , + ) , ); CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']); } diff --git a/CRM/Price/Form/Set.php b/CRM/Price/Form/Set.php index 90d9be60fd..1e689ecfa2 100644 --- a/CRM/Price/Form/Set.php +++ b/CRM/Price/Form/Set.php @@ -183,6 +183,8 @@ class CRM_Price_Form_Set extends CRM_Core_Form { } } + $this->addElement('text', 'min_amount', ts('Minimum Amount')); + if (CRM_Utils_System::isNull($extends)) { $this->assign('extends', FALSE); } diff --git a/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl index 4995379ef5..4ac2399c60 100644 --- a/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl @@ -1,4 +1,7 @@ {* file to handle db changes in 4.7.15 during upgrade *} -- CRM-19685 (fix for inconsistencies) -UPDATE civicrm_contact SET preferred_mail_format = 'Both' WHERE preferred_mail_format IS NULL; \ No newline at end of file +UPDATE civicrm_contact SET preferred_mail_format = 'Both' WHERE preferred_mail_format IS NULL; + +-- CRM-19626 +ALTER TABLE civicrm_price_set ADD min_amount INT(10) UNSIGNED DEFAULT '0' COMMENT 'Minimum Amount required for this set.'; diff --git a/templates/CRM/Price/Form/Set.tpl b/templates/CRM/Price/Form/Set.tpl index a85bc2dd68..b72398f7ca 100644 --- a/templates/CRM/Price/Form/Set.tpl +++ b/templates/CRM/Price/Form/Set.tpl @@ -52,8 +52,12 @@ {/if} - - {$form.financial_type_id.label} + + {$form.min_amount.label} + {$form.min_amount.html} + + + {$form.financial_type_id.label} {$form.financial_type_id.html}   diff --git a/tests/phpunit/CRM/Event/Form/Registration/RegisterTest.php b/tests/phpunit/CRM/Event/Form/Registration/RegisterTest.php new file mode 100644 index 0000000000..f301b3045c --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/Registration/RegisterTest.php @@ -0,0 +1,77 @@ +controller = new CRM_Core_Controller(); + + $minAmt = 100; + $feeAmt = 1000; + $event = $this->eventCreate(); + $priceSetId = $this->eventPriceSetCreate($feeAmt, $minAmt); + $priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); + $form->_values['fee'] = $form->_feeBlock = $priceSet['fields']; + $form->_values['event'] = $event['values'][$event['id']]; + $form->_skipDupeRegistrationCheck = 1; + + $priceField = $this->callAPISuccess('PriceField', 'get', array('price_set_id' => $priceSetId)); + $params = array( + 'email-Primary' => 'someone@example.com', + 'priceSetId' => $priceSetId, + ); + // Check empty values for price fields. + foreach (array_keys($priceField['values']) as $fieldId) { + $params['price_' . $fieldId] = 0; + } + $form->set('priceSetId', $priceSetId); + $form->set('priceSet', $priceSet); + $form->set('name', 'CRM_Event_Form_Registration_Register'); + $files = array(); + $errors = CRM_Event_Form_Registration_Register::formRule($params, $files, $form); + + //Assert the validation Error. + $expectedResult = array( + '_qf_default' => ts('A minimum amount of %1 should be selected from Event Fee(s).', array(1 => CRM_Utils_Money::format($minAmt))), + ); + $this->checkArrayEquals($expectedResult, $errors); + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 643c4153d0..bb661a2ef8 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3270,16 +3270,18 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * Create a price set for an event. * * @param int $feeTotal + * @param int $minAmt * * @return int * Price Set ID. */ - protected function eventPriceSetCreate($feeTotal) { + protected function eventPriceSetCreate($feeTotal, $minAmt = 0) { // creating price set, price field $paramsSet['title'] = 'Price Set'; $paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set'); $paramsSet['is_active'] = FALSE; $paramsSet['extends'] = 1; + $paramsSet['min_amount'] = $minAmt; $priceset = CRM_Price_BAO_PriceSet::create($paramsSet); $priceSetId = $priceset->id; diff --git a/xml/schema/Price/PriceSet.xml b/xml/schema/Price/PriceSet.xml index 034fe1b5e0..27b8852eb9 100644 --- a/xml/schema/Price/PriceSet.xml +++ b/xml/schema/Price/PriceSet.xml @@ -196,4 +196,15 @@ CheckBox + + min_amount + Minimum Amount + int unsigned + 0 + Minimum Amount required for this set. + 4.7 + + Text + + -- 2.25.1