From b72222c3c72051c705313daafbc22a281cc5a3d4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 19 Oct 2022 09:17:32 +1100 Subject: [PATCH] Fixes for formatting of quantity column --- app/Utils/Number.php | 22 +++++- app/Utils/Traits/MakesInvoiceValues.php | 2 +- tests/Feature/ClientApiTest.php | 86 ++++++++++++++++++++++ tests/Unit/NumberTest.php | 94 ------------------------- 4 files changed, 106 insertions(+), 98 deletions(-) diff --git a/app/Utils/Number.php b/app/Utils/Number.php index e7e0c66673b5..f424dd5c739a 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -54,17 +54,33 @@ class Number * Formats a given value based on the clients currency. * * @param float $value The number to be formatted - * @param object $currency The client currency object * * @return string The formatted value */ - public static function formatValueNoTrailingZeroes($value, $currency) :string + public static function formatValueNoTrailingZeroes($value, $entity) :string { $value = floatval($value); + $currency = $entity->currency(); + $thousand = $currency->thousand_separator; $decimal = $currency->decimal_separator; - $precision = $currency->precision; + // $precision = $currency->precision; + + if ($entity instanceof Company) { + $country = $entity->country(); + } else { + $country = $entity->country; + } + + /* Country settings override client settings */ + if (isset($country->thousand_separator) && strlen($country->thousand_separator) >= 1) { + $thousand = $country->thousand_separator; + } + + if (isset($country->decimal_separator) && strlen($country->decimal_separator) >= 1) { + $decimal = $country->decimal_separator; + } $precision = 10; diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index e9e009a7b989..1c30b237b0fc 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -306,7 +306,7 @@ trait MakesInvoiceValues $data[$key][$table_type.".{$_table_type}4"] = strlen($item->custom_value4) >= 1 ? $helpers->formatCustomFieldValue($this->company->custom_fields, "{$_table_type}4", $item->custom_value4, $entity) : ''; if ($item->quantity > 0 || $item->cost > 0) { - $data[$key][$table_type.'.quantity'] = Number::formatValueNoTrailingZeroes($item->quantity, $entity_currency); + $data[$key][$table_type.'.quantity'] = Number::formatValueNoTrailingZeroes($item->quantity, $entity); $data[$key][$table_type.'.unit_cost'] = Number::formatMoneyNoRounding($item->cost, $entity); diff --git a/tests/Feature/ClientApiTest.php b/tests/Feature/ClientApiTest.php index 6ea5af053c29..aae531575656 100644 --- a/tests/Feature/ClientApiTest.php +++ b/tests/Feature/ClientApiTest.php @@ -12,6 +12,7 @@ namespace Tests\Feature; use App\Models\Country; +use App\Utils\Number; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -401,4 +402,89 @@ class ClientApiTest extends TestCase $response->assertStatus(302); } + + public function testRoundingDecimalsTwo() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.05, $currency); + + $this->assertEquals(0.05, $x); + } + + public function testRoundingDecimalsThree() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.005, $currency); + + $this->assertEquals(0.005, $x); + } + + public function testRoundingDecimalsFour() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.0005, $currency); + + $this->assertEquals(0.0005, $x); + } + + public function testRoundingDecimalsFive() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.00005, $currency); + + $this->assertEquals(0.00005, $x); + } + + public function testRoundingDecimalsSix() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.000005, $currency); + + $this->assertEquals(0.000005, $x); + } + + public function testRoundingDecimalsSeven() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.0000005, $currency); + + $this->assertEquals(0.0000005, $x); + } + + public function testRoundingDecimalsEight() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(0.00000005, $currency); + + $this->assertEquals(0.00000005, $x); + } + + public function testRoundingPositive() + { + $currency = $this->company; + + $x = Number::formatValueNoTrailingZeroes(1.5, $currency); + $this->assertEquals(1.5, $x); + + $x = Number::formatValueNoTrailingZeroes(1.50, $currency); + $this->assertEquals(1.5, $x); + + $x = Number::formatValueNoTrailingZeroes(1.500, $currency); + $this->assertEquals(1.5, $x); + + $x = Number::formatValueNoTrailingZeroes(1.50005, $currency); + $this->assertEquals(1.50005, $x); + + $x = Number::formatValueNoTrailingZeroes(1.50000005, $currency); + $this->assertEquals(1.50000005, $x); + } + + } diff --git a/tests/Unit/NumberTest.php b/tests/Unit/NumberTest.php index 7ed201b8e69c..f76e15685bec 100644 --- a/tests/Unit/NumberTest.php +++ b/tests/Unit/NumberTest.php @@ -89,18 +89,6 @@ class NumberTest extends TestCase $this->assertEquals(2.15, $rounded); } - //this method proved an error! removing this method from production - // public function testImportFloatConversion() - // { - - // $amount = '€7,99'; - - // $converted_amount = Number::parseStringFloat($amount); - - // $this->assertEquals(799, $converted_amount); - - // } - public function testParsingStringCurrency() { $amount = '€7,99'; @@ -110,86 +98,4 @@ class NumberTest extends TestCase $this->assertEquals(7.99, $converted_amount); } - public function testRoundingDecimalsTwo() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.05, $currency); - - $this->assertEquals(0.05, $x); - } - - public function testRoundingDecimalsThree() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.005, $currency); - - $this->assertEquals(0.005, $x); - } - - public function testRoundingDecimalsFour() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.0005, $currency); - - $this->assertEquals(0.0005, $x); - } - - public function testRoundingDecimalsFive() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.00005, $currency); - - $this->assertEquals(0.00005, $x); - } - - public function testRoundingDecimalsSix() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.000005, $currency); - - $this->assertEquals(0.000005, $x); - } - - public function testRoundingDecimalsSeven() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.0000005, $currency); - - $this->assertEquals(0.0000005, $x); - } - - public function testRoundingDecimalsEight() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(0.00000005, $currency); - - $this->assertEquals(0.00000005, $x); - } - - public function testRoundingPositive() - { - $currency = Currency::find(1); - - $x = Number::formatValueNoTrailingZeroes(1.5, $currency); - $this->assertEquals(1.5, $x); - - $x = Number::formatValueNoTrailingZeroes(1.50, $currency); - $this->assertEquals(1.5, $x); - - $x = Number::formatValueNoTrailingZeroes(1.500, $currency); - $this->assertEquals(1.5, $x); - - $x = Number::formatValueNoTrailingZeroes(1.50005, $currency); - $this->assertEquals(1.50005, $x); - - $x = Number::formatValueNoTrailingZeroes(1.50000005, $currency); - $this->assertEquals(1.50000005, $x); - } }