return $financialAccount;
}
+ /**
+ * Create tax entry in civicrm_entity_financial_trxn table.
+ *
+ * @param array $entityParams
+ *
+ * @param array $eftParams
+ *
+ */
+ public static function createProportionalEntry($entityParams, $eftParams) {
+ $paid = $entityParams['line_item_amount'] * ($entityParams['trxn_total_amount'] / $entityParams['contribution_total_amount']);
+ // Record Entity Financial Trxn
+ $eftParams['amount'] = round($paid, 2);
+ civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
+ }
+
+ /**
+ * Create array of last financial item id's.
+ *
+ * @param array $contributionId
+ *
+ */
+ public static function getLastFinancialItemIds($contributionId) {
+ $sql = "SELECT fi.id, li.price_field_value_id, li.tax_amount, fi.financial_account_id
+ FROM civicrm_financial_item fi
+ INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'
+ WHERE li.contribution_id = %1";
+ $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contributionId, 'Integer')));
+ $ftIds = $taxItems = array();
+ $salesTaxFinancialAccount = self::getSalesTaxFinancialAccounts();
+ while ($dao->fetch()) {
+ /* if sales tax item*/
+ if (in_array($dao->financial_account_id, $salesTaxFinancialAccount)) {
+ $taxItems[$dao->price_field_value_id] = array(
+ 'financial_item_id' => $dao->id,
+ 'amount' => $dao->tax_amount,
+ );
+ }
+ else {
+ $ftIds[$dao->price_field_value_id] = $dao->id;
+ }
+ }
+ return array($ftIds, $taxItems);
+ }
+
+ /**
+ * Create proportional entries in civicrm_entity_financial_trxn.
+ *
+ * @param array $entityParams
+ *
+ * @param array $lineItems
+ *
+ * @param array $ftIds
+ *
+ * @param array $taxItems
+ *
+ */
+ public static function createProportionalFinancialEntries($entityParams, $lineItems, $ftIds, $taxItems) {
+ $eftParams = array(
+ 'entity_table' => 'civicrm_financial_item',
+ 'financial_trxn_id' => $entityParams['trxn_id'],
+ );
+ foreach ($lineItems as $key => $value) {
+ if ($value['qty'] == 0) {
+ continue;
+ }
+ $eftParams['entity_id'] = $ftIds[$value['price_field_value_id']];
+ $entityParams['line_item_amount'] = $value['line_total'];
+ self::createProportionalEntry($entityParams, $eftParams);
+ if (array_key_exists($value['price_field_value_id'], $taxItems)) {
+ $entityParams['line_item_amount'] = $taxItems[$value['price_field_value_id']]['amount'];
+ $eftParams['entity_id'] = $taxItems[$value['price_field_value_id']]['financial_item_id'];
+ self::createProportionalEntry($entityParams, $eftParams);
+ }
+ }
+ }
+
}