86function 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 =
'')
88 global $conf, $mysoc, $db;
93 if (empty($info_bits)) {
99 if (empty($seller) || !is_object($seller)) {
100 dol_syslog(
"Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
101 if (!is_object($mysoc)) {
103 $mysoc->setMysoc($conf);
108 if (empty($localtaxes_array) || !is_array($localtaxes_array)) {
109 dol_syslog(
"Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing or empty", LOG_WARNING);
111 if (!is_numeric($txtva)) {
112 dol_syslog(
"Price.lib::calcul_price_total Warning: function was called with a parameter vat rate that is not a real numeric value. There is surely a bug.", LOG_ERR);
113 } elseif ($txtva >= 1000) {
114 dol_syslog(
"Price.lib::calcul_price_total Warning: function was called with a bad value for vat rate (should be often < 100, always < 1000). There is surely a bug.", LOG_ERR);
119 $countryid = $seller->country_id;
121 if (is_numeric($uselocaltax1_rate)) {
122 $uselocaltax1_rate = (float) $uselocaltax1_rate;
124 if (is_numeric($uselocaltax2_rate)) {
125 $uselocaltax2_rate = (float) $uselocaltax2_rate;
128 if ($uselocaltax1_rate < 0) {
129 $uselocaltax1_rate = $seller->localtax1_assuj;
131 if ($uselocaltax2_rate < 0) {
132 $uselocaltax2_rate = $seller->localtax2_assuj;
136 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);
142 if (is_array($localtaxes_array) && count($localtaxes_array)) {
143 $localtax1_type = $localtaxes_array[0];
144 $localtax1_rate = $localtaxes_array[1];
145 $localtax2_type = $localtaxes_array[2];
146 $localtax2_rate = $localtaxes_array[3];
150 dol_syslog(
"Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
152 $sql =
"SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
153 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as cv";
154 $sql .=
" WHERE cv.taux = ".((float) $txtva);
155 $sql .=
" AND cv.fk_pays = ".((int) $countryid);
156 $resql = $db->query($sql);
158 $obj = $db->fetch_object($resql);
160 $localtax1_rate = (float) $obj->localtax1;
161 $localtax2_rate = (float) $obj->localtax2;
162 $localtax1_type = $obj->localtax1_type;
163 $localtax2_type = $obj->localtax2_type;
172 if (empty($pu) && !empty($pu_devise)) {
173 if (!empty($multicurrency_tx)) {
174 $pu = $pu_devise / $multicurrency_tx;
176 dol_syslog(
'Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR);
184 if (empty($pu_devise) && !empty($multicurrency_tx)) {
185 if (is_numeric($pu) && is_numeric($multicurrency_tx)) {
186 $pu_devise = $pu * $multicurrency_tx;
188 dol_syslog(
'Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR);
194 $tot_sans_remise = $pu * $qty * $progress / 100;
195 $tot_avec_remise_ligne = $tot_sans_remise * (1 - ((float) $remise_percent_ligne / 100));
196 $tot_avec_remise = $tot_avec_remise_ligne * (1 - ((float) $remise_percent_global / 100));
199 for ($i = 0; $i <= 15; $i++) {
206 if ($price_base_type !=
'TTC') {
207 $tot_sans_remise_wt =
price2num($tot_sans_remise * (1 + ($txtva / 100)),
'MU');
208 $tot_avec_remise_wt =
price2num($tot_avec_remise * (1 + ($txtva / 100)),
'MU');
209 $pu_wt =
price2num($pu * (1 + ($txtva / 100)),
'MU');
211 $tot_sans_remise_wt = $tot_sans_remise;
212 $tot_avec_remise_wt = $tot_avec_remise;
218 $localtaxes = array(0, 0, 0);
220 switch ($localtax1_type) {
236 if ($uselocaltax1_rate && $apply_tax) {
237 $result[14] =
price2num(($tot_sans_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_sans_remise_wt,
'MT');
238 $localtaxes[0] += $result[14];
240 $result[9] =
price2num(($tot_avec_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_avec_remise_wt,
'MT');
241 $localtaxes[1] += $result[9];
243 $result[11] =
price2num(($pu_wt * (1 + ($localtax1_rate / 100))) - $pu_wt,
'MU');
244 $localtaxes[2] += $result[11];
248 switch ($localtax2_type) {
263 if ($uselocaltax2_rate && $apply_tax) {
264 $result[15] =
price2num(($tot_sans_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_sans_remise_wt,
'MT');
265 $localtaxes[0] += $result[15];
267 $result[10] =
price2num(($tot_avec_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_avec_remise_wt,
'MT');
268 $localtaxes[1] += $result[10];
270 $result[12] =
price2num(($pu_wt * (1 + ($localtax2_rate / 100))) - $pu_wt,
'MU');
271 $localtaxes[2] += $result[12];
275 if ($price_base_type ==
'HT') {
277 $result[6] =
price2num($tot_sans_remise,
'MT');
278 $result[8] =
price2num($tot_sans_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[0],
'MT');
279 $result8bis =
price2num($tot_sans_remise * (1 + ($txtva / 100)) + $localtaxes[0],
'MT');
280 $result[7] =
price2num($result8bis - ($result[6] + $localtaxes[0]),
'MT');
282 $result[0] =
price2num($tot_avec_remise,
'MT');
283 $result[2] =
price2num($tot_avec_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[1],
'MT');
284 $result2bis =
price2num($tot_avec_remise * (1 + ($txtva / 100)) + $localtaxes[1],
'MT');
285 $result[1] =
price2num($result2bis - ($result[0] + $localtaxes[1]),
'MT');
288 $result[5] =
price2num($pu * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[2],
'MU');
289 $result5bis =
price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2],
'MU');
290 $result[4] =
price2num($result5bis - ($result[3] + $localtaxes[2]),
'MU');
293 $result[8] =
price2num($tot_sans_remise + $localtaxes[0],
'MT');
294 $result[6] =
price2num($tot_sans_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MT');
295 $result6bis =
price2num($tot_sans_remise / (1 + ($txtva / 100)),
'MT');
296 $result[7] =
price2num($result[8] - ($result6bis + $localtaxes[0]),
'MT');
298 $result[2] =
price2num($tot_avec_remise + $localtaxes[1],
'MT');
299 $result[0] =
price2num($tot_avec_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MT');
300 $result0bis =
price2num($tot_avec_remise / (1 + ($txtva / 100)),
'MT');
301 $result[1] =
price2num($result[2] - ($result0bis + $localtaxes[1]),
'MT');
303 $result[5] =
price2num($pu + $localtaxes[2],
'MU');
304 $result[3] =
price2num($pu / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MU');
305 $result3bis =
price2num($pu / (1 + ($txtva / 100)),
'MU');
306 $result[4] =
price2num($result[5] - ($result3bis + $localtaxes[2]),
'MU');
312 if ($price_base_type ==
'TTC') {
313 $tot_sans_remise =
price2num($tot_sans_remise / (1 + ($txtva / 100)),
'MU');
314 $tot_avec_remise =
price2num($tot_avec_remise / (1 + ($txtva / 100)),
'MU');
315 $pu =
price2num($pu / (1 + ($txtva / 100)),
'MU');
319 switch ($localtax1_type) {
334 if ($uselocaltax1_rate && $apply_tax) {
335 $result[14] =
price2num(($tot_sans_remise * (1 + ($localtax1_rate / 100))) - $tot_sans_remise,
'MT');
336 $result[8] += $result[14];
338 $result[9] =
price2num(($tot_avec_remise * (1 + ($localtax1_rate / 100))) - $tot_avec_remise,
'MT');
339 $result[2] += $result[9];
341 $result[11] =
price2num(($pu * (1 + ($localtax1_rate / 100))) - $pu,
'MU');
342 $result[5] += $result[11];
346 switch ($localtax2_type) {
361 if ($uselocaltax2_rate && $apply_tax) {
362 $result[15] =
price2num(($tot_sans_remise * (1 + ($localtax2_rate / 100))) - $tot_sans_remise,
'MT');
363 $result[8] += $result[15];
365 $result[10] =
price2num(($tot_avec_remise * (1 + ($localtax2_rate / 100))) - $tot_avec_remise,
'MT');
366 $result[2] += $result[10];
368 $result[12] =
price2num(($pu * (1 + ($localtax2_rate / 100))) - $pu,
'MU');
369 $result[5] += $result[12];
373 if (!empty($conf->global->MAIN_ROUNDING_RULE_TOT)) {
374 if ($price_base_type ==
'HT') {
375 $result[0] =
price2num(round($result[0] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
376 $result[1] =
price2num(round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
377 $result[9] =
price2num(round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
378 $result[10] =
price2num(round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
379 $result[2] =
price2num($result[0] + $result[1] + $result[9] + $result[10],
'MT');
381 $result[1] =
price2num(round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
382 $result[2] =
price2num(round($result[2] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
383 $result[9] =
price2num(round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
384 $result[10] =
price2num(round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
385 $result[0] =
price2num($result[2] - $result[1] - $result[9] - $result[10],
'MT');
390 if ($multicurrency_tx != 1) {
391 if ($multicurrency_code) {
392 $savMAIN_MAX_DECIMALS_UNIT = $conf->global->MAIN_MAX_DECIMALS_UNIT;
393 $savMAIN_MAX_DECIMALS_TOT = $conf->global->MAIN_MAX_DECIMALS_TOT;
394 $savMAIN_ROUNDING_RULE_TOT = $conf->global->MAIN_ROUNDING_RULE_TOT;
397 $keyforforeignMAIN_MAX_DECIMALS_UNIT =
'MAIN_MAX_DECIMALS_UNIT_'.$multicurrency_code;
398 $keyforforeignMAIN_MAX_DECIMALS_TOT =
'MAIN_MAX_DECIMALS_TOT_'.$multicurrency_code;
399 $keyforforeignMAIN_ROUNDING_RULE_TOT =
'MAIN_ROUNDING_RULE_TOT_'.$multicurrency_code;
400 if (!empty($conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT)) {
401 $conf->global->MAIN_MAX_DECIMALS_UNIT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_UNIT;
402 $conf->global->MAIN_MAX_DECIMALS_TOT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_TOT;
403 $conf->global->MAIN_ROUNDING_RULE_TOT = $conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT;
408 $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,
'');
410 if ($multicurrency_code) {
412 $conf->global->MAIN_MAX_DECIMALS_UNIT = $savMAIN_MAX_DECIMALS_UNIT;
413 $conf->global->MAIN_MAX_DECIMALS_TOT = $savMAIN_MAX_DECIMALS_TOT;
414 $conf->global->MAIN_ROUNDING_RULE_TOT = $savMAIN_ROUNDING_RULE_TOT;
417 $result[16] = $newresult[0];
418 $result[17] = $newresult[1];
419 $result[18] = $newresult[2];
420 $result[19] = $newresult[3];
421 $result[20] = $newresult[4];
422 $result[21] = $newresult[5];
423 $result[22] = $newresult[6];
424 $result[23] = $newresult[7];
425 $result[24] = $newresult[8];
426 $result[25] = $newresult[9];
427 $result[26] = $newresult[10];
429 $result[16] = $result[0];
430 $result[17] = $result[1];
431 $result[18] = $result[2];
432 $result[19] = $result[3];
433 $result[20] = $result[4];
434 $result[21] = $result[5];
435 $result[22] = $result[6];
436 $result[23] = $result[7];
437 $result[24] = $result[8];
438 $result[25] = $result[9];
439 $result[26] = $result[10];
446 dol_syslog(
'Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.(empty($conf->global->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]);