deplacement.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/deplacement/class/deplacement.class.php
  22. * \ingroup deplacement
  23. * \brief File of class to manage trips
  24. */
  25. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  26. /**
  27. * Class to manage trips and working credit notes
  28. */
  29. class Deplacement extends CommonObject
  30. {
  31. /**
  32. * @var string ID to identify managed object
  33. */
  34. public $element='deplacement';
  35. /**
  36. * @var string Name of table without prefix where object is stored
  37. */
  38. public $table_element='deplacement';
  39. public $table_element_line = '';
  40. public $fk_element = '';
  41. public $ismultientitymanaged = 0; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  42. var $datec; // Creation date
  43. var $dated;
  44. var $fk_user_author;
  45. var $fk_user;
  46. var $km;
  47. var $socid;
  48. var $statut; // 0=draft, 1=validated
  49. var $extraparams=array();
  50. var $statuts=array();
  51. var $statuts_short=array();
  52. /**
  53. * Constructor
  54. *
  55. * @param DoliDB $db Database handler
  56. */
  57. function __construct($db)
  58. {
  59. $this->db = $db;
  60. $this->statuts_short = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
  61. $this->statuts = array(0 => 'Draft', 1 => 'Validated', 2 => 'Refunded');
  62. }
  63. /**
  64. * Create object in database
  65. * TODO Add ref number
  66. *
  67. * @param User $user User that creates
  68. * @return int <0 if KO, >0 if OK
  69. */
  70. function create($user)
  71. {
  72. global $conf;
  73. // Check parameters
  74. if (empty($this->type) || $this->type < 0)
  75. {
  76. $this->error='ErrorBadParameter';
  77. return -1;
  78. }
  79. if (empty($this->fk_user) || $this->fk_user < 0)
  80. {
  81. $this->error='ErrorBadParameter';
  82. return -1;
  83. }
  84. $now=dol_now();
  85. $this->db->begin();
  86. $sql = "INSERT INTO ".MAIN_DB_PREFIX."deplacement (";
  87. $sql.= "datec";
  88. //$sql.= ", dated";
  89. $sql.= ", entity";
  90. $sql.= ", fk_user_author";
  91. $sql.= ", fk_user";
  92. $sql.= ", type";
  93. $sql.= ", note_private";
  94. $sql.= ", note_public";
  95. $sql.= ", fk_projet";
  96. $sql.= ", fk_soc";
  97. $sql.= ") VALUES (";
  98. $sql.= " '".$this->db->idate($now)."'";
  99. $sql.= ", ".$conf->entity;
  100. $sql.= ", ".$user->id;
  101. $sql.= ", ".$this->fk_user;
  102. $sql.= ", '".$this->db->escape($this->type)."'";
  103. $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  104. $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  105. $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
  106. $sql.= ", ".($this->fk_soc > 0? $this->fk_soc : "null");
  107. $sql.= ")";
  108. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  109. $result = $this->db->query($sql);
  110. if ($result)
  111. {
  112. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."deplacement");
  113. // Call trigger
  114. $result=$this->call_trigger('DEPLACEMENT_CREATE',$user);
  115. if ($result < 0)
  116. {
  117. $this->db->rollback();
  118. return -2;
  119. }
  120. // End call triggers
  121. $result=$this->update($user);
  122. if ($result > 0)
  123. {
  124. $this->db->commit();
  125. return $this->id;
  126. }
  127. else
  128. {
  129. $this->error=$this->db->error();
  130. $this->db->rollback();
  131. return $result;
  132. }
  133. }
  134. else
  135. {
  136. $this->error=$this->db->error()." sql=".$sql;
  137. $this->db->rollback();
  138. return -1;
  139. }
  140. }
  141. /**
  142. * Update record
  143. *
  144. * @param User $user User making update
  145. * @return int <0 if KO, >0 if OK
  146. */
  147. function update($user)
  148. {
  149. global $langs;
  150. // Clean parameters
  151. $this->km=price2num($this->km);
  152. // Check parameters
  153. if (! is_numeric($this->km)) $this->km = 0;
  154. if (empty($this->date))
  155. {
  156. $this->error='ErrorBadParameter';
  157. return -1;
  158. }
  159. if (empty($this->type) || $this->type < 0)
  160. {
  161. $this->error='ErrorBadParameter';
  162. return -1;
  163. }
  164. if (empty($this->fk_user) || $this->fk_user < 0)
  165. {
  166. $this->error='ErrorBadParameter';
  167. return -1;
  168. }
  169. $this->db->begin();
  170. $sql = "UPDATE ".MAIN_DB_PREFIX."deplacement ";
  171. $sql .= " SET km = ".$this->km; // This is a distance or amount
  172. $sql .= " , dated = '".$this->db->idate($this->date)."'";
  173. $sql .= " , type = '".$this->db->escape($this->type)."'";
  174. $sql .= " , fk_statut = '".$this->db->escape($this->statut)."'";
  175. $sql .= " , fk_user = ".$this->fk_user;
  176. $sql .= " , fk_user_modif = ".$user->id;
  177. $sql .= " , fk_soc = ".($this->socid > 0?$this->socid:'null');
  178. $sql .= " , note_private = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  179. $sql .= " , note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  180. $sql .= " , fk_projet = ".($this->fk_project>0?$this->fk_project:0);
  181. $sql .= " WHERE rowid = ".$this->id;
  182. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  183. $result = $this->db->query($sql);
  184. if ($result)
  185. {
  186. $this->db->commit();
  187. return 1;
  188. }
  189. else
  190. {
  191. $this->error=$this->db->lasterror();
  192. $this->db->rollback();
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Load an object from database
  198. *
  199. * @param int $id Id of record to load
  200. * @param string $ref Ref of record
  201. * @return int <0 if KO, >0 if OK
  202. */
  203. function fetch($id, $ref='')
  204. {
  205. $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet, extraparams";
  206. $sql.= " FROM ".MAIN_DB_PREFIX."deplacement";
  207. $sql.= " WHERE entity IN (".getEntity('deplacement').")";
  208. if ($ref) $sql.= " AND ref ='".$this->db->escape($ref)."'";
  209. else $sql.= " AND rowid = ".$id;
  210. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  211. $result = $this->db->query($sql);
  212. if ( $result )
  213. {
  214. $obj = $this->db->fetch_object($result);
  215. $this->id = $obj->rowid;
  216. $this->ref = $obj->rowid;
  217. $this->date = $this->db->jdate($obj->dated);
  218. $this->fk_user = $obj->fk_user;
  219. $this->socid = $obj->fk_soc;
  220. $this->km = $obj->km;
  221. $this->type = $obj->type;
  222. $this->statut = $obj->fk_statut;
  223. $this->note_private = $obj->note_private;
  224. $this->note_public = $obj->note_public;
  225. $this->fk_project = $obj->fk_projet;
  226. $this->extraparams = (array) json_decode($obj->extraparams, true);
  227. return 1;
  228. }
  229. else
  230. {
  231. $this->error=$this->db->error();
  232. return -1;
  233. }
  234. }
  235. /**
  236. * Delete record
  237. *
  238. * @param int $id Id of record to delete
  239. * @return int <0 if KO, >0 if OK
  240. */
  241. function delete($id)
  242. {
  243. $this->db->begin();
  244. $sql = "DELETE FROM ".MAIN_DB_PREFIX."deplacement WHERE rowid = ".$id;
  245. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  246. $result = $this->db->query($sql);
  247. if ($result)
  248. {
  249. $this->db->commit();
  250. return 1;
  251. }
  252. else
  253. {
  254. $this->error=$this->db->error();
  255. $this->db->rollback();
  256. return -1;
  257. }
  258. }
  259. /**
  260. * Retourne le libelle du statut
  261. *
  262. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  263. * @return string Libelle
  264. */
  265. function getLibStatut($mode=0)
  266. {
  267. return $this->LibStatut($this->statut,$mode);
  268. }
  269. /**
  270. * Renvoi le libelle d'un statut donne
  271. *
  272. * @param int $statut Id status
  273. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  274. * @return string Libelle
  275. */
  276. // phpcs:ignore PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  277. function LibStatut($statut,$mode=0)
  278. {
  279. global $langs;
  280. if ($mode == 0)
  281. {
  282. return $langs->trans($this->statuts[$statut]);
  283. }
  284. if ($mode == 1)
  285. {
  286. return $langs->trans($this->statuts_short[$statut]);
  287. }
  288. if ($mode == 2)
  289. {
  290. if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
  291. if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
  292. if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts_short[$statut]);
  293. }
  294. if ($mode == 3)
  295. {
  296. if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  297. if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  298. if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
  299. }
  300. if ($mode == 4)
  301. {
  302. if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  303. if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
  304. if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
  305. }
  306. if ($mode == 5)
  307. {
  308. if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  309. if ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  310. if ($statut==2 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
  311. }
  312. }
  313. /**
  314. * Return clicable name (with picto eventually)
  315. *
  316. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  317. * @return string Chaine avec URL
  318. */
  319. function getNomUrl($withpicto=0)
  320. {
  321. global $langs;
  322. $result='';
  323. $label=$langs->trans("Show").': '.$this->ref;
  324. $link = '<a href="'.DOL_URL_ROOT.'/compta/deplacement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  325. $linkend='</a>';
  326. $picto='trip';
  327. if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
  328. if ($withpicto && $withpicto != 2) $result.=' ';
  329. if ($withpicto != 2) $result.=$link.$this->ref.$linkend;
  330. return $result;
  331. }
  332. /**
  333. * List of types
  334. *
  335. * @param int $active Active or not
  336. * @return array
  337. */
  338. function listOfTypes($active=1)
  339. {
  340. global $langs;
  341. $ret=array();
  342. $sql = "SELECT id, code, label";
  343. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees";
  344. $sql.= " WHERE active = ".$active;
  345. dol_syslog(get_class($this)."::listOfTypes", LOG_DEBUG);
  346. $result = $this->db->query($sql);
  347. if ( $result )
  348. {
  349. $num = $this->db->num_rows($result);
  350. $i=0;
  351. while ($i < $num)
  352. {
  353. $obj = $this->db->fetch_object($result);
  354. $ret[$obj->code]=(($langs->trans($obj->code)!=$obj->code)?$langs->trans($obj->code):$obj->label);
  355. $i++;
  356. }
  357. }
  358. else
  359. {
  360. dol_print_error($this->db);
  361. }
  362. return $ret;
  363. }
  364. /**
  365. * Information on record
  366. *
  367. * @param int $id Id of record
  368. * @return void
  369. */
  370. function info($id)
  371. {
  372. $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
  373. $sql.= ' c.tms';
  374. $sql.= ' FROM '.MAIN_DB_PREFIX.'deplacement as c';
  375. $sql.= ' WHERE c.rowid = '.$id;
  376. dol_syslog(get_class($this).'::info', LOG_DEBUG);
  377. $result = $this->db->query($sql);
  378. if ($result)
  379. {
  380. if ($this->db->num_rows($result))
  381. {
  382. $obj = $this->db->fetch_object($result);
  383. $this->id = $obj->rowid;
  384. if ($obj->fk_user_author)
  385. {
  386. $cuser = new User($this->db);
  387. $cuser->fetch($obj->fk_user_author);
  388. $this->user_creation = $cuser;
  389. }
  390. if ($obj->fk_user_modif)
  391. {
  392. $muser = new User($this->db);
  393. $muser->fetch($obj->fk_user_modif);
  394. $this->user_modification = $muser;
  395. }
  396. $this->date_creation = $this->db->jdate($obj->datec);
  397. $this->date_modification = $this->db->jdate($obj->tms);
  398. }
  399. $this->db->free($result);
  400. }
  401. else
  402. {
  403. dol_print_error($this->db);
  404. }
  405. }
  406. }