3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Financial Type Account
40 class CRM_Financial_Form_FinancialTypeAccount
extends CRM_Contribute_Form
{
43 * The financial type id saved to the session for an update
50 * The financial type accounts id, used when editing the field
57 * The name of the BAO object for this form
64 * Flag if its a AR account type
68 protected $_isARFlag = FALSE;
71 * Set variables up before form is built
75 public function preProcess() {
76 $this->_aid
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this);
77 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
79 if (!$this->_id
&& ($this->_action
& CRM_Core_Action
::UPDATE
)) {
80 $this->_id
= CRM_Utils_Type
::escape($this->_id
, 'Positive');
82 $url = CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
83 "reset=1&action=browse&aid={$this->_aid}");
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'));
90 $session = CRM_Core_Session
::singleton();
91 $session->pushUserContext($url);
94 if (!($this->_action
& CRM_Core_Action
::ADD
)) {
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
) {
100 CRM_Core_Session
::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."),
102 CRM_Utils_System
::redirect($url);
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');
109 CRM_Utils_System
::setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts'));
114 'title' => ts('Financial Type Accounts'),
118 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
122 * Build the form object
126 public function buildQuickForm() {
127 parent
::buildQuickForm();
128 $this->setPageTitle(ts('Financial Type Account'));
130 if ($this->_action
& CRM_Core_Action
::DELETE
) {
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');
141 $this->applyFilter('__ALL__', 'trim');
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
);
148 //hidden field to catch the field id in profile
149 $this->add('hidden', 'account_type_id', $this->_id
);
151 $AccountTypeRelationship = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
152 if (!empty($AccountTypeRelationship)) {
153 $element = $this->add('select',
154 'account_relationship',
155 ts('Financial Account Relationship'),
156 array('select' => '- select -') +
$AccountTypeRelationship,
161 if ($this->_isARFlag
) {
165 if ($this->_action
== CRM_Core_Action
::ADD
) {
166 if (!empty($this->_submitValues
['account_relationship']) ||
!empty($this->_submitValues
['financial_account_id'])) {
167 $financialAccountType = array(
169 '3' => 1, //AR relation
172 '7' => 4, //cost of sales
173 '8' => 1, //premium inventory
174 '9' => 3, //discount account is,
177 $financialAccountType = CRM_Utils_Array
::value($this->_submitValues
['account_relationship'], $financialAccountType);
178 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
180 $financialAccountSelect = array('' => ts('- select -')) +
$result;
183 $financialAccountSelect = array(
184 'select' => ts('- select -'),
185 ) + CRM_Contribute_PseudoConstant
::financialAccount();
188 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
189 $financialAccountType = array(
191 '3' => 1, //AR relation
194 '7' => 4, //cost of sales
195 '8' => 1, //premium inventory
196 '9' => 3, //discount account is,
199 $financialAccountType = $financialAccountType[$this->_defaultValues
['account_relationship']];
200 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
202 $financialAccountSelect = array('' => ts('- select -')) +
$result;
206 'financial_account_id',
207 ts('Financial Account'),
208 $financialAccountSelect,
212 $this->addButtons(array(
215 'name' => ts('Save'),
220 'name' => ts('Save and New'),
225 'name' => ts('Cancel'),
229 $this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this);
233 * Global validation rules for the form
235 * @param array $values
236 * posted values of the form
241 * list of errors to be posted back to the form
243 public static function formRule($values, $files, $self) {
246 if ($self->_action
== CRM_Core_Action
::DELETE
) {
247 $relationValues = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
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.';
258 if (CRM_Utils_Array
::value('account_relationship', $values) == 'select') {
259 $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.';
261 if (CRM_Utils_Array
::value('financial_account_id', $values) == 'select') {
262 $errorMsg['financial_account_id'] = 'Financial Account is a required field.';
264 if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
266 'account_relationship' => $values['account_relationship'],
267 'entity_id' => $self->_aid
,
270 if ($self->_action
== CRM_Core_Action
::ADD
) {
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) {
275 $errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account');
278 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
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']) {
288 $params['financial_account_id'] = $values['financial_account_id'];
289 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
297 $errorMsg['account_relationship'] = ts('This account relationship already exits');
300 return CRM_Utils_Array
::crmIsEmptyArray($errorMsg) ?
TRUE : $errorMsg;
304 * Process the form submission
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.'));
314 $params = $ids = array();
315 // store the submitted values in an array
316 $params = $this->exportValues();
318 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
319 $ids['entityFinancialAccount'] = $this->_id
;
321 if ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
) {
322 $params['financial_account_id'] = $this->_submitValues
['financial_account_id'];
324 $params['entity_table'] = 'civicrm_financial_type';
325 if ($this->_action
& CRM_Core_Action
::ADD
) {
326 $params['entity_id'] = $this->_aid
;
328 $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount
::add($params, $ids);
329 CRM_Core_Session
::setStatus(ts('The financial type Account has been saved.'));
332 $buttonName = $this->controller
->getButtonName();
333 $session = CRM_Core_Session
::singleton();
335 if ($buttonName == $this->getButtonName('next', 'new')) {
336 CRM_Core_Session
::setStatus(ts(' You can add another Financial Account Type.'));
337 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
338 "reset=1&action=add&aid={$this->_aid}"));
341 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
342 "reset=1&action=browse&aid={$this->_aid}"));