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).
This commit is contained in:
Laurent Destailleur 2012-09-11 01:09:34 +02:00
parent b6239427f6
commit e68f861923
4 changed files with 84 additions and 91 deletions

View File

@ -110,6 +110,8 @@ class Facture extends CommonInvoice
var $nbtodolate; var $nbtodolate;
var $specimen; var $specimen;
var $fac_rec;
/** /**
* Constructor * Constructor

View File

@ -20,25 +20,33 @@
/** /**
* \file htdocs/core/lib/price.lib.php * \file htdocs/core/lib/price.lib.php
* \brief Librairie contenant les fonctions pour calculer un prix. * \brief Library with functions to calculate prices
* \version $Id: price.lib.php,v 1.36 2011/07/31 23:26:01 eldy Exp $
*/ */
/** /**
* Calculate totals (net, vat, ...) of a line. * 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
*
* @param int $qty Quantity * @param int $qty Quantity
* @param float $pu Unit price (HT or TTC selon price_base_type) * @param float $pu Unit price (HT or TTC selon price_base_type)
* @param float $remise_percent_ligne Discount for line * @param float $remise_percent_ligne Discount for line
* @param float $txtva Vat rate * @param float $txtva Vat rate
* @param float $localtax1_rate Localtax1 rate * @param float $localtax1_rate Localtax1 rate (used for some countries only, like spain)
* @param float $localtax2_rate Localtax2 rate * @param float $localtax2_rate Localtax2 rate (used for some countries only, like spain)
* @param float $remise_percent_global 0 * @param float $remise_percent_global 0
* @param string $price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC * @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 int $info_bits Miscellanous informations on line
* @param char $localtax1_type Localtax1 type * @param int $type 0/1=Product/service
* @param char $localtax2_type Localtax2 type * @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] (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 = '0', $localtax2_type = '0')
@ -47,7 +55,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
$result=array(); $result=array();
// initialize total // initialize total (may be HT or TTC depending on price_base_type)
$tot_sans_remise = $pu * $qty; $tot_sans_remise = $pu * $qty;
$tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 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)); $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
@ -56,7 +64,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
for ($i=0; $i <= 15; $i++) for ($i=0; $i <= 15; $i++)
$result[$i] = 0; $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); $localtaxes = array(0,0,0);
$apply_tax = false; $apply_tax = false;
switch($localtax1_type) { switch($localtax1_type) {
@ -77,7 +85,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
$result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT');
$localtaxes[1] += $result[9]; $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]; $localtaxes[2] += $result[11];
} }
@ -100,7 +108,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
$result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT');
$localtaxes[1] += $result[10]; $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]; $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[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 $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) $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[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
$result[3] = price2num($pu, 'MU'); $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[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 $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) $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[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');
@ -144,7 +150,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
$result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU'); $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; $apply_tax = false;
switch($localtax1_type) { switch($localtax1_type) {
case '1': // localtax on product or service case '1': // localtax on product or service
@ -158,14 +164,14 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
break; break;
} }
if ($apply_tax) { if ($apply_tax) {
$result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT'); $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]; $result[8] += $result[14]; // total_ttc_without_discount + tax1
$result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax1 for total_ht
$result[2] += $result[9]; $result[2] += $result[9]; // total_ttc + tax1
$result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); // amount tax1 for pu_ht
$result[5] += $result[11]; $result[5] += $result[11]; // pu_ht + tax1
} }
$apply_tax = false; $apply_tax = false;
@ -181,14 +187,14 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $localtax1
break; break;
} }
if ($apply_tax) { if ($apply_tax) {
$result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise, 'MT'); $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]; $result[8] += $result[15]; // total_ttc_without_discount + tax2
$result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax2 for total_ht
$result[2] += $result[10]; $result[2] += $result[10]; // total_ttc + tax2
$result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); // amount tax2 for pu_ht
$result[5] += $result[12]; $result[5] += $result[12]; // pu_ht + tax2
} }
// If rounding is not using base 10 (rare) // If rounding is not using base 10 (rare)

View File

@ -176,23 +176,23 @@ 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); 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) -- 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 (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 (862, 86, '0', '0', 'SIN IVA', 1);
-- SLOVAKIA (id country=201) -- 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 (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 (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 (2013, 201, '0', '0', 'VAT Rate 0', 1);
-- SLOVENIA (id country=202) -- 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 (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 (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 (2023, 202, '0', '0', 'VAT Rate 0', 1);
-- SPAIN (id country=4) -- 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,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,note,active) values ( 42, 4, '10','0','1.4','VAT reduced 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,note,active) values ( 43, 4, '4','0','0.5','VAT super-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); 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) -- SWEDEN (id country=20)
@ -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); 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) -- 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 (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 (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 (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 (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 (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 (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 (107,10, '0','0','VAT Rate 0', 1, 1, '4', 0.4, '7');
-- UKRAINE (id country=226) -- 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); INSERT INTO llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values (2261,226, '20','0','VAT standart rate',1);

View File

@ -124,31 +124,16 @@ class PricesTest extends PHPUnit_Framework_TestCase
*/ */
public function testCalculPriceTotal() public function testCalculPriceTotal()
{ {
// Line 1
// qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT' // 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); $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) // 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"; // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 1, 0% localtax2 type 0
$this->assertEquals(1.24,$result1[0]); $result2=calcul_price_total(10, 10, 0, 10, 1.4, 0, 0, 'HT', 0, 0, 1, 0);
print __METHOD__." value1=0.12 result1=".$result1[1]."\n"; print __METHOD__." result2=".join(', ',$result2)."\n";
$this->assertEquals(0.12,$result1[1]); $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);
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]);
return true; return true;
} }