functionsnumtoword.lib.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /**
  20. * \file htdocs/core/lib/functionsnumtoword.lib.php
  21. * \brief A set of functions for Dolibarr
  22. * This file contains all frequently used functions.
  23. */
  24. /**
  25. * Function to return a number into a text.
  26. * May use module NUMBERWORDS if found.
  27. *
  28. * @param float $num Number to convert (must be a numeric value, like reported by price2num())
  29. * @param Translate $langs Language
  30. * @param string $currency ''=number to translate | 'XX'=currency code to use into text
  31. * @param boolean $centimes false=no cents/centimes | true=there is cents/centimes
  32. * @return string|false Text of the number
  33. */
  34. function dol_convertToWord($num, $langs, $currency = '', $centimes = false)
  35. {
  36. global $conf;
  37. //$num = str_replace(array(',', ' '), '', trim($num)); This should be useless since $num MUST be a php numeric value
  38. if (!$num) {
  39. return false;
  40. }
  41. if ($centimes && strlen($num) == 1) {
  42. $num = $num * 10;
  43. }
  44. if (!empty($conf->global->MAIN_MODULE_NUMBERWORDS)) {
  45. if ($currency) {
  46. $type = '1';
  47. } else {
  48. $type = '0';
  49. }
  50. $concatWords = $langs->getLabelFromNumber($num, $type);
  51. return $concatWords;
  52. } else {
  53. $TNum = explode('.', $num);
  54. $num = (int) $TNum[0];
  55. $words = array();
  56. $list1 = array(
  57. '',
  58. $langs->transnoentitiesnoconv('one'),
  59. $langs->transnoentitiesnoconv('two'),
  60. $langs->transnoentitiesnoconv('three'),
  61. $langs->transnoentitiesnoconv('four'),
  62. $langs->transnoentitiesnoconv('five'),
  63. $langs->transnoentitiesnoconv('six'),
  64. $langs->transnoentitiesnoconv('seven'),
  65. $langs->transnoentitiesnoconv('eight'),
  66. $langs->transnoentitiesnoconv('nine'),
  67. $langs->transnoentitiesnoconv('ten'),
  68. $langs->transnoentitiesnoconv('eleven'),
  69. $langs->transnoentitiesnoconv('twelve'),
  70. $langs->transnoentitiesnoconv('thirteen'),
  71. $langs->transnoentitiesnoconv('fourteen'),
  72. $langs->transnoentitiesnoconv('fifteen'),
  73. $langs->transnoentitiesnoconv('sixteen'),
  74. $langs->transnoentitiesnoconv('seventeen'),
  75. $langs->transnoentitiesnoconv('eighteen'),
  76. $langs->transnoentitiesnoconv('nineteen')
  77. );
  78. $list2 = array(
  79. '',
  80. $langs->transnoentitiesnoconv('ten'),
  81. $langs->transnoentitiesnoconv('twenty'),
  82. $langs->transnoentitiesnoconv('thirty'),
  83. $langs->transnoentitiesnoconv('forty'),
  84. $langs->transnoentitiesnoconv('fifty'),
  85. $langs->transnoentitiesnoconv('sixty'),
  86. $langs->transnoentitiesnoconv('seventy'),
  87. $langs->transnoentitiesnoconv('eighty'),
  88. $langs->transnoentitiesnoconv('ninety'),
  89. $langs->transnoentitiesnoconv('hundred')
  90. );
  91. $list3 = array(
  92. '',
  93. $langs->transnoentitiesnoconv('thousand'),
  94. $langs->transnoentitiesnoconv('million'),
  95. $langs->transnoentitiesnoconv('billion'),
  96. $langs->transnoentitiesnoconv('trillion'),
  97. $langs->transnoentitiesnoconv('quadrillion')
  98. );
  99. $num_length = strlen($num);
  100. $levels = (int) (($num_length + 2) / 3);
  101. $max_length = $levels * 3;
  102. $num = substr('00'.$num, -$max_length);
  103. $num_levels = str_split($num, 3);
  104. $nboflevels = count($num_levels);
  105. for ($i = 0; $i < $nboflevels; $i++) {
  106. $levels--;
  107. $hundreds = (int) ($num_levels[$i] / 100);
  108. $hundreds = ($hundreds ? ' '.$list1[$hundreds].' '.$langs->transnoentities('hundred').($hundreds == 1 ? '' : 's').' ' : '');
  109. $tens = (int) ($num_levels[$i] % 100);
  110. $singles = '';
  111. if ($tens < 20) {
  112. $tens = ($tens ? ' '.$list1[$tens].' ' : '');
  113. } else {
  114. $tens = (int) ($tens / 10);
  115. $tens = ' '.$list2[$tens].' ';
  116. $singles = (int) ($num_levels[$i] % 10);
  117. $singles = ' '.$list1[$singles].' ';
  118. }
  119. $words[] = $hundreds.$tens.$singles.(($levels && (int) ($num_levels[$i])) ? ' '.$list3[$levels].' ' : '');
  120. } //end for loop
  121. $commas = count($words);
  122. if ($commas > 1) {
  123. $commas = $commas - 1;
  124. }
  125. $concatWords = implode(' ', $words);
  126. // Delete multi whitespaces
  127. $concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords));
  128. if (!empty($currency)) {
  129. $concatWords .= ' '.$currency;
  130. }
  131. // If we need to write cents call again this function for cents
  132. $decimalpart = empty($TNum[1]) ? '' : preg_replace('/0+$/', '', $TNum[1]);
  133. if ($decimalpart) {
  134. if (!empty($currency)) {
  135. $concatWords .= ' '.$langs->transnoentities('and');
  136. }
  137. $concatWords .= ' '.dol_convertToWord($decimalpart, $langs, '', true);
  138. if (!empty($currency)) {
  139. $concatWords .= ' '.$langs->transnoentities('centimes');
  140. }
  141. }
  142. return $concatWords;
  143. }
  144. }
  145. /**
  146. * Function to return number or amount in text.
  147. *
  148. * @deprecated
  149. * @param float $numero Number to convert
  150. * @param Translate $langs Language
  151. * @param string $numorcurrency 'number' or 'amount'
  152. * @return string Text of the number or -1 in case TOO LONG (more than 1000000000000.99)
  153. */
  154. function dolNumberToWord($numero, $langs, $numorcurrency = 'number')
  155. {
  156. // If the number is negative convert to positive and return -1 if it is too long
  157. if ($numero < 0) {
  158. $numero *= -1;
  159. }
  160. if ($numero >= 1000000000001) {
  161. return -1;
  162. }
  163. // Get 2 decimals to cents, another functions round or truncate
  164. $strnumber = number_format($numero, 10);
  165. $len = strlen($strnumber);
  166. for ($i = 0; $i < $len; $i++) {
  167. if ($strnumber[$i] == '.') {
  168. $parte_decimal = $strnumber[$i + 1].$strnumber[$i + 2];
  169. break;
  170. }
  171. }
  172. /*In dolibarr 3.6.2 (my current version) doesn't have $langs->default and
  173. in case exist why ask $lang like a parameter?*/
  174. if (((is_object($langs) && $langs->getDefaultLang(0) == 'es_MX') || (!is_object($langs) && $langs == 'es_MX')) && $numorcurrency == 'currency') {
  175. if ($numero >= 1 && $numero < 2) {
  176. return ("UN PESO ".$parte_decimal." / 100 M.N.");
  177. } elseif ($numero >= 0 && $numero < 1) {
  178. return ("CERO PESOS ".$parte_decimal." / 100 M.N.");
  179. } elseif ($numero >= 1000000 && $numero < 1000001) {
  180. return ("UN MILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
  181. } elseif ($numero >= 1000000000000 && $numero < 1000000000001) {
  182. return ("UN BILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
  183. } else {
  184. $entexto = "";
  185. $number = $numero;
  186. if ($number >= 1000000000) {
  187. $CdMMillon = (int) ($numero / 100000000000);
  188. $numero = $numero - $CdMMillon * 100000000000;
  189. $DdMMillon = (int) ($numero / 10000000000);
  190. $numero = $numero - $DdMMillon * 10000000000;
  191. $UdMMillon = (int) ($numero / 1000000000);
  192. $numero = $numero - $UdMMillon * 1000000000;
  193. $entexto .= hundreds2text($CdMMillon, $DdMMillon, $UdMMillon);
  194. $entexto .= " MIL ";
  195. }
  196. if ($number >= 1000000) {
  197. $CdMILLON = (int) ($numero / 100000000);
  198. $numero = $numero - $CdMILLON * 100000000;
  199. $DdMILLON = (int) ($numero / 10000000);
  200. $numero = $numero - $DdMILLON * 10000000;
  201. $udMILLON = (int) ($numero / 1000000);
  202. $numero = $numero - $udMILLON * 1000000;
  203. $entexto .= hundreds2text($CdMILLON, $DdMILLON, $udMILLON);
  204. if (!$CdMMillon && !$DdMMillon && !$UdMMillon && !$CdMILLON && !$DdMILLON && $udMILLON == 1) {
  205. $entexto .= " MILL&OacuteN ";
  206. } else {
  207. $entexto .= " MILLONES ";
  208. }
  209. }
  210. if ($number >= 1000) {
  211. $cdm = (int) ($numero / 100000);
  212. $numero = $numero - $cdm * 100000;
  213. $ddm = (int) ($numero / 10000);
  214. $numero = $numero - $ddm * 10000;
  215. $udm = (int) ($numero / 1000);
  216. $numero = $numero - $udm * 1000;
  217. $entexto .= hundreds2text($cdm, $ddm, $udm);
  218. if ($cdm || $ddm || $udm) {
  219. $entexto .= " MIL ";
  220. }
  221. }
  222. $c = (int) ($numero / 100);
  223. $numero = $numero - $c * 100;
  224. $d = (int) ($numero / 10);
  225. $u = (int) $numero - $d * 10;
  226. $entexto .= hundreds2text($c, $d, $u);
  227. if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number > 1000000) {
  228. $entexto .= " DE";
  229. }
  230. $entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
  231. }
  232. return $entexto;
  233. }
  234. }
  235. /**
  236. * hundreds2text
  237. *
  238. * @param integer $hundreds Hundreds
  239. * @param integer $tens Tens
  240. * @param integer $units Units
  241. * @return string
  242. */
  243. function hundreds2text($hundreds, $tens, $units)
  244. {
  245. if ($hundreds == 1 && $tens == 0 && $units == 0) {
  246. return "CIEN";
  247. }
  248. $centenas = array("CIENTO", "DOSCIENTOS", "TRESCIENTOS", "CUATROCIENTOS", "QUINIENTOS", "SEISCIENTOS", "SETECIENTOS", "OCHOCIENTOS", "NOVECIENTOS");
  249. $decenas = array("", "", "TREINTA ", "CUARENTA ", "CINCUENTA ", "SESENTA ", "SETENTA ", "OCHENTA ", "NOVENTA ");
  250. $veintis = array("VEINTE", "VEINTIUN", "VEINTID&OacuteS", "VEINTITR&EacuteS", "VEINTICUATRO", "VEINTICINCO", "VEINTIS&EacuteIS", "VEINTISIETE", "VEINTIOCHO", "VEINTINUEVE");
  251. $diecis = array("DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", "QUINCE", "DIECIS&EacuteIS", "DIECISIETE", "DIECIOCHO", "DIECINUEVE");
  252. $unidades = array("UN", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO", "NUEVE");
  253. $entexto = "";
  254. if ($hundreds != 0) {
  255. $entexto .= $centenas[$hundreds - 1];
  256. }
  257. if ($tens > 2) {
  258. if ($hundreds != 0) {
  259. $entexto .= " ";
  260. }
  261. $entexto .= $decenas[$tens - 1];
  262. if ($units != 0) {
  263. $entexto .= " Y ";
  264. $entexto .= $unidades[$units - 1];
  265. }
  266. return $entexto;
  267. } elseif ($tens == 2) {
  268. if ($hundreds != 0) {
  269. $entexto .= " ";
  270. }
  271. $entexto .= " ".$veintis[$units];
  272. return $entexto;
  273. } elseif ($tens == 1) {
  274. if ($hundreds != 0) {
  275. $entexto .= " ";
  276. }
  277. $entexto .= $diecis[$units];
  278. return $entexto;
  279. }
  280. if ($units != 0) {
  281. if ($hundreds != 0 || $tens != 0) {
  282. $entexto .= " ";
  283. }
  284. $entexto .= $unidades[$units - 1];
  285. }
  286. return $entexto;
  287. }