using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DescargaMasiva
{
    public partial class frmDownloadCfdi : Form
    {
        private string _currentUrl;

        private  bool _supplierStructure;

        private  WebBrowser _wbSat;

        //private IContainer components = null;

        public frmDownloadCfdi()
        {
            InitializeComponent();
        }

        private void frmDownloadCfdi_Load(object sender, EventArgs e)
        {
            this._wbSat = new WebBrowser();
            this._wbSat.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.wbSAT_DocumentCompleted);
            this._supplierStructure = true;//bool.Parse(ConfigurationManager.AppSettings["SupplierStructure"]);
            this.lblDownloadPath.Text = @"c:\cfdi\"; //ConfigurationManager.AppSettings["DownloadPath"];
            this.InitControls();
        }

        private void txtPassword_Enter(object sender, EventArgs e)
        {
            if (this.txtPassword.Text.Trim() != "")
            {
                this.lblStatus.Text = "";
                this.txtPassword.SelectAll();
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Internet.IsConnectedToInternet())
                {
                    this.Message("Se requiere conexión a Internet para utilizar esta aplicación.");
                    return;
                }
                else if (!(this.txtPassword.Text.Trim() == ""))
                {
                    this.Authenticate();
                }
                else
                {
                    this.txtPassword.Focus();
                    this.Message("Debe de proporcionar la contraseña CIEC.");
                    return;
                }
            }
            catch (Exception exception)
            {
                this.Message(exception.Message);
            }
        }

        private void Authenticate()
        {
            try
            {
                string strUri =
                    string.Format(
                        "https://cfdiau.sat.gob.mx/nidp/app/login?id=SATUPCFDiCon&sid=0&option=credential&sid=0&Ecom_User_ID={0}&Ecom_Password={1}",
                        this.cmbAccount.Text.Trim().ToUpper(), this.txtPassword.Text.Trim());
                Uri uri = new Uri(strUri);
                this.Message(string.Format("Iniciando sesion del usuario {0}.", this.cmbAccount.Text.Trim().ToUpper()));
                this._wbSat.Navigate(uri);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void DownloadFile(string address, string fileName)
        {
            try
            {
                string cookie = CookieReader.GetCookie(this._wbSat.Url.ToString());
                WebClient webClient = new WebClient();
                if (File.Exists(fileName))
                {
                    this.Message(string.Format("El archivo {0} ya existe.\n", fileName));
                }
                else
                {
                    webClient.Headers.Add(HttpRequestHeader.Cookie, cookie);
                    webClient.DownloadFile(address, fileName);
                    this.Message(string.Format("Creando el archivo {0}.\n", fileName));
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private static IEnumerable<HtmlElement> ElementsByClass(HtmlDocument doc, string className)
        {
            IEnumerable<HtmlElement> htmlElements = doc.All.Cast<HtmlElement>().Where<HtmlElement>((HtmlElement e) =>
            {
                bool attribute = e.GetAttribute("className") == className;
                return attribute;
            });
            return htmlElements;
        }

        private void Message(string message)
        {
            try
            {
                this.lblStatus.Text = message;
                this.txtLog.AppendText(string.Concat(message, "\n"));
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void InitControls()
		{
			try
			{
                this.FillCombos();
                //this.lblStatus.Text = "";
                //this.cmbAccount.Items.Clear();
				//foreach (string keyAccount in (new FrmAddAccount()).GetKeyAccounts())
				//{
			    //this.cmbAccount.Items.Add("SARM8209281F1");//keyAccount);
				//}
                //if (this.cmbAccount.Items.Count > 0)
                //{
                //    this.cmbAccount.SelectedIndex = 0;
                //}
			}
			catch (Exception exception)
			{
				throw new Exception(exception.Message);
			}
		}

        private void ProcessResult()
        {
            DateTime dateTime;
            string str1;
            bool flag;
            string str2;
            try
            {
                Regex regex = new Regex("'.*?'");
                if (!(this._wbSat.Document == null))
                {
                    HtmlElementCollection elementsByTagName = this._wbSat.Document.GetElementsByTagName("table");
                    foreach (HtmlElement htmlElement in elementsByTagName)
                    {
                        foreach (HtmlElement elementsByTagName1 in htmlElement.GetElementsByTagName("tr"))
                        {
                            HtmlElementCollection elementsByName = elementsByTagName1.GetElementsByTagName("img").GetElementsByName("BtnDescarga");
                            if (elementsByName.Count == 1)
                            {
                                string str3 = string.Format("https://portalcfdi.facturaelectronica.sat.gob.mx/{0}", regex.Match(elementsByName[0].OuterHtml).Value.Replace("'", ""));
                                IEnumerable<HtmlElement> htmlElements = elementsByTagName1.GetElementsByTagName("span").Cast<HtmlElement>();
                                IEnumerable<HtmlElement> htmlElements1 = htmlElements.Where<HtmlElement>((HtmlElement span) =>
                                {
                                    bool innerText = span.InnerText != "";
                                    return innerText;
                                });
                                string str4 = htmlElements1.Aggregate<HtmlElement, string>("", (string current, HtmlElement span) =>
                                {
                                    string str = string.Concat(current, span.InnerText, "|");
                                    return str;
                                });
                                char[] chrArray = new char[] { '|' };
                                string[] strArrays = str4.Split(chrArray);
                                DateTime.TryParse(strArrays[5], out dateTime);
                                string[] text = new string[] { this.lblDownloadPath.Text, this.cmbAccount.Text.ToString(), null, null, null, null };
                                string[] strArrays1 = text;
                                str1 = (this.rbtReceived.Checked ? "Received" : "Sended");
                                strArrays1[2] = str1;
                                int year = dateTime.Year;
                                text[3] = year.ToString("0000");
                                year = dateTime.Month;
                                text[4] = year.ToString("00");
                                year = dateTime.Day;
                                text[5] = year.ToString("00");
                                string str5 = Path.Combine(text);
                                if (!Directory.Exists(str5))
                                {
                                    Directory.CreateDirectory(str5);
                                }
                                string str6 = Path.Combine(str5, string.Format("{0}_{1}_{2}.xml", strArrays[1], strArrays[3], strArrays[0]));
                                this.DownloadFile(str3, str6);
                                flag = (!this._supplierStructure ? true : !this.rbtReceived.Checked);
                                if (!flag)
                                {
                                    text = new string[] { this.lblDownloadPath.Text, this.cmbAccount.Text.ToString(), null, null, null, null, null };
                                    string[] strArrays2 = text;
                                    str2 = (this.rbtReceived.Checked ? "Received" : "Sended");
                                    strArrays2[2] = str2;
                                    text[3] = strArrays[1];
                                    year = dateTime.Year;
                                    text[4] = year.ToString("0000");
                                    year = dateTime.Month;
                                    text[5] = year.ToString("00");
                                    year = dateTime.Day;
                                    text[6] = year.ToString("00");
                                    str5 = Path.Combine(text);
                                    if (!Directory.Exists(str5))
                                    {
                                        Directory.CreateDirectory(str5);
                                    }
                                    str6 = Path.Combine(str5, string.Format("{0}_{1}_{2}.xml", strArrays[1], strArrays[3], strArrays[0]));
                                    this.DownloadFile(str3, str6);
                                }
                            }
                        }
                    }
                }
                else
                {
                    return;
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void wbSAT_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            int anio;
            int num;
            int selectedItem1;
            int i;
            int j;
            bool flag;
            bool flag1;
            if (this._wbSat.Document != null)
            {
                if (this._wbSat.Url == new Uri("https://cfdiau.sat.gob.mx/nidp/app?sid=0"))
                {
                    this.Message(string.Format("Usuario {0} Autentificado correctamente.", this.cmbAccount.SelectedItem));
                    if (this.rbtReceived.Checked)
                    {
                        this._wbSat.Navigate("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaReceptor.aspx");
                    }
                    if (this.rbtSended.Checked)
                    {
                        this._wbSat.Navigate("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaEmisor.aspx");
                    }
                }
                if (this._wbSat.Url == new Uri("https://cfdiau.sat.gob.mx/nidp/app/login?sid=0&sid=0"))
                {
                    this.txtPassword.Focus();
                    this.txtPassword.SelectionStart = 0;
                    this.txtPassword.SelectionLength = this.txtPassword.Text.Length;
                    this.Message("Clave CIEC incorrecta, favor de verificar RFC y Clave.");
                }

                flag = (this._wbSat.Url != new Uri("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaReceptor.aspx") ? true : !(this._wbSat.Url != new Uri(this._currentUrl)));
                if (!flag)
                {
                    anio = (int)this.cmbYear.SelectedItem;

                    if (!this.rbtDay.Checked) //Verifica si la descarga NO es diaria
                    {
                        int MesIni = int.Parse(this.MesIni_txt.SelectedItem.ToString());
                        int MesFin = int.Parse(this.MesFin_txt.SelectedItem.ToString());

                        for (i = MesIni; i <= MesFin; i++)
                        {
                            this.Message(string.Format("Procesando documentos recibidos del ejercicio {0}, periodo {1} ...", anio, i));
                            this.GetReceivedDocuments(anio, i);
                        }
                    }
                    else {

                        int DiaIni = int.Parse(this.DiaIni_txt.SelectedItem.ToString());
                        int DiaFin = int.Parse(this.DiaFin_txt.SelectedItem.ToString());
                        int Mes = int.Parse(this.mes_txt.SelectedItem.ToString());

                        for (i = DiaIni; i <= DiaFin; i++)
                        {
                            this.Message(string.Format("Procesando documentos recibidos de la fecha  {0}/{1}/{2} ...", i.ToString("00"), Mes.ToString("00"), anio));
                            this.GetReceivedDocuments(anio, Mes, i);
                        }
                    }

                    this.FinishSession();
                }

                flag1 = (this._wbSat.Url != new Uri("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaEmisor.aspx") ? true : !(this._wbSat.Url != new Uri(this.lblStatus.Text)));
                if (!flag1)
                {
                    anio = (int)this.cmbYear.SelectedItem;

                    if (!this.rbtDay.Checked) //Verifica si la descarga NO es diaria
                    {
                        int MesIni = int.Parse(this.MesIni_txt.SelectedItem.ToString());
                        int MesFin = int.Parse(this.MesFin_txt.SelectedItem.ToString());

                        for (i = MesIni; i <= MesFin; i++)
                        {
                            this.Message(string.Format("Procesando documentos emitidos del ejercicio {0}, periodo {1} ...", anio, i));
                            this.GetSendedDocuments(anio, i);
                        }
                    }
                    else
                    {
                        int DiaIni = int.Parse(this.DiaIni_txt.SelectedItem.ToString());
                        int DiaFin = int.Parse(this.DiaFin_txt.SelectedItem.ToString());
                        int Mes = int.Parse(this.mes_txt.SelectedItem.ToString());

                        for (i = DiaIni; i <= DiaFin; i++)
                        {
                            this.Message(string.Format("Procesando documentos emitidos de la fecha  {0}/{1}/{2} ...", i.ToString("00"), Mes.ToString("00"), anio));
                            this.GetSendedDocuments(anio, Mes, i);
                        }
                    }
                    this.FinishSession();
                }
                while (true)
                {
                    Application.DoEvents();
                    if (!this._wbSat.IsBusy)
                    {
                        break;
                    }
                }
                this._currentUrl = this._wbSat.Url.ToString();
            }
        }

        private void FinishSession()
        {
            try
            {
                this.Message(string.Format("Cerrar sesion usuario {0}.", this.cmbAccount.SelectedItem));
                this._wbSat.Navigate(new Uri("https://portalcfdi.facturaelectronica.sat.gob.mx/logout.aspx?salir=y"));
                this.Message("Proceso terminado.");
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void GetReceivedDocuments(int year, int month, int day)
        {
            bool flag;
            bool flag1;
            bool flag2;
            try
            {
                if (this._wbSat.Document != null)
                {
                    HtmlElement elementById = this._wbSat.Document.GetElementById("ctl00_MainContent_RdoFechas");
                    if (elementById != null)
                    {
                        elementById.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement htmlElement = this._wbSat.Document.GetElementById("DdlAnio");
                        flag1 = (htmlElement == null ? true : !(htmlElement.GetAttribute("disabled") != "disabled"));
                        if (!flag1)
                        {
                            break;
                        }
                    }
                    HtmlElement elementById1 = this._wbSat.Document.GetElementById("DdlAnio");
                    if (elementById1 != null)
                    {
                        elementById1.SetAttribute("value", year.ToString(CultureInfo.InvariantCulture));
                    }
                    HtmlElement htmlElement1 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFecha_DdlMes");
                    if (htmlElement1 != null)
                    {
                        htmlElement1.SetAttribute("value", month.ToString("0"));
                    }
                    HtmlElement elementById2 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFecha_DdlDia");
                    if (elementById2 != null)
                    {
                        elementById2.SetAttribute("value", day.ToString("00"));
                    }
                    HtmlElement htmlElement2 = this._wbSat.Document.GetElementById("ctl00_MainContent_BtnBusqueda");
                    if (htmlElement2 != null)
                    {
                        htmlElement2.InvokeMember("click");
                    }
                    int num = 0;
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement elementById3 = this._wbSat.Document.GetElementById("ctl00_MainContent_UpdateProgress1");
                        if (!(elementById3 == null))
                        {
                            flag2 = (elementById3.Style != "display: none;" ? true : num <= 100000);
                            if (!flag2)
                            {
                                break;
                            }
                            num++;
                        }
                    }
                    this.ProcessResult();
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void GetReceivedDocuments(int year, int month)
        {
            bool flag;
            bool flag1;
            bool flag2;
            try
            {
                if (this._wbSat.Document != null)
                {
                    HtmlElement elementById = this._wbSat.Document.GetElementById("ctl00_MainContent_RdoFechas");
                    if (elementById != null)
                    {
                        elementById.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement htmlElement = this._wbSat.Document.GetElementById("DdlAnio");
                        flag1 = (htmlElement == null ? true : !(htmlElement.GetAttribute("disabled") != "disabled"));
                        if (!flag1)
                        {
                            break;
                        }
                    }
                    HtmlElement elementById1 = this._wbSat.Document.GetElementById("DdlAnio");
                    if (elementById1 != null)
                    {
                        elementById1.SetAttribute("value", year.ToString(CultureInfo.InvariantCulture));
                    }
                    HtmlElement htmlElement1 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFecha_DdlMes");
                    if (htmlElement1 != null)
                    {
                        htmlElement1.SetAttribute("value", month.ToString("0"));
                    }
                    HtmlElement elementById2 = this._wbSat.Document.GetElementById("ctl00_MainContent_BtnBusqueda");
                    if (elementById2 != null)
                    {
                        elementById2.InvokeMember("click");
                    }
                    int num = 0;
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement htmlElement2 = this._wbSat.Document.GetElementById("ctl00_MainContent_UpdateProgress1");
                        if (!(htmlElement2 == null))
                        {
                            flag2 = (htmlElement2.Style != "display: none;" ? true : num <= 100000);
                            if (!flag2)
                            {
                                break;
                            }
                            num++;
                        }
                    }
                    if (this._wbSat.Document.GetElementById("ctl00_MainContent_PnlLimiteRegistros") != null)
                    {
                        MessageBox.Show("La consulta realizada solo muestra los primeros 500 registros, se recomienda hacer la descarga por día", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    this.ProcessResult();
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void GetSendedDocuments(int year, int month, int day)
        {
            HtmlElement elementById;
            bool flag;
            bool flag1;
            bool flag2;
            bool flag3;
            bool flag4;
            try
            {
                if (this._wbSat.Document != null)
                {
                    HtmlElement htmlElement = this._wbSat.Document.GetElementById("ctl00_MainContent_RdoFechas");
                    if (htmlElement != null)
                    {
                        htmlElement.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement elementById1 = this._wbSat.Document.GetElementById("ctl00_MainContent_TxtRfcReceptor");
                        flag1 = (elementById1 == null ? true : !(elementById1.GetAttribute("disabled") != "disabled"));
                        if (!flag1)
                        {
                            break;
                        }
                    }
                    DateTime dateTime = new DateTime(year, month, day);
                    HtmlElement htmlElement1 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFechaInicial2_Calendario_text");
                    if (htmlElement1 != null)
                    {
                        htmlElement1.SetAttribute("value", dateTime.ToString("dd/MM/yyyy"));
                    }
                    HtmlElement elementById2 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFechaInicial2_BtnFecha2");
                    if (elementById2 != null)
                    {
                        elementById2.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        elementById = this._wbSat.Document.GetElementById("datepicker");
                        flag2 = (elementById == null ? true : !elementById.Style.Contains("visibility: visible"));
                        if (!flag2)
                        {
                            break;
                        }
                    }
                    IEnumerable<HtmlElement> htmlElements = frmDownloadCfdi.ElementsByClass(this._wbSat.Document, "dpDayHighlightTD");
                    if (htmlElements != null)
                    {
                        htmlElements.First<HtmlElement>().InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        elementById = this._wbSat.Document.GetElementById("datepicker");
                        flag3 = (elementById == null ? true : elementById.Style.Contains("visibility: visible"));
                        if (!flag3)
                        {
                            break;
                        }
                    }
                    HtmlElement htmlElement2 = this._wbSat.Document.GetElementById("ctl00_MainContent_BtnBusqueda");
                    if (htmlElement2 != null)
                    {
                        htmlElement2.InvokeMember("click");
                    }
                    int num = 0;
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement elementById3 = this._wbSat.Document.GetElementById("ctl00_MainContent_UpdateProgress1");
                        if (!(elementById3 == null))
                        {
                            flag4 = (elementById3.Style != "display: none;" ? true : num <= 100000);
                            if (!flag4)
                            {
                                break;
                            }
                            num++;
                        }
                    }
                    this.ProcessResult();
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void GetSendedDocuments(int year, int month)
        {
            HtmlElement elementById;
            bool flag;
            DateTime dateTime;
            bool flag1;
            bool flag2;
            bool flag3;
            bool flag4;
            bool flag5;
            bool flag6;
            try
            {
                if (this._wbSat.Document != null)
                {
                    HtmlElement htmlElement = this._wbSat.Document.GetElementById("ctl00_MainContent_RdoFechas");
                    if (htmlElement != null)
                    {
                        htmlElement.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement elementById1 = this._wbSat.Document.GetElementById("ctl00_MainContent_TxtRfcReceptor");
                        flag1 = (elementById1 == null ? true : !(elementById1.GetAttribute("disabled") != "disabled"));
                        if (!flag1)
                        {
                            break;
                        }
                    }
                    HtmlElement htmlElement1 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFechaInicial2_Calendario_text");
                    if (htmlElement1 != null)
                    {
                        dateTime = new DateTime(year, month, 1);
                        htmlElement1.SetAttribute("value", dateTime.ToString("dd/MM/yyyy"));
                    }
                    HtmlElement elementById2 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFechaInicial2_BtnFecha2");
                    if (elementById2 != null)
                    {
                        elementById2.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        elementById = this._wbSat.Document.GetElementById("datepicker");
                        flag2 = (elementById == null ? true : !elementById.Style.Contains("visibility: visible"));
                        if (!flag2)
                        {
                            break;
                        }
                    }
                    IEnumerable<HtmlElement> htmlElements = frmDownloadCfdi.ElementsByClass(this._wbSat.Document, "dpDayHighlightTD");
                    if (htmlElements != null)
                    {
                        htmlElements.First().InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        elementById = this._wbSat.Document.GetElementById("datepicker");
                        flag3 = (elementById == null ? true : elementById.Style.Contains("visibility: visible"));
                        if (!flag3)
                        {
                            break;
                        }
                    }
                    HtmlElement htmlElement2 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFechaFinal2_Calendario_text");
                    if (htmlElement2 != null)
                    {
                        dateTime = new DateTime(year, month, DateTime.DaysInMonth(year, month));
                        htmlElement2.SetAttribute("value", dateTime.ToString("dd/MM/yyyy"));
                    }
                    HtmlElement elementById3 = this._wbSat.Document.GetElementById("ctl00_MainContent_CldFechaFinal2_BtnFecha2");
                    if (elementById3 != null)
                    {
                        elementById3.InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        elementById = this._wbSat.Document.GetElementById("datepicker");
                        flag4 = (elementById == null ? true : !elementById.Style.Contains("visibility: visible"));
                        if (!flag4)
                        {
                            break;
                        }
                    }
                    htmlElements = frmDownloadCfdi.ElementsByClass(this._wbSat.Document, "dpDayHighlightTD");
                    if (htmlElements != null)
                    {
                        htmlElements.First<HtmlElement>().InvokeMember("click");
                    }
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        elementById = this._wbSat.Document.GetElementById("datepicker");
                        flag5 = (elementById == null ? true : elementById.Style.Contains("visibility: visible"));
                        if (!flag5)
                        {
                            break;
                        }
                    }
                    HtmlElement htmlElement3 = this._wbSat.Document.GetElementById("ctl00_MainContent_BtnBusqueda");
                    if (htmlElement3 != null)
                    {
                        htmlElement3.InvokeMember("click");
                    }
                    int num = 0;
                    while (true)
                    {
                        flag = true;
                        Application.DoEvents();
                        HtmlElement elementById4 = this._wbSat.Document.GetElementById("ctl00_MainContent_UpdateProgress1");
                        if (!(elementById4 == null))
                        {
                            flag6 = (elementById4.Style != "display: none;" ? true : num <= 100000);
                            if (!flag6)
                            {
                                break;
                            }
                            num++;
                        }
                    }
                    if (this._wbSat.Document.GetElementById("ctl00_MainContent_PnlLimiteRegistros") != null)
                    {
                        MessageBox.Show("La consulta realizada solo muestra los primeros 500 registros, se recomienda hacer la descarga por día", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    this.ProcessResult();
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void FillCombos()
        {
            try
            {
                this.cmbYear.Items.Clear();
                for (int i = DateTime.Now.Year; i >= 2011; i--)
                {
                    this.cmbYear.Items.Add(i);
                }
                this.cmbYear.SelectedIndex = 0;
                cmbAccount.SelectedIndex = 0;
                mes_txt.SelectedIndex = 0;
                DiaIni_txt.SelectedIndex = 0;
                DiaFin_txt.SelectedIndex = 0;
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {

        }

    }
}
