From 07ebcb2331f62b33d9ba130267a8ab0190d06284 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 4 Sep 2020 18:55:51 +1200 Subject: [PATCH] Move tax handling from line item api to BAO to make it available from apiv4 This implementation has some limitations. I only address one in this PR - removing the rounding - as the focus of the PR is the move. The rounding from all save layers was previously removed but it was reverted when it was eroneously believed to have caused a bug. The bug turned out to be https://github.com/civicrm/civicrm-core/pull/18297 --- CRM/Price/BAO/LineItem.php | 8 +++++++- api/v3/LineItem.php | 11 +++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index 0067fef0a6..dca601f200 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -26,7 +26,7 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem { * @param array $params * (reference) an assoc array of name/value pairs. * - * @return \CRM_Price_DAO_LineItem + * @return CRM_Price_BAO_LineItem * * @throws \CiviCRM_API3_Exception * @throws \Exception @@ -53,6 +53,12 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem { } } + $taxRates = CRM_Core_PseudoConstant::getTaxRates(); + if (isset($params['financial_type_id'], $params['line_total'], $taxRates[$params['financial_type_id']])) { + $taxRate = $taxRates[$params['financial_type_id']]; + $params['tax_amount'] = ($taxRate / 100) * $params['line_total']; + } + $lineItemBAO = new CRM_Price_BAO_LineItem(); $lineItemBAO->copyValues($params); diff --git a/api/v3/LineItem.php b/api/v3/LineItem.php index 6e3690388f..b255dc1d06 100644 --- a/api/v3/LineItem.php +++ b/api/v3/LineItem.php @@ -26,16 +26,11 @@ * * @return array * api result array + * + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException */ function civicrm_api3_line_item_create($params) { - // @todo the following line is not really appropriate for the api. The BAO should - // do the work. - $taxRates = CRM_Core_PseudoConstant::getTaxRates(); - if (isset($params['financial_type_id']) && array_key_exists($params['financial_type_id'], $taxRates)) { - $taxRate = $taxRates[$params['financial_type_id']]; - $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($params['line_total'], $taxRate); - $params['tax_amount'] = round($taxAmount['tax_amount'], 2); - } return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'LineItem'); } -- 2.25.1