FactureTestRounding.php 10 KB

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