43 public $errors = array();
44 public $records = array();
83 public function create($user, $notrigger = 0)
90 if (isset($this->
id)) {
91 $this->
id = (int) $this->
id;
93 if (isset($this->code)) {
94 $this->code = trim($this->code);
96 if (isset($this->label)) {
97 $this->libelle = trim($this->label);
99 if (isset($this->short_label)) {
100 $this->libelle = trim($this->short_label);
102 if (isset($this->unit_type)) {
103 $this->active = trim($this->unit_type);
105 if (isset($this->active)) {
106 $this->active = trim($this->active);
108 if (isset($this->scale)) {
109 $this->scale = trim($this->scale);
116 $sql =
"INSERT INTO ".$this->db->prefix().
"c_units(";
120 $sql .=
"short_label,";
121 $sql .=
"unit_type,";
123 $sql .=
") VALUES (";
124 $sql .=
" ".(!isset($this->
id) ?
'NULL' :
"'".$this->db->escape($this->
id).
"'").
",";
125 $sql .=
" ".(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
",";
126 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
127 $sql .=
" ".(!isset($this->short_label) ?
'NULL' :
"'".$this->db->escape($this->short_label).
"'").
",";
128 $sql .=
" ".(!isset($this->unit_type) ?
'NULL' :
"'".$this->db->escape($this->unit_type).
"'").
",";
129 $sql .=
" ".(!isset($this->scale) ?
'NULL' :
"'".$this->db->escape($this->scale).
"'");
134 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
135 $resql = $this->db->query($sql);
138 $this->errors[] =
"Error ".$this->db->lasterror();
142 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_units");
147 foreach ($this->errors as $errmsg) {
148 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
149 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
151 $this->db->rollback();
169 public function fetch($id, $code =
'', $short_label =
'', $unit_type =
'')
177 $sql .=
" t.short_label,";
179 $sql .=
" t.unit_type,";
182 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
183 $sql_where = array();
185 $sql_where[] =
" t.rowid = ".((int) $id);
188 $sql_where[] =
" t.unit_type = '".$this->db->escape($unit_type).
"'";
191 $sql_where[] =
" t.code = '".$this->db->escape($code).
"'";
194 $sql_where[] =
" t.short_label = '".$this->db->escape($short_label).
"'";
196 if (count($sql_where) > 0) {
197 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
200 $resql = $this->db->query($sql);
202 if ($this->db->num_rows($resql)) {
203 $obj = $this->db->fetch_object($resql);
205 $this->
id = $obj->rowid;
206 $this->code = $obj->code;
207 $this->label = $obj->label;
208 $this->short_label = $obj->short_label;
209 $this->scale = $obj->scale;
210 $this->unit_type = $obj->unit_type;
211 $this->scale = $obj->scale;
212 $this->active = $obj->active;
214 $this->db->free($resql);
218 $this->error =
"Error ".$this->db->lasterror();
235 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
244 $sql .=
" t.sortorder,";
246 $sql .=
" t.short_label,";
247 $sql .=
" t.unit_type,";
250 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
253 if (count($filter) > 0) {
254 foreach ($filter as $key => $value) {
255 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
256 $sqlwhere[] = $key.
" = ".((int) $value);
257 } elseif (strpos($key,
'date') !==
false) {
258 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
259 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
260 $sqlwhere[] = $key.
" = '".$this->db->escape($value).
"'";
262 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
266 if (count($sqlwhere) > 0) {
267 $sql .=
' WHERE ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
270 if (!empty($sortfield)) {
271 $sql .= $this->db->order($sortfield, $sortorder);
273 if (!empty($limit)) {
274 $sql .= $this->db->plimit($limit, $offset);
277 $resql = $this->db->query($sql);
279 $this->records = array();
280 $num = $this->db->num_rows($resql);
282 while ($obj = $this->db->fetch_object($resql)) {
283 $record =
new self($this->db);
285 $record->id = $obj->rowid;
286 $record->code = $obj->code;
287 $record->sortorder = $obj->sortorder;
288 $record->label = $obj->label;
289 $record->short_label = $obj->short_label;
290 $record->unit_type = $obj->unit_type;
291 $record->scale = $obj->scale;
292 $record->active = $obj->active;
294 $this->records[$record->id] = $record;
297 $this->db->free($resql);
299 return $this->records;
301 $this->errors[] =
'Error '.$this->db->lasterror();
302 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
316 public function update($user =
null, $notrigger = 0)
318 global $conf, $langs;
322 if (isset($this->code)) {
323 $this->code = trim($this->code);
325 if (isset($this->sortorder)) {
326 $this->sortorder = trim($this->sortorder);
328 if (isset($this->label)) {
329 $this->libelle = trim($this->label);
331 if (isset($this->short_label)) {
332 $this->libelle = trim($this->short_label);
334 if (isset($this->unit_type)) {
335 $this->libelle = trim($this->unit_type);
337 if (isset($this->scale)) {
338 $this->scale = trim($this->scale);
340 if (isset($this->active)) {
341 $this->active = trim($this->active);
348 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
349 $sql .=
" code=".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
",";
350 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
351 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
352 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
353 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
354 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
355 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
356 $sql .=
" WHERE rowid=".((int) $this->
id);
360 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
361 $resql = $this->db->query($sql);
364 $this->errors[] =
"Error ".$this->db->lasterror();
369 foreach ($this->errors as $errmsg) {
370 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
371 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
373 $this->db->rollback();
389 public function delete($user, $notrigger = 0)
391 global $conf, $langs;
394 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
395 $sql .=
" WHERE rowid=".((int) $this->
id);
399 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
400 $resql = $this->db->query($sql);
403 $this->errors[] =
"Error ".$this->db->lasterror();
408 foreach ($this->errors as $errmsg) {
409 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
410 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
412 $this->db->rollback();
431 if ($mode ==
'short_label') {
432 return dol_getIdFromCode($this->db, $code,
'c_units',
'short_label',
'rowid', 0,
" AND unit_type = '".$this->db->escape($unit_type).
"'");
433 } elseif ($mode ==
'code') {
434 return dol_getIdFromCode($this->db, $code,
'c_units',
'code',
'rowid', 0,
" AND unit_type = '". $this->db->escape($unit_type) .
"'");
450 $fk_unit = intval($fk_unit);
456 $value = $value * $scaleUnitPow;
457 if ($fk_new_unit != 0) {
460 if (!empty($scaleUnitPow)) {
462 $value = $value / $scaleUnitPow;
465 return round($value, 2);
478 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
480 $resql = $this->db->query($sql);
483 $unit = $this->db->fetch_object($sql);
487 if ($unit->unit_type ==
'time') {
488 return floatval($unit->scale);
491 return pow($base, floatval($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.
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.