using Gestor.Application.Helpers; using Gestor.Application.Servicos.Generic; using Gestor.Infrastructure.Repository.Interface; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Infrastructure.UnitOfWork.Logic; using Gestor.Model.Common; using Gestor.Model.Domain.Common; using Gestor.Model.Domain.Financeiro; using Gestor.Model.Domain.Financeiro.Relatorios; using Gestor.Model.Domain.Generic; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace Gestor.Application.Servicos.Financeiro { public class FinanceiroServico : BaseServico { public FinanceiroServico() { } public async Task> AddRange(List lancamentos) { int num = 3; base.Sucesso = true; DateTime networkTime = Funcoes.GetNetworkTime(); List lancamentos1 = lancamentos; List lancamentos2 = await Task.Run>(() => { List lancamentos3; while (num > 0) { List registroLogs = new List(); lancamentos = lancamentos1; try { using (UnitOfWork commited = Instancia.Commited) { ControleFinanceiro controle = lancamentos.First().get_Controle(); controle = (controle.get_Id() == 0 ? commited.get_ControleFinanceiroRepository().SaveOrUpdate(controle) : commited.get_ControleFinanceiroRepository().Merge(controle)); lancamentos.ForEach((Lancamento x) => { x.set_Controle(controle); x.set_DataLancamento(new DateTime?(networkTime)); }); lancamentos = commited.get_LancamentoRepository().AddRange(lancamentos); lancamentos.ForEach((Lancamento x) => this.logs.Add(base.CreateLog(x.get_Id(), x.GetValorOriginal(), 25, 0))); base.SaveLog(registroLogs, commited); commited.Commit(); lancamentos3 = lancamentos; } } catch (Exception exception) { num = base.Registrar(exception, 279, num, lancamentos, true); continue; } return lancamentos3; } return lancamentos1; }); return lancamentos2; } public async Task> Atualizar(List lancamentos) { int num = 3; base.Sucesso = true; Funcoes.GetNetworkTime(); List lancamentos1 = lancamentos; List lancamentos2 = await Task.Run>(() => { List lancamentos3; while (num > 0) { List registroLogs = new List(); lancamentos = lancamentos1; try { using (UnitOfWork commited = Instancia.Commited) { lancamentos.ForEach((Lancamento lancamento) => { registroLogs.Add(base.CreateLog(lancamento.get_Id(), lancamento, 25)); lancamento.set_Controle(commited.get_ControleFinanceiroRepository().Merge(lancamento.get_Controle())); lancamento = commited.get_LancamentoRepository().Merge(lancamento); }); base.SaveLog(registroLogs, commited); commited.Commit(); lancamentos3 = lancamentos; } } catch (Exception exception) { num = base.Registrar(exception, 280, num, lancamentos, true); continue; } return lancamentos3; } return lancamentos1; }); return lancamentos2; } public async Task> BuscarFechamento(FiltroFinanceiro filtro) { int num = 3; List lancamentos1 = await Task.Run>(() => { List lancamentos; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { lancamentos = read.get_LancamentoRepository().Fechamento(filtro); } } catch (Exception exception) { num = base.Registrar(exception, 294, num, filtro, true); continue; } return lancamentos; } return new List(); }); return lancamentos1; } public async Task BuscarLancamento(long id, int parcela) { int num = 3; Lancamento lancamento1 = await Task.Run(() => { Lancamento lancamento; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { lancamento = read.get_LancamentoRepository().FindByControle(id, parcela); } } catch (Exception exception) { num = base.Registrar(exception, 163, num, new { id = id, parcela = parcela }, true); continue; } return lancamento; } return new Lancamento(); }); return lancamento1; } public async Task BuscarLancamento(long id) { int num = 3; Lancamento lancamento1 = await Task.Run(() => { Lancamento lancamento; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { lancamento = read.get_LancamentoRepository().FindById(id); } } catch (Exception exception) { num = base.Registrar(exception, 164, num, id, true); continue; } return lancamento; } return new Lancamento(); }); return lancamento1; } public async Task> BuscarLancamentos(DateTime inicio, DateTime fim, StatusLancamento status) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().Find(inicio, fim, status); Func u003cu003e9_41 = FinanceiroServico.u003cu003ec.u003cu003e9__4_1; if (u003cu003e9_41 == null) { u003cu003e9_41 = (Lancamento x) => x.get_Vencimento(); FinanceiroServico.u003cu003ec.u003cu003e9__4_1 = u003cu003e9_41; } list = lancamentos.OrderByDescending(u003cu003e9_41).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 160, num, new { inicio = inicio, fim = fim, status = status }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentos(DateTime inicio, DateTime fim, long conta) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindLancamentosByConta(inicio, fim, conta); Func u003cu003e9_61 = FinanceiroServico.u003cu003ec.u003cu003e9__6_1; if (u003cu003e9_61 == null) { u003cu003e9_61 = (Lancamento x) => x.get_Vencimento(); FinanceiroServico.u003cu003ec.u003cu003e9__6_1 = u003cu003e9_61; } list = lancamentos.OrderByDescending(u003cu003e9_61).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 165, num, new { inicio = inicio, fim = fim, conta = conta }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPersonalizados(DateTime inicio, DateTime fim, StatusLancamento status, FiltroLancamentoData filtrodata) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindPersonalizado(inicio, fim, status, filtrodata); Func u003cu003e9_71 = FinanceiroServico.u003cu003ec.u003cu003e9__7_1; if (u003cu003e9_71 == null) { u003cu003e9_71 = (Lancamento x) => x.get_Vencimento(); FinanceiroServico.u003cu003ec.u003cu003e9__7_1 = u003cu003e9_71; } list = lancamentos.OrderByDescending(u003cu003e9_71).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 160, num, new { inicio = inicio, fim = fim, status = status }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPorBaixa(DateTime inicio, DateTime fim) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindByBaixa(inicio, fim); Func u003cu003e9_81 = FinanceiroServico.u003cu003ec.u003cu003e9__8_1; if (u003cu003e9_81 == null) { u003cu003e9_81 = (Lancamento x) => x.get_Baixa(); FinanceiroServico.u003cu003ec.u003cu003e9__8_1 = u003cu003e9_81; } list = lancamentos.OrderByDescending(u003cu003e9_81).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 161, num, new { inicio = inicio, fim = fim }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPorConta(DateTime inicio, DateTime fim, long id) { int num = 3; List extratoContas1 = await Task.Run>(() => { List extratoContas; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { extratoContas = read.get_LancamentoRepository().FindByConta(inicio, fim, id); } } catch (Exception exception) { num = base.Registrar(exception, 165, num, new { inicio = inicio, fim = fim, id = id }, true); continue; } return extratoContas; } return new List(); }); return extratoContas1; } public async Task> BuscarLancamentosPorControle(long id) { int num = 3; List lancamentos1 = await Task.Run>(() => { List lancamentos; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { lancamentos = read.get_LancamentoRepository().FindByControle(id, 2); } } catch (Exception exception) { num = base.Registrar(exception, 158, num, new { id = id }, true); continue; } return lancamentos; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPorFornecedor(long id, StatusLancamento status) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindByFornecedor(id, status); Func u003cu003e9_11 = FinanceiroServico.u003cu003ec.u003cu003e9__1_1; if (u003cu003e9_11 == null) { u003cu003e9_11 = (Lancamento x) => x.get_Vencimento(); FinanceiroServico.u003cu003ec.u003cu003e9__1_1 = u003cu003e9_11; } list = lancamentos.OrderByDescending(u003cu003e9_11).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 157, num, new { id = id, status = status }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPorFornecedor(long id, DateTime date, Sinal sinal) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindByFornecedor(id, date, sinal); Func u003cu003e9_21 = FinanceiroServico.u003cu003ec.u003cu003e9__2_1; if (u003cu003e9_21 == null) { u003cu003e9_21 = (Lancamento x) => x.get_Vencimento(); FinanceiroServico.u003cu003ec.u003cu003e9__2_1 = u003cu003e9_21; } list = lancamentos.OrderByDescending(u003cu003e9_21).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 157, num, new { id = id, date = date }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPorLancamento(DateTime inicio, DateTime fim, StatusLancamento status) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindByLancamento(inicio, fim, status); Func u003cu003e9_51 = FinanceiroServico.u003cu003ec.u003cu003e9__5_1; if (u003cu003e9_51 == null) { u003cu003e9_51 = (Lancamento x) => x.get_DataLancamento(); FinanceiroServico.u003cu003ec.u003cu003e9__5_1 = u003cu003e9_51; } list = lancamentos.OrderByDescending(u003cu003e9_51).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 329, num, new { inicio = inicio, fim = fim, status = status }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task> BuscarLancamentosPorPagamento(DateTime inicio, DateTime fim) { int num = 3; List lancamentos1 = await Task.Run>(() => { List list; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { List lancamentos = read.get_LancamentoRepository().FindByPagamento(inicio, fim); Func u003cu003e9_91 = FinanceiroServico.u003cu003ec.u003cu003e9__9_1; if (u003cu003e9_91 == null) { u003cu003e9_91 = (Lancamento x) => x.get_Pagamento(); FinanceiroServico.u003cu003ec.u003cu003e9__9_1 = u003cu003e9_91; } list = lancamentos.OrderBy(u003cu003e9_91).ToList(); } } catch (Exception exception) { num = base.Registrar(exception, 162, num, new { inicio = inicio, fim = fim }, true); continue; } return list; } return new List(); }); return lancamentos1; } public async Task Excluir(List lancamentos) { int num = 3; Funcoes.GetNetworkTime(); bool flag1 = await Task.Run(() => { bool flag; while (num > 0) { List registroLogs = new List(); try { using (UnitOfWork commited = Instancia.Commited) { lancamentos.ForEach((Lancamento x) => { registroLogs.Add(base.CreateLog(x.get_Id(), x.GetValorOriginal(), 25, 2)); if (commited.get_LancamentoRepository().FindByControle(x.get_Controle().get_Id(), 2).Count == 1) { commited.get_ControleFinanceiroRepository().Delete(x.get_Controle().get_Id()); return; } x.get_Controle().set_Parcelas(x.get_Controle().get_Parcelas() - 1); commited.get_ControleFinanceiroRepository().Merge(x.get_Controle()); commited.get_LancamentoRepository().Delete(x.get_Id()); }); base.SaveLog(registroLogs, commited); commited.Commit(); } flag = true; } catch (Exception exception) { num = base.Registrar(exception, 282, num, lancamentos, true); continue; } return flag; } return false; }); return flag1; } public async Task Excluir(Lancamento lancamento) { int num = 3; Funcoes.GetNetworkTime(); bool flag1 = await Task.Run(() => { bool flag; while (num > 0) { List registroLogs = new List(); try { using (UnitOfWork commited = Instancia.Commited) { registroLogs.Add(base.CreateLog(lancamento.get_Id(), lancamento.GetValorOriginal(), 25, 2)); if (commited.get_LancamentoRepository().FindByControle(lancamento.get_Controle().get_Id(), 2).Count != 1) { lancamento.get_Controle().set_Parcelas(lancamento.get_Controle().get_Parcelas() - 1); commited.get_ControleFinanceiroRepository().Merge(lancamento.get_Controle()); commited.get_LancamentoRepository().Delete(lancamento.get_Id()); } else { commited.get_ControleFinanceiroRepository().Delete(lancamento.get_Controle().get_Id()); } base.SaveLog(registroLogs, commited); commited.Commit(); flag = true; } } catch (Exception exception) { num = base.Registrar(exception, 282, num, lancamento, true); continue; } return flag; } return false; }); return flag1; } public async Task> IncluirRange(List lancamentos) { int num = 3; base.Sucesso = true; Funcoes.GetNetworkTime(); List lancamentos1 = lancamentos; List lancamentos2 = await Task.Run>(() => { List lancamentos3; while (num > 0) { List registroLogs = new List(); lancamentos = lancamentos1; try { using (UnitOfWork commited = Instancia.Commited) { lancamentos.ForEach((Lancamento lancamento) => { lancamento.set_Controle(commited.get_ControleFinanceiroRepository().Merge(lancamento.get_Controle())); lancamento = commited.get_LancamentoRepository().SaveOrUpdate(lancamento); registroLogs.Add(base.CreateLog(lancamento.get_Id(), lancamento.GetValorOriginal(), 25, 0)); }); base.SaveLog(registroLogs, commited); commited.Commit(); lancamentos3 = lancamentos; } } catch (Exception exception) { num = base.Registrar(exception, 281, num, lancamentos, true); continue; } return lancamentos3; } return lancamentos1; }); return lancamentos2; } public async Task Save(Lancamento lancamento) { int num = 3; base.Sucesso = true; DateTime networkTime = Funcoes.GetNetworkTime(); Lancamento lancamento1 = lancamento; Lancamento lancamento2 = await Task.Run(() => { Lancamento lancamento3; bool flag; while (num > 0) { List registroLogs = new List(); lancamento = lancamento1; try { using (UnitOfWork commited = Instancia.Commited) { flag = (lancamento.get_Id() == 0 ? false : true); if (!flag) { lancamento.set_DataLancamento(new DateTime?(networkTime)); } if (flag) { registroLogs.Add(base.CreateLog(lancamento.get_Id(), lancamento, 25)); } ControleFinanceiro controle = lancamento.get_Controle(); controle = (controle.get_Id() == 0 ? commited.get_ControleFinanceiroRepository().SaveOrUpdate(controle) : commited.get_ControleFinanceiroRepository().Merge(controle)); lancamento.set_Controle(controle); lancamento = (lancamento.get_Id() == 0 ? commited.get_LancamentoRepository().SaveOrUpdate(lancamento) : commited.get_LancamentoRepository().Merge(lancamento)); if (!flag) { registroLogs.Add(base.CreateLog(lancamento.get_Id(), lancamento.GetValorOriginal(), 25, 0)); } base.SaveLog(registroLogs, commited); commited.Commit(); lancamento3 = lancamento; } } catch (Exception exception) { num = base.Registrar(exception, 279, num, lancamento, true); continue; } return lancamento3; } return lancamento1; }); return lancamento2; } public async Task TemLancamentosPorFornecedor(long id) { int num = 3; bool flag1 = await Task.Run(() => { bool flag; while (num > 0) { try { using (UnitOfWork read = Instancia.Read) { flag = read.get_LancamentoRepository().HasByFornecedor(id); } } catch (Exception exception) { num = base.Registrar(exception, 154, num, id, true); continue; } return flag; } return false; }); return flag1; } public async Task Transferir(Lancamento lancamentoOrigem, Lancamento lancamentoDestino) { int num = 3; Lancamento lancamento = lancamentoOrigem; Lancamento lancamento1 = lancamentoDestino; Funcoes.GetNetworkTime(); bool flag1 = await Task.Run(() => { bool flag; while (num > 0) { List registroLogs = new List(); lancamentoOrigem = lancamento; lancamentoDestino = lancamento1; try { using (UnitOfWork commited = Instancia.Commited) { ControleFinanceiro controle = lancamentoOrigem.get_Controle(); controle = commited.get_ControleFinanceiroRepository().SaveOrUpdate(controle); lancamentoOrigem.set_Controle(controle); lancamentoDestino.set_Controle(controle); lancamentoOrigem = commited.get_LancamentoRepository().SaveOrUpdate(lancamentoOrigem); lancamentoDestino = commited.get_LancamentoRepository().SaveOrUpdate(lancamentoDestino); registroLogs.Add(base.CreateLog(lancamentoOrigem.get_Id(), lancamentoOrigem.GetValorOriginal(), 25, 0)); registroLogs.Add(base.CreateLog(lancamentoDestino.get_Id(), lancamentoDestino.GetValorOriginal(), 25, 0)); base.SaveLog(registroLogs, commited); commited.Commit(); flag = true; } } catch (Exception exception) { num = base.Registrar(exception, 283, num, new { lancamentoOrigem = lancamentoOrigem, lancamentoDestino = lancamentoDestino }, true); continue; } return flag; } return false; }); return flag1; } } }