dolibarr  13.0.2
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)) $this->id = (int) $this->id;
90  if (isset($this->code)) $this->code = trim($this->code);
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);
96 
97  // Check parameters
98  // Put here code to add control on parameters values
99 
100  // Insert request
101  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_units(";
102  $sql .= "rowid,";
103  $sql .= "code,";
104  $sql .= "label,";
105  $sql .= "short_label,";
106  $sql .= "unit_type";
107  $sql .= "scale";
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)."'");
115  $sql .= ")";
116 
117  $this->db->begin();
118 
119  dol_syslog(get_class($this)."::create", LOG_DEBUG);
120  $resql = $this->db->query($sql);
121  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
122 
123  if (!$error)
124  {
125  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_units");
126  }
127 
128  // Commit or rollback
129  if ($error)
130  {
131  foreach ($this->errors as $errmsg)
132  {
133  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
134  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
135  }
136  $this->db->rollback();
137  return -1 * $error;
138  } else {
139  $this->db->commit();
140  return $this->id;
141  }
142  }
143 
144 
154  public function fetch($id, $code = '', $short_label = '', $unit_type = '')
155  {
156  global $langs;
157 
158  $sql = "SELECT";
159  $sql .= " t.rowid,";
160  $sql .= " t.code,";
161  $sql .= " t.label,";
162  $sql .= " t.short_label,";
163  $sql .= " t.scale,";
164  $sql .= " t.unit_type,";
165  $sql .= " t.scale,";
166  $sql .= " t.active";
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);
175  }
176 
177  $resql = $this->db->query($sql);
178  if ($resql)
179  {
180  if ($this->db->num_rows($resql))
181  {
182  $obj = $this->db->fetch_object($resql);
183 
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;
192  }
193  $this->db->free($resql);
194 
195  return 1;
196  } else {
197  $this->error = "Error ".$this->db->lasterror();
198  return -1;
199  }
200  }
201 
202 
214  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
215  {
216  global $conf;
217 
218  dol_syslog(__METHOD__, LOG_DEBUG);
219 
220  $sql = 'SELECT';
221  $sql .= " t.rowid,";
222  $sql .= " t.code,";
223  $sql .= " t.label,";
224  $sql .= " t.short_label,";
225  $sql .= " t.unit_type,";
226  $sql .= " t.scale,";
227  $sql .= " t.active";
228  $sql .= ' FROM '.MAIN_DB_PREFIX.'c_units as t';
229  // Manage filter
230  $sqlwhere = array();
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).'\'';
239  } else {
240  $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
241  }
242  }
243  }
244  if (count($sqlwhere) > 0) {
245  $sql .= ' WHERE ('.implode(' '.$filtermode.' ', $sqlwhere).')';
246  }
247 
248  if (!empty($sortfield)) {
249  $sql .= $this->db->order($sortfield, $sortorder);
250  }
251  if (!empty($limit)) {
252  $sql .= ' '.$this->db->plimit($limit, $offset);
253  }
254 
255  $resql = $this->db->query($sql);
256  if ($resql) {
257  $this->records = array();
258  $num = $this->db->num_rows($resql);
259  if ($num > 0) {
260  while ($obj = $this->db->fetch_object($resql)) {
261  $record = new self($this->db);
262 
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;
271  }
272  }
273  $this->db->free($resql);
274 
275  return $this->records;
276  } else {
277  $this->errors[] = 'Error '.$this->db->lasterror();
278  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
279 
280  return -1;
281  }
282  }
283 
284 
292  public function update($user = null, $notrigger = 0)
293  {
294  global $conf, $langs;
295  $error = 0;
296 
297  // Clean parameters
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);
304 
305  // Check parameters
306  // Put here code to add control on parameters values
307 
308  // Update request
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;
317 
318  $this->db->begin();
319 
320  dol_syslog(get_class($this)."::update", LOG_DEBUG);
321  $resql = $this->db->query($sql);
322  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
323 
324  // Commit or rollback
325  if ($error)
326  {
327  foreach ($this->errors as $errmsg)
328  {
329  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
330  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
331  }
332  $this->db->rollback();
333  return -1 * $error;
334  } else {
335  $this->db->commit();
336  return 1;
337  }
338  }
339 
340 
348  public function delete($user, $notrigger = 0)
349  {
350  global $conf, $langs;
351  $error = 0;
352 
353  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_units";
354  $sql .= " WHERE rowid=".$this->id;
355 
356  $this->db->begin();
357 
358  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
359  $resql = $this->db->query($sql);
360  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
361 
362  // Commit or rollback
363  if ($error)
364  {
365  foreach ($this->errors as $errmsg)
366  {
367  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
368  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
369  }
370  $this->db->rollback();
371  return -1 * $error;
372  } else {
373  $this->db->commit();
374  return 1;
375  }
376  }
377 
378 
385  public function getUnitFromCode($code, $mode = 'code')
386  {
387 
388  if ($mode == 'short_label') {
389  return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid');
390  } elseif ($mode == 'code') {
391  return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid');
392  }
393 
394  return $code;
395  }
396 
404  public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
405  {
406  $value = doubleval(price2num($value));
407  $fk_unit = intval($fk_unit);
408 
409  // Calcul en unité de base
410  $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
411 
412  // convert to standard unit
413  $value = $value * $scaleUnitPow;
414  if ($fk_new_unit != 0) {
415  // Calcul en unité de base
416  $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
417  if (!empty($scaleUnitPow))
418  {
419  // convert to new unit
420  $value = $value / $scaleUnitPow;
421  }
422  }
423  return round($value, 2);
424  }
425 
431  public function scaleOfUnitPow($id)
432  {
433  $base = 10;
434  // TODO : add base col into unit dictionary table
435  $unit = $this->db->getRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id));
436  if ($unit) {
437  // TODO : if base exist in unit dictionary table remove this convertion exception and update convertion infos in database exemple time hour currently scale 3600 will become scale 2 base 60
438  if ($unit->unit_type == 'time') {
439  return doubleval($unit->scale);
440  }
441 
442  return pow($base, doubleval($unit->scale));
443  }
444 
445  return 0;
446  }
447 }
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
Definition: list.php:566
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.
Definition: inc.php:54
__construct($db)
Constructor.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
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.
Definition: index.php:1232
unitConverter($value, $fk_unit, $fk_new_unit=0)
Unit converter.