Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix editar parcelas #53

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions OpenAdm.Api/Controllers/FaturaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public async Task<IActionResult> Criar(FaturaCriarAdmDto faturaCriarAdmDto)
var result = await _faturaService.CriarAdmAsync(faturaCriarAdmDto);
return Ok(result);
}

[HttpGet("get")]
[ProducesResponseType<FaturaViewModel>(200)]
[ProducesResponseType<ErrorResponse>(400)]
public async Task<IActionResult> Get([FromQuery] Guid id)
{
var result = await _faturaService.GetCompletaAsync(id);
return Ok(result);
}
}
31 changes: 31 additions & 0 deletions OpenAdm.Api/Controllers/ParcelaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OpenAdm.Application.Dtos.Response;
using OpenAdm.Application.Interfaces;
using OpenAdm.Application.Models.ContasAReceberModel;
using OpenAdm.Application.Models.FaturasModel;
using OpenAdm.Domain.Enuns;
using OpenAdm.Domain.Model;
using OpenAdm.Infra.Paginacao;
Expand Down Expand Up @@ -68,4 +69,34 @@ public async Task<IActionResult> Edit(FaturaEdit faturaAReceberEdit)
var fatura = await _parcelaService.EditAsync(faturaAReceberEdit);
return Ok(fatura);
}

[HttpPut("editar-parcela")]
[ProducesResponseType<ParcelaViewModel>(200)]
[ProducesResponseType<ErrorResponse>(400)]
public async Task<IActionResult> EditarParcela(ParcelaEditDto parcelaEditDto)
{
var fatura = await _parcelaService.EditarAsync(parcelaEditDto);
return Ok(fatura);
}

[HttpPost("nova-parcela")]
[ProducesResponseType<ParcelaViewModel>(200)]
[ProducesResponseType<ErrorResponse>(400)]
public async Task<IActionResult> NovaParcela(ParcelaCriarDto parcelaCriarDto)
{
var fatura = await _parcelaService.AddAsync(parcelaCriarDto);
return Ok(fatura);
}

[HttpDelete("excluir")]
[ProducesResponseType(200)]
[ProducesResponseType<ErrorResponse>(400)]
public async Task<IActionResult> Excluir([FromQuery] Guid id)
{
var result = await _parcelaService.ExcluirAsync(id);
return Ok(new
{
result
});
}
}
25 changes: 25 additions & 0 deletions OpenAdm.Application/Dtos/FaturasDtos/ParcelaCriarAdmDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenAdm.Domain.Enuns;
using OpenAdm.Domain.Exceptions;

namespace OpenAdm.Application.Dtos.FaturasDtos;

Expand All @@ -11,3 +12,27 @@ public class ParcelaCriarAdmDto
public decimal? Desconto { get; set; }
public string? Observacao { get; set; }
}

public class ParcelaCriarDto
{
public Guid FaturaId { get; set; }
public DateTime? DataDeVencimento { get; set; }
public int NumeroDaFatura { get; set; }
public MeioDePagamentoEnum? MeioDePagamento { get; set; }
public decimal Valor { get; set; }
public decimal? Desconto { get; set; }
public string? Observacao { get; set; }

public void Validar()
{
if (Valor == 0)
{
throw new ExceptionApi("Informe o valor da fatura!");
}

if (Desconto.HasValue && Desconto.Value > Valor)
{
throw new ExceptionApi("O desconto da fatura não pode ser maior que o desconto!");
}
}
}
14 changes: 14 additions & 0 deletions OpenAdm.Application/Dtos/FaturasDtos/ParcelaEditDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using OpenAdm.Domain.Enuns;

namespace OpenAdm.Application.Dtos.FaturasDtos;

public class ParcelaEditDto
{
public Guid Id { get; set; }
public decimal Valor { get; set; }
public decimal? Desconto { get; set; }
public MeioDePagamentoEnum? MeioDePagamento { get; set; }
public StatusParcelaEnum Status { get; set; }
public DateTime DataDeVencimento { get; set; }
public DateTime? DataDePagamento { get; set; }
}
2 changes: 2 additions & 0 deletions OpenAdm.Application/Interfaces/IFaturaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface IFaturaService
Task VerificarFechamentoAsync(Guid id);
Task<PagamentoViewModel> GerarPagamentoAsync(MeioDePagamentoEnum meioDePagamento, Guid pedidoId);
Task<FaturaViewModel> CriarAdmAsync(FaturaCriarAdmDto faturaCriarAdmDto);
Task<FaturaViewModel> GetCompletaAsync(Guid id);
Task<FaturaViewModel> GetByIdAsync(Guid id);
}
3 changes: 3 additions & 0 deletions OpenAdm.Application/Interfaces/IParcelaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public interface IParcelaService
Task<decimal> GetSumAReceberAsync();
Task<IList<ParcelaPagaDashBoardModel>> FaturasDashBoardAsync();
Task<ParcelaViewModel> PagarAsync(PagarParcelaDto pagarFaturaAReceberDto);
Task<ParcelaViewModel> EditarAsync(ParcelaEditDto parcelaEditDto);
Task<ParcelaViewModel> AddAsync(ParcelaCriarDto parcelaCriarDto);
Task<ParcelaViewModel> GetByIdAsync(Guid id);
Task<ParcelaViewModel> EditAsync(FaturaEdit faturaAReceberEdit);
Task<bool> ExcluirAsync(Guid id);
Task<PaginacaoViewModel<ParcelaViewModel>> PaginacaoAsync(PaginacaoParcelaDto paginacaoFaturaAReceberDto);
Task<IList<ParcelaViewModel>> GetByPedidoIdAsync(Guid pedidoId, StatusParcelaEnum? statusFaturaContasAReceberEnum);
}
1 change: 1 addition & 0 deletions OpenAdm.Application/Interfaces/IPedidoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IPedidoService
{
Task<PaginacaoViewModel<PedidoViewModel>> GetPaginacaoAsync(PaginacaoPedidoDto paginacaoPedidoDto);
Task<IList<PedidoViewModel>> GetPedidosEmAbertAsync();
Task<IDictionary<Guid, PedidoViewModel>> GetPedidosAsync(IList<Guid> ids);
Task<List<PedidoViewModel>> GetPedidosUsuarioAsync(int statusPedido, Guid usuarioId);
Task<PedidoViewModel> GetAsync(Guid pedidoId);
Task<byte[]> PedidoProducaoAsync(RelatorioProducaoDto relatorioProducaoDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ParcelaViewModel : BaseViewModel
public DateTime DataDeVencimento { get; set; }
public DateTime? DataDePagamento { get; set; }
public int NumeroDaFatura { get; set; }
public long NumeroDoPedido { get; set; }
public MeioDePagamentoEnum? MeioDePagamento { get; set; }
public decimal Valor { get; set; }
public decimal? Desconto { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions OpenAdm.Application/Services/FaturaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ public async Task<PagamentoViewModel> GerarPagamentoAsync(MeioDePagamentoEnum me
};
}

public async Task<FaturaViewModel> GetByIdAsync(Guid id)
{
var fatura = await _contasAReceberRepository.GetByIdAsync(id)
?? throw new ExceptionApi("Não foi possível localizar a fatura!");
return (FaturaViewModel)fatura;
}

public async Task<FaturaViewModel> GetCompletaAsync(Guid id)
{
var fatura = await _contasAReceberRepository.GetByIdCompletaAsync(id)
?? throw new ExceptionApi("Não foi possível localizar a fatura!");

return (FaturaViewModel)fatura;
}

public async Task VerificarFechamentoAsync(Guid id)
{
var contasAReceber = await _contasAReceberRepository.GetByIdAsync(id)
Expand Down
81 changes: 77 additions & 4 deletions OpenAdm.Application/Services/ParcelaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenAdm.Application.Interfaces.Pedidos;
using OpenAdm.Application.Models.FaturasModel;
using OpenAdm.Application.Models.ParcelasModel;
using OpenAdm.Domain.Entities;
using OpenAdm.Domain.Enuns;
using OpenAdm.Domain.Exceptions;
using OpenAdm.Domain.Extensions;
Expand All @@ -16,17 +17,41 @@ public sealed class ParcelaService : IParcelaService
{
private readonly IParcelaRepository _faturaContasAReceberRepository;
private readonly IFaturaService _contasAReceberService;
private readonly IUpdateStatusPedidoService _pedidoService;
private readonly IUpdateStatusPedidoService _updateStatusPedidoService;
private readonly IPedidoService _pedidoService;
public ParcelaService(
IParcelaRepository faturaContasAReceberRepository,
IFaturaService contasAReceberService,
IUpdateStatusPedidoService pedidoService)
IUpdateStatusPedidoService updateStatusPedidoService,
IPedidoService pedidoService)
{
_faturaContasAReceberRepository = faturaContasAReceberRepository;
_contasAReceberService = contasAReceberService;
_updateStatusPedidoService = updateStatusPedidoService;
_pedidoService = pedidoService;
}

public async Task<ParcelaViewModel> AddAsync(ParcelaCriarDto parcelaCriarDto)
{
parcelaCriarDto.Validar();
var fatura = await _contasAReceberService.GetByIdAsync(parcelaCriarDto.FaturaId);
var proximoNumeroParcela = (fatura.Parcelas.MaxBy(x => x.NumeroDaFatura)?.NumeroDaFatura ?? 0) + 1;

var parcela = Parcela.NovaFatura(
dataDeVencimento: parcelaCriarDto.DataDeVencimento ?? DateTime.Now,
numeroDaFatura: proximoNumeroParcela,
meioDePagamento: parcelaCriarDto.MeioDePagamento,
valor: parcelaCriarDto.Valor,
desconto: parcelaCriarDto.Desconto,
observacao: parcelaCriarDto.Observacao,
faturaId: parcelaCriarDto.FaturaId,
idExterno: null);

await _faturaContasAReceberRepository.AddAsync(parcela);

return (ParcelaViewModel)parcela;
}

public async Task BaixarFaturaWebHookAsync(NotificationFaturaWebHook notificationFaturaWebHook)
{
var fatura = await _faturaContasAReceberRepository.GetByIdExternoAsync(notificationFaturaWebHook.Data.Id);
Expand All @@ -40,7 +65,7 @@ public async Task BaixarFaturaWebHookAsync(NotificationFaturaWebHook notificatio

if (fatura.Fatura != null && fatura.Fatura.PedidoId.HasValue)
{
await _pedidoService.UpdateStatusPedidoAsync(new()
await _updateStatusPedidoService.UpdateStatusPedidoAsync(new()
{
PedidoId = fatura.Fatura.PedidoId.Value,
StatusPedido = StatusPedido.Faturado
Expand All @@ -50,6 +75,25 @@ await _pedidoService.UpdateStatusPedidoAsync(new()
await _contasAReceberService.VerificarFechamentoAsync(fatura.FaturaId);
}

public async Task<ParcelaViewModel> EditarAsync(ParcelaEditDto parcelaEditDto)
{
var parcela = await _faturaContasAReceberRepository.GetByIdAsync(parcelaEditDto.Id)
?? throw new ExceptionApi("Não foi possível localizar a parcela");

parcela.Edit(
status: parcelaEditDto.Status,
dataDeVencimento: parcelaEditDto.DataDeVencimento,
dataDePagamento: parcelaEditDto.DataDePagamento,
meioDePagamento: parcelaEditDto.MeioDePagamento,
valor: parcelaEditDto.Valor,
desconto: parcelaEditDto.Desconto,
observacao: parcela.Observacao);

await _faturaContasAReceberRepository.UpdateAsync(parcela);

return (ParcelaViewModel)parcela;
}

public async Task<ParcelaViewModel> EditAsync(FaturaEdit faturaAReceberEdit)
{
var fatura = await _faturaContasAReceberRepository.GetByIdAsync(faturaAReceberEdit.Id)
Expand All @@ -69,6 +113,16 @@ public async Task<ParcelaViewModel> EditAsync(FaturaEdit faturaAReceberEdit)
return (ParcelaViewModel)fatura;
}

public async Task<bool> ExcluirAsync(Guid id)
{
var parcela = await _faturaContasAReceberRepository.GetByIdAsync(id)
?? throw new ExceptionApi("Não foi possível localizar a parcela");

await _faturaContasAReceberRepository.DeleteAsync(parcela);

return true;
}

public async Task<IList<ParcelaPagaDashBoardModel>> FaturasDashBoardAsync()
{
var faturas = await _faturaContasAReceberRepository.SumTotalMesesAsync(TipoFaturaEnum.A_Receber);
Expand Down Expand Up @@ -124,10 +178,29 @@ public async Task<PaginacaoViewModel<ParcelaViewModel>> PaginacaoAsync(Paginacao
var paginacao = await _faturaContasAReceberRepository
.PaginacaoAsync(paginacaoFaturaAReceberDto);

var pedidosIds = paginacao
.Values
.Where(x => x.Fatura.PedidoId.HasValue)
.Select(x => x.Fatura.PedidoId!.Value)
.ToList();

var pedidos = await _pedidoService.GetPedidosAsync(pedidosIds);
var parcelasViewModel = new List<ParcelaViewModel>();

foreach (var parcela in paginacao.Values)
{
var parcelaViewModel = (ParcelaViewModel)parcela;
if (parcela.Fatura.PedidoId.HasValue && pedidos.TryGetValue(parcela.Fatura.PedidoId.Value, out var pedido))
{
parcelaViewModel.NumeroDoPedido = pedido.Numero;
}
parcelasViewModel.Add(parcelaViewModel);
}

return new PaginacaoViewModel<ParcelaViewModel>()
{
TotalDeRegistros = paginacao.TotalDeRegistros,
Values = paginacao.Values.Select(x => (ParcelaViewModel)x).ToList(),
Values = parcelasViewModel,
TotalPaginas = paginacao.TotalPaginas,
};
}
Expand Down
13 changes: 13 additions & 0 deletions OpenAdm.Application/Services/PedidoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public async Task<PaginacaoViewModel<PedidoViewModel>> GetPaginacaoAsync(Paginac
};
}

public async Task<IDictionary<Guid, PedidoViewModel>> GetPedidosAsync(IList<Guid> ids)
{
var pedidos = await _pedidoRepository.GetPedidosAsync(ids);
var pedidosViewModel = new Dictionary<Guid, PedidoViewModel>();

foreach (var pedido in pedidos)
{
pedidosViewModel.TryAdd(pedido.Key, new PedidoViewModel().ForModelPedidoEmAberto(pedido.Value));
}

return pedidosViewModel;
}

public async Task<IList<PedidoViewModel>> GetPedidosEmAbertAsync()
{
var pedidos = await _pedidoRepository.GetPedidosEmAbertoAsync();
Expand Down
1 change: 1 addition & 0 deletions OpenAdm.Domain/Interfaces/IFaturaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace OpenAdm.Domain.Interfaces;
public interface IFaturaRepository : IGenericRepository<Fatura>
{
Task<Fatura?> GetByIdAsync(Guid id);
Task<Fatura?> GetByIdCompletaAsync(Guid id);
}
1 change: 1 addition & 0 deletions OpenAdm.Domain/Interfaces/IPedidoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public interface IPedidoRepository : IGenericRepository<Pedido>
Task<int> GetQuantidadePorStatusUsuarioAsync(Guid usuarioId, StatusPedido statusPedido);
Task<IList<Pedido>> GetPedidosByRelatorioPorPeriodoAsync(RelatorioPedidoDto relatorioPedidoDto);
Task<IList<Pedido>> GetPedidosEmAbertoAsync();
Task<IDictionary<Guid, Pedido>> GetPedidosAsync(IList<Guid> ids);
Task<int> GetCountPedidosEmAbertoAsync();
}
3 changes: 3 additions & 0 deletions OpenAdm.Infra/Cached/Cached/PedidoCached.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ public async Task<Pedido> UpdateAsync(Pedido entity)

public Task<IList<Pedido>> PaginacaoDropDownAsync(PaginacaoDropDown<Pedido> paginacaoDropDown)
=> _pedidoRepository.PaginacaoDropDownAsync(paginacaoDropDown);

public Task<IDictionary<Guid, Pedido>> GetPedidosAsync(IList<Guid> ids)
=> _pedidoRepository.GetPedidosAsync(ids);
}
11 changes: 11 additions & 0 deletions OpenAdm.Infra/Repositories/FaturaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ public FaturaRepository(ParceiroContext parceiroContext) : base(parceiroContext)
.Include(x => x.Parcelas)
.FirstOrDefaultAsync(x => x.Id == id);
}

public async Task<Fatura?> GetByIdCompletaAsync(Guid id)
{
return await _parceiroContext
.Faturas
.AsNoTracking()
.Include(x => x.Parcelas)
.Include(x => x.Usuario)
.Include(x => x.Pedido)
.FirstOrDefaultAsync(x => x.Id == id);
}
}
9 changes: 9 additions & 0 deletions OpenAdm.Infra/Repositories/PedidoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public async Task<int> GetCountPedidosEmAbertoAsync()
return pedido;
}

public async Task<IDictionary<Guid, Pedido>> GetPedidosAsync(IList<Guid> ids)
{
return await _parceiroContext
.Pedidos
.AsNoTracking()
.Where(x => ids.Contains(x.Id))
.ToDictionaryAsync(x => x.Id, x => x);
}

public async Task<IList<Pedido>> GetPedidosByRelatorioPorPeriodoAsync(RelatorioPedidoDto relatorioPedidoDto)
{
return await _parceiroContext
Expand Down