price_global_variable.class.php 8.4 KB

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