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