NumberingModulesTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <?php
  2. /* Copyright (C) 2012-2023 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/NumberingModulesTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. require_once dirname(__FILE__).'/../../htdocs/master.inc.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 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. * @param string $name Name
  53. * @return NumberingModulesTest
  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. * testFactureMercure
  115. *
  116. * @return int
  117. */
  118. public function testFactureMercure()
  119. {
  120. global $conf,$user,$langs,$db,$mysoc;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.class.php';
  126. require_once dirname(__FILE__).'/../../htdocs/core/modules/facture/mod_facture_mercure.php';
  127. // First we try with a simple mask, with no reset
  128. // and we test counter is still increase second year.
  129. $conf->global->FACTURE_ADDON='mercure';
  130. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000}';
  131. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000}';
  132. $conf->global->FACTURE_MERCURE_MASK_DEPOSIT='{yyyy}-{0000}';
  133. $conf->global->FACTURE_MERCURE_MASK_REPLACEMENT='{yyyy}-{0000}';
  134. $conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED=0;
  135. $localobject=new Facture($db);
  136. $localobject->initAsSpecimen();
  137. $localobject->fetch_thirdparty();
  138. $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 (usefull only if numbering is {0000@1}
  139. $numbering=new mod_facture_mercure();
  140. $result=$numbering->getNextValue($mysoc, $localobject);
  141. print __METHOD__." result=".$result."\n";
  142. $this->assertEquals('1915-0001', $result, 'Test for {yyyy}-{0000}, 1st invoice'); // counter must start to 1
  143. $result2=$localobject->create($user, 1);
  144. print __METHOD__." result2=".$result."\n";
  145. $result3=$localobject->validate($user, $result); // create invoice by forcing ref
  146. print __METHOD__." result3=".$result."\n";
  147. $this->assertEquals(1, $result3, 'Test validation of invoice with forced ref is ok'); // counter must start to 1
  148. $result=$localobject->is_erasable();
  149. print __METHOD__." is_erasable=".$result."\n";
  150. $this->assertGreaterThanOrEqual(1, $result, 'Test for is_erasable, 1st invoice'); // Can be deleted
  151. $localobject2=new Facture($db);
  152. $localobject2->initAsSpecimen();
  153. $localobject2->fetch_thirdparty();
  154. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1916); // we use following year for second invoice (there is no reset into mask)
  155. $numbering=new mod_facture_mercure();
  156. $result=$numbering->getNextValue($mysoc, $localobject2, 'last');
  157. print __METHOD__." result=".$result."\n";
  158. $this->assertEquals('1915-0001', $result, "Test to get last value with param 'last'");
  159. $result=$numbering->getNextValue($mysoc, $localobject2);
  160. print __METHOD__." result=".$result."\n";
  161. $this->assertEquals('1916-0002', $result); // counter must be now 2 (not reseted)
  162. $result2=$localobject2->create($user, 1);
  163. print __METHOD__." result2=".$result."\n";
  164. $result3=$localobject2->validate($user, $result); // create invoice by forcing ref
  165. print __METHOD__." result3=".$result."\n";
  166. $this->assertEquals(1, $result3, 'Test validation of invoice with forced ref is ok'); // counter must start to 1
  167. $result=$localobject2->is_erasable();
  168. print __METHOD__." is_erasable=".$result."\n";
  169. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  170. $result=$localobject->is_erasable();
  171. print __METHOD__." is_erasable=".$result."\n";
  172. $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)
  173. // Now we try with a reset
  174. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000@1}';
  175. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000@1}';
  176. $localobject=new Facture($db);
  177. $localobject->initAsSpecimen();
  178. $localobject->fetch_thirdparty();
  179. $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
  180. $numbering=new mod_facture_mercure();
  181. $result=$numbering->getNextValue($mysoc, $localobject);
  182. $result2=$localobject->create($user, 1);
  183. $result3=$localobject->validate($user, $result);
  184. print __METHOD__." result=".$result."\n";
  185. $this->assertEquals('1910-0001', $result, 'Test for {yyyy}-{0000@1} 1st invoice'); // counter must start to 1
  186. $localobject2=new Facture($db);
  187. $localobject2->initAsSpecimen();
  188. $localobject2->fetch_thirdparty();
  189. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1910); // we use same year for second invoice (and there is a reset required)
  190. $numbering=new mod_facture_mercure();
  191. $result=$numbering->getNextValue($mysoc, $localobject2);
  192. print __METHOD__." result=".$result."\n";
  193. $this->assertEquals('1910-0002', $result, 'Test for {yyyy}-{0000@1} 2nd invoice, same day'); // counter must be now 2
  194. $localobject3=new Facture($db);
  195. $localobject3->initAsSpecimen();
  196. $localobject3->fetch_thirdparty();
  197. $localobject3->date=dol_mktime(12, 0, 0, 1, 1, 1911); // we use next year for third invoice (and there is a reset required)
  198. $numbering=new mod_facture_mercure();
  199. $result=$numbering->getNextValue($mysoc, $localobject3);
  200. print __METHOD__." result=".$result."\n";
  201. $this->assertEquals('1911-0001', $result, 'Test for {yyyy}-{0000@1} 3nd invoice, same day'); // counter must be now 1
  202. // Same but we add month after year
  203. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@1}';
  204. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@1}';
  205. $localobject=new Facture($db);
  206. $localobject->initAsSpecimen();
  207. $localobject->fetch_thirdparty();
  208. $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
  209. $numbering=new mod_facture_mercure();
  210. $result=$numbering->getNextValue($mysoc, $localobject);
  211. $result2=$localobject->create($user, 1);
  212. $result3=$localobject->validate($user, $result);
  213. print __METHOD__." result=".$result."\n";
  214. $this->assertEquals('192001-0001', $result, 'Test for {yyyy}{mm}-{0000@1} 1st invoice'); // counter must start to 1
  215. $result=$localobject->is_erasable();
  216. print __METHOD__." is_erasable=".$result."\n";
  217. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  218. $localobject2=new Facture($db);
  219. $localobject2->initAsSpecimen();
  220. $localobject2->fetch_thirdparty();
  221. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1921); // we use following year for second invoice (and there is a reset required)
  222. $numbering=new mod_facture_mercure();
  223. $result=$numbering->getNextValue($mysoc, $localobject2);
  224. $result2=$localobject2->create($user, 1);
  225. $result3=$localobject2->validate($user, $result);
  226. print __METHOD__." result=".$result."\n";
  227. $this->assertEquals('192101-0001', $result); // counter must be reseted to 1
  228. $result=$localobject2->is_erasable();
  229. print __METHOD__." is_erasable=".$result."\n";
  230. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  231. $result=$localobject->is_erasable();
  232. print __METHOD__." is_erasable=".$result."\n";
  233. $this->assertGreaterThanOrEqual(1, $result); // Case 1 can be deleted (because there was a reset for case 2)
  234. // Same but we add month before year and use a year on 2 digits
  235. $conf->global->FACTURE_MERCURE_MASK_CREDIT='[mm}{yy}-{0000@1}';
  236. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{mm}{yy}-{0000@1}';
  237. $localobject=new Facture($db);
  238. $localobject->initAsSpecimen();
  239. $localobject->fetch_thirdparty();
  240. $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
  241. $numbering=new mod_facture_mercure();
  242. $result=$numbering->getNextValue($mysoc, $localobject);
  243. $result2=$localobject->create($user, 1);
  244. $result3=$localobject->validate($user, $result);
  245. print __METHOD__." result=".$result."\n";
  246. $this->assertEquals('0125-0001', $result, 'Test for {mm}{yy}-{0000@1} 1st invoice'); // counter must start to 1
  247. $result=$localobject->is_erasable(); // This call get getNextNumRef with param 'last'
  248. print __METHOD__." is_erasable=".$result."\n";
  249. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  250. $localobject2=new Facture($db);
  251. $localobject2->initAsSpecimen();
  252. $localobject2->fetch_thirdparty();
  253. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1925); // we use same year 1925 for second invoice (and there is a reset required)
  254. $numbering=new mod_facture_mercure();
  255. $result=$numbering->getNextValue($mysoc, $localobject2);
  256. $result2=$localobject2->create($user, 1);
  257. $result3=$localobject2->validate($user, $result);
  258. print __METHOD__." result=".$result."\n";
  259. $this->assertEquals('0125-0002', $result, 'Test for {mm}{yy}-{0000@1} 2st invoice'); // counter must be now 2
  260. $result=$localobject2->is_erasable();
  261. print __METHOD__." is_erasable=".$result."\n";
  262. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  263. $result=$localobject->is_erasable();
  264. print __METHOD__." is_erasable=".$result."\n";
  265. $this->assertLessThanOrEqual(0, $result); // Case 1 can not be deleted (because there is an invoice 2)
  266. $localobject3=new Facture($db);
  267. $localobject3->initAsSpecimen();
  268. $localobject3->fetch_thirdparty();
  269. $localobject3->date=dol_mktime(12, 0, 0, 1, 1, 1926); // we use following year for third invoice (and there is a reset required)
  270. $numbering=new mod_facture_mercure();
  271. $result=$numbering->getNextValue($mysoc, $localobject3);
  272. print __METHOD__." result=".$result."\n";
  273. $this->assertEquals('0126-0001', $result, 'Test for {mm}{yy}-{0000@1} 3rd invoice'); // counter must be now 1
  274. // Try an offset when an invoice already exists
  275. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000+9990}';
  276. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000+9990}';
  277. $result=$numbering->getNextValue($mysoc, $localobject2);
  278. // Now we try with a different fiscal month (forced by mask)
  279. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@6}';
  280. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@6}';
  281. $localobject=new Facture($db);
  282. $localobject->initAsSpecimen();
  283. $localobject->fetch_thirdparty();
  284. $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
  285. $numbering=new mod_facture_mercure();
  286. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  287. print __METHOD__." result for last=".$result."\n";
  288. $this->assertEquals('', $result); // no existing ref into reset range
  289. $result=$numbering->getNextValue($mysoc, $localobject);
  290. $result2=$localobject->create($user, 1);
  291. $result3=$localobject->validate($user, $result);
  292. print __METHOD__." result=".$result."\n";
  293. $this->assertEquals('193001-0001', $result); // counter must start to 1
  294. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  295. print __METHOD__." result for last=".$result."\n";
  296. $this->assertEquals('193001-0001', $result); // last ref into reset range should be same than last created
  297. $localobject=new Facture($db);
  298. $localobject->initAsSpecimen();
  299. $localobject->fetch_thirdparty();
  300. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1930); // we use same year but fiscal month after
  301. $numbering=new mod_facture_mercure();
  302. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  303. print __METHOD__." result for last=".$result."\n";
  304. $this->assertEquals('', $result); // last ref into reset range should be ''
  305. $result=$numbering->getNextValue($mysoc, $localobject);
  306. $result2=$localobject->create($user, 1);
  307. $result3=$localobject->validate($user, $result);
  308. print __METHOD__." result=".$result."\n";
  309. $this->assertEquals('193012-0001', $result); // counter must be reset to 1
  310. $localobject=new Facture($db);
  311. $localobject->initAsSpecimen();
  312. $localobject->fetch_thirdparty();
  313. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1931); // we use same fiscal year but different year
  314. $numbering=new mod_facture_mercure();
  315. $result=$numbering->getNextValue($mysoc, $localobject);
  316. $result2=$localobject->create($user, 1);
  317. $result3=$localobject->validate($user, $result);
  318. print __METHOD__." result=".$result."\n";
  319. $this->assertEquals('193101-0002', $result); // counter must be 2
  320. $localobject=new Facture($db);
  321. $localobject->initAsSpecimen();
  322. $localobject->fetch_thirdparty();
  323. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1931); // we use different fiscal year but same year
  324. $numbering=new mod_facture_mercure();
  325. $result=$numbering->getNextValue($mysoc, $localobject);
  326. print __METHOD__." result=".$result."\n";
  327. $this->assertEquals('193112-0001', $result); // counter must be reset to 1
  328. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START)
  329. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  330. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@0}';
  331. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@0}';
  332. $localobject=new Facture($db);
  333. $localobject->initAsSpecimen();
  334. $localobject->fetch_thirdparty();
  335. $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
  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('194001-0001', $result); // counter must start to 1
  342. $localobject=new Facture($db);
  343. $localobject->initAsSpecimen();
  344. $localobject->fetch_thirdparty();
  345. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1940); // we use same year but fiscal month after
  346. $numbering=new mod_facture_mercure();
  347. $result=$numbering->getNextValue($mysoc, $localobject);
  348. $result2=$localobject->create($user, 1);
  349. $result3=$localobject->validate($user, $result);
  350. print __METHOD__." result=".$result."\n";
  351. $this->assertEquals('194012-0001', $result); // counter must be reset to 1
  352. $localobject=new Facture($db);
  353. $localobject->initAsSpecimen();
  354. $localobject->fetch_thirdparty();
  355. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1941); // we use same fiscal year but different 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('194101-0002', $result); // counter must be 2
  362. $localobject=new Facture($db);
  363. $localobject->initAsSpecimen();
  364. $localobject->fetch_thirdparty();
  365. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1941); // we use different discal year but same year
  366. $numbering=new mod_facture_mercure();
  367. $result=$numbering->getNextValue($mysoc, $localobject);
  368. print __METHOD__." result=".$result."\n";
  369. $this->assertEquals('194112-0001', $result); // counter must be reset to 1
  370. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want year of element
  371. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  372. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@=}';
  373. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@=}';
  374. $localobject=new Facture($db);
  375. $localobject->initAsSpecimen();
  376. $localobject->fetch_thirdparty();
  377. $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
  378. $numbering=new mod_facture_mercure();
  379. $result=$numbering->getNextValue($mysoc, $localobject);
  380. $result2=$localobject->create($user, 1);
  381. $result3=$localobject->validate($user, $result);
  382. print __METHOD__." result=".$result."\n";
  383. $this->assertEquals('195001-0001', $result); // counter must start to 1
  384. $localobject=new Facture($db);
  385. $localobject->initAsSpecimen();
  386. $localobject->fetch_thirdparty();
  387. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1950); // we use same year but fiscal month after
  388. $numbering=new mod_facture_mercure();
  389. $result=$numbering->getNextValue($mysoc, $localobject);
  390. $result2=$localobject->create($user, 1);
  391. $result3=$localobject->validate($user, $result);
  392. print __METHOD__." result=".$result."\n";
  393. $this->assertEquals('195012-0001', $result); // counter must be reset to 1
  394. $localobject=new Facture($db);
  395. $localobject->initAsSpecimen();
  396. $localobject->fetch_thirdparty();
  397. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1951); // we use same fiscal year but different year
  398. $numbering=new mod_facture_mercure();
  399. $result=$numbering->getNextValue($mysoc, $localobject);
  400. $result2=$localobject->create($user, 1);
  401. $result3=$localobject->validate($user, $result);
  402. print __METHOD__." result=".$result."\n";
  403. $this->assertEquals('195101-0002', $result); // counter must be 2
  404. $localobject=new Facture($db);
  405. $localobject->initAsSpecimen();
  406. $localobject->fetch_thirdparty();
  407. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1951); // we use different discal year but same year
  408. $numbering=new mod_facture_mercure();
  409. $result=$numbering->getNextValue($mysoc, $localobject);
  410. print __METHOD__." result=".$result."\n";
  411. $this->assertEquals('195112-0001', $result); // counter must be reset to 1
  412. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want start year
  413. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  414. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@-}';
  415. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@-}';
  416. $localobject=new Facture($db);
  417. $localobject->initAsSpecimen();
  418. $localobject->fetch_thirdparty();
  419. $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
  420. $numbering=new mod_facture_mercure();
  421. $result=$numbering->getNextValue($mysoc, $localobject);
  422. $result2=$localobject->create($user, 1);
  423. $result3=$localobject->validate($user, $result);
  424. print __METHOD__." result=".$result."\n";
  425. $this->assertEquals('195901-0001', $result); // counter must start to 1
  426. $localobject=new Facture($db);
  427. $localobject->initAsSpecimen();
  428. $localobject->fetch_thirdparty();
  429. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1960); // we use same year but fiscal month after
  430. $numbering=new mod_facture_mercure();
  431. $result=$numbering->getNextValue($mysoc, $localobject);
  432. $result2=$localobject->create($user, 1);
  433. $result3=$localobject->validate($user, $result);
  434. print __METHOD__." result=".$result."\n";
  435. $this->assertEquals('196012-0001', $result); // counter must be reset to 1
  436. $localobject=new Facture($db);
  437. $localobject->initAsSpecimen();
  438. $localobject->fetch_thirdparty();
  439. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1961); // we use same fiscal year but different year
  440. $numbering=new mod_facture_mercure();
  441. $result=$numbering->getNextValue($mysoc, $localobject);
  442. $result2=$localobject->create($user, 1);
  443. $result3=$localobject->validate($user, $result);
  444. print __METHOD__." result=".$result."\n";
  445. $this->assertEquals('196001-0002', $result); // counter must be 2
  446. $localobject=new Facture($db);
  447. $localobject->initAsSpecimen();
  448. $localobject->fetch_thirdparty();
  449. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1961); // we use different discal year but same year
  450. $numbering=new mod_facture_mercure();
  451. $result=$numbering->getNextValue($mysoc, $localobject);
  452. print __METHOD__." result=".$result."\n";
  453. $this->assertEquals('196112-0001', $result); // counter must be reset to 1
  454. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want end year
  455. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  456. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@+}';
  457. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@+}';
  458. $localobject=new Facture($db);
  459. $localobject->initAsSpecimen();
  460. $localobject->fetch_thirdparty();
  461. $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
  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('197001-0001', $result); // counter must start to 1
  468. $localobject=new Facture($db);
  469. $localobject->initAsSpecimen();
  470. $localobject->fetch_thirdparty();
  471. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1970); // we use same year but fiscal month after
  472. $numbering=new mod_facture_mercure();
  473. $result=$numbering->getNextValue($mysoc, $localobject);
  474. $result2=$localobject->create($user, 1);
  475. $result3=$localobject->validate($user, $result);
  476. print __METHOD__." result=".$result."\n";
  477. $this->assertEquals('197112-0001', $result); // counter must be reset to 1
  478. $localobject=new Facture($db);
  479. $localobject->initAsSpecimen();
  480. $localobject->fetch_thirdparty();
  481. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1971); // we use same fiscal year but different year
  482. $numbering=new mod_facture_mercure();
  483. $result=$numbering->getNextValue($mysoc, $localobject);
  484. $result2=$localobject->create($user, 1);
  485. $result3=$localobject->validate($user, $result);
  486. print __METHOD__." result=".$result."\n";
  487. $this->assertEquals('197101-0002', $result); // counter must be 2
  488. $localobject=new Facture($db);
  489. $localobject->initAsSpecimen();
  490. $localobject->fetch_thirdparty();
  491. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1971); // we use different fiscal year but same year
  492. $numbering=new mod_facture_mercure();
  493. $result=$numbering->getNextValue($mysoc, $localobject);
  494. print __METHOD__." result=".$result."\n";
  495. $this->assertEquals('197212-0001', $result); // counter must be reset to 1
  496. // Now we try with a reset every month (@99)
  497. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  498. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@99}';
  499. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@99}';
  500. $localobject=new Facture($db);
  501. $localobject->initAsSpecimen();
  502. $localobject->fetch_thirdparty();
  503. $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
  504. $numbering=new mod_facture_mercure();
  505. $result=$numbering->getNextValue($mysoc, $localobject);
  506. $result2=$localobject->create($user, 1);
  507. $result3=$localobject->validate($user, $result);
  508. print __METHOD__." result=".$result."\n";
  509. $this->assertEquals('198001-0001', $result); // counter must start to 1
  510. $localobject=new Facture($db);
  511. $localobject->initAsSpecimen();
  512. $localobject->fetch_thirdparty();
  513. $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
  514. $numbering=new mod_facture_mercure();
  515. $result=$numbering->getNextValue($mysoc, $localobject);
  516. $result2=$localobject->create($user, 1);
  517. $result3=$localobject->validate($user, $result);
  518. print __METHOD__." result=".$result."\n";
  519. $this->assertEquals('198001-0002', $result); // counter must start to 2
  520. $localobject=new Facture($db);
  521. $localobject->initAsSpecimen();
  522. $localobject->fetch_thirdparty();
  523. $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
  524. $numbering=new mod_facture_mercure();
  525. $result=$numbering->getNextValue($mysoc, $localobject);
  526. $result2=$localobject->create($user, 1);
  527. $result3=$localobject->validate($user, $result);
  528. print __METHOD__." result=".$result."\n";
  529. $this->assertEquals('198002-0001', $result); // counter must start to 1
  530. $localobject=new Facture($db);
  531. $localobject->initAsSpecimen();
  532. $localobject->fetch_thirdparty();
  533. $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
  534. $numbering=new mod_facture_mercure();
  535. $result=$numbering->getNextValue($mysoc, $localobject);
  536. $result2=$localobject->create($user, 1);
  537. $result3=$localobject->validate($user, $result);
  538. print __METHOD__." result=".$result."\n";
  539. $this->assertEquals('198101-0001', $result); // counter must start to 1
  540. // Test with {t} tag
  541. $conf->global->SOCIETE_FISCAL_MONTH_START=1;
  542. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{t}{yyyy}{mm}-{0000}';
  543. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{t}{yyyy}{mm}-{0000}';
  544. $tmpthirdparty=new Societe($db);
  545. $tmpthirdparty->initAsSpecimen();
  546. $tmpthirdparty->typent_code = 'TE_ABC';
  547. $localobject=new Facture($db);
  548. $localobject->initAsSpecimen();
  549. $localobject->fetch_thirdparty();
  550. $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
  551. $numbering=new mod_facture_mercure();
  552. $result=$numbering->getNextValue($tmpthirdparty, $localobject);
  553. $result2=$localobject->create($user, 1);
  554. $result3=$localobject->validate($user, $result);
  555. print __METHOD__." result=".$result."\n";
  556. $this->assertEquals('A198201-0001', $result); // counter must start to 1
  557. return $result;
  558. }
  559. }