diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 3be41789535..bfa6ec798cd 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -4783,6 +4783,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 @@ -4792,9 +4795,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 928437ce946..2acca68a63f 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -226,6 +226,7 @@ if (empty($reshook)) $tva_tx = $tva_tx_txt; $vatratecode = ''; + $reg = array(); if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg)) { $vat_src_code = $reg[1]; @@ -261,8 +262,8 @@ if (empty($reshook)) } $pricestoupdate[$i] = array( - 'price' => $newprice[$i], - 'price_min' => $newprice_min[$i], + 'price' => price2num($newprice[$i]), + 'price_min' => price2num($newprice_min[$i]), 'price_base_type' => $newpricebase[$i], 'default_vat_code' => $vatratecode, 'vat_tx' => $tva_tx, // default_vat_code should be used in priority in a future @@ -277,10 +278,14 @@ if (empty($reshook)) } } elseif (!$error) { + $newprice = price2num(GETPOST('price', 'alpha')); + $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; $vatratecode = ''; + $reg = array(); if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg)) { $vat_src_code = $reg[1]; @@ -320,10 +325,10 @@ if (empty($reshook)) } } $pricestoupdate[0] = array( - 'price' => $_POST["price"], - 'price_min' => $_POST["price_min"], - 'price_base_type' => $_POST["price_base_type"], - 'default_vat_code' => $vatratecode, + 'price' => $newprice, + 'price_min' => $newprice_min, + 'price_base_type' => $newpricebase, + 'default_vat_code' => $vatratecode, 'vat_tx' => $tva_tx, // default_vat_code should be used in priority in a future 'npr' => $npr, // default_vat_code should be used in priority in a future 'localtaxes_array' => array('0'=>$localtax1_type, '1'=>$localtax1, '2'=>$localtax2_type, '3'=>$localtax2) // default_vat_code should be used in priority in a future diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 2cb4d98610c..e24abcf4c0f 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; }