From 4cca760feb73b3c69de6520603464742a1902d81 Mon Sep 17 00:00:00 2001 From: Christophe Battarel Date: Mon, 10 Sep 2012 15:47:33 +0200 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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)) From 61e2596e4d9c60ec6ff66d9083e0fa5b5c0c9a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a?= Date: Mon, 10 Sep 2012 22:51:54 +0200 Subject: [PATCH 04/12] Task #539 - Add new products into proposals,invoices... with barcode --- ChangeLog | 3 ++- htdocs/core/class/html.form.class.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 82c2b13c337c3..47ffaa284137f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -43,10 +43,11 @@ For users: - New: [ task #498 ] Improvement of the block to add products/services lines. - New: ECM autodir works also for files joined to products and services. - New: Add a selection module for emailing to enter a recipient from gui. +- New: Allow to search product from barcodes directly from the permanent mini search left box +- New: Allow to search product from barcodes directly from invoices, proposals... throught AJAX New experimental modules: - New: Add margin and commissions management module. - New: Add holiday module. -- New: Allow to search product from barcodes directly from the permanent mini search left box - Fix: [ bug #499 ]: Supplier order input method not translated - Fix: No images into product description lines as PDF generation does diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 1901ff5965206..81fb49bdd6010 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -12,6 +12,7 @@ * Copyright (C) 2010 Juanjo Menent * Copyright (C) 2010 Philippe Grand * Copyright (C) 2011 Herve Prot + * Copyright (C) 2012 Marcos García * * 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 @@ -1196,6 +1197,11 @@ function select_produits_do($selected='',$htmlname='productid',$filtertype='',$l if (! empty($conf->global->MAIN_MULTILANGS)) $sql.=" OR pl.label LIKE '%".$filterkey."%'"; $sql.=")"; } + + if (! empty($conf->global->MAIN_MODULE_BARCODE)) + { + $sql .= " OR p.barcode LIKE '".$filterkey."'"; + } } $sql.= $db->order("p.ref"); $sql.= $db->plimit($limit); @@ -1441,6 +1447,11 @@ function select_produits_fournisseurs_do($socid,$selected='',$htmlname='producti { $sql.=" AND (pfp.ref_fourn LIKE '%".$filterkey."%' OR p.ref LIKE '%".$filterkey."%' OR p.label LIKE '%".$filterkey."%')"; } + + if (! empty($conf->global->MAIN_MODULE_BARCODE)) + { + $sql .= " OR p.barcode LIKE '".$filterkey."'"; + } } $sql.= " ORDER BY pfp.ref_fourn DESC"; From ded4e061e11e0ed6b53c08e2341782d400d02f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a?= Date: Mon, 10 Sep 2012 22:59:03 +0200 Subject: [PATCH 05/12] Typo --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 47ffaa284137f..a7e647fcc9962 100644 --- a/ChangeLog +++ b/ChangeLog @@ -44,7 +44,7 @@ For users: - New: ECM autodir works also for files joined to products and services. - New: Add a selection module for emailing to enter a recipient from gui. - New: Allow to search product from barcodes directly from the permanent mini search left box -- New: Allow to search product from barcodes directly from invoices, proposals... throught AJAX +- New: Allow to search product from barcodes directly from invoices, proposals... through AJAX New experimental modules: - New: Add margin and commissions management module. - New: Add holiday module. From de339278bc1592ea78500222b1aee45a8387b504 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 10 Sep 2012 15:43:58 +0200 Subject: [PATCH 06/12] Fix: removed warning --- htdocs/install/etape2.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/install/etape2.php b/htdocs/install/etape2.php index 0e07722c93d9a..9dfb54dbaefca 100644 --- a/htdocs/install/etape2.php +++ b/htdocs/install/etape2.php @@ -607,5 +607,5 @@ pFooter(!$ok,$setuplang); -$db->close(); -?> \ No newline at end of file +if (isset($db) && is_object($db)) $db->close(); +?> From 62b7ba607d3313dc6c9ffe8142763b9c3ba5d16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a?= Date: Mon, 10 Sep 2012 23:23:25 +0200 Subject: [PATCH 07/12] Better practice Replaced `empty($conf->global->MAIN_MODULE_BARCODE)` with `empty($conf->barcode->enabled)` --- htdocs/core/class/html.form.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 81fb49bdd6010..4af811aeb7c7a 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1198,7 +1198,7 @@ function select_produits_do($selected='',$htmlname='productid',$filtertype='',$l $sql.=")"; } - if (! empty($conf->global->MAIN_MODULE_BARCODE)) + if (! empty($conf->barcode->enabled)) { $sql .= " OR p.barcode LIKE '".$filterkey."'"; } @@ -1448,7 +1448,7 @@ function select_produits_fournisseurs_do($socid,$selected='',$htmlname='producti $sql.=" AND (pfp.ref_fourn LIKE '%".$filterkey."%' OR p.ref LIKE '%".$filterkey."%' OR p.label LIKE '%".$filterkey."%')"; } - if (! empty($conf->global->MAIN_MODULE_BARCODE)) + if (! empty($conf->barcode->enabled)) { $sql .= " OR p.barcode LIKE '".$filterkey."'"; } From b3235887d3efc77bf41aac684bef873fff12f797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a?= Date: Mon, 10 Sep 2012 23:50:46 +0200 Subject: [PATCH 08/12] Improved translations of menu edit --- htdocs/admin/menus/edit.php | 12 +++++++----- htdocs/langs/es_ES/admin.lang | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/htdocs/admin/menus/edit.php b/htdocs/admin/menus/edit.php index d0619ec80de49..b94a6014068a1 100644 --- a/htdocs/admin/menus/edit.php +++ b/htdocs/admin/menus/edit.php @@ -377,17 +377,19 @@ function init_topleft() print ''.$langs->trans('MenuModule').''.$menu->module.''.$langs->trans('DetailMenuModule').''; // Handler - print ''.$langs->trans('MenuHandler').''.$menu->menu_handler.''.$langs->trans('DetailMenuHandler').''; + if ($menu->menu_handler == 'all') $handler = $langs->trans('AllMenus'); + else $handler = $menu->menu_handler; + print ''.$langs->trans('MenuHandler').''.$handler.''.$langs->trans('DetailMenuHandler').''; // User print ''.$langs->trans('MenuForUsers').''.$langs->trans('DetailUser').''; // Type - print ''.$langs->trans('Type').''.$menu->type.''.$langs->trans('DetailType').''; + print ''.$langs->trans('Type').''.$langs->trans(ucfirst($menu->type)).''.$langs->trans('DetailType').''; // MenuId Parent print ''.$langs->trans('MenuIdParent').''; diff --git a/htdocs/langs/es_ES/admin.lang b/htdocs/langs/es_ES/admin.lang index 330efc3f346d9..0e15d03b8d8af 100644 --- a/htdocs/langs/es_ES/admin.lang +++ b/htdocs/langs/es_ES/admin.lang @@ -1259,7 +1259,7 @@ DetailMainmenu=Grupo al cual pertenece (obsoleto) DetailUrl=URL de la página hacia la cual el menú apunta DetailLeftmenu=Condición de visualización o no (obsoleto) DetailEnabled=Condición de mostrar o no -DetailRight=Condición de visualización completa o cristálida +DetailRight=Condición de visualización completa o restringida DetailLangs=Archivo .lang para la traducción del título DetailUser=Interno / Externo / Todos Target=Objetivo From f2393f08bc5ad242ea7329742333b7eefbdcb2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a?= Date: Tue, 11 Sep 2012 00:36:49 +0200 Subject: [PATCH 09/12] Translated paper formats and size units in PDF admin page --- htdocs/admin/pdf.php | 4 +++- htdocs/core/class/html.formadmin.class.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/htdocs/admin/pdf.php b/htdocs/admin/pdf.php index e1174df4f2b49..501e7d6968e2b 100755 --- a/htdocs/admin/pdf.php +++ b/htdocs/admin/pdf.php @@ -269,7 +269,9 @@ if ($resql) { $obj=$db->fetch_object($resql); - $pdfformatlabel=$obj->label.' - '.round($obj->width).'x'.round($obj->height).' '.$obj->unit; + $paperKey = $langs->trans('PaperFormat'.$obj->code); + $unitKey = $langs->trans('SizeUnit'.$obj->unit); + $pdfformatlabel = ($paperKey == 'PaperFormat'.$obj->code ? $obj->label : $paperKey).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey); } } print $pdfformatlabel; diff --git a/htdocs/core/class/html.formadmin.class.php b/htdocs/core/class/html.formadmin.class.php index e91c47fbb106e..f3af3b8a01054 100644 --- a/htdocs/core/class/html.formadmin.class.php +++ b/htdocs/core/class/html.formadmin.class.php @@ -339,7 +339,9 @@ function select_paper_format($selected='',$htmlname='paperformat_id',$filter=0,$ while ($i < $num) { $obj=$this->db->fetch_object($resql); - $paperformat[$obj->code]= $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.$obj->unit; + $unitKey = $langs->trans('SizeUnit'.$obj->unit); + + $paperformat[$obj->code]= $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey); $i++; } From e68f8619231a1291bb13ca47f710afeed3d23338 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 11 Sep 2012 01:09:34 +0200 Subject: [PATCH 10/12] New: Add more test and enhancement to make new calcul_price function more reliable. We need to add more PHPUnit test case on this function (testcase using localtax1 and localtax2). --- htdocs/compta/facture/class/facture.class.php | 2 + htdocs/core/lib/price.lib.php | 108 +++++++++--------- htdocs/install/mysql/data/llx_c_tva.sql | 38 +++--- test/phpunit/PricesTest.php | 27 +---- 4 files changed, 84 insertions(+), 91 deletions(-) diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index d14eadca5b5ad..dbf3bd849cbf1 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -110,6 +110,8 @@ class Facture extends CommonInvoice var $nbtodolate; var $specimen; + var $fac_rec; + /** * Constructor diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 227d3d54049cb..8dc4c54e560d9 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -20,25 +20,33 @@ /** * \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 $ + * \brief Library with functions to calculate prices */ /** * 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 $localtax1_rate Localtax1 rate - * @param float $localtax2_rate Localtax2 rate + * Value for localtaxtype are '0' : local tax not applied + * '1' : local tax apply on products and services without vat (vat is not applied on local tax) + * '2' : local tax apply on products and services before vat (vat is calculated on amount + localtax) + * '3' : local tax apply on products without vat (vat is not applied on local tax) + * '4' : local tax apply on products before vat (vat is calculated on amount + localtax) + * '5' : local tax apply on services without vat (vat is not applied on local tax) + * '6' : local tax apply on services before vat (vat is calculated on amount + localtax) + * '7' : local tax is a fix amount applied on global invoice + * + * @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 (used for some countries only, like spain) + * @param float $localtax2_rate 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 char $localtax1_type Localtax1 type - * @param char $localtax2_type Localtax2 type - + * @param int $type 0/1=Product/service + * @param string $localtax1_type Localtax1 type (used for some countries only, like spain) + * @param string $localtax2_type Localtax2 type (used for some countries only, like spain) * @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_rate=0, $localtax2_rate=0, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0, $localtax1_type = '0', $localtax2_type = '0') @@ -46,8 +54,8 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 global $conf,$mysoc; $result=array(); - - // initialize total + + // initialize total (may be HT or TTC depending on price_base_type) $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)); @@ -56,28 +64,28 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 for ($i=0; $i <= 15; $i++) $result[$i] = 0; - // if there's some localtax before vat + // if there's some localtax including vat, we calculate localtaxes (we will add later) $localtaxes = array(0,0,0); $apply_tax = false; switch($localtax1_type) { case '2': // localtax on product or service $apply_tax = true; break; - case '4': // localtax on product - if ($type == 0) $apply_tax = true; + case '4': // localtax on product + if ($type == 0) $apply_tax = true; break; case '6': // localtax on service - if ($type == 1) $apply_tax = true; + if ($type == 1) $apply_tax = true; break; } if ($apply_tax) { $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_rate / 100))) - $tot_avec_remise, 'MT'); $localtaxes[1] += $result[9]; - - $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MT'); + + $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); $localtaxes[2] += $result[11]; } @@ -86,21 +94,21 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 case '2': // localtax on product or service $apply_tax = true; break; - case '4': // localtax on product - if ($type == 0) $apply_tax = true; + case '4': // localtax on product + if ($type == 0) $apply_tax = true; break; case '6': // localtax on service - if ($type == 1) $apply_tax = true; + if ($type == 1) $apply_tax = true; break; } if ($apply_tax) { $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_rate / 100))) - $tot_avec_remise, 'MT'); $localtaxes[1] += $result[10]; - - $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MT'); + + $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); $localtaxes[2] += $result[12]; } @@ -116,7 +124,6 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 $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[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax) $result[3] = price2num($pu, 'MU'); @@ -135,7 +142,6 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 $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] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - HT $result[5] = price2num(($pu + $localtaxes[2]) , 'MU'); @@ -144,28 +150,28 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 $result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU'); } - // if there's some localtax without vat + // if there's some localtax without vat, we calculate localtaxes (we will add them at end) $apply_tax = false; switch($localtax1_type) { case '1': // localtax on product or service $apply_tax = true; break; - case '3': // localtax on product - if ($type == 0) $apply_tax = true; + case '3': // localtax on product + if ($type == 0) $apply_tax = true; break; case '5': // localtax on service - if ($type == 1) $apply_tax = true; + if ($type == 1) $apply_tax = true; break; } if ($apply_tax) { - $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_rate / 100))) - $tot_avec_remise, 'MT'); - $result[2] += $result[9]; - - $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); - $result[5] += $result[11]; + $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax1 for total_ht_without_discount + $result[8] += $result[14]; // total_ttc_without_discount + tax1 + + $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax1 for total_ht + $result[2] += $result[9]; // total_ttc + tax1 + + $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); // amount tax1 for pu_ht + $result[5] += $result[11]; // pu_ht + tax1 } $apply_tax = false; @@ -173,22 +179,22 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 case '1': // localtax on product or service $apply_tax = true; break; - case '3': // localtax on product - if ($type == 0) $apply_tax = true; + case '3': // localtax on product + if ($type == 0) $apply_tax = true; break; case '5': // localtax on service - if ($type == 1) $apply_tax = true; + if ($type == 1) $apply_tax = true; break; } if ($apply_tax) { - $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_rate / 100))) - $tot_avec_remise, 'MT'); - $result[2] += $result[10]; - - $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); - $result[5] += $result[12]; + $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax2 for total_ht_without_discount + $result[8] += $result[15]; // total_ttc_without_discount + tax2 + + $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax2 for total_ht + $result[2] += $result[10]; // total_ttc + tax2 + + $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); // amount tax2 for pu_ht + $result[5] += $result[12]; // pu_ht + tax2 } // If rounding is not using base 10 (rare) diff --git a/htdocs/install/mysql/data/llx_c_tva.sql b/htdocs/install/mysql/data/llx_c_tva.sql index 82348a15f89b3..a24c419f2f7ef 100644 --- a/htdocs/install/mysql/data/llx_c_tva.sql +++ b/htdocs/install/mysql/data/llx_c_tva.sql @@ -176,24 +176,24 @@ insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values (18 insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values (1883,188, '0','0','VAT Rate 0', 1); -- SAN SALVADOR (id country=86) -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (861, 86, '13', '0', 'IVA 13', 1); -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (862, 86, '0', '0', 'SIN IVA', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (861, 86, '13', '0', 'IVA 13', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (862, 86, '0', '0', 'SIN IVA', 1); -- SLOVAKIA (id country=201) -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (2011, 201, '19', '0', 'VAT standard rate', 1); -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (2012, 201, '10', '0', 'VAT reduced rate', 1); -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (2013, 201, '0', '0', 'VAT Rate 0', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2011, 201, '19', '0', 'VAT standard rate', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2012, 201, '10', '0', 'VAT reduced rate', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2013, 201, '0', '0', 'VAT Rate 0', 1); -- SLOVENIA (id country=202) -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (2021, 202, '20', '0', 'VAT standard rate', 1); -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (2022, 202,'8.5', '0', 'VAT reduced rate', 1); -INSERT INTO llx_c_tva(rowid, fk_pays, taux, recuperableonly, note, active) VALUES (2023, 202, '0', '0', 'VAT Rate 0', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2021, 202, '20', '0', 'VAT standard rate', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2022, 202,'8.5', '0', 'VAT reduced rate', 1); +INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) VALUES (2023, 202, '0', '0', 'VAT Rate 0', 1); -- SPAIN (id country=4) -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,note,active) values ( 41, 4, '21','0','5.2','VAT standard rate',1); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,note,active) values ( 42, 4, '10','0','1.4','VAT reduced rate',1); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,note,active) values ( 43, 4, '4','0','0.5','VAT super-reduced rate',1); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values ( 44, 4, '0','0','VAT Rate 0',1); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,note,active) values ( 41, 4, '21','0','5.2','1','VAT standard rate',1); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,note,active) values ( 42, 4, '10','0','1.4','1','VAT reduced rate',1); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,localtax1,localtax1_type,note,active) values ( 43, 4, '4','0','0.5','1','VAT super-reduced rate',1); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values ( 44, 4, '0','0','VAT Rate 0',1); -- SWEDEN (id country=20) insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values (201,20, '25','0','VAT standard rate',1); @@ -208,13 +208,13 @@ insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values ( 6 insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values ( 64, 6, '0','0','VAT Rate 0', 1); -- TUNISIA (id country=10) -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (101,10, '6','0','VAT 6%', 1, 1, '4', 0.4, '7'); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (102,10, '12','0','VAT 12%',1, 1, '4', 0.4, '7'); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (103,10, '18','0','VAT 18%',1, 1, '4', 0.4, '7'); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (104,10, '7.5','0','VAT 6% Majoré à 25% (7.5%)',1, 1, '4', 0.4, '7'); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (105,10, '15','0','VAT 12% Majoré à 25% (15%)',1, 1, '4', 0.4, '7'); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (106,10, '22.5','0','VAT 18% Majoré à 25% (22.5%)',1, 1, '4', 0.4, '7'); -insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1, localtax1_type, localtax2, localtax2_type) values (107,10, '0','0','VAT Rate 0', 1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (101,10, '6','0','VAT 6%', 1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (102,10, '12','0','VAT 12%',1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (103,10, '18','0','VAT 18%',1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (104,10, '7.5','0','VAT 6% Majoré à 25% (7.5%)',1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (105,10, '15','0','VAT 12% Majoré à 25% (15%)',1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (106,10, '22.5','0','VAT 18% Majoré à 25% (22.5%)',1, 1, '4', 0.4, '7'); +insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active,localtax1,localtax1_type,localtax2,localtax2_type) values (107,10, '0','0','VAT Rate 0', 1, 1, '4', 0.4, '7'); -- UKRAINE (id country=226) INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values (2261,226, '20','0','VAT standart rate',1); diff --git a/test/phpunit/PricesTest.php b/test/phpunit/PricesTest.php index d0be9c47e2780..9268707af4dc2 100755 --- a/test/phpunit/PricesTest.php +++ b/test/phpunit/PricesTest.php @@ -124,31 +124,16 @@ protected function tearDown() */ public function testCalculPriceTotal() { - // Line 1 // qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT' $result1=calcul_price_total(1, 1.24, 0, 10, 0, 0, 0, 'HT', 0); + print __METHOD__." result1=".join(', ',$result1)."\n"; // 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) + $this->assertEquals(array(1.24, 0.12, 1.36, 1.24, 0.124, 1.364, 1.24, 0.12, 1.36, 0, 0, 0, 0, 0, 0, 0),$result1); - print __METHOD__." value0=1.24 result0=".$result1[0]."\n"; - $this->assertEquals(1.24,$result1[0]); - print __METHOD__." value1=0.12 result1=".$result1[1]."\n"; - $this->assertEquals(0.12,$result1[1]); - print __METHOD__." value2=1.36 result2=".$result1[2]."\n"; - $this->assertEquals(1.36,$result1[2]); - - print __METHOD__." value3=1.24 result3=".$result1[3]."\n"; - $this->assertEquals(1.24, $result1[3]); - print __METHOD__." value4=0.124 result4=".$result1[4]."\n"; - $this->assertEquals(0.124,$result1[4]); - print __METHOD__." value5=1.364 result5=".$result1[5]."\n"; - $this->assertEquals(1.364,$result1[5]); - - print __METHOD__." value6=1.24 result6=".$result1[6]."\n"; - $this->assertEquals(1.24,$result1[6]); - print __METHOD__." value7=0.12 result7=".$result1[7]."\n"; - $this->assertEquals(0.12,$result1[7]); - print __METHOD__." value8=1.36 result8=".$result1[8]."\n"; - $this->assertEquals(1.36,$result1[8]); + // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 1, 0% localtax2 type 0 + $result2=calcul_price_total(10, 10, 0, 10, 1.4, 0, 0, 'HT', 0, 0, 1, 0); + print __METHOD__." result2=".join(', ',$result2)."\n"; + $this->assertEquals(array(100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0, 0.14, 0, 0, 1.4, 0),$result2); return true; } From ef00d592e722b7cce501cae7de121cbaa3a29f8d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 11 Sep 2012 01:42:50 +0200 Subject: [PATCH 11/12] Fix: Backward compatibility with spain. --- htdocs/core/lib/price.lib.php | 34 +++++++++++++++++++++++----------- test/phpunit/PricesTest.php | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 8dc4c54e560d9..01e04558cd93f 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -26,14 +26,14 @@ /** * Calculate totals (net, vat, ...) of a line. - * Value for localtaxtype are '0' : local tax not applied - * '1' : local tax apply on products and services without vat (vat is not applied on local tax) - * '2' : local tax apply on products and services before vat (vat is calculated on amount + localtax) - * '3' : local tax apply on products without vat (vat is not applied on local tax) - * '4' : local tax apply on products before vat (vat is calculated on amount + localtax) - * '5' : local tax apply on services without vat (vat is not applied on local tax) - * '6' : local tax apply on services before vat (vat is calculated on amount + localtax) - * '7' : local tax is a fix amount applied on global invoice + * Value for localtaxX_type are '0' : local tax not applied + * '1' : local tax apply on products and services without vat (vat is not applied on local tax) + * '2' : local tax apply on products and services before vat (vat is calculated on amount + localtax) + * '3' : local tax apply on products without vat (vat is not applied on local tax) + * '4' : local tax apply on products before vat (vat is calculated on amount + localtax) + * '5' : local tax apply on services without vat (vat is not applied on local tax) + * '6' : local tax apply on services before vat (vat is calculated on amount + localtax) + * '7' : local tax is a fix amount applied on global invoice * * @param int $qty Quantity * @param float $pu Unit price (HT or TTC selon price_base_type) @@ -47,14 +47,26 @@ * @param int $type 0/1=Product/service * @param string $localtax1_type Localtax1 type (used for some countries only, like spain) * @param string $localtax2_type Localtax2 type (used for some countries only, like spain) - * @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) + * @return result[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] (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_rate=0, $localtax2_rate=0, $remise_percent_global=0, $price_base_type='HT', $info_bits=0, $type=0, $localtax1_type = '0', $localtax2_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 = '?', $localtax2_type = '?') { global $conf,$mysoc; $result=array(); + // TODO Remove this code. Added for backward compatibility. To remove once localtaxX_type is provided by caller. + if ($localtax1_type == '?') + { + if ($mysoc->country_code=='ES') $localtax1_type='1'; + else $localtax1_type='0'; + } + if ($localtax2_type == '?') + { + if ($mysoc->country_code=='ES') $localtax2_type='1'; + else $localtax2_type='0'; + } + // initialize total (may be HT or TTC depending on price_base_type) $tot_sans_remise = $pu * $qty; $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100)); @@ -144,7 +156,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1 $result0bis= price2num(($tot_avec_remise + $localtaxes[1]) / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) $result[1] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - HT - $result[5] = price2num(($pu + $localtaxes[2]) , 'MU'); + $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] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU'); diff --git a/test/phpunit/PricesTest.php b/test/phpunit/PricesTest.php index 9268707af4dc2..4029704d0bb6e 100755 --- a/test/phpunit/PricesTest.php +++ b/test/phpunit/PricesTest.php @@ -124,7 +124,13 @@ protected function tearDown() */ public function testCalculPriceTotal() { - // qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT' + global $conf,$user,$langs,$db; + $this->savconf=$conf; + $this->savuser=$user; + $this->savlangs=$langs; + $this->savdb=$db; + + // qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT' $result1=calcul_price_total(1, 1.24, 0, 10, 0, 0, 0, 'HT', 0); print __METHOD__." result1=".join(', ',$result1)."\n"; // 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) @@ -135,6 +141,14 @@ public function testCalculPriceTotal() print __METHOD__." result2=".join(', ',$result2)."\n"; $this->assertEquals(array(100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0, 0.14, 0, 0, 1.4, 0),$result2); + // Old function for spain countries. To check backward compatibility. + global $mysoc; + $mysoc=new Societe($db); + $mysoc->country_code='ES'; + $result3=calcul_price_total(10, 10, 0, 10, 1.4, 0, 0, 'HT', 0); // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1, 0% localtax2 + print __METHOD__." result3=".join(', ',$result3)."\n"; + $this->assertEquals(array(100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0, 0.14, 0, 0, 1.4, 0),$result3); + return true; } From 12b42ab1b49684269af8232c614ba03d43bfc541 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 11 Sep 2012 01:53:15 +0200 Subject: [PATCH 12/12] Doc --- htdocs/core/lib/price.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 01e04558cd93f..8e7d951514c06 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -39,8 +39,8 @@ * @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 (used for some countries only, like spain) - * @param float $localtax2_rate Localtax2 rate (used for some countries only, like spain) + * @param float $localtax1_rate Localtax1 rate (used for some countries only, like spain). Can also be negative + * @param float $localtax2_rate Localtax2 rate (used for some countries only, like spain). Can also be negative * @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