public Task <string> GerarZipEnvioContabilidadeAsync(DateTime periodo) { return(Task.Run(() => { var config = _configuracaoService.GetConfiguracao(); var notasNoPeriodo = _notaFiscalRepository.GetNotasFiscaisPorMesAno(periodo, config.IsProducao, true); var nfeNoPeriodo = notasNoPeriodo.Where(n => n.Modelo.Equals("55")).ToList(); var nfceNoPeriodo = notasNoPeriodo.Where(n => n.Modelo.Equals("65")).ToList(); var eventosCancelamentoNFe = _eventoService.GetEventosPorNotasId(nfeNoPeriodo.Select(n => n.Id), true); var eventosCancelamentoNFCe = _eventoService.GetEventosPorNotasId(nfceNoPeriodo.Select(n => n.Id), true); var nfeXMLs = nfeNoPeriodo.Select(n => new NotaXml(n.Chave, n.LoadXml())).ToList(); var eventoNfeXMLs = eventosCancelamentoNFe.Select(e => new EventoCancelamentoXml(e.ChaveIdEvento, e.Xml)).ToList(); var nfceXMLs = nfceNoPeriodo.Select(n => new NotaXml(n.Chave, n.LoadXml())).ToList(); var eventoNfceXMLs = eventosCancelamentoNFCe.Select(e => new EventoCancelamentoXml(e.ChaveIdEvento, e.Xml)).ToList(); var notasZip = notasNoPeriodo.ToList(); var notasInutilizadas = _notaInutilizadaService.GetNotasFiscaisPorMesAno(periodo, config.IsProducao).ToList(); string startPath = Path.Combine(Path.GetTempPath(), "EmissorNFe"); try { if (!Directory.Exists(startPath)) { Directory.CreateDirectory(startPath); } else { Directory.Delete(startPath, true); Directory.CreateDirectory(startPath); } string nfeDir = Path.Combine(startPath, "NFe"); string nfceDir = Path.Combine(startPath, "NFCe"); string zipPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), @"Notas Fiscais " + periodo.ToString("MM_yyyy") + ".zip"); if (File.Exists(zipPath)) { File.Delete(zipPath); } _geradorPdf.GerarRelatorioGerencial(notasZip, notasInutilizadas, periodo, startPath); //Gerar arquivos de notas inutilizadas e adicioná-las ao relatório if (GravarXmlsNfe(nfeXMLs, eventoNfeXMLs, nfeDir) && GravarXmlsNfce(nfceXMLs, eventoNfceXMLs, nfceDir) && GerarXmlsNotasInutilizadas(notasInutilizadas, startPath)) { ZipFile.CreateFromDirectory(startPath, zipPath); } return zipPath; } catch (Exception e) { log.Error(e); throw; } finally { Directory.Delete(startPath, true); } })); }