Updates for parseformat

This commit is contained in:
David Bomba 2024-02-29 13:06:11 +11:00
parent f325aecaed
commit c4807be9df
3 changed files with 55 additions and 6 deletions

View File

@ -211,6 +211,7 @@ class Document extends BaseModel
public function link() public function link()
{ {
$entity_id = $this->encodePrimaryKey($this->documentable_id); $entity_id = $this->encodePrimaryKey($this->documentable_id);
$link = '';
match($this->documentable_type) { match($this->documentable_type) {
'App\Models\Vendor' => $link = "vendors/{$entity_id}", '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\Payment' => $link = "payments/{$entity_id}/edit",
'App\Models\Task' => $link = "tasks/{$entity_id}/edit", 'App\Models\Task' => $link = "tasks/{$entity_id}/edit",
'App\Models\Client' => $link = "clients/{$entity_id}", 'App\Models\Client' => $link = "clients/{$entity_id}",
default => $link = '' default => $link = '',
}; };
return $link; return $link;

View File

@ -93,7 +93,7 @@ class Number
* @param string $value The formatted number to be converted back to float * @param string $value The formatted number to be converted back to float
* @return float The formatted value * @return float The formatted value
*/ */
public static function parseFloat($value) public static function parseFloat2($value)
{ {
if(!$value) if(!$value)
return 0; return 0;
@ -104,7 +104,7 @@ class Number
$decimal = strpos($value, '.'); $decimal = strpos($value, '.');
$comma = 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; return (float) $value;
if($decimal < $comma){ //decimal before a comma = euro if($decimal < $comma){ //decimal before a comma = euro
@ -143,9 +143,9 @@ class Number
// return (float) $s; // return (float) $s;
} }
/*
//next iteration of float parsing //next iteration of float parsing
public static function parseFloatv2($value) public static function parseFloat($value)
{ {
if(!$value) { if(!$value) {
@ -187,7 +187,8 @@ class Number
return (float)$value; return (float)$value;
} }
*/
public static function parseStringFloat($value) public static function parseStringFloat($value)
{ {
$value = preg_replace('/[^0-9-.]+/', '', $value); $value = preg_replace('/[^0-9-.]+/', '', $value);

View File

@ -20,6 +20,53 @@ use Tests\TestCase;
*/ */
class NumberTest extends 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() public function testNegativeFloatParse()
{ {