From e7dbc8407f532b8be44163f4490092ca5905157f Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 23 Nov 2020 19:08:26 +0100 Subject: [PATCH 1/3] FIX formating of prices with foreign languages --- htdocs/product/price.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 6fa2a77d5da..48cf4722ae7 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 @@ -278,10 +279,15 @@ 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]; @@ -321,9 +327,9 @@ if (empty($reshook)) } } $pricestoupdate[0] = array( - 'price' => $_POST["price"], - 'price_min' => $_POST["price_min"], - 'price_base_type' => $_POST["price_base_type"], + '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 From c5278197fb9772cdd0f4d5a2f641d9abd7e305a2 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 23 Nov 2020 19:45:11 +0100 Subject: [PATCH 2/3] More phpunit test for price2num --- htdocs/core/lib/functions.lib.php | 9 ++++++-- htdocs/product/price.php | 3 ++- test/phpunit/FunctionsLibTest.php | 35 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) 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; } From 9cb5673c36561aba1a251ee712add0f98199f245 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 23 Nov 2020 19:46:24 +0100 Subject: [PATCH 3/3] Fix remove var_dump --- htdocs/product/price.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/htdocs/product/price.php b/htdocs/product/price.php index d89020b33a3..f93545ee19e 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -279,9 +279,7 @@ 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)'