diff --git a/htdocs/core/lib/functionsnumtoword.lib.php b/htdocs/core/lib/functionsnumtoword.lib.php
index daab7085c253f58aa2ec72ab8ba6ede14d1f9f5c..c1a21e8191663dff563428cf31aca0e9751b600a 100644
--- a/htdocs/core/lib/functionsnumtoword.lib.php
+++ b/htdocs/core/lib/functionsnumtoword.lib.php
@@ -16,117 +16,135 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  * or see http://www.gnu.org/
  */
-
 /**
  *	\file			htdocs/core/lib/functionsnumbertoword.lib.php
  *	\brief			A set of functions for Dolibarr
  *					This file contains all frequently used functions.
  */
-
 /**
  * Function to return number or amount in text.
  *
  * @param	float 	$numero			Number to convert
  * @param	Lang	$langs			Language
  * @param	string	$numorcurrency	'number' or 'amount'
- * @return 	string  				Text of the number
+ * @return 	string  				Text of the number or -1 in case TOO LONG (more than 1000000000000.99)
  */
 function dolNumberToWord($numero, $langs, $numorcurrency='number')
 {
-	$entexto=$numero;
-
-	if ($langs->default == 'es_MX' && $numorcurrency == 'currency')
-	{
-	    $veintis = array("VEINTE","VEINTIUN","VEINTID&OacuteS","VEINTITR&EacuteS","VEINTICUATRO","VEINTICINCO","VEINTIS&EacuteIS","VEINTISIETE","VEINTIOCHO","VEINTINUEVE");
-	    $unidades = array("UN","DOS","TRES","CUATRO","CINCO","SEIS","SIETE","OCHO","NUEVE");
-	    $decenas = array("","","TREINTA ","CUARENTA ","CINCUENTA ","SESENTA ","SETENTA ","OCHENTA ","NOVENTA ");
-	    $centenas = array("CIENTO","DOSCIENTOS","TRESCIENTOS","CUATROCIENTOS","QUINIENTOS","SEISCIENTOS","SETECIENTOS","OCHOCIENTOS","NOVECIENTOS");
-	    $number = $numero;
-	    $parte_decimal = $numero - (int) $numero;
-	    $parte_decimal = (int) round($parte_decimal*100);
-	    if ($parte_decimal < 10)
-		$parte_decimal = "0".$parte_decimal;
-	    $entexto ="";
-	    if ($numero>=1 && $numero<2) {
-		$entexto .= " UN PESO ".$parte_decimal." / 100 M.N.";
-	    }
-	    elseif ($numero>=0 && $numero<1){
-		$entexto .= " CERO PESOS ".$parte_decimal." / 100 M.N.";
-	    }
-	    elseif ($numero>=100 && $numero<101){
-		$entexto .= " CIEN PESOS ".$parte_decimal." / 100 M.N.";
-	    }
-	    else {
-		$cdm = (int) ($numero / 100000);
-		$numero = $numero - $cdm * 100000;
-		$ddm = (int) ($numero / 10000);
-		$numero = $numero - $ddm * 10000;
-		$udm = (int) ($numero / 1000);
-		$numero = $numero - $udm * 1000;
-		$c = (int) ($numero / 100);
-		$numero = $numero - $c * 100;
-		$d = (int) ($numero / 10);
-		$u = (int) $numero - $d * 10;
-		$completo=FALSE;
-		if ($cdm==1 && $ddm==0 && $udm==0){
-	            $entexto .= "CIEN";
-	            $completo = TRUE;
+	// If the number is negative convert to positive and return -1 if is too long
+	if ($numero < 0) $numero *= -1;
+	if ($numero >= 1000000000001)
+		return -1;
+	// Get 2 decimals to cents, another functions round or truncate
+	$strnumber = number_format ($numero,10);
+	for ($i=0; $i<strlen($strnumber); $i++){
+		if ($strnumber[$i]=='.') {
+			$parte_decimal = $strnumber[$i+1].$strnumber[$i+2];
+			break;
 		}
-		if ($cdm!=0 && !$completo){
-	            $entexto .= $centenas[$cdm-1]." ";
+	}
+	/*In dolibarr 3.6.2 (my current version) doesn't have $langs->default and
+	in case exist why ask $lang like a parameter?*/
+	if ($langs == 'es_MX' && $numorcurrency == 'currency')
+	{	
+		
+		if ($numero>=1 && $numero<2) {
+			return ("UN PESO ".$parte_decimal." / 100 M.N.");
 		}
-		$completo=FALSE;
-	        if ($ddm>2){
-	            $entexto .= " ".$decenas[$ddm-1];
-	            if ($udm!=0){
-			$entexto .= " Y ";
-	            }
+		elseif ($numero>=0 && $numero<1){
+			return ("CERO PESOS ".$parte_decimal." / 100 M.N.");
 		}
-		elseif ($ddm!=0){
-	            $completo=TRUE;
-	            if ($ddm==1){
-			$entexto .= " ".$diecis[$udm];
-	            }
-	            else{
-			$entexto .= " ".$veintis[$udm];
-	            }
+		elseif ($numero>=1000000 && $numero<1000001){
+			return ("UN MILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
 		}
-		if ($udm!=0 && !$completo){
-	            $entexto .= $unidades[$udm-1];
+		elseif ($numero>=1000000000000 && $numero<1000000000001){
+			return ("UN BILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
 		}
-		$completo=FALSE;
-		if ($number>=1000){
-	            $entexto .= " MIL ";
+		else {
+			$entexto ="";
+			$number = $numero;
+			if ($number >= 1000000000){
+				$CdMMillon = (int) ($numero / 100000000000);
+				$numero = $numero - $CdMMillon * 100000000000;
+				$DdMMillon = (int) ($numero / 10000000000);
+				$numero = $numero - $DdMMillon * 10000000000;
+				$UdMMillon = (int) ($numero / 1000000000);
+				$numero = $numero - $UdMMillon * 1000000000;
+				$entexto .= hundreds2text ($CdMMillon, $DdMMillon, $UdMMillon);
+				$entexto .= " MIL ";
+			}
+			if ($number >= 1000000){
+				$CdMILLON = (int) ($numero / 100000000);
+				$numero = $numero - $CdMILLON * 100000000;
+				$DdMILLON = (int) ($numero / 10000000);
+				$numero = $numero - $DdMILLON * 10000000;
+				$udMILLON = (int) ($numero / 1000000);
+				$numero = $numero - $udMILLON * 1000000;
+				$entexto .= hundreds2text ($CdMILLON, $DdMILLON, $udMILLON);
+				if (!$CdMMillon && !$DdMMillon && !$UdMMillon && !$CdMILLON && !$DdMILLON && $udMILLON==1)
+					$entexto .= " MILL&OacuteN ";
+				else
+					$entexto .= " MILLONES ";
+			}
+			if ($number >= 1000) {
+				$cdm = (int) ($numero / 100000);
+				$numero = $numero - $cdm * 100000;
+				$ddm = (int) ($numero / 10000);
+				$numero = $numero - $ddm * 10000;
+				$udm = (int) ($numero / 1000);
+				$numero = $numero - $udm * 1000;
+				$entexto .= hundreds2text ($cdm, $ddm, $udm);
+				if ($cdm || $ddm || $udm)
+					$entexto .= " MIL ";
+			}
+			$c = (int) ($numero / 100);
+			$numero = $numero - $c * 100;
+			$d = (int) ($numero / 10);
+			$u = (int) $numero - $d * 10;
+			$entexto .= hundreds2text ($c, $d, $u);
+			if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number>1000000)
+				$entexto .= " DE";
+			$entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
 		}
+		return $entexto;
+	}
+}
 
-	        if ($c==1 && $d==0 && $u==0){
-	            $entexto .= "CIEN";
-	            $completo = TRUE;
-		}
-		if ($c!=0 && !$completo){
-	            $entexto .= $centenas[$c-1]." ";
-		}
-	        if ($d>2){
-	            $entexto .= " ".$decenas[$d-1];
-	            if ($u!=0){
+function hundreds2text ($hundreds, $tens, $units){
+	if ($hundreds==1 && $tens==0 && $units==0){
+		return "CIEN";
+	}
+	$centenas = array("CIENTO","DOSCIENTOS","TRESCIENTOS","CUATROCIENTOS","QUINIENTOS","SEISCIENTOS","SETECIENTOS","OCHOCIENTOS","NOVECIENTOS");
+	$decenas = array("","","TREINTA ","CUARENTA ","CINCUENTA ","SESENTA ","SETENTA ","OCHENTA ","NOVENTA ");
+	$veintis = array("VEINTE","VEINTIUN","VEINTID&OacuteS","VEINTITR&EacuteS","VEINTICUATRO","VEINTICINCO","VEINTIS&EacuteIS","VEINTISIETE","VEINTIOCHO","VEINTINUEVE");
+	$diecis = array("DIEZ","ONCE","DOCE","TRECE","CATORCE","QUINCE","DIECIS&EacuteIS","DIECISIETE","DIECIOCHO","DIECINUEVE");
+    $unidades = array("UN","DOS","TRES","CUATRO","CINCO","SEIS","SIETE","OCHO","NUEVE");
+	$entexto = "";
+	if ($hundreds!=0){
+		$entexto .= $centenas[$hundreds-1];
+	}
+	if ($tens>2){
+		if ($hundreds!=0) $entexto .= " ";
+		$entexto .= $decenas[$tens-1];
+		if ($units!=0){
 			$entexto .= " Y ";
-	            }
-	        }
-		elseif ($d!=0){
-	            $completo=TRUE;
-	            if ($d==1){
-			$entexto .= " ".$diecis[$u];
-	            }
-	            else{
-			$entexto .= " ".$veintis[$u];
-	            }
-		}
-		if ($u!=0 && !$completo){
-	            $entexto .= $unidades[$u-1];
+			$entexto .= $unidades[$units-1];
 		}
-		$entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
-	    }
+		return $entexto;
 	}
-
-    return $entexto;
+	elseif ($tens==2){
+		if ($hundreds!=0) $entexto .= " ";
+		$entexto .= " ".$veintis[$units];
+		return $entexto;
+	}
+	elseif ($tens==1){
+		if ($hundreds!=0) $entexto .= " ";
+		$entexto .= $diecis[$units];
+		return $entexto;
+	}
+	if ($units!=0) {
+		if ($hundreds!=0 || $tens!=0) $entexto .= " ";
+		$entexto .= $unidades[$units-1];
+	}
+	return $entexto;
 }