NumberingModulesTest.php 27 KB

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