Exemple #1
0
        protected async void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var id = Request.QueryString["id"].ToInt();

                if (id > 0)
                {
                    var dueBusca = _documentoUnicoExportacaoDAO.ObterDUEPorId(id);

                    if (dueBusca.CriadoPorNF > 0)
                    {
                        this.gvNotasFiscais.DataSource = _documentoUnicoExportacaoDAO.ObterNotasPorDueId(id);
                        this.gvNotasFiscais.DataBind();

                        foreach (GridViewRow linhaGrid in this.gvNotasFiscais.Rows)
                        {
                            if (linhaGrid.Cells[0].Text != "EXP")
                            {
                                var chaveNf    = linhaGrid.Cells[1].Text;
                                var quantidade = linhaGrid.Cells[4].Text.ToDecimal();

                                var saldoDue = _documentoUnicoExportacaoDAO.ObterQuantidadeDUEPorNF(chaveNf);

                                var saldoNota = await ServicoSiscomex2.ConsultarDadosNotaPreACD(chaveNf, ConfigurationManager.AppSettings["CpfCertificado"].ToString());

                                foreach (var linha in saldoNota)
                                {
                                    if (linha != null)
                                    {
                                        if (linha.Sucesso == false)
                                        {
                                            linhaGrid.BackColor = System.Drawing.Color.MistyRose;
                                        }
                                        else
                                        {
                                            quantidade = quantidade - saldoDue;

                                            if (!((quantidade) <= (decimal)linha.Saldo))
                                            {
                                                linhaGrid.BackColor = System.Drawing.Color.LightYellow;
                                            }
                                            else
                                            {
                                                linhaGrid.BackColor = System.Drawing.Color.LightGreen;
                                            }

                                            linhaGrid.Cells[5].Text  = linha.Saldo.ToString();
                                            linhaGrid.Cells[6].Text  = saldoDue.ToString();
                                            linhaGrid.Cells[11].Text = linha.Item.ToString();
                                            linhaGrid.Cells[12].Text = linha.Recinto;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        List <NotaFiscal> notasExportacao = _documentoUnicoExportacaoDAO.ObterNotasExportacaoPorDueId(id).ToList();

                        if (notasExportacao.Count() == 0)
                        {
                            var itensDUE = _documentoUnicoExportacaoDAO.ObterItensDUE(id);

                            foreach (var itemDue in itensDUE)
                            {
                                var notaExportacao = _notaFiscalDAO.ObterNotasExportacaoPorChave(itemDue.NF);

                                if (notaExportacao != null)
                                {
                                    notasExportacao.Add(notaExportacao);
                                }
                            }
                        }

                        var notasGrid = new List <NotaFiscal>();

                        foreach (var notaExportacao in notasExportacao)
                        {
                            notasGrid.Add(notaExportacao);

                            var notasRemessa = _documentoUnicoExportacaoDAO.ObterNotasRemessaPorDueId(notaExportacao.ChaveNF).ToList();

                            foreach (var notaRemessa in notasRemessa)
                            {
                                notasGrid.Add(notaRemessa);
                            }
                        }

                        this.gvNotasFiscais.DataSource = notasGrid.ToList();
                        this.gvNotasFiscais.DataBind();

                        foreach (GridViewRow linhaGrid in this.gvNotasFiscais.Rows)
                        {
                            if (linhaGrid.Cells[0].Text != "EXP")
                            {
                                var chaveNf    = linhaGrid.Cells[1].Text;
                                var quantidade = linhaGrid.Cells[4].Text.ToDecimal();

                                var saldoDue = _documentoUnicoExportacaoDAO.ObterQuantidadeDUEPorNF(chaveNf);

                                var saldoNota = await ServicoSiscomex2.ConsultarDadosNotaPreACD(chaveNf, ConfigurationManager.AppSettings["CpfCertificado"].ToString());

                                foreach (var linha in saldoNota)
                                {
                                    if (linha != null)
                                    {
                                        if (linha.Sucesso == false)
                                        {
                                            linhaGrid.BackColor = System.Drawing.Color.MistyRose;
                                        }
                                        else
                                        {
                                            quantidade = quantidade - saldoDue;

                                            if (!((quantidade) <= (decimal)linha.Saldo))
                                            {
                                                linhaGrid.BackColor = System.Drawing.Color.LightYellow;
                                            }
                                            else
                                            {
                                                linhaGrid.BackColor = System.Drawing.Color.LightGreen;
                                            }

                                            linhaGrid.Cells[5].Text  = linha.Saldo.ToString();
                                            linhaGrid.Cells[6].Text  = saldoDue.ToString();
                                            linhaGrid.Cells[11].Text = linha.Item.ToString();
                                            linhaGrid.Cells[12].Text = linha.Recinto;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }