diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 1e6ef8d6ebe..efeed543865 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -4726,6 +4726,9 @@ function price2num($amount, $rounding = '', $alreadysqlnb = 0) // Convert amount to format with dolibarr dec and thousand (this is because PHP convert a number // to format defined by LC_NUMERIC after a calculation and we want source format to be like defined by Dolibarr setup. + if ($thousand == '.') { + $amount = str_replace($thousand, '', $amount); // Replace of thousand before test of is_numeric to avoid pb if thousand is . + } if (is_numeric($amount)) { // We put in temps value of decimal ("0.00001"). Works with 0 and 2.0E-5 and 9999.10 @@ -4735,9 +4738,11 @@ function price2num($amount, $rounding = '', $alreadysqlnb = 0) $amount = number_format($amount, $nbofdec, $dec, $thousand); } //print "QQ".$amount.'
'; - + // Now make replace (the main goal of function) - if ($thousand != ',' && $thousand != '.') $amount = str_replace(',', '.', $amount); // To accept 2 notations for french users + if ($thousand != ',' && $thousand != '.') { + $amount = str_replace(',', '.', $amount); // To accept 2 notations for french users + } $amount = str_replace(' ', '', $amount); // To avoid spaces $amount = str_replace($thousand, '', $amount); // Replace of thousand before replace of dec to avoid pb if thousand is . $amount = str_replace($dec, '.', $amount); diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 48cf4722ae7..d89020b33a3 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -279,10 +279,11 @@ if (empty($reshook)) } elseif (!$error) { + var_dump(GETPOST('price', 'alpha')); $newprice = price2num(GETPOST('price', 'alpha')); + var_dump($newprice);exit; $newprice_min = price2num(GETPOST('price_min', 'alpha')); $newpricebase = GETPOST('price_base_type', 'alpha'); - $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)' $tva_tx = $tva_tx_txt; diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index e3dabff6df9..0fc2583178c 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -1225,6 +1225,15 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase */ public function testDolPrice2Num() { + global $langs, $conf; + + $oldlangs = $langs; + + $newlangs = new Translate('', $conf); + $newlangs->setDefaultLang('en_US'); + $newlangs->load("main"); + $langs = $newlangs; + $this->assertEquals(1000, price2num('1 000.0')); $this->assertEquals(1000, price2num('1 000', 'MT')); $this->assertEquals(1000, price2num('1 000', 'MU')); @@ -1239,10 +1248,36 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $this->assertEquals(1000.13, price2num('1 000.125456', 'MT')); $this->assertEquals(1000.12546, price2num('1 000.125456', 'MU'), "Test MU"); + $this->assertEquals(1, price2num('1.000'), 'Test 1.000 give 1 with english language'); + // Text can't be converted $this->assertEquals('12.4$', price2num('12.4$')); $this->assertEquals('12r.4$', price2num('12r.4$')); + // For spanish language + $newlangs2 = new Translate('', $conf); + $newlangs2->setDefaultLang('es_ES'); + $newlangs2->load("main"); + $langs = $newlangs2; + + $this->assertEquals(1000, price2num('1.000'), 'Test 1.000 give 1000 with spanish language'); + $this->assertEquals(1000, price2num('1 000'), 'Test 1 000 give 1000 with spanish language'); + $this->assertEquals(1234, price2num('1.234'), 'Test 1.234 give 1234 with spanish language'); + $this->assertEquals(1.234, price2num('1,234'), 'Test 1,234 give 1.234 with spanish language'); + + // For french language + $newlangs3 = new Translate('', $conf); + $newlangs3->setDefaultLang('fr_FR'); + $newlangs3->load("main"); + $langs = $newlangs3; + + $this->assertEquals(1, price2num('1.000'), 'Test 1.000 give 1 with french language'); + $this->assertEquals(1000, price2num('1 000'), 'Test 1.000 give 1 with french language'); + $this->assertEquals(1.234, price2num('1.234'), 'Test 1.234 give 1.234 with french language'); + $this->assertEquals(1.234, price2num('1,234'), 'Test 1,234 give 1.234 with french language'); + + $langs = $oldlangs; + return true; }