fichinter.class.php 47 KB

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