fichinter.class.php 49 KB

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