Commit | Line | Data |
---|---|---|
6a488035 TO |
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 | * Function to set variables up before form is built | |
68 | * | |
69 | * @return void | |
70 | * @access public | |
71 | */ | |
72 | public function preProcess() { | |
73 | $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this); | |
74 | $this->_id = CRM_Utils_Request::retrieve('id' , 'Positive', $this); | |
75 | if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { | |
76 | $this->_id = CRM_Utils_Type::escape($this->_id , 'Positive'); | |
77 | } | |
78 | $this->_BAOName = 'CRM_Financial_BAO_FinancialTypeAccount'; | |
79 | if ($this->_aid && ($this->_action & CRM_Core_Action::ADD)) { | |
80 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name'); | |
81 | CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Financial Accounts')); | |
82 | ||
83 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', | |
84 | "reset=1&action=browse&aid={$this->_aid}"); | |
85 | ||
86 | $session = CRM_Core_Session::singleton(); | |
87 | $session->pushUserContext($url); | |
88 | } | |
89 | if ($this->_id) { | |
90 | $financialAccount = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'financial_account_id'); | |
91 | $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name'); | |
92 | CRM_Utils_System::setTitle($fieldTitle .' - '.ts('Financial Type Accounts')); | |
93 | } | |
94 | ||
95 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$this->_aid}"); | |
96 | $breadCrumb = array( | |
97 | array('title' => ts('Financial Type Accounts'), | |
98 | 'url' => $url, | |
99 | ) | |
100 | ); | |
101 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Function to build the form | |
106 | * | |
107 | * @return None | |
108 | * @access public | |
109 | */ | |
110 | public function buildQuickForm() { | |
111 | if ($this->_action & CRM_Core_Action::DELETE) { | |
112 | $this->addButtons(array( | |
113 | array( | |
114 | 'type' => 'next', | |
115 | 'name' => ts('Delete Financial Account Type'), | |
116 | 'spacing' => ' ', | |
117 | 'isDefault' => TRUE | |
118 | ), | |
119 | array( | |
120 | 'type' => 'cancel', | |
121 | 'name' => ts('Cancel')) | |
122 | ) | |
123 | ); | |
124 | return; | |
125 | } | |
126 | ||
127 | parent::buildQuickForm(); | |
128 | ||
129 | if (isset($this->_id)) { | |
130 | $params = array('id' => $this->_id); | |
131 | CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); | |
132 | $this->setDefaults($defaults); | |
133 | $financialAccountTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name'); | |
134 | } | |
135 | ||
136 | $this->applyFilter('__ALL__', 'trim'); | |
137 | ||
138 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
139 | $this->assign('aid', $this->_id); | |
140 | //hidden field to catch the group id in profile | |
141 | $this->add('hidden', 'financial_type_id', $this->_aid); | |
142 | ||
143 | //hidden field to catch the field id in profile | |
144 | $this->add('hidden', 'account_type_id', $this->_id); | |
145 | } | |
146 | $AccountTypeRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship'); | |
147 | if (!empty($AccountTypeRelationship)) { | |
148 | $this->add('select', | |
149 | 'account_relationship', | |
150 | ts('Financial Account Relationship'), | |
151 | array('select' => '- select -') + $AccountTypeRelationship, | |
152 | TRUE | |
153 | ); | |
154 | } | |
155 | ||
156 | if ($this->_action == CRM_Core_Action::ADD) { | |
157 | if (CRM_Utils_Array::value('account_relationship', $this->_submitValues) || CRM_Utils_Array::value('financial_account_id', $this->_submitValues)) { | |
158 | $financialAccountType = array( | |
159 | '5' => 5, //expense | |
160 | '3' => 1, //AR relation | |
161 | '1' => 3, //revenue | |
162 | '6' => 1, //Asset | |
163 | '7' => 4, //cost of sales | |
164 | '8' => 1, //premium inventory | |
165 | '9' => 3 //discount account is | |
166 | ); | |
167 | ||
168 | $financialAccountType = $financialAccountType[$this->_submitValues['account_relationship']]; | |
169 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); | |
170 | ||
171 | $financialAccountSelect = array('' => ts('- select -')) + $result; | |
172 | } | |
173 | else { | |
174 | $financialAccountSelect = array( | |
175 | 'select' => ts('- select -') | |
176 | ) + CRM_Contribute_PseudoConstant::financialAccount(); | |
177 | } | |
178 | } | |
179 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
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 = $financialAccountType[$this->_defaultValues['account_relationship']]; | |
191 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); | |
192 | ||
193 | $financialAccountSelect = array('' => ts('- select -')) + $result; | |
194 | ||
195 | } | |
196 | $this->add('select', | |
197 | 'financial_account_id', | |
198 | ts('Financial Account'), | |
199 | $financialAccountSelect, | |
200 | TRUE | |
201 | ); | |
202 | ||
203 | $this->addButtons(array( | |
204 | array( | |
205 | 'type' => 'next', | |
206 | 'name' => ts('Save'), | |
207 | 'isDefault' => TRUE | |
208 | ), | |
209 | array( | |
210 | 'type' => 'next', | |
211 | 'name' => ts('Save and New'), | |
212 | 'subName' => 'new' | |
213 | ), | |
214 | array ( | |
215 | 'type' => 'cancel', | |
216 | 'name' => ts('Cancel') | |
217 | )) | |
218 | ); | |
219 | $this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this); | |
220 | } | |
221 | ||
222 | /** | |
223 | * global validation rules for the form | |
224 | * | |
225 | * @param array $fields posted values of the form | |
226 | * | |
227 | * @return array list of errors to be posted back to the form | |
228 | * @static | |
229 | * @access public | |
230 | */ | |
231 | static function formRule($values, $files, $self) { | |
232 | $errorMsg = array(); | |
233 | $errorFlag = FALSE; | |
234 | if ($self->_action == CRM_Core_Action::DELETE) { | |
235 | $groupName = 'account_relationship'; | |
236 | $relationValues = CRM_Core_PseudoConstant::accountOptionValues($groupName); | |
237 | if (CRM_Utils_Array::value('financial_account_id', $values) != 'select') { | |
238 | if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' || $relationValues[$values['account_relationship']] == 'Cost of Sales Account is') { | |
239 | $premiumsProduct = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id'); | |
240 | $product = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id'); | |
241 | if (!empty($premiumsProduct) || !empty($product)) { | |
242 | $errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.'; | |
243 | } | |
244 | } | |
245 | } | |
246 | } | |
247 | if (CRM_Utils_Array::value('account_relationship', $values) == 'select') { | |
248 | $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.'; | |
249 | } | |
250 | if (CRM_Utils_Array::value('financial_account_id', $values) == 'select') { | |
251 | $errorMsg['financial_account_id'] = 'Financial Account is a required field.'; | |
252 | } | |
253 | if (CRM_Utils_Array::value('account_relationship', $values) && CRM_Utils_Array::value('financial_account_id', $values)) { | |
254 | $params = array( | |
255 | 'account_relationship' => $values['account_relationship'], | |
256 | 'entity_id' => $self->_aid | |
257 | ); | |
258 | $defaults = array(); | |
259 | if ($self->_action == CRM_Core_Action::ADD) { | |
260 | $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); | |
261 | if ($result) { | |
262 | $errorFlag = TRUE; | |
263 | } | |
264 | } | |
265 | if ($self->_action == CRM_Core_Action::UPDATE) { | |
266 | if ($values['account_relationship'] == $self->_defaultValues['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues['financial_account_id']) { | |
267 | $errorFlag = FALSE; | |
268 | } | |
269 | else { | |
270 | $params['financial_account_id'] = $values['financial_account_id']; | |
271 | $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); | |
272 | if ($result) { | |
273 | $errorFlag = TRUE; | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | if ($errorFlag) { | |
279 | $errorMsg['account_relationship'] = ts('This account relationship already exits'); | |
280 | } | |
281 | } | |
282 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; | |
283 | } | |
284 | ||
285 | /** | |
286 | * Function to process the form | |
287 | * | |
288 | * @access public | |
289 | * @return None | |
290 | */ | |
291 | public function postProcess() { | |
292 | if ($this->_action & CRM_Core_Action::DELETE) { | |
293 | CRM_Financial_BAO_FinancialTypeAccount::del($this->_id, $this->_aid); | |
294 | CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.')); | |
295 | } | |
296 | else { | |
297 | $params = $ids = array(); | |
298 | // store the submitted values in an array | |
299 | $params = $this->exportValues(); | |
300 | ||
301 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
302 | $ids['entityFinancialAccount'] = $this->_id; | |
303 | } | |
304 | if ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) { | |
305 | $params['financial_account_id'] = $this->_submitValues['financial_account_id']; | |
306 | } | |
307 | $params['entity_table'] = 'civicrm_financial_type'; | |
308 | if ($this->_action & CRM_Core_Action::ADD) { | |
309 | $params['entity_id'] = $this->_aid; | |
310 | } | |
311 | $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids); | |
312 | CRM_Core_Session::setStatus(ts('The financial type Account has been saved.')); | |
313 | } | |
314 | ||
315 | $buttonName = $this->controller->getButtonName(); | |
316 | $session = CRM_Core_Session::singleton(); | |
317 | ||
318 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
319 | CRM_Core_Session::setStatus(ts(' You can add another Financial Account Type.')); | |
320 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', | |
321 | "reset=1&action=add&aid={$this->_aid}")); | |
322 | } | |
323 | else { | |
324 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', | |
325 | "reset=1&action=browse&aid={$this->_aid}")); | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 |