From efc59a3356e8daddc2413e462409e479e0cc5188 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 31 Mar 2022 22:18:29 -0400 Subject: [PATCH] Cleanup FinancialAccount BAO to use `writeRecord` and deprecate `add` Sets default values for boolean fields --- CRM/Financial/BAO/FinancialAccount.php | 88 +++++++------------ CRM/Financial/DAO/FinancialAccount.php | 25 ++++-- CRM/Upgrade/Incremental/php/FiveFortyNine.php | 8 ++ xml/schema/Financial/FinancialAccount.xml | 11 ++- 4 files changed, 68 insertions(+), 64 deletions(-) diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index b92970a8d6..77f65011ac 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -51,65 +51,13 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco /** * Add the financial types. * + * @deprecated * @param array $params - * Reference array contains the values submitted by the form. * * @return CRM_Financial_DAO_FinancialAccount */ - public static function add(&$params) { - if (empty($params['id'])) { - $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); - $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE); - $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE); - $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE); - $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); - } - if (!empty($params['id']) - && !empty($params['financial_account_type_id']) - && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount( - $params['id'], - $params['financial_account_type_id'] - ) - ) { - throw new CRM_Core_Exception(ts('You cannot change the account type since this financial account refers to a financial item having an account type of Revenue/Liability.')); - } - if (!empty($params['is_default'])) { - $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1'; - $queryParams = [1 => [$params['financial_account_type_id'], 'Integer']]; - CRM_Core_DAO::executeQuery($query, $queryParams); - } - - // action is taken depending upon the mode - $financialAccount = new CRM_Financial_DAO_FinancialAccount(); - - // invoke pre hook - $op = 'create'; - if (!empty($params['id'])) { - $op = 'edit'; - } - CRM_Utils_Hook::pre($op, 'FinancialAccount', CRM_Utils_Array::value('id', $params), $params); - - if (!empty($params['id'])) { - $financialAccount->id = $params['id']; - $financialAccount->find(TRUE); - } - - $financialAccount->copyValues($params); - $financialAccount->save(); - - if (!empty($params['custom']) && is_array($params['custom'])) { - CRM_Core_BAO_CustomValueTable::store($params['custom'], static::$_tableName, $financialAccount->id, $op); - } - - // invoke post hook - $op = 'create'; - if (!empty($params['id'])) { - $op = 'edit'; - } - CRM_Utils_Hook::post($op, 'FinancialAccount', $financialAccount->id, $financialAccount); - CRM_Core_PseudoConstant::flush(); - - return $financialAccount; + public static function add($params) { + return self::writeRecord($params); } /** @@ -117,6 +65,8 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco * * @deprecated * @param int $financialAccountId + * + * @return bool */ public static function del($financialAccountId) { try { @@ -152,6 +102,34 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco } } } + if ($event->action === 'create' || $event->action === 'edit') { + $params = $event->params; + if (!empty($params['id']) + && !empty($params['financial_account_type_id']) + && CRM_Financial_BAO_FinancialAccount::validateFinancialAccount( + $params['id'], + $params['financial_account_type_id'] + ) + ) { + throw new CRM_Core_Exception(ts('You cannot change the account type since this financial account refers to a financial item having an account type of Revenue/Liability.')); + } + if (!empty($params['is_default'])) { + if (empty($params['financial_account_type_id'])) { + $params['financial_account_type_id'] = CRM_Core_DAO::getFieldValue(__CLASS__, $params['id'], 'financial_account_type_id'); + } + $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1'; + $queryParams = [1 => [$params['financial_account_type_id'], 'Integer']]; + CRM_Core_DAO::executeQuery($query, $queryParams); + } + } + } + + /** + * Callback for hook_civicrm_post(). + * @param \Civi\Core\Event\PostEvent $event + */ + public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + CRM_Core_PseudoConstant::flush(); } /** diff --git a/CRM/Financial/DAO/FinancialAccount.php b/CRM/Financial/DAO/FinancialAccount.php index 0f83708dd3..568fa3fe5e 100644 --- a/CRM/Financial/DAO/FinancialAccount.php +++ b/CRM/Financial/DAO/FinancialAccount.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Financial/FinancialAccount.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:ae32a35de5bc66a8e230b5595b77810f) + * (GenCodeChecksum:bbb54fc9abdb9f5187f84b0b4eadb53e) */ /** @@ -106,7 +106,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { /** * Is this a header account which does not allow transactions to be posted against it directly, but only to its sub-accounts? * - * @var bool|string|null + * @var bool|string * (SQL type: tinyint) * Note that values will be retrieved from the database as a string. */ @@ -115,7 +115,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { /** * Is this account tax-deductible? * - * @var bool|string|null + * @var bool|string * (SQL type: tinyint) * Note that values will be retrieved from the database as a string. */ @@ -124,7 +124,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { /** * Is this account for taxes? * - * @var bool|string|null + * @var bool|string * (SQL type: tinyint) * Note that values will be retrieved from the database as a string. */ @@ -142,7 +142,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { /** * Is this a predefined system object? * - * @var bool|string|null + * @var bool|string * (SQL type: tinyint) * Note that values will be retrieved from the database as a string. */ @@ -151,7 +151,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { /** * Is this property active? * - * @var bool|string|null + * @var bool|string * (SQL type: tinyint) * Note that values will be retrieved from the database as a string. */ @@ -160,7 +160,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { /** * Is this account the default one (or default tax one) for its financial_account_type? * - * @var bool|string|null + * @var bool|string * (SQL type: tinyint) * Note that values will be retrieved from the database as a string. */ @@ -342,6 +342,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_BOOLEAN, 'title' => ts('Header Financial Account?'), 'description' => ts('Is this a header account which does not allow transactions to be posted against it directly, but only to its sub-accounts?'), + 'required' => TRUE, 'where' => 'civicrm_financial_account.is_header_account', 'default' => '0', 'table_name' => 'civicrm_financial_account', @@ -355,8 +356,9 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_BOOLEAN, 'title' => ts('Deductible Financial Account?'), 'description' => ts('Is this account tax-deductible?'), + 'required' => TRUE, 'where' => 'civicrm_financial_account.is_deductible', - 'default' => '1', + 'default' => '0', 'table_name' => 'civicrm_financial_account', 'entity' => 'FinancialAccount', 'bao' => 'CRM_Financial_BAO_FinancialAccount', @@ -368,6 +370,7 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_BOOLEAN, 'title' => ts('Tax Financial Account?'), 'description' => ts('Is this account for taxes?'), + 'required' => TRUE, 'where' => 'civicrm_financial_account.is_tax', 'default' => '0', 'table_name' => 'civicrm_financial_account', @@ -397,7 +400,9 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_BOOLEAN, 'title' => ts('Reserved Financial Account?'), 'description' => ts('Is this a predefined system object?'), + 'required' => TRUE, 'where' => 'civicrm_financial_account.is_reserved', + 'default' => '0', 'table_name' => 'civicrm_financial_account', 'entity' => 'FinancialAccount', 'bao' => 'CRM_Financial_BAO_FinancialAccount', @@ -409,7 +414,9 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_BOOLEAN, 'title' => ts('Financial Account is Active'), 'description' => ts('Is this property active?'), + 'required' => TRUE, 'where' => 'civicrm_financial_account.is_active', + 'default' => '1', 'table_name' => 'civicrm_financial_account', 'entity' => 'FinancialAccount', 'bao' => 'CRM_Financial_BAO_FinancialAccount', @@ -421,7 +428,9 @@ class CRM_Financial_DAO_FinancialAccount extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_BOOLEAN, 'title' => ts('Default Financial Account'), 'description' => ts('Is this account the default one (or default tax one) for its financial_account_type?'), + 'required' => TRUE, 'where' => 'civicrm_financial_account.is_default', + 'default' => '0', 'table_name' => 'civicrm_financial_account', 'entity' => 'FinancialAccount', 'bao' => 'CRM_Financial_BAO_FinancialAccount', diff --git a/CRM/Upgrade/Incremental/php/FiveFortyNine.php b/CRM/Upgrade/Incremental/php/FiveFortyNine.php index 269073cea2..7013f9d4df 100644 --- a/CRM/Upgrade/Incremental/php/FiveFortyNine.php +++ b/CRM/Upgrade/Incremental/php/FiveFortyNine.php @@ -52,6 +52,14 @@ class CRM_Upgrade_Incremental_php_FiveFortyNine extends CRM_Upgrade_Incremental_ 'is_pay_later' => "DEFAULT 0", 'is_template' => "DEFAULT 0 COMMENT 'Shows this is a template for recurring contributions.'", ], + 'civicrm_financial_account' => [ + 'is_header_account' => "DEFAULT 0 COMMENT 'Is this a header account which does not allow transactions to be posted against it directly, but only to its sub-accounts?'", + 'is_deductible' => "DEFAULT 0 COMMENT 'Is this account tax-deductible?'", + 'is_tax' => "DEFAULT 0 COMMENT 'Is this account for taxes?'", + 'is_reserved' => "DEFAULT 0 COMMENT 'Is this a predefined system object?'", + 'is_active' => "DEFAULT 1 COMMENT 'Is this property active?'", + 'is_default' => "DEFAULT 0 COMMENT 'Is this account the default one (or default tax one) for its financial_account_type?'", + ], ]; /** diff --git a/xml/schema/Financial/FinancialAccount.xml b/xml/schema/Financial/FinancialAccount.xml index 4b970a3616..aa6815cbe7 100644 --- a/xml/schema/Financial/FinancialAccount.xml +++ b/xml/schema/Financial/FinancialAccount.xml @@ -111,6 +111,7 @@ boolean Header Financial Account? 0 + true Is this a header account which does not allow transactions to be posted against it directly, but only to its sub-accounts? 4.3 @@ -118,7 +119,8 @@ is_deductible boolean Deductible Financial Account? - 1 + 0 + true Is this account tax-deductible? 4.3 @@ -127,6 +129,7 @@ boolean Tax Financial Account? 0 + true Is this account for taxes? 4.3 @@ -143,6 +146,8 @@ boolean Reserved Financial Account? Is this a predefined system object? + 0 + true 4.3 @@ -150,6 +155,8 @@ boolean Financial Account is Active Is this property active? + 1 + true 4.3 @@ -157,6 +164,8 @@ boolean Default Financial Account Is this account the default one (or default tax one) for its financial_account_type? + 0 + true 4.3 -- 2.25.1