INFRA-132 - Spaces around "."
[civicrm-core.git] / CRM / Financial / Form / FinancialTypeAccount.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 42 /**
100fef9d 43 * The financial type id saved to the session for an update
6a488035
TO
44 *
45 * @var int
6a488035
TO
46 */
47 protected $_aid;
03e04002 48
6a488035
TO
49 /**
50 * The financial type accounts id, used when editing the field
51 *
52 * @var int
6a488035
TO
53 */
54 protected $_id;
03e04002 55
6a488035
TO
56 /**
57 * The name of the BAO object for this form
58 *
59 * @var string
60 */
61 protected $_BAOName;
03e04002 62
7d289724
PN
63 /**
64 * Flag if its a AR account type
65 *
66 * @var boolean
67 */
68 protected $_isARFlag = FALSE;
8ef12e64 69
6a488035 70 /**
100fef9d 71 * Set variables up before form is built
6a488035
TO
72 *
73 * @return void
6a488035
TO
74 */
75 public function preProcess() {
76 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this);
d0f466d1
KJ
77 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
78
6a488035 79 if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) {
7d289724 80 $this->_id = CRM_Utils_Type::escape($this->_id, 'Positive');
6a488035 81 }
8ef12e64 82 $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
83 "reset=1&action=browse&aid={$this->_aid}");
84
6a488035
TO
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'));
03e04002 89
8ef12e64 90 $session = CRM_Core_Session::singleton();
6a488035 91 $session->pushUserContext($url);
8ef12e64 92 }
7d289724 93 // CRM-12492
8ef12e64 94 if (!($this->_action & CRM_Core_Action::ADD)) {
7d289724
PN
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) {
8ef12e64 100 CRM_Core_Session::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."),
7d289724
PN
101 '', 'error');
102 CRM_Utils_System::redirect($url);
103 }
104 }
03e04002 105 }
6a488035
TO
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');
92fcb95f 109 CRM_Utils_System::setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts'));
6a488035 110 }
03e04002 111
6a488035 112 $breadCrumb = array(
045f52a3
TO
113 array(
114 'title' => ts('Financial Type Accounts'),
6a488035
TO
115 'url' => $url,
116 )
117 );
118 CRM_Utils_System::appendBreadCrumb($breadCrumb);
119 }
03e04002 120
6a488035 121 /**
c490a46a 122 * Build the form object
6a488035 123 *
355ba699 124 * @return void
6a488035
TO
125 */
126 public function buildQuickForm() {
e2046b33 127 parent::buildQuickForm();
468dd6c0 128 $this->setPageTitle(ts('Financial Type Account'));
e2046b33 129
6a488035 130 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
131 return;
132 }
03e04002 133
6a488035
TO
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 }
03e04002 140
6a488035 141 $this->applyFilter('__ALL__', 'trim');
03e04002 142
6a488035
TO
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);
03e04002 147
6a488035
TO
148 //hidden field to catch the field id in profile
149 $this->add('hidden', 'account_type_id', $this->_id);
150 }
7611ae71 151 $AccountTypeRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
6a488035 152 if (!empty($AccountTypeRelationship)) {
d0f466d1 153 $element = $this->add('select',
03e04002 154 'account_relationship',
6a488035
TO
155 ts('Financial Account Relationship'),
156 array('select' => '- select -') + $AccountTypeRelationship,
03e04002 157 TRUE
6a488035
TO
158 );
159 }
03e04002 160
7d289724
PN
161 if ($this->_isARFlag) {
162 $element->freeze();
163 }
d0f466d1 164
6a488035 165 if ($this->_action == CRM_Core_Action::ADD) {
8cc574cf 166 if (!empty($this->_submitValues['account_relationship']) || !empty($this->_submitValues['financial_account_id'])) {
03e04002 167 $financialAccountType = array(
6a488035
TO
168 '5' => 5, //expense
169 '3' => 1, //AR relation
170 '1' => 3, //revenue
045f52a3 171 '6' => 1, //Asset
6a488035
TO
172 '7' => 4, //cost of sales
173 '8' => 1, //premium inventory
174 '9' => 3 //discount account is
175 );
03e04002 176
b800df93 177 $financialAccountType = CRM_Utils_Array::value($this->_submitValues['account_relationship'], $financialAccountType);
6a488035 178 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
03e04002 179
6a488035
TO
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) {
03e04002 189 $financialAccountType = array(
6a488035
TO
190 '5' => 5, //expense
191 '3' => 1, //AR relation
192 '1' => 3, //revenue
045f52a3 193 '6' => 1, //Asset
6a488035
TO
194 '7' => 4, //cost of sales
195 '8' => 1, //premium inventory
196 '9' => 3 //discount account is
197 );
03e04002 198
6a488035
TO
199 $financialAccountType = $financialAccountType[$this->_defaultValues['account_relationship']];
200 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
03e04002 201
6a488035 202 $financialAccountSelect = array('' => ts('- select -')) + $result;
6a488035 203 }
dae00956 204
03e04002 205 $this->add('select',
206 'financial_account_id',
207 ts('Financial Account'),
6a488035
TO
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 ),
045f52a3 223 array(
6a488035
TO
224 'type' => 'cancel',
225 'name' => ts('Cancel')
226 ))
227 );
228 $this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this);
229 }
03e04002 230
6a488035 231 /**
100fef9d 232 * Global validation rules for the form
6a488035 233 *
fd31fa4c
EM
234 * @param $values
235 * @param $files
236 * @param $self
237 *
238 * @internal param array $fields posted values of the form
6a488035
TO
239 *
240 * @return array list of errors to be posted back to the form
241 * @static
6a488035 242 */
00be9182 243 public static function formRule($values, $files, $self) {
6a488035
TO
244 $errorMsg = array();
245 $errorFlag = FALSE;
246 if ($self->_action == CRM_Core_Action::DELETE) {
7611ae71 247 $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
6a488035
TO
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 }
8cc574cf 264 if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
03e04002 265 $params = array(
6a488035
TO
266 'account_relationship' => $values['account_relationship'],
267 'entity_id' => $self->_aid
268 );
269 $defaults = array();
270 if ($self->_action == CRM_Core_Action::ADD) {
707f6952
RK
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 }
6a488035
TO
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 }
03e04002 294 }
295
6a488035
TO
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 }
03e04002 302
6a488035 303 /**
c490a46a 304 * Process the form submission
6a488035 305 *
355ba699 306 * @return void
6a488035
TO
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.'));
03e04002 312 }
313 else {
6a488035
TO
314 $params = $ids = array();
315 // store the submitted values in an array
316 $params = $this->exportValues();
03e04002 317
6a488035
TO
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) {
03e04002 322 $params['financial_account_id'] = $this->_submitValues['financial_account_id'];
6a488035
TO
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.'));
03e04002 337 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
6a488035 338 "reset=1&action=add&aid={$this->_aid}"));
03e04002 339 }
6a488035 340 else {
03e04002 341 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
6a488035 342 "reset=1&action=browse&aid={$this->_aid}"));
03e04002 343 }
6a488035 344 }
e2046b33 345
6a488035 346}