functionsnumtoword.lib.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/functionsnumbertoword.lib.php
  21. * \brief A set of functions for Dolibarr
  22. * This file contains all frequently used functions.
  23. */
  24. /**
  25. * Function to return number or amount in text.
  26. *
  27. * @param float $numero Number to convert
  28. * @param Lang $langs Language
  29. * @param string $numorcurrency 'number' or 'amount'
  30. * @return string Text of the number or -1 in case TOO LONG (more than 1000000000000.99)
  31. */
  32. function dolNumberToWord($numero, $langs, $numorcurrency='number')
  33. {
  34. // If the number is negative convert to positive and return -1 if is too long
  35. if ($numero < 0) $numero *= -1;
  36. if ($numero >= 1000000000001)
  37. return -1;
  38. // Get 2 decimals to cents, another functions round or truncate
  39. $strnumber = number_format ($numero,10);
  40. $len=strlen($strnumber);
  41. for ($i=0; $i<$len; $i++)
  42. {
  43. if ($strnumber[$i]=='.') {
  44. $parte_decimal = $strnumber[$i+1].$strnumber[$i+2];
  45. break;
  46. }
  47. }
  48. /*In dolibarr 3.6.2 (my current version) doesn't have $langs->default and
  49. in case exist why ask $lang like a parameter?*/
  50. if (((is_object($langs) && $langs->default == 'es_MX') || (! is_object($langs) && $langs == 'es_MX')) && $numorcurrency == 'currency')
  51. {
  52. if ($numero>=1 && $numero<2) {
  53. return ("UN PESO ".$parte_decimal." / 100 M.N.");
  54. }
  55. elseif ($numero>=0 && $numero<1){
  56. return ("CERO PESOS ".$parte_decimal." / 100 M.N.");
  57. }
  58. elseif ($numero>=1000000 && $numero<1000001){
  59. return ("UN MILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
  60. }
  61. elseif ($numero>=1000000000000 && $numero<1000000000001){
  62. return ("UN BILL&OacuteN DE PESOS ".$parte_decimal." / 100 M.N.");
  63. }
  64. else {
  65. $entexto ="";
  66. $number = $numero;
  67. if ($number >= 1000000000){
  68. $CdMMillon = (int) ($numero / 100000000000);
  69. $numero = $numero - $CdMMillon * 100000000000;
  70. $DdMMillon = (int) ($numero / 10000000000);
  71. $numero = $numero - $DdMMillon * 10000000000;
  72. $UdMMillon = (int) ($numero / 1000000000);
  73. $numero = $numero - $UdMMillon * 1000000000;
  74. $entexto .= hundreds2text ($CdMMillon, $DdMMillon, $UdMMillon);
  75. $entexto .= " MIL ";
  76. }
  77. if ($number >= 1000000){
  78. $CdMILLON = (int) ($numero / 100000000);
  79. $numero = $numero - $CdMILLON * 100000000;
  80. $DdMILLON = (int) ($numero / 10000000);
  81. $numero = $numero - $DdMILLON * 10000000;
  82. $udMILLON = (int) ($numero / 1000000);
  83. $numero = $numero - $udMILLON * 1000000;
  84. $entexto .= hundreds2text ($CdMILLON, $DdMILLON, $udMILLON);
  85. if (!$CdMMillon && !$DdMMillon && !$UdMMillon && !$CdMILLON && !$DdMILLON && $udMILLON==1)
  86. $entexto .= " MILL&OacuteN ";
  87. else
  88. $entexto .= " MILLONES ";
  89. }
  90. if ($number >= 1000) {
  91. $cdm = (int) ($numero / 100000);
  92. $numero = $numero - $cdm * 100000;
  93. $ddm = (int) ($numero / 10000);
  94. $numero = $numero - $ddm * 10000;
  95. $udm = (int) ($numero / 1000);
  96. $numero = $numero - $udm * 1000;
  97. $entexto .= hundreds2text ($cdm, $ddm, $udm);
  98. if ($cdm || $ddm || $udm)
  99. $entexto .= " MIL ";
  100. }
  101. $c = (int) ($numero / 100);
  102. $numero = $numero - $c * 100;
  103. $d = (int) ($numero / 10);
  104. $u = (int) $numero - $d * 10;
  105. $entexto .= hundreds2text ($c, $d, $u);
  106. if (!$cdm && !$ddm && !$udm && !$c && !$d && !$u && $number>1000000)
  107. $entexto .= " DE";
  108. $entexto .= " PESOS ".$parte_decimal." / 100 M.N.";
  109. }
  110. return $entexto;
  111. }
  112. }
  113. /**
  114. * hundreds2text
  115. *
  116. * @param integer $hundreds Hundreds
  117. * @param integer $tens Tens
  118. * @param integer $units Units
  119. */
  120. function hundreds2text($hundreds, $tens, $units)
  121. {
  122. if ($hundreds==1 && $tens==0 && $units==0){
  123. return "CIEN";
  124. }
  125. $centenas = array("CIENTO","DOSCIENTOS","TRESCIENTOS","CUATROCIENTOS","QUINIENTOS","SEISCIENTOS","SETECIENTOS","OCHOCIENTOS","NOVECIENTOS");
  126. $decenas = array("","","TREINTA ","CUARENTA ","CINCUENTA ","SESENTA ","SETENTA ","OCHENTA ","NOVENTA ");
  127. $veintis = array("VEINTE","VEINTIUN","VEINTID&OacuteS","VEINTITR&EacuteS","VEINTICUATRO","VEINTICINCO","VEINTIS&EacuteIS","VEINTISIETE","VEINTIOCHO","VEINTINUEVE");
  128. $diecis = array("DIEZ","ONCE","DOCE","TRECE","CATORCE","QUINCE","DIECIS&EacuteIS","DIECISIETE","DIECIOCHO","DIECINUEVE");
  129. $unidades = array("UN","DOS","TRES","CUATRO","CINCO","SEIS","SIETE","OCHO","NUEVE");
  130. $entexto = "";
  131. if ($hundreds!=0){
  132. $entexto .= $centenas[$hundreds-1];
  133. }
  134. if ($tens>2){
  135. if ($hundreds!=0) $entexto .= " ";
  136. $entexto .= $decenas[$tens-1];
  137. if ($units!=0){
  138. $entexto .= " Y ";
  139. $entexto .= $unidades[$units-1];
  140. }
  141. return $entexto;
  142. }
  143. elseif ($tens==2){
  144. if ($hundreds!=0) $entexto .= " ";
  145. $entexto .= " ".$veintis[$units];
  146. return $entexto;
  147. }
  148. elseif ($tens==1){
  149. if ($hundreds!=0) $entexto .= " ";
  150. $entexto .= $diecis[$units];
  151. return $entexto;
  152. }
  153. if ($units!=0) {
  154. if ($hundreds!=0 || $tens!=0) $entexto .= " ";
  155. $entexto .= $unidades[$units-1];
  156. }
  157. return $entexto;
  158. }