dolibarr 18.0.6
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
28class 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 $sortorder;
57 public $short_label;
58 public $unit_type;
59 public $scale;
60 public $active;
61
62
63
64
70 public function __construct($db)
71 {
72 $this->db = $db;
73 }
74
75
83 public function create($user, $notrigger = 0)
84 {
85 global $conf, $langs;
86 $error = 0;
87
88 // Clean parameters
89
90 if (isset($this->id)) {
91 $this->id = (int) $this->id;
92 }
93 if (isset($this->code)) {
94 $this->code = trim($this->code);
95 }
96 if (isset($this->label)) {
97 $this->libelle = trim($this->label);
98 }
99 if (isset($this->short_label)) {
100 $this->libelle = trim($this->short_label);
101 }
102 if (isset($this->unit_type)) {
103 $this->active = trim($this->unit_type);
104 }
105 if (isset($this->active)) {
106 $this->active = trim($this->active);
107 }
108 if (isset($this->scale)) {
109 $this->scale = trim($this->scale);
110 }
111
112 // Check parameters
113 // Put here code to add control on parameters values
114
115 // Insert request
116 $sql = "INSERT INTO ".$this->db->prefix()."c_units(";
117 $sql .= "rowid,";
118 $sql .= "code,";
119 $sql .= "label,";
120 $sql .= "short_label,";
121 $sql .= "unit_type,";
122 $sql .= "scale";
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)."'");
130 $sql .= ")";
131
132 $this->db->begin();
133
134 dol_syslog(get_class($this)."::create", LOG_DEBUG);
135 $resql = $this->db->query($sql);
136 if (!$resql) {
137 $error++;
138 $this->errors[] = "Error ".$this->db->lasterror();
139 }
140
141 if (!$error) {
142 $this->id = $this->db->last_insert_id($this->db->prefix()."c_units");
143 }
144
145 // Commit or rollback
146 if ($error) {
147 foreach ($this->errors as $errmsg) {
148 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
149 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
150 }
151 $this->db->rollback();
152 return -1 * $error;
153 } else {
154 $this->db->commit();
155 return $this->id;
156 }
157 }
158
159
169 public function fetch($id, $code = '', $short_label = '', $unit_type = '')
170 {
171 global $langs;
172
173 $sql = "SELECT";
174 $sql .= " t.rowid,";
175 $sql .= " t.code,";
176 $sql .= " t.label,";
177 $sql .= " t.short_label,";
178 $sql .= " t.scale,";
179 $sql .= " t.unit_type,";
180 $sql .= " t.scale,";
181 $sql .= " t.active";
182 $sql .= " FROM ".$this->db->prefix()."c_units as t";
183 $sql_where = array();
184 if ($id) {
185 $sql_where[] = " t.rowid = ".((int) $id);
186 }
187 if ($unit_type) {
188 $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
189 }
190 if ($code) {
191 $sql_where[] = " t.code = '".$this->db->escape($code)."'";
192 }
193 if ($short_label) {
194 $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
195 }
196 if (count($sql_where) > 0) {
197 $sql .= ' WHERE '.implode(' AND ', $sql_where);
198 }
199
200 $resql = $this->db->query($sql);
201 if ($resql) {
202 if ($this->db->num_rows($resql)) {
203 $obj = $this->db->fetch_object($resql);
204
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;
213 }
214 $this->db->free($resql);
215
216 return 1;
217 } else {
218 $this->error = "Error ".$this->db->lasterror();
219 return -1;
220 }
221 }
222
223
235 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
236 {
237 global $conf;
238
239 dol_syslog(__METHOD__, LOG_DEBUG);
240
241 $sql = "SELECT";
242 $sql .= " t.rowid,";
243 $sql .= " t.code,";
244 $sql .= " t.sortorder,";
245 $sql .= " t.label,";
246 $sql .= " t.short_label,";
247 $sql .= " t.unit_type,";
248 $sql .= " t.scale,";
249 $sql .= " t.active";
250 $sql .= " FROM ".$this->db->prefix()."c_units as t";
251 // Manage filter
252 $sqlwhere = array();
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)."'";
261 } else {
262 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
263 }
264 }
265 }
266 if (count($sqlwhere) > 0) {
267 $sql .= ' WHERE ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
268 }
269
270 if (!empty($sortfield)) {
271 $sql .= $this->db->order($sortfield, $sortorder);
272 }
273 if (!empty($limit)) {
274 $sql .= $this->db->plimit($limit, $offset);
275 }
276
277 $resql = $this->db->query($sql);
278 if ($resql) {
279 $this->records = array();
280 $num = $this->db->num_rows($resql);
281 if ($num > 0) {
282 while ($obj = $this->db->fetch_object($resql)) {
283 $record = new self($this->db);
284
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;
293
294 $this->records[$record->id] = $record;
295 }
296 }
297 $this->db->free($resql);
298
299 return $this->records;
300 } else {
301 $this->errors[] = 'Error '.$this->db->lasterror();
302 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
303
304 return -1;
305 }
306 }
307
308
316 public function update($user = null, $notrigger = 0)
317 {
318 global $conf, $langs;
319 $error = 0;
320
321 // Clean parameters
322 if (isset($this->code)) {
323 $this->code = trim($this->code);
324 }
325 if (isset($this->sortorder)) {
326 $this->sortorder = trim($this->sortorder);
327 }
328 if (isset($this->label)) {
329 $this->libelle = trim($this->label);
330 }
331 if (isset($this->short_label)) {
332 $this->libelle = trim($this->short_label);
333 }
334 if (isset($this->unit_type)) {
335 $this->libelle = trim($this->unit_type);
336 }
337 if (isset($this->scale)) {
338 $this->scale = trim($this->scale);
339 }
340 if (isset($this->active)) {
341 $this->active = trim($this->active);
342 }
343
344 // Check parameters
345 // Put here code to add control on parameters values
346
347 // Update request
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);
357
358 $this->db->begin();
359
360 dol_syslog(get_class($this)."::update", LOG_DEBUG);
361 $resql = $this->db->query($sql);
362 if (!$resql) {
363 $error++;
364 $this->errors[] = "Error ".$this->db->lasterror();
365 }
366
367 // Commit or rollback
368 if ($error) {
369 foreach ($this->errors as $errmsg) {
370 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
371 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
372 }
373 $this->db->rollback();
374 return -1 * $error;
375 } else {
376 $this->db->commit();
377 return 1;
378 }
379 }
380
381
389 public function delete($user, $notrigger = 0)
390 {
391 global $conf, $langs;
392 $error = 0;
393
394 $sql = "DELETE FROM ".$this->db->prefix()."c_units";
395 $sql .= " WHERE rowid=".((int) $this->id);
396
397 $this->db->begin();
398
399 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
400 $resql = $this->db->query($sql);
401 if (!$resql) {
402 $error++;
403 $this->errors[] = "Error ".$this->db->lasterror();
404 }
405
406 // Commit or rollback
407 if ($error) {
408 foreach ($this->errors as $errmsg) {
409 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
410 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
411 }
412 $this->db->rollback();
413 return -1 * $error;
414 } else {
415 $this->db->commit();
416 return 1;
417 }
418 }
419
420
428 public function getUnitFromCode($code, $mode = 'code', $unit_type = '')
429 {
430
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) ."'");
435 }
436
437 return $code;
438 }
439
447 public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
448 {
449 $value = floatval(price2num($value));
450 $fk_unit = intval($fk_unit);
451
452 // Calcul en unité de base
453 $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
454
455 // convert to standard unit
456 $value = $value * $scaleUnitPow;
457 if ($fk_new_unit != 0) {
458 // Calcul en unité de base
459 $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
460 if (!empty($scaleUnitPow)) {
461 // convert to new unit
462 $value = $value / $scaleUnitPow;
463 }
464 }
465 return round($value, 2);
466 }
467
474 public function scaleOfUnitPow($id)
475 {
476 $base = 10;
477
478 $sql = "SELECT scale, unit_type FROM ".$this->db->prefix()."c_units WHERE rowid = ".((int) $id);
479
480 $resql = $this->db->query($sql);
481 if ($resql) {
482 // TODO : add base col into unit dictionary table
483 $unit = $this->db->fetch_object($sql);
484 if ($unit) {
485 // TODO : if base exists in unit dictionary table, remove this convertion exception and update convertion infos in database.
486 // Example time hour currently scale 3600 will become scale 2 base 60
487 if ($unit->unit_type == 'time') {
488 return floatval($unit->scale);
489 }
490
491 return pow($base, floatval($unit->scale));
492 }
493 }
494
495 return 0;
496 }
497}
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.