Example #1
0
        public override async Task <Command> GetAll(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var entities = await MyRepository.Get(null, null, "Endereco");

                var list = entities.ToList();

                if (list.Any())
                {
                    cmd.Cmd = ServerCommands.LogResultOk;
                    var dtos = list.Select(t => t.ConvertDto()).ToList();
                    cmd.Json = await SerializerAsync.SerializeJsonList(dtos);
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <UserPlantaDto>(command.Json);

                if (!string.IsNullOrEmpty(dto.UserId) && dto.PlantaId > 0)
                {
                    var userPlantas = await MyRepository.Get(t => t.PlantaId == dto.PlantaId &&
                                                             t.UserId.Equals(dto.UserId));

                    var userDbSet = Context.Set <UserAccount>();
                    var user      = await userDbSet.FindAsync(dto.UserId);

                    var plantaRep = new Repository <Planta>(Context);
                    var planta    = await plantaRep.GetById(dto.PlantaId);

                    if (userPlantas != null && !userPlantas.Any() && user != null && planta != null)
                    {
                        var entity = new UserPlanta();
                        entity.UpdateEntity(dto);
                        entity.Codigo = dto.UserId + dto.PlantaId + DateTime.Now.ToString();
                        entity.Planta = planta;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.LogResultDeny;
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
Example #3
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <ServicoDto>(command.Json);

                var servicos = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                if (servicos != null && !servicos.Any())
                {
                    var unidadeMedidaRep = new Repository <UnidadeMedida>(Context);
                    var unidadeMedida    = await unidadeMedidaRep.GetById(dto.UnidadeMedidaId);

                    var tipoServicoRep = new Repository <TipoServico>(Context);
                    var tipoServico    = await tipoServicoRep.GetById(dto.TipoServicoId);

                    var centroCustoRep = new Repository <CentroCustoSintetico>(Context);
                    var centroCusto    = await centroCustoRep.GetById(dto.CentroCustoId);

                    if (unidadeMedida != null && tipoServico != null && centroCusto != null)
                    {
                        var entity = new Servico();
                        entity.UpdateEntity(dto);
                        entity.UnidadeMedida = unidadeMedida;
                        entity.TipoServico   = tipoServico;
                        entity.CentroCusto   = centroCusto;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
Example #4
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <TabelaDto>(command.Json);

                var tabelas = await MyRepository.Get(t => t.ServicoId == dto.ServicoId && t.Descricao.Equals(dto.Description));

                if (tabelas != null && !tabelas.Any())
                {
                    var servicoRep = new Repository <Servico>(Context);
                    var servico    = await servicoRep.GetById(dto.ServicoId);

                    if (servico != null)
                    {
                        var entity = new TabelaCusto();
                        entity.UpdateEntity(dto);
                        entity.DataTabela = DateTime.Now;
                        entity.Servico    = servico;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
Example #5
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <TipoEntradaDto>(command.Json);

                var tipos = await MyRepository.Get(t => t.Descricao.Equals(dto.Description));

                if (tipos != null && !tipos.Any())
                {
                    var entity = new TipoEntrada();
                    entity.UpdateEntity(dto);
                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
Example #6
0
        public override async Task <Command> Edit(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <CompraManualDto>(command.Json);

                var compras = await MyRepository.Get(c => c.Id == cmd.EntityId, null, "TipoEntrada,Fornecedor,Itens,Itens.Servico,Itens.Unidade");

                var compraManual = compras.FirstOrDefault();

                if (compraManual != null)
                {
                    compraManual.UpdateEntity(dto);

                    if (compraManual.Itens != null && compraManual.Itens.Count > 0)
                    {
                        compraManual.ValorTotal = 0;
                        foreach (var item in compraManual.Itens)
                        {
                            compraManual.ValorTotal += item.ValorTotal;
                        }
                    }

                    cmd.Cmd  = ServerCommands.LogResultOk;
                    cmd.Json = await SerializerAsync.SerializeJson(true);

                    await MyRepository.Save();
                }
                else
                {
                    cmd.Cmd  = ServerCommands.LogResultDeny;
                    cmd.Json = await SerializerAsync.SerializeJson(false);
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
Example #7
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <UserDadosDto>(command.Json);

                var clientes = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                if (clientes != null && !clientes.Any())
                {
                    var entity = new Cliente();
                    entity.UpdateEntity(dto);

                    var myCmd = new Command();
                    if (entity.EnderecoId <= 0)
                    {
                        var enderecoCmd = new Command {
                            Json = await SerializerAsync.SerializeJson(dto.Endereco)
                        };

                        if (string.IsNullOrEmpty(entity.Endereco.Cep))
                        {
                            myCmd = await LocalizationManager.GetAddress(enderecoCmd);
                        }
                        else
                        {
                            myCmd = await LocalizationManager.GetAddressByZipCode(enderecoCmd);
                        }
                        entity.Endereco.UpdateEntity(await SerializerAsync.DeserializeJson <EnderecoDto>(myCmd.Json));
                        entity.EnderecoId = entity.Endereco.Id;
                    }

                    if (entity.EnderecoId > 0)
                    {
                        entity.Endereco = null;
                    }

                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
Example #8
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <CompraManualDto>(command.Json);

                var compras = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                if (compras != null && !compras.Any())
                {
                    var unidadeMedidaRep = new Repository <UnidadeMedida>(Context);
                    var servicoRep       = new Repository <Servico>(Context);

                    var tipoEntradaRep = new Repository <TipoEntrada>(Context);
                    var tipoEntrada    = await tipoEntradaRep.GetById(dto.TipoEntradaId);

                    var fornecedorRep = new Repository <Fornecedor>(Context);
                    var fornecedor    = await fornecedorRep.GetById(dto.FornecedorId);

                    var entity = new CompraManual();
                    entity.UpdateEntity(dto);

                    if (tipoEntrada != null)
                    {
                        entity.TipoEntrada = tipoEntrada;
                    }

                    if (fornecedor != null)
                    {
                        entity.Fornecedor = fornecedor;
                    }

                    if (entity.DataEmissao == DateTime.MinValue)
                    {
                        entity.DataEmissao = DateTime.Now;
                    }

                    if (String.IsNullOrEmpty(entity.Codigo))
                    {
                        if (!string.IsNullOrEmpty(entity.Fornecedor.NomeFantasia))
                        {
                            entity.Codigo = entity.Fornecedor.NomeFantasia + DateTime.Now.ToString();
                        }
                    }

                    if (entity.Itens != null && entity.Itens.Count > 0)
                    {
                        entity.ValorTotal = 0;
                        foreach (var item in entity.Itens)
                        {
                            var unidadeMedida = await unidadeMedidaRep.GetById(item.UnidadeId);

                            var servico = await servicoRep.GetById(item.ServicoId);

                            if (unidadeMedida != null)
                            {
                                item.Unidade = unidadeMedida;
                            }

                            if (servico != null)
                            {
                                item.Servico = servico;
                                item.Codigo  = servico.Codigo + DateTime.Now.ToString();
                            }

                            entity.ValorTotal += item.ValorTotal;
                        }
                    }

                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <DtoLigacao>(command.Json);

                if (dto.FirstDtoId > 0 && dto.SecondDtoId > 0)
                {
                    var funcionarioEstoques = await MyRepository.Get(t => t.FuncionarioId == dto.FirstDtoId &&
                                                                     t.EstoqueId == dto.SecondDtoId);

                    var funcRep     = new Repository <Funcionario>(Context);
                    var funcionario = await funcRep.GetById(dto.FirstDtoId);

                    var estoqueRep = new Repository <Estoque>(Context);
                    var estoque    = await estoqueRep.GetById(dto.SecondDtoId);

                    if (funcionarioEstoques != null && !funcionarioEstoques.Any() && funcionario != null && estoque != null)
                    {
                        var entity = new FuncionarioEstoque();
                        entity.UpdateEntity(dto);
                        entity.Codigo      = DateTime.Now.ToString();
                        entity.Funcionario = funcionario;
                        entity.Estoque     = estoque;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.LogResultDeny;
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
        public async Task <List <SaldoEstoque> > UpdateSaldoEstoque()
        {
            var saldoList = new List <SaldoEstoque>();

            try
            {
                var updateDate = DateTime.Now;
                var saldoRep   = new Repository <SaldoEstoque>(Context);
                LastDateUpdate = saldoRep.Get(null, o => o.OrderByDescending(s => s.Id)).Result.FirstOrDefault().Data;
                var lastSaldos = await saldoRep.Get(s => s.Data == LastDateUpdate);

                var movimentos = await MyRepository.Get(m => m.Data > LastDateUpdate);

                var servicoRep = new Repository <Servico>(Context);
                var servicos   = await servicoRep.Get(s => s.ControlaEstoque);

                var estoqueRep = new Repository <Estoque>(Context);
                var estoques   = await estoqueRep.Get();

                var servicoList       = servicos.ToList();
                var estoqueList       = estoques.ToList();
                var movimentoEstoques = movimentos.ToList();
                var saldoEstoques     = lastSaldos.ToList();

                if (movimentoEstoques?.Count() > 0)
                {
                    if (servicoList?.Count > 0 && estoqueList?.Count > 0)
                    {
                        foreach (var servico in servicoList)
                        {
                            foreach (var estoque in estoqueList)
                            {
                                var saldo = new SaldoEstoque
                                {
                                    Data      = updateDate,
                                    EstoqueId = estoque.Id,
                                    Estoque   = estoque,
                                    Servico   = servico,
                                    ServicoId = servico.Id,
                                    Codigo    = servico.Codigo + estoque.Codigo + updateDate.ToString()
                                };

                                var lastSaldo = saldoEstoques
                                                .Find(s => s.EstoqueId == estoque.Id && s.ServicoId == servico.Id);

                                if (lastSaldo != null)
                                {
                                    saldo.Quantidade = lastSaldo.Quantidade;
                                }
                                else
                                {
                                    saldo.Quantidade = 0;
                                }

                                var servicoMovimentos = movimentoEstoques
                                                        .FindAll(m => m.ServicoId == servico.Id && m.EstoqueId == estoque.Id);

                                foreach (var movimento in servicoMovimentos)
                                {
                                    int multiplicadorSaldo = 1;

                                    if (!movimento.IsEntrada)
                                    {
                                        multiplicadorSaldo = -1;
                                    }

                                    saldo.Quantidade += movimento.Quantidade * multiplicadorSaldo;
                                }

                                saldoList.Add(saldo);
                            }
                        }
                    }

                    await saldoRep.InsertList(saldoList);

                    await saldoRep.Save();
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
                throw;
            }

            return(saldoList);
        }
        public async Task <SaldoEstoque> GetSaldoEstoque(Servico servico, Estoque estoque)
        {
            try
            {
                var updateDate = DateTime.Now;
                var saldo      = new SaldoEstoque
                {
                    Data       = updateDate,
                    EstoqueId  = estoque.Id,
                    Estoque    = estoque,
                    Servico    = servico,
                    ServicoId  = servico.Id,
                    Codigo     = servico.Codigo + estoque.Codigo + updateDate.ToString(),
                    Quantidade = 0
                };

                var saldoRep     = new Repository <SaldoEstoque>(Context);
                var lastRegister = saldoRep.Get(null, o => o.OrderByDescending(s => s.Id)).Result.FirstOrDefault();

                LastDateUpdate = lastRegister == null ? DateTime.MinValue : lastRegister.Data;

                var lastSaldos = await saldoRep.Get(s =>
                                                    s.Data == LastDateUpdate && s.EstoqueId == estoque.Id && s.ServicoId == servico.Id);

                if (lastSaldos != null)
                {
                    var saldoEstoques = lastSaldos.ToList();

                    var lastSaldo = saldoEstoques?
                                    .Find(s => s.EstoqueId == estoque.Id && s.ServicoId == servico.Id);

                    if (lastSaldo != null)
                    {
                        saldo.Quantidade = lastSaldo.Quantidade;
                    }
                }

                var movimentos = await MyRepository.Get(m =>
                                                        m.Data > LastDateUpdate && m.EstoqueId == estoque.Id && m.ServicoId == servico.Id);

                if (movimentos == null)
                {
                    return(saldo);
                }

                var movimentoEstoques = movimentos.ToList();

                if (movimentoEstoques?.Count > 0)
                {
                    var servicoMovimentos = movimentoEstoques
                                            .FindAll(m => m.ServicoId == servico.Id && m.EstoqueId == estoque.Id);

                    foreach (var movimento in servicoMovimentos)
                    {
                        int multiplicadorSaldo = 1;

                        if (!movimento.IsEntrada)
                        {
                            multiplicadorSaldo = -1;
                        }

                        saldo.Quantidade += movimento.Quantidade * multiplicadorSaldo;
                    }
                }

                return(saldo);
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
                return(null);
            }
        }
        public async Task <bool> VerififyMovimentoEstoque(MovimentoEstoqueDto dto)
        {
            try
            {
                var movimentoEstoques = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                var servicoRep = new Repository <Servico>(Context);
                var servico    = await servicoRep.GetById(dto.ServicoId);

                var tipoEntradaRep = new Repository <TipoEntrada>(Context);
                var tipoEntrada    = await tipoEntradaRep.GetById(dto.TipoEntradaId);

                var estoqueRep = new Repository <Estoque>(Context);
                var estoque    = await estoqueRep.GetById(dto.EstoqueId);

                if (movimentoEstoques == null || movimentoEstoques.Any())
                {
                    return(false);
                }

                if (tipoEntrada == null || estoque == null || servico == null)
                {
                    ConsoleEx.WriteLine(ServerCommands.RequisitoNaoCadastrado);
                    return(false);
                }

                if (!servico.ControlaEstoque)
                {
                    ConsoleEx.WriteLine(ServerCommands.NaoControlaEstoque);
                    return(false);
                }

                var entity = new MovimentoEstoque();
                entity.UpdateEntity(dto);

                entity.Estoque     = estoque;
                entity.Servico     = servico;
                entity.TipoEntrada = tipoEntrada;

                var mySaldo = await GetSaldoEstoque(servico, estoque);

                int multiplicadorSaldo = 1;

                if (!entity.IsEntrada)
                {
                    multiplicadorSaldo = -1;
                }


                if (mySaldo == null || mySaldo.Quantidade + (multiplicadorSaldo * entity.Quantidade) <= 0)
                {
                    ConsoleEx.WriteLine(ServerCommands.SaldoInsuficiente);
                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
                return(false);
            }
        }