dolibarr 20.0.0
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{
34 public $records = array();
35
36 //var $element='ctypent'; //!< Id that identify managed objects
37 //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
38
44 public $libelle;
45
46 public $sortorder;
47 public $short_label;
48 public $unit_type;
49 public $scale;
50
51
57 public function __construct($db)
58 {
59 $this->db = $db;
60 }
61
62
70 public function create($user, $notrigger = 0)
71 {
72 $error = 0;
73
74 // Clean parameters
75
76 if (isset($this->id)) {
77 $this->id = (int) $this->id;
78 }
79 if (isset($this->code)) {
80 $this->code = trim($this->code);
81 }
82 if (isset($this->label)) {
83 $this->libelle = trim($this->label);
84 }
85 if (isset($this->short_label)) {
86 $this->libelle = trim($this->short_label);
87 }
88 if (isset($this->unit_type)) {
89 $this->unit_type = trim($this->unit_type);
90 }
91 if (isset($this->active)) {
92 $this->active = (int) $this->active;
93 }
94 if (isset($this->scale)) {
95 $this->scale = trim($this->scale);
96 }
97
98 // Check parameters
99 // Put here code to add control on parameters values
100
101 // Insert request
102 $sql = "INSERT INTO ".$this->db->prefix()."c_units(";
103 $sql .= "rowid,";
104 $sql .= "code,";
105 $sql .= "label,";
106 $sql .= "short_label,";
107 $sql .= "unit_type,";
108 $sql .= "scale";
109 $sql .= ") VALUES (";
110 $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
111 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
112 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
113 $sql .= " ".(!isset($this->short_label) ? 'NULL' : "'".$this->db->escape($this->short_label)."'").",";
114 $sql .= " ".(!isset($this->unit_type) ? 'NULL' : "'".$this->db->escape($this->unit_type)."'").",";
115 $sql .= " ".(!isset($this->scale) ? 'NULL' : "'".$this->db->escape($this->scale)."'");
116 $sql .= ")";
117
118 $this->db->begin();
119
120 dol_syslog(get_class($this)."::create", LOG_DEBUG);
121 $resql = $this->db->query($sql);
122 if (!$resql) {
123 $error++;
124 $this->errors[] = "Error ".$this->db->lasterror();
125 }
126
127 if (!$error) {
128 $this->id = $this->db->last_insert_id($this->db->prefix()."c_units");
129 }
130
131 // Commit or rollback
132 if ($error) {
133 foreach ($this->errors as $errmsg) {
134 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
135 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
136 }
137 $this->db->rollback();
138 return -1 * $error;
139 } else {
140 $this->db->commit();
141 return $this->id;
142 }
143 }
144
145
155 public function fetch($id, $code = '', $short_label = '', $unit_type = '')
156 {
157 $sql = "SELECT";
158 $sql .= " t.rowid,";
159 $sql .= " t.code,";
160 $sql .= " t.label,";
161 $sql .= " t.short_label,";
162 $sql .= " t.scale,";
163 $sql .= " t.unit_type,";
164 $sql .= " t.scale,";
165 $sql .= " t.active";
166 $sql .= " FROM ".$this->db->prefix()."c_units as t";
167 $sql_where = array();
168 if ($id) {
169 $sql_where[] = " t.rowid = ".((int) $id);
170 }
171 if ($unit_type) {
172 $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
173 }
174 if ($code) {
175 $sql_where[] = " t.code = '".$this->db->escape($code)."'";
176 }
177 if ($short_label) {
178 $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
179 }
180 if (count($sql_where) > 0) {
181 $sql .= ' WHERE '.implode(' AND ', $sql_where);
182 }
183
184 $resql = $this->db->query($sql);
185 if ($resql) {
186 if ($this->db->num_rows($resql)) {
187 $obj = $this->db->fetch_object($resql);
188
189 $this->id = $obj->rowid;
190 $this->code = $obj->code;
191 $this->label = $obj->label;
192 $this->short_label = $obj->short_label;
193 $this->scale = $obj->scale;
194 $this->unit_type = $obj->unit_type;
195 $this->scale = $obj->scale;
196 $this->active = $obj->active;
197 }
198 $this->db->free($resql);
199
200 return 1;
201 } else {
202 $this->error = "Error ".$this->db->lasterror();
203 return -1;
204 }
205 }
206
207
219 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
220 {
221 dol_syslog(__METHOD__, LOG_DEBUG);
222
223 $sql = "SELECT";
224 $sql .= " t.rowid,";
225 $sql .= " t.code,";
226 $sql .= " t.sortorder,";
227 $sql .= " t.label,";
228 $sql .= " t.short_label,";
229 $sql .= " t.unit_type,";
230 $sql .= " t.scale,";
231 $sql .= " t.active";
232 $sql .= " FROM ".$this->db->prefix()."c_units as t";
233 $sql .= " WHERE 1 = 1";
234
235 // Manage filter
236 if (is_array($filter)) {
237 $sqlwhere = array();
238 if (count($filter) > 0) {
239 foreach ($filter as $key => $value) {
240 if ($key == 't.rowid' || $key == 't.active' || $key == 't.scale') {
241 $sqlwhere[] = $this->db->sanitize($key)." = ".((int) $value);
242 } elseif (strpos($key, 'date') !== false) {
243 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->idate($value)."'";
244 } elseif ($key == 't.unit_type' || $key == 't.code' || $key == 't.short_label') {
245 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
246 } else {
247 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
248 }
249 }
250 }
251 if (count($sqlwhere) > 0) {
252 $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
253 }
254
255 $filter = '';
256 }
257
258 // Manage filter
259 $errormessage = '';
260 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
261 if ($errormessage) {
262 $this->errors[] = $errormessage;
263 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
264 return -1;
265 }
266
267 if (!empty($sortfield)) {
268 $sql .= $this->db->order($sortfield, $sortorder);
269 }
270 if (!empty($limit)) {
271 $sql .= $this->db->plimit($limit, $offset);
272 }
273
274 $resql = $this->db->query($sql);
275 if ($resql) {
276 $this->records = array();
277 $num = $this->db->num_rows($resql);
278 if ($num > 0) {
279 while ($obj = $this->db->fetch_object($resql)) {
280 $record = new self($this->db);
281
282 $record->id = $obj->rowid;
283 $record->code = $obj->code;
284 $record->sortorder = $obj->sortorder;
285 $record->label = $obj->label;
286 $record->short_label = $obj->short_label;
287 $record->unit_type = $obj->unit_type;
288 $record->scale = $obj->scale;
289 $record->active = $obj->active;
290
291 $this->records[$record->id] = $record;
292 }
293 }
294 $this->db->free($resql);
295
296 return $this->records;
297 } else {
298 $this->errors[] = 'Error '.$this->db->lasterror();
299 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
300
301 return -1;
302 }
303 }
304
305
313 public function update($user = null, $notrigger = 0)
314 {
315 $error = 0;
316
317 // Clean parameters
318 if (isset($this->code)) {
319 $this->code = trim($this->code);
320 }
321 if (isset($this->sortorder)) {
322 $this->sortorder = trim($this->sortorder);
323 }
324 if (isset($this->label)) {
325 $this->libelle = trim($this->label);
326 }
327 if (isset($this->short_label)) {
328 $this->libelle = trim($this->short_label);
329 }
330 if (isset($this->unit_type)) {
331 $this->libelle = trim($this->unit_type);
332 }
333 if (isset($this->scale)) {
334 $this->scale = trim($this->scale);
335 }
336 if (isset($this->active)) {
337 $this->active = (int) $this->active;
338 }
339
340 // Check parameters
341 // Put here code to add control on parameters values
342
343 // Update request
344 $sql = "UPDATE ".$this->db->prefix()."c_units SET";
345 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
346 $sql .= " sortorder=".(isset($this->sortorder) ? "'".$this->db->escape($this->sortorder)."'" : "null").",";
347 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
348 $sql .= " short_label=".(isset($this->short_label) ? "'".$this->db->escape($this->short_label)."'" : "null").",";
349 $sql .= " unit_type=".(isset($this->unit_type) ? "'".$this->db->escape($this->unit_type)."'" : "null").",";
350 $sql .= " scale=".(isset($this->scale) ? "'".$this->db->escape($this->scale)."'" : "null").",";
351 $sql .= " active=".(isset($this->active) ? $this->active : "null");
352 $sql .= " WHERE rowid=".((int) $this->id);
353
354 $this->db->begin();
355
356 dol_syslog(get_class($this)."::update", LOG_DEBUG);
357 $resql = $this->db->query($sql);
358 if (!$resql) {
359 $error++;
360 $this->errors[] = "Error ".$this->db->lasterror();
361 }
362
363 // Commit or rollback
364 if ($error) {
365 foreach ($this->errors as $errmsg) {
366 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
367 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
368 }
369 $this->db->rollback();
370 return -1 * $error;
371 } else {
372 $this->db->commit();
373 return 1;
374 }
375 }
376
377
385 public function delete($user, $notrigger = 0)
386 {
387 $error = 0;
388
389 $sql = "DELETE FROM ".$this->db->prefix()."c_units";
390 $sql .= " WHERE rowid=".((int) $this->id);
391
392 $this->db->begin();
393
394 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
395 $resql = $this->db->query($sql);
396 if (!$resql) {
397 $error++;
398 $this->errors[] = "Error ".$this->db->lasterror();
399 }
400
401 // Commit or rollback
402 if ($error) {
403 foreach ($this->errors as $errmsg) {
404 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
405 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
406 }
407 $this->db->rollback();
408 return -1 * $error;
409 } else {
410 $this->db->commit();
411 return 1;
412 }
413 }
414
415
423 public function getUnitFromCode($code, $mode = 'code', $unit_type = '')
424 {
425 if ($mode == 'short_label' || $mode == 'code') {
426 return dol_getIdFromCode($this->db, $code, 'c_units', $mode, 'rowid', 0, " AND unit_type = '".$this->db->escape($unit_type)."'");
427 }
428 return $code;
429 }
430
438 public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
439 {
440 $value = (float) price2num($value);
441 $fk_unit = intval($fk_unit);
442
443 // Calcul en unité de base
444 $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
445
446 // convert to standard unit
447 $value = $value * $scaleUnitPow;
448 if ($fk_new_unit != 0) {
449 // Calcul en unité de base
450 $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
451 if (!empty($scaleUnitPow)) {
452 // convert to new unit
453 $value = $value / $scaleUnitPow;
454 }
455 }
456 return round($value, 2);
457 }
458
465 public function scaleOfUnitPow($id)
466 {
467 $base = 10;
468
469 $sql = "SELECT scale, unit_type FROM ".$this->db->prefix()."c_units WHERE rowid = ".((int) $id);
470
471 $resql = $this->db->query($sql);
472 if ($resql) {
473 // TODO : add base col into unit dictionary table
474 $unit = $this->db->fetch_object($sql);
475 if ($unit) {
476 // TODO : if base exists in unit dictionary table, remove this conversion exception and update conversion infos in database.
477 // Example time hour currently scale 3600 will become scale 2 base 60
478 if ($unit->unit_type == 'time') {
479 return (float) $unit->scale;
480 }
481
482 return pow($base, (float) $unit->scale);
483 }
484 }
485
486 return 0;
487 }
488}
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.