[ready-for-core-team-review]CRM-16189, added code to add settings for accrual (#8573)
authorPradeep Nayak <pradpnayak@gmail.com>
Sat, 2 Jul 2016 20:05:36 +0000 (01:35 +0530)
committercolemanw <coleman@civicrm.org>
Sat, 2 Jul 2016 20:05:36 +0000 (16:05 -0400)
* 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
settings/Contribute.setting.php
templates/CRM/Admin/Form/Preferences/Contribute.tpl
templates/CRM/Form/basicForm.tpl
tests/phpunit/WebTest/Contribute/AccrualSettingTest.php [new file with mode: 0644]

index ec733fdc1442c272b764ec285e9620fd12dc4378..3d484f7541a02a9b5408237bbdc47d594ba36673 100644 (file)
 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");
   }
index de45ebae619a9a1b8b389746c70402e260de92fe..b953b79e57a47a6928a5d85ef1bcb31e9c7a4020 100644 (file)
@@ -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,
+  ),
 );
index 7a263b6a630507bc434b295bee7c5da8c1ad2d54..e0093457cd515e836372df565a5920e261243a48 100644 (file)
  +--------------------------------------------------------------------+
 *}
 {include file="CRM/Form/basicForm.tpl"}
+{literal}
+  <script type="text/javascript">
+    CRM.$(function($) {
+      showHideElement('deferred_revenue_enabled', 'default_invoice_page');
+      $("#deferred_revenue_enabled").click(function() {
+        showHideElement('deferred_revenue_enabled', 'default_invoice_page');
+      });
+      showHideElement('financial_account_bal_enable', 'fiscalYearStart');
+      $("#financial_account_bal_enable").click(function() {
+        showHideElement('financial_account_bal_enable', 'fiscalYearStart');
+      });
+      function showHideElement(checkEle, toHide) {
+        if ($('#' + checkEle).prop('checked')) {
+          $("tr.crm-preferences-form-block-" + toHide).show();
+        }
+        else {
+          $("tr.crm-preferences-form-block-" + toHide).hide();
+        }
+      }
+      $('input[name=_qf_Contribute_next]').on('click', checkPeriod);
+      function checkPeriod() {
+        var speriod = $('#prior_financial_period').val();
+       var hperiod = '{/literal}{$priorFinancialPeriod}{literal}';
+       if (((hperiod && speriod == '') || (hperiod && speriod != '')) && (speriod != hperiod)) {
+         var msg = '{/literal}{ts}Changing the Prior Financial Period may result in problems calculating closing account balances accurately and / or exporting of financial transactions. Do you want to proceed?{/ts}{literal}';
+          return confirm(msg);
+        }
+      }
+    });
+  </script>
+{/literal}
index 694385a8d37124601f17fe043e86a75ae8a651dc..0fc8b1e52520f8f74f13b3ffac66ca7410bda56c 100644 (file)
     <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
     {if $formName == "Contribute_Preferences"}
       <table class = "form-layout">
-        <tr class="crm-miscellaneous-form-block-cvv-backoffice-required">
-          <td class="label">{$form.cvv_backoffice_required.label}</td>
-          <td>
-            {$form.cvv_backoffice_required.html}<br />
-            <p class="description">{ts}{$cvv_backoffice_required_description}{/ts}</p>
-          </td>
-        </tr>
-        <tr class="crm-preferences-form-block-acl_financial_type">
-          <td class="label">{$form.acl_financial_type.label}&nbsp;{help id="acl_financial_type"}</td>
-          <td>
-            {$form.acl_financial_type.html}
-          </td>
-        </tr>
-        {if $formName == "Contribute_Preferences" }
-          <tr class="crm-preferences-form-block-invoicing">
-            <td class="label">{$form.invoicing.label}</td>
-            <td>
-              {$form.invoicing.html}
-            </td>
-          </tr>
-        {/if}
+        {foreach from=$htmlFields item=desc key=htmlField}
+          {if $form.$htmlField}
+           {assign var=n value=$htmlField|cat:'_description'}
+            <tr class="crm-preferences-form-block-{$htmlField}">
+              {if $form.$htmlField.html_type EQ 'checkbox'|| $form.$htmlField.html_type EQ 'checkboxes'}
+                <td class="label"></td>
+                <td>
+                  {$form.$htmlField.html} {$form.$htmlField.label}
+                  {if $desc}
+                    <br /><span class="description">{$desc}</span>
+                  {/if}
+                </td>
+              {else}
+                <td class="label">{$form.$htmlField.label}&nbsp;{if $htmlField eq 'acl_financial_type'}{help id="$htmlField"}{/if}</td>
+                <td>
+                  {if $htmlField eq 'prior_financial_period'}
+                    {include file="CRM/common/jcalendar.tpl" elementName=$htmlField}
+                 {else}
+                     {$form.$htmlField.html}
+                  {/if}
+                  {if $desc}
+                    <br /><span class="description">{$desc}</span>
+                  {/if}
+                </td>
+              {/if}
+            </tr>
+          {/if}
+        {/foreach}
+       {$form.prior_financial_period_M_hidden.html}
+       {$form.prior_financial_period_d_hidden.html}
       </table>
     {/if}
     <table class="form-layout" id="invoicing_blocks">
diff --git a/tests/phpunit/WebTest/Contribute/AccrualSettingTest.php b/tests/phpunit/WebTest/Contribute/AccrualSettingTest.php
new file mode 100644 (file)
index 0000000..7362798
--- /dev/null
@@ -0,0 +1,86 @@
+<?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 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        |
+ +--------------------------------------------------------------------+
+ */
+
+require_once 'CiviTest/CiviSeleniumTestCase.php';
+
+/**
+ * Class WebTest_Admin_Form_Setting_LocalizationTest
+ */
+class WebTest_Contribute_AccrualSettingTest extends CiviSeleniumTestCase {
+
+  protected function setUp() {
+    parent::setUp();
+  }
+
+  public function testAccrualSettings() {
+    $this->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!"
+    );
+  }
+
+}