Skip to content

Commit

Permalink
Merge pull request #66 from Brunobento1990/develop
Browse files Browse the repository at this point in the history
hotfix movimentação de estoque
  • Loading branch information
Brunobento1990 authored Feb 9, 2025
2 parents 3f072ee + 8e034ad commit 6e44854
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 20 deletions.
2 changes: 2 additions & 0 deletions OpenAdm.Application/Interfaces/IEstoqueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace OpenAdm.Application.Interfaces;
public interface IEstoqueService
{
Task<bool> MovimentacaoDeProdutoAsync(MovimentacaoDeProdutoDto movimentacaoDeProdutoDto);
Task<bool> MovimentacaoDePedidoEntregueAsync(IList<ItemPedido> itens);
Task<PaginacaoViewModel<EstoqueViewModel>> GetPaginacaoAsync(FilterModel<Estoque> paginacaoEstoqueDto);
Task<bool> UpdateEstoqueAsync(UpdateEstoqueDto updateEstoqueDto);
Task<EstoqueViewModel> GetEstoqueViewModelAsync(Guid id);
Task<IList<EstoqueViewModel>> GetPosicaoDeEstoqueAsync();
}
1 change: 1 addition & 0 deletions OpenAdm.Application/Models/Estoques/EstoqueViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public EstoqueViewModel ToModel(Estoque estoque, string? produto, string? tamanh
Produto = produto;
Quantidade = estoque.Quantidade;
ProdutoId = estoque.ProdutoId;
Numero = estoque.Numero;

return this;
}
Expand Down
4 changes: 3 additions & 1 deletion OpenAdm.Application/Models/Home/HomeAdmViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using OpenAdm.Application.Models.MovimentacaoDeProdutos;
using OpenAdm.Application.Models.Estoques;
using OpenAdm.Application.Models.MovimentacaoDeProdutos;
using OpenAdm.Application.Models.ParcelasModel;
using OpenAdm.Application.Models.TopUsuarios;

namespace OpenAdm.Application.Models.Home;

public class HomeAdmViewModel
{
public IList<EstoqueViewModel> PosicaoDeEstoques { get; set; } = [];
public IList<TopUsuariosViewModel> TopUsuariosTotalCompra { get; set; } = [];
public IList<TopUsuariosViewModel> TopUsuariosTotalPedido { get; set; } = [];
public IList<MovimentoDeProdutoDashBoardModel> Movimentos { get; set; } = [];
Expand Down
90 changes: 90 additions & 0 deletions OpenAdm.Application/Services/EstoqueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,96 @@ public async Task<PaginacaoViewModel<EstoqueViewModel>> GetPaginacaoAsync(Filter
};
}

public async Task<IList<EstoqueViewModel>> GetPosicaoDeEstoqueAsync()
{
var estoquesViewModel = new List<EstoqueViewModel>();

var estoques = await _estoqueRepository.GetPosicaoEstoqueAsync();

foreach (var estoque in estoques)
{
var produto = await _produtoRepository.GetProdutoByIdAsync(estoque.ProdutoId);
var peso = string.Empty;
var tamanho = string.Empty;

if (estoque.TamanhoId != null)
{
var tamanhoDb = await _tamanhoRepository.GetTamanhoByIdAsync(estoque.TamanhoId.Value);
tamanho = tamanhoDb?.Descricao;
}

if (estoque.PesoId != null)
{
var pesoDb = await _pesoRepository.GetPesoByIdAsync(estoque.PesoId.Value);
peso = pesoDb?.Descricao;
}

estoquesViewModel.Add(new EstoqueViewModel().ToModel(estoque, produto?.Descricao, tamanho, peso));
}

return estoquesViewModel;
}

public async Task<bool> MovimentacaoDePedidoEntregueAsync(IList<ItemPedido> itens)
{
var estoques = new List<Estoque>();

foreach (var item in itens)
{
var estoque = estoques
.FirstOrDefault(x => x.ProdutoId == item.ProdutoId &&
x.PesoId == item.PesoId &&
x.TamanhoId == item.TamanhoId);

if (estoque == null)
{
estoque = await GetEstoqueLocalAsync(
item.ProdutoId,
item.PesoId,
item.TamanhoId);

if (estoque == null)
{
estoque ??= new Estoque(
Guid.NewGuid(),
DateTime.Now,
DateTime.Now,
0,
item.ProdutoId,
-item.Quantidade,
item.TamanhoId,
item.PesoId);
}
else
{
estoque.UpdateEstoque(item.Quantidade, TipoMovimentacaoDeProduto.Saida);
}

estoques.Add(estoque);
continue;
}

estoque.UpdateEstoque(item.Quantidade, TipoMovimentacaoDeProduto.Saida);
}

var estoquesAdd = estoques.Where(x => x.Numero == 0).ToList();
var estoquesUpdate = estoques.Where(x => x.Numero > 0).ToList();

if (estoquesUpdate.Count > 0)
{
_estoqueRepository.UpdateRange(estoquesUpdate);
}

if (estoquesAdd.Count > 0)
{
await _estoqueRepository.AddRangeAsync(estoquesAdd);
}

await _estoqueRepository.SaveChangesAsync();

return true;
}

public async Task<bool> MovimentacaoDeProdutoAsync(MovimentacaoDeProdutoDto movimentacaoDeProdutoDto)
{
var estoque = await GetEstoqueLocalAsync(
Expand Down
10 changes: 7 additions & 3 deletions OpenAdm.Application/Services/HomeSevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ public class HomeSevice : IHomeSevice
private readonly IPedidoRepository _pedidoRepository;
private readonly IAcessoEcommerceService _acessoEcommerceService;
private readonly IUsuarioRepository _usuarioRepository;
private readonly IEstoqueService _estoqueService;

public HomeSevice(
ITopUsuariosRepository topUsuariosRepository,
IMovimentacaoDeProdutosService movimentacaoDeProdutosService,
IParcelaService faturaContasAReceberService,
IPedidoRepository pedidoRepository,
IAcessoEcommerceService acessoEcommerceService,
IUsuarioRepository usuarioRepository)
IUsuarioRepository usuarioRepository,
IEstoqueService estoqueService)
{
_topUsuariosRepository = topUsuariosRepository;
_movimentacaoDeProdutosService = movimentacaoDeProdutosService;
_faturaContasAReceberService = faturaContasAReceberService;
_pedidoRepository = pedidoRepository;
_acessoEcommerceService = acessoEcommerceService;
_usuarioRepository = usuarioRepository;
_estoqueService = estoqueService;
}

public async Task<HomeAdmViewModel> GetHomeAdmAsync()
Expand All @@ -40,6 +43,7 @@ public async Task<HomeAdmViewModel> GetHomeAdmAsync()
var pedidosEmAberto = await _pedidoRepository.GetCountPedidosEmAbertoAsync();
var quantidadeDeAcessoEcommerce = await _acessoEcommerceService.QuantidadeDeAcessoAsync();
var quantidadeDeUsuario = await _usuarioRepository.GetCountAsync();
var estoques = await _estoqueService.GetPosicaoDeEstoqueAsync();

return new HomeAdmViewModel()
{
Expand All @@ -50,8 +54,8 @@ public async Task<HomeAdmViewModel> GetHomeAdmAsync()
TotalAReceber = totalAReceber,
PedidosEmAberto = pedidosEmAberto,
QuantidadeDeAcessoEcommerce = quantidadeDeAcessoEcommerce,
QuantidadeDeUsuario
= quantidadeDeUsuario
QuantidadeDeUsuario = quantidadeDeUsuario,
PosicaoDeEstoques = estoques
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ public class UpdateStatusPedidoService : IUpdateStatusPedidoService
private readonly IProdutosMaisVendidosService _produtosMaisVendidosService;
private readonly ITopUsuarioService _topUsuarioService;
private readonly IMovimentacaoDeProdutosService _movimentacaoDeProdutosService;
private readonly IEstoqueService _estoqueService;

public UpdateStatusPedidoService(
IPedidoRepository pedidoRepository,
IProdutosMaisVendidosService produtosMaisVendidosService,
ITopUsuarioService topUsuarioService,
IMovimentacaoDeProdutosService movimentacaoDeProdutosService)
IMovimentacaoDeProdutosService movimentacaoDeProdutosService,
IEstoqueService estoqueService)
{
_pedidoRepository = pedidoRepository;
_produtosMaisVendidosService = produtosMaisVendidosService;
_topUsuarioService = topUsuarioService;
_movimentacaoDeProdutosService = movimentacaoDeProdutosService;
_estoqueService = estoqueService;
}

public async Task<PedidoViewModel> UpdateStatusPedidoAsync(UpdateStatusPedidoDto updateStatusPedidoDto)
Expand All @@ -40,6 +43,7 @@ public async Task<PedidoViewModel> UpdateStatusPedidoAsync(UpdateStatusPedidoDto
await _produtosMaisVendidosService.ProcessarAsync(pedido);
await _topUsuarioService.AddOrUpdateTopUsuarioAsync(pedido);
await _movimentacaoDeProdutosService.MovimentarItensPedidoAsync(pedido.ItensPedido);
await _estoqueService.MovimentacaoDePedidoEntregueAsync(pedido.ItensPedido);
}

return new PedidoViewModel().ForModel(pedido);
Expand Down
2 changes: 2 additions & 0 deletions OpenAdm.Domain/Entities/Estoque.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public void UpdateEstoque(decimal quantidade, TipoMovimentacaoDeProduto tipoMovi
{
Quantidade -= quantidade;
}

DataDeAtualizacao = DateTime.Now;
}

public Guid ProdutoId { get; private set; }
Expand Down
3 changes: 3 additions & 0 deletions OpenAdm.Domain/Interfaces/IEstoqueRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ namespace OpenAdm.Domain.Interfaces;
public interface IEstoqueRepository : IGenericRepository<Estoque>
{
Task<Estoque?> GetEstoqueAsync(Expression<Func<Estoque, bool>> where);
Task<IList<Estoque>> GetPosicaoEstoqueAsync();
Task AddRangeAsync(IList<Estoque> entities);
void UpdateRange(IList<Estoque> entities);
}
25 changes: 10 additions & 15 deletions OpenAdm.Infra/Repositories/EstoqueRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,30 @@ public EstoqueRepository(ParceiroContext parceiroContext) : base(parceiroContext
{
}

public async Task<Estoque?> GetEstoqueAsync(Expression<Func<Estoque, bool>> where)
public async Task AddRangeAsync(IList<Estoque> entities)
{
return await ParceiroContext
.Estoques
.AsNoTracking()
.FirstOrDefaultAsync(where);
await ParceiroContext.AddRangeAsync(entities);
}

public async Task<Estoque?> GetEstoqueByProdutoIdAndPesoIdAsync(Guid produtoId, Guid pesoId)
public async Task<Estoque?> GetEstoqueAsync(Expression<Func<Estoque, bool>> where)
{
return await ParceiroContext
.Estoques
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProdutoId == produtoId && x.PesoId == pesoId);
.FirstOrDefaultAsync(where);
}

public async Task<Estoque?> GetEstoqueByProdutoIdAndTamanhoIdAsync(Guid produtoId, Guid tamanhoId)
public async Task<IList<Estoque>> GetPosicaoEstoqueAsync()
{
return await ParceiroContext
.Estoques
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProdutoId == produtoId && x.TamanhoId == tamanhoId);
.OrderByDescending(x => x.DataDeAtualizacao)
.Take(3)
.ToListAsync();
}

public async Task<Estoque?> GetEstoqueByProdutoIdAsync(Guid produtoId)
public void UpdateRange(IList<Estoque> entities)
{
return await ParceiroContext
.Estoques
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProdutoId == produtoId);
ParceiroContext.UpdateRange(entities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ public async Task<IList<MovimentacaoDeProduto>> RelatorioAsync(

return await query.ToListAsync();
}

}

0 comments on commit 6e44854

Please sign in to comment.