Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class generates form components for Financial Type Account | |
6a488035 | 20 | */ |
e7d7f47d | 21 | class CRM_Financial_Form_FinancialTypeAccount extends CRM_Core_Form { |
03e04002 | 22 | |
6a488035 | 23 | /** |
fe482240 | 24 | * The financial type id saved to the session for an update. |
6a488035 TO |
25 | * |
26 | * @var int | |
6a488035 TO |
27 | */ |
28 | protected $_aid; | |
03e04002 | 29 | |
6a488035 TO |
30 | /** |
31 | * The financial type accounts id, used when editing the field | |
32 | * | |
33 | * @var int | |
6a488035 TO |
34 | */ |
35 | protected $_id; | |
03e04002 | 36 | |
6a488035 | 37 | /** |
fe482240 | 38 | * The name of the BAO object for this form. |
6a488035 TO |
39 | * |
40 | * @var string | |
41 | */ | |
42 | protected $_BAOName; | |
03e04002 | 43 | |
7d289724 | 44 | /** |
fe482240 | 45 | * Flag if its a AR account type. |
7d289724 | 46 | * |
d51c6add | 47 | * @var bool |
7d289724 PN |
48 | */ |
49 | protected $_isARFlag = FALSE; | |
8ef12e64 | 50 | |
6a488035 | 51 | /** |
fe482240 | 52 | * Set variables up before form is built. |
6a488035 TO |
53 | */ |
54 | public function preProcess() { | |
55 | $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this); | |
d0f466d1 KJ |
56 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
57 | ||
6a488035 | 58 | if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { |
7d289724 | 59 | $this->_id = CRM_Utils_Type::escape($this->_id, 'Positive'); |
6a488035 | 60 | } |
8ef12e64 | 61 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', |
62 | "reset=1&action=browse&aid={$this->_aid}"); | |
63 | ||
6a488035 TO |
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'); | |
94fd9d17 | 67 | $this->setTitle($this->_title . ' - ' . ts('Financial Accounts')); |
03e04002 | 68 | |
8ef12e64 | 69 | $session = CRM_Core_Session::singleton(); |
6a488035 | 70 | $session->pushUserContext($url); |
8ef12e64 | 71 | } |
7d289724 | 72 | // CRM-12492 |
8ef12e64 | 73 | if (!($this->_action & CRM_Core_Action::ADD)) { |
7d289724 PN |
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) { | |
8ef12e64 | 79 | CRM_Core_Session::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."), |
7d289724 PN |
80 | '', 'error'); |
81 | CRM_Utils_System::redirect($url); | |
82 | } | |
83 | } | |
03e04002 | 84 | } |
6a488035 TO |
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'); | |
94fd9d17 | 88 | $this->setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts')); |
6a488035 | 89 | } |
03e04002 | 90 | |
be2fb01f CW |
91 | $breadCrumb = [ |
92 | [ | |
353ffa53 | 93 | 'title' => ts('Financial Type Accounts'), |
6a488035 | 94 | 'url' => $url, |
be2fb01f CW |
95 | ], |
96 | ]; | |
6a488035 TO |
97 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
98 | } | |
03e04002 | 99 | |
6a488035 | 100 | /** |
fe482240 | 101 | * Build the form object. |
6a488035 TO |
102 | */ |
103 | public function buildQuickForm() { | |
e2046b33 | 104 | parent::buildQuickForm(); |
e7d7f47d | 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 | } | |
468dd6c0 | 127 | $this->setPageTitle(ts('Financial Type Account')); |
e2046b33 | 128 | |
6a488035 | 129 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
130 | return; |
131 | } | |
03e04002 | 132 | |
6a488035 | 133 | if (isset($this->_id)) { |
be2fb01f | 134 | $params = ['id' => $this->_id]; |
6a488035 TO |
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 | } | |
03e04002 | 139 | |
6a488035 | 140 | $this->applyFilter('__ALL__', 'trim'); |
03e04002 | 141 | |
6a488035 TO |
142 | if ($this->_action == CRM_Core_Action::UPDATE) { |
143 | $this->assign('aid', $this->_id); | |
cded2ebf | 144 | // hidden field to catch the group id in profile |
6a488035 | 145 | $this->add('hidden', 'financial_type_id', $this->_aid); |
03e04002 | 146 | |
cded2ebf | 147 | // hidden field to catch the field id in profile |
6a488035 TO |
148 | $this->add('hidden', 'account_type_id', $this->_id); |
149 | } | |
48a29690 | 150 | $params['orderColumn'] = 'label'; |
6d02eb56 | 151 | $AccountTypeRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params); |
6a488035 | 152 | if (!empty($AccountTypeRelationship)) { |
d0f466d1 | 153 | $element = $this->add('select', |
03e04002 | 154 | 'account_relationship', |
6a488035 | 155 | ts('Financial Account Relationship'), |
be2fb01f | 156 | ['select' => ts('- Select Financial Account Relationship -')] + $AccountTypeRelationship, |
03e04002 | 157 | TRUE |
6a488035 TO |
158 | ); |
159 | } | |
03e04002 | 160 | |
7d289724 PN |
161 | if ($this->_isARFlag) { |
162 | $element->freeze(); | |
163 | } | |
d0f466d1 | 164 | |
6a488035 | 165 | if ($this->_action == CRM_Core_Action::ADD) { |
8cc574cf | 166 | if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) { |
48a29690 | 167 | $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); |
9c1bc317 | 168 | $financialAccountType = $financialAccountType[$this->_submitValues['account_relationship']] ?? NULL; |
6a488035 | 169 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); |
03e04002 | 170 | |
be2fb01f | 171 | $financialAccountSelect = ['' => ts('- select -')] + $result; |
6a488035 TO |
172 | } |
173 | else { | |
be2fb01f | 174 | $financialAccountSelect = [ |
408b79bf | 175 | 'select' => ts('- select -'), |
be2fb01f | 176 | ] + CRM_Contribute_PseudoConstant::financialAccount(); |
6a488035 TO |
177 | } |
178 | } | |
179 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
48a29690 | 180 | $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); |
6a488035 TO |
181 | $financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']]; |
182 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); | |
03e04002 | 183 | |
be2fb01f | 184 | $financialAccountSelect = ['' => ts('- select -')] + $result; |
6a488035 | 185 | } |
dae00956 | 186 | |
03e04002 | 187 | $this->add('select', |
188 | 'financial_account_id', | |
189 | ts('Financial Account'), | |
6a488035 TO |
190 | $financialAccountSelect, |
191 | TRUE | |
192 | ); | |
193 | ||
be2fb01f | 194 | $this->addFormRule(['CRM_Financial_Form_FinancialTypeAccount', 'formRule'], $this); |
6a488035 | 195 | } |
03e04002 | 196 | |
6a488035 | 197 | /** |
fe482240 | 198 | * Global validation rules for the form. |
6a488035 | 199 | * |
16b10e64 CW |
200 | * @param array $values |
201 | * posted values of the form | |
fd31fa4c EM |
202 | * @param $files |
203 | * @param $self | |
204 | * | |
a6c01b45 CW |
205 | * @return array |
206 | * list of errors to be posted back to the form | |
6a488035 | 207 | */ |
00be9182 | 208 | public static function formRule($values, $files, $self) { |
be2fb01f | 209 | $errorMsg = []; |
6a488035 TO |
210 | $errorFlag = FALSE; |
211 | if ($self->_action == CRM_Core_Action::DELETE) { | |
7611ae71 | 212 | $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); |
6a488035 TO |
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 | } | |
8cc574cf | 229 | if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) { |
be2fb01f | 230 | $params = [ |
6a488035 | 231 | 'account_relationship' => $values['account_relationship'], |
353ffa53 | 232 | 'entity_id' => $self->_aid, |
55e6847a | 233 | 'entity_table' => 'civicrm_financial_type', |
be2fb01f CW |
234 | ]; |
235 | $defaults = []; | |
6a488035 | 236 | if ($self->_action == CRM_Core_Action::ADD) { |
07376a08 | 237 | // @todo replace with call to CRM_Financial_BAO_FinancialAccount getSalesTaxFinancialAccount |
707f6952 RK |
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 | } | |
6a488035 TO |
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 | } | |
03e04002 | 261 | } |
262 | ||
6a488035 TO |
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 | } | |
03e04002 | 269 | |
6a488035 | 270 | /** |
fe482240 | 271 | * Process the form submission. |
6a488035 TO |
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.')); | |
03e04002 | 277 | } |
278 | else { | |
be2fb01f | 279 | $params = $ids = []; |
6a488035 TO |
280 | // store the submitted values in an array |
281 | $params = $this->exportValues(); | |
03e04002 | 282 | |
6a488035 TO |
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) { | |
03e04002 | 287 | $params['financial_account_id'] = $this->_submitValues['financial_account_id']; |
6a488035 TO |
288 | } |
289 | $params['entity_table'] = 'civicrm_financial_type'; | |
290 | if ($this->_action & CRM_Core_Action::ADD) { | |
291 | $params['entity_id'] = $this->_aid; | |
292 | } | |
a48a4a2b JP |
293 | try { |
294 | $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids); | |
6bef1cdd | 295 | CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'), ts('Saved'), 'success'); |
74c3274a | 296 | } |
a48a4a2b JP |
297 | catch (CRM_Core_Exception $e) { |
298 | CRM_Core_Error::statusBounce($e->getMessage()); | |
299 | } | |
6a488035 TO |
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.')); | |
03e04002 | 307 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', |
6a488035 | 308 | "reset=1&action=add&aid={$this->_aid}")); |
03e04002 | 309 | } |
6a488035 | 310 | else { |
03e04002 | 311 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', |
6a488035 | 312 | "reset=1&action=browse&aid={$this->_aid}")); |
03e04002 | 313 | } |
6a488035 | 314 | } |
e2046b33 | 315 | |
6a488035 | 316 | } |