Merge pull request #21859 from mariav0/patch-2
[civicrm-core.git] / CRM / Financial / Form / FinancialAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Financial Account
20 */
21 class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
22
23 /**
24 * Flag if its a AR account type.
25 *
26 * @var bool
27 */
28 protected $_isARFlag = FALSE;
29
30 /**
31 * Set variables up before form is built.
32 */
33 public function preProcess() {
34 parent::preProcess();
35
36 if ($this->_id) {
37 $params = [
38 'id' => $this->_id,
39 ];
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)
45 ) {
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);
50 }
51 }
52 }
53 }
54
55 /**
56 * Build the form object.
57 */
58 public function buildQuickForm() {
59 parent::buildQuickForm();
60 $this->setPageTitle(ts('Financial Account'));
61
62 if ($this->_action & CRM_Core_Action::DELETE) {
63 return;
64 }
65
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]);
71
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']],
77 'create' => TRUE,
78 ]);
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?'));
83
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')) {
87 $element->freeze();
88 }
89
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) {
95 $element->freeze();
96 $elementAccounting->freeze();
97 $elementActive->freeze();
98 }
99 elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) {
100 $element->freeze();
101 }
102 }
103
104 if ($this->_action == CRM_Core_Action::UPDATE &&
105 CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')
106 ) {
107 $this->freeze(['name', 'description', 'is_active']);
108 }
109 $this->addFormRule(['CRM_Financial_Form_FinancialAccount', 'formRule'], $this);
110 }
111
112 /**
113 * Global validation rules for the form.
114 *
115 * @param array $values
116 * posted values of the form
117 * @param $files
118 * @param $self
119 *
120 * @return array
121 * list of errors to be posted back to the form
122 */
123 public static function formRule($values, $files, $self) {
124 $errorMsg = [];
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.');
129 }
130 if (!isset($values['tax_rate'])) {
131 $errorMsg['tax_rate'] = ts('Please enter value for tax rate');
132 }
133 }
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');
137 }
138 }
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' "));
143 $params = [
144 'financial_account_id' => $self->_id,
145 'account_relationship' => $relationshipId,
146 ];
147 $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
148 if ($result) {
149 $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
150 }
151 }
152 }
153 return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
154 }
155
156 /**
157 * Set default values for the form.
158 * the default values are retrieved from the database.
159 */
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;
164 }
165 return $defaults;
166 }
167
168 /**
169 * Process the form submission.
170 */
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.'));
175 }
176 else {
177 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
178 }
179 }
180 else {
181 // store the submitted values in an array
182 $params = $this->exportValues();
183
184 if ($this->_action & CRM_Core_Action::UPDATE) {
185 $params['id'] = $this->_id;
186 }
187 foreach ([
188 'is_active',
189 'is_deductible',
190 'is_tax',
191 'is_default',
192 ] as $field) {
193 $params[$field] = CRM_Utils_Array::value($field, $params, FALSE);
194 }
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');
197 }
198 }
199
200 }