From a886136e67eaf0711674583919830aeb9368b3f2 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 13 Jan 2015 22:13:15 +0100
Subject: [PATCH] More comment and add a unit test to check that {t} tag is
 correctly supported.

---
 htdocs/core/lib/functions2.lib.php    |  4 ++--
 test/phpunit/NumberingModulesTest.php | 31 ++++++++++++++++++++++-----
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php
index 54014d907b1..cd99afaf373 100644
--- a/htdocs/core/lib/functions2.lib.php
+++ b/htdocs/core/lib/functions2.lib.php
@@ -628,8 +628,8 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
     if (preg_match('/\{(t+)\}/i',$mask,$regType))
     {
         $masktype=$regType[1];
-        $masktype_value=substr(preg_replace('/^TE_/','',$objsoc->typent_code),0,dol_strlen($regType[1]));//get n first characters of client code where n is length in mask
-        $masktype_value=str_pad($masktype_value,dol_strlen($regType[1]),"#",STR_PAD_RIGHT);
+        $masktype_value=substr(preg_replace('/^TE_/','',$objsoc->typent_code),0,dol_strlen($regType[1]));// get n first characters of thirdpaty typent_code (where n is length in mask)
+        $masktype_value=str_pad($masktype_value,dol_strlen($regType[1]),"#",STR_PAD_RIGHT);				 // we fill on right with # to have same number of char than into mask
     }
     else
     {
diff --git a/test/phpunit/NumberingModulesTest.php b/test/phpunit/NumberingModulesTest.php
index a3788b309d1..48d26216268 100644
--- a/test/phpunit/NumberingModulesTest.php
+++ b/test/phpunit/NumberingModulesTest.php
@@ -500,7 +500,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
     	$conf->global->SOCIETE_FISCAL_MONTH_START=6;
     	$conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@99}';
     	$conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000@99}';
-    	
+
     	$localobject=new Facture($this->savdb);
     	$localobject->initAsSpecimen();
     	$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
@@ -510,7 +510,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
     	$result3=$localobject->validate($user, $result);
     	print __METHOD__." result=".$result."\n";
     	$this->assertEquals('198001-0001', $result);	// counter must start to 1
-    	
+
     	$localobject=new Facture($this->savdb);
     	$localobject->initAsSpecimen();
     	$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
@@ -520,7 +520,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
     	$result3=$localobject->validate($user, $result);
     	print __METHOD__." result=".$result."\n";
     	$this->assertEquals('198001-0002', $result);	// counter must start to 2
-    	
+
     	$localobject=new Facture($this->savdb);
     	$localobject->initAsSpecimen();
     	$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
@@ -530,7 +530,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
     	$result3=$localobject->validate($user, $result);
     	print __METHOD__." result=".$result."\n";
     	$this->assertEquals('198002-0001', $result);	// counter must start to 1
-    	
+
     	$localobject=new Facture($this->savdb);
     	$localobject->initAsSpecimen();
     	$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
@@ -540,7 +540,28 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
     	$result3=$localobject->validate($user, $result);
     	print __METHOD__." result=".$result."\n";
     	$this->assertEquals('198101-0001', $result);	// counter must start to 1
-      	
+
+    	// Test with {t} tag
+    	$conf->global->SOCIETE_FISCAL_MONTH_START=1;
+    	$conf->global->FACTURE_MERCURE_MASK_CREDIT='{t}{yyyy}{mm}-{0000}';
+    	$conf->global->FACTURE_MERCURE_MASK_INVOICE='{t}{yyyy}{mm}-{0000}';
+
+    	$tmpthirdparty=new Societe($this->savdb);
+    	$tmpthirdparty->initAsSpecimen();
+    	$tmpthirdparty->typent_code = 'TE_ABC';
+
+    	$localobject=new Facture($this->savdb);
+    	$localobject->initAsSpecimen();
+    	$localobject->date=dol_mktime(12, 0, 0, 1, 1, 1982);	// we use year 1980 to be sure to not have existing invoice for this year
+    	$numbering=new mod_facture_mercure();
+    	$result=$numbering->getNextValue($tmpthirdparty, $localobject);
+    	$result2=$localobject->create($user,1);
+    	$result3=$localobject->validate($user, $result);
+    	print __METHOD__." result=".$result."\n";
+    	$this->assertEquals('A198201-0001', $result);	// counter must start to 1
+
+
+
     	return $result;
     }
 
-- 
GitLab