Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class generates form components for Financial Type Account | |
6a488035 TO |
36 | */ |
37 | class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form { | |
03e04002 | 38 | |
6a488035 | 39 | /** |
fe482240 | 40 | * The financial type id saved to the session for an update. |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | protected $_aid; | |
03e04002 | 45 | |
6a488035 TO |
46 | /** |
47 | * The financial type accounts id, used when editing the field | |
48 | * | |
49 | * @var int | |
6a488035 TO |
50 | */ |
51 | protected $_id; | |
03e04002 | 52 | |
6a488035 | 53 | /** |
fe482240 | 54 | * The name of the BAO object for this form. |
6a488035 TO |
55 | * |
56 | * @var string | |
57 | */ | |
58 | protected $_BAOName; | |
03e04002 | 59 | |
7d289724 | 60 | /** |
fe482240 | 61 | * Flag if its a AR account type. |
7d289724 | 62 | * |
d51c6add | 63 | * @var bool |
7d289724 PN |
64 | */ |
65 | protected $_isARFlag = FALSE; | |
8ef12e64 | 66 | |
6a488035 | 67 | /** |
fe482240 | 68 | * Set variables up before form is built. |
6a488035 TO |
69 | */ |
70 | public function preProcess() { | |
71 | $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this); | |
d0f466d1 KJ |
72 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
73 | ||
6a488035 | 74 | if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { |
7d289724 | 75 | $this->_id = CRM_Utils_Type::escape($this->_id, 'Positive'); |
6a488035 | 76 | } |
8ef12e64 | 77 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', |
78 | "reset=1&action=browse&aid={$this->_aid}"); | |
79 | ||
6a488035 TO |
80 | $this->_BAOName = 'CRM_Financial_BAO_FinancialTypeAccount'; |
81 | if ($this->_aid && ($this->_action & CRM_Core_Action::ADD)) { | |
82 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name'); | |
83 | CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Financial Accounts')); | |
03e04002 | 84 | |
8ef12e64 | 85 | $session = CRM_Core_Session::singleton(); |
6a488035 | 86 | $session->pushUserContext($url); |
8ef12e64 | 87 | } |
7d289724 | 88 | // CRM-12492 |
8ef12e64 | 89 | if (!($this->_action & CRM_Core_Action::ADD)) { |
7d289724 PN |
90 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
91 | $accountRelationship = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'account_relationship'); | |
92 | if ($accountRelationship == $relationTypeId) { | |
93 | $this->_isARFlag = TRUE; | |
94 | if ($this->_action & CRM_Core_Action::DELETE) { | |
8ef12e64 | 95 | CRM_Core_Session::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."), |
7d289724 PN |
96 | '', 'error'); |
97 | CRM_Utils_System::redirect($url); | |
98 | } | |
99 | } | |
03e04002 | 100 | } |
6a488035 TO |
101 | if ($this->_id) { |
102 | $financialAccount = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'financial_account_id'); | |
103 | $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name'); | |
86bfa4f6 | 104 | CRM_Utils_System::setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts')); |
6a488035 | 105 | } |
03e04002 | 106 | |
be2fb01f CW |
107 | $breadCrumb = [ |
108 | [ | |
353ffa53 | 109 | 'title' => ts('Financial Type Accounts'), |
6a488035 | 110 | 'url' => $url, |
be2fb01f CW |
111 | ], |
112 | ]; | |
6a488035 TO |
113 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
114 | } | |
03e04002 | 115 | |
6a488035 | 116 | /** |
fe482240 | 117 | * Build the form object. |
6a488035 TO |
118 | */ |
119 | public function buildQuickForm() { | |
e2046b33 | 120 | parent::buildQuickForm(); |
468dd6c0 | 121 | $this->setPageTitle(ts('Financial Type Account')); |
e2046b33 | 122 | |
6a488035 | 123 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
124 | return; |
125 | } | |
03e04002 | 126 | |
6a488035 | 127 | if (isset($this->_id)) { |
be2fb01f | 128 | $params = ['id' => $this->_id]; |
6a488035 TO |
129 | CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); |
130 | $this->setDefaults($defaults); | |
131 | $financialAccountTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name'); | |
132 | } | |
03e04002 | 133 | |
6a488035 | 134 | $this->applyFilter('__ALL__', 'trim'); |
03e04002 | 135 | |
6a488035 TO |
136 | if ($this->_action == CRM_Core_Action::UPDATE) { |
137 | $this->assign('aid', $this->_id); | |
cded2ebf | 138 | // hidden field to catch the group id in profile |
6a488035 | 139 | $this->add('hidden', 'financial_type_id', $this->_aid); |
03e04002 | 140 | |
cded2ebf | 141 | // hidden field to catch the field id in profile |
6a488035 TO |
142 | $this->add('hidden', 'account_type_id', $this->_id); |
143 | } | |
48a29690 | 144 | $params['orderColumn'] = 'label'; |
6d02eb56 | 145 | $AccountTypeRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params); |
6a488035 | 146 | if (!empty($AccountTypeRelationship)) { |
d0f466d1 | 147 | $element = $this->add('select', |
03e04002 | 148 | 'account_relationship', |
6a488035 | 149 | ts('Financial Account Relationship'), |
be2fb01f | 150 | ['select' => ts('- Select Financial Account Relationship -')] + $AccountTypeRelationship, |
03e04002 | 151 | TRUE |
6a488035 TO |
152 | ); |
153 | } | |
03e04002 | 154 | |
7d289724 PN |
155 | if ($this->_isARFlag) { |
156 | $element->freeze(); | |
157 | } | |
d0f466d1 | 158 | |
6a488035 | 159 | if ($this->_action == CRM_Core_Action::ADD) { |
8cc574cf | 160 | if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) { |
48a29690 | 161 | $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); |
b800df93 | 162 | $financialAccountType = CRM_Utils_Array::value($this->_submitValues['account_relationship'], $financialAccountType); |
6a488035 | 163 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); |
03e04002 | 164 | |
be2fb01f | 165 | $financialAccountSelect = ['' => ts('- select -')] + $result; |
6a488035 TO |
166 | } |
167 | else { | |
be2fb01f | 168 | $financialAccountSelect = [ |
408b79bf | 169 | 'select' => ts('- select -'), |
be2fb01f | 170 | ] + CRM_Contribute_PseudoConstant::financialAccount(); |
6a488035 TO |
171 | } |
172 | } | |
173 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
48a29690 | 174 | $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); |
6a488035 TO |
175 | $financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']]; |
176 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); | |
03e04002 | 177 | |
be2fb01f | 178 | $financialAccountSelect = ['' => ts('- select -')] + $result; |
6a488035 | 179 | } |
dae00956 | 180 | |
03e04002 | 181 | $this->add('select', |
182 | 'financial_account_id', | |
183 | ts('Financial Account'), | |
6a488035 TO |
184 | $financialAccountSelect, |
185 | TRUE | |
186 | ); | |
187 | ||
be2fb01f | 188 | $this->addButtons([ |
7b966967 SL |
189 | [ |
190 | 'type' => 'next', | |
191 | 'name' => ts('Save'), | |
192 | 'isDefault' => TRUE, | |
193 | ], | |
194 | [ | |
195 | 'type' => 'next', | |
196 | 'name' => ts('Save and New'), | |
197 | 'subName' => 'new', | |
198 | ], | |
199 | [ | |
200 | 'type' => 'cancel', | |
201 | 'name' => ts('Cancel'), | |
202 | ], | |
203 | ]); | |
be2fb01f | 204 | $this->addFormRule(['CRM_Financial_Form_FinancialTypeAccount', 'formRule'], $this); |
6a488035 | 205 | } |
03e04002 | 206 | |
6a488035 | 207 | /** |
fe482240 | 208 | * Global validation rules for the form. |
6a488035 | 209 | * |
16b10e64 CW |
210 | * @param array $values |
211 | * posted values of the form | |
fd31fa4c EM |
212 | * @param $files |
213 | * @param $self | |
214 | * | |
a6c01b45 CW |
215 | * @return array |
216 | * list of errors to be posted back to the form | |
6a488035 | 217 | */ |
00be9182 | 218 | public static function formRule($values, $files, $self) { |
be2fb01f | 219 | $errorMsg = []; |
6a488035 TO |
220 | $errorFlag = FALSE; |
221 | if ($self->_action == CRM_Core_Action::DELETE) { | |
7611ae71 | 222 | $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); |
6a488035 TO |
223 | if (CRM_Utils_Array::value('financial_account_id', $values) != 'select') { |
224 | if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' || $relationValues[$values['account_relationship']] == 'Cost of Sales Account is') { | |
225 | $premiumsProduct = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id'); | |
226 | $product = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id'); | |
227 | if (!empty($premiumsProduct) || !empty($product)) { | |
228 | $errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.'; | |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | if (CRM_Utils_Array::value('account_relationship', $values) == 'select') { | |
234 | $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.'; | |
235 | } | |
236 | if (CRM_Utils_Array::value('financial_account_id', $values) == 'select') { | |
237 | $errorMsg['financial_account_id'] = 'Financial Account is a required field.'; | |
238 | } | |
8cc574cf | 239 | if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) { |
be2fb01f | 240 | $params = [ |
6a488035 | 241 | 'account_relationship' => $values['account_relationship'], |
353ffa53 | 242 | 'entity_id' => $self->_aid, |
55e6847a | 243 | 'entity_table' => 'civicrm_financial_type', |
be2fb01f CW |
244 | ]; |
245 | $defaults = []; | |
6a488035 | 246 | if ($self->_action == CRM_Core_Action::ADD) { |
707f6952 RK |
247 | $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); |
248 | $isTax = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $values['financial_account_id'], 'is_tax'); | |
249 | if ($values['account_relationship'] == $relationshipId) { | |
250 | if (!($isTax)) { | |
251 | $errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account'); | |
252 | } | |
253 | } | |
6a488035 TO |
254 | $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); |
255 | if ($result) { | |
256 | $errorFlag = TRUE; | |
257 | } | |
258 | } | |
259 | if ($self->_action == CRM_Core_Action::UPDATE) { | |
260 | if ($values['account_relationship'] == $self->_defaultValues['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues['financial_account_id']) { | |
261 | $errorFlag = FALSE; | |
262 | } | |
263 | else { | |
264 | $params['financial_account_id'] = $values['financial_account_id']; | |
265 | $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); | |
266 | if ($result) { | |
267 | $errorFlag = TRUE; | |
268 | } | |
269 | } | |
03e04002 | 270 | } |
271 | ||
6a488035 TO |
272 | if ($errorFlag) { |
273 | $errorMsg['account_relationship'] = ts('This account relationship already exits'); | |
274 | } | |
275 | } | |
276 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; | |
277 | } | |
03e04002 | 278 | |
6a488035 | 279 | /** |
fe482240 | 280 | * Process the form submission. |
6a488035 TO |
281 | */ |
282 | public function postProcess() { | |
283 | if ($this->_action & CRM_Core_Action::DELETE) { | |
284 | CRM_Financial_BAO_FinancialTypeAccount::del($this->_id, $this->_aid); | |
285 | CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.')); | |
03e04002 | 286 | } |
287 | else { | |
be2fb01f | 288 | $params = $ids = []; |
6a488035 TO |
289 | // store the submitted values in an array |
290 | $params = $this->exportValues(); | |
03e04002 | 291 | |
6a488035 TO |
292 | if ($this->_action & CRM_Core_Action::UPDATE) { |
293 | $ids['entityFinancialAccount'] = $this->_id; | |
294 | } | |
295 | if ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) { | |
03e04002 | 296 | $params['financial_account_id'] = $this->_submitValues['financial_account_id']; |
6a488035 TO |
297 | } |
298 | $params['entity_table'] = 'civicrm_financial_type'; | |
299 | if ($this->_action & CRM_Core_Action::ADD) { | |
300 | $params['entity_id'] = $this->_aid; | |
301 | } | |
a48a4a2b JP |
302 | try { |
303 | $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids); | |
6bef1cdd | 304 | CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'), ts('Saved'), 'success'); |
74c3274a | 305 | } |
a48a4a2b JP |
306 | catch (CRM_Core_Exception $e) { |
307 | CRM_Core_Error::statusBounce($e->getMessage()); | |
308 | } | |
6a488035 TO |
309 | } |
310 | ||
311 | $buttonName = $this->controller->getButtonName(); | |
312 | $session = CRM_Core_Session::singleton(); | |
313 | ||
314 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
315 | CRM_Core_Session::setStatus(ts(' You can add another Financial Account Type.')); | |
03e04002 | 316 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', |
6a488035 | 317 | "reset=1&action=add&aid={$this->_aid}")); |
03e04002 | 318 | } |
6a488035 | 319 | else { |
03e04002 | 320 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', |
6a488035 | 321 | "reset=1&action=browse&aid={$this->_aid}")); |
03e04002 | 322 | } |
6a488035 | 323 | } |
e2046b33 | 324 | |
6a488035 | 325 | } |