From e915552ae18882a1c39d9a3252ba77e9d9c688f3 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 18 Apr 2020 11:39:33 +0200 Subject: [PATCH 1/6] New class tool for converting units --- htdocs/core/class/unitsTools.class.php | 119 +++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 htdocs/core/class/unitsTools.class.php diff --git a/htdocs/core/class/unitsTools.class.php b/htdocs/core/class/unitsTools.class.php new file mode 100644 index 00000000000..79ffdb82338 --- /dev/null +++ b/htdocs/core/class/unitsTools.class.php @@ -0,0 +1,119 @@ +unit_type == 'time'){ + return doubleval($unit->scale); + } + + return pow($base, doubleval($unit->scale)); + } + + return 0; + } + + /** + * return first result from query + * @param string $sql the sql query string + * @return bool| var + */ + static public function dbGetvalue($sql) + { + global $db; + $sql .= ' LIMIT 1;'; + + $res = $db->query($sql); + if ($res) + { + $Tresult = $db->fetch_row($res); + return $Tresult[0]; + } + + return false; + } + + /** + * return first result from query + * @param string $sql the sql query string + * @return bool| var + */ + static public function dbGetRow($sql) + { + global $db; + $sql .= ' LIMIT 1;'; + + $res = $db->query($sql); + if ($res) + { + return $db->fetch_object($res); + } + + return false; + } +} From 09591d75f6bfd848363cddd4a4dbf0888bf5ddb9 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 18 Apr 2020 13:20:08 +0200 Subject: [PATCH 2/6] Sql write less do more... --- htdocs/core/class/unitsTools.class.php | 42 ++------------------------ htdocs/core/db/DoliDB.class.php | 38 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/htdocs/core/class/unitsTools.class.php b/htdocs/core/class/unitsTools.class.php index 79ffdb82338..a5bc839da0c 100644 --- a/htdocs/core/class/unitsTools.class.php +++ b/htdocs/core/class/unitsTools.class.php @@ -63,9 +63,10 @@ class UnitsTools */ static public function scaleOfUnitPow($id) { + global $db; $base = 10; // TODO : add base col into unit dictionary table - $unit = self::dbGetRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id)); + $unit = $db->getRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id)); if($unit){ // TODO : if base exist in unit dictionary table remove this convertion exception and update convertion infos in database exemple time hour currently scale 3600 will become scale 2 base 60 if($unit->unit_type == 'time'){ @@ -77,43 +78,4 @@ class UnitsTools return 0; } - - /** - * return first result from query - * @param string $sql the sql query string - * @return bool| var - */ - static public function dbGetvalue($sql) - { - global $db; - $sql .= ' LIMIT 1;'; - - $res = $db->query($sql); - if ($res) - { - $Tresult = $db->fetch_row($res); - return $Tresult[0]; - } - - return false; - } - - /** - * return first result from query - * @param string $sql the sql query string - * @return bool| var - */ - static public function dbGetRow($sql) - { - global $db; - $sql .= ' LIMIT 1;'; - - $res = $db->query($sql); - if ($res) - { - return $db->fetch_object($res); - } - - return false; - } } diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 96e76097877..c38db8f9c99 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -297,4 +297,42 @@ abstract class DoliDB implements Database { return $this->lastqueryerror; } + + + /** + * return first result value from query + * @param string $sql the sql query string + * @return bool| var + */ + public function getvalue($sql) + { + $sql .= ' LIMIT 1;'; + + $res = $this->query($sql); + if ($res) + { + $Tresult = $this->fetch_row($res); + return $Tresult[0]; + } + + return false; + } + + /** + * return first result from query as object + * @param string $sql the sql query string + * @return bool| var + */ + public function getRow($sql) + { + $sql .= ' LIMIT 1;'; + + $res = $this->query($sql); + if ($res) + { + return $this->fetch_object($res); + } + + return false; + } } From 395446a924f6a5ed7d3f3851f6906b40dfe35180 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 18 Apr 2020 13:44:13 +0200 Subject: [PATCH 3/6] Add getRows method and comments --- htdocs/core/db/DoliDB.class.php | 36 +++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index c38db8f9c99..e3e0491b298 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -300,11 +300,13 @@ abstract class DoliDB implements Database /** - * return first result value from query + * Return first result value from query + * Note : This method executes a given SQL query and retrieves the first value of the first row of results. It should only be used with SELECT queries. + * Dont add LIMIT to your query, it will be added by this method * @param string $sql the sql query string * @return bool| var */ - public function getvalue($sql) + public function getValue($sql) { $sql .= ' LIMIT 1;'; @@ -319,9 +321,11 @@ abstract class DoliDB implements Database } /** - * return first result from query as object + * Return first result from query as object + * Note : This method executes a given SQL query and retrieves the first row of results as an object. It should only be used with SELECT queries + * Dont add LIMIT to your query, it will be added by this method * @param string $sql the sql query string - * @return bool| var + * @return bool| object */ public function getRow($sql) { @@ -335,4 +339,28 @@ abstract class DoliDB implements Database return false; } + + /** + * return all results from query as an array of objects + * Note : This method executes a given SQL query and retrieves all row of results as an array of objects. It should only be used with SELECT queries + * be carefull with this method use it only with some limit of results to avoid performences loss + * @param string $sql the sql query string + * @return bool| array + */ + public function getRows($sql) + { + $res = $this->query($sql); + if ($res) + { + $results = array(); + if($this->num_rows($res) > 0){ + while ($obj = $this->fetch_object($res)){ + $results[] = $obj; + } + } + return $results; + } + + return false; + } } From f2435f6247adb16374ff732dd8797e81a335c720 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 18 Apr 2020 16:08:26 +0200 Subject: [PATCH 4/6] Move methods to cunits class --- htdocs/core/class/cunits.class.php | 73 +++++++++++++++++++++++ htdocs/core/class/unitsTools.class.php | 81 -------------------------- 2 files changed, 73 insertions(+), 81 deletions(-) delete mode 100644 htdocs/core/class/unitsTools.class.php diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index 65baf0056e9..6de3af70416 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -386,4 +386,77 @@ class CUnits // extends CommonObject return 1; } } + + + /** + * Get unit from code + * @param string $code code of unit + * @param string $mode 0= id , short_label=Use short label as value, code=use code + * @return int <0 if KO, Id of code if OK + */ + public function getUnitFromCode($code, $mode = 'code') + { + + if($mode == 'short_label'){ + return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid'); + } + elseif($mode == 'code'){ + return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid'); + } + + return $code; + } + + /** + * Unit converter + * @param double $value value to convert + * @param int $fk_unit current unit id of value + * @param int $fk_new_unit the id of unit to convert in + * @return double + */ + public function unitConverter($value, $fk_unit, $fk_new_unit = 0) + { + $value = doubleval(price2num($value)); + $fk_unit = intval($fk_unit); + + // Calcul en unité de base + $scaleUnitPow = $this->scaleOfUnitPow($fk_unit); + + // convert to standard unit + $value = $value * $scaleUnitPow; + if($fk_new_unit !=0 ){ + // Calcul en unité de base + $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit); + if(!empty($scaleUnitPow)) + { + // convert to new unit + $value = $value / $scaleUnitPow; + } + } + return round($value, 2); + } + + + + /** + * get scale of unit factor + * @param $id int id of unit in dictionary + * @return float|int + */ + public function scaleOfUnitPow($id) + { + $base = 10; + // TODO : add base col into unit dictionary table + $unit = $this->db->getRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id)); + if($unit){ + // TODO : if base exist in unit dictionary table remove this convertion exception and update convertion infos in database exemple time hour currently scale 3600 will become scale 2 base 60 + if($unit->unit_type == 'time'){ + return doubleval($unit->scale); + } + + return pow($base, doubleval($unit->scale)); + } + + return 0; + } } diff --git a/htdocs/core/class/unitsTools.class.php b/htdocs/core/class/unitsTools.class.php deleted file mode 100644 index a5bc839da0c..00000000000 --- a/htdocs/core/class/unitsTools.class.php +++ /dev/null @@ -1,81 +0,0 @@ -getRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id)); - if($unit){ - // TODO : if base exist in unit dictionary table remove this convertion exception and update convertion infos in database exemple time hour currently scale 3600 will become scale 2 base 60 - if($unit->unit_type == 'time'){ - return doubleval($unit->scale); - } - - return pow($base, doubleval($unit->scale)); - } - - return 0; - } -} From e230a3a264ce7e81b0ea61111ebc72161f406a82 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 18 Apr 2020 16:24:12 +0200 Subject: [PATCH 5/6] add num row test --- htdocs/core/db/DoliDB.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index e3e0491b298..3bb5e025c34 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -313,8 +313,10 @@ abstract class DoliDB implements Database $res = $this->query($sql); if ($res) { - $Tresult = $this->fetch_row($res); - return $Tresult[0]; + if($this->num_rows($res) > 0) { + $Tresult = $this->fetch_row($res); + return reset($Tresult); + } } return false; From 3f5270cf3906cc13156addc507fe688dc5451007 Mon Sep 17 00:00:00 2001 From: ATM john Date: Wed, 22 Apr 2020 23:12:15 +0200 Subject: [PATCH 6/6] Remove getValue Method : usage of fetch_row is deprecated --- htdocs/core/db/DoliDB.class.php | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 3bb5e025c34..2818eff65e2 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -298,30 +298,6 @@ abstract class DoliDB implements Database return $this->lastqueryerror; } - - /** - * Return first result value from query - * Note : This method executes a given SQL query and retrieves the first value of the first row of results. It should only be used with SELECT queries. - * Dont add LIMIT to your query, it will be added by this method - * @param string $sql the sql query string - * @return bool| var - */ - public function getValue($sql) - { - $sql .= ' LIMIT 1;'; - - $res = $this->query($sql); - if ($res) - { - if($this->num_rows($res) > 0) { - $Tresult = $this->fetch_row($res); - return reset($Tresult); - } - } - - return false; - } - /** * Return first result from query as object * Note : This method executes a given SQL query and retrieves the first row of results as an object. It should only be used with SELECT queries