dolibarr 21.0.0-beta
cunits.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
28
29
33class CUnits extends CommonDict
34{
38 public $records = array();
39
40 // public $element = 'cunits'; //!< Id that identify managed objects
41 // public $table_element = 'c_units'; //!< Name of table without prefix where object is stored
42
48 public $libelle;
49
53 public $sortorder;
54
58 public $short_label;
59
63 public $unit_type;
64
68 public $scale;
69
70
76 public function __construct($db)
77 {
78 $this->db = $db;
79 }
80
81
89 public function create($user, $notrigger = 0)
90 {
91 $error = 0;
92
93 // Clean parameters
94
95 if (isset($this->id)) {
96 $this->id = (int) $this->id;
97 }
98 if (isset($this->code)) {
99 $this->code = trim($this->code);
100 }
101 if (isset($this->label)) {
102 $this->libelle = trim($this->label);
103 }
104 if (isset($this->short_label)) {
105 $this->libelle = trim($this->short_label);
106 }
107 if (isset($this->unit_type)) {
108 $this->unit_type = trim($this->unit_type);
109 }
110 if (isset($this->active)) {
111 $this->active = (int) $this->active;
112 }
113 if (isset($this->scale)) {
114 $this->scale = (int) $this->scale;
115 }
116
117 // Check parameters
118 // Put here code to add control on parameters values
119
120 // Insert request
121 $sql = "INSERT INTO ".$this->db->prefix()."c_units(";
122 $sql .= "rowid,";
123 $sql .= "code,";
124 $sql .= "label,";
125 $sql .= "short_label,";
126 $sql .= "unit_type,";
127 $sql .= "scale";
128 $sql .= ") VALUES (";
129 $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
130 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
131 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
132 $sql .= " ".(!isset($this->short_label) ? 'NULL' : "'".$this->db->escape($this->short_label)."'").",";
133 $sql .= " ".(!isset($this->unit_type) ? 'NULL' : "'".$this->db->escape($this->unit_type)."'").",";
134 $sql .= " ".(!isset($this->scale) ? 'NULL' : (int) $this->scale);
135 $sql .= ")";
136
137 $this->db->begin();
138
139 dol_syslog(get_class($this)."::create", LOG_DEBUG);
140 $resql = $this->db->query($sql);
141 if (!$resql) {
142 $error++;
143 $this->errors[] = "Error ".$this->db->lasterror();
144 }
145
146 if (!$error) {
147 $this->id = $this->db->last_insert_id($this->db->prefix()."c_units");
148 }
149
150 // Commit or rollback
151 if ($error) {
152 foreach ($this->errors as $errmsg) {
153 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
154 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
155 }
156 $this->db->rollback();
157 return -1 * $error;
158 } else {
159 $this->db->commit();
160 return $this->id;
161 }
162 }
163
164
174 public function fetch($id, $code = '', $short_label = '', $unit_type = '')
175 {
176 $sql = "SELECT";
177 $sql .= " t.rowid,";
178 $sql .= " t.code,";
179 $sql .= " t.label,";
180 $sql .= " t.short_label,";
181 $sql .= " t.scale,";
182 $sql .= " t.unit_type,";
183 $sql .= " t.active";
184 $sql .= " FROM ".$this->db->prefix()."c_units as t";
185 $sql_where = array();
186 if ($id) {
187 $sql_where[] = " t.rowid = ".((int) $id);
188 }
189 if ($unit_type) {
190 $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
191 }
192 if ($code) {
193 $sql_where[] = " t.code = '".$this->db->escape($code)."'";
194 }
195 if ($short_label) {
196 $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
197 }
198 if (count($sql_where) > 0) {
199 $sql .= ' WHERE '.implode(' AND ', $sql_where);
200 }
201
202 $resql = $this->db->query($sql);
203 if ($resql) {
204 if ($this->db->num_rows($resql)) {
205 $obj = $this->db->fetch_object($resql);
206
207 $this->id = (int) $obj->rowid;
208 $this->code = $obj->code;
209 $this->label = $obj->label;
210 $this->short_label = $obj->short_label;
211 $this->scale = $obj->scale ? (int) $obj->scale : null;
212 $this->unit_type = $obj->unit_type;
213 $this->active = (int) $obj->active;
214 }
215 $this->db->free($resql);
216
217 return 1;
218 } else {
219 $this->error = "Error ".$this->db->lasterror();
220 return -1;
221 }
222 }
223
224
236 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
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 $sql .= " WHERE 1 = 1";
251
252 // Manage filter
253 if (is_array($filter)) {
254 $sqlwhere = array();
255 if (count($filter) > 0) {
256 foreach ($filter as $key => $value) {
257 if ($key == 't.rowid' || $key == 't.active' || $key == 't.scale') {
258 $sqlwhere[] = $this->db->sanitize($key)." = ".((int) $value);
259 } elseif (strpos($key, 'date') !== false) {
260 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->idate($value)."'";
261 } elseif ($key == 't.unit_type' || $key == 't.code' || $key == 't.short_label') {
262 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
263 } else {
264 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
265 }
266 }
267 }
268 if (count($sqlwhere) > 0) {
269 $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
270 }
271
272 $filter = '';
273 }
274
275 // Manage filter
276 $errormessage = '';
277 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
278 if ($errormessage) {
279 $this->errors[] = $errormessage;
280 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
281 return -1;
282 }
283
284 if (!empty($sortfield)) {
285 $sql .= $this->db->order($sortfield, $sortorder);
286 }
287 if (!empty($limit)) {
288 $sql .= $this->db->plimit($limit, $offset);
289 }
290
291 $resql = $this->db->query($sql);
292 if ($resql) {
293 $this->records = array();
294 $num = $this->db->num_rows($resql);
295 if ($num > 0) {
296 while ($obj = $this->db->fetch_object($resql)) {
297 $record = new self($this->db);
298
299 $record->id = (int) $obj->rowid;
300 $record->code = $obj->code;
301 $record->sortorder = $obj->sortorder;
302 $record->label = $obj->label;
303 $record->short_label = $obj->short_label;
304 $record->unit_type = $obj->unit_type;
305 $record->scale = $obj->scale ? (int) $obj->scale : null;
306 $record->active = (int) $obj->active;
307
308 $this->records[$record->id] = $record;
309 }
310 }
311 $this->db->free($resql);
312
313 return $this->records;
314 } else {
315 $this->errors[] = 'Error '.$this->db->lasterror();
316 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
317
318 return -1;
319 }
320 }
321
322
330 public function update($user = null, $notrigger = 0)
331 {
332 $error = 0;
333
334 // Clean parameters
335 if (isset($this->code)) {
336 $this->code = trim($this->code);
337 }
338 if (isset($this->sortorder)) {
339 $this->sortorder = trim($this->sortorder);
340 }
341 if (isset($this->label)) {
342 $this->libelle = trim($this->label);
343 }
344 if (isset($this->short_label)) {
345 $this->libelle = trim($this->short_label);
346 }
347 if (isset($this->unit_type)) {
348 $this->libelle = trim($this->unit_type);
349 }
350 if (isset($this->scale)) {
351 $this->scale = (int) $this->scale;
352 }
353 if (isset($this->active)) {
354 $this->active = (int) $this->active;
355 }
356
357 // Check parameters
358 // Put here code to add control on parameters values
359
360 // Update request
361 $sql = "UPDATE ".$this->db->prefix()."c_units SET";
362 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
363 $sql .= " sortorder=".(isset($this->sortorder) ? "'".$this->db->escape($this->sortorder)."'" : "null").",";
364 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
365 $sql .= " short_label=".(isset($this->short_label) ? "'".$this->db->escape($this->short_label)."'" : "null").",";
366 $sql .= " unit_type=".(isset($this->unit_type) ? "'".$this->db->escape($this->unit_type)."'" : "null").",";
367 $sql .= " scale=".(isset($this->scale) ? "'".$this->db->escape($this->scale)."'" : "null").",";
368 $sql .= " active=".(isset($this->active) ? $this->active : "null");
369 $sql .= " WHERE rowid=".((int) $this->id);
370
371 $this->db->begin();
372
373 dol_syslog(get_class($this)."::update", LOG_DEBUG);
374 $resql = $this->db->query($sql);
375 if (!$resql) {
376 $error++;
377 $this->errors[] = "Error ".$this->db->lasterror();
378 }
379
380 // Commit or rollback
381 if ($error) {
382 foreach ($this->errors as $errmsg) {
383 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
384 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
385 }
386 $this->db->rollback();
387 return -1 * $error;
388 } else {
389 $this->db->commit();
390 return 1;
391 }
392 }
393
394
402 public function delete($user, $notrigger = 0)
403 {
404 $error = 0;
405
406 $sql = "DELETE FROM ".$this->db->prefix()."c_units";
407 $sql .= " WHERE rowid=".((int) $this->id);
408
409 $this->db->begin();
410
411 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
412 $resql = $this->db->query($sql);
413 if (!$resql) {
414 $error++;
415 $this->errors[] = "Error ".$this->db->lasterror();
416 }
417
418 // Commit or rollback
419 if ($error) {
420 foreach ($this->errors as $errmsg) {
421 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
422 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
423 }
424 $this->db->rollback();
425 return -1 * $error;
426 } else {
427 $this->db->commit();
428 return 1;
429 }
430 }
431
432
440 public function getUnitFromCode($code, $mode = 'code', $unit_type = '')
441 {
442 if ($mode == 'short_label' || $mode == 'code') {
443 return dol_getIdFromCode($this->db, $code, 'c_units', $mode, 'rowid', 0, " AND unit_type = '".$this->db->escape($unit_type)."'");
444 }
445 return $code;
446 }
447
455 public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
456 {
457 $value = (float) price2num($value);
458 $fk_unit = intval($fk_unit);
459
460 // Calcul en unité de base
461 $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
462
463 // convert to standard unit
464 $value *= $scaleUnitPow;
465 if ($fk_new_unit != 0) {
466 // Calcul en unité de base
467 $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
468 if (!empty($scaleUnitPow)) {
469 // convert to new unit
470 $value /= $scaleUnitPow;
471 }
472 }
473 return round($value, 2);
474 }
475
482 public function scaleOfUnitPow($id)
483 {
484 $base = 10;
485
486 $sql = "SELECT scale, unit_type FROM ".$this->db->prefix()."c_units WHERE rowid = ".((int) $id);
487
488 $resql = $this->db->query($sql);
489 if ($resql) {
490 // TODO : add base col into unit dictionary table
491 $unit = $this->db->fetch_object($sql);
492 if ($unit) {
493 // TODO : if base exists in unit dictionary table, remove this conversion exception and update conversion infos in database.
494 // Example time hour currently scale 3600 will become scale 2 base 60
495 if ($unit->unit_type == 'time') {
496 return (float) $unit->scale;
497 }
498
499 return pow($base, (float) $unit->scale);
500 }
501 }
502
503 return 0;
504 }
505}
Class of dictionary type of thirdparty (used by imports)
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.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
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.
Parent class of all other dictionary classes.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.