Commit 4f5911bb authored by KarinG's avatar KarinG

Fundamental Fixes for TaxMath Calculations.

parent 2e0b9fbc
...@@ -475,7 +475,8 @@ LIMIT 1 ...@@ -475,7 +475,8 @@ LIMIT 1
*/ */
public static function calculateTaxAmount($amount, $taxRate) { public static function calculateTaxAmount($amount, $taxRate) {
$taxAmount = array(); $taxAmount = array();
$taxAmount['tax_amount'] = round(($taxRate / 100) * CRM_Utils_Rule::cleanMoney($amount), 2); // There can not be any rounding at this stage - as this is prior to quantity multiplication
$taxAmount['tax_amount'] = ($taxRate / 100) * CRM_Utils_Rule::cleanMoney($amount);
return $taxAmount; return $taxAmount;
} }
......
...@@ -298,7 +298,15 @@ WHERE li.contribution_id = %1"; ...@@ -298,7 +298,15 @@ WHERE li.contribution_id = %1";
'tax_amount' => $dao->tax_amount, 'tax_amount' => $dao->tax_amount,
'price_set_id' => $dao->price_set_id, 'price_set_id' => $dao->price_set_id,
); );
$lineItems[$dao->id]['tax_rate'] = CRM_Price_BAO_LineItem::calculateTaxRate($lineItems[$dao->id]); $taxRates = CRM_Core_PseudoConstant::getTaxRates();
if (isset($lineItems[$dao->id]['financial_type_id']) && array_key_exists($lineItems[$dao->id]['financial_type_id'], $taxRates)) {
// We are close to output/display here - so apply some rounding at output/display level - to not show Tax Rate in all 8 decimals
$lineItems[$dao->id]['tax_rate'] = round($taxRates[$lineItems[$dao->id]['financial_type_id']], 3);
}
else {
// There is no Tax Rate associated with this Financial Type
$lineItems[$dao->id]['tax_rate'] = FALSE;
}
$lineItems[$dao->id]['subTotal'] = $lineItems[$dao->id]['qty'] * $lineItems[$dao->id]['unit_price']; $lineItems[$dao->id]['subTotal'] = $lineItems[$dao->id]['qty'] * $lineItems[$dao->id]['unit_price'];
if ($lineItems[$dao->id]['tax_amount'] != '') { if ($lineItems[$dao->id]['tax_amount'] != '') {
$getTaxDetails = TRUE; $getTaxDetails = TRUE;
...@@ -597,26 +605,4 @@ WHERE li.contribution_id = %1"; ...@@ -597,26 +605,4 @@ WHERE li.contribution_id = %1";
} }
} }
/**
* Calculate tax rate in percentage.
*
* @param array $lineItemId
* An assoc array of lineItem.
*
* @return int|void
* tax rate
*/
public static function calculateTaxRate($lineItemId) {
if ($lineItemId['unit_price'] == 0 || $lineItemId['qty'] == 0) {
return FALSE;
}
if ($lineItemId['html_type'] == 'Text') {
$tax = round($lineItemId['tax_amount'] / ($lineItemId['unit_price'] * $lineItemId['qty']) * 100, 2);
}
else {
$tax = round(($lineItemId['tax_amount'] / $lineItemId['unit_price']) * 100, 2);
}
return $tax;
}
} }
...@@ -1666,11 +1666,12 @@ WHERE ps.id = %1 ...@@ -1666,11 +1666,12 @@ WHERE ps.id = %1
* @return array * @return array
*/ */
public static function setLineItem($field, $lineItem, $optionValueId, &$totalTax) { public static function setLineItem($field, $lineItem, $optionValueId, &$totalTax) {
// Here we round - i.e. after multiplying by quantity
if ($field['html_type'] == 'Text') { if ($field['html_type'] == 'Text') {
$taxAmount = $field['options'][$optionValueId]['tax_amount'] * $lineItem[$optionValueId]['qty']; $taxAmount = round($field['options'][$optionValueId]['tax_amount'] * $lineItem[$optionValueId]['qty'], 2);
} }
else { else {
$taxAmount = $field['options'][$optionValueId]['tax_amount']; $taxAmount = round($field['options'][$optionValueId]['tax_amount'], 2);
} }
$taxRate = $field['options'][$optionValueId]['tax_rate']; $taxRate = $field['options'][$optionValueId]['tax_rate'];
$lineItem[$optionValueId]['tax_amount'] = $taxAmount; $lineItem[$optionValueId]['tax_amount'] = $taxAmount;
......
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
{if $getTaxDetails} {if $getTaxDetails}
<td class="right">{$line.line_total|crmMoney}</td> <td class="right">{$line.line_total|crmMoney}</td>
{if $line.tax_rate != "" || $line.tax_amount != ""} {if $line.tax_rate != "" || $line.tax_amount != ""}
<td class="right">{$taxTerm} ({$line.tax_rate|string_format:"%.2f"}%)</td> <td class="right">{$taxTerm} ({$line.tax_rate|string_format:"%.3f"}%)</td>
<td class="right">{$line.tax_amount|crmMoney}</td> <td class="right">{$line.tax_amount|crmMoney}</td>
{else} {else}
<td></td> <td></td>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment