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