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;
217 $this->scale = isset($obj->scale) ? (int) $obj->scale :
null;
218 $this->unit_type = $obj->unit_type;
219 $this->active = (int) $obj->active;
221 $this->db->free($resql);
225 $this->error =
"Error ".$this->db->lasterror();
242 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
249 $sql .=
" t.sortorder,";
251 $sql .=
" t.short_label,";
252 $sql .=
" t.unit_type,";
255 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
256 $sql .=
" WHERE 1 = 1";
259 if (is_array($filter)) {
261 if (count($filter) > 0) {
262 foreach ($filter as $key => $value) {
263 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
264 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
265 } elseif (strpos($key,
'date') !==
false) {
266 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
267 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
268 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
270 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
274 if (count($sqlwhere) > 0) {
275 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
285 $this->errors[] = $errormessage;
286 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
290 if (!empty($sortfield)) {
291 $sql .= $this->db->order($sortfield, $sortorder);
293 if (!empty($limit)) {
294 $sql .= $this->db->plimit($limit, $offset);
297 $resql = $this->db->query($sql);
299 $this->records = array();
300 $num = $this->db->num_rows($resql);
302 while ($obj = $this->db->fetch_object($resql)) {
303 $record =
new self($this->db);
305 $record->id = (int) $obj->rowid;
306 $record->code = $obj->code;
307 $record->sortorder = $obj->sortorder;
308 $record->label = $obj->label;
309 $record->short_label = $obj->short_label;
310 $record->unit_type = $obj->unit_type;
312 $record->scale = isset($obj->scale) ? (int) $obj->scale :
null;
313 $record->active = (int) $obj->active;
315 $this->records[$record->id] = $record;
318 $this->db->free($resql);
320 return $this->records;
322 $this->errors[] =
'Error '.$this->db->lasterror();
323 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
337 public function update($user =
null, $notrigger = 0)
342 if (isset($this->code)) {
343 $this->code = trim($this->code);
345 if (isset($this->sortorder)) {
346 $this->sortorder = trim($this->sortorder);
348 if (isset($this->label)) {
349 $this->libelle = trim($this->label);
351 if (isset($this->short_label)) {
352 $this->libelle = trim($this->short_label);
354 if (isset($this->unit_type)) {
355 $this->libelle = trim($this->unit_type);
357 if (isset($this->scale)) {
358 $this->scale = (int) $this->scale;
360 if (isset($this->active)) {
361 $this->active = (int) $this->active;
368 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
369 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
370 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
371 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
372 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
373 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
374 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
375 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
376 $sql .=
" WHERE rowid=".((int) $this->
id);
380 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
381 $resql = $this->db->query($sql);
384 $this->errors[] =
"Error ".$this->db->lasterror();
389 foreach ($this->errors as $errmsg) {
390 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
391 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
393 $this->db->rollback();
409 public function delete($user, $notrigger = 0)
413 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
414 $sql .=
" WHERE rowid=".((int) $this->
id);
418 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
419 $resql = $this->db->query($sql);
422 $this->errors[] =
"Error ".$this->db->lasterror();
427 foreach ($this->errors as $errmsg) {
428 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
429 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
431 $this->db->rollback();
449 if ($mode ==
'short_label' || $mode ==
'code') {
450 return dol_getIdFromCode($this->db, $code,
'c_units', $mode,
'rowid', 0,
" AND unit_type = '".$this->db->escape($unit_type).
"'");
465 $fk_unit = intval($fk_unit);
471 $value *= $scaleUnitPow;
472 if ($fk_new_unit != 0) {
475 if (!empty($scaleUnitPow)) {
477 $value /= $scaleUnitPow;
480 return round($value, 2);
493 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
495 $resql = $this->db->query($sql);
498 $unit = $this->db->fetch_object($sql);
502 if ($unit->unit_type ==
'time') {
503 return (
float) $unit->scale;
506 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.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.