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