+ /**
+ * Retrieve Sales Tax Financial Accounts.
+ *
+ *
+ * @return array
+ *
+ */
+ public static function getSalesTaxFinancialAccounts() {
+ $query = "SELECT cfa.id FROM civicrm_entity_financial_account ce
+ INNER JOIN civicrm_financial_account cfa ON ce.financial_account_id = cfa.id
+ WHERE `entity_table` = 'civicrm_financial_type' AND cfa.is_tax = 1 AND ce.account_relationship = %1 GROUP BY cfa.id";
+ $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' "));
+ $queryParams = array(1 => array($accountRel, 'Integer'));
+ $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
+ $financialAccount = array();
+ while ($dao->fetch()) {
+ $financialAccount[$dao->id] = $dao->id;
+ }
+ 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
+ // CRM-20145
+ $eftParams['amount'] = number_format((float)round($paid, 2), 2, '.', '');
+ civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
+ }
+
+ /**
+ * Create array of last financial item id's.
+ *
+ * @param int $contributionId
+ *
+ * @return array
+ */
+ 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);
+ }
+ }
+ }
+