43 public $errors = array();
44 public $records = array();
82 public function create($user, $notrigger = 0)
89 if (isset($this->
id)) $this->
id = (int) $this->
id;
91 if (isset($this->label)) $this->libelle = trim($this->label);
92 if (isset($this->short_label)) $this->libelle = trim($this->short_label);
93 if (isset($this->unit_type)) $this->active = trim($this->unit_type);
94 if (isset($this->active)) $this->active = trim($this->active);
95 if (isset($this->scale)) $this->scale = trim($this->scale);
101 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"c_units(";
105 $sql .=
"short_label,";
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);
121 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
125 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"c_units");
131 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 =
'')
162 $sql .=
" t.short_label,";
164 $sql .=
" t.unit_type,";
167 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_units as t";
168 $sql_where = array();
169 if ($id) $sql_where[] =
" t.rowid = ".$id;
170 if ($unit_type) $sql_where[] =
" t.unit_type = '".$this->db->escape($unit_type).
"'";
171 if ($code) $sql_where[] =
" t.code = '".$this->db->escape($code).
"'";
172 if ($short_label) $sql_where[] =
" t.short_label = '".$this->db->escape($short_label).
"'";
173 if (count($sql_where) > 0) {
174 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
182 $obj = $this->
db->fetch_object(
$resql);
184 $this->
id = $obj->rowid;
185 $this->
code = $obj->code;
186 $this->label = $obj->label;
187 $this->short_label = $obj->short_label;
188 $this->scale = $obj->scale;
189 $this->unit_type = $obj->unit_type;
190 $this->scale = $obj->scale;
191 $this->active = $obj->active;
197 $this->error =
"Error ".$this->db->lasterror();
214 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
224 $sql .=
" t.short_label,";
225 $sql .=
" t.unit_type,";
228 $sql .=
' FROM '.MAIN_DB_PREFIX.
'c_units as t';
231 if (count($filter) > 0) {
232 foreach ($filter as $key => $value) {
233 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
234 $sqlwhere[] = $key.
'='.(int) $value;
235 } elseif (strpos($key,
'date') !==
false) {
236 $sqlwhere[] = $key.
' = \''.$this->
db->idate($value).
'\'';
237 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
238 $sqlwhere[] = $key.
' = \''.$this->
db->escape($value).
'\'';
240 $sqlwhere[] = $key.
' LIKE \'%'.$this->
db->escape($value).
'%\'';
244 if (count($sqlwhere) > 0) {
245 $sql .=
' WHERE ('.implode(
' '.$filtermode.
' ', $sqlwhere).
')';
248 if (!empty($sortfield)) {
249 $sql .= $this->
db->order($sortfield, $sortorder);
251 if (!empty($limit)) {
252 $sql .=
' '.$this->db->plimit($limit, $offset);
257 $this->records = array();
260 while ($obj = $this->
db->fetch_object(
$resql)) {
261 $record =
new self($this->db);
263 $record->id = $obj->rowid;
264 $record->code = $obj->code;
265 $record->label = $obj->label;
266 $record->short_label = $obj->short_label;
267 $record->unit_type = $obj->unit_type;
268 $record->scale = $obj->scale;
269 $record->active = $obj->active;
270 $this->records[$record->id] = $record;
275 return $this->records;
277 $this->errors[] =
'Error '.$this->db->lasterror();
278 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
292 public function update($user = null, $notrigger = 0)
294 global $conf, $langs;
298 if (isset($this->
code)) $this->
code = trim($this->
code);
299 if (isset($this->label)) $this->libelle = trim($this->label);
300 if (isset($this->short_label)) $this->libelle = trim($this->short_label);
301 if (isset($this->unit_type)) $this->libelle = trim($this->unit_type);
302 if (isset($this->scale)) $this->scale = trim($this->scale);
303 if (isset($this->active)) $this->active = trim($this->active);
309 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"c_units SET";
310 $sql .=
" code=".(isset($this->
code) ?
"'".$this->db->escape($this->
code).
"'" :
"null").
",";
311 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
312 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
313 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
314 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
315 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
316 $sql .=
" WHERE rowid=".$this->id;
320 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
322 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
327 foreach ($this->errors as $errmsg)
329 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
330 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
332 $this->
db->rollback();
348 public function delete($user, $notrigger = 0)
350 global $conf, $langs;
353 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"c_units";
354 $sql .=
" WHERE rowid=".$this->id;
358 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
360 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
365 foreach ($this->errors as $errmsg)
367 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
368 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
370 $this->
db->rollback();
388 if ($mode ==
'short_label') {
390 } elseif ($mode ==
'code') {
407 $fk_unit = intval($fk_unit);
413 $value = $value * $scaleUnitPow;
414 if ($fk_new_unit != 0) {
417 if (!empty($scaleUnitPow))
420 $value = $value / $scaleUnitPow;
423 return round($value, 2);
435 $unit = $this->
db->getRow(
'SELECT scale, unit_type from '.MAIN_DB_PREFIX.
'c_units WHERE rowid = '.intval($id));
438 if ($unit->unit_type ==
'time') {
439 return doubleval($unit->scale);
442 return pow($base, doubleval($unit->scale));
Class of dictionary type of thirdparty (used by imports)
scaleOfUnitPow($id)
get scale of unit factor
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
create($user, $notrigger=0)
Create object into database.
update($user=null, $notrigger=0)
Update object into database.
getUnitFromCode($code, $mode= 'code')
Get unit from code.
$conf db
API class for accounts.
__construct($db)
Constructor.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
fetchAll($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND')
Load list of objects in memory from the database.
fetch($id, $code= '', $short_label= '', $unit_type= '')
Load object in memory from database.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
unitConverter($value, $fk_unit, $fk_new_unit=0)
Unit converter.