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