From c4807be9dfb1c3ca4fcabf906e57d8d4478af71e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 29 Feb 2024 13:06:11 +1100 Subject: [PATCH] Updates for parseformat --- app/Models/Document.php | 3 ++- app/Utils/Number.php | 11 ++++----- tests/Unit/NumberTest.php | 47 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/Models/Document.php b/app/Models/Document.php index 8b2a45f7a508..53be87341a4d 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -211,6 +211,7 @@ class Document extends BaseModel public function link() { $entity_id = $this->encodePrimaryKey($this->documentable_id); + $link = ''; match($this->documentable_type) { 'App\Models\Vendor' => $link = "vendors/{$entity_id}", @@ -222,7 +223,7 @@ class Document extends BaseModel 'App\Models\Payment' => $link = "payments/{$entity_id}/edit", 'App\Models\Task' => $link = "tasks/{$entity_id}/edit", 'App\Models\Client' => $link = "clients/{$entity_id}", - default => $link = '' + default => $link = '', }; return $link; diff --git a/app/Utils/Number.php b/app/Utils/Number.php index 6a10998d3538..53e030093c2c 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -93,7 +93,7 @@ class Number * @param string $value The formatted number to be converted back to float * @return float The formatted value */ - public static function parseFloat($value) + public static function parseFloat2($value) { if(!$value) return 0; @@ -104,7 +104,7 @@ class Number $decimal = strpos($value, '.'); $comma = strpos($value, ','); - if(!$comma) //no comma must be a decimal number already + if($comma === false) //no comma must be a decimal number already return (float) $value; if($decimal < $comma){ //decimal before a comma = euro @@ -143,9 +143,9 @@ class Number // return (float) $s; } - /* + //next iteration of float parsing - public static function parseFloatv2($value) + public static function parseFloat($value) { if(!$value) { @@ -187,7 +187,8 @@ class Number return (float)$value; } - */ + + public static function parseStringFloat($value) { $value = preg_replace('/[^0-9-.]+/', '', $value); diff --git a/tests/Unit/NumberTest.php b/tests/Unit/NumberTest.php index feecae8abf7d..538a48e338e4 100644 --- a/tests/Unit/NumberTest.php +++ b/tests/Unit/NumberTest.php @@ -20,6 +20,53 @@ use Tests\TestCase; */ class NumberTest extends TestCase { + + public function testRangeOfNumberFormats() + { + + + $floatvals = [ + "22000.76" =>"22 000,76", + "22000.76" =>"22.000,76", + "22000.76" =>"22,000.76", + "22000" =>"22 000", + "22000" =>"22,000", + "22000" =>"22.000", + "22000.76" =>"22000.76", + "22000.76" =>"22000,76", + "1022000.76" =>"1.022.000,76", + "1022000.76" =>"1,022,000.76", + "1000000" =>"1,000,000", + "1000000" =>"1.000.000", + "1022000.76" =>"1022000.76", + "1022000.76" =>"1022000,76", + "1022000" =>"1022000", + "0.76" =>"0.76", + "0.76" =>"0,76", + "0" =>"0.00", + "0" =>"0,00", + "1" =>"1.00", + "1" =>"1,00", + "423545" =>"423545 €", + "423545" =>"423,545 €", + "423545" =>"423.545 €", + "1" =>"1,00 €", + "1.02" =>"€ 1.02", + "1000.02" =>"1'000,02 EUR", + "1000.02" =>"1 000.02$", + "1000.02" =>"1,000.02$", + "1000.02" =>"1.000,02 EURO" + ]; + + + foreach($floatvals as $key => $value) { + + $this->assertEquals($key, Number::parseFloat($value)); + + } + + } + public function testNegativeFloatParse() {