dolibarr  13.0.2
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 <https://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  // Data processing methods
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  $reg = array();
118  $vat_src_code = '';
119  if (preg_match('/\((.*)\)/', $txtva, $reg))
120  {
121  $vat_src_code = $reg[1];
122  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
123  }
124 
125  // Define part of HT, VAT, TTC
126  $resultarray = calcul_price_total($this->qte, $this->prix(), $this->remisePercent(), $txtva, -1, -1, 0, 'HT', $vat_npr, $product->type, $mysoc, $localtaxarray);
127 
128  // Calculation of total HT without discount
129  $total_ht = $resultarray[0];
130  $total_vat = $resultarray[1];
131  $total_ttc = $resultarray[2];
132  $total_localtax1 = $resultarray[9];
133  $total_localtax2 = $resultarray[10];
134 
135  // Calculation of the discount amount
136  if ($this->remisePercent())
137  {
138  $remise_percent = $this->remisePercent();
139  } else {
140  $remise_percent = 0;
141  }
142  $montant_remise_ht = ($resultarray[6] - $resultarray[0]);
143  $this->amountDiscount($montant_remise_ht);
144 
145  $newcartarray = $_SESSION['poscart'];
146 
147  $i = 0;
148  if (!is_null($newcartarray) && !empty($newcartarray)) {
149  $i = count($newcartarray);
150  }
151 
152  $newcartarray[$i]['id'] = $i;
153  $newcartarray[$i]['ref'] = $product->ref;
154  $newcartarray[$i]['label'] = $product->label;
155  $newcartarray[$i]['price'] = $product->price;
156  $newcartarray[$i]['price_ttc'] = $product->price_ttc;
157 
158  if (!empty($conf->global->PRODUIT_MULTIPRICES))
159  {
160  if (isset($product->multiprices[$societe->price_level]))
161  {
162  $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level];
163  $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level];
164  }
165  }
166 
167  $newcartarray[$i]['fk_article'] = $this->id;
168  $newcartarray[$i]['qte'] = $this->qte();
169  $newcartarray[$i]['fk_tva'] = $this->tva(); // Vat rowid
170  $newcartarray[$i]['remise_percent'] = $remise_percent;
171  $newcartarray[$i]['remise'] = price2num($montant_remise_ht);
172  $newcartarray[$i]['total_ht'] = price2num($total_ht, 'MT');
173  $newcartarray[$i]['total_ttc'] = price2num($total_ttc, 'MT');
174  $newcartarray[$i]['total_vat'] = price2num($total_vat, 'MT');
175  $newcartarray[$i]['total_localtax1'] = price2num($total_localtax1, 'MT');
176  $newcartarray[$i]['total_localtax2'] = price2num($total_localtax2, 'MT');
177  $_SESSION['poscart'] = $newcartarray;
178 
179  $this->raz();
180  }
181 
188  public function supprArticle($aArticle)
189  {
190  $poscart = $_SESSION['poscart'];
191 
192  $j = 0;
193  $newposcart = array();
194  foreach ($poscart as $key => $val)
195  {
196  if ($poscart[$key]['id'] != $aArticle)
197  {
198  $newposcart[$j] = $poscart[$key];
199  $newposcart[$j]['id'] = $j;
200  $j++;
201  }
202  }
203  unset($poscart);
204  //var_dump($poscart);exit;
205  $_SESSION['poscart'] = $newposcart;
206  }
207 
213  public function calculTotaux()
214  {
215  global $db;
216 
217  $total_ht = 0;
218  $total_ttc = 0;
219  $total_vat = 0;
220  $total_localtax1 = 0;
221  $total_localtax2 = 0;
222 
223  $tab = (!empty($_SESSION['poscart']) ? $_SESSION['poscart'] : array());
224 
225  $tab_size = count($tab);
226  for ($i = 0; $i < $tab_size; $i++)
227  {
228  // Total HT
229  $remise = $tab[$i]['remise'];
230  $total_ht += ($tab[$i]['total_ht']);
231  $total_vat += ($tab[$i]['total_vat']);
232  $total_ttc += ($tab[$i]['total_ttc']);
233  $total_localtax1 += ($tab[$i]['total_localtax1']);
234  $total_localtax2 += ($tab[$i]['total_localtax2']);
235  }
236 
237  $this->prix_total_ttc = $total_ttc;
238  $this->prix_total_ht = $total_ht;
239  $this->prix_total_vat = $total_vat;
240  $this->prix_total_localtax1 = $total_localtax1;
241  $this->prix_total_localtax2 = $total_localtax2;
242 
243  $this->montant_tva = $total_ttc - $total_ht;
244  //print 'total: '.$this->prix_total_ttc; exit;
245  }
246 
252  public function raz()
253  {
254  $this->id('RESET');
255  $this->ref('RESET');
256  $this->qte('RESET');
257  $this->stock('RESET');
258  $this->remisePercent('RESET');
259  $this->amountDiscount('RESET');
260  $this->prix('RESET');
261  $this->tva('RESET');
262  }
263 
269  private function razPers()
270  {
271  $this->numInvoice('RESET');
272  $this->getSetPaymentMode('RESET');
273  $this->amountCollected('RESET');
274  $this->amountReturned('RESET');
275  $this->paiementLe('RESET');
276 
277  $this->amountWithoutTax('RESET');
278  $this->amountVat('RESET');
279  $this->amountWithTax('RESET');
280  }
281 
282 
283  // Methods for modifying protected attributes
284 
291  public function id($aId = null)
292  {
293 
294  if (!$aId)
295  {
296  return $this->id;
297  } elseif ($aId == 'RESET')
298  {
299  $this->id = null;
300  } else {
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  } elseif ($aRef == 'RESET')
318  {
319  $this->ref = null;
320  } else {
321  $this->ref = $aRef;
322  }
323  }
324 
331  public function qte($aQte = null)
332  {
333  if (is_null($aQte))
334  {
335  return $this->qte;
336  } elseif ($aQte == 'RESET')
337  {
338  $this->qte = null;
339  } else {
340  $this->qte = $aQte;
341  }
342  }
343 
350  public function stock($aStock = null)
351  {
352 
353  if (is_null($aStock))
354  {
355  return $this->stock;
356  } elseif ($aStock == 'RESET')
357  {
358  $this->stock = null;
359  } else {
360  $this->stock = $aStock;
361  }
362  }
363 
370  public function remisePercent($aRemisePercent = null)
371  {
372 
373  if (is_null($aRemisePercent))
374  {
375  return $this->remise_percent;
376  } elseif ($aRemisePercent == 'RESET')
377  {
378  $this->remise_percent = null;
379  } else {
380  $this->remise_percent = $aRemisePercent;
381  }
382  }
383 
390  public function amountDiscount($aMontantRemise = null)
391  {
392 
393  if (is_null($aMontantRemise)) {
394  return $this->montant_remise;
395  } elseif ($aMontantRemise == 'RESET') {
396  $this->montant_remise = null;
397  } else {
398  $this->montant_remise = $aMontantRemise;
399  }
400  }
401 
408  public function prix($aPrix = null)
409  {
410 
411  if (is_null($aPrix)) {
412  return $this->prix;
413  } elseif ($aPrix == 'RESET') {
414  $this->prix = null;
415  } else {
416  $this->prix = $aPrix;
417  }
418  }
419 
426  public function tva($aTva = null)
427  {
428  if (is_null($aTva)) {
429  return $this->tva;
430  } elseif ($aTva == 'RESET') {
431  $this->tva = null;
432  } else {
433  $this->tva = $aTva;
434  }
435  }
436 
443  public function numInvoice($aNumFacture = null)
444  {
445  if (is_null($aNumFacture)) {
446  return $this->num_facture;
447  } elseif ($aNumFacture == 'RESET') {
448  $this->num_facture = null;
449  } else {
450  $this->num_facture = $aNumFacture;
451  }
452  }
453 
460  public function getSetPaymentMode($aModeReglement = null)
461  {
462 
463  if (is_null($aModeReglement)) {
464  return $this->mode_reglement;
465  } elseif ($aModeReglement == 'RESET') {
466  $this->mode_reglement = null;
467  } else {
468  $this->mode_reglement = $aModeReglement;
469  }
470  }
471 
478  public function amountCollected($aMontantEncaisse = null)
479  {
480 
481  if (is_null($aMontantEncaisse)) {
482  return $this->montant_encaisse;
483  } elseif ($aMontantEncaisse == 'RESET') {
484  $this->montant_encaisse = null;
485  } else {
486  $this->montant_encaisse = $aMontantEncaisse;
487  }
488  }
489 
496  public function amountReturned($aMontantRendu = null)
497  {
498 
499  if (is_null($aMontantRendu)) {
500  return $this->montant_rendu;
501  } elseif ($aMontantRendu == 'RESET') {
502  $this->montant_rendu = null;
503  } else {
504  $this->montant_rendu = $aMontantRendu;
505  }
506  }
507 
514  public function paiementLe($aPaiementLe = null)
515  {
516  if (is_null($aPaiementLe)) {
517  return $this->paiement_le;
518  } elseif ($aPaiementLe == 'RESET') {
519  $this->paiement_le = null;
520  } else {
521  $this->paiement_le = $aPaiementLe;
522  }
523  }
524 
531  public function amountWithoutTax($aTotalHt = null)
532  {
533  if (is_null($aTotalHt)) {
534  return $this->prix_total_ht;
535  } elseif ($aTotalHt == 'RESET') {
536  $this->prix_total_ht = null;
537  } else {
538  $this->prix_total_ht = $aTotalHt;
539  }
540  }
541 
548  public function amountVat($aMontantTva = null)
549  {
550  if (is_null($aMontantTva)) {
551  return $this->montant_tva;
552  } elseif ($aMontantTva == 'RESET') {
553  $this->montant_tva = null;
554  } else {
555  $this->montant_tva = $aMontantTva;
556  }
557  }
558 
565  public function amountWithTax($aTotalTtc = null)
566  {
567  if (is_null($aTotalTtc))
568  {
569  return $this->prix_total_ttc;
570  } elseif ($aTotalTtc == 'RESET')
571  {
572  $this->prix_total_ttc = null;
573  } else {
574  $this->prix_total_ttc = $aTotalTtc;
575  }
576  }
577 }
amountCollected($aMontantEncaisse=null)
Get amount.
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()
Reset attributes.
razPers()
Resetting persistent attributes.
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.
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, $multicurrency_code= '')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:86
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get tax (VAT) main information from Id.
calculTotaux()
Calculation of total HT, total TTC and VAT amounts.
amountWithTax($aTotalTtc=null)
Get total TTC.
amountDiscount($aMontantRemise=null)
Getter for montant_remise.
paiementLe($aPaiementLe=null)
Get payment date.
qte($aQte=null)
Getter for qte.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage invoices for pos module (cashdesk)
id($aId=null)
Getter for id.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
ajoutArticle()
Add a product into cart.
stock($aStock=null)
Getter for stock.
amountWithoutTax($aTotalHt=null)
Get total HT.
prix($aPrix=null)
Getter for prix.
amountReturned($aMontantRendu=null)
Get amount.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
amountVat($aMontantTva=null)
Get amount vat.