25require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
33 public $records = array();
69 public function create($user, $notrigger = 0)
75 if (isset($this->
id)) {
76 $this->
id = (int) $this->
id;
78 if (isset($this->code)) {
79 $this->code = trim($this->code);
81 if (isset($this->label)) {
82 $this->libelle = trim($this->label);
84 if (isset($this->short_label)) {
85 $this->libelle = trim($this->short_label);
87 if (isset($this->unit_type)) {
88 $this->unit_type = trim($this->unit_type);
90 if (isset($this->active)) {
91 $this->active = trim($this->active);
93 if (isset($this->scale)) {
94 $this->scale = trim($this->scale);
101 $sql =
"INSERT INTO ".$this->db->prefix().
"c_units(";
105 $sql .=
"short_label,";
106 $sql .=
"unit_type,";
108 $sql .=
") VALUES (";
109 $sql .=
" ".(!isset($this->
id) ?
'NULL' :
"'".$this->db->escape($this->
id).
"'").
",";
110 $sql .=
" ".(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
",";
111 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
112 $sql .=
" ".(!isset($this->short_label) ?
'NULL' :
"'".$this->db->escape($this->short_label).
"'").
",";
113 $sql .=
" ".(!isset($this->unit_type) ?
'NULL' :
"'".$this->db->escape($this->unit_type).
"'").
",";
114 $sql .=
" ".(!isset($this->scale) ?
'NULL' :
"'".$this->db->escape($this->scale).
"'");
119 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
120 $resql = $this->db->query($sql);
123 $this->errors[] =
"Error ".$this->db->lasterror();
127 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_units");
132 foreach ($this->errors as $errmsg) {
133 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
134 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
136 $this->db->rollback();
154 public function fetch($id, $code =
'', $short_label =
'', $unit_type =
'')
160 $sql .=
" t.short_label,";
162 $sql .=
" t.unit_type,";
165 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
166 $sql_where = array();
168 $sql_where[] =
" t.rowid = ".((int) $id);
171 $sql_where[] =
" t.unit_type = '".$this->db->escape($unit_type).
"'";
174 $sql_where[] =
" t.code = '".$this->db->escape($code).
"'";
177 $sql_where[] =
" t.short_label = '".$this->db->escape($short_label).
"'";
179 if (count($sql_where) > 0) {
180 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
183 $resql = $this->db->query($sql);
185 if ($this->db->num_rows($resql)) {
186 $obj = $this->db->fetch_object($resql);
188 $this->
id = $obj->rowid;
189 $this->code = $obj->code;
190 $this->label = $obj->label;
191 $this->short_label = $obj->short_label;
192 $this->scale = $obj->scale;
193 $this->unit_type = $obj->unit_type;
194 $this->scale = $obj->scale;
195 $this->active = $obj->active;
197 $this->db->free($resql);
201 $this->error =
"Error ".$this->db->lasterror();
218 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
225 $sql .=
" t.sortorder,";
227 $sql .=
" t.short_label,";
228 $sql .=
" t.unit_type,";
231 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
234 if (count($filter) > 0) {
235 foreach ($filter as $key => $value) {
236 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
237 $sqlwhere[] = $key.
" = ".((int) $value);
238 } elseif (strpos($key,
'date') !==
false) {
239 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
240 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
241 $sqlwhere[] = $key.
" = '".$this->db->escape($value).
"'";
243 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
247 if (count($sqlwhere) > 0) {
248 $sql .=
' WHERE ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
251 if (!empty($sortfield)) {
252 $sql .= $this->db->order($sortfield, $sortorder);
254 if (!empty($limit)) {
255 $sql .= $this->db->plimit($limit, $offset);
258 $resql = $this->db->query($sql);
260 $this->records = array();
261 $num = $this->db->num_rows($resql);
263 while ($obj = $this->db->fetch_object($resql)) {
264 $record =
new self($this->db);
266 $record->id = $obj->rowid;
267 $record->code = $obj->code;
268 $record->sortorder = $obj->sortorder;
269 $record->label = $obj->label;
270 $record->short_label = $obj->short_label;
271 $record->unit_type = $obj->unit_type;
272 $record->scale = $obj->scale;
273 $record->active = $obj->active;
275 $this->records[$record->id] = $record;
278 $this->db->free($resql);
280 return $this->records;
282 $this->errors[] =
'Error '.$this->db->lasterror();
283 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
297 public function update($user =
null, $notrigger = 0)
302 if (isset($this->code)) {
303 $this->code = trim($this->code);
305 if (isset($this->sortorder)) {
306 $this->sortorder = trim($this->sortorder);
308 if (isset($this->label)) {
309 $this->libelle = trim($this->label);
311 if (isset($this->short_label)) {
312 $this->libelle = trim($this->short_label);
314 if (isset($this->unit_type)) {
315 $this->libelle = trim($this->unit_type);
317 if (isset($this->scale)) {
318 $this->scale = trim($this->scale);
320 if (isset($this->active)) {
321 $this->active = trim($this->active);
328 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
329 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
330 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
331 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
332 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
333 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
334 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
335 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
336 $sql .=
" WHERE rowid=".((int) $this->
id);
340 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
341 $resql = $this->db->query($sql);
344 $this->errors[] =
"Error ".$this->db->lasterror();
349 foreach ($this->errors as $errmsg) {
350 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
351 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
353 $this->db->rollback();
369 public function delete($user, $notrigger = 0)
373 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
374 $sql .=
" WHERE rowid=".((int) $this->
id);
378 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
379 $resql = $this->db->query($sql);
382 $this->errors[] =
"Error ".$this->db->lasterror();
387 foreach ($this->errors as $errmsg) {
388 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
389 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
391 $this->db->rollback();
409 if ($mode ==
'short_label') {
410 return dol_getIdFromCode($this->db, $code,
'c_units',
'short_label',
'rowid', 0,
" AND unit_type = '".$this->db->escape($unit_type).
"'");
411 } elseif ($mode ==
'code') {
412 return dol_getIdFromCode($this->db, $code,
'c_units',
'code',
'rowid', 0,
" AND unit_type = '". $this->db->escape($unit_type) .
"'");
428 $fk_unit = intval($fk_unit);
434 $value = $value * $scaleUnitPow;
435 if ($fk_new_unit != 0) {
438 if (!empty($scaleUnitPow)) {
440 $value = $value / $scaleUnitPow;
443 return round($value, 2);
456 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
458 $resql = $this->db->query($sql);
461 $unit = $this->db->fetch_object($sql);
465 if ($unit->unit_type ==
'time') {
466 return (
float) $unit->scale;
469 return pow($base, (
float) $unit->scale);
Class of dictionary type of thirdparty (used by imports)
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
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.
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 '.
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.