PricesTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file test/phpunit/PricesTest.php
  22. * \ingroup test
  23. * \brief PHPUnit test
  24. * \remarks To run this script as CLI: phpunit filename.php
  25. */
  26. global $conf,$user,$langs,$db;
  27. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  28. //require_once 'PHPUnit/Autoload.php';
  29. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  30. require_once dirname(__FILE__).'/../../htdocs/core/lib/price.lib.php';
  31. require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.class.php';
  32. if (empty($user->id)) {
  33. print "Load permissions for admin user nb 1\n";
  34. $user->fetch(1);
  35. $user->getrights();
  36. }
  37. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  38. if (getDolGlobalString('MAIN_ROUNDING_RULE_TOT')) {
  39. print "Parameter MAIN_ROUNDING_RULE_TOT must be set to 0 or not set.\n";
  40. exit(1);
  41. }
  42. /**
  43. * Class for PHPUnit tests
  44. *
  45. * @backupGlobals disabled
  46. * @backupStaticAttributes enabled
  47. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  48. */
  49. class PricesTest extends PHPUnit\Framework\TestCase
  50. {
  51. protected $savconf;
  52. protected $savuser;
  53. protected $savlangs;
  54. protected $savdb;
  55. /**
  56. * Constructor
  57. * We save global variables into local variables
  58. *
  59. * @param string $name Name
  60. * @return PriceTest
  61. */
  62. public function __construct($name = '')
  63. {
  64. parent::__construct($name);
  65. //$this->sharedFixture
  66. global $conf,$user,$langs,$db;
  67. $this->savconf=$conf;
  68. $this->savuser=$user;
  69. $this->savlangs=$langs;
  70. $this->savdb=$db;
  71. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  72. //print " - db ".$db->db;
  73. print "\n";
  74. }
  75. /**
  76. * setUpBeforeClass
  77. *
  78. * @return void
  79. */
  80. public static function setUpBeforeClass(): void
  81. {
  82. global $conf,$user,$langs,$db;
  83. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  84. print __METHOD__."\n";
  85. }
  86. /**
  87. * tearDownAfterClass
  88. *
  89. * @return void
  90. */
  91. public static function tearDownAfterClass(): void
  92. {
  93. global $conf,$user,$langs,$db;
  94. //$db->rollback();
  95. print __METHOD__."\n";
  96. }
  97. /**
  98. * Init phpunit tests
  99. *
  100. * @return void
  101. */
  102. protected function setUp(): void
  103. {
  104. global $conf,$user,$langs,$db;
  105. $conf=$this->savconf;
  106. $user=$this->savuser;
  107. $langs=$this->savlangs;
  108. $db=$this->savdb;
  109. print __METHOD__."\n";
  110. }
  111. /**
  112. * End phpunit tests
  113. *
  114. * @return void
  115. */
  116. protected function tearDown(): void
  117. {
  118. print __METHOD__."\n";
  119. }
  120. /**
  121. * Test function calcul_price_total
  122. *
  123. * @return boolean
  124. * @see http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
  125. */
  126. public function testCalculPriceTotal()
  127. {
  128. global $conf,$user,$langs,$db;
  129. $this->savconf=$conf;
  130. $this->savuser=$user;
  131. $this->savlangs=$langs;
  132. $this->savdb=$db;
  133. global $mysoc;
  134. $mysoc=new Societe($db);
  135. /*
  136. * Country France
  137. */
  138. // qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT' (method we provide value)
  139. $mysoc->country_code='FR';
  140. $mysoc->country_id=1;
  141. $result1=calcul_price_total(1, 1.24, 0, 10, 0, 0, 0, 'HT', 0, 0);
  142. print __METHOD__." result1=".join(', ', $result1)."\n";
  143. // 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)
  144. $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, 1.24, 0.12, 1.36, 1.24, 0.124, 1.364, 1.24, 0.12, 1.36, 0, 0), $result1, 'Test1 FR');
  145. // qty=1, unit_price=1.24, discount_line=0, vat_rate=10, price_base_type='HT', multicurrency_tx=1.09205 (method we provide value)
  146. $mysoc->country_code='FR';
  147. $mysoc->country_id=1;
  148. $result1=calcul_price_total(2, 8.56, 0, 10, 0, 0, 0, 'HT', 0, 0, '', '', 100, 1.09205);
  149. print __METHOD__." result1=".join(', ', $result1)."\n";
  150. // 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)
  151. $this->assertEquals(array(17.12, 1.71, 18.83, 8.56, 0.856, 9.416, 17.12, 1.71, 18.83, 0, 0, 0, 0, 0, 0, 0, 18.7, 1.87, 20.57, 9.34795, 0.93479, 10.28274, 18.7, 1.87, 20.57, 0, 0), $result1, 'Test1b FR');
  152. // qty=2, unit_price=0, discount_line=0, vat_rate=10, price_base_type='HT', multicurrency_tx=1.09205 (method we provide value), pu_ht_devise=100
  153. $mysoc->country_code='FR';
  154. $mysoc->country_id=1;
  155. $result1=calcul_price_total(2, 0, 0, 10, 0, 0, 0, 'HT', 0, 0, '', '', 100, 1.09205, 20);
  156. print __METHOD__." result1=".join(', ', $result1)."\n";
  157. // 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)
  158. $this->assertEquals(array(36.63, 3.66, 40.29, 18.31418, 1.83142, 20.1456, 36.63, 3.66, 40.29, 0, 0, 0, 0, 0, 0, 0, 40, 4, 44, 20, 2, 22, 40, 4, 44, 0, 0), $result1, 'Test1c FR');
  159. /*
  160. * Country Spain
  161. */
  162. // 10 * 10 HT - 0% discount with 10% vat, seller not using localtax1, not localtax2 (method we provide value)
  163. $mysoc->country_code='ES';
  164. $mysoc->country_id=4;
  165. $mysoc->localtax1_assuj=0;
  166. $mysoc->localtax2_assuj=0;
  167. $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
  168. print __METHOD__." result2=".join(', ', $result2)."\n";
  169. // 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)
  170. $this->assertEquals(array(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0, 100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0), $result2, 'Test1 ES');
  171. // 10 * 10 HT - 0% discount with 10% vat, seller not using localtax1, not localtax2 (other method autodetect)
  172. $mysoc->country_code='ES';
  173. $mysoc->country_id=4;
  174. $mysoc->localtax1_assuj=0;
  175. $mysoc->localtax2_assuj=0;
  176. $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
  177. print __METHOD__." result2=".join(', ', $result2)."\n";
  178. // 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)
  179. $this->assertEquals(array(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0, 100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0), $result2, 'Test2 ES');
  180. // --------------------------------------------------------
  181. // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 3, 0% localtax2 type 5 (method we provide value)
  182. $mysoc->country_code='ES';
  183. $mysoc->country_id=4;
  184. $mysoc->localtax1_assuj=1;
  185. $mysoc->localtax2_assuj=0;
  186. $result2=calcul_price_total(10, 10, 0, 10, 1.4, 0, 0, 'HT', 0, 0);
  187. print __METHOD__." result2=".join(', ', $result2)."\n";
  188. // 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)
  189. $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, 100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0), $result2, 'Test3 ES');
  190. // 10 * 10 HT - 0% discount with 10% vat and 1.4% localtax1 type 3, 0% localtax2 type 5 (other method autodetect)
  191. $mysoc->country_code='ES';
  192. $mysoc->country_id=4;
  193. $mysoc->localtax1_assuj=1;
  194. $mysoc->localtax2_assuj=0;
  195. $result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 0);
  196. print __METHOD__." result2=".join(', ', $result2)."\n";
  197. // 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)
  198. $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, 100, 10, 111.4, 10, 1, 11.14, 100, 10, 111.4, 1.4, 0), $result2, 'Test4 ES');
  199. // --------------------------------------------------------
  200. // 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
  201. $mysoc->country_code='ES';
  202. $mysoc->country_id=4;
  203. $mysoc->localtax1_assuj=0;
  204. $mysoc->localtax2_assuj=1;
  205. $result2=calcul_price_total(10, 10, 0, 10, 0, -19, 0, 'HT', 0, 1);
  206. // 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)
  207. $this->assertEquals(array(100, 10, 91, 10, 1, 9.1, 100, 10, 91, 0, -19, 0, -1.90, 0, 0, -19, 100, 10, 91, 10, 1, 9.1, 100, 10, 91, 0, -19), $result2, 'Test5 ES for service');
  208. // 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
  209. $mysoc->country_code='ES';
  210. $mysoc->country_id=4;
  211. $mysoc->localtax1_assuj=0;
  212. $mysoc->localtax2_assuj=1;
  213. $result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 0);
  214. print __METHOD__." result2=".join(', ', $result2)."\n";
  215. // 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)
  216. $this->assertEquals(array(100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0, 0, 0, 0, 0, 0, 100, 10, 110, 10, 1, 11, 100, 10, 110, 0, 0), $result2, 'Test6 ES for product');
  217. // 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
  218. $mysoc->country_code='ES';
  219. $mysoc->country_id=4;
  220. $mysoc->localtax1_assuj=0;
  221. $mysoc->localtax2_assuj=1;
  222. $result2=calcul_price_total(10, 10, 0, 10, -1, -1, 0, 'HT', 0, 1);
  223. print __METHOD__." result2=".join(', ', $result2)."\n";
  224. // 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)
  225. $this->assertEquals(array(100, 10, 91, 10, 1, 9.1, 100, 10, 91, 0, -19, 0, -1.90, 0, 0, -19, 100, 10, 91, 10, 1, 9.1, 100, 10, 91, 0, -19), $result2, 'Test6 ES for service');
  226. // --------------------------------------------------------
  227. // 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
  228. $mysoc->country_code='ES';
  229. $mysoc->country_id=4;
  230. $mysoc->localtax1_assuj=0;
  231. $mysoc->localtax2_assuj=1;
  232. $result2=calcul_price_total(10, -10, 0, 10, 0, 19, 0, 'HT', 0, 0);
  233. print __METHOD__." result2=".join(', ', $result2)."\n";
  234. // 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)
  235. $this->assertEquals(array(-100, -10, -110, -10, -1, -11, -100, -10, -110, 0, 0, 0, 0, 0, 0, 0, -100, -10, -110, -10, -1, -11, -100, -10, -110, 0, 0), $result2, 'Test7 ES for product');
  236. // 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
  237. $mysoc->country_code='ES';
  238. $mysoc->country_id=4;
  239. $mysoc->localtax1_assuj=0;
  240. $mysoc->localtax2_assuj=1;
  241. $result2=calcul_price_total(10, -10, 0, 10, -1, -1, 0, 'HT', 0, 1);
  242. print __METHOD__." result2=".join(', ', $result2)."\n";
  243. $this->assertEquals(array(-100, -10, -91, -10, -1, -9.1, -100, -10, -91, 0, 19, 0, 1.90, 0, 0, 19, -100, -10, -91, -10, -1, -9.1, -100, -10, -91, 0, 19), $result2, 'Test8 ES for service');
  244. /*
  245. * Country Côte d'Ivoire
  246. */
  247. // 10 * 10 HT - 0% discount with 18% vat, seller using localtax1 type 2, not localtax2 (method we provide value)
  248. $mysoc->country_code='CI';
  249. $mysoc->country_id=21;
  250. $mysoc->localtax1_assuj=1;
  251. $mysoc->localtax2_assuj=0;
  252. //$localtaxes=getLocalTaxesFromRate(18, 0, null, $mysoc);
  253. //var_dump($locataxes);
  254. $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
  255. print __METHOD__." result3=".join(', ', $result3)."\n";
  256. // 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)
  257. $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, 100, 18, 126.85, 10, 1.8, 12.685, 100, 18, 126.85, 8.85, 0), $result3, 'Test9 CI');
  258. // 10 * 10 HT - 0% discount with 18% vat, seller using localtax1 type 2, not localtax2 (other method autodetect)
  259. $mysoc->country_code='CI';
  260. $mysoc->country_id=21;
  261. $mysoc->localtax1_assuj=1;
  262. $mysoc->localtax2_assuj=0;
  263. $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
  264. print __METHOD__." result3=".join(', ', $result3)."\n";
  265. // 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)
  266. $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, 100, 18, 126.85, 10, 1.8, 12.685, 100, 18, 126.85, 8.85, 0), $result3, 'Test10 CI');
  267. return true;
  268. }
  269. /**
  270. * Test function addline and update_price
  271. *
  272. * @return boolean
  273. * @see http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
  274. */
  275. public function testUpdatePrice()
  276. {
  277. //$this->sharedFixture
  278. global $conf,$user,$langs,$db;
  279. $this->savconf=$conf;
  280. $this->savuser=$user;
  281. $this->savlangs=$langs;
  282. $this->savdb=$db;
  283. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  284. // Two lines of 1.24 give 2.48 HT and 2.72 TTC with standard vat rounding mode
  285. $localobject=new Facture($db);
  286. $localobject->initAsSpecimen('nolines');
  287. $invoiceid=$localobject->create($user);
  288. $localobject->addline('Desc', 1.24, 1, 10, 0, 0, 0, 0, '', '', 0, 0, 0, 'HT');
  289. $localobject->addline('Desc', 1.24, 1, 10, 0, 0, 0, 0, '', '', 0, 0, 0, 'HT');
  290. $newlocalobject=new Facture($db);
  291. $newlocalobject->fetch($invoiceid);
  292. $this->assertEquals(2.48, $newlocalobject->total_ht, "testUpdatePrice test1");
  293. $this->assertEquals(0.24, $newlocalobject->total_tva, "testUpdatePrice test2");
  294. $this->assertEquals(2.72, $newlocalobject->total_ttc, "testUpdatePrice test3");
  295. // Two lines of 1.24 give 2.48 HT and 2.73 TTC with global vat rounding mode
  296. $localobject=new Facture($db);
  297. $localobject->initAsSpecimen('nolines');
  298. $invoiceid=$localobject->create($user);
  299. $localobject->addline('Desc', 1.24, 1, 10, 0, 0, 0, 0, '', '', 0, 0, 0, 'HT');
  300. $localobject->addline('Desc', 1.24, 1, 10, 0, 0, 0, 0, '', '', 0, 0, 0, 'HT');
  301. $newlocalobject=new Facture($db);
  302. $newlocalobject->fetch($invoiceid);
  303. $this->assertEquals(2.48, $newlocalobject->total_ht, "testUpdatePrice test4");
  304. //$this->assertEquals(0.25,$newlocalobject->total_tva);
  305. //$this->assertEquals(2.73,$newlocalobject->total_ttc);
  306. }
  307. }