dolibarr 19.0.3
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
24// Put here all includes required by your class file
25require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
26
27
31class CUnits extends CommonDict
32{
33 public $records = array();
34
35 //var $element='ctypent'; //!< Id that identify managed objects
36 //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
37
43 public $libelle;
44
45 public $sortorder;
46 public $short_label;
47 public $unit_type;
48 public $scale;
49
50
56 public function __construct($db)
57 {
58 $this->db = $db;
59 }
60
61
69 public function create($user, $notrigger = 0)
70 {
71 $error = 0;
72
73 // Clean parameters
74
75 if (isset($this->id)) {
76 $this->id = (int) $this->id;
77 }
78 if (isset($this->code)) {
79 $this->code = trim($this->code);
80 }
81 if (isset($this->label)) {
82 $this->libelle = trim($this->label);
83 }
84 if (isset($this->short_label)) {
85 $this->libelle = trim($this->short_label);
86 }
87 if (isset($this->unit_type)) {
88 $this->unit_type = trim($this->unit_type);
89 }
90 if (isset($this->active)) {
91 $this->active = trim($this->active);
92 }
93 if (isset($this->scale)) {
94 $this->scale = trim($this->scale);
95 }
96
97 // Check parameters
98 // Put here code to add control on parameters values
99
100 // Insert request
101 $sql = "INSERT INTO ".$this->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) {
122 $error++;
123 $this->errors[] = "Error ".$this->db->lasterror();
124 }
125
126 if (!$error) {
127 $this->id = $this->db->last_insert_id($this->db->prefix()."c_units");
128 }
129
130 // Commit or rollback
131 if ($error) {
132 foreach ($this->errors as $errmsg) {
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 $sql = "SELECT";
157 $sql .= " t.rowid,";
158 $sql .= " t.code,";
159 $sql .= " t.label,";
160 $sql .= " t.short_label,";
161 $sql .= " t.scale,";
162 $sql .= " t.unit_type,";
163 $sql .= " t.scale,";
164 $sql .= " t.active";
165 $sql .= " FROM ".$this->db->prefix()."c_units as t";
166 $sql_where = array();
167 if ($id) {
168 $sql_where[] = " t.rowid = ".((int) $id);
169 }
170 if ($unit_type) {
171 $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
172 }
173 if ($code) {
174 $sql_where[] = " t.code = '".$this->db->escape($code)."'";
175 }
176 if ($short_label) {
177 $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
178 }
179 if (count($sql_where) > 0) {
180 $sql .= ' WHERE '.implode(' AND ', $sql_where);
181 }
182
183 $resql = $this->db->query($sql);
184 if ($resql) {
185 if ($this->db->num_rows($resql)) {
186 $obj = $this->db->fetch_object($resql);
187
188 $this->id = $obj->rowid;
189 $this->code = $obj->code;
190 $this->label = $obj->label;
191 $this->short_label = $obj->short_label;
192 $this->scale = $obj->scale;
193 $this->unit_type = $obj->unit_type;
194 $this->scale = $obj->scale;
195 $this->active = $obj->active;
196 }
197 $this->db->free($resql);
198
199 return 1;
200 } else {
201 $this->error = "Error ".$this->db->lasterror();
202 return -1;
203 }
204 }
205
206
218 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
219 {
220 dol_syslog(__METHOD__, LOG_DEBUG);
221
222 $sql = "SELECT";
223 $sql .= " t.rowid,";
224 $sql .= " t.code,";
225 $sql .= " t.sortorder,";
226 $sql .= " t.label,";
227 $sql .= " t.short_label,";
228 $sql .= " t.unit_type,";
229 $sql .= " t.scale,";
230 $sql .= " t.active";
231 $sql .= " FROM ".$this->db->prefix()."c_units as t";
232 // Manage filter
233 $sqlwhere = array();
234 if (count($filter) > 0) {
235 foreach ($filter as $key => $value) {
236 if ($key == 't.rowid' || $key == 't.active' || $key == 't.scale') {
237 $sqlwhere[] = $key." = ".((int) $value);
238 } elseif (strpos($key, 'date') !== false) {
239 $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
240 } elseif ($key == 't.unit_type' || $key == 't.code' || $key == 't.short_label') {
241 $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
242 } else {
243 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
244 }
245 }
246 }
247 if (count($sqlwhere) > 0) {
248 $sql .= ' WHERE ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
249 }
250
251 if (!empty($sortfield)) {
252 $sql .= $this->db->order($sortfield, $sortorder);
253 }
254 if (!empty($limit)) {
255 $sql .= $this->db->plimit($limit, $offset);
256 }
257
258 $resql = $this->db->query($sql);
259 if ($resql) {
260 $this->records = array();
261 $num = $this->db->num_rows($resql);
262 if ($num > 0) {
263 while ($obj = $this->db->fetch_object($resql)) {
264 $record = new self($this->db);
265
266 $record->id = $obj->rowid;
267 $record->code = $obj->code;
268 $record->sortorder = $obj->sortorder;
269 $record->label = $obj->label;
270 $record->short_label = $obj->short_label;
271 $record->unit_type = $obj->unit_type;
272 $record->scale = $obj->scale;
273 $record->active = $obj->active;
274
275 $this->records[$record->id] = $record;
276 }
277 }
278 $this->db->free($resql);
279
280 return $this->records;
281 } else {
282 $this->errors[] = 'Error '.$this->db->lasterror();
283 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
284
285 return -1;
286 }
287 }
288
289
297 public function update($user = null, $notrigger = 0)
298 {
299 $error = 0;
300
301 // Clean parameters
302 if (isset($this->code)) {
303 $this->code = trim($this->code);
304 }
305 if (isset($this->sortorder)) {
306 $this->sortorder = trim($this->sortorder);
307 }
308 if (isset($this->label)) {
309 $this->libelle = trim($this->label);
310 }
311 if (isset($this->short_label)) {
312 $this->libelle = trim($this->short_label);
313 }
314 if (isset($this->unit_type)) {
315 $this->libelle = trim($this->unit_type);
316 }
317 if (isset($this->scale)) {
318 $this->scale = trim($this->scale);
319 }
320 if (isset($this->active)) {
321 $this->active = trim($this->active);
322 }
323
324 // Check parameters
325 // Put here code to add control on parameters values
326
327 // Update request
328 $sql = "UPDATE ".$this->db->prefix()."c_units SET";
329 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
330 $sql .= " sortorder=".(isset($this->sortorder) ? "'".$this->db->escape($this->sortorder)."'" : "null").",";
331 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
332 $sql .= " short_label=".(isset($this->short_label) ? "'".$this->db->escape($this->short_label)."'" : "null").",";
333 $sql .= " unit_type=".(isset($this->unit_type) ? "'".$this->db->escape($this->unit_type)."'" : "null").",";
334 $sql .= " scale=".(isset($this->scale) ? "'".$this->db->escape($this->scale)."'" : "null").",";
335 $sql .= " active=".(isset($this->active) ? $this->active : "null");
336 $sql .= " WHERE rowid=".((int) $this->id);
337
338 $this->db->begin();
339
340 dol_syslog(get_class($this)."::update", LOG_DEBUG);
341 $resql = $this->db->query($sql);
342 if (!$resql) {
343 $error++;
344 $this->errors[] = "Error ".$this->db->lasterror();
345 }
346
347 // Commit or rollback
348 if ($error) {
349 foreach ($this->errors as $errmsg) {
350 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
351 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
352 }
353 $this->db->rollback();
354 return -1 * $error;
355 } else {
356 $this->db->commit();
357 return 1;
358 }
359 }
360
361
369 public function delete($user, $notrigger = 0)
370 {
371 $error = 0;
372
373 $sql = "DELETE FROM ".$this->db->prefix()."c_units";
374 $sql .= " WHERE rowid=".((int) $this->id);
375
376 $this->db->begin();
377
378 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
379 $resql = $this->db->query($sql);
380 if (!$resql) {
381 $error++;
382 $this->errors[] = "Error ".$this->db->lasterror();
383 }
384
385 // Commit or rollback
386 if ($error) {
387 foreach ($this->errors as $errmsg) {
388 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
389 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
390 }
391 $this->db->rollback();
392 return -1 * $error;
393 } else {
394 $this->db->commit();
395 return 1;
396 }
397 }
398
399
407 public function getUnitFromCode($code, $mode = 'code', $unit_type = '')
408 {
409 if ($mode == 'short_label') {
410 return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid', 0, " AND unit_type = '".$this->db->escape($unit_type)."'");
411 } elseif ($mode == 'code') {
412 return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid', 0, " AND unit_type = '". $this->db->escape($unit_type) ."'");
413 }
414
415 return $code;
416 }
417
425 public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
426 {
427 $value = (float) price2num($value);
428 $fk_unit = intval($fk_unit);
429
430 // Calcul en unité de base
431 $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
432
433 // convert to standard unit
434 $value = $value * $scaleUnitPow;
435 if ($fk_new_unit != 0) {
436 // Calcul en unité de base
437 $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
438 if (!empty($scaleUnitPow)) {
439 // convert to new unit
440 $value = $value / $scaleUnitPow;
441 }
442 }
443 return round($value, 2);
444 }
445
452 public function scaleOfUnitPow($id)
453 {
454 $base = 10;
455
456 $sql = "SELECT scale, unit_type FROM ".$this->db->prefix()."c_units WHERE rowid = ".((int) $id);
457
458 $resql = $this->db->query($sql);
459 if ($resql) {
460 // TODO : add base col into unit dictionary table
461 $unit = $this->db->fetch_object($sql);
462 if ($unit) {
463 // TODO : if base exists in unit dictionary table, remove this convertion exception and update convertion infos in database.
464 // Example time hour currently scale 3600 will become scale 2 base 60
465 if ($unit->unit_type == 'time') {
466 return (float) $unit->scale;
467 }
468
469 return pow($base, (float) $unit->scale);
470 }
471 }
472
473 return 0;
474 }
475}
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.
Parent class of all other dictionary classes.
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.