fichinter.class.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011-2020 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2015-2020 Charlene Benke <charlie@patas-monkey.com>
  8. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  9. * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/fichinter/class/fichinter.class.php
  26. * \ingroup ficheinter
  27. * \brief Fichier de la classe des gestion des fiches interventions
  28. */
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
  31. /**
  32. * Class to manage interventions
  33. */
  34. class Fichinter extends CommonObject
  35. {
  36. public $fields = array(
  37. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  38. 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
  39. 'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk projet', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
  40. 'fk_contrat' =>array('type'=>'integer', 'label'=>'Fk contrat', 'enabled'=>1, 'visible'=>-1, 'position'=>25),
  41. 'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>30),
  42. 'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>35),
  43. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>40, 'index'=>1),
  44. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>45),
  45. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
  46. 'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  47. 'datei' =>array('type'=>'date', 'label'=>'Datei', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  48. 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
  49. 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>70),
  50. 'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
  51. 'fk_statut' =>array('type'=>'smallint(6)', 'label'=>'Fk statut', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
  52. 'dateo' =>array('type'=>'date', 'label'=>'Dateo', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
  53. 'datee' =>array('type'=>'date', 'label'=>'Datee', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
  54. 'datet' =>array('type'=>'date', 'label'=>'Datet', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
  55. 'duree' =>array('type'=>'double', 'label'=>'Duree', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
  56. 'description' =>array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2),
  57. 'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110),
  58. 'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>115),
  59. 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>120),
  60. 'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'Last main doc', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
  61. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>130),
  62. 'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>135),
  63. );
  64. /**
  65. * @var string ID to identify managed object
  66. */
  67. public $element = 'fichinter';
  68. /**
  69. * @var string Name of table without prefix where object is stored
  70. */
  71. public $table_element = 'fichinter';
  72. /**
  73. * @var string Field with ID of parent key if this field has a parent
  74. */
  75. public $fk_element = 'fk_fichinter';
  76. /**
  77. * @var int Name of subtable line
  78. */
  79. public $table_element_line = 'fichinterdet';
  80. /**
  81. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  82. */
  83. public $picto = 'intervention';
  84. /**
  85. * {@inheritdoc}
  86. */
  87. protected $table_ref_field = 'ref';
  88. /**
  89. * @var int Thirdparty Id
  90. */
  91. public $socid;
  92. public $author;
  93. /**
  94. * Date creation record (datec)
  95. *
  96. * @var integer
  97. */
  98. public $datec;
  99. public $datev;
  100. public $dateo;
  101. public $datee;
  102. public $datet;
  103. /**
  104. * Date modification record (tms)
  105. *
  106. * @var integer
  107. */
  108. public $datem;
  109. /**
  110. * @var int duration
  111. */
  112. public $duration;
  113. /**
  114. * @var int status
  115. */
  116. public $statut = 0; // 0=draft, 1=validated, 2=invoiced, 3=Terminate
  117. /**
  118. * @var string description
  119. */
  120. public $description;
  121. /**
  122. * @var int Contract ID
  123. */
  124. public $fk_contrat = 0;
  125. /**
  126. * @var int Project ID
  127. */
  128. public $fk_project = 0;
  129. /**
  130. * @var array extraparams
  131. */
  132. public $extraparams = array();
  133. /**
  134. * @var array lines
  135. */
  136. public $lines = array();
  137. /**
  138. * Draft status
  139. */
  140. const STATUS_DRAFT = 0;
  141. /**
  142. * Validated status
  143. */
  144. const STATUS_VALIDATED = 1;
  145. /**
  146. * Billed
  147. */
  148. const STATUS_BILLED = 2;
  149. /**
  150. * Closed
  151. */
  152. const STATUS_CLOSED = 3;
  153. /**
  154. * Constructor
  155. *
  156. * @param DoliDB $db Database handler
  157. */
  158. public function __construct($db)
  159. {
  160. $this->db = $db;
  161. }
  162. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  163. /**
  164. * Load indicators into this->nb for board
  165. *
  166. * @return int <0 if KO, >0 if OK
  167. */
  168. public function load_state_board()
  169. {
  170. // phpcs:enable
  171. global $user;
  172. $this->nb = array();
  173. $clause = "WHERE";
  174. $sql = "SELECT count(fi.rowid) as nb";
  175. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as fi";
  176. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON fi.fk_soc = s.rowid";
  177. if (!$user->rights->societe->client->voir && !$user->socid) {
  178. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  179. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  180. $clause = "AND";
  181. }
  182. $sql .= " ".$clause." fi.entity IN (".getEntity('intervention').")";
  183. $resql = $this->db->query($sql);
  184. if ($resql) {
  185. while ($obj = $this->db->fetch_object($resql)) {
  186. $this->nb["interventions"] = $obj->nb;
  187. }
  188. $this->db->free($resql);
  189. return 1;
  190. } else {
  191. dol_print_error($this->db);
  192. $this->error = $this->db->error();
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Create an intervention into data base
  198. *
  199. * @param User $user Objet user that make creation
  200. * @param int $notrigger Disable all triggers
  201. * @return int <0 if KO, >0 if OK
  202. */
  203. public function create($user, $notrigger = 0)
  204. {
  205. global $conf, $langs;
  206. $error = 0;
  207. dol_syslog(get_class($this)."::create ref=".$this->ref);
  208. // Check parameters
  209. if (!empty($this->ref)) { // We check that ref is not already used
  210. $result = self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
  211. if ($result > 0) {
  212. $this->error = 'ErrorRefAlreadyExists';
  213. dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
  214. $this->db->rollback();
  215. return -1;
  216. }
  217. }
  218. if (!is_numeric($this->duration)) {
  219. $this->duration = 0;
  220. }
  221. if ($this->socid <= 0) {
  222. $this->error = 'ErrorBadParameterForFunc';
  223. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  224. return -1;
  225. }
  226. $soc = new Societe($this->db);
  227. $result = $soc->fetch($this->socid);
  228. $now = dol_now();
  229. $this->db->begin();
  230. $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter (";
  231. $sql .= "fk_soc";
  232. $sql .= ", datec";
  233. $sql .= ", ref";
  234. $sql .= ", entity";
  235. $sql .= ", fk_user_author";
  236. $sql .= ", fk_user_modif";
  237. $sql .= ", description";
  238. $sql .= ", model_pdf";
  239. $sql .= ", fk_projet";
  240. $sql .= ", fk_contrat";
  241. $sql .= ", fk_statut";
  242. $sql .= ", note_private";
  243. $sql .= ", note_public";
  244. $sql .= ") ";
  245. $sql .= " VALUES (";
  246. $sql .= $this->socid;
  247. $sql .= ", '".$this->db->idate($now)."'";
  248. $sql .= ", '".$this->db->escape($this->ref)."'";
  249. $sql .= ", ".$conf->entity;
  250. $sql .= ", ".$user->id;
  251. $sql .= ", ".$user->id;
  252. $sql .= ", ".($this->description ? "'".$this->db->escape($this->description)."'" : "null");
  253. $sql .= ", '".$this->db->escape($this->model_pdf)."'";
  254. $sql .= ", ".($this->fk_project ? $this->fk_project : 0);
  255. $sql .= ", ".($this->fk_contrat ? $this->fk_contrat : 0);
  256. $sql .= ", ".$this->statut;
  257. $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
  258. $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
  259. $sql .= ")";
  260. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  261. $result = $this->db->query($sql);
  262. if ($result) {
  263. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."fichinter");
  264. if ($this->id) {
  265. $this->ref = '(PROV'.$this->id.')';
  266. $sql = 'UPDATE '.MAIN_DB_PREFIX."fichinter SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
  267. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  268. $resql = $this->db->query($sql);
  269. if (!$resql) {
  270. $error++;
  271. }
  272. }
  273. if (!$error) {
  274. $result = $this->insertExtraFields();
  275. if ($result < 0) {
  276. $error++;
  277. }
  278. }
  279. // Add linked object
  280. if (!$error && $this->origin && $this->origin_id) {
  281. $ret = $this->add_object_linked();
  282. if (!$ret) {
  283. dol_print_error($this->db);
  284. }
  285. }
  286. if (!$error && !$notrigger) {
  287. // Call trigger
  288. $result = $this->call_trigger('FICHINTER_CREATE', $user);
  289. if ($result < 0) {
  290. $error++;
  291. }
  292. // End call triggers
  293. }
  294. if (!$error) {
  295. $this->db->commit();
  296. return $this->id;
  297. } else {
  298. $this->db->rollback();
  299. $this->error = join(',', $this->errors);
  300. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  301. return -1;
  302. }
  303. } else {
  304. $this->error = $this->db->error();
  305. $this->db->rollback();
  306. return -1;
  307. }
  308. }
  309. /**
  310. * Update an intervention
  311. *
  312. * @param User $user Objet user that make creation
  313. * @param int $notrigger Disable all triggers
  314. * @return int <0 if KO, >0 if OK
  315. */
  316. public function update($user, $notrigger = 0)
  317. {
  318. global $conf;
  319. if (!is_numeric($this->duration)) {
  320. $this->duration = 0;
  321. }
  322. if (!dol_strlen($this->fk_project)) {
  323. $this->fk_project = 0;
  324. }
  325. $error = 0;
  326. $this->db->begin();
  327. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter SET ";
  328. $sql .= "description = '".$this->db->escape($this->description)."'";
  329. $sql .= ", duree = ".((int) $this->duration);
  330. $sql .= ", fk_projet = ".((int) $this->fk_project);
  331. $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
  332. $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
  333. $sql .= ", fk_user_modif = ".((int) $user->id);
  334. $sql .= " WHERE rowid = ".((int) $this->id);
  335. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  336. if ($this->db->query($sql)) {
  337. if (!$error) {
  338. $result = $this->insertExtraFields();
  339. if ($result < 0) {
  340. $error++;
  341. }
  342. }
  343. if (!$error && !$notrigger) {
  344. // Call trigger
  345. $result = $this->call_trigger('FICHINTER_MODIFY', $user);
  346. if ($result < 0) {
  347. $error++; $this->db->rollback(); return -1;
  348. }
  349. // End call triggers
  350. }
  351. $this->db->commit();
  352. return 1;
  353. } else {
  354. $this->error = $this->db->error();
  355. $this->db->rollback();
  356. return -1;
  357. }
  358. }
  359. /**
  360. * Fetch a intervention
  361. *
  362. * @param int $rowid Id of intervention
  363. * @param string $ref Ref of intervention
  364. * @return int <0 if KO, >0 if OK
  365. */
  366. public function fetch($rowid, $ref = '')
  367. {
  368. $sql = "SELECT f.rowid, f.ref, f.description, f.fk_soc, f.fk_statut,";
  369. $sql .= " f.datec, f.dateo, f.datee, f.datet, f.fk_user_author,";
  370. $sql .= " f.date_valid as datev,";
  371. $sql .= " f.tms as datem,";
  372. $sql .= " f.duree, f.fk_projet as fk_project, f.note_public, f.note_private, f.model_pdf, f.extraparams, fk_contrat";
  373. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
  374. if ($ref) {
  375. $sql .= " WHERE f.entity IN (".getEntity('intervention').")";
  376. $sql .= " AND f.ref='".$this->db->escape($ref)."'";
  377. } else {
  378. $sql .= " WHERE f.rowid=".((int) $rowid);
  379. }
  380. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  381. $resql = $this->db->query($sql);
  382. if ($resql) {
  383. if ($this->db->num_rows($resql)) {
  384. $obj = $this->db->fetch_object($resql);
  385. $this->id = $obj->rowid;
  386. $this->ref = $obj->ref;
  387. $this->description = $obj->description;
  388. $this->socid = $obj->fk_soc;
  389. $this->statut = $obj->fk_statut;
  390. $this->duration = $obj->duree;
  391. $this->datec = $this->db->jdate($obj->datec);
  392. $this->dateo = $this->db->jdate($obj->dateo);
  393. $this->datee = $this->db->jdate($obj->datee);
  394. $this->datet = $this->db->jdate($obj->datet);
  395. $this->datev = $this->db->jdate($obj->datev);
  396. $this->datem = $this->db->jdate($obj->datem);
  397. $this->fk_project = $obj->fk_project;
  398. $this->note_public = $obj->note_public;
  399. $this->note_private = $obj->note_private;
  400. $this->model_pdf = $obj->model_pdf;
  401. $this->modelpdf = $obj->model_pdf; // deprecated
  402. $this->fk_contrat = $obj->fk_contrat;
  403. $this->user_creation = $obj->fk_user_author;
  404. $this->extraparams = (array) json_decode($obj->extraparams, true);
  405. if ($this->statut == 0) {
  406. $this->brouillon = 1;
  407. }
  408. // Retrieve extrafields
  409. $this->fetch_optionals();
  410. /*
  411. * Lines
  412. */
  413. $result = $this->fetch_lines();
  414. if ($result < 0) {
  415. return -3;
  416. }
  417. $this->db->free($resql);
  418. return 1;
  419. }
  420. } else {
  421. $this->error = $this->db->lasterror();
  422. return -1;
  423. }
  424. }
  425. /**
  426. * Set status to draft
  427. *
  428. * @param User $user User that set draft
  429. * @return int <0 if KO, >0 if OK
  430. */
  431. public function setDraft($user)
  432. {
  433. global $langs, $conf;
  434. $error = 0;
  435. // Protection
  436. if ($this->statut <= self::STATUS_DRAFT) {
  437. return 0;
  438. }
  439. dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
  440. $this->db->begin();
  441. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  442. $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
  443. $sql .= " WHERE rowid = ".((int) $this->id);
  444. $resql = $this->db->query($sql);
  445. if ($resql) {
  446. if (!$error) {
  447. $this->oldcopy = clone $this;
  448. }
  449. if (!$error) {
  450. // Call trigger
  451. $result = $this->call_trigger('FICHINTER_UNVALIDATE', $user);
  452. if ($result < 0) {
  453. $error++;
  454. }
  455. }
  456. if (!$error) {
  457. $this->statut = self::STATUS_DRAFT;
  458. $this->db->commit();
  459. return 1;
  460. } else {
  461. $this->db->rollback();
  462. return -1;
  463. }
  464. } else {
  465. $this->db->rollback();
  466. $this->error = $this->db->lasterror();
  467. return -1;
  468. }
  469. }
  470. /**
  471. * Validate a intervention
  472. *
  473. * @param User $user User that validate
  474. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  475. * @return int <0 if KO, >0 if OK
  476. */
  477. public function setValid($user, $notrigger = 0)
  478. {
  479. global $conf;
  480. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  481. $error = 0;
  482. if ($this->statut != 1) {
  483. $this->db->begin();
  484. $now = dol_now();
  485. // Define new ref
  486. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  487. $num = $this->getNextNumRef($this->thirdparty);
  488. } else {
  489. $num = $this->ref;
  490. }
  491. $this->newref = dol_sanitizeFileName($num);
  492. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  493. $sql .= " SET fk_statut = 1";
  494. $sql .= ", ref = '".$this->db->escape($num)."'";
  495. $sql .= ", date_valid = '".$this->db->idate($now)."'";
  496. $sql .= ", fk_user_valid = ".((int) $user->id);
  497. $sql .= " WHERE rowid = ".((int) $this->id);
  498. $sql .= " AND entity = ".((int) $conf->entity);
  499. $sql .= " AND fk_statut = 0";
  500. dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
  501. $resql = $this->db->query($sql);
  502. if (!$resql) {
  503. dol_print_error($this->db);
  504. $error++;
  505. }
  506. if (!$error && !$notrigger) {
  507. // Call trigger
  508. $result = $this->call_trigger('FICHINTER_VALIDATE', $user);
  509. if ($result < 0) {
  510. $error++;
  511. }
  512. // End call triggers
  513. }
  514. if (!$error) {
  515. $this->oldref = $this->ref;
  516. // Rename directory if dir was a temporary ref
  517. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  518. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  519. // Now we rename also files into index
  520. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'ficheinter/".$this->db->escape($this->newref)."'";
  521. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'ficheinter/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  522. $resql = $this->db->query($sql);
  523. if (!$resql) {
  524. $error++; $this->error = $this->db->lasterror();
  525. }
  526. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  527. $oldref = dol_sanitizeFileName($this->ref);
  528. $newref = dol_sanitizeFileName($num);
  529. $dirsource = $conf->ficheinter->dir_output.'/'.$oldref;
  530. $dirdest = $conf->ficheinter->dir_output.'/'.$newref;
  531. if (!$error && file_exists($dirsource)) {
  532. dol_syslog(get_class($this)."::setValid rename dir ".$dirsource." into ".$dirdest);
  533. if (@rename($dirsource, $dirdest)) {
  534. dol_syslog("Rename ok");
  535. // Rename docs starting with $oldref with $newref
  536. $listoffiles = dol_dir_list($conf->ficheinter->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  537. foreach ($listoffiles as $fileentry) {
  538. $dirsource = $fileentry['name'];
  539. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  540. $dirsource = $fileentry['path'].'/'.$dirsource;
  541. $dirdest = $fileentry['path'].'/'.$dirdest;
  542. @rename($dirsource, $dirdest);
  543. }
  544. }
  545. }
  546. }
  547. }
  548. // Set new ref and define current statut
  549. if (!$error) {
  550. $this->ref = $num;
  551. $this->statut = 1;
  552. $this->brouillon = 0;
  553. $this->date_validation = $now;
  554. $this->db->commit();
  555. return 1;
  556. } else {
  557. $this->db->rollback();
  558. dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
  559. return -1;
  560. }
  561. }
  562. }
  563. /**
  564. * Returns amount based on user thm
  565. *
  566. * @return float Amount
  567. */
  568. public function getAmount()
  569. {
  570. global $db;
  571. $amount = 0;
  572. $this->author = new User($db);
  573. $this->author->fetch($this->user_creation);
  574. $thm = $this->author->thm;
  575. foreach ($this->lines as $line) {
  576. $amount += ($line->duration / 60 / 60 * $thm);
  577. }
  578. return price2num($amount, 'MT');
  579. }
  580. /**
  581. * Create a document onto disk according to template module.
  582. *
  583. * @param string $modele Force model to use ('' to not force)
  584. * @param Translate $outputlangs Object langs to use for output
  585. * @param int $hidedetails Hide details of lines
  586. * @param int $hidedesc Hide description
  587. * @param int $hideref Hide ref
  588. * @param null|array $moreparams Array to provide more information
  589. * @return int 0 if KO, 1 if OK
  590. */
  591. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  592. {
  593. global $conf;
  594. $outputlangs->load("interventions");
  595. if (!dol_strlen($modele)) {
  596. $modele = 'soleil';
  597. if (!empty($this->model_pdf)) {
  598. $modele = $this->model_pdf;
  599. } elseif (!empty($this->modelpdf)) { // deprecated
  600. $modele = $this->modelpdf;
  601. } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) {
  602. $modele = $conf->global->FICHEINTER_ADDON_PDF;
  603. }
  604. }
  605. $modelpath = "core/modules/fichinter/doc/";
  606. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  607. }
  608. /**
  609. * Returns the label status
  610. *
  611. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  612. * @return string Label
  613. */
  614. public function getLibStatut($mode = 0)
  615. {
  616. return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
  617. }
  618. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  619. /**
  620. * Returns the label of a status
  621. *
  622. * @param int $status Id status
  623. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  624. * @return string Label
  625. */
  626. public function LibStatut($status, $mode = 0)
  627. {
  628. // phpcs:enable
  629. // Init/load array of translation of status
  630. if (empty($this->statuts) || empty($this->statuts_short) || empty($this->statuts_logo)) {
  631. global $langs;
  632. $langs->load("fichinter");
  633. $this->statuts[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  634. $this->statuts[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  635. $this->statuts[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
  636. $this->statuts[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
  637. $this->statuts_short[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  638. $this->statuts_short[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  639. $this->statuts_short[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
  640. $this->statuts_short[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
  641. $this->statuts_logo[self::STATUS_DRAFT] = 'status0';
  642. $this->statuts_logo[self::STATUS_VALIDATED] = 'status1';
  643. $this->statuts_logo[self::STATUS_BILLED] = 'status6';
  644. $this->statuts_logo[self::STATUS_CLOSED] = 'status6';
  645. }
  646. return dolGetStatus($this->statuts[$status], $this->statuts_short[$status], '', $this->statuts_logo[$status], $mode);
  647. }
  648. /**
  649. * Return clicable name (with picto eventually)
  650. *
  651. * @param int $withpicto 0=_No picto, 1=Includes the picto in the linkn, 2=Picto only
  652. * @param string $option Options
  653. * @param int $notooltip 1=Disable tooltip
  654. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  655. * @return string String with URL
  656. */
  657. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1)
  658. {
  659. global $conf, $langs, $hookmanager;
  660. $result = '';
  661. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Intervention").'</u>';
  662. if (isset($this->status)) {
  663. $label .= ' '.$this->getLibStatut(5);
  664. }
  665. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  666. $url = DOL_URL_ROOT.'/fichinter/card.php?id='.$this->id;
  667. if ($option !== 'nolink') {
  668. // Add param to save lastsearch_values or not
  669. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  670. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  671. $add_save_lastsearch_values = 1;
  672. }
  673. if ($add_save_lastsearch_values) {
  674. $url .= '&save_lastsearch_values=1';
  675. }
  676. }
  677. $linkclose = '';
  678. if (empty($notooltip)) {
  679. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  680. $label = $langs->trans("ShowIntervention");
  681. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  682. }
  683. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  684. $linkclose .= ' class="classfortooltip"';
  685. /*
  686. $hookmanager->initHooks(array('fichinterdao'));
  687. $parameters=array('id'=>$this->id);
  688. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  689. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  690. */
  691. }
  692. $linkstart = '<a href="'.$url.'"';
  693. $linkstart .= $linkclose.'>';
  694. $linkend = '</a>';
  695. $result .= $linkstart;
  696. if ($withpicto) {
  697. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  698. }
  699. if ($withpicto != 2) {
  700. $result .= $this->ref;
  701. }
  702. $result .= $linkend;
  703. global $action;
  704. $hookmanager->initHooks(array('interventiondao'));
  705. $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
  706. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  707. if ($reshook > 0) {
  708. $result = $hookmanager->resPrint;
  709. } else {
  710. $result .= $hookmanager->resPrint;
  711. }
  712. return $result;
  713. }
  714. /**
  715. * Returns the next non used reference of intervention
  716. * depending on the module numbering assets within FICHEINTER_ADDON
  717. *
  718. * @param Societe $soc Thirdparty object
  719. * @return string Free reference for intervention
  720. */
  721. public function getNextNumRef($soc)
  722. {
  723. global $conf, $db, $langs;
  724. $langs->load("interventions");
  725. if (!empty($conf->global->FICHEINTER_ADDON)) {
  726. $mybool = false;
  727. $file = "mod_".$conf->global->FICHEINTER_ADDON.".php";
  728. $classname = "mod_".$conf->global->FICHEINTER_ADDON;
  729. // Include file with class
  730. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  731. foreach ($dirmodels as $reldir) {
  732. $dir = dol_buildpath($reldir."core/modules/fichinter/");
  733. // Load file with numbering class (if found)
  734. $mybool |= @include_once $dir.$file;
  735. }
  736. if ($mybool === false) {
  737. dol_print_error('', "Failed to include file ".$file);
  738. return '';
  739. }
  740. $obj = new $classname();
  741. $numref = "";
  742. $numref = $obj->getNextValue($soc, $this);
  743. if ($numref != "") {
  744. return $numref;
  745. } else {
  746. dol_print_error($db, "Fichinter::getNextNumRef ".$obj->error);
  747. return "";
  748. }
  749. } else {
  750. $langs->load("errors");
  751. print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
  752. return "";
  753. }
  754. }
  755. /**
  756. * Load information on object
  757. *
  758. * @param int $id Id of object
  759. * @return void
  760. */
  761. public function info($id)
  762. {
  763. global $conf;
  764. $sql = "SELECT f.rowid,";
  765. $sql .= " f.datec,";
  766. $sql .= " f.tms as date_modification,";
  767. $sql .= " f.date_valid as datev,";
  768. $sql .= " f.fk_user_author,";
  769. $sql .= " f.fk_user_modif as fk_user_modification,";
  770. $sql .= " f.fk_user_valid";
  771. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
  772. $sql .= " WHERE f.rowid = ".((int) $id);
  773. $resql = $this->db->query($sql);
  774. if ($resql) {
  775. if ($this->db->num_rows($resql)) {
  776. $obj = $this->db->fetch_object($resql);
  777. $this->id = $obj->rowid;
  778. $this->date_creation = $this->db->jdate($obj->datec);
  779. $this->date_modification = $this->db->jdate($obj->date_modification);
  780. $this->date_validation = $this->db->jdate($obj->datev);
  781. $cuser = new User($this->db);
  782. $cuser->fetch($obj->fk_user_author);
  783. $this->user_creation = $cuser;
  784. if ($obj->fk_user_valid) {
  785. $vuser = new User($this->db);
  786. $vuser->fetch($obj->fk_user_valid);
  787. $this->user_validation = $vuser;
  788. }
  789. if ($obj->fk_user_modification) {
  790. $muser = new User($this->db);
  791. $muser->fetch($obj->fk_user_modification);
  792. $this->user_modification = $muser;
  793. }
  794. }
  795. $this->db->free($resql);
  796. } else {
  797. dol_print_error($this->db);
  798. }
  799. }
  800. /**
  801. * Delete intervetnion
  802. *
  803. * @param User $user Object user who delete
  804. * @param int $notrigger Disable trigger
  805. * @return int <0 if KO, >0 if OK
  806. */
  807. public function delete($user, $notrigger = 0)
  808. {
  809. global $conf, $langs;
  810. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  811. $error = 0;
  812. $this->db->begin();
  813. if (!$error && !$notrigger) {
  814. // Call trigger
  815. $result = $this->call_trigger('FICHINTER_DELETE', $user);
  816. if ($result < 0) {
  817. $error++; $this->db->rollback(); return -1;
  818. }
  819. // End call triggers
  820. }
  821. // Delete linked object
  822. if (!$error) {
  823. $res = $this->deleteObjectLinked();
  824. if ($res < 0) {
  825. $error++;
  826. }
  827. }
  828. // Delete linked contacts
  829. if (!$error) {
  830. $res = $this->delete_linked_contact();
  831. if ($res < 0) {
  832. $this->error = 'ErrorFailToDeleteLinkedContact';
  833. $error++;
  834. }
  835. }
  836. if (!$error) {
  837. $main = MAIN_DB_PREFIX.'fichinterdet';
  838. $ef = $main."_extrafields";
  839. $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = ".((int) $this->id).")";
  840. $resql = $this->db->query($sql);
  841. if (!$resql) {
  842. $error++;
  843. }
  844. }
  845. if (!$error) {
  846. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
  847. $sql .= " WHERE fk_fichinter = ".((int) $this->id);
  848. $resql = $this->db->query($sql);
  849. if (!$resql) {
  850. $error++;
  851. }
  852. }
  853. if (!$error) {
  854. // Remove extrafields
  855. $res = $this->deleteExtraFields();
  856. if ($res < 0) {
  857. $error++;
  858. }
  859. }
  860. if (!$error) {
  861. // Delete object
  862. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
  863. $sql .= " WHERE rowid = ".((int) $this->id);
  864. dol_syslog("Fichinter::delete", LOG_DEBUG);
  865. $resql = $this->db->query($sql);
  866. if (!$resql) {
  867. $error++;
  868. }
  869. }
  870. if (!$error) {
  871. // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
  872. $this->deleteEcmFiles();
  873. // Remove directory with files
  874. $fichinterref = dol_sanitizeFileName($this->ref);
  875. if ($conf->ficheinter->dir_output) {
  876. $dir = $conf->ficheinter->dir_output."/".$fichinterref;
  877. $file = $conf->ficheinter->dir_output."/".$fichinterref."/".$fichinterref.".pdf";
  878. if (file_exists($file)) {
  879. dol_delete_preview($this);
  880. if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
  881. $langs->load("errors");
  882. $this->error = $langs->trans("ErrorFailToDeleteFile", $file);
  883. return 0;
  884. }
  885. }
  886. if (file_exists($dir)) {
  887. if (!dol_delete_dir_recursive($dir)) {
  888. $langs->load("errors");
  889. $this->error = $langs->trans("ErrorFailToDeleteDir", $dir);
  890. return 0;
  891. }
  892. }
  893. }
  894. }
  895. if (!$error) {
  896. $this->db->commit();
  897. return 1;
  898. } else {
  899. $this->db->rollback();
  900. return -1;
  901. }
  902. }
  903. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  904. /**
  905. * Defines a delivery date of intervention
  906. *
  907. * @param User $user Object user who define
  908. * @param integer $date_delivery date of delivery
  909. * @return int <0 if ko, >0 if ok
  910. */
  911. public function set_date_delivery($user, $date_delivery)
  912. {
  913. // phpcs:enable
  914. global $conf;
  915. if ($user->rights->ficheinter->creer) {
  916. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  917. $sql .= " SET datei = '".$this->db->idate($date_delivery)."'";
  918. $sql .= " WHERE rowid = ".((int) $this->id);
  919. $sql .= " AND fk_statut = 0";
  920. if ($this->db->query($sql)) {
  921. $this->date_delivery = $date_delivery;
  922. return 1;
  923. } else {
  924. $this->error = $this->db->error();
  925. dol_syslog("Fichinter::set_date_delivery Erreur SQL");
  926. return -1;
  927. }
  928. }
  929. }
  930. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  931. /**
  932. * Define the label of the intervention
  933. *
  934. * @param User $user Object user who modify
  935. * @param string $description description
  936. * @return int <0 if KO, >0 if OK
  937. */
  938. public function set_description($user, $description)
  939. {
  940. // phpcs:enable
  941. global $conf;
  942. if ($user->rights->ficheinter->creer) {
  943. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  944. $sql .= " SET description = '".$this->db->escape($description)."',";
  945. $sql .= " fk_user_modif = ".$user->id;
  946. $sql .= " WHERE rowid = ".((int) $this->id);
  947. if ($this->db->query($sql)) {
  948. $this->description = $description;
  949. return 1;
  950. } else {
  951. $this->error = $this->db->error();
  952. dol_syslog("Fichinter::set_description Erreur SQL");
  953. return -1;
  954. }
  955. }
  956. }
  957. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  958. /**
  959. * Link intervention to a contract
  960. *
  961. * @param User $user Object user who modify
  962. * @param int $contractid Description
  963. * @return int <0 if ko, >0 if ok
  964. */
  965. public function set_contrat($user, $contractid)
  966. {
  967. // phpcs:enable
  968. global $conf;
  969. if ($user->rights->ficheinter->creer) {
  970. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  971. $sql .= " SET fk_contrat = ".((int) $contractid);
  972. $sql .= " WHERE rowid = ".((int) $this->id);
  973. if ($this->db->query($sql)) {
  974. $this->fk_contrat = $contractid;
  975. return 1;
  976. } else {
  977. $this->error = $this->db->error();
  978. return -1;
  979. }
  980. }
  981. return -2;
  982. }
  983. /**
  984. * Load an object from its id and create a new one in database
  985. *
  986. * @param User $user User making the clone
  987. * @param int $socid Id of thirdparty
  988. * @return int New id of clone
  989. */
  990. public function createFromClone(User $user, $socid = 0)
  991. {
  992. global $hookmanager;
  993. $error = 0;
  994. $this->db->begin();
  995. // get extrafields so they will be clone
  996. foreach ($this->lines as $line) {
  997. $line->fetch_optionals();
  998. }
  999. // Load source object
  1000. $objFrom = clone $this;
  1001. // Change socid if needed
  1002. if (!empty($socid) && $socid != $this->socid) {
  1003. $objsoc = new Societe($this->db);
  1004. if ($objsoc->fetch($socid) > 0) {
  1005. $this->socid = $objsoc->id;
  1006. //$this->cond_reglement_id = (! empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
  1007. //$this->mode_reglement_id = (! empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
  1008. $this->fk_project = '';
  1009. $this->fk_delivery_address = '';
  1010. }
  1011. // TODO Change product price if multi-prices
  1012. }
  1013. $this->id = 0;
  1014. $this->ref = '';
  1015. $this->statut = 0;
  1016. // Clear fields
  1017. $this->user_author_id = $user->id;
  1018. $this->user_valid = '';
  1019. $this->date_creation = '';
  1020. $this->date_validation = '';
  1021. $this->ref_client = '';
  1022. // Create clone
  1023. $this->context['createfromclone'] = 'createfromclone';
  1024. $result = $this->create($user);
  1025. if ($result < 0) {
  1026. $error++;
  1027. }
  1028. if (!$error) {
  1029. // Add lines because it is not included into create function
  1030. foreach ($this->lines as $line) {
  1031. $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration);
  1032. }
  1033. // Hook of thirdparty module
  1034. if (is_object($hookmanager)) {
  1035. $parameters = array('objFrom'=>$objFrom);
  1036. $action = '';
  1037. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1038. if ($reshook < 0) {
  1039. $error++;
  1040. }
  1041. }
  1042. }
  1043. unset($this->context['createfromclone']);
  1044. // End
  1045. if (!$error) {
  1046. $this->db->commit();
  1047. return $this->id;
  1048. } else {
  1049. $this->db->rollback();
  1050. return -1;
  1051. }
  1052. }
  1053. /**
  1054. * Adding a line of intervention into data base
  1055. *
  1056. * @param user $user User that do the action
  1057. * @param int $fichinterid Id of intervention
  1058. * @param string $desc Line description
  1059. * @param integer $date_intervention Intervention date
  1060. * @param int $duration Intervention duration
  1061. * @param array $array_options Array option
  1062. * @return int >0 if ok, <0 if ko
  1063. */
  1064. public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
  1065. {
  1066. dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
  1067. if ($this->statut == 0) {
  1068. $this->db->begin();
  1069. // Insertion ligne
  1070. $line = new FichinterLigne($this->db);
  1071. $line->fk_fichinter = $fichinterid;
  1072. $line->desc = $desc;
  1073. $line->datei = $date_intervention;
  1074. $line->duration = $duration;
  1075. if (is_array($array_options) && count($array_options) > 0) {
  1076. $line->array_options = $array_options;
  1077. }
  1078. $result = $line->insert($user);
  1079. if ($result >= 0) {
  1080. $this->db->commit();
  1081. return 1;
  1082. } else {
  1083. $this->error = $this->db->error();
  1084. $this->db->rollback();
  1085. return -1;
  1086. }
  1087. }
  1088. }
  1089. /**
  1090. * Initialise an instance with random values.
  1091. * Used to build previews or test instances.
  1092. * id must be 0 if object instance is a specimen.
  1093. *
  1094. * @return void
  1095. */
  1096. public function initAsSpecimen()
  1097. {
  1098. global $user, $langs, $conf;
  1099. $now = dol_now();
  1100. // Initialise parametres
  1101. $this->id = 0;
  1102. $this->ref = 'SPECIMEN';
  1103. $this->specimen = 1;
  1104. $this->socid = 1;
  1105. $this->datec = $now;
  1106. $this->note_private = 'Private note';
  1107. $this->note_public = 'SPECIMEN';
  1108. $this->duration = 0;
  1109. $nbp = 25;
  1110. $xnbp = 0;
  1111. while ($xnbp < $nbp) {
  1112. $line = new FichinterLigne($this->db);
  1113. $line->desc = $langs->trans("Description")." ".$xnbp;
  1114. $line->datei = ($now - 3600 * (1 + $xnbp));
  1115. $line->duration = 600;
  1116. $line->fk_fichinter = 0;
  1117. $this->lines[$xnbp] = $line;
  1118. $xnbp++;
  1119. $this->duration += $line->duration;
  1120. }
  1121. }
  1122. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1123. /**
  1124. * Load array lines ->lines
  1125. *
  1126. * @return int <0 if KO, >0 if OK
  1127. */
  1128. public function fetch_lines()
  1129. {
  1130. // phpcs:enable
  1131. $this->lines = array();
  1132. $sql = 'SELECT rowid, fk_fichinter, description, duree, date, rang';
  1133. $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet';
  1134. $sql .= ' WHERE fk_fichinter = '.((int) $this->id).' ORDER BY rang ASC, date ASC';
  1135. dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
  1136. $resql = $this->db->query($sql);
  1137. if ($resql) {
  1138. $num = $this->db->num_rows($resql);
  1139. $i = 0;
  1140. while ($i < $num) {
  1141. $objp = $this->db->fetch_object($resql);
  1142. $line = new FichinterLigne($this->db);
  1143. $line->id = $objp->rowid;
  1144. $line->fk_fichinter = $objp->fk_fichinter;
  1145. $line->desc = $objp->description;
  1146. $line->duration = $objp->duree;
  1147. //For invoicing we calculing hours
  1148. $line->qty = round($objp->duree / 3600, 2);
  1149. $line->date = $this->db->jdate($objp->date);
  1150. $line->datei = $this->db->jdate($objp->date);
  1151. $line->rang = $objp->rang;
  1152. $line->product_type = 1;
  1153. $line->fetch_optionals();
  1154. $this->lines[$i] = $line;
  1155. $i++;
  1156. }
  1157. $this->db->free($resql);
  1158. return 1;
  1159. } else {
  1160. $this->error = $this->db->error();
  1161. return -1;
  1162. }
  1163. }
  1164. /**
  1165. * Function used to replace a thirdparty id with another one.
  1166. *
  1167. * @param DoliDB $db Database handler
  1168. * @param int $origin_id Old thirdparty id
  1169. * @param int $dest_id New thirdparty id
  1170. * @return bool
  1171. */
  1172. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  1173. {
  1174. $tables = array(
  1175. 'fichinter'
  1176. );
  1177. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1178. }
  1179. }
  1180. /**
  1181. * Class to manage intervention lines
  1182. */
  1183. class FichinterLigne extends CommonObjectLine
  1184. {
  1185. /**
  1186. * @var DoliDB Database handler.
  1187. */
  1188. public $db;
  1189. /**
  1190. * @var string Error code (or message)
  1191. */
  1192. public $error = '';
  1193. // From llx_fichinterdet
  1194. /**
  1195. * @var int ID
  1196. */
  1197. public $fk_fichinter;
  1198. public $desc; // Description ligne
  1199. public $datei; // Date intervention
  1200. public $duration; // Duree de l'intervention
  1201. public $rang = 0;
  1202. /**
  1203. * @var string ID to identify managed object
  1204. */
  1205. public $element = 'fichinterdet';
  1206. /**
  1207. * @var string Name of table without prefix where object is stored
  1208. */
  1209. public $table_element = 'fichinterdet';
  1210. /**
  1211. * @var string Field with ID of parent key if this field has a parent
  1212. */
  1213. public $fk_element = 'fk_fichinter';
  1214. /**
  1215. * Constructor
  1216. *
  1217. * @param DoliDB $db Database handler
  1218. */
  1219. public function __construct($db)
  1220. {
  1221. $this->db = $db;
  1222. }
  1223. /**
  1224. * Retrieve the line of intervention
  1225. *
  1226. * @param int $rowid Line id
  1227. * @return int <0 if KO, >0 if OK
  1228. */
  1229. public function fetch($rowid)
  1230. {
  1231. $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,';
  1232. $sql .= ' ft.date as datei';
  1233. $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
  1234. $sql .= ' WHERE ft.rowid = '.((int) $rowid);
  1235. dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
  1236. $result = $this->db->query($sql);
  1237. if ($result) {
  1238. $objp = $this->db->fetch_object($result);
  1239. $this->rowid = $objp->rowid;
  1240. $this->id = $objp->rowid;
  1241. $this->fk_fichinter = $objp->fk_fichinter;
  1242. $this->datei = $this->db->jdate($objp->datei);
  1243. $this->desc = $objp->description;
  1244. $this->duration = $objp->duree;
  1245. $this->rang = $objp->rang;
  1246. $this->db->free($result);
  1247. return 1;
  1248. } else {
  1249. $this->error = $this->db->error().' sql='.$sql;
  1250. return -1;
  1251. }
  1252. }
  1253. /**
  1254. * Insert the line into database
  1255. *
  1256. * @param User $user Objet user that make creation
  1257. * @param int $notrigger Disable all triggers
  1258. * @return int <0 if ko, >0 if ok
  1259. */
  1260. public function insert($user, $notrigger = 0)
  1261. {
  1262. global $langs, $conf;
  1263. $error = 0;
  1264. dol_syslog("FichinterLigne::insert rang=".$this->rang);
  1265. $this->db->begin();
  1266. $rangToUse = $this->rang;
  1267. if ($rangToUse == -1) {
  1268. // Recupere rang max de la ligne d'intervention dans $rangmax
  1269. $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
  1270. $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
  1271. $resql = $this->db->query($sql);
  1272. if ($resql) {
  1273. $obj = $this->db->fetch_object($resql);
  1274. $rangToUse = $obj->max + 1;
  1275. } else {
  1276. dol_print_error($this->db);
  1277. $this->db->rollback();
  1278. return -1;
  1279. }
  1280. }
  1281. // Insertion dans base de la ligne
  1282. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
  1283. $sql .= ' (fk_fichinter, description, date, duree, rang)';
  1284. $sql .= " VALUES (".((int) $this->fk_fichinter).",";
  1285. $sql .= " '".$this->db->escape($this->desc)."',";
  1286. $sql .= " '".$this->db->idate($this->datei)."',";
  1287. $sql .= " ".((int) $this->duration).",";
  1288. $sql .= ' '.((int) $rangToUse);
  1289. $sql .= ')';
  1290. dol_syslog("FichinterLigne::insert", LOG_DEBUG);
  1291. $resql = $this->db->query($sql);
  1292. if ($resql) {
  1293. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
  1294. $this->rowid = $this->id;
  1295. if (!$error) {
  1296. $result = $this->insertExtraFields();
  1297. if ($result < 0) {
  1298. $error++;
  1299. }
  1300. }
  1301. $result = $this->update_total();
  1302. if ($result > 0) {
  1303. $this->rang = $rangToUse;
  1304. if (!$notrigger) {
  1305. // Call trigger
  1306. $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
  1307. if ($result < 0) {
  1308. $error++;
  1309. }
  1310. // End call triggers
  1311. }
  1312. }
  1313. if (!$error) {
  1314. $this->db->commit();
  1315. return $result;
  1316. } else {
  1317. $this->db->rollback();
  1318. return -1;
  1319. }
  1320. } else {
  1321. $this->error = $this->db->error()." sql=".$sql;
  1322. $this->db->rollback();
  1323. return -1;
  1324. }
  1325. }
  1326. /**
  1327. * Update intervention into database
  1328. *
  1329. * @param User $user Objet user that make creation
  1330. * @param int $notrigger Disable all triggers
  1331. * @return int <0 if ko, >0 if ok
  1332. */
  1333. public function update($user, $notrigger = 0)
  1334. {
  1335. global $langs, $conf;
  1336. $error = 0;
  1337. $this->db->begin();
  1338. // Mise a jour ligne en base
  1339. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
  1340. $sql .= " description='".$this->db->escape($this->desc)."'";
  1341. $sql .= ",date='".$this->db->idate($this->datei)."'";
  1342. $sql .= ",duree=".$this->duration;
  1343. $sql .= ",rang='".$this->db->escape($this->rang)."'";
  1344. $sql .= " WHERE rowid = ".((int) $this->id);
  1345. dol_syslog("FichinterLigne::update", LOG_DEBUG);
  1346. $resql = $this->db->query($sql);
  1347. if ($resql) {
  1348. if (!$error) {
  1349. $result = $this->insertExtraFields();
  1350. if ($result < 0) {
  1351. $error++;
  1352. }
  1353. }
  1354. $result = $this->update_total();
  1355. if ($result > 0) {
  1356. if (!$notrigger) {
  1357. // Call trigger
  1358. $result = $this->call_trigger('LINEFICHINTER_UPDATE', $user);
  1359. if ($result < 0) {
  1360. $error++;
  1361. }
  1362. // End call triggers
  1363. }
  1364. }
  1365. if (!$error) {
  1366. $this->db->commit();
  1367. return $result;
  1368. } else {
  1369. $this->error = $this->db->lasterror();
  1370. $this->db->rollback();
  1371. return -1;
  1372. }
  1373. } else {
  1374. $this->error = $this->db->lasterror();
  1375. $this->db->rollback();
  1376. return -1;
  1377. }
  1378. }
  1379. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1380. /**
  1381. * Update total duration into llx_fichinter
  1382. *
  1383. * @return int <0 si ko, >0 si ok
  1384. */
  1385. public function update_total()
  1386. {
  1387. // phpcs:enable
  1388. global $conf;
  1389. $this->db->begin();
  1390. $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
  1391. $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
  1392. $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
  1393. dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
  1394. $resql = $this->db->query($sql);
  1395. if ($resql) {
  1396. $obj = $this->db->fetch_object($resql);
  1397. $total_duration = 0;
  1398. if (!empty($obj->total_duration)) {
  1399. $total_duration = $obj->total_duration;
  1400. }
  1401. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  1402. $sql .= " SET duree = ".((int) $total_duration);
  1403. $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->idate($obj->dateo)."'" : "null");
  1404. $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->idate($obj->datee)."'" : "null");
  1405. $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
  1406. dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
  1407. $resql = $this->db->query($sql);
  1408. if ($resql) {
  1409. $this->db->commit();
  1410. return 1;
  1411. } else {
  1412. $this->error = $this->db->error();
  1413. $this->db->rollback();
  1414. return -2;
  1415. }
  1416. } else {
  1417. $this->error = $this->db->error();
  1418. $this->db->rollback();
  1419. return -1;
  1420. }
  1421. }
  1422. /**
  1423. * Delete a intervention line
  1424. *
  1425. * @param User $user Objet user that make creation
  1426. * @param int $notrigger Disable all triggers
  1427. * @return int >0 if ok, <0 if ko
  1428. */
  1429. public function deleteline($user, $notrigger = 0)
  1430. {
  1431. global $langs, $conf;
  1432. $error = 0;
  1433. if ($this->statut == 0) {
  1434. dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
  1435. $this->db->begin();
  1436. $result = $this->deleteExtraFields();
  1437. if ($result < 0) {
  1438. $error++;
  1439. $this->db->rollback();
  1440. return -1;
  1441. }
  1442. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
  1443. $resql = $this->db->query($sql);
  1444. if ($resql) {
  1445. $result = $this->update_total();
  1446. if ($result > 0) {
  1447. if (!$notrigger) {
  1448. // Call trigger
  1449. $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
  1450. if ($result < 0) {
  1451. $error++; $this->db->rollback(); return -1;
  1452. }
  1453. // End call triggers
  1454. }
  1455. $this->db->commit();
  1456. return $result;
  1457. } else {
  1458. $this->db->rollback();
  1459. return -1;
  1460. }
  1461. } else {
  1462. $this->error = $this->db->error()." sql=".$sql;
  1463. $this->db->rollback();
  1464. return -1;
  1465. }
  1466. } else {
  1467. return -2;
  1468. }
  1469. }
  1470. }