From 1c14c6106470407a5d004d8af146285b0f0fd99a Mon Sep 17 00:00:00 2001 From: atm-lena Date: Thu, 15 Sep 2022 11:55:08 +0200 Subject: [PATCH 1/5] Function getUnitFromCode : add filter by type of unit --- htdocs/core/class/cunits.class.php | 7 ++++--- htdocs/core/lib/functions.lib.php | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index dfc22fd37ec..6aea69e8a8a 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -420,15 +420,16 @@ class CUnits // extends CommonObject * 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 + * @param string $unit_type weight,size,surface,volume,qty,time... * @return int <0 if KO, Id of code if OK */ - public function getUnitFromCode($code, $mode = 'code') + public function getUnitFromCode($code, $mode = 'code', $unit_type = '') { if ($mode == 'short_label') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid', 0, ' AND unit_type = "'.$this->db->escape($unit_type).'"'); } elseif ($mode == 'code') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid',0, ' AND unit_type = "'. $this->db->escape($unit_type) .'"'); } return $code; diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index e6c69cadc96..280b723df12 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8498,7 +8498,7 @@ function dol_osencode($str) * @return int <0 if KO, Id of code if OK * @see $langs->getLabelFromKey */ -function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0) +function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0, $filters = '') { global $cache_codes; @@ -8520,6 +8520,9 @@ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = if (!empty($entityfilter)) { $sql .= " AND entity IN (".getEntity($tablename).")"; } + if ($filters) { + $sql .= $filters; + } $resql = $db->query($sql); if ($resql) { From 91bde420d4361b8f19570fe1580b0016f0c4a938 Mon Sep 17 00:00:00 2001 From: atm-lena Date: Thu, 15 Sep 2022 11:57:02 +0200 Subject: [PATCH 2/5] Clean --- htdocs/core/class/cunits.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index 6aea69e8a8a..7259592ed41 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -420,7 +420,7 @@ class CUnits // extends CommonObject * 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 - * @param string $unit_type weight,size,surface,volume,qty,time... + * @param string $unit_type weight,size,surface,volume,qty,time... * @return int <0 if KO, Id of code if OK */ public function getUnitFromCode($code, $mode = 'code', $unit_type = '') From f273dd1711a09ae96d76c835fda20ebd86a876d3 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 20 Sep 2022 08:35:22 +0000 Subject: [PATCH 3/5] Fixing style errors. --- htdocs/core/class/cunits.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index 7259592ed41..102bbbda652 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -429,7 +429,7 @@ class CUnits // extends CommonObject if ($mode == 'short_label') { return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid', 0, ' AND unit_type = "'.$this->db->escape($unit_type).'"'); } elseif ($mode == 'code') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid',0, ' AND unit_type = "'. $this->db->escape($unit_type) .'"'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid', 0, ' AND unit_type = "'. $this->db->escape($unit_type) .'"'); } return $code; From 8a032a560a76c36921d84867e36eb28bd1b40695 Mon Sep 17 00:00:00 2001 From: atm-lena Date: Tue, 20 Sep 2022 15:40:55 +0200 Subject: [PATCH 4/5] FIX --- htdocs/core/class/cunits.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index 7259592ed41..477b6796a3c 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -427,9 +427,9 @@ class CUnits // extends CommonObject { if ($mode == 'short_label') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid', 0, ' AND unit_type = "'.$this->db->escape($unit_type).'"'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid', 0, " AND unit_type = '".$this->db->escape($unit_type)."'"); } elseif ($mode == 'code') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid',0, ' AND unit_type = "'. $this->db->escape($unit_type) .'"'); + return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid',0, " AND unit_type = '". $this->db->escape($unit_type) ."'"); } return $code; From c1e6ac6326764406aefaa13673a1bce1fe3d7a2d Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 20 Sep 2022 13:47:24 +0000 Subject: [PATCH 5/5] Fixing style errors. --- htdocs/core/class/cunits.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index 477b6796a3c..4afd59b5780 100644 --- a/htdocs/core/class/cunits.class.php +++ b/htdocs/core/class/cunits.class.php @@ -429,7 +429,7 @@ class CUnits // extends CommonObject if ($mode == 'short_label') { return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid', 0, " AND unit_type = '".$this->db->escape($unit_type)."'"); } elseif ($mode == 'code') { - return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid',0, " AND unit_type = '". $this->db->escape($unit_type) ."'"); + return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid', 0, " AND unit_type = '". $this->db->escape($unit_type) ."'"); } return $code;