FactureTestRounding.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* Copyright (C) 2012-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/FactureTestRounding.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.class.php';
  29. if (empty($user->id)) {
  30. print "Load permissions for admin user nb 1\n";
  31. $user->fetch(1);
  32. $user->getrights();
  33. }
  34. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  35. /**
  36. * Class for PHPUnit tests
  37. *
  38. * @backupGlobals disabled
  39. * @backupStaticAttributes enabled
  40. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  41. */
  42. class FactureTestRounding extends PHPUnit\Framework\TestCase
  43. {
  44. protected $savconf;
  45. protected $savuser;
  46. protected $savlangs;
  47. protected $savdb;
  48. /**
  49. * Constructor
  50. * We save global variables into local variables
  51. *
  52. * @param string $name Name
  53. * @return FactureTest
  54. */
  55. public function __construct($name = '')
  56. {
  57. parent::__construct($name);
  58. //$this->sharedFixture
  59. global $conf,$user,$langs,$db;
  60. $this->savconf=$conf;
  61. $this->savuser=$user;
  62. $this->savlangs=$langs;
  63. $this->savdb=$db;
  64. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  65. //print " - db ".$db->db;
  66. print "\n";
  67. }
  68. /**
  69. * setUpBeforeClass
  70. *
  71. * @return void
  72. */
  73. public static function setUpBeforeClass(): void
  74. {
  75. global $conf,$user,$langs,$db;
  76. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  77. print __METHOD__."\n";
  78. }
  79. /**
  80. * tearDownAfterClass
  81. *
  82. * @return void
  83. */
  84. public static function tearDownAfterClass(): void
  85. {
  86. global $conf,$user,$langs,$db;
  87. $db->rollback();
  88. print __METHOD__."\n";
  89. }
  90. /**
  91. * Init phpunit tests
  92. *
  93. * @return void
  94. */
  95. protected function setUp(): void
  96. {
  97. global $conf,$user,$langs,$db;
  98. $conf=$this->savconf;
  99. $user=$this->savuser;
  100. $langs=$this->savlangs;
  101. $db=$this->savdb;
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * End phpunit tests
  106. *
  107. * @return void
  108. */
  109. protected function tearDown(): void
  110. {
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * testFactureRoundingCreate1
  115. * Test according to page http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
  116. *
  117. * @return int
  118. */
  119. public function testFactureRoundingCreate1()
  120. {
  121. global $conf,$user,$langs,$db;
  122. $conf=$this->savconf;
  123. $user=$this->savuser;
  124. $langs=$this->savlangs;
  125. $db=$this->savdb;
  126. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  127. $localobject=new Facture($db);
  128. $localobject->initAsSpecimen();
  129. $localobject->lines=array();
  130. unset($localobject->total_ht);
  131. unset($localobject->total_ttc);
  132. unset($localobject->total_tva);
  133. $result=$localobject->create($user);
  134. // Add two lines
  135. for ($i=0; $i<2; $i++) {
  136. $localobject->addline('Description '.$i, 1.24, 1, 10);
  137. }
  138. $newlocalobject=new Facture($db);
  139. $newlocalobject->fetch($result);
  140. //var_dump($newlocalobject);
  141. $this->assertEquals($newlocalobject->total_ht, 2.48);
  142. $this->assertEquals($newlocalobject->total_tva, 0.24);
  143. $this->assertEquals($newlocalobject->total_ttc, 2.72);
  144. return $result;
  145. }
  146. /**
  147. * testFactureRoundingCreate2
  148. *
  149. * @return int
  150. *
  151. * @depends testFactureRoundingCreate1
  152. * Test according to page http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
  153. */
  154. public function testFactureRoundingCreate2()
  155. {
  156. global $conf,$user,$langs,$db;
  157. $conf=$this->savconf;
  158. $user=$this->savuser;
  159. $langs=$this->savlangs;
  160. $db=$this->savdb;
  161. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  162. $localobject=new Facture($db);
  163. $localobject->initAsSpecimen();
  164. $localobject->lines=array();
  165. unset($localobject->total_ht);
  166. unset($localobject->total_ttc);
  167. unset($localobject->total_vat);
  168. $result=$localobject->create($user);
  169. // Add two lines
  170. for ($i=0; $i<2; $i++) {
  171. $localobject->addline('Description '.$i, 1.24, 1, 10);
  172. }
  173. $newlocalobject=new Facture($db);
  174. $newlocalobject->fetch($result);
  175. //var_dump($newlocalobject);
  176. $this->assertEquals($newlocalobject->total_ht, 2.48);
  177. //$this->assertEquals($newlocalobject->total_tva, 0.25);
  178. //$this->assertEquals($newlocalobject->total_ttc, 2.73);
  179. return $result;
  180. }
  181. /**
  182. * testFactureAddLine1
  183. *
  184. * @return void
  185. */
  186. public function testFactureAddLine1()
  187. {
  188. global $conf,$user,$langs,$db;
  189. $conf=$this->savconf;
  190. $user=$this->savuser;
  191. $langs=$this->savlangs;
  192. $db=$this->savdb;
  193. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 0
  194. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  195. $localobject1a=new Facture($db);
  196. $localobject1a->initAsSpecimen('nolines');
  197. $facid=$localobject1a->create($user);
  198. $localobject1a->addline('Line 1', 6.36, 15, 21); // This include update_price
  199. print __METHOD__." id=".$facid." total_ttc=".$localobject1a->total_ttc."\n";
  200. $this->assertEquals(95.40, $localobject1a->total_ht);
  201. $this->assertEquals(20.03, $localobject1a->total_tva);
  202. $this->assertEquals(115.43, $localobject1a->total_ttc);
  203. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 1
  204. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=1;
  205. $localobject1b=new Facture($db);
  206. $localobject1b->initAsSpecimen('nolines');
  207. $facid=$localobject1b->create($user);
  208. $localobject1b->addline('Line 1', 6.36, 15, 21); // This include update_price
  209. print __METHOD__." id=".$facid." total_ttc=".$localobject1b->total_ttc."\n";
  210. $this->assertEquals(95.40, $localobject1b->total_ht, 'testFactureAddLine1 total_ht');
  211. $this->assertEquals(20.03, $localobject1b->total_tva, 'testFactureAddLine1 total_tva');
  212. $this->assertEquals(115.43, $localobject1b->total_ttc, 'testFactureAddLine1 total_ttc');
  213. }
  214. /**
  215. * testFactureAddLine2
  216. *
  217. * @return void
  218. *
  219. * @depends testFactureAddLine1
  220. * The depends says test is run only if previous is ok
  221. */
  222. public function testFactureAddLine2()
  223. {
  224. global $conf,$user,$langs,$db;
  225. $conf=$this->savconf;
  226. $user=$this->savuser;
  227. $langs=$this->savlangs;
  228. $db=$this->savdb;
  229. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 0
  230. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  231. $localobject2=new Facture($db);
  232. $localobject2->initAsSpecimen('nolines');
  233. $facid=$localobject2->create($user);
  234. $localobject2->addline('Line 1', 6.36, 5, 21);
  235. $localobject2->addline('Line 2', 6.36, 5, 21);
  236. $localobject2->addline('Line 3', 6.36, 5, 21);
  237. print __METHOD__." id=".$facid." total_ttc=".$localobject2->total_ttc."\n";
  238. $this->assertEquals(95.40, $localobject2->total_ht);
  239. $this->assertEquals(20.04, $localobject2->total_tva);
  240. $this->assertEquals(115.44, $localobject2->total_ttc);
  241. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 1
  242. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=1;
  243. $localobject2=new Facture($db);
  244. $localobject2->initAsSpecimen('nolines');
  245. $facid=$localobject2->create($user);
  246. $localobject2->addline('Line 1', 6.36, 5, 21);
  247. $localobject2->addline('Line 2', 6.36, 5, 21);
  248. $localobject2->addline('Line 3', 6.36, 5, 21);
  249. print __METHOD__." id=".$facid." total_ttc=".$localobject2->total_ttc."\n";
  250. $this->assertEquals(95.40, $localobject2->total_ht);
  251. $this->assertEquals(20.03, $localobject2->total_tva);
  252. $this->assertEquals(115.43, $localobject2->total_ttc);
  253. }
  254. /**
  255. * testFactureAddLine3
  256. *
  257. * @return void
  258. *
  259. * @depends testFactureAddLine2
  260. * The depends says test is run only if previous is ok
  261. */
  262. public function testFactureAddLine3()
  263. {
  264. global $conf,$user,$langs,$db;
  265. $conf=$this->savconf;
  266. $user=$this->savuser;
  267. $langs=$this->savlangs;
  268. $db=$this->savdb;
  269. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 0
  270. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  271. $localobject3=new Facture($db);
  272. $localobject3->initAsSpecimen('nolines');
  273. $facid=$localobject3->create($user);
  274. $localobject3->addline('Line 1', 6.36, 3, 21);
  275. $localobject3->addline('Line 2', 6.36, 3, 21);
  276. $localobject3->addline('Line 3', 6.36, 3, 21);
  277. $localobject3->addline('Line 4', 6.36, 3, 21);
  278. $localobject3->addline('Line 5', 6.36, 3, 21);
  279. print __METHOD__." id=".$facid." total_ttc=".$localobject3->total_ttc."\n";
  280. $this->assertEquals(95.40, $localobject3->total_ht);
  281. $this->assertEquals(20.05, $localobject3->total_tva);
  282. $this->assertEquals(115.45, $localobject3->total_ttc);
  283. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 1
  284. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=1;
  285. $localobject3=new Facture($db);
  286. $localobject3->initAsSpecimen('nolines');
  287. $facid=$localobject3->create($user);
  288. $localobject3->addline('Line 1', 6.36, 3, 21);
  289. $localobject3->addline('Line 2', 6.36, 3, 21);
  290. $localobject3->addline('Line 3', 6.36, 3, 21);
  291. $localobject3->addline('Line 4', 6.36, 3, 21);
  292. $localobject3->addline('Line 5', 6.36, 3, 21);
  293. print __METHOD__." id=".$facid." total_ttc=".$localobject3->total_ttc."\n";
  294. $this->assertEquals(95.40, $localobject3->total_ht);
  295. $this->assertEquals(20.03, $localobject3->total_tva);
  296. $this->assertEquals(115.43, $localobject3->total_ttc);
  297. }
  298. }