dolibarr  7.0.0-beta
Facturation.class.php
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 
20 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
21 
22 
27 {
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 
61  protected $num_facture;
62  protected $mode_reglement;
63  protected $montant_encaisse;
64  protected $montant_rendu;
65  protected $paiement_le;
66 
67  protected $prix_total_ht;
68  protected $montant_tva;
69  protected $prix_total_ttc;
70 
71 
75  public function __construct()
76  {
77  $this->raz();
78  $this->razPers();
79  }
80 
81 
82  // Methodes de traitement des donnees
83 
84 
90  public function ajoutArticle()
91  {
92  global $conf,$db,$mysoc;
93 
94  $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
95 
96  $societe = new Societe($db);
97  $societe->fetch($thirdpartyid);
98 
99  $product = new Product($db);
100  $product->fetch($this->id);
101 
102 
103  $vatrowid = $this->tva();
104 
105  $tmp = getTaxesFromId($vatrowid);
106  $txtva = $tmp['rate'].(empty($tmp['code'])?'':' ('.$tmp['code'].')');
107  $vat_npr = $tmp['npr'];
108 
109  $localtaxarray = getLocalTaxesFromRate($vatrowid, 0, $societe, $mysoc, 1);
110 
111  // Clean vat code
112  $vat_src_code='';
113  if (preg_match('/\((.*)\)/', $txtva, $reg))
114  {
115  $vat_src_code = $reg[1];
116  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
117  }
118 
119  // Define part of HT, VAT, TTC
120  $resultarray=calcul_price_total($this->qte, $this->prix(), $this->remisePercent(), $txtva, -1, -1, 0, 'HT', $vat_npr, $product->type, $mysoc, $localtaxarray);
121 
122  // Calcul du total ht sans remise
123  $total_ht = $resultarray[0];
124  $total_vat = $resultarray[1];
125  $total_ttc = $resultarray[2];
126  $total_localtax1 = $resultarray[9];
127  $total_localtax2 = $resultarray[10];
128 
129  // Calcul du montant de la remise
130  if ($this->remisePercent())
131  {
132  $remise_percent = $this->remisePercent();
133  } else {
134  $remise_percent = 0;
135  }
136  $montant_remise_ht = ($resultarray[6] - $resultarray[0]);
137  $this->montantRemise($montant_remise_ht);
138 
139  $newcartarray=$_SESSION['poscart'];
140  $i=count($newcartarray);
141 
142  $newcartarray[$i]['id']=$i;
143  $newcartarray[$i]['ref']=$product->ref;
144  $newcartarray[$i]['label']=$product->label;
145  $newcartarray[$i]['price']=$product->price;
146  $newcartarray[$i]['price_ttc']=$product->price_ttc;
147 
148  if (! empty($conf->global->PRODUIT_MULTIPRICES))
149  {
150  if (isset($product->multiprices[$societe->price_level]))
151  {
152  $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level];
153  $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level];
154  }
155  }
156 
157  $newcartarray[$i]['fk_article']=$this->id;
158  $newcartarray[$i]['qte']=$this->qte();
159  $newcartarray[$i]['fk_tva']=$this->tva(); // Vat rowid
160  $newcartarray[$i]['remise_percent']=$remise_percent;
161  $newcartarray[$i]['remise']=price2num($montant_remise_ht);
162  $newcartarray[$i]['total_ht']=price2num($total_ht,'MT');
163  $newcartarray[$i]['total_ttc']=price2num($total_ttc,'MT');
164  $newcartarray[$i]['total_vat']=price2num($total_vat, 'MT');
165  $newcartarray[$i]['total_localtax1']=price2num($total_localtax1, 'MT');
166  $newcartarray[$i]['total_localtax2']=price2num($total_localtax2, 'MT');
167  $_SESSION['poscart']=$newcartarray;
168 
169  $this->raz();
170 
171  }
172 
179  public function supprArticle($aArticle)
180  {
181  $poscart=$_SESSION['poscart'];
182 
183  $j=0;
184  $newposcart=array();
185  foreach($poscart as $key => $val)
186  {
187  if ($poscart[$key]['id'] != $aArticle)
188  {
189  $newposcart[$j]=$poscart[$key];
190  $newposcart[$j]['id']=$j;
191  $j++;
192  }
193  }
194  unset($poscart);
195  //var_dump($poscart);exit;
196  $_SESSION['poscart']=$newposcart;
197  }
198 
204  public function calculTotaux()
205  {
206  global $db;
207 
208  $total_ht=0;
209  $total_ttc=0;
210  $total_vat = 0;
211  $total_localtax1 = 0;
212  $total_localtax2 = 0;
213 
214  $tab=array();
215  $tab = $_SESSION['poscart'];
216 
217  $tab_size=count($tab);
218  for($i=0;$i < $tab_size;$i++)
219  {
220  // Total HT
221  $remise = $tab[$i]['remise'];
222  $total_ht += ($tab[$i]['total_ht']);
223  $total_vat += ($tab[$i]['total_vat']);
224  $total_ttc += ($tab[$i]['total_ttc']);
225  $total_localtax1 += ($tab[$i]['total_localtax1']);
226  $total_localtax2 += ($tab[$i]['total_localtax2']);
227  }
228 
229  $this->prix_total_ttc = $total_ttc;
230  $this->prix_total_ht = $total_ht;
231  $this->prix_total_vat = $total_vat;
232  $this->prix_total_localtax1 = $total_localtax1;
233  $this->prix_total_localtax2 = $total_localtax2;
234 
235  $this->montant_tva = $total_ttc - $total_ht;
236  //print $this->prix_total_ttc.'eeee'; exit;
237  }
238 
244  public function raz()
245  {
246  $this->id('RESET');
247  $this->ref('RESET');
248  $this->qte('RESET');
249  $this->stock('RESET');
250  $this->remisePercent('RESET');
251  $this->montantRemise('RESET');
252  $this->prix('RESET');
253  $this->tva('RESET');
254  }
255 
261  private function razPers()
262  {
263  $this->numInvoice('RESET');
264  $this->getSetPaymentMode('RESET');
265  $this->montantEncaisse('RESET');
266  $this->montantRendu('RESET');
267  $this->paiementLe('RESET');
268 
269  $this->prixTotalHt('RESET');
270  $this->montantTva('RESET');
271  $this->prixTotalTtc('RESET');
272 
273  }
274 
275 
276  // Methodes de modification des attributs proteges
277 
284  public function id($aId=null)
285  {
286 
287  if ( !$aId )
288  {
289  return $this->id;
290 
291  }
292  else if ( $aId == 'RESET' )
293  {
294 
295  $this->id = NULL;
296 
297  }
298  else
299  {
300 
301  $this->id = $aId;
302 
303  }
304  }
305 
312  public function ref($aRef=null)
313  {
314 
315  if (is_null($aRef))
316  {
317  return $this->ref;
318  }
319  else if ( $aRef == 'RESET' )
320  {
321  $this->ref = NULL;
322  }
323  else
324  {
325  $this->ref = $aRef;
326  }
327 
328  }
329 
336  public function qte($aQte=null)
337  {
338  if (is_null($aQte))
339  {
340  return $this->qte;
341  }
342  else if ( $aQte == 'RESET' )
343  {
344 
345  $this->qte = NULL;
346  }
347  else
348  {
349  $this->qte = $aQte;
350  }
351 
352  }
353 
360  public function stock($aStock=null)
361  {
362 
363  if (is_null($aStock))
364  {
365  return $this->stock;
366  }
367  else if ( $aStock == 'RESET' )
368  {
369  $this->stock = NULL;
370  }
371  else
372  {
373  $this->stock = $aStock;
374  }
375 
376  }
377 
384  public function remisePercent($aRemisePercent=null)
385  {
386 
387  if (is_null($aRemisePercent))
388  {
389  return $this->remise_percent;
390  }
391  else if ($aRemisePercent == 'RESET')
392  {
393  $this->remise_percent = NULL;
394  }
395  else
396  {
397  $this->remise_percent = $aRemisePercent;
398  }
399 
400  }
401 
408  public function montantRemise($aMontantRemise=null)
409  {
410 
411  if (is_null($aMontantRemise)) {
412 
413  return $this->montant_remise;
414 
415  } else if ( $aMontantRemise == 'RESET' ) {
416 
417  $this->montant_remise = NULL;
418 
419  } else {
420 
421  $this->montant_remise = $aMontantRemise;
422 
423  }
424 
425  }
426 
433  public function prix($aPrix=null)
434  {
435 
436  if (is_null($aPrix)) {
437 
438  return $this->prix;
439 
440  } else if ( $aPrix == 'RESET' ) {
441 
442  $this->prix = NULL;
443 
444  } else {
445 
446  $this->prix = $aPrix;
447 
448  }
449 
450  }
451 
458  public function tva($aTva=null)
459  {
460  if (is_null($aTva)) {
461 
462  return $this->tva;
463 
464  } else if ( $aTva == 'RESET' ) {
465 
466  $this->tva = NULL;
467 
468  } else {
469 
470  $this->tva = $aTva;
471 
472  }
473 
474  }
475 
482  public function numInvoice($aNumFacture=null)
483  {
484  if (is_null($aNumFacture)) {
485 
486  return $this->num_facture;
487 
488  } else if ( $aNumFacture == 'RESET' ) {
489 
490  $this->num_facture = NULL;
491 
492  } else {
493 
494  $this->num_facture = $aNumFacture;
495 
496  }
497  }
498 
505  public function getSetPaymentMode($aModeReglement=null)
506  {
507 
508  if (is_null($aModeReglement)) {
509 
510  return $this->mode_reglement;
511 
512  } else if ( $aModeReglement == 'RESET' ) {
513 
514  $this->mode_reglement = NULL;
515 
516  } else {
517 
518  $this->mode_reglement = $aModeReglement;
519 
520  }
521 
522  }
523 
530  public function montantEncaisse($aMontantEncaisse=null)
531  {
532 
533  if (is_null($aMontantEncaisse)) {
534 
535  return $this->montant_encaisse;
536 
537  } else if ( $aMontantEncaisse == 'RESET' ) {
538 
539  $this->montant_encaisse = NULL;
540 
541  } else {
542 
543  $this->montant_encaisse = $aMontantEncaisse;
544 
545  }
546 
547  }
548 
555  public function montantRendu($aMontantRendu=null)
556  {
557 
558  if (is_null($aMontantRendu)) {
559 
560  return $this->montant_rendu;
561  } else if ( $aMontantRendu == 'RESET' ) {
562 
563  $this->montant_rendu = NULL;
564 
565  } else {
566 
567  $this->montant_rendu = $aMontantRendu;
568 
569  }
570 
571  }
572 
579  public function paiementLe($aPaiementLe=null)
580  {
581  if (is_null($aPaiementLe)) {
582 
583  return $this->paiement_le;
584 
585  } else if ( $aPaiementLe == 'RESET' ) {
586 
587  $this->paiement_le = NULL;
588 
589  } else {
590 
591  $this->paiement_le = $aPaiementLe;
592 
593  }
594  }
595 
602  public function prixTotalHt($aTotalHt=null)
603  {
604  if (is_null($aTotalHt)) {
605 
606  return $this->prix_total_ht;
607 
608  } else if ( $aTotalHt == 'RESET' ) {
609 
610  $this->prix_total_ht = NULL;
611 
612  } else {
613 
614  $this->prix_total_ht = $aTotalHt;
615 
616  }
617  }
618 
625  public function montantTva($aMontantTva=null)
626  {
627  if (is_null($aMontantTva)) {
628 
629  return $this->montant_tva;
630 
631  } else if ( $aMontantTva == 'RESET' ) {
632 
633  $this->montant_tva = NULL;
634 
635  } else {
636 
637  $this->montant_tva = $aMontantTva;
638 
639  }
640 
641  }
642 
649  public function prixTotalTtc($aTotalTtc=null)
650  {
651  if (is_null($aTotalTtc))
652  {
653  return $this->prix_total_ttc;
654  }
655  else if ( $aTotalTtc == 'RESET' )
656  {
657  $this->prix_total_ttc = NULL;
658  }
659  else
660  {
661  $this->prix_total_ttc = $aTotalTtc;
662  }
663  }
664 
665 }
666 
ref($aRef=null)
Getter for ref.
tva($aTva=null)
Getter for tva.
__construct()
Constructor.
$num_facture
Attributs persistants : utilises pour toute la duree de la vente (jusqu'a validation ou annulation) s...
getSetPaymentMode($aModeReglement=null)
Get payment mode.
raz()
Reinitialisation des attributs.
razPers()
Reinitialisation des attributs persistants.
remisePercent($aRemisePercent=null)
Getter for remise_percent.
numInvoice($aNumFacture=null)
Get num invoice.
supprArticle($aArticle)
Remove a product from panel.
Class to manage products or services.
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get vat main information from Id.
calculTotaux()
Calcul du total HT, total TTC et montants TVA.
montantRendu($aMontantRendu=null)
Get amount.
montantRemise($aMontantRemise=null)
Getter for montant_remise.
montantEncaisse($aMontantEncaisse=null)
Get amount.
montantTva($aMontantTva=null)
Get amount vat.
paiementLe($aPaiementLe=null)
Get payment date.
qte($aQte=null)
Getter for qte.
Class to manage third parties objects (customers, suppliers, prospects...)
Class ot manage invoices for pos module (cashdesk)
id($aId=null)
Getter for id.
prixTotalTtc($aTotalTtc=null)
Get total TTC.
ajoutArticle()
Add a product into cart.
stock($aStock=null)
Getter for stock.
prix($aPrix=null)
Getter for prix.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller= '', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0)
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:85
$id
Attributs "volatiles" : reinitialises apres chaque traitement d'un article.
prixTotalHt($aTotalHt=null)
Get totla HT.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...