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 Account
40 class CRM_Financial_Form_FinancialAccount
extends CRM_Contribute_Form
{
43 * Flag if its a AR account type
47 protected $_isARFlag = FALSE;
51 * Set variables up before form is built
55 public function preProcess() {
62 $financialAccount = CRM_Financial_BAO_FinancialAccount
::retrieve($params, CRM_Core_DAO
::$_nullArray);
63 $financialAccountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type');
64 if ($financialAccount->financial_account_type_id
== array_search('Asset', $financialAccountType)
65 && strtolower($financialAccount->account_type_code
) == 'ar'
66 && !CRM_Financial_BAO_FinancialAccount
::getARAccounts($this->_id
, array_search('Asset', $financialAccountType))) {
67 $this->_isARFlag
= TRUE;
68 if ($this->_action
& CRM_Core_Action
::DELETE
) {
69 $msg = ts("The selected financial account cannot be deleted because at least one Accounts Receivable type account is required (to ensure that accounting transactions are in balance).");
70 CRM_Core_Error
::statusBounce($msg);
77 * Build the form object
81 public function buildQuickForm() {
82 parent
::buildQuickForm();
83 $this->setPageTitle(ts('Financial Account'));
85 if ($this->_action
& CRM_Core_Action
::DELETE
) {
89 $this->applyFilter('__ALL__', 'trim');
90 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_FinancialAccount');
91 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
92 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
93 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id
));
95 $this->add('text', 'description', ts('Description'), $attributes['description']);
96 $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
97 $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
98 $this->addEntityRef('contact_id', ts('Owner'), array('api' => array('params' => array('contact_type' => 'Organization')), 'create' => TRUE));
99 $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
100 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
101 $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
102 $this->add('checkbox', 'is_tax', ts('Is Tax?'));
104 $element = $this->add('checkbox', 'is_default', ts('Default?'));
105 // CRM-12470 freeze is default if is_default is set
106 if ($this->_id
&& CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id
, 'is_default')) {
110 $financialAccountType = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
111 if (!empty($financialAccountType)) {
112 $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
113 array('' => '- select -') +
$financialAccountType, TRUE, array('class' => 'crm-select2 huge'));
114 if ($this->_isARFlag
) {
116 $elementAccounting->freeze();
117 $elementActive->freeze();
121 if ($this->_action
== CRM_Core_Action
::UPDATE
&&
122 CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id
, 'is_reserved')) {
123 $this->freeze(array('name', 'description', 'is_active'));
125 $this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this);
129 * Global validation rules for the form
135 * @internal param array $fields posted values of the form
137 * @return array list of errors to be posted back to the form
140 public static function formRule($values, $files, $self) {
142 $financialAccountTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' "));
143 if (isset($values['is_tax'])) {
144 if ($values['financial_account_type_id'] != $financialAccountTypeId) {
145 $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.');
147 if (CRM_Utils_Array
::value('tax_rate', $values) == NULL) {
148 $errorMsg['tax_rate'] = ts('Please enter value for tax rate');
151 if ((CRM_Utils_Array
::value('tax_rate', $values) != NULL)) {
152 if ($values['tax_rate'] < 0 ||
$values['tax_rate'] >= 100) {
153 $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
156 if ($self->_action
& CRM_Core_Action
::UPDATE
) {
157 if (!(isset($values['is_tax']))) {
158 $relationshipId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
160 'financial_account_id' => $self->_id
,
161 'account_relationship' => $relationshipId
163 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
165 $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account');
169 return CRM_Utils_Array
::crmIsEmptyArray($errorMsg) ?
TRUE : $errorMsg;
173 * Set default values for the form.
174 * the default values are retrieved from the database
179 public function setDefaultValues() {
180 $defaults = parent
::setDefaultValues();
181 if ($this->_action
& CRM_Core_Action
::ADD
) {
182 $defaults['contact_id'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config
::domainID(), 'contact_id');
188 * Process the form submission
192 public function postProcess() {
193 if ($this->_action
& CRM_Core_Action
::DELETE
) {
194 CRM_Financial_BAO_FinancialAccount
::del($this->_id
);
195 CRM_Core_Session
::setStatus(ts('Selected Financial Account has been deleted.'));
199 // store the submitted values in an array
200 $params = $this->exportValues();
202 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
203 $ids['contributionType'] = $this->_id
;
206 $contributionType = CRM_Financial_BAO_FinancialAccount
::add($params, $ids);
207 CRM_Core_Session
::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name
)));