| | | 1 | | using FAU.API.Security; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using FAU.Logica.Services; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace FAU.API.Controllers; |
| | | 9 | | |
| | | 10 | | [ApiController] |
| | | 11 | | [Route("api/liquidacion/reportes-deficit")] |
| | | 12 | | [Authorize] |
| | | 13 | | public class ReporteDeficitController : ControllerBase |
| | | 14 | | { |
| | | 15 | | private readonly IReporteDeficitService _service; |
| | | 16 | | private readonly IPeriodoService _periodoService; |
| | | 17 | | private readonly ILogger<ReporteDeficitController> _logger; |
| | | 18 | | |
| | 0 | 19 | | public ReporteDeficitController( |
| | 0 | 20 | | IReporteDeficitService service, |
| | 0 | 21 | | IPeriodoService periodoService, |
| | 0 | 22 | | ILogger<ReporteDeficitController> logger) |
| | | 23 | | { |
| | 0 | 24 | | _service = service; |
| | 0 | 25 | | _periodoService = periodoService; |
| | 0 | 26 | | _logger = logger; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | // ─── Déficit por Persona ───────────────────────────────────────────────── |
| | | 30 | | |
| | | 31 | | [HttpGet("{periodoId}/por-persona")] |
| | | 32 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 33 | | public async Task<IActionResult> GetDeficitPorPersona(long periodoId, CancellationToken ct) |
| | | 34 | | { |
| | | 35 | | try |
| | | 36 | | { |
| | 0 | 37 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 38 | | if (periodo == null) |
| | 0 | 39 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 40 | | |
| | 0 | 41 | | var result = await _service.ObtenerDeficitPorPersonaAsync(periodoId, ct); |
| | 0 | 42 | | return Ok(ApiResponse<ReporteDeficitPorPersonaDto>.SuccessResult(result)); |
| | | 43 | | } |
| | 0 | 44 | | catch (Exception ex) |
| | | 45 | | { |
| | 0 | 46 | | _logger.LogError(ex, "Error al obtener déficit por persona del período {PeriodoId}", periodoId); |
| | 0 | 47 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | [HttpGet("{periodoId}/por-persona/csv")] |
| | | 52 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 53 | | public async Task<IActionResult> GetDeficitPorPersonaCsv(long periodoId, CancellationToken ct) |
| | | 54 | | { |
| | | 55 | | try |
| | | 56 | | { |
| | 0 | 57 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 58 | | if (periodo == null) |
| | 0 | 59 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 60 | | |
| | 0 | 61 | | var bytes = await _service.GenerarCsvDeficitPorPersonaAsync(periodoId, ct); |
| | 0 | 62 | | return File(bytes, "text/csv", $"deficit-por-persona-{periodoId}.csv"); |
| | | 63 | | } |
| | 0 | 64 | | catch (Exception ex) |
| | | 65 | | { |
| | 0 | 66 | | _logger.LogError(ex, "Error al generar CSV déficit por persona del período {PeriodoId}", periodoId); |
| | 0 | 67 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 68 | | } |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | // ─── Déficit por Acreedor ──────────────────────────────────────────────── |
| | | 72 | | |
| | | 73 | | [HttpGet("{periodoId}/por-acreedor")] |
| | | 74 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 75 | | public async Task<IActionResult> GetDeficitPorAcreedor(long periodoId, CancellationToken ct) |
| | | 76 | | { |
| | | 77 | | try |
| | | 78 | | { |
| | 0 | 79 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 80 | | if (periodo == null) |
| | 0 | 81 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 82 | | |
| | 0 | 83 | | var result = await _service.ObtenerDeficitPorAcreedorAsync(periodoId, ct); |
| | 0 | 84 | | return Ok(ApiResponse<ReporteDeficitPorAcreedorDto>.SuccessResult(result)); |
| | | 85 | | } |
| | 0 | 86 | | catch (Exception ex) |
| | | 87 | | { |
| | 0 | 88 | | _logger.LogError(ex, "Error al obtener déficit por acreedor del período {PeriodoId}", periodoId); |
| | 0 | 89 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | [HttpGet("{periodoId}/por-acreedor/csv")] |
| | | 94 | | [RequirePermission(PermisoEnum.VerReportesControl)] |
| | | 95 | | public async Task<IActionResult> GetDeficitPorAcreedorCsv(long periodoId, CancellationToken ct) |
| | | 96 | | { |
| | | 97 | | try |
| | | 98 | | { |
| | 0 | 99 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 100 | | if (periodo == null) |
| | 0 | 101 | | return NotFound(ApiResponse<string>.ErrorResult("Período no encontrado")); |
| | | 102 | | |
| | 0 | 103 | | var bytes = await _service.GenerarCsvDeficitPorAcreedorAsync(periodoId, ct); |
| | 0 | 104 | | return File(bytes, "text/csv", $"deficit-por-acreedor-{periodoId}.csv"); |
| | | 105 | | } |
| | 0 | 106 | | catch (Exception ex) |
| | | 107 | | { |
| | 0 | 108 | | _logger.LogError(ex, "Error al generar CSV déficit por acreedor del período {PeriodoId}", periodoId); |
| | 0 | 109 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 110 | | } |
| | 0 | 111 | | } |
| | | 112 | | } |