Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Financial Account | |
03e04002 | 38 | * |
6a488035 TO |
39 | */ |
40 | class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { | |
41 | ||
7d289724 | 42 | /** |
fe482240 | 43 | * Flag if its a AR account type. |
7d289724 PN |
44 | * |
45 | * @var boolean | |
46 | */ | |
47 | protected $_isARFlag = FALSE; | |
8ef12e64 | 48 | |
7d289724 PN |
49 | |
50 | /** | |
fe482240 | 51 | * Set variables up before form is built. |
7d289724 PN |
52 | * |
53 | * @return void | |
7d289724 PN |
54 | */ |
55 | public function preProcess() { | |
56 | parent::preProcess(); | |
8ef12e64 | 57 | |
7d289724 PN |
58 | if ($this->_id) { |
59 | $params = array( | |
60 | 'id' => $this->_id, | |
61 | ); | |
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) | |
8ef12e64 | 65 | && strtolower($financialAccount->account_type_code) == 'ar' |
353ffa53 TO |
66 | && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, array_search('Asset', $financialAccountType)) |
67 | ) { | |
7d289724 PN |
68 | $this->_isARFlag = TRUE; |
69 | if ($this->_action & CRM_Core_Action::DELETE) { | |
134bc82c | 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)."); |
e2046b33 | 71 | CRM_Core_Error::statusBounce($msg); |
7d289724 PN |
72 | } |
73 | } | |
74 | } | |
75 | } | |
76 | ||
6a488035 | 77 | /** |
fe482240 | 78 | * Build the form object. |
6a488035 | 79 | * |
355ba699 | 80 | * @return void |
6a488035 | 81 | */ |
045f52a3 | 82 | public function buildQuickForm() { |
481a74f4 | 83 | parent::buildQuickForm(); |
e2046b33 | 84 | $this->setPageTitle(ts('Financial Account')); |
6a488035 TO |
85 | |
86 | if ($this->_action & CRM_Core_Action::DELETE) { | |
87 | return; | |
88 | } | |
03e04002 | 89 | |
6a488035 TO |
90 | $this->applyFilter('__ALL__', 'trim'); |
91 | $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount'); | |
7d289724 | 92 | $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE); |
6a488035 TO |
93 | $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), |
94 | 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id)); | |
03e04002 | 95 | |
6a488035 TO |
96 | $this->add('text', 'description', ts('Description'), $attributes['description']); |
97 | $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']); | |
7d289724 | 98 | $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']); |
353ffa53 | 99 | $this->addEntityRef('contact_id', ts('Owner'), array( |
ae5ffbb7 TO |
100 | 'api' => array('params' => array('contact_type' => 'Organization')), |
101 | 'create' => TRUE, | |
102 | )); | |
6a488035 TO |
103 | $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']); |
104 | $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?')); | |
ddaa8ef1 | 105 | $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?')); |
6a488035 | 106 | $this->add('checkbox', 'is_tax', ts('Is Tax?')); |
d0f466d1 | 107 | |
ddaa8ef1 PN |
108 | $element = $this->add('checkbox', 'is_default', ts('Default?')); |
109 | // CRM-12470 freeze is default if is_default is set | |
110 | if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) { | |
111 | $element->freeze(); | |
112 | } | |
03e04002 | 113 | |
7611ae71 | 114 | $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id'); |
6a488035 | 115 | if (!empty($financialAccountType)) { |
7d289724 | 116 | $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), |
ce22fda6 | 117 | array('' => '- select -') + $financialAccountType, TRUE, array('class' => 'crm-select2 huge')); |
7d289724 PN |
118 | if ($this->_isARFlag) { |
119 | $element->freeze(); | |
120 | $elementAccounting->freeze(); | |
ddaa8ef1 | 121 | $elementActive->freeze(); |
7d289724 | 122 | } |
6a488035 | 123 | } |
03e04002 | 124 | |
6a488035 | 125 | if ($this->_action == CRM_Core_Action::UPDATE && |
353ffa53 TO |
126 | CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved') |
127 | ) { | |
6a488035 TO |
128 | $this->freeze(array('name', 'description', 'is_active')); |
129 | } | |
130 | $this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this); | |
131 | } | |
03e04002 | 132 | |
6a488035 | 133 | /** |
fe482240 | 134 | * Global validation rules for the form. |
6a488035 | 135 | * |
16b10e64 CW |
136 | * @param array $values |
137 | * posted values of the form | |
da6b46f4 EM |
138 | * @param $files |
139 | * @param $self | |
140 | * | |
a6c01b45 CW |
141 | * @return array |
142 | * list of errors to be posted back to the form | |
6a488035 | 143 | */ |
045f52a3 TO |
144 | public static function formRule($values, $files, $self) { |
145 | $errorMsg = array(); | |
707f6952 RK |
146 | $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")); |
147 | if (isset($values['is_tax'])) { | |
045f52a3 | 148 | if ($values['financial_account_type_id'] != $financialAccountTypeId) { |
707f6952 RK |
149 | $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.'); |
150 | } | |
151 | if (CRM_Utils_Array::value('tax_rate', $values) == NULL) { | |
152 | $errorMsg['tax_rate'] = ts('Please enter value for tax rate'); | |
153 | } | |
154 | } | |
33421d01 | 155 | if ((CRM_Utils_Array::value('tax_rate', $values) != NULL)) { |
707f6952 | 156 | if ($values['tax_rate'] < 0 || $values['tax_rate'] >= 100) { |
6a488035 TO |
157 | $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100'); |
158 | } | |
159 | } | |
707f6952 RK |
160 | if ($self->_action & CRM_Core_Action::UPDATE) { |
161 | if (!(isset($values['is_tax']))) { | |
162 | $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); | |
163 | $params = array( | |
353ffa53 | 164 | 'financial_account_id' => $self->_id, |
21dfd5f5 | 165 | 'account_relationship' => $relationshipId, |
707f6952 | 166 | ); |
d75f2f47 | 167 | $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); |
707f6952 RK |
168 | if ($result) { |
169 | $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account'); | |
170 | } | |
171 | } | |
172 | } | |
481a74f4 | 173 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; |
6a488035 | 174 | } |
03e04002 | 175 | |
6a488035 | 176 | /** |
c490a46a | 177 | * Set default values for the form. |
6a488035 TO |
178 | * the default values are retrieved from the database |
179 | * | |
6a488035 | 180 | * |
355ba699 | 181 | * @return void |
6a488035 | 182 | */ |
00be9182 | 183 | public function setDefaultValues() { |
6a488035 TO |
184 | $defaults = parent::setDefaultValues(); |
185 | if ($this->_action & CRM_Core_Action::ADD) { | |
186 | $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'); | |
6a488035 TO |
187 | } |
188 | return $defaults; | |
189 | } | |
03e04002 | 190 | |
6a488035 | 191 | /** |
fe482240 | 192 | * Process the form submission. |
6a488035 | 193 | * |
355ba699 | 194 | * @return void |
6a488035 TO |
195 | */ |
196 | public function postProcess() { | |
197 | if ($this->_action & CRM_Core_Action::DELETE) { | |
198 | CRM_Financial_BAO_FinancialAccount::del($this->_id); | |
481a74f4 | 199 | CRM_Core_Session::setStatus(ts('Selected Financial Account has been deleted.')); |
6a488035 TO |
200 | } |
201 | else { | |
045f52a3 | 202 | $ids = array(); |
6a488035 TO |
203 | // store the submitted values in an array |
204 | $params = $this->exportValues(); | |
03e04002 | 205 | |
6a488035 TO |
206 | if ($this->_action & CRM_Core_Action::UPDATE) { |
207 | $ids['contributionType'] = $this->_id; | |
208 | } | |
03e04002 | 209 | |
6a488035 TO |
210 | $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids); |
211 | CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name))); | |
212 | } | |
213 | } | |
e2046b33 | 214 | |
6a488035 | 215 | } |