price_expression.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 Return integer <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++;
  89. $this->errors[] = "Error ".$this->db->lasterror();
  90. }
  91. if (!$error) {
  92. $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
  93. }
  94. // Commit or rollback
  95. if ($error) {
  96. foreach ($this->errors as $errmsg) {
  97. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  98. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  99. }
  100. $this->db->rollback();
  101. return -1 * $error;
  102. } else {
  103. $this->db->commit();
  104. return $this->id;
  105. }
  106. }
  107. /**
  108. * Load object in memory from the database
  109. *
  110. * @param int $id Id object
  111. * @return int Return integer < 0 if KO, 0 if OK but not found, > 0 if OK
  112. */
  113. public function fetch($id)
  114. {
  115. // Check parameters
  116. if (empty($id)) {
  117. $this->error = 'ErrorWrongParameters';
  118. return -1;
  119. }
  120. $sql = "SELECT title, expression";
  121. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  122. $sql .= " WHERE rowid = ".((int) $id);
  123. dol_syslog(__METHOD__);
  124. $resql = $this->db->query($sql);
  125. if ($resql) {
  126. $obj = $this->db->fetch_object($resql);
  127. if ($obj) {
  128. $this->id = $id;
  129. $this->title = $obj->title;
  130. $this->expression = $obj->expression;
  131. return 1;
  132. } else {
  133. return 0;
  134. }
  135. } else {
  136. $this->error = "Error ".$this->db->lasterror();
  137. return -1;
  138. }
  139. }
  140. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  141. /**
  142. * List all price expressions
  143. *
  144. * @return array|int Array of price expressions, <0 if ko
  145. */
  146. public function list_price_expression()
  147. {
  148. // phpcs:enable
  149. $sql = "SELECT rowid, title, expression";
  150. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  151. $sql .= " ORDER BY title";
  152. dol_syslog(__METHOD__, LOG_DEBUG);
  153. $resql = $this->db->query($sql);
  154. if ($resql) {
  155. $retarray = array();
  156. while ($record = $this->db->fetch_array($resql)) {
  157. $price_expression_obj = new PriceExpression($this->db);
  158. $price_expression_obj->id = $record["rowid"];
  159. $price_expression_obj->title = $record["title"];
  160. $price_expression_obj->expression = $record["expression"];
  161. $retarray[] = $price_expression_obj;
  162. }
  163. $this->db->free($resql);
  164. return $retarray;
  165. } else {
  166. $this->error = $this->db->error();
  167. return -1;
  168. }
  169. }
  170. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  171. /**
  172. * Returns any existing rowid with specified title
  173. *
  174. * @param String $title Title of expression
  175. * @return int Return integer < 0 if KO, 0 if OK but not found, > 0 rowid
  176. */
  177. public function find_title($title)
  178. {
  179. // phpcs:enable
  180. $sql = "SELECT rowid";
  181. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  182. $sql .= " WHERE title = '".$this->db->escape($title)."'";
  183. dol_syslog(__METHOD__, LOG_DEBUG);
  184. $resql = $this->db->query($sql);
  185. if ($resql) {
  186. $obj = $this->db->fetch_object($resql);
  187. if ($obj) {
  188. return (int) $obj->rowid;
  189. } else {
  190. return 0;
  191. }
  192. } else {
  193. $this->error = "Error ".$this->db->lasterror();
  194. return -1;
  195. }
  196. }
  197. /**
  198. * Update object into database
  199. *
  200. * @param User $user User that modifies
  201. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  202. * @return int Return integer <0 if KO, >0 if OK
  203. */
  204. public function update($user = 0, $notrigger = 0)
  205. {
  206. $error = 0;
  207. // Clean parameters
  208. if (isset($this->title)) {
  209. $this->title = trim($this->title);
  210. }
  211. if (isset($this->expression)) {
  212. $this->expression = trim($this->expression);
  213. }
  214. // Update request
  215. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  216. $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
  217. $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
  218. $sql .= " WHERE rowid = ".((int) $this->id);
  219. $this->db->begin();
  220. dol_syslog(__METHOD__);
  221. $resql = $this->db->query($sql);
  222. if (!$resql) {
  223. $error++;
  224. $this->errors[] = "Error ".$this->db->lasterror();
  225. }
  226. // Commit or rollback
  227. if ($error) {
  228. foreach ($this->errors as $errmsg) {
  229. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  230. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  231. }
  232. $this->db->rollback();
  233. return -1 * $error;
  234. } else {
  235. $this->db->commit();
  236. return 1;
  237. }
  238. }
  239. /**
  240. * Delete object in database
  241. *
  242. * @param User $user User that deletes
  243. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  244. * @return int Return integer <0 if KO, >0 if OK
  245. */
  246. public function delete(User $user, $notrigger = 0)
  247. {
  248. $error = 0;
  249. $rowid = $this->id;
  250. $this->db->begin();
  251. if (!$error) {
  252. $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
  253. $sql .= " WHERE rowid = ".((int) $rowid);
  254. dol_syslog(__METHOD__);
  255. $resql = $this->db->query($sql);
  256. if (!$resql) {
  257. $error++;
  258. $this->errors[] = "Error ".$this->db->lasterror();
  259. }
  260. }
  261. // Commit or rollback
  262. if ($error) {
  263. foreach ($this->errors as $errmsg) {
  264. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  265. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  266. }
  267. $this->db->rollback();
  268. return -1 * $error;
  269. } else {
  270. $this->db->commit();
  271. return 1;
  272. }
  273. }
  274. /**
  275. * Initialise object with example values
  276. * Id must be 0 if object instance is a specimen
  277. *
  278. * @return void
  279. */
  280. public function initAsSpecimen()
  281. {
  282. $this->id = 0;
  283. $this->expression = '';
  284. }
  285. }