From 201b45e2ddfae6fa38617e190848184611f9c971 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Sun, 3 Jul 2016 01:35:36 +0530 Subject: [PATCH] [ready-for-core-team-review]CRM-16189, added code to add settings for accrual (#8573) * CRM-16189, added code to add settings for accrual ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * --CRM-16189, added webtest * --CRM-16189, changd code to show message on pop up and to highlight error field * CRM-16189, fixed indentation ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, changed class name ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, changed to JS function as per doc in https://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * --CRM-16189, removed cuft code, used on jq function to trigger click function * CRM-16189, updated code as per standards ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- CRM/Admin/Form/Preferences/Contribute.php | 86 ++++++++++++++--- settings/Contribute.setting.php | 93 +++++++++++++++++++ .../CRM/Admin/Form/Preferences/Contribute.tpl | 31 +++++++ templates/CRM/Form/basicForm.tpl | 51 +++++----- .../WebTest/Contribute/AccrualSettingTest.php | 86 +++++++++++++++++ 5 files changed, 312 insertions(+), 35 deletions(-) create mode 100644 tests/phpunit/WebTest/Contribute/AccrualSettingTest.php diff --git a/CRM/Admin/Form/Preferences/Contribute.php b/CRM/Admin/Form/Preferences/Contribute.php index ec733fdc14..3d484f7541 100644 --- a/CRM/Admin/Form/Preferences/Contribute.php +++ b/CRM/Admin/Form/Preferences/Contribute.php @@ -37,6 +37,13 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { protected $_settings = array( 'cvv_backoffice_required' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, + 'acl_financial_type' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, + 'deferred_revenue_enabled' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, + 'default_invoice_page' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, + 'financial_account_bal_enable' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, + 'fiscalYearStart' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, + 'prior_financial_period' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, + 'invoicing' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, ); /** @@ -118,30 +125,77 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { * Build the form object. */ public function buildQuickForm() { - //CRM-16691: Changes made related to settings of 'CVV'. + $htmlFields = array(); foreach ($this->_settings as $setting => $group) { $settingMetaData = civicrm_api3('setting', 'getfields', array('name' => $setting)); $props = $settingMetaData['values'][$setting]; if (isset($props['quick_form_type'])) { $add = 'add' . $props['quick_form_type']; if ($add == 'addElement') { - $this->$add( - $props['html_type'], - $setting, - ts($props['title']), - CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()), - $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL - ); + if (in_array($props['html_type'], array('checkbox', 'textarea'))) { + $this->add($props['html_type'], + $setting, + $props['title'] + ); + } + else { + if ($props['html_type'] == 'select') { + $functionName = CRM_Utils_Array::value('name', CRM_Utils_Array::value('pseudoconstant', $props)); + if ($functionName) { + $props['option_values'] = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::$functionName(); + } + } + $this->$add( + $props['html_type'], + $setting, + ts($props['title']), + CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()), + $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL + ); + } + } + elseif ($add == 'addMonthDay') { + $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d')); + } + elseif ($add == 'addDate') { + $this->addDate($setting, ts($props['title']), FALSE, array('formatType' => $props['type'])); } else { $this->$add($setting, ts($props['title'])); } } - $this->assign("{$setting}_description", ts($props['description'])); + $htmlFields[$setting] = ts($props['description']); } - $this->add('checkbox', 'invoicing', ts('Enable Tax and Invoicing')); - $this->add('checkbox', 'acl_financial_type', ts('Enable Access Control by Financial Type')); + $this->assign('htmlFields', $htmlFields); parent::buildQuickForm(); + $this->addFormRule(array('CRM_Admin_Form_Preferences_Contribute', 'formRule'), $this); + } + + /** + * Global validation rules for the form. + * + * @param array $values + * posted values of the form + * @param $files + * @param $self + * + * @return array + * list of errors to be posted back to the form + */ + public static function formRule($values, $files, $self) { + $errors = array(); + if (CRM_Utils_Array::value('deferred_revenue_enabled', $values)) { + $errorMessage = CRM_Financial_BAO_FinancialAccount::validateTogglingDeferredRevenue(); + if ($errorMessage) { + // Since the error msg is too long and + // takes the whole space to display inline + // therefore setting blank text to highlight the field + // setting actual error msg to _qf_default to show in pop-up screen + $errors['deferred_revenue_enabled'] = ' '; + $errors['_qf_default'] = $errorMessage; + } + } + return $errors; } /** @@ -152,16 +206,19 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { public function setDefaultValues() { $defaults = Civi::settings()->get('contribution_invoice_settings'); //CRM-16691: Changes made related to settings of 'CVV'. - foreach ($this->_settings as $setting => $group) { + foreach (array('cvv_backoffice_required') as $setting) { $settingMetaData = civicrm_api3('setting', 'getfields', array('name' => $setting)); $defaults[$setting] = civicrm_api3('setting', 'getvalue', array( 'name' => $setting, - 'group' => $group, + 'group' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting]), ) ); } + $defaults['fiscalYearStart'] = Civi::settings()->get('fiscalYearStart'); + $period = CRM_Contribute_BAO_Contribution::checkContributeSettings('prior_financial_period'); + $this->assign('priorFinancialPeriod', $period); return $defaults; } @@ -174,6 +231,7 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { unset($params['qfKey']); unset($params['entryURL']); Civi::settings()->set('contribution_invoice_settings', $params); + Civi::settings()->set('fiscalYearStart', $params['fiscalYearStart']); // to set default value for 'Invoices / Credit Notes' checkbox on display preferences $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences"); @@ -197,7 +255,7 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { Civi::settings()->set('user_dashboard_options', $settingName); } //CRM-16691: Changes made related to settings of 'CVV'. - $settings = array_intersect_key($params, $this->_settings); + $settings = array_intersect_key($params, array('cvv_backoffice_required' => 1)); $result = civicrm_api3('setting', 'create', $settings); CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Changes Saved'), "success"); } diff --git a/settings/Contribute.setting.php b/settings/Contribute.setting.php index de45ebae61..b953b79e57 100644 --- a/settings/Contribute.setting.php +++ b/settings/Contribute.setting.php @@ -72,4 +72,97 @@ return array( 'description' => NULL, 'help_text' => NULL, ), + 'invoicing' => array( + 'group_name' => 'Contribute Preferences', + 'group' => 'contribute', + 'name' => 'invoicing', + 'type' => 'Integer', + 'html_type' => 'checkbox', + 'quick_form_type' => 'Element', + 'default' => 0, + 'add' => '4.7', + 'title' => 'Enable Tax and Invoicing', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => NULL, + 'help_text' => NULL, + ), + 'acl_financial_type' => array( + 'group_name' => 'Contribute Preferences', + 'group' => 'contribute', + 'name' => 'acl_financial_type', + 'type' => 'Integer', + 'html_type' => 'checkbox', + 'quick_form_type' => 'Element', + 'default' => 0, + 'add' => '4.7', + 'title' => 'Enable Access Control by Financial Type', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => NULL, + 'help_text' => NULL, + ), + 'deferred_revenue_enabled' => array( + 'group_name' => 'Contribute Preferences', + 'group' => 'contribute', + 'name' => 'deferred_revenue_enabled', + 'type' => 'Integer', + 'html_type' => 'checkbox', + 'quick_form_type' => 'Element', + 'default' => 0, + 'add' => '4.7', + 'title' => 'Enable Deferred Revenue', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => NULL, + 'help_text' => NULL, + ), + 'default_invoice_page' => array( + 'group_name' => 'Contribute Preferences', + 'group' => 'contribute', + 'name' => 'default_invoice_page', + 'type' => 'Integer', + 'quick_form_type' => 'Element', + 'default' => NULL, + 'pseudoconstant' => array( + 'name' => 'contributionPage', + ), + 'html_type' => 'select', + 'add' => '4.7', + 'title' => 'Default invoice payment page', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => NULL, + 'help_text' => NULL, + ), + 'financial_account_bal_enable' => array( + 'group_name' => 'Contribute Preferences', + 'group' => 'contribute', + 'name' => 'financial_account_bal_enable', + 'type' => 'Integer', + 'html_type' => 'checkbox', + 'quick_form_type' => 'Element', + 'default' => 0, + 'add' => '4.7', + 'title' => 'Enable Financial Account Balances', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => NULL, + 'help_text' => NULL, + ), + 'prior_financial_period' => array( + 'group_name' => 'Contribute Preferences', + 'group' => 'contribute', + 'name' => 'prior_financial_period', + 'type' => 'activityDate', + 'quick_form_type' => 'Date', + 'html_type' => 'Date', + 'default' => NULL, + 'add' => '4.7', + 'title' => 'Prior Financial Period', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => NULL, + 'help_text' => NULL, + ), ); diff --git a/templates/CRM/Admin/Form/Preferences/Contribute.tpl b/templates/CRM/Admin/Form/Preferences/Contribute.tpl index 7a263b6a63..e0093457cd 100644 --- a/templates/CRM/Admin/Form/Preferences/Contribute.tpl +++ b/templates/CRM/Admin/Form/Preferences/Contribute.tpl @@ -24,3 +24,34 @@ +--------------------------------------------------------------------+ *} {include file="CRM/Form/basicForm.tpl"} +{literal} + +{/literal} diff --git a/templates/CRM/Form/basicForm.tpl b/templates/CRM/Form/basicForm.tpl index 694385a8d3..0fc8b1e525 100644 --- a/templates/CRM/Form/basicForm.tpl +++ b/templates/CRM/Form/basicForm.tpl @@ -27,27 +27,36 @@
{include file="CRM/common/formButtons.tpl" location="top"}
{if $formName == "Contribute_Preferences"} - - - - - - - - - {if $formName == "Contribute_Preferences" } - - - - - {/if} + {foreach from=$htmlFields item=desc key=htmlField} + {if $form.$htmlField} + {assign var=n value=$htmlField|cat:'_description'} + + {if $form.$htmlField.html_type EQ 'checkbox'|| $form.$htmlField.html_type EQ 'checkboxes'} + + + {else} + + + {/if} + + {/if} + {/foreach} + {$form.prior_financial_period_M_hidden.html} + {$form.prior_financial_period_d_hidden.html}
{$form.cvv_backoffice_required.label} - {$form.cvv_backoffice_required.html}
-

{ts}{$cvv_backoffice_required_description}{/ts}

-
{$form.acl_financial_type.label} {help id="acl_financial_type"} - {$form.acl_financial_type.html} -
{$form.invoicing.label} - {$form.invoicing.html} -
+ {$form.$htmlField.html} {$form.$htmlField.label} + {if $desc} +
{$desc} + {/if} +
{$form.$htmlField.label} {if $htmlField eq 'acl_financial_type'}{help id="$htmlField"}{/if} + {if $htmlField eq 'prior_financial_period'} + {include file="CRM/common/jcalendar.tpl" elementName=$htmlField} + {else} + {$form.$htmlField.html} + {/if} + {if $desc} +
{$desc} + {/if} +
{/if} diff --git a/tests/phpunit/WebTest/Contribute/AccrualSettingTest.php b/tests/phpunit/WebTest/Contribute/AccrualSettingTest.php new file mode 100644 index 0000000000..7362798419 --- /dev/null +++ b/tests/phpunit/WebTest/Contribute/AccrualSettingTest.php @@ -0,0 +1,86 @@ +webtestLogin(); + $this->openCiviPage("admin/setting/preferences/contribute", "reset=1"); + $this->waitForElementPresent("_qf_Contribute_next"); + + // Check hide/show + $this->click("deferred_revenue_enabled"); + $this->waitForElementPresent("xpath=//tr[@class='crm-preferences-form-block-default_invoice_page'][@style='display: table-row;']"); + $this->click("deferred_revenue_enabled"); + $this->waitForElementPresent("xpath=//tr[@class='crm-preferences-form-block-default_invoice_page'][@style='display: none;']"); + + // a random 7-char string and an even number to make this pass unique + $hash = substr(sha1(rand()), 0, 7); + $rand = 2 * rand(2, 50); + $pageTitle = 'Test Contribution Page ' . $hash; + // create contribution page with randomized title and default params + $pageId = $this->webtestAddContributionPage($hash, $rand, $pageTitle, array('Test Processor' => 'Dummy'), FALSE, FALSE, FALSE, FALSE, + FALSE, FALSE, NULL, FALSE, 1, 7, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 'Donation', TRUE, FALSE); + + // Input value + $this->openCiviPage("admin/setting/preferences/contribute", "reset=1"); + $this->waitForElementPresent("_qf_Contribute_next"); + $this->click("deferred_revenue_enabled"); + $this->waitForElementPresent("xpath=//*[@id='default_invoice_page']"); + + $this->select('default_invoice_page', "value={$pageId}"); + + // Check hide/show + $this->click("financial_account_bal_enable"); + $this->waitForElementPresent("xpath=//tr[@class='crm-preferences-form-block-fiscalYearStart'][@style='display: table-row;']"); + $this->click("financial_account_bal_enable"); + $this->waitForElementPresent("xpath=//tr[@class='crm-preferences-form-block-fiscalYearStart'][@style='display: none;']"); + + $this->click("financial_account_bal_enable"); + $this->waitForElementPresent("xpath=//*[@id='fiscalYearStart_M']"); + $this->select('fiscalYearStart_M', "value=4"); + $this->select('fiscalYearStart_d', "value=30"); + + $this->webtestFillDate('period_closing_date', 'now+2'); + $this->click('_qf_Contribute_next'); + $this->waitForPageToLoad($this->getTimeoutMsec()); + + //does data saved. + $this->assertTrue($this->isTextPresent('Changes saved.'), + "Status message didn't show up after saving!" + ); + } + +} -- 2.25.1