示例#1
0
        public TRetConsReciNFe ConsultaRecLote2(TConsReciNFe consReciNFe, TRetEnviNFe retEnvNFe, X509Certificate2 cert)
        {
            XmlNode strRetorno = null;

            C_WebService.ListaUrl listaURL = default(C_WebService.ListaUrl);
            listaURL = WsUrls.BuscaURL(retEnvNFe.cUF, retEnvNFe.tpAmb);

            string nomeArquivoPedido  = _PastaLog + retEnvNFe.infRec.nRec + "-ped-rec.xml";
            string nomeArquivoRetProc = _PastaLog + retEnvNFe.infRec.nRec + "-pro-rec.xml";

            try
            {
                RetRecepcao2.NfeRetRecepcao2 wsMsg = default(RetRecepcao2.NfeRetRecepcao2);
                RetRecepcao2.nfeCabecMsg     cab   = new RetRecepcao2.nfeCabecMsg();

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

                //CRIA UMA INSTANCIA DA CONEXÃO COM O WEBSERVICE
                wsMsg = new RetRecepcao2.NfeRetRecepcao2(listaURL.UrlNfeRetRecepcao);

                //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
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(Funcoes.RemoveNameSpaceFromXml(consReciNFe.Serialize()));
                xmldoc.Save(nomeArquivoPedido);

                //ENVIA CONSULTA PARA SEFAZ E OBTEM RETORNO EM FORMATO STRING
                strRetorno = wsMsg.nfeRetRecepcao2(xmldoc);

                TRetConsReciNFe retornoReciNFe = new TRetConsReciNFe();
                XmlDocument     retornoXML     = new XmlDocument();
                retornoXML.LoadXml(strRetorno.OuterXml);
                retornoXML.Save(nomeArquivoRetProc);
                retornoReciNFe            = TRetConsReciNFe.LoadFromFile(nomeArquivoRetProc);
                retornoReciNFe.XmlRetorno = retornoXML;

                return(retornoReciNFe);
            }
            catch (Exception)
            {
                throw new ACBr.Net.NFe.Exceptions.ConsultaRecLote2Exception("Falha ao consultar o recebimento de lote de NFe.", consReciNFe, retEnvNFe, cert);
            }
        }
        private List <RetornoNotaFiscal> ConsultarReciboLoteContingencia(string nRec, Modelo modelo)
        {
            var config = ConfiguracaoService.GetConfiguracao();
            X509Certificate2 certificado = null;

            var certificadoEntity = CertificadoService.GetCertificado();

            if (!string.IsNullOrWhiteSpace(certificadoEntity.Caminho))
            {
                certificado = CertificateManager.GetCertificateByPath(certificadoEntity.Caminho,
                                                                      RijndaelManagedEncryption.DecryptRijndael(certificadoEntity.Senha));
            }
            else
            {
                certificado = CertificateManager.GetCertificateBySerialNumber(certificadoEntity.NumeroSerial, false);
            }

            var consultaRecibo = new TConsReciNFe();

            consultaRecibo.versao = "4.00";
            consultaRecibo.tpAmb  = config.IsProducao ? XmlSchemas.NfeRetAutorizacao.Envio.TAmb.Item1 : XmlSchemas.NfeRetAutorizacao.Envio.TAmb.Item2;
            consultaRecibo.nRec   = nRec;

            string parametroXML = XmlUtil.Serialize(consultaRecibo, "http://www.portalfiscal.inf.br/nfe");

            var node = new XmlDocument();

            node.LoadXml(parametroXML);

            Ambiente ambiente = config.IsProducao ? Ambiente.Producao : Ambiente.Homologacao;
            var      codigoUF = (CodigoUfIbge)Enum.Parse(typeof(CodigoUfIbge), EmissorService.GetEmissor().Endereco.UF);

            try
            {
                var servico = ServiceFactory.GetService(modelo, ambiente, Factory.Servico.RetAutorizacao, codigoUF, certificado);
                var client  = (NFeRetAutorizacao4.NFeRetAutorizacao4SoapClient)servico.SoapClient;

                var result = client.nfeRetAutorizacaoLote(node);

                TRetConsReciNFe          retorno             = (TRetConsReciNFe)XmlUtil.Deserialize <TRetConsReciNFe>(result.OuterXml);
                List <RetornoNotaFiscal> retornoConsultaList = new List <RetornoNotaFiscal>();

                foreach (var protNFe in retorno.protNFe)
                {
                    var retornoConsultaNota = new RetornoNotaFiscal();

                    retornoConsultaNota.Chave           = protNFe.infProt.chNFe;
                    retornoConsultaNota.CodigoStatus    = protNFe.infProt.cStat;
                    retornoConsultaNota.DataAutorizacao = protNFe.infProt.dhRecbto;
                    retornoConsultaNota.Motivo          = protNFe.infProt.xMotivo;
                    retornoConsultaNota.Protocolo       = protNFe.infProt.nProt;
                    retornoConsultaNota.Xml             = XmlUtil.Serialize(protNFe, string.Empty)
                                                          .Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", string.Empty)
                                                          .Replace("TProtNFe", "protNFe")
                                                          .Replace("<infProt xmlns=\"http://www.portalfiscal.inf.br/nfe\">", "<infProt>");

                    retornoConsultaList.Add(retornoConsultaNota);
                }

                return(retornoConsultaList);
            }
            catch (Exception)
            {
                if (!_isFirstTimeRecheckingRecipts)
                {
                    _isFirstTimeRecheckingRecipts = true;
                    return(ConsultarReciboLoteContingencia(nRec, modelo));
                }
                else
                {
                    _isFirstTimeRecheckingRecipts = false;
                    return(null);
                }
            }
        }
示例#3
0
        public void DeserilizarEvento(object obj, ref Entidade_Recibo objDes)
        {
            Entidade_Recibo  retRecibo = new Entidade_Recibo();
            Model_NotaFiscal mNf       = new Model_NotaFiscal();
            Model_Lote       mLote     = new Model_Lote();
            XmlDocument      docRet    = new XmlDocument();

            RetReciNFe = new TRetConsReciNFe();
            StreamWriter SW;
            string       xProtNFe = string.Empty;

            object objRet = new object();

            retXmlNodeReader = new XmlNodeReader((XmlNode)obj);
            xmlDesSerializar = new XmlSerializer(typeof(TRetConsReciNFe));

            objRet = xmlDesSerializar.Deserialize(retXmlNodeReader);

            var Retorno = (TRetConsReciNFe)objRet;


            XmlNodeList xNodList = ((XmlNode)obj).ChildNodes;

            try
            {
                if (Retorno.protNFe != null)
                {
                    foreach (var Prot in Retorno.protNFe)
                    {
                        if (objDes.TpNf.Trim() == "S")
                        {
                            mNf.AtualizaRetornoNfeSaida(Prot.infProt.cStat, Prot.infProt.nProt, objDes.Loja, Prot.infProt.chNFe, Model_NotaFiscal.NotaFiscal.Saida);
                        }
                        else
                        {
                            mNf.AtualizaRetornoNfeSaida(Prot.infProt.cStat, Prot.infProt.nProt, objDes.Loja, Prot.infProt.chNFe, Model_NotaFiscal.NotaFiscal.Entrada);
                        }

                        for (int i = 0; i < xNodList.Count; i++)
                        {
                            if (xNodList.Item(i).Name.Equals("protNFe"))
                            {
                                XmlNode     xNodListProc  = xNodList.Item(i).FirstChild;
                                XmlNodeList xNodListProc2 = xNodListProc.ChildNodes;

                                for (int j = 0; j < xNodListProc2.Count; j++)
                                {
                                    if (xNodListProc2.Item(j).Name.Equals("chNFe"))
                                    {
                                        xProtNFe = xNodList.Item(i).OuterXml;
                                        break;
                                    }
                                }
                            }
                        }
                        mLote.ItemLoteNfe(Prot.infProt.cStat, Prot.infProt.chNFe, xProtNFe);
                        mLote.LoteProcessado(Retorno.cStat, Convert.ToDateTime(Prot.infProt.dhRecbto), Retorno.nRec);
                    }
                }
                else
                {
                    mLote.LoteProcessado(Retorno.cStat, Convert.ToDateTime(Retorno.dhRecbto), Retorno.nRec);
                }

                if (!Directory.Exists(@"C:\NFe\Retorno_Proc_NFe\"))
                {
                    Directory.CreateDirectory(@"C:\NFe\Retorno_Proc_NFe\");
                }
                FileStream FS = new FileStream(@"C:\NFe\Retorno_Proc_NFe\" + string.Format("{0:MMddyyyy}", DateTime.Now.Date) + "_" + Retorno.nRec.Trim() + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                SW = new StreamWriter(FS);

                xmlDesSerializar.Serialize(SW, Retorno);
            }
            catch (Exception Ex)
            {
                Mensagem.MensagemErro(Mensagem.TipoMensagem.RetAutoriz, "Saida", Ex.Message.ToString());

                if (!Directory.Exists(@"C:\NFe\Retorno_Proc_NFe\"))
                {
                    Directory.CreateDirectory(@"C:\NFe\Retorno_Proc_NFe\");
                }
                FileStream FS = new FileStream(@"C:\NFe\Retorno_Proc_NFe\" + string.Format("{0:MMddyyyy}", DateTime.Now.Date) + "_" + Retorno.nRec.Trim() + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                SW = new StreamWriter(FS);

                xmlDesSerializar.Serialize(SW, Retorno);
            }
        }