NumberingModulesTest.php 26 KB

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