fichinterrec.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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(); // 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 int $rowid Id of fichinter rec to delete. If empty, we delete current instance of fichinter rec
  359. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  360. * @param int $idwarehouse Id warehouse to use for stock change.
  361. * @return int <0 if KO, >0 if OK
  362. */
  363. public function delete($rowid = 0, $notrigger = 0, $idwarehouse = -1)
  364. {
  365. if (empty($rowid)) {
  366. $rowid = $this->id;
  367. }
  368. dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG);
  369. $error = 0;
  370. $this->db->begin();
  371. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet_rec WHERE fk_fichinter = ".((int) $rowid);
  372. dol_syslog($sql);
  373. if ($this->db->query($sql)) {
  374. $sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter_rec WHERE rowid = ".((int) $rowid);
  375. dol_syslog($sql);
  376. if (!$this->db->query($sql)) {
  377. $this->error = $this->db->lasterror();
  378. $error = -1;
  379. }
  380. } else {
  381. $this->error = $this->db->lasterror();
  382. $error = -2;
  383. }
  384. if (!$error) {
  385. $this->db->commit();
  386. return 1;
  387. } else {
  388. $this->db->rollback();
  389. return $error;
  390. }
  391. }
  392. /**
  393. * Add a line to fichinter rec
  394. *
  395. * @param string $desc Description de la ligne
  396. * @param integer $duration Durée
  397. * @param string $date Date
  398. * @param int $rang Position of line
  399. * @param double $pu_ht Unit price without tax (> 0 even for credit note)
  400. * @param double $qty Quantity
  401. * @param double $txtva Forced VAT rate, otherwise -1
  402. * @param int $fk_product Id of predefined product/service
  403. * @param double $remise_percent Percentage of discount on line
  404. * @param string $price_base_type HT or TTC
  405. * @param int $info_bits Bits for type of lines
  406. * @param int $fk_remise_except Id discount
  407. * @param double $pu_ttc Unit price with tax (> 0 even for credit note)
  408. * @param int $type Type of line (0=product, 1=service)
  409. * @param int $special_code Special code
  410. * @param string $label Label of the line
  411. * @param string $fk_unit Unit
  412. * @return int <0 if KO, Id of line if OK
  413. */
  414. 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)
  415. {
  416. global $mysoc;
  417. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  418. // Check parameters
  419. if ($type < 0) {
  420. return -1;
  421. }
  422. if ($this->brouillon) {
  423. // Clean parameters
  424. $remise_percent = price2num($remise_percent);
  425. $qty = price2num($qty);
  426. if (!$qty) {
  427. $qty = 1;
  428. }
  429. if (!$info_bits) {
  430. $info_bits = 0;
  431. }
  432. $pu_ht = price2num($pu_ht);
  433. $pu_ttc = price2num($pu_ttc);
  434. if (!preg_match('/\((.*)\)/', $txtva)) {
  435. $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
  436. }
  437. if ($price_base_type == 'HT') {
  438. $pu = $pu_ht;
  439. } else {
  440. $pu = $pu_ttc;
  441. }
  442. // Calcul du total TTC et de la TVA pour la ligne a partir de
  443. // qty, pu, remise_percent et txtva
  444. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  445. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  446. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits, $type, $mysoc);
  447. $total_ht = $tabprice[0];
  448. $total_tva = $tabprice[1];
  449. $total_ttc = $tabprice[2];
  450. $pu_ht = $tabprice[3];
  451. $product_type = $type;
  452. if ($fk_product) {
  453. $product = new Product($this->db);
  454. $result = $product->fetch($fk_product);
  455. $product_type = $product->type;
  456. }
  457. $sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinterdet_rec (";
  458. $sql .= "fk_fichinter";
  459. $sql .= ", label";
  460. $sql .= ", description";
  461. $sql .= ", date";
  462. $sql .= ", duree";
  463. //$sql.= ", price";
  464. //$sql.= ", qty";
  465. //$sql.= ", tva_tx";
  466. $sql .= ", fk_product";
  467. $sql .= ", product_type";
  468. $sql .= ", remise_percent";
  469. $sql .= ", subprice";
  470. $sql .= ", total_ht";
  471. $sql .= ", total_tva";
  472. $sql .= ", total_ttc";
  473. $sql .= ", rang";
  474. //$sql.= ", special_code";
  475. $sql .= ", fk_unit";
  476. $sql .= ") VALUES (";
  477. $sql .= (int) $this->id;
  478. $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
  479. $sql .= ", ".(!empty($desc) ? "'".$this->db->escape($desc)."'" : "null");
  480. $sql .= ", ".(!empty($date) ? "'".$this->db->idate($date)."'" : "null");
  481. $sql .= ", ".$duration;
  482. //$sql.= ", ".price2num($pu_ht);
  483. //$sql.= ", ".(!empty($qty)? $qty :(!empty($duration)? $duration :"null"));
  484. //$sql.= ", ".price2num($txtva);
  485. $sql .= ", ".(!empty($fk_product) ? $fk_product : "null");
  486. $sql .= ", ".$product_type;
  487. $sql .= ", ".(!empty($remise_percent) ? $remise_percent : "null");
  488. $sql.= ", '".price2num($pu_ht)."'";
  489. $sql .= ", '".price2num($total_ht)."'";
  490. $sql .= ", '".price2num($total_tva)."'";
  491. $sql .= ", '".price2num($total_ttc)."'";
  492. $sql .= ", ".(int) $rang;
  493. //$sql.= ", ".$special_code;
  494. $sql .= ", ".(!empty($fk_unit) ? $fk_unit : "null");
  495. $sql .= ")";
  496. dol_syslog(get_class($this)."::addline", LOG_DEBUG);
  497. if ($this->db->query($sql)) {
  498. return 1;
  499. } else {
  500. $this->error = $this->db->lasterror();
  501. return -1;
  502. }
  503. }
  504. }
  505. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  506. /**
  507. * Rend la fichinter automatique
  508. *
  509. * @param User $user User object
  510. * @param int $freq Freq
  511. * @param string $courant Courant
  512. * @return int 0 if OK, <0 if KO
  513. */
  514. public function set_auto($user, $freq, $courant)
  515. {
  516. // phpcs:enable
  517. if ($user->rights->fichinter->creer) {
  518. $sql = "UPDATE ".MAIN_DB_PREFIX."fichinter_rec ";
  519. $sql .= " SET frequency='".$this->db->escape($freq)."'";
  520. $sql .= ", date_last_gen='".$this->db->escape($courant)."'";
  521. $sql .= " WHERE rowid = ".((int) $this->id);
  522. $resql = $this->db->query($sql);
  523. if ($resql) {
  524. $this->frequency = $freq;
  525. $this->date_last_gen = $courant;
  526. return 0;
  527. } else {
  528. dol_print_error($this->db);
  529. return -1;
  530. }
  531. } else {
  532. return -2;
  533. }
  534. }
  535. /**
  536. * Return clicable name (with picto eventually)
  537. *
  538. * @param int $withpicto Add picto into link
  539. * @param string $option Where point the link
  540. * @param int $max Maxlength of ref
  541. * @param int $short 1=Return just URL
  542. * @param string $moretitle Add more text to title tooltip
  543. * @return string String with URL
  544. */
  545. public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '')
  546. {
  547. global $langs, $hookmanager;
  548. $result = '';
  549. $label = $langs->trans("ShowInterventionModel").': '.$this->ref;
  550. $url = DOL_URL_ROOT.'/fichinter/card-rec.php?id='.$this->id;
  551. if ($short) {
  552. return $url;
  553. }
  554. $picto = 'intervention';
  555. $link = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  556. $linkend = '</a>';
  557. if ($withpicto) {
  558. $result .= $link.img_object($label, $picto, 'class="classfortooltip"').$linkend;
  559. }
  560. if ($withpicto && $withpicto != 2) {
  561. $result .= ' ';
  562. }
  563. if ($withpicto != 2) {
  564. $result .= $link.$this->ref.$linkend;
  565. }
  566. global $action;
  567. $hookmanager->initHooks(array($this->element . 'dao'));
  568. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  569. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  570. if ($reshook > 0) {
  571. $result = $hookmanager->resPrint;
  572. } else {
  573. $result .= $hookmanager->resPrint;
  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. * Function used to replace a product id with another one.
  609. *
  610. * @param DoliDB $db Database handler
  611. * @param int $origin_id Old product id
  612. * @param int $dest_id New product id
  613. * @return bool
  614. */
  615. public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
  616. {
  617. $tables = array(
  618. 'fichinterdet_rec'
  619. );
  620. return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
  621. }
  622. /**
  623. * Update frequency and unit
  624. *
  625. * @param int $frequency value of frequency
  626. * @param string $unit unit of frequency (d, m, y)
  627. * @return int <0 if KO, >0 if OK
  628. */
  629. public function setFrequencyAndUnit($frequency, $unit)
  630. {
  631. if (!$this->table_element) {
  632. dol_syslog(get_class($this)."::setFrequencyAndUnit called with table_element not defined", LOG_ERR);
  633. return -1;
  634. }
  635. if (!empty($frequency) && empty($unit)) {
  636. dol_syslog(get_class($this)."::setFrequencyAndUnit called with frequency defined but unit not ", LOG_ERR);
  637. return -2;
  638. }
  639. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  640. $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
  641. if (!empty($unit)) {
  642. $sql .= ', unit_frequency = "'.$this->db->escape($unit).'"';
  643. }
  644. $sql .= " WHERE rowid = ".((int) $this->id);
  645. dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
  646. if ($this->db->query($sql)) {
  647. $this->frequency = $frequency;
  648. if (!empty($unit)) {
  649. $this->unit_frequency = $unit;
  650. }
  651. return 1;
  652. } else {
  653. dol_print_error($this->db);
  654. return -1;
  655. }
  656. }
  657. /**
  658. * Update the next date of execution
  659. *
  660. * @param datetime $date date of execution
  661. * @param int $increment_nb_gen_done 0 do nothing more, >0 increment nb_gen_done
  662. * @return int <0 if KO, >0 if OK
  663. */
  664. public function setNextDate($date, $increment_nb_gen_done = 0)
  665. {
  666. if (!$this->table_element) {
  667. dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined", LOG_ERR);
  668. return -1;
  669. }
  670. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  671. $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
  672. if ($increment_nb_gen_done > 0) {
  673. $sql .= ', nb_gen_done = nb_gen_done + 1';
  674. }
  675. $sql .= " WHERE rowid = ".((int) $this->id);
  676. dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
  677. if ($this->db->query($sql)) {
  678. $this->date_when = $date;
  679. if ($increment_nb_gen_done > 0) {
  680. $this->nb_gen_done++;
  681. }
  682. return 1;
  683. } else {
  684. dol_print_error($this->db);
  685. return -1;
  686. }
  687. }
  688. /**
  689. * Update the maximum period
  690. *
  691. * @param int $nb number of maximum period
  692. * @return int <0 if KO, >0 if OK
  693. */
  694. public function setMaxPeriod($nb)
  695. {
  696. if (!$this->table_element) {
  697. dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
  698. return -1;
  699. }
  700. if (empty($nb)) {
  701. $nb = 0;
  702. }
  703. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  704. $sql .= ' SET nb_gen_max = '.((int) $nb);
  705. $sql .= ' WHERE rowid = '.((int) $this->id);
  706. dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
  707. if ($this->db->query($sql)) {
  708. $this->nb_gen_max = $nb;
  709. return 1;
  710. } else {
  711. dol_print_error($this->db);
  712. return -1;
  713. }
  714. }
  715. /**
  716. * Update the auto validate fichinter
  717. *
  718. * @param int $validate 0 to create in draft, 1 to create and validate fichinter
  719. * @return int <0 if KO, >0 if OK
  720. */
  721. public function setAutoValidate($validate)
  722. {
  723. if (!$this->table_element) {
  724. dol_syslog(get_class($this)."::setAutoValidate called with property table_element not defined", LOG_ERR);
  725. return -1;
  726. }
  727. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  728. $sql .= ' SET auto_validate = '.((int) $validate);
  729. $sql .= ' WHERE rowid = '.((int) $this->id);
  730. dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
  731. if ($this->db->query($sql)) {
  732. $this->auto_validate = $validate;
  733. return 1;
  734. } else {
  735. dol_print_error($this->db);
  736. return -1;
  737. }
  738. }
  739. /**
  740. * Update the Number of Generation Done
  741. *
  742. * @return int <0 if KO, >0 if OK
  743. */
  744. public function updateNbGenDone()
  745. {
  746. if (!$this->table_element) {
  747. dol_syslog(get_class($this)."::updateNbGenDone called with property table_element not defined", LOG_ERR);
  748. return -1;
  749. }
  750. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
  751. $sql .= ' SET nb_gen_done = nb_gen_done + 1';
  752. $sql .= ' , date_last_gen = now()';
  753. // si on et arrivé à la fin des génération
  754. if ($this->nb_gen_max == $this->nb_gen_done + 1) {
  755. $sql .= ' , statut = 1';
  756. }
  757. $sql .= " WHERE rowid = ".((int) $this->id);
  758. dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
  759. if ($this->db->query($sql)) {
  760. $this->nb_gen_done = $this->nb_gen_done + 1;
  761. $this->nb_gen_done = dol_now();
  762. return 1;
  763. } else {
  764. dol_print_error($this->db);
  765. return -1;
  766. }
  767. }
  768. }