129 lines
3.7 KiB
C#
129 lines
3.7 KiB
C#
using line_gestao_api.Data;
|
|
using line_gestao_api.Dtos;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace line_gestao_api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/parcelamentos")]
|
|
[Authorize]
|
|
public class ParcelamentosController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _db;
|
|
|
|
public ParcelamentosController(AppDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<PagedResult<ParcelamentoListDto>>> GetAll(
|
|
[FromQuery] int? anoRef,
|
|
[FromQuery] string? linha,
|
|
[FromQuery] string? cliente,
|
|
[FromQuery] int? competenciaAno,
|
|
[FromQuery] int? competenciaMes,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20)
|
|
{
|
|
page = page < 1 ? 1 : page;
|
|
pageSize = pageSize < 1 ? 20 : pageSize;
|
|
|
|
var query = _db.ParcelamentoLines.AsNoTracking();
|
|
|
|
if (anoRef.HasValue)
|
|
{
|
|
query = query.Where(x => x.AnoRef == anoRef.Value);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(linha))
|
|
{
|
|
var l = linha.Trim();
|
|
query = query.Where(x => x.Linha == l);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(cliente))
|
|
{
|
|
var c = cliente.Trim();
|
|
query = query.Where(x => x.Cliente != null && EF.Functions.ILike(x.Cliente, $"%{c}%"));
|
|
}
|
|
|
|
if (competenciaAno.HasValue && competenciaMes.HasValue)
|
|
{
|
|
var competencia = new DateOnly(competenciaAno.Value, competenciaMes.Value, 1);
|
|
query = query.Where(x => x.MonthValues.Any(m => m.Competencia == competencia));
|
|
}
|
|
|
|
var total = await query.CountAsync();
|
|
|
|
var items = await query
|
|
.OrderBy(x => x.Item)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(x => new ParcelamentoListDto
|
|
{
|
|
Id = x.Id,
|
|
AnoRef = x.AnoRef,
|
|
Item = x.Item,
|
|
Linha = x.Linha,
|
|
Cliente = x.Cliente,
|
|
QtParcelas = x.QtParcelas,
|
|
ParcelaAtual = x.ParcelaAtual,
|
|
TotalParcelas = x.TotalParcelas,
|
|
ValorCheio = x.ValorCheio,
|
|
Desconto = x.Desconto,
|
|
ValorComDesconto = x.ValorComDesconto
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(new PagedResult<ParcelamentoListDto>
|
|
{
|
|
Page = page,
|
|
PageSize = pageSize,
|
|
Total = total,
|
|
Items = items
|
|
});
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<ActionResult<ParcelamentoDetailDto>> GetById(Guid id)
|
|
{
|
|
var item = await _db.ParcelamentoLines
|
|
.AsNoTracking()
|
|
.Include(x => x.MonthValues)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (item == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var dto = new ParcelamentoDetailDto
|
|
{
|
|
Id = item.Id,
|
|
AnoRef = item.AnoRef,
|
|
Item = item.Item,
|
|
Linha = item.Linha,
|
|
Cliente = item.Cliente,
|
|
QtParcelas = item.QtParcelas,
|
|
ParcelaAtual = item.ParcelaAtual,
|
|
TotalParcelas = item.TotalParcelas,
|
|
ValorCheio = item.ValorCheio,
|
|
Desconto = item.Desconto,
|
|
ValorComDesconto = item.ValorComDesconto,
|
|
MonthValues = item.MonthValues
|
|
.OrderBy(x => x.Competencia)
|
|
.Select(x => new ParcelamentoMonthDto
|
|
{
|
|
Competencia = x.Competencia,
|
|
Valor = x.Valor
|
|
})
|
|
.ToList()
|
|
};
|
|
|
|
return Ok(dto);
|
|
}
|
|
}
|