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
{
22 use CRM_Core_Form_EntityFormTrait
;
25 * Flag if its a AR account type.
29 protected $_isARFlag = FALSE;
32 * Explicitly declare the entity api name.
36 public function getDefaultEntity() {
37 return 'FinancialAccount';
41 * Set variables up before form is built.
43 public function preProcess() {
46 // Add custom data to form
47 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'FinancialAccount', $this->_id
);
48 CRM_Custom_Form_CustomData
::buildQuickForm($this);
54 $financialAccount = CRM_Financial_BAO_FinancialAccount
::retrieve($params);
55 $financialAccountTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
56 if ($financialAccount->financial_account_type_id
== $financialAccountTypeId
57 && strtolower($financialAccount->account_type_code
) == 'ar'
58 && !CRM_Financial_BAO_FinancialAccount
::getARAccounts($this->_id
, $financialAccountTypeId)
60 $this->_isARFlag
= TRUE;
61 if ($this->_action
& CRM_Core_Action
::DELETE
) {
62 $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).");
63 CRM_Core_Error
::statusBounce($msg);
70 * Build the form object.
72 public function buildQuickForm() {
73 parent
::buildQuickForm();
74 $this->setPageTitle(ts('Financial Account'));
76 if ($this->_action
& CRM_Core_Action
::DELETE
) {
80 $this->applyFilter('__ALL__', 'trim');
81 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_FinancialAccount');
82 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
83 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
84 'objectExists', ['CRM_Financial_DAO_FinancialAccount', $this->_id
]);
86 $this->add('text', 'description', ts('Description'), $attributes['description']);
87 $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
88 $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
89 $this->addEntityRef('contact_id', ts('Owner'), [
90 'api' => ['params' => ['contact_type' => 'Organization']],
93 $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
94 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
95 $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
96 $this->add('checkbox', 'is_tax', ts('Is Tax?'));
98 $element = $this->add('checkbox', 'is_default', ts('Default?'));
99 // CRM-12470 freeze is default if is_default is set
100 if ($this->_id
&& CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id
, 'is_default')) {
104 $financialAccountType = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
105 if (!empty($financialAccountType)) {
106 $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
107 ['' => ts('- select -')] +
$financialAccountType, TRUE, ['class' => 'crm-select2 huge']);
108 if ($this->_isARFlag
) {
110 $elementAccounting->freeze();
111 $elementActive->freeze();
113 elseif ($this->_id
&& CRM_Financial_BAO_FinancialAccount
::validateFinancialAccount($this->_id
)) {
118 $this->addCustomDataToForm();
119 if ($this->_action
== CRM_Core_Action
::UPDATE
&&
120 CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id
, 'is_reserved')
122 $this->freeze(['name', 'description', 'is_active']);
124 $this->addFormRule(['CRM_Financial_Form_FinancialAccount', 'formRule'], $this);
128 * Global validation rules for the form.
130 * @param array $values
131 * posted values of the form
136 * list of errors to be posted back to the form
138 public static function formRule($values, $files, $self) {
140 $financialAccountTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' "));
141 if (isset($values['is_tax'])) {
142 if ($values['financial_account_type_id'] != $financialAccountTypeId) {
143 $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.');
145 if (!isset($values['tax_rate'])) {
146 $errorMsg['tax_rate'] = ts('Please enter value for tax rate');
149 if ((CRM_Utils_Array
::value('tax_rate', $values) != NULL)) {
150 if ($values['tax_rate'] < 0 ||
$values['tax_rate'] >= 100) {
151 $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
154 if ($self->_action
& CRM_Core_Action
::UPDATE
) {
155 if (!(isset($values['is_tax']))) {
156 // @todo replace with call to CRM_Financial_BAO_FinancialAccount getSalesTaxFinancialAccount
157 $relationshipId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
159 'financial_account_id' => $self->_id
,
160 'account_relationship' => $relationshipId,
162 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
164 $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
168 return CRM_Utils_Array
::crmIsEmptyArray($errorMsg) ?
TRUE : $errorMsg;
172 * Set default values for the form.
173 * the default values are retrieved from the database.
175 public function setDefaultValues() {
176 $defaults = parent
::setDefaultValues();
177 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
178 if ($this->_action
& CRM_Core_Action
::ADD
) {
179 $defaults['contact_id'] = CRM_Core_BAO_Domain
::getDomain()->contact_id
;
185 * Process the form submission.
187 public function postProcess() {
188 if ($this->_action
& CRM_Core_Action
::DELETE
) {
189 if (CRM_Financial_BAO_FinancialAccount
::del($this->_id
)) {
190 CRM_Core_Session
::setStatus(ts('Selected Financial Account has been deleted.'));
193 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
197 // store the submitted values in an array
198 $params = $this->exportValues();
199 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($this->_submitValues
, $this->_id
, 'FinancialAccount');
201 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
202 $params['id'] = $this->_id
;
210 $params[$field] = CRM_Utils_Array
::value($field, $params, FALSE);
212 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params);
213 CRM_Core_Session
::setStatus(ts('The Financial Account \'%1\' has been saved.', [1 => $financialAccount->name
]), ts('Saved'), 'success');