fichinterrec.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
  7. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  8. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  9. * Copyright (C) 2016-2018 Charlie Benke <charlie@patas-monkey.com>
  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 <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/fichinter/class/fichinterrec.class.php
  26. * \ingroup facture
  27. * \brief Fichier de la classe des factures recurentes
  28. */
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  32. /**
  33. * Classe de gestion des factures recurrentes/Modeles
  34. */
  35. class FichinterRec extends Fichinter
  36. {
  37. public $element = 'fichinterrec';
  38. public $table_element = 'fichinter_rec';
  39. public $table_element_line = 'fichinter_rec';
  40. /**
  41. * @var string Fieldname with ID of parent key if this field has a parent
  42. */
  43. public $fk_element = 'fk_fichinter';
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected $table_ref_field = 'titre';
  48. /**
  49. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  50. */
  51. public $picto = 'intervention';
  52. /**
  53. * @var string title
  54. */
  55. public $title;
  56. public $number;
  57. public $date;
  58. public $amount;
  59. public $remise;
  60. public $tva;
  61. public $total;
  62. /**
  63. * @var int Proposal Id
  64. */
  65. public $propalid;
  66. public $date_last_gen;
  67. public $date_when;
  68. /**
  69. * @var int number of generation done
  70. */
  71. public $nb_gen_done;
  72. /**
  73. * @var int number of maximum generation
  74. */
  75. public $nb_gen_max;
  76. /**
  77. * int rank
  78. */
  79. public $rang;
  80. public $special_code;
  81. public $usenewprice = 0;
  82. /**
  83. * Constructor
  84. *
  85. * @param DoliDB $db Database handler
  86. */
  87. public function __construct($db)
  88. {
  89. $this->db = $db;
  90. //status dans l'ordre de l'intervention
  91. $this->statuts[0] = 'Draft';
  92. $this->statuts[1] = 'Closed';
  93. $this->statuts_short[0] = 'Draft';
  94. $this->statuts_short[1] = 'Closed';
  95. $this->statuts_logo[0] = 'statut0';
  96. $this->statuts_logo[1] = 'statut1';
  97. }
  98. /**
  99. * Returns the label status
  100. *
  101. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  102. * @return string Label
  103. */
  104. public function getLibStatut($mode = 0)
  105. {
  106. return $this->LibStatut($this->statut, $mode);
  107. }
  108. /**
  109. * Create a predefined fichinter
  110. *
  111. * @param User $user User object
  112. * @param int $notrigger no trigger
  113. * @return int <0 if KO, id of fichinter if OK
  114. */
  115. public function create($user, $notrigger = 0)
  116. {
  117. global $conf;
  118. $error = 0;
  119. $now = dol_now();
  120. // Clean parameters
  121. $this->title = trim($this->title);
  122. $this->description = trim($this->description);
  123. $this->db->begin();
  124. // Load fichinter model
  125. $fichintsrc = new Fichinter($this->db);
  126. $result = $fichintsrc->fetch($this->id_origin);
  127. $result = $fichintsrc->fetch_lines(1); // to get all lines
  128. if ($result > 0) {
  129. // On positionne en mode brouillon la facture
  130. $this->brouillon = 1;
  131. $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter_rec (";
  132. $sql .= "titre";
  133. $sql .= ", fk_soc";
  134. $sql .= ", entity";
  135. $sql .= ", datec";
  136. $sql .= ", duree";
  137. $sql .= ", description";
  138. $sql .= ", note_private";
  139. $sql .= ", note_public";
  140. $sql .= ", fk_user_author";
  141. $sql .= ", fk_projet";
  142. $sql .= ", fk_contrat";
  143. $sql .= ", modelpdf";
  144. $sql .= ", frequency";
  145. $sql .= ", unit_frequency";
  146. $sql .= ", date_when";
  147. $sql .= ", date_last_gen";
  148. $sql .= ", nb_gen_done";
  149. $sql .= ", nb_gen_max";
  150. // $sql.= ", auto_validate";
  151. $sql .= ") VALUES (";
  152. $sql .= "'".$this->db->escape($this->title)."'";
  153. $sql .= ", ".($this->socid > 0 ? $this->socid : 'null');
  154. $sql .= ", ".$conf->entity;
  155. $sql .= ", '".$this->db->idate($now)."'";
  156. $sql .= ", ".(!empty($fichintsrc->duration) ? $fichintsrc->duration : '0');
  157. $sql .= ", ".(!empty($this->description) ? ("'".$this->db->escape($this->description)."'") : "null");
  158. $sql .= ", ".(!empty($fichintsrc->note_private) ? ("'".$this->db->escape($fichintsrc->note_private)."'") : "null");
  159. $sql .= ", ".(!empty($fichintsrc->note_public) ? ("'".$this->db->escape($fichintsrc->note_public)."'") : "null");
  160. $sql .= ", ".$user->id;
  161. // si c'est la même société on conserve les liens vers le projet et le contrat
  162. if ($this->socid == $fichintsrc->socid) {
  163. $sql .= ", ".(!empty($fichintsrc->fk_project) ? $fichintsrc->fk_project : "null");
  164. $sql .= ", ".(!empty($fichintsrc->fk_contrat) ? $fichintsrc->fk_contrat : "null");
  165. } else {
  166. $sql .= ", null, null";
  167. }
  168. $sql .= ", ".(!empty($fichintsrc->model_pdf) ? "'".$this->db->escape($fichintsrc->model_pdf)."'" : "''");
  169. // récurrence
  170. $sql .= ", ".(!empty($this->frequency) ? $this->frequency : "null");
  171. $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
  172. $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'null');
  173. $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'null');
  174. $sql .= ", 0"; // we start à 0
  175. $sql .= ", ".$this->nb_gen_max;
  176. // $sql.= ", ".$this->auto_validate;
  177. $sql .= ")";
  178. if ($this->db->query($sql)) {
  179. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  180. /*
  181. * Lines
  182. */
  183. $num = count($fichintsrc->lines);
  184. for ($i = 0; $i < $num; $i++) {
  185. //var_dump($fichintsrc->lines[$i]);
  186. $result_insert = $this->addline(
  187. $fichintsrc->lines[$i]->desc,
  188. $fichintsrc->lines[$i]->duration,
  189. $fichintsrc->lines[$i]->datei,
  190. $fichintsrc->lines[$i]->rang,
  191. $fichintsrc->lines[$i]->subprice,
  192. $fichintsrc->lines[$i]->qty,
  193. $fichintsrc->lines[$i]->tva_tx,
  194. $fichintsrc->lines[$i]->fk_product,
  195. $fichintsrc->lines[$i]->remise_percent,
  196. 'HT',
  197. 0,
  198. '',
  199. 0,
  200. $fichintsrc->lines[$i]->product_type,
  201. $fichintsrc->lines[$i]->special_code,
  202. $fichintsrc->lines[$i]->label,
  203. $fichintsrc->lines[$i]->fk_unit
  204. );
  205. if ($result_insert < 0) {
  206. $error++;
  207. }
  208. }
  209. if ($error) {
  210. $this->db->rollback();
  211. } else {
  212. $this->db->commit();
  213. return $this->id;
  214. }
  215. } else {
  216. $this->error = $this->db->error().' sql='.$sql;
  217. $this->db->rollback();
  218. return -2;
  219. }
  220. } else {
  221. $this->db->rollback();
  222. return -1;
  223. }
  224. }
  225. /**
  226. * Get the template of intervention object and lines
  227. *
  228. * @param int $rowid Id of object to load
  229. * @param string $ref Reference of fichinter
  230. * @param string $ref_ext External reference of fichinter
  231. * @return int >0 if OK, <0 if KO, 0 if not found
  232. */
  233. public function fetch($rowid = 0, $ref = '', $ref_ext = '')
  234. {
  235. $sql = 'SELECT f.titre as title, f.fk_soc';
  236. $sql .= ', f.datec, f.duree, f.fk_projet, f.fk_contrat, f.description';
  237. $sql .= ', f.note_private, f.note_public, f.fk_user_author';
  238. $sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.auto_validate';
  239. $sql .= ', f.note_private, f.note_public, f.fk_user_author';
  240. $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinter_rec as f';
  241. if ($rowid > 0) {
  242. $sql .= ' WHERE f.rowid='.$rowid;
  243. } elseif ($ref) {
  244. $sql .= " WHERE f.titre='".$this->db->escape($ref)."'";
  245. }
  246. dol_syslog(get_class($this)."::fetch rowid=".$rowid, LOG_DEBUG);
  247. $result = $this->db->query($sql);
  248. if ($result) {
  249. if ($this->db->num_rows($result)) {
  250. $obj = $this->db->fetch_object($result);
  251. $this->id = $rowid;
  252. $this->titre = $obj->title;
  253. $this->title = $obj->title;
  254. $this->ref = $obj->title;
  255. $this->description = $obj->description;
  256. $this->datec = $obj->datec;
  257. $this->duration = $obj->duree;
  258. $this->socid = $obj->fk_soc;
  259. $this->statut = 0;
  260. $this->fk_project = $obj->fk_projet;
  261. $this->fk_contrat = $obj->fk_contrat;
  262. $this->note_private = $obj->note_private;
  263. $this->note_public = $obj->note_public;
  264. $this->user_author = $obj->fk_user_author;
  265. $this->model_pdf = $obj->model_pdf;
  266. $this->modelpdf = $obj->model_pdf; // deprecated
  267. $this->rang = $obj->rang;
  268. $this->special_code = $obj->special_code;
  269. $this->frequency = $obj->frequency;
  270. $this->unit_frequency = $obj->unit_frequency;
  271. $this->date_when = $this->db->jdate($obj->date_when);
  272. $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
  273. $this->nb_gen_done = $obj->nb_gen_done;
  274. $this->nb_gen_max = $obj->nb_gen_max;
  275. $this->auto_validate = $obj->auto_validate;
  276. $this->brouillon = 1;
  277. // Lines
  278. $result = $this->fetch_lines();
  279. if ($result < 0) {
  280. $this->error = $this->db->error();
  281. return -3;
  282. }
  283. return 1;
  284. } else {
  285. $this->error = 'Interventional with id '.$rowid.' not found sql='.$sql;
  286. dol_syslog(get_class($this).'::Fetch Error '.$this->error, LOG_ERR);
  287. return -2;
  288. }
  289. } else {
  290. $this->error = $this->db->error();
  291. return -1;
  292. }
  293. }
  294. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  295. /**
  296. * Load all lines of template of intervention into this->lines
  297. *
  298. * @param int $sall sall
  299. * @return int 1 if OK, < 0 if KO
  300. */
  301. public function fetch_lines($sall = 0)
  302. {
  303. // phpcs:enable
  304. $sql = 'SELECT l.rowid, l.fk_product, l.product_type, l.label as custom_label, l.description, ';
  305. $sql .= ' l.price, l.qty, l.tva_tx, l.remise, l.remise_percent, l.subprice, l.duree, ';
  306. $sql .= ' l.total_ht, l.total_tva, l.total_ttc,';
  307. $sql .= ' l.rang, l.special_code,';
  308. $sql .= ' l.fk_unit, p.ref as product_ref, p.fk_product_type as fk_product_type,';
  309. $sql .= ' p.label as product_label, p.description as product_desc';
  310. $sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet_rec as l';
  311. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
  312. $sql .= ' WHERE l.fk_fichinter = '.$this->id;
  313. dol_syslog('FichInter-rec::fetch_lines', LOG_DEBUG);
  314. $result = $this->db->query($sql);
  315. if ($result) {
  316. $num = $this->db->num_rows($result);
  317. $i = 0;
  318. while ($i < $num) {
  319. $objp = $this->db->fetch_object($result);
  320. $line = new FichinterLigne($this->db);
  321. $line->id = $objp->rowid;
  322. $line->label = $objp->custom_label; // Label line
  323. $line->desc = $objp->description; // Description line
  324. $line->product_type = $objp->product_type; // Type of line
  325. $line->product_ref = $objp->product_ref; // Ref product
  326. $line->product_label = $objp->product_label; // Label product
  327. $line->product_desc = $objp->product_desc; // Description product
  328. $line->fk_product_type = $objp->fk_product_type; // Type of product
  329. $line->qty = $objp->qty;
  330. $line->duree = $objp->duree;
  331. $line->duration = $objp->duree;
  332. $line->datei = $objp->date;
  333. $line->subprice = $objp->subprice;
  334. $line->tva_tx = $objp->tva_tx;
  335. $line->remise_percent = $objp->remise_percent;
  336. $line->fk_remise_except = $objp->fk_remise_except;
  337. $line->fk_product = $objp->fk_product;
  338. $line->date_start = $objp->date_start;
  339. $line->date_end = $objp->date_end;
  340. $line->date_start = $objp->date_start;
  341. $line->date_end = $objp->date_end;
  342. $line->info_bits = $objp->info_bits;
  343. $line->total_ht = $objp->total_ht;
  344. $line->total_tva = $objp->total_tva;
  345. $line->total_ttc = $objp->total_ttc;
  346. $line->code_ventilation = $objp->fk_code_ventilation;
  347. $line->rang = $objp->rang;
  348. $line->special_code = $objp->special_code;
  349. $line->fk_unit = $objp->fk_unit;
  350. // Ne plus utiliser
  351. $line->price = $objp->price;
  352. $line->remise = $objp->remise;
  353. $this->lines[$i] = $line;
  354. $i++;
  355. }
  356. $this->db->free($result);
  357. return 1;
  358. } else {
  359. $this->error = $this->db->error();
  360. return -3;
  361. }
  362. }
  363. /**
  364. * Delete template fichinter rec
  365. *
  366. * @param int $rowid Id of fichinter rec to delete. If empty, we delete current instance of fichinter rec
  367. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  368. * @param int $idwarehouse Id warehouse to use for stock change.
  369. * @return int <0 if KO, >0 if OK
  370. */
  371. public function delete($rowid = 0, $notrigger = 0, $idwarehouse = -1)
  372. {
  373. if (empty($rowid)) {
  374. $rowid = $this->id;
  375. }
  376. dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG);
  377. $error = 0;
  378. $this->db->begin();
  379. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet_rec WHERE fk_fichinter = ".$rowid;
  380. dol_syslog($sql);
  381. if ($this->db->query($sql)) {
  382. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter_rec WHERE rowid = ".$rowid;
  383. dol_syslog($sql);
  384. if (!$this->db->query($sql)) {
  385. $this->error = $this->db->lasterror();
  386. $error = -1;
  387. }
  388. } else {
  389. $this->error = $this->db->lasterror();
  390. $error = -2;
  391. }
  392. if (!$error) {
  393. $this->db->commit();
  394. return 1;
  395. } else {
  396. $this->db->rollback();
  397. return $error;
  398. }
  399. }
  400. /**
  401. * Add a line to fichinter rec
  402. *
  403. * @param string $desc Description de la ligne
  404. * @param integer $duration Durée
  405. * @param string $datei Date
  406. * @param int $rang Position of line
  407. * @param double $pu_ht Unit price without tax (> 0 even for credit note)
  408. * @param double $qty Quantity
  409. * @param double $txtva Forced VAT rate, otherwise -1
  410. * @param int $fk_product Id of predefined product/service
  411. * @param double $remise_percent Percentage of discount on line
  412. * @param string $price_base_type HT or TTC
  413. * @param int $info_bits Bits for type of lines
  414. * @param int $fk_remise_except Id discount
  415. * @param double $pu_ttc Unit price with tax (> 0 even for credit note)
  416. * @param int $type Type of line (0=product, 1=service)
  417. * @param int $special_code Special code
  418. * @param string $label Label of the line
  419. * @param string $fk_unit Unit
  420. * @return int <0 if KO, Id of line if OK
  421. */
  422. public function addline($desc, $duration, $datei, $rang = -1, $pu_ht = 0, $qty = 0, $txtva = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = '', $pu_ttc = 0, $type = 0, $special_code = 0, $label = '', $fk_unit = null)
  423. {
  424. global $mysoc;
  425. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  426. // Check parameters
  427. if ($type < 0) {
  428. return -1;
  429. }
  430. if ($this->brouillon) {
  431. // Clean parameters
  432. $remise_percent = price2num($remise_percent);
  433. $qty = price2num($qty);
  434. if (!$qty) {
  435. $qty = 1;
  436. }
  437. if (!$info_bits) {
  438. $info_bits = 0;
  439. }
  440. $pu_ht = price2num($pu_ht);
  441. $pu_ttc = price2num($pu_ttc);
  442. if (!preg_match('/\((.*)\)/', $txtva)) {
  443. $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
  444. }
  445. if ($price_base_type == 'HT') {
  446. $pu = $pu_ht;
  447. } else {
  448. $pu = $pu_ttc;
  449. }
  450. // Calcul du total TTC et de la TVA pour la ligne a partir de
  451. // qty, pu, remise_percent et txtva
  452. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  453. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  454. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits, $type, $mysoc);
  455. $total_ht = $tabprice[0];
  456. $total_tva = $tabprice[1];
  457. $total_ttc = $tabprice[2];
  458. $product_type = $type;
  459. if ($fk_product) {
  460. $product = new Product($this->db);
  461. $result = $product->fetch($fk_product);
  462. $product_type = $product->type;
  463. }
  464. $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinterdet_rec (";
  465. $sql .= "fk_fichinter";
  466. $sql .= ", label";
  467. $sql .= ", description";
  468. $sql .= ", date";
  469. $sql .= ", duree";
  470. //$sql.= ", price";
  471. //$sql.= ", qty";
  472. //$sql.= ", tva_tx";
  473. $sql .= ", fk_product";
  474. $sql .= ", product_type";
  475. $sql .= ", remise_percent";
  476. //$sql.= ", subprice";
  477. $sql .= ", remise";
  478. $sql .= ", total_ht";
  479. $sql .= ", total_tva";
  480. $sql .= ", total_ttc";
  481. $sql .= ", rang";
  482. //$sql.= ", special_code";
  483. $sql .= ", fk_unit";
  484. $sql .= ") VALUES (";
  485. $sql .= (int) $this->id;
  486. $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
  487. $sql .= ", ".(!empty($desc) ? "'".$this->db->escape($desc)."'" : "null");
  488. $sql .= ", ".(!empty($datei) ? "'".$this->db->idate($datei)."'" : "null");
  489. $sql .= ", ".$duration;
  490. //$sql.= ", ".price2num($pu_ht);
  491. //$sql.= ", ".(!empty($qty)? $qty :(!empty($duration)? $duration :"null"));
  492. //$sql.= ", ".price2num($txtva);
  493. $sql .= ", ".(!empty($fk_product) ? $fk_product : "null");
  494. $sql .= ", ".$product_type;
  495. $sql .= ", ".(!empty($remise_percent) ? $remise_percent : "null");
  496. //$sql.= ", '".price2num($pu_ht)."'";
  497. $sql .= ", null";
  498. $sql .= ", '".price2num($total_ht)."'";
  499. $sql .= ", '".price2num($total_tva)."'";
  500. $sql .= ", '".price2num($total_ttc)."'";
  501. $sql .= ", ".(int) $rang;
  502. //$sql.= ", ".$special_code;
  503. $sql .= ", ".(!empty($fk_unit) ? $fk_unit : "null");
  504. $sql .= ")";
  505. dol_syslog(get_class($this)."::addline", LOG_DEBUG);
  506. if ($this->db->query($sql)) {
  507. return 1;
  508. } else {
  509. $this->error = $this->db->lasterror();
  510. return -1;
  511. }
  512. }
  513. }
  514. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  515. /**
  516. * Rend la fichinter automatique
  517. *
  518. * @param User $user User object
  519. * @param int $freq Freq
  520. * @param string $courant Courant
  521. * @return int 0 if OK, <0 if KO
  522. */
  523. public function set_auto($user, $freq, $courant)
  524. {
  525. // phpcs:enable
  526. if ($user->rights->fichinter->creer) {
  527. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter_rec ";
  528. $sql .= " SET frequency='".$this->db->escape($freq)."'";
  529. $sql .= ", date_last_gen='".$this->db->escape($courant)."'";
  530. $sql .= " WHERE rowid = ".$this->id;
  531. $resql = $this->db->query($sql);
  532. if ($resql) {
  533. $this->frequency = $freq;
  534. $this->date_last_gen = $courant;
  535. return 0;
  536. } else {
  537. dol_print_error($this->db);
  538. return -1;
  539. }
  540. } else {
  541. return -2;
  542. }
  543. }
  544. /**
  545. * Return clicable name (with picto eventually)
  546. *
  547. * @param int $withpicto Add picto into link
  548. * @param string $option Where point the link
  549. * @param int $max Maxlength of ref
  550. * @param int $short 1=Return just URL
  551. * @param string $moretitle Add more text to title tooltip
  552. * @return string String with URL
  553. */
  554. public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '')
  555. {
  556. global $langs;
  557. $result = '';
  558. $label = $langs->trans("ShowInterventionModel").': '.$this->ref;
  559. $url = DOL_URL_ROOT.'/fichinter/card-rec.php?id='.$this->id;
  560. if ($short) {
  561. return $url;
  562. }
  563. $picto = 'intervention';
  564. $link = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  565. $linkend = '</a>';
  566. if ($withpicto) {
  567. $result .= $link.img_object($label, $picto, 'class="classfortooltip"').$linkend;
  568. }
  569. if ($withpicto && $withpicto != 2) {
  570. $result .= ' ';
  571. }
  572. if ($withpicto != 2) {
  573. $result .= $link.$this->ref.$linkend;
  574. }
  575. return $result;
  576. }
  577. /**
  578. * Initialise an instance with random values.
  579. * Used to build previews or test instances.
  580. * id must be 0 if object instance is a specimen.
  581. *
  582. * @param string $option ''=Create a specimen fichinter with lines, 'nolines'=No lines
  583. * @return void
  584. */
  585. public function initAsSpecimen($option = '')
  586. {
  587. global $user, $langs, $conf;
  588. $now = dol_now();
  589. $arraynow = dol_getdate($now);
  590. $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
  591. parent::initAsSpecimen($option);
  592. $this->usenewprice = 1;
  593. }
  594. /**
  595. * Function used to replace a thirdparty id with another one.
  596. *
  597. * @param DoliDB $db Database handler
  598. * @param int $origin_id Old thirdparty id
  599. * @param int $dest_id New thirdparty id
  600. * @return bool
  601. */
  602. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  603. {
  604. $tables = array('fichinter_rec');
  605. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  606. }
  607. /**
  608. * Update frequency and unit
  609. *
  610. * @param int $frequency value of frequency
  611. * @param string $unit unit of frequency (d, m, y)
  612. * @return int <0 if KO, >0 if OK
  613. */
  614. public function setFrequencyAndUnit($frequency, $unit)
  615. {
  616. if (!$this->table_element) {
  617. dol_syslog(get_class($this)."::setFrequencyAndUnit called with table_element not defined", LOG_ERR);
  618. return -1;
  619. }
  620. if (!empty($frequency) && empty($unit)) {
  621. dol_syslog(get_class($this)."::setFrequencyAndUnit called with frequency defined but unit not ", LOG_ERR);
  622. return -2;
  623. }
  624. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  625. $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
  626. if (!empty($unit)) {
  627. $sql .= ', unit_frequency = "'.$this->db->escape($unit).'"';
  628. }
  629. $sql .= ' WHERE rowid = '.$this->id;
  630. dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
  631. if ($this->db->query($sql)) {
  632. $this->frequency = $frequency;
  633. if (!empty($unit)) {
  634. $this->unit_frequency = $unit;
  635. }
  636. return 1;
  637. } else {
  638. dol_print_error($this->db);
  639. return -1;
  640. }
  641. }
  642. /**
  643. * Update the next date of execution
  644. *
  645. * @param datetime $date date of execution
  646. * @param int $increment_nb_gen_done 0 do nothing more, >0 increment nb_gen_done
  647. * @return int <0 if KO, >0 if OK
  648. */
  649. public function setNextDate($date, $increment_nb_gen_done = 0)
  650. {
  651. if (!$this->table_element) {
  652. dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined", LOG_ERR);
  653. return -1;
  654. }
  655. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  656. $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
  657. if ($increment_nb_gen_done > 0) {
  658. $sql .= ', nb_gen_done = nb_gen_done + 1';
  659. }
  660. $sql .= ' WHERE rowid = '.$this->id;
  661. dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
  662. if ($this->db->query($sql)) {
  663. $this->date_when = $date;
  664. if ($increment_nb_gen_done > 0) {
  665. $this->nb_gen_done++;
  666. }
  667. return 1;
  668. } else {
  669. dol_print_error($this->db);
  670. return -1;
  671. }
  672. }
  673. /**
  674. * Update the maximum period
  675. *
  676. * @param int $nb number of maximum period
  677. * @return int <0 if KO, >0 if OK
  678. */
  679. public function setMaxPeriod($nb)
  680. {
  681. if (!$this->table_element) {
  682. dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
  683. return -1;
  684. }
  685. if (empty($nb)) {
  686. $nb = 0;
  687. }
  688. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  689. $sql .= ' SET nb_gen_max = '.$nb;
  690. $sql .= ' WHERE rowid = '.$this->id;
  691. dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
  692. if ($this->db->query($sql)) {
  693. $this->nb_gen_max = $nb;
  694. return 1;
  695. } else {
  696. dol_print_error($this->db);
  697. return -1;
  698. }
  699. }
  700. /**
  701. * Update the auto validate fichinter
  702. *
  703. * @param int $validate 0 to create in draft, 1 to create and validate fichinter
  704. * @return int <0 if KO, >0 if OK
  705. */
  706. public function setAutoValidate($validate)
  707. {
  708. if (!$this->table_element) {
  709. dol_syslog(get_class($this)."::setAutoValidate called with property table_element not defined", LOG_ERR);
  710. return -1;
  711. }
  712. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  713. $sql .= ' SET auto_validate = '.$validate;
  714. $sql .= ' WHERE rowid = '.$this->id;
  715. dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
  716. if ($this->db->query($sql)) {
  717. $this->auto_validate = $validate;
  718. return 1;
  719. } else {
  720. dol_print_error($this->db);
  721. return -1;
  722. }
  723. }
  724. /**
  725. * Update the Number of Generation Done
  726. *
  727. * @return int <0 if KO, >0 if OK
  728. */
  729. public function updateNbGenDone()
  730. {
  731. if (!$this->table_element) {
  732. dol_syslog(get_class($this)."::updateNbGenDone called with property table_element not defined", LOG_ERR);
  733. return -1;
  734. }
  735. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  736. $sql .= ' SET nb_gen_done = nb_gen_done + 1';
  737. $sql .= ' , date_last_gen = now()';
  738. // si on et arrivé à la fin des génération
  739. if ($this->nb_gen_max == $this->nb_gen_done + 1) {
  740. $sql .= ' , statut = 1';
  741. }
  742. $sql .= ' WHERE rowid = '.$this->id;
  743. dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
  744. if ($this->db->query($sql)) {
  745. $this->nb_gen_done = $this->nb_gen_done + 1;
  746. $this->nb_gen_done = dol_now();
  747. return 1;
  748. } else {
  749. dol_print_error($this->db);
  750. return -1;
  751. }
  752. }
  753. }