Deprecate `Financial_BAO_EntityFinancialAccount::del()`
authorAidan Saunders <aidan.saunders@squiffle.uk>
Tue, 22 Nov 2022 16:55:21 +0000 (16:55 +0000)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Tue, 29 Nov 2022 21:10:31 +0000 (21:10 +0000)
Change 'setStatus' to exceptions and create status messages at form
layer.

CRM/Financial/BAO/EntityFinancialAccount.php
CRM/Financial/BAO/FinancialTypeAccount.php
CRM/Financial/Form/FinancialTypeAccount.php

index df7c195749b1aa999a9743699c9b33b94a6974bd..f100f717bbc59bee762b38a8700db0ad84af81ae 100644 (file)
@@ -75,59 +75,65 @@ class CRM_Financial_BAO_EntityFinancialAccount extends CRM_Financial_DAO_EntityF
    * 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]]));
   }
 
   /**
index 4e0443aa64adbd3993b053d5a53597ab2750a35c..fdc1de245fbd4299ef8577ffe7f130275ff95674 100644 (file)
@@ -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');
index 561853ad384d8fd341f785bfc9620d122f64f6a4..15abe94df53da83d3bd6d7c9ecdeb7eb09db1783 100644 (file)
@@ -272,7 +272,14 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Core_Form {
    */
   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 {