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