3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Financial Account
21 class CRM_Financial_Form_FinancialAccount
extends CRM_Contribute_Form
{
24 * Flag if its a AR account type.
28 protected $_isARFlag = FALSE;
31 * Set variables up before form is built.
33 public function preProcess() {
40 $financialAccount = CRM_Financial_BAO_FinancialAccount
::retrieve($params);
41 $financialAccountTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
42 if ($financialAccount->financial_account_type_id
== $financialAccountTypeId
43 && strtolower($financialAccount->account_type_code
) == 'ar'
44 && !CRM_Financial_BAO_FinancialAccount
::getARAccounts($this->_id
, $financialAccountTypeId)
46 $this->_isARFlag
= TRUE;
47 if ($this->_action
& CRM_Core_Action
::DELETE
) {
48 $msg = ts("The selected financial account cannot be deleted because at least one Accounts Receivable type account is required (to ensure that accounting transactions are in balance).");
49 CRM_Core_Error
::statusBounce($msg);
56 * Build the form object.
58 public function buildQuickForm() {
59 parent
::buildQuickForm();
60 $this->setPageTitle(ts('Financial Account'));
62 if ($this->_action
& CRM_Core_Action
::DELETE
) {
66 $this->applyFilter('__ALL__', 'trim');
67 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_FinancialAccount');
68 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
69 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
70 'objectExists', ['CRM_Financial_DAO_FinancialAccount', $this->_id
]);
72 $this->add('text', 'description', ts('Description'), $attributes['description']);
73 $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
74 $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
75 $this->addEntityRef('contact_id', ts('Owner'), [
76 'api' => ['params' => ['contact_type' => 'Organization']],
79 $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
80 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
81 $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
82 $this->add('checkbox', 'is_tax', ts('Is Tax?'));
84 $element = $this->add('checkbox', 'is_default', ts('Default?'));
85 // CRM-12470 freeze is default if is_default is set
86 if ($this->_id
&& CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id
, 'is_default')) {
90 $financialAccountType = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
91 if (!empty($financialAccountType)) {
92 $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
93 ['' => ts('- select -')] +
$financialAccountType, TRUE, ['class' => 'crm-select2 huge']);
94 if ($this->_isARFlag
) {
96 $elementAccounting->freeze();
97 $elementActive->freeze();
99 elseif ($this->_id
&& CRM_Financial_BAO_FinancialAccount
::validateFinancialAccount($this->_id
)) {
104 if ($this->_action
== CRM_Core_Action
::UPDATE
&&
105 CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id
, 'is_reserved')
107 $this->freeze(['name', 'description', 'is_active']);
109 $this->addFormRule(['CRM_Financial_Form_FinancialAccount', 'formRule'], $this);
113 * Global validation rules for the form.
115 * @param array $values
116 * posted values of the form
121 * list of errors to be posted back to the form
123 public static function formRule($values, $files, $self) {
125 $financialAccountTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' "));
126 if (isset($values['is_tax'])) {
127 if ($values['financial_account_type_id'] != $financialAccountTypeId) {
128 $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.');
130 if (!isset($values['tax_rate'])) {
131 $errorMsg['tax_rate'] = ts('Please enter value for tax rate');
134 if ((CRM_Utils_Array
::value('tax_rate', $values) != NULL)) {
135 if ($values['tax_rate'] < 0 ||
$values['tax_rate'] >= 100) {
136 $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
139 if ($self->_action
& CRM_Core_Action
::UPDATE
) {
140 if (!(isset($values['is_tax']))) {
141 // @todo replace with call to CRM_Financial_BAO_FinancialAccount getSalesTaxFinancialAccount
142 $relationshipId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
144 'financial_account_id' => $self->_id
,
145 'account_relationship' => $relationshipId,
147 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
149 $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
153 return CRM_Utils_Array
::crmIsEmptyArray($errorMsg) ?
TRUE : $errorMsg;
157 * Set default values for the form.
158 * the default values are retrieved from the database.
160 public function setDefaultValues() {
161 $defaults = parent
::setDefaultValues();
162 if ($this->_action
& CRM_Core_Action
::ADD
) {
163 $defaults['contact_id'] = CRM_Core_BAO_Domain
::getDomain()->contact_id
;
169 * Process the form submission.
171 public function postProcess() {
172 if ($this->_action
& CRM_Core_Action
::DELETE
) {
173 if (CRM_Financial_BAO_FinancialAccount
::del($this->_id
)) {
174 CRM_Core_Session
::setStatus(ts('Selected Financial Account has been deleted.'));
177 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
181 // store the submitted values in an array
182 $params = $this->exportValues();
184 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
185 $params['id'] = $this->_id
;
193 $params[$field] = CRM_Utils_Array
::value($field, $params, FALSE);
195 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params);
196 CRM_Core_Session
::setStatus(ts('The Financial Account \'%1\' has been saved.', [1 => $financialAccount->name
]), ts('Saved'), 'success');