-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptComponent.cs
More file actions
223 lines (168 loc) · 9.16 KB
/
ScriptComponent.cs
File metadata and controls
223 lines (168 loc) · 9.16 KB
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
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Text.RegularExpressions; // Se importa esto para poder usar el método Regex.Split()
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
// >>> Escrito por Ernesto Moscoso Cam
// >>> Abril 2017
// >>>
// >>> Basado en el código C# hecho por Afu Nang Tse Mundaca: http://r3xet.blogspot.pe/2013/12/obtener-el-tipo-de-cambio-de-sunat-del.html
// Este es un helper method porque DateSerial() no existe en C#
private DateTime DateSerial(int intYear, int intMonth, int intDay)
{
return new DateTime(intYear, intMonth, intDay);
}
// Este es el método que devuelve las filas del Script Component
public override void CreateNewOutputRows()
{
System.Data.DataTable objDataTable; // Tabla auxiliar para guardar los datos de la Web
// Primero se configura la tabla para que tenga las 3 columnas necesarias...
objDataTable = new System.Data.DataTable();
objDataTable.Columns.Add("Fecha", typeof(System.DateTime));
objDataTable.Columns.Add("Compra", typeof(double));
objDataTable.Columns.Add("Venta", typeof(double));
// Se manda poblar la tabla...
ObtenerDatos(objDataTable);
// Si toda ha salido bien y objDataTable tiene la data, entonces se copian los datos al buffer...
foreach (System.Data.DataRow objDataRow in objDataTable.Rows)
{
SUNATTCBuffer.AddRow();
SUNATTCBuffer.Fecha = Convert.ToDateTime(objDataRow[0]);
SUNATTCBuffer.Compra = Convert.ToDecimal(objDataRow[1]);
SUNATTCBuffer.Venta = Convert.ToDecimal(objDataRow[2]);
}
}
private void ObtenerDatos(System.Data.DataTable objDataTable)
{
System.Net.WebClient objWebClient;
string strHTML = "";
string strURL = null;
int intAño;
int intMes;
System.DateTime dtDateAux = default(System.DateTime);
objWebClient = new System.Net.WebClient();
dtDateAux = DateSerial(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
// Este es el último día del mes anterior
// Primero: Obtener el TC del mes anterior
intAño = dtDateAux.Year;
intMes = dtDateAux.Month;
// La página del TC de la SUNAT acepta un Query String donde se le indica el año y el mes
strURL = "http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias?mes=" + intMes + "&anho=" + intAño;
// Si la función retorna falso, entonces no se procesa
if (CargarDocHTML(objWebClient, strURL, ref strHTML))
{
ProcesarDocHTML(strHTML, intAño, intMes, objDataTable);
}
// Segundo: Obtener el TC del mes en curso
intAño = DateTime.Now.Year;
intMes = DateTime.Now.Month;
strURL = "http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias?mes=" + intMes + "&anho=" + intAño;
// Si la función retorna falso, entonces no se procesa
if (CargarDocHTML(objWebClient, strURL, ref strHTML))
{
ProcesarDocHTML(strHTML, intAño, intMes, objDataTable);
}
objWebClient.Dispose();
objDataTable.AcceptChanges(); // Commit
}
// Esta rutina sólo se encarga de conectarse a la web y obtener el HTML
private bool CargarDocHTML(System.Net.WebClient objWebClient, string strURL, ref string strHTML)
{
bool bAux = false;
try
{
strHTML = objWebClient.DownloadString(strURL);
// Esto podría fallar (debido a problemas con la red, o que el servidor esté fuera de linea, etc)
bAux = !(strHTML.IndexOf("No existe Informaci") > 0);
// Esta función retorna False si no hay datos
}
catch (Exception ex)
{
// Si hay una excepción no se hace nada
}
return bAux;
}
// Esta rutina "procesa" el HTML como String
// Lo que hace es recorrer el código HTML de la página y obtener de allí subcadenas...
private void ProcesarDocHTML(string strHTML, int intAño, int intMes, System.Data.DataTable objDataTable)
{
string strTable;
string[] strFilas;
string[] strCeldas;
int intFirstPosition;
int intLastPosition;
int i;
int j;
System.DateTime dtFecha = DateTime.Now; // En C# se debe inicializar la variable para poder pasarle por Referencia
double dblCompra = 0.0;
double dblVenta = 0.0;
intFirstPosition = strHTML.IndexOf("form-table"); // Primero se obtiene la posición de la cadena "form-table"
intFirstPosition = strHTML.IndexOf("<tr>", intFirstPosition) - 1; // Una vez que el elemento table se ha localizado, se avanza el puntero hasta la primera fila
// En una tabla HTML, las filas se identifican con <tr>
intLastPosition = strHTML.IndexOf("</table>", intFirstPosition) - 7; // Se obtiene la posición del elemento de cierre "</table>"
strTable = strHTML.Substring(intFirstPosition, intLastPosition - intFirstPosition); // Se extrae una subcadena que contiene todas las filas de la tabla
strFilas = Regex.Split(strTable, "</tr>"); // Se usa Split() para obtener un arreglo de filas. Ojo que la primera fila contiene las cabeceras de la tabla
// El bucle empieza desde 1 en vez de 0 para saltearse la fila de cabeceras
for (i = 1; i <= strFilas.GetUpperBound(0); i++)
{
strCeldas = Regex.Split(strFilas[i].Trim(), "/td>"); // Se usa Split() para obtener un arrego de celdas
// Al dejar el caracter '<' se puede delimitar luego el contenido de la celda
// strCeldas tiene TODAS las celdas de una fila, pero se necesitan leer las celdas 3 a la vez...
//Las celdas siempre aparecen en grupos de 3: ("Dia", "Compra", "Venta")
for (j = 0; j <= strCeldas.GetUpperBound(0); j += 3)
{
// La función RegEx.Split() a veces deja una cadena nula al final del arreglo. Por eso se itera sólo donde hay datos
if (!string.IsNullOrEmpty(strCeldas[j]))
{
ObtenerTriplete(strCeldas, j, intAño, intMes, ref dtFecha, ref dblCompra, ref dblVenta); // Un triplete se define como (dtFecha, dblCompra, dblVenta)
AddRow(objDataTable, dtFecha, dblCompra, dblVenta);
}
}
}
}
// Valores posibles para intIndice: 0, 3, 6, 9
// Un triplete es un grupo de 3 celdas, una para la Fecha, otra para el valor de Compra y finalmente otra para el valor de Venta
private void ObtenerTriplete(string[] strCeldas, int intIndice, int intAño, int intMes, ref System.DateTime dtFecha, ref double dblCompra, ref double dblVenta)
{
int intDia = 0;
intDia = Convert.ToInt32(ObtenerValorCelda(strCeldas[intIndice]));
dtFecha = DateSerial(intAño, intMes, intDia);
dblCompra = Convert.ToDouble(ObtenerValorCelda(strCeldas[intIndice + 1]));
dblVenta = Convert.ToDouble(ObtenerValorCelda(strCeldas[intIndice + 2]));
}
// Esta función extrae el Valor del elemento <td>
private string ObtenerValorCelda(string strCelda)
{
string strValorCelda;
int intFirstPosition;
int intLastPosition;
intFirstPosition = strCelda.IndexOf("<strong>"); // El elemento "<strong>" sólo se encuentra en la primera columna del triplete ("Dia")
if (intFirstPosition >= 0) // Los indices de IndexOf() empiezan en 0
{
intLastPosition = strCelda.IndexOf("</strong>");
intFirstPosition = intFirstPosition + 8;
}
else // Si el elemento "<strong>" no se ha encontrado, entonces estamos en la columna "Compra" o "Venta"
{
intFirstPosition = strCelda.IndexOf("tne10"); // la cadena 'class = "tne10"' sólo se encuentra en las columnas "Compra" o "Venta"
intFirstPosition = intFirstPosition + 8;
intLastPosition = strCelda.IndexOf("<", intFirstPosition); // El caracter '<' fue dejado intencionalmente cuando se usó RegEx.Split() para poderse usarsele como marcador aquí
}
strValorCelda = strCelda.Substring(intFirstPosition, intLastPosition - intFirstPosition).Trim();
// Después de obtenerse las posiciones, se extrae la subcadena del valor
return strValorCelda;
}
// Agrega una fila en objDataTable
private void AddRow(System.Data.DataTable objDataTable, System.DateTime dtFecha, double dblCompra, double dblVenta)
{
System.Data.DataRow objDataRow;
objDataRow = objDataTable.NewRow();
objDataRow[0] = dtFecha;
objDataRow[1] = dblCompra;
objDataRow[2] = dblVenta;
objDataTable.Rows.Add(objDataRow);
}
}