Esempio n. 1
0
        private IList <NotaDestinada> ConsultaNotasDestinadas()
        {
            var aCodigoUf = Interaction.InputBox("Informe o código da UF", "ConsultarDistribuicaoDFe", "41");
            var aNSU      = Interaction.InputBox("Informe o inicio da sequencia NSU", "ConsultarDistribuicaoDFe", "000000000000000");

            var aTipoNSU = MessageBox.Show("aNSU é específico",
                                           "ConsultarDistribuicaoDFe",
                                           MessageBoxButtons.YesNo,
                                           MessageBoxIcon.Question) == DialogResult.Yes ? TipoNSU.nkEspecifico : TipoNSU.nkUltimo;

            var xml                  = String.Empty;
            var ultimoNSU            = String.Empty;
            var cStat                = String.Empty;
            var ultNSU               = String.Empty;
            var maxNSU               = String.Empty;
            var proximaConsulta      = true;
            var listaNotasDestinadas = new List <NotaDestinada>();

            do
            {
                xml   = spdNFe.ConsultarDistribuicaoDFe(aCodigoUf, spdNFe.CNPJ, aNSU, aTipoNSU);
                cStat = GetTagValueXml(xml, "cStat");

                if (!cStat.Equals("138"))
                {
                    proximaConsulta = false;
                    break;
                }

                ultNSU          = GetTagValueXml(xml, "ultNSU");
                maxNSU          = GetTagValueXml(xml, "maxNSU");
                proximaConsulta = cStat.Equals("138") && aNSU != maxNSU;

                aNSU = ultNSU;

                var documentXml = new XmlDocument();
                documentXml.LoadXml(xml);
                var xmlNodes = documentXml.GetElementsByTagName("docZip");

                foreach (XmlNode node in xmlNodes)
                {
                    var conteudoDocZip        = node.InnerText;
                    var conteudoDescompactado = spdNFe.DescompactarXMLZip(conteudoDocZip);
                    var schema   = node.Attributes["schema"].InnerText;
                    var chaveNfe = ObterChaveNFe(schema, conteudoDescompactado);

                    var notaDestinada = new NotaDestinada();
                    notaDestinada.Nsu                   = node.Attributes["NSU"].InnerText;
                    notaDestinada.Schema                = schema;
                    notaDestinada.ConteudoOriginal      = conteudoDocZip;
                    notaDestinada.ConteudoDescompactado = conteudoDescompactado;
                    notaDestinada.Chave                 = chaveNfe;

                    listaNotasDestinadas.Add(notaDestinada);
                }
            } while (proximaConsulta);

            return(listaNotasDestinadas);
        }
        private void btnConsultarDFe_Click(object sender, EventArgs e)
        {
            txtQtdNotas.Text = "0";
            txtChaveDownload.Clear();
            txtStatusDownloadNFe.Clear();
            rtbXML.Clear();

            var _tipoNSU = rdbUltimoNSU.Checked ? TipoNSU.nkUltimo : TipoNSU.nkEspecifico;

            var _xml = _spdNFeX.ConsultarDistribuicaoDFe(GetCodigoUF(cbUF.SelectedIndex),
                                                         txtCnpjCpf.Text,
                                                         txtNSU.Text,
                                                         _tipoNSU);

            if (!string.IsNullOrEmpty(_xml))
            {
                rtbXML.Text = _xml;
                DescompactarDFe(_xml);
            }
        }