cproductnature.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2020 Florian HENRY <florian.henry@scopen.fr>
  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. /**
  19. * \file htdocs/core/class/cproductnature.class.php
  20. * \ingroup core
  21. * \brief This file is CRUD class file (Create/Read/Update/Delete) for c_units dictionary
  22. */
  23. /**
  24. * Class of dictionary of nature of product (used by imports)
  25. */
  26. class CProductNature // extends CommonObject
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error = '';
  36. /**
  37. * @var string[] Error codes (or messages)
  38. */
  39. public $errors = array();
  40. /**
  41. * @var array record
  42. */
  43. public $records = array();
  44. /**
  45. * @var string element
  46. */
  47. public $element = 'cproductnbature';
  48. /**
  49. * @var string table element
  50. */
  51. public $table_element = 'c_product_nature';
  52. /**
  53. * @var int ID
  54. */
  55. public $id;
  56. /**
  57. * @var int code
  58. */
  59. public $code;
  60. /**
  61. * @var string label
  62. */
  63. public $label;
  64. /**
  65. * @var int active
  66. */
  67. public $active;
  68. /**
  69. * Constructor
  70. *
  71. * @param DoliDb $db Database handler
  72. */
  73. public function __construct($db)
  74. {
  75. $this->db = $db;
  76. }
  77. /**
  78. * Create object into database
  79. *
  80. * @param User $user User that create
  81. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  82. * @return int <0 if KO, Id of created object if OK
  83. */
  84. public function create($user, $notrigger = 0)
  85. {
  86. global $conf, $langs;
  87. // Insert request
  88. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
  89. $sql .= "rowid,";
  90. $sql .= "code,";
  91. $sql .= "label,";
  92. $sql .= "active";
  93. $sql .= ") VALUES (";
  94. $sql .= " ".(!isset($this->id) ? 'NULL' : ((int) $this->id)).",";
  95. $sql .= " ".(!isset($this->code) ? 'NULL' : ((int) $this->code)).",";
  96. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape(trim($this->label))."'").",";
  97. $sql .= " ".(!isset($this->active) ? 'NULL' : ((int) $this->active)).",";
  98. $sql .= ")";
  99. $this->db->begin();
  100. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  101. $resql = $this->db->query($sql);
  102. // Commit or rollback
  103. if (!$resql) {
  104. dol_syslog(get_class($this)."::create ".$this->db->lasterror(), LOG_ERR);
  105. $this->error = "Error ".$this->db->lasterror();
  106. $this->db->rollback();
  107. return -1;
  108. } else {
  109. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  110. $this->db->commit();
  111. return $this->id;
  112. }
  113. }
  114. /**
  115. * Load object in memory from database
  116. *
  117. * @param int $id Id of CUnit object to fetch (rowid)
  118. * @param string $code Code
  119. * @return int <0 if KO, >0 if OK
  120. */
  121. public function fetch($id, $code = '')
  122. {
  123. global $langs;
  124. $sql = "SELECT";
  125. $sql .= " t.rowid,";
  126. $sql .= " t.code,";
  127. $sql .= " t.label,";
  128. $sql .= " t.active";
  129. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  130. $sql_where = array();
  131. if ($id) {
  132. $sql_where[] = " t.rowid = ".((int) $id);
  133. }
  134. if ($code >= 0) {
  135. $sql_where[] = " t.code = ".((int) $code);
  136. }
  137. if (count($sql_where) > 0) {
  138. $sql .= ' WHERE '.implode(' AND ', $sql_where);
  139. }
  140. $resql = $this->db->query($sql);
  141. if ($resql) {
  142. if ($this->db->num_rows($resql)) {
  143. $obj = $this->db->fetch_object($resql);
  144. $this->id = $obj->rowid;
  145. $this->code = $obj->code;
  146. $this->label = $obj->label;
  147. $this->active = $obj->active;
  148. }
  149. $this->db->free($resql);
  150. return 1;
  151. } else {
  152. $this->error = "Error ".$this->db->lasterror();
  153. return -1;
  154. }
  155. }
  156. /**
  157. * Load list of objects in memory from the database.
  158. *
  159. * @param string $sortorder Sort Order
  160. * @param string $sortfield Sort field
  161. * @param int $limit limit
  162. * @param int $offset Offset
  163. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  164. * @param string $filtermode Filter mode (AND or OR)
  165. * @return array|int int <0 if KO, array of pages if OK
  166. */
  167. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  168. {
  169. global $conf;
  170. dol_syslog(__METHOD__, LOG_DEBUG);
  171. $sql = 'SELECT';
  172. $sql .= " t.rowid,";
  173. $sql .= " t.code,";
  174. $sql .= " t.label,";
  175. $sql .= " t.active";
  176. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  177. // Manage filter
  178. $sqlwhere = array();
  179. if (count($filter) > 0) {
  180. foreach ($filter as $key => $value) {
  181. if ($key == 't.rowid' || $key == 't.active' || $key == 't.code') {
  182. $sqlwhere[] = $key." = ".((int) $value);
  183. } elseif (strpos($key, 'date') !== false) {
  184. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  185. } elseif ($key == 't.label') {
  186. $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
  187. } else {
  188. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  189. }
  190. }
  191. }
  192. if (count($sqlwhere) > 0) {
  193. $sql .= ' WHERE ('.implode(' '.$filtermode.' ', $sqlwhere).')';
  194. }
  195. if (!empty($sortfield)) {
  196. $sql .= $this->db->order($sortfield, $sortorder);
  197. }
  198. if (!empty($limit)) {
  199. $sql .= $this->db->plimit($limit, $offset);
  200. }
  201. $resql = $this->db->query($sql);
  202. if ($resql) {
  203. $this->records = array();
  204. $num = $this->db->num_rows($resql);
  205. if ($num > 0) {
  206. while ($obj = $this->db->fetch_object($resql)) {
  207. $record = new self($this->db);
  208. $record->id = $obj->rowid;
  209. $record->code = $obj->code;
  210. $record->label = $obj->label;
  211. $this->records[$record->id] = $record;
  212. }
  213. }
  214. $this->db->free($resql);
  215. return $this->records;
  216. } else {
  217. $this->errors[] = 'Error '.$this->db->lasterror();
  218. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  219. return -1;
  220. }
  221. }
  222. /**
  223. * Update object into database
  224. *
  225. * @param User $user User that modify
  226. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  227. * @return int <0 if KO, >0 if OK
  228. */
  229. public function update($user = null, $notrigger = 0)
  230. {
  231. global $conf, $langs;
  232. // Update request
  233. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  234. $sql .= " code=".(isset($this->code) ? ((int) $this->code) : "null").",";
  235. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape(trim($this->label))."'" : "null").",";
  236. $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
  237. $sql .= " WHERE rowid=".(int) $this->id;
  238. $this->db->begin();
  239. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  240. $resql = $this->db->query($sql);
  241. // Commit or rollback
  242. if (!$resql) {
  243. dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
  244. $this->error = "Error ".$this->db->lasterror();
  245. $this->db->rollback();
  246. return -1;
  247. } else {
  248. $this->db->commit();
  249. return 1;
  250. }
  251. }
  252. /**
  253. * Delete object in database
  254. *
  255. * @param User $user User that delete
  256. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  257. * @return int <0 if KO, >0 if OK
  258. */
  259. public function delete($user, $notrigger = 0)
  260. {
  261. global $conf, $langs;
  262. $error = 0;
  263. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  264. $sql .= " WHERE rowid=".(int) $this->id;
  265. $this->db->begin();
  266. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  267. $resql = $this->db->query($sql);
  268. // Commit or rollback
  269. if (!$resql) {
  270. dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
  271. $this->error = "Error ".$this->db->lasterror();
  272. $this->db->rollback();
  273. return -1;
  274. } else {
  275. $this->db->commit();
  276. return 1;
  277. }
  278. }
  279. /**
  280. * Get unit from code
  281. * @param int $code code of unit
  282. * @param string $mode 0= id , short_label=Use short label as value, code=use code
  283. * @return int <0 if KO, Id of code if OK
  284. */
  285. public function getProductNatureFromCode($code, $mode = 'code')
  286. {
  287. if ($mode == 'label') {
  288. return dol_getIdFromCode($this->db, $code, $this->table_element, 'label', 'code');
  289. } elseif ($mode == 'code') {
  290. return dol_getIdFromCode($this->db, $code, $this->table_element, 'code', 'code');
  291. }
  292. return $code;
  293. }
  294. }