Skip to content

Commit

Permalink
Deprecate Financial_BAO_EntityFinancialAccount::del()
Browse files Browse the repository at this point in the history
Change 'setStatus' to exceptions and create status messages at form
layer.
  • Loading branch information
aydun committed Nov 29, 2022
1 parent 879e495 commit 8cd283f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
92 changes: 49 additions & 43 deletions CRM/Financial/BAO/EntityFinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,59 +75,65 @@ public static function add(&$params, $ids = NULL) {
* Delete financial Types.
*
* @param int $financialTypeAccountId
* @param int $accountId
* @deprecated
* @param int $accountId (not used)
*
* @throws \CRM_Core_Exception
* @deprecated
*/
public static function del($financialTypeAccountId, $accountId = NULL) {
// check if financial type is present
$check = FALSE;
$relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
static::deleteRecord(['id' => $financialTypeAccountId]);
}

$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id');
// check dependencies
// FIXME hardcoded list = bad
$dependency = [
['Contribute', 'Contribution'],
['Contribute', 'ContributionPage'],
['Member', 'MembershipType'],
['Price', 'PriceFieldValue'],
['Grant', 'Grant'],
['Contribute', 'PremiumsProduct'],
['Contribute', 'Product'],
['Price', 'LineItem'],
];
/**
* Callback for hook_civicrm_pre().
* @param \Civi\Core\Event\PreEvent $event
* @throws CRM_Core_Exception
*/
public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
if ($event->action === 'delete' && $event->id) {
$financialTypeAccountId = $event->id;

foreach ($dependency as $name) {
$daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
if (class_exists($daoString)) {
/** @var \CRM_Core_DAO $dao */
$dao = new $daoString();
$dao->financial_type_id = $financialTypeId;
if ($dao->find(TRUE)) {
$check = TRUE;
break;
// check if financial type is present
$check = FALSE;
$relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');

$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id');
// check dependencies
// FIXME hardcoded list = bad
$dependency = [
['Contribute', 'Contribution'],
['Contribute', 'ContributionPage'],
['Member', 'MembershipType'],
['Price', 'PriceFieldValue'],
['Grant', 'Grant'],
['Contribute', 'PremiumsProduct'],
['Contribute', 'Product'],
['Price', 'LineItem'],
];

foreach ($dependency as $name) {
$daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
if (class_exists($daoString)) {
/** @var \CRM_Core_DAO $dao */
$dao = new $daoString();
$dao->financial_type_id = $financialTypeId;
if ($dao->find(TRUE)) {
$check = TRUE;
break;
}
}
}
}

if ($check) {
if ($name[1] === 'PremiumsProduct' || $name[1] === 'Product') {
CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', [1 => $relationValues[$financialTypeAccountId]]));
}
else {
$accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship');
CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', [1 => $relationValues[$accountRelationShipId]]), NULL, 'error');
if ($check) {
if ($name[1] === 'PremiumsProduct' || $name[1] === 'Product') {
throw new \CRM_Core_Exception(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', [1 => $relationValues[$financialTypeAccountId]]));
}
else {
$accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship');
throw new \CRM_Core_Exception(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', [1 => $relationValues[$accountRelationShipId]]), NULL, 'error');
}
}
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}"));
}

// delete from financial Type table
$financialType = new CRM_Financial_DAO_EntityFinancialAccount();
$financialType->id = $financialTypeAccountId;
$financialType->find(TRUE);
$financialType->delete();
CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', [1 => $relationValues[$financialType->account_relationship]]));
}

/**
Expand Down
1 change: 1 addition & 0 deletions CRM/Financial/BAO/FinancialTypeAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
* Class was consolidated in 5.57 to follow standard naming convention
* @deprecated
*/

class_alias('CRM_Financial_BAO_EntityFinancialAccount', 'CRM_Financial_BAO_FinancialTypeAccount');
9 changes: 8 additions & 1 deletion CRM/Financial/Form/FinancialTypeAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,14 @@ public static function formRule($values, $files, $self) {
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_Financial_BAO_EntityFinancialAccount::del($this->_id, $this->_aid);
try {
CRM_Financial_BAO_FinancialTypeAccount::del($this->_id);
CRM_Financial_BAO_EntityFinancialAccount::del($this->_id);
}
catch (CRM_Core_Exception $e) {
CRM_Core_Session::setStatus($e->message);
return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$this->_aid}"));
}
CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.'));
}
else {
Expand Down

0 comments on commit 8cd283f

Please sign in to comment.