NumberingModulesTest.php 27 KB

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