3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
35 * This class generates form components for Financial Type Account
37 class CRM_Financial_Form_FinancialTypeAccount
extends CRM_Contribute_Form
{
40 * The financial type id saved to the session for an update.
47 * The financial type accounts id, used when editing the field
54 * The name of the BAO object for this form.
61 * Flag if its a AR account type.
65 protected $_isARFlag = FALSE;
68 * Set variables up before form is built.
70 public function preProcess() {
71 $this->_aid
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this);
72 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
74 if (!$this->_id
&& ($this->_action
& CRM_Core_Action
::UPDATE
)) {
75 $this->_id
= CRM_Utils_Type
::escape($this->_id
, 'Positive');
77 $url = CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
78 "reset=1&action=browse&aid={$this->_aid}");
80 $this->_BAOName
= 'CRM_Financial_BAO_FinancialTypeAccount';
81 if ($this->_aid
&& ($this->_action
& CRM_Core_Action
::ADD
)) {
82 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid
, 'name');
83 CRM_Utils_System
::setTitle($this->_title
. ' - ' . ts('Financial Accounts'));
85 $session = CRM_Core_Session
::singleton();
86 $session->pushUserContext($url);
89 if (!($this->_action
& CRM_Core_Action
::ADD
)) {
90 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
91 $accountRelationship = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id
, 'account_relationship');
92 if ($accountRelationship == $relationTypeId) {
93 $this->_isARFlag
= TRUE;
94 if ($this->_action
& CRM_Core_Action
::DELETE
) {
95 CRM_Core_Session
::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."),
97 CRM_Utils_System
::redirect($url);
102 $financialAccount = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id
, 'financial_account_id');
103 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name');
104 CRM_Utils_System
::setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts'));
109 'title' => ts('Financial Type Accounts'),
113 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
117 * Build the form object.
119 public function buildQuickForm() {
120 parent
::buildQuickForm();
121 $this->setPageTitle(ts('Financial Type Account'));
123 if ($this->_action
& CRM_Core_Action
::DELETE
) {
127 if (isset($this->_id
)) {
128 $params = array('id' => $this->_id
);
129 CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
130 $this->setDefaults($defaults);
131 $financialAccountTitle = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name');
134 $this->applyFilter('__ALL__', 'trim');
136 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
137 $this->assign('aid', $this->_id
);
138 // hidden field to catch the group id in profile
139 $this->add('hidden', 'financial_type_id', $this->_aid
);
141 // hidden field to catch the field id in profile
142 $this->add('hidden', 'account_type_id', $this->_id
);
144 $params['orderColumn'] = 'label';
145 $AccountTypeRelationship = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
146 if (!empty($AccountTypeRelationship)) {
147 $element = $this->add('select',
148 'account_relationship',
149 ts('Financial Account Relationship'),
150 array('select' => ts('- Select Financial Account Relationship -')) +
$AccountTypeRelationship,
155 if ($this->_isARFlag
) {
159 if ($this->_action
== CRM_Core_Action
::ADD
) {
160 if (!empty($this->_submitValues
['account_relationship']) ||
!empty($this->_submitValues
['financial_account_id'])) {
161 $financialAccountType = CRM_Financial_BAO_FinancialAccount
::getfinancialAccountRelations();
162 $financialAccountType = CRM_Utils_Array
::value($this->_submitValues
['account_relationship'], $financialAccountType);
163 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
165 $financialAccountSelect = array('' => ts('- select -')) +
$result;
168 $financialAccountSelect = array(
169 'select' => ts('- select -'),
170 ) + CRM_Contribute_PseudoConstant
::financialAccount();
173 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
174 $financialAccountType = CRM_Financial_BAO_FinancialAccount
::getfinancialAccountRelations();
175 $financialAccountType = $financialAccountType[$this->_defaultValues
['account_relationship']];
176 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
178 $financialAccountSelect = array('' => ts('- select -')) +
$result;
182 'financial_account_id',
183 ts('Financial Account'),
184 $financialAccountSelect,
188 $this->addButtons(array(
191 'name' => ts('Save'),
196 'name' => ts('Save and New'),
201 'name' => ts('Cancel'),
205 $this->addFormRule(array('CRM_Financial_Form_FinancialTypeAccount', 'formRule'), $this);
209 * Global validation rules for the form.
211 * @param array $values
212 * posted values of the form
217 * list of errors to be posted back to the form
219 public static function formRule($values, $files, $self) {
222 if ($self->_action
== CRM_Core_Action
::DELETE
) {
223 $relationValues = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
224 if (CRM_Utils_Array
::value('financial_account_id', $values) != 'select') {
225 if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' ||
$relationValues[$values['account_relationship']] == 'Cost of Sales Account is') {
226 $premiumsProduct = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id');
227 $product = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id');
228 if (!empty($premiumsProduct) ||
!empty($product)) {
229 $errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.';
234 if (CRM_Utils_Array
::value('account_relationship', $values) == 'select') {
235 $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.';
237 if (CRM_Utils_Array
::value('financial_account_id', $values) == 'select') {
238 $errorMsg['financial_account_id'] = 'Financial Account is a required field.';
240 if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
242 'account_relationship' => $values['account_relationship'],
243 'entity_id' => $self->_aid
,
244 'entity_table' => 'civicrm_financial_type',
247 if ($self->_action
== CRM_Core_Action
::ADD
) {
248 $relationshipId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
249 $isTax = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $values['financial_account_id'], 'is_tax');
250 if ($values['account_relationship'] == $relationshipId) {
252 $errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account');
255 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
260 if ($self->_action
== CRM_Core_Action
::UPDATE
) {
261 if ($values['account_relationship'] == $self->_defaultValues
['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues
['financial_account_id']) {
265 $params['financial_account_id'] = $values['financial_account_id'];
266 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
274 $errorMsg['account_relationship'] = ts('This account relationship already exits');
277 return CRM_Utils_Array
::crmIsEmptyArray($errorMsg) ?
TRUE : $errorMsg;
281 * Process the form submission.
283 public function postProcess() {
284 if ($this->_action
& CRM_Core_Action
::DELETE
) {
285 CRM_Financial_BAO_FinancialTypeAccount
::del($this->_id
, $this->_aid
);
286 CRM_Core_Session
::setStatus(ts('Selected financial type account has been deleted.'));
289 $params = $ids = array();
290 // store the submitted values in an array
291 $params = $this->exportValues();
293 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
294 $ids['entityFinancialAccount'] = $this->_id
;
296 if ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
) {
297 $params['financial_account_id'] = $this->_submitValues
['financial_account_id'];
299 $params['entity_table'] = 'civicrm_financial_type';
300 if ($this->_action
& CRM_Core_Action
::ADD
) {
301 $params['entity_id'] = $this->_aid
;
304 $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount
::add($params, $ids);
305 CRM_Core_Session
::setStatus(ts('The financial type Account has been saved.'), ts('Saved'), 'success');
307 catch (CRM_Core_Exception
$e) {
308 CRM_Core_Error
::statusBounce($e->getMessage());
312 $buttonName = $this->controller
->getButtonName();
313 $session = CRM_Core_Session
::singleton();
315 if ($buttonName == $this->getButtonName('next', 'new')) {
316 CRM_Core_Session
::setStatus(ts(' You can add another Financial Account Type.'));
317 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
318 "reset=1&action=add&aid={$this->_aid}"));
321 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
322 "reset=1&action=browse&aid={$this->_aid}"));