private void ProcessResult(ref int TotArchivosDescargados, ref DateTime UltimaFechaEmision, String TipoER)
{
//Utilerias utilerias = new global::Utilerias();
DateTime dtFechaEmision;
string flag_Enviar_o_Recibir_CFDI;
bool flag;
bool Recibidos = true;
if (TipoER == "Emitidos")
{
Recibidos = false;
}
try
{
Regex regex = new Regex("'.*?'");
if (!(this._wbSat.Document == null))
{
HtmlElementCollection elementTablas = this._wbSat.Document.GetElementsByTagName("table");
foreach (HtmlElement elementTabla in elementTablas)
{
foreach (HtmlElement elementRenglon in elementTabla.GetElementsByTagName("tr"))
{
HtmlElementCollection elementBtnDescarga = elementRenglon.GetElementsByTagName("img").GetElementsByName("BtnDescarga");
if (elementBtnDescarga.Count == 0)
{
#region XML Cancelados
IEnumerable<HtmlElement> htmlElements = elementRenglon.GetElementsByTagName("span").Cast<HtmlElement>();
IEnumerable<HtmlElement> htmlElements1 = htmlElements.Where<HtmlElement>((HtmlElement span) =>
{
bool innerText = span.InnerText != "";
return innerText;
});
string DatosDelRenglon = htmlElements1.Aggregate<HtmlElement, string>("", (string current, HtmlElement span) =>
{
string str = string.Concat(current, span.InnerText, "|");
return str;
});
char[] chrArray = new char[] { '|' };
// 0 UUID 1 RFC Emisor 2 Nombre Emisor 3 RFC Receptor 4 Nombre Receptor 5 Fecha Emision 6 Fecha Timbrado 7 PAC 8 Total 9 Tipo CFDI 10 Estatus 11 Fecha Cancelacion
string[] aDatosDelRenglon = DatosDelRenglon.Split(chrArray);
try
{
if (aDatosDelRenglon[10] == "Cancelado")
{
String FechaCancelacion = aDatosDelRenglon[11];
String FechaEmision = aDatosDelRenglon[05];
DateTime.TryParse(aDatosDelRenglon[11], out dtFechaEmision);
#region Forma la Estructura del Directorio a Descargar
string[] EstructuraDelDirectorio = new string[] { gRutaDescargaXML.Trim(), gUsuario.ToString().Trim(), null, null, null, null };
string[] strArrays1 = EstructuraDelDirectorio;
flag_Enviar_o_Recibir_CFDI = (Recibidos ? "Received" : "Sended");
strArrays1[2] = flag_Enviar_o_Recibir_CFDI;
int yearEmision = dtFechaEmision.Year;
EstructuraDelDirectorio[3] = yearEmision.ToString("0000");
int MonthEmision = dtFechaEmision.Month;
EstructuraDelDirectorio[4] = MonthEmision.ToString("00");
int DayEmision = dtFechaEmision.Day;
EstructuraDelDirectorio[5] = DayEmision.ToString("00");
string RutaDelDirectorioDescarga = Path.Combine(EstructuraDelDirectorio);
if (!Directory.Exists(RutaDelDirectorioDescarga))
{
Directory.CreateDirectory(RutaDelDirectorioDescarga);
}
#endregion
string ArchivoADescargar = Path.Combine(RutaDelDirectorioDescarga, string.Format("{0}_{1}_{2}.txt", aDatosDelRenglon[1], aDatosDelRenglon[3], aDatosDelRenglon[0] + "_CANCELADO"));
this.DownloadFileCancelado(aDatosDelRenglon, ArchivoADescargar, RutaDelDirectorioDescarga, (Recibidos ? "Recibido" : "Emitido"));
TotArchivosDescargados = TotArchivosDescargados + 1;
UltimaFechaEmision = dtFechaEmision;
// this.lblStatus.Text = "(" + TotArchivosDescargados.ToString() + " | " + dtFechaEmision.ToString("dd/MM/yyyy HH:mm:ss") + ")";
//utilerias.EscribeArchSal(gRutaDescargaXML.Trim() + "LOG.TXT", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " " + "(" + TotArchivosDescargados.ToString() + " | " + dtFechaEmision.ToString("dd/MM/yyyy HH:mm:ss") + ")");
flag = (!this._supplierStructure ? true : !Recibidos);
if (!flag)
{
#region Forma la Estructura del Directorio a Descargar el archivo
EstructuraDelDirectorio = new string[] { gRutaDescargaXML, gUsuario.ToString().Trim(), null, null, null, null, null };
string[] strArrays2 = EstructuraDelDirectorio;
strArrays2[2] = (Recibidos ? "Received" : "Sended");
EstructuraDelDirectorio[3] = aDatosDelRenglon[1];
yearEmision = dtFechaEmision.Year;
EstructuraDelDirectorio[4] = yearEmision.ToString("0000");
yearEmision = dtFechaEmision.Month;
EstructuraDelDirectorio[5] = yearEmision.ToString("00");
yearEmision = dtFechaEmision.Day;
EstructuraDelDirectorio[6] = yearEmision.ToString("00");
RutaDelDirectorioDescarga = Path.Combine(EstructuraDelDirectorio);
if (!Directory.Exists(RutaDelDirectorioDescarga))
{
Directory.CreateDirectory(RutaDelDirectorioDescarga);
}
#endregion
ArchivoADescargar = Path.Combine(RutaDelDirectorioDescarga, string.Format("{0}_{1}_{2}.txt", aDatosDelRenglon[1], aDatosDelRenglon[3], aDatosDelRenglon[0] + "_CANCELADO"));
this.DownloadFileCancelado(aDatosDelRenglon, ArchivoADescargar, RutaDelDirectorioDescarga, (Recibidos ? "Recibido" : "Emitido"));
}
}
}
catch
{
}
#endregion
}
if (elementBtnDescarga.Count == 1)
{
#region XML VIGENTE
string urlFileDownload = string.Format("https://portalcfdi.facturaelectronica.sat.gob.mx/{0}", regex.Match(elementBtnDescarga[0].OuterHtml).Value.Replace("'", ""));
IEnumerable<HtmlElement> htmlElements = elementRenglon.GetElementsByTagName("span").Cast<HtmlElement>();
IEnumerable<HtmlElement> htmlElements1 = htmlElements.Where<HtmlElement>((HtmlElement span) =>
{
bool innerText = span.InnerText != "";
return innerText;
});
string DatosDelRenglon = htmlElements1.Aggregate<HtmlElement, string>("", (string current, HtmlElement span) =>
{
string str = string.Concat(current, span.InnerText, "|");
return str;
});
char[] chrArray = new char[] { '|' };
// 0 UUID 1 RFC Emisor 2 Nombre Emisor 3 RFC Receptor 4 Nombre Receptor 5 Fecha Emision 6 Fecha Timbrado 7 PAC 8 Total 9 Tipo CFDI 10 Estatus
string[] aDatosDelRenglon = DatosDelRenglon.Split(chrArray);
DateTime.TryParse(aDatosDelRenglon[5], out dtFechaEmision);
#region Forma la Estructura del Directorio a Descargar
string[] EstructuraDelDirectorio = new string[] { gRutaDescargaXML.Trim(), gUsuario.ToString().Trim(), null, null, null, null };
string[] strArrays1 = EstructuraDelDirectorio;
flag_Enviar_o_Recibir_CFDI = (Recibidos ? "Received" : "Sended");
strArrays1[2] = flag_Enviar_o_Recibir_CFDI;
int yearEmision = dtFechaEmision.Year;
EstructuraDelDirectorio[3] = yearEmision.ToString("0000");
int MonthEmision = dtFechaEmision.Month;
EstructuraDelDirectorio[4] = MonthEmision.ToString("00");
int DayEmision = dtFechaEmision.Day;
EstructuraDelDirectorio[5] = DayEmision.ToString("00");
string RutaDelDirectorioDescarga = Path.Combine(EstructuraDelDirectorio);
if (!Directory.Exists(RutaDelDirectorioDescarga))
{
Directory.CreateDirectory(RutaDelDirectorioDescarga);
}
#endregion
string ArchivoADescargar = Path.Combine(RutaDelDirectorioDescarga, string.Format("{0}_{1}_{2}.xml", aDatosDelRenglon[1], aDatosDelRenglon[3], aDatosDelRenglon[0]));
this.DownloadFile(urlFileDownload, ArchivoADescargar, RutaDelDirectorioDescarga, aDatosDelRenglon, (Recibidos ? "Recibido" : "Emitido"));
TotArchivosDescargados = TotArchivosDescargados + 1;
UltimaFechaEmision = dtFechaEmision;
//this.lblStatus.Text = "(" + TotArchivosDescargados.ToString() + " | " + dtFechaEmision.ToString("dd/MM/yyyy HH:mm:ss") + ")";
//utilerias.EscribeArchSal(gRutaDescargaXML.Trim() + "LOG.TXT", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " " + "(" + TotArchivosDescargados.ToString() + " | " + dtFechaEmision.ToString("dd/MM/yyyy HH:mm:ss") + ")");
flag = (!this._supplierStructure ? true : !Recibidos);
if (!flag)
{
#region Forma la Estructura del Directorio a Descargar el archivo
EstructuraDelDirectorio = new string[] { gRutaDescargaXML, gUsuario.ToString().Trim(), null, null, null, null, null };
string[] strArrays2 = EstructuraDelDirectorio;
strArrays2[2] = (Recibidos ? "Received" : "Sended");
EstructuraDelDirectorio[3] = aDatosDelRenglon[1];
yearEmision = dtFechaEmision.Year;
EstructuraDelDirectorio[4] = yearEmision.ToString("0000");
yearEmision = dtFechaEmision.Month;
EstructuraDelDirectorio[5] = yearEmision.ToString("00");
yearEmision = dtFechaEmision.Day;
EstructuraDelDirectorio[6] = yearEmision.ToString("00");
RutaDelDirectorioDescarga = Path.Combine(EstructuraDelDirectorio);
if (!Directory.Exists(RutaDelDirectorioDescarga))
{
Directory.CreateDirectory(RutaDelDirectorioDescarga);
}
#endregion
ArchivoADescargar = Path.Combine(RutaDelDirectorioDescarga, string.Format("{0}_{1}_{2}.xml", aDatosDelRenglon[1], aDatosDelRenglon[3], aDatosDelRenglon[0]));
this.DownloadFile(urlFileDownload, ArchivoADescargar, RutaDelDirectorioDescarga, aDatosDelRenglon, (Recibidos ? "Recibido" : "Emitido"));
}
#endregion
}
if (TotArchivosDescargados >= MaximoDeDescargasSAT)
return;
}
}
}
else
{
return;
}
}
catch (Exception exception)
{
this.Message("ERROR AL ESTAR PROCESANDO LA TABLA DE LOS URL DE DESCARGAS: " + exception.Message);
throw new Exception(exception.Message);
}
}