Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-16-12-52-48
[civicrm-core.git] / CRM / Financial / Form / FinancialTypeAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @access protected
47 */
48 protected $_aid;
49
50 /**
51 * The financial type accounts id, used when editing the field
52 *
53 * @var int
54 * @access protected
55 */
56 protected $_id;
57
58 /**
59 * The name of the BAO object for this form
60 *
61 * @var string
62 */
63 protected $_BAOName;
64
65 /**
66 * Flag if its a AR account type
67 *
68 * @var boolean
69 */
70 protected $_isARFlag = FALSE;
71
72 /**
73 * Function to set variables up before form is built
74 *
75 * @return void
76 * @access public
77 */
78 public function preProcess() {
79 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this);
80 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
81
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 void
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::get('CRM_Financial_DAO_EntityFinancialAccount', '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
175 if ($this->_isARFlag) {
176 $element->freeze();
177 }
178
179 if ($this->_action == CRM_Core_Action::ADD) {
180 if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) {
181 $financialAccountType = array(
182 '5' => 5, //expense
183 '3' => 1, //AR relation
184 '1' => 3, //revenue
185 '6' => 1, //Asset
186 '7' => 4, //cost of sales
187 '8' => 1, //premium inventory
188 '9' => 3 //discount account is
189 );
190
191 $financialAccountType = CRM_Utils_Array::value($this->_submitValues['account_relationship'], $financialAccountType);
192 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
193
194 $financialAccountSelect = array('' => ts('- select -')) + $result;
195 }
196 else {
197 $financialAccountSelect = array(
198 'select' => ts('- select -')
199 ) + CRM_Contribute_PseudoConstant::financialAccount();
200 }
201 }
202 if ($this->_action == CRM_Core_Action::UPDATE) {
203 $financialAccountType = array(
204 '5' => 5, //expense
205 '3' => 1, //AR relation
206 '1' => 3, //revenue
207 '6' => 1, //Asset
208 '7' => 4, //cost of sales
209 '8' => 1, //premium inventory
210 '9' => 3 //discount account is
211 );
212
213 $financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']];
214 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
215
216 $financialAccountSelect = array('' => ts('- select -')) + $result;
217 }
218
219 $this->add('select',
220 'financial_account_id',
221 ts('Financial Account'),
222 $financialAccountSelect,
223 TRUE
224 );
225
226 $this->addButtons(array(
227 array(
228 'type' => 'next',
229 'name' => ts('Save'),
230 'isDefault' => TRUE
231 ),
232 array(
233 'type' => 'next',
234 'name' => ts('Save and New'),
235 'subName' => 'new'
236 ),
237 array (
238 'type' => 'cancel',
239 'name' => ts('Cancel')
240 ))
241 );
242 $this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this);
243 }
244
245 /**
246 * global validation rules for the form
247 *
248 * @param $values
249 * @param $files
250 * @param $self
251 *
252 * @internal param array $fields posted values of the form
253 *
254 * @return array list of errors to be posted back to the form
255 * @static
256 * @access public
257 */
258 static function formRule($values, $files, $self) {
259 $errorMsg = array();
260 $errorFlag = FALSE;
261 if ($self->_action == CRM_Core_Action::DELETE) {
262 $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
263 if (CRM_Utils_Array::value('financial_account_id', $values) != 'select') {
264 if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' || $relationValues[$values['account_relationship']] == 'Cost of Sales Account is') {
265 $premiumsProduct = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id');
266 $product = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id');
267 if (!empty($premiumsProduct) || !empty($product)) {
268 $errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.';
269 }
270 }
271 }
272 }
273 if (CRM_Utils_Array::value('account_relationship', $values) == 'select') {
274 $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.';
275 }
276 if (CRM_Utils_Array::value('financial_account_id', $values) == 'select') {
277 $errorMsg['financial_account_id'] = 'Financial Account is a required field.';
278 }
279 if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
280 $params = array(
281 'account_relationship' => $values['account_relationship'],
282 'entity_id' => $self->_aid
283 );
284 $defaults = array();
285 if ($self->_action == CRM_Core_Action::ADD) {
286 $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
287 if ($result) {
288 $errorFlag = TRUE;
289 }
290 }
291 if ($self->_action == CRM_Core_Action::UPDATE) {
292 if ($values['account_relationship'] == $self->_defaultValues['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues['financial_account_id']) {
293 $errorFlag = FALSE;
294 }
295 else {
296 $params['financial_account_id'] = $values['financial_account_id'];
297 $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults);
298 if ($result) {
299 $errorFlag = TRUE;
300 }
301 }
302 }
303
304 if ($errorFlag) {
305 $errorMsg['account_relationship'] = ts('This account relationship already exits');
306 }
307 }
308 return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg;
309 }
310
311 /**
312 * Function to process the form
313 *
314 * @access public
315 * @return void
316 */
317 public function postProcess() {
318 if ($this->_action & CRM_Core_Action::DELETE) {
319 CRM_Financial_BAO_FinancialTypeAccount::del($this->_id, $this->_aid);
320 CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.'));
321 }
322 else {
323 $params = $ids = array();
324 // store the submitted values in an array
325 $params = $this->exportValues();
326
327 if ($this->_action & CRM_Core_Action::UPDATE) {
328 $ids['entityFinancialAccount'] = $this->_id;
329 }
330 if ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) {
331 $params['financial_account_id'] = $this->_submitValues['financial_account_id'];
332 }
333 $params['entity_table'] = 'civicrm_financial_type';
334 if ($this->_action & CRM_Core_Action::ADD) {
335 $params['entity_id'] = $this->_aid;
336 }
337 $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids);
338 CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'));
339 }
340
341 $buttonName = $this->controller->getButtonName();
342 $session = CRM_Core_Session::singleton();
343
344 if ($buttonName == $this->getButtonName('next', 'new')) {
345 CRM_Core_Session::setStatus(ts(' You can add another Financial Account Type.'));
346 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
347 "reset=1&action=add&aid={$this->_aid}"));
348 }
349 else {
350 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
351 "reset=1&action=browse&aid={$this->_aid}"));
352 }
353 }
354 }
355
356