--worked on CRM-12470
[civicrm-core.git] / CRM / Financial / Form / FinancialAccount.php
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 Account
39 *
40 */
41 class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
42
43 /**
44 * Flag if its a AR account type
45 *
46 * @var boolean
47 */
48 protected $_isARFlag = FALSE;
49
50
51 /**
52 * Function to set variables up before form is built
53 *
54 * @return void
55 * @access public
56 */
57 public function preProcess() {
58 parent::preProcess();
59
60 if ($this->_id) {
61 $params = array(
62 'id' => $this->_id,
63 );
64 $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, CRM_Core_DAO::$_nullArray);
65 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
66 if ($financialAccount->financial_account_type_id == array_search('Asset', $financialAccountType)
67 && strtolower($financialAccount->account_type_code) == 'ar'
68 && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, array_search('Asset', $financialAccountType))) {
69 $this->_isARFlag = TRUE;
70 if ($this->_action & CRM_Core_Action::DELETE) {
71 CRM_Core_Session::setStatus(ts("Financial account with 'AR' account cannot be deleted."),
72 '', 'error');
73 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount',
74 "reset=1&action=browse"));
75 }
76 }
77 }
78 }
79
80 /**
81 * Function to build the form
82 *
83 * @return None
84 * @access public
85 */
86 public function buildQuickForm( ) {
87 parent::buildQuickForm( );
88 $dataURL = CRM_Utils_System::url('civicrm/ajax/rest',
89 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&org=1', FALSE, NULL, FALSE);
90 $this->assign('dataURL', $dataURL);
91
92 if ($this->_action & CRM_Core_Action::DELETE) {
93 return;
94 }
95
96 $this->applyFilter('__ALL__', 'trim');
97 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount');
98 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
99 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
100 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id));
101
102 $this->add('text', 'description', ts('Description'), $attributes['description']);
103 $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
104 $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
105 $this->add('text', 'contact_name', ts('Owner'), $attributes['name']);
106 $this->add('hidden', 'contact_id', '', array('id' => 'contact_id'));
107 $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
108 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
109 $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
110 $this->add('checkbox', 'is_tax', ts('Is Tax?'));
111 $element = $this->add('checkbox', 'is_default', ts('Default?'));
112 // CRM-12470 freeze is default if is_default is set
113 if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) {
114 $element->freeze();
115 }
116 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
117 if (!empty($financialAccountType)) {
118 $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
119 array('' => '- select -') + $financialAccountType, TRUE);
120 if ($this->_isARFlag) {
121 $element->freeze();
122 $elementAccounting->freeze();
123 $elementActive->freeze();
124 }
125 }
126
127 if ($this->_action == CRM_Core_Action::UPDATE &&
128 CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')) {
129 $this->freeze(array('name', 'description', 'is_active'));
130 }
131 $this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this);
132 }
133
134 /**
135 * global validation rules for the form
136 *
137 * @param array $fields posted values of the form
138 *
139 * @return array list of errors to be posted back to the form
140 * @static
141 * @access public
142 */
143 static function formRule( $values, $files, $self ) {
144 $errorMsg = array( );
145 if (!empty( $values['tax_rate'])) {
146 if ($values['tax_rate'] <= 0 || $values['tax_rate'] > 100) {
147 $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
148 }
149 }
150 return CRM_Utils_Array::crmIsEmptyArray( $errorMsg ) ? true : $errorMsg;
151 }
152
153 /**
154 * This function sets the default values for the form.
155 * the default values are retrieved from the database
156 *
157 * @access public
158 *
159 * @return None
160 */
161 function setDefaultValues() {
162 $defaults = parent::setDefaultValues();
163 if ($this->_action & CRM_Core_Action::ADD) {
164 $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
165 $defaults['contact_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $defaults['contact_id'], 'sort_name');
166 }
167 return $defaults;
168 }
169
170 /**
171 * Function to process the form
172 *
173 * @access public
174 * @return None
175 */
176 public function postProcess() {
177 if ($this->_action & CRM_Core_Action::DELETE) {
178 CRM_Financial_BAO_FinancialAccount::del($this->_id);
179 CRM_Core_Session::setStatus( ts('Selected Financial Account has been deleted.') );
180 }
181 else {
182 $ids = array( );
183 // store the submitted values in an array
184 $params = $this->exportValues();
185
186 if ($this->_action & CRM_Core_Action::UPDATE) {
187 $ids['contributionType'] = $this->_id;
188 }
189
190 $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
191 CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name)));
192 }
193 }
194 }
195
196