PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/fichinter/class/fichinter.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 1159 lines | 829 code | 133 blank | 197 comment | 101 complexity | 8144ee47636e60c79a1aee26a4f0f29c MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  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/fichinter/class/fichinter.class.php
  22. * \ingroup ficheinter
  23. * \brief Fichier de la classe des gestion des fiches interventions
  24. */
  25. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  26. /**
  27. * Classe des gestion des fiches interventions
  28. */
  29. class Fichinter extends CommonObject
  30. {
  31. public $element='fichinter';
  32. public $table_element='fichinter';
  33. public $fk_element='fk_fichinter';
  34. public $table_element_line='fichinterdet';
  35. var $id;
  36. var $socid; // Id client
  37. var $client; // Objet societe client (a charger par fetch_client)
  38. var $author;
  39. var $ref;
  40. var $datec;
  41. var $datev;
  42. var $datem;
  43. var $duree;
  44. var $statut; // 0=draft, 1=validated, 2=invoiced
  45. var $description;
  46. var $note_private;
  47. var $note_public;
  48. var $fk_project;
  49. var $modelpdf;
  50. var $extraparams=array();
  51. var $lines = array();
  52. /**
  53. * Constructor
  54. *
  55. * @param DoliDB $db Database handler
  56. */
  57. function __construct($db = '')
  58. {
  59. $this->db = $db;
  60. $this->products = array();
  61. $this->fk_project = 0;
  62. $this->statut = 0;
  63. // List of language codes for status
  64. $this->statuts[0]='Draft';
  65. $this->statuts[1]='Validated';
  66. $this->statuts[2]='StatusInterInvoiced';
  67. $this->statuts_short[0]='Draft';
  68. $this->statuts_short[1]='Validated';
  69. $this->statuts_short[2]='StatusInterInvoiced';
  70. }
  71. /**
  72. * Create an intervention into data base
  73. *
  74. * @return int <0 if KO, >0 if OK
  75. */
  76. function create()
  77. {
  78. global $conf, $user, $langs;
  79. dol_syslog(get_class($this)."::create ref=".$this->ref);
  80. // Check parameters
  81. if (! is_numeric($this->duree)) { $this->duree = 0; }
  82. if ($this->socid <= 0)
  83. {
  84. $this->error='ErrorBadParameterForFunc';
  85. dol_syslog(get_class($this)."::create ".$this->error,LOG_ERR);
  86. return -1;
  87. }
  88. // on verifie si la ref n'est pas utilisee
  89. $soc = new Societe($this->db);
  90. $result=$soc->fetch($this->socid);
  91. if (! empty($this->ref))
  92. {
  93. $result=$this->verifyNumRef(); // Check ref is not yet used
  94. if ($result > 0)
  95. {
  96. $this->error='ErrorRefAlreadyExists';
  97. dol_syslog(get_class($this)."::create ".$this->error,LOG_WARNING);
  98. $this->db->rollback();
  99. return -3;
  100. }
  101. else if ($result < 0)
  102. {
  103. $this->error=$this->db->error();
  104. dol_syslog(get_class($this)."::create ".$this->error,LOG_ERR);
  105. $this->db->rollback();
  106. return -2;
  107. }
  108. }
  109. $now=dol_now();
  110. $this->db->begin();
  111. $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter (";
  112. $sql.= "fk_soc";
  113. $sql.= ", datec";
  114. $sql.= ", ref";
  115. $sql.= ", entity";
  116. $sql.= ", fk_user_author";
  117. $sql.= ", description";
  118. $sql.= ", model_pdf";
  119. $sql.= ", fk_projet";
  120. $sql.= ", fk_statut";
  121. $sql.= ", note_private";
  122. $sql.= ", note_public";
  123. $sql.= ") ";
  124. $sql.= " VALUES (";
  125. $sql.= $this->socid;
  126. $sql.= ", '".$this->db->idate($now)."'";
  127. $sql.= ", '".$this->ref."'";
  128. $sql.= ", ".$conf->entity;
  129. $sql.= ", ".$this->author;
  130. $sql.= ", ".($this->description?"'".$this->db->escape($this->description)."'":"null");
  131. $sql.= ", '".$this->modelpdf."'";
  132. $sql.= ", ".($this->fk_project ? $this->fk_project : 0);
  133. $sql.= ", ".$this->statut;
  134. $sql.= ", ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  135. $sql.= ", ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  136. $sql.= ")";
  137. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  138. $result=$this->db->query($sql);
  139. if ($result)
  140. {
  141. $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."fichinter");
  142. $this->db->commit();
  143. // Appel des triggers
  144. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  145. $interface=new Interfaces($this->db);
  146. $result=$interface->run_triggers('FICHEINTER_CREATE',$this,$user,$langs,$conf);
  147. if ($result < 0) {
  148. $error++; $this->errors=$interface->errors;
  149. }
  150. // Fin appel triggers
  151. return $this->id;
  152. }
  153. else
  154. {
  155. $this->error=$this->db->error();
  156. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  157. $this->db->rollback();
  158. return -1;
  159. }
  160. }
  161. /**
  162. * Update an intervention
  163. *
  164. * @return int <0 if KO, >0 if OK
  165. */
  166. function update()
  167. {
  168. if (! is_numeric($this->duree)) { $this->duree = 0; }
  169. if (! dol_strlen($this->fk_project)) { $this->fk_project = 0; }
  170. $this->db->begin();
  171. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter SET ";
  172. $sql.= ", description = '".$this->db->escape($this->description)."'";
  173. $sql.= ", duree = ".$this->duree;
  174. $sql.= ", fk_projet = ".$this->fk_project;
  175. $sql.= ", note_private = ".($this->note_private?"'".$this->db->escape($this->note_private)."'":"null");
  176. $sql.= ", note_public = ".($this->note_public?"'".$this->db->escape($this->note_public)."'":"null");
  177. $sql.= " WHERE rowid = ".$this->id;
  178. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  179. if ($this->db->query($sql))
  180. {
  181. // Appel des triggers
  182. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  183. $interface=new Interfaces($this->db);
  184. $result=$interface->run_triggers('FICHEINTER_MODIFY',$this,$user,$langs,$conf);
  185. if ($result < 0) {
  186. $error++; $this->errors=$interface->errors;
  187. }
  188. // Fin appel triggers
  189. $this->db->commit();
  190. return 1;
  191. }
  192. else
  193. {
  194. $this->error=$this->db->error();
  195. dol_syslog(get_class($this)."::update error ".$this->error, LOG_ERR);
  196. $this->db->rollback();
  197. return -1;
  198. }
  199. }
  200. /**
  201. * Fetch a intervention
  202. *
  203. * @param int $rowid Id of intervention
  204. * @param string $ref Ref of intervention
  205. * @return int <0 if KO, >0 if OK
  206. */
  207. function fetch($rowid,$ref='')
  208. {
  209. $sql = "SELECT f.rowid, f.ref, f.description, f.fk_soc, f.fk_statut,";
  210. $sql.= " f.datec,";
  211. $sql.= " f.date_valid as datev,";
  212. $sql.= " f.tms as datem,";
  213. $sql.= " f.duree, f.fk_projet, f.note_public, f.note_private, f.model_pdf, f.extraparams";
  214. $sql.= " FROM ".MAIN_DB_PREFIX."fichinter as f";
  215. if ($ref) $sql.= " WHERE f.ref='".$this->db->escape($ref)."'";
  216. else $sql.= " WHERE f.rowid=".$rowid;
  217. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  218. $resql=$this->db->query($sql);
  219. if ($resql)
  220. {
  221. if ($this->db->num_rows($resql))
  222. {
  223. $obj = $this->db->fetch_object($resql);
  224. $this->id = $obj->rowid;
  225. $this->ref = $obj->ref;
  226. $this->description = $obj->description;
  227. $this->socid = $obj->fk_soc;
  228. $this->statut = $obj->fk_statut;
  229. $this->duree = $obj->duree;
  230. $this->datec = $this->db->jdate($obj->datec);
  231. $this->datev = $this->db->jdate($obj->datev);
  232. $this->datem = $this->db->jdate($obj->datem);
  233. $this->fk_project = $obj->fk_projet;
  234. $this->note_public = $obj->note_public;
  235. $this->note_private = $obj->note_private;
  236. $this->modelpdf = $obj->model_pdf;
  237. $this->extraparams = (array) json_decode($obj->extraparams, true);
  238. if ($this->statut == 0) $this->brouillon = 1;
  239. /*
  240. * Lines
  241. */
  242. $result=$this->fetch_lines();
  243. if ($result < 0)
  244. {
  245. return -3;
  246. }
  247. $this->db->free($resql);
  248. return 1;
  249. }
  250. }
  251. else
  252. {
  253. $this->error=$this->db->error();
  254. dol_syslog(get_class($this)."::fetch ".$this->error,LOG_ERR);
  255. return -1;
  256. }
  257. }
  258. /**
  259. * Set status to draft
  260. *
  261. * @param User $user User that set draft
  262. * @return int <0 if KO, >0 if OK
  263. */
  264. function setDraft($user)
  265. {
  266. global $langs, $conf;
  267. if ($this->statut != 0)
  268. {
  269. $this->db->begin();
  270. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  271. $sql.= " SET fk_statut = 0";
  272. $sql.= " WHERE rowid = ".$this->id;
  273. $sql.= " AND entity = ".$conf->entity;
  274. dol_syslog("Fichinter::setDraft sql=".$sql);
  275. $resql=$this->db->query($sql);
  276. if ($resql)
  277. {
  278. $this->db->commit();
  279. return 1;
  280. }
  281. else
  282. {
  283. $this->db->rollback();
  284. $this->error=$this->db->lasterror();
  285. dol_syslog("Fichinter::setDraft ".$this->error,LOG_ERR);
  286. return -1;
  287. }
  288. }
  289. }
  290. /**
  291. * Validate a intervention
  292. *
  293. * @param User $user User that validate
  294. * @return int <0 if KO, >0 if OK
  295. */
  296. function setValid($user)
  297. {
  298. global $langs, $conf;
  299. $error=0;
  300. if ($this->statut != 1)
  301. {
  302. $this->db->begin();
  303. $now=dol_now();
  304. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  305. $sql.= " SET fk_statut = 1";
  306. $sql.= ", date_valid = ".$this->db->idate($now);
  307. $sql.= ", fk_user_valid = ".$user->id;
  308. $sql.= " WHERE rowid = ".$this->id;
  309. $sql.= " AND entity = ".$conf->entity;
  310. $sql.= " AND fk_statut = 0";
  311. dol_syslog("Fichinter::setValid sql=".$sql);
  312. $resql=$this->db->query($sql);
  313. if ($resql)
  314. {
  315. // Appel des triggers
  316. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  317. $interface=new Interfaces($this->db);
  318. $result=$interface->run_triggers('FICHEINTER_VALIDATE',$this,$user,$langs,$conf);
  319. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  320. // Fin appel triggers
  321. if (! $error)
  322. {
  323. $this->db->commit();
  324. return 1;
  325. }
  326. else
  327. {
  328. $this->db->rollback();
  329. $this->error=join(',',$this->errors);
  330. dol_syslog("Fichinter::setValid ".$this->error,LOG_ERR);
  331. return -1;
  332. }
  333. }
  334. else
  335. {
  336. $this->db->rollback();
  337. $this->error=$this->db->lasterror();
  338. dol_syslog("Fichinter::setValid ".$this->error,LOG_ERR);
  339. return -1;
  340. }
  341. }
  342. }
  343. /**
  344. * Set intervetnion as billed
  345. *
  346. * @return int <0 si ko, >0 si ok
  347. */
  348. function setBilled()
  349. {
  350. global $conf;
  351. $sql = 'UPDATE '.MAIN_DB_PREFIX.'fichinter SET fk_statut = 2';
  352. $sql.= ' WHERE rowid = '.$this->id;
  353. $sql.= " AND entity = ".$conf->entity;
  354. $sql.= " AND fk_statut = 1";
  355. if ($this->db->query($sql) )
  356. {
  357. return 1;
  358. }
  359. else
  360. {
  361. dol_print_error($this->db);
  362. return -1;
  363. }
  364. }
  365. /**
  366. * Returns the label status
  367. *
  368. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  369. * @return string Label
  370. */
  371. function getLibStatut($mode=0)
  372. {
  373. return $this->LibStatut($this->statut,$mode);
  374. }
  375. /**
  376. * Returns the label of a statut
  377. *
  378. * @param int $statut id statut
  379. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  380. * @return string Label
  381. */
  382. function LibStatut($statut,$mode=0)
  383. {
  384. global $langs;
  385. if ($mode == 0)
  386. {
  387. return $langs->trans($this->statuts[$statut]);
  388. }
  389. if ($mode == 1)
  390. {
  391. return $langs->trans($this->statuts_short[$statut]);
  392. }
  393. if ($mode == 2)
  394. {
  395. if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
  396. if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
  397. if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts_short[$statut]);
  398. }
  399. if ($mode == 3)
  400. {
  401. if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  402. if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  403. if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
  404. }
  405. if ($mode == 4)
  406. {
  407. if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  408. if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
  409. if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
  410. }
  411. if ($mode == 5)
  412. {
  413. if ($statut==0) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
  414. if ($statut==1) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
  415. if ($statut==2) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
  416. }
  417. }
  418. /**
  419. * Return clicable name (with picto eventually)
  420. *
  421. * @param int $withpicto 0=_No picto, 1=Includes the picto in the linkn, 2=Picto only
  422. * @return string String with URL
  423. */
  424. function getNomUrl($withpicto=0)
  425. {
  426. global $langs;
  427. $result='';
  428. $lien = '<a href="'.DOL_URL_ROOT.'/fichinter/fiche.php?id='.$this->id.'">';
  429. $lienfin='</a>';
  430. $picto='intervention';
  431. $label=$langs->trans("Show").': '.$this->ref;
  432. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  433. if ($withpicto && $withpicto != 2) $result.=' ';
  434. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  435. return $result;
  436. }
  437. /**
  438. * Returns the next non used reference of intervention
  439. * depending on the module numbering assets within FICHEINTER_ADDON
  440. *
  441. * @param Societe $soc Object society
  442. * @return string Free reference for intervention
  443. */
  444. function getNextNumRef($soc)
  445. {
  446. global $conf, $db, $langs;
  447. $langs->load("interventions");
  448. $dir = DOL_DOCUMENT_ROOT . "/core/modules/fichinter/";
  449. if (! empty($conf->global->FICHEINTER_ADDON))
  450. {
  451. $file = $conf->global->FICHEINTER_ADDON.".php";
  452. $classname = $conf->global->FICHEINTER_ADDON;
  453. if (! file_exists($dir.$file))
  454. {
  455. $file='mod_'.$file;
  456. $classname='mod_'.$classname;
  457. }
  458. // Chargement de la classe de numerotation
  459. require_once $dir.$file;
  460. $obj = new $classname();
  461. $numref = "";
  462. $numref = $obj->getNumRef($soc,$this);
  463. if ( $numref != "")
  464. {
  465. return $numref;
  466. }
  467. else
  468. {
  469. dol_print_error($db,"Fichinter::getNextNumRef ".$obj->error);
  470. return "";
  471. }
  472. }
  473. else
  474. {
  475. print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
  476. return "";
  477. }
  478. }
  479. /**
  480. * Information sur l'objet fiche intervention
  481. *
  482. * @param int $id Id de la fiche d'intervention
  483. * @return void
  484. */
  485. function info($id)
  486. {
  487. global $conf;
  488. $sql = "SELECT f.rowid,";
  489. $sql.= " datec,";
  490. $sql.= " f.date_valid as datev,";
  491. $sql.= " f.fk_user_author,";
  492. $sql.= " f.fk_user_valid";
  493. $sql.= " FROM ".MAIN_DB_PREFIX."fichinter as f";
  494. $sql.= " WHERE f.rowid = ".$id;
  495. $sql.= " AND f.entity = ".$conf->entity;
  496. $result = $this->db->query($sql);
  497. if ($result)
  498. {
  499. if ($this->db->num_rows($result))
  500. {
  501. $obj = $this->db->fetch_object($result);
  502. $this->id = $obj->rowid;
  503. $this->date_creation = $this->db->jdate($obj->datec);
  504. $this->date_validation = $this->db->jdate($obj->datev);
  505. $cuser = new User($this->db);
  506. $cuser->fetch($obj->fk_user_author);
  507. $this->user_creation = $cuser;
  508. if ($obj->fk_user_valid)
  509. {
  510. $vuser = new User($this->db);
  511. $vuser->fetch($obj->fk_user_valid);
  512. $this->user_validation = $vuser;
  513. }
  514. }
  515. $this->db->free($result);
  516. }
  517. else
  518. {
  519. dol_print_error($this->db);
  520. }
  521. }
  522. /**
  523. * Delete intervetnion
  524. *
  525. * @param User $user Object user who delete
  526. * @return int <0 if KO, >0 if OK
  527. */
  528. function delete($user)
  529. {
  530. global $conf;
  531. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  532. $error=0;
  533. $this->db->begin();
  534. // Delete linked object
  535. $res = $this->deleteObjectLinked();
  536. if ($res < 0) $error++;
  537. // Delete linked contacts
  538. $res = $this->delete_linked_contact();
  539. if ($res < 0)
  540. {
  541. $this->error='ErrorFailToDeleteLinkedContact';
  542. $error++;
  543. }
  544. if ($error)
  545. {
  546. $this->db->rollback();
  547. return -1;
  548. }
  549. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
  550. $sql.= " WHERE fk_fichinter = ".$this->id;
  551. dol_syslog("Fichinter::delete sql=".$sql);
  552. if ( $this->db->query($sql) )
  553. {
  554. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
  555. $sql.= " WHERE rowid = ".$this->id;
  556. $sql.= " AND entity = ".$conf->entity;
  557. dol_syslog("Fichinter::delete sql=".$sql);
  558. if ( $this->db->query($sql) )
  559. {
  560. // Remove directory with files
  561. $fichinterref = dol_sanitizeFileName($this->ref);
  562. if ($conf->ficheinter->dir_output)
  563. {
  564. $dir = $conf->ficheinter->dir_output . "/" . $fichinterref ;
  565. $file = $conf->ficheinter->dir_output . "/" . $fichinterref . "/" . $fichinterref . ".pdf";
  566. if (file_exists($file))
  567. {
  568. dol_delete_preview($this);
  569. if (! dol_delete_file($file,0,0,0,$this)) // For triggers
  570. {
  571. $this->error=$langs->trans("ErrorCanNotDeleteFile",$file);
  572. return 0;
  573. }
  574. }
  575. if (file_exists($dir))
  576. {
  577. if (! dol_delete_dir_recursive($dir))
  578. {
  579. $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir);
  580. return 0;
  581. }
  582. }
  583. }
  584. // Appel des triggers
  585. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  586. $interface=new Interfaces($this->db);
  587. $result=$interface->run_triggers('FICHEINTER_DELETE',$this,$user,$langs,$conf);
  588. if ($result < 0) {
  589. $error++; $this->errors=$interface->errors;
  590. }
  591. // Fin appel triggers
  592. $this->db->commit();
  593. return 1;
  594. }
  595. else
  596. {
  597. $this->error=$this->db->lasterror();
  598. $this->db->rollback();
  599. return -2;
  600. }
  601. }
  602. else
  603. {
  604. $this->error=$this->db->lasterror();
  605. $this->db->rollback();
  606. return -1;
  607. }
  608. }
  609. /**
  610. * Defines a delivery date of intervention
  611. *
  612. * @param User $user Object user who define
  613. * @param date $date_delivery date of delivery
  614. * @return int <0 if ko, >0 if ok
  615. */
  616. function set_date_delivery($user, $date_delivery)
  617. {
  618. global $conf;
  619. if ($user->rights->ficheinter->creer)
  620. {
  621. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  622. $sql.= " SET datei = ".$this->db->idate($date_delivery);
  623. $sql.= " WHERE rowid = ".$this->id;
  624. $sql.= " AND entity = ".$conf->entity;
  625. $sql.= " AND fk_statut = 0";
  626. if ($this->db->query($sql))
  627. {
  628. $this->date_delivery = $date_delivery;
  629. return 1;
  630. }
  631. else
  632. {
  633. $this->error=$this->db->error();
  634. dol_syslog("Fichinter::set_date_delivery Erreur SQL");
  635. return -1;
  636. }
  637. }
  638. }
  639. /**
  640. * Define the label of the intervention
  641. *
  642. * @param User $user Object user who modify
  643. * @param string $description description
  644. * @return int <0 if ko, >0 if ok
  645. */
  646. function set_description($user, $description)
  647. {
  648. global $conf;
  649. if ($user->rights->ficheinter->creer)
  650. {
  651. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  652. $sql.= " SET description = '".$this->db->escape($description)."'";
  653. $sql.= " WHERE rowid = ".$this->id;
  654. $sql.= " AND entity = ".$conf->entity;
  655. $sql.= " AND fk_statut = 0";
  656. if ($this->db->query($sql))
  657. {
  658. $this->description = $description;
  659. return 1;
  660. }
  661. else
  662. {
  663. $this->error=$this->db->error();
  664. dol_syslog("Fichinter::set_description Erreur SQL");
  665. return -1;
  666. }
  667. }
  668. }
  669. /**
  670. * Adding a line of intervention into data base
  671. *
  672. * @param int $fichinterid Id of intervention
  673. * @param string $desc Line description
  674. * @param date $date_intervention Intervention date
  675. * @param int $duration Intervention duration
  676. * @return int >0 if ok, <0 if ko
  677. */
  678. function addline($fichinterid, $desc, $date_intervention, $duration)
  679. {
  680. dol_syslog("Fichinter::Addline $fichinterid, $desc, $date_intervention, $duration");
  681. if ($this->statut == 0)
  682. {
  683. $this->db->begin();
  684. // Insertion ligne
  685. $line=new FichinterLigne($this->db);
  686. $line->fk_fichinter = $fichinterid;
  687. $line->desc = $desc;
  688. $line->datei = $date_intervention;
  689. $line->duration = $duration;
  690. $result=$line->insert();
  691. if ($result > 0)
  692. {
  693. $this->db->commit();
  694. return 1;
  695. }
  696. else
  697. {
  698. $this->error=$this->db->error();
  699. dol_syslog("Error sql=$sql, error=".$this->error, LOG_ERR);
  700. $this->db->rollback();
  701. return -1;
  702. }
  703. }
  704. }
  705. /**
  706. * Initialise an instance with random values.
  707. * Used to build previews or test instances.
  708. * id must be 0 if object instance is a specimen.
  709. *
  710. * @return void
  711. */
  712. function initAsSpecimen()
  713. {
  714. global $user,$langs,$conf;
  715. $now=dol_now();
  716. // Initialise parametres
  717. $this->id=0;
  718. $this->ref = 'SPECIMEN';
  719. $this->specimen=1;
  720. $this->socid = 1;
  721. $this->datec = $now;
  722. $this->note_private='Private note';
  723. $this->note_public='SPECIMEN';
  724. $this->duree = 0;
  725. $nbp = 20;
  726. $xnbp = 0;
  727. while ($xnbp < $nbp)
  728. {
  729. $line=new FichinterLigne($this->db);
  730. $line->desc=$langs->trans("Description")." ".$xnbp;
  731. $line->datei=($now-3600*(1+$xnbp));
  732. $line->duration=600;
  733. $line->fk_fichinter=0;
  734. $this->lines[$xnbp]=$line;
  735. $xnbp++;
  736. $this->duree+=$line->duration;
  737. }
  738. }
  739. /**
  740. * Load array lines
  741. *
  742. * @return int <0 if Ko, >0 if OK
  743. */
  744. function fetch_lines()
  745. {
  746. $sql = 'SELECT rowid, description, duree, date, rang';
  747. $sql.= ' FROM '.MAIN_DB_PREFIX.'fichinterdet';
  748. $sql.= ' WHERE fk_fichinter = '.$this->id;
  749. dol_syslog(get_class($this)."::fetch_lines sql=".$sql);
  750. $resql=$this->db->query($sql);
  751. if ($resql)
  752. {
  753. $num = $this->db->num_rows($resql);
  754. $i = 0;
  755. while ($i < $num)
  756. {
  757. $objp = $this->db->fetch_object($resql);
  758. $line = new FichinterLigne($this->db);
  759. $line->id = $objp->rowid;
  760. $line->desc = $objp->description;
  761. //For invoicing we calculing hours
  762. $line->qty = round($objp->duree/3600,2);
  763. $line->date = $this->db->jdate($objp->date);
  764. $line->rang = $objp->rang;
  765. $line->product_type = 1;
  766. $this->lines[$i] = $line;
  767. $i++;
  768. }
  769. $this->db->free($resql);
  770. return 1;
  771. }
  772. else
  773. {
  774. $this->error=$this->db->error();
  775. return -1;
  776. }
  777. }
  778. }
  779. /**
  780. * Classe permettant la gestion des lignes d'intervention
  781. */
  782. class FichinterLigne
  783. {
  784. var $db;
  785. var $error;
  786. // From llx_fichinterdet
  787. var $rowid;
  788. var $fk_fichinter;
  789. var $desc; // Description ligne
  790. var $datei; // Date intervention
  791. var $duration; // Duree de l'intervention
  792. var $rang = 0;
  793. /**
  794. * Constructor
  795. *
  796. * @param DoliDB $db Database handler
  797. */
  798. function __construct($db = '')
  799. {
  800. $this->db = $db;
  801. }
  802. /**
  803. * Retrieve the line of intervention
  804. *
  805. * @param int $rowid Line id
  806. * @return int <0 if KO, >0 if OK
  807. */
  808. function fetch($rowid)
  809. {
  810. $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,';
  811. $sql.= ' ft.date as datei';
  812. $sql.= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
  813. $sql.= ' WHERE ft.rowid = '.$rowid;
  814. dol_syslog("FichinterLigne::fetch sql=".$sql);
  815. $result = $this->db->query($sql);
  816. if ($result)
  817. {
  818. $objp = $this->db->fetch_object($result);
  819. $this->rowid = $objp->rowid;
  820. $this->fk_fichinter = $objp->fk_fichinter;
  821. $this->datei = $this->db->jdate($objp->datei);
  822. $this->desc = $objp->description;
  823. $this->duration = $objp->duree;
  824. $this->rang = $objp->rang;
  825. $this->db->free($result);
  826. return 1;
  827. }
  828. else
  829. {
  830. $this->error=$this->db->error().' sql='.$sql;
  831. dol_print_error($this->db,$this->error, LOG_ERR);
  832. return -1;
  833. }
  834. }
  835. /**
  836. * Insert the line into database
  837. *
  838. * @return int <0 if ko, >0 if ok
  839. */
  840. function insert()
  841. {
  842. dol_syslog("FichinterLigne::insert rang=".$this->rang);
  843. $this->db->begin();
  844. $rangToUse=$this->rang;
  845. if ($rangToUse == -1)
  846. {
  847. // Recupere rang max de la ligne d'intervention dans $rangmax
  848. $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
  849. $sql.= ' WHERE fk_fichinter ='.$this->fk_fichinter;
  850. $resql = $this->db->query($sql);
  851. if ($resql)
  852. {
  853. $obj = $this->db->fetch_object($resql);
  854. $rangToUse = $obj->max + 1;
  855. }
  856. else
  857. {
  858. dol_print_error($this->db);
  859. $this->db->rollback();
  860. return -1;
  861. }
  862. }
  863. // Insertion dans base de la ligne
  864. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
  865. $sql.= ' (fk_fichinter, description, date, duree, rang)';
  866. $sql.= " VALUES (".$this->fk_fichinter.",";
  867. $sql.= " '".$this->db->escape($this->desc)."',";
  868. $sql.= " ".$this->db->idate($this->datei).",";
  869. $sql.= " ".$this->duration.",";
  870. $sql.= ' '.$rangToUse;
  871. $sql.= ')';
  872. dol_syslog("FichinterLigne::insert sql=".$sql);
  873. $resql=$this->db->query($sql);
  874. if ($resql)
  875. {
  876. $result=$this->update_total();
  877. if ($result > 0)
  878. {
  879. $this->rang=$rangToUse;
  880. $this->db->commit();
  881. return $result;
  882. }
  883. else
  884. {
  885. $this->db->rollback();
  886. return -1;
  887. }
  888. }
  889. else
  890. {
  891. $this->error=$this->db->error()." sql=".$sql;
  892. dol_syslog("FichinterLigne::insert Error ".$this->error, LOG_ERR);
  893. $this->db->rollback();
  894. return -1;
  895. }
  896. }
  897. /**
  898. * Update intervention into database
  899. *
  900. * @return int <0 if ko, >0 if ok
  901. */
  902. function update()
  903. {
  904. $this->db->begin();
  905. // Mise a jour ligne en base
  906. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
  907. $sql.= " description='".$this->db->escape($this->desc)."'";
  908. $sql.= ",date=".$this->db->idate($this->datei);
  909. $sql.= ",duree=".$this->duration;
  910. $sql.= ",rang='".$this->rang."'";
  911. $sql.= " WHERE rowid = ".$this->rowid;
  912. dol_syslog("FichinterLigne::update sql=".$sql);
  913. $resql=$this->db->query($sql);
  914. if ($resql)
  915. {
  916. $result=$this->update_total();
  917. if ($result > 0)
  918. {
  919. $this->db->commit();
  920. return $result;
  921. }
  922. else
  923. {
  924. $this->error=$this->db->lasterror();
  925. dol_syslog("FichinterLigne::update Error ".$this->error, LOG_ERR);
  926. $this->db->rollback();
  927. return -1;
  928. }
  929. }
  930. else
  931. {
  932. $this->error=$this->db->lasterror();
  933. dol_syslog("FichinterLigne::update Error ".$this->error, LOG_ERR);
  934. $this->db->rollback();
  935. return -1;
  936. }
  937. }
  938. /**
  939. * Update total duration into llx_fichinter
  940. *
  941. * @return int <0 si ko, >0 si ok
  942. */
  943. function update_total()
  944. {
  945. global $conf;
  946. $this->db->begin();
  947. $sql = "SELECT SUM(duree) as total_duration";
  948. $sql.= " FROM ".MAIN_DB_PREFIX."fichinterdet";
  949. $sql.= " WHERE fk_fichinter=".$this->fk_fichinter;
  950. dol_syslog("FichinterLigne::update_total sql=".$sql);
  951. $resql=$this->db->query($sql);
  952. if ($resql)
  953. {
  954. $obj=$this->db->fetch_object($resql);
  955. $total_duration=0;
  956. if (!empty($obj->total_duration)) $total_duration = $obj->total_duration;
  957. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  958. $sql.= " SET duree = ".$total_duration;
  959. $sql.= " WHERE rowid = ".$this->fk_fichinter;
  960. $sql.= " AND entity = ".$conf->entity;
  961. dol_syslog("FichinterLigne::update_total sql=".$sql);
  962. $resql=$this->db->query($sql);
  963. if ($resql)
  964. {
  965. $this->db->commit();
  966. return 1;
  967. }
  968. else
  969. {
  970. $this->error=$this->db->error();
  971. dol_syslog("FichinterLigne::update_total Error ".$this->error, LOG_ERR);
  972. $this->db->rollback();
  973. return -2;
  974. }
  975. }
  976. else
  977. {
  978. $this->error=$this->db->error();
  979. dol_syslog("FichinterLigne::update Error ".$this->error, LOG_ERR);
  980. $this->db->rollback();
  981. return -1;
  982. }
  983. }
  984. /**
  985. * Delete a intervention line
  986. *
  987. * @return int >0 if ok, <0 if ko
  988. */
  989. function deleteline()
  990. {
  991. if ($this->statut == 0)
  992. {
  993. dol_syslog(get_class($this)."::deleteline lineid=".$this->rowid);
  994. $this->db->begin();
  995. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".$this->rowid;
  996. $resql = $this->db->query($sql);
  997. dol_syslog(get_class($this)."::deleteline sql=".$sql);
  998. if ($resql)
  999. {
  1000. $result = $this->update_total();
  1001. if ($result > 0)
  1002. {
  1003. $this->db->commit();
  1004. return $result;
  1005. }
  1006. else
  1007. {
  1008. $this->db->rollback();
  1009. return -1;
  1010. }
  1011. }
  1012. else
  1013. {
  1014. $this->error=$this->db->error()." sql=".$sql;
  1015. dol_syslog(get_class($this)."::deleteline Error ".$this->error, LOG_ERR);
  1016. $this->db->rollback();
  1017. return -1;
  1018. }
  1019. }
  1020. else
  1021. {
  1022. return -2;
  1023. }
  1024. }
  1025. }
  1026. ?>