fichinterrec.class.php 26 KB

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