fichinter.class.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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'=>'$conf->societe->enabled', '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'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>105, 'showoncombobox'=>2),
  58. 'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>110),
  59. 'note_public' =>array('type'=>'text', '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 array 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. } else {
  439. $this->error = $this->db->lasterror();
  440. return -1;
  441. }
  442. }
  443. /**
  444. * Set status to draft
  445. *
  446. * @param User $user User that set draft
  447. * @return int <0 if KO, >0 if OK
  448. */
  449. public function setDraft($user)
  450. {
  451. global $langs, $conf;
  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. }
  579. /**
  580. * Returns amount based on user thm
  581. *
  582. * @return float Amount
  583. */
  584. public function getAmount()
  585. {
  586. global $db;
  587. $amount = 0;
  588. $this->author = new User($db);
  589. $this->author->fetch($this->user_creation);
  590. $thm = $this->author->thm;
  591. foreach ($this->lines as $line) {
  592. $amount += ($line->duration / 60 / 60 * $thm);
  593. }
  594. return price2num($amount, 'MT');
  595. }
  596. /**
  597. * Create a document onto disk according to template module.
  598. *
  599. * @param string $modele Force model to use ('' to not force)
  600. * @param Translate $outputlangs Object langs to use for output
  601. * @param int $hidedetails Hide details of lines
  602. * @param int $hidedesc Hide description
  603. * @param int $hideref Hide ref
  604. * @param null|array $moreparams Array to provide more information
  605. * @return int 0 if KO, 1 if OK
  606. */
  607. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  608. {
  609. global $conf;
  610. $outputlangs->load("interventions");
  611. if (!dol_strlen($modele)) {
  612. $modele = 'soleil';
  613. if (!empty($this->model_pdf)) {
  614. $modele = $this->model_pdf;
  615. } elseif (!empty($this->modelpdf)) { // deprecated
  616. $modele = $this->modelpdf;
  617. } elseif (!empty($conf->global->FICHEINTER_ADDON_PDF)) {
  618. $modele = $conf->global->FICHEINTER_ADDON_PDF;
  619. }
  620. }
  621. $modelpath = "core/modules/fichinter/doc/";
  622. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  623. }
  624. /**
  625. * Returns the label status
  626. *
  627. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  628. * @return string Label
  629. */
  630. public function getLibStatut($mode = 0)
  631. {
  632. return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
  633. }
  634. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  635. /**
  636. * Returns the label of a status
  637. *
  638. * @param int $status Id status
  639. * @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
  640. * @return string Label
  641. */
  642. public function LibStatut($status, $mode = 0)
  643. {
  644. // phpcs:enable
  645. // Init/load array of translation of status
  646. if (empty($this->statuts) || empty($this->statuts_short) || empty($this->statuts_logo)) {
  647. global $langs;
  648. $langs->load("fichinter");
  649. $this->statuts[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  650. $this->statuts[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  651. $this->statuts[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
  652. $this->statuts[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
  653. $this->statuts_short[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  654. $this->statuts_short[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  655. $this->statuts_short[self::STATUS_BILLED] = $langs->transnoentitiesnoconv('StatusInterInvoiced');
  656. $this->statuts_short[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Done');
  657. $this->statuts_logo[self::STATUS_DRAFT] = 'status0';
  658. $this->statuts_logo[self::STATUS_VALIDATED] = 'status1';
  659. $this->statuts_logo[self::STATUS_BILLED] = 'status6';
  660. $this->statuts_logo[self::STATUS_CLOSED] = 'status6';
  661. }
  662. return dolGetStatus($this->statuts[$status], $this->statuts_short[$status], '', $this->statuts_logo[$status], $mode);
  663. }
  664. /**
  665. * Return clicable name (with picto eventually)
  666. *
  667. * @param int $withpicto 0=_No picto, 1=Includes the picto in the linkn, 2=Picto only
  668. * @param string $option Options
  669. * @param int $notooltip 1=Disable tooltip
  670. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  671. * @return string String with URL
  672. */
  673. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $save_lastsearch_value = -1)
  674. {
  675. global $conf, $langs, $hookmanager;
  676. $result = '';
  677. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Intervention").'</u>';
  678. if (isset($this->status)) {
  679. $label .= ' '.$this->getLibStatut(5);
  680. }
  681. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  682. $url = DOL_URL_ROOT.'/fichinter/card.php?id='.$this->id;
  683. if ($option !== 'nolink') {
  684. // Add param to save lastsearch_values or not
  685. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  686. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  687. $add_save_lastsearch_values = 1;
  688. }
  689. if ($add_save_lastsearch_values) {
  690. $url .= '&save_lastsearch_values=1';
  691. }
  692. }
  693. $linkclose = '';
  694. if (empty($notooltip)) {
  695. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  696. $label = $langs->trans("ShowIntervention");
  697. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  698. }
  699. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  700. $linkclose .= ' class="classfortooltip"';
  701. /*
  702. $hookmanager->initHooks(array('fichinterdao'));
  703. $parameters=array('id'=>$this->id);
  704. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  705. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  706. */
  707. }
  708. $linkstart = '<a href="'.$url.'"';
  709. $linkstart .= $linkclose.'>';
  710. $linkend = '</a>';
  711. $result .= $linkstart;
  712. if ($withpicto) {
  713. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  714. }
  715. if ($withpicto != 2) {
  716. $result .= $this->ref;
  717. }
  718. $result .= $linkend;
  719. global $action;
  720. $hookmanager->initHooks(array('interventiondao'));
  721. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  722. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  723. if ($reshook > 0) {
  724. $result = $hookmanager->resPrint;
  725. } else {
  726. $result .= $hookmanager->resPrint;
  727. }
  728. return $result;
  729. }
  730. /**
  731. * Returns the next non used reference of intervention
  732. * depending on the module numbering assets within FICHEINTER_ADDON
  733. *
  734. * @param Societe $soc Thirdparty object
  735. * @return string Free reference for intervention
  736. */
  737. public function getNextNumRef($soc)
  738. {
  739. global $conf, $db, $langs;
  740. $langs->load("interventions");
  741. if (!empty($conf->global->FICHEINTER_ADDON)) {
  742. $mybool = false;
  743. $file = "mod_".$conf->global->FICHEINTER_ADDON.".php";
  744. $classname = "mod_".$conf->global->FICHEINTER_ADDON;
  745. // Include file with class
  746. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  747. foreach ($dirmodels as $reldir) {
  748. $dir = dol_buildpath($reldir."core/modules/fichinter/");
  749. // Load file with numbering class (if found)
  750. $mybool |= @include_once $dir.$file;
  751. }
  752. if ($mybool === false) {
  753. dol_print_error('', "Failed to include file ".$file);
  754. return '';
  755. }
  756. $obj = new $classname();
  757. $numref = "";
  758. $numref = $obj->getNextValue($soc, $this);
  759. if ($numref != "") {
  760. return $numref;
  761. } else {
  762. dol_print_error($db, "Fichinter::getNextNumRef ".$obj->error);
  763. return "";
  764. }
  765. } else {
  766. $langs->load("errors");
  767. print $langs->trans("Error")." ".$langs->trans("Error_FICHEINTER_ADDON_NotDefined");
  768. return "";
  769. }
  770. }
  771. /**
  772. * Load information on object
  773. *
  774. * @param int $id Id of object
  775. * @return void
  776. */
  777. public function info($id)
  778. {
  779. global $conf;
  780. $sql = "SELECT f.rowid,";
  781. $sql .= " f.datec,";
  782. $sql .= " f.tms as date_modification,";
  783. $sql .= " f.date_valid as datev,";
  784. $sql .= " f.fk_user_author,";
  785. $sql .= " f.fk_user_modif as fk_user_modification,";
  786. $sql .= " f.fk_user_valid";
  787. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as f";
  788. $sql .= " WHERE f.rowid = ".((int) $id);
  789. $resql = $this->db->query($sql);
  790. if ($resql) {
  791. if ($this->db->num_rows($resql)) {
  792. $obj = $this->db->fetch_object($resql);
  793. $this->id = $obj->rowid;
  794. $this->date_creation = $this->db->jdate($obj->datec);
  795. $this->date_modification = $this->db->jdate($obj->date_modification);
  796. $this->date_validation = $this->db->jdate($obj->datev);
  797. $cuser = new User($this->db);
  798. $cuser->fetch($obj->fk_user_author);
  799. $this->user_creation = $cuser;
  800. if ($obj->fk_user_valid) {
  801. $vuser = new User($this->db);
  802. $vuser->fetch($obj->fk_user_valid);
  803. $this->user_validation = $vuser;
  804. }
  805. if ($obj->fk_user_modification) {
  806. $muser = new User($this->db);
  807. $muser->fetch($obj->fk_user_modification);
  808. $this->user_modification = $muser;
  809. }
  810. }
  811. $this->db->free($resql);
  812. } else {
  813. dol_print_error($this->db);
  814. }
  815. }
  816. /**
  817. * Delete intervetnion
  818. *
  819. * @param User $user Object user who delete
  820. * @param int $notrigger Disable trigger
  821. * @return int <0 if KO, >0 if OK
  822. */
  823. public function delete($user, $notrigger = 0)
  824. {
  825. global $conf, $langs;
  826. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  827. $error = 0;
  828. $this->db->begin();
  829. if (!$error && !$notrigger) {
  830. // Call trigger
  831. $result = $this->call_trigger('FICHINTER_DELETE', $user);
  832. if ($result < 0) {
  833. $error++; $this->db->rollback(); return -1;
  834. }
  835. // End call triggers
  836. }
  837. // Delete linked object
  838. if (!$error) {
  839. $res = $this->deleteObjectLinked();
  840. if ($res < 0) {
  841. $error++;
  842. }
  843. }
  844. // Delete linked contacts
  845. if (!$error) {
  846. $res = $this->delete_linked_contact();
  847. if ($res < 0) {
  848. $this->error = 'ErrorFailToDeleteLinkedContact';
  849. $error++;
  850. }
  851. }
  852. if (!$error) {
  853. $main = MAIN_DB_PREFIX.'fichinterdet';
  854. $ef = $main."_extrafields";
  855. $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_fichinter = ".((int) $this->id).")";
  856. $resql = $this->db->query($sql);
  857. if (!$resql) {
  858. $error++;
  859. }
  860. }
  861. if (!$error) {
  862. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet";
  863. $sql .= " WHERE fk_fichinter = ".((int) $this->id);
  864. $resql = $this->db->query($sql);
  865. if (!$resql) {
  866. $error++;
  867. }
  868. }
  869. if (!$error) {
  870. // Remove extrafields
  871. $res = $this->deleteExtraFields();
  872. if ($res < 0) {
  873. $error++;
  874. }
  875. }
  876. if (!$error) {
  877. // Delete object
  878. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter";
  879. $sql .= " WHERE rowid = ".((int) $this->id);
  880. dol_syslog("Fichinter::delete", LOG_DEBUG);
  881. $resql = $this->db->query($sql);
  882. if (!$resql) {
  883. $error++;
  884. }
  885. }
  886. if (!$error) {
  887. // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
  888. $this->deleteEcmFiles();
  889. // Remove directory with files
  890. $fichinterref = dol_sanitizeFileName($this->ref);
  891. if ($conf->ficheinter->dir_output) {
  892. $dir = $conf->ficheinter->dir_output."/".$fichinterref;
  893. $file = $conf->ficheinter->dir_output."/".$fichinterref."/".$fichinterref.".pdf";
  894. if (file_exists($file)) {
  895. dol_delete_preview($this);
  896. if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
  897. $langs->load("errors");
  898. $this->error = $langs->trans("ErrorFailToDeleteFile", $file);
  899. return 0;
  900. }
  901. }
  902. if (file_exists($dir)) {
  903. if (!dol_delete_dir_recursive($dir)) {
  904. $langs->load("errors");
  905. $this->error = $langs->trans("ErrorFailToDeleteDir", $dir);
  906. return 0;
  907. }
  908. }
  909. }
  910. }
  911. if (!$error) {
  912. $this->db->commit();
  913. return 1;
  914. } else {
  915. $this->db->rollback();
  916. return -1;
  917. }
  918. }
  919. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  920. /**
  921. * Defines a delivery date of intervention
  922. *
  923. * @param User $user Object user who define
  924. * @param integer $date_delivery date of delivery
  925. * @return int <0 if ko, >0 if ok
  926. */
  927. public function set_date_delivery($user, $date_delivery)
  928. {
  929. // phpcs:enable
  930. global $conf;
  931. if ($user->rights->ficheinter->creer) {
  932. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  933. $sql .= " SET datei = '".$this->db->idate($date_delivery)."'";
  934. $sql .= " WHERE rowid = ".((int) $this->id);
  935. $sql .= " AND fk_statut = 0";
  936. if ($this->db->query($sql)) {
  937. $this->date_delivery = $date_delivery;
  938. return 1;
  939. } else {
  940. $this->error = $this->db->error();
  941. dol_syslog("Fichinter::set_date_delivery Erreur SQL");
  942. return -1;
  943. }
  944. }
  945. }
  946. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  947. /**
  948. * Define the label of the intervention
  949. *
  950. * @param User $user Object user who modify
  951. * @param string $description description
  952. * @return int <0 if KO, >0 if OK
  953. */
  954. public function set_description($user, $description)
  955. {
  956. // phpcs:enable
  957. global $conf;
  958. if ($user->rights->ficheinter->creer) {
  959. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  960. $sql .= " SET description = '".$this->db->escape($description)."',";
  961. $sql .= " fk_user_modif = ".$user->id;
  962. $sql .= " WHERE rowid = ".((int) $this->id);
  963. if ($this->db->query($sql)) {
  964. $this->description = $description;
  965. return 1;
  966. } else {
  967. $this->error = $this->db->error();
  968. dol_syslog("Fichinter::set_description Erreur SQL");
  969. return -1;
  970. }
  971. }
  972. }
  973. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  974. /**
  975. * Link intervention to a contract
  976. *
  977. * @param User $user Object user who modify
  978. * @param int $contractid Description
  979. * @return int <0 if ko, >0 if ok
  980. */
  981. public function set_contrat($user, $contractid)
  982. {
  983. // phpcs:enable
  984. global $conf;
  985. if ($user->rights->ficheinter->creer) {
  986. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter ";
  987. $sql .= " SET fk_contrat = ".((int) $contractid);
  988. $sql .= " WHERE rowid = ".((int) $this->id);
  989. if ($this->db->query($sql)) {
  990. $this->fk_contrat = $contractid;
  991. return 1;
  992. } else {
  993. $this->error = $this->db->error();
  994. return -1;
  995. }
  996. }
  997. return -2;
  998. }
  999. /**
  1000. * Load an object from its id and create a new one in database
  1001. *
  1002. * @param User $user User making the clone
  1003. * @param int $socid Id of thirdparty
  1004. * @return int New id of clone
  1005. */
  1006. public function createFromClone(User $user, $socid = 0)
  1007. {
  1008. global $hookmanager;
  1009. $error = 0;
  1010. $this->db->begin();
  1011. // get extrafields so they will be clone
  1012. foreach ($this->lines as $line) {
  1013. $line->fetch_optionals();
  1014. }
  1015. // Load source object
  1016. $objFrom = clone $this;
  1017. // Change socid if needed
  1018. if (!empty($socid) && $socid != $this->socid) {
  1019. $objsoc = new Societe($this->db);
  1020. if ($objsoc->fetch($socid) > 0) {
  1021. $this->socid = $objsoc->id;
  1022. //$this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
  1023. //$this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
  1024. $this->fk_project = '';
  1025. $this->fk_delivery_address = '';
  1026. }
  1027. // TODO Change product price if multi-prices
  1028. }
  1029. $this->id = 0;
  1030. $this->ref = '';
  1031. $this->statut = 0;
  1032. // Clear fields
  1033. $this->user_author_id = $user->id;
  1034. $this->user_valid = 0;
  1035. $this->date_creation = '';
  1036. $this->date_validation = '';
  1037. $this->ref_client = '';
  1038. // Create clone
  1039. $this->context['createfromclone'] = 'createfromclone';
  1040. $result = $this->create($user);
  1041. if ($result < 0) {
  1042. $error++;
  1043. }
  1044. if (!$error) {
  1045. // Add lines because it is not included into create function
  1046. foreach ($this->lines as $line) {
  1047. $this->addline($user, $this->id, $line->desc, $line->datei, $line->duration, $line->array_options);
  1048. }
  1049. // Hook of thirdparty module
  1050. if (is_object($hookmanager)) {
  1051. $parameters = array('objFrom'=>$objFrom);
  1052. $action = '';
  1053. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1054. if ($reshook < 0) {
  1055. $this->errors += $hookmanager->errors;
  1056. $this->error = $hookmanager->error;
  1057. $error++;
  1058. }
  1059. }
  1060. }
  1061. unset($this->context['createfromclone']);
  1062. // End
  1063. if (!$error) {
  1064. $this->db->commit();
  1065. return $this->id;
  1066. } else {
  1067. $this->db->rollback();
  1068. return -1;
  1069. }
  1070. }
  1071. /**
  1072. * Adding a line of intervention into data base
  1073. *
  1074. * @param user $user User that do the action
  1075. * @param int $fichinterid Id of intervention
  1076. * @param string $desc Line description
  1077. * @param integer $date_intervention Intervention date
  1078. * @param int $duration Intervention duration
  1079. * @param array $array_options Array option
  1080. * @return int >0 if ok, <0 if ko
  1081. */
  1082. public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
  1083. {
  1084. dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
  1085. if ($this->statut == 0) {
  1086. $this->db->begin();
  1087. // Insertion ligne
  1088. $line = new FichinterLigne($this->db);
  1089. $line->fk_fichinter = $fichinterid;
  1090. $line->desc = $desc;
  1091. $line->date = $date_intervention;
  1092. $line->datei = $date_intervention; // For backward compatibility
  1093. $line->duration = $duration;
  1094. if (is_array($array_options) && count($array_options) > 0) {
  1095. $line->array_options = $array_options;
  1096. }
  1097. $result = $line->insert($user);
  1098. if ($result >= 0) {
  1099. $this->db->commit();
  1100. return 1;
  1101. } else {
  1102. $this->error = $this->db->error();
  1103. $this->db->rollback();
  1104. return -1;
  1105. }
  1106. }
  1107. }
  1108. /**
  1109. * Initialise an instance with random values.
  1110. * Used to build previews or test instances.
  1111. * id must be 0 if object instance is a specimen.
  1112. *
  1113. * @return void
  1114. */
  1115. public function initAsSpecimen()
  1116. {
  1117. global $user, $langs, $conf;
  1118. $now = dol_now();
  1119. // Initialise parametres
  1120. $this->id = 0;
  1121. $this->ref = 'SPECIMEN';
  1122. $this->ref_client = 'SPECIMEN CLIENT';
  1123. $this->specimen = 1;
  1124. $this->socid = 1;
  1125. $this->datec = $now;
  1126. $this->note_private = 'Private note';
  1127. $this->note_public = 'SPECIMEN';
  1128. $this->duration = 0;
  1129. $nbp = 25;
  1130. $xnbp = 0;
  1131. while ($xnbp < $nbp) {
  1132. $line = new FichinterLigne($this->db);
  1133. $line->desc = $langs->trans("Description")." ".$xnbp;
  1134. $line->date = ($now - 3600 * (1 + $xnbp));
  1135. $line->datei = ($now - 3600 * (1 + $xnbp)); // For backward compatibility
  1136. $line->duration = 600;
  1137. $line->fk_fichinter = 0;
  1138. $this->lines[$xnbp] = $line;
  1139. $xnbp++;
  1140. $this->duration += $line->duration;
  1141. }
  1142. }
  1143. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1144. /**
  1145. * Load array lines ->lines
  1146. *
  1147. * @return int <0 if KO, >0 if OK
  1148. */
  1149. public function fetch_lines()
  1150. {
  1151. // phpcs:enable
  1152. $this->lines = array();
  1153. $sql = "SELECT rowid, fk_fichinter, description, duree, date, rang";
  1154. $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
  1155. $sql .= " WHERE fk_fichinter = ".((int) $this->id);
  1156. $sql .= " ORDER BY rang ASC, date ASC";
  1157. dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
  1158. $resql = $this->db->query($sql);
  1159. if ($resql) {
  1160. $num = $this->db->num_rows($resql);
  1161. $i = 0;
  1162. while ($i < $num) {
  1163. $objp = $this->db->fetch_object($resql);
  1164. $line = new FichinterLigne($this->db);
  1165. $line->id = $objp->rowid;
  1166. $line->fk_fichinter = $objp->fk_fichinter;
  1167. $line->desc = $objp->description;
  1168. $line->duration = $objp->duree;
  1169. //For invoicing we calculing hours
  1170. $line->qty = round($objp->duree / 3600, 2);
  1171. $line->date = $this->db->jdate($objp->date);
  1172. $line->datei = $this->db->jdate($objp->date); // For backward compatibility
  1173. $line->rang = $objp->rang;
  1174. $line->product_type = 1;
  1175. $line->fetch_optionals();
  1176. $this->lines[$i] = $line;
  1177. $i++;
  1178. }
  1179. $this->db->free($resql);
  1180. return 1;
  1181. } else {
  1182. $this->error = $this->db->error();
  1183. return -1;
  1184. }
  1185. }
  1186. /**
  1187. * Function used to replace a thirdparty id with another one.
  1188. *
  1189. * @param DoliDB $db Database handler
  1190. * @param int $origin_id Old thirdparty id
  1191. * @param int $dest_id New thirdparty id
  1192. * @return bool
  1193. */
  1194. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  1195. {
  1196. $tables = array(
  1197. 'fichinter'
  1198. );
  1199. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1200. }
  1201. /**
  1202. * Set customer reference number
  1203. *
  1204. * @param User $user Object user that modify
  1205. * @param string $ref_client Customer reference
  1206. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1207. * @return int <0 if ko, >0 if ok
  1208. */
  1209. public function setRefClient($user, $ref_client, $notrigger = 0)
  1210. {
  1211. // phpcs:enable
  1212. if (!empty($user->rights->ficheinter->creer)) {
  1213. $error = 0;
  1214. $this->db->begin();
  1215. $this->oldcopy = dol_clone($this);
  1216. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
  1217. $sql .= " WHERE rowid = ".((int) $this->id);
  1218. dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
  1219. $resql = $this->db->query($sql);
  1220. if (!$resql) {
  1221. $this->errors[] = $this->db->error();
  1222. $error++;
  1223. }
  1224. if (!$error) {
  1225. $this->ref_client = $ref_client;
  1226. }
  1227. if (!$notrigger && empty($error)) {
  1228. // Call trigger
  1229. $result = $this->call_trigger('FICHINTER_MODIFY', $user);
  1230. if ($result < 0) {
  1231. $error++;
  1232. }
  1233. // End call triggers
  1234. }
  1235. if (!$error) {
  1236. $this->db->commit();
  1237. return 1;
  1238. } else {
  1239. foreach ($this->errors as $errmsg) {
  1240. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  1241. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1242. }
  1243. $this->db->rollback();
  1244. return -1 * $error;
  1245. }
  1246. } else {
  1247. return -1;
  1248. }
  1249. }
  1250. }
  1251. /**
  1252. * Class to manage intervention lines
  1253. */
  1254. class FichinterLigne extends CommonObjectLine
  1255. {
  1256. /**
  1257. * @var DoliDB Database handler.
  1258. */
  1259. public $db;
  1260. /**
  1261. * @var string Error code (or message)
  1262. */
  1263. public $error = '';
  1264. // From llx_fichinterdet
  1265. /**
  1266. * @var int ID
  1267. */
  1268. public $fk_fichinter;
  1269. public $desc; // Description ligne
  1270. /**
  1271. * @var int Date of intervention
  1272. */
  1273. public $date; // Date intervention
  1274. /**
  1275. * @var int Date of intervention
  1276. * @deprecated
  1277. */
  1278. public $datei; // Date intervention
  1279. public $duration; // Duration of intervention
  1280. public $rang = 0;
  1281. public $tva_tx;
  1282. public $subprice;
  1283. /**
  1284. * @var string ID to identify managed object
  1285. */
  1286. public $element = 'fichinterdet';
  1287. /**
  1288. * @var string Name of table without prefix where object is stored
  1289. */
  1290. public $table_element = 'fichinterdet';
  1291. /**
  1292. * @var string Field with ID of parent key if this field has a parent
  1293. */
  1294. public $fk_element = 'fk_fichinter';
  1295. /**
  1296. * Constructor
  1297. *
  1298. * @param DoliDB $db Database handler
  1299. */
  1300. public function __construct($db)
  1301. {
  1302. $this->db = $db;
  1303. }
  1304. /**
  1305. * Retrieve the line of intervention
  1306. *
  1307. * @param int $rowid Line id
  1308. * @return int <0 if KO, >0 if OK
  1309. */
  1310. public function fetch($rowid)
  1311. {
  1312. $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang, ft.date';
  1313. $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
  1314. $sql .= ' WHERE ft.rowid = '.((int) $rowid);
  1315. dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
  1316. $result = $this->db->query($sql);
  1317. if ($result) {
  1318. $objp = $this->db->fetch_object($result);
  1319. $this->rowid = $objp->rowid;
  1320. $this->id = $objp->rowid;
  1321. $this->fk_fichinter = $objp->fk_fichinter;
  1322. $this->date = $this->db->jdate($objp->date);
  1323. $this->datei = $this->db->jdate($objp->date); // For backward compatibility
  1324. $this->desc = $objp->description;
  1325. $this->duration = $objp->duree;
  1326. $this->rang = $objp->rang;
  1327. $this->db->free($result);
  1328. return 1;
  1329. } else {
  1330. $this->error = $this->db->error().' sql='.$sql;
  1331. return -1;
  1332. }
  1333. }
  1334. /**
  1335. * Insert the line into database
  1336. *
  1337. * @param User $user Objet user that make creation
  1338. * @param int $notrigger Disable all triggers
  1339. * @return int <0 if ko, >0 if ok
  1340. */
  1341. public function insert($user, $notrigger = 0)
  1342. {
  1343. global $langs, $conf;
  1344. $error = 0;
  1345. dol_syslog("FichinterLigne::insert rang=".$this->rang);
  1346. if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
  1347. $this->date = $this->datei;
  1348. }
  1349. $this->db->begin();
  1350. $rangToUse = $this->rang;
  1351. if ($rangToUse == -1) {
  1352. // Recupere rang max de la ligne d'intervention dans $rangmax
  1353. $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
  1354. $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
  1355. $resql = $this->db->query($sql);
  1356. if ($resql) {
  1357. $obj = $this->db->fetch_object($resql);
  1358. $rangToUse = $obj->max + 1;
  1359. } else {
  1360. dol_print_error($this->db);
  1361. $this->db->rollback();
  1362. return -1;
  1363. }
  1364. }
  1365. // Insertion dans base de la ligne
  1366. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
  1367. $sql .= ' (fk_fichinter, description, date, duree, rang)';
  1368. $sql .= " VALUES (".((int) $this->fk_fichinter).",";
  1369. $sql .= " '".$this->db->escape($this->desc)."',";
  1370. $sql .= " '".$this->db->idate($this->date)."',";
  1371. $sql .= " ".((int) $this->duration).",";
  1372. $sql .= ' '.((int) $rangToUse);
  1373. $sql .= ')';
  1374. dol_syslog("FichinterLigne::insert", LOG_DEBUG);
  1375. $resql = $this->db->query($sql);
  1376. if ($resql) {
  1377. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
  1378. $this->rowid = $this->id;
  1379. if (!$error) {
  1380. $result = $this->insertExtraFields();
  1381. if ($result < 0) {
  1382. $error++;
  1383. }
  1384. }
  1385. $result = $this->update_total();
  1386. if ($result > 0) {
  1387. $this->rang = $rangToUse;
  1388. if (!$notrigger) {
  1389. // Call trigger
  1390. $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
  1391. if ($result < 0) {
  1392. $error++;
  1393. }
  1394. // End call triggers
  1395. }
  1396. }
  1397. if (!$error) {
  1398. $this->db->commit();
  1399. return $result;
  1400. } else {
  1401. $this->db->rollback();
  1402. return -1;
  1403. }
  1404. } else {
  1405. $this->error = $this->db->error()." sql=".$sql;
  1406. $this->db->rollback();
  1407. return -1;
  1408. }
  1409. }
  1410. /**
  1411. * Update intervention into database
  1412. *
  1413. * @param User $user Objet user that make creation
  1414. * @param int $notrigger Disable all triggers
  1415. * @return int <0 if ko, >0 if ok
  1416. */
  1417. public function update($user, $notrigger = 0)
  1418. {
  1419. global $langs, $conf;
  1420. $error = 0;
  1421. if (empty($this->date) && !empty($this->datei)) { // For backward compatibility
  1422. $this->date = $this->datei;
  1423. }
  1424. $this->db->begin();
  1425. // Mise a jour ligne en base
  1426. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
  1427. $sql .= " description = '".$this->db->escape($this->desc)."',";
  1428. $sql .= " date = '".$this->db->idate($this->date)."',";
  1429. $sql .= " duree = ".((int) $this->duration).",";
  1430. $sql .= " rang = ".((int) $this->rang);
  1431. $sql .= " WHERE rowid = ".((int) $this->id);
  1432. dol_syslog("FichinterLigne::update", LOG_DEBUG);
  1433. $resql = $this->db->query($sql);
  1434. if ($resql) {
  1435. if (!$error) {
  1436. $result = $this->insertExtraFields();
  1437. if ($result < 0) {
  1438. $error++;
  1439. }
  1440. }
  1441. $result = $this->update_total();
  1442. if ($result > 0) {
  1443. if (!$notrigger) {
  1444. // Call trigger
  1445. $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
  1446. if ($result < 0) {
  1447. $error++;
  1448. }
  1449. // End call triggers
  1450. }
  1451. }
  1452. if (!$error) {
  1453. $this->db->commit();
  1454. return $result;
  1455. } else {
  1456. $this->error = $this->db->lasterror();
  1457. $this->db->rollback();
  1458. return -1;
  1459. }
  1460. } else {
  1461. $this->error = $this->db->lasterror();
  1462. $this->db->rollback();
  1463. return -1;
  1464. }
  1465. }
  1466. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1467. /**
  1468. * Update total duration into llx_fichinter
  1469. *
  1470. * @return int <0 si ko, >0 si ok
  1471. */
  1472. public function update_total()
  1473. {
  1474. // phpcs:enable
  1475. global $conf;
  1476. $this->db->begin();
  1477. $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
  1478. $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
  1479. $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
  1480. dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
  1481. $resql = $this->db->query($sql);
  1482. if ($resql) {
  1483. $obj = $this->db->fetch_object($resql);
  1484. $total_duration = 0;
  1485. if (!empty($obj->total_duration)) {
  1486. $total_duration = $obj->total_duration;
  1487. }
  1488. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  1489. $sql .= " SET duree = ".((int) $total_duration);
  1490. $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->escape($obj->dateo)."'" : "null");
  1491. $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->escape($obj->datee)."'" : "null");
  1492. $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
  1493. dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
  1494. $resql = $this->db->query($sql);
  1495. if ($resql) {
  1496. $this->db->commit();
  1497. return 1;
  1498. } else {
  1499. $this->error = $this->db->error();
  1500. $this->db->rollback();
  1501. return -2;
  1502. }
  1503. } else {
  1504. $this->error = $this->db->error();
  1505. $this->db->rollback();
  1506. return -1;
  1507. }
  1508. }
  1509. /**
  1510. * Delete a intervention line
  1511. *
  1512. * @param User $user Objet user that make creation
  1513. * @param int $notrigger Disable all triggers
  1514. * @return int >0 if ok, <0 if ko
  1515. */
  1516. public function deleteline($user, $notrigger = 0)
  1517. {
  1518. global $langs, $conf;
  1519. $error = 0;
  1520. if ($this->statut == 0) {
  1521. dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
  1522. $this->db->begin();
  1523. $result = $this->deleteExtraFields();
  1524. if ($result < 0) {
  1525. $error++;
  1526. $this->db->rollback();
  1527. return -1;
  1528. }
  1529. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
  1530. $resql = $this->db->query($sql);
  1531. if ($resql) {
  1532. $result = $this->update_total();
  1533. if ($result > 0) {
  1534. if (!$notrigger) {
  1535. // Call trigger
  1536. $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
  1537. if ($result < 0) {
  1538. $error++; $this->db->rollback(); return -1;
  1539. }
  1540. // End call triggers
  1541. }
  1542. $this->db->commit();
  1543. return $result;
  1544. } else {
  1545. $this->db->rollback();
  1546. return -1;
  1547. }
  1548. } else {
  1549. $this->error = $this->db->error()." sql=".$sql;
  1550. $this->db->rollback();
  1551. return -1;
  1552. }
  1553. } else {
  1554. return -2;
  1555. }
  1556. }
  1557. }