Feat: Implementa flujo completo de facturación y promociones
Este commit introduce la funcionalidad completa para la facturación mensual, la gestión de promociones y la comunicación con el cliente en el módulo de suscripciones. Backend: - Se añade el servicio de Facturación que calcula automáticamente los importes mensuales basándose en las suscripciones activas, días de entrega y precios. - Se implementa el servicio DebitoAutomaticoService, capaz de generar el archivo de texto plano para "Pago Directo Galicia" y de procesar el archivo de respuesta para la conciliación de pagos. - Se desarrolla el ABM completo para Promociones (Servicio, Repositorio, Controlador y DTOs), permitiendo la creación de descuentos por porcentaje o monto fijo. - Se implementa la lógica para asignar y desasignar promociones a suscripciones específicas. - Se añade un servicio de envío de email (EmailService) integrado con MailKit y un endpoint para notificar facturas a los clientes. - Se crea la lógica para registrar pagos manuales (efectivo, tarjeta, etc.) y actualizar el estado de las facturas. - Se añaden todos los permisos necesarios a la base de datos para segmentar el acceso a las nuevas funcionalidades. Frontend: - Se crea la página de Facturación, que permite al usuario seleccionar un período, generar la facturación, listar los resultados y generar el archivo de débito para el banco. - Se implementa la funcionalidad para subir y procesar el archivo de respuesta del banco, actualizando la UI en consecuencia. - Se añade la página completa para el ABM de Promociones. - Se integra un modal en la gestión de suscripciones para asignar y desasignar promociones a un cliente. - Se añade la opción "Enviar Email" en el menú de acciones de las facturas, conectada al nuevo endpoint del backend. - Se completan y corrigen los componentes `PagoManualModal` y `FacturacionPage` para incluir la lógica de registro de pagos y solucionar errores de TypeScript.
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using GestionIntegral.Api.Dtos.Suscripciones;
|
||||
using GestionIntegral.Api.Services.Suscripciones;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
{
|
||||
[Route("api/facturacion")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class FacturacionController : ControllerBase
|
||||
{
|
||||
private readonly IFacturacionService _facturacionService;
|
||||
private readonly ILogger<FacturacionController> _logger;
|
||||
|
||||
// Permiso para generar facturación (a crear en la BD)
|
||||
private const string PermisoGenerarFacturacion = "SU006";
|
||||
|
||||
public FacturacionController(IFacturacionService facturacionService, ILogger<FacturacionController> logger)
|
||||
{
|
||||
_facturacionService = facturacionService;
|
||||
_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/facturacion/{anio}/{mes}
|
||||
[HttpPost("{anio:int}/{mes:int}")]
|
||||
public async Task<IActionResult> GenerarFacturacion(int anio, int mes)
|
||||
{
|
||||
if (!TienePermiso(PermisoGenerarFacturacion)) return Forbid();
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
if (anio < 2020 || mes < 1 || mes > 12)
|
||||
{
|
||||
return BadRequest(new { message = "El año y el mes proporcionados no son válidos." });
|
||||
}
|
||||
|
||||
var (exito, mensaje, facturasGeneradas) = await _facturacionService.GenerarFacturacionMensual(anio, mes, userId.Value);
|
||||
|
||||
if (!exito)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, new { message = mensaje });
|
||||
}
|
||||
|
||||
return Ok(new { message = mensaje, facturasGeneradas });
|
||||
}
|
||||
|
||||
// GET: api/facturacion/{anio}/{mes}
|
||||
[HttpGet("{anio:int}/{mes:int}")]
|
||||
[ProducesResponseType(typeof(IEnumerable<FacturaDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
public async Task<IActionResult> GetFacturas(int anio, int mes)
|
||||
{
|
||||
// Usamos el permiso de generar facturación también para verlas.
|
||||
if (!TienePermiso(PermisoGenerarFacturacion)) return Forbid();
|
||||
|
||||
if (anio < 2020 || mes < 1 || mes > 12)
|
||||
{
|
||||
return BadRequest(new { message = "El período no es válido." });
|
||||
}
|
||||
|
||||
var facturas = await _facturacionService.ObtenerFacturasPorPeriodo(anio, mes);
|
||||
return Ok(facturas);
|
||||
}
|
||||
|
||||
// POST: api/facturacion/{idFactura}/enviar-email
|
||||
[HttpPost("{idFactura:int}/enviar-email")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> EnviarEmail(int idFactura)
|
||||
{
|
||||
// Usaremos un nuevo permiso para esta acción
|
||||
if (!TienePermiso("SU009")) return Forbid();
|
||||
|
||||
var (exito, error) = await _facturacionService.EnviarFacturaPorEmail(idFactura);
|
||||
|
||||
if (!exito)
|
||||
{
|
||||
return BadRequest(new { message = error });
|
||||
}
|
||||
|
||||
return Ok(new { message = "Email enviado a la cola de procesamiento." });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using GestionIntegral.Api.Dtos.Suscripciones;
|
||||
using GestionIntegral.Api.Services.Suscripciones;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
{
|
||||
[Route("api/pagos")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class PagosController : ControllerBase
|
||||
{
|
||||
private readonly IPagoService _pagoService;
|
||||
private readonly ILogger<PagosController> _logger;
|
||||
|
||||
// Permiso para registrar pagos manuales (a crear en BD)
|
||||
private const string PermisoRegistrarPago = "SU008";
|
||||
|
||||
public PagosController(IPagoService pagoService, ILogger<PagosController> 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;
|
||||
return null;
|
||||
}
|
||||
|
||||
// GET: api/facturas/{idFactura}/pagos
|
||||
[HttpGet("~/api/facturas/{idFactura:int}/pagos")]
|
||||
[ProducesResponseType(typeof(IEnumerable<PagoDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
public async Task<IActionResult> GetPagosPorFactura(int idFactura)
|
||||
{
|
||||
// Se podría usar un permiso de "Ver Facturación"
|
||||
if (!TienePermiso("SU006")) return Forbid();
|
||||
|
||||
var pagos = await _pagoService.ObtenerPagosPorFacturaId(idFactura);
|
||||
return Ok(pagos);
|
||||
}
|
||||
|
||||
// POST: api/pagos
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(PagoDto), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
public async Task<IActionResult> RegistrarPago([FromBody] CreatePagoDto createDto)
|
||||
{
|
||||
if (!TienePermiso(PermisoRegistrarPago)) return Forbid();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (dto, error) = await _pagoService.RegistrarPagoManual(createDto, userId.Value);
|
||||
|
||||
if (error != null) return BadRequest(new { message = error });
|
||||
if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al registrar el pago.");
|
||||
|
||||
// No tenemos un "GetById" para pagos, así que devolvemos el objeto con un 201.
|
||||
return StatusCode(201, dto);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using GestionIntegral.Api.Dtos.Suscripciones;
|
||||
using GestionIntegral.Api.Services.Suscripciones;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
{
|
||||
[Route("api/promociones")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class PromocionesController : ControllerBase
|
||||
{
|
||||
private readonly IPromocionService _promocionService;
|
||||
private readonly ILogger<PromocionesController> _logger;
|
||||
|
||||
// Permiso a crear en BD
|
||||
private const string PermisoGestionarPromociones = "SU010";
|
||||
|
||||
public PromocionesController(IPromocionService promocionService, ILogger<PromocionesController> logger)
|
||||
{
|
||||
_promocionService = promocionService;
|
||||
_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;
|
||||
}
|
||||
|
||||
// GET: api/promociones
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll([FromQuery] bool soloActivas = true)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||||
var promociones = await _promocionService.ObtenerTodas(soloActivas);
|
||||
return Ok(promociones);
|
||||
}
|
||||
|
||||
// GET: api/promociones/{id}
|
||||
[HttpGet("{id:int}", Name = "GetPromocionById")]
|
||||
public async Task<IActionResult> GetById(int id)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||||
var promocion = await _promocionService.ObtenerPorId(id);
|
||||
if (promocion == null) return NotFound();
|
||||
return Ok(promocion);
|
||||
}
|
||||
|
||||
// POST: api/promociones
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreatePromocionDto createDto)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (dto, error) = await _promocionService.Crear(createDto, userId.Value);
|
||||
|
||||
if (error != null) return BadRequest(new { message = error });
|
||||
if (dto == null) return StatusCode(500, "Error al crear la promoción.");
|
||||
|
||||
return CreatedAtRoute("GetPromocionById", new { id = dto.IdPromocion }, dto);
|
||||
}
|
||||
|
||||
// PUT: api/promociones/{id}
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<IActionResult> Update(int id, [FromBody] UpdatePromocionDto updateDto)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (exito, error) = await _promocionService.Actualizar(id, updateDto, userId.Value);
|
||||
|
||||
if (!exito)
|
||||
{
|
||||
if (error != null && error.Contains("no encontrada")) return NotFound(new { message = error });
|
||||
return BadRequest(new { message = error });
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
private readonly ILogger<SuscripcionesController> _logger;
|
||||
|
||||
// Permisos (nuevos, a crear en la BD)
|
||||
private const string PermisoGestionarSuscripciones = "SU005";
|
||||
private const string PermisoGestionarSuscripciones = "SU005";
|
||||
|
||||
public SuscripcionesController(ISuscripcionService suscripcionService, ILogger<SuscripcionesController> logger)
|
||||
{
|
||||
@@ -26,7 +26,7 @@ namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -41,7 +41,7 @@ namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
{
|
||||
// Se podría usar el permiso de ver suscriptores (SU001) o el de gestionar suscripciones (SU005)
|
||||
if (!TienePermiso("SU001")) return Forbid();
|
||||
|
||||
|
||||
var suscripciones = await _suscripcionService.ObtenerPorSuscriptorId(idSuscriptor);
|
||||
return Ok(suscripciones);
|
||||
}
|
||||
@@ -62,15 +62,15 @@ namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (dto, error) = await _suscripcionService.Crear(createDto, userId.Value);
|
||||
|
||||
|
||||
if (error != null) return BadRequest(new { message = error });
|
||||
if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al crear la suscripción.");
|
||||
|
||||
|
||||
return CreatedAtRoute("GetSuscripcionById", new { id = dto.IdSuscripcion }, dto);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (exito, error) = await _suscripcionService.Actualizar(id, updateDto, userId.Value);
|
||||
|
||||
|
||||
if (!exito)
|
||||
{
|
||||
if (error != null && error.Contains("no encontrada")) return NotFound(new { message = error });
|
||||
@@ -93,5 +93,46 @@ namespace GestionIntegral.Api.Controllers.Suscripciones
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// GET: api/suscripciones/{idSuscripcion}/promociones
|
||||
[HttpGet("{idSuscripcion:int}/promociones")]
|
||||
public async Task<IActionResult> GetPromocionesAsignadas(int idSuscripcion)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
var promos = await _suscripcionService.ObtenerPromocionesAsignadas(idSuscripcion);
|
||||
return Ok(promos);
|
||||
}
|
||||
|
||||
// GET: api/suscripciones/{idSuscripcion}/promociones-disponibles
|
||||
[HttpGet("{idSuscripcion:int}/promociones-disponibles")]
|
||||
public async Task<IActionResult> GetPromocionesDisponibles(int idSuscripcion)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
var promos = await _suscripcionService.ObtenerPromocionesDisponibles(idSuscripcion);
|
||||
return Ok(promos);
|
||||
}
|
||||
|
||||
// POST: api/suscripciones/{idSuscripcion}/promociones/{idPromocion}
|
||||
[HttpPost("{idSuscripcion:int}/promociones/{idPromocion:int}")]
|
||||
public async Task<IActionResult> AsignarPromocion(int idSuscripcion, int idPromocion)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (exito, error) = await _suscripcionService.AsignarPromocion(idSuscripcion, idPromocion, userId.Value);
|
||||
if (!exito) return BadRequest(new { message = error });
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// DELETE: api/suscripciones/{idSuscripcion}/promociones/{idPromocion}
|
||||
[HttpDelete("{idSuscripcion:int}/promociones/{idPromocion:int}")]
|
||||
public async Task<IActionResult> QuitarPromocion(int idSuscripcion, int idPromocion)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
var (exito, error) = await _suscripcionService.QuitarPromocion(idSuscripcion, idPromocion);
|
||||
if (!exito) return BadRequest(new { message = error });
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user