26require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
34 public $records = array();
70 public function create($user, $notrigger = 0)
76 if (isset($this->
id)) {
77 $this->
id = (int) $this->
id;
79 if (isset($this->code)) {
80 $this->code = trim($this->code);
82 if (isset($this->label)) {
83 $this->libelle = trim($this->label);
85 if (isset($this->short_label)) {
86 $this->libelle = trim($this->short_label);
88 if (isset($this->unit_type)) {
89 $this->unit_type = trim($this->unit_type);
91 if (isset($this->active)) {
92 $this->active = (int) $this->active;
94 if (isset($this->scale)) {
95 $this->scale = trim($this->scale);
102 $sql =
"INSERT INTO ".$this->db->prefix().
"c_units(";
106 $sql .=
"short_label,";
107 $sql .=
"unit_type,";
109 $sql .=
") VALUES (";
110 $sql .=
" ".(!isset($this->
id) ?
'NULL' :
"'".$this->db->escape($this->
id).
"'").
",";
111 $sql .=
" ".(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
",";
112 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
113 $sql .=
" ".(!isset($this->short_label) ?
'NULL' :
"'".$this->db->escape($this->short_label).
"'").
",";
114 $sql .=
" ".(!isset($this->unit_type) ?
'NULL' :
"'".$this->db->escape($this->unit_type).
"'").
",";
115 $sql .=
" ".(!isset($this->scale) ?
'NULL' :
"'".$this->db->escape($this->scale).
"'");
120 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
121 $resql = $this->db->query($sql);
124 $this->errors[] =
"Error ".$this->db->lasterror();
128 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_units");
133 foreach ($this->errors as $errmsg) {
134 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
135 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
137 $this->db->rollback();
155 public function fetch($id, $code =
'', $short_label =
'', $unit_type =
'')
161 $sql .=
" t.short_label,";
163 $sql .=
" t.unit_type,";
166 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
167 $sql_where = array();
169 $sql_where[] =
" t.rowid = ".((int) $id);
172 $sql_where[] =
" t.unit_type = '".$this->db->escape($unit_type).
"'";
175 $sql_where[] =
" t.code = '".$this->db->escape($code).
"'";
178 $sql_where[] =
" t.short_label = '".$this->db->escape($short_label).
"'";
180 if (count($sql_where) > 0) {
181 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
184 $resql = $this->db->query($sql);
186 if ($this->db->num_rows($resql)) {
187 $obj = $this->db->fetch_object($resql);
189 $this->
id = $obj->rowid;
190 $this->code = $obj->code;
191 $this->label = $obj->label;
192 $this->short_label = $obj->short_label;
193 $this->scale = $obj->scale;
194 $this->unit_type = $obj->unit_type;
195 $this->scale = $obj->scale;
196 $this->active = $obj->active;
198 $this->db->free($resql);
202 $this->error =
"Error ".$this->db->lasterror();
219 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
226 $sql .=
" t.sortorder,";
228 $sql .=
" t.short_label,";
229 $sql .=
" t.unit_type,";
232 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
233 $sql .=
" WHERE 1 = 1";
236 if (is_array($filter)) {
238 if (count($filter) > 0) {
239 foreach ($filter as $key => $value) {
240 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
241 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
242 } elseif (strpos($key,
'date') !==
false) {
243 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
244 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
245 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
247 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
251 if (count($sqlwhere) > 0) {
252 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
262 $this->errors[] = $errormessage;
263 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
267 if (!empty($sortfield)) {
268 $sql .= $this->db->order($sortfield, $sortorder);
270 if (!empty($limit)) {
271 $sql .= $this->db->plimit($limit, $offset);
274 $resql = $this->db->query($sql);
276 $this->records = array();
277 $num = $this->db->num_rows($resql);
279 while ($obj = $this->db->fetch_object($resql)) {
280 $record =
new self($this->db);
282 $record->id = $obj->rowid;
283 $record->code = $obj->code;
284 $record->sortorder = $obj->sortorder;
285 $record->label = $obj->label;
286 $record->short_label = $obj->short_label;
287 $record->unit_type = $obj->unit_type;
288 $record->scale = $obj->scale;
289 $record->active = $obj->active;
291 $this->records[$record->id] = $record;
294 $this->db->free($resql);
296 return $this->records;
298 $this->errors[] =
'Error '.$this->db->lasterror();
299 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
313 public function update($user =
null, $notrigger = 0)
318 if (isset($this->code)) {
319 $this->code = trim($this->code);
321 if (isset($this->sortorder)) {
322 $this->sortorder = trim($this->sortorder);
324 if (isset($this->label)) {
325 $this->libelle = trim($this->label);
327 if (isset($this->short_label)) {
328 $this->libelle = trim($this->short_label);
330 if (isset($this->unit_type)) {
331 $this->libelle = trim($this->unit_type);
333 if (isset($this->scale)) {
334 $this->scale = trim($this->scale);
336 if (isset($this->active)) {
337 $this->active = (int) $this->active;
344 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
345 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
346 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
347 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
348 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
349 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
350 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
351 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
352 $sql .=
" WHERE rowid=".((int) $this->
id);
356 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
357 $resql = $this->db->query($sql);
360 $this->errors[] =
"Error ".$this->db->lasterror();
365 foreach ($this->errors as $errmsg) {
366 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
367 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
369 $this->db->rollback();
385 public function delete($user, $notrigger = 0)
389 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
390 $sql .=
" WHERE rowid=".((int) $this->
id);
394 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
395 $resql = $this->db->query($sql);
398 $this->errors[] =
"Error ".$this->db->lasterror();
403 foreach ($this->errors as $errmsg) {
404 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
405 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
407 $this->db->rollback();
425 if ($mode ==
'short_label' || $mode ==
'code') {
426 return dol_getIdFromCode($this->db, $code,
'c_units', $mode,
'rowid', 0,
" AND unit_type = '".$this->db->escape($unit_type).
"'");
441 $fk_unit = intval($fk_unit);
447 $value = $value * $scaleUnitPow;
448 if ($fk_new_unit != 0) {
451 if (!empty($scaleUnitPow)) {
453 $value = $value / $scaleUnitPow;
456 return round($value, 2);
469 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
471 $resql = $this->db->query($sql);
474 $unit = $this->db->fetch_object($sql);
478 if ($unit->unit_type ==
'time') {
479 return (
float) $unit->scale;
482 return pow($base, (
float) $unit->scale);
Class of dictionary type of thirdparty (used by imports)
getUnitFromCode($code, $mode='code', $unit_type='')
Get unit from code.
unitConverter($value, $fk_unit, $fk_new_unit=0)
Unit converter.
scaleOfUnitPow($id)
Get scale of unit factor.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
fetch($id, $code='', $short_label='', $unit_type='')
Load object in memory from database.
create($user, $notrigger=0)
Create object into database.
__construct($db)
Constructor.
update($user=null, $notrigger=0)
Update object into database.
Parent class of all other dictionary classes.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.