26require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
37 public $records = array();
85 public function create($user, $notrigger = 0)
91 if (isset($this->
id)) {
92 $this->
id = (int) $this->
id;
94 if (isset($this->code)) {
95 $this->code = trim($this->code);
97 if (isset($this->label)) {
98 $this->libelle = trim($this->label);
100 if (isset($this->short_label)) {
101 $this->libelle = trim($this->short_label);
103 if (isset($this->unit_type)) {
104 $this->unit_type = trim($this->unit_type);
106 if (isset($this->active)) {
107 $this->active = (int) $this->active;
109 if (isset($this->scale)) {
110 $this->scale = trim($this->scale);
117 $sql =
"INSERT INTO ".$this->db->prefix().
"c_units(";
121 $sql .=
"short_label,";
122 $sql .=
"unit_type,";
124 $sql .=
") VALUES (";
125 $sql .=
" ".(!isset($this->
id) ?
'NULL' :
"'".$this->db->escape($this->
id).
"'").
",";
126 $sql .=
" ".(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
",";
127 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
128 $sql .=
" ".(!isset($this->short_label) ?
'NULL' :
"'".$this->db->escape($this->short_label).
"'").
",";
129 $sql .=
" ".(!isset($this->unit_type) ?
'NULL' :
"'".$this->db->escape($this->unit_type).
"'").
",";
130 $sql .=
" ".(!isset($this->scale) ?
'NULL' :
"'".$this->db->escape($this->scale).
"'");
135 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
136 $resql = $this->db->query($sql);
139 $this->errors[] =
"Error ".$this->db->lasterror();
143 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_units");
148 foreach ($this->errors as $errmsg) {
149 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
150 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
152 $this->db->rollback();
170 public function fetch($id, $code =
'', $short_label =
'', $unit_type =
'')
176 $sql .=
" t.short_label,";
178 $sql .=
" t.unit_type,";
181 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
182 $sql_where = array();
184 $sql_where[] =
" t.rowid = ".((int) $id);
187 $sql_where[] =
" t.unit_type = '".$this->db->escape($unit_type).
"'";
190 $sql_where[] =
" t.code = '".$this->db->escape($code).
"'";
193 $sql_where[] =
" t.short_label = '".$this->db->escape($short_label).
"'";
195 if (count($sql_where) > 0) {
196 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
199 $resql = $this->db->query($sql);
201 if ($this->db->num_rows($resql)) {
202 $obj = $this->db->fetch_object($resql);
204 $this->
id = $obj->rowid;
205 $this->code = $obj->code;
206 $this->label = $obj->label;
207 $this->short_label = $obj->short_label;
208 $this->scale = $obj->scale;
209 $this->unit_type = $obj->unit_type;
210 $this->scale = $obj->scale;
211 $this->active = $obj->active;
213 $this->db->free($resql);
217 $this->error =
"Error ".$this->db->lasterror();
234 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
241 $sql .=
" t.sortorder,";
243 $sql .=
" t.short_label,";
244 $sql .=
" t.unit_type,";
247 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
248 $sql .=
" WHERE 1 = 1";
251 if (is_array($filter)) {
253 if (count($filter) > 0) {
254 foreach ($filter as $key => $value) {
255 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
256 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
257 } elseif (strpos($key,
'date') !==
false) {
258 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
259 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
260 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
262 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
266 if (count($sqlwhere) > 0) {
267 $sql .=
' AND ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
277 $this->errors[] = $errormessage;
278 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
282 if (!empty($sortfield)) {
283 $sql .= $this->db->order($sortfield, $sortorder);
285 if (!empty($limit)) {
286 $sql .= $this->db->plimit($limit, $offset);
289 $resql = $this->db->query($sql);
291 $this->records = array();
292 $num = $this->db->num_rows($resql);
294 while ($obj = $this->db->fetch_object($resql)) {
295 $record =
new self($this->db);
297 $record->id = $obj->rowid;
298 $record->code = $obj->code;
299 $record->sortorder = $obj->sortorder;
300 $record->label = $obj->label;
301 $record->short_label = $obj->short_label;
302 $record->unit_type = $obj->unit_type;
303 $record->scale = $obj->scale;
304 $record->active = $obj->active;
306 $this->records[$record->id] = $record;
309 $this->db->free($resql);
311 return $this->records;
313 $this->errors[] =
'Error '.$this->db->lasterror();
314 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
328 public function update($user =
null, $notrigger = 0)
333 if (isset($this->code)) {
334 $this->code = trim($this->code);
336 if (isset($this->sortorder)) {
337 $this->sortorder = trim($this->sortorder);
339 if (isset($this->label)) {
340 $this->libelle = trim($this->label);
342 if (isset($this->short_label)) {
343 $this->libelle = trim($this->short_label);
345 if (isset($this->unit_type)) {
346 $this->libelle = trim($this->unit_type);
348 if (isset($this->scale)) {
349 $this->scale = trim($this->scale);
351 if (isset($this->active)) {
352 $this->active = (int) $this->active;
359 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
360 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
361 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
362 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
363 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
364 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
365 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
366 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
367 $sql .=
" WHERE rowid=".((int) $this->
id);
371 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
372 $resql = $this->db->query($sql);
375 $this->errors[] =
"Error ".$this->db->lasterror();
380 foreach ($this->errors as $errmsg) {
381 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
382 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
384 $this->db->rollback();
400 public function delete($user, $notrigger = 0)
404 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
405 $sql .=
" WHERE rowid=".((int) $this->
id);
409 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
410 $resql = $this->db->query($sql);
413 $this->errors[] =
"Error ".$this->db->lasterror();
418 foreach ($this->errors as $errmsg) {
419 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
420 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
422 $this->db->rollback();
440 if ($mode ==
'short_label' || $mode ==
'code') {
441 return dol_getIdFromCode($this->db, $code,
'c_units', $mode,
'rowid', 0,
" AND unit_type = '".$this->db->escape($unit_type).
"'");
456 $fk_unit = intval($fk_unit);
462 $value *= $scaleUnitPow;
463 if ($fk_new_unit != 0) {
466 if (!empty($scaleUnitPow)) {
468 $value /= $scaleUnitPow;
471 return round($value, 2);
484 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
486 $resql = $this->db->query($sql);
489 $unit = $this->db->fetch_object($sql);
493 if ($unit->unit_type ==
'time') {
494 return (
float) $unit->scale;
497 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.