Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class generates form components for Financial Account | |
6a488035 TO |
20 | */ |
21 | class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { | |
2f33ec4a | 22 | use CRM_Core_Form_EntityFormTrait; |
6a488035 | 23 | |
7d289724 | 24 | /** |
fe482240 | 25 | * Flag if its a AR account type. |
7d289724 | 26 | * |
d51c6add | 27 | * @var bool |
7d289724 PN |
28 | */ |
29 | protected $_isARFlag = FALSE; | |
8ef12e64 | 30 | |
2f33ec4a JG |
31 | /** |
32 | * Explicitly declare the entity api name. | |
33 | * | |
34 | * @return string | |
35 | */ | |
36 | public function getDefaultEntity() { | |
37 | return 'FinancialAccount'; | |
38 | } | |
39 | ||
7d289724 | 40 | /** |
fe482240 | 41 | * Set variables up before form is built. |
7d289724 PN |
42 | */ |
43 | public function preProcess() { | |
44 | parent::preProcess(); | |
8ef12e64 | 45 | |
2f33ec4a JG |
46 | // Add custom data to form |
47 | CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'FinancialAccount', $this->_id); | |
48 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
49 | ||
7d289724 | 50 | if ($this->_id) { |
be2fb01f | 51 | $params = [ |
7d289724 | 52 | 'id' => $this->_id, |
be2fb01f | 53 | ]; |
e03e1641 | 54 | $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params); |
7d45c236 MM |
55 | $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' ")); |
56 | if ($financialAccount->financial_account_type_id == $financialAccountTypeId | |
8ef12e64 | 57 | && strtolower($financialAccount->account_type_code) == 'ar' |
7d45c236 | 58 | && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, $financialAccountTypeId) |
353ffa53 | 59 | ) { |
7d289724 PN |
60 | $this->_isARFlag = TRUE; |
61 | if ($this->_action & CRM_Core_Action::DELETE) { | |
134bc82c | 62 | $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 | 63 | CRM_Core_Error::statusBounce($msg); |
7d289724 PN |
64 | } |
65 | } | |
66 | } | |
67 | } | |
68 | ||
6a488035 | 69 | /** |
fe482240 | 70 | * Build the form object. |
6a488035 | 71 | */ |
045f52a3 | 72 | public function buildQuickForm() { |
481a74f4 | 73 | parent::buildQuickForm(); |
e2046b33 | 74 | $this->setPageTitle(ts('Financial Account')); |
6a488035 TO |
75 | |
76 | if ($this->_action & CRM_Core_Action::DELETE) { | |
77 | return; | |
78 | } | |
03e04002 | 79 | |
6a488035 TO |
80 | $this->applyFilter('__ALL__', 'trim'); |
81 | $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount'); | |
7d289724 | 82 | $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE); |
6a488035 | 83 | $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), |
be2fb01f | 84 | 'objectExists', ['CRM_Financial_DAO_FinancialAccount', $this->_id]); |
03e04002 | 85 | |
6a488035 TO |
86 | $this->add('text', 'description', ts('Description'), $attributes['description']); |
87 | $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']); | |
7d289724 | 88 | $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']); |
be2fb01f CW |
89 | $this->addEntityRef('contact_id', ts('Owner'), [ |
90 | 'api' => ['params' => ['contact_type' => 'Organization']], | |
ae5ffbb7 | 91 | 'create' => TRUE, |
be2fb01f | 92 | ]); |
6a488035 TO |
93 | $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']); |
94 | $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?')); | |
ddaa8ef1 | 95 | $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?')); |
6a488035 | 96 | $this->add('checkbox', 'is_tax', ts('Is Tax?')); |
d0f466d1 | 97 | |
ddaa8ef1 PN |
98 | $element = $this->add('checkbox', 'is_default', ts('Default?')); |
99 | // CRM-12470 freeze is default if is_default is set | |
100 | if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) { | |
101 | $element->freeze(); | |
102 | } | |
03e04002 | 103 | |
7611ae71 | 104 | $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id'); |
6a488035 | 105 | if (!empty($financialAccountType)) { |
7d289724 | 106 | $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), |
fbaf3c7d | 107 | ['' => ts('- select -')] + $financialAccountType, TRUE, ['class' => 'crm-select2 huge']); |
7d289724 PN |
108 | if ($this->_isARFlag) { |
109 | $element->freeze(); | |
110 | $elementAccounting->freeze(); | |
ddaa8ef1 | 111 | $elementActive->freeze(); |
7d289724 | 112 | } |
235929d0 PN |
113 | elseif ($this->_id && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($this->_id)) { |
114 | $element->freeze(); | |
115 | } | |
6a488035 | 116 | } |
03e04002 | 117 | |
2f33ec4a | 118 | $this->addCustomDataToForm(); |
6a488035 | 119 | if ($this->_action == CRM_Core_Action::UPDATE && |
353ffa53 TO |
120 | CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved') |
121 | ) { | |
be2fb01f | 122 | $this->freeze(['name', 'description', 'is_active']); |
6a488035 | 123 | } |
be2fb01f | 124 | $this->addFormRule(['CRM_Financial_Form_FinancialAccount', 'formRule'], $this); |
6a488035 | 125 | } |
03e04002 | 126 | |
6a488035 | 127 | /** |
fe482240 | 128 | * Global validation rules for the form. |
6a488035 | 129 | * |
16b10e64 CW |
130 | * @param array $values |
131 | * posted values of the form | |
da6b46f4 | 132 | * @param $files |
e8cf95b4 | 133 | * @param self $self |
da6b46f4 | 134 | * |
a6c01b45 CW |
135 | * @return array |
136 | * list of errors to be posted back to the form | |
6a488035 | 137 | */ |
045f52a3 | 138 | public static function formRule($values, $files, $self) { |
be2fb01f | 139 | $errorMsg = []; |
707f6952 RK |
140 | $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")); |
141 | if (isset($values['is_tax'])) { | |
045f52a3 | 142 | if ($values['financial_account_type_id'] != $financialAccountTypeId) { |
707f6952 RK |
143 | $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.'); |
144 | } | |
de6c59ca | 145 | if (!isset($values['tax_rate'])) { |
707f6952 RK |
146 | $errorMsg['tax_rate'] = ts('Please enter value for tax rate'); |
147 | } | |
148 | } | |
33421d01 | 149 | if ((CRM_Utils_Array::value('tax_rate', $values) != NULL)) { |
707f6952 | 150 | if ($values['tax_rate'] < 0 || $values['tax_rate'] >= 100) { |
6a488035 TO |
151 | $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100'); |
152 | } | |
153 | } | |
707f6952 RK |
154 | if ($self->_action & CRM_Core_Action::UPDATE) { |
155 | if (!(isset($values['is_tax']))) { | |
07376a08 | 156 | // @todo replace with call to CRM_Financial_BAO_FinancialAccount getSalesTaxFinancialAccount |
707f6952 | 157 | $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); |
be2fb01f | 158 | $params = [ |
353ffa53 | 159 | 'financial_account_id' => $self->_id, |
21dfd5f5 | 160 | 'account_relationship' => $relationshipId, |
be2fb01f | 161 | ]; |
d75f2f47 | 162 | $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); |
707f6952 RK |
163 | if ($result) { |
164 | $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account'); | |
165 | } | |
166 | } | |
167 | } | |
481a74f4 | 168 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; |
6a488035 | 169 | } |
03e04002 | 170 | |
6a488035 | 171 | /** |
c490a46a | 172 | * Set default values for the form. |
cded2ebf | 173 | * the default values are retrieved from the database. |
6a488035 | 174 | */ |
00be9182 | 175 | public function setDefaultValues() { |
6a488035 | 176 | $defaults = parent::setDefaultValues(); |
2f33ec4a | 177 | $defaults = array_merge($defaults, CRM_Custom_Form_CustomData::setDefaultValues($this)); |
6a488035 | 178 | if ($this->_action & CRM_Core_Action::ADD) { |
d357f225 | 179 | $defaults['contact_id'] = CRM_Core_BAO_Domain::getDomain()->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(); | |
2f33ec4a | 199 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($this->_submitValues, $this->_id, 'FinancialAccount'); |
03e04002 | 200 | |
6a488035 | 201 | if ($this->_action & CRM_Core_Action::UPDATE) { |
235929d0 | 202 | $params['id'] = $this->_id; |
6a488035 | 203 | } |
5a9f7af1 PN |
204 | foreach ([ |
205 | 'is_active', | |
206 | 'is_deductible', | |
207 | 'is_tax', | |
208 | 'is_default', | |
209 | ] as $field) { | |
210 | $params[$field] = CRM_Utils_Array::value($field, $params, FALSE); | |
211 | } | |
235929d0 | 212 | $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params); |
be2fb01f | 213 | CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', [1 => $financialAccount->name]), ts('Saved'), 'success'); |
6a488035 TO |
214 | } |
215 | } | |
e2046b33 | 216 | |
6a488035 | 217 | } |