PHP - Error numero a letras

 
Vista:

Error numero a letras

Publicado por Pompilio Arroyave (3 intervenciones) el 17/08/2017 06:53:35
Hola a todos estoy implementando una clase para convertir numeros a letras en php7

esto es lo que tengo en el archivo donde llamo la clases para convertir:

1
2
3
4
5
6
<?php
      //Convertimos el total en letras
      require_once "Letras.php";
      $V=new EnLetras();
      $con_letra=strtoupper($V->ValorEnLetras($regv->total_venta,'PESOS'));
?>

con esto tendria en la variable $con_letra el resultado de la conversion

en la clase Letras.php tengo lo siguiente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
 
  class EnLetras
  {
    var $Void = "";
    var $SP = " ";
    var $Dot = ".";
    var $Zero = "0";
    var $Neg = "Menos";
 
    function ValorEnLetras($x, $Moneda )
    {
      $s="";
      $Ent="";
      $Frc="";
      $Signo="";
 
      if(floatVal($x) < 0)
      {
        $Signo = $this->Neg . " ";
      }
      else
      {
        $Signo = "";
      }
 
      if(intval(number_format($x,2,'.','') )!=$x) //<- averiguar si tiene decimales
      {
        $s = number_format($x,2,'.','');
      }
      else
      {
        $s = number_format($x,0,'.','');
      }
 
      $Pto = strpos($s, $this->Dot);
 
      if ($Pto === false)
      {
        $Ent = $s;
        $Frc = $this->Void;
      }
      else
      {
        $Ent = substr($s, 0, $Pto );
        $Frc =  substr($s, $Pto+1);
      }
 
      if($Ent == $this->Zero || $Ent == $this->Void)
        $s = "Cero ";
      elseif( strlen($Ent) > 7)
      {
        $s = $this->SubValLetra(intval( substr($Ent, 0,  strlen($Ent) - 6))) .
               "Millones " . $this->SubValLetra(intval(substr($Ent,-6, 6)));
      }
      else
      {
        $s = $this->SubValLetra(intval($Ent));
      }
 
      if (substr($s,-9, 9) == "Millones " || substr($s,-7, 7) == "Millon ")
      $s = $s . "de ";
      $s = $s . $Moneda;
 
      if($Frc != $this->Void)
      {
         $s = $s . " Con " . $this->SubValLetra(intval($Frc)) . "Centavos";
         //$s = $s . " " . $Frc . "/100";
      }
      return ($Signo . $s . " MCTE");
    }
 
 
    function SubValLetra($numero)
    {
      $Ptr="";
      $n=0;
      $i=0;
      $x ="";
      $Rtn ="";
      $Tem ="";
 
      $x = trim("$numero");
      $n = strlen($x);
 
      $Tem = $this->Void;
      $i = $n;
 
      while( $i > 0)
      {
        $Tem = $this->Parte(intval(substr($x, $n - $i, 1).
                           str_repeat($this->Zero, $i - 1 )));
        If( $Tem != "Cero" )
          $Rtn .= $Tem . $this->SP;
        $i = $i - 1;
      }
      //--------------------- GoSub FiltroMil ------------------------------
      $Rtn=str_replace(" Mil Mil", " Un Mil", $Rtn );
      while(1)
      {
        $Ptr = strpos($Rtn, "Mil ");
        If(!($Ptr===false))
        {
          If(! (strpos($Rtn, "Mil ",$Ptr + 1) === false ))
          {
            $this->ReplaceStringFrom($Rtn, "Mil ", "", $Ptr);
          }
          Else
          {
            break;
          }
        }
        else break;
      }
 
      //--------------------- GoSub FiltroCiento ------------------------------
      $Ptr = -1;
      do
      {
        $Ptr = strpos($Rtn, "Cien ", $Ptr+1);
        if(!($Ptr===false))
        {
          $Tem = substr($Rtn, $Ptr + 5 ,1);
          if( $Tem == "M" || $Tem == $this->Void);
          else
          {
            $this->ReplaceStringFrom($Rtn, "Cien", "Ciento", $Ptr);
          }
        }
      }
 
      while(!($Ptr === false));
 
      //--------------------- FiltroEspeciales ------------------------------
      $Rtn=str_replace("Diez Un", "Once", $Rtn );
      $Rtn=str_replace("Diez Dos", "Doce", $Rtn );
      $Rtn=str_replace("Diez Tres", "Trece", $Rtn );
      $Rtn=str_replace("Diez Cuatro", "Catorce", $Rtn );
      $Rtn=str_replace("Diez Cinco", "Quince", $Rtn );
      $Rtn=str_replace("Diez Seis", "Dieciseis", $Rtn );
      $Rtn=str_replace("Diez Siete", "Diecisiete", $Rtn );
      $Rtn=str_replace("Diez Ocho", "Dieciocho", $Rtn );
      $Rtn=str_replace("Diez Nueve", "Diecinueve", $Rtn );
      $Rtn=str_replace("Veinte Un", "Veintiun", $Rtn );
      $Rtn=str_replace("Veinte Dos", "Veintidos", $Rtn );
      $Rtn=str_replace("Veinte Tres", "Veintitres", $Rtn );
      $Rtn=str_replace("Veinte Cuatro", "Veinticuatro", $Rtn );
      $Rtn=str_replace("Veinte Cinco", "Veinticinco", $Rtn );
      $Rtn=str_replace("Veinte Seis", "Veintiseís", $Rtn );
      $Rtn=str_replace("Veinte Siete", "Veintisiete", $Rtn );
      $Rtn=str_replace("Veinte Ocho", "Veintiocho", $Rtn );
      $Rtn=str_replace("Veinte Nueve", "Veintinueve", $Rtn );
 
      //--------------------- FiltroUn ------------------------------
      If(substr($Rtn,0,1) == "M")
      {
        $Rtn = "Un " . $Rtn;
      }
      //--------------------- Adicionar Y ------------------------------
      for($i=65; $i<=88; $i++)
      {
        If($i != 77)
        {
          $Rtn=str_replace("a " . Chr($i), "* y " . Chr($i), $Rtn);
        }
      }
      $Rtn=str_replace("*", "a" , $Rtn);
      return($Rtn);
    }
 
 
    function ReplaceStringFrom(&$x, $OldWrd, $NewWrd, $Ptr)
    {
      $x = substr($x, 0, $Ptr)  . $NewWrd . substr($x, strlen($OldWrd) + $Ptr);
    }
 
    function Parte($x)
    {
      $Rtn='';
      $t='';
      $i='';
      Do
      {
        switch($x)
        {
          Case 0:  $t = "Cero";break;
          Case 1:  $t = "Un";break;
          Case 2:  $t = "Dos";break;
          Case 3:  $t = "Tres";break;
          Case 4:  $t = "Cuatro";break;
          Case 5:  $t = "Cinco";break;
          Case 6:  $t = "Seis";break;
          Case 7:  $t = "Siete";break;
          Case 8:  $t = "Ocho";break;
          Case 9:  $t = "Nueve";break;
          Case 10: $t = "Diez";break;
          Case 20: $t = "Veinte";break;
          Case 30: $t = "Treinta";break;
          Case 40: $t = "Cuarenta";break;
          Case 50: $t = "Cincuenta";break;
          Case 60: $t = "Sesenta";break;
          Case 70: $t = "Setenta";break;
          Case 80: $t = "Ochenta";break;
          Case 90: $t = "Noventa";break;
          Case 100: $t = "Cien";break;
          Case 200: $t = "Doscientos";break;
          Case 300: $t = "Trescientos";break;
          Case 400: $t = "Cuatrocientos";break;
          Case 500: $t = "Quinientos";break;
          Case 600: $t = "Seiscientos";break;
          Case 700: $t = "Setecientos";break;
          Case 800: $t = "Ochocientos";break;
          Case 900: $t = "Novecientos";break;
          Case 1000: $t = "Mil";break;
          Case 1000000: $t = "Millon";break;
        }
 
        If($t == $this->Void)
        {
          $i = $i ++;
          $x = $x / 1000;
          If($x== 0)
          {
            $i = 0;
          }
        }
        else
        {
          break;
        }
      }
 
      while($i != 0);
      $Rtn = $t;
      Switch($i)
      {
        Case 0: $t = $this->Void;break;
        Case 1: $t = " Mil";break;
        Case 2: $t = " Millones";break;
        Case 3: $t = " Billones";break;
      }
      return($Rtn . $t);
    }
  }
?>

y no logro ponerla totalmente funcional.
realice muchas pruebas y obtuve lo siguiente:

0 a 999 funciona
1.000 a 1.999 funciona pero aparece Un Mil y debe ser Mil
de 2.000 a 999.999 no funciona correctamente
1.000.000 a 1.001.999 funciona
1.002.000 a 9.999.999 no funciona correctamente
10.000.000 a 10.001.999 funciona
10.002.000 a 10.999.999 no funciona correctamente
11.000.000 a 11.001.999 funciona
11.002.000 a 11.999.999 no funciona correctamente
12.000.000 a 12.001.999 funciona
12.002.000 a 12.999.999 no funciona correctamente
13.000.000 a 13.001.999 funciona
13.002.000 a 13.999.999 no funciona correctamente
14.000.000 a 14.001.999 funciona
14.002.000 a 14.999.999 no funciona correctamente
15.000.000 a 11.001.999 funciona
15.002.000 a 15.999.999 no funciona correctamente
16.000.000 a 16.001.999 funciona
16.002.000 a 16.999.999 no funciona correctamente
17.000.000 a 17.001.999 funciona
17.002.000 a 17.999.999 no funciona correctamente
18.000.000 a 18.001.999 funciona
18.002.000 a 18.999.999 no funciona correctamente
19.000.000 a 19.001.999 funciona
19.002.000 a 19.999.999 no funciona correctamente
20.000.000 a 20.001.999 funciona
20.002.000 a 29.999.999 no funciona correctamente
.
.
.

y asi sucesivamente


lo que me permite deducir que el error esta en los numeros del

2000 al 999.999




Alguien me puede hechar una manito para solucionarlo

Mil gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Error numero a letras

Publicado por Pompilio Arroyave (3 intervenciones) el 17/08/2017 09:32:22
Hola he estado mirando con xdebug en sublime y el error esta en

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//--------------------- GoSub FiltroMil ------------------------------
$Rtn=str_replace(" Mil", " Un Mil", $Rtn );
while(1)
{
	$Ptr = strpos($Rtn, "Mil ");
	If(!($Ptr===false))
	{
		If(! (strpos($Rtn, "Mil ",$Ptr + 1) === false ))
		{
		$this->ReplaceStringFrom($Rtn, "Mil ", "", $Ptr);
		}
		Else
		{
		break;
		}
	}
	else break;
}

desde la instrucción

1
If(!($Ptr===false))

siempre pasa a

1
else break;

entonces nunca entra y de ahí el error, alguien me puede colaborar con eso, mil gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Error numero a letras

Publicado por Pompilio Arroyave (3 intervenciones) el 17/08/2017 09:54:15
Disculpa el codigo es

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//--------------------- GoSub FiltroMil ------------------------------
$Rtn=str_replace(" Mil Mil", " Un Mil", $Rtn );
while(1)
{
	$Ptr = strpos($Rtn, "Mil ");
	If(!($Ptr===false))
	{
		If(! (strpos($Rtn, "Mil ",$Ptr + 1) === false ))
		{
		$this->ReplaceStringFrom($Rtn, "Mil ", "", $Ptr);
		}
		Else
		{
		break;
		}
	}
	else break;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar