27require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
38 public $records = array();
89 public function create($user, $notrigger = 0)
95 if (isset($this->
id)) {
96 $this->
id = (int) $this->
id;
98 if (isset($this->code)) {
99 $this->code = trim($this->code);
101 if (isset($this->label)) {
102 $this->libelle = trim($this->label);
104 if (isset($this->short_label)) {
105 $this->libelle = trim($this->short_label);
107 if (isset($this->unit_type)) {
108 $this->unit_type = trim($this->unit_type);
110 if (isset($this->active)) {
111 $this->active = (int) $this->active;
113 if (isset($this->scale)) {
114 $this->scale = (int) $this->scale;
121 $sql =
"INSERT INTO ".$this->db->prefix().
"c_units(";
125 $sql .=
"short_label,";
126 $sql .=
"unit_type,";
128 $sql .=
") VALUES (";
129 $sql .=
" ".(!isset($this->
id) ?
'NULL' :
"'".$this->db->escape($this->
id).
"'").
",";
130 $sql .=
" ".(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
",";
131 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
132 $sql .=
" ".(!isset($this->short_label) ?
'NULL' :
"'".$this->db->escape($this->short_label).
"'").
",";
133 $sql .=
" ".(!isset($this->unit_type) ?
'NULL' :
"'".$this->db->escape($this->unit_type).
"'").
",";
134 $sql .=
" ".(!isset($this->scale) ?
'NULL' : (int) $this->scale);
139 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
140 $resql = $this->db->query($sql);
143 $this->errors[] =
"Error ".$this->db->lasterror();
147 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_units");
152 foreach ($this->errors as $errmsg) {
153 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
154 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
156 $this->db->rollback();
174 public function fetch($id, $code =
'', $short_label =
'', $unit_type =
'')
180 $sql .=
" t.short_label,";
182 $sql .=
" t.unit_type,";
184 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
185 $sql_where = array();
187 $sql_where[] =
" t.rowid = ".((int) $id);
190 $sql_where[] =
" t.unit_type = '".$this->db->escape($unit_type).
"'";
193 $sql_where[] =
" t.code = '".$this->db->escape($code).
"'";
196 $sql_where[] =
" t.short_label = '".$this->db->escape($short_label).
"'";
198 if (count($sql_where) > 0) {
199 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
202 $resql = $this->db->query($sql);
204 if ($this->db->num_rows($resql)) {
205 $obj = $this->db->fetch_object($resql);
207 $this->
id = (int) $obj->rowid;
208 $this->code = $obj->code;
209 $this->label = $obj->label;
210 $this->short_label = $obj->short_label;
211 $this->scale = $obj->scale ? (int) $obj->scale :
null;
212 $this->unit_type = $obj->unit_type;
213 $this->active = (int) $obj->active;
215 $this->db->free($resql);
219 $this->error =
"Error ".$this->db->lasterror();
236 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
243 $sql .=
" t.sortorder,";
245 $sql .=
" t.short_label,";
246 $sql .=
" t.unit_type,";
249 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
250 $sql .=
" WHERE 1 = 1";
253 if (is_array($filter)) {
255 if (count($filter) > 0) {
256 foreach ($filter as $key => $value) {
257 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
258 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
259 } elseif (strpos($key,
'date') !==
false) {
260 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
261 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
262 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
264 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
268 if (count($sqlwhere) > 0) {
269 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
279 $this->errors[] = $errormessage;
280 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
284 if (!empty($sortfield)) {
285 $sql .= $this->db->order($sortfield, $sortorder);
287 if (!empty($limit)) {
288 $sql .= $this->db->plimit($limit, $offset);
291 $resql = $this->db->query($sql);
293 $this->records = array();
294 $num = $this->db->num_rows($resql);
296 while ($obj = $this->db->fetch_object($resql)) {
297 $record =
new self($this->db);
299 $record->id = (int) $obj->rowid;
300 $record->code = $obj->code;
301 $record->sortorder = $obj->sortorder;
302 $record->label = $obj->label;
303 $record->short_label = $obj->short_label;
304 $record->unit_type = $obj->unit_type;
305 $record->scale = $obj->scale ? (int) $obj->scale :
null;
306 $record->active = (int) $obj->active;
308 $this->records[$record->id] = $record;
311 $this->db->free($resql);
313 return $this->records;
315 $this->errors[] =
'Error '.$this->db->lasterror();
316 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
330 public function update($user =
null, $notrigger = 0)
335 if (isset($this->code)) {
336 $this->code = trim($this->code);
338 if (isset($this->sortorder)) {
339 $this->sortorder = trim($this->sortorder);
341 if (isset($this->label)) {
342 $this->libelle = trim($this->label);
344 if (isset($this->short_label)) {
345 $this->libelle = trim($this->short_label);
347 if (isset($this->unit_type)) {
348 $this->libelle = trim($this->unit_type);
350 if (isset($this->scale)) {
351 $this->scale = (int) $this->scale;
353 if (isset($this->active)) {
354 $this->active = (int) $this->active;
361 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
362 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
363 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
364 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
365 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
366 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
367 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
368 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
369 $sql .=
" WHERE rowid=".((int) $this->
id);
373 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
374 $resql = $this->db->query($sql);
377 $this->errors[] =
"Error ".$this->db->lasterror();
382 foreach ($this->errors as $errmsg) {
383 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
384 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
386 $this->db->rollback();
402 public function delete($user, $notrigger = 0)
406 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
407 $sql .=
" WHERE rowid=".((int) $this->
id);
411 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
412 $resql = $this->db->query($sql);
415 $this->errors[] =
"Error ".$this->db->lasterror();
420 foreach ($this->errors as $errmsg) {
421 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
422 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
424 $this->db->rollback();
442 if ($mode ==
'short_label' || $mode ==
'code') {
443 return dol_getIdFromCode($this->db, $code,
'c_units', $mode,
'rowid', 0,
" AND unit_type = '".$this->db->escape($unit_type).
"'");
458 $fk_unit = intval($fk_unit);
464 $value *= $scaleUnitPow;
465 if ($fk_new_unit != 0) {
468 if (!empty($scaleUnitPow)) {
470 $value /= $scaleUnitPow;
473 return round($value, 2);
486 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
488 $resql = $this->db->query($sql);
491 $unit = $this->db->fetch_object($sql);
495 if ($unit->unit_type ==
'time') {
496 return (
float) $unit->scale;
499 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.