Exemple #1
0
        public NotasFiscais.Emissor GetEmissor()
        {
            var emitenteDb = _emitenteRepository.GetEmitente();

            var enderecoEmitente = new Endereco(emitenteDb.Logradouro, emitenteDb.Numero, emitenteDb.Bairro,
                                                emitenteDb.Municipio, emitenteDb.CEP, emitenteDb.UF);

            return(new NotasFiscais.Emissor(emitenteDb.RazaoSocial, emitenteDb.NomeFantasia, emitenteDb.CNPJ,
                                            emitenteDb.InscricaoEstadual, emitenteDb.InscricaoMunicipal, emitenteDb.CNAE,
                                            emitenteDb.RegimeTributario, enderecoEmitente, emitenteDb.Telefone));
        }
Exemple #2
0
        public Task EnviarNotasParaContabilidade(int diaParaEnvio)
        {
            return(Task.Run(async() =>
            {
                var historicoPeriodoCount =
                    await _historicoEnvioContabilidadeRepository.GetHistoricoByPeriodoAsync(DateTime.Now.AddMonths(-1));

                if (DateTime.Now.Day >= diaParaEnvio && historicoPeriodoCount == 0)
                {
                    var periodo = DateTime.Now.AddMonths(-1);
                    var periodoStr = periodo.ToString("MM/yyyy");
                    var path = await _geradorZip.GerarZipEnvioContabilidadeAsync(periodo);

                    if (string.IsNullOrWhiteSpace(path))
                    {
                        throw new Exception("Ocorreu um erro ao gerar os arquivos das notas fiscais!");
                    }

                    try
                    {
                        var emissor = _emitenteRepository.GetEmitente();

                        string fromAccount = ConfigurationManager.AppSettings["fromMailAccount"];
                        string fromMail = ConfigurationManager.AppSettings["fromMailName"];

                        var fromAddress = new MailAddress(fromAccount, fromMail);
                        var toAddress = new MailAddress(ConfigurationManager.AppSettings["toMailAccount"], ConfigurationManager.AppSettings["toMailName"]);
                        string fromPassword = ConfigurationManager.AppSettings["fromMailPassword"];
                        var subject = string.Concat("Notas Fiscais " + periodoStr, " - ", emissor.RazaoSocial, " - ",
                                                    emissor.CNPJ);
                        const string body = "Notas fiscais em anexo a esse e-mail. \n\nEsta é uma mensagem automática.";

                        var smtp = new SmtpClient
                        {
                            Host = "smtp.outlook.com",
                            Port = 587,
                            EnableSsl = true,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                        };

                        var data = new Attachment(path, MediaTypeNames.Application.Octet);

                        using (var message = new MailMessage(fromAddress, toAddress)
                        {
                            Subject = subject,
                            Body = body
                        })
                        {
                            message.Attachments.Add(data);
                            smtp.Send(message);
                        }

                        _historicoEnvioContabilidadeRepository.Salvar(new HistoricoEnvioContabilidade
                        {
                            DataEnvio = DateTime.Now, Periodo = periodoStr
                        });
                    }
                    catch (Exception e)
                    {
                        log.Error(e);
                        if (e.GetType() == typeof(SmtpException))
                        {
                            throw new Exception(
                                "Não foi possível enviar o e-mail para a contabilidade, verifique sua conexão com a internet.",
                                e);
                        }

                        if (!_hasAlreadyTried)
                        {
                            await EnviarNotasParaContabilidade(diaParaEnvio);
                        }
                        else
                        {
                            historicoPeriodoCount =
                                await _historicoEnvioContabilidadeRepository.GetHistoricoByPeriodoAsync(
                                    DateTime.Now.AddMonths(-1));

                            if (historicoPeriodoCount == 0)
                            {
                                throw new Exception("Ocorreu um erro ao tentar enviar as notas para a contabilidade.");
                            }
                        }

                        _hasAlreadyTried = true;
                    }
                }
            }));
        }
Exemple #3
0
        public void GerarRelatorioGerencial(List <NotaFiscalEntity> notasPeriodo, List <NotaInutilizadaTO> notasInutilizadas, DateTime mesAno, string startPath)
        {
            Warning[] warnings;
            string[]  streamIds;
            string    mimeType  = string.Empty;
            string    encoding  = string.Empty;
            string    extension = string.Empty;

            var emitenteEntity = _emitenteRepository.GetEmitente();
            var emitente       = new RelatorioGerencial.Emitente()
            {
                RazaoSocial = emitenteEntity.RazaoSocial,
                CNPJ        = emitenteEntity.CNPJ
            };

            IEnumerable <RelatorioGerencial.NotaFiscal> nfceAutorizadas = notasPeriodo
                                                                          .Where(n => n.Modelo.Equals("65") && n.Status == (int)Status.ENVIADA)
                                                                          .Select(nota => new RelatorioGerencial.NotaFiscal()
            {
                ValorDesconto   = nota.ValorDesconto,
                DataAutorizacao = nota.DataAutorizacao,
                DataEmissao     = nota.DataEmissao,
                Modelo          = nota.Modelo,
                Numero          = nota.Numero,
                Protocolo       = nota.Protocolo,
                Serie           = nota.Serie,
                TipoEmissao     = nota.TipoEmissao,
                ValorDespesas   = nota.ValorDespesas,
                ValorFrete      = nota.ValorFrete,
                ValorICMS       = nota.ValorICMS,
                ValorICMSST     = nota.ValorICMSST,
                ValorIPI        = nota.ValorIPI,
                ValorISS        = nota.ValorISS,
                ValorProdutos   = nota.ValorProdutos,
                ValorSeguro     = nota.ValorSeguro,
                ValorServicos   = nota.ValorServicos,
                ValorTotal      = nota.ValorTotal
            });

            IEnumerable <RelatorioGerencial.NotaFiscal> nfeAutorizadas = notasPeriodo.Where(n => n.Modelo.Equals("55") && n.Status == (int)Status.ENVIADA).Select(nota => new RelatorioGerencial.NotaFiscal()
            {
                ValorDesconto   = nota.ValorDesconto,
                DataAutorizacao = nota.DataAutorizacao,
                DataEmissao     = nota.DataEmissao,
                Modelo          = nota.Modelo,
                Numero          = nota.Numero,
                Protocolo       = nota.Protocolo,
                Serie           = nota.Serie,
                TipoEmissao     = nota.TipoEmissao,
                ValorDespesas   = nota.ValorDespesas,
                ValorFrete      = nota.ValorFrete,
                ValorICMS       = nota.ValorICMS,
                ValorICMSST     = nota.ValorICMSST,
                ValorIPI        = nota.ValorIPI,
                ValorISS        = nota.ValorISS,
                ValorProdutos   = nota.ValorProdutos,
                ValorSeguro     = nota.ValorSeguro,
                ValorServicos   = nota.ValorServicos,
                ValorTotal      = nota.ValorTotal
            });

            IEnumerable <RelatorioGerencial.NotaFiscal> nfceCanceladas = notasPeriodo.Where(n => n.Modelo.Equals("65") && n.Status == (int)Status.CANCELADA).Select(nota => new RelatorioGerencial.NotaFiscal()
            {
                Id              = nota.Id,
                ValorDesconto   = nota.ValorDesconto,
                DataAutorizacao = nota.DataAutorizacao,
                DataEmissao     = nota.DataEmissao,
                Modelo          = nota.Modelo,
                Numero          = nota.Numero,
                Protocolo       = nota.Protocolo,
                Serie           = nota.Serie,
                TipoEmissao     = nota.TipoEmissao,
                ValorDespesas   = nota.ValorDespesas,
                ValorFrete      = nota.ValorFrete,
                ValorICMS       = nota.ValorICMS,
                ValorICMSST     = nota.ValorICMSST,
                ValorIPI        = nota.ValorIPI,
                ValorISS        = nota.ValorISS,
                ValorProdutos   = nota.ValorProdutos,
                ValorSeguro     = nota.ValorSeguro,
                ValorServicos   = nota.ValorServicos,
                ValorTotal      = nota.ValorTotal
            });
            IEnumerable <RelatorioGerencial.NotaFiscal> nfeCanceladas = notasPeriodo.Where(n => n.Modelo.Equals("55") && n.Status == (int)Status.CANCELADA).Select(nota => new RelatorioGerencial.NotaFiscal()
            {
                Id              = nota.Id,
                ValorDesconto   = nota.ValorDesconto,
                DataAutorizacao = nota.DataAutorizacao,
                DataEmissao     = nota.DataEmissao,
                Modelo          = nota.Modelo,
                Numero          = nota.Numero,
                Protocolo       = nota.Protocolo,
                Serie           = nota.Serie,
                TipoEmissao     = nota.TipoEmissao,
                ValorDespesas   = nota.ValorDespesas,
                ValorFrete      = nota.ValorFrete,
                ValorICMS       = nota.ValorICMS,
                ValorICMSST     = nota.ValorICMSST,
                ValorIPI        = nota.ValorIPI,
                ValorISS        = nota.ValorISS,
                ValorProdutos   = nota.ValorProdutos,
                ValorSeguro     = nota.ValorSeguro,
                ValorServicos   = nota.ValorServicos,
                ValorTotal      = nota.ValorTotal
            });

            var eventoCancelamentoNFCe = _eventoRepository.GetEventosPorNotasId(nfceCanceladas.Select(n => n.Id));
            var eventoCancelamentoNFe  = _eventoRepository.GetEventosPorNotasId(nfeCanceladas.Select(n => n.Id));

            List <RelatorioGerencial.NotaInutilizada> nfceInutilizadas = notasInutilizadas.Where(n => n.Modelo.Equals(65)).Select(
                nota => new RelatorioGerencial.NotaInutilizada()
            {
                Serie            = nota.Serie,
                Numero           = nota.Numero,
                Protocolo        = nota.Protocolo,
                DataInutilizacao = nota.DataInutilizacao,
                Motivo           = nota.Motivo
            }
                ).ToList();

            List <RelatorioGerencial.NotaInutilizada> nfeInutilizadas = notasInutilizadas.Where(n => n.Modelo.Equals(55)).Select(
                nota => new RelatorioGerencial.NotaInutilizada()
            {
                Serie            = nota.Serie,
                Numero           = nota.Numero,
                Protocolo        = nota.Protocolo,
                DataInutilizacao = nota.DataInutilizacao,
                Motivo           = nota.Motivo
            }
                ).ToList();

            var NFeCanceladas = new List <NotaCancelada>();

            foreach (var nfe in nfeCanceladas)
            {
                var eventoCancelamento = eventoCancelamentoNFe.FirstOrDefault(e => e.NotaId == nfe.Id);

                var nfeCancelada = new NotaCancelada();
                nfeCancelada.DataCancelamento      = eventoCancelamento.DataEvento.ToString();
                nfeCancelada.DataEmissao           = nfe.DataEmissao.ToString();
                nfeCancelada.Numero                = nfe.Numero;
                nfeCancelada.MotivoCancelamento    = eventoCancelamento.MotivoCancelamento;
                nfeCancelada.ProtocoloCancelamento = eventoCancelamento.ProtocoloCancelamento;
                nfeCancelada.Serie = nfe.Serie;

                NFeCanceladas.Add(nfeCancelada);
            }

            List <NotaCancelada> NFCeCanceladas = new List <NotaCancelada>();

            foreach (var nfce in nfceCanceladas)
            {
                var eventoCancelamento = eventoCancelamentoNFCe.FirstOrDefault(e => e.NotaId == nfce.Id);

                var nfceCancelada = new NotaCancelada
                {
                    DataCancelamento      = eventoCancelamento.DataEvento.ToString(),
                    DataEmissao           = nfce.DataEmissao.ToString(),
                    Numero                = nfce.Numero,
                    MotivoCancelamento    = eventoCancelamento.MotivoCancelamento,
                    ProtocoloCancelamento = eventoCancelamento.ProtocoloCancelamento,
                    Serie = nfce.Serie
                };

                NFCeCanceladas.Add(nfceCancelada);
            }

            SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

            ReportViewer reportViewer = new ReportViewer();

            reportViewer.LocalReport.ReleaseSandboxAppDomain();

            ReportDataSource nfceAutorizadasDataSource = new ReportDataSource()
            {
                Name  = "NFCe",
                Value = nfceAutorizadas
            };

            ReportDataSource nfeAutorizadasDataSource = new ReportDataSource()
            {
                Name  = "NFe",
                Value = nfeAutorizadas
            };

            ReportDataSource nfceCanceladasDataSource = new ReportDataSource()
            {
                Name  = "NFCeCanceladas",
                Value = NFCeCanceladas
            };

            ReportDataSource nfeCanceladasDataSource = new ReportDataSource()
            {
                Name  = "NFeCanceladas",
                Value = NFeCanceladas
            };

            ReportDataSource emitenteDataSource = new ReportDataSource()
            {
                Name  = "Emitente",
                Value = new List <Emitente>()
                {
                    emitente
                }
            };

            ReportDataSource periodoDataSource = new ReportDataSource()
            {
                Name  = "Periodo",
                Value = new List <DateTime>()
                {
                    mesAno
                }
            };

            ReportDataSource nfeInutilizadasDataSource = new ReportDataSource()
            {
                Name  = "NFeInutilizadas",
                Value = nfeInutilizadas
            };

            ReportDataSource nfceInutilizadasDataSource = new ReportDataSource()
            {
                Name  = "NFCeInutilizadas",
                Value = nfceInutilizadas
            };

            reportViewer.LocalReport.DataSources.Add(nfceAutorizadasDataSource);
            reportViewer.LocalReport.DataSources.Add(nfeAutorizadasDataSource);
            reportViewer.LocalReport.DataSources.Add(emitenteDataSource);
            reportViewer.LocalReport.DataSources.Add(periodoDataSource);
            reportViewer.LocalReport.DataSources.Add(nfceCanceladasDataSource);
            reportViewer.LocalReport.DataSources.Add(nfeCanceladasDataSource);
            reportViewer.LocalReport.DataSources.Add(nfeInutilizadasDataSource);
            reportViewer.LocalReport.DataSources.Add(nfceInutilizadasDataSource);

            reportViewer.ProcessingMode         = ProcessingMode.Local;
            reportViewer.LocalReport.ReportPath = Path.Combine(Directory.GetCurrentDirectory(), @"Reports\RelatorioGerencial.rdlc");

            byte[] bytes = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

            string pathToPdf = Path.Combine(startPath, "RelatorioGerencial.pdf");

            File.WriteAllBytes(pathToPdf, bytes);
            reportViewer.Dispose();
        }