From 235929d0c3e717d37950614b5766a38e94dd3908 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 6 Jul 2016 08:49:35 +0530 Subject: [PATCH] [ready-for-core-team-review]CRM-16189, added form fields on financial account form (#8581) * CRM-16189, added form fields on financial account form ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, updated code to change class and other changes ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, fixed jenkins error ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * --CRM-16189, updated JS --- CRM/Financial/BAO/FinancialAccount.php | 28 ++++++++++++--- CRM/Financial/Form/FinancialAccount.php | 26 +++++++++++--- .../CRM/Financial/Form/FinancialAccount.tpl | 34 +++++++++++++++++++ 3 files changed, 79 insertions(+), 9 deletions(-) diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index 894f5e9937..f3aecbf4d0 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -84,12 +84,10 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco * * @param array $params * Reference array contains the values submitted by the form. - * @param array $ids - * Reference array contains the id. * * @return CRM_Financial_DAO_FinancialAccount */ - public static function add(&$params, &$ids = array()) { + public static function add(&$params) { if (empty($params['id'])) { $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE); @@ -97,6 +95,15 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE); $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); } + if (!empty($params['id']) + && !empty($params['financial_account_type_id']) + && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount( + $params['id'], + $params['financial_account_type_id'] + ) + ) { + throw new CRM_Core_Exception(ts('You cannot change the account type since this financial account refers to a financial item having an account type of Revenue/Liability.')); + } if (!empty($params['is_default'])) { $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1'; $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer')); @@ -105,9 +112,20 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco // action is taken depending upon the mode $financialAccount = new CRM_Financial_DAO_FinancialAccount(); + if (!empty($params['id'])) { + $financialAccount->id = $params['id']; + $financialAccount->find(TRUE); + } + $financialAccount->copyValues($params); - if (!empty($ids['contributionType'])) { - $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids); + //CRM-16189 + $accountType = CRM_Core_PseudoConstant::accountOptionValues( + 'financial_account_type', + NULL, + " AND v.name IN ('Liability', 'Asset') " + ); + if (!CRM_Utils_Array::value($financialAccount->financial_account_type_id, $accountType)) { + $financialAccount->opening_balance = $financialAccount->current_period_opening_balance = '0.00'; } $financialAccount->save(); return $financialAccount; diff --git a/CRM/Financial/Form/FinancialAccount.php b/CRM/Financial/Form/FinancialAccount.php index 7e59ffb694..7e7ce26dd3 100644 --- a/CRM/Financial/Form/FinancialAccount.php +++ b/CRM/Financial/Form/FinancialAccount.php @@ -104,6 +104,21 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { $element->freeze(); } + //CRM-16189 + if (CRM_Contribute_BAO_Contribution::checkContributeSettings('financial_account_bal_enable')) { + $this->add('text', 'opening_balance', ts('Opening Balance'), $attributes['opening_balance']); + $this->add('text', 'current_period_opening_balance', ts('Current Period Opening Balance'), $attributes['current_period_opening_balance']); + $financialAccountType = CRM_Core_PseudoConstant::get( + 'CRM_Financial_DAO_FinancialAccount', + 'financial_account_type_id', + array('labelColumn' => 'name') + ); + $limitedAccount = array( + array_search('Asset', $financialAccountType), + array_search('Liability', $financialAccountType), + ); + $this->assign('limitedAccount', json_encode($limitedAccount)); + } $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id'); if (!empty($financialAccountType)) { $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), @@ -113,6 +128,9 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { $elementAccounting->freeze(); $elementActive->freeze(); } + elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) { + $element->freeze(); + } } if ($this->_action == CRM_Core_Action::UPDATE && @@ -174,6 +192,7 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { $defaults = parent::setDefaultValues(); if ($this->_action & CRM_Core_Action::ADD) { $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'); + $defaults['opening_balance'] = $defaults['current_period_opening_balance'] = '0.00'; } return $defaults; } @@ -187,16 +206,15 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { CRM_Core_Session::setStatus(ts('Selected Financial Account has been deleted.')); } else { - $ids = array(); // store the submitted values in an array $params = $this->exportValues(); if ($this->_action & CRM_Core_Action::UPDATE) { - $ids['contributionType'] = $this->_id; + $params['id'] = $this->_id; } - $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids); - CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name))); + $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params); + CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $financialAccount->name)), ts('Saved'), 'success'); } } diff --git a/templates/CRM/Financial/Form/FinancialAccount.tpl b/templates/CRM/Financial/Form/FinancialAccount.tpl index 9f60213c3b..3efd907472 100644 --- a/templates/CRM/Financial/Form/FinancialAccount.tpl +++ b/templates/CRM/Financial/Form/FinancialAccount.tpl @@ -91,7 +91,41 @@ {ts}Is this account to be used as the default account for its financial account type when associating financial accounts with financial types?{/ts} + {if $form.opening_balance} + + {$form.opening_balance.label} + {$form.opening_balance.html} + + + + {$form.current_period_opening_balance.label} + {$form.current_period_opening_balance.html} + + + {/if} {/if}
{include file="CRM/common/formButtons.tpl" location="botttom"}
+{if $form.opening_balance} +{literal} + +{/literal} +{/if} -- 2.25.1