fichinterrec.class.php 26 KB

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