cunits.class.php 13 KB

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