93 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			93 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | // Archivo: GestionIntegral.Api/Controllers/Suscripciones/DebitosController.cs | ||
|  | 
 | ||
|  | using GestionIntegral.Api.Dtos.Suscripciones; | ||
|  | using GestionIntegral.Api.Services.Suscripciones; | ||
|  | using Microsoft.AspNetCore.Authorization; | ||
|  | using Microsoft.AspNetCore.Mvc; | ||
|  | using System.Security.Claims; | ||
|  | using System.Text; | ||
|  | 
 | ||
|  | namespace GestionIntegral.Api.Controllers.Suscripciones | ||
|  | { | ||
|  |     [Route("api/debitos")] | ||
|  |     [ApiController] | ||
|  |     [Authorize] | ||
|  |     public class DebitosController : ControllerBase | ||
|  |     { | ||
|  |         private readonly IDebitoAutomaticoService _debitoService; | ||
|  |         private readonly ILogger<DebitosController> _logger; | ||
|  | 
 | ||
|  |         // Permiso para generar archivos de débito (a crear en BD) | ||
|  |         private const string PermisoGenerarDebitos = "SU007"; | ||
|  | 
 | ||
|  |         public DebitosController(IDebitoAutomaticoService debitoService, ILogger<DebitosController> logger) | ||
|  |         { | ||
|  |             _debitoService = debitoService; | ||
|  |             _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; | ||
|  |             return null; | ||
|  |         } | ||
|  | 
 | ||
|  |         // POST: api/debitos/{anio}/{mes}/generar-archivo | ||
|  |         [HttpPost("{anio:int}/{mes:int}/generar-archivo")] | ||
|  |         [ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)] | ||
|  |         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
|  |         public async Task<IActionResult> GenerarArchivo(int anio, int mes) | ||
|  |         { | ||
|  |             if (!TienePermiso(PermisoGenerarDebitos)) return Forbid(); | ||
|  | 
 | ||
|  |             var userId = GetCurrentUserId(); | ||
|  |             if (userId == null) return Unauthorized(); | ||
|  | 
 | ||
|  |             var (contenido, nombreArchivo, error) = await _debitoService.GenerarArchivoPagoDirecto(anio, mes, userId.Value); | ||
|  | 
 | ||
|  |             if (error != null) | ||
|  |             { | ||
|  |                 // Si el error es "No se encontraron facturas", es un 404. Otros son 400. | ||
|  |                 if (error.Contains("No se encontraron")) | ||
|  |                 { | ||
|  |                     return NotFound(new { message = error }); | ||
|  |                 } | ||
|  |                 return BadRequest(new { message = error }); | ||
|  |             } | ||
|  | 
 | ||
|  |             if (string.IsNullOrEmpty(contenido) || string.IsNullOrEmpty(nombreArchivo)) | ||
|  |             { | ||
|  |                 return StatusCode(500, new { message = "El servicio no pudo generar el contenido del archivo correctamente." }); | ||
|  |             } | ||
|  | 
 | ||
|  |             // Devolver el archivo para descarga | ||
|  |             var fileBytes = Encoding.UTF8.GetBytes(contenido); | ||
|  |             return File(fileBytes, "text/plain", nombreArchivo); | ||
|  |         } | ||
|  | 
 | ||
|  |         // POST: api/debitos/procesar-respuesta | ||
|  |         [HttpPost("procesar-respuesta")] | ||
|  |         [ProducesResponseType(typeof(ProcesamientoLoteResponseDto), StatusCodes.Status200OK)] | ||
|  |         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
|  |         public async Task<IActionResult> ProcesarArchivoRespuesta(IFormFile archivo) | ||
|  |         { | ||
|  |             // Usamos el mismo permiso de generar débitos para procesar la respuesta. | ||
|  |             if (!TienePermiso(PermisoGenerarDebitos)) return Forbid(); | ||
|  | 
 | ||
|  |             var userId = GetCurrentUserId(); | ||
|  |             if (userId == null) return Unauthorized(); | ||
|  | 
 | ||
|  |             var resultado = await _debitoService.ProcesarArchivoRespuesta(archivo, userId.Value); | ||
|  | 
 | ||
|  |             if (resultado.Errores.Any() && resultado.PagosAprobados == 0 && resultado.PagosRechazados == 0) | ||
|  |             { | ||
|  |                 return BadRequest(resultado); | ||
|  |             } | ||
|  | 
 | ||
|  |             return Ok(resultado); | ||
|  |         } | ||
|  |     } | ||
|  | } |