Facturation.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. /**
  38. * @var int ID
  39. */
  40. public $id;
  41. protected $ref;
  42. protected $qte;
  43. protected $stock;
  44. protected $remise_percent;
  45. protected $montant_remise;
  46. protected $prix;
  47. protected $tva;
  48. /**
  49. * Attributs persistants : utilises pour toute la duree de la vente (jusqu'a validation ou annulation)
  50. * string $num_facture => Numero de la facture (de la forme FAYYMM-XXXX)
  51. * string $mode_reglement => Mode de reglement (ESP, CB ou CHQ)
  52. * int $montant_encaisse => Montant encaisse en cas de reglement en especes
  53. * int $montant_rendu => Monnaie rendue en cas de reglement en especes
  54. * int $paiement_le => Date de paiement en cas de paiement differe
  55. *
  56. * int $prix_total_ht => Prix total hors taxes
  57. * int $montant_tva => Montant total de la TVA, tous taux confondus
  58. * int $prix_total_ttc => Prix total TTC
  59. */
  60. protected $num_facture;
  61. protected $mode_reglement;
  62. protected $montant_encaisse;
  63. protected $montant_rendu;
  64. protected $paiement_le;
  65. protected $prix_total_ht;
  66. protected $montant_tva;
  67. protected $prix_total_ttc;
  68. /**
  69. * Constructor
  70. */
  71. public function __construct()
  72. {
  73. $this->raz();
  74. $this->razPers();
  75. }
  76. // Methodes de traitement des donnees
  77. /**
  78. * Add a product into cart
  79. *
  80. * @return void
  81. */
  82. public function ajoutArticle()
  83. {
  84. global $conf,$db,$mysoc;
  85. $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
  86. $societe = new Societe($db);
  87. $societe->fetch($thirdpartyid);
  88. $product = new Product($db);
  89. $product->fetch($this->id);
  90. $vatrowid = $this->tva();
  91. $tmp = getTaxesFromId($vatrowid);
  92. $txtva = $tmp['rate'].(empty($tmp['code'])?'':' ('.$tmp['code'].')');
  93. $vat_npr = $tmp['npr'];
  94. $localtaxarray = getLocalTaxesFromRate($vatrowid, 0, $societe, $mysoc, 1);
  95. // Clean vat code
  96. $vat_src_code='';
  97. if (preg_match('/\((.*)\)/', $txtva, $reg))
  98. {
  99. $vat_src_code = $reg[1];
  100. $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
  101. }
  102. // Define part of HT, VAT, TTC
  103. $resultarray=calcul_price_total($this->qte, $this->prix(), $this->remisePercent(), $txtva, -1, -1, 0, 'HT', $vat_npr, $product->type, $mysoc, $localtaxarray);
  104. // Calcul du total ht sans remise
  105. $total_ht = $resultarray[0];
  106. $total_vat = $resultarray[1];
  107. $total_ttc = $resultarray[2];
  108. $total_localtax1 = $resultarray[9];
  109. $total_localtax2 = $resultarray[10];
  110. // Calcul du montant de la remise
  111. if ($this->remisePercent())
  112. {
  113. $remise_percent = $this->remisePercent();
  114. } else {
  115. $remise_percent = 0;
  116. }
  117. $montant_remise_ht = ($resultarray[6] - $resultarray[0]);
  118. $this->montantRemise($montant_remise_ht);
  119. $newcartarray=$_SESSION['poscart'];
  120. $i=count($newcartarray);
  121. $newcartarray[$i]['id']=$i;
  122. $newcartarray[$i]['ref']=$product->ref;
  123. $newcartarray[$i]['label']=$product->label;
  124. $newcartarray[$i]['price']=$product->price;
  125. $newcartarray[$i]['price_ttc']=$product->price_ttc;
  126. if (! empty($conf->global->PRODUIT_MULTIPRICES))
  127. {
  128. if (isset($product->multiprices[$societe->price_level]))
  129. {
  130. $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level];
  131. $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level];
  132. }
  133. }
  134. $newcartarray[$i]['fk_article']=$this->id;
  135. $newcartarray[$i]['qte']=$this->qte();
  136. $newcartarray[$i]['fk_tva']=$this->tva(); // Vat rowid
  137. $newcartarray[$i]['remise_percent']=$remise_percent;
  138. $newcartarray[$i]['remise']=price2num($montant_remise_ht);
  139. $newcartarray[$i]['total_ht']=price2num($total_ht,'MT');
  140. $newcartarray[$i]['total_ttc']=price2num($total_ttc,'MT');
  141. $newcartarray[$i]['total_vat']=price2num($total_vat, 'MT');
  142. $newcartarray[$i]['total_localtax1']=price2num($total_localtax1, 'MT');
  143. $newcartarray[$i]['total_localtax2']=price2num($total_localtax2, 'MT');
  144. $_SESSION['poscart']=$newcartarray;
  145. $this->raz();
  146. }
  147. /**
  148. * Remove a product from panel
  149. *
  150. * @param int $aArticle Id of line into cart to remove
  151. * @return void
  152. */
  153. public function supprArticle($aArticle)
  154. {
  155. $poscart=$_SESSION['poscart'];
  156. $j=0;
  157. $newposcart=array();
  158. foreach($poscart as $key => $val)
  159. {
  160. if ($poscart[$key]['id'] != $aArticle)
  161. {
  162. $newposcart[$j]=$poscart[$key];
  163. $newposcart[$j]['id']=$j;
  164. $j++;
  165. }
  166. }
  167. unset($poscart);
  168. //var_dump($poscart);exit;
  169. $_SESSION['poscart']=$newposcart;
  170. }
  171. /**
  172. * Calcul du total HT, total TTC et montants TVA
  173. *
  174. * @return int Total
  175. */
  176. public function calculTotaux()
  177. {
  178. global $db;
  179. $total_ht=0;
  180. $total_ttc=0;
  181. $total_vat = 0;
  182. $total_localtax1 = 0;
  183. $total_localtax2 = 0;
  184. $tab = (! empty($_SESSION['poscart'])?$_SESSION['poscart']:array());
  185. $tab_size=count($tab);
  186. for($i=0;$i < $tab_size;$i++)
  187. {
  188. // Total HT
  189. $remise = $tab[$i]['remise'];
  190. $total_ht += ($tab[$i]['total_ht']);
  191. $total_vat += ($tab[$i]['total_vat']);
  192. $total_ttc += ($tab[$i]['total_ttc']);
  193. $total_localtax1 += ($tab[$i]['total_localtax1']);
  194. $total_localtax2 += ($tab[$i]['total_localtax2']);
  195. }
  196. $this->prix_total_ttc = $total_ttc;
  197. $this->prix_total_ht = $total_ht;
  198. $this->prix_total_vat = $total_vat;
  199. $this->prix_total_localtax1 = $total_localtax1;
  200. $this->prix_total_localtax2 = $total_localtax2;
  201. $this->montant_tva = $total_ttc - $total_ht;
  202. //print 'total: '.$this->prix_total_ttc; exit;
  203. }
  204. /**
  205. * Reinitialisation des attributs
  206. *
  207. * @return void
  208. */
  209. public function raz()
  210. {
  211. $this->id('RESET');
  212. $this->ref('RESET');
  213. $this->qte('RESET');
  214. $this->stock('RESET');
  215. $this->remisePercent('RESET');
  216. $this->montantRemise('RESET');
  217. $this->prix('RESET');
  218. $this->tva('RESET');
  219. }
  220. /**
  221. * Reinitialisation des attributs persistants
  222. *
  223. * @return void
  224. */
  225. private function razPers()
  226. {
  227. $this->numInvoice('RESET');
  228. $this->getSetPaymentMode('RESET');
  229. $this->montantEncaisse('RESET');
  230. $this->montantRendu('RESET');
  231. $this->paiementLe('RESET');
  232. $this->prixTotalHt('RESET');
  233. $this->montantTva('RESET');
  234. $this->prixTotalTtc('RESET');
  235. }
  236. // Methodes de modification des attributs proteges
  237. /**
  238. * Getter for id
  239. *
  240. * @param int $aId Id
  241. * @return id
  242. */
  243. public function id($aId = null)
  244. {
  245. if ( !$aId )
  246. {
  247. return $this->id;
  248. }
  249. elseif ( $aId == 'RESET' )
  250. {
  251. $this->id = null;
  252. }
  253. else
  254. {
  255. $this->id = $aId;
  256. }
  257. }
  258. /**
  259. * Getter for ref
  260. *
  261. * @param string $aRef Ref
  262. * @return string Ref
  263. */
  264. public function ref($aRef = null)
  265. {
  266. if (is_null($aRef))
  267. {
  268. return $this->ref;
  269. }
  270. elseif ( $aRef == 'RESET' )
  271. {
  272. $this->ref = null;
  273. }
  274. else
  275. {
  276. $this->ref = $aRef;
  277. }
  278. }
  279. /**
  280. * Getter for qte
  281. *
  282. * @param int $aQte Qty
  283. * @return int Qty
  284. */
  285. public function qte($aQte = null)
  286. {
  287. if (is_null($aQte))
  288. {
  289. return $this->qte;
  290. }
  291. elseif ( $aQte == 'RESET' )
  292. {
  293. $this->qte = null;
  294. }
  295. else
  296. {
  297. $this->qte = $aQte;
  298. }
  299. }
  300. /**
  301. * Getter for stock
  302. *
  303. * @param string $aStock Stock
  304. * @return string Stock
  305. */
  306. public function stock($aStock = null)
  307. {
  308. if (is_null($aStock))
  309. {
  310. return $this->stock;
  311. }
  312. elseif ( $aStock == 'RESET' )
  313. {
  314. $this->stock = null;
  315. }
  316. else
  317. {
  318. $this->stock = $aStock;
  319. }
  320. }
  321. /**
  322. * Getter for remise_percent
  323. *
  324. * @param string $aRemisePercent Discount
  325. * @return string Discount
  326. */
  327. public function remisePercent($aRemisePercent = null)
  328. {
  329. if (is_null($aRemisePercent))
  330. {
  331. return $this->remise_percent;
  332. }
  333. elseif ($aRemisePercent == 'RESET')
  334. {
  335. $this->remise_percent = null;
  336. }
  337. else
  338. {
  339. $this->remise_percent = $aRemisePercent;
  340. }
  341. }
  342. /**
  343. * Getter for montant_remise
  344. *
  345. * @param int $aMontantRemise Amount
  346. * @return string Amount
  347. */
  348. public function montantRemise($aMontantRemise = null)
  349. {
  350. if (is_null($aMontantRemise)) {
  351. return $this->montant_remise;
  352. } elseif ( $aMontantRemise == 'RESET' ) {
  353. $this->montant_remise = null;
  354. } else {
  355. $this->montant_remise = $aMontantRemise;
  356. }
  357. }
  358. /**
  359. * Getter for prix
  360. *
  361. * @param int $aPrix Price
  362. * @return string Stock
  363. */
  364. public function prix($aPrix = null)
  365. {
  366. if (is_null($aPrix)) {
  367. return $this->prix;
  368. } elseif ( $aPrix == 'RESET' ) {
  369. $this->prix = null;
  370. } else {
  371. $this->prix = $aPrix;
  372. }
  373. }
  374. /**
  375. * Getter for tva
  376. *
  377. * @param int $aTva Vat
  378. * @return int Vat
  379. */
  380. public function tva($aTva = null)
  381. {
  382. if (is_null($aTva)) {
  383. return $this->tva;
  384. } elseif ( $aTva == 'RESET' ) {
  385. $this->tva = null;
  386. } else {
  387. $this->tva = $aTva;
  388. }
  389. }
  390. /**
  391. * Get num invoice
  392. *
  393. * @param string $aNumFacture Invoice ref
  394. * @return string Invoice ref
  395. */
  396. public function numInvoice($aNumFacture = null)
  397. {
  398. if (is_null($aNumFacture)) {
  399. return $this->num_facture;
  400. } elseif ( $aNumFacture == 'RESET' ) {
  401. $this->num_facture = null;
  402. } else {
  403. $this->num_facture = $aNumFacture;
  404. }
  405. }
  406. /**
  407. * Get payment mode
  408. *
  409. * @param int $aModeReglement Payment mode
  410. * @return int Payment mode
  411. */
  412. public function getSetPaymentMode($aModeReglement = null)
  413. {
  414. if (is_null($aModeReglement)) {
  415. return $this->mode_reglement;
  416. } elseif ( $aModeReglement == 'RESET' ) {
  417. $this->mode_reglement = null;
  418. } else {
  419. $this->mode_reglement = $aModeReglement;
  420. }
  421. }
  422. /**
  423. * Get amount
  424. *
  425. * @param int $aMontantEncaisse Amount
  426. * @return int Amount
  427. */
  428. public function montantEncaisse($aMontantEncaisse = null)
  429. {
  430. if (is_null($aMontantEncaisse)) {
  431. return $this->montant_encaisse;
  432. } elseif ( $aMontantEncaisse == 'RESET' ) {
  433. $this->montant_encaisse = null;
  434. } else {
  435. $this->montant_encaisse = $aMontantEncaisse;
  436. }
  437. }
  438. /**
  439. * Get amount
  440. *
  441. * @param int $aMontantRendu Amount
  442. * @return int Amount
  443. */
  444. public function montantRendu($aMontantRendu = null)
  445. {
  446. if (is_null($aMontantRendu)) {
  447. return $this->montant_rendu;
  448. } elseif ( $aMontantRendu == 'RESET' ) {
  449. $this->montant_rendu = null;
  450. } else {
  451. $this->montant_rendu = $aMontantRendu;
  452. }
  453. }
  454. /**
  455. * Get payment date
  456. *
  457. * @param date $aPaiementLe Date
  458. * @return date Date
  459. */
  460. public function paiementLe($aPaiementLe = null)
  461. {
  462. if (is_null($aPaiementLe)) {
  463. return $this->paiement_le;
  464. } elseif ( $aPaiementLe == 'RESET' ) {
  465. $this->paiement_le = null;
  466. } else {
  467. $this->paiement_le = $aPaiementLe;
  468. }
  469. }
  470. /**
  471. * Get totla HT
  472. *
  473. * @param int $aTotalHt Total amount
  474. * @return int Total amount
  475. */
  476. public function prixTotalHt($aTotalHt = null)
  477. {
  478. if (is_null($aTotalHt)) {
  479. return $this->prix_total_ht;
  480. } elseif ( $aTotalHt == 'RESET' ) {
  481. $this->prix_total_ht = null;
  482. } else {
  483. $this->prix_total_ht = $aTotalHt;
  484. }
  485. }
  486. /**
  487. * Get amount vat
  488. *
  489. * @param int $aMontantTva Amount vat
  490. * @return int Amount vat
  491. */
  492. public function montantTva($aMontantTva = null)
  493. {
  494. if (is_null($aMontantTva)) {
  495. return $this->montant_tva;
  496. } elseif ( $aMontantTva == 'RESET' ) {
  497. $this->montant_tva = null;
  498. } else {
  499. $this->montant_tva = $aMontantTva;
  500. }
  501. }
  502. /**
  503. * Get total TTC
  504. *
  505. * @param int $aTotalTtc Amount ttc
  506. * @return int Amount ttc
  507. */
  508. public function prixTotalTtc($aTotalTtc = null)
  509. {
  510. if (is_null($aTotalTtc))
  511. {
  512. return $this->prix_total_ttc;
  513. }
  514. elseif ( $aTotalTtc == 'RESET' )
  515. {
  516. $this->prix_total_ttc = null;
  517. }
  518. else
  519. {
  520. $this->prix_total_ttc = $aTotalTtc;
  521. }
  522. }
  523. }