protected override void TratarRetornoEnviarSincrono(RetornoEnviar retornoWebservice, NotaServicoCollection notas) { // Analisa mensagem de retorno var xmlRet = XDocument.Parse(retornoWebservice.XmlRetorno); MensagemErro(retornoWebservice, xmlRet, "GerarNfseResposta"); if (retornoWebservice.Erros.Any()) { return; } retornoWebservice.Sucesso = xmlRet.Root.ElementAnyNs("ListaNfse") != null; if (!retornoWebservice.Sucesso) { return; } retornoWebservice.Data = xmlRet.Root.ElementAnyNs("ListaNfse").ElementAnyNs("CompNfse").ElementAnyNs("Nfse").ElementAnyNs("InfNfse").ElementAnyNs("DataEmissao")?.GetValue <DateTime>() ?? DateTime.MinValue; retornoWebservice.Protocolo = xmlRet.Root.ElementAnyNs("ListaNfse").ElementAnyNs("CompNfse").ElementAnyNs("Nfse").ElementAnyNs("InfNfse").ElementAnyNs("CodigoVerificacao")?.GetValue <string>() ?? ""; var listaNfse = xmlRet.Root.ElementAnyNs("ListaNfse"); if (listaNfse == null) { retornoWebservice.Erros.Add(new Evento { Codigo = "0", Descricao = "Lista de NFSe não encontrada! (ListaNfse)" }); return; } foreach (var compNfse in listaNfse.ElementsAnyNs("CompNfse")) { var nfse = compNfse.ElementAnyNs("Nfse").ElementAnyNs("InfNfse"); var numeroNFSe = nfse.ElementAnyNs("Numero")?.GetValue <string>() ?? string.Empty; var chaveNFSe = nfse.ElementAnyNs("CodigoVerificacao")?.GetValue <string>() ?? string.Empty; var dataNFSe = nfse.ElementAnyNs("DataEmissao")?.GetValue <DateTime>() ?? DateTime.Now; var numeroRps = nfse.ElementAnyNs("DeclaracaoPrestacaoServico")? .ElementAnyNs("InfDeclaracaoPrestacaoServico")? .ElementAnyNs("Rps")? .ElementAnyNs("IdentificacaoRps")? .ElementAnyNs("Numero").GetValue <string>() ?? string.Empty; GravarNFSeEmDisco(compNfse.AsString(true), $"NFSe-{numeroNFSe}-{chaveNFSe}-.xml", dataNFSe); var nota = notas.FirstOrDefault(x => x.IdentificacaoRps.Numero == numeroRps); if (nota == null) { nota = notas.Load(compNfse.ToString()); } else { nota.IdentificacaoNFSe.Numero = numeroNFSe; nota.IdentificacaoNFSe.Chave = chaveNFSe; } nota.Protocolo = retornoWebservice.Protocolo; } }
//CancelarNota protected override void TratarRetornoCancelarNFSe(RetornoCancelar retornoWebservice, NotaServicoCollection notas) { var xmlRet = XDocument.Parse(retornoWebservice.XmlRetorno); var root = xmlRet.ElementAnyNs("CancelarNotaResponse"); var data = root.ElementAnyNs("RetornoNota") ?? throw new Exception("Elemento do xml RetornoNota não encontado"); var errors = root.ElementAnyNs("DescricaoErros") ?? throw new Exception("Elemento do xml DescricaoErros não encontado"); var resultado = data.ElementAnyNs("Resultado")?.GetValue <int>() ?? 0; //var linkImpressao = data.ElementAnyNs("LinkImpressao")?.GetValue<string>() ?? string.Empty; //não utilizado pois não tem como retornar if (resultado != 1 && errors.HasElements) { retornoWebservice.Sucesso = false; foreach (var node in errors.Descendants().Where(x => x.Name == "item")) { var errorId = node.ElementAnyNs("id")?.Value ?? string.Empty; var errorProcesso = node.ElementAnyNs("DescricaoProcesso")?.Value ?? string.Empty; var errorDescricao = node.ElementAnyNs("DescricaoErro")?.Value ?? string.Empty; retornoWebservice.Erros.Add(new Evento() { Codigo = errorId, Correcao = errorProcesso, Descricao = errorDescricao }); } } else { retornoWebservice.Sucesso = true; } }