PageRenderTime 75ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/asterix14/dolibarr
PHP | 432 lines | 289 code | 48 blank | 95 comment | 47 complexity | 8bcedb4e86af6661e8a64f2963d1eed0 MD5 | raw file
Possible License(s): LGPL-2.0
  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 Regis Houssin <regis@dolibarr.fr>
  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 2 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. /**
  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 in database
  60. * TODO Add ref number
  61. *
  62. * @param User $user User that creates
  63. * @return int <0 if KO, >0 if OK
  64. */
  65. function create($user)
  66. {
  67. global $conf;
  68. // Check parameters
  69. if (empty($this->type) || $this->type < 0)
  70. {
  71. $this->error='ErrorBadParameter';
  72. return -1;
  73. }
  74. if (empty($this->fk_user) || $this->fk_user < 0)
  75. {
  76. $this->error='ErrorBadParameter';
  77. return -1;
  78. }
  79. $now=dol_now();
  80. $this->db->begin();
  81. $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
  82. $sql.= "datec";
  83. //$sql.= ", dated";
  84. $sql.= ", entity";
  85. $sql.= ", fk_user_author";
  86. $sql.= ", fk_user";
  87. $sql.= ", type";
  88. $sql.= ", note";
  89. $sql.= ", note_public";
  90. $sql.= ", fk_projet";
  91. $sql.= ", fk_soc";
  92. $sql.= ") VALUES (";
  93. $sql.= " '".$this->db->idate($now)."'";
  94. $sql.= ", ".$conf->entity;
  95. $sql.= ", ".$user->id;
  96. $sql.= ", ".$this->fk_user;
  97. $sql.= ", '".$this->type."'";
  98. $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  99. $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  100. $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
  101. $sql.= ", ".($this->fk_soc > 0? $this->fk_soc : "null");
  102. $sql.= ")";
  103. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  104. $result = $this->db->query($sql);
  105. if ($result)
  106. {
  107. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
  108. $result=$this->update($user);
  109. if ($result > 0)
  110. {
  111. $this->db->commit();
  112. return $this->id;
  113. }
  114. else
  115. {
  116. $this->error=$this->db->error();
  117. $this->db->rollback();
  118. return $result;
  119. }
  120. }
  121. else
  122. {
  123. $this->error=$this->db->error()." sql=".$sql;
  124. $this->db->rollback();
  125. return -1;
  126. }
  127. }
  128. /**
  129. * Update record
  130. *
  131. * @param User $user User making update
  132. * @return int <0 if KO, >0 if OK
  133. */
  134. function update($user)
  135. {
  136. global $langs;
  137. // Clean parameters
  138. $this->km=price2num($this->km);
  139. // Check parameters
  140. if (! is_numeric($this->km)) $this->km = 0;
  141. if (empty($this->date))
  142. {
  143. $this->error='ErrorBadParameter';
  144. return -1;
  145. }
  146. if (empty($this->type) || $this->type < 0)
  147. {
  148. $this->error='ErrorBadParameter';
  149. return -1;
  150. }
  151. if (empty($this->fk_user) || $this->fk_user < 0)
  152. {
  153. $this->error='ErrorBadParameter';
  154. return -1;
  155. }
  156. $this->db->begin();
  157. $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
  158. $sql .= " SET km = ".$this->km; // This is a distance or amount
  159. $sql .= " , dated = '".$this->db->idate($this->date)."'";
  160. $sql .= " , type = '".$this->type."'";
  161. $sql .= " , fk_user = ".$this->fk_user;
  162. $sql .= " , fk_user_modif = ".$user->id;
  163. $sql .= " , fk_soc = ".($this->socid > 0?$this->socid:'null');
  164. $sql .= " , note = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  165. $sql .= " , note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  166. $sql .= " , fk_projet = ".($this->fk_project>0?$this->fk_project:0);
  167. $sql .= " WHERE rowid = ".$this->id;
  168. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  169. $result = $this->db->query($sql);
  170. if ($result)
  171. {
  172. $this->db->commit();
  173. return 1;
  174. }
  175. else
  176. {
  177. $this->error=$this->db->lasterror();
  178. $this->db->rollback();
  179. return -1;
  180. }
  181. }
  182. /**
  183. * Load an object from database
  184. *
  185. * @param int $id Id of record to load
  186. * @return int <0 if KO, >0 if OK
  187. */
  188. function fetch($id)
  189. {
  190. $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note, note_public, fk_projet";
  191. $sql.= " FROM ".MAIN_DB_PREFIX."deplacement";
  192. $sql.= " WHERE rowid = ".$id;
  193. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  194. $result = $this->db->query($sql);
  195. if ( $result )
  196. {
  197. $obj = $this->db->fetch_object($result);
  198. $this->id = $obj->rowid;
  199. $this->ref = $obj->rowid;
  200. $this->date = $this->db->jdate($obj->dated);
  201. $this->fk_user = $obj->fk_user;
  202. $this->socid = $obj->fk_soc;
  203. $this->km = $obj->km;
  204. $this->type = $obj->type;
  205. $this->fk_statut = $obj->fk_statut;
  206. $this->note_private = $obj->note;
  207. $this->note_public = $obj->note_public;
  208. $this->fk_project = $obj->fk_projet;
  209. return 1;
  210. }
  211. else
  212. {
  213. $this->error=$this->db->error();
  214. return -1;
  215. }
  216. }
  217. /**
  218. * Delete record
  219. *
  220. * @param int $id Id of record to delete
  221. * @return int <0 if KO, >0 if OK
  222. */
  223. function delete($id)
  224. {
  225. $this->db->begin();
  226. $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
  227. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  228. $result = $this->db->query($sql);
  229. if ($result)
  230. {
  231. $this->db->commit();
  232. return 1;
  233. }
  234. else
  235. {
  236. $this->error=$this->db->error();
  237. $this->db->rollback();
  238. return -1;
  239. }
  240. }
  241. /**
  242. * Retourne le libelle du statut
  243. *
  244. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  245. * @return string Libelle
  246. */
  247. function getLibStatut($mode=0)
  248. {
  249. return $this->LibStatut($this->statut,$mode);
  250. }
  251. /**
  252. * Renvoi le libelle d'un statut donne
  253. *
  254. * @param int $statut Id status
  255. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  256. * @return string Libelle
  257. */
  258. function LibStatut($statut,$mode=0)
  259. {
  260. global $langs;
  261. if ($mode == 0)
  262. {
  263. return $langs->trans($this->statuts[$statut]);
  264. }
  265. if ($mode == 1)
  266. {
  267. return $langs->trans($this->statuts_short[$statut]);
  268. }
  269. if ($mode == 2)
  270. {
  271. //if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
  272. if ($statut==0 || $statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
  273. }
  274. if ($mode == 3)
  275. {
  276. //if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  277. if ($statut==0 || $statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  278. }
  279. if ($mode == 4)
  280. {
  281. //if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  282. if ($statut==0 || $statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
  283. }
  284. if ($mode == 5)
  285. {
  286. //if ($statut==0) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  287. if ($statut==0 || $statut==1) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  288. }
  289. }
  290. /**
  291. * Return clicable name (with picto eventually)
  292. *
  293. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  294. * @return string Chaine avec URL
  295. */
  296. function getNomUrl($withpicto=0)
  297. {
  298. global $langs;
  299. $result='';
  300. $lien = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/fiche.php?id='.$this->id.'">';
  301. $lienfin='</a>';
  302. $picto='trip';
  303. $label=$langs->trans("Show").': '.$this->ref;
  304. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  305. if ($withpicto && $withpicto != 2) $result.=' ';
  306. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  307. return $result;
  308. }
  309. /**
  310. * List of types
  311. *
  312. * @param int $active Active or not
  313. * @return void
  314. */
  315. function listOfTypes($active=1)
  316. {
  317. global $conf,$langs;
  318. $ret=array();
  319. $sql = "SELECT id, code, libelle as label";
  320. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
  321. $sql.= " WHERE active = ".$active;
  322. dol_syslog(get_class($this)."::listOfTypes sql=".$sql, LOG_DEBUG);
  323. $result = $this->db->query($sql);
  324. if ( $result )
  325. {
  326. $num = $this->db->num_rows($result);
  327. $i=0;
  328. while ($i < $num)
  329. {
  330. $obj = $this->db->fetch_object($result);
  331. $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
  332. $i++;
  333. }
  334. }
  335. else
  336. {
  337. dol_print_error($this->db);
  338. }
  339. return $ret;
  340. }
  341. /**
  342. * Information on record
  343. *
  344. * @param int $id Id of record
  345. * @return void
  346. */
  347. function info($id)
  348. {
  349. $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
  350. $sql.= ' c.tms';
  351. $sql.= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
  352. $sql.= ' WHERE c.rowid = '.$id;
  353. dol_syslog(get_class($this).'::info sql='.$sql);
  354. $result = $this->db->query($sql);
  355. if ($result)
  356. {
  357. if ($this->db->num_rows($result))
  358. {
  359. $obj = $this->db->fetch_object($result);
  360. $this->id = $obj->rowid;
  361. if ($obj->fk_user_author)
  362. {
  363. $cuser = new User($this->db);
  364. $cuser->fetch($obj->fk_user_author);
  365. $this->user_creation = $cuser;
  366. }
  367. if ($obj->fk_user_modif)
  368. {
  369. $muser = new User($this->db);
  370. $muser->fetch($obj->fk_user_modif);
  371. $this->user_modification = $muser;
  372. }
  373. $this->date_creation = $this->db->jdate($obj->datec);
  374. $this->date_modification = $this->db->jdate($obj->tms);
  375. }
  376. $this->db->free($result);
  377. }
  378. else
  379. {
  380. dol_print_error($this->db);
  381. }
  382. }
  383. }
  384. ?>