From f014ced0d5334043341c194fdaa7ae0f4cc31e0e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 16 Aug 2022 08:02:48 +1000 Subject: [PATCH] Fixes for tax floats with precision greater than 2 --- app/Import/Transformers/BaseTransformer.php | 6 +++ .../Transformers/InvoiceTransformer.php | 6 +-- tests/Unit/NumberTest.php | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index f805010abadc..17daf8105b8e 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -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 * diff --git a/app/Import/Transformers/InvoiceTransformer.php b/app/Import/Transformers/InvoiceTransformer.php index 46f949c1e813..4142c0e544c8 100644 --- a/app/Import/Transformers/InvoiceTransformer.php +++ b/app/Import/Transformers/InvoiceTransformer.php @@ -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'), diff --git a/tests/Unit/NumberTest.php b/tests/Unit/NumberTest.php index 4d5587b0b407..7ed201b8e69c 100644 --- a/tests/Unit/NumberTest.php +++ b/tests/Unit/NumberTest.php @@ -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);