Fixes for tax floats with precision greater than 2

This commit is contained in:
David Bomba 2022-08-16 08:02:48 +10:00
parent e0c1d0d29e
commit f014ced0d5
3 changed files with 56 additions and 3 deletions

View File

@ -152,6 +152,12 @@ class BaseTransformer
return Number::parseFloat($number);
}
public function getFloatWithSamePrecision($data, $field)
{
$precision = (int) strpos(strrev($data[$field]), ".");
return round($data[$field], $precision);
}
/**
* @param $name
*

View File

@ -39,11 +39,11 @@ class InvoiceTransformer extends BaseTransformer
'is_sent' => $this->getString($data, 'invoice.is_sent'),
'private_notes' => $this->getString($data, 'invoice.private_notes'),
'tax_name1' => $this->getString($data, 'invoice.tax_name1'),
'tax_rate1' => $this->getFloat($data, 'invoice.tax_rate1'),
'tax_rate1' => $this->getFloatWithSamePrecision($data, 'invoice.tax_rate1'),
'tax_name2' => $this->getString($data, 'invoice.tax_name2'),
'tax_rate2' => $this->getFloat($data, 'invoice.tax_rate2'),
'tax_rate2' => $this->getFloatWithSamePrecision($data, 'invoice.tax_rate2'),
'tax_name3' => $this->getString($data, 'invoice.tax_name3'),
'tax_rate3' => $this->getFloat($data, 'invoice.tax_rate3'),
'tax_rate3' => $this->getFloatWithSamePrecision($data, 'invoice.tax_rate3'),
'custom_value1' => $this->getString($data, 'invoice.custom_value1'),
'custom_value2' => $this->getString($data, 'invoice.custom_value2'),
'custom_value3' => $this->getString($data, 'invoice.custom_value3'),

View File

@ -21,6 +21,53 @@ use Tests\TestCase;
*/
class NumberTest extends TestCase
{
public function testFloatPrecision()
{
$value = 1.1;
$precision = (int) strpos(strrev($value), ".");
$result = round($value, $precision);
$this->assertEquals(1.1, $result);
}
public function testFloatPrecision1()
{
$value = "1.1";
$precision = (int) strpos(strrev($value), ".");
$result = round($value, $precision);
$this->assertEquals(1.1, $result);
}
public function testFloatPrecision2()
{
$value = 9.975;
$precision = (int) strpos(strrev($value), ".");
$result = round($value, $precision);
$this->assertEquals(9.975, $result);
}
public function testFloatPrecision3()
{
$value = "9.975";
$precision = (int) strpos(strrev($value), ".");
$result = round($value, $precision);
$this->assertEquals(9.975, $result);
}
public function testRoundingThreeLow()
{
$rounded = Number::roundValue(3.144444444444, 3);