| | | 1 | | using FAU.Entidades; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace FAU.API.Controllers; |
| | | 9 | | |
| | | 10 | | [ApiController] |
| | | 11 | | [Route("api/liquidacion/reportes-deficit")] |
| | | 12 | | [Authorize] |
| | | 13 | | [Produces("application/json")] |
| | | 14 | | public class ReporteDeficitController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IReporteDeficitService _service; |
| | | 17 | | private readonly IPeriodoService _periodoService; |
| | | 18 | | private readonly ILogger<ReporteDeficitController> _logger; |
| | | 19 | | |
| | 0 | 20 | | public ReporteDeficitController( |
| | 0 | 21 | | IReporteDeficitService service, |
| | 0 | 22 | | IPeriodoService periodoService, |
| | 0 | 23 | | ILogger<ReporteDeficitController> logger) |
| | | 24 | | { |
| | 0 | 25 | | _service = service; |
| | 0 | 26 | | _periodoService = periodoService; |
| | 0 | 27 | | _logger = logger; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | [HttpGet("{periodoId}/por-persona")] |
| | | 31 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 32 | | [ProducesResponseType(typeof(ApiResponse<ReporteDeficitPorPersonaDto>), StatusCodes.Status200OK)] |
| | | 33 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 34 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 35 | | public async Task<IActionResult> GetDeficitPorPersona(long periodoId, CancellationToken ct) |
| | | 36 | | { |
| | | 37 | | try |
| | | 38 | | { |
| | 0 | 39 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 40 | | if (periodo == null) |
| | 0 | 41 | | return NotFound(ApiResponse<string>.ErrorResult("Periodo no encontrado")); |
| | | 42 | | |
| | 0 | 43 | | var result = await _service.ObtenerDeficitPorPersonaAsync(periodoId, ct); |
| | 0 | 44 | | return Ok(ApiResponse<ReporteDeficitPorPersonaDto>.SuccessResult(result)); |
| | | 45 | | } |
| | 0 | 46 | | catch (Exception ex) |
| | | 47 | | { |
| | 0 | 48 | | _logger.LogError(ex, "Error al obtener deficit por persona del periodo {PeriodoId}", periodoId); |
| | 0 | 49 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | [HttpGet("{periodoId}/por-persona/txt")] |
| | | 54 | | [HttpGet("{periodoId}/por-persona/csv")] |
| | | 55 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 56 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 57 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 58 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 59 | | public async Task<IActionResult> GetDeficitPorPersonaTxt(long periodoId, CancellationToken ct) |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 0 | 63 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 64 | | if (periodo == null) |
| | 0 | 65 | | return NotFound(ApiResponse<string>.ErrorResult("Periodo no encontrado")); |
| | | 66 | | |
| | 0 | 67 | | var bytes = await _service.GenerarTxtDeficitPorPersonaAsync(periodoId, periodo.Anio, periodo.Mes, ct); |
| | 0 | 68 | | return File(bytes, "text/plain; charset=ibm850", $"deficit-por-persona-{periodoId}.txt"); |
| | | 69 | | } |
| | 0 | 70 | | catch (Exception ex) |
| | | 71 | | { |
| | 0 | 72 | | _logger.LogError(ex, "Error al generar TXT deficit por persona del periodo {PeriodoId}", periodoId); |
| | 0 | 73 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | [HttpGet("{periodoId}/por-acreedor")] |
| | | 78 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 79 | | [ProducesResponseType(typeof(ApiResponse<ReporteDeficitPorAcreedorDto>), StatusCodes.Status200OK)] |
| | | 80 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 81 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 82 | | public async Task<IActionResult> GetDeficitPorAcreedor(long periodoId, CancellationToken ct) |
| | | 83 | | { |
| | | 84 | | try |
| | | 85 | | { |
| | 0 | 86 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 87 | | if (periodo == null) |
| | 0 | 88 | | return NotFound(ApiResponse<string>.ErrorResult("Periodo no encontrado")); |
| | | 89 | | |
| | 0 | 90 | | var result = await _service.ObtenerDeficitPorAcreedorAsync(periodoId, ct); |
| | 0 | 91 | | return Ok(ApiResponse<ReporteDeficitPorAcreedorDto>.SuccessResult(result)); |
| | | 92 | | } |
| | 0 | 93 | | catch (Exception ex) |
| | | 94 | | { |
| | 0 | 95 | | _logger.LogError(ex, "Error al obtener deficit por acreedor del periodo {PeriodoId}", periodoId); |
| | 0 | 96 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | [HttpGet("{periodoId}/por-acreedor/txt")] |
| | | 101 | | [HttpGet("{periodoId}/por-acreedor/csv")] |
| | | 102 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 103 | | [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] |
| | | 104 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 105 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 106 | | public async Task<IActionResult> GetDeficitPorAcreedorTxt(long periodoId, CancellationToken ct) |
| | | 107 | | { |
| | | 108 | | try |
| | | 109 | | { |
| | 0 | 110 | | var periodo = await _periodoService.ObtenerPorIdAsync(periodoId); |
| | 0 | 111 | | if (periodo == null) |
| | 0 | 112 | | return NotFound(ApiResponse<string>.ErrorResult("Periodo no encontrado")); |
| | | 113 | | |
| | 0 | 114 | | var bytes = await _service.GenerarTxtDeficitPorAcreedorAsync(periodoId, periodo.Anio, periodo.Mes, ct); |
| | 0 | 115 | | return File(bytes, "text/plain; charset=ibm850", $"deficit-por-acreedor-{periodoId}.txt"); |
| | | 116 | | } |
| | 0 | 117 | | catch (Exception ex) |
| | | 118 | | { |
| | 0 | 119 | | _logger.LogError(ex, "Error al generar TXT deficit por acreedor del periodo {PeriodoId}", periodoId); |
| | 0 | 120 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | } |