PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/facture/class/facture-rec.class.php

https://github.com/asterix14/dolibarr
PHP | 573 lines | 423 code | 62 blank | 88 comment | 34 complexity | 79ea1c0a578bbbcb1b9ea13843e0becf MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009 Regis Houssin <regis@dolibarr.fr>
  5. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/facture/class/facture-rec.class.php
  22. * \ingroup facture
  23. * \brief Fichier de la classe des factures recurentes
  24. */
  25. require_once(DOL_DOCUMENT_ROOT."/core/class/notify.class.php");
  26. require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php");
  28. /**
  29. * \class FactureRec
  30. * \brief Classe de gestion des factures recurrentes/Modeles
  31. */
  32. class FactureRec extends Facture
  33. {
  34. public $element='facturerec';
  35. public $table_element='facture_rec';
  36. public $table_element_line='facturedet_rec';
  37. public $fk_element='fk_facture';
  38. var $id;
  39. //! Id customer
  40. var $socid;
  41. //! Customer object (charging by fetch_client)
  42. var $client;
  43. var $number;
  44. var $author;
  45. var $date;
  46. var $ref;
  47. var $amount;
  48. var $remise;
  49. var $tva;
  50. var $total;
  51. var $note;
  52. var $db_table;
  53. var $propalid;
  54. var $fk_project;
  55. var $rang;
  56. var $special_code;
  57. var $lines=array();
  58. /**
  59. * Constructor
  60. *
  61. * @param DoliDB $DB Database handler
  62. */
  63. function FactureRec($DB)
  64. {
  65. $this->db = $DB ;
  66. }
  67. /**
  68. * Create a predefined invoice
  69. *
  70. * @param User $user
  71. * @param int $facid Id of source invoice
  72. * @return int <0 if KO, id of invoice if OK
  73. */
  74. function create($user,$facid)
  75. {
  76. global $conf, $langs;
  77. $error=0;
  78. // Clean parameters
  79. $this->titre=trim($this->titre);
  80. // Validate parameters
  81. if (empty($this->titre))
  82. {
  83. $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Title"));
  84. return -3;
  85. }
  86. $this->db->begin();
  87. // Charge facture modele
  88. $facsrc=new Facture($this->db);
  89. $result=$facsrc->fetch($facid);
  90. if ($result > 0)
  91. {
  92. // On positionne en mode brouillon la facture
  93. $this->brouillon = 1;
  94. $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (";
  95. $sql.= "titre";
  96. $sql.= ", fk_soc";
  97. $sql.= ", entity";
  98. $sql.= ", datec";
  99. $sql.= ", amount";
  100. $sql.= ", remise";
  101. $sql.= ", note";
  102. $sql.= ", fk_user_author";
  103. $sql.= ", fk_projet";
  104. $sql.= ", fk_cond_reglement";
  105. $sql.= ", fk_mode_reglement";
  106. $sql.= ") VALUES (";
  107. $sql.= "'".$this->titre."'";
  108. $sql.= ", '".$facsrc->socid."'";
  109. $sql.= ", ".$conf->entity;
  110. $sql.= ", ".$this->db->idate(mktime());
  111. $sql.= ", '".$facsrc->amount."'";
  112. $sql.= ", '".$facsrc->remise."'";
  113. $sql.= ", '".$this->db->escape($this->note)."'";
  114. $sql.= ", '".$user->id."'";
  115. $sql.= ", ".($facsrc->fk_project?"'".$facsrc->fk_project."'":"null");
  116. $sql.= ", '".$facsrc->cond_reglement_id."'";
  117. $sql.= ", '".$facsrc->mode_reglement_id."'";
  118. $sql.= ")";
  119. if ( $this->db->query($sql) )
  120. {
  121. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
  122. /*
  123. * Lines
  124. */
  125. $num=count($facsrc->lines);
  126. for ($i = 0; $i < $num; $i++)
  127. {
  128. $result_insert = $this->addline($this->id,
  129. $facsrc->lines[$i]->desc,
  130. $facsrc->lines[$i]->subprice,
  131. $facsrc->lines[$i]->qty,
  132. $facsrc->lines[$i]->tva_tx,
  133. $facsrc->lines[$i]->fk_product,
  134. $facsrc->lines[$i]->remise_percent,
  135. 'HT',0,'',0,
  136. $facsrc->lines[$i]->product_type,
  137. $facsrc->lines[$i]->rang,
  138. $facsrc->lines[$i]->special_code
  139. );
  140. if ($result_insert < 0)
  141. {
  142. $error++;
  143. }
  144. }
  145. if ($error)
  146. {
  147. $this->db->rollback();
  148. }
  149. else
  150. {
  151. $this->db->commit();
  152. return $this->id;
  153. }
  154. }
  155. else
  156. {
  157. $this->error=$this->db->error().' sql='.$sql;
  158. $this->db->rollback();
  159. return -2;
  160. }
  161. }
  162. else
  163. {
  164. $this->db->rollback();
  165. return -1;
  166. }
  167. }
  168. /**
  169. * \brief Recupere l'objet facture et ses lignes de factures
  170. * \param rowid id de la facture a recuperer
  171. * \return int >0 si ok, <0 si ko
  172. */
  173. function fetch($rowid)
  174. {
  175. $sql = 'SELECT f.titre,f.fk_soc,f.amount,f.tva,f.total,f.total_ttc,f.remise_percent,f.remise_absolue,f.remise';
  176. $sql.= ', f.date_lim_reglement as dlr';
  177. $sql.= ', f.note, f.note_public, f.fk_user_author';
  178. $sql.= ', f.fk_mode_reglement, f.fk_cond_reglement';
  179. $sql.= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
  180. $sql.= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
  181. $sql.= ', el.fk_source';
  182. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f';
  183. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
  184. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
  185. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'"; // TODO remplacer par une fonction
  186. $sql.= ' WHERE f.rowid='.$rowid;
  187. dol_syslog("FactureRec::Fetch rowid=".$rowid.", societe_id=".$socid." sql=".$sql, LOG_DEBUG);
  188. $result = $this->db->query($sql);
  189. if ($result)
  190. {
  191. if ($this->db->num_rows($result))
  192. {
  193. $obj = $this->db->fetch_object($result);
  194. $this->id = $rowid;
  195. $this->titre = $obj->titre;
  196. $this->ref = $obj->titre;
  197. $this->ref_client = $obj->ref_client;
  198. $this->type = $obj->type;
  199. $this->datep = $obj->dp;
  200. $this->date = $obj->df;
  201. $this->amount = $obj->amount;
  202. $this->remise_percent = $obj->remise_percent;
  203. $this->remise_absolue = $obj->remise_absolue;
  204. $this->remise = $obj->remise;
  205. $this->total_ht = $obj->total;
  206. $this->total_tva = $obj->tva;
  207. $this->total_ttc = $obj->total_ttc;
  208. $this->paye = $obj->paye;
  209. $this->close_code = $obj->close_code;
  210. $this->close_note = $obj->close_note;
  211. $this->socid = $obj->fk_soc;
  212. $this->statut = $obj->fk_statut;
  213. $this->date_lim_reglement = $this->db->jdate($obj->dlr);
  214. $this->mode_reglement_id = $obj->fk_mode_reglement;
  215. $this->mode_reglement_code = $obj->mode_reglement_code;
  216. $this->mode_reglement = $obj->mode_reglement_libelle;
  217. $this->cond_reglement_id = $obj->fk_cond_reglement;
  218. $this->cond_reglement_code = $obj->cond_reglement_code;
  219. $this->cond_reglement = $obj->cond_reglement_libelle;
  220. $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
  221. $this->fk_project = $obj->fk_projet;
  222. $this->fk_facture_source = $obj->fk_facture_source;
  223. $this->note = $obj->note;
  224. $this->note_public = $obj->note_public;
  225. $this->user_author = $obj->fk_user_author;
  226. $this->modelpdf = $obj->model_pdf;
  227. $this->rang = $obj->rang;
  228. $this->special_code = $obj->special_code;
  229. $this->commande_id = $obj->fk_commande;
  230. if ($this->commande_id)
  231. {
  232. $sql = "SELECT ref";
  233. $sql.= " FROM ".MAIN_DB_PREFIX."commande";
  234. $sql.= " WHERE rowid = ".$this->commande_id;
  235. $resqlcomm = $this->db->query($sql);
  236. if ($resqlcomm)
  237. {
  238. $objc = $this->db->fetch_object($resqlcomm);
  239. $this->commande_ref = $objc->ref;
  240. $this->db->free($resqlcomm);
  241. }
  242. }
  243. if ($this->statut == 0) $this->brouillon = 1;
  244. /*
  245. * Lines
  246. */
  247. $result=$this->fetch_lines();
  248. if ($result < 0)
  249. {
  250. $this->error=$this->db->error();
  251. dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
  252. return -3;
  253. }
  254. return 1;
  255. }
  256. else
  257. {
  258. $this->error='Bill with id '.$rowid.' not found sql='.$sql;
  259. dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
  260. return -2;
  261. }
  262. }
  263. else
  264. {
  265. $this->error=$this->db->error();
  266. dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
  267. return -1;
  268. }
  269. }
  270. /**
  271. * \brief Recupere les lignes de factures predefinies dans this->lines
  272. * \return int 1 if OK, < 0 if KO
  273. */
  274. function fetch_lines()
  275. {
  276. $sql = 'SELECT l.rowid, l.fk_product, l.product_type, l.description, l.price, l.qty, l.tva_tx, ';
  277. $sql.= ' l.remise, l.remise_percent, l.subprice,';
  278. $sql.= ' l.total_ht, l.total_tva, l.total_ttc,';
  279. $sql.= ' l.rang, l.special_code,';
  280. $sql.= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as label, p.description as product_desc';
  281. $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
  282. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
  283. $sql.= ' WHERE l.fk_facture = '.$this->id;
  284. dol_syslog('Facture::fetch_lines', LOG_DEBUG);
  285. $result = $this->db->query($sql);
  286. if ($result)
  287. {
  288. $num = $this->db->num_rows($result);
  289. $i = 0;
  290. while ($i < $num)
  291. {
  292. $objp = $this->db->fetch_object($result);
  293. $line = new FactureLigne($this->db);
  294. $line->rowid = $objp->rowid;
  295. $line->desc = $objp->description; // Description line
  296. $line->product_type = $objp->product_type; // Type of line
  297. $line->product_ref = $objp->product_ref; // Ref product
  298. $line->libelle = $objp->label; // Label product
  299. $line->product_desc = $objp->product_desc; // Description product
  300. $line->fk_product_type = $objp->fk_product_type; // Type of product
  301. $line->qty = $objp->qty;
  302. $line->subprice = $objp->subprice;
  303. $line->tva_tx = $objp->tva_tx;
  304. $line->remise_percent = $objp->remise_percent;
  305. $line->fk_remise_except = $objp->fk_remise_except;
  306. $line->fk_product = $objp->fk_product;
  307. $line->date_start = $objp->date_start;
  308. $line->date_end = $objp->date_end;
  309. $line->date_start = $objp->date_start;
  310. $line->date_end = $objp->date_end;
  311. $line->info_bits = $objp->info_bits;
  312. $line->total_ht = $objp->total_ht;
  313. $line->total_tva = $objp->total_tva;
  314. $line->total_ttc = $objp->total_ttc;
  315. $line->export_compta = $objp->fk_export_compta;
  316. $line->code_ventilation = $objp->fk_code_ventilation;
  317. $line->rang = $objp->rang;
  318. $line->special_code = $objp->special_code;
  319. // Ne plus utiliser
  320. $line->price = $objp->price;
  321. $line->remise = $objp->remise;
  322. $this->lines[$i] = $line;
  323. $i++;
  324. }
  325. $this->db->free($result);
  326. return 1;
  327. }
  328. else
  329. {
  330. $this->error=$this->db->error();
  331. dol_syslog('Facture::fetch_lines: Error '.$this->error, LOG_ERR);
  332. return -3;
  333. }
  334. }
  335. /**
  336. * Delete current invoice
  337. * @return int <0 if KO, >0 if OK
  338. */
  339. function delete()
  340. {
  341. $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".$this->id;
  342. dol_syslog($sql);
  343. if ($this->db->query($sql))
  344. {
  345. $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".$this->id;
  346. dol_syslog($sql);
  347. if ($this->db->query($sql))
  348. {
  349. return 1;
  350. }
  351. else
  352. {
  353. $this->error=$this->db->lasterror();
  354. return -1;
  355. }
  356. }
  357. else
  358. {
  359. $this->error=$this->db->lasterror();
  360. return -2;
  361. }
  362. }
  363. /**
  364. * \brief Add a line to invoice
  365. */
  366. function addline($facid, $desc, $pu_ht, $qty, $txtva, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except='', $pu_ttc=0, $type=0, $rang=-1, $special_code=0)
  367. {
  368. dol_syslog("FactureRec::addline facid=$facid,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva,fk_product=$fk_product,remise_percent=$remise_percent,date_start=$date_start,date_end=$date_end,ventil=$ventil,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type", LOG_DEBUG);
  369. include_once(DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php');
  370. // Check parameters
  371. if ($type < 0) return -1;
  372. if ($this->brouillon)
  373. {
  374. // Clean parameters
  375. $remise_percent=price2num($remise_percent);
  376. $qty=price2num($qty);
  377. if (! $qty) $qty=1;
  378. if (! $ventil) $ventil=0;
  379. if (! $info_bits) $info_bits=0;
  380. $pu_ht=price2num($pu_ht);
  381. $pu_ttc=price2num($pu_ttc);
  382. $txtva=price2num($txtva);
  383. if ($price_base_type=='HT')
  384. {
  385. $pu=$pu_ht;
  386. }
  387. else
  388. {
  389. $pu=$pu_ttc;
  390. }
  391. // Calcul du total TTC et de la TVA pour la ligne a partir de
  392. // qty, pu, remise_percent et txtva
  393. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  394. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  395. $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits);
  396. $total_ht = $tabprice[0];
  397. $total_tva = $tabprice[1];
  398. $total_ttc = $tabprice[2];
  399. // TODO A virer
  400. // Anciens indicateurs: $price, $remise (a ne plus utiliser)
  401. if (trim(dol_strlen($remise_percent)) > 0)
  402. {
  403. $remise = round(($pu * $remise_percent / 100), 2);
  404. $price = $pu - $remise;
  405. }
  406. $product_type=$type;
  407. if ($fk_product)
  408. {
  409. $product=new Product($this->db);
  410. $result=$product->fetch($fk_product);
  411. $product_type=$product->type;
  412. }
  413. $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (";
  414. $sql.= "fk_facture";
  415. $sql.= ", description";
  416. $sql.= ", price";
  417. $sql.= ", qty";
  418. $sql.= ", tva_tx";
  419. $sql.= ", fk_product";
  420. $sql.= ", product_type";
  421. $sql.= ", remise_percent";
  422. $sql.= ", subprice";
  423. $sql.= ", remise";
  424. $sql.= ", total_ht";
  425. $sql.= ", total_tva";
  426. $sql.= ", total_ttc";
  427. $sql.= ", rang";
  428. $sql.= ", special_code";
  429. $sql.= ") VALUES (";
  430. $sql.= "'".$facid."'";
  431. $sql.= ", '".$this->db->escape($desc)."'";
  432. $sql.= ", ".price2num($price);
  433. $sql.= ", ".price2num($qty);
  434. $sql.= ", ".price2num($txtva);
  435. $sql.= ", ".($fk_product?"'".$fk_product."'":"null");
  436. $sql.= ", ".$product_type;
  437. $sql.= ", '".price2num($remise_percent)."'";
  438. $sql.= ", '".price2num($pu_ht)."'";
  439. $sql.= ", '".price2num($remise)."'";
  440. $sql.= ", '".price2num($total_ht)."'";
  441. $sql.= ", '".price2num($total_tva)."'";
  442. $sql.= ", '".price2num($total_ttc)."'";
  443. $sql.= ", ".$rang;
  444. $sql.= ", ".$special_code.")";
  445. dol_syslog("FactureRec::addline sql=".$sql, LOG_DEBUG);
  446. if ($this->db->query( $sql))
  447. {
  448. $this->id=$facid; // TODO A virer
  449. $this->update_price();
  450. return 1;
  451. }
  452. else
  453. {
  454. $this->error=$this->db->lasterror();
  455. dol_syslog("FactureRec::addline sql=".$this->error, LOG_ERR);
  456. return -1;
  457. }
  458. }
  459. }
  460. /**
  461. * Rend la facture automatique
  462. * @param user
  463. * @param freq
  464. * @param courant
  465. */
  466. function set_auto($user, $freq, $courant)
  467. {
  468. if ($user->rights->facture->creer)
  469. {
  470. $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec ";
  471. $sql .= " SET frequency = '".$freq."', last_gen='".$courant."'";
  472. $sql .= " WHERE rowid = ".$this->id;
  473. $resql = $this->db->query($sql);
  474. if ($resql)
  475. {
  476. $this->frequency = $freq;
  477. $this->last_gen = $courant;
  478. return 0;
  479. }
  480. else
  481. {
  482. dol_print_error($this->db);
  483. return -1;
  484. }
  485. }
  486. else
  487. {
  488. return -2;
  489. }
  490. }
  491. /**
  492. * \brief Renvoie nom clicable (avec eventuellement le picto)
  493. * \param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  494. * \param option Sur quoi pointe le lien ('', 'withdraw')
  495. * \return string Chaine avec URL
  496. */
  497. function getNomUrl($withpicto=0,$option='')
  498. {
  499. global $langs;
  500. $result='';
  501. $lien = '<a href="'.DOL_URL_ROOT.'/compta/facture/fiche-rec.php?facid='.$this->id.'">';
  502. $lienfin='</a>';
  503. $picto='bill';
  504. $label=$langs->trans("ShowInvoice").': '.$this->ref;
  505. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  506. if ($withpicto && $withpicto != 2) $result.=' ';
  507. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  508. return $result;
  509. }
  510. }
  511. ?>