88function 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 =
'')
90 global $conf, $mysoc, $db;
95 if (empty($info_bits)) {
101 if (empty($seller) || !is_object($seller)) {
102 dol_syslog(
"Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
103 if (!is_object($mysoc)) {
105 $mysoc->setMysoc($conf);
110 if (empty($localtaxes_array) || !is_array($localtaxes_array)) {
111 dol_syslog(
"Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing or empty", LOG_WARNING);
113 if (!is_numeric($txtva)) {
114 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);
115 } elseif ($txtva >= 1000) {
116 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);
121 $countryid = $seller->country_id;
123 if (is_numeric($uselocaltax1_rate)) {
124 $uselocaltax1_rate = (float) $uselocaltax1_rate;
126 if (is_numeric($uselocaltax2_rate)) {
127 $uselocaltax2_rate = (float) $uselocaltax2_rate;
130 if ($uselocaltax1_rate < 0) {
131 $uselocaltax1_rate = $seller->localtax1_assuj;
133 if ($uselocaltax2_rate < 0) {
134 $uselocaltax2_rate = $seller->localtax2_assuj;
138 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);
144 if (is_array($localtaxes_array) && count($localtaxes_array)) {
145 $localtax1_type = $localtaxes_array[0];
146 $localtax1_rate = $localtaxes_array[1];
147 $localtax2_type = $localtaxes_array[2];
148 $localtax2_rate = $localtaxes_array[3];
152 dol_syslog(
"Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
154 $sql =
"SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
155 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as cv";
156 $sql .=
" WHERE cv.taux = ".((float) $txtva);
157 $sql .=
" AND cv.fk_pays = ".((int) $countryid);
158 $sql .=
" AND cv.entity IN (".getEntity(
'c_tva').
")";
159 $resql = $db->query($sql);
161 $obj = $db->fetch_object($resql);
163 $localtax1_rate = (float) $obj->localtax1;
164 $localtax2_rate = (float) $obj->localtax2;
165 $localtax1_type = $obj->localtax1_type;
166 $localtax2_type = $obj->localtax2_type;
175 if (empty($pu) && !empty($pu_devise)) {
176 if (!empty($multicurrency_tx)) {
177 $pu = $pu_devise / $multicurrency_tx;
179 dol_syslog(
'Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR);
187 if (empty($pu_devise) && !empty($multicurrency_tx)) {
188 if (is_numeric($pu) && is_numeric($multicurrency_tx)) {
189 $pu_devise = $pu * $multicurrency_tx;
191 dol_syslog(
'Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR);
197 $tot_sans_remise = $pu * $qty * $progress / 100;
198 $tot_avec_remise_ligne = $tot_sans_remise * (1 - ((float) $remise_percent_ligne / 100));
199 $tot_avec_remise = $tot_avec_remise_ligne * (1 - ((float) $remise_percent_global / 100));
202 for ($i = 0; $i <= 15; $i++) {
209 if ($price_base_type !=
'TTC') {
210 $tot_sans_remise_wt =
price2num($tot_sans_remise * (1 + ($txtva / 100)),
'MU');
211 $tot_avec_remise_wt =
price2num($tot_avec_remise * (1 + ($txtva / 100)),
'MU');
212 $pu_wt =
price2num($pu * (1 + ($txtva / 100)),
'MU');
214 $tot_sans_remise_wt = $tot_sans_remise;
215 $tot_avec_remise_wt = $tot_avec_remise;
221 $localtaxes = array(0, 0, 0);
223 switch ($localtax1_type) {
239 if ($uselocaltax1_rate && $apply_tax) {
240 $result[14] =
price2num(($tot_sans_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_sans_remise_wt,
'MT');
241 $localtaxes[0] += $result[14];
243 $result[9] =
price2num(($tot_avec_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_avec_remise_wt,
'MT');
244 $localtaxes[1] += $result[9];
246 $result[11] =
price2num(($pu_wt * (1 + ($localtax1_rate / 100))) - $pu_wt,
'MU');
247 $localtaxes[2] += $result[11];
251 switch ($localtax2_type) {
266 if ($uselocaltax2_rate && $apply_tax) {
267 $result[15] =
price2num(($tot_sans_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_sans_remise_wt,
'MT');
268 $localtaxes[0] += $result[15];
270 $result[10] =
price2num(($tot_avec_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_avec_remise_wt,
'MT');
271 $localtaxes[1] += $result[10];
273 $result[12] =
price2num(($pu_wt * (1 + ($localtax2_rate / 100))) - $pu_wt,
'MU');
274 $localtaxes[2] += $result[12];
278 if ($price_base_type ==
'HT') {
280 $result[6] =
price2num($tot_sans_remise,
'MT');
281 $result[8] =
price2num($tot_sans_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[0],
'MT');
282 $result8bis =
price2num($tot_sans_remise * (1 + ($txtva / 100)) + $localtaxes[0],
'MT');
283 $result[7] =
price2num((
float) $result8bis - ((
float) $result[6] + $localtaxes[0]),
'MT');
285 $result[0] =
price2num($tot_avec_remise,
'MT');
286 $result[2] =
price2num($tot_avec_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[1],
'MT');
287 $result2bis =
price2num($tot_avec_remise * (1 + ($txtva / 100)) + $localtaxes[1],
'MT');
288 $result[1] =
price2num((
float) $result2bis - ((
float) $result[0] + $localtaxes[1]),
'MT');
291 $result[5] =
price2num($pu * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[2],
'MU');
292 $result5bis =
price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2],
'MU');
293 $result[4] =
price2num((
float) $result5bis - ((
float) $result[3] + $localtaxes[2]),
'MU');
296 $result[8] =
price2num($tot_sans_remise + $localtaxes[0],
'MT');
297 $result[6] =
price2num($tot_sans_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MT');
298 $result6bis =
price2num($tot_sans_remise / (1 + ($txtva / 100)),
'MT');
299 $result[7] =
price2num((
float) $result[8] - ((
float) $result6bis + $localtaxes[0]),
'MT');
301 $result[2] =
price2num((
float) $tot_avec_remise + (
float) $localtaxes[1],
'MT');
302 $result[0] =
price2num((
float) $tot_avec_remise / (1 + ((($info_bits & 1) ? 0 : (float) $txtva) / 100)),
'MT');
303 $result0bis =
price2num((
float) $tot_avec_remise / (1 + ((
float) $txtva / 100)),
'MT');
304 $result[1] =
price2num((
float) $result[2] - ((
float) $result0bis + (
float) $localtaxes[1]),
'MT');
306 $result[5] =
price2num($pu + $localtaxes[2],
'MU');
307 $result[3] =
price2num($pu / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MU');
308 $result3bis =
price2num($pu / (1 + ($txtva / 100)),
'MU');
309 $result[4] =
price2num((
float) $result[5] - ((
float) $result3bis + (
float) $localtaxes[2]),
'MU');
315 if ($price_base_type ==
'TTC') {
316 $tot_sans_remise =
price2num($tot_sans_remise / (1 + ($txtva / 100)),
'MU');
317 $tot_avec_remise =
price2num($tot_avec_remise / (1 + ($txtva / 100)),
'MU');
318 $pu =
price2num($pu / (1 + ($txtva / 100)),
'MU');
322 switch ($localtax1_type) {
337 if ($uselocaltax1_rate && $apply_tax) {
338 $result[14] =
price2num(($tot_sans_remise * (1 + ($localtax1_rate / 100))) - $tot_sans_remise,
'MT');
339 $result[8] += $result[14];
341 $result[9] =
price2num(($tot_avec_remise * (1 + ($localtax1_rate / 100))) - $tot_avec_remise,
'MT');
342 $result[2] += $result[9];
344 $result[11] =
price2num(($pu * (1 + ($localtax1_rate / 100))) - $pu,
'MU');
345 $result[5] += $result[11];
349 switch ($localtax2_type) {
364 if ($uselocaltax2_rate && $apply_tax) {
365 $result[15] =
price2num(($tot_sans_remise * (1 + ($localtax2_rate / 100))) - $tot_sans_remise,
'MT');
366 $result[8] += $result[15];
368 $result[10] =
price2num(($tot_avec_remise * (1 + ($localtax2_rate / 100))) - $tot_avec_remise,
'MT');
369 $result[2] += $result[10];
371 $result[12] =
price2num(($pu * (1 + ($localtax2_rate / 100))) - $pu,
'MU');
372 $result[5] += $result[12];
377 if ($price_base_type ==
'HT') {
378 $result[0] =
price2num(round((
float) $result[0] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
379 $result[1] =
price2num(round((
float) $result[1] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
380 $result[9] =
price2num(round((
float) $result[9] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
381 $result[10] =
price2num(round((
float) $result[10] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
382 $result[2] =
price2num((
float) $result[0] + (
float) $result[1] + (
float) $result[9] + (
float) $result[10],
'MT');
384 $result[1] =
price2num(round((
float) $result[1] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
385 $result[2] =
price2num(round((
float) $result[2] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
386 $result[9] =
price2num(round((
float) $result[9] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
387 $result[10] =
price2num(round((
float) $result[10] / (
float) $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * (
float) $conf->global->MAIN_ROUNDING_RULE_TOT,
'MT');
388 $result[0] =
price2num((
float) $result[2] - (
float) $result[1] - (
float) $result[9] - (
float) $result[10],
'MT');
393 if ($multicurrency_tx != 1) {
394 if ($multicurrency_code) {
400 $keyforforeignMAIN_MAX_DECIMALS_UNIT =
'MAIN_MAX_DECIMALS_UNIT_'.$multicurrency_code;
401 $keyforforeignMAIN_MAX_DECIMALS_TOT =
'MAIN_MAX_DECIMALS_TOT_'.$multicurrency_code;
402 $keyforforeignMAIN_ROUNDING_RULE_TOT =
'MAIN_ROUNDING_RULE_TOT_'.$multicurrency_code;
404 $conf->global->MAIN_MAX_DECIMALS_UNIT =
getDolGlobalString($keyforforeignMAIN_MAX_DECIMALS_UNIT);
405 $conf->global->MAIN_MAX_DECIMALS_TOT =
getDolGlobalString($keyforforeignMAIN_MAX_DECIMALS_TOT);
406 $conf->global->MAIN_ROUNDING_RULE_TOT =
getDolGlobalString($keyforforeignMAIN_ROUNDING_RULE_TOT);
411 $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,
'');
413 if ($multicurrency_code) {
415 $conf->global->MAIN_MAX_DECIMALS_UNIT = $savMAIN_MAX_DECIMALS_UNIT;
416 $conf->global->MAIN_MAX_DECIMALS_TOT = $savMAIN_MAX_DECIMALS_TOT;
417 $conf->global->MAIN_ROUNDING_RULE_TOT = $savMAIN_ROUNDING_RULE_TOT;
420 $result[16] = $newresult[0];
421 $result[17] = $newresult[1];
422 $result[18] = $newresult[2];
423 $result[19] = $newresult[3];
424 $result[20] = $newresult[4];
425 $result[21] = $newresult[5];
426 $result[22] = $newresult[6];
427 $result[23] = $newresult[7];
428 $result[24] = $newresult[8];
429 $result[25] = $newresult[9];
430 $result[26] = $newresult[10];
432 $result[16] = $result[0];
433 $result[17] = $result[1];
434 $result[18] = $result[2];
435 $result[19] = $result[3];
436 $result[20] = $result[4];
437 $result[21] = $result[5];
438 $result[22] = $result[6];
439 $result[23] = $result[7];
440 $result[24] = $result[8];
441 $result[25] = $result[9];
442 $result[26] = $result[10];
444 dol_syslog(
'Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.
getDolGlobalString(
'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]);