FactureTestRounding.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. * @return FactureTest
  53. */
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. //$this->sharedFixture
  58. global $conf,$user,$langs,$db;
  59. $this->savconf=$conf;
  60. $this->savuser=$user;
  61. $this->savlangs=$langs;
  62. $this->savdb=$db;
  63. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  64. //print " - db ".$db->db;
  65. print "\n";
  66. }
  67. /**
  68. * setUpBeforeClass
  69. *
  70. * @return void
  71. */
  72. public static function setUpBeforeClass()
  73. {
  74. global $conf,$user,$langs,$db;
  75. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  76. print __METHOD__."\n";
  77. }
  78. /**
  79. * tearDownAfterClass
  80. *
  81. * @return void
  82. */
  83. public static function tearDownAfterClass()
  84. {
  85. global $conf,$user,$langs,$db;
  86. $db->rollback();
  87. print __METHOD__."\n";
  88. }
  89. /**
  90. * Init phpunit tests
  91. *
  92. * @return void
  93. */
  94. protected function setUp()
  95. {
  96. global $conf,$user,$langs,$db;
  97. $conf=$this->savconf;
  98. $user=$this->savuser;
  99. $langs=$this->savlangs;
  100. $db=$this->savdb;
  101. print __METHOD__."\n";
  102. }
  103. /**
  104. * End phpunit tests
  105. *
  106. * @return void
  107. */
  108. protected function tearDown()
  109. {
  110. print __METHOD__."\n";
  111. }
  112. /**
  113. * testFactureRoundingCreate1
  114. * Test according to page http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
  115. *
  116. * @return int
  117. */
  118. public function testFactureRoundingCreate1()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  126. $localobject=new Facture($this->savdb);
  127. $localobject->initAsSpecimen();
  128. $localobject->lines=array();
  129. unset($localobject->total_ht);
  130. unset($localobject->total_ttc);
  131. unset($localobject->total_tva);
  132. $result=$localobject->create($user);
  133. // Add two lines
  134. for ($i=0; $i<2; $i++) {
  135. $localobject->addline('Description '.$i, 1.24, 1, 10);
  136. }
  137. $newlocalobject=new Facture($this->savdb);
  138. $newlocalobject->fetch($result);
  139. //var_dump($newlocalobject);
  140. $this->assertEquals($newlocalobject->total_ht, 2.48);
  141. $this->assertEquals($newlocalobject->total_tva, 0.24);
  142. $this->assertEquals($newlocalobject->total_ttc, 2.72);
  143. return $result;
  144. }
  145. /**
  146. * testFactureRoundingCreate2
  147. *
  148. * @return int
  149. *
  150. * @depends testFactureRoundingCreate1
  151. * Test according to page http://wiki.dolibarr.org/index.php/Draft:VAT_calculation_and_rounding#Standard_usage
  152. */
  153. public function testFactureRoundingCreate2()
  154. {
  155. global $conf,$user,$langs,$db;
  156. $conf=$this->savconf;
  157. $user=$this->savuser;
  158. $langs=$this->savlangs;
  159. $db=$this->savdb;
  160. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  161. $localobject=new Facture($this->savdb);
  162. $localobject->initAsSpecimen();
  163. $localobject->lines=array();
  164. unset($localobject->total_ht);
  165. unset($localobject->total_ttc);
  166. unset($localobject->total_vat);
  167. $result=$localobject->create($user);
  168. // Add two lines
  169. for ($i=0; $i<2; $i++) {
  170. $localobject->addline('Description '.$i, 1.24, 1, 10);
  171. }
  172. $newlocalobject=new Facture($this->savdb);
  173. $newlocalobject->fetch($result);
  174. //var_dump($newlocalobject);
  175. $this->assertEquals($newlocalobject->total_ht, 2.48);
  176. //$this->assertEquals($newlocalobject->total_tva, 0.25);
  177. //$this->assertEquals($newlocalobject->total_ttc, 2.73);
  178. return $result;
  179. }
  180. /**
  181. * testFactureAddLine1
  182. *
  183. * @return void
  184. */
  185. public function testFactureAddLine1()
  186. {
  187. global $conf,$user,$langs,$db;
  188. $conf=$this->savconf;
  189. $user=$this->savuser;
  190. $langs=$this->savlangs;
  191. $db=$this->savdb;
  192. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 0
  193. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  194. $localobject1a=new Facture($this->savdb);
  195. $localobject1a->initAsSpecimen('nolines');
  196. $facid=$localobject1a->create($user);
  197. $localobject1a->addline('Line 1', 6.36, 15, 21); // This include update_price
  198. print __METHOD__." id=".$facid." total_ttc=".$localobject1a->total_ttc."\n";
  199. $this->assertEquals(95.40, $localobject1a->total_ht);
  200. $this->assertEquals(20.03, $localobject1a->total_tva);
  201. $this->assertEquals(115.43, $localobject1a->total_ttc);
  202. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 1
  203. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=1;
  204. $localobject1b=new Facture($this->savdb);
  205. $localobject1b->initAsSpecimen('nolines');
  206. $facid=$localobject1b->create($user);
  207. $localobject1b->addline('Line 1', 6.36, 15, 21); // This include update_price
  208. print __METHOD__." id=".$facid." total_ttc=".$localobject1b->total_ttc."\n";
  209. $this->assertEquals(95.40, $localobject1b->total_ht, 'testFactureAddLine1 total_ht');
  210. $this->assertEquals(20.03, $localobject1b->total_tva, 'testFactureAddLine1 total_tva');
  211. $this->assertEquals(115.43, $localobject1b->total_ttc, 'testFactureAddLine1 total_ttc');
  212. }
  213. /**
  214. * testFactureAddLine2
  215. *
  216. * @return void
  217. *
  218. * @depends testFactureAddLine1
  219. * The depends says test is run only if previous is ok
  220. */
  221. public function testFactureAddLine2()
  222. {
  223. global $conf,$user,$langs,$db;
  224. $conf=$this->savconf;
  225. $user=$this->savuser;
  226. $langs=$this->savlangs;
  227. $db=$this->savdb;
  228. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 0
  229. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  230. $localobject2=new Facture($this->savdb);
  231. $localobject2->initAsSpecimen('nolines');
  232. $facid=$localobject2->create($user);
  233. $localobject2->addline('Line 1', 6.36, 5, 21);
  234. $localobject2->addline('Line 2', 6.36, 5, 21);
  235. $localobject2->addline('Line 3', 6.36, 5, 21);
  236. print __METHOD__." id=".$facid." total_ttc=".$localobject2->total_ttc."\n";
  237. $this->assertEquals(95.40, $localobject2->total_ht);
  238. $this->assertEquals(20.04, $localobject2->total_tva);
  239. $this->assertEquals(115.44, $localobject2->total_ttc);
  240. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 1
  241. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=1;
  242. $localobject2=new Facture($this->savdb);
  243. $localobject2->initAsSpecimen('nolines');
  244. $facid=$localobject2->create($user);
  245. $localobject2->addline('Line 1', 6.36, 5, 21);
  246. $localobject2->addline('Line 2', 6.36, 5, 21);
  247. $localobject2->addline('Line 3', 6.36, 5, 21);
  248. print __METHOD__." id=".$facid." total_ttc=".$localobject2->total_ttc."\n";
  249. $this->assertEquals(95.40, $localobject2->total_ht);
  250. $this->assertEquals(20.03, $localobject2->total_tva);
  251. $this->assertEquals(115.43, $localobject2->total_ttc);
  252. }
  253. /**
  254. * testFactureAddLine3
  255. *
  256. * @return void
  257. *
  258. * @depends testFactureAddLine2
  259. * The depends says test is run only if previous is ok
  260. */
  261. public function testFactureAddLine3()
  262. {
  263. global $conf,$user,$langs,$db;
  264. $conf=$this->savconf;
  265. $user=$this->savuser;
  266. $langs=$this->savlangs;
  267. $db=$this->savdb;
  268. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 0
  269. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=0;
  270. $localobject3=new Facture($this->savdb);
  271. $localobject3->initAsSpecimen('nolines');
  272. $facid=$localobject3->create($user);
  273. $localobject3->addline('Line 1', 6.36, 3, 21);
  274. $localobject3->addline('Line 2', 6.36, 3, 21);
  275. $localobject3->addline('Line 3', 6.36, 3, 21);
  276. $localobject3->addline('Line 4', 6.36, 3, 21);
  277. $localobject3->addline('Line 5', 6.36, 3, 21);
  278. print __METHOD__." id=".$facid." total_ttc=".$localobject3->total_ttc."\n";
  279. $this->assertEquals(95.40, $localobject3->total_ht);
  280. $this->assertEquals(20.05, $localobject3->total_tva);
  281. $this->assertEquals(115.45, $localobject3->total_ttc);
  282. // With option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND = 1
  283. $conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND=1;
  284. $localobject3=new Facture($this->savdb);
  285. $localobject3->initAsSpecimen('nolines');
  286. $facid=$localobject3->create($user);
  287. $localobject3->addline('Line 1', 6.36, 3, 21);
  288. $localobject3->addline('Line 2', 6.36, 3, 21);
  289. $localobject3->addline('Line 3', 6.36, 3, 21);
  290. $localobject3->addline('Line 4', 6.36, 3, 21);
  291. $localobject3->addline('Line 5', 6.36, 3, 21);
  292. print __METHOD__." id=".$facid." total_ttc=".$localobject3->total_ttc."\n";
  293. $this->assertEquals(95.40, $localobject3->total_ht);
  294. $this->assertEquals(20.03, $localobject3->total_tva);
  295. $this->assertEquals(115.43, $localobject3->total_ttc);
  296. }
  297. }