NumberingModulesTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. /* Copyright (C) 2012 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/NumberingModulesTest.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. if (empty($user->id))
  29. {
  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 NumberingModulesTest 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 NumberingModulesTest
  53. */
  54. 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. // Static methods
  68. public static function setUpBeforeClass()
  69. {
  70. global $conf,$user,$langs,$db;
  71. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  72. print __METHOD__."\n";
  73. }
  74. // tear down after class
  75. public static function tearDownAfterClass()
  76. {
  77. global $conf,$user,$langs,$db;
  78. $db->rollback();
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. * Init phpunit tests
  83. *
  84. * @return void
  85. */
  86. protected function setUp()
  87. {
  88. global $conf,$user,$langs,$db;
  89. $conf=$this->savconf;
  90. $user=$this->savuser;
  91. $langs=$this->savlangs;
  92. $db=$this->savdb;
  93. print __METHOD__."\n";
  94. }
  95. /**
  96. * End phpunit tests
  97. *
  98. * @return void
  99. */
  100. protected function tearDown()
  101. {
  102. print __METHOD__."\n";
  103. }
  104. /**
  105. * testFactureMercure
  106. *
  107. * @return int
  108. */
  109. public function testFactureMercure()
  110. {
  111. global $conf,$user,$langs,$db,$mysoc;
  112. $conf=$this->savconf;
  113. $user=$this->savuser;
  114. $langs=$this->savlangs;
  115. $db=$this->savdb;
  116. require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.class.php';
  117. require_once dirname(__FILE__).'/../../htdocs/core/modules/facture/mod_facture_mercure.php';
  118. // First we try with a simple mask, with no reset
  119. // and we test counter is still increase second year.
  120. $conf->global->FACTURE_ADDON='mercure';
  121. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000}';
  122. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000}';
  123. $conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED=0;
  124. $localobject=new Facture($this->savdb);
  125. $localobject->initAsSpecimen();
  126. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1915); // we use year 1915 to be sure to not have existing invoice for this year
  127. $numbering=new mod_facture_mercure();
  128. $result=$numbering->getNextValue($mysoc, $localobject);
  129. print __METHOD__." result=".$result."\n";
  130. $this->assertEquals('1915-0001', $result, 'Test for {yyyy}-{0000}, 1st invoice'); // counter must start to 1
  131. $result2=$localobject->create($user,1);
  132. print __METHOD__." result2=".$result."\n";
  133. $result3=$localobject->validate($user, $result); // create invoice by forcing ref
  134. print __METHOD__." result3=".$result."\n";
  135. $this->assertEquals(1, $result3, 'Test validation of invoice with forced ref is ok'); // counter must start to 1
  136. $result=$localobject->is_erasable();
  137. print __METHOD__." is_erasable=".$result."\n";
  138. $this->assertGreaterThanOrEqual(1, $result, 'Test for is_erasable, 1st invoice'); // Can be deleted
  139. $localobject2=new Facture($this->savdb);
  140. $localobject2->initAsSpecimen();
  141. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1916); // we use following year for second invoice (there is no reset into mask)
  142. $numbering=new mod_facture_mercure();
  143. $result=$numbering->getNextValue($mysoc, $localobject2, 'last');
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals('1915-0001', $result, "Test to get last value with param 'last'");
  146. $result=$numbering->getNextValue($mysoc, $localobject2);
  147. print __METHOD__." result=".$result."\n";
  148. $this->assertEquals('1916-0002', $result); // counter must be now 2 (not reseted)
  149. $result2=$localobject2->create($user,1);
  150. print __METHOD__." result2=".$result."\n";
  151. $result3=$localobject2->validate($user, $result); // create invoice by forcing ref
  152. print __METHOD__." result3=".$result."\n";
  153. $this->assertEquals(1, $result3, 'Test validation of invoice with forced ref is ok'); // counter must start to 1
  154. $result=$localobject2->is_erasable();
  155. print __METHOD__." is_erasable=".$result."\n";
  156. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  157. $result=$localobject->is_erasable();
  158. print __METHOD__." is_erasable=".$result."\n";
  159. $this->assertLessThanOrEqual(0, $result, 'Test for {yyyy}-{0000} that is_erasable is 0 for 1st invoice'); // 1 can no more be deleted (2 is more recent)
  160. // Now we try with a reset
  161. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000@1}';
  162. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000@1}';
  163. $localobject=new Facture($this->savdb);
  164. $localobject->initAsSpecimen();
  165. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1910); // we use year 1910 to be sure to not have existing invoice for this year
  166. $numbering=new mod_facture_mercure();
  167. $result=$numbering->getNextValue($mysoc, $localobject);
  168. $result2=$localobject->create($user,1);
  169. $result3=$localobject->validate($user, $result);
  170. print __METHOD__." result=".$result."\n";
  171. $this->assertEquals('1910-0001', $result, 'Test for {yyyy}-{0000@1} 1st invoice'); // counter must start to 1
  172. $localobject2=new Facture($this->savdb);
  173. $localobject2->initAsSpecimen();
  174. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1910); // we use same year for second invoice (and there is a reset required)
  175. $numbering=new mod_facture_mercure();
  176. $result=$numbering->getNextValue($mysoc, $localobject2);
  177. print __METHOD__." result=".$result."\n";
  178. $this->assertEquals('1910-0002', $result, 'Test for {yyyy}-{0000@1} 2nd invoice, same day'); // counter must be now 2
  179. $localobject3=new Facture($this->savdb);
  180. $localobject3->initAsSpecimen();
  181. $localobject3->date=dol_mktime(12, 0, 0, 1, 1, 1911); // we use next year for third invoice (and there is a reset required)
  182. $numbering=new mod_facture_mercure();
  183. $result=$numbering->getNextValue($mysoc, $localobject3);
  184. print __METHOD__." result=".$result."\n";
  185. $this->assertEquals('1911-0001', $result, 'Test for {yyyy}-{0000@1} 3nd invoice, same day'); // counter must be now 1
  186. // Same but we add month after year
  187. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@1}';
  188. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@1}';
  189. $localobject=new Facture($this->savdb);
  190. $localobject->initAsSpecimen();
  191. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1920); // we use year 1920 to be sure to not have existing invoice for this year
  192. $numbering=new mod_facture_mercure();
  193. $result=$numbering->getNextValue($mysoc, $localobject);
  194. $result2=$localobject->create($user,1);
  195. $result3=$localobject->validate($user, $result);
  196. print __METHOD__." result=".$result."\n";
  197. $this->assertEquals('192001-0001', $result, 'Test for {yyyy}{mm}-{0000@1} 1st invoice'); // counter must start to 1
  198. $result=$localobject->is_erasable();
  199. print __METHOD__." is_erasable=".$result."\n";
  200. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  201. $localobject2=new Facture($this->savdb);
  202. $localobject2->initAsSpecimen();
  203. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1921); // we use following year for second invoice (and there is a reset required)
  204. $numbering=new mod_facture_mercure();
  205. $result=$numbering->getNextValue($mysoc, $localobject2);
  206. $result2=$localobject2->create($user,1);
  207. $result3=$localobject2->validate($user, $result);
  208. print __METHOD__." result=".$result."\n";
  209. $this->assertEquals('192101-0001', $result); // counter must be reseted to 1
  210. $result=$localobject2->is_erasable();
  211. print __METHOD__." is_erasable=".$result."\n";
  212. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  213. $result=$localobject->is_erasable();
  214. print __METHOD__." is_erasable=".$result."\n";
  215. $this->assertGreaterThanOrEqual(1, $result); // Case 1 can be deleted (because there was a reset for case 2)
  216. // Same but we add month before year and use a year on 2 digits
  217. $conf->global->FACTURE_MERCURE_MASK_CREDIT='[mm}{yy}-{0000@1}';
  218. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{mm}{yy}-{0000@1}';
  219. $localobject=new Facture($this->savdb);
  220. $localobject->initAsSpecimen();
  221. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1925); // we use year 1925 to be sure to not have existing invoice for this year
  222. $numbering=new mod_facture_mercure();
  223. $result=$numbering->getNextValue($mysoc, $localobject);
  224. $result2=$localobject->create($user,1);
  225. $result3=$localobject->validate($user, $result);
  226. print __METHOD__." result=".$result."\n";
  227. $this->assertEquals('0125-0001', $result, 'Test for {mm}{yy}-{0000@1} 1st invoice'); // counter must start to 1
  228. $result=$localobject->is_erasable(); // This call get getNextNumRef with param 'last'
  229. print __METHOD__." is_erasable=".$result."\n";
  230. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  231. $localobject2=new Facture($this->savdb);
  232. $localobject2->initAsSpecimen();
  233. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1925); // we use same year 1925 for second invoice (and there is a reset required)
  234. $numbering=new mod_facture_mercure();
  235. $result=$numbering->getNextValue($mysoc, $localobject2);
  236. $result2=$localobject2->create($user,1);
  237. $result3=$localobject2->validate($user, $result);
  238. print __METHOD__." result=".$result."\n";
  239. $this->assertEquals('0125-0002', $result, 'Test for {mm}{yy}-{0000@1} 2st invoice'); // counter must be now 2
  240. $result=$localobject2->is_erasable();
  241. print __METHOD__." is_erasable=".$result."\n";
  242. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  243. $result=$localobject->is_erasable();
  244. print __METHOD__." is_erasable=".$result."\n";
  245. $this->assertLessThanOrEqual(0, $result); // Case 1 can not be deleted (because there is an invoice 2)
  246. $localobject3=new Facture($this->savdb);
  247. $localobject3->initAsSpecimen();
  248. $localobject3->date=dol_mktime(12, 0, 0, 1, 1, 1926); // we use following year for third invoice (and there is a reset required)
  249. $numbering=new mod_facture_mercure();
  250. $result=$numbering->getNextValue($mysoc, $localobject3);
  251. print __METHOD__." result=".$result."\n";
  252. $this->assertEquals('0126-0001', $result, 'Test for {mm}{yy}-{0000@1} 3rd invoice'); // counter must be now 1
  253. // Try an offset when an invoice already exists
  254. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000+9990}';
  255. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000+9990}';
  256. $result=$numbering->getNextValue($mysoc, $localobject2);
  257. // Now we try with a different fiscal month (forced by mask)
  258. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@6}';
  259. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@6}';
  260. $localobject=new Facture($this->savdb);
  261. $localobject->initAsSpecimen();
  262. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1930); // we use year 1930 to be sure to not have existing invoice for this year
  263. $numbering=new mod_facture_mercure();
  264. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  265. print __METHOD__." result for last=".$result."\n";
  266. $this->assertEquals('', $result); // no existing ref into reset range
  267. $result=$numbering->getNextValue($mysoc, $localobject);
  268. $result2=$localobject->create($user,1);
  269. $result3=$localobject->validate($user, $result);
  270. print __METHOD__." result=".$result."\n";
  271. $this->assertEquals('193001-0001', $result); // counter must start to 1
  272. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  273. print __METHOD__." result for last=".$result."\n";
  274. $this->assertEquals('193001-0001', $result); // last ref into reset range should be same than last created
  275. $localobject=new Facture($this->savdb);
  276. $localobject->initAsSpecimen();
  277. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1930); // we use same year but fiscal month after
  278. $numbering=new mod_facture_mercure();
  279. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  280. print __METHOD__." result for last=".$result."\n";
  281. $this->assertEquals('', $result); // last ref into reset range should be ''
  282. $result=$numbering->getNextValue($mysoc, $localobject);
  283. $result2=$localobject->create($user,1);
  284. $result3=$localobject->validate($user, $result);
  285. print __METHOD__." result=".$result."\n";
  286. $this->assertEquals('193012-0001', $result); // counter must be reset to 1
  287. $localobject=new Facture($this->savdb);
  288. $localobject->initAsSpecimen();
  289. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1931); // we use same fiscal year but different year
  290. $numbering=new mod_facture_mercure();
  291. $result=$numbering->getNextValue($mysoc, $localobject);
  292. $result2=$localobject->create($user,1);
  293. $result3=$localobject->validate($user, $result);
  294. print __METHOD__." result=".$result."\n";
  295. $this->assertEquals('193101-0002', $result); // counter must be 2
  296. $localobject=new Facture($this->savdb);
  297. $localobject->initAsSpecimen();
  298. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1931); // we use different fiscal year but same year
  299. $numbering=new mod_facture_mercure();
  300. $result=$numbering->getNextValue($mysoc, $localobject);
  301. print __METHOD__." result=".$result."\n";
  302. $this->assertEquals('193112-0001', $result); // counter must be reset to 1
  303. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START)
  304. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  305. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@0}';
  306. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@0}';
  307. $localobject=new Facture($this->savdb);
  308. $localobject->initAsSpecimen();
  309. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1940); // we use year 1940 to be sure to not have existing invoice for this year
  310. $numbering=new mod_facture_mercure();
  311. $result=$numbering->getNextValue($mysoc, $localobject);
  312. $result2=$localobject->create($user,1);
  313. $result3=$localobject->validate($user, $result);
  314. print __METHOD__." result=".$result."\n";
  315. $this->assertEquals('194001-0001', $result); // counter must start to 1
  316. $localobject=new Facture($this->savdb);
  317. $localobject->initAsSpecimen();
  318. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1940); // we use same year but fiscal month after
  319. $numbering=new mod_facture_mercure();
  320. $result=$numbering->getNextValue($mysoc, $localobject);
  321. $result2=$localobject->create($user,1);
  322. $result3=$localobject->validate($user, $result);
  323. print __METHOD__." result=".$result."\n";
  324. $this->assertEquals('194012-0001', $result); // counter must be reset to 1
  325. $localobject=new Facture($this->savdb);
  326. $localobject->initAsSpecimen();
  327. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1941); // we use same fiscal year but different year
  328. $numbering=new mod_facture_mercure();
  329. $result=$numbering->getNextValue($mysoc, $localobject);
  330. $result2=$localobject->create($user,1);
  331. $result3=$localobject->validate($user, $result);
  332. print __METHOD__." result=".$result."\n";
  333. $this->assertEquals('194101-0002', $result); // counter must be 2
  334. $localobject=new Facture($this->savdb);
  335. $localobject->initAsSpecimen();
  336. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1941); // we use different discal year but same year
  337. $numbering=new mod_facture_mercure();
  338. $result=$numbering->getNextValue($mysoc, $localobject);
  339. print __METHOD__." result=".$result."\n";
  340. $this->assertEquals('194112-0001', $result); // counter must be reset to 1
  341. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want year of element
  342. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  343. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@=}';
  344. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@=}';
  345. $localobject=new Facture($this->savdb);
  346. $localobject->initAsSpecimen();
  347. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1950); // we use year 1950 to be sure to not have existing invoice for this year
  348. $numbering=new mod_facture_mercure();
  349. $result=$numbering->getNextValue($mysoc, $localobject);
  350. $result2=$localobject->create($user,1);
  351. $result3=$localobject->validate($user, $result);
  352. print __METHOD__." result=".$result."\n";
  353. $this->assertEquals('195001-0001', $result); // counter must start to 1
  354. $localobject=new Facture($this->savdb);
  355. $localobject->initAsSpecimen();
  356. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1950); // we use same year but fiscal month after
  357. $numbering=new mod_facture_mercure();
  358. $result=$numbering->getNextValue($mysoc, $localobject);
  359. $result2=$localobject->create($user,1);
  360. $result3=$localobject->validate($user, $result);
  361. print __METHOD__." result=".$result."\n";
  362. $this->assertEquals('195012-0001', $result); // counter must be reset to 1
  363. $localobject=new Facture($this->savdb);
  364. $localobject->initAsSpecimen();
  365. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1951); // we use same fiscal year but different year
  366. $numbering=new mod_facture_mercure();
  367. $result=$numbering->getNextValue($mysoc, $localobject);
  368. $result2=$localobject->create($user,1);
  369. $result3=$localobject->validate($user, $result);
  370. print __METHOD__." result=".$result."\n";
  371. $this->assertEquals('195101-0002', $result); // counter must be 2
  372. $localobject=new Facture($this->savdb);
  373. $localobject->initAsSpecimen();
  374. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1951); // we use different discal year but same year
  375. $numbering=new mod_facture_mercure();
  376. $result=$numbering->getNextValue($mysoc, $localobject);
  377. print __METHOD__." result=".$result."\n";
  378. $this->assertEquals('195112-0001', $result); // counter must be reset to 1
  379. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want start year
  380. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  381. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@-}';
  382. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@-}';
  383. $localobject=new Facture($this->savdb);
  384. $localobject->initAsSpecimen();
  385. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1960); // we use year 1960 to be sure to not have existing invoice for this year
  386. $numbering=new mod_facture_mercure();
  387. $result=$numbering->getNextValue($mysoc, $localobject);
  388. $result2=$localobject->create($user,1);
  389. $result3=$localobject->validate($user, $result);
  390. print __METHOD__." result=".$result."\n";
  391. $this->assertEquals('195901-0001', $result); // counter must start to 1
  392. $localobject=new Facture($this->savdb);
  393. $localobject->initAsSpecimen();
  394. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1960); // we use same year but fiscal month after
  395. $numbering=new mod_facture_mercure();
  396. $result=$numbering->getNextValue($mysoc, $localobject);
  397. $result2=$localobject->create($user,1);
  398. $result3=$localobject->validate($user, $result);
  399. print __METHOD__." result=".$result."\n";
  400. $this->assertEquals('196012-0001', $result); // counter must be reset to 1
  401. $localobject=new Facture($this->savdb);
  402. $localobject->initAsSpecimen();
  403. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1961); // we use same fiscal year but different year
  404. $numbering=new mod_facture_mercure();
  405. $result=$numbering->getNextValue($mysoc, $localobject);
  406. $result2=$localobject->create($user,1);
  407. $result3=$localobject->validate($user, $result);
  408. print __METHOD__." result=".$result."\n";
  409. $this->assertEquals('196001-0002', $result); // counter must be 2
  410. $localobject=new Facture($this->savdb);
  411. $localobject->initAsSpecimen();
  412. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1961); // we use different discal year but same year
  413. $numbering=new mod_facture_mercure();
  414. $result=$numbering->getNextValue($mysoc, $localobject);
  415. print __METHOD__." result=".$result."\n";
  416. $this->assertEquals('196112-0001', $result); // counter must be reset to 1
  417. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want end year
  418. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  419. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@+}';
  420. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@+}';
  421. $localobject=new Facture($this->savdb);
  422. $localobject->initAsSpecimen();
  423. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1970); // we use year 1970 to be sure to not have existing invoice for this year
  424. $numbering=new mod_facture_mercure();
  425. $result=$numbering->getNextValue($mysoc, $localobject);
  426. $result2=$localobject->create($user,1);
  427. $result3=$localobject->validate($user, $result);
  428. print __METHOD__." result=".$result."\n";
  429. $this->assertEquals('197001-0001', $result); // counter must start to 1
  430. $localobject=new Facture($this->savdb);
  431. $localobject->initAsSpecimen();
  432. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1970); // we use same year but fiscal month after
  433. $numbering=new mod_facture_mercure();
  434. $result=$numbering->getNextValue($mysoc, $localobject);
  435. $result2=$localobject->create($user,1);
  436. $result3=$localobject->validate($user, $result);
  437. print __METHOD__." result=".$result."\n";
  438. $this->assertEquals('197112-0001', $result); // counter must be reset to 1
  439. $localobject=new Facture($this->savdb);
  440. $localobject->initAsSpecimen();
  441. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1971); // we use same fiscal year but different year
  442. $numbering=new mod_facture_mercure();
  443. $result=$numbering->getNextValue($mysoc, $localobject);
  444. $result2=$localobject->create($user,1);
  445. $result3=$localobject->validate($user, $result);
  446. print __METHOD__." result=".$result."\n";
  447. $this->assertEquals('197101-0002', $result); // counter must be 2
  448. $localobject=new Facture($this->savdb);
  449. $localobject->initAsSpecimen();
  450. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1971); // we use different fiscal year but same year
  451. $numbering=new mod_facture_mercure();
  452. $result=$numbering->getNextValue($mysoc, $localobject);
  453. print __METHOD__." result=".$result."\n";
  454. $this->assertEquals('197212-0001', $result); // counter must be reset to 1
  455. // Now we try with a reset every month (@99)
  456. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  457. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@99}';
  458. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@99}';
  459. $localobject=new Facture($this->savdb);
  460. $localobject->initAsSpecimen();
  461. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1980); // we use year 1980 to be sure to not have existing invoice for this year
  462. $numbering=new mod_facture_mercure();
  463. $result=$numbering->getNextValue($mysoc, $localobject);
  464. $result2=$localobject->create($user,1);
  465. $result3=$localobject->validate($user, $result);
  466. print __METHOD__." result=".$result."\n";
  467. $this->assertEquals('198001-0001', $result); // counter must start to 1
  468. $localobject=new Facture($this->savdb);
  469. $localobject->initAsSpecimen();
  470. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1980); // we use year 1980 to be sure to not have existing invoice for this year
  471. $numbering=new mod_facture_mercure();
  472. $result=$numbering->getNextValue($mysoc, $localobject);
  473. $result2=$localobject->create($user,1);
  474. $result3=$localobject->validate($user, $result);
  475. print __METHOD__." result=".$result."\n";
  476. $this->assertEquals('198001-0002', $result); // counter must start to 2
  477. $localobject=new Facture($this->savdb);
  478. $localobject->initAsSpecimen();
  479. $localobject->date=dol_mktime(12, 0, 0, 2, 1, 1980); // we use year 1980 to be sure to not have existing invoice for this year
  480. $numbering=new mod_facture_mercure();
  481. $result=$numbering->getNextValue($mysoc, $localobject);
  482. $result2=$localobject->create($user,1);
  483. $result3=$localobject->validate($user, $result);
  484. print __METHOD__." result=".$result."\n";
  485. $this->assertEquals('198002-0001', $result); // counter must start to 1
  486. $localobject=new Facture($this->savdb);
  487. $localobject->initAsSpecimen();
  488. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1981); // we use year 1981 to be sure to not have existing invoice for this year
  489. $numbering=new mod_facture_mercure();
  490. $result=$numbering->getNextValue($mysoc, $localobject);
  491. $result2=$localobject->create($user,1);
  492. $result3=$localobject->validate($user, $result);
  493. print __METHOD__." result=".$result."\n";
  494. $this->assertEquals('198101-0001', $result); // counter must start to 1
  495. // Test with {t} tag
  496. $conf->global->SOCIETE_FISCAL_MONTH_START=1;
  497. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{t}{yyyy}{mm}-{0000}';
  498. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{t}{yyyy}{mm}-{0000}';
  499. $tmpthirdparty=new Societe($this->savdb);
  500. $tmpthirdparty->initAsSpecimen();
  501. $tmpthirdparty->typent_code = 'TE_ABC';
  502. $localobject=new Facture($this->savdb);
  503. $localobject->initAsSpecimen();
  504. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1982); // we use year 1982 to be sure to not have existing invoice for this year
  505. $numbering=new mod_facture_mercure();
  506. $result=$numbering->getNextValue($tmpthirdparty, $localobject);
  507. $result2=$localobject->create($user,1);
  508. $result3=$localobject->validate($user, $result);
  509. print __METHOD__." result=".$result."\n";
  510. $this->assertEquals('A198201-0001', $result); // counter must start to 1
  511. return $result;
  512. }
  513. }