Facturation.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. /* Copyright (C) 2007-2008 Jeremie Ollivier <jeremie.o@laposte.net>
  3. * Copyright (C) 2008-2010 Laurent Destailleur <eldy@uers.sourceforge.net>
  4. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  20. /**
  21. * Class ot manage invoices for pos module (cashdesk)
  22. */
  23. class Facturation
  24. {
  25. /**
  26. * Attributs "volatiles" : reinitialises apres chaque traitement d'un article
  27. * <p>Attributs "volatiles" : reinitialises apres chaque traitement d'un article</p>
  28. * int $id => 'rowid' du produit dans llx_product
  29. * string $ref => 'ref' du produit dans llx_product
  30. * int $qte => Quantite pour le produit en cours de traitement
  31. * int $stock => Stock theorique pour le produit en cours de traitement
  32. * int $remise_percent => Remise en pourcent sur le produit en cours
  33. * int $montant_remise => Remise en pourcent sur le produit en cours
  34. * int $prix => Prix HT du produit en cours
  35. * int $tva => 'rowid' du taux de tva dans llx_c_tva
  36. */
  37. var $id;
  38. protected $ref;
  39. protected $qte;
  40. protected $stock;
  41. protected $remise_percent;
  42. protected $montant_remise;
  43. protected $prix;
  44. protected $tva;
  45. /**
  46. * Attributs persistants : utilises pour toute la duree de la vente (jusqu'a validation ou annulation)
  47. * string $num_facture => Numero de la facture (de la forme FAYYMM-XXXX)
  48. * string $mode_reglement => Mode de reglement (ESP, CB ou CHQ)
  49. * int $montant_encaisse => Montant encaisse en cas de reglement en especes
  50. * int $montant_rendu => Monnaie rendue en cas de reglement en especes
  51. * int $paiement_le => Date de paiement en cas de paiement differe
  52. *
  53. * int $prix_total_ht => Prix total hors taxes
  54. * int $montant_tva => Montant total de la TVA, tous taux confondus
  55. * int $prix_total_ttc => Prix total TTC
  56. */
  57. protected $num_facture;
  58. protected $mode_reglement;
  59. protected $montant_encaisse;
  60. protected $montant_rendu;
  61. protected $paiement_le;
  62. protected $prix_total_ht;
  63. protected $montant_tva;
  64. protected $prix_total_ttc;
  65. /**
  66. * Constructor
  67. */
  68. public function __construct()
  69. {
  70. $this->raz();
  71. $this->razPers();
  72. }
  73. // Methodes de traitement des donnees
  74. /**
  75. * Add a product into cart
  76. *
  77. * @return void
  78. */
  79. public function ajoutArticle()
  80. {
  81. global $conf,$db;
  82. $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
  83. $societe = new Societe($db);
  84. $societe->fetch($thirdpartyid);
  85. $product = new Product($db);
  86. $product->fetch($this->id);
  87. $sql = "SELECT taux";
  88. $sql.= " FROM ".MAIN_DB_PREFIX."c_tva";
  89. $sql.= " WHERE rowid = ".$this->tva();
  90. dol_syslog("ajoutArticle sql=".$sql);
  91. $resql = $db->query($sql);
  92. if ($resql)
  93. {
  94. $obj = $db->fetch_object($resql);
  95. $vat_rate=$obj->taux;
  96. //var_dump($vat_rate);exit;
  97. }
  98. else
  99. {
  100. dol_print_error($db);
  101. }
  102. // Define part of HT, VAT, TTC
  103. $resultarray=calcul_price_total($this->qte,$this->prix(),$this->remisePercent(),$vat_rate,0,0,0,'HT',0,$product->type,0);
  104. // Calcul du total ht sans remise
  105. $total_ht = $resultarray[0];
  106. $total_vat = $resultarray[1];
  107. $total_ttc = $resultarray[2];
  108. // Calcul du montant de la remise
  109. if ($this->remisePercent())
  110. {
  111. $remise_percent = $this->remisePercent();
  112. } else {
  113. $remise_percent = 0;
  114. }
  115. $montant_remise_ht = ($resultarray[6] - $resultarray[0]);
  116. $this->montantRemise($montant_remise_ht);
  117. $newcartarray=$_SESSION['poscart'];
  118. $i=count($newcartarray);
  119. $newcartarray[$i]['id']=$i;
  120. $newcartarray[$i]['ref']=$product->ref;
  121. $newcartarray[$i]['label']=$product->label;
  122. $newcartarray[$i]['price']=$product->price;
  123. $newcartarray[$i]['price_ttc']=$product->price_ttc;
  124. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  125. {
  126. if (isset($product->multiprices[$societe->price_level]))
  127. {
  128. $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level];
  129. $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level];
  130. }
  131. }
  132. $newcartarray[$i]['fk_article']=$this->id;
  133. $newcartarray[$i]['qte']=$this->qte();
  134. $newcartarray[$i]['fk_tva']=$this->tva();
  135. $newcartarray[$i]['remise_percent']=$remise_percent;
  136. $newcartarray[$i]['remise']=price2num($montant_remise_ht);
  137. $newcartarray[$i]['total_ht']=price2num($total_ht,'MT');
  138. $newcartarray[$i]['total_ttc']=price2num($total_ttc,'MT');
  139. $_SESSION['poscart']=$newcartarray;
  140. $this->raz();
  141. }
  142. /**
  143. * Remove a product from panel
  144. *
  145. * @param int $aArticle Id of line into cart to remove
  146. * @return void
  147. */
  148. public function supprArticle($aArticle)
  149. {
  150. $poscart=$_SESSION['poscart'];
  151. $j=0;
  152. $newposcart=array();
  153. foreach($poscart as $key => $val)
  154. {
  155. if ($poscart[$key]['id'] != $aArticle)
  156. {
  157. $newposcart[$j]=$poscart[$key];
  158. $newposcart[$j]['id']=$j;
  159. $j++;
  160. }
  161. }
  162. unset($poscart);
  163. //var_dump($poscart);exit;
  164. $_SESSION['poscart']=$newposcart;
  165. }
  166. /**
  167. * Calcul du total HT, total TTC et montants TVA
  168. *
  169. * @return int Total
  170. */
  171. public function calculTotaux()
  172. {
  173. global $db;
  174. $total_ht=0;
  175. $total_ttc=0;
  176. $tab=array();
  177. $tab = $_SESSION['poscart'];
  178. $tab_size=count($tab);
  179. for($i=0;$i < $tab_size;$i++)
  180. {
  181. // Total HT
  182. $remise = $tab[$i]['remise'];
  183. $total_ht += ($tab[$i]['total_ht']);
  184. $total_ttc += ($tab[$i]['total_ttc']);
  185. }
  186. $this->prix_total_ttc = $total_ttc;
  187. $this->prix_total_ht = $total_ht;
  188. $this->montant_tva = $total_ttc - $total_ht;
  189. //print $this->prix_total_ttc.'eeee'; exit;
  190. }
  191. /**
  192. * Reinitialisation des attributs
  193. *
  194. * @return void
  195. */
  196. public function raz()
  197. {
  198. $this->id('RESET');
  199. $this->ref('RESET');
  200. $this->qte('RESET');
  201. $this->stock('RESET');
  202. $this->remisePercent('RESET');
  203. $this->montantRemise('RESET');
  204. $this->prix('RESET');
  205. $this->tva('RESET');
  206. }
  207. /**
  208. * Reinitialisation des attributs persistants
  209. *
  210. * @return void
  211. */
  212. private function razPers()
  213. {
  214. $this->numInvoice('RESET');
  215. $this->getSetPaymentMode('RESET');
  216. $this->montantEncaisse('RESET');
  217. $this->montantRendu('RESET');
  218. $this->paiementLe('RESET');
  219. $this->prixTotalHt('RESET');
  220. $this->montantTva('RESET');
  221. $this->prixTotalTtc('RESET');
  222. }
  223. // Methodes de modification des attributs proteges
  224. /**
  225. * Getter for id
  226. *
  227. * @param int $aId Id
  228. * @return id
  229. */
  230. public function id($aId=null)
  231. {
  232. if ( !$aId )
  233. {
  234. return $this->id;
  235. }
  236. else if ( $aId == 'RESET' )
  237. {
  238. $this->id = NULL;
  239. }
  240. else
  241. {
  242. $this->id = $aId;
  243. }
  244. }
  245. /**
  246. * Getter for ref
  247. *
  248. * @param string $aRef Ref
  249. * @return string Ref
  250. */
  251. public function ref($aRef=null)
  252. {
  253. if ( !$aRef )
  254. {
  255. return $this->ref;
  256. }
  257. else if ( $aRef == 'RESET' )
  258. {
  259. $this->ref = NULL;
  260. }
  261. else
  262. {
  263. $this->ref = $aRef;
  264. }
  265. }
  266. /**
  267. * Getter for qte
  268. *
  269. * @param int $aQte Qty
  270. * @return int Qty
  271. */
  272. public function qte( $aQte=null )
  273. {
  274. if ( !$aQte )
  275. {
  276. return $this->qte;
  277. }
  278. else if ( $aQte == 'RESET' )
  279. {
  280. $this->qte = NULL;
  281. }
  282. else
  283. {
  284. $this->qte = $aQte;
  285. }
  286. }
  287. /**
  288. * Getter for stock
  289. *
  290. * @param string $aStock Stock
  291. * @return string Stock
  292. */
  293. public function stock($aStock=null)
  294. {
  295. if ( !$aStock )
  296. {
  297. return $this->stock;
  298. }
  299. else if ( $aStock == 'RESET' )
  300. {
  301. $this->stock = NULL;
  302. }
  303. else
  304. {
  305. $this->stock = $aStock;
  306. }
  307. }
  308. /**
  309. * Getter for remise_percent
  310. *
  311. * @param string $aRemisePercent Discount
  312. * @return string Discount
  313. */
  314. public function remisePercent($aRemisePercent=null)
  315. {
  316. if ( !$aRemisePercent )
  317. {
  318. return $this->remise_percent;
  319. }
  320. else if ($aRemisePercent == 'RESET')
  321. {
  322. $this->remise_percent = NULL;
  323. }
  324. else
  325. {
  326. $this->remise_percent = $aRemisePercent;
  327. }
  328. }
  329. /**
  330. * Getter for montant_remise
  331. *
  332. * @param int $aMontantRemise Amount
  333. * @return string Amount
  334. */
  335. public function montantRemise($aMontantRemise=null)
  336. {
  337. if ( !$aMontantRemise ) {
  338. return $this->montant_remise;
  339. } else if ( $aMontantRemise == 'RESET' ) {
  340. $this->montant_remise = NULL;
  341. } else {
  342. $this->montant_remise = $aMontantRemise;
  343. }
  344. }
  345. /**
  346. * Getter for prix
  347. *
  348. * @param int $aPrix Price
  349. * @return string Stock
  350. */
  351. public function prix ( $aPrix=null )
  352. {
  353. if ( !$aPrix ) {
  354. return $this->prix;
  355. } else if ( $aPrix == 'RESET' ) {
  356. $this->prix = NULL;
  357. } else {
  358. $this->prix = $aPrix;
  359. }
  360. }
  361. /**
  362. * Getter for tva
  363. *
  364. * @param int $aTva Vat
  365. * @return int Vat
  366. */
  367. public function tva ( $aTva=null )
  368. {
  369. if ( !$aTva ) {
  370. return $this->tva;
  371. } else if ( $aTva == 'RESET' ) {
  372. $this->tva = NULL;
  373. } else {
  374. $this->tva = $aTva;
  375. }
  376. }
  377. /**
  378. * Get num invoice
  379. *
  380. * @param string $aNumFacture Invoice ref
  381. * @return string Invoice ref
  382. */
  383. public function numInvoice( $aNumFacture=null )
  384. {
  385. if ( !$aNumFacture ) {
  386. return $this->num_facture;
  387. } else if ( $aNumFacture == 'RESET' ) {
  388. $this->num_facture = NULL;
  389. } else {
  390. $this->num_facture = $aNumFacture;
  391. }
  392. }
  393. /**
  394. * Get payment mode
  395. *
  396. * @param int $aModeReglement Payment mode
  397. * @return int Payment mode
  398. */
  399. public function getSetPaymentMode( $aModeReglement=null )
  400. {
  401. if ( !$aModeReglement ) {
  402. return $this->mode_reglement;
  403. } else if ( $aModeReglement == 'RESET' ) {
  404. $this->mode_reglement = NULL;
  405. } else {
  406. $this->mode_reglement = $aModeReglement;
  407. }
  408. }
  409. /**
  410. * Get amount
  411. *
  412. * @param int $aMontantEncaisse Amount
  413. * @return int Amount
  414. */
  415. public function montantEncaisse( $aMontantEncaisse=null )
  416. {
  417. if ( !$aMontantEncaisse ) {
  418. return $this->montant_encaisse;
  419. } else if ( $aMontantEncaisse == 'RESET' ) {
  420. $this->montant_encaisse = NULL;
  421. } else {
  422. $this->montant_encaisse = $aMontantEncaisse;
  423. }
  424. }
  425. /**
  426. * Get amount
  427. *
  428. * @param int $aMontantRendu Amount
  429. * @return int Amount
  430. */
  431. public function montantRendu( $aMontantRendu=null )
  432. {
  433. if ( !$aMontantRendu ) {
  434. return $this->montant_rendu;
  435. } else if ( $aMontantRendu == 'RESET' ) {
  436. $this->montant_rendu = NULL;
  437. } else {
  438. $this->montant_rendu = $aMontantRendu;
  439. }
  440. }
  441. /**
  442. * Get payment date
  443. *
  444. * @param date $aPaiementLe Date
  445. * @return date Date
  446. */
  447. public function paiementLe( $aPaiementLe=null )
  448. {
  449. if ( !$aPaiementLe ) {
  450. return $this->paiement_le;
  451. } else if ( $aPaiementLe == 'RESET' ) {
  452. $this->paiement_le = NULL;
  453. } else {
  454. $this->paiement_le = $aPaiementLe;
  455. }
  456. }
  457. /**
  458. * Get totla HT
  459. *
  460. * @param int $aTotalHt Total amount
  461. * @return int Total amount
  462. */
  463. public function prixTotalHt( $aTotalHt=null )
  464. {
  465. if ( !$aTotalHt ) {
  466. return $this->prix_total_ht;
  467. } else if ( $aTotalHt == 'RESET' ) {
  468. $this->prix_total_ht = NULL;
  469. } else {
  470. $this->prix_total_ht = $aTotalHt;
  471. }
  472. }
  473. /**
  474. * Get amount vat
  475. *
  476. * @param int $aMontantTva Amount vat
  477. * @return int Amount vat
  478. */
  479. public function montantTva( $aMontantTva=null )
  480. {
  481. if ( !$aMontantTva ) {
  482. return $this->montant_tva;
  483. } else if ( $aMontantTva == 'RESET' ) {
  484. $this->montant_tva = NULL;
  485. } else {
  486. $this->montant_tva = $aMontantTva;
  487. }
  488. }
  489. /**
  490. * Get total TTC
  491. *
  492. * @param int $aTotalTtc Amount ttc
  493. * @return int Amount ttc
  494. */
  495. public function prixTotalTtc( $aTotalTtc=null )
  496. {
  497. if ( !$aTotalTtc )
  498. {
  499. return $this->prix_total_ttc;
  500. }
  501. else if ( $aTotalTtc == 'RESET' )
  502. {
  503. $this->prix_total_ttc = NULL;
  504. }
  505. else
  506. {
  507. $this->prix_total_ttc = $aTotalTtc;
  508. }
  509. }
  510. }