deplacement.class.php 12 KB

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