3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Financial Type Account
21 class CRM_Financial_Form_FinancialTypeAccount
extends CRM_Core_Form
{
24 * The financial type id saved to the session for an update.
31 * The financial type accounts id, used when editing the field
38 * The name of the BAO object for this form.
45 * Flag if its a AR account type.
49 protected $_isARFlag = FALSE;
52 * Set variables up before form is built.
54 public function preProcess() {
55 $this->_aid
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this);
56 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
58 if (!$this->_id
&& ($this->_action
& CRM_Core_Action
::UPDATE
)) {
59 $this->_id
= CRM_Utils_Type
::escape($this->_id
, 'Positive');
61 $url = CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
62 "reset=1&action=browse&aid={$this->_aid}");
64 $this->_BAOName
= 'CRM_Financial_BAO_FinancialTypeAccount';
65 if ($this->_aid
&& ($this->_action
& CRM_Core_Action
::ADD
)) {
66 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid
, 'name');
67 CRM_Utils_System
::setTitle($this->_title
. ' - ' . ts('Financial Accounts'));
69 $session = CRM_Core_Session
::singleton();
70 $session->pushUserContext($url);
73 if (!($this->_action
& CRM_Core_Action
::ADD
)) {
74 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
75 $accountRelationship = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id
, 'account_relationship');
76 if ($accountRelationship == $relationTypeId) {
77 $this->_isARFlag
= TRUE;
78 if ($this->_action
& CRM_Core_Action
::DELETE
) {
79 CRM_Core_Session
::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."),
81 CRM_Utils_System
::redirect($url);
86 $financialAccount = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id
, 'financial_account_id');
87 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name');
88 CRM_Utils_System
::setTitle($fieldTitle . ' - ' . ts('Financial Type Accounts'));
93 'title' => ts('Financial Type Accounts'),
97 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
101 * Build the form object.
103 public function buildQuickForm() {
104 parent
::buildQuickForm();
105 if ($this->_action
& CRM_Core_Action
::VIEW ||
$this->_action
& CRM_Core_Action
::PREVIEW
) {
109 'name' => ts('Done'),
118 'name' => $this->_action
& CRM_Core_Action
::DELETE ?
ts('Delete') : ts('Save'),
123 'name' => ts('Cancel'),
127 $this->setPageTitle(ts('Financial Type Account'));
129 if ($this->_action
& CRM_Core_Action
::DELETE
) {
133 if (isset($this->_id
)) {
134 $params = ['id' => $this->_id
];
135 CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
136 $this->setDefaults($defaults);
137 $financialAccountTitle = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $defaults['financial_account_id'], 'name');
140 $this->applyFilter('__ALL__', 'trim');
142 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
143 $this->assign('aid', $this->_id
);
144 // hidden field to catch the group id in profile
145 $this->add('hidden', 'financial_type_id', $this->_aid
);
147 // hidden field to catch the field id in profile
148 $this->add('hidden', 'account_type_id', $this->_id
);
150 $params['orderColumn'] = 'label';
151 $AccountTypeRelationship = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
152 if (!empty($AccountTypeRelationship)) {
153 $element = $this->add('select',
154 'account_relationship',
155 ts('Financial Account Relationship'),
156 ['select' => ts('- Select Financial Account Relationship -')] +
$AccountTypeRelationship,
161 if ($this->_isARFlag
) {
165 if ($this->_action
== CRM_Core_Action
::ADD
) {
166 if (!empty($this->_submitValues
['account_relationship']) ||
!empty($this->_submitValues
['financial_account_id'])) {
167 $financialAccountType = CRM_Financial_BAO_FinancialAccount
::getfinancialAccountRelations();
168 $financialAccountType = $financialAccountType[$this->_submitValues
['account_relationship']] ??
NULL;
169 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
171 $financialAccountSelect = ['' => ts('- select -')] +
$result;
174 $financialAccountSelect = [
175 'select' => ts('- select -'),
176 ] + CRM_Contribute_PseudoConstant
::financialAccount();
179 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
180 $financialAccountType = CRM_Financial_BAO_FinancialAccount
::getfinancialAccountRelations();
181 $financialAccountType = $financialAccountType[$this->_defaultValues
['account_relationship']];
182 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
184 $financialAccountSelect = ['' => ts('- select -')] +
$result;
188 'financial_account_id',
189 ts('Financial Account'),
190 $financialAccountSelect,
194 $this->addFormRule(['CRM_Financial_Form_FinancialTypeAccount', 'formRule'], $this);
198 * Global validation rules for the form.
200 * @param array $values
201 * posted values of the form
206 * list of errors to be posted back to the form
208 public static function formRule($values, $files, $self) {
211 if ($self->_action
== CRM_Core_Action
::DELETE
) {
212 $relationValues = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
213 if (CRM_Utils_Array
::value('financial_account_id', $values) != 'select') {
214 if ($relationValues[$values['account_relationship']] == 'Premiums Inventory Account is' ||
$relationValues[$values['account_relationship']] == 'Cost of Sales Account is') {
215 $premiumsProduct = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $values['financial_type_id'], 'product_id', 'financial_type_id');
216 $product = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Product', $values['financial_type_id'], 'name', 'financial_type_id');
217 if (!empty($premiumsProduct) ||
!empty($product)) {
218 $errorMsg['account_relationship'] = 'You cannot remove ' . $relationValues[$values['account_relationship']] . ' relationship while the Financial Type is used for a Premium.';
223 if (CRM_Utils_Array
::value('account_relationship', $values) == 'select') {
224 $errorMsg['account_relationship'] = 'Financial Account relationship is a required field.';
226 if (CRM_Utils_Array
::value('financial_account_id', $values) == 'select') {
227 $errorMsg['financial_account_id'] = 'Financial Account is a required field.';
229 if (!empty($values['account_relationship']) && !empty($values['financial_account_id'])) {
231 'account_relationship' => $values['account_relationship'],
232 'entity_id' => $self->_aid
,
233 'entity_table' => 'civicrm_financial_type',
236 if ($self->_action
== CRM_Core_Action
::ADD
) {
237 // @todo replace with call to CRM_Financial_BAO_FinancialAccount getSalesTaxFinancialAccount
238 $relationshipId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
239 $isTax = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $values['financial_account_id'], 'is_tax');
240 if ($values['account_relationship'] == $relationshipId) {
242 $errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account');
245 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
250 if ($self->_action
== CRM_Core_Action
::UPDATE
) {
251 if ($values['account_relationship'] == $self->_defaultValues
['account_relationship'] && $values['financial_account_id'] == $self->_defaultValues
['financial_account_id']) {
255 $params['financial_account_id'] = $values['financial_account_id'];
256 $result = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($params, $defaults);
264 $errorMsg['account_relationship'] = ts('This account relationship already exits');
267 return CRM_Utils_Array
::crmIsEmptyArray($errorMsg) ?
TRUE : $errorMsg;
271 * Process the form submission.
273 public function postProcess() {
274 if ($this->_action
& CRM_Core_Action
::DELETE
) {
275 CRM_Financial_BAO_FinancialTypeAccount
::del($this->_id
, $this->_aid
);
276 CRM_Core_Session
::setStatus(ts('Selected financial type account has been deleted.'));
280 // store the submitted values in an array
281 $params = $this->exportValues();
283 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
284 $ids['entityFinancialAccount'] = $this->_id
;
286 if ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
) {
287 $params['financial_account_id'] = $this->_submitValues
['financial_account_id'];
289 $params['entity_table'] = 'civicrm_financial_type';
290 if ($this->_action
& CRM_Core_Action
::ADD
) {
291 $params['entity_id'] = $this->_aid
;
294 $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount
::add($params, $ids);
295 CRM_Core_Session
::setStatus(ts('The financial type Account has been saved.'), ts('Saved'), 'success');
297 catch (CRM_Core_Exception
$e) {
298 CRM_Core_Error
::statusBounce($e->getMessage());
302 $buttonName = $this->controller
->getButtonName();
303 $session = CRM_Core_Session
::singleton();
305 if ($buttonName == $this->getButtonName('next', 'new')) {
306 CRM_Core_Session
::setStatus(ts(' You can add another Financial Account Type.'));
307 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
308 "reset=1&action=add&aid={$this->_aid}"));
311 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts',
312 "reset=1&action=browse&aid={$this->_aid}"));