Skip to content

Commit

Permalink
Move tax handling from line item api to BAO to make it available from…
Browse files Browse the repository at this point in the history
… 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 civicrm#18297
  • Loading branch information
eileenmcnaughton committed Sep 4, 2020
1 parent a3ae022 commit 07ebcb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CRM/Price/BAO/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,6 +53,12 @@ public static function create(&$params) {
}
}

$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);

Expand Down
11 changes: 3 additions & 8 deletions api/v3/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit 07ebcb2

Please sign in to comment.