fichinter.class.php 47 KB

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