price_global_variable.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_global_variable.class.php
  21. * \ingroup product
  22. * \brief Class for accessing price global variables table
  23. */
  24. /**
  25. * Class for accesing price global variables table
  26. */
  27. class PriceGlobalVariable
  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 $code;
  46. /**
  47. * @var string description
  48. */
  49. public $description;
  50. public $value;
  51. /**
  52. * @var string Name of table without prefix where object is stored
  53. */
  54. public $table_element = "c_price_global_variable";
  55. /**
  56. * Constructor
  57. *
  58. * @param DoliDb $db Database handler
  59. */
  60. public function __construct($db)
  61. {
  62. $this->db = $db;
  63. }
  64. /**
  65. * Create object into database
  66. *
  67. * @param User $user User that creates
  68. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  69. * @return int Return integer <0 if KO, Id of created object if OK
  70. */
  71. public function create($user, $notrigger = 0)
  72. {
  73. $error = 0;
  74. $this->checkParameters();
  75. // Insert request
  76. $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
  77. $sql .= "code, description, value";
  78. $sql .= ") VALUES (";
  79. $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
  80. $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
  81. $sql .= " ".((float) $this->value);
  82. $sql .= ")";
  83. $this->db->begin();
  84. dol_syslog(__METHOD__);
  85. $resql = $this->db->query($sql);
  86. if (!$resql) {
  87. $error++;
  88. $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. if (!$notrigger) {
  93. // Uncomment this and change MYOBJECT to your own tag if you
  94. // want this action calls a trigger.
  95. //// Call triggers
  96. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  97. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  98. //// End call triggers
  99. }
  100. }
  101. // Commit or rollback
  102. if ($error) {
  103. foreach ($this->errors as $errmsg) {
  104. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  105. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  106. }
  107. $this->db->rollback();
  108. return -1 * $error;
  109. } else {
  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 Return integer < 0 if KO, 0 if OK but not found, > 0 if OK
  119. */
  120. public function fetch($id)
  121. {
  122. $sql = "SELECT code, description, value";
  123. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  124. $sql .= " WHERE rowid = ".((int) $id);
  125. dol_syslog(__METHOD__);
  126. $resql = $this->db->query($sql);
  127. if ($resql) {
  128. $obj = $this->db->fetch_object($resql);
  129. if ($obj) {
  130. $this->id = $id;
  131. $this->code = $obj->code;
  132. $this->description = $obj->description;
  133. $this->value = $obj->value;
  134. $this->checkParameters();
  135. return 1;
  136. } else {
  137. return 0;
  138. }
  139. } else {
  140. $this->error = "Error ".$this->db->lasterror();
  141. return -1;
  142. }
  143. }
  144. /**
  145. * Update object into database
  146. *
  147. * @param User $user User that modifies
  148. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  149. * @return int Return integer <0 if KO, >0 if OK
  150. */
  151. public function update($user = 0, $notrigger = 0)
  152. {
  153. $error = 0;
  154. $this->checkParameters();
  155. // Update request
  156. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  157. $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
  158. $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
  159. $sql .= " value = ".((float) $this->value);
  160. $sql .= " WHERE rowid = ".((int) $this->id);
  161. $this->db->begin();
  162. dol_syslog(__METHOD__);
  163. $resql = $this->db->query($sql);
  164. if (!$resql) {
  165. $error++;
  166. $this->errors[] = "Error ".$this->db->lasterror();
  167. }
  168. // if (! $error)
  169. // {
  170. // if (! $notrigger)
  171. // {
  172. // // Uncomment this and change MYOBJECT to your own tag if you
  173. // // want this action calls a trigger.
  174. // //// Call triggers
  175. // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  176. // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  177. // //// End call triggers
  178. // }
  179. // }
  180. // Commit or rollback
  181. if ($error) {
  182. foreach ($this->errors as $errmsg) {
  183. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  184. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  185. }
  186. $this->db->rollback();
  187. return -1 * $error;
  188. } else {
  189. $this->db->commit();
  190. return 1;
  191. }
  192. }
  193. /**
  194. * Delete object in database
  195. *
  196. * @param int $rowid Row id of global variable
  197. * @param User $user User that deletes
  198. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  199. * @return int Return integer <0 if KO, >0 if OK
  200. */
  201. public function delete($rowid, $user, $notrigger = 0)
  202. {
  203. $error = 0;
  204. $this->db->begin();
  205. if (!$error) {
  206. if (!$notrigger) {
  207. // Uncomment this and change MYOBJECT to your own tag if you
  208. // want this action calls a trigger.
  209. //// Call triggers
  210. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  211. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  212. //// End call triggers
  213. }
  214. }
  215. if (!$error) {
  216. $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
  217. $sql .= " WHERE rowid = ".((int) $rowid);
  218. dol_syslog(__METHOD__);
  219. $resql = $this->db->query($sql);
  220. if (!$resql) {
  221. $error++;
  222. $this->errors[] = "Error ".$this->db->lasterror();
  223. }
  224. }
  225. // Commit or rollback
  226. if ($error) {
  227. foreach ($this->errors as $errmsg) {
  228. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  229. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  230. }
  231. $this->db->rollback();
  232. return -1 * $error;
  233. } else {
  234. $this->db->commit();
  235. return 1;
  236. }
  237. }
  238. /**
  239. * Initialise object with example values
  240. * Id must be 0 if object instance is a specimen
  241. *
  242. * @return void
  243. */
  244. public function initAsSpecimen()
  245. {
  246. $this->id = 0;
  247. $this->code = '';
  248. $this->description = '';
  249. $this->value = '';
  250. }
  251. /**
  252. * Checks if all parameters are in order
  253. *
  254. * @return void
  255. */
  256. public function checkParameters()
  257. {
  258. // Clean parameters
  259. if (isset($this->code)) {
  260. $this->code = trim($this->code);
  261. }
  262. if (isset($this->description)) {
  263. $this->description = trim($this->description);
  264. }
  265. // Check parameters
  266. if (empty($this->value) || !is_numeric($this->value)) {
  267. $this->value = 0;
  268. }
  269. }
  270. /**
  271. * List all price global variables
  272. *
  273. * @return array|int Array of price global variables, <0 if ko
  274. */
  275. public function listGlobalVariables()
  276. {
  277. $sql = "SELECT rowid, code, description, value";
  278. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  279. $sql .= " ORDER BY code";
  280. dol_syslog(__METHOD__, LOG_DEBUG);
  281. $resql = $this->db->query($sql);
  282. if ($resql) {
  283. $retarray = array();
  284. while ($record = $this->db->fetch_array($resql)) {
  285. $variable_obj = new PriceGlobalVariable($this->db);
  286. $variable_obj->id = $record["rowid"];
  287. $variable_obj->code = $record["code"];
  288. $variable_obj->description = $record["description"];
  289. $variable_obj->value = $record["value"];
  290. $variable_obj->checkParameters();
  291. $retarray[] = $variable_obj;
  292. }
  293. $this->db->free($resql);
  294. return $retarray;
  295. } else {
  296. $this->error = $this->db->error();
  297. return -1;
  298. }
  299. }
  300. }