diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php
index ab41e5aaec7..66b64035c4d 100644
--- a/htdocs/core/lib/functions2.lib.php
+++ b/htdocs/core/lib/functions2.lib.php
@@ -607,7 +607,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$maskraz=$yearoffsettype; // For backward compatibility
else if ($yearoffsettype === '0' || (! empty($yearoffsettype) && ! is_numeric($yearoffsettype) && $conf->global->SOCIETE_FISCAL_MONTH_START > 1))
$maskraz = $conf->global->SOCIETE_FISCAL_MONTH_START;
- //print "maskraz=".$maskraz;
+ //print "maskraz=".$maskraz; // -1=no reset
if ($maskraz > 0) // A reset is required
{
@@ -699,8 +699,8 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$sqlwhere.='(SUBSTRING('.$field.', '.(dol_strlen($reg[1])+1).', '.dol_strlen($reg[2]).") = '".$yearcomp."')";
}
}
- //print "sqlwhere=".$sqlwhere."
\n";
- //print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset." yearcomp=".$yearcomp."
\n";
+ //print "sqlwhere=".$sqlwhere." yearcomp=".$yearcomp."
\n"; // sqlwhere and yearcomp defined only if we ask a reset
+ //print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."
\n";
// Define $sqlstring
$posnumstart=strpos($maskwithnocode,$maskcounter); // Pos of counter in final string (from 0 to ...)
@@ -739,7 +739,10 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
$counter = $obj->val;
}
else dol_print_error($db);
+
+ // Check if we must force counter to maskoffset
if (empty($counter) || preg_match('/[^0-9]/i',$counter)) $counter=$maskoffset;
+ else if ($counter < $maskoffset && empty($conf->global->MAIN_NUMBERING_OFFSET_ONLY_FOR_FIRST)) $counter=$maskoffset;
if ($mode == 'last') // We found value for counter = last counter value. Now need to get corresponding ref of invoice.
{
@@ -824,7 +827,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
}
else dol_print_error($db);
if (empty($maskrefclient_counter) || preg_match('/[^0-9]/i',$maskrefclient_counter)) $maskrefclient_counter=$maskrefclient_maskoffset;
- $maskrefclient_counter++;
+ $maskrefclient_counter++;
}
// Build numFinal
diff --git a/test/phpunit/NumberingModulesTest.php b/test/phpunit/NumberingModulesTest.php
index 82bb96dc0be..5e93e32fe22 100644
--- a/test/phpunit/NumberingModulesTest.php
+++ b/test/phpunit/NumberingModulesTest.php
@@ -147,7 +147,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
$result=$localobject->is_erasable();
print __METHOD__." is_erasable=".$result."\n";
$this->assertEquals(1, $result); // Can be deleted
-
+
$localobject2=new Facture($this->savdb);
$localobject2->initAsSpecimen();
$localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1916); // we use following year for second invoice (there is no reset into mask)
@@ -166,7 +166,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
$result=$localobject->is_erasable();
print __METHOD__." is_erasable=".$result."\n";
$this->assertEquals(0, $result); // Case 1 can not be deleted (case 2 is more recent)
-
+
// Now we try with a reset
$conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}-{0000@1}';
$conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}-{0000@1}';
@@ -194,7 +194,7 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
$result=$localobject->is_erasable();
print __METHOD__." is_erasable=".$result."\n";
$this->assertEquals(1, $result); // Can be deleted
-
+
$localobject2=new Facture($this->savdb);
$localobject2->initAsSpecimen();
$localobject2->date=dol_mktime(12, 0, 0, 1, 1, 1921); // we use following year for second invoice (and there is a reset required)
@@ -210,7 +210,11 @@ class NumberingModulesTest extends PHPUnit_Framework_TestCase
$result=$localobject->is_erasable();
print __METHOD__." is_erasable=".$result."\n";
$this->assertEquals(1, $result); // Case 1 can be deleted (because there was a reset for case 2)
-
+
+ // Try an offset when an invoice already exists
+ $conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000+9990}';
+ $conf->global->FACTURE_MERCURE_MASK_INVOICE='{yyyy}{mm}-{0000+9990}';
+ $result=$numbering->getNextValue($mysoc, $localobject2);
// Now we try with a different fiscal month (forced by mask)
$conf->global->FACTURE_MERCURE_MASK_CREDIT='{yyyy}{mm}-{0000@6}';