fichinter.class.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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 = 'ErrorFicheinterCompanyDoesNotExist';
  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) $conf->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 = ".$conf->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. $error++;
  1041. }
  1042. }
  1043. }
  1044. unset($this->context['createfromclone']);
  1045. // End
  1046. if (!$error) {
  1047. $this->db->commit();
  1048. return $this->id;
  1049. } else {
  1050. $this->db->rollback();
  1051. return -1;
  1052. }
  1053. }
  1054. /**
  1055. * Adding a line of intervention into data base
  1056. *
  1057. * @param user $user User that do the action
  1058. * @param int $fichinterid Id of intervention
  1059. * @param string $desc Line description
  1060. * @param integer $date_intervention Intervention date
  1061. * @param int $duration Intervention duration
  1062. * @param array $array_options Array option
  1063. * @return int >0 if ok, <0 if ko
  1064. */
  1065. public function addline($user, $fichinterid, $desc, $date_intervention, $duration, $array_options = '')
  1066. {
  1067. dol_syslog(get_class($this)."::addline $fichinterid, $desc, $date_intervention, $duration");
  1068. if ($this->statut == 0) {
  1069. $this->db->begin();
  1070. // Insertion ligne
  1071. $line = new FichinterLigne($this->db);
  1072. $line->fk_fichinter = $fichinterid;
  1073. $line->desc = $desc;
  1074. $line->datei = $date_intervention;
  1075. $line->duration = $duration;
  1076. if (is_array($array_options) && count($array_options) > 0) {
  1077. $line->array_options = $array_options;
  1078. }
  1079. $result = $line->insert($user);
  1080. if ($result >= 0) {
  1081. $this->db->commit();
  1082. return 1;
  1083. } else {
  1084. $this->error = $this->db->error();
  1085. $this->db->rollback();
  1086. return -1;
  1087. }
  1088. }
  1089. }
  1090. /**
  1091. * Initialise an instance with random values.
  1092. * Used to build previews or test instances.
  1093. * id must be 0 if object instance is a specimen.
  1094. *
  1095. * @return void
  1096. */
  1097. public function initAsSpecimen()
  1098. {
  1099. global $user, $langs, $conf;
  1100. $now = dol_now();
  1101. // Initialise parametres
  1102. $this->id = 0;
  1103. $this->ref = 'SPECIMEN';
  1104. $this->specimen = 1;
  1105. $this->socid = 1;
  1106. $this->datec = $now;
  1107. $this->note_private = 'Private note';
  1108. $this->note_public = 'SPECIMEN';
  1109. $this->duration = 0;
  1110. $nbp = 25;
  1111. $xnbp = 0;
  1112. while ($xnbp < $nbp) {
  1113. $line = new FichinterLigne($this->db);
  1114. $line->desc = $langs->trans("Description")." ".$xnbp;
  1115. $line->datei = ($now - 3600 * (1 + $xnbp));
  1116. $line->duration = 600;
  1117. $line->fk_fichinter = 0;
  1118. $this->lines[$xnbp] = $line;
  1119. $xnbp++;
  1120. $this->duration += $line->duration;
  1121. }
  1122. }
  1123. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1124. /**
  1125. * Load array lines ->lines
  1126. *
  1127. * @return int <0 if KO, >0 if OK
  1128. */
  1129. public function fetch_lines()
  1130. {
  1131. // phpcs:enable
  1132. $this->lines = array();
  1133. $sql = "SELECT rowid, fk_fichinter, description, duree, date, rang";
  1134. $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
  1135. $sql .= " WHERE fk_fichinter = ".((int) $this->id);
  1136. $sql .= " ORDER BY rang ASC, date ASC";
  1137. dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
  1138. $resql = $this->db->query($sql);
  1139. if ($resql) {
  1140. $num = $this->db->num_rows($resql);
  1141. $i = 0;
  1142. while ($i < $num) {
  1143. $objp = $this->db->fetch_object($resql);
  1144. $line = new FichinterLigne($this->db);
  1145. $line->id = $objp->rowid;
  1146. $line->fk_fichinter = $objp->fk_fichinter;
  1147. $line->desc = $objp->description;
  1148. $line->duration = $objp->duree;
  1149. //For invoicing we calculing hours
  1150. $line->qty = round($objp->duree / 3600, 2);
  1151. $line->date = $this->db->jdate($objp->date);
  1152. $line->datei = $this->db->jdate($objp->date);
  1153. $line->rang = $objp->rang;
  1154. $line->product_type = 1;
  1155. $line->fetch_optionals();
  1156. $this->lines[$i] = $line;
  1157. $i++;
  1158. }
  1159. $this->db->free($resql);
  1160. return 1;
  1161. } else {
  1162. $this->error = $this->db->error();
  1163. return -1;
  1164. }
  1165. }
  1166. /**
  1167. * Function used to replace a thirdparty id with another one.
  1168. *
  1169. * @param DoliDB $db Database handler
  1170. * @param int $origin_id Old thirdparty id
  1171. * @param int $dest_id New thirdparty id
  1172. * @return bool
  1173. */
  1174. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  1175. {
  1176. $tables = array(
  1177. 'fichinter'
  1178. );
  1179. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1180. }
  1181. }
  1182. /**
  1183. * Class to manage intervention lines
  1184. */
  1185. class FichinterLigne extends CommonObjectLine
  1186. {
  1187. /**
  1188. * @var DoliDB Database handler.
  1189. */
  1190. public $db;
  1191. /**
  1192. * @var string Error code (or message)
  1193. */
  1194. public $error = '';
  1195. // From llx_fichinterdet
  1196. /**
  1197. * @var int ID
  1198. */
  1199. public $fk_fichinter;
  1200. public $desc; // Description ligne
  1201. public $datei; // Date intervention
  1202. public $duration; // Duree de l'intervention
  1203. public $rang = 0;
  1204. /**
  1205. * @var string ID to identify managed object
  1206. */
  1207. public $element = 'fichinterdet';
  1208. /**
  1209. * @var string Name of table without prefix where object is stored
  1210. */
  1211. public $table_element = 'fichinterdet';
  1212. /**
  1213. * @var string Field with ID of parent key if this field has a parent
  1214. */
  1215. public $fk_element = 'fk_fichinter';
  1216. /**
  1217. * Constructor
  1218. *
  1219. * @param DoliDB $db Database handler
  1220. */
  1221. public function __construct($db)
  1222. {
  1223. $this->db = $db;
  1224. }
  1225. /**
  1226. * Retrieve the line of intervention
  1227. *
  1228. * @param int $rowid Line id
  1229. * @return int <0 if KO, >0 if OK
  1230. */
  1231. public function fetch($rowid)
  1232. {
  1233. $sql = 'SELECT ft.rowid, ft.fk_fichinter, ft.description, ft.duree, ft.rang,';
  1234. $sql .= ' ft.date as datei';
  1235. $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet as ft';
  1236. $sql .= ' WHERE ft.rowid = '.((int) $rowid);
  1237. dol_syslog("FichinterLigne::fetch", LOG_DEBUG);
  1238. $result = $this->db->query($sql);
  1239. if ($result) {
  1240. $objp = $this->db->fetch_object($result);
  1241. $this->rowid = $objp->rowid;
  1242. $this->id = $objp->rowid;
  1243. $this->fk_fichinter = $objp->fk_fichinter;
  1244. $this->datei = $this->db->jdate($objp->datei);
  1245. $this->desc = $objp->description;
  1246. $this->duration = $objp->duree;
  1247. $this->rang = $objp->rang;
  1248. $this->db->free($result);
  1249. return 1;
  1250. } else {
  1251. $this->error = $this->db->error().' sql='.$sql;
  1252. return -1;
  1253. }
  1254. }
  1255. /**
  1256. * Insert the line into database
  1257. *
  1258. * @param User $user Objet user that make creation
  1259. * @param int $notrigger Disable all triggers
  1260. * @return int <0 if ko, >0 if ok
  1261. */
  1262. public function insert($user, $notrigger = 0)
  1263. {
  1264. global $langs, $conf;
  1265. $error = 0;
  1266. dol_syslog("FichinterLigne::insert rang=".$this->rang);
  1267. $this->db->begin();
  1268. $rangToUse = $this->rang;
  1269. if ($rangToUse == -1) {
  1270. // Recupere rang max de la ligne d'intervention dans $rangmax
  1271. $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'fichinterdet';
  1272. $sql .= ' WHERE fk_fichinter = '.((int) $this->fk_fichinter);
  1273. $resql = $this->db->query($sql);
  1274. if ($resql) {
  1275. $obj = $this->db->fetch_object($resql);
  1276. $rangToUse = $obj->max + 1;
  1277. } else {
  1278. dol_print_error($this->db);
  1279. $this->db->rollback();
  1280. return -1;
  1281. }
  1282. }
  1283. // Insertion dans base de la ligne
  1284. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fichinterdet';
  1285. $sql .= ' (fk_fichinter, description, date, duree, rang)';
  1286. $sql .= " VALUES (".((int) $this->fk_fichinter).",";
  1287. $sql .= " '".$this->db->escape($this->desc)."',";
  1288. $sql .= " '".$this->db->idate($this->datei)."',";
  1289. $sql .= " ".((int) $this->duration).",";
  1290. $sql .= ' '.((int) $rangToUse);
  1291. $sql .= ')';
  1292. dol_syslog("FichinterLigne::insert", LOG_DEBUG);
  1293. $resql = $this->db->query($sql);
  1294. if ($resql) {
  1295. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'fichinterdet');
  1296. $this->rowid = $this->id;
  1297. if (!$error) {
  1298. $result = $this->insertExtraFields();
  1299. if ($result < 0) {
  1300. $error++;
  1301. }
  1302. }
  1303. $result = $this->update_total();
  1304. if ($result > 0) {
  1305. $this->rang = $rangToUse;
  1306. if (!$notrigger) {
  1307. // Call trigger
  1308. $result = $this->call_trigger('LINEFICHINTER_CREATE', $user);
  1309. if ($result < 0) {
  1310. $error++;
  1311. }
  1312. // End call triggers
  1313. }
  1314. }
  1315. if (!$error) {
  1316. $this->db->commit();
  1317. return $result;
  1318. } else {
  1319. $this->db->rollback();
  1320. return -1;
  1321. }
  1322. } else {
  1323. $this->error = $this->db->error()." sql=".$sql;
  1324. $this->db->rollback();
  1325. return -1;
  1326. }
  1327. }
  1328. /**
  1329. * Update intervention into database
  1330. *
  1331. * @param User $user Objet user that make creation
  1332. * @param int $notrigger Disable all triggers
  1333. * @return int <0 if ko, >0 if ok
  1334. */
  1335. public function update($user, $notrigger = 0)
  1336. {
  1337. global $langs, $conf;
  1338. $error = 0;
  1339. $this->db->begin();
  1340. // Mise a jour ligne en base
  1341. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinterdet SET";
  1342. $sql .= " description='".$this->db->escape($this->desc)."'";
  1343. $sql .= ",date='".$this->db->idate($this->datei)."'";
  1344. $sql .= ",duree=".$this->duration;
  1345. $sql .= ",rang='".$this->db->escape($this->rang)."'";
  1346. $sql .= " WHERE rowid = ".((int) $this->id);
  1347. dol_syslog("FichinterLigne::update", LOG_DEBUG);
  1348. $resql = $this->db->query($sql);
  1349. if ($resql) {
  1350. if (!$error) {
  1351. $result = $this->insertExtraFields();
  1352. if ($result < 0) {
  1353. $error++;
  1354. }
  1355. }
  1356. $result = $this->update_total();
  1357. if ($result > 0) {
  1358. if (!$notrigger) {
  1359. // Call trigger
  1360. $result = $this->call_trigger('LINEFICHINTER_MODIFY', $user);
  1361. if ($result < 0) {
  1362. $error++;
  1363. }
  1364. // End call triggers
  1365. }
  1366. }
  1367. if (!$error) {
  1368. $this->db->commit();
  1369. return $result;
  1370. } else {
  1371. $this->error = $this->db->lasterror();
  1372. $this->db->rollback();
  1373. return -1;
  1374. }
  1375. } else {
  1376. $this->error = $this->db->lasterror();
  1377. $this->db->rollback();
  1378. return -1;
  1379. }
  1380. }
  1381. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1382. /**
  1383. * Update total duration into llx_fichinter
  1384. *
  1385. * @return int <0 si ko, >0 si ok
  1386. */
  1387. public function update_total()
  1388. {
  1389. // phpcs:enable
  1390. global $conf;
  1391. $this->db->begin();
  1392. $sql = "SELECT SUM(duree) as total_duration, min(date) as dateo, max(date) as datee ";
  1393. $sql .= " FROM ".MAIN_DB_PREFIX."fichinterdet";
  1394. $sql .= " WHERE fk_fichinter=".((int) $this->fk_fichinter);
  1395. dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
  1396. $resql = $this->db->query($sql);
  1397. if ($resql) {
  1398. $obj = $this->db->fetch_object($resql);
  1399. $total_duration = 0;
  1400. if (!empty($obj->total_duration)) {
  1401. $total_duration = $obj->total_duration;
  1402. }
  1403. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter";
  1404. $sql .= " SET duree = ".((int) $total_duration);
  1405. $sql .= " , dateo = ".(!empty($obj->dateo) ? "'".$this->db->idate($obj->dateo)."'" : "null");
  1406. $sql .= " , datee = ".(!empty($obj->datee) ? "'".$this->db->idate($obj->datee)."'" : "null");
  1407. $sql .= " WHERE rowid = ".((int) $this->fk_fichinter);
  1408. dol_syslog("FichinterLigne::update_total", LOG_DEBUG);
  1409. $resql = $this->db->query($sql);
  1410. if ($resql) {
  1411. $this->db->commit();
  1412. return 1;
  1413. } else {
  1414. $this->error = $this->db->error();
  1415. $this->db->rollback();
  1416. return -2;
  1417. }
  1418. } else {
  1419. $this->error = $this->db->error();
  1420. $this->db->rollback();
  1421. return -1;
  1422. }
  1423. }
  1424. /**
  1425. * Delete a intervention line
  1426. *
  1427. * @param User $user Objet user that make creation
  1428. * @param int $notrigger Disable all triggers
  1429. * @return int >0 if ok, <0 if ko
  1430. */
  1431. public function deleteline($user, $notrigger = 0)
  1432. {
  1433. global $langs, $conf;
  1434. $error = 0;
  1435. if ($this->statut == 0) {
  1436. dol_syslog(get_class($this)."::deleteline lineid=".$this->id);
  1437. $this->db->begin();
  1438. $result = $this->deleteExtraFields();
  1439. if ($result < 0) {
  1440. $error++;
  1441. $this->db->rollback();
  1442. return -1;
  1443. }
  1444. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet WHERE rowid = ".((int) $this->id);
  1445. $resql = $this->db->query($sql);
  1446. if ($resql) {
  1447. $result = $this->update_total();
  1448. if ($result > 0) {
  1449. if (!$notrigger) {
  1450. // Call trigger
  1451. $result = $this->call_trigger('LINEFICHINTER_DELETE', $user);
  1452. if ($result < 0) {
  1453. $error++; $this->db->rollback(); return -1;
  1454. }
  1455. // End call triggers
  1456. }
  1457. $this->db->commit();
  1458. return $result;
  1459. } else {
  1460. $this->db->rollback();
  1461. return -1;
  1462. }
  1463. } else {
  1464. $this->error = $this->db->error()." sql=".$sql;
  1465. $this->db->rollback();
  1466. return -1;
  1467. }
  1468. } else {
  1469. return -2;
  1470. }
  1471. }
  1472. }