Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
[civicrm-core.git] / CRM / Financial / Form / FinancialAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Financial Account
38 *
39 */
40 class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
41
42 /**
43 * Flag if its a AR account type
44 *
45 * @var boolean
46 */
47 protected $_isARFlag = FALSE;
48
49
50 /**
51 * Function to set variables up before form is built
52 *
53 * @return void
54 * @access public
55 */
56 public function preProcess() {
57 parent::preProcess();
58
59 if ($this->_id) {
60 $params = array(
61 'id' => $this->_id,
62 );
63 $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, CRM_Core_DAO::$_nullArray);
64 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
65 if ($financialAccount->financial_account_type_id == array_search('Asset', $financialAccountType)
66 && strtolower($financialAccount->account_type_code) == 'ar'
67 && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, array_search('Asset', $financialAccountType))) {
68 $this->_isARFlag = TRUE;
69 if ($this->_action & CRM_Core_Action::DELETE) {
70 $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).");
71 if (CRM_Utils_Array::value('snippet', $_REQUEST) == 'json') {
72 CRM_Core_Page_AJAX::returnJsonResponse($msg);
73 }
74 CRM_Core_Session::setStatus($msg, '', 'error');
75 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount',
76 "reset=1&action=browse"));
77 }
78 }
79 }
80 }
81
82 /**
83 * Function to build the form
84 *
85 * @return void
86 * @access public
87 */
88 public function buildQuickForm( ) {
89 parent::buildQuickForm( );
90
91 if ($this->_action & CRM_Core_Action::DELETE) {
92 return;
93 }
94
95 $this->applyFilter('__ALL__', 'trim');
96 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount');
97 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
98 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
99 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id));
100
101 $this->add('text', 'description', ts('Description'), $attributes['description']);
102 $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
103 $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
104 $this->addEntityRef('contact_id', ts('Owner'), array('api' => array('params' => array('contact_type' => 'Organization')), 'create' => TRUE));
105 $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
106 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
107 $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?'));
108 $this->add('checkbox', 'is_tax', ts('Is Tax?'));
109
110 $element = $this->add('checkbox', 'is_default', ts('Default?'));
111 // CRM-12470 freeze is default if is_default is set
112 if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) {
113 $element->freeze();
114 }
115
116 $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
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 $values
138 * @param $files
139 * @param $self
140 *
141 * @internal param array $fields posted values of the form
142 *
143 * @return array list of errors to be posted back to the form
144 * @static
145 * @access public
146 */
147 static function formRule( $values, $files, $self ) {
148 $errorMsg = array( );
149 if (!empty( $values['tax_rate'])) {
150 if ($values['tax_rate'] <= 0 || $values['tax_rate'] > 100) {
151 $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
152 }
153 }
154 return CRM_Utils_Array::crmIsEmptyArray( $errorMsg ) ? true : $errorMsg;
155 }
156
157 /**
158 * This function sets the default values for the form.
159 * the default values are retrieved from the database
160 *
161 * @access public
162 *
163 * @return void
164 */
165 function setDefaultValues() {
166 $defaults = parent::setDefaultValues();
167 if ($this->_action & CRM_Core_Action::ADD) {
168 $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
169 }
170 return $defaults;
171 }
172
173 /**
174 * Function to process the form
175 *
176 * @access public
177 * @return void
178 */
179 public function postProcess() {
180 if ($this->_action & CRM_Core_Action::DELETE) {
181 CRM_Financial_BAO_FinancialAccount::del($this->_id);
182 CRM_Core_Session::setStatus( ts('Selected Financial Account has been deleted.') );
183 }
184 else {
185 $ids = array( );
186 // store the submitted values in an array
187 $params = $this->exportValues();
188
189 if ($this->_action & CRM_Core_Action::UPDATE) {
190 $ids['contributionType'] = $this->_id;
191 }
192
193 $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
194 CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name)));
195 }
196 }
197 }