dolibarr 19.0.4
functionsnumtoword.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2015 Víctor Ortiz Pérez <victor@accett.com.mx>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
36function dol_convertToWord($num, $langs, $currency = '', $centimes = false)
37{
38 global $conf;
39
40 //$num = str_replace(array(',', ' '), '', trim($num)); This should be useless since $num MUST be a php numeric value
41 if (!$num) {
42 return false;
43 }
44
45 if ($centimes && strlen($num) == 1) {
46 $num = $num * 10;
47 }
48
49 if (isModEnabled('numberwords')) {
50 if ($currency) {
51 $type = '1';
52 } else {
53 $type = '0';
54 }
55
56 $concatWords = $langs->getLabelFromNumber($num, $type);
57 return $concatWords;
58 } else {
59 $TNum = explode('.', $num);
60
61 $num = (int) $TNum[0];
62 $words = array();
63 $list1 = array(
64 '',
65 $langs->transnoentitiesnoconv('one'),
66 $langs->transnoentitiesnoconv('two'),
67 $langs->transnoentitiesnoconv('three'),
68 $langs->transnoentitiesnoconv('four'),
69 $langs->transnoentitiesnoconv('five'),
70 $langs->transnoentitiesnoconv('six'),
71 $langs->transnoentitiesnoconv('seven'),
72 $langs->transnoentitiesnoconv('eight'),
73 $langs->transnoentitiesnoconv('nine'),
74 $langs->transnoentitiesnoconv('ten'),
75 $langs->transnoentitiesnoconv('eleven'),
76 $langs->transnoentitiesnoconv('twelve'),
77 $langs->transnoentitiesnoconv('thirteen'),
78 $langs->transnoentitiesnoconv('fourteen'),
79 $langs->transnoentitiesnoconv('fifteen'),
80 $langs->transnoentitiesnoconv('sixteen'),
81 $langs->transnoentitiesnoconv('seventeen'),
82 $langs->transnoentitiesnoconv('eighteen'),
83 $langs->transnoentitiesnoconv('nineteen')
84 );
85 $list2 = array(
86 '',
87 $langs->transnoentitiesnoconv('ten'),
88 $langs->transnoentitiesnoconv('twenty'),
89 $langs->transnoentitiesnoconv('thirty'),
90 $langs->transnoentitiesnoconv('forty'),
91 $langs->transnoentitiesnoconv('fifty'),
92 $langs->transnoentitiesnoconv('sixty'),
93 $langs->transnoentitiesnoconv('seventy'),
94 $langs->transnoentitiesnoconv('eighty'),
95 $langs->transnoentitiesnoconv('ninety'),
96 $langs->transnoentitiesnoconv('hundred')
97 );
98 $list3 = array(
99 '',
100 $langs->transnoentitiesnoconv('thousand'),
101 $langs->transnoentitiesnoconv('million'),
102 $langs->transnoentitiesnoconv('billion'),
103 $langs->transnoentitiesnoconv('trillion'),
104 $langs->transnoentitiesnoconv('quadrillion')
105 );
106
107 $num_length = strlen($num);
108 $levels = (int) (($num_length + 2) / 3);
109 $max_length = $levels * 3;
110 $num = substr('00'.$num, -$max_length);
111 $num_levels = str_split($num, 3);
112 $nboflevels = count($num_levels);
113 for ($i = 0; $i < $nboflevels; $i++) {
114 $levels--;
115 $hundreds = (int) ($num_levels[$i] / 100);
116 $hundreds = ($hundreds ? ' '.$list1[$hundreds].' '.$langs->transnoentities('hundred').($hundreds == 1 ? '' : 's').' ' : '');
117 $tens = (int) ($num_levels[$i] % 100);
118 $singles = '';
119 if ($tens < 20) {
120 $tens = ($tens ? ' '.$list1[$tens].' ' : '');
121 } else {
122 $tens = (int) ($tens / 10);
123 $tens = ' '.$list2[$tens].' ';
124 $singles = (int) ($num_levels[$i] % 10);
125 $singles = ' '.$list1[$singles].' ';
126 }
127 $words[] = $hundreds.$tens.$singles.(($levels && (int) ($num_levels[$i])) ? ' '.$list3[$levels].' ' : '');
128 } //end for loop
129 $commas = count($words);
130 if ($commas > 1) {
131 $commas = $commas - 1;
132 }
133 $concatWords = implode(' ', $words);
134 // Delete multi whitespaces
135 $concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords));
136
137 if (!empty($currency)) {
138 $concatWords .= ' '.$currency;
139 }
140
141 // If we need to write cents call again this function for cents
142 $decimalpart = empty($TNum[1]) ? '' : preg_replace('/0+$/', '', $TNum[1]);
143
144 if ($decimalpart) {
145 if (!empty($currency)) {
146 $concatWords .= ' '.$langs->transnoentities('and');
147 }
148
149 $concatWords .= ' '.dol_convertToWord($decimalpart, $langs, '', true);
150 if (!empty($currency)) {
151 $concatWords .= ' '.$langs->transnoentities('centimes');
152 }
153 }
154 return $concatWords;
155 }
156}
157
158
168function dolNumberToWord($numero, $langs, $numorcurrency = 'number')
169{
170 // If the number is negative convert to positive and return -1 if it is too long
171 if ($numero < 0) {
172 $numero *= -1;
173 }
174 if ($numero >= 1000000000001) {
175 return -1;
176 }
177
178 // Get 2 decimals to cents, another functions round or truncate
179 $strnumber = number_format($numero, 10);
180 $len = strlen($strnumber);
181 for ($i = 0; $i < $len; $i++) {
182 if ($strnumber[$i] == '.') {
183 $parte_decimal = $strnumber[$i + 1].$strnumber[$i + 2];
184 break;
185 }
186 }
187
188 /*In dolibarr 3.6.2 (my current version) doesn't have $langs->default and
189 in case exist why ask $lang like a parameter?*/
190 if (((is_object($langs) && $langs->getDefaultLang(0) == 'es_MX') || (!is_object($langs) && $langs == 'es_MX')) && $numorcurrency == 'currency') {
191 if ($numero >= 1 && $numero < 2) {
192 return ("UN PESO ".$parte_decimal." / 100 M.N.");
193 } elseif ($numero >= 0 && $numero < 1) {
194 return ("CERO PESOS ".$parte_decimal." / 100 M.N.");
195 } elseif ($numero >= 1000000 && $numero < 1000001) {
196 return ("UN MILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
197 } elseif ($numero >= 1000000000000 && $numero < 1000000000001) {
198 return ("UN BILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
199 } else {
200 $entexto = "";
201 $number = $numero;
202 if ($number >= 1000000000) {
203 $CdMMillon = (int) ($numero / 100000000000);
204 $numero = $numero - $CdMMillon * 100000000000;
205 $DdMMillon = (int) ($numero / 10000000000);
206 $numero = $numero - $DdMMillon * 10000000000;
207 $UdMMillon = (int) ($numero / 1000000000);
208 $numero = $numero - $UdMMillon * 1000000000;
209 $entexto .= hundreds2text($CdMMillon, $DdMMillon, $UdMMillon);
210 $entexto .= " MIL ";
211 }
212 if ($number >= 1000000) {
213 $CdMILLON = (int) ($numero / 100000000);
214 $numero = $numero - $CdMILLON * 100000000;
215 $DdMILLON = (int) ($numero / 10000000);
216 $numero = $numero - $DdMILLON * 10000000;
217 $udMILLON = (int) ($numero / 1000000);
218 $numero = $numero - $udMILLON * 1000000;
219 $entexto .= hundreds2text($CdMILLON, $DdMILLON, $udMILLON);
220 if (!$CdMMillon && !$DdMMillon && !$UdMMillon && !$CdMILLON && !$DdMILLON && $udMILLON == 1) {
221 $entexto .= " MILL&OacuteN ";
222 } else {
223 $entexto .= " MILLONES ";
224 }
225 }
226 if ($number >= 1000) {
227 $cdm = (int) ($numero / 100000);
228 $numero = $numero - $cdm * 100000;
229 $ddm = (int) ($numero / 10000);
230 $numero = $numero - $ddm * 10000;
231 $udm = (int) ($numero / 1000);
232 $numero = $numero - $udm * 1000;
233 $entexto .= hundreds2text($cdm, $ddm, $udm);
234 if ($cdm || $ddm || $udm) {
235 $entexto .= " MIL ";
236 }
237 }
238 $c = (int) ($numero / 100);
239 $numero = $numero - $c * 100;
240 $d = (int) ($numero / 10);
241 $u = (int) $numero - $d * 10;
242 $entexto .= hundreds2text($c, $d, $u);
243 if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number > 1000000) {
244 $entexto .= " DE";
245 }
246 $entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
247 }
248 return $entexto;
249 }
250 return -1;
251}
252
261function hundreds2text($hundreds, $tens, $units)
262{
263 if ($hundreds == 1 && $tens == 0 && $units == 0) {
264 return "CIEN";
265 }
266 $centenas = array("CIENTO", "DOSCIENTOS", "TRESCIENTOS", "CUATROCIENTOS", "QUINIENTOS", "SEISCIENTOS", "SETECIENTOS", "OCHOCIENTOS", "NOVECIENTOS");
267 $decenas = array("", "", "TREINTA ", "CUARENTA ", "CINCUENTA ", "SESENTA ", "SETENTA ", "OCHENTA ", "NOVENTA ");
268 $veintis = array("VEINTE", "VEINTIUN", "VEINTID&OacuteS", "VEINTITR&EacuteS", "VEINTICUATRO", "VEINTICINCO", "VEINTIS&EacuteIS", "VEINTISIETE", "VEINTIOCHO", "VEINTINUEVE");
269 $diecis = array("DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", "QUINCE", "DIECIS&EacuteIS", "DIECISIETE", "DIECIOCHO", "DIECINUEVE");
270 $unidades = array("UN", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO", "NUEVE");
271 $entexto = "";
272 if ($hundreds != 0) {
273 $entexto .= $centenas[$hundreds - 1];
274 }
275 if ($tens > 2) {
276 if ($hundreds != 0) {
277 $entexto .= " ";
278 }
279 $entexto .= $decenas[$tens - 1];
280 if ($units != 0) {
281 $entexto .= " Y ";
282 $entexto .= $unidades[$units - 1];
283 }
284 return $entexto;
285 } elseif ($tens == 2) {
286 if ($hundreds != 0) {
287 $entexto .= " ";
288 }
289 $entexto .= " ".$veintis[$units];
290 return $entexto;
291 } elseif ($tens == 1) {
292 if ($hundreds != 0) {
293 $entexto .= " ";
294 }
295 $entexto .= $diecis[$units];
296 return $entexto;
297 }
298 if ($units != 0) {
299 if ($hundreds != 0 || $tens != 0) {
300 $entexto .= " ";
301 }
302 $entexto .= $unidades[$units - 1];
303 }
304 return $entexto;
305}
dol_convertToWord($num, $langs, $currency='', $centimes=false)
Function to return a number into a text.
hundreds2text($hundreds, $tens, $units)
hundreds2text
dolNumberToWord($numero, $langs, $numorcurrency='number')
Function to return number or amount in text.