Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-09-14-48-51
[civicrm-core.git] / CRM / Financial / BAO / FinancialTypeAccount.php
index f22c0992ab04b7a986c49faba9d5912763862e6e..6538e520d1ac7d3f17d5ddbae16d1bc9f2c49a7a 100644 (file)
 class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFinancialAccount {
 
   /**
-   * class constructor
+   * Class constructor
    */
   function __construct( ) {
     parent::__construct( );
   }
 
   /**
-   * financial account
+   * Financial account
    * @var array
    * @static
    */
   private static $financialAccount;
 
   /**
-   * Takes a bunch of params that are needed to match certain criteria and
-   * retrieves the relevant objects. Typically the valid params are only
-   * contact_id. We'll tweak this function to be more full featured over a period
-   * of time. This is the inverse function of create. It also stores all the retrieved
-   * values in the default array
+   * Fetch object based on array of properties
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
+   * @param array $params (reference ) an assoc array of name/value pairs
    * @param array $defaults (reference ) an assoc array to hold the flattened values
    *
-   * @return object CRM_Contribute_BAO_ContributionType object
+   * @param array $allValues
+   *
+   * @return CRM_Contribute_BAO_ContributionType object
    * @access public
    * @static
    */
@@ -75,7 +73,7 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
   }
 
   /**
-   * function to add the financial types
+   * Add the financial types
    *
    * @param array $params reference array contains the values submitted by the form
    * @param array $ids    reference array contains the id
@@ -104,9 +102,11 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
   }
 
   /**
-   * Function to delete financial Types
+   * Delete financial Types
+   *
+   * @param int $financialTypeAccountId
+   * @param int $accountId
    *
-   * @param int $contributionTypeId
    * @static
    */
   static function del($financialTypeAccountId, $accountId = null) {
@@ -158,7 +158,7 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
   }
 
   /**
-   * Function to get Financial Account Name
+   * Get Financial Account Name
    *
    * @param int $entityId
    *
@@ -186,7 +186,7 @@ AND entity_id = %2";
   }
 
   /**
-   * Function to financial Account for payment instrument
+   * Financial Account for payment instrument
    *
    * @param int $paymentInstrumentValue payment instrument value
    *
@@ -218,15 +218,13 @@ WHERE cog.name = 'payment_instrument' ";
   }
 
   /**
-   * Function to create default entity financial accounts
+   * Create default entity financial accounts
    * for financial type
    * CRM-12470
    *
    * @param $financialType
    *
    * @return array
-   * @internal param int $financialTypeId financial type id
-   *
    * @static
    */
   static function createDefaultFinancialAccounts($financialType) {
@@ -239,48 +237,68 @@ WHERE cog.name = 'payment_instrument' ";
       array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
       array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
     );
-    $params = array(
-      'name' => $financialType->name,
-      'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
-      'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
-      'description' => $financialType->description,
-      'account_type_code' => 'INC',
-      'is_active' => 1,
+
+    $dao =  CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1',
+      array(1 => array($financialType->name, 'String'))
     );
-    $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
+    $dao->fetch();
+    $existingFinancialAccount = array();
+    if (!$dao->N) {
+      $params = array(
+        'name' => $financialType->name,
+        'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
+        'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
+        'description' => $financialType->description,
+        'account_type_code' => 'INC',
+        'is_active' => 1,
+      );
+      $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
+    }
+    else {
+      $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id;
+    }
     $params = array (
       'entity_table' => 'civicrm_financial_type',
       'entity_id' => $financialType->id,
     );
     foreach ($relationships as $key => $value) {
-      if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
-        $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
-        if (!empty($params['financial_account_id'])) {
-          $titles[] = 'Accounts Receivable';
-        }
-        else {
-          $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
+      if (!array_key_exists($value, $existingFinancialAccount)) {
+        if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
+          $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
+          if (!empty($params['financial_account_id'])) {
+            $titles[] = 'Accounts Receivable';
+          }
+          else {
+            $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
             LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
             WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
+            $dao = CRM_Core_DAO::executeQuery($query);
+            $dao->fetch();
+            $params['financial_account_id'] = $dao->financial_account_id;
+            $titles[] = $dao->name;
+          }
+        }
+        elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) {
+          $params['financial_account_id'] = $financialAccount->id;
+        }
+        else {
+          $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
           $dao = CRM_Core_DAO::executeQuery($query);
           $dao->fetch();
-          $params['financial_account_id'] = $dao->financial_account_id;
+          $params['financial_account_id'] = $dao->id;
           $titles[] = $dao->name;
         }
       }
-      elseif ($accountRelationship[$key] == 'Income Account is') {
-        $params['financial_account_id'] = $financialAccount->id;
-      }
       else {
-        $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
-        $dao = CRM_Core_DAO::executeQuery($query);
-        $dao->fetch();
-        $params['financial_account_id'] = $dao->id;
-        $titles[] = $dao->name;
+         $params['financial_account_id'] = $existingFinancialAccount[$value];
+         $titles[] = $financialType->name;
       }
       $params['account_relationship'] = $key;
       self::add($params);
     }
+    if (!empty($existingFinancialAccount)) {
+      $titles = array();
+    }
     return $titles;
   }
 }