130 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using GestionIntegral.Api.Dtos.Contables;
 | |
| using GestionIntegral.Api.Services.Contables;
 | |
| using Microsoft.AspNetCore.Authorization;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using Microsoft.Extensions.Logging;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Security.Claims;
 | |
| using System.Threading.Tasks;
 | |
| 
 | |
| namespace GestionIntegral.Api.Controllers.Contables
 | |
| {
 | |
|     [Route("api/pagosdistribuidor")] // Ruta base
 | |
|     [ApiController]
 | |
|     [Authorize]
 | |
|     public class PagosDistribuidorController : ControllerBase
 | |
|     {
 | |
|         private readonly IPagoDistribuidorService _pagoService;
 | |
|         private readonly ILogger<PagosDistribuidorController> _logger;
 | |
| 
 | |
|         // Permisos para Pagos Distribuidores (CP001 a CP004)
 | |
|         private const string PermisoVer = "CP001";
 | |
|         private const string PermisoCrear = "CP002";
 | |
|         private const string PermisoModificar = "CP003";
 | |
|         private const string PermisoEliminar = "CP004";
 | |
| 
 | |
|         public PagosDistribuidorController(IPagoDistribuidorService pagoService, ILogger<PagosDistribuidorController> logger)
 | |
|         {
 | |
|             _pagoService = pagoService;
 | |
|             _logger = logger;
 | |
|         }
 | |
| 
 | |
|         private bool TienePermiso(string codAcc) => User.IsInRole("SuperAdmin") || User.HasClaim(c => c.Type == "permission" && c.Value == codAcc);
 | |
|         private int? GetCurrentUserId()
 | |
|         {
 | |
|             if (int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub"), out int userId)) return userId;
 | |
|             _logger.LogWarning("No se pudo obtener el UserId del token JWT en PagosDistribuidorController.");
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         // GET: api/pagosdistribuidor
 | |
|         [HttpGet]
 | |
|         [ProducesResponseType(typeof(IEnumerable<PagoDistribuidorDto>), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         public async Task<IActionResult> GetAll(
 | |
|             [FromQuery] DateTime? fechaDesde, [FromQuery] DateTime? fechaHasta,
 | |
|             [FromQuery] int? idDistribuidor, [FromQuery] int? idEmpresa,
 | |
|             [FromQuery] string? tipoMovimiento)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoVer)) return Forbid();
 | |
|             var pagos = await _pagoService.ObtenerTodosAsync(fechaDesde, fechaHasta, idDistribuidor, idEmpresa, tipoMovimiento);
 | |
|             return Ok(pagos);
 | |
|         }
 | |
| 
 | |
|         // GET: api/pagosdistribuidor/{idPago}
 | |
|         [HttpGet("{idPago:int}", Name = "GetPagoDistribuidorById")]
 | |
|         [ProducesResponseType(typeof(PagoDistribuidorDto), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> GetById(int idPago)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoVer)) return Forbid();
 | |
|             var pago = await _pagoService.ObtenerPorIdAsync(idPago);
 | |
|             if (pago == null) return NotFound(new { message = $"Pago con ID {idPago} no encontrado." });
 | |
|             return Ok(pago);
 | |
|         }
 | |
| 
 | |
|         // POST: api/pagosdistribuidor
 | |
|         [HttpPost]
 | |
|         [ProducesResponseType(typeof(PagoDistribuidorDto), StatusCodes.Status201Created)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         public async Task<IActionResult> Create([FromBody] CreatePagoDistribuidorDto createDto)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoCrear)) return Forbid();
 | |
|             if (!ModelState.IsValid) return BadRequest(ModelState);
 | |
|             var userId = GetCurrentUserId();
 | |
|             if (userId == null) return Unauthorized();
 | |
| 
 | |
|             var (dto, error) = await _pagoService.CrearAsync(createDto, userId.Value);
 | |
|             if (error != null) return BadRequest(new { message = error });
 | |
|             if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al registrar el pago.");
 | |
|             
 | |
|             return CreatedAtRoute("GetPagoDistribuidorById", new { idPago = dto.IdPago }, dto);
 | |
|         }
 | |
| 
 | |
|         // PUT: api/pagosdistribuidor/{idPago}
 | |
|         [HttpPut("{idPago:int}")]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> Update(int idPago, [FromBody] UpdatePagoDistribuidorDto updateDto)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoModificar)) return Forbid();
 | |
|             if (!ModelState.IsValid) return BadRequest(ModelState);
 | |
|             var userId = GetCurrentUserId();
 | |
|             if (userId == null) return Unauthorized();
 | |
| 
 | |
|             var (exito, error) = await _pagoService.ActualizarAsync(idPago, updateDto, userId.Value);
 | |
|             if (!exito)
 | |
|             {
 | |
|                 if (error == "Pago no encontrado.") return NotFound(new { message = error });
 | |
|                 return BadRequest(new { message = error });
 | |
|             }
 | |
|             return NoContent();
 | |
|         }
 | |
| 
 | |
|         // DELETE: api/pagosdistribuidor/{idPago}
 | |
|         [HttpDelete("{idPago:int}")]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> Delete(int idPago)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoEliminar)) return Forbid();
 | |
|             var userId = GetCurrentUserId();
 | |
|             if (userId == null) return Unauthorized();
 | |
| 
 | |
|             var (exito, error) = await _pagoService.EliminarAsync(idPago, userId.Value);
 | |
|             if (!exito)
 | |
|             {
 | |
|                 if (error == "Pago no encontrado.") return NotFound(new { message = error });
 | |
|                 return BadRequest(new { message = error });
 | |
|             }
 | |
|             return NoContent();
 | |
|         }
 | |
|     }
 | |
| } |