From 4cca760feb73b3c69de6520603464742a1902d81 Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Mon, 10 Sep 2012 15:47:33 +0200 Subject: [PATCH 1/3] calculate prices with generic local taxes management --- htdocs/core/lib/price.lib.php | 230 ++++++++++++++++++++++------------ 1 file changed, 152 insertions(+), 78 deletions(-) diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index f241182b4656b..949891b12b95d 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -2,6 +2,7 @@ /* Copyright (C) 2002-2006 Rodolphe Quiedeville * Copyright (C) 2006-2008 Laurent Destailleur * Copyright (C) 2010 Juanjo Menent + * Copyright (C) 2012 Christophe Battarel * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,110 +19,183 @@ */ /** - * \file htdocs/core/lib/price.lib.php + * \file htdocs/lib/price.lib.php * \brief Librairie contenant les fonctions pour calculer un prix. + * \version $Id: price.lib.php,v 1.36 2011/07/31 23:26:01 eldy Exp $ */ /** * Calculate totals (net, vat, ...) of a line. - * - * @param int $qty Quantity - * @param float $pu Unit price (HT or TTC selon price_base_type) - * @param float $remise_percent_ligne Discount for line - * @param float $txtva Vat rate - * @param float $txlocaltax1 Localtax1 rate (used for some countries only, like spain) - * @param float $txlocaltax2 Localtax2 rate (used for some countries only, like spain) - * @param float $remise_percent_global 0 - * @param string $price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC - * @param int $info_bits Miscellanous informations on line + * @param qty Quantity + * @param pu Unit price (HT or TTC selon price_base_type) + * @param remise_percent_ligne Discount for line + * @param txtva Vat rate + * @param localtax1 Array of localtax1, localtax1_type + * @param localtax2 Array of localtax2, localtax2_type + * @param remise_percent_global 0 + * @param price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC + * @param info_bits Miscellanous informations on line + * \param type Type of line (0=product, 1=service) + * * @return result[0,1,2,3,4,5,6,7,8] (total_ht, total_vat, total_ttc, pu_ht, pu_tva, pu_ttc, total_ht_without_discount, total_vat_without_discount, total_ttc_without_discount) */ -function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $txlocaltax1=0, $txlocaltax2=0, $remise_percent_global=0, $price_base_type='HT', $info_bits=0) +function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1=null, $localtax2=null, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0) { global $conf,$mysoc; $result=array(); - - // We work to define prices using the price without tax + + // initialize total $tot_sans_remise = $pu * $qty; - $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100)); - $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100)); + $tot_avec_remise_ligne = $tot_sans_remise * ( 1 - ($remise_percent_ligne / 100)); + $tot_avec_remise = $tot_avec_remise_ligne * ( 1 - ($remise_percent_global / 100)); + + // initialize result + for ($i=0; $i <= 15; $i++) + $result[$i] = 0; + + // if there's some localtax before vat + $localtaxes = array(0,0,0); + if ($localtax1) { + $apply_tax = false; + switch($localtax1[1]) { + case '2': // localtax on product or service + $apply_tax = true; + break; + case '4': // localtax on product + if ($type == 0) $apply_tax = true; + break; + case '6': // localtax on service + if ($type == 1) $apply_tax = true; + break; + } + if ($apply_tax) { + $result[14] = price2num(($tot_sans_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); + $localtaxes[0] += $result[14]; + + $result[9] = price2num(($tot_avec_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); + $localtaxes[1] += $result[9]; + + $result[11] = price2num(($pu * ( 1 + ( $localtax1[0] / 100))) - $pu, 'MT'); + $localtaxes[2] += $result[11]; + } + } + if ($localtax2) { + $apply_tax = false; + switch($localtax2[1]) { + case '2': // localtax on product or service + $apply_tax = true; + break; + case '4': // localtax on product + if ($type == 0) $apply_tax = true; + break; + case '6': // localtax on service + if ($type == 1) $apply_tax = true; + break; + } + if ($apply_tax) { + $result[15] = price2num(($tot_sans_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); + $localtaxes[0] += $result[15]; + + $result[10] = price2num(($tot_avec_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); + $localtaxes[1] += $result[10]; + + $result[12] = price2num(($pu * ( 1 + ( $localtax2[0] / 100))) - $pu, 'MT'); + $localtaxes[2] += $result[12]; + } + } //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits"); if ($price_base_type == 'HT') { + // We work to define prices using the price without tax $result[6] = price2num($tot_sans_remise, 'MT'); - $result[8] = price2num($tot_sans_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result8bis= price2num($tot_sans_remise * (1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[7] = price2num($result8bis - $result[6], 'MT'); + $result[8] = price2num(($tot_sans_remise + $localtaxes[0]) * ( 1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result8bis= price2num(($tot_sans_remise + $localtaxes[0]) * ( 1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[7] = $result8bis - ($result[6] + $localtaxes[0]); $result[0] = price2num($tot_avec_remise, 'MT'); - $result[2] = price2num($tot_avec_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result2bis= price2num($tot_avec_remise * (1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[2] = price2num(($tot_avec_remise + $localtaxes[1]) * ( 1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result2bis= price2num(($tot_avec_remise + $localtaxes[1]) * ( 1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[1] = price2num($result2bis - $result[0], 'MT'); // Total VAT = TTC - HT + $result[1] = $result2bis - ($result[0] + $localtaxes[1]); // Total VAT = TTC - (HT + localtax) $result[3] = price2num($pu, 'MU'); - $result[5] = price2num($pu * (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non - $result5bis= price2num($pu * (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) - $result[4] = price2num($result5bis - $result[3], 'MU'); + $result[5] = price2num(($pu + $localtaxes[2]) * ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non + $result5bis= price2num(($pu + $localtaxes[2]) * ( 1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) + $result[4] = $result5bis - ($result[3] + $localtaxes[2]); } else { - $result[8] = price2num($tot_sans_remise, 'MT'); - $result[6] = price2num($tot_sans_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result6bis= price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[7] = price2num($result[8] - $result6bis, 'MT'); - - $result[2] = price2num($tot_avec_remise, 'MT'); - $result[0] = price2num($tot_avec_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result0bis= price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - - $result[1] = price2num($result[2] - $result0bis, 'MT'); // Total VAT = TTC - HT - - $result[5] = price2num($pu, 'MU'); - $result[3] = price2num($pu / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non - $result3bis= price2num($pu / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) - $result[4] = price2num($result[5] - $result3bis, 'MU'); + // We work to define prices using the price with tax + $result[8] = price2num($tot_sans_remise + $localtaxes[0], 'MT'); + $result[6] = price2num(($tot_sans_remise + $localtaxes[0]) / ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result6bis= price2num(($tot_sans_remise + $localtaxes[0]) / ( 1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[7] = $result[8] - ($result6bis + $localtaxes[0]); + + $result[2] = price2num($tot_avec_remise + $localtaxes[1], 'MT'); + $result[0] = price2num(($tot_avec_remise + $localtaxes[1]) / ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result0bis= price2num(($tot_avec_remise + $localtaxes[1]) / ( 1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + + $result[1] = $result[2] - ($result0bis + $localtaxes[1]); // Total VAT = TTC - HT + + $result[5] = price2num(($pu + $localtaxes[2]) , 'MU'); + $result[3] = price2num(($pu + $localtaxes[2]) / ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non + $result3bis= price2num(($pu + $localtaxes[2]) / ( 1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) + $result[4] = $result[5] - ($result3bis + $localtaxes[2]); } - // Local taxes 1. Local tax1 is a second tax that is added after standard one) - if ($txlocaltax1 > 0) - { - $result[14] = price2num(($result[6] * ( 1 + ( $txlocaltax1 / 100))) - $result[6], 'MT'); // amount tax1 for total_ht_without_discount - $result[9] = price2num(($result[0] * ( 1 + ( $txlocaltax1 / 100))) - $result[0], 'MT'); // amount tax1 for total_ht - $result[11] = price2num(($result[3] * ( 1 + ( $txlocaltax1 / 100))) - $pu, 'MU'); // amount tax1 for pu_ht + // if there's some localtax without vat + if ($localtax1) { + $apply_tax = false; + switch($localtax1[1]) { + case '1': // localtax on product or service + $apply_tax = true; + break; + case '3': // localtax on product + if ($type == 0) $apply_tax = true; + break; + case '5': // localtax on service + if ($type == 1) $apply_tax = true; + break; + } + if ($apply_tax) { + $result[14] = price2num(($tot_sans_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); + $result[8] += $result[14]; + + $result[9] = price2num(($tot_avec_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); + $result[2] += $result[9]; + + $result[11] = price2num(($pu * ( 1 + ( $localtax1[0] / 100))) - $pu, 'MT'); + $result[5] += $result[11]; + } + } + if ($localtax2) { + $apply_tax = false; + switch($localtax2[1]) { + case '1': // localtax on product or service + $apply_tax = true; + break; + case '3': // localtax on product + if ($type == 0) $apply_tax = true; + break; + case '5': // localtax on service + if ($type == 1) $apply_tax = true; + break; + } + if ($apply_tax) { + $result[15] = price2num(($tot_sans_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); + $result[8] += $result[15]; + + $result[10] = price2num(($tot_avec_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); + $result[2] += $result[10]; + + $result[12] = price2num(($pu * ( 1 + ( $localtax2[0] / 100))) - $pu, 'MT'); + $result[5] += $result[12]; + } + } - $result[8] = price2num($result[8] + $result[14], 'MT'); // total_ttc_without_discount + tax1 - $result[2] = price2num($result[2] + $result[9], 'MT'); // total_ttc + tax1 - $result[5] = price2num($result[5] + $result[11], 'MU'); // pu_ht + tax1 - } - else - { - $result[14] = 0; - $result[9] = 0; - $result[11] = 0; - } - - // Local taxes 2. Local tax2 is a second tax that is substracted after standard one) - // Example: Country = Spain, localtax2 is IRPF - if ($txlocaltax2 > 0) - { - $result[15] = price2num(($result[6] * ( 1 + ( $txlocaltax2 / 100))) - $result[6], 'MT'); // amount tax2 for total_ht_without_discount - $result[10] = price2num(($result[0] * ( 1 + ( $txlocaltax2 / 100))) - $result[0], 'MT'); // amount tax2 for total_ht - $result[12] = price2num(($result[3] * ( 1 + ( $txlocaltax2 / 100))) - $pu, 'MU'); // amount tax2 for pu_ht - - $result[8] = price2num($result[8] - $result[15], 'MT'); // total_ttc_without_discount + tax2 - $result[2] = price2num($result[2] - $result[10], 'MT'); // total_ttc + tax2 - $result[5] = price2num($result[5] - $result[12], 'MU'); // pu_ttc + tax2 - } - else - { - $result[15] = 0; - $result[10] = 0; - $result[12] = 0; - } // If rounding is not using base 10 (rare) if (! empty($conf->global->MAIN_ROUNDING_RULE_TOT)) @@ -130,7 +204,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $txlocalta { $result[0]=round($result[0]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; - $result[2]=price2num($result[0]+$result[1], 'MT'); + $result[2]=$result[0]+$result[1]; $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; } @@ -138,13 +212,13 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $txlocalta { $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[2]=round($result[2]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; - $result[0]=price2num($result[2]-$result[0], 'MT'); + $result[0]=$result[2]-$result[0]; $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; } } - //print "Price.lib::calcul_price_total ".$result[0]."-".$result[1]."-".$result[2]; + //print_r($result); return $result; } From 8c11da1df89f3fb587a6f5234ac27805ea5163a3 Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Mon, 10 Sep 2012 16:01:20 +0200 Subject: [PATCH 2/3] readapt to 3.3 --- htdocs/core/lib/price.lib.php | 92 +++++++++++++++++------------------ 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 949891b12b95d..dd7778286e764 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -19,7 +19,7 @@ */ /** - * \file htdocs/lib/price.lib.php + * \file htdocs/core/lib/price.lib.php * \brief Librairie contenant les fonctions pour calculer un prix. * \version $Id: price.lib.php,v 1.36 2011/07/31 23:26:01 eldy Exp $ */ @@ -27,17 +27,15 @@ /** * Calculate totals (net, vat, ...) of a line. - * @param qty Quantity - * @param pu Unit price (HT or TTC selon price_base_type) - * @param remise_percent_ligne Discount for line - * @param txtva Vat rate - * @param localtax1 Array of localtax1, localtax1_type - * @param localtax2 Array of localtax2, localtax2_type - * @param remise_percent_global 0 - * @param price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC - * @param info_bits Miscellanous informations on line - * \param type Type of line (0=product, 1=service) - * + * @param int $qty Quantity + * @param float $pu Unit price (HT or TTC selon price_base_type) + * @param float $remise_percent_ligne Discount for line + * @param float $txtva Vat rate + * @param array $localtax1 Array of localtax1, localtax1_type + * @param array $localtax2 Array of localtax2, localtax2_type + * @param float $remise_percent_global 0 + * @param string $price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC + * @param int $info_bits Miscellanous informations on line * @return result[0,1,2,3,4,5,6,7,8] (total_ht, total_vat, total_ttc, pu_ht, pu_tva, pu_ttc, total_ht_without_discount, total_vat_without_discount, total_ttc_without_discount) */ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1=null, $localtax2=null, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0) @@ -48,8 +46,8 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 // initialize total $tot_sans_remise = $pu * $qty; - $tot_avec_remise_ligne = $tot_sans_remise * ( 1 - ($remise_percent_ligne / 100)); - $tot_avec_remise = $tot_avec_remise_ligne * ( 1 - ($remise_percent_global / 100)); + $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100)); + $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100)); // initialize result for ($i=0; $i <= 15; $i++) @@ -71,13 +69,13 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[14] = price2num(($tot_sans_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); + $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); $localtaxes[0] += $result[14]; - $result[9] = price2num(($tot_avec_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); + $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); $localtaxes[1] += $result[9]; - $result[11] = price2num(($pu * ( 1 + ( $localtax1[0] / 100))) - $pu, 'MT'); + $result[11] = price2num(($pu * (1 + ( $localtax1[0] / 100))) - $pu, 'MT'); $localtaxes[2] += $result[11]; } } @@ -95,13 +93,13 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[15] = price2num(($tot_sans_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); + $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); $localtaxes[0] += $result[15]; - $result[10] = price2num(($tot_avec_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); + $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); $localtaxes[1] += $result[10]; - $result[12] = price2num(($pu * ( 1 + ( $localtax2[0] / 100))) - $pu, 'MT'); + $result[12] = price2num(($pu * (1 + ( $localtax2[0] / 100))) - $pu, 'MT'); $localtaxes[2] += $result[12]; } } @@ -111,39 +109,39 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 { // We work to define prices using the price without tax $result[6] = price2num($tot_sans_remise, 'MT'); - $result[8] = price2num(($tot_sans_remise + $localtaxes[0]) * ( 1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result8bis= price2num(($tot_sans_remise + $localtaxes[0]) * ( 1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[7] = $result8bis - ($result[6] + $localtaxes[0]); + $result[8] = price2num(($tot_sans_remise + $localtaxes[0]) * (1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result8bis= price2num(($tot_sans_remise + $localtaxes[0]) * (1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[7] = price2num($result8bis - ($result[6] + $localtaxes[0]), 'MT'); $result[0] = price2num($tot_avec_remise, 'MT'); - $result[2] = price2num(($tot_avec_remise + $localtaxes[1]) * ( 1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result2bis= price2num(($tot_avec_remise + $localtaxes[1]) * ( 1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[2] = price2num(($tot_avec_remise + $localtaxes[1]) * (1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result2bis= price2num(($tot_avec_remise + $localtaxes[1]) * (1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[1] = $result2bis - ($result[0] + $localtaxes[1]); // Total VAT = TTC - (HT + localtax) + $result[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax) $result[3] = price2num($pu, 'MU'); - $result[5] = price2num(($pu + $localtaxes[2]) * ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non - $result5bis= price2num(($pu + $localtaxes[2]) * ( 1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) - $result[4] = $result5bis - ($result[3] + $localtaxes[2]); + $result[5] = price2num(($pu + $localtaxes[2]) * (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non + $result5bis= price2num(($pu + $localtaxes[2]) * (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) + $result[4] = price2num($result5bis - ($result[3] + $localtaxes[2]), 'MU'); } else { // We work to define prices using the price with tax $result[8] = price2num($tot_sans_remise + $localtaxes[0], 'MT'); - $result[6] = price2num(($tot_sans_remise + $localtaxes[0]) / ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result6bis= price2num(($tot_sans_remise + $localtaxes[0]) / ( 1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[7] = $result[8] - ($result6bis + $localtaxes[0]); + $result[6] = price2num(($tot_sans_remise + $localtaxes[0]) / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result6bis= price2num(($tot_sans_remise + $localtaxes[0]) / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[7] = price2num($result[8] - ($result6bis + $localtaxes[0]), 'MT'); $result[2] = price2num($tot_avec_remise + $localtaxes[1], 'MT'); - $result[0] = price2num(($tot_avec_remise + $localtaxes[1]) / ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result0bis= price2num(($tot_avec_remise + $localtaxes[1]) / ( 1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result[0] = price2num(($tot_avec_remise + $localtaxes[1]) / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non + $result0bis= price2num(($tot_avec_remise + $localtaxes[1]) / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) - $result[1] = $result[2] - ($result0bis + $localtaxes[1]); // Total VAT = TTC - HT + $result[1] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - HT $result[5] = price2num(($pu + $localtaxes[2]) , 'MU'); - $result[3] = price2num(($pu + $localtaxes[2]) / ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non - $result3bis= price2num(($pu + $localtaxes[2]) / ( 1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) - $result[4] = $result[5] - ($result3bis + $localtaxes[2]); + $result[3] = price2num(($pu + $localtaxes[2]) / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non + $result3bis= price2num(($pu + $localtaxes[2]) / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) + $result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU'); } // if there's some localtax without vat @@ -161,13 +159,13 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[14] = price2num(($tot_sans_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); + $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); $result[8] += $result[14]; - $result[9] = price2num(($tot_avec_remise * ( 1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); + $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); $result[2] += $result[9]; - $result[11] = price2num(($pu * ( 1 + ( $localtax1[0] / 100))) - $pu, 'MT'); + $result[11] = price2num(($pu * (1 + ( $localtax1[0] / 100))) - $pu, 'MU'); $result[5] += $result[11]; } } @@ -185,13 +183,13 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[15] = price2num(($tot_sans_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); + $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); $result[8] += $result[15]; - $result[10] = price2num(($tot_avec_remise * ( 1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); + $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); $result[2] += $result[10]; - $result[12] = price2num(($pu * ( 1 + ( $localtax2[0] / 100))) - $pu, 'MT'); + $result[12] = price2num(($pu * (1 + ( $localtax2[0] / 100))) - $pu, 'MU'); $result[5] += $result[12]; } } @@ -204,7 +202,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 { $result[0]=round($result[0]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; - $result[2]=$result[0]+$result[1]; + $result[2]=price2num($result[0]+$result[1], 'MT'); $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; } @@ -212,13 +210,13 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 { $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[2]=round($result[2]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; - $result[0]=$result[2]-$result[0]; + $result[0]=price2num($result[2]-$result[0], 'MT'); $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT; } } - //print_r($result); + //print "Price.lib::calcul_price_total ".$result[0]."-".$result[1]."-".$result[2]; return $result; } From c6da5b79fe9836c8313894529d52dcea2ba06bda Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Mon, 10 Sep 2012 16:49:36 +0200 Subject: [PATCH 3/3] change localtaxes parameters to be compatible with older calls --- htdocs/core/lib/price.lib.php | 68 +++++++++++++++++------------------ 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index dd7778286e764..227d3d54049cb 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -27,18 +27,21 @@ /** * Calculate totals (net, vat, ...) of a line. - * @param int $qty Quantity - * @param float $pu Unit price (HT or TTC selon price_base_type) - * @param float $remise_percent_ligne Discount for line - * @param float $txtva Vat rate - * @param array $localtax1 Array of localtax1, localtax1_type - * @param array $localtax2 Array of localtax2, localtax2_type + * @param int $qty Quantity + * @param float $pu Unit price (HT or TTC selon price_base_type) + * @param float $remise_percent_ligne Discount for line + * @param float $txtva Vat rate + * @param float $localtax1_rate Localtax1 rate + * @param float $localtax2_rate Localtax2 rate * @param float $remise_percent_global 0 * @param string $price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC * @param int $info_bits Miscellanous informations on line + * @param char $localtax1_type Localtax1 type + * @param char $localtax2_type Localtax2 type + * @return result[0,1,2,3,4,5,6,7,8] (total_ht, total_vat, total_ttc, pu_ht, pu_tva, pu_ttc, total_ht_without_discount, total_vat_without_discount, total_ttc_without_discount) */ -function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1=null, $localtax2=null, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0) +function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1_rate=0, $localtax2_rate=0, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0, $localtax1_type = '0', $localtax2_type = '0') { global $conf,$mysoc; @@ -55,9 +58,8 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 // if there's some localtax before vat $localtaxes = array(0,0,0); - if ($localtax1) { - $apply_tax = false; - switch($localtax1[1]) { + $apply_tax = false; + switch($localtax1_type) { case '2': // localtax on product or service $apply_tax = true; break; @@ -69,19 +71,18 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); + $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT'); $localtaxes[0] += $result[14]; - $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); + $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); $localtaxes[1] += $result[9]; - $result[11] = price2num(($pu * (1 + ( $localtax1[0] / 100))) - $pu, 'MT'); + $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MT'); $localtaxes[2] += $result[11]; } - } - if ($localtax2) { - $apply_tax = false; - switch($localtax2[1]) { + + $apply_tax = false; + switch($localtax2_type) { case '2': // localtax on product or service $apply_tax = true; break; @@ -93,16 +94,15 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); + $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise, 'MT'); $localtaxes[0] += $result[15]; - $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); + $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); $localtaxes[1] += $result[10]; - $result[12] = price2num(($pu * (1 + ( $localtax2[0] / 100))) - $pu, 'MT'); + $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MT'); $localtaxes[2] += $result[12]; } - } //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits"); if ($price_base_type == 'HT') @@ -145,9 +145,8 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 } // if there's some localtax without vat - if ($localtax1) { - $apply_tax = false; - switch($localtax1[1]) { + $apply_tax = false; + switch($localtax1_type) { case '1': // localtax on product or service $apply_tax = true; break; @@ -159,19 +158,18 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1[0] / 100))) - $tot_sans_remise, 'MT'); + $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT'); $result[8] += $result[14]; - $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1[0] / 100))) - $tot_avec_remise, 'MT'); + $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); $result[2] += $result[9]; - $result[11] = price2num(($pu * (1 + ( $localtax1[0] / 100))) - $pu, 'MU'); + $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); $result[5] += $result[11]; } - } - if ($localtax2) { - $apply_tax = false; - switch($localtax2[1]) { + + $apply_tax = false; + switch($localtax2_type) { case '1': // localtax on product or service $apply_tax = true; break; @@ -183,17 +181,15 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 break; } if ($apply_tax) { - $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2[0] / 100))) - $tot_sans_remise, 'MT'); + $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise, 'MT'); $result[8] += $result[15]; - $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2[0] / 100))) - $tot_avec_remise, 'MT'); + $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); $result[2] += $result[10]; - $result[12] = price2num(($pu * (1 + ( $localtax2[0] / 100))) - $pu, 'MU'); + $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); $result[5] += $result[12]; } - } - // If rounding is not using base 10 (rare) if (! empty($conf->global->MAIN_ROUNDING_RULE_TOT))