diff --git a/htdocs/core/class/cunits.class.php b/htdocs/core/class/cunits.class.php index a25250516f4..096ae47a7b8 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/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 96e76097877..2818eff65e2 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -297,4 +297,48 @@ abstract class DoliDB implements Database { return $this->lastqueryerror; } + + /** + * 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| object + */ + public function getRow($sql) + { + $sql .= ' LIMIT 1;'; + + $res = $this->query($sql); + if ($res) + { + return $this->fetch_object($res); + } + + 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; + } }