dolibarr  9.0.0
price.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
6  * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
7  * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
85 function 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)
86 {
87  global $conf,$mysoc,$db;
88 
89  $result=array();
90 
91  // Clean parameters
92  if (empty($info_bits)) $info_bits=0;
93  if (empty($txtva)) $txtva=0;
94  if (empty($seller) || ! is_object($seller))
95  {
96  dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
97  if (! is_object($mysoc)) // mysoc may be not defined (during migration process)
98  {
99  $mysoc=new Societe($db);
100  $mysoc->setMysoc($conf);
101  }
102  $seller=$mysoc; // If sell is done to a customer, $seller is not provided, we use $mysoc
103  //var_dump($seller->country_id);exit;
104  }
105  if (empty($localtaxes_array) || ! is_array($localtaxes_array))
106  {
107  dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing", LOG_WARNING);
108  }
109  // Too verbose. Enable for debug only
110  //dol_syslog("Price.lib::calcul_price_total qty=".$qty." pu=".$pu." remiserpercent_ligne=".$remise_percent_ligne." txtva=".$txtva." uselocaltax1_rate=".$uselocaltax1_rate." uselocaltax2_rate=".$uselocaltax2_rate.' remise_percent_global='.$remise_percent_global.' price_base_type='.$ice_base_type.' type='.$type.' progress='.$progress);
111 
112  $countryid=$seller->country_id;
113 
114  if (is_numeric($uselocaltax1_rate)) $uselocaltax1_rate=(float) $uselocaltax1_rate;
115  if (is_numeric($uselocaltax2_rate)) $uselocaltax2_rate=(float) $uselocaltax2_rate;
116 
117  if ($uselocaltax1_rate < 0) $uselocaltax1_rate=$seller->localtax1_assuj;
118  if ($uselocaltax2_rate < 0) $uselocaltax2_rate=$seller->localtax2_assuj;
119 
120  //var_dump($uselocaltax1_rate.' - '.$uselocaltax2_rate);
121  dol_syslog('Price.lib::calcul_price_total qty='.$qty.' pu='.$pu.' remise_percent_ligne='.$remise_percent_ligne.' txtva='.$txtva.' uselocaltax1_rate='.$uselocaltax1_rate.' uselocaltax2_rate='.$uselocaltax2_rate.' remise_percent_global='.$remise_percent_global.' price_base_type='.$price_base_type.' type='.$type.' progress='.$progress);
122 
123  // Now we search localtaxes information ourself (rates and types).
124  $localtax1_type=0;
125  $localtax2_type=0;
126 
127  if (is_array($localtaxes_array))
128  {
129  $localtax1_type = $localtaxes_array[0];
130  $localtax1_rate = $localtaxes_array[1];
131  $localtax2_type = $localtaxes_array[2];
132  $localtax2_rate = $localtaxes_array[3];
133  }
134  else // deprecated method. values and type for localtaxes must be provided by caller and loaded with getLocalTaxesFromRate
135  {
136  $sql = "SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
137  $sql.= " FROM ".MAIN_DB_PREFIX."c_tva as cv";
138  $sql.= " WHERE cv.taux = ".$txtva;
139  $sql.= " AND cv.fk_pays = ".$countryid;
140  dol_syslog("Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
141  $resql = $db->query($sql);
142  if ($resql)
143  {
144  $obj = $db->fetch_object($resql);
145  if ($obj)
146  {
147  $localtax1_rate=$obj->localtax1;
148  $localtax2_rate=$obj->localtax2;
149  $localtax1_type=$obj->localtax1_type;
150  $localtax2_type=$obj->localtax2_type;
151  //var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type);exit;
152  }
153  }
154  else dol_print_error($db);
155  }
156 
157  // pu calculation from pu_devise if pu empty
158  if (empty($pu) && !empty($pu_devise)) {
159  if (! empty($multicurrency_tx)) $pu = $pu_devise / $multicurrency_tx;
160  else
161  {
162  dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR);
163  return array();
164  }
165  }
166  if ($pu === '') $pu=0;
167  // pu_devise calculation from pu
168  if (empty($pu_devise) && !empty($multicurrency_tx)) {
169  if (is_numeric($pu) && is_numeric($multicurrency_tx)) $pu_devise = $pu * $multicurrency_tx;
170  else
171  {
172  dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR);
173  return array();
174  }
175  }
176 
177  // initialize total (may be HT or TTC depending on price_base_type)
178  $tot_sans_remise = $pu * $qty * $progress / 100;
179  $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100));
180  $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
181 
182  // initialize result array
183  for ($i=0; $i <= 15; $i++) $result[$i] = 0;
184 
185  // if there's some localtax including vat, we calculate localtaxes (we will add later)
186 
187  //If input unit price is 'HT', we need to have the totals with main VAT for a correct calculation
188  if ($price_base_type != 'TTC')
189  {
190  $tot_sans_remise_wt = price2num($tot_sans_remise * (1 + ($txtva / 100)),'MU');
191  $tot_avec_remise_wt = price2num($tot_avec_remise * (1 + ($txtva / 100)),'MU');
192  $pu_wt = price2num($pu * (1 + ($txtva / 100)),'MU');
193  }
194  else
195  {
196  $tot_sans_remise_wt = $tot_sans_remise;
197  $tot_avec_remise_wt = $tot_avec_remise;
198  $pu_wt = $pu;
199  }
200 
201  //print 'rr'.$price_base_type.'-'.$txtva.'-'.$tot_sans_remise_wt."-".$pu_wt."-".$uselocaltax1_rate."-".$localtax1_rate."-".$localtax1_type."\n";
202 
203  $localtaxes = array(0,0,0);
204  $apply_tax = false;
205  switch($localtax1_type) {
206  case '2': // localtax on product or service
207  $apply_tax = true;
208  break;
209  case '4': // localtax on product
210  if ($type == 0) $apply_tax = true;
211  break;
212  case '6': // localtax on service
213  if ($type == 1) $apply_tax = true;
214  break;
215  }
216 
217  if ($uselocaltax1_rate && $apply_tax) {
218  $result[14] = price2num(($tot_sans_remise_wt * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise_wt, 'MT');
219  $localtaxes[0] += $result[14];
220 
221  $result[9] = price2num(($tot_avec_remise_wt * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise_wt, 'MT');
222  $localtaxes[1] += $result[9];
223 
224  $result[11] = price2num(($pu_wt * (1 + ( $localtax1_rate / 100))) - $pu_wt, 'MU');
225  $localtaxes[2] += $result[11];
226  }
227 
228  $apply_tax = false;
229  switch($localtax2_type) {
230  case '2': // localtax on product or service
231  $apply_tax = true;
232  break;
233  case '4': // localtax on product
234  if ($type == 0) $apply_tax = true;
235  break;
236  case '6': // localtax on service
237  if ($type == 1) $apply_tax = true;
238  break;
239  }
240  if ($uselocaltax2_rate && $apply_tax) {
241  $result[15] = price2num(($tot_sans_remise_wt * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise_wt, 'MT');
242  $localtaxes[0] += $result[15];
243 
244  $result[10] = price2num(($tot_avec_remise_wt * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise_wt, 'MT');
245  $localtaxes[1] += $result[10];
246 
247  $result[12] = price2num(($pu_wt * (1 + ( $localtax2_rate / 100))) - $pu_wt, 'MU');
248  $localtaxes[2] += $result[12];
249  }
250 
251  //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits");
252  if ($price_base_type == 'HT')
253  {
254  // We work to define prices using the price without tax
255  $result[6] = price2num($tot_sans_remise, 'MT');
256  $result[8] = price2num($tot_sans_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)) + $localtaxes[0], 'MT'); // Selon TVA NPR ou non
257  $result8bis= price2num($tot_sans_remise * (1 + ( $txtva / 100)) + $localtaxes[0], 'MT'); // Si TVA consideree normale (non NPR)
258  $result[7] = price2num($result8bis - ($result[6] + $localtaxes[0]), 'MT');
259 
260  $result[0] = price2num($tot_avec_remise, 'MT');
261  $result[2] = price2num($tot_avec_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)) + $localtaxes[1], 'MT'); // Selon TVA NPR ou non
262  $result2bis= price2num($tot_avec_remise * (1 + ( $txtva / 100)) + $localtaxes[1], 'MT'); // Si TVA consideree normale (non NPR)
263  $result[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
264 
265  $result[3] = price2num($pu, 'MU');
266  $result[5] = price2num($pu * (1 + ( (($info_bits & 1)?0:$txtva) / 100)) + $localtaxes[2], 'MU'); // Selon TVA NPR ou non
267  $result5bis= price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2], 'MU'); // Si TVA consideree normale (non NPR)
268  $result[4] = price2num($result5bis - ($result[3] + $localtaxes[2]), 'MU');
269  }
270  else
271  {
272  // We work to define prices using the price with tax
273  $result[8] = price2num($tot_sans_remise + $localtaxes[0], 'MT');
274  $result[6] = price2num($tot_sans_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
275  $result6bis= price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
276  $result[7] = price2num($result[8] - ($result6bis + $localtaxes[0]), 'MT');
277 
278  $result[2] = price2num($tot_avec_remise + $localtaxes[1], 'MT');
279  $result[0] = price2num($tot_avec_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
280  $result0bis= price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
281  $result[1] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
282 
283  $result[5] = price2num($pu + $localtaxes[2], 'MU');
284  $result[3] = price2num($pu / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non
285  $result3bis= price2num($pu / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR)
286  $result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU');
287  }
288 
289  // if there's some localtax without vat, we calculate localtaxes (we will add them at end)
290 
291  //If input unit price is 'TTC', we need to have the totals without main VAT for a correct calculation
292  if ($price_base_type == 'TTC')
293  {
294  $tot_sans_remise= price2num($tot_sans_remise / (1 + ($txtva / 100)),'MU');
295  $tot_avec_remise= price2num($tot_avec_remise / (1 + ($txtva / 100)),'MU');
296  $pu = price2num($pu / (1 + ($txtva / 100)),'MU');
297  }
298 
299  $apply_tax = false;
300  switch($localtax1_type) {
301  case '1': // localtax on product or service
302  $apply_tax = true;
303  break;
304  case '3': // localtax on product
305  if ($type == 0) $apply_tax = true;
306  break;
307  case '5': // localtax on service
308  if ($type == 1) $apply_tax = true;
309  break;
310  }
311  if ($uselocaltax1_rate && $apply_tax) {
312  $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax1 for total_ht_without_discount
313  $result[8] += $result[14]; // total_ttc_without_discount + tax1
314 
315  $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax1 for total_ht
316  $result[2] += $result[9]; // total_ttc + tax1
317 
318  $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); // amount tax1 for pu_ht
319  $result[5] += $result[11]; // pu_ht + tax1
320  }
321 
322  $apply_tax = false;
323  switch($localtax2_type) {
324  case '1': // localtax on product or service
325  $apply_tax = true;
326  break;
327  case '3': // localtax on product
328  if ($type == 0) $apply_tax = true;
329  break;
330  case '5': // localtax on service
331  if ($type == 1) $apply_tax = true;
332  break;
333  }
334  if ($uselocaltax2_rate && $apply_tax) {
335  $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax2 for total_ht_without_discount
336  $result[8] += $result[15]; // total_ttc_without_discount + tax2
337 
338  $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax2 for total_ht
339  $result[2] += $result[10]; // total_ttc + tax2
340 
341  $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); // amount tax2 for pu_ht
342  $result[5] += $result[12]; // pu_ht + tax2
343  }
344 
345  // If rounding is not using base 10 (rare)
346  if (! empty($conf->global->MAIN_ROUNDING_RULE_TOT))
347  {
348  if ($price_base_type == 'HT')
349  {
350  $result[0]=round($result[0]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
351  $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
352  $result[2]=price2num($result[0]+$result[1], 'MT');
353  $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
354  $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
355  }
356  else
357  {
358  $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
359  $result[2]=round($result[2]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
360  $result[0]=price2num($result[2]-$result[1], 'MT');
361  $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
362  $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
363  }
364  }
365 
366  // Multicurrency
367  if ($multicurrency_tx != 1)
368  {
369  // Recal function using the multicurrency price as reference price. We must set param $multicurrency_tx to 1 to avoid infinite loop.
370  $newresult = calcul_price_total($qty, $pu_devise, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller, $localtaxes_array, $progress, 1, 0);
371 
372  $result[16] = $newresult[0];
373  $result[17] = $newresult[1];
374  $result[18] = $newresult[2];
375  $result[19] = $newresult[3];
376  $result[20] = $newresult[4];
377  $result[21] = $newresult[5];
378  $result[22] = $newresult[6];
379  $result[23] = $newresult[7];
380  $result[24] = $newresult[8];
381  $result[25] = $newresult[9];
382  $result[26] = $newresult[10];
383  /*
384  $result[16] = price2num($result[0] * $multicurrency_tx, 'MT');
385  $result[17] = price2num($result[1] * $multicurrency_tx, 'MT');
386  $result[18] = price2num($result[2] * $multicurrency_tx, 'MT');
387  $result[19] = price2num($pu_devise, 'MU');
388  */
389  }
390  else
391  {
392  $result[16] = $result[0];
393  $result[17] = $result[1];
394  $result[18] = $result[2];
395  $result[19] = $result[3];
396  $result[20] = $result[4];
397  $result[21] = $result[5];
398  $result[22] = $result[6];
399  $result[23] = $result[7];
400  $result[24] = $result[8];
401  $result[25] = $result[9];
402  $result[26] = $result[10];
403  }
404 
405  //var_dump($result);
406  // initialize result array
407  //for ($i=0; $i <= 18; $i++) $result[$i] = (float) $result[$i];
408 
409  dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.$conf->global->MAIN_ROUNDING_RULE_TOT.' pu='.$pu.' qty='.$qty.' price_base_type='.$price_base_type.' total_ht='.$result[0].'-total_vat='.$result[1].'-total_ttc='.$result[2]);
410 
411  return $result;
412 }
413 
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
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
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
div float
Buy price without taxes.
Definition: style.css.php:564