Revert "Schema - Fix boolean fields in various tables"
[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 use CRM_Core_Form_EntityFormTrait;
23
24 /**
25 * Flag if its a AR account type.
26 *
27 * @var bool
28 */
29 protected $_isARFlag = FALSE;
30
31 /**
32 * Explicitly declare the entity api name.
33 *
34 * @return string
35 */
36 public function getDefaultEntity() {
37 return 'FinancialAccount';
38 }
39
40 /**
41 * Set variables up before form is built.
42 */
43 public function preProcess() {
44 parent::preProcess();
45
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);
49
50 if ($this->_id) {
51 $params = [
52 'id' => $this->_id,
53 ];
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)
59 ) {
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);
64 }
65 }
66 }
67 }
68
69 /**
70 * Build the form object.
71 */
72 public function buildQuickForm() {
73 parent::buildQuickForm();
74 $this->setPageTitle(ts('Financial Account'));
75
76 if ($this->_action & CRM_Core_Action::DELETE) {
77 return;
78 }
79
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]);
85
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']],
91 'create' => TRUE,
92 ]);
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?'));
97
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')) {
101 $element->freeze();
102 }
103
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) {
109 $element->freeze();
110 $elementAccounting->freeze();
111 $elementActive->freeze();
112 }
113 elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) {
114 $element->freeze();
115 }
116 }
117
118 $this->addCustomDataToForm();
119 if ($this->_action == CRM_Core_Action::UPDATE &&
120 CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')
121 ) {
122 $this->freeze(['name', 'description', 'is_active']);
123 }
124 $this->addFormRule(['CRM_Financial_Form_FinancialAccount', 'formRule'], $this);
125 }
126
127 /**
128 * Global validation rules for the form.
129 *
130 * @param array $values
131 * posted values of the form
132 * @param $files
133 * @param self $self
134 *
135 * @return array
136 * list of errors to be posted back to the form
137 */
138 public static function formRule($values, $files, $self) {
139 $errorMsg = [];
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.');
144 }
145 if (!isset($values['tax_rate'])) {
146 $errorMsg['tax_rate'] = ts('Please enter value for tax rate');
147 }
148 }
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');
152 }
153 }
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' "));
158 $params = [
159 'financial_account_id' => $self->_id,
160 'account_relationship' => $relationshipId,
161 ];
162 $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
163 if ($result) {
164 $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
165 }
166 }
167 }
168 return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
169 }
170
171 /**
172 * Set default values for the form.
173 * the default values are retrieved from the database.
174 */
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;
180 }
181 return $defaults;
182 }
183
184 /**
185 * Process the form submission.
186 */
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.'));
191 }
192 else {
193 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
194 }
195 }
196 else {
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');
200
201 if ($this->_action & CRM_Core_Action::UPDATE) {
202 $params['id'] = $this->_id;
203 }
204 foreach ([
205 'is_active',
206 'is_deductible',
207 'is_tax',
208 'is_default',
209 ] as $field) {
210 $params[$field] = CRM_Utils_Array::value($field, $params, FALSE);
211 }
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');
214 }
215 }
216
217 }