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