cunits.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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)) {
  74. $this->id = (int) $this->id;
  75. }
  76. if (isset($this->code)) {
  77. $this->code = trim($this->code);
  78. }
  79. if (isset($this->label)) {
  80. $this->libelle = trim($this->label);
  81. }
  82. if (isset($this->short_label)) {
  83. $this->libelle = trim($this->short_label);
  84. }
  85. if (isset($this->unit_type)) {
  86. $this->active = trim($this->unit_type);
  87. }
  88. if (isset($this->active)) {
  89. $this->active = trim($this->active);
  90. }
  91. if (isset($this->scale)) {
  92. $this->scale = trim($this->scale);
  93. }
  94. // Check parameters
  95. // Put here code to add control on parameters values
  96. // Insert request
  97. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_units(";
  98. $sql .= "rowid,";
  99. $sql .= "code,";
  100. $sql .= "label,";
  101. $sql .= "short_label,";
  102. $sql .= "unit_type";
  103. $sql .= "scale";
  104. $sql .= ") VALUES (";
  105. $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
  106. $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
  107. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
  108. $sql .= " ".(!isset($this->short_label) ? 'NULL' : "'".$this->db->escape($this->short_label)."'").",";
  109. $sql .= " ".(!isset($this->unit_type) ? 'NULL' : "'".$this->db->escape($this->unit_type)."'");
  110. $sql .= " ".(!isset($this->scale) ? 'NULL' : "'".$this->db->escape($this->scale)."'");
  111. $sql .= ")";
  112. $this->db->begin();
  113. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  114. $resql = $this->db->query($sql);
  115. if (!$resql) {
  116. $error++;
  117. $this->errors[] = "Error ".$this->db->lasterror();
  118. }
  119. if (!$error) {
  120. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_units");
  121. }
  122. // Commit or rollback
  123. if ($error) {
  124. foreach ($this->errors as $errmsg) {
  125. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  126. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  127. }
  128. $this->db->rollback();
  129. return -1 * $error;
  130. } else {
  131. $this->db->commit();
  132. return $this->id;
  133. }
  134. }
  135. /**
  136. * Load object in memory from database
  137. *
  138. * @param int $id Id of CUnit object to fetch (rowid)
  139. * @param string $code Code
  140. * @param string $short_label Short Label ('g', 'kg', ...)
  141. * @param string $unit_type Unit type ('size', 'surface', 'volume', 'weight', ...)
  142. * @return int <0 if KO, >0 if OK
  143. */
  144. public function fetch($id, $code = '', $short_label = '', $unit_type = '')
  145. {
  146. global $langs;
  147. $sql = "SELECT";
  148. $sql .= " t.rowid,";
  149. $sql .= " t.code,";
  150. $sql .= " t.label,";
  151. $sql .= " t.short_label,";
  152. $sql .= " t.scale,";
  153. $sql .= " t.unit_type,";
  154. $sql .= " t.scale,";
  155. $sql .= " t.active";
  156. $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
  157. $sql_where = array();
  158. if ($id) {
  159. $sql_where[] = " t.rowid = ".((int) $id);
  160. }
  161. if ($unit_type) {
  162. $sql_where[] = " t.unit_type = '".$this->db->escape($unit_type)."'";
  163. }
  164. if ($code) {
  165. $sql_where[] = " t.code = '".$this->db->escape($code)."'";
  166. }
  167. if ($short_label) {
  168. $sql_where[] = " t.short_label = '".$this->db->escape($short_label)."'";
  169. }
  170. if (count($sql_where) > 0) {
  171. $sql .= ' WHERE '.implode(' AND ', $sql_where);
  172. }
  173. $resql = $this->db->query($sql);
  174. if ($resql) {
  175. if ($this->db->num_rows($resql)) {
  176. $obj = $this->db->fetch_object($resql);
  177. $this->id = $obj->rowid;
  178. $this->code = $obj->code;
  179. $this->label = $obj->label;
  180. $this->short_label = $obj->short_label;
  181. $this->scale = $obj->scale;
  182. $this->unit_type = $obj->unit_type;
  183. $this->scale = $obj->scale;
  184. $this->active = $obj->active;
  185. }
  186. $this->db->free($resql);
  187. return 1;
  188. } else {
  189. $this->error = "Error ".$this->db->lasterror();
  190. return -1;
  191. }
  192. }
  193. /**
  194. * Load list of objects in memory from the database.
  195. *
  196. * @param string $sortorder Sort Order
  197. * @param string $sortfield Sort field
  198. * @param int $limit limit
  199. * @param int $offset Offset
  200. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  201. * @param string $filtermode Filter mode (AND or OR)
  202. * @return array|int int <0 if KO, array of pages if OK
  203. */
  204. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  205. {
  206. global $conf;
  207. dol_syslog(__METHOD__, LOG_DEBUG);
  208. $sql = 'SELECT';
  209. $sql .= " t.rowid,";
  210. $sql .= " t.code,";
  211. $sql .= " t.label,";
  212. $sql .= " t.short_label,";
  213. $sql .= " t.unit_type,";
  214. $sql .= " t.scale,";
  215. $sql .= " t.active";
  216. $sql .= ' FROM '.MAIN_DB_PREFIX.'c_units as t';
  217. // Manage filter
  218. $sqlwhere = array();
  219. if (count($filter) > 0) {
  220. foreach ($filter as $key => $value) {
  221. if ($key == 't.rowid' || $key == 't.active' || $key == 't.scale') {
  222. $sqlwhere[] = $key." = ".((int) $value);
  223. } elseif (strpos($key, 'date') !== false) {
  224. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  225. } elseif ($key == 't.unit_type' || $key == 't.code' || $key == 't.short_label') {
  226. $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
  227. } else {
  228. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  229. }
  230. }
  231. }
  232. if (count($sqlwhere) > 0) {
  233. $sql .= ' WHERE ('.implode(' '.$filtermode.' ', $sqlwhere).')';
  234. }
  235. if (!empty($sortfield)) {
  236. $sql .= $this->db->order($sortfield, $sortorder);
  237. }
  238. if (!empty($limit)) {
  239. $sql .= $this->db->plimit($limit, $offset);
  240. }
  241. $resql = $this->db->query($sql);
  242. if ($resql) {
  243. $this->records = array();
  244. $num = $this->db->num_rows($resql);
  245. if ($num > 0) {
  246. while ($obj = $this->db->fetch_object($resql)) {
  247. $record = new self($this->db);
  248. $record->id = $obj->rowid;
  249. $record->code = $obj->code;
  250. $record->label = $obj->label;
  251. $record->short_label = $obj->short_label;
  252. $record->unit_type = $obj->unit_type;
  253. $record->scale = $obj->scale;
  254. $record->active = $obj->active;
  255. $this->records[$record->id] = $record;
  256. }
  257. }
  258. $this->db->free($resql);
  259. return $this->records;
  260. } else {
  261. $this->errors[] = 'Error '.$this->db->lasterror();
  262. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  263. return -1;
  264. }
  265. }
  266. /**
  267. * Update object into database
  268. *
  269. * @param User $user User that modify
  270. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  271. * @return int <0 if KO, >0 if OK
  272. */
  273. public function update($user = null, $notrigger = 0)
  274. {
  275. global $conf, $langs;
  276. $error = 0;
  277. // Clean parameters
  278. if (isset($this->code)) {
  279. $this->code = trim($this->code);
  280. }
  281. if (isset($this->label)) {
  282. $this->libelle = trim($this->label);
  283. }
  284. if (isset($this->short_label)) {
  285. $this->libelle = trim($this->short_label);
  286. }
  287. if (isset($this->unit_type)) {
  288. $this->libelle = trim($this->unit_type);
  289. }
  290. if (isset($this->scale)) {
  291. $this->scale = trim($this->scale);
  292. }
  293. if (isset($this->active)) {
  294. $this->active = trim($this->active);
  295. }
  296. // Check parameters
  297. // Put here code to add control on parameters values
  298. // Update request
  299. $sql = "UPDATE ".MAIN_DB_PREFIX."c_units SET";
  300. $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
  301. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  302. $sql .= " short_label=".(isset($this->short_label) ? "'".$this->db->escape($this->short_label)."'" : "null").",";
  303. $sql .= " unit_type=".(isset($this->unit_type) ? "'".$this->db->escape($this->unit_type)."'" : "null").",";
  304. $sql .= " scale=".(isset($this->scale) ? "'".$this->db->escape($this->scale)."'" : "null").",";
  305. $sql .= " active=".(isset($this->active) ? $this->active : "null");
  306. $sql .= " WHERE rowid=".((int) $this->id);
  307. $this->db->begin();
  308. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  309. $resql = $this->db->query($sql);
  310. if (!$resql) {
  311. $error++;
  312. $this->errors[] = "Error ".$this->db->lasterror();
  313. }
  314. // Commit or rollback
  315. if ($error) {
  316. foreach ($this->errors as $errmsg) {
  317. dol_syslog(get_class($this)."::update ".$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. * Delete object in database
  329. *
  330. * @param User $user User that delete
  331. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  332. * @return int <0 if KO, >0 if OK
  333. */
  334. public function delete($user, $notrigger = 0)
  335. {
  336. global $conf, $langs;
  337. $error = 0;
  338. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_units";
  339. $sql .= " WHERE rowid=".((int) $this->id);
  340. $this->db->begin();
  341. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  342. $resql = $this->db->query($sql);
  343. if (!$resql) {
  344. $error++;
  345. $this->errors[] = "Error ".$this->db->lasterror();
  346. }
  347. // Commit or rollback
  348. if ($error) {
  349. foreach ($this->errors as $errmsg) {
  350. dol_syslog(get_class($this)."::delete ".$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. * Get unit from code
  362. * @param string $code code of unit
  363. * @param string $mode 0= id , short_label=Use short label as value, code=use code
  364. * @return int <0 if KO, Id of code if OK
  365. */
  366. public function getUnitFromCode($code, $mode = 'code')
  367. {
  368. if ($mode == 'short_label') {
  369. return dol_getIdFromCode($this->db, $code, 'c_units', 'short_label', 'rowid');
  370. } elseif ($mode == 'code') {
  371. return dol_getIdFromCode($this->db, $code, 'c_units', 'code', 'rowid');
  372. }
  373. return $code;
  374. }
  375. /**
  376. * Unit converter
  377. * @param double $value value to convert
  378. * @param int $fk_unit current unit id of value
  379. * @param int $fk_new_unit the id of unit to convert in
  380. * @return double
  381. */
  382. public function unitConverter($value, $fk_unit, $fk_new_unit = 0)
  383. {
  384. $value = floatval(price2num($value));
  385. $fk_unit = intval($fk_unit);
  386. // Calcul en unité de base
  387. $scaleUnitPow = $this->scaleOfUnitPow($fk_unit);
  388. // convert to standard unit
  389. $value = $value * $scaleUnitPow;
  390. if ($fk_new_unit != 0) {
  391. // Calcul en unité de base
  392. $scaleUnitPow = $this->scaleOfUnitPow($fk_new_unit);
  393. if (!empty($scaleUnitPow)) {
  394. // convert to new unit
  395. $value = $value / $scaleUnitPow;
  396. }
  397. }
  398. return round($value, 2);
  399. }
  400. /**
  401. * get scale of unit factor
  402. * @param int $id id of unit in dictionary
  403. * @return float|int
  404. */
  405. public function scaleOfUnitPow($id)
  406. {
  407. $base = 10;
  408. // TODO : add base col into unit dictionary table
  409. $unit = $this->db->getRow('SELECT scale, unit_type from '.MAIN_DB_PREFIX.'c_units WHERE rowid = '.intval($id));
  410. if ($unit) {
  411. // 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
  412. if ($unit->unit_type == 'time') {
  413. return floatval($unit->scale);
  414. }
  415. return pow($base, floatval($unit->scale));
  416. }
  417. return 0;
  418. }
  419. }