dolibarr 21.0.0-alpha
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25// Put here all includes required by your class file
26require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
27
28
32class CUnits extends CommonDict
33{
37 public $records = array();
38
39 //var $element='ctypent'; //!< Id that identify managed objects
40 //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
41
47 public $libelle;
48
52 public $sortorder;
56 public $short_label;
60 public $unit_type;
64 public $scale;
65
66
72 public function __construct($db)
73 {
74 $this->db = $db;
75 }
76
77
85 public function create($user, $notrigger = 0)
86 {
87 $error = 0;
88
89 // Clean parameters
90
91 if (isset($this->id)) {
92 $this->id = (int) $this->id;
93 }
94 if (isset($this->code)) {
95 $this->code = trim($this->code);
96 }
97 if (isset($this->label)) {
98 $this->libelle = trim($this->label);
99 }
100 if (isset($this->short_label)) {
101 $this->libelle = trim($this->short_label);
102 }
103 if (isset($this->unit_type)) {
104 $this->unit_type = trim($this->unit_type);
105 }
106 if (isset($this->active)) {
107 $this->active = (int) $this->active;
108 }
109 if (isset($this->scale)) {
110 $this->scale = trim($this->scale);
111 }
112
113 // Check parameters
114 // Put here code to add control on parameters values
115
116 // Insert request
117 $sql = "INSERT INTO ".$this->db->prefix()."c_units(";
118 $sql .= "rowid,";
119 $sql .= "code,";
120 $sql .= "label,";
121 $sql .= "short_label,";
122 $sql .= "unit_type,";
123 $sql .= "scale";
124 $sql .= ") VALUES (";
125 $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
126 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
127 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
128 $sql .= " ".(!isset($this->short_label) ? 'NULL' : "'".$this->db->escape($this->short_label)."'").",";
129 $sql .= " ".(!isset($this->unit_type) ? 'NULL' : "'".$this->db->escape($this->unit_type)."'").",";
130 $sql .= " ".(!isset($this->scale) ? 'NULL' : "'".$this->db->escape($this->scale)."'");
131 $sql .= ")";
132
133 $this->db->begin();
134
135 dol_syslog(get_class($this)."::create", LOG_DEBUG);
136 $resql = $this->db->query($sql);
137 if (!$resql) {
138 $error++;
139 $this->errors[] = "Error ".$this->db->lasterror();
140 }
141
142 if (!$error) {
143 $this->id = $this->db->last_insert_id($this->db->prefix()."c_units");
144 }
145
146 // Commit or rollback
147 if ($error) {
148 foreach ($this->errors as $errmsg) {
149 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
150 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
151 }
152 $this->db->rollback();
153 return -1 * $error;
154 } else {
155 $this->db->commit();
156 return $this->id;
157 }
158 }
159
160
170 public function fetch($id, $code = '', $short_label = '', $unit_type = '')
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, $filter = '', $filtermode = 'AND')
235 {
236 dol_syslog(__METHOD__, LOG_DEBUG);
237
238 $sql = "SELECT";
239 $sql .= " t.rowid,";
240 $sql .= " t.code,";
241 $sql .= " t.sortorder,";
242 $sql .= " t.label,";
243 $sql .= " t.short_label,";
244 $sql .= " t.unit_type,";
245 $sql .= " t.scale,";
246 $sql .= " t.active";
247 $sql .= " FROM ".$this->db->prefix()."c_units as t";
248 $sql .= " WHERE 1 = 1";
249
250 // Manage filter
251 if (is_array($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[] = $this->db->sanitize($key)." = ".((int) $value);
257 } elseif (strpos($key, 'date') !== false) {
258 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->idate($value)."'";
259 } elseif ($key == 't.unit_type' || $key == 't.code' || $key == 't.short_label') {
260 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
261 } else {
262 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
263 }
264 }
265 }
266 if (count($sqlwhere) > 0) {
267 $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
268 }
269
270 $filter = '';
271 }
272
273 // Manage filter
274 $errormessage = '';
275 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
276 if ($errormessage) {
277 $this->errors[] = $errormessage;
278 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
279 return -1;
280 }
281
282 if (!empty($sortfield)) {
283 $sql .= $this->db->order($sortfield, $sortorder);
284 }
285 if (!empty($limit)) {
286 $sql .= $this->db->plimit($limit, $offset);
287 }
288
289 $resql = $this->db->query($sql);
290 if ($resql) {
291 $this->records = array();
292 $num = $this->db->num_rows($resql);
293 if ($num > 0) {
294 while ($obj = $this->db->fetch_object($resql)) {
295 $record = new self($this->db);
296
297 $record->id = $obj->rowid;
298 $record->code = $obj->code;
299 $record->sortorder = $obj->sortorder;
300 $record->label = $obj->label;
301 $record->short_label = $obj->short_label;
302 $record->unit_type = $obj->unit_type;
303 $record->scale = $obj->scale;
304 $record->active = $obj->active;
305
306 $this->records[$record->id] = $record;
307 }
308 }
309 $this->db->free($resql);
310
311 return $this->records;
312 } else {
313 $this->errors[] = 'Error '.$this->db->lasterror();
314 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
315
316 return -1;
317 }
318 }
319
320
328 public function update($user = null, $notrigger = 0)
329 {
330 $error = 0;
331
332 // Clean parameters
333 if (isset($this->code)) {
334 $this->code = trim($this->code);
335 }
336 if (isset($this->sortorder)) {
337 $this->sortorder = trim($this->sortorder);
338 }
339 if (isset($this->label)) {
340 $this->libelle = trim($this->label);
341 }
342 if (isset($this->short_label)) {
343 $this->libelle = trim($this->short_label);
344 }
345 if (isset($this->unit_type)) {
346 $this->libelle = trim($this->unit_type);
347 }
348 if (isset($this->scale)) {
349 $this->scale = trim($this->scale);
350 }
351 if (isset($this->active)) {
352 $this->active = (int) $this->active;
353 }
354
355 // Check parameters
356 // Put here code to add control on parameters values
357
358 // Update request
359 $sql = "UPDATE ".$this->db->prefix()."c_units SET";
360 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
361 $sql .= " sortorder=".(isset($this->sortorder) ? "'".$this->db->escape($this->sortorder)."'" : "null").",";
362 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
363 $sql .= " short_label=".(isset($this->short_label) ? "'".$this->db->escape($this->short_label)."'" : "null").",";
364 $sql .= " unit_type=".(isset($this->unit_type) ? "'".$this->db->escape($this->unit_type)."'" : "null").",";
365 $sql .= " scale=".(isset($this->scale) ? "'".$this->db->escape($this->scale)."'" : "null").",";
366 $sql .= " active=".(isset($this->active) ? $this->active : "null");
367 $sql .= " WHERE rowid=".((int) $this->id);
368
369 $this->db->begin();
370
371 dol_syslog(get_class($this)."::update", LOG_DEBUG);
372 $resql = $this->db->query($sql);
373 if (!$resql) {
374 $error++;
375 $this->errors[] = "Error ".$this->db->lasterror();
376 }
377
378 // Commit or rollback
379 if ($error) {
380 foreach ($this->errors as $errmsg) {
381 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
382 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
383 }
384 $this->db->rollback();
385 return -1 * $error;
386 } else {
387 $this->db->commit();
388 return 1;
389 }
390 }
391
392
400 public function delete($user, $notrigger = 0)
401 {
402 $error = 0;
403
404 $sql = "DELETE FROM ".$this->db->prefix()."c_units";
405 $sql .= " WHERE rowid=".((int) $this->id);
406
407 $this->db->begin();
408
409 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
410 $resql = $this->db->query($sql);
411 if (!$resql) {
412 $error++;
413 $this->errors[] = "Error ".$this->db->lasterror();
414 }
415
416 // Commit or rollback
417 if ($error) {
418 foreach ($this->errors as $errmsg) {
419 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
420 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
421 }
422 $this->db->rollback();
423 return -1 * $error;
424 } else {
425 $this->db->commit();
426 return 1;
427 }
428 }
429
430
438 public function getUnitFromCode($code, $mode = 'code', $unit_type = '')
439 {
440 if ($mode == 'short_label' || $mode == 'code') {
441 return dol_getIdFromCode($this->db, $code, 'c_units', $mode, 'rowid', 0, " AND unit_type = '".$this->db->escape($unit_type)."'");
442 }
443 return $code;
444 }
445
453 public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
454 {
455 $value = (float) price2num($value);
456 $fk_unit = intval($fk_unit);
457
458 // Calcul en unité de base
459 $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
460
461 // convert to standard unit
462 $value *= $scaleUnitPow;
463 if ($fk_new_unit != 0) {
464 // Calcul en unité de base
465 $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
466 if (!empty($scaleUnitPow)) {
467 // convert to new unit
468 $value /= $scaleUnitPow;
469 }
470 }
471 return round($value, 2);
472 }
473
480 public function scaleOfUnitPow($id)
481 {
482 $base = 10;
483
484 $sql = "SELECT scale, unit_type FROM ".$this->db->prefix()."c_units WHERE rowid = ".((int) $id);
485
486 $resql = $this->db->query($sql);
487 if ($resql) {
488 // TODO : add base col into unit dictionary table
489 $unit = $this->db->fetch_object($sql);
490 if ($unit) {
491 // TODO : if base exists in unit dictionary table, remove this conversion exception and update conversion infos in database.
492 // Example time hour currently scale 3600 will become scale 2 base 60
493 if ($unit->unit_type == 'time') {
494 return (float) $unit->scale;
495 }
496
497 return pow($base, (float) $unit->scale);
498 }
499 }
500
501 return 0;
502 }
503}
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.