-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
166 lines (154 loc) · 5.35 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsultaAutorizacionSRI
{
public partial class Form1 : Form
{
public List<ClaveAcceso> listaClavesAcceso = new List<ClaveAcceso>();
public List<DatosTributarios> listaResultados = new List<DatosTributarios>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dgvIn.DataSource = ClaveAcceso.GetData();
}
private void btnConsultar_Click(object sender, EventArgs e)
{
gvAutorizados.DataSource = null;
gvNoAutorizados.DataSource = null;
listaResultados = new List<DatosTributarios>();
List<ClaveAcceso> lista = new List<ClaveAcceso>();
lista= (List<ClaveAcceso>)dgvIn.DataSource;
barra.Minimum = 0;
barra.Value = 0;
barra.Maximum = lista.Count;
foreach (ClaveAcceso claveAcceso in lista)
{
if (claveAcceso.Numero.Length == 49)
{
listaResultados.Add(LlamarSri(claveAcceso.Numero));
}
barra.Value ++;
lblOut.Text = barra.Value.ToString();
}
int Autorizados = listaResultados.Where(x => x.Estado.Equals("AUTORIZADO")).Count();
lblOut.Text = Autorizados.ToString() +" Autorizados";
gvAutorizados.DataSource = listaResultados.Where(x=>x.Estado.Equals("AUTORIZADO")).ToList();
gvNoAutorizados.DataSource = listaResultados.Where(x => !x.Estado.Equals("AUTORIZADO")).ToList();
}
#region Metodos
private DatosTributarios LlamarSri(string claveAcceso)
{
var resultado = string.Empty;
DatosTributarios datosTributarios = null;
string url = "https://cel.sri.gob.ec/comprobantes-electronicos-ws/AutorizacionComprobantesOffline?wsdl";
string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ec=\"http://ec.gob.sri.ws.autorizacion\">";
xml = xml + "<soapenv:Header/>";
xml = xml + "<soapenv:Body>";
xml = xml + "<ec:autorizacionComprobante>";
xml = xml + "<claveAccesoComprobante>" + claveAcceso + "</claveAccesoComprobante>";
xml = xml + "</ec:autorizacionComprobante>";
xml = xml + "</soapenv:Body>";
xml = xml + "</soapenv:Envelope>";
try
{
byte[] bytes = Encoding.ASCII.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
resultado = reader.ReadToEnd();
}
resultado = WebUtility.HtmlDecode(resultado);
response.Close();
if (resultado.Contains("<numeroComprobantes>0</numeroComprobantes>"))
{
datosTributarios = new DatosTributarios
{
claveAcceso = claveAcceso,
numeroAutorizacion = "",
FechaAutorizacion = DateTime.Now,
Estado = "NO RECIBIDO",
};
}
else
{
var caracterPrincipal = resultado.IndexOf('?') - 1;
var caracterSecundario = resultado.LastIndexOf('?') + 2;
resultado = resultado.Remove(caracterPrincipal, (caracterSecundario - caracterPrincipal));
resultado = "<?xml version=" + "\"1.0\"" + " encoding=" + "\"UTF-8\"" + "?>" + resultado;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(resultado);
System.Xml.XmlNode element = doc.SelectSingleNode("numeroAutorizacion");
datosTributarios = new DatosTributarios
{
claveAcceso = doc.GetElementsByTagName("claveAcceso")[0].InnerText,
numeroAutorizacion = doc.GetElementsByTagName("numeroAutorizacion")[0].InnerText,
FechaAutorizacion = Convert.ToDateTime(doc.GetElementsByTagName("fechaAutorizacion")[0].InnerText),
Estado = doc.GetElementsByTagName("estado")[0].InnerText,
};
}
}
catch //(Exception ex)
{
if (datosTributarios == null)
{
datosTributarios = new DatosTributarios();
datosTributarios.claveAcceso = claveAcceso;
datosTributarios.Estado = "Error";
datosTributarios.FechaAutorizacion = new DateTime(1990, 1, 1);
datosTributarios.numeroAutorizacion = claveAcceso;
}
}
return datosTributarios;
}
private void Copiar()
{
listaClavesAcceso = new List<ClaveAcceso>();
string formato = DataFormats.Text;
object contenido = Clipboard.GetData(formato);
string con = (String)contenido;
if (contenido != null)
{
string lista = con.Replace("\r\n", ",");
string[] data = lista.Split(',');
foreach (string a in data)
{
if (a.ToString().Length == 49)
{
listaClavesAcceso.Add(new ClaveAcceso { Numero = a.ToString() });
}
}
dgvIn.DataSource = null;
dgvIn.DataSource = listaClavesAcceso;
lblIn.Text = listaClavesAcceso.Count.ToString()+ " Claves de Accesos";
}
else MessageBox.Show("Numero de columnas a copiar del Excel y la Grilla deben ser iguales");
}
#endregion
private void btnPegar_Click(object sender, EventArgs e)
{
this.Copiar();
}
}
}