price_expression.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <http://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. 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. function create($user, $notrigger=0)
  68. {
  69. $error=0;
  70. // Clean parameters
  71. if (isset($this->title)) $this->title=trim($this->title);
  72. if (isset($this->expression)) $this->expression=trim($this->expression);
  73. // Insert request
  74. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
  75. $sql.= "title, expression";
  76. $sql.= ") VALUES (";
  77. $sql.= " ".(isset($this->title)?"'".$this->db->escape($this->title)."'":"''").",";
  78. $sql.= " ".(isset($this->expression)?"'".$this->db->escape($this->expression)."'":"''");
  79. $sql.= ")";
  80. $this->db->begin();
  81. dol_syslog(__METHOD__, LOG_DEBUG);
  82. $resql=$this->db->query($sql);
  83. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  84. if (! $error)
  85. {
  86. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  87. if (! $notrigger)
  88. {
  89. // Uncomment this and change MYOBJECT to your own tag if you
  90. // want this action calls a trigger.
  91. //// Call triggers
  92. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  93. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  94. //// End call triggers
  95. }
  96. }
  97. // Commit or rollback
  98. if ($error)
  99. {
  100. foreach($this->errors as $errmsg)
  101. {
  102. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  103. $this->error.=($this->error?', '.$errmsg:$errmsg);
  104. }
  105. $this->db->rollback();
  106. return -1*$error;
  107. }
  108. else
  109. {
  110. $this->db->commit();
  111. return $this->id;
  112. }
  113. }
  114. /**
  115. * Load object in memory from the database
  116. *
  117. * @param int $id Id object
  118. * @return int < 0 if KO, 0 if OK but not found, > 0 if OK
  119. */
  120. function fetch($id)
  121. {
  122. // Check parameters
  123. if (empty($id))
  124. {
  125. $this->error='ErrorWrongParameters';
  126. return -1;
  127. }
  128. $sql = "SELECT title, expression";
  129. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  130. $sql.= " WHERE rowid = ".$id;
  131. dol_syslog(__METHOD__);
  132. $resql=$this->db->query($sql);
  133. if ($resql)
  134. {
  135. $obj = $this->db->fetch_object($resql);
  136. if ($obj)
  137. {
  138. $this->id = $id;
  139. $this->title = $obj->title;
  140. $this->expression = $obj->expression;
  141. return 1;
  142. }
  143. else
  144. {
  145. return 0;
  146. }
  147. }
  148. else
  149. {
  150. $this->error="Error ".$this->db->lasterror();
  151. return -1;
  152. }
  153. }
  154. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  155. /**
  156. * List all price expressions
  157. *
  158. * @return array Array of price expressions
  159. */
  160. function list_price_expression()
  161. {
  162. // phpcs:enable
  163. $sql = "SELECT rowid, title, expression";
  164. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  165. $sql.= " ORDER BY title";
  166. dol_syslog(__METHOD__, LOG_DEBUG);
  167. $resql=$this->db->query($sql);
  168. if ($resql)
  169. {
  170. $retarray = array();
  171. while ($record = $this->db->fetch_array($resql))
  172. {
  173. $price_expression_obj = new PriceExpression($this->db);
  174. $price_expression_obj->id = $record["rowid"];
  175. $price_expression_obj->title = $record["title"];
  176. $price_expression_obj->expression = $record["expression"];
  177. $retarray[]=$price_expression_obj;
  178. }
  179. $this->db->free($resql);
  180. return $retarray;
  181. }
  182. else
  183. {
  184. $this->error=$this->db->error();
  185. return -1;
  186. }
  187. }
  188. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  189. /**
  190. * Returns any existing rowid with specified title
  191. *
  192. * @param String $title Title of expression
  193. * @return int < 0 if KO, 0 if OK but not found, > 0 rowid
  194. */
  195. function find_title($title)
  196. {
  197. // phpcs:enable
  198. $sql = "SELECT rowid";
  199. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  200. $sql.= " WHERE title = '".$this->db->escape($title)."'";
  201. dol_syslog(__METHOD__, LOG_DEBUG);
  202. $resql=$this->db->query($sql);
  203. if ($resql)
  204. {
  205. $obj = $this->db->fetch_object($resql);
  206. if ($obj)
  207. {
  208. return (int) $obj->rowid;
  209. }
  210. else
  211. {
  212. return 0;
  213. }
  214. }
  215. else
  216. {
  217. $this->error="Error ".$this->db->lasterror();
  218. return -1;
  219. }
  220. }
  221. /**
  222. * Update object into database
  223. *
  224. * @param User $user User that modifies
  225. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  226. * @return int <0 if KO, >0 if OK
  227. */
  228. function update($user=0, $notrigger=0)
  229. {
  230. $error=0;
  231. // Clean parameters
  232. if (isset($this->title)) $this->title=trim($this->title);
  233. if (isset($this->expression)) $this->expression=trim($this->expression);
  234. // Update request
  235. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  236. $sql.= " title = ".(isset($this->title)?"'".$this->db->escape($this->title)."'":"''").",";
  237. $sql.= " expression = ".(isset($this->expression)?"'".$this->db->escape($this->expression)."'":"''")."";
  238. $sql.= " WHERE rowid = ".$this->id;
  239. $this->db->begin();
  240. dol_syslog(__METHOD__);
  241. $resql = $this->db->query($sql);
  242. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  243. if (! $error)
  244. {
  245. if (! $notrigger)
  246. {
  247. // Uncomment this and change MYOBJECT to your own tag if you
  248. // want this action calls a trigger.
  249. //// Call triggers
  250. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  251. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  252. //// End call triggers
  253. }
  254. }
  255. // Commit or rollback
  256. if ($error)
  257. {
  258. foreach($this->errors as $errmsg)
  259. {
  260. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  261. $this->error.=($this->error?', '.$errmsg:$errmsg);
  262. }
  263. $this->db->rollback();
  264. return -1*$error;
  265. }
  266. else
  267. {
  268. $this->db->commit();
  269. return 1;
  270. }
  271. }
  272. /**
  273. * Delete object in database
  274. *
  275. * @param User $user User that deletes
  276. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  277. * @return int <0 if KO, >0 if OK
  278. */
  279. function delete(User $user, $notrigger=0)
  280. {
  281. $error=0;
  282. $rowid = $this->id;
  283. $this->db->begin();
  284. if (! $error)
  285. {
  286. if (! $notrigger)
  287. {
  288. // Uncomment this and change MYOBJECT to your own tag if you
  289. // want this action calls a trigger.
  290. //// Call triggers
  291. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  292. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  293. //// End call triggers
  294. }
  295. }
  296. if (! $error)
  297. {
  298. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  299. $sql.= " WHERE rowid = ".$rowid;
  300. dol_syslog(__METHOD__);
  301. $resql = $this->db->query($sql);
  302. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  303. }
  304. // Commit or rollback
  305. if ($error)
  306. {
  307. foreach($this->errors as $errmsg)
  308. {
  309. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  310. $this->error.=($this->error?', '.$errmsg:$errmsg);
  311. }
  312. $this->db->rollback();
  313. return -1*$error;
  314. }
  315. else
  316. {
  317. $this->db->commit();
  318. return 1;
  319. }
  320. }
  321. /**
  322. * Initialise object with example values
  323. * Id must be 0 if object instance is a specimen
  324. *
  325. * @return void
  326. */
  327. function initAsSpecimen()
  328. {
  329. $this->id=0;
  330. $this->expression='';
  331. }
  332. }