price_expression.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Ion Agorria <ion@agorria.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/product/dynamic_price/class/price_expression.class.php
  21. * \ingroup product
  22. * \brief Class for accessing price expression table
  23. */
  24. /**
  25. * Class for accesing price expression table
  26. */
  27. class PriceExpression
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error = '';
  37. /**
  38. * @var string[] Error codes (or messages)
  39. */
  40. public $errors = array();
  41. /**
  42. * @var int ID
  43. */
  44. public $id;
  45. public $title;
  46. public $expression;
  47. /**
  48. * @var string Name of table without prefix where object is stored
  49. */
  50. public $table_element = "c_price_expression";
  51. /**
  52. * Constructor
  53. *
  54. * @param DoliDb $db Database handler
  55. */
  56. public function __construct($db)
  57. {
  58. $this->db = $db;
  59. }
  60. /**
  61. * Create object into database
  62. *
  63. * @param User $user User that creates
  64. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  65. * @return int <0 if KO, Id of created object if OK
  66. */
  67. public function create($user, $notrigger = 0)
  68. {
  69. $error = 0;
  70. // Clean parameters
  71. if (isset($this->title)) {
  72. $this->title = trim($this->title);
  73. }
  74. if (isset($this->expression)) {
  75. $this->expression = trim($this->expression);
  76. }
  77. // Insert request
  78. $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
  79. $sql .= "title, expression";
  80. $sql .= ") VALUES (";
  81. $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
  82. $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
  83. $sql .= ")";
  84. $this->db->begin();
  85. dol_syslog(__METHOD__, LOG_DEBUG);
  86. $resql = $this->db->query($sql);
  87. if (!$resql) {
  88. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  89. }
  90. if (!$error) {
  91. $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
  92. }
  93. // Commit or rollback
  94. if ($error) {
  95. foreach ($this->errors as $errmsg) {
  96. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  97. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  98. }
  99. $this->db->rollback();
  100. return -1 * $error;
  101. } else {
  102. $this->db->commit();
  103. return $this->id;
  104. }
  105. }
  106. /**
  107. * Load object in memory from the database
  108. *
  109. * @param int $id Id object
  110. * @return int < 0 if KO, 0 if OK but not found, > 0 if OK
  111. */
  112. public function fetch($id)
  113. {
  114. // Check parameters
  115. if (empty($id)) {
  116. $this->error = 'ErrorWrongParameters';
  117. return -1;
  118. }
  119. $sql = "SELECT title, expression";
  120. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  121. $sql .= " WHERE rowid = ".((int) $id);
  122. dol_syslog(__METHOD__);
  123. $resql = $this->db->query($sql);
  124. if ($resql) {
  125. $obj = $this->db->fetch_object($resql);
  126. if ($obj) {
  127. $this->id = $id;
  128. $this->title = $obj->title;
  129. $this->expression = $obj->expression;
  130. return 1;
  131. } else {
  132. return 0;
  133. }
  134. } else {
  135. $this->error = "Error ".$this->db->lasterror();
  136. return -1;
  137. }
  138. }
  139. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  140. /**
  141. * List all price expressions
  142. *
  143. * @return array Array of price expressions
  144. */
  145. public function list_price_expression()
  146. {
  147. // phpcs:enable
  148. $sql = "SELECT rowid, title, expression";
  149. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  150. $sql .= " ORDER BY title";
  151. dol_syslog(__METHOD__, LOG_DEBUG);
  152. $resql = $this->db->query($sql);
  153. if ($resql) {
  154. $retarray = array();
  155. while ($record = $this->db->fetch_array($resql)) {
  156. $price_expression_obj = new PriceExpression($this->db);
  157. $price_expression_obj->id = $record["rowid"];
  158. $price_expression_obj->title = $record["title"];
  159. $price_expression_obj->expression = $record["expression"];
  160. $retarray[] = $price_expression_obj;
  161. }
  162. $this->db->free($resql);
  163. return $retarray;
  164. } else {
  165. $this->error = $this->db->error();
  166. return -1;
  167. }
  168. }
  169. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  170. /**
  171. * Returns any existing rowid with specified title
  172. *
  173. * @param String $title Title of expression
  174. * @return int < 0 if KO, 0 if OK but not found, > 0 rowid
  175. */
  176. public function find_title($title)
  177. {
  178. // phpcs:enable
  179. $sql = "SELECT rowid";
  180. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  181. $sql .= " WHERE title = '".$this->db->escape($title)."'";
  182. dol_syslog(__METHOD__, LOG_DEBUG);
  183. $resql = $this->db->query($sql);
  184. if ($resql) {
  185. $obj = $this->db->fetch_object($resql);
  186. if ($obj) {
  187. return (int) $obj->rowid;
  188. } else {
  189. return 0;
  190. }
  191. } else {
  192. $this->error = "Error ".$this->db->lasterror();
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Update object into database
  198. *
  199. * @param User $user User that modifies
  200. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  201. * @return int <0 if KO, >0 if OK
  202. */
  203. public function update($user = 0, $notrigger = 0)
  204. {
  205. $error = 0;
  206. // Clean parameters
  207. if (isset($this->title)) {
  208. $this->title = trim($this->title);
  209. }
  210. if (isset($this->expression)) {
  211. $this->expression = trim($this->expression);
  212. }
  213. // Update request
  214. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  215. $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
  216. $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''")."";
  217. $sql .= " WHERE rowid = ".((int) $this->id);
  218. $this->db->begin();
  219. dol_syslog(__METHOD__);
  220. $resql = $this->db->query($sql);
  221. if (!$resql) {
  222. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  223. }
  224. // Commit or rollback
  225. if ($error) {
  226. foreach ($this->errors as $errmsg) {
  227. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  228. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  229. }
  230. $this->db->rollback();
  231. return -1 * $error;
  232. } else {
  233. $this->db->commit();
  234. return 1;
  235. }
  236. }
  237. /**
  238. * Delete object in database
  239. *
  240. * @param User $user User that deletes
  241. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  242. * @return int <0 if KO, >0 if OK
  243. */
  244. public function delete(User $user, $notrigger = 0)
  245. {
  246. $error = 0;
  247. $rowid = $this->id;
  248. $this->db->begin();
  249. if (!$error) {
  250. $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
  251. $sql .= " WHERE rowid = ".((int) $rowid);
  252. dol_syslog(__METHOD__);
  253. $resql = $this->db->query($sql);
  254. if (!$resql) {
  255. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  256. }
  257. }
  258. // Commit or rollback
  259. if ($error) {
  260. foreach ($this->errors as $errmsg) {
  261. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  262. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  263. }
  264. $this->db->rollback();
  265. return -1 * $error;
  266. } else {
  267. $this->db->commit();
  268. return 1;
  269. }
  270. }
  271. /**
  272. * Initialise object with example values
  273. * Id must be 0 if object instance is a specimen
  274. *
  275. * @return void
  276. */
  277. public function initAsSpecimen()
  278. {
  279. $this->id = 0;
  280. $this->expression = '';
  281. }
  282. }