Merge pull request #10157 from eileenmcnaughton/iida3
[civicrm-core.git] / CRM / Financial / BAO / FinancialTypeAccount.php
index c34235e6e2c807eb1584a8c9247f301fdc10113c..92b8aa8d6199e39f2ca84770126e0e5c122222e9 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFinancialAccount {
 
@@ -86,13 +86,12 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
       $financialTypeAccount->entity_table = $params['entity_table'];
       $financialTypeAccount->find(TRUE);
     }
-    else {
-      $financialTypeAccount->id = CRM_Utils_Array::value('entityFinancialAccount', $ids);
-    }
     if (!empty($ids['entityFinancialAccount'])) {
       $financialTypeAccount->id = $ids['entityFinancialAccount'];
+      $financialTypeAccount->find(TRUE);
     }
     $financialTypeAccount->copyValues($params);
+    self::validateRelationship($financialTypeAccount);
     $financialTypeAccount->save();
     return $financialTypeAccount;
   }
@@ -152,34 +151,6 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
     CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', array(1 => $relationValues[$financialType->account_relationship])));
   }
 
-  /**
-   * Get Financial Account Name.
-   *
-   * @param int $entityId
-   *
-   * @param string $entityTable
-   *
-   * @param string $columnName
-   *   Column to fetch.
-   *
-   * @return null|string
-   */
-  public static function getFinancialAccount($entityId, $entityTable, $columnName = 'name') {
-    $join = $columnName == 'name' ? 'LEFT JOIN civicrm_financial_account ON civicrm_entity_financial_account.financial_account_id = civicrm_financial_account.id' : NULL;
-    $query = "
-SELECT {$columnName}
-FROM civicrm_entity_financial_account
-{$join}
-WHERE entity_table = %1
-AND entity_id = %2";
-
-    $params = array(
-      1 => array($entityTable, 'String'),
-      2 => array($entityId, 'Integer'),
-    );
-    return CRM_Core_DAO::singleValueQuery($query, $params);
-  }
-
   /**
    * Financial Account for payment instrument.
    *
@@ -247,7 +218,7 @@ WHERE cog.name = 'payment_instrument' ";
         'account_type_code' => 'INC',
         'is_active' => 1,
       );
-      $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
+      $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
     }
     else {
       $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id;
@@ -297,4 +268,22 @@ WHERE cog.name = 'payment_instrument' ";
     return $titles;
   }
 
+  /**
+   * Validate account relationship with financial account type
+   *
+   * @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount
+   *
+   */
+  public static function validateRelationship($financialTypeAccount) {
+    $financialAccountLinks = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
+    $financialAccountType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialTypeAccount->financial_account_id, 'financial_account_type_id');
+    if (CRM_Utils_Array::value($financialTypeAccount->account_relationship, $financialAccountLinks) != $financialAccountType) {
+      $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
+      $params = array(
+        1 => $accountRelationships[$financialTypeAccount->account_relationship],
+      );
+      throw new Exception(ts("This financial account cannot have '%1' relationship.", $params));
+    }
+  }
+
 }