NumberingModulesTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. print "Load permissions for admin user nb 1\n";
  30. $user->fetch(1);
  31. $user->getrights();
  32. }
  33. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  34. /**
  35. * Class for PHPUnit tests
  36. *
  37. * @backupGlobals disabled
  38. * @backupStaticAttributes enabled
  39. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  40. */
  41. class NumberingModulesTest extends PHPUnit\Framework\TestCase
  42. {
  43. protected $savconf;
  44. protected $savuser;
  45. protected $savlangs;
  46. protected $savdb;
  47. /**
  48. * Constructor
  49. * We save global variables into local variables
  50. *
  51. * @return NumberingModulesTest
  52. */
  53. public function __construct()
  54. {
  55. parent::__construct();
  56. //$this->sharedFixture
  57. global $conf,$user,$langs,$db;
  58. $this->savconf=$conf;
  59. $this->savuser=$user;
  60. $this->savlangs=$langs;
  61. $this->savdb=$db;
  62. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  63. //print " - db ".$db->db;
  64. print "\n";
  65. }
  66. /**
  67. * setUpBeforeClass
  68. *
  69. * @return void
  70. */
  71. public static function setUpBeforeClass()
  72. {
  73. global $conf,$user,$langs,$db;
  74. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  75. print __METHOD__."\n";
  76. }
  77. /**
  78. * tearDownAfterClass
  79. *
  80. * @return void
  81. */
  82. public static function tearDownAfterClass()
  83. {
  84. global $conf,$user,$langs,$db;
  85. $db->rollback();
  86. print __METHOD__."\n";
  87. }
  88. /**
  89. * Init phpunit tests
  90. *
  91. * @return void
  92. */
  93. protected function setUp()
  94. {
  95. global $conf,$user,$langs,$db;
  96. $conf=$this->savconf;
  97. $user=$this->savuser;
  98. $langs=$this->savlangs;
  99. $db=$this->savdb;
  100. print __METHOD__."\n";
  101. }
  102. /**
  103. * End phpunit tests
  104. *
  105. * @return void
  106. */
  107. protected function tearDown()
  108. {
  109. print __METHOD__."\n";
  110. }
  111. /**
  112. * testFactureMercure
  113. *
  114. * @return int
  115. */
  116. public function testFactureMercure()
  117. {
  118. global $conf,$user,$langs,$db,$mysoc;
  119. $conf=$this->savconf;
  120. $user=$this->savuser;
  121. $langs=$this->savlangs;
  122. $db=$this->savdb;
  123. require_once dirname(__FILE__).'/../../htdocs/compta/facture/class/facture.class.php';
  124. require_once dirname(__FILE__).'/../../htdocs/core/modules/facture/mod_facture_mercure.php';
  125. // First we try with a simple mask, with no reset
  126. // and we test counter is still increase second year.
  127. $conf->global->FACTURE_ADDON='mercure';
  128. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000}';
  129. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000}';
  130. $conf->global->FACTURE_MERCURE_MASK_DEPOSIT='{yyyy}-{0000}';
  131. $conf->global->FACTURE_MERCURE_MASK_REPLACEMENT='{yyyy}-{0000}';
  132. $conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED=0;
  133. $localobject=new Facture($this->savdb);
  134. $localobject->initAsSpecimen();
  135. $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}
  136. $numbering=new mod_facture_mercure();
  137. $result=$numbering->getNextValue($mysoc, $localobject);
  138. print __METHOD__." result=".$result."\n";
  139. $this->assertEquals('1915-0001', $result, 'Test for {yyyy}-{0000}, 1st invoice'); // counter must start to 1
  140. $result2=$localobject->create($user, 1);
  141. print __METHOD__." result2=".$result."\n";
  142. $result3=$localobject->validate($user, $result); // create invoice by forcing ref
  143. print __METHOD__." result3=".$result."\n";
  144. $this->assertEquals(1, $result3, 'Test validation of invoice with forced ref is ok'); // counter must start to 1
  145. $result=$localobject->is_erasable();
  146. print __METHOD__." is_erasable=".$result."\n";
  147. $this->assertGreaterThanOrEqual(1, $result, 'Test for is_erasable, 1st invoice'); // Can be deleted
  148. $localobject2=new Facture($this->savdb);
  149. $localobject2->initAsSpecimen();
  150. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1916); // we use following year for second invoice (there is no reset into mask)
  151. $numbering=new mod_facture_mercure();
  152. $result=$numbering->getNextValue($mysoc, $localobject2, 'last');
  153. print __METHOD__." result=".$result."\n";
  154. $this->assertEquals('1915-0001', $result, "Test to get last value with param 'last'");
  155. $result=$numbering->getNextValue($mysoc, $localobject2);
  156. print __METHOD__." result=".$result."\n";
  157. $this->assertEquals('1916-0002', $result); // counter must be now 2 (not reseted)
  158. $result2=$localobject2->create($user, 1);
  159. print __METHOD__." result2=".$result."\n";
  160. $result3=$localobject2->validate($user, $result); // create invoice by forcing ref
  161. print __METHOD__." result3=".$result."\n";
  162. $this->assertEquals(1, $result3, 'Test validation of invoice with forced ref is ok'); // counter must start to 1
  163. $result=$localobject2->is_erasable();
  164. print __METHOD__." is_erasable=".$result."\n";
  165. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  166. $result=$localobject->is_erasable();
  167. print __METHOD__." is_erasable=".$result."\n";
  168. $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)
  169. // Now we try with a reset
  170. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000@1}';
  171. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000@1}';
  172. $localobject=new Facture($this->savdb);
  173. $localobject->initAsSpecimen();
  174. $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
  175. $numbering=new mod_facture_mercure();
  176. $result=$numbering->getNextValue($mysoc, $localobject);
  177. $result2=$localobject->create($user, 1);
  178. $result3=$localobject->validate($user, $result);
  179. print __METHOD__." result=".$result."\n";
  180. $this->assertEquals('1910-0001', $result, 'Test for {yyyy}-{0000@1} 1st invoice'); // counter must start to 1
  181. $localobject2=new Facture($this->savdb);
  182. $localobject2->initAsSpecimen();
  183. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1910); // we use same year for second invoice (and there is a reset required)
  184. $numbering=new mod_facture_mercure();
  185. $result=$numbering->getNextValue($mysoc, $localobject2);
  186. print __METHOD__." result=".$result."\n";
  187. $this->assertEquals('1910-0002', $result, 'Test for {yyyy}-{0000@1} 2nd invoice, same day'); // counter must be now 2
  188. $localobject3=new Facture($this->savdb);
  189. $localobject3->initAsSpecimen();
  190. $localobject3->date=dol_mktime(12, 0, 0, 1, 1, 1911); // we use next year for third invoice (and there is a reset required)
  191. $numbering=new mod_facture_mercure();
  192. $result=$numbering->getNextValue($mysoc, $localobject3);
  193. print __METHOD__." result=".$result."\n";
  194. $this->assertEquals('1911-0001', $result, 'Test for {yyyy}-{0000@1} 3nd invoice, same day'); // counter must be now 1
  195. // Same but we add month after year
  196. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@1}';
  197. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@1}';
  198. $localobject=new Facture($this->savdb);
  199. $localobject->initAsSpecimen();
  200. $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
  201. $numbering=new mod_facture_mercure();
  202. $result=$numbering->getNextValue($mysoc, $localobject);
  203. $result2=$localobject->create($user, 1);
  204. $result3=$localobject->validate($user, $result);
  205. print __METHOD__." result=".$result."\n";
  206. $this->assertEquals('192001-0001', $result, 'Test for {yyyy}{mm}-{0000@1} 1st invoice'); // counter must start to 1
  207. $result=$localobject->is_erasable();
  208. print __METHOD__." is_erasable=".$result."\n";
  209. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  210. $localobject2=new Facture($this->savdb);
  211. $localobject2->initAsSpecimen();
  212. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1921); // we use following year for second invoice (and there is a reset required)
  213. $numbering=new mod_facture_mercure();
  214. $result=$numbering->getNextValue($mysoc, $localobject2);
  215. $result2=$localobject2->create($user, 1);
  216. $result3=$localobject2->validate($user, $result);
  217. print __METHOD__." result=".$result."\n";
  218. $this->assertEquals('192101-0001', $result); // counter must be reseted to 1
  219. $result=$localobject2->is_erasable();
  220. print __METHOD__." is_erasable=".$result."\n";
  221. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  222. $result=$localobject->is_erasable();
  223. print __METHOD__." is_erasable=".$result."\n";
  224. $this->assertGreaterThanOrEqual(1, $result); // Case 1 can be deleted (because there was a reset for case 2)
  225. // Same but we add month before year and use a year on 2 digits
  226. $conf->global->FACTURE_MERCURE_MASK_CREDIT='[mm}{yy}-{0000@1}';
  227. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{mm}{yy}-{0000@1}';
  228. $localobject=new Facture($this->savdb);
  229. $localobject->initAsSpecimen();
  230. $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
  231. $numbering=new mod_facture_mercure();
  232. $result=$numbering->getNextValue($mysoc, $localobject);
  233. $result2=$localobject->create($user, 1);
  234. $result3=$localobject->validate($user, $result);
  235. print __METHOD__." result=".$result."\n";
  236. $this->assertEquals('0125-0001', $result, 'Test for {mm}{yy}-{0000@1} 1st invoice'); // counter must start to 1
  237. $result=$localobject->is_erasable(); // This call get getNextNumRef with param 'last'
  238. print __METHOD__." is_erasable=".$result."\n";
  239. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  240. $localobject2=new Facture($this->savdb);
  241. $localobject2->initAsSpecimen();
  242. $localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1925); // we use same year 1925 for second invoice (and there is a reset required)
  243. $numbering=new mod_facture_mercure();
  244. $result=$numbering->getNextValue($mysoc, $localobject2);
  245. $result2=$localobject2->create($user, 1);
  246. $result3=$localobject2->validate($user, $result);
  247. print __METHOD__." result=".$result."\n";
  248. $this->assertEquals('0125-0002', $result, 'Test for {mm}{yy}-{0000@1} 2st invoice'); // counter must be now 2
  249. $result=$localobject2->is_erasable();
  250. print __METHOD__." is_erasable=".$result."\n";
  251. $this->assertGreaterThanOrEqual(1, $result); // Can be deleted
  252. $result=$localobject->is_erasable();
  253. print __METHOD__." is_erasable=".$result."\n";
  254. $this->assertLessThanOrEqual(0, $result); // Case 1 can not be deleted (because there is an invoice 2)
  255. $localobject3=new Facture($this->savdb);
  256. $localobject3->initAsSpecimen();
  257. $localobject3->date=dol_mktime(12, 0, 0, 1, 1, 1926); // we use following year for third invoice (and there is a reset required)
  258. $numbering=new mod_facture_mercure();
  259. $result=$numbering->getNextValue($mysoc, $localobject3);
  260. print __METHOD__." result=".$result."\n";
  261. $this->assertEquals('0126-0001', $result, 'Test for {mm}{yy}-{0000@1} 3rd invoice'); // counter must be now 1
  262. // Try an offset when an invoice already exists
  263. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000+9990}';
  264. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000+9990}';
  265. $result=$numbering->getNextValue($mysoc, $localobject2);
  266. // Now we try with a different fiscal month (forced by mask)
  267. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@6}';
  268. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@6}';
  269. $localobject=new Facture($this->savdb);
  270. $localobject->initAsSpecimen();
  271. $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
  272. $numbering=new mod_facture_mercure();
  273. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  274. print __METHOD__." result for last=".$result."\n";
  275. $this->assertEquals('', $result); // no existing ref into reset range
  276. $result=$numbering->getNextValue($mysoc, $localobject);
  277. $result2=$localobject->create($user, 1);
  278. $result3=$localobject->validate($user, $result);
  279. print __METHOD__." result=".$result."\n";
  280. $this->assertEquals('193001-0001', $result); // counter must start to 1
  281. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  282. print __METHOD__." result for last=".$result."\n";
  283. $this->assertEquals('193001-0001', $result); // last ref into reset range should be same than last created
  284. $localobject=new Facture($this->savdb);
  285. $localobject->initAsSpecimen();
  286. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1930); // we use same year but fiscal month after
  287. $numbering=new mod_facture_mercure();
  288. $result=$numbering->getNextValue($mysoc, $localobject, 'last');
  289. print __METHOD__." result for last=".$result."\n";
  290. $this->assertEquals('', $result); // last ref into reset range should be ''
  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('193012-0001', $result); // counter must be reset to 1
  296. $localobject=new Facture($this->savdb);
  297. $localobject->initAsSpecimen();
  298. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1931); // we use same fiscal year but different year
  299. $numbering=new mod_facture_mercure();
  300. $result=$numbering->getNextValue($mysoc, $localobject);
  301. $result2=$localobject->create($user, 1);
  302. $result3=$localobject->validate($user, $result);
  303. print __METHOD__." result=".$result."\n";
  304. $this->assertEquals('193101-0002', $result); // counter must be 2
  305. $localobject=new Facture($this->savdb);
  306. $localobject->initAsSpecimen();
  307. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1931); // we use different fiscal year but same year
  308. $numbering=new mod_facture_mercure();
  309. $result=$numbering->getNextValue($mysoc, $localobject);
  310. print __METHOD__." result=".$result."\n";
  311. $this->assertEquals('193112-0001', $result); // counter must be reset to 1
  312. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START)
  313. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  314. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@0}';
  315. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@0}';
  316. $localobject=new Facture($this->savdb);
  317. $localobject->initAsSpecimen();
  318. $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
  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('194001-0001', $result); // counter must start to 1
  325. $localobject=new Facture($this->savdb);
  326. $localobject->initAsSpecimen();
  327. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1940); // we use same year but fiscal month after
  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('194012-0001', $result); // counter must be reset to 1
  334. $localobject=new Facture($this->savdb);
  335. $localobject->initAsSpecimen();
  336. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1941); // we use same fiscal year but different year
  337. $numbering=new mod_facture_mercure();
  338. $result=$numbering->getNextValue($mysoc, $localobject);
  339. $result2=$localobject->create($user, 1);
  340. $result3=$localobject->validate($user, $result);
  341. print __METHOD__." result=".$result."\n";
  342. $this->assertEquals('194101-0002', $result); // counter must be 2
  343. $localobject=new Facture($this->savdb);
  344. $localobject->initAsSpecimen();
  345. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1941); // we use different discal year but same year
  346. $numbering=new mod_facture_mercure();
  347. $result=$numbering->getNextValue($mysoc, $localobject);
  348. print __METHOD__." result=".$result."\n";
  349. $this->assertEquals('194112-0001', $result); // counter must be reset to 1
  350. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want year of element
  351. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  352. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@=}';
  353. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@=}';
  354. $localobject=new Facture($this->savdb);
  355. $localobject->initAsSpecimen();
  356. $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
  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('195001-0001', $result); // counter must start to 1
  363. $localobject=new Facture($this->savdb);
  364. $localobject->initAsSpecimen();
  365. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1950); // we use same year but fiscal month after
  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('195012-0001', $result); // counter must be reset to 1
  372. $localobject=new Facture($this->savdb);
  373. $localobject->initAsSpecimen();
  374. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1951); // we use same fiscal year but different year
  375. $numbering=new mod_facture_mercure();
  376. $result=$numbering->getNextValue($mysoc, $localobject);
  377. $result2=$localobject->create($user, 1);
  378. $result3=$localobject->validate($user, $result);
  379. print __METHOD__." result=".$result."\n";
  380. $this->assertEquals('195101-0002', $result); // counter must be 2
  381. $localobject=new Facture($this->savdb);
  382. $localobject->initAsSpecimen();
  383. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1951); // we use different discal year but same year
  384. $numbering=new mod_facture_mercure();
  385. $result=$numbering->getNextValue($mysoc, $localobject);
  386. print __METHOD__." result=".$result."\n";
  387. $this->assertEquals('195112-0001', $result); // counter must be reset to 1
  388. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want start year
  389. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  390. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@-}';
  391. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@-}';
  392. $localobject=new Facture($this->savdb);
  393. $localobject->initAsSpecimen();
  394. $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
  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('195901-0001', $result); // counter must start to 1
  401. $localobject=new Facture($this->savdb);
  402. $localobject->initAsSpecimen();
  403. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1960); // we use same year but fiscal month after
  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('196012-0001', $result); // counter must be reset to 1
  410. $localobject=new Facture($this->savdb);
  411. $localobject->initAsSpecimen();
  412. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1961); // we use same fiscal year but different year
  413. $numbering=new mod_facture_mercure();
  414. $result=$numbering->getNextValue($mysoc, $localobject);
  415. $result2=$localobject->create($user, 1);
  416. $result3=$localobject->validate($user, $result);
  417. print __METHOD__." result=".$result."\n";
  418. $this->assertEquals('196001-0002', $result); // counter must be 2
  419. $localobject=new Facture($this->savdb);
  420. $localobject->initAsSpecimen();
  421. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1961); // we use different discal year but same year
  422. $numbering=new mod_facture_mercure();
  423. $result=$numbering->getNextValue($mysoc, $localobject);
  424. print __METHOD__." result=".$result."\n";
  425. $this->assertEquals('196112-0001', $result); // counter must be reset to 1
  426. // Now we try with a different fiscal month (defined by SOCIETE_FISCAL_MONTH_START) and we always want end year
  427. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  428. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@+}';
  429. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@+}';
  430. $localobject=new Facture($this->savdb);
  431. $localobject->initAsSpecimen();
  432. $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
  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('197001-0001', $result); // counter must start to 1
  439. $localobject=new Facture($this->savdb);
  440. $localobject->initAsSpecimen();
  441. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1970); // we use same year but fiscal month after
  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('197112-0001', $result); // counter must be reset to 1
  448. $localobject=new Facture($this->savdb);
  449. $localobject->initAsSpecimen();
  450. $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1971); // we use same fiscal year but different year
  451. $numbering=new mod_facture_mercure();
  452. $result=$numbering->getNextValue($mysoc, $localobject);
  453. $result2=$localobject->create($user, 1);
  454. $result3=$localobject->validate($user, $result);
  455. print __METHOD__." result=".$result."\n";
  456. $this->assertEquals('197101-0002', $result); // counter must be 2
  457. $localobject=new Facture($this->savdb);
  458. $localobject->initAsSpecimen();
  459. $localobject->date=dol_mktime(12, 0, 0, 12, 1, 1971); // we use different fiscal year but same year
  460. $numbering=new mod_facture_mercure();
  461. $result=$numbering->getNextValue($mysoc, $localobject);
  462. print __METHOD__." result=".$result."\n";
  463. $this->assertEquals('197212-0001', $result); // counter must be reset to 1
  464. // Now we try with a reset every month (@99)
  465. $conf->global->SOCIETE_FISCAL_MONTH_START=6;
  466. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@99}';
  467. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@99}';
  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-0001', $result); // counter must start to 1
  477. $localobject=new Facture($this->savdb);
  478. $localobject->initAsSpecimen();
  479. $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
  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('198001-0002', $result); // counter must start to 2
  486. $localobject=new Facture($this->savdb);
  487. $localobject->initAsSpecimen();
  488. $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
  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('198002-0001', $result); // counter must start to 1
  495. $localobject=new Facture($this->savdb);
  496. $localobject->initAsSpecimen();
  497. $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
  498. $numbering=new mod_facture_mercure();
  499. $result=$numbering->getNextValue($mysoc, $localobject);
  500. $result2=$localobject->create($user, 1);
  501. $result3=$localobject->validate($user, $result);
  502. print __METHOD__." result=".$result."\n";
  503. $this->assertEquals('198101-0001', $result); // counter must start to 1
  504. // Test with {t} tag
  505. $conf->global->SOCIETE_FISCAL_MONTH_START=1;
  506. $conf->global->FACTURE_MERCURE_MASK_CREDIT='{t}{yyyy}{mm}-{0000}';
  507. $conf->global->FACTURE_MERCURE_MASK_INVOICE='{t}{yyyy}{mm}-{0000}';
  508. $tmpthirdparty=new Societe($this->savdb);
  509. $tmpthirdparty->initAsSpecimen();
  510. $tmpthirdparty->typent_code = 'TE_ABC';
  511. $localobject=new Facture($this->savdb);
  512. $localobject->initAsSpecimen();
  513. $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
  514. $numbering=new mod_facture_mercure();
  515. $result=$numbering->getNextValue($tmpthirdparty, $localobject);
  516. $result2=$localobject->create($user, 1);
  517. $result3=$localobject->validate($user, $result);
  518. print __METHOD__." result=".$result."\n";
  519. $this->assertEquals('A198201-0001', $result); // counter must start to 1
  520. return $result;
  521. }
  522. }