Example #1
0
        public TRetEnviNFe EnviaLote2(HiperNFe Notas, int NumLote)
        {
            XmlNode strRetorno = null;
            dynamic xmldoc = new XmlDocument();

            ListaUrl listaURL = default(ListaUrl);
            listaURL = WsUrls.BuscaURL(Notas.Configuracao.CodUF, Notas.Configuracao.Ambiente);

            string nomeArquivoLote = _PastaLog + NumLote.ToString() + "-env-lot.xml";
            string nomeArquivoRetLote = _PastaLog + NumLote.ToString() + "-rec.xml";

            try
            {
                TEnviNFe LoteNFe = new TEnviNFe();
                LoteNFe.idLote = NumLote.ToString();
                LoteNFe.versao = Notas.Configuracao.Versao;
                // ERROR: Not supported in C#: ReDimStatement

                int indice = 0;
                LoteNFe.NFe = new TNFe[Notas.NotasFiscais.Count];
                foreach (TNfeProc nota in Notas.NotasFiscais)
                {
                    LoteNFe.NFe[indice] = nota.NFe;
                    indice += 1;
                }
                LoteNFe.GeraLoteNFe(nomeArquivoLote);
                xmldoc.Load(nomeArquivoLote);
                //Carrega o arquivo XML 

                Recepcao2.NfeRecepcao2 wsMsg = default(Recepcao2.NfeRecepcao2);
                Recepcao2.nfeCabecMsg cab = new Recepcao2.nfeCabecMsg();

                //UF E VERSÃO DO CABEÇALHO
                cab.cUF = PegarCodigoUF(Notas.Configuracao.CodUF);
                cab.versaoDados = LoteNFe.versao;

                //CABEÇALHO USADA PARA ENVIO DE LOTE
                wsMsg = new Recepcao2.NfeRecepcao2(listaURL.UrlNfeRecepcao);
                wsMsg.nfeCabecMsgValue = cab;

                wsMsg.Timeout = 100000;
                wsMsg.ClientCertificates.Add(Notas.Certificado);
                wsMsg.SoapVersion = SoapProtocolVersion.Soap12;

                xmldoc.Save(nomeArquivoLote);
                //RETORNO DA SEFAZ
                strRetorno = wsMsg.nfeRecepcaoLote2(xmldoc);
                TRetEnviNFe retornoEnvio = new TRetEnviNFe();
                XmlDocument retornoXML = new XmlDocument();
                retornoXML.LoadXml(strRetorno.OuterXml);
                retornoXML.Save(nomeArquivoRetLote);
                retornoEnvio = TRetEnviNFe.LoadFromFile(nomeArquivoRetLote);

                return retornoEnvio;
            }
            catch (Exception)
            {
                throw new ACBr.Net.NFe.Exceptions.EnviaLote2Exception("Falha ao enviar o lote de NFe.");
            }
        }
Example #2
0
        public List<TRetConsSitNFe> ConsultaSitNFe(HiperNFe hiperNFe, X509Certificate2 cert)
        {
            XmlNode strRetorno = null;
            C_WebService.ListaUrl listaURL = default(C_WebService.ListaUrl);
            listaURL = WsUrls.BuscaURL(hiperNFe.Configuracao.CodUF, hiperNFe.Configuracao.Ambiente);
            try
            {
                Consulta2.NfeConsulta2 wsMsg = default(Consulta2.NfeConsulta2);
                Consulta2.nfeCabecMsg cab = new Consulta2.nfeCabecMsg();

                //UF E VERSÃO DO CABEÇALHO
                cab.cUF = PegarCodigoUF(hiperNFe.Configuracao.CodUF);
                cab.versaoDados = hiperNFe.Configuracao.Versao;

                //CRIA UMA INSTANCIA DA CONEXÃO COM O WEBSERVICE
                wsMsg = new Consulta2.NfeConsulta2(listaURL.UrlNfeConsultaProtocolo);

                //ASSOCIA CABEÇALHO NFE
                wsMsg.nfeCabecMsgValue = cab;

                //DEFINE TEMPO MAXIMO DE ESPERA POR RETORNO
                wsMsg.Timeout = 100000;

                //ASSOCIA CERTIFICADO A CONEXAO WEBSERVICE
                wsMsg.ClientCertificates.Add(cert);

                //DEFINE PROTOCOLO USADO NA CONEXÃO
                wsMsg.SoapVersion = SoapProtocolVersion.Soap12;

                //CRIA UM NOVO DOCUMENTO XML                
                List<TRetConsSitNFe> listaRetorno = new List<TRetConsSitNFe>();
                foreach (TNfeProc nota in hiperNFe.NotasFiscais)
                {
                    TConsSitNFe consSitNfe = new TConsSitNFe();
                    string chaveNfe = nota.NFe.infNFe.Id.Substring(3);
                    string arquivoConsulta = _PastaLog + chaveNfe + "-ped-sit.xml";
                    string arquivoRetorno = _PastaLog + chaveNfe + "-sit.xml";
                    consSitNfe.chNFe = chaveNfe;
                    consSitNfe.tpAmb = hiperNFe.Configuracao.Ambiente;
                    //consSitNfe.versao = nota.NFe.infNFe.versao;
                    consSitNfe.versao = TVerConsSitNFe.Versao201;
                    consSitNfe.xServ = TConsSitNFeXServ.CONSULTAR;

                    XmlDocument dados = new XmlDocument();
                    dados.LoadXml(Funcoes.RemoveNameSpaceFromXml(consSitNfe.Serialize()));
                    dados.Save(arquivoConsulta);

                    strRetorno = wsMsg.nfeConsultaNF2(dados);

                    XmlDocument retornoXML = new XmlDocument();
                    retornoXML.LoadXml(strRetorno.OuterXml);
                    retornoXML.Save(arquivoRetorno);
                    TRetConsSitNFe retornoSitNFe = new TRetConsSitNFe();
                    retornoSitNFe = TRetConsSitNFe.LoadFromFile(arquivoRetorno);
                    nota.procEventoNFe = retornoSitNFe.procEventoNFe;

                    if (nota.procEventoNFe != null)
                    {
                        foreach (var item in nota.procEventoNFe)
                        {
                            string arquivoProcEvento = _PastaLog + item.evento.infEvento.tpEvento + chaveNfe + item.evento.infEvento.nSeqEvento + "-procEventoNfe.xml";
                            XmlDocument xmlEvento = new XmlDocument();
                            item.SaveToFile(arquivoProcEvento);
                            xmlEvento.LoadXml(item.Serialize());
                            item.ArquivoXML = xmlEvento;
                            item.NomeArquivo = arquivoProcEvento;
                        }
                    }

                    listaRetorno.Add(retornoSitNFe);
                }

                return listaRetorno;

            }
            catch (Exception ex)
            {
                throw new System.InvalidOperationException("Erro ao consultar a situação da NFe. Erro: " + ex.Message);
            }
        }