43 public $errors = array();
44 public $records = array();
82 public function create($user, $notrigger = 0)
89 if (isset($this->
id)) {
90 $this->
id = (int) $this->
id;
92 if (isset($this->
code)) {
95 if (isset($this->label)) {
96 $this->libelle = trim($this->label);
98 if (isset($this->short_label)) {
99 $this->libelle = trim($this->short_label);
101 if (isset($this->unit_type)) {
102 $this->active = trim($this->unit_type);
104 if (isset($this->active)) {
105 $this->active = trim($this->active);
107 if (isset($this->scale)) {
108 $this->scale = trim($this->scale);
115 $sql =
"INSERT INTO ".$this->db->prefix().
"c_units(";
119 $sql .=
"short_label,";
122 $sql .=
") VALUES (";
123 $sql .=
" ".(!isset($this->
id) ?
'NULL' :
"'".$this->db->escape($this->
id).
"'").
",";
124 $sql .=
" ".(!isset($this->
code) ?
'NULL' :
"'".$this->db->escape($this->
code).
"'").
",";
125 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
126 $sql .=
" ".(!isset($this->short_label) ?
'NULL' :
"'".$this->db->escape($this->short_label).
"'").
",";
127 $sql .=
" ".(!isset($this->unit_type) ?
'NULL' :
"'".$this->db->escape($this->unit_type).
"'");
128 $sql .=
" ".(!isset($this->scale) ?
'NULL' :
"'".$this->db->escape($this->scale).
"'");
133 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
137 $this->errors[] =
"Error ".$this->db->lasterror();
141 $this->
id = $this->
db->last_insert_id($this->
db->prefix().
"c_units");
146 foreach ($this->errors as $errmsg) {
147 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
148 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
150 $this->
db->rollback();
168 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);
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;
217 $this->error =
"Error ".$this->db->lasterror();
234 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
243 $sql .=
" t.sortorder,";
245 $sql .=
" t.short_label,";
246 $sql .=
" t.unit_type,";
249 $sql .=
" FROM ".$this->db->prefix().
"c_units as t";
252 if (count($filter) > 0) {
253 foreach ($filter as $key => $value) {
254 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.scale') {
255 $sqlwhere[] = $key.
" = ".((int) $value);
256 } elseif (strpos($key,
'date') !==
false) {
257 $sqlwhere[] = $key.
" = '".$this->
db->idate($value).
"'";
258 } elseif ($key ==
't.unit_type' || $key ==
't.code' || $key ==
't.short_label') {
259 $sqlwhere[] = $key.
" = '".$this->
db->escape($value).
"'";
261 $sqlwhere[] = $key.
" LIKE '%".$this->
db->escape($value).
"%'";
265 if (count($sqlwhere) > 0) {
266 $sql .=
' WHERE ('.implode(
' '.$this->
db->escape($filtermode).
' ', $sqlwhere).
')';
269 if (!empty($sortfield)) {
270 $sql .= $this->
db->order($sortfield, $sortorder);
272 if (!empty($limit)) {
273 $sql .= $this->
db->plimit($limit, $offset);
278 $this->records = array();
281 while ($obj = $this->
db->fetch_object(
$resql)) {
282 $record =
new self($this->db);
284 $record->id = $obj->rowid;
285 $record->code = $obj->code;
286 $record->sortorder = $obj->sortorder;
287 $record->label = $obj->label;
288 $record->short_label = $obj->short_label;
289 $record->unit_type = $obj->unit_type;
290 $record->scale = $obj->scale;
291 $record->active = $obj->active;
292 $this->records[$record->id] = $record;
297 return $this->records;
299 $this->errors[] =
'Error '.$this->db->lasterror();
300 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
314 public function update($user =
null, $notrigger = 0)
316 global $conf, $langs;
320 if (isset($this->
code)) {
323 if (isset($this->sortorder)) {
324 $this->sortorder = trim($this->sortorder);
326 if (isset($this->label)) {
327 $this->libelle = trim($this->label);
329 if (isset($this->short_label)) {
330 $this->libelle = trim($this->short_label);
332 if (isset($this->unit_type)) {
333 $this->libelle = trim($this->unit_type);
335 if (isset($this->scale)) {
336 $this->scale = trim($this->scale);
338 if (isset($this->active)) {
339 $this->active = trim($this->active);
346 $sql =
"UPDATE ".$this->db->prefix().
"c_units SET";
347 $sql .=
" code=".(isset($this->
code) ?
"'".$this->db->escape($this->
code).
"'" :
"null").
",";
348 $sql .=
" sortorder=".(isset($this->sortorder) ?
"'".$this->db->escape($this->sortorder).
"'" :
"null").
",";
349 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
350 $sql .=
" short_label=".(isset($this->short_label) ?
"'".$this->db->escape($this->short_label).
"'" :
"null").
",";
351 $sql .=
" unit_type=".(isset($this->unit_type) ?
"'".$this->db->escape($this->unit_type).
"'" :
"null").
",";
352 $sql .=
" scale=".(isset($this->scale) ?
"'".$this->db->escape($this->scale).
"'" :
"null").
",";
353 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
354 $sql .=
" WHERE rowid=".((int) $this->
id);
358 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
362 $this->errors[] =
"Error ".$this->db->lasterror();
367 foreach ($this->errors as $errmsg) {
368 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
369 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
371 $this->
db->rollback();
387 public function delete($user, $notrigger = 0)
389 global $conf, $langs;
392 $sql =
"DELETE FROM ".$this->db->prefix().
"c_units";
393 $sql .=
" WHERE rowid=".((int) $this->
id);
397 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
401 $this->errors[] =
"Error ".$this->db->lasterror();
406 foreach ($this->errors as $errmsg) {
407 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
408 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
410 $this->
db->rollback();
429 if ($mode ==
'short_label') {
430 return dol_getIdFromCode($this->
db, $code,
'c_units',
'short_label',
'rowid', 0,
" AND unit_type = '".$this->
db->escape($unit_type).
"'");
431 } elseif ($mode ==
'code') {
432 return dol_getIdFromCode($this->
db, $code,
'c_units',
'code',
'rowid', 0,
" AND unit_type = '". $this->
db->escape($unit_type) .
"'");
448 $fk_unit = intval($fk_unit);
454 $value = $value * $scaleUnitPow;
455 if ($fk_new_unit != 0) {
458 if (!empty($scaleUnitPow)) {
460 $value = $value / $scaleUnitPow;
463 return round($value, 2);
476 $sql =
"SELECT scale, unit_type FROM ".$this->db->prefix().
"c_units WHERE rowid = ".((int) $id);
481 $unit = $this->
db->fetch_object($sql);
485 if ($unit->unit_type ==
'time') {
486 return floatval($unit->scale);
489 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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
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.
$conf db
API class for accounts.
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...