fichinter.class.php 52 KB

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