From 1abcd5f4fe934a852457bd573fb22a44bb4b1e72 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sun, 26 Apr 2015 16:58:00 +0200
Subject: [PATCH] Fix test for vat calculation

---
 test/phpunit/PricesTest.php | 147 +++++++++++++++++++++++++++---------
 1 file changed, 110 insertions(+), 37 deletions(-)

diff --git a/test/phpunit/PricesTest.php b/test/phpunit/PricesTest.php
index 5f0b503d911..b8a942d5899 100755
--- a/test/phpunit/PricesTest.php
+++ b/test/phpunit/PricesTest.php
@@ -1,20 +1,21 @@
 <?php
-/* Copyright (C) 2010 Laurent Destailleur  <eldy@users.sourceforge.net>
+/* Copyright (C) 2010 Laurent Destailleur	<eldy@users.sourceforge.net>
+ * Copyright (C) 2015 Juanjo Menent			<jmenent@2byte.es>
+ *
+ * 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
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
-* 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
-* the Free Software Foundation; either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-*
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  * or see http://www.gnu.org/
-*/
+ */
 
 /**
  *      \file       test/phpunit/PricesTest.php
@@ -81,6 +82,8 @@ class PricesTest extends PHPUnit_Framework_TestCase
 
         print __METHOD__."\n";
     }
+
+    // tear down after class
     public static function tearDownAfterClass()
     {
         global $conf,$user,$langs,$db;
@@ -133,51 +136,121 @@ class PricesTest extends PHPUnit_Framework_TestCase
 		global $mysoc;
 		$mysoc=new Societe($db);
 
+		/*
+		 *  Country France
+		 */
+
 		// qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT' (method we provide value)
 		$mysoc->country_code='FR';
 		$mysoc->country_id=1;
 		$result1=calcul_price_total(1, 1.24, 0, 10, 0, 0, 0, 'HT', 0, 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,'Test1');
+        $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,'Test1 FR');
+
+
+        /*
+		 *  Country Spain
+		 */
+
+        // 10 * 10 HT - 0% discount with 10% vat, seller not using localtax1, not localtax2 (method we provide value)
+		$mysoc->country_code='ES';
+		$mysoc->country_id=4;
+		$mysoc->localtax1_assuj=0;
+		$mysoc->localtax2_assuj=0;
+		$result2=calcul_price_total(10, 10, 0, 10, 0, 0, 0, 'HT', 0, 0);	// 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1, 0% localtax2
+        print __METHOD__." result2=".join(', ',$result2)."\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(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0),$result2,'Test1 ES');
 
-        // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 1, 0% localtax2 type 0 (method we provide value)
+        // 10 * 10 HT - 0% discount with 10% vat, seller not using localtax1, not localtax2 (other method autodetect)
+		$mysoc->country_code='ES';
+		$mysoc->country_id=4;
+		$mysoc->localtax1_assuj=0;
+		$mysoc->localtax2_assuj=0;
+		$result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 0);	// 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1, 0% localtax2
+        print __METHOD__." result2=".join(', ',$result2)."\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(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0),$result2,'Test2 ES');
+
+        // --------------------------------------------------------
+
+        // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 3, 0% localtax2 type 5 (method we provide value)
 		$mysoc->country_code='ES';
 		$mysoc->country_id=4;
 		$mysoc->localtax1_assuj=1;
 		$mysoc->localtax2_assuj=0;
         $result2=calcul_price_total(10, 10, 0, 10, 1.4, 0, 0, 'HT', 0, 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,'Test2');
+		// 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(100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0, 0.14, 0, 0, 1.4, 0),$result2,'Test3 ES');
 
-        // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 1, 0% localtax2 type 0 (other method autodetect)
+        // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 3, 0% localtax2 type 5 (other method autodetect)
 		$mysoc->country_code='ES';
 		$mysoc->country_id=4;
 		$mysoc->localtax1_assuj=1;
 		$mysoc->localtax2_assuj=0;
         $result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 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,'Test3 ES');
+		// 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(100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0, 0.14, 0, 0, 1.4, 0),$result2,'Test4 ES');
+
+        // --------------------------------------------------------
 
-        // 10 * 10 HT - 0% discount with 10% vat, seller not using localtax1, nor localtax2 (method we provide value)
+        // 10 * 10 HT - 0% discount with 10% vat and 0% localtax1 type 3, 19% localtax2 type 5 (method we provide value), we provide a service and not a product
 		$mysoc->country_code='ES';
 		$mysoc->country_id=4;
 		$mysoc->localtax1_assuj=0;
-		$mysoc->localtax2_assuj=0;
-		$result3=calcul_price_total(10, 10, 0, 10, 0, 0, 0, 'HT', 0, 0);	// 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1, 0% localtax2
-        print __METHOD__." result3=".join(', ',$result3)."\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(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0),$result3,'Test4');
+		$mysoc->localtax2_assuj=1;
+        $result2=calcul_price_total(10, 10, 0, 10, 0, -19, 0, 'HT', 0, 1);
+		// 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(100, 10, 91, 10, 1, 9.1, 100, 10, 91, 0, -19, 0, -1.90, 0, 0, -19),$result2,'Test5 ES for service');
 
-        // 10 * 10 HT - 0% discount with 10% vat, seller not using localtax1, nor localtax2 (other method autodetect)
+        // 10 * 10 HT - 0% discount with 10% vat and 0% localtax1 type 3, 21% localtax2 type 5 (other method autodetect), we provide a service and not a product
 		$mysoc->country_code='ES';
 		$mysoc->country_id=4;
 		$mysoc->localtax1_assuj=0;
-		$mysoc->localtax2_assuj=0;
-		$result3=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 0);	// 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1, 0% localtax2
-        print __METHOD__." result3=".join(', ',$result3)."\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(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0),$result3,'Test5');
+		$mysoc->localtax2_assuj=1;
+        $result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 0);
+		print __METHOD__." result2=".join(', ',$result2)."\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(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0),$result2,'Test6 ES for product');
+
+        // 10 * 10 HT - 0% discount with 10% vat and 0% localtax1 type 3, 21% localtax2 type 5 (other method autodetect), we provide a product and not a service
+		$mysoc->country_code='ES';
+		$mysoc->country_id=4;
+		$mysoc->localtax1_assuj=0;
+		$mysoc->localtax2_assuj=1;
+        $result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 1);
+		print __METHOD__." result2=".join(', ',$result2)."\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(100, 10, 91, 10, 1, 9.1, 100, 10, 91, 0, -19, 0, -1.90, 0, 0, -19),$result2,'Test6 ES for service');
+
+		// --------------------------------------------------------
+
+        // Credit Note: 10 * -10 HT - 0% discount with 10% vat and 0% localtax1 type 3, 19% localtax2 type 5 (method we provide value), we provide a product and not a service
+		$mysoc->country_code='ES';
+		$mysoc->country_id=4;
+		$mysoc->localtax1_assuj=0;
+		$mysoc->localtax2_assuj=1;
+        $result2=calcul_price_total(10, -10, 0, 10, 0, 19, 0, 'HT', 0, 0);
+		print __METHOD__." result2=".join(', ',$result2)."\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(-100, -10, -110, -10, -1, -11, -100, -10, -110, 0, 0, 0, 0, 0, 0, 0),$result2,'Test7 ES for product');
+
+        // Credit Note: 10 * -10 HT - 0% discount with 10% vat and 1.4% localtax1 type 3, 0% localtax2 type 5 (other method autodetect), we provide a service and not a product
+		$mysoc->country_code='ES';
+		$mysoc->country_id=4;
+		$mysoc->localtax1_assuj=0;
+		$mysoc->localtax2_assuj=1;
+        $result2=calcul_price_total(10, -10, 0, 10, -1, -1, 0, 'HT', 0, 1);
+		print __METHOD__." result2=".join(', ',$result2)."\n";
+		$this->assertEquals(array(-100, -10, -91, -10, -1, -9.1, -100, -10, -91, 0, 19, 0, 1.90, 0, 0, 19),$result2,'Test8 ES for service');
+
+
+        /*
+         * Country Côte d'Ivoire
+         */
 
         // 10 * 10 HT - 0% discount with 18% vat, seller using localtax1 type 2, not localtax2 (method we provide value)
 		$mysoc->country_code='CI';
@@ -189,8 +262,8 @@ class PricesTest extends PHPUnit_Framework_TestCase
 		$result3=calcul_price_total(10, 10, 0, 18, 7.5, 0, 0, 'HT', 0, 0);	// 10 * 10 HT - 0% discount with 18% vat and 7.5% localtax1, 0% localtax2
         print __METHOD__." result3=".join(', ',$result3)."\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(100, 18, 126.85, 10, 1.8, 12.685, 100, 18, 126.85, 8.85, 0, 0.885, 0, 0, 8.85, 0),$result3,'Test6 CI');
-        
+        $this->assertEquals(array(100, 18, 126.85, 10, 1.8, 12.685, 100, 18, 126.85, 8.85, 0, 0.885, 0, 0, 8.85, 0),$result3,'Test9 CI');
+
         // 10 * 10 HT - 0% discount with 18% vat, seller using localtax1 type 2, not localtax2 (other method autodetect)
 		$mysoc->country_code='CI';
 		$mysoc->country_id=21;
@@ -199,8 +272,8 @@ class PricesTest extends PHPUnit_Framework_TestCase
 		$result3=calcul_price_total(10, 10, 0, 18, -1, -1, 0, 'HT', 0, 0);	// 10 * 10 HT - 0% discount with 18% vat and 7.5% localtax1, 0% localtax2
         print __METHOD__." result3=".join(', ',$result3)."\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(100, 18, 126.85, 10, 1.8, 12.685, 100, 18, 126.85, 8.85, 0, 0.885, 0, 0, 8.85, 0),$result3,'Test7 CI');
-        
+        $this->assertEquals(array(100, 18, 126.85, 10, 1.8, 12.685, 100, 18, 126.85, 8.85, 0, 0.885, 0, 0, 8.85, 0),$result3,'Test10 CI');
+
         return true;
     }
 
@@ -233,9 +306,9 @@ class PricesTest extends PHPUnit_Framework_TestCase
         $newlocalobject=new Facture($this->savdb);
         $newlocalobject->fetch($invoiceid);
 
-        $this->assertEquals(2.48,$newlocalobject->total_ht);
-        $this->assertEquals(0.24,$newlocalobject->total_tva);
-        $this->assertEquals(2.72,$newlocalobject->total_ttc);
+        $this->assertEquals(2.48,$newlocalobject->total_ht, "testUpdatePrice test1");
+        $this->assertEquals(0.24,$newlocalobject->total_tva, "testUpdatePrice test2");
+        $this->assertEquals(2.72,$newlocalobject->total_ttc, "testUpdatePrice test3");
 
 
         // Two lines of 1.24 give 2.48 HT and 2.73 TTC with global vat rounding mode
@@ -249,7 +322,7 @@ class PricesTest extends PHPUnit_Framework_TestCase
         $newlocalobject=new Facture($this->savdb);
         $newlocalobject->fetch($invoiceid);
 
-        $this->assertEquals(2.48,$newlocalobject->total_ht);
+        $this->assertEquals(2.48,$newlocalobject->total_ht, "testUpdatePrice test4");
         //$this->assertEquals(0.25,$newlocalobject->total_tva);
         //$this->assertEquals(2.73,$newlocalobject->total_ttc);
     }
-- 
GitLab