You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compteur.class.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) ---Put here your own copyright and developer email---
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file dev/skeletons/compteur.class.php
  20. * \ingroup mymodule othermodule1 othermodule2
  21. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  22. * Initialy built by build_class_from_table on 2013-09-19 10:38
  23. */
  24. // Put here all includes required by your class file
  25. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  26. require_once(DOL_DOCUMENT_ROOT."/photocopies/class/phcp.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  28. /**
  29. * Put here description of your class
  30. */
  31. class Compteur extends CommonObject
  32. {
  33. var $db; //!< To store db handler
  34. var $error; //!< To return error code (or message)
  35. var $errors=array(); //!< To return several error codes (or messages)
  36. var $element='compteur'; //!< Id that identify managed objects
  37. var $table_element='compteur'; //!< Name of table without prefix where object is stored
  38. var $id;
  39. var $ref;
  40. var $updated_at='';
  41. var $created_at='';
  42. var $compteur;
  43. var $last_compteur;
  44. var $fk_phcp;
  45. var $phcp;
  46. var $fk_product;
  47. var $product;
  48. /**
  49. * Constructor
  50. *
  51. * @param DoliDb $db Database handler
  52. */
  53. function __construct($db)
  54. {
  55. $this->db = $db;
  56. return 1;
  57. }
  58. /**
  59. * Create object into database
  60. *
  61. * @param User $user User that creates
  62. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  63. * @return int <0 if KO, Id of created object if OK
  64. */
  65. function create($user, $notrigger=0)
  66. {
  67. global $conf, $langs;
  68. $error=0;
  69. // Clean parameters
  70. if (isset($this->ref)) $this->ref=trim($this->ref);
  71. if (isset($this->compteur)) $this->compteur=trim($this->compteur);
  72. if (isset($this->last_compteur)) $this->last_compteur=trim($this->last_compteur);
  73. if (isset($this->fk_phcp)){
  74. $this->fk_phcp=trim($this->fk_phcp);
  75. $this->phcp=new Phcp($this->db);
  76. $this->phcp->fetch($this->fk_phcp);
  77. }
  78. if (isset($this->fk_product)){
  79. $this->fk_product=trim($this->fk_product);
  80. $this->product=new Product($this->db);
  81. $this->product->fetch($this->fk_product);
  82. }
  83. // Check parameters
  84. // Put here code to add control on parameters values
  85. // Insert request
  86. $sql = "INSERT INTO ".MAIN_DB_PREFIX."compteur(";
  87. $sql.= "ref,";
  88. $sql.= "updated_at,";
  89. $sql.= "created_at,";
  90. $sql.= "compteur,";
  91. $sql.= "last_compteur,";
  92. $sql.= "fk_phcp,";
  93. $sql.= "fk_product";
  94. $sql.= ") VALUES (";
  95. $sql.= " ".(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").",";
  96. $sql.= " ".(! isset($this->created_at) || dol_strlen($this->created_at)==0?'NULL':$this->db->idate($this->created_at)).",";
  97. $sql.= " ".(! isset($this->compteur)?'NULL':"'".$this->compteur."'").",";
  98. $sql.= " ".(! isset($this->last_compteur)?'NULL':"'".$this->last_compteur."'").",";
  99. $sql.= " ".(! isset($this->fk_phcp)?'NULL':"'".$this->fk_phcp."'").",";
  100. $sql.= " ".(! isset($this->fk_product)?'NULL':"'".$this->fk_product."'")."";
  101. $sql.= ")";
  102. $this->db->begin();
  103. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  104. $resql=$this->db->query($sql);
  105. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  106. if (! $error)
  107. {
  108. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."compteur");
  109. if (! $notrigger)
  110. {
  111. // Uncomment this and change MYOBJECT to your own tag if you
  112. // want this action calls a trigger.
  113. //// Call triggers
  114. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  115. //$interface=new Interfaces($this->db);
  116. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  117. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  118. //// End call triggers
  119. }
  120. }
  121. // Commit or rollback
  122. if ($error)
  123. {
  124. foreach($this->errors as $errmsg)
  125. {
  126. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  127. $this->error.=($this->error?', '.$errmsg:$errmsg);
  128. }
  129. $this->db->rollback();
  130. return -1*$error;
  131. }
  132. else
  133. {
  134. $this->db->commit();
  135. return $this->id;
  136. }
  137. }
  138. /**
  139. * Load object in memory from the database
  140. *
  141. * @param int $id Id object
  142. * @return int <0 if KO, >0 if OK
  143. */
  144. function fetch($id)
  145. {
  146. global $langs;
  147. $sql = "SELECT";
  148. $sql.= " t.rowid,";
  149. $sql.= " t.ref,";
  150. $sql.= " t.updated_at,";
  151. $sql.= " t.created_at,";
  152. $sql.= " t.compteur,";
  153. $sql.= " t.last_compteur,";
  154. $sql.= " t.fk_phcp,";
  155. $sql.= " t.fk_product";
  156. $sql.= " FROM ".MAIN_DB_PREFIX."compteur as t";
  157. $sql.= " WHERE t.rowid = ".$id;
  158. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  159. $resql=$this->db->query($sql);
  160. if ($resql)
  161. {
  162. if ($this->db->num_rows($resql))
  163. {
  164. $obj = $this->db->fetch_object($resql);
  165. $this->id = $obj->rowid;
  166. $this->ref = $obj->ref;
  167. $this->updated_at = $this->db->jdate($obj->updated_at);
  168. $this->created_at = $this->db->jdate($obj->created_at);
  169. $this->compteur = $obj->compteur;
  170. $this->last_compteur = $obj->last_compteur;
  171. $this->fk_phcp = $obj->fk_phcp;
  172. if (isset($this->fk_phcp)){
  173. if(!isset($this->phcp)){
  174. $this->phcp=new Phcp($this->db);
  175. $this->phcp->fetch($this->fk_phcp);
  176. }
  177. }
  178. $this->fk_product = $obj->fk_product;
  179. if (isset($this->fk_product)){
  180. if(!isset($this->product)){
  181. $this->product=new Product($this->db);
  182. $this->product->fetch($this->fk_product);
  183. }
  184. }
  185. }
  186. $this->db->free($resql);
  187. return 1;
  188. }
  189. else
  190. {
  191. $this->error="Error ".$this->db->lasterror();
  192. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  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. function update($user=0, $notrigger=0)
  204. {
  205. global $conf, $langs;
  206. $error=0;
  207. // Clean parameters
  208. if (isset($this->ref)) $this->ref=trim($this->ref);
  209. //if (isset($this->compteur)) $this->compteur=trim($this->compteur);
  210. //if (isset($this->last_compteur)) $this->last_compteur=trim($this->last_compteur);
  211. if (isset($this->fk_phcp)){
  212. $this->fk_phcp=trim($this->fk_phcp);
  213. $this->phcp=new Phcp($this->db);
  214. $this->phcp->fetch($this->fk_phcp);
  215. }
  216. if (isset($this->fk_product)){
  217. $this->fk_product=trim($this->fk_product);
  218. $this->product=new Product($this->db);
  219. $this->product->fetch($this->fk_product);
  220. }
  221. // Check parameters
  222. // Put here code to add a control on parameters values
  223. // Update request
  224. $sql = "UPDATE ".MAIN_DB_PREFIX."compteur SET";
  225. $sql.= " ref=".(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null").",";
  226. $sql.= " compteur=".(isset($this->compteur)?$this->compteur:"null").",";
  227. $sql.= " last_compteur=".(isset($this->last_compteur)?$this->last_compteur:"null").",";
  228. $sql.= " fk_phcp=".(isset($this->fk_phcp)?$this->fk_phcp:"null").",";
  229. $sql.= " fk_product=".(isset($this->fk_product)?$this->fk_product:"null")."";
  230. $sql.= " WHERE rowid=".$this->id;
  231. $this->db->begin();
  232. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  233. $resql = $this->db->query($sql);
  234. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  235. if (! $error)
  236. {
  237. if (! $notrigger)
  238. {
  239. // Uncomment this and change MYOBJECT to your own tag if you
  240. // want this action calls a trigger.
  241. //// Call triggers
  242. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  243. //$interface=new Interfaces($this->db);
  244. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  245. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  246. //// End call triggers
  247. }
  248. }
  249. // Commit or rollback
  250. if ($error)
  251. {
  252. foreach($this->errors as $errmsg)
  253. {
  254. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  255. $this->error.=($this->error?', '.$errmsg:$errmsg);
  256. }
  257. $this->db->rollback();
  258. return -1*$error;
  259. }
  260. else
  261. {
  262. $this->db->commit();
  263. return 1;
  264. }
  265. }
  266. /**
  267. * Delete object in database
  268. *
  269. * @param User $user User that deletes
  270. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  271. * @return int <0 if KO, >0 if OK
  272. */
  273. function delete($user, $notrigger=0)
  274. {
  275. global $conf, $langs;
  276. $error=0;
  277. $this->db->begin();
  278. if (! $error)
  279. {
  280. if (! $notrigger)
  281. {
  282. // Uncomment this and change MYOBJECT to your own tag if you
  283. // want this action calls a trigger.
  284. //// Call triggers
  285. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  286. //$interface=new Interfaces($this->db);
  287. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  288. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  289. //// End call triggers
  290. }
  291. }
  292. if (! $error)
  293. {
  294. $sql = "DELETE FROM ".MAIN_DB_PREFIX."compteur";
  295. $sql.= " WHERE rowid=".$this->id;
  296. dol_syslog(get_class($this)."::delete sql=".$sql);
  297. $resql = $this->db->query($sql);
  298. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  299. }
  300. // Commit or rollback
  301. if ($error)
  302. {
  303. foreach($this->errors as $errmsg)
  304. {
  305. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  306. $this->error.=($this->error?', '.$errmsg:$errmsg);
  307. }
  308. $this->db->rollback();
  309. return -1*$error;
  310. }
  311. else
  312. {
  313. $this->db->commit();
  314. $this->product = null;
  315. $this->phcp = null;
  316. return 1;
  317. }
  318. }
  319. /**
  320. * Load an object from its id and create a new one in database
  321. *
  322. * @param int $fromid Id of object to clone
  323. * @return int New id of clone
  324. */
  325. function createFromClone($fromid)
  326. {
  327. global $user,$langs;
  328. $error=0;
  329. $object=new Compteur($this->db);
  330. $this->db->begin();
  331. // Load source object
  332. $object->fetch($fromid);
  333. $object->id=0;
  334. $object->statut=0;
  335. // Clear fields
  336. // ...
  337. // Create clone
  338. $result=$object->create($user);
  339. // Other options
  340. if ($result < 0)
  341. {
  342. $this->error=$object->error;
  343. $error++;
  344. }
  345. if (! $error)
  346. {
  347. }
  348. // End
  349. if (! $error)
  350. {
  351. $this->db->commit();
  352. return $object->id;
  353. }
  354. else
  355. {
  356. $this->db->rollback();
  357. return -1;
  358. }
  359. }
  360. /**
  361. * Initialise object with example values
  362. * Id must be 0 if object instance is a specimen
  363. *
  364. * @return void
  365. */
  366. function initAsSpecimen()
  367. {
  368. $this->id=0;
  369. $this->ref='';
  370. $this->updated_at='';
  371. $this->created_at='';
  372. $this->compteur='';
  373. $this->last_compteur='';
  374. $this->fk_phcp='';
  375. $this->fk_product='';
  376. $this->phcp = null;
  377. $this->product = null;
  378. }
  379. }
  380. ?>