PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

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