--CRM-16259, added function to allocate line item proportinally
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 16 Dec 2015 13:49:52 +0000 (19:19 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Wed, 20 Jan 2016 21:31:40 +0000 (03:01 +0530)
CRM/Contribute/BAO/Contribution.php

index 617033da661ccc1514e265babd5c26ae6be66602..af4de0e67f04ec8fbdeb3a69de615983512d2e47 100644 (file)
@@ -4776,4 +4776,43 @@ LIMIT 1;";
     }
   }
 
+  /**
+   * Function use to store line item proportionaly in 
+   * in entity financial trxn table
+   *
+   * @param array $params
+   *  array of contribution params.
+   * @param object $trxn
+   *  CRM_Financial_DAO_FinancialTrxn object
+   * @param array $contribution
+   *
+   */
+  public static function assignProportionalLineItems($params, $trxn, $contribution) {
+    $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution_id']);
+    if (!empty($lineItems)) {
+      // get financial item
+      $sql = "SELECT fi.id, li.price_field_value_id
+      FROM civicrm_financial_item fi
+      INNER JOIN civicrm_line_item li ON li.id = fi.entity_id
+      WHERE li.contribution_id = %1";
+      $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($params['contribution_id'], 'Integer')));
+      while ($dao->fetch()) {
+        $ftIds[$dao->price_field_value_id] = $dao->id;
+      }
+      foreach ($lineItems as $key => $value) {
+        $paid = $value['line_total'] * ($params['total_amount'] / $contribution['total_amount']);
+        // Record Entity Financial Trxn
+        $eftParams = array(
+          'entity_table' => 'civicrm_financial_item',
+          'financial_trxn_id' => $trxn->id,
+          'amount' => $paid,
+          'entity_id' => $ftIds[$value['price_field_value_id']],
+        );
+        $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
+        $entityTrxn->copyValues($eftParams);
+        $entityTrxn->save();
+      }
+    }
+  }
+
 }