Skip to content

Commit

Permalink
CRM-19626: Add min-amount handling to priceset
Browse files Browse the repository at this point in the history
  • Loading branch information
jitendrapurohit committed Dec 8, 2016
1 parent 791c343 commit 937640e
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,17 @@ public static function formRule($fields, $files, $self) {
$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) {
$errorMsg = ts('A minimum amount of %1 should be selected from Contribution(s).', array(
1 => CRM_Utils_Money::format($minAmt)
));
$errors['_qf_default'] = $errorMsg;
}

$amount = $fields['amount'];
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/DAO/AllCoreTables.data.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
// (GenCodeChecksum:89f332e5e88581181224fac51b45a1a4)
// (GenCodeChecksum:adc57a6fcde47d2eeabf043734987a37)
return array(
'CRM_Core_DAO_AddressFormat' => array(
'name' => 'AddressFormat',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/DAO/StateProvince.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* Generated from xml/schema/CRM/Core/StateProvince.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:c27dc7717825021cafc443d7d3244c8d)
* (GenCodeChecksum:70c072816098c4cbd13b7c13218c35e8)
*/
require_once 'CRM/Core/DAO.php';
require_once 'CRM/Utils/Type.php';
Expand Down
8 changes: 8 additions & 0 deletions CRM/Event/Form/Registration/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,17 @@ public static function formRule($fields, $files, $self) {

$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) {
$errorMsg = ts('A minimum amount of %1 should be selected from Event Fee(s).', array(
1 => CRM_Utils_Money::format($minAmt)
));
$errors['_qf_default'] = $errorMsg;
}
}

// @todo - can we remove the 'is_monetary' concept?
Expand Down
3 changes: 2 additions & 1 deletion CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE

// 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);
Expand All @@ -602,6 +602,7 @@ public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE
$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;
}
Expand Down
17 changes: 16 additions & 1 deletion CRM/Price/DAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -292,6 +298,15 @@ static function &fields() {
'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']);
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Price/Form/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public function buildQuickForm() {
}
}

$this->addElement('text', 'min_amount', ts('Minimum Amount'));

if (CRM_Utils_System::isNull($extends)) {
$this->assign('extends', FALSE);
}
Expand Down
5 changes: 4 additions & 1 deletion CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl
Original file line number Diff line number Diff line change
@@ -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;
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.';
8 changes: 6 additions & 2 deletions templates/CRM/Price/Form/Set.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@
{/if}
</td>
</tr>
<tr id="financial_type_id_row" class="crm-price-set-form-block-contribution_type_id crm-price-set-form-block-financial_type_id">
<td class="label">{$form.financial_type_id.label}</td>
<tr id="min_amount" class="crm-price-set-form-block-min_amount">
<td class="label">{$form.min_amount.label}</td>
<td>{$form.min_amount.html}</td>
</tr>
<tr id="financial_type_id_row" class="crm-price-set-form-block-contribution_type_id crm-price-set-form-block-financial_type_id">
<td class="label">{$form.financial_type_id.label}</td>
<td>{$form.financial_type_id.html}</td>
<td>&nbsp;</td>
</tr>
Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/CRM/Event/Form/Registration/RegisterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2016 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Class CRM_Event_Form_RegistrationTest
* @group headless
*/
class CRM_Event_Form_RegistrationTest extends CiviUnitTestCase {

public function setUp() {
parent::setUp();
}


/**
* CRM-19626 - Test minimum value configured for priceset.
*/
public function testMinValueForPriceSet() {
$form = new CRM_Event_Form_Registration();
$form->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);
}

}
4 changes: 3 additions & 1 deletion tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3270,16 +3270,18 @@ protected function assertAttachmentExistence($exists, $apiResult) {
* 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;
Expand Down
11 changes: 11 additions & 0 deletions xml/schema/Price/PriceSet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,15 @@
<type>CheckBox</type>
</html>
</field>
<field>
<name>min_amount</name>
<title>Minimum Amount</title>
<type>int unsigned</type>
<default>0</default>
<comment>Minimum Amount required for this set.</comment>
<add>4.7</add>
<html>
<type>Text</type>
</html>
</field>
</table>

0 comments on commit 937640e

Please sign in to comment.