86 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, $multicurrency_code =
'')
88 global $conf, $mysoc, $db;
93 if (empty($info_bits)) $info_bits = 0;
94 if (empty($txtva)) $txtva = 0;
95 if (empty($seller) || !is_object($seller))
97 dol_syslog(
"Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
98 if (!is_object($mysoc))
101 $mysoc->setMysoc($conf);
106 if (empty($localtaxes_array) || !is_array($localtaxes_array)) {
107 dol_syslog(
"Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing or empty", LOG_WARNING);
109 if (!is_numeric($txtva)) {
110 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);
111 } elseif ($txtva >= 1000) {
112 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);
117 $countryid = $seller->country_id;
119 if (is_numeric($uselocaltax1_rate)) $uselocaltax1_rate = (
float) $uselocaltax1_rate;
120 if (is_numeric($uselocaltax2_rate)) $uselocaltax2_rate = (
float) $uselocaltax2_rate;
122 if ($uselocaltax1_rate < 0) $uselocaltax1_rate = $seller->localtax1_assuj;
123 if ($uselocaltax2_rate < 0) $uselocaltax2_rate = $seller->localtax2_assuj;
126 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);
132 if (is_array($localtaxes_array) && count($localtaxes_array)) {
133 $localtax1_type = $localtaxes_array[0];
134 $localtax1_rate = $localtaxes_array[1];
135 $localtax2_type = $localtaxes_array[2];
136 $localtax2_rate = $localtaxes_array[3];
140 dol_syslog(
"Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
142 $sql =
"SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
143 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as cv";
144 $sql .=
" WHERE cv.taux = ".$txtva;
145 $sql .=
" AND cv.fk_pays = ".$countryid;
146 $resql = $db->query($sql);
149 $obj = $db->fetch_object(
$resql);
152 $localtax1_rate = (
float) $obj->localtax1;
153 $localtax2_rate = (
float) $obj->localtax2;
154 $localtax1_type = $obj->localtax1_type;
155 $localtax2_type = $obj->localtax2_type;
162 if (empty($pu) && !empty($pu_devise)) {
163 if (!empty($multicurrency_tx)) $pu = $pu_devise / $multicurrency_tx;
165 dol_syslog(
'Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR);
169 if ($pu ===
'') $pu = 0;
171 if (empty($pu_devise) && !empty($multicurrency_tx)) {
172 if (is_numeric($pu) && is_numeric($multicurrency_tx)) $pu_devise = $pu * $multicurrency_tx;
174 dol_syslog(
'Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR);
180 $tot_sans_remise = $pu * $qty * $progress / 100;
181 $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100));
182 $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
185 for ($i = 0; $i <= 15; $i++) $result[$i] = 0;
190 if ($price_base_type !=
'TTC')
192 $tot_sans_remise_wt =
price2num($tot_sans_remise * (1 + ($txtva / 100)),
'MU');
193 $tot_avec_remise_wt =
price2num($tot_avec_remise * (1 + ($txtva / 100)),
'MU');
194 $pu_wt =
price2num($pu * (1 + ($txtva / 100)),
'MU');
196 $tot_sans_remise_wt = $tot_sans_remise;
197 $tot_avec_remise_wt = $tot_avec_remise;
203 $localtaxes = array(0, 0, 0);
205 switch ($localtax1_type) {
210 if ($type == 0) $apply_tax =
true;
213 if ($type == 1) $apply_tax =
true;
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];
221 $result[9] =
price2num(($tot_avec_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_avec_remise_wt,
'MT');
222 $localtaxes[1] += $result[9];
224 $result[11] =
price2num(($pu_wt * (1 + ($localtax1_rate / 100))) - $pu_wt,
'MU');
225 $localtaxes[2] += $result[11];
229 switch ($localtax2_type) {
234 if ($type == 0) $apply_tax =
true;
237 if ($type == 1) $apply_tax =
true;
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];
244 $result[10] =
price2num(($tot_avec_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_avec_remise_wt,
'MT');
245 $localtaxes[1] += $result[10];
247 $result[12] =
price2num(($pu_wt * (1 + ($localtax2_rate / 100))) - $pu_wt,
'MU');
248 $localtaxes[2] += $result[12];
252 if ($price_base_type ==
'HT')
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');
257 $result8bis =
price2num($tot_sans_remise * (1 + ($txtva / 100)) + $localtaxes[0],
'MT');
258 $result[7] =
price2num($result8bis - ($result[6] + $localtaxes[0]),
'MT');
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');
262 $result2bis =
price2num($tot_avec_remise * (1 + ($txtva / 100)) + $localtaxes[1],
'MT');
263 $result[1] =
price2num($result2bis - ($result[0] + $localtaxes[1]),
'MT');
266 $result[5] =
price2num($pu * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[2],
'MU');
267 $result5bis =
price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2],
'MU');
268 $result[4] =
price2num($result5bis - ($result[3] + $localtaxes[2]),
'MU');
271 $result[8] =
price2num($tot_sans_remise + $localtaxes[0],
'MT');
272 $result[6] =
price2num($tot_sans_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MT');
273 $result6bis =
price2num($tot_sans_remise / (1 + ($txtva / 100)),
'MT');
274 $result[7] =
price2num($result[8] - ($result6bis + $localtaxes[0]),
'MT');
276 $result[2] =
price2num($tot_avec_remise + $localtaxes[1],
'MT');
277 $result[0] =
price2num($tot_avec_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MT');
278 $result0bis =
price2num($tot_avec_remise / (1 + ($txtva / 100)),
'MT');
279 $result[1] =
price2num($result[2] - ($result0bis + $localtaxes[1]),
'MT');
281 $result[5] =
price2num($pu + $localtaxes[2],
'MU');
282 $result[3] =
price2num($pu / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)),
'MU');
283 $result3bis =
price2num($pu / (1 + ($txtva / 100)),
'MU');
284 $result[4] =
price2num($result[5] - ($result3bis + $localtaxes[2]),
'MU');
290 if ($price_base_type ==
'TTC')
292 $tot_sans_remise =
price2num($tot_sans_remise / (1 + ($txtva / 100)),
'MU');
293 $tot_avec_remise =
price2num($tot_avec_remise / (1 + ($txtva / 100)),
'MU');
294 $pu =
price2num($pu / (1 + ($txtva / 100)),
'MU');
298 switch ($localtax1_type) {
303 if ($type == 0) $apply_tax =
true;
306 if ($type == 1) $apply_tax =
true;
309 if ($uselocaltax1_rate && $apply_tax) {
310 $result[14] =
price2num(($tot_sans_remise * (1 + ($localtax1_rate / 100))) - $tot_sans_remise,
'MT');
311 $result[8] += $result[14];
313 $result[9] =
price2num(($tot_avec_remise * (1 + ($localtax1_rate / 100))) - $tot_avec_remise,
'MT');
314 $result[2] += $result[9];
316 $result[11] =
price2num(($pu * (1 + ($localtax1_rate / 100))) - $pu,
'MU');
317 $result[5] += $result[11];
321 switch ($localtax2_type) {
326 if ($type == 0) $apply_tax =
true;
329 if ($type == 1) $apply_tax =
true;
332 if ($uselocaltax2_rate && $apply_tax) {
333 $result[15] =
price2num(($tot_sans_remise * (1 + ($localtax2_rate / 100))) - $tot_sans_remise,
'MT');
334 $result[8] += $result[15];
336 $result[10] =
price2num(($tot_avec_remise * (1 + ($localtax2_rate / 100))) - $tot_avec_remise,
'MT');
337 $result[2] += $result[10];
339 $result[12] =
price2num(($pu * (1 + ($localtax2_rate / 100))) - $pu,
'MU');
340 $result[5] += $result[12];
344 if (!empty($conf->global->MAIN_ROUNDING_RULE_TOT))
346 if ($price_base_type ==
'HT')
348 $result[0] = round($result[0] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
349 $result[1] = round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
350 $result[9] = round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
351 $result[10] = round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
352 $result[2] =
price2num($result[0] + $result[1] + $result[9] + $result[10],
'MT');
354 $result[1] = round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
355 $result[2] = round($result[2] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
356 $result[9] = round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
357 $result[10] = round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT;
358 $result[0] =
price2num($result[2] - $result[1] - $result[9] - $result[10],
'MT');
363 if ($multicurrency_tx != 1)
365 if ($multicurrency_code) {
366 $savMAIN_MAX_DECIMALS_UNIT = $conf->global->MAIN_MAX_DECIMALS_UNIT;
367 $savMAIN_MAX_DECIMALS_TOT = $conf->global->MAIN_MAX_DECIMALS_TOT;
368 $savMAIN_ROUNDING_RULE_TOT = $conf->global->MAIN_ROUNDING_RULE_TOT;
371 $keyforforeignMAIN_MAX_DECIMALS_UNIT =
'MAIN_MAX_DECIMALS_UNIT_'.$multicurrency_code;
372 $keyforforeignMAIN_MAX_DECIMALS_TOT =
'MAIN_MAX_DECIMALS_TOT_'.$multicurrency_code;
373 $keyforforeignMAIN_ROUNDING_RULE_TOT =
'MAIN_ROUNDING_RULE_TOT_'.$multicurrency_code;
374 if (!empty($conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT)) {
375 $conf->global->MAIN_MAX_DECIMALS_UNIT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_UNIT;
376 $conf->global->MAIN_MAX_DECIMALS_TOT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_TOT;
377 $conf->global->MAIN_ROUNDING_RULE_TOT = $conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT;
382 $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,
'');
384 if ($multicurrency_code) {
386 $conf->global->MAIN_MAX_DECIMALS_UNIT = $savMAIN_MAX_DECIMALS_UNIT;
387 $conf->global->MAIN_MAX_DECIMALS_TOT = $savMAIN_MAX_DECIMALS_TOT;
388 $conf->global->MAIN_ROUNDING_RULE_TOT = $savMAIN_ROUNDING_RULE_TOT;
391 $result[16] = $newresult[0];
392 $result[17] = $newresult[1];
393 $result[18] = $newresult[2];
394 $result[19] = $newresult[3];
395 $result[20] = $newresult[4];
396 $result[21] = $newresult[5];
397 $result[22] = $newresult[6];
398 $result[23] = $newresult[7];
399 $result[24] = $newresult[8];
400 $result[25] = $newresult[9];
401 $result[26] = $newresult[10];
403 $result[16] = $result[0];
404 $result[17] = $result[1];
405 $result[18] = $result[2];
406 $result[19] = $result[3];
407 $result[20] = $result[4];
408 $result[21] = $result[5];
409 $result[22] = $result[6];
410 $result[23] = $result[7];
411 $result[24] = $result[8];
412 $result[25] = $result[9];
413 $result[26] = $result[10];
420 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]);
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.
Class to manage third parties objects (customers, suppliers, prospects...)
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
div float
Buy price without taxes.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...