Пример #1
0
 public UrlSefaz(eTipoServico servico, eUF uf, eTipoAmbiente tipoAmbiente, eModeloDocumento modeloDocumento, string url, string action)
 {
     Url             = url;
     Action          = action;
     UF              = uf;
     TipoAmbiente    = tipoAmbiente;
     ModeloDocumento = modeloDocumento;
     Servico         = servico;
 }
Пример #2
0
 public ChaveFiscal(eUF uf, DateTime dataEmissao, string cnpj, eModeloDocumento modelo, int serie, long numero, eTipoEmissao tipoEmissao, string cNF)
 {
     try
     {
         this.UF            = uf;
         this.AnoMesEmissao = dataEmissao.ToString("yyMM");
         this.Cnpj          = cnpj;
         this.Modelo        = modelo;
         this.Serie         = serie;
         this.Numero        = numero;
         this.TipoEmissao   = tipoEmissao;
         this.CNF           = cNF;
         this.chave         = GerarChave();
     }
     catch
     {
         throw new Exception("Chave inválida");
     }
 }
Пример #3
0
        private async Task <IRetornoAutorizacao> Autorizar(string xmlenviNFe4, eModeloDocumento modeloDocumento)
        {
            var arqEnv = Path.Combine(cfgServico.DiretorioSalvarXml, "Logs", $"{DateTime.Now.Ticks}-env-nfe.xml", xmlenviNFe4);
            await storage.SaveAsync(arqEnv, xmlenviNFe4);

            var urlSefaz = Fabrica.FabricarUrl.ObterUrl(eTipoServico.AutorizarNFe, cfgServico.TipoAmbiente, modeloDocumento, cfgServico.UF);

            var envelope = Fabrica.SoapEnvelopeFabrica.FabricarEnvelope(eTipoServico.AutorizarNFe, xmlenviNFe4);

            var retornoXmlString = await transmitir.TransmitirAsync(urlSefaz, envelope);

            var retornoLimpo = Soap.LimparEnvelope(retornoXmlString, "retEnviNFe").OuterXml;

            var arqRet = Path.Combine("Logs", $"{DateTime.Now.Ticks}-ret-env-nfe.xml");
            await storage.SaveAsync(arqRet, retornoLimpo);

            var retEnviNFe = new RetNFeAutorizacao4(retornoLimpo);

            retEnviNFe.XmlEnviado = xmlenviNFe4;
            return(retEnviNFe);
        }
        public async Task <retInutNFe> Inutilizar(int ano, eModeloDocumento modeloDocumento, int serie, int numeroInicial, int numeroFinal, string justificativa)
        {
            var cnpj            = cfgServico.Emitente.CNPJ ?? cfgServico.Emitente.CPF;
            var tpAmb           = cfgServico.TipoAmbiente;
            var uf              = cfgServico.UF;
            var pedInutilizacao = FabricarInutNFe(tpAmb, uf, ano, cnpj, modeloDocumento, serie, numeroInicial, numeroFinal, justificativa);

            var xmlInutilizacao = XmlUtils.ClasseParaXmlString <inutNFe>(pedInutilizacao);

            var arqEnv = Path.Combine("Logs", Arquivo.MontarNomeArquivo("ped-inut.xml", cfgServico));
            await storage.SaveAsync(arqEnv, xmlInutilizacao);

            var envelope         = SoapEnvelopeFabrica.FabricarEnvelope(eTipoServico.InutilizacaoNFe, xmlInutilizacao);
            var sefazUrl         = FabricarUrl.ObterUrl(eTipoServico.InutilizacaoNFe, cfgServico.TipoAmbiente, modeloDocumento, cfgServico.UF);
            var retornoXmlString = await transmitir.TransmitirAsync(sefazUrl, envelope);

            var retornoLimpo = Soap.LimparEnvelope(retornoXmlString, "retInutNFe").OuterXml;

            var arqRet = Path.Combine("Logs", Arquivo.MontarNomeArquivo("ret-inut.xml", cfgServico));
            await storage.SaveAsync(arqRet, retornoLimpo);

            return(XmlUtils.XmlStringParaClasse <retInutNFe>(retornoLimpo));
        }
Пример #5
0
        private async Task <retEnvEvento> Cancelar(List <evento> eventos, eModeloDocumento modeloDoc)
        {
            var pedEvento = new envEvento
            {
                versao = versao,
                idLote = 1,
                evento = eventos
            };

            foreach (var eventoTmp in eventos)
            {
                eventoTmp.infEvento.Id = "ID" + ((int)eventoTmp.infEvento.tpEvento) + eventoTmp.infEvento.chNFe +
                                         eventoTmp.infEvento.nSeqEvento.ToString().PadLeft(2, '0');

                var _certificado = ObterCertificado.Obter(cfgServico.ConfigCertificado);
                eventoTmp.Assinar(_certificado, cfgServico.ConfigCertificado.SignatureMethodSignedXml, cfgServico.ConfigCertificado.DigestMethodReference);
            }

            var xmlEvento = XmlUtils.ClasseParaXmlString <envEvento>(pedEvento);

            var arqEnv = Path.Combine("Logs", $"{DateTime.Now.Ticks}-ped-eve.xml");
            await storage.SaveAsync(arqEnv, xmlEvento);

            var sefazUrl = Fabrica.FabricarUrl.ObterUrl(eTipoServico.CancelarNFe, cfgServico.TipoAmbiente, modeloDoc, cfgServico.UF);
            var envelope = Fabrica.SoapEnvelopeFabrica.FabricarEnvelope(eTipoServico.CancelarNFe, xmlEvento);

            var retornoXmlString = await transmitir.TransmitirAsync(sefazUrl, envelope);

            var retornoXmlStringLimpa = Soap.LimparEnvelope(retornoXmlString, "retEnvEvento").OuterXml;

            var arqRet = Path.Combine("Logs", $"{DateTime.Now.Ticks}-ret-eve.xml");
            await storage.SaveAsync(arqRet, retornoXmlStringLimpa);

            var retEnvEvento = new retEnvEvento().CarregarDeXmlString(retornoXmlStringLimpa, xmlEvento);

            return(retEnvEvento);
        }
Пример #6
0
        public static UrlSefaz ObterUrl(eTipoServico tipoServico, eTipoAmbiente tipoAmbiente, eModeloDocumento modeloDocumento, eUF uf)
        {
            var urlAction = UrlsSefaz().Where(x => x.Servico == tipoServico && x.UF == uf && x.TipoAmbiente == tipoAmbiente && x.ModeloDocumento == modeloDocumento).FirstOrDefault();

            if (urlAction == null)
            {
                throw new ArgumentOutOfRangeException("Nenhuma URL do Webservice encontrada");
            }
            return(urlAction);
        }
Пример #7
0
        public async Task <retEnvEvento> Cancelar(IList <InfoNFeCancelar> infos)
        {
            if (infos == null || infos.Count <= 0)
            {
                throw new Exception("Informações da NFe não encontrada");
            }

            if (infos.Count > 20)
            {
                throw new Exception("No máximo 20 NFes podem ser canceladas");
            }

            var modelos = infos.Select(x => x.ChaveAcesso.Modelo).Distinct().ToList();

            if (modelos.Count != 1)
            {
                throw new FalhaValidacaoException("Lista de cancelamento deve conter somente 1 modelo");
            }

            eModeloDocumento modeloDocumento = modelos.FirstOrDefault();

            List <evento> eventos = new List <evento>();

            foreach (var item in infos)
            {
                var protocolo = item.ProtocoloAutorizacao;
                var chave     = item.ChaveAcesso;
                var just      = item.Justificativa;

                if (string.IsNullOrEmpty(just) || just.Length < 15 || just.Length > 255)
                {
                    throw new Exception("Justificativa de conter entre 15 e 255 caracteres");
                }

                var detEvento = new detEvento
                {
                    nProt      = protocolo.Protocolo,
                    versao     = versao,
                    xJust      = just,
                    descEvento = "Cancelamento",
                };

                var infEvento = new infEventoEnv
                {
                    cOrgao     = cfgServico.UF,
                    tpAmb      = cfgServico.TipoAmbiente,
                    chNFe      = chave.Chave,
                    dhEvento   = DateTime.Now,
                    tpEvento   = eTipoEventoNFe.NFeCancelamento,
                    nSeqEvento = 1,
                    verEvento  = versao,
                    detEvento  = detEvento
                };

                if (!string.IsNullOrEmpty(cfgServico.Emitente.CNPJ))
                {
                    infEvento.CNPJ = cfgServico.Emitente.CNPJ;
                }
                else
                {
                    infEvento.CPF = cfgServico.Emitente.CPF;
                }

                var evento = new evento {
                    versao = versao, infEvento = infEvento
                };
                eventos.Add(evento);
            }
            return(await Cancelar(eventos, modeloDocumento));
        }
        private inutNFe FabricarInutNFe(eTipoAmbiente tpAmb, eUF uf, int ano, string cnpj, eModeloDocumento modelo, int serie, int numeroInicial, int numeroFinal, string justificativa)
        {
            //Zion.Common.Assertions.ZionAssertion.StringHasMinLen(justificativa, 15, "Justificativa deve conter entre 15 e 255 caracteres");
            //Zion.Common.Assertions.ZionAssertion.StringHasMaxLen(justificativa, 255, "Justificativa deve conter entre 15 e 255 caracteres");

            string versaoServico = cfgServico.VersaoInutilizacaoNFe.Descricao();

            var pedInutilizacao = new inutNFe
            {
                versao  = versaoServico,
                infInut = new infInutEnv
                {
                    tpAmb  = tpAmb,
                    cUF    = uf,
                    ano    = ano,
                    CNPJ   = cnpj,
                    mod    = modelo,
                    serie  = serie,
                    nNFIni = numeroInicial,
                    nNFFin = numeroFinal,
                    xJust  = justificativa
                }
            };

            var numId = string.Concat(
                (int)pedInutilizacao.infInut.cUF,
                pedInutilizacao.infInut.ano.ToString("D2"),
                pedInutilizacao.infInut.CNPJ, (int)pedInutilizacao.infInut.mod,
                pedInutilizacao.infInut.serie.ToString().PadLeft(3, '0'),
                pedInutilizacao.infInut.nNFIni.ToString().PadLeft(9, '0'),
                pedInutilizacao.infInut.nNFFin.ToString().PadLeft(9, '0')
                );

            pedInutilizacao.infInut.Id = "ID" + numId;

            pedInutilizacao.Assinar(ObterCertificado.Obter(cfgServico.ConfigCertificado));

            return(pedInutilizacao);
        }
        public async Task <retEnvEvento> TransmitirCorrecao(IList <InfoCartaCorrecao> infos)
        {
            if (infos == null || infos.Count <= 0)
            {
                throw new Exception("Informações da NFe não encontrada");
            }

            if (infos.Count > 20)
            {
                throw new Exception("No máximo 20 NFes podem ser Corrigidas");
            }


            List <evento> eventos = new List <evento>();

            foreach (var item in infos)
            {
                var chave      = item.ChaveAcesso;
                var correcao   = item.Correcao;
                var nSeqEvento = item.nSeqEvento;
                chave = chave.Replace("NFe", "");
                eModeloDocumento _modeloDocumento = chave.Substring(20, 2).ModeloDocumento();

                if (item.Correcao.Length < 15 || item.Correcao.Length > 1000)
                {
                    throw new FalhaValidacaoException("A descrição da correção deve conter entre 15 e 1000 caracteres");
                }

                if (_modeloDocumento != eModeloDocumento.NFe)
                {
                    throw new FalhaValidacaoException("Somente NFe pode ter Carta Correção");
                }

                var infEvento = new infEventoEnv
                {
                    chNFe      = chave,
                    CNPJ       = cfgServico.Emitente.CNPJ,
                    CPF        = cfgServico.Emitente.CPF,
                    cOrgao     = cfgServico.UF,
                    dhEvento   = DateTime.Now,
                    nSeqEvento = nSeqEvento,
                    tpAmb      = cfgServico.TipoAmbiente,
                    tpEvento   = eTipoEventoNFe.NFeCartaCorrecao,
                    verEvento  = _versao,
                    detEvento  = new detEvento()
                    {
                        versao     = _versao,
                        descEvento = "Carta de Correção",
                        xCorrecao  = correcao
                    }
                };
                var evento = new evento {
                    versao = _versao, infEvento = infEvento
                };
                eventos.Add(evento);
            }

            foreach (var eventoTmp in eventos)
            {
                eventoTmp.infEvento.Id = "ID" + ((int)eventoTmp.infEvento.tpEvento) + eventoTmp.infEvento.chNFe +
                                         eventoTmp.infEvento.nSeqEvento.ToString().PadLeft(2, '0');

                var _certificado = ObterCertificado.Obter(cfgServico.ConfigCertificado);
                eventoTmp.Assinar(_certificado, cfgServico.ConfigCertificado.SignatureMethodSignedXml, cfgServico.ConfigCertificado.DigestMethodReference);
            }

            var pedEvento = new envEvento
            {
                versao = _versao,
                idLote = 1,
                evento = eventos
            };

            var xmlEvento = XmlUtils.ClasseParaXmlString <envEvento>(pedEvento);

            var arqEnv = Path.Combine("Logs", Arquivo.MontarNomeArquivo("ped-eve.xml", cfgServico));
            await storage.SaveAsync(arqEnv, xmlEvento);

            var sefazUrl = Fabrica.FabricarUrl.ObterUrl(eTipoServico.CartaCorrecao, cfgServico.TipoAmbiente, eModeloDocumento.NFe, cfgServico.UF);
            var envelope = Fabrica.SoapEnvelopeFabrica.FabricarEnvelope(eTipoServico.CartaCorrecao, xmlEvento);

            var retornoXmlString = await transmitir.TransmitirAsync(sefazUrl, envelope);

            var retornoXmlStringLimpa = Soap.LimparEnvelope(retornoXmlString, "retEnvEvento").OuterXml;

            var arqRet = Path.Combine("Logs", Arquivo.MontarNomeArquivo("ret-eve.xml", cfgServico));
            await storage.SaveAsync(arqRet, retornoXmlStringLimpa);

            var retorno = new retEnvEvento().CarregarDeXmlString(retornoXmlStringLimpa, xmlEvento);

            return(retorno);
        }