Revert "Schema - Fix boolean fields in various tables"
[civicrm-core.git] / CRM / Financial / Form / FinancialTypeAccount.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 Type Account
20 */
21 class CRM_Financial_Form_FinancialTypeAccount extends CRM_Core_Form {
22
23 /**
24 * The financial type id saved to the session for an update.
25 *
26 * @var int
27 */
28 protected $_aid;
29
30 /**
31 * The financial type accounts id, used when editing the field
32 *
33 * @var int
34 */
35 protected $_id;
36
37 /**
38 * The name of the BAO object for this form.
39 *
40 * @var string
41 */
42 protected $_BAOName;
43
44 /**
45 * Flag if its a AR account type.
46 *
47 * @var bool
48 */
49 protected $_isARFlag = FALSE;
50
51 /**
52 * Set variables up before form is built.
53 */
54 public function preProcess() {
55 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this);
56 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
57
58 if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) {
59 $this->_id = CRM_Utils_Type::escape($this->_id, 'Positive');
60 }
61 $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
62 "reset=1&action=browse&aid={$this->_aid}");
63
64 $this->_BAOName = 'CRM_Financial_BAO_FinancialTypeAccount';
65 if ($this->_aid && ($this->_action & CRM_Core_Action::ADD)) {
66 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name');
67 $this->setTitle($this->_title . ' - ' . ts('Financial Accounts'));
68
69 $session = CRM_Core_Session::singleton();
70 $session->pushUserContext($url);
71 }
72 // CRM-12492
73 if (!($this->_action & CRM_Core_Action::ADD)) {
74 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
75 $accountRelationship = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'account_relationship');
76 if ($accountRelationship == $relationTypeId) {
77 $this->_isARFlag = TRUE;
78 if ($this->_action & CRM_Core_Action::DELETE) {
79 CRM_Core_Session::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."),
80 '', 'error');
81 CRM_Utils_System::redirect($url);
82 }
83 }
84 }
85 if ($this->_id) {
86 $financialAccount = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'financial_account_id');
87 $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name');
88 $this->setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts'));
89 }
90
91 $breadCrumb = [
92 [
93 'title' => ts('Financial Type Accounts'),
94 'url' => $url,
95 ],
96 ];
97 CRM_Utils_System::appendBreadCrumb($breadCrumb);
98 }
99
100 /**
101 * Build the form object.
102 */
103 public function buildQuickForm() {
104 parent::buildQuickForm();
105 if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) {
106 $this->addButtons([
107 [
108 'type' => 'cancel',
109 'name' => ts('Done'),
110 'isDefault' => TRUE,
111 ],
112 ]);
113 }
114 else {
115 $this->addButtons([
116 [
117 'type' => 'next',
118 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
119 'isDefault' => TRUE,
120 ],
121 [
122 'type' => 'cancel',
123 'name' => ts('Cancel'),
124 ],
125 ]);
126 }
127 $this->setPageTitle(ts('Financial Type Account'));
128
129 if ($this->_action & CRM_Core_Action::DELETE) {
130 return;
131 }
132
133 if (isset($this->_id)) {
134 $params = ['id' => $this->_id];
135 CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
136 $this->setDefaults($defaults);
137 $financialAccountTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name');
138 }
139
140 $this->applyFilter('__ALL__', 'trim');
141
142 if ($this->_action == CRM_Core_Action::UPDATE) {
143 $this->assign('aid', $this->_id);
144 // hidden field to catch the group id in profile
145 $this->add('hidden', 'financial_type_id', $this->_aid);
146
147 // hidden field to catch the field id in profile
148 $this->add('hidden', 'account_type_id', $this->_id);
149 }
150 $params['orderColumn'] = 'label';
151 $AccountTypeRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
152 if (!empty($AccountTypeRelationship)) {
153 $element = $this->add('select',
154 'account_relationship',
155 ts('Financial Account Relationship'),
156 ['select' => ts('- Select Financial Account Relationship -')] + $AccountTypeRelationship,
157 TRUE
158 );
159 }
160
161 if ($this->_isARFlag) {
162 $element->freeze();
163 }
164
165 if ($this->_action == CRM_Core_Action::ADD) {
166 if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) {
167 $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
168 $financialAccountType = $financialAccountType[$this->_submitValues['account_relationship']] ?? NULL;
169 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
170
171 $financialAccountSelect = ['' => ts('- select -')] + $result;
172 }
173 else {
174 $financialAccountSelect = [
175 'select' => ts('- select -'),
176 ] + CRM_Contribute_PseudoConstant::financialAccount();
177 }
178 }
179 if ($this->_action == CRM_Core_Action::UPDATE) {
180 $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
181 $financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']];
182 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
183
184 $financialAccountSelect = ['' => ts('- select -')] + $result;
185 }
186
187 $this->add('select',
188 'financial_account_id',
189 ts('Financial Account'),
190 $financialAccountSelect,
191 TRUE
192 );
193
194 $this->addFormRule(['CRM_Financial_Form_FinancialTypeAccount', 'formRule'], $this);
195 }
196
197 /**
198 * Global validation rules for the form.
199 *
200 * @param array $values
201 * posted values of the form
202 * @param $files
203 * @param self $self
204 *
205 * @return array
206 * list of errors to be posted back to the form
207 */
208 public static function formRule($values, $files, $self) {
209 $errorMsg = [];
210 $errorFlag = FALSE;
211 if ($self->_action == CRM_Core_Action::DELETE) {
212 $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
213 if (CRM_Utils_Array::value('financial_account_id', $values) != 'select') {
214 if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' || $relationValues[$values['account_relationship']] == 'Cost of Sales Account is') {
215 $premiumsProduct = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id');
216 $product = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id');
217 if (!empty($premiumsProduct) || !empty($product)) {
218 $errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.';
219 }
220 }
221 }
222 }
223 if (CRM_Utils_Array::value('account_relationship', $values) == 'select') {
224 $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.';
225 }
226 if (CRM_Utils_Array::value('financial_account_id', $values) == 'select') {
227 $errorMsg['financial_account_id'] = 'Financial Account is a required field.';
228 }
229 if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
230 $params = [
231 'account_relationship' => $values['account_relationship'],
232 'entity_id' => $self->_aid,
233 'entity_table' => 'civicrm_financial_type',
234 ];
235 $defaults = [];
236 if ($self->_action == CRM_Core_Action::ADD) {
237 // @todo replace with call to CRM_Financial_BAO_FinancialAccount getSalesTaxFinancialAccount
238 $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
239 $isTax = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $values['financial_account_id'], 'is_tax');
240 if ($values['account_relationship'] == $relationshipId) {
241 if (!($isTax)) {
242 $errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account');
243 }
244 }
245 $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
246 if ($result) {
247 $errorFlag = TRUE;
248 }
249 }
250 if ($self->_action == CRM_Core_Action::UPDATE) {
251 if ($values['account_relationship'] == $self->_defaultValues['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues['financial_account_id']) {
252 $errorFlag = FALSE;
253 }
254 else {
255 $params['financial_account_id'] = $values['financial_account_id'];
256 $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
257 if ($result) {
258 $errorFlag = TRUE;
259 }
260 }
261 }
262
263 if ($errorFlag) {
264 $errorMsg['account_relationship'] = ts('This account relationship already exits');
265 }
266 }
267 return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
268 }
269
270 /**
271 * Process the form submission.
272 */
273 public function postProcess() {
274 if ($this->_action & CRM_Core_Action::DELETE) {
275 CRM_Financial_BAO_FinancialTypeAccount::del($this->_id, $this->_aid);
276 CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.'));
277 }
278 else {
279 $params = $ids = [];
280 // store the submitted values in an array
281 $params = $this->exportValues();
282
283 if ($this->_action & CRM_Core_Action::UPDATE) {
284 $ids['entityFinancialAccount'] = $this->_id;
285 }
286 if ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) {
287 $params['financial_account_id'] = $this->_submitValues['financial_account_id'];
288 }
289 $params['entity_table'] = 'civicrm_financial_type';
290 if ($this->_action & CRM_Core_Action::ADD) {
291 $params['entity_id'] = $this->_aid;
292 }
293 try {
294 $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids);
295 CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'), ts('Saved'), 'success');
296 }
297 catch (CRM_Core_Exception $e) {
298 CRM_Core_Error::statusBounce($e->getMessage());
299 }
300 }
301
302 $buttonName = $this->controller->getButtonName();
303 $session = CRM_Core_Session::singleton();
304
305 if ($buttonName == $this->getButtonName('next', 'new')) {
306 CRM_Core_Session::setStatus(ts(' You can add another Financial Account Type.'));
307 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
308 "reset=1&action=add&aid={$this->_aid}"));
309 }
310 else {
311 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
312 "reset=1&action=browse&aid={$this->_aid}"));
313 }
314 }
315
316 }