dolibarr  17.0.4
cunits.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
28 class CUnits // extends CommonObject
29 {
33  public $db;
34 
38  public $error = '';
39 
43  public $errors = array();
44  public $records = array();
45 
46  //var $element='ctypent'; //!< Id that identify managed objects
47  //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
48 
52  public $id;
53 
54  public $code;
55  public $label;
56  public $short_label;
57  public $unit_type;
58  public $scale;
59  public $active;
60 
61 
62 
63 
69  public function __construct($db)
70  {
71  $this->db = $db;
72  }
73 
74 
82  public function create($user, $notrigger = 0)
83  {
84  global $conf, $langs;
85  $error = 0;
86 
87  // Clean parameters
88 
89  if (isset($this->id)) {
90  $this->id = (int) $this->id;
91  }
92  if (isset($this->code)) {
93  $this->code = trim($this->code);
94  }
95  if (isset($this->label)) {
96  $this->libelle = trim($this->label);
97  }
98  if (isset($this->short_label)) {
99  $this->libelle = trim($this->short_label);
100  }
101  if (isset($this->unit_type)) {
102  $this->active = trim($this->unit_type);
103  }
104  if (isset($this->active)) {
105  $this->active = trim($this->active);
106  }
107  if (isset($this->scale)) {
108  $this->scale = trim($this->scale);
109  }
110 
111  // Check parameters
112  // Put here code to add control on parameters values
113 
114  // Insert request
115  $sql = "INSERT INTO ".$this->db->prefix()."c_units(";
116  $sql .= "rowid,";
117  $sql .= "code,";
118  $sql .= "label,";
119  $sql .= "short_label,";
120  $sql .= "unit_type,";
121  $sql .= "scale";
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)."'");
129  $sql .= ")";
130 
131  $this->db->begin();
132 
133  dol_syslog(get_class($this)."::create", LOG_DEBUG);
134  $resql = $this->db->query($sql);
135  if (!$resql) {
136  $error++;
137  $this->errors[] = "Error ".$this->db->lasterror();
138  }
139 
140  if (!$error) {
141  $this->id = $this->db->last_insert_id($this->db->prefix()."c_units");
142  }
143 
144  // Commit or rollback
145  if ($error) {
146  foreach ($this->errors as $errmsg) {
147  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
148  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
149  }
150  $this->db->rollback();
151  return -1 * $error;
152  } else {
153  $this->db->commit();
154  return $this->id;
155  }
156  }
157 
158 
168  public function fetch($id, $code = '', $short_label = '', $unit_type = '')
169  {
170  global $langs;
171 
172  $sql = "SELECT";
173  $sql .= " t.rowid,";
174  $sql .= " t.code,";
175  $sql .= " t.label,";
176  $sql .= " t.short_label,";
177  $sql .= " t.scale,";
178  $sql .= " t.unit_type,";
179  $sql .= " t.scale,";
180  $sql .= " t.active";
181  $sql .= " FROM ".$this->db->prefix()."c_units as t";
182  $sql_where = array();
183  if ($id) {
184  $sql_where[] = " t.rowid = ".((int) $id);
185  }
186  if ($unit_type) {
187  $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
188  }
189  if ($code) {
190  $sql_where[] = " t.code = '".$this->db->escape($code)."'";
191  }
192  if ($short_label) {
193  $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
194  }
195  if (count($sql_where) > 0) {
196  $sql .= ' WHERE '.implode(' AND ', $sql_where);
197  }
198 
199  $resql = $this->db->query($sql);
200  if ($resql) {
201  if ($this->db->num_rows($resql)) {
202  $obj = $this->db->fetch_object($resql);
203 
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;
212  }
213  $this->db->free($resql);
214 
215  return 1;
216  } else {
217  $this->error = "Error ".$this->db->lasterror();
218  return -1;
219  }
220  }
221 
222 
234  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
235  {
236  global $conf;
237 
238  dol_syslog(__METHOD__, LOG_DEBUG);
239 
240  $sql = "SELECT";
241  $sql .= " t.rowid,";
242  $sql .= " t.code,";
243  $sql .= " t.sortorder,";
244  $sql .= " t.label,";
245  $sql .= " t.short_label,";
246  $sql .= " t.unit_type,";
247  $sql .= " t.scale,";
248  $sql .= " t.active";
249  $sql .= " FROM ".$this->db->prefix()."c_units as t";
250  // Manage filter
251  $sqlwhere = array();
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)."'";
260  } else {
261  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
262  }
263  }
264  }
265  if (count($sqlwhere) > 0) {
266  $sql .= ' WHERE ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
267  }
268 
269  if (!empty($sortfield)) {
270  $sql .= $this->db->order($sortfield, $sortorder);
271  }
272  if (!empty($limit)) {
273  $sql .= $this->db->plimit($limit, $offset);
274  }
275 
276  $resql = $this->db->query($sql);
277  if ($resql) {
278  $this->records = array();
279  $num = $this->db->num_rows($resql);
280  if ($num > 0) {
281  while ($obj = $this->db->fetch_object($resql)) {
282  $record = new self($this->db);
283 
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;
293  }
294  }
295  $this->db->free($resql);
296 
297  return $this->records;
298  } else {
299  $this->errors[] = 'Error '.$this->db->lasterror();
300  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
301 
302  return -1;
303  }
304  }
305 
306 
314  public function update($user = null, $notrigger = 0)
315  {
316  global $conf, $langs;
317  $error = 0;
318 
319  // Clean parameters
320  if (isset($this->code)) {
321  $this->code = trim($this->code);
322  }
323  if (isset($this->sortorder)) {
324  $this->sortorder = trim($this->sortorder);
325  }
326  if (isset($this->label)) {
327  $this->libelle = trim($this->label);
328  }
329  if (isset($this->short_label)) {
330  $this->libelle = trim($this->short_label);
331  }
332  if (isset($this->unit_type)) {
333  $this->libelle = trim($this->unit_type);
334  }
335  if (isset($this->scale)) {
336  $this->scale = trim($this->scale);
337  }
338  if (isset($this->active)) {
339  $this->active = trim($this->active);
340  }
341 
342  // Check parameters
343  // Put here code to add control on parameters values
344 
345  // Update request
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);
355 
356  $this->db->begin();
357 
358  dol_syslog(get_class($this)."::update", LOG_DEBUG);
359  $resql = $this->db->query($sql);
360  if (!$resql) {
361  $error++;
362  $this->errors[] = "Error ".$this->db->lasterror();
363  }
364 
365  // Commit or rollback
366  if ($error) {
367  foreach ($this->errors as $errmsg) {
368  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
369  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
370  }
371  $this->db->rollback();
372  return -1 * $error;
373  } else {
374  $this->db->commit();
375  return 1;
376  }
377  }
378 
379 
387  public function delete($user, $notrigger = 0)
388  {
389  global $conf, $langs;
390  $error = 0;
391 
392  $sql = "DELETE FROM ".$this->db->prefix()."c_units";
393  $sql .= " WHERE rowid=".((int) $this->id);
394 
395  $this->db->begin();
396 
397  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
398  $resql = $this->db->query($sql);
399  if (!$resql) {
400  $error++;
401  $this->errors[] = "Error ".$this->db->lasterror();
402  }
403 
404  // Commit or rollback
405  if ($error) {
406  foreach ($this->errors as $errmsg) {
407  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
408  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
409  }
410  $this->db->rollback();
411  return -1 * $error;
412  } else {
413  $this->db->commit();
414  return 1;
415  }
416  }
417 
418 
426  public function getUnitFromCode($code, $mode = 'code', $unit_type = '')
427  {
428 
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) ."'");
433  }
434 
435  return $code;
436  }
437 
445  public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
446  {
447  $value = floatval(price2num($value));
448  $fk_unit = intval($fk_unit);
449 
450  // Calcul en unit√© de base
451  $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
452 
453  // convert to standard unit
454  $value = $value * $scaleUnitPow;
455  if ($fk_new_unit != 0) {
456  // Calcul en unit√© de base
457  $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
458  if (!empty($scaleUnitPow)) {
459  // convert to new unit
460  $value = $value / $scaleUnitPow;
461  }
462  }
463  return round($value, 2);
464  }
465 
472  public function scaleOfUnitPow($id)
473  {
474  $base = 10;
475 
476  $sql = "SELECT scale, unit_type FROM ".$this->db->prefix()."c_units WHERE rowid = ".((int) $id);
477 
478  $resql = $this->db->query($sql);
479  if ($resql) {
480  // TODO : add base col into unit dictionary table
481  $unit = $this->db->fetch_object($sql);
482  if ($unit) {
483  // TODO : if base exists in unit dictionary table, remove this convertion exception and update convertion infos in database.
484  // Example time hour currently scale 3600 will become scale 2 base 60
485  if ($unit->unit_type == 'time') {
486  return floatval($unit->scale);
487  }
488 
489  return pow($base, floatval($unit->scale));
490  }
491  }
492 
493  return 0;
494  }
495 }
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.
Definition: index.php:745
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.
Definition: inc.php:41
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...