2
0
forked from Wavyzz/dolibarr

Merge pull request #22281 from atm-lena/16.0_FIX_getunitFromCode_AddFilterType

Function getUnitFromCode : add filter by type of unit
This commit is contained in:
Laurent Destailleur
2022-09-25 14:19:33 +02:00
committed by GitHub
2 changed files with 8 additions and 4 deletions

View File

@@ -420,15 +420,16 @@ class CUnits // extends CommonObject
* Get unit from code * Get unit from code
* @param string $code code of unit * @param string $code code of unit
* @param string $mode 0= id , short_label=Use short label as value, code=use code * @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 * @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') { 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') { } 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; return $code;

View File

@@ -8508,7 +8508,7 @@ function dol_osencode($str)
* @return int <0 if KO, Id of code if OK * @return int <0 if KO, Id of code if OK
* @see $langs->getLabelFromKey * @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; global $cache_codes;
@@ -8530,6 +8530,9 @@ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid =
if (!empty($entityfilter)) { if (!empty($entityfilter)) {
$sql .= " AND entity IN (".getEntity($tablename).")"; $sql .= " AND entity IN (".getEntity($tablename).")";
} }
if ($filters) {
$sql .= $filters;
}
$resql = $db->query($sql); $resql = $db->query($sql);
if ($resql) { if ($resql) {