INFRA-132 - Remove white space after an opening "(" or before a closing ")"
[civicrm-core.git] / CRM / Core / BAO / FinancialTrxn.php
index 7008bf9607d6447ee964d671aefd9b53de32b8e1..ce87300849194a60ec9d7f74ba89a7a10ace0a67 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
 
 class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {
   /**
-   * class constructor
+   * Class constructor
    *
-   * @access public
    * @return \CRM_Financial_DAO_FinancialTrxn
    */
   /**
    *
    */
-  function __construct() {
+  public function __construct() {
     parent::__construct();
   }
 
   /**
-   * takes an associative array and creates a financial transaction object
+   * Takes an associative array and creates a financial transaction object
    *
-   * @param array  $params (reference ) an assoc array of name/value pairs
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
    *
-   * @param string $trxnEntityTable entity_table
+   * @param string $trxnEntityTable
+   *   Entity_table.
    *
-   * @return object CRM_Core_BAO_FinancialTrxn object
-   * @access public
+   * @return CRM_Core_BAO_FinancialTrxn object
    * @static
    */
-  static function create(&$params, $trxnEntityTable = null ) {
+  public static function create(&$params, $trxnEntityTable = null) {
     $trxn = new CRM_Financial_DAO_FinancialTrxn();
     $trxn->copyValues($params);
     $fids = array();
@@ -93,12 +93,12 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {
   }
 
   /**
-   * @param $contributionId
-   * @param null $contributionFinancialTypeId
+   * @param int $contributionId
+   * @param int $contributionFinancialTypeId
    *
    * @return array
    */
-  static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId = NULL) {
+  public static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId = NULL) {
     if (!$contributionFinancialTypeId) {
       $contributionFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'financial_type_id');
     }
@@ -111,7 +111,7 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {
 
     $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p);
     $ret = array();
-    while($balanceAmtDAO->fetch()) {
+    while ($balanceAmtDAO->fetch()) {
       $ret['trxn_id'] = $balanceAmtDAO->id;
       $ret['total_amount'] = $balanceAmtDAO->total_amount;
     }
@@ -120,47 +120,41 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {
   }
 
   /**
-   * 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 $defaults (reference ) an assoc array to hold the flattened values
+   * @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
-   * @access public
+   * @return CRM_Contribute_BAO_ContributionType object
    * @static
    */
-  static function retrieve( &$params, &$defaults ) {
-    $financialItem = new CRM_Financial_DAO_FinancialTrxn( );
+  public static function retrieve(&$params, &$defaults) {
+    $financialItem = new CRM_Financial_DAO_FinancialTrxn();
     $financialItem->copyValues($params);
     if ($financialItem->find(true)) {
-      CRM_Core_DAO::storeValues( $financialItem, $defaults );
+      CRM_Core_DAO::storeValues($financialItem, $defaults);
       return $financialItem;
     }
     return null;
   }
 
   /**
-   *
    * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record.
    * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
    *
    * @param $entity_id
-   * @param string $orderBy to get single trxn id for a entity table i.e last or first.
-   *
+   *   Id of the entity usually the contactID.
+   * @param string $orderBy
+   *   To get single trxn id for a entity table i.e last or first.
    * @param bool $newTrxn
    *
-   * @internal param string $entityTable name of the entity table usually 'civicrm_contact'
-   * @internal param int $entityID id of the entity usually the contactID.
-   * @return array( ) reference $tag array of category id's the contact belongs to.
+   * @return array $tag array of category id's the contact belongs to.
    *
-   * @access public
    * @static
    */
-  static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE) {
+  public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE) {
     $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL);
 
     $condition = "";
@@ -194,19 +188,16 @@ LIMIT 1;";
 
   /**
    * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record.
-   * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
-   *
-   * @param $entity_id
+   * @todo This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
    *
-   * @internal param string $entityTable name of the entity table usually 'civicrm_contact'
-   * @internal param int $entityID id of the entity usually the contactID.
+   * @param int $entity_id
+   *   Id of the entity usually the contactID.
    *
-   * @return array( ) reference $tag array of catagory id's the contact belongs to.
+   * @return array $tag array of catagory id's the contact belongs to.
    *
-   * @access public
    * @static
    */
-  static function getFinancialTrxnTotal($entity_id) {
+  public static function getFinancialTrxnTotal($entity_id) {
     $query = "
       SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft
 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id
@@ -222,15 +213,14 @@ WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1
    * Given an financial_trxn_id  check for previous entity_financial_trxn.
    *
    * @param $financial_trxn_id
+   *   Id of the latest payment.
    *
-   * @internal param int $financialTrxn_id id of the latest payment.
    *
-   * @return array( ) $payment array of previous payments
+   * @return array $payment array of previous payments
    *
-   * @access public
    * @static
    */
-  static function getPayments($financial_trxn_id) {
+  public static function getPayments($financial_trxn_id) {
     $query = "
 SELECT ef1.financial_trxn_id, sum(ef1.amount) amount
 FROM civicrm_entity_financial_trxn ef1
@@ -275,17 +265,15 @@ WHERE  ef2.financial_trxn_id =%1
    * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
    *
    * @param $entity_id
+   *   Id of the entity usually the contactID.
    * @param string $entity_table
+   *   Name of the entity table usually 'civicrm_contact'.
    *
-   * @internal param string $entityTable name of the entity table usually 'civicrm_contact'
-   * @internal param int $entityID id of the entity usually the contactID.
+   * @return array $tag array of catagory id's the contact belongs to.
    *
-   * @return array(  ) reference $tag array of catagory id's the contact belongs to.
-   *
-   * @access public
    * @static
    */
-  static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') {
+  public static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') {
     $query = "SELECT lt.price_field_value_id AS id, ft.financial_trxn_id,ft.amount AS amount FROM civicrm_entity_financial_trxn AS ft
 LEFT JOIN civicrm_financial_item AS fi ON fi.id = ft.entity_id AND fi.entity_table = 'civicrm_line_item' AND ft.entity_table = 'civicrm_financial_item'
 LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2
@@ -293,7 +281,7 @@ WHERE lt.entity_id = %1 ";
 
     $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String'));
     $dao =  CRM_Core_DAO::executeQuery($query, $sqlParams);
-    while($dao->fetch()){
+    while ($dao->fetch()) {
       $result[$dao->financial_trxn_id][$dao->id] = $dao->amount;
     }
     if (!empty($result)) {
@@ -307,12 +295,11 @@ WHERE lt.entity_id = %1 ";
   /**
    * Delete financial transaction
    *
-   * @param $entity_id
+   * @param int $entity_id
    * @return true on success, false otherwise
-   * @access public
    * @static
    */
-  static function deleteFinancialTrxn($entity_id) {
+  public static function deleteFinancialTrxn($entity_id) {
     $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft
 LEFT JOIN civicrm_entity_financial_trxn ceft
   ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution'
@@ -326,12 +313,11 @@ WHERE ceft.entity_id = %1";
   }
 
   /**
-   * create financial transaction for premium
+   * Create financial transaction for premium
    *
-   * @access public
    * @static
    */
-  static function createPremiumTrxn($params) {
+  public static function createPremiumTrxn($params) {
     if ((empty($params['financial_type_id']) || empty($params['contributionId'])) && empty($params['oldPremium'])) {
       return;
     }
@@ -372,16 +358,16 @@ WHERE ceft.entity_id = %1";
       CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params);
     }
   }
+
   /**
-   * create financial trxn and items when fee is charged
+   * Create financial trxn and items when fee is charged
    *
-   * @params params to create trxn entries
+   * @param array $params
+   *   To create trxn entries.
    *
-   * @access public
    * @static
    */
-
-  static function recordFees($params) {
+  public static function recordFees($params) {
     $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
     $domainId = CRM_Core_Config::domainID();
     $amount = 0;
@@ -431,19 +417,19 @@ WHERE ceft.entity_id = %1";
   }
 
   /*
-   * function to get partial payment amount and type of it
+   * get partial payment amount and type of it
    * return @array : payment type => amount
    * payment type  : 'amount_owed' or 'refund_due'
    */
   /**
-   * @param $entityId
+   * @param int $entityId
    * @param string $entityName
    * @param bool $returnType
    * @param null $lineItemTotal
    *
    * @return array|int|null|string
    */
-  static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) {
+  public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) {
     $value = NULL;
     if (empty($entityName)) {
       return $value;
@@ -459,6 +445,8 @@ WHERE ceft.entity_id = %1";
 
         $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
         $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId);
+        $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
+        $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId);
 
         if (empty($lineItemTotal)) {
           $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
@@ -479,7 +467,7 @@ FROM civicrm_financial_trxn ft
   LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution')
   LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id)
   LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id)
-WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount}
+WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount}
   AND ft.status_id IN ({$statusId}, {$refundStatusId})
 ";
         $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);