cunits.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. /**
  18. * \file htdocs/core/class/cunits.class.php
  19. * \ingroup core
  20. * \brief This file is CRUD class file (Create/Read/Update/Delete) for c_units dictionary
  21. */
  22. /**
  23. * Class of dictionary type of thirdparty (used by imports)
  24. */
  25. class CUnits // extends CommonObject
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. /**
  32. * @var string Error code (or message)
  33. */
  34. public $error = '';
  35. /**
  36. * @var string[] Error codes (or messages)
  37. */
  38. public $errors = array();
  39. public $records = array();
  40. //var $element='ctypent'; //!< Id that identify managed objects
  41. //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
  42. /**
  43. * @var int ID
  44. */
  45. public $id;
  46. public $code;
  47. public $label;
  48. public $short_label;
  49. public $unit_type;
  50. public $scale;
  51. public $active;
  52. /**
  53. * Constructor
  54. *
  55. * @param DoliDb $db Database handler
  56. */
  57. public function __construct($db)
  58. {
  59. $this->db = $db;
  60. }
  61. /**
  62. * Create object into database
  63. *
  64. * @param User $user User that create
  65. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  66. * @return int <0 if KO, Id of created object if OK
  67. */
  68. public function create($user, $notrigger = 0)
  69. {
  70. global $conf, $langs;
  71. $error = 0;
  72. // Clean parameters
  73. if (isset($this->id)) $this->id = (int) $this->id;
  74. if (isset($this->code)) $this->code = trim($this->code);
  75. if (isset($this->label)) $this->libelle = trim($this->label);
  76. if (isset($this->short_label)) $this->libelle = trim($this->short_label);
  77. if (isset($this->unit_type)) $this->active = trim($this->unit_type);
  78. if (isset($this->active)) $this->active = trim($this->active);
  79. if (isset($this->scale)) $this->scale = trim($this->scale);
  80. // Check parameters
  81. // Put here code to add control on parameters values
  82. // Insert request
  83. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_units(";
  84. $sql .= "rowid,";
  85. $sql .= "code,";
  86. $sql .= "label,";
  87. $sql .= "short_label,";
  88. $sql .= "unit_type";
  89. $sql .= "scale";
  90. $sql .= ") VALUES (";
  91. $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
  92. $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
  93. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
  94. $sql .= " ".(!isset($this->short_label) ? 'NULL' : "'".$this->db->escape($this->short_label)."'").",";
  95. $sql .= " ".(!isset($this->unit_type) ? 'NULL' : "'".$this->db->escape($this->unit_type)."'");
  96. $sql .= " ".(!isset($this->scale) ? 'NULL' : "'".$this->db->escape($this->scale)."'");
  97. $sql .= ")";
  98. $this->db->begin();
  99. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  100. $resql = $this->db->query($sql);
  101. if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
  102. if (!$error)
  103. {
  104. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_units");
  105. }
  106. // Commit or rollback
  107. if ($error)
  108. {
  109. foreach ($this->errors as $errmsg)
  110. {
  111. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  112. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  113. }
  114. $this->db->rollback();
  115. return -1 * $error;
  116. } else {
  117. $this->db->commit();
  118. return $this->id;
  119. }
  120. }
  121. /**
  122. * Load object in memory from database
  123. *
  124. * @param int $id Id of CUnit object to fetch (rowid)
  125. * @param string $code Code
  126. * @param string $short_label Short Label ('g', 'kg', ...)
  127. * @param string $unit_type Unit type ('size', 'surface', 'volume', 'weight', ...)
  128. * @return int <0 if KO, >0 if OK
  129. */
  130. public function fetch($id, $code = '', $short_label = '', $unit_type = '')
  131. {
  132. global $langs;
  133. $sql = "SELECT";
  134. $sql .= " t.rowid,";
  135. $sql .= " t.code,";
  136. $sql .= " t.label,";
  137. $sql .= " t.short_label,";
  138. $sql .= " t.scale,";
  139. $sql .= " t.unit_type,";
  140. $sql .= " t.scale,";
  141. $sql .= " t.active";
  142. $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
  143. $sql_where = array();
  144. if ($id) $sql_where[] = " t.rowid = ".$id;
  145. if ($unit_type) $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
  146. if ($code) $sql_where[] = " t.code = '".$this->db->escape($code)."'";
  147. if ($short_label) $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
  148. if (count($sql_where) > 0) {
  149. $sql .= ' WHERE '.implode(' AND ', $sql_where);
  150. }
  151. $resql = $this->db->query($sql);
  152. if ($resql)
  153. {
  154. if ($this->db->num_rows($resql))
  155. {
  156. $obj = $this->db->fetch_object($resql);
  157. $this->id = $obj->rowid;
  158. $this->code = $obj->code;
  159. $this->label = $obj->label;
  160. $this->short_label = $obj->short_label;
  161. $this->scale = $obj->scale;
  162. $this->unit_type = $obj->unit_type;
  163. $this->scale = $obj->scale;
  164. $this->active = $obj->active;
  165. }
  166. $this->db->free($resql);
  167. return 1;
  168. } else {
  169. $this->error = "Error ".$this->db->lasterror();
  170. return -1;
  171. }
  172. }
  173. /**
  174. * Load list of objects in memory from the database.
  175. *
  176. * @param string $sortorder Sort Order
  177. * @param string $sortfield Sort field
  178. * @param int $limit limit
  179. * @param int $offset Offset
  180. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  181. * @param string $filtermode Filter mode (AND or OR)
  182. * @return array|int int <0 if KO, array of pages if OK
  183. */
  184. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  185. {
  186. global $conf;
  187. dol_syslog(__METHOD__, LOG_DEBUG);
  188. $sql = 'SELECT';
  189. $sql .= " t.rowid,";
  190. $sql .= " t.code,";
  191. $sql .= " t.label,";
  192. $sql .= " t.short_label,";
  193. $sql .= " t.unit_type,";
  194. $sql .= " t.scale,";
  195. $sql .= " t.active";
  196. $sql .= ' FROM '.MAIN_DB_PREFIX.'c_units as t';
  197. // Manage filter
  198. $sqlwhere = array();
  199. if (count($filter) > 0) {
  200. foreach ($filter as $key => $value) {
  201. if ($key == 't.rowid' || $key == 't.active' || $key == 't.scale') {
  202. $sqlwhere[] = $key.'='.(int) $value;
  203. } elseif (strpos($key, 'date') !== false) {
  204. $sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
  205. } elseif ($key == 't.unit_type' || $key == 't.code' || $key == 't.short_label') {
  206. $sqlwhere[] = $key.' = \''.$this->db->escape($value).'\'';
  207. } else {
  208. $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
  209. }
  210. }
  211. }
  212. if (count($sqlwhere) > 0) {
  213. $sql .= ' WHERE ('.implode(' '.$filtermode.' ', $sqlwhere).')';
  214. }
  215. if (!empty($sortfield)) {
  216. $sql .= $this->db->order($sortfield, $sortorder);
  217. }
  218. if (!empty($limit)) {
  219. $sql .= ' '.$this->db->plimit($limit, $offset);
  220. }
  221. $resql = $this->db->query($sql);
  222. if ($resql) {
  223. $this->records = array();
  224. $num = $this->db->num_rows($resql);
  225. if ($num > 0) {
  226. while ($obj = $this->db->fetch_object($resql))
  227. {
  228. $record = new self($this->db);
  229. $record->id = $obj->rowid;
  230. $record->code = $obj->code;
  231. $record->label = $obj->label;
  232. $record->short_label = $obj->short_label;
  233. $record->unit_type = $obj->unit_type;
  234. $record->scale = $obj->scale;
  235. $record->active = $obj->active;
  236. $this->records[$record->id] = $record;
  237. }
  238. }
  239. $this->db->free($resql);
  240. return $this->records;
  241. } else {
  242. $this->errors[] = 'Error '.$this->db->lasterror();
  243. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  244. return -1;
  245. }
  246. }
  247. /**
  248. * Update object into database
  249. *
  250. * @param User $user User that modify
  251. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  252. * @return int <0 if KO, >0 if OK
  253. */
  254. public function update($user = null, $notrigger = 0)
  255. {
  256. global $conf, $langs;
  257. $error = 0;
  258. // Clean parameters
  259. if (isset($this->code)) $this->code = trim($this->code);
  260. if (isset($this->label)) $this->libelle = trim($this->label);
  261. if (isset($this->short_label)) $this->libelle = trim($this->short_label);
  262. if (isset($this->unit_type)) $this->libelle = trim($this->unit_type);
  263. if (isset($this->scale)) $this->scale = trim($this->scale);
  264. if (isset($this->active)) $this->active = trim($this->active);
  265. // Check parameters
  266. // Put here code to add control on parameters values
  267. // Update request
  268. $sql = "UPDATE ".MAIN_DB_PREFIX."c_units SET";
  269. $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
  270. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  271. $sql .= " short_label=".(isset($this->short_label) ? "'".$this->db->escape($this->short_label)."'" : "null").",";
  272. $sql .= " unit_type=".(isset($this->unit_type) ? "'".$this->db->escape($this->unit_type)."'" : "null").",";
  273. $sql .= " scale=".(isset($this->scale) ? "'".$this->db->escape($this->scale)."'" : "null").",";
  274. $sql .= " active=".(isset($this->active) ? $this->active : "null");
  275. $sql .= " WHERE rowid=".$this->id;
  276. $this->db->begin();
  277. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  278. $resql = $this->db->query($sql);
  279. if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
  280. // Commit or rollback
  281. if ($error)
  282. {
  283. foreach ($this->errors as $errmsg)
  284. {
  285. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  286. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  287. }
  288. $this->db->rollback();
  289. return -1 * $error;
  290. } else {
  291. $this->db->commit();
  292. return 1;
  293. }
  294. }
  295. /**
  296. * Delete object in database
  297. *
  298. * @param User $user User that delete
  299. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  300. * @return int <0 if KO, >0 if OK
  301. */
  302. public function delete($user, $notrigger = 0)
  303. {
  304. global $conf, $langs;
  305. $error = 0;
  306. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_units";
  307. $sql .= " WHERE rowid=".$this->id;
  308. $this->db->begin();
  309. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  310. $resql = $this->db->query($sql);
  311. if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
  312. // Commit or rollback
  313. if ($error)
  314. {
  315. foreach ($this->errors as $errmsg)
  316. {
  317. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  318. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  319. }
  320. $this->db->rollback();
  321. return -1 * $error;
  322. } else {
  323. $this->db->commit();
  324. return 1;
  325. }
  326. }
  327. /**
  328. * Get unit from code
  329. * @param string $code code of unit
  330. * @param string $mode 0= id , short_label=Use short label as value, code=use code
  331. * @return int <0 if KO, Id of code if OK
  332. */
  333. public function getUnitFromCode($code, $mode = 'code')
  334. {
  335. if ($mode == 'short_label'){
  336. return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid');
  337. } elseif ($mode == 'code'){
  338. return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid');
  339. }
  340. return $code;
  341. }
  342. /**
  343. * Unit converter
  344. * @param double $value value to convert
  345. * @param int $fk_unit current unit id of value
  346. * @param int $fk_new_unit the id of unit to convert in
  347. * @return double
  348. */
  349. public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
  350. {
  351. $value = doubleval(price2num($value));
  352. $fk_unit = intval($fk_unit);
  353. // Calcul en unité de base
  354. $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
  355. // convert to standard unit
  356. $value = $value * $scaleUnitPow;
  357. if ($fk_new_unit !=0 ){
  358. // Calcul en unité de base
  359. $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
  360. if (!empty($scaleUnitPow))
  361. {
  362. // convert to new unit
  363. $value = $value / $scaleUnitPow;
  364. }
  365. }
  366. return round($value, 2);
  367. }
  368. /**
  369. * get scale of unit factor
  370. * @param int $id id of unit in dictionary
  371. * @return float|int
  372. */
  373. public function scaleOfUnitPow($id)
  374. {
  375. $base = 10;
  376. // TODO : add base col into unit dictionary table
  377. $unit = $this->db->getRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id));
  378. if ($unit) {
  379. // TODO : if base exist in unit dictionary table remove this convertion exception and update convertion infos in database exemple time hour currently scale 3600 will become scale 2 base 60
  380. if ($unit->unit_type == 'time') {
  381. return doubleval($unit->scale);
  382. }
  383. return pow($base, doubleval($unit->scale));
  384. }
  385. return 0;
  386. }
  387. }