forked from Wavyzz/dolibarr
NEW PDF with numbertoword
This commit is contained in:
@@ -21,9 +21,121 @@
|
||||
* \brief A set of functions for Dolibarr
|
||||
* This file contains all frequently used functions.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Function to return number in text.
|
||||
*
|
||||
* @deprecated
|
||||
* @param float $numero Number to convert
|
||||
* @param Lang $langs Language
|
||||
* @param string $numorcurrency 'number' or 'amount'
|
||||
* @return string Text of the number or -1 in case TOO LONG (more than 1000000000000.99)
|
||||
*/
|
||||
function dol_convertToWord($num = false, $langs, $currency=false, $centimes=false)
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$num = str_replace(array(',', ' '), '' , trim($num));
|
||||
if(! $num) {
|
||||
return false;
|
||||
}
|
||||
if($centimes && strlen($num) == 1) {
|
||||
$num = $num*10;
|
||||
}
|
||||
$TNum = explode('.',$num);
|
||||
$num = (int) $TNum[0];
|
||||
$words = array();
|
||||
$list1 = array(
|
||||
'',
|
||||
$langs->transnoentitiesnoconv('one'),
|
||||
$langs->transnoentitiesnoconv('two'),
|
||||
$langs->transnoentitiesnoconv('three'),
|
||||
$langs->transnoentitiesnoconv('four'),
|
||||
$langs->transnoentitiesnoconv('five'),
|
||||
$langs->transnoentitiesnoconv('six'),
|
||||
$langs->transnoentitiesnoconv('seven'),
|
||||
$langs->transnoentitiesnoconv('eight'),
|
||||
$langs->transnoentitiesnoconv('nine'),
|
||||
$langs->transnoentitiesnoconv('ten'),
|
||||
$langs->transnoentitiesnoconv('eleven'),
|
||||
$langs->transnoentitiesnoconv('twelve'),
|
||||
$langs->transnoentitiesnoconv('thirteen'),
|
||||
$langs->transnoentitiesnoconv('fourteen'),
|
||||
$langs->transnoentitiesnoconv('fifteen'),
|
||||
$langs->transnoentitiesnoconv('sixteen'),
|
||||
$langs->transnoentitiesnoconv('seventeen'),
|
||||
$langs->transnoentitiesnoconv('eighteen'),
|
||||
$langs->transnoentitiesnoconv('nineteen')
|
||||
);
|
||||
$list2 = array(
|
||||
'',
|
||||
$langs->transnoentitiesnoconv('ten'),
|
||||
$langs->transnoentitiesnoconv('twenty'),
|
||||
$langs->transnoentitiesnoconv('thirty'),
|
||||
$langs->transnoentitiesnoconv('forty'),
|
||||
$langs->transnoentitiesnoconv('fifty'),
|
||||
$langs->transnoentitiesnoconv('sixty'),
|
||||
$langs->transnoentitiesnoconv('seventy'),
|
||||
$langs->transnoentitiesnoconv('eighty'),
|
||||
$langs->transnoentitiesnoconv('ninety'),
|
||||
$langs->transnoentitiesnoconv('hundred')
|
||||
);
|
||||
$list3 = array(
|
||||
'',
|
||||
$langs->transnoentitiesnoconv('thousand'),
|
||||
$langs->transnoentitiesnoconv('million'),
|
||||
$langs->transnoentitiesnoconv('billion'),
|
||||
$langs->transnoentitiesnoconv('trillion'),
|
||||
$langs->transnoentitiesnoconv('quadrillion')
|
||||
);
|
||||
$num_length = strlen($num);
|
||||
$levels = (int) (($num_length + 2) / 3);
|
||||
$max_length = $levels * 3;
|
||||
$num = substr('00' . $num, -$max_length);
|
||||
$num_levels = str_split($num, 3);
|
||||
for ($i = 0; $i < count($num_levels); $i++) {
|
||||
$levels--;
|
||||
$hundreds = (int) ($num_levels[$i] / 100);
|
||||
$hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' '.$langs->transnoentities('hundred') . ( $hundreds == 1 ? '' : 's' ) . ' ': '');
|
||||
$tens = (int) ($num_levels[$i] % 100);
|
||||
$singles = '';
|
||||
if ( $tens < 20 ) {
|
||||
$tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' );
|
||||
} else {
|
||||
$tens = (int)($tens / 10);
|
||||
$tens = ' ' . $list2[$tens] . ' ';
|
||||
$singles = (int) ($num_levels[$i] % 10);
|
||||
$singles = ' ' . $list1[$singles] . ' ';
|
||||
}
|
||||
$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );
|
||||
} //end for loop
|
||||
$commas = count($words);
|
||||
if ($commas > 1) {
|
||||
$commas = $commas - 1;
|
||||
}
|
||||
$concatWords = implode(' ', $words);
|
||||
// Delete multi whitespaces
|
||||
$concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords));
|
||||
|
||||
if(!empty($currency)) {
|
||||
$concatWords .= ' '.$currency;
|
||||
}
|
||||
|
||||
// If we need to write cents call again this function for cents
|
||||
if(!empty($TNum[1])) {
|
||||
if(!empty($currency)) $concatWords .= ' '.$langs->transnoentities('and');
|
||||
$concatWords .= ' '.dol_convertToWord($TNum[1], $langs, $currency, true);
|
||||
if(!empty($currency)) $concatWords .= ' '.$langs->transnoentities('centimes');
|
||||
}
|
||||
return $concatWords;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Function to return number or amount in text.
|
||||
*
|
||||
*
|
||||
* @deprecated
|
||||
* @param float $numero Number to convert
|
||||
* @param Lang $langs Language
|
||||
* @param string $numorcurrency 'number' or 'amount'
|
||||
@@ -45,6 +157,7 @@ function dolNumberToWord($numero, $langs, $numorcurrency='number')
|
||||
break;
|
||||
}
|
||||
}
|
||||
var_dump($langs);
|
||||
/*In dolibarr 3.6.2 (my current version) doesn't have $langs->default and
|
||||
in case exist why ask $lang like a parameter?*/
|
||||
if (((is_object($langs) && $langs->default == 'es_MX') || (! is_object($langs) && $langs == 'es_MX')) && $numorcurrency == 'currency')
|
||||
|
||||
Reference in New Issue
Block a user