PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/compta/deplacement/class/deplacement.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 450 lines | 304 code | 52 blank | 94 comment | 54 complexity | f3750d8fcf165bb18537ff4f3f56eb88 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.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/compta/deplacement/class/deplacement.class.php
  21. * \ingroup deplacement
  22. * \brief File of class to manage trips
  23. */
  24. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  25. /**
  26. * \class Deplacement
  27. * \brief Class to manage trips and working credit notes
  28. */
  29. class Deplacement extends CommonObject
  30. {
  31. public $element='deplacement';
  32. public $table_element='deplacement';
  33. public $table_element_line = '';
  34. public $fk_element = '';
  35. protected $ismultientitymanaged = 0; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  36. var $id;
  37. var $datec; // Creation date
  38. var $dated;
  39. var $fk_user_author;
  40. var $fk_user;
  41. var $km;
  42. var $note; // TODO obsolete
  43. var $note_private;
  44. var $note_public;
  45. var $socid;
  46. var $statut; // 0=draft, 1=validated
  47. var $fk_project;
  48. var $extraparams=array();
  49. var $statuts=array();
  50. var $statuts_short=array();
  51. /**
  52. * Constructor
  53. *
  54. * @param DoliDB $db Database handler
  55. */
  56. function __construct($db = '')
  57. {
  58. $this->db = $db;
  59. return 1;
  60. }
  61. /**
  62. * Create object in database
  63. * TODO Add ref number
  64. *
  65. * @param User $user User that creates
  66. * @return int <0 if KO, >0 if OK
  67. */
  68. function create($user)
  69. {
  70. global $conf;
  71. // Check parameters
  72. if (empty($this->type) || $this->type < 0)
  73. {
  74. $this->error='ErrorBadParameter';
  75. return -1;
  76. }
  77. if (empty($this->fk_user) || $this->fk_user < 0)
  78. {
  79. $this->error='ErrorBadParameter';
  80. return -1;
  81. }
  82. $now=dol_now();
  83. $this->db->begin();
  84. $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
  85. $sql.= "datec";
  86. //$sql.= ", dated";
  87. $sql.= ", entity";
  88. $sql.= ", fk_user_author";
  89. $sql.= ", fk_user";
  90. $sql.= ", type";
  91. $sql.= ", note";
  92. $sql.= ", note_public";
  93. $sql.= ", fk_projet";
  94. $sql.= ", fk_soc";
  95. $sql.= ") VALUES (";
  96. $sql.= " '".$this->db->idate($now)."'";
  97. $sql.= ", ".$conf->entity;
  98. $sql.= ", ".$user->id;
  99. $sql.= ", ".$this->fk_user;
  100. $sql.= ", '".$this->type."'";
  101. $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  102. $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  103. $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
  104. $sql.= ", ".($this->fk_soc > 0? $this->fk_soc : "null");
  105. $sql.= ")";
  106. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  107. $result = $this->db->query($sql);
  108. if ($result)
  109. {
  110. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
  111. // Appel des triggers
  112. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  113. $interface=new Interfaces($this->db);
  114. $result=$interface->run_triggers('DEPLACEMENT_CREATE',$this,$user,$langs,$conf);
  115. if ($result < 0) {
  116. $error++; $this->errors=$interface->errors;
  117. }
  118. // Fin appel triggers
  119. $result=$this->update($user);
  120. if ($result > 0)
  121. {
  122. $this->db->commit();
  123. return $this->id;
  124. }
  125. else
  126. {
  127. $this->error=$this->db->error();
  128. $this->db->rollback();
  129. return $result;
  130. }
  131. }
  132. else
  133. {
  134. $this->error=$this->db->error()." sql=".$sql;
  135. $this->db->rollback();
  136. return -1;
  137. }
  138. }
  139. /**
  140. * Update record
  141. *
  142. * @param User $user User making update
  143. * @return int <0 if KO, >0 if OK
  144. */
  145. function update($user)
  146. {
  147. global $langs;
  148. // Clean parameters
  149. $this->km=price2num($this->km);
  150. // Check parameters
  151. if (! is_numeric($this->km)) $this->km = 0;
  152. if (empty($this->date))
  153. {
  154. $this->error='ErrorBadParameter';
  155. return -1;
  156. }
  157. if (empty($this->type) || $this->type < 0)
  158. {
  159. $this->error='ErrorBadParameter';
  160. return -1;
  161. }
  162. if (empty($this->fk_user) || $this->fk_user < 0)
  163. {
  164. $this->error='ErrorBadParameter';
  165. return -1;
  166. }
  167. $this->db->begin();
  168. $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
  169. $sql .= " SET km = ".$this->km; // This is a distance or amount
  170. $sql .= " , dated = '".$this->db->idate($this->date)."'";
  171. $sql .= " , type = '".$this->type."'";
  172. $sql .= " , fk_statut = '".$this->statut."'";
  173. $sql .= " , fk_user = ".$this->fk_user;
  174. $sql .= " , fk_user_modif = ".$user->id;
  175. $sql .= " , fk_soc = ".($this->socid > 0?$this->socid:'null');
  176. $sql .= " , note = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  177. $sql .= " , note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  178. $sql .= " , fk_projet = ".($this->fk_project>0?$this->fk_project:0);
  179. $sql .= " WHERE rowid = ".$this->id;
  180. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  181. $result = $this->db->query($sql);
  182. if ($result)
  183. {
  184. $this->db->commit();
  185. return 1;
  186. }
  187. else
  188. {
  189. $this->error=$this->db->lasterror();
  190. $this->db->rollback();
  191. return -1;
  192. }
  193. }
  194. /**
  195. * Load an object from database
  196. *
  197. * @param int $id Id of record to load
  198. * @return int <0 if KO, >0 if OK
  199. */
  200. function fetch($id)
  201. {
  202. $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note, note_public, fk_projet, extraparams";
  203. $sql.= " FROM ".MAIN_DB_PREFIX."deplacement";
  204. $sql.= " WHERE rowid = ".$id;
  205. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  206. $result = $this->db->query($sql);
  207. if ( $result )
  208. {
  209. $obj = $this->db->fetch_object($result);
  210. $this->id = $obj->rowid;
  211. $this->ref = $obj->rowid;
  212. $this->date = $this->db->jdate($obj->dated);
  213. $this->fk_user = $obj->fk_user;
  214. $this->socid = $obj->fk_soc;
  215. $this->km = $obj->km;
  216. $this->type = $obj->type;
  217. $this->statut = $obj->fk_statut;
  218. $this->note_private = $obj->note;
  219. $this->note_public = $obj->note_public;
  220. $this->fk_project = $obj->fk_projet;
  221. $this->extraparams = (array) json_decode($obj->extraparams, true);
  222. return 1;
  223. }
  224. else
  225. {
  226. $this->error=$this->db->error();
  227. return -1;
  228. }
  229. }
  230. /**
  231. * Delete record
  232. *
  233. * @param int $id Id of record to delete
  234. * @return int <0 if KO, >0 if OK
  235. */
  236. function delete($id)
  237. {
  238. $this->db->begin();
  239. $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
  240. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  241. $result = $this->db->query($sql);
  242. if ($result)
  243. {
  244. $this->db->commit();
  245. return 1;
  246. }
  247. else
  248. {
  249. $this->error=$this->db->error();
  250. $this->db->rollback();
  251. return -1;
  252. }
  253. }
  254. /**
  255. * Retourne le libelle du statut
  256. *
  257. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  258. * @return string Libelle
  259. */
  260. function getLibStatut($mode=0)
  261. {
  262. return $this->LibStatut($this->statut,$mode);
  263. }
  264. /**
  265. * Renvoi le libelle d'un statut donne
  266. *
  267. * @param int $statut Id status
  268. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  269. * @return string Libelle
  270. */
  271. function LibStatut($statut,$mode=0)
  272. {
  273. global $langs;
  274. if ($mode == 0)
  275. {
  276. return $langs->trans($this->statuts[$statut]);
  277. }
  278. if ($mode == 1)
  279. {
  280. return $langs->trans($this->statuts_short[$statut]);
  281. }
  282. if ($mode == 2)
  283. {
  284. if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
  285. if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
  286. }
  287. if ($mode == 3)
  288. {
  289. if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  290. if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  291. }
  292. if ($mode == 4)
  293. {
  294. //if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  295. if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  296. if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
  297. }
  298. if ($mode == 5)
  299. {
  300. if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  301. if ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  302. }
  303. }
  304. /**
  305. * Return clicable name (with picto eventually)
  306. *
  307. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  308. * @return string Chaine avec URL
  309. */
  310. function getNomUrl($withpicto=0)
  311. {
  312. global $langs;
  313. $result='';
  314. $lien = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/fiche.php?id='.$this->id.'">';
  315. $lienfin='</a>';
  316. $picto='trip';
  317. $label=$langs->trans("Show").': '.$this->ref;
  318. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  319. if ($withpicto && $withpicto != 2) $result.=' ';
  320. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  321. return $result;
  322. }
  323. /**
  324. * List of types
  325. *
  326. * @param int $active Active or not
  327. * @return void
  328. */
  329. function listOfTypes($active=1)
  330. {
  331. global $conf,$langs;
  332. $ret=array();
  333. $sql = "SELECT id, code, libelle as label";
  334. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
  335. $sql.= " WHERE active = ".$active;
  336. dol_syslog(get_class($this)."::listOfTypes sql=".$sql, LOG_DEBUG);
  337. $result = $this->db->query($sql);
  338. if ( $result )
  339. {
  340. $num = $this->db->num_rows($result);
  341. $i=0;
  342. while ($i < $num)
  343. {
  344. $obj = $this->db->fetch_object($result);
  345. $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
  346. $i++;
  347. }
  348. }
  349. else
  350. {
  351. dol_print_error($this->db);
  352. }
  353. return $ret;
  354. }
  355. /**
  356. * Information on record
  357. *
  358. * @param int $id Id of record
  359. * @return void
  360. */
  361. function info($id)
  362. {
  363. $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
  364. $sql.= ' c.tms';
  365. $sql.= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
  366. $sql.= ' WHERE c.rowid = '.$id;
  367. dol_syslog(get_class($this).'::info sql='.$sql);
  368. $result = $this->db->query($sql);
  369. if ($result)
  370. {
  371. if ($this->db->num_rows($result))
  372. {
  373. $obj = $this->db->fetch_object($result);
  374. $this->id = $obj->rowid;
  375. if ($obj->fk_user_author)
  376. {
  377. $cuser = new User($this->db);
  378. $cuser->fetch($obj->fk_user_author);
  379. $this->user_creation = $cuser;
  380. }
  381. if ($obj->fk_user_modif)
  382. {
  383. $muser = new User($this->db);
  384. $muser->fetch($obj->fk_user_modif);
  385. $this->user_modification = $muser;
  386. }
  387. $this->date_creation = $this->db->jdate($obj->datec);
  388. $this->date_modification = $this->db->jdate($obj->tms);
  389. }
  390. $this->db->free($result);
  391. }
  392. else
  393. {
  394. dol_print_error($this->db);
  395. }
  396. }
  397. }
  398. ?>