No Description

phcp.class.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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/phcp.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-12 16:46
  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/phcptype.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/photocopies/class/compteur.class.php");
  28. /**
  29. * Put here description of your class
  30. */
  31. class Phcp 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='phcp'; //!< Id that identify managed objects
  37. var $table_element='phcp'; //!< Name of table without prefix where object is stored
  38. var $id;
  39. var $nom;
  40. var $ref;
  41. var $fk_phcp_type;
  42. var $fk_societe;
  43. var $phcp_type;
  44. var $societe;
  45. var $compteurs;
  46. /**
  47. * Constructor
  48. *
  49. * @param DoliDb $db Database handler
  50. */
  51. function __construct($db)
  52. {
  53. $this->db = $db;
  54. return 1;
  55. }
  56. function all($sortfield = 'p.ref', $sortorder = 'asc'){
  57. global $conf, $langs;
  58. $sql = "SELECT";
  59. $sql.= " p.rowid,";
  60. $sql.= " p.ref,";
  61. $sql.= " p.nom,";
  62. $sql.= " pt.ref,";
  63. $sql.= " pt.nom,";
  64. $sql.= " s.nom";
  65. $sql.= " FROM ".MAIN_DB_PREFIX."phcp as p";
  66. //$sql.= " ".MAIN_DB_PREFIX."phcp_type as pt,";
  67. //$sql.= " ".MAIN_DB_PREFIX."societe as s";
  68. $sql.= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."phcp_type as pt";
  69. $sql.= " ON pt.rowid = p.fk_phcp_type";
  70. $sql.= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe as s";
  71. $sql.= " ON s.rowid = p.fk_societe";
  72. // $sql.= " WHERE (s.rowid = p.fk_societe OR p.fk_societe IS NULL)";
  73. // $sql.= " AND (pt.rowid = p.fk_phcp_type AND p.fk_phcp_type IS NULL)";
  74. $sql.= " ORDER BY ".$this->db->escape($sortfield)." ".$this->db->escape($sortorder);
  75. $list = array();
  76. dol_syslog(get_class($this)."::all sql=".$sql, LOG_DEBUG);
  77. $resql=$this->db->query($sql);
  78. if ($resql){
  79. if ($num = $this->db->num_rows($resql)){
  80. $i = 0;
  81. while ($i < $num){
  82. $obj = $this->db->fetch_object($resql);
  83. if ($obj){
  84. $phcp = new Phcp($this->db);
  85. $phcp->fetch($obj->rowid);
  86. $list[]= $phcp;
  87. }
  88. $i++;
  89. }
  90. }
  91. }
  92. return $list;
  93. }
  94. function by_societe($socid){
  95. global $conf, $langs;
  96. $sql = "SELECT";
  97. $sql.= " p.rowid";
  98. $sql.= " FROM ".MAIN_DB_PREFIX."phcp as p";
  99. $sql.= " WHERE p.fk_societe = '".$socid."'";
  100. dol_syslog(get_class($this)."::all sql=".$sql, LOG_DEBUG);
  101. $resql=$this->db->query($sql);
  102. if ($resql){
  103. if ($num = $this->db->num_rows($resql)){
  104. $i = 0;
  105. while ($i < $num){
  106. $obj = $this->db->fetch_object($resql);
  107. if ($obj){
  108. $phcp = new Phcp($this->db);
  109. $phcp->fetch($obj->rowid);
  110. $list[]= $phcp;
  111. }
  112. $i++;
  113. }
  114. }
  115. }
  116. return $list;
  117. }
  118. function no_societe(){
  119. global $conf, $langs;
  120. $sql = "SELECT";
  121. $sql.= " t.rowid";
  122. $sql.= " FROM ".MAIN_DB_PREFIX."phcp as t";
  123. $sql.= " WHERE t.fk_societe IS NULL";
  124. $list = array();
  125. dol_syslog(get_class($this)."::no_societe sql=".$sql, LOG_DEBUG);
  126. $resql=$this->db->query($sql);
  127. if ($resql){
  128. if ($num = $this->db->num_rows($resql)){
  129. $i = 0;
  130. while ($i < $num){
  131. $obj = $this->db->fetch_object($resql);
  132. if ($obj){
  133. $phcp = new Phcp($this->db);
  134. $phcp->fetch($obj->rowid);
  135. $list[]= $phcp;
  136. }
  137. $i++;
  138. }
  139. }
  140. }
  141. return $list;
  142. }
  143. /**
  144. * Create object into database
  145. *
  146. * @param User $user User that creates
  147. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  148. * @return int <0 if KO, Id of created object if OK
  149. */
  150. function create($user, $notrigger=0)
  151. {
  152. global $conf, $langs;
  153. $error=0;
  154. // Clean parameters
  155. if (isset($this->nom)) $this->nom=trim($this->nom);
  156. if (isset($this->ref)) $this->ref=trim($this->ref);
  157. if (isset($this->fk_phcp_type)){
  158. $this->fk_phcp_type=trim($this->fk_phcp_type);
  159. $this->phcp_type=new PhcpType($this->db);
  160. $this->phcp_type->fetch($this->fk_phcp_type);
  161. }
  162. if (isset($this->fk_societe)){
  163. $this->fk_societe=trim($this->fk_societe);
  164. $this->societe=new Societe($this->db);
  165. $this->societe->fetch($this->fk_societe);
  166. }
  167. // Check parameters
  168. // Put here code to add control on parameters values
  169. // Insert request
  170. $sql = "INSERT INTO ".MAIN_DB_PREFIX."phcp (";
  171. $sql.= "nom,";
  172. $sql.= "ref,";
  173. $sql.= "fk_phcp_type,";
  174. $sql.= "fk_societe";
  175. $sql.= ") VALUES (";
  176. $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
  177. $sql.= " ".(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").",";
  178. $sql.= " ".(! isset($this->fk_phcp_type)?'NULL':"'".$this->fk_phcp_type."'").",";
  179. $sql.= " ".(! isset($this->fk_societe)?'NULL':"'".$this->fk_societe."'")."";
  180. $sql.= ")";
  181. $this->db->begin();
  182. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  183. $resql=$this->db->query($sql);
  184. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  185. if (! $error)
  186. {
  187. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."phcp");
  188. foreach($this->phcp_type->products as $product){
  189. $sql = "INSERT INTO ";
  190. $sql.= MAIN_DB_PREFIX."compteur (";
  191. $sql.= "fk_phcp, fk_product, compteur, last_compteur)";
  192. $sql.= " VALUES ('".$this->db->escape($this->id)."','".$this->db->escape($product->id)."', '0', '0')";
  193. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  194. $resql=$this->db->query($sql);
  195. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  196. }
  197. if (! $notrigger)
  198. {
  199. // Uncomment this and change MYOBJECT to your own tag if you
  200. // want this action calls a trigger.
  201. //// Call triggers
  202. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  203. //$interface=new Interfaces($this->db);
  204. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  205. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  206. //// End call triggers
  207. }
  208. }
  209. // Commit or rollback
  210. if ($error)
  211. {
  212. foreach($this->errors as $errmsg)
  213. {
  214. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  215. $this->error.=($this->error?', '.$errmsg:$errmsg);
  216. }
  217. $this->db->rollback();
  218. return -1*$error;
  219. }
  220. else
  221. {
  222. $this->db->commit();
  223. return $this->id;
  224. }
  225. }
  226. /**
  227. * Load object in memory from the database
  228. *
  229. * @param int $id Id object
  230. * @return int <0 if KO, >0 if OK
  231. */
  232. function fetch($id)
  233. {
  234. global $langs;
  235. $sql = "SELECT";
  236. $sql.= " t.rowid,";
  237. $sql.= " t.nom,";
  238. $sql.= " t.ref,";
  239. $sql.= " t.fk_phcp_type,";
  240. $sql.= " t.fk_societe";
  241. $sql.= " FROM ".MAIN_DB_PREFIX."phcp as t";
  242. $sql.= " WHERE t.rowid = ".$id;
  243. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  244. $resql=$this->db->query($sql);
  245. if ($resql)
  246. {
  247. if ($this->db->num_rows($resql))
  248. {
  249. $obj = $this->db->fetch_object($resql);
  250. $this->id = $obj->rowid;
  251. $this->nom = $obj->nom;
  252. $this->ref = $obj->ref;
  253. $this->fk_phcp_type = $obj->fk_phcp_type;
  254. $this->fk_societe = $obj->fk_societe;
  255. if (isset($this->fk_phcp_type)){
  256. $this->phcp_type=new PhcpType($this->db);
  257. $this->phcp_type->fetch($this->fk_phcp_type);
  258. }
  259. $this->fk_societe = $obj->fk_societe;
  260. if (isset($this->fk_societe)){
  261. $this->societe=new Societe($this->db);
  262. $this->societe->fetch($this->fk_societe);
  263. }
  264. $this->db->free($resql);
  265. $this->compteurs = array();
  266. $sql2 = "SELECT";
  267. $sql2.= " c.rowid";
  268. $sql2.= " FROM ".MAIN_DB_PREFIX."compteur as c";
  269. $sql2.= " WHERE c.fk_phcp = ".$this->id;
  270. dol_syslog(get_class($this)."::fetch sql=".$sql2, LOG_DEBUG);
  271. $resql2=$this->db->query($sql2);
  272. if ($resql2){
  273. $num2 = $this->db->num_rows($resql2);
  274. $i2 = 0;
  275. if ($num2){
  276. while ($i2 < $num2){
  277. $obj2 = $this->db->fetch_object($resql2);
  278. if ($obj2){
  279. $compteur = new Compteur($this->db);
  280. $compteur->phcp = $this;
  281. $compteur->fetch($obj2->rowid);
  282. $this->compteurs[] = $compteur;
  283. }
  284. $i2++;
  285. }
  286. }
  287. }
  288. }
  289. $this->db->free($resql2);
  290. return 1;
  291. }
  292. else
  293. {
  294. $this->error="Error ".$this->db->lasterror();
  295. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  296. return -1;
  297. }
  298. }
  299. /**
  300. * Update object into database
  301. *
  302. * @param User $user User that modifies
  303. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  304. * @return int <0 if KO, >0 if OK
  305. */
  306. function update($user=0, $notrigger=0)
  307. {
  308. global $conf, $langs;
  309. $error=0;
  310. // Clean parameters
  311. if (isset($this->nom)) $this->nom=trim($this->nom);
  312. if (isset($this->ref)) $this->ref=trim($this->ref);
  313. if (isset($this->fk_phcp_type)){
  314. $this->fk_phcp_type=trim($this->fk_phcp_type);
  315. $this->phcp_type=new PhcpType($this->db);
  316. $this->phcp_type->fetch($this->fk_phcp_type);
  317. } else $this->phcp_type = null;
  318. if (isset($this->fk_societe)){
  319. $this->fk_societe=trim($this->fk_societe);
  320. $this->societe=new Societe($this->db);
  321. $this->societe->fetch($this->fk_societe);
  322. } else $this->societe = null;
  323. // Check parameters
  324. // Put here code to add a control on parameters values
  325. // Update request
  326. $sql = "UPDATE ".MAIN_DB_PREFIX."phcp SET";
  327. $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
  328. $sql.= " ref=".(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null").",";
  329. $sql.= " fk_phcp_type=".(isset($this->fk_phcp_type)?$this->fk_phcp_type:"null").",";
  330. $sql.= " fk_societe=".(isset($this->fk_societe)?$this->fk_societe:"null")."";
  331. $sql.= " WHERE rowid=".$this->id;
  332. $this->db->begin();
  333. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  334. $resql = $this->db->query($sql);
  335. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  336. if (! $error)
  337. {
  338. if (! $notrigger)
  339. {
  340. // Uncomment this and change MYOBJECT to your own tag if you
  341. // want this action calls a trigger.
  342. //// Call triggers
  343. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  344. //$interface=new Interfaces($this->db);
  345. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  346. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  347. //// End call triggers
  348. }
  349. }
  350. // Commit or rollback
  351. if ($error)
  352. {
  353. foreach($this->errors as $errmsg)
  354. {
  355. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  356. $this->error.=($this->error?', '.$errmsg:$errmsg);
  357. }
  358. $this->db->rollback();
  359. return -1*$error;
  360. }
  361. else
  362. {
  363. $this->db->commit();
  364. return 1;
  365. }
  366. }
  367. /**
  368. * Delete object in database
  369. *
  370. * @param User $user User that deletes
  371. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  372. * @return int <0 if KO, >0 if OK
  373. */
  374. function delete($user, $notrigger=0)
  375. {
  376. global $conf, $langs;
  377. $error=0;
  378. $this->db->begin();
  379. if (! $error)
  380. {
  381. if (! $notrigger)
  382. {
  383. // Uncomment this and change MYOBJECT to your own tag if you
  384. // want this action calls a trigger.
  385. //// Call triggers
  386. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  387. //$interface=new Interfaces($this->db);
  388. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  389. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  390. //// End call triggers
  391. }
  392. }
  393. if (! $error)
  394. {
  395. $sql = "DELETE FROM ".MAIN_DB_PREFIX."compteur";
  396. $sql.= " WHERE fk_phcp=".$this->id;
  397. dol_syslog(get_class($this)."::delete sql=".$sql);
  398. $resql = $this->db->query($sql);
  399. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  400. }
  401. if (! $error)
  402. {
  403. $sql = "DELETE FROM ".MAIN_DB_PREFIX."phcp";
  404. $sql.= " WHERE rowid=".$this->id;
  405. dol_syslog(get_class($this)."::delete sql=".$sql);
  406. $resql = $this->db->query($sql);
  407. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  408. }
  409. // Commit or rollback
  410. if ($error)
  411. {
  412. foreach($this->errors as $errmsg)
  413. {
  414. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  415. $this->error.=($this->error?', '.$errmsg:$errmsg);
  416. }
  417. $this->db->rollback();
  418. return -1*$error;
  419. }
  420. else
  421. {
  422. $this->db->commit();
  423. return 1;
  424. }
  425. }
  426. /**
  427. * Load an object from its id and create a new one in database
  428. *
  429. * @param int $fromid Id of object to clone
  430. * @return int New id of clone
  431. */
  432. function createFromClone($fromid)
  433. {
  434. global $user,$langs;
  435. $error=0;
  436. $object=new Phcp($this->db);
  437. $this->db->begin();
  438. // Load source object
  439. $object->fetch($fromid);
  440. $object->id=0;
  441. $object->statut=0;
  442. // Clear fields
  443. // ...
  444. // Create clone
  445. $result=$object->create($user);
  446. // Other options
  447. if ($result < 0)
  448. {
  449. $this->error=$object->error;
  450. $error++;
  451. }
  452. if (! $error)
  453. {
  454. }
  455. // End
  456. if (! $error)
  457. {
  458. $this->db->commit();
  459. return $object->id;
  460. }
  461. else
  462. {
  463. $this->db->rollback();
  464. return -1;
  465. }
  466. }
  467. /**
  468. * Initialise object with example values
  469. * Id must be 0 if object instance is a specimen
  470. *
  471. * @return void
  472. */
  473. function initAsSpecimen()
  474. {
  475. $this->id=0;
  476. $this->nom='';
  477. $this->ref='';
  478. $this->fk_phcp_type='';
  479. $this->phcp_type=null;
  480. $this->fk_societe='';
  481. $this->societe=null;
  482. $this->compteurs=array();
  483. }
  484. }
  485. ?>