dolibarr  9.0.0
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 {
44  public $id;
45 
46  protected $ref;
47  protected $qte;
48  protected $stock;
49  protected $remise_percent;
50  protected $montant_remise;
51  protected $prix;
52  protected $tva;
53 
66  protected $num_facture;
67  protected $mode_reglement;
68  protected $montant_encaisse;
69  protected $montant_rendu;
70  protected $paiement_le;
71 
72  protected $prix_total_ht;
73  protected $montant_tva;
74  protected $prix_total_ttc;
75 
76 
80  public function __construct()
81  {
82  $this->raz();
83  $this->razPers();
84  }
85 
86 
87  // Methodes de traitement des donnees
88 
89 
95  public function ajoutArticle()
96  {
97  global $conf,$db,$mysoc;
98 
99  $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
100 
101  $societe = new Societe($db);
102  $societe->fetch($thirdpartyid);
103 
104  $product = new Product($db);
105  $product->fetch($this->id);
106 
107 
108  $vatrowid = $this->tva();
109 
110  $tmp = getTaxesFromId($vatrowid);
111  $txtva = $tmp['rate'].(empty($tmp['code'])?'':' ('.$tmp['code'].')');
112  $vat_npr = $tmp['npr'];
113 
114  $localtaxarray = getLocalTaxesFromRate($vatrowid, 0, $societe, $mysoc, 1);
115 
116  // Clean vat code
117  $vat_src_code='';
118  if (preg_match('/\((.*)\)/', $txtva, $reg))
119  {
120  $vat_src_code = $reg[1];
121  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
122  }
123 
124  // Define part of HT, VAT, TTC
125  $resultarray=calcul_price_total($this->qte, $this->prix(), $this->remisePercent(), $txtva, -1, -1, 0, 'HT', $vat_npr, $product->type, $mysoc, $localtaxarray);
126 
127  // Calcul du total ht sans remise
128  $total_ht = $resultarray[0];
129  $total_vat = $resultarray[1];
130  $total_ttc = $resultarray[2];
131  $total_localtax1 = $resultarray[9];
132  $total_localtax2 = $resultarray[10];
133 
134  // Calcul du montant de la remise
135  if ($this->remisePercent())
136  {
137  $remise_percent = $this->remisePercent();
138  } else {
139  $remise_percent = 0;
140  }
141  $montant_remise_ht = ($resultarray[6] - $resultarray[0]);
142  $this->montantRemise($montant_remise_ht);
143 
144  $newcartarray=$_SESSION['poscart'];
145  $i=count($newcartarray);
146 
147  $newcartarray[$i]['id']=$i;
148  $newcartarray[$i]['ref']=$product->ref;
149  $newcartarray[$i]['label']=$product->label;
150  $newcartarray[$i]['price']=$product->price;
151  $newcartarray[$i]['price_ttc']=$product->price_ttc;
152 
153  if (! empty($conf->global->PRODUIT_MULTIPRICES))
154  {
155  if (isset($product->multiprices[$societe->price_level]))
156  {
157  $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level];
158  $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level];
159  }
160  }
161 
162  $newcartarray[$i]['fk_article']=$this->id;
163  $newcartarray[$i]['qte']=$this->qte();
164  $newcartarray[$i]['fk_tva']=$this->tva(); // Vat rowid
165  $newcartarray[$i]['remise_percent']=$remise_percent;
166  $newcartarray[$i]['remise']=price2num($montant_remise_ht);
167  $newcartarray[$i]['total_ht']=price2num($total_ht,'MT');
168  $newcartarray[$i]['total_ttc']=price2num($total_ttc,'MT');
169  $newcartarray[$i]['total_vat']=price2num($total_vat, 'MT');
170  $newcartarray[$i]['total_localtax1']=price2num($total_localtax1, 'MT');
171  $newcartarray[$i]['total_localtax2']=price2num($total_localtax2, 'MT');
172  $_SESSION['poscart']=$newcartarray;
173 
174  $this->raz();
175  }
176 
183  public function supprArticle($aArticle)
184  {
185  $poscart=$_SESSION['poscart'];
186 
187  $j=0;
188  $newposcart=array();
189  foreach($poscart as $key => $val)
190  {
191  if ($poscart[$key]['id'] != $aArticle)
192  {
193  $newposcart[$j]=$poscart[$key];
194  $newposcart[$j]['id']=$j;
195  $j++;
196  }
197  }
198  unset($poscart);
199  //var_dump($poscart);exit;
200  $_SESSION['poscart']=$newposcart;
201  }
202 
208  public function calculTotaux()
209  {
210  global $db;
211 
212  $total_ht=0;
213  $total_ttc=0;
214  $total_vat = 0;
215  $total_localtax1 = 0;
216  $total_localtax2 = 0;
217 
218  $tab = (! empty($_SESSION['poscart'])?$_SESSION['poscart']:array());
219 
220  $tab_size=count($tab);
221  for($i=0;$i < $tab_size;$i++)
222  {
223  // Total HT
224  $remise = $tab[$i]['remise'];
225  $total_ht += ($tab[$i]['total_ht']);
226  $total_vat += ($tab[$i]['total_vat']);
227  $total_ttc += ($tab[$i]['total_ttc']);
228  $total_localtax1 += ($tab[$i]['total_localtax1']);
229  $total_localtax2 += ($tab[$i]['total_localtax2']);
230  }
231 
232  $this->prix_total_ttc = $total_ttc;
233  $this->prix_total_ht = $total_ht;
234  $this->prix_total_vat = $total_vat;
235  $this->prix_total_localtax1 = $total_localtax1;
236  $this->prix_total_localtax2 = $total_localtax2;
237 
238  $this->montant_tva = $total_ttc - $total_ht;
239  //print 'total: '.$this->prix_total_ttc; exit;
240  }
241 
247  public function raz()
248  {
249  $this->id('RESET');
250  $this->ref('RESET');
251  $this->qte('RESET');
252  $this->stock('RESET');
253  $this->remisePercent('RESET');
254  $this->montantRemise('RESET');
255  $this->prix('RESET');
256  $this->tva('RESET');
257  }
258 
264  private function razPers()
265  {
266  $this->numInvoice('RESET');
267  $this->getSetPaymentMode('RESET');
268  $this->montantEncaisse('RESET');
269  $this->montantRendu('RESET');
270  $this->paiementLe('RESET');
271 
272  $this->prixTotalHt('RESET');
273  $this->montantTva('RESET');
274  $this->prixTotalTtc('RESET');
275  }
276 
277 
278  // Methodes de modification des attributs proteges
279 
286  public function id($aId=null)
287  {
288 
289  if ( !$aId )
290  {
291  return $this->id;
292  }
293  else if ( $aId == 'RESET' )
294  {
295 
296  $this->id = null;
297  }
298  else
299  {
300 
301  $this->id = $aId;
302  }
303  }
304 
311  public function ref($aRef=null)
312  {
313 
314  if (is_null($aRef))
315  {
316  return $this->ref;
317  }
318  else if ( $aRef == 'RESET' )
319  {
320  $this->ref = null;
321  }
322  else
323  {
324  $this->ref = $aRef;
325  }
326  }
327 
334  public function qte($aQte=null)
335  {
336  if (is_null($aQte))
337  {
338  return $this->qte;
339  }
340  else if ( $aQte == 'RESET' )
341  {
342 
343  $this->qte = null;
344  }
345  else
346  {
347  $this->qte = $aQte;
348  }
349  }
350 
357  public function stock($aStock=null)
358  {
359 
360  if (is_null($aStock))
361  {
362  return $this->stock;
363  }
364  else if ( $aStock == 'RESET' )
365  {
366  $this->stock = null;
367  }
368  else
369  {
370  $this->stock = $aStock;
371  }
372  }
373 
380  public function remisePercent($aRemisePercent=null)
381  {
382 
383  if (is_null($aRemisePercent))
384  {
385  return $this->remise_percent;
386  }
387  else if ($aRemisePercent == 'RESET')
388  {
389  $this->remise_percent = null;
390  }
391  else
392  {
393  $this->remise_percent = $aRemisePercent;
394  }
395  }
396 
403  public function montantRemise($aMontantRemise=null)
404  {
405 
406  if (is_null($aMontantRemise)) {
407 
408  return $this->montant_remise;
409  } else if ( $aMontantRemise == 'RESET' ) {
410 
411  $this->montant_remise = null;
412  } else {
413 
414  $this->montant_remise = $aMontantRemise;
415  }
416  }
417 
424  public function prix($aPrix=null)
425  {
426 
427  if (is_null($aPrix)) {
428 
429  return $this->prix;
430  } else if ( $aPrix == 'RESET' ) {
431 
432  $this->prix = null;
433  } else {
434 
435  $this->prix = $aPrix;
436  }
437  }
438 
445  public function tva($aTva=null)
446  {
447  if (is_null($aTva)) {
448 
449  return $this->tva;
450  } else if ( $aTva == 'RESET' ) {
451 
452  $this->tva = null;
453  } else {
454 
455  $this->tva = $aTva;
456  }
457  }
458 
465  public function numInvoice($aNumFacture=null)
466  {
467  if (is_null($aNumFacture)) {
468 
469  return $this->num_facture;
470  } else if ( $aNumFacture == 'RESET' ) {
471 
472  $this->num_facture = null;
473  } else {
474 
475  $this->num_facture = $aNumFacture;
476  }
477  }
478 
485  public function getSetPaymentMode($aModeReglement=null)
486  {
487 
488  if (is_null($aModeReglement)) {
489 
490  return $this->mode_reglement;
491  } else if ( $aModeReglement == 'RESET' ) {
492 
493  $this->mode_reglement = null;
494  } else {
495 
496  $this->mode_reglement = $aModeReglement;
497  }
498  }
499 
506  public function montantEncaisse($aMontantEncaisse=null)
507  {
508 
509  if (is_null($aMontantEncaisse)) {
510 
511  return $this->montant_encaisse;
512  } else if ( $aMontantEncaisse == 'RESET' ) {
513 
514  $this->montant_encaisse = null;
515  } else {
516 
517  $this->montant_encaisse = $aMontantEncaisse;
518  }
519  }
520 
527  public function montantRendu($aMontantRendu=null)
528  {
529 
530  if (is_null($aMontantRendu)) {
531 
532  return $this->montant_rendu;
533  } else if ( $aMontantRendu == 'RESET' ) {
534 
535  $this->montant_rendu = null;
536  } else {
537 
538  $this->montant_rendu = $aMontantRendu;
539  }
540  }
541 
548  public function paiementLe($aPaiementLe=null)
549  {
550  if (is_null($aPaiementLe)) {
551 
552  return $this->paiement_le;
553  } else if ( $aPaiementLe == 'RESET' ) {
554 
555  $this->paiement_le = null;
556  } else {
557 
558  $this->paiement_le = $aPaiementLe;
559  }
560  }
561 
568  public function prixTotalHt($aTotalHt=null)
569  {
570  if (is_null($aTotalHt)) {
571 
572  return $this->prix_total_ht;
573  } else if ( $aTotalHt == 'RESET' ) {
574 
575  $this->prix_total_ht = null;
576  } else {
577 
578  $this->prix_total_ht = $aTotalHt;
579  }
580  }
581 
588  public function montantTva($aMontantTva=null)
589  {
590  if (is_null($aMontantTva)) {
591 
592  return $this->montant_tva;
593  } else if ( $aMontantTva == 'RESET' ) {
594 
595  $this->montant_tva = null;
596  } else {
597 
598  $this->montant_tva = $aMontantTva;
599  }
600  }
601 
608  public function prixTotalTtc($aTotalTtc=null)
609  {
610  if (is_null($aTotalTtc))
611  {
612  return $this->prix_total_ttc;
613  }
614  elseif ( $aTotalTtc == 'RESET' )
615  {
616  $this->prix_total_ttc = null;
617  }
618  else
619  {
620  $this->prix_total_ttc = $aTotalTtc;
621  }
622  }
623 }
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&#39;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
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 &#39;...