diff --git a/Backend/GestionIntegral.Api/Controllers/Reportes/PdfTemplates/DistribucionSuscripcionesDocument.cs b/Backend/GestionIntegral.Api/Controllers/Reportes/PdfTemplates/DistribucionSuscripcionesDocument.cs new file mode 100644 index 0000000..7753632 --- /dev/null +++ b/Backend/GestionIntegral.Api/Controllers/Reportes/PdfTemplates/DistribucionSuscripcionesDocument.cs @@ -0,0 +1,112 @@ +using GestionIntegral.Api.Dtos.Reportes.ViewModels; +using QuestPDF.Fluent; +using QuestPDF.Helpers; +using QuestPDF.Infrastructure; + +namespace GestionIntegral.Api.Controllers.Reportes.PdfTemplates +{ + public class DistribucionSuscripcionesDocument : IDocument + { + public DistribucionSuscripcionesViewModel Model { get; } + + public DistribucionSuscripcionesDocument(DistribucionSuscripcionesViewModel model) + { + Model = model; + } + + public DocumentMetadata GetMetadata() => DocumentMetadata.Default; + + public void Compose(IDocumentContainer container) + { + container.Page(page => + { + page.Margin(1, Unit.Centimetre); + page.DefaultTextStyle(x => x.FontFamily("Arial").FontSize(9)); + page.Header().Element(ComposeHeader); + page.Content().Element(ComposeContent); + page.Footer().AlignCenter().Text(x => { x.Span("Página "); x.CurrentPageNumber(); }); + }); + } + + void ComposeHeader(IContainer container) + { + container.Column(column => + { + column.Item().Row(row => + { + row.RelativeItem().Column(col => + { + col.Item().Text("Reporte de Distribución de Suscripciones").SemiBold().FontSize(14); + col.Item().Text($"Período: {Model.FechaDesde} al {Model.FechaHasta}").FontSize(11); + }); + row.ConstantItem(150).AlignRight().Text($"Generado: {Model.FechaGeneracion}"); + }); + column.Item().PaddingTop(5).BorderBottom(1).BorderColor(Colors.Grey.Lighten2); + }); + } + + void ComposeContent(IContainer container) + { + container.PaddingTop(10).Column(column => + { + column.Spacing(20); + foreach (var empresa in Model.DatosAgrupados) + { + column.Item().Element(c => ComposeEmpresa(c, empresa)); + } + }); + } + + void ComposeEmpresa(IContainer container, GrupoEmpresa empresa) + { + container.Column(column => + { + column.Item().Background(Colors.Grey.Lighten2).Padding(5).Text(empresa.NombreEmpresa).Bold().FontSize(12); + column.Item().Column(colPub => + { + colPub.Spacing(10); + foreach (var publicacion in empresa.Publicaciones) + { + colPub.Item().Element(c => ComposePublicacion(c, publicacion)); + } + }); + }); + } + + void ComposePublicacion(IContainer container, GrupoPublicacion publicacion) + { + container.Table(table => + { + table.ColumnsDefinition(columns => + { + columns.RelativeColumn(2.5f); // Nombre + columns.RelativeColumn(3); // Dirección + columns.RelativeColumn(1.5f); // Teléfono + columns.RelativeColumn(1.5f); // Días + columns.RelativeColumn(2.5f); // Observaciones + }); + + table.Header(header => + { + header.Cell().ColumnSpan(5).Background(Colors.Grey.Lighten4).Padding(3) + .Text(publicacion.NombrePublicacion).SemiBold().FontSize(10); + + header.Cell().BorderBottom(1).Padding(2).Text("Suscriptor").SemiBold(); + header.Cell().BorderBottom(1).Padding(2).Text("Dirección").SemiBold(); + header.Cell().BorderBottom(1).Padding(2).Text("Teléfono").SemiBold(); + header.Cell().BorderBottom(1).Padding(2).Text("Días Entrega").SemiBold(); + header.Cell().BorderBottom(1).Padding(2).Text("Observaciones").SemiBold(); + }); + + foreach (var item in publicacion.Suscripciones) + { + table.Cell().Padding(2).Text(item.NombreSuscriptor); + table.Cell().Padding(2).Text(item.Direccion); + table.Cell().Padding(2).Text(item.Telefono ?? "-"); + table.Cell().Padding(2).Text(item.DiasEntrega); + table.Cell().Padding(2).Text(item.Observaciones ?? "-"); + } + }); + } + } +} \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Controllers/Reportes/ReportesController.cs b/Backend/GestionIntegral.Api/Controllers/Reportes/ReportesController.cs index 4ff304f..2616cbd 100644 --- a/Backend/GestionIntegral.Api/Controllers/Reportes/ReportesController.cs +++ b/Backend/GestionIntegral.Api/Controllers/Reportes/ReportesController.cs @@ -39,6 +39,7 @@ namespace GestionIntegral.Api.Controllers private const string PermisoVerReporteNovedadesCanillas = "RR004"; private const string PermisoVerReporteListadoDistMensual = "RR009"; private const string PermisoVerReporteFacturasPublicidad = "RR010"; + private const string PermisoVerReporteDistSuscripciones = "RR011"; public ReportesController( IReportesService reportesService, @@ -1719,5 +1720,39 @@ namespace GestionIntegral.Api.Controllers return StatusCode(500, "Error interno al generar el PDF del reporte."); } } + + [HttpGet("suscripciones/distribucion/pdf")] + [ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)] + public async Task GetReporteDistribucionSuscripcionesPdf([FromQuery] DateTime fechaDesde, [FromQuery] DateTime fechaHasta) + { + if (!TienePermiso(PermisoVerReporteDistSuscripciones)) return Forbid(); + + var (data, error) = await _reportesService.ObtenerReporteDistribucionSuscripcionesAsync(fechaDesde, fechaHasta); + if (error != null) return BadRequest(new { message = error }); + if (data == null || !data.Any()) + { + return NotFound(new { message = "No se encontraron suscripciones activas para el período seleccionado." }); + } + + try + { + var viewModel = new DistribucionSuscripcionesViewModel(data) + { + FechaDesde = fechaDesde.ToString("dd/MM/yyyy"), + FechaHasta = fechaHasta.ToString("dd/MM/yyyy"), + FechaGeneracion = DateTime.Now.ToString("dd/MM/yyyy HH:mm") + }; + + var document = new DistribucionSuscripcionesDocument(viewModel); + byte[] pdfBytes = await _pdfGenerator.GeneratePdfAsync(document); + string fileName = $"ReporteDistribucionSuscripciones_{fechaDesde:yyyyMMdd}_al_{fechaHasta:yyyyMMdd}.pdf"; + return File(pdfBytes, "application/pdf", fileName); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error al generar PDF para Reporte de Distribución de Suscripciones."); + return StatusCode(500, "Error interno al generar el PDF del reporte."); + } + } } } \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Reportes/IReportesRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Reportes/IReportesRepository.cs index 055ed86..fa079a0 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Reportes/IReportesRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Reportes/IReportesRepository.cs @@ -46,5 +46,6 @@ namespace GestionIntegral.Api.Data.Repositories.Reportes Task> GetReporteMensualDiariosAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista); Task> GetReporteMensualPorPublicacionAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista); Task> GetDatosReportePublicidadAsync(string periodo); + Task> GetDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta); } } \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Reportes/ReportesRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Reportes/ReportesRepository.cs index 0ef3e9c..a47ea65 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Reportes/ReportesRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Reportes/ReportesRepository.cs @@ -592,5 +592,45 @@ namespace GestionIntegral.Api.Data.Repositories.Reportes return Enumerable.Empty(); } } + + public async Task> GetDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta) + { + const string sql = @" + SELECT + e.Nombre AS NombreEmpresa, + p.Nombre AS NombrePublicacion, + sus.NombreCompleto AS NombreSuscriptor, + sus.Direccion, + sus.Telefono, + s.FechaInicio, + s.FechaFin, + s.DiasEntrega, + s.Observaciones + FROM dbo.susc_Suscripciones s + JOIN dbo.susc_Suscriptores sus ON s.IdSuscriptor = sus.IdSuscriptor + JOIN dbo.dist_dtPublicaciones p ON s.IdPublicacion = p.Id_Publicacion + JOIN dbo.dist_dtEmpresas e ON p.Id_Empresa = e.Id_Empresa + WHERE + s.Estado = 'Activa' + AND sus.Activo = 1 + -- La suscripción debe haber comenzado ANTES de que termine el rango de fechas + AND s.FechaInicio <= @FechaHasta + -- Y debe terminar DESPUÉS de que comience el rango de fechas (o no tener fecha de fin) + AND (s.FechaFin IS NULL OR s.FechaFin >= @FechaDesde) + ORDER BY + e.Nombre, p.Nombre, sus.NombreCompleto; + "; + + try + { + using var connection = _dbConnectionFactory.CreateConnection(); + return await connection.QueryAsync(sql, new { FechaDesde = fechaDesde, FechaHasta = fechaHasta }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error al obtener datos para el Reporte de Distribución de Suscripciones."); + return Enumerable.Empty(); + } + } } } \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/AjusteRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/AjusteRepository.cs index 5431e16..a5bf849 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/AjusteRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/AjusteRepository.cs @@ -20,11 +20,12 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones { const string sql = @" UPDATE dbo.susc_Ajustes SET + IdEmpresa = @IdEmpresa, FechaAjuste = @FechaAjuste, TipoAjuste = @TipoAjuste, Monto = @Monto, Motivo = @Motivo - WHERE IdAjuste = @IdAjuste AND Estado = 'Pendiente';"; // Solo se pueden editar los pendientes + WHERE IdAjuste = @IdAjuste AND Estado = 'Pendiente';"; if (transaction?.Connection == null) { throw new ArgumentNullException(nameof(transaction.Connection), "La conexión de la transacción no puede ser nula."); @@ -33,13 +34,12 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones return rows == 1; } - // Actualizar también el CreateAsync public async Task CreateAsync(Ajuste nuevoAjuste, IDbTransaction transaction) { const string sql = @" - INSERT INTO dbo.susc_Ajustes (IdSuscriptor, FechaAjuste, TipoAjuste, Monto, Motivo, Estado, IdUsuarioAlta, FechaAlta) + INSERT INTO dbo.susc_Ajustes (IdSuscriptor, IdEmpresa, FechaAjuste, TipoAjuste, Monto, Motivo, Estado, IdUsuarioAlta, FechaAlta) OUTPUT INSERTED.* - VALUES (@IdSuscriptor, @FechaAjuste, @TipoAjuste, @Monto, @Motivo, 'Pendiente', @IdUsuarioAlta, GETDATE());"; + VALUES (@IdSuscriptor, @IdEmpresa, @FechaAjuste, @TipoAjuste, @Monto, @Motivo, 'Pendiente', @IdUsuarioAlta, GETDATE());"; if (transaction?.Connection == null) { throw new ArgumentNullException(nameof(transaction.Connection), "La conexión de la transacción no puede ser nula."); @@ -70,18 +70,20 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones return await connection.QueryAsync(sqlBuilder.ToString(), parameters); } - public async Task> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, DateTime fechaHasta, IDbTransaction transaction) + public async Task> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, int idEmpresa, DateTime fechaHasta, IDbTransaction transaction) { const string sql = @" SELECT * FROM dbo.susc_Ajustes WHERE IdSuscriptor = @IdSuscriptor + AND IdEmpresa = @IdEmpresa AND Estado = 'Pendiente' - AND FechaAjuste <= @FechaHasta;"; // La condición clave es que la fecha del ajuste sea HASTA la fecha límite + AND FechaAjuste <= @FechaHasta;"; + if (transaction?.Connection == null) { throw new ArgumentNullException(nameof(transaction.Connection), "La conexión de la transacción no puede ser nula."); } - return await transaction.Connection.QueryAsync(sql, new { idSuscriptor, FechaHasta = fechaHasta }, transaction); + return await transaction.Connection.QueryAsync(sql, new { idSuscriptor, idEmpresa, FechaHasta = fechaHasta }, transaction); } public async Task MarcarAjustesComoAplicadosAsync(IEnumerable idsAjustes, int idFactura, IDbTransaction transaction) @@ -116,7 +118,7 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones Estado = 'Anulado', IdUsuarioAnulo = @IdUsuario, FechaAnulacion = GETDATE() - WHERE IdAjuste = @IdAjuste AND Estado = 'Pendiente';"; // Solo se pueden anular los pendientes + WHERE IdAjuste = @IdAjuste AND Estado = 'Pendiente';"; if (transaction?.Connection == null) { diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/FacturaRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/FacturaRepository.cs index 4347903..0ae176c 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/FacturaRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/FacturaRepository.cs @@ -191,6 +191,40 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones return await connection.QuerySingleOrDefaultAsync(sql); } + public async Task> GetFacturasConEmpresaAsync(int idSuscriptor, string periodo) + { + // Esta consulta es más robusta y eficiente. Obtiene la factura y el nombre de la empresa en una sola llamada. + const string sql = @" + SELECT f.*, e.Nombre AS NombreEmpresa + FROM dbo.susc_Facturas f + OUTER APPLY ( + SELECT TOP 1 emp.Nombre + FROM dbo.susc_FacturaDetalles fd + JOIN dbo.susc_Suscripciones s ON fd.IdSuscripcion = s.IdSuscripcion + JOIN dbo.dist_dtPublicaciones p ON s.IdPublicacion = p.Id_Publicacion + JOIN dbo.dist_dtEmpresas emp ON p.Id_Empresa = emp.Id_Empresa + WHERE fd.IdFactura = f.IdFactura + ) e + WHERE f.IdSuscriptor = @IdSuscriptor AND f.Periodo = @Periodo;"; + + try + { + using var connection = _connectionFactory.CreateConnection(); + var result = await connection.QueryAsync( + sql, + (factura, nombreEmpresa) => (factura, nombreEmpresa ?? "N/A"), // Asignamos "N/A" si no encuentra empresa + new { IdSuscriptor = idSuscriptor, Periodo = periodo }, + splitOn: "NombreEmpresa" + ); + return result; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error al obtener facturas con empresa para suscriptor {IdSuscriptor} y período {Periodo}", idSuscriptor, periodo); + return Enumerable.Empty<(Factura, string)>(); + } + } + public async Task> GetFacturasPagadasPendientesDeFacturar(string periodo) { // Consulta simplificada pero robusta. diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IAjusteRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IAjusteRepository.cs index 60af0fe..d528a98 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IAjusteRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IAjusteRepository.cs @@ -15,7 +15,7 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones Task UpdateAsync(Ajuste ajuste, IDbTransaction transaction); Task AnularAjusteAsync(int idAjuste, int idUsuario, IDbTransaction transaction); Task> GetAjustesPorSuscriptorAsync(int idSuscriptor, DateTime? fechaDesde, DateTime? fechaHasta); - Task> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, DateTime fechaHasta, IDbTransaction transaction); + Task> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, int idEmpresa, DateTime fechaHasta, IDbTransaction transaction); Task MarcarAjustesComoAplicadosAsync(IEnumerable idsAjustes, int idFactura, IDbTransaction transaction); Task> GetAjustesPorIdFacturaAsync(int idFactura); } diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IFacturaRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IFacturaRepository.cs index 13d76c1..3c8c35c 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IFacturaRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Suscripciones/IFacturaRepository.cs @@ -9,6 +9,7 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones Task> GetByPeriodoAsync(string periodo); Task GetBySuscriptorYPeriodoAsync(int idSuscriptor, string periodo, IDbTransaction transaction); Task> GetListBySuscriptorYPeriodoAsync(int idSuscriptor, string periodo); + Task> GetFacturasConEmpresaAsync(int idSuscriptor, string periodo); Task CreateAsync(Factura nuevaFactura, IDbTransaction transaction); Task UpdateEstadoPagoAsync(int idFactura, string nuevoEstadoPago, IDbTransaction transaction); Task UpdateNumeroFacturaAsync(int idFactura, string numeroFactura, IDbTransaction transaction); diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/IUsuarioRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/IUsuarioRepository.cs index 8d7bd10..2ea5e35 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/IUsuarioRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/IUsuarioRepository.cs @@ -1,3 +1,5 @@ +// Archivo: GestionIntegral.Api/Data/Repositories/Usuarios/IUsuarioRepository.cs + using GestionIntegral.Api.Models.Usuarios; // Para Usuario using GestionIntegral.Api.Dtos.Usuarios.Auditoria; using System.Collections.Generic; @@ -10,6 +12,7 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios { Task> GetAllAsync(string? userFilter, string? nombreFilter); Task GetByIdAsync(int id); + Task> GetByIdsAsync(IEnumerable ids); Task GetByUsernameAsync(string username); // Ya existe en IAuthRepository, pero lo duplicamos para cohesión del CRUD Task CreateAsync(Usuario nuevoUsuario, int idUsuarioCreador, IDbTransaction transaction); Task UpdateAsync(Usuario usuarioAActualizar, int idUsuarioModificador, IDbTransaction transaction); @@ -17,7 +20,6 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios // Task DeleteAsync(int id, int idUsuarioModificador, IDbTransaction transaction); Task SetPasswordAsync(int userId, string newHash, string newSalt, bool debeCambiarClave, int idUsuarioModificador, IDbTransaction transaction); Task UserExistsAsync(string username, int? excludeId = null); - // Para el DTO de listado Task> GetAllWithProfileNameAsync(string? userFilter, string? nombreFilter); Task<(Usuario? Usuario, string? NombrePerfil)> GetByIdWithProfileNameAsync(int id); Task> GetHistorialByUsuarioIdAsync(int idUsuarioAfectado, DateTime? fechaDesde, DateTime? fechaHasta); diff --git a/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/UsuarioRepository.cs b/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/UsuarioRepository.cs index 0730080..083aa1d 100644 --- a/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/UsuarioRepository.cs +++ b/Backend/GestionIntegral.Api/Data/Repositories/Usuarios/UsuarioRepository.cs @@ -1,12 +1,8 @@ using Dapper; using GestionIntegral.Api.Models.Usuarios; using GestionIntegral.Api.Dtos.Usuarios.Auditoria; -using Microsoft.Extensions.Logging; -using System.Collections.Generic; using System.Data; -using System.Linq; using System.Text; -using System.Threading.Tasks; namespace GestionIntegral.Api.Data.Repositories.Usuarios { @@ -88,7 +84,6 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios } } - public async Task GetByIdAsync(int id) { const string sql = "SELECT * FROM dbo.gral_Usuarios WHERE Id = @Id"; @@ -103,6 +98,33 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios return null; } } + + public async Task> GetByIdsAsync(IEnumerable ids) + { + // 1. Validar si la lista de IDs está vacía para evitar una consulta innecesaria a la BD. + if (ids == null || !ids.Any()) + { + return Enumerable.Empty(); + } + + // 2. Definir la consulta. Dapper manejará la expansión de la cláusula IN de forma segura. + const string sql = "SELECT * FROM dbo.gral_Usuarios WHERE Id IN @Ids"; + + try + { + // 3. Crear conexión y ejecutar la consulta. + using var connection = _connectionFactory.CreateConnection(); + // 4. Pasar la colección de IDs como parámetro. El nombre 'Ids' debe coincidir con el placeholder '@Ids'. + return await connection.QueryAsync(sql, new { Ids = ids }); + } + catch (Exception ex) + { + // 5. Registrar el error y devolver una lista vacía en caso de fallo para no romper la aplicación. + _logger.LogError(ex, "Error al obtener Usuarios por lista de IDs."); + return Enumerable.Empty(); + } + } + public async Task<(Usuario? Usuario, string? NombrePerfil)> GetByIdWithProfileNameAsync(int id) { const string sql = @" @@ -128,7 +150,6 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios } } - public async Task GetByUsernameAsync(string username) { // Esta es la misma que en AuthRepository, si se unifican, se puede eliminar una. diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Reportes/DistribucionSuscripcionDto.cs b/Backend/GestionIntegral.Api/Models/Dtos/Reportes/DistribucionSuscripcionDto.cs new file mode 100644 index 0000000..edf8cee --- /dev/null +++ b/Backend/GestionIntegral.Api/Models/Dtos/Reportes/DistribucionSuscripcionDto.cs @@ -0,0 +1,15 @@ +namespace GestionIntegral.Api.Dtos.Reportes +{ + public class DistribucionSuscripcionDto + { + public string NombreEmpresa { get; set; } = string.Empty; + public string NombrePublicacion { get; set; } = string.Empty; + public string NombreSuscriptor { get; set; } = string.Empty; + public string Direccion { get; set; } = string.Empty; + public string? Telefono { get; set; } + public DateTime FechaInicio { get; set; } + public DateTime? FechaFin { get; set; } + public string DiasEntrega { get; set; } = string.Empty; + public string? Observaciones { get; set; } + } +} \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Reportes/ViewModels/DistribucionSuscripcionesViewModel.cs b/Backend/GestionIntegral.Api/Models/Dtos/Reportes/ViewModels/DistribucionSuscripcionesViewModel.cs new file mode 100644 index 0000000..38fb329 --- /dev/null +++ b/Backend/GestionIntegral.Api/Models/Dtos/Reportes/ViewModels/DistribucionSuscripcionesViewModel.cs @@ -0,0 +1,42 @@ +namespace GestionIntegral.Api.Dtos.Reportes.ViewModels +{ + // Clases internas para la agrupación + public class GrupoPublicacion + { + public string NombrePublicacion { get; set; } = string.Empty; + public IEnumerable Suscripciones { get; set; } = Enumerable.Empty(); + } + + public class GrupoEmpresa + { + public string NombreEmpresa { get; set; } = string.Empty; + public IEnumerable Publicaciones { get; set; } = Enumerable.Empty(); + } + + public class DistribucionSuscripcionesViewModel + { + public IEnumerable DatosAgrupados { get; } + public string FechaDesde { get; set; } = string.Empty; + public string FechaHasta { get; set; } = string.Empty; + public string FechaGeneracion { get; set; } = string.Empty; + + public DistribucionSuscripcionesViewModel(IEnumerable suscripciones) + { + DatosAgrupados = suscripciones + .GroupBy(s => s.NombreEmpresa) + .Select(gEmpresa => new GrupoEmpresa + { + NombreEmpresa = gEmpresa.Key, + Publicaciones = gEmpresa + .GroupBy(s => s.NombrePublicacion) + .Select(gPub => new GrupoPublicacion + { + NombrePublicacion = gPub.Key, + Suscripciones = gPub.OrderBy(s => s.NombreSuscriptor).ToList() + }) + .OrderBy(p => p.NombrePublicacion) + }) + .OrderBy(e => e.NombreEmpresa); + } + } +} \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/AjusteDto.cs b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/AjusteDto.cs index 4032f5b..a44c8d9 100644 --- a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/AjusteDto.cs +++ b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/AjusteDto.cs @@ -4,13 +4,15 @@ namespace GestionIntegral.Api.Dtos.Suscripciones { public int IdAjuste { get; set; } public int IdSuscriptor { get; set; } + public int IdEmpresa { get; set; } + public string? NombreEmpresa { get; set; } public string FechaAjuste { get; set; } = string.Empty; public string TipoAjuste { get; set; } = string.Empty; public decimal Monto { get; set; } public string Motivo { get; set; } = string.Empty; public string Estado { get; set; } = string.Empty; public int? IdFacturaAplicado { get; set; } - public string FechaAlta { get; set; } = string.Empty; // yyyy-MM-dd + public string FechaAlta { get; set; } = string.Empty; public string NombreUsuarioAlta { get; set; } = string.Empty; } } \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateAjusteDto.cs b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateAjusteDto.cs index 8267545..563a5d1 100644 --- a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateAjusteDto.cs +++ b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateAjusteDto.cs @@ -6,6 +6,9 @@ namespace GestionIntegral.Api.Dtos.Suscripciones { [Required] public int IdSuscriptor { get; set; } + + [Required] + public int IdEmpresa { get; set; } [Required] public DateTime FechaAjuste { get; set; } diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateSuscriptorDto.cs b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateSuscriptorDto.cs index eba6091..7742f34 100644 --- a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateSuscriptorDto.cs +++ b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/CreateSuscriptorDto.cs @@ -1,3 +1,5 @@ +// Archivo: GestionIntegral.Api/Dtos/Suscripciones/CreateSuscriptorDto.cs + using System.ComponentModel.DataAnnotations; namespace GestionIntegral.Api.Dtos.Suscripciones @@ -13,6 +15,7 @@ namespace GestionIntegral.Api.Dtos.Suscripciones public string? Email { get; set; } [StringLength(50)] + [RegularExpression(@"^[0-9\s\+\-\(\)]*$", ErrorMessage = "El teléfono solo puede contener números y los símbolos +, -, () y espacios.")] public string? Telefono { get; set; } [Required(ErrorMessage = "La dirección es obligatoria.")] @@ -25,9 +28,11 @@ namespace GestionIntegral.Api.Dtos.Suscripciones [Required(ErrorMessage = "El número de documento es obligatorio.")] [StringLength(11)] + [RegularExpression("^[0-9]*$", ErrorMessage = "El número de documento solo puede contener números.")] public string NroDocumento { get; set; } = string.Empty; [StringLength(22, MinimumLength = 22, ErrorMessage = "El CBU debe tener 22 dígitos.")] + [RegularExpression("^[0-9]*$", ErrorMessage = "El CBU solo puede contener números.")] public string? CBU { get; set; } [Required(ErrorMessage = "La forma de pago es obligatoria.")] diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateAjusteDto.cs b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateAjusteDto.cs index 5a2a1eb..71b8a6b 100644 --- a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateAjusteDto.cs +++ b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateAjusteDto.cs @@ -3,8 +3,12 @@ using System.ComponentModel.DataAnnotations; namespace GestionIntegral.Api.Dtos.Suscripciones; public class UpdateAjusteDto { + [Required] + public int IdEmpresa { get; set; } + [Required] public DateTime FechaAjuste { get; set; } + [Required] [RegularExpression("^(Credito|Debito)$")] public string TipoAjuste { get; set; } = string.Empty; diff --git a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateSuscriptorDto.cs b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateSuscriptorDto.cs index 2fc51f4..acd2339 100644 --- a/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateSuscriptorDto.cs +++ b/Backend/GestionIntegral.Api/Models/Dtos/Suscripciones/UpdateSuscriptorDto.cs @@ -2,7 +2,6 @@ using System.ComponentModel.DataAnnotations; namespace GestionIntegral.Api.Dtos.Suscripciones { - // Es idéntico al CreateDto, pero se mantiene separado por si las reglas de validación cambian. public class UpdateSuscriptorDto { [Required(ErrorMessage = "El nombre completo es obligatorio.")] @@ -14,6 +13,7 @@ namespace GestionIntegral.Api.Dtos.Suscripciones public string? Email { get; set; } [StringLength(50)] + [RegularExpression(@"^[0-9\s\+\-\(\)]*$", ErrorMessage = "El teléfono solo puede contener números y los símbolos +, -, () y espacios.")] public string? Telefono { get; set; } [Required(ErrorMessage = "La dirección es obligatoria.")] @@ -26,9 +26,11 @@ namespace GestionIntegral.Api.Dtos.Suscripciones [Required(ErrorMessage = "El número de documento es obligatorio.")] [StringLength(11)] + [RegularExpression("^[0-9]*$", ErrorMessage = "El número de documento solo puede contener números.")] public string NroDocumento { get; set; } = string.Empty; [StringLength(22, MinimumLength = 22, ErrorMessage = "El CBU debe tener 22 dígitos.")] + [RegularExpression("^[0-9]*$", ErrorMessage = "El CBU solo puede contener números.")] public string? CBU { get; set; } [Required(ErrorMessage = "La forma de pago es obligatoria.")] diff --git a/Backend/GestionIntegral.Api/Models/Suscripciones/Ajuste.cs b/Backend/GestionIntegral.Api/Models/Suscripciones/Ajuste.cs index aec573a..73ae2be 100644 --- a/Backend/GestionIntegral.Api/Models/Suscripciones/Ajuste.cs +++ b/Backend/GestionIntegral.Api/Models/Suscripciones/Ajuste.cs @@ -4,6 +4,7 @@ namespace GestionIntegral.Api.Models.Suscripciones { public int IdAjuste { get; set; } public int IdSuscriptor { get; set; } + public int IdEmpresa { get; set; } public DateTime FechaAjuste { get; set; } public string TipoAjuste { get; set; } = string.Empty; public decimal Monto { get; set; } diff --git a/Backend/GestionIntegral.Api/Services/Reportes/IReportesService.cs b/Backend/GestionIntegral.Api/Services/Reportes/IReportesService.cs index 893be31..60c0a71 100644 --- a/Backend/GestionIntegral.Api/Services/Reportes/IReportesService.cs +++ b/Backend/GestionIntegral.Api/Services/Reportes/IReportesService.cs @@ -61,17 +61,11 @@ namespace GestionIntegral.Api.Services.Reportes IEnumerable Saldos, string? Error )> ObtenerReporteCuentasDistribuidorAsync(int idDistribuidor, int idEmpresa, DateTime fechaDesde, DateTime fechaHasta); - Task<(IEnumerable Simple, IEnumerable Promedios, string? Error)> ObtenerListadoDistribucionDistribuidoresAsync(int idDistribuidor, int idPublicacion, DateTime fechaDesde, DateTime fechaHasta); - - Task<( - IEnumerable Detalles, - IEnumerable Ganancias, - string? Error - )> ObtenerDatosTicketLiquidacionAsync(DateTime fecha, int idCanilla); - + Task<(IEnumerable Detalles, IEnumerable Ganancias, string? Error)> ObtenerDatosTicketLiquidacionAsync(DateTime fecha, int idCanilla); Task<(IEnumerable Data, string? Error)> ObtenerReporteMensualDiariosAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista); Task<(IEnumerable Data, string? Error)> ObtenerReporteMensualPorPublicacionAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista); Task<(IEnumerable Data, string? Error)> ObtenerFacturasParaReportePublicidad(int anio, int mes); + Task<(IEnumerable Data, string? Error)> ObtenerReporteDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta); } } \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Services/Reportes/ReportesService.cs b/Backend/GestionIntegral.Api/Services/Reportes/ReportesService.cs index 6676322..5fdb539 100644 --- a/Backend/GestionIntegral.Api/Services/Reportes/ReportesService.cs +++ b/Backend/GestionIntegral.Api/Services/Reportes/ReportesService.cs @@ -550,5 +550,23 @@ namespace GestionIntegral.Api.Services.Reportes return (new List(), "Error interno al generar el reporte."); } } + + public async Task<(IEnumerable Data, string? Error)> ObtenerReporteDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta) + { + if (fechaDesde > fechaHasta) + { + return (Enumerable.Empty(), "La fecha 'Desde' no puede ser mayor que la fecha 'Hasta'."); + } + try + { + var data = await _reportesRepository.GetDistribucionSuscripcionesAsync(fechaDesde, fechaHasta); + return (data, null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error en servicio al obtener datos para reporte de distribución de suscripciones."); + return (new List(), "Error interno al generar el reporte."); + } + } } } \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/Services/Suscripciones/AjusteService.cs b/Backend/GestionIntegral.Api/Services/Suscripciones/AjusteService.cs index 64fb374..f4f4edd 100644 --- a/Backend/GestionIntegral.Api/Services/Suscripciones/AjusteService.cs +++ b/Backend/GestionIntegral.Api/Services/Suscripciones/AjusteService.cs @@ -4,6 +4,7 @@ using GestionIntegral.Api.Data.Repositories.Usuarios; using GestionIntegral.Api.Dtos.Suscripciones; using GestionIntegral.Api.Models.Suscripciones; using System.Data; +using GestionIntegral.Api.Data.Repositories.Distribucion; namespace GestionIntegral.Api.Services.Suscripciones { @@ -12,6 +13,7 @@ namespace GestionIntegral.Api.Services.Suscripciones private readonly IAjusteRepository _ajusteRepository; private readonly ISuscriptorRepository _suscriptorRepository; private readonly IUsuarioRepository _usuarioRepository; + private readonly IEmpresaRepository _empresaRepository; private readonly DbConnectionFactory _connectionFactory; private readonly ILogger _logger; @@ -19,12 +21,14 @@ namespace GestionIntegral.Api.Services.Suscripciones IAjusteRepository ajusteRepository, ISuscriptorRepository suscriptorRepository, IUsuarioRepository usuarioRepository, + IEmpresaRepository empresaRepository, DbConnectionFactory connectionFactory, ILogger logger) { _ajusteRepository = ajusteRepository; _suscriptorRepository = suscriptorRepository; _usuarioRepository = usuarioRepository; + _empresaRepository = empresaRepository; _connectionFactory = connectionFactory; _logger = logger; } @@ -33,10 +37,13 @@ namespace GestionIntegral.Api.Services.Suscripciones { if (ajuste == null) return null; var usuario = await _usuarioRepository.GetByIdAsync(ajuste.IdUsuarioAlta); + var empresa = await _empresaRepository.GetByIdAsync(ajuste.IdEmpresa); return new AjusteDto { IdAjuste = ajuste.IdAjuste, IdSuscriptor = ajuste.IdSuscriptor, + IdEmpresa = ajuste.IdEmpresa, + NombreEmpresa = empresa?.Nombre ?? "N/A", FechaAjuste = ajuste.FechaAjuste.ToString("yyyy-MM-dd"), TipoAjuste = ajuste.TipoAjuste, Monto = ajuste.Monto, @@ -51,9 +58,50 @@ namespace GestionIntegral.Api.Services.Suscripciones public async Task> ObtenerAjustesPorSuscriptor(int idSuscriptor, DateTime? fechaDesde, DateTime? fechaHasta) { var ajustes = await _ajusteRepository.GetAjustesPorSuscriptorAsync(idSuscriptor, fechaDesde, fechaHasta); - var dtosTasks = ajustes.Select(a => MapToDto(a)); - var dtos = await Task.WhenAll(dtosTasks); - return dtos.Where(dto => dto != null)!; + if (!ajustes.Any()) + { + return Enumerable.Empty(); + } + + // 1. Recolectar IDs únicos de usuarios Y empresas de la lista de ajustes + var idsUsuarios = ajustes.Select(a => a.IdUsuarioAlta).Distinct().ToList(); + var idsEmpresas = ajustes.Select(a => a.IdEmpresa).Distinct().ToList(); + + // 2. Obtener todos los usuarios y empresas necesarios en dos consultas masivas. + var usuariosTask = _usuarioRepository.GetByIdsAsync(idsUsuarios); + var empresasTask = _empresaRepository.GetAllAsync(null, null); // Asumiendo que GetAllAsync es suficiente o crear un GetByIds. + + // Esperamos a que ambas consultas terminen + await Task.WhenAll(usuariosTask, empresasTask); + + // Convertimos los resultados a diccionarios para búsqueda rápida + var usuariosDict = (await usuariosTask).ToDictionary(u => u.Id); + var empresasDict = (await empresasTask).ToDictionary(e => e.IdEmpresa); + + // 3. Mapear en memoria, ahora con toda la información disponible. + var dtos = ajustes.Select(ajuste => + { + usuariosDict.TryGetValue(ajuste.IdUsuarioAlta, out var usuario); + empresasDict.TryGetValue(ajuste.IdEmpresa, out var empresa); + + return new AjusteDto + { + IdAjuste = ajuste.IdAjuste, + IdSuscriptor = ajuste.IdSuscriptor, + IdEmpresa = ajuste.IdEmpresa, + NombreEmpresa = empresa?.Nombre ?? "N/A", + FechaAjuste = ajuste.FechaAjuste.ToString("yyyy-MM-dd"), + TipoAjuste = ajuste.TipoAjuste, + Monto = ajuste.Monto, + Motivo = ajuste.Motivo, + Estado = ajuste.Estado, + IdFacturaAplicado = ajuste.IdFacturaAplicado, + FechaAlta = ajuste.FechaAlta.ToString("yyyy-MM-dd HH:mm"), + NombreUsuarioAlta = usuario != null ? $"{usuario.Nombre} {usuario.Apellido}" : "N/A" + }; + }); + + return dtos; } public async Task<(AjusteDto? Ajuste, string? Error)> CrearAjusteManual(CreateAjusteDto createDto, int idUsuario) @@ -63,10 +111,16 @@ namespace GestionIntegral.Api.Services.Suscripciones { return (null, "El suscriptor especificado no existe."); } + var empresa = await _empresaRepository.GetByIdAsync(createDto.IdEmpresa); + if (empresa == null) + { + return (null, "La empresa especificada no existe."); + } var nuevoAjuste = new Ajuste { IdSuscriptor = createDto.IdSuscriptor, + IdEmpresa = createDto.IdEmpresa, FechaAjuste = createDto.FechaAjuste.Date, TipoAjuste = createDto.TipoAjuste, Monto = createDto.Monto, @@ -132,7 +186,11 @@ namespace GestionIntegral.Api.Services.Suscripciones var ajuste = await _ajusteRepository.GetByIdAsync(idAjuste); if (ajuste == null) return (false, "Ajuste no encontrado."); if (ajuste.Estado != "Pendiente") return (false, $"No se puede modificar un ajuste en estado '{ajuste.Estado}'."); + + var empresa = await _empresaRepository.GetByIdAsync(updateDto.IdEmpresa); + if (empresa == null) return (false, "La empresa especificada no existe."); + ajuste.IdEmpresa = updateDto.IdEmpresa; ajuste.FechaAjuste = updateDto.FechaAjuste; ajuste.TipoAjuste = updateDto.TipoAjuste; ajuste.Monto = updateDto.Monto; diff --git a/Backend/GestionIntegral.Api/Services/Suscripciones/DebitoAutomaticoService.cs b/Backend/GestionIntegral.Api/Services/Suscripciones/DebitoAutomaticoService.cs index ea225dc..72e8d1f 100644 --- a/Backend/GestionIntegral.Api/Services/Suscripciones/DebitoAutomaticoService.cs +++ b/Backend/GestionIntegral.Api/Services/Suscripciones/DebitoAutomaticoService.cs @@ -4,13 +4,6 @@ using GestionIntegral.Api.Models.Suscripciones; using System.Data; using System.Text; using GestionIntegral.Api.Dtos.Suscripciones; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using System.IO; namespace GestionIntegral.Api.Services.Suscripciones { @@ -24,8 +17,8 @@ namespace GestionIntegral.Api.Services.Suscripciones private readonly DbConnectionFactory _connectionFactory; private readonly ILogger _logger; - private const string NRO_PRESTACION = "123456"; // Nro. de prestación asignado por el banco - private const string ORIGEN_EMPRESA = "ELDIA"; // Nombre de la empresa (7 chars) + private const string NRO_PRESTACION = "123456"; + private const string ORIGEN_EMPRESA = "ELDIA"; public DebitoAutomaticoService( IFacturaRepository facturaRepository, @@ -47,6 +40,11 @@ namespace GestionIntegral.Api.Services.Suscripciones public async Task<(string? ContenidoArchivo, string? NombreArchivo, string? Error)> GenerarArchivoPagoDirecto(int anio, int mes, int idUsuario) { + // Se define la identificación del archivo. + // Este número debe ser gestionado para no repetirse en archivos generados + // para la misma prestación y fecha. + const int identificacionArchivo = 1; + var periodo = $"{anio}-{mes:D2}"; var fechaGeneracion = DateTime.Now; @@ -64,7 +62,9 @@ namespace GestionIntegral.Api.Services.Suscripciones var importeTotal = facturasParaDebito.Sum(f => f.Factura.ImporteFinal); var cantidadRegistros = facturasParaDebito.Count(); - var nombreArchivo = $"PD_{ORIGEN_EMPRESA.Trim()}_{fechaGeneracion:yyyyMMdd}.txt"; + + // Se utiliza la variable 'identificacionArchivo' para nombrar el archivo. + var nombreArchivo = $"{NRO_PRESTACION}{fechaGeneracion:yyyyMMdd}{identificacionArchivo}.txt"; var nuevoLote = new LoteDebito { @@ -78,12 +78,14 @@ namespace GestionIntegral.Api.Services.Suscripciones if (loteCreado == null) throw new DataException("No se pudo crear el registro del lote de débito."); var sb = new StringBuilder(); - sb.Append(CrearRegistroHeader(fechaGeneracion, importeTotal, cantidadRegistros)); + // Se pasa la 'identificacionArchivo' al método que crea el Header. + sb.Append(CrearRegistroHeader(fechaGeneracion, importeTotal, cantidadRegistros, identificacionArchivo)); foreach (var item in facturasParaDebito) { sb.Append(CrearRegistroDetalle(item.Factura, item.Suscriptor)); } - sb.Append(CrearRegistroTrailer(fechaGeneracion, importeTotal, cantidadRegistros)); + // Se pasa la 'identificacionArchivo' al método que crea el Trailer. + sb.Append(CrearRegistroTrailer(fechaGeneracion, importeTotal, cantidadRegistros, identificacionArchivo)); var idsFacturas = facturasParaDebito.Select(f => f.Factura.IdFactura); bool actualizadas = await _facturaRepository.UpdateLoteDebitoAsync(idsFacturas, loteCreado.IdLoteDebito, transaction); @@ -103,13 +105,19 @@ namespace GestionIntegral.Api.Services.Suscripciones private async Task> GetFacturasParaDebito(string periodo, IDbTransaction transaction) { - var facturasDelPeriodo = await _facturaRepository.GetByPeriodoAsync(periodo); + var facturas = await _facturaRepository.GetByPeriodoAsync(periodo); var resultado = new List<(Factura, Suscriptor)>(); - - foreach (var f in facturasDelPeriodo.Where(fa => fa.EstadoPago == "Pendiente")) + + foreach (var f in facturas.Where(fa => fa.EstadoPago == "Pendiente")) { var suscriptor = await _suscriptorRepository.GetByIdAsync(f.IdSuscriptor); - if (suscriptor == null || string.IsNullOrWhiteSpace(suscriptor.CBU)) continue; + + // Se valida que el CBU de Banelco (22 caracteres) exista antes de intentar la conversión. + if (suscriptor == null || string.IsNullOrWhiteSpace(suscriptor.CBU) || suscriptor.CBU.Length != 22) + { + _logger.LogWarning("Suscriptor ID {IdSuscriptor} omitido del lote de débito por CBU inválido o ausente (se esperan 22 dígitos).", suscriptor?.IdSuscriptor); + continue; + } var formaPago = await _formaPagoRepository.GetByIdAsync(suscriptor.IdFormaPagoPreferida); if (formaPago != null && formaPago.RequiereCBU) @@ -120,83 +128,119 @@ namespace GestionIntegral.Api.Services.Suscripciones return resultado; } - // --- Métodos de Formateo de Campos --- - private string FormatString(string? value, int length) => (value ?? "").PadRight(length).Substring(0, length); - private string FormatNumeric(long value, int length) => value.ToString().PadLeft(length, '0'); + // Lógica de conversión de CBU. + private string ConvertirCbuBanelcoASnp(string cbu22) + { + if (string.IsNullOrEmpty(cbu22) || cbu22.Length != 22) + { + _logger.LogError("Se intentó convertir un CBU inválido de {Length} caracteres. Se devolverá un campo vacío.", cbu22?.Length ?? 0); + // Devolver un string de 26 espacios/ceros según la preferencia del banco para campos erróneos. + return "".PadRight(26); + } - private string CrearRegistroHeader(DateTime fechaGeneracion, decimal importeTotal, int cantidadRegistros) + // El formato SNP de 26 se obtiene insertando un "0" al inicio y "000" después del 8vo caracter del CBU de 22. + // Formato Banelco (22): [BBBSSSSX] [T....Y] + // Posiciones: (0-7) (8-21) + // Formato SNP (26): 0[BBBSSSSX]000[T....Y] + try + { + string bloque1 = cbu22.Substring(0, 8); // Contiene código de banco, sucursal y DV del bloque 1. + string bloque2 = cbu22.Substring(8); // Contiene el resto de la cadena. + + // Reconstruir en formato SNP de 26 dígitos según el instructivo. + return $"0{bloque1}000{bloque2}"; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error al parsear y convertir CBU de 22 dígitos: {CBU}", cbu22); + return "".PadRight(26); + } + } + + // --- Métodos de Formateo y Mapeo --- + private string FormatString(string? value, int length) => (value ?? "").PadRight(length); + private string FormatNumeric(long value, int length) => value.ToString().PadLeft(length, '0'); + private string MapTipoDocumento(string tipo) => tipo.ToUpper() switch + { + "DNI" => "0096", + "CUIT" => "0080", + "CUIL" => "0086", + "LE" => "0089", + "LC" => "0090", + _ => "0000" // Tipo no especificado o C.I. Policía Federal según anexo. + }; + + private string CrearRegistroHeader(DateTime fechaGeneracion, decimal importeTotal, int cantidadRegistros, int identificacionArchivo) { var sb = new StringBuilder(); - sb.Append("00"); // Tipo de Registro + sb.Append("00"); // Tipo de Registro Header sb.Append(FormatString(NRO_PRESTACION, 6)); - sb.Append("C"); // Servicio + sb.Append("C"); // Servicio: Sistema Nacional de Pagos sb.Append(fechaGeneracion.ToString("yyyyMMdd")); - sb.Append("1"); // Identificación de Archivo (ej. '1' para el primer envío del día) + sb.Append(FormatString(identificacionArchivo.ToString(), 1)); // Identificación de Archivo sb.Append(FormatString(ORIGEN_EMPRESA, 7)); - sb.Append(FormatNumeric((long)(importeTotal * 100), 14)); // 12 enteros + 2 decimales + sb.Append(FormatNumeric((long)(importeTotal * 100), 14)); sb.Append(FormatNumeric(cantidadRegistros, 7)); - sb.Append(FormatString("", 304)); // Libre + sb.Append(FormatString("", 304)); sb.Append("\r\n"); return sb.ToString(); } private string CrearRegistroDetalle(Factura factura, Suscriptor suscriptor) { + // Convertimos el CBU de 22 (Banelco) a 26 (SNP) antes de usarlo. + string cbu26 = ConvertirCbuBanelcoASnp(suscriptor.CBU!); + var sb = new StringBuilder(); - sb.Append("0101"); // Tipo de Registro - sb.Append(FormatString(suscriptor.IdSuscriptor.ToString(), 22)); // Identificación Cliente - sb.Append(FormatString(suscriptor.CBU, 26)); // CBU - - // Referencia Unívoca: Usaremos ID Factura para asegurar unicidad - sb.Append(FormatString($"SUSC-{factura.IdFactura}", 15)); - - sb.Append(factura.FechaVencimiento.ToString("yyyyMMdd")); // Fecha 1er Vto - sb.Append(FormatNumeric((long)(factura.ImporteFinal * 100), 14)); // Importe 1er Vto - - // Campos opcionales o con valores fijos - sb.Append(FormatNumeric(0, 8)); // Fecha 2do Vto - sb.Append(FormatNumeric(0, 14)); // Importe 2do Vto - sb.Append(FormatNumeric(0, 8)); // Fecha 3er Vto - sb.Append(FormatNumeric(0, 14)); // Importe 3er Vto + sb.Append("0370"); // Tipo de Registro Detalle (Orden de Débito) + sb.Append(FormatString(suscriptor.IdSuscriptor.ToString(), 22)); // Identificación de Cliente + sb.Append(FormatString(cbu26, 26)); // CBU en formato SNP de 26 caracteres. + sb.Append(FormatString($"SUSC-{factura.IdFactura}", 15)); // Referencia Unívoca de la factura. + sb.Append(factura.FechaVencimiento.ToString("yyyyMMdd")); + sb.Append(FormatNumeric((long)(factura.ImporteFinal * 100), 14)); + sb.Append(FormatNumeric(0, 8)); // Fecha 2do Vencimiento + sb.Append(FormatNumeric(0, 14)); // Importe 2do Vencimiento + sb.Append(FormatNumeric(0, 8)); // Fecha 3er Vencimiento + sb.Append(FormatNumeric(0, 14)); // Importe 3er Vencimiento sb.Append("0"); // Moneda (0 = Pesos) - sb.Append(FormatString("", 3)); // Motivo Rechazo - sb.Append(FormatString(suscriptor.TipoDocumento, 4)); + sb.Append(FormatString("", 3)); // Motivo Rechazo (vacío en el envío) + sb.Append(FormatString(MapTipoDocumento(suscriptor.TipoDocumento), 4)); sb.Append(FormatString(suscriptor.NroDocumento, 11)); - - // El resto son campos opcionales que rellenamos con espacios/ceros sb.Append(FormatString("", 22)); // Nueva ID Cliente - sb.Append(FormatNumeric(0, 26)); // Nuevo CBU + sb.Append(FormatString("", 26)); // Nueva CBU sb.Append(FormatNumeric(0, 14)); // Importe Mínimo - sb.Append(FormatNumeric(0, 8)); // Fecha Próximo Vto - sb.Append(FormatString("", 22)); // ID Cuenta Anterior + sb.Append(FormatNumeric(0, 8)); // Fecha Próximo Vencimiento + sb.Append(FormatString("", 22)); // Identificación Cuenta Anterior sb.Append(FormatString("", 40)); // Mensaje ATM - sb.Append(FormatString($"Suscripcion {factura.Periodo}", 10)); // Concepto Factura + sb.Append(FormatString($"Susc.{factura.Periodo}", 10)); // Concepto Factura sb.Append(FormatNumeric(0, 8)); // Fecha de Cobro sb.Append(FormatNumeric(0, 14)); // Importe Cobrado - sb.Append(FormatNumeric(0, 8)); // Fecha Acreditación + sb.Append(FormatNumeric(0, 8)); // Fecha de Acreditamiento sb.Append(FormatString("", 26)); // Libre sb.Append("\r\n"); return sb.ToString(); } - private string CrearRegistroTrailer(DateTime fechaGeneracion, decimal importeTotal, int cantidadRegistros) + private string CrearRegistroTrailer(DateTime fechaGeneracion, decimal importeTotal, int cantidadRegistros, int identificacionArchivo) { var sb = new StringBuilder(); - sb.Append("99"); // Tipo de Registro + sb.Append("99"); // Tipo de Registro Trailer sb.Append(FormatString(NRO_PRESTACION, 6)); - sb.Append("C"); // Servicio + sb.Append("C"); // Servicio: Sistema Nacional de Pagos sb.Append(fechaGeneracion.ToString("yyyyMMdd")); - sb.Append("1"); // Identificación de Archivo + sb.Append(FormatString(identificacionArchivo.ToString(), 1)); // Identificación de Archivo sb.Append(FormatString(ORIGEN_EMPRESA, 7)); sb.Append(FormatNumeric((long)(importeTotal * 100), 14)); sb.Append(FormatNumeric(cantidadRegistros, 7)); - sb.Append(FormatString("", 304)); // Libre - // No se añade \r\n al final del último registro + sb.Append(FormatString("", 304)); + // La última línea del archivo no lleva salto de línea (\r\n). return sb.ToString(); } public async Task ProcesarArchivoRespuesta(IFormFile archivo, int idUsuario) { + // Se mantiene la lógica original para procesar el archivo de respuesta del banco. + var respuesta = new ProcesamientoLoteResponseDto(); if (archivo == null || archivo.Length == 0) { @@ -237,7 +281,7 @@ namespace GestionIntegral.Api.Services.Suscripciones { IdFactura = idFactura, FechaPago = DateTime.Now.Date, - IdFormaPago = 1, + IdFormaPago = 1, // Se asume una forma de pago para el débito. Monto = factura.ImporteFinal, IdUsuarioRegistro = idUsuario, Referencia = $"Lote {factura.IdLoteDebito} - Banco" diff --git a/Backend/GestionIntegral.Api/Services/Suscripciones/FacturacionService.cs b/Backend/GestionIntegral.Api/Services/Suscripciones/FacturacionService.cs index 267dde7..f76b8de 100644 --- a/Backend/GestionIntegral.Api/Services/Suscripciones/FacturacionService.cs +++ b/Backend/GestionIntegral.Api/Services/Suscripciones/FacturacionService.cs @@ -105,10 +105,7 @@ namespace GestionIntegral.Api.Services.Suscripciones foreach (var grupo in gruposParaFacturar) { int idSuscriptor = grupo.Key.IdSuscriptor; - int idEmpresa = grupo.Key.IdEmpresa; - - // La verificación de existencia ahora debe ser más específica, pero por ahora la omitimos - // para no añadir otro método al repositorio. Asumimos que no se corre dos veces. + int idEmpresa = grupo.Key.IdEmpresa; // <-- Ya tenemos la empresa del grupo decimal importeBrutoTotal = 0; decimal descuentoPromocionesTotal = 0; @@ -136,10 +133,10 @@ namespace GestionIntegral.Api.Services.Suscripciones }); } - // 4. Aplicar ajustes. Se aplican a la PRIMERA factura que se genere para el cliente. + // 4. Aplicar ajustes. Ahora se buscan por Suscriptor Y por Empresa. var ultimoDiaDelMes = periodoActual.AddMonths(1).AddDays(-1); - var ajustesPendientes = await _ajusteRepository.GetAjustesPendientesHastaFechaAsync(idSuscriptor, ultimoDiaDelMes, transaction); - decimal totalAjustes = 0; + var ajustesPendientes = await _ajusteRepository.GetAjustesPendientesHastaFechaAsync(idSuscriptor, idEmpresa, ultimoDiaDelMes, transaction); + decimal totalAjustes = ajustesPendientes.Sum(a => a.TipoAjuste == "Credito" ? -a.Monto : a.Monto); // Verificamos si este grupo es el "primero" para este cliente para no aplicar ajustes varias veces bool esPrimerGrupoParaCliente = !facturasCreadas.Any(f => f.IdSuscriptor == idSuscriptor); @@ -177,7 +174,7 @@ namespace GestionIntegral.Api.Services.Suscripciones await _facturaDetalleRepository.CreateAsync(detalle, transaction); } - if (esPrimerGrupoParaCliente && ajustesPendientes.Any()) + if (ajustesPendientes.Any()) { await _ajusteRepository.MarcarAjustesComoAplicadosAsync(ajustesPendientes.Select(a => a.IdAjuste), facturaCreada.IdFactura, transaction); } @@ -317,8 +314,9 @@ namespace GestionIntegral.Api.Services.Suscripciones var periodo = $"{anio}-{mes:D2}"; try { - var facturas = await _facturaRepository.GetListBySuscriptorYPeriodoAsync(idSuscriptor, periodo); - if (!facturas.Any()) return (false, "No se encontraron facturas para este suscriptor en el período."); + // 1. Reemplazamos la llamada original por la nueva, que ya trae toda la información necesaria. + var facturasConEmpresa = await _facturaRepository.GetFacturasConEmpresaAsync(idSuscriptor, periodo); + if (!facturasConEmpresa.Any()) return (false, "No se encontraron facturas para este suscriptor en el período."); var suscriptor = await _suscriptorRepository.GetByIdAsync(idSuscriptor); if (suscriptor == null || string.IsNullOrEmpty(suscriptor.Email)) return (false, "El suscriptor no es válido o no tiene email."); @@ -326,21 +324,36 @@ namespace GestionIntegral.Api.Services.Suscripciones var resumenHtml = new StringBuilder(); var adjuntos = new List<(byte[] content, string name)>(); - foreach (var factura in facturas.Where(f => f.EstadoPago != "Anulada")) + // 2. Iteramos sobre la nueva lista de tuplas. + foreach (var item in facturasConEmpresa.Where(f => f.Factura.EstadoPago != "Anulada")) { + var factura = item.Factura; + var nombreEmpresa = item.NombreEmpresa; + + // 3. Eliminamos la lógica compleja y propensa a errores para obtener la empresa. + // La llamada a GetDetallesPorFacturaIdAsync sigue siendo necesaria para el cuerpo del email. var detalles = await _facturaDetalleRepository.GetDetallesPorFacturaIdAsync(factura.IdFactura); - if (!detalles.Any()) continue; - var primeraSuscripcionId = detalles.First().IdSuscripcion; - var publicacion = await _publicacionRepository.GetByIdSimpleAsync(primeraSuscripcionId); - var empresa = await _empresaRepository.GetByIdAsync(publicacion?.IdEmpresa ?? 0); - - resumenHtml.Append($"

Resumen de Suscripción

"); + // Título mejorado para claridad + resumenHtml.Append($"

Resumen para {nombreEmpresa}

"); resumenHtml.Append(""); + + // 1. Mostrar Detalles de Suscripciones foreach (var detalle in detalles) { resumenHtml.Append($""); } + var ajustes = await _ajusteRepository.GetAjustesPorIdFacturaAsync(factura.IdFactura); + if (ajustes.Any()) + { + foreach (var ajuste in ajustes) + { + bool esCredito = ajuste.TipoAjuste == "Credito"; + string colorMonto = esCredito ? "#d9534f" : "#5cb85c"; + string signo = esCredito ? "-" : "+"; + resumenHtml.Append($""); + } + } resumenHtml.Append($""); resumenHtml.Append("
{detalle.Descripcion}${detalle.ImporteNeto:N2}
Ajuste: {ajuste.Motivo}{signo} ${ajuste.Monto:N2}
Subtotal${factura.ImporteFinal:N2}
"); @@ -350,7 +363,8 @@ namespace GestionIntegral.Api.Services.Suscripciones if (File.Exists(rutaCompleta)) { byte[] pdfBytes = await File.ReadAllBytesAsync(rutaCompleta); - string pdfFileName = $"Factura_{empresa?.Nombre?.Replace(" ", "")}_{factura.NumeroFactura}.pdf"; + // Usamos el nombre de la empresa para un nombre de archivo más claro + string pdfFileName = $"Factura_{nombreEmpresa.Replace(" ", "")}_{factura.NumeroFactura}.pdf"; adjuntos.Add((pdfBytes, pdfFileName)); _logger.LogInformation("PDF adjuntado: {FileName}", pdfFileName); } @@ -361,7 +375,7 @@ namespace GestionIntegral.Api.Services.Suscripciones } } - var totalGeneral = facturas.Where(f => f.EstadoPago != "Anulada").Sum(f => f.ImporteFinal); + var totalGeneral = facturasConEmpresa.Where(f => f.Factura.EstadoPago != "Anulada").Sum(f => f.Factura.ImporteFinal); string asunto = $"Resumen de Cuenta - Diario El Día - Período {periodo}"; string cuerpoHtml = ConstruirCuerpoEmailConsolidado(suscriptor, periodo, resumenHtml.ToString(), totalGeneral); diff --git a/Backend/GestionIntegral.Api/Services/Suscripciones/SuscriptorService.cs b/Backend/GestionIntegral.Api/Services/Suscripciones/SuscriptorService.cs index 3bc84d1..183ffcf 100644 --- a/Backend/GestionIntegral.Api/Services/Suscripciones/SuscriptorService.cs +++ b/Backend/GestionIntegral.Api/Services/Suscripciones/SuscriptorService.cs @@ -51,10 +51,42 @@ namespace GestionIntegral.Api.Services.Suscripciones public async Task> ObtenerTodos(string? nombreFilter, string? nroDocFilter, bool soloActivos) { + // 1. Obtener todos los suscriptores en una sola consulta var suscriptores = await _suscriptorRepository.GetAllAsync(nombreFilter, nroDocFilter, soloActivos); - var dtosTasks = suscriptores.Select(s => MapToDto(s)); - var dtos = await Task.WhenAll(dtosTasks); - return dtos.Where(dto => dto != null).Select(dto => dto!); + if (!suscriptores.Any()) + { + return Enumerable.Empty(); + } + + // 2. Obtener todas las formas de pago en una sola consulta + // y convertirlas a un diccionario para una búsqueda rápida (O(1) en lugar de O(n)). + var formasDePago = (await _formaPagoRepository.GetAllAsync()) + .ToDictionary(fp => fp.IdFormaPago); + + // 3. Mapear en memoria, evitando múltiples llamadas a la base de datos. + var dtos = suscriptores.Select(s => + { + // Busca la forma de pago en el diccionario en memoria. + formasDePago.TryGetValue(s.IdFormaPagoPreferida, out var formaPago); + + return new SuscriptorDto + { + IdSuscriptor = s.IdSuscriptor, + NombreCompleto = s.NombreCompleto, + Email = s.Email, + Telefono = s.Telefono, + Direccion = s.Direccion, + TipoDocumento = s.TipoDocumento, + NroDocumento = s.NroDocumento, + CBU = s.CBU, + IdFormaPagoPreferida = s.IdFormaPagoPreferida, + NombreFormaPagoPreferida = formaPago?.Nombre ?? "Desconocida", // Asigna el nombre + Observaciones = s.Observaciones, + Activo = s.Activo + }; + }); + + return dtos; } public async Task ObtenerPorId(int id) @@ -108,7 +140,7 @@ namespace GestionIntegral.Api.Services.Suscripciones transaction.Commit(); _logger.LogInformation("Suscriptor ID {IdSuscriptor} creado por Usuario ID {IdUsuario}.", suscriptorCreado.IdSuscriptor, idUsuario); - + var dtoCreado = await MapToDto(suscriptorCreado); return (dtoCreado, null); } @@ -124,7 +156,7 @@ namespace GestionIntegral.Api.Services.Suscripciones { var suscriptorExistente = await _suscriptorRepository.GetByIdAsync(id); if (suscriptorExistente == null) return (false, "Suscriptor no encontrado."); - + if (await _suscriptorRepository.ExistsByDocumentoAsync(updateDto.TipoDocumento, updateDto.NroDocumento, id)) { return (false, "El tipo y número de documento ya pertenecen a otro suscriptor."); @@ -139,7 +171,7 @@ namespace GestionIntegral.Api.Services.Suscripciones { return (false, "El CBU es obligatorio para la forma de pago seleccionada."); } - + // Mapeo DTO -> Modelo suscriptorExistente.NombreCompleto = updateDto.NombreCompleto; suscriptorExistente.Email = updateDto.Email; @@ -156,7 +188,7 @@ namespace GestionIntegral.Api.Services.Suscripciones using var connection = _connectionFactory.CreateConnection(); await (connection as System.Data.Common.DbConnection)!.OpenAsync(); using var transaction = connection.BeginTransaction(); - + try { var actualizado = await _suscriptorRepository.UpdateAsync(suscriptorExistente, transaction); @@ -183,7 +215,7 @@ namespace GestionIntegral.Api.Services.Suscripciones { return (false, "No se puede desactivar un suscriptor con suscripciones activas."); } - + using var connection = _connectionFactory.CreateConnection(); await (connection as System.Data.Common.DbConnection)!.OpenAsync(); using var transaction = connection.BeginTransaction(); @@ -197,7 +229,7 @@ namespace GestionIntegral.Api.Services.Suscripciones _logger.LogInformation("El estado del Suscriptor ID {IdSuscriptor} se cambió a {Estado} por el Usuario ID {IdUsuario}.", id, activar ? "Activo" : "Inactivo", idUsuario); return (true, null); } - catch(Exception ex) + catch (Exception ex) { try { transaction.Rollback(); } catch { } _logger.LogError(ex, "Error al cambiar estado del suscriptor ID: {IdSuscriptor}", id); diff --git a/Frontend/src/components/Modals/Suscripciones/AjusteFormModal.tsx b/Frontend/src/components/Modals/Suscripciones/AjusteFormModal.tsx index 41c2e04..41d1411 100644 --- a/Frontend/src/components/Modals/Suscripciones/AjusteFormModal.tsx +++ b/Frontend/src/components/Modals/Suscripciones/AjusteFormModal.tsx @@ -1,10 +1,9 @@ -// Archivo: Frontend/src/components/Modals/Suscripciones/AjusteFormModal.tsx - import React, { useState, useEffect } from 'react'; import { Modal, Box, Typography, TextField, Button, CircularProgress, Alert, FormControl, InputLabel, Select, MenuItem, type SelectChangeEvent, InputAdornment } from '@mui/material'; import type { CreateAjusteDto } from '../../../models/dtos/Suscripciones/CreateAjusteDto'; import type { UpdateAjusteDto } from '../../../models/dtos/Suscripciones/UpdateAjusteDto'; import type { AjusteDto } from '../../../models/dtos/Suscripciones/AjusteDto'; +import type { EmpresaDropdownDto } from '../../../models/dtos/Distribucion/EmpresaDropdownDto'; const modalStyle = { position: 'absolute' as 'absolute', @@ -27,9 +26,10 @@ interface AjusteFormModalProps { idSuscriptor: number; errorMessage?: string | null; clearErrorMessage: () => void; + empresas: EmpresaDropdownDto[]; } -const AjusteFormModal: React.FC = ({ open, onClose, onSubmit, idSuscriptor, errorMessage, clearErrorMessage, initialData }) => { +const AjusteFormModal: React.FC = ({ open, onClose, onSubmit, idSuscriptor, errorMessage, clearErrorMessage, initialData, empresas }) => { const [formData, setFormData] = useState({}); const [loading, setLoading] = useState(false); const [localErrors, setLocalErrors] = useState<{ [key: string]: string | null }>({}); @@ -38,16 +38,16 @@ const AjusteFormModal: React.FC = ({ open, onClose, onSubm useEffect(() => { if (open) { - // Formatear fecha correctamente: el DTO de Ajuste tiene FechaAlta con hora, pero el input necesita "yyyy-MM-dd" const fechaParaFormulario = initialData?.fechaAjuste - ? initialData.fechaAjuste.split(' ')[0] // Tomar solo la parte de la fecha + ? initialData.fechaAjuste.split(' ')[0] : new Date().toISOString().split('T')[0]; setFormData({ idSuscriptor: initialData?.idSuscriptor || idSuscriptor, + idEmpresa: initialData?.idEmpresa || undefined, // undefined para que el placeholder se muestre fechaAjuste: fechaParaFormulario, tipoAjuste: initialData?.tipoAjuste || 'Credito', - monto: initialData?.monto || undefined, // undefined para que el placeholder se muestre + monto: initialData?.monto || undefined, motivo: initialData?.motivo || '' }); setLocalErrors({}); @@ -56,6 +56,7 @@ const AjusteFormModal: React.FC = ({ open, onClose, onSubm const validate = (): boolean => { const errors: { [key: string]: string | null } = {}; + if (!formData.idEmpresa) errors.idEmpresa = "Debe seleccionar una empresa."; if (!formData.fechaAjuste) errors.fechaAjuste = "La fecha es obligatoria."; if (!formData.tipoAjuste) errors.tipoAjuste = "Seleccione un tipo."; if (!formData.monto || formData.monto <= 0) errors.monto = "El monto debe ser mayor a cero."; @@ -64,7 +65,6 @@ const AjusteFormModal: React.FC = ({ open, onClose, onSubm return Object.keys(errors).length === 0; }; - // --- HANDLERS CON TIPADO EXPLÍCITO --- const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev: AjusteFormData) => ({ @@ -75,7 +75,7 @@ const AjusteFormModal: React.FC = ({ open, onClose, onSubm if (errorMessage) clearErrorMessage(); }; - const handleSelectChange = (e: SelectChangeEvent) => { // Tipado como string + const handleSelectChange = (e: SelectChangeEvent) => { // Acepta string o number const { name, value } = e.target; setFormData((prev: AjusteFormData) => ({ ...prev, [name]: value })); if (localErrors[name]) setLocalErrors(prev => ({ ...prev, [name]: null })); @@ -108,7 +108,25 @@ const AjusteFormModal: React.FC = ({ open, onClose, onSubm {isEditing ? 'Editar Ajuste Manual' : 'Registrar Ajuste Manual'} + + + Empresa + + {localErrors.idEmpresa && {localErrors.idEmpresa}} + Tipo de Ajuste + $ }} inputProps={{ step: "0.01" }} /> + - Nota: Este ajuste se aplicará en la facturación del período correspondiente a la "Fecha del Ajuste". + Nota: Este ajuste se aplicará a la factura de la empresa seleccionada en el período correspondiente a la "Fecha del Ajuste". + {errorMessage && {errorMessage}} + + + + ); +}; + +export default SeleccionaReporteDistribucionSuscripciones; \ No newline at end of file diff --git a/Frontend/src/pages/Suscripciones/CuentaCorrienteSuscriptorPage.tsx b/Frontend/src/pages/Suscripciones/CuentaCorrienteSuscriptorPage.tsx index 2c851e6..60607f0 100644 --- a/Frontend/src/pages/Suscripciones/CuentaCorrienteSuscriptorPage.tsx +++ b/Frontend/src/pages/Suscripciones/CuentaCorrienteSuscriptorPage.tsx @@ -7,6 +7,8 @@ import EditIcon from '@mui/icons-material/Edit'; import CancelIcon from '@mui/icons-material/Cancel'; import ajusteService from '../../services/Suscripciones/ajusteService'; import suscriptorService from '../../services/Suscripciones/suscriptorService'; +import empresaService from '../../services/Distribucion/empresaService'; +import type { EmpresaDropdownDto } from '../../models/dtos/Distribucion/EmpresaDropdownDto'; import { usePermissions } from '../../hooks/usePermissions'; import axios from 'axios'; import type { AjusteDto } from '../../models/dtos/Suscripciones/AjusteDto'; @@ -33,6 +35,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => { const [suscriptor, setSuscriptor] = useState(null); const [ajustes, setAjustes] = useState([]); + const [empresas, setEmpresas] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [modalOpen, setModalOpen] = useState(false); @@ -50,18 +53,23 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => { } setLoading(true); setApiErrorMessage(null); setError(null); try { - const suscriptorData = await suscriptorService.getSuscriptorById(idSuscriptor); + // Usamos Promise.all para cargar todo en paralelo y mejorar el rendimiento + const [suscriptorData, ajustesData, empresasData] = await Promise.all([ + suscriptorService.getSuscriptorById(idSuscriptor), + ajusteService.getAjustesPorSuscriptor(idSuscriptor, filtroFechaDesde || undefined, filtroFechaHasta || undefined), + empresaService.getEmpresasDropdown() + ]); + setSuscriptor(suscriptorData); - - const ajustesData = await ajusteService.getAjustesPorSuscriptor(idSuscriptor, filtroFechaDesde || undefined, filtroFechaHasta || undefined); setAjustes(ajustesData); + setEmpresas(empresasData); } catch (err) { setError("Error al cargar los datos."); } finally { setLoading(false); } - }, [idSuscriptor, puedeGestionar, filtroFechaDesde, filtroFechaHasta]); + }, [idSuscriptor, filtroFechaDesde, filtroFechaHasta]); useEffect(() => { cargarDatos(); }, [cargarDatos]); @@ -151,7 +159,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => { type="date" size="small" value={filtroFechaDesde} - onChange={handleFechaDesdeChange} // <-- USAR NUEVO HANDLER + onChange={handleFechaDesdeChange} InputLabelProps={{ shrink: true }} /> { type="date" size="small" value={filtroFechaHasta} - onChange={handleFechaHastaChange} // <-- USAR NUEVO HANDLER + onChange={handleFechaHastaChange} InputLabelProps={{ shrink: true }} /> @@ -178,6 +186,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => { Fecha Ajuste + Empresa Tipo Motivo Monto @@ -188,13 +197,14 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => { {loading ? ( - + ) : ajustes.length === 0 ? ( - No se encontraron ajustes para los filtros seleccionados. + No se encontraron ajustes para los filtros seleccionados. ) : ( ajustes.map(a => ( {formatDisplayDate(a.fechaAjuste)} + {a.nombreEmpresa || 'N/A'} @@ -233,6 +243,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => { initialData={editingAjuste} errorMessage={apiErrorMessage} clearErrorMessage={() => setApiErrorMessage(null)} + empresas={empresas} /> ); diff --git a/Frontend/src/routes/AppRoutes.tsx b/Frontend/src/routes/AppRoutes.tsx index c69ed9e..9921133 100644 --- a/Frontend/src/routes/AppRoutes.tsx +++ b/Frontend/src/routes/AppRoutes.tsx @@ -76,6 +76,7 @@ import GestionarNovedadesCanillaPage from '../pages/Distribucion/GestionarNoveda import ReporteNovedadesCanillasPage from '../pages/Reportes/ReporteNovedadesCanillasPage'; import ReporteListadoDistMensualPage from '../pages/Reportes/ReporteListadoDistMensualPage'; import ReporteFacturasPublicidadPage from '../pages/Reportes/ReporteFacturasPublicidadPage'; +import ReporteDistribucionSuscripcionesPage from '../pages/Reportes/ReporteDistribucionSuscripcionesPage'; // Suscripciones import SuscripcionesIndexPage from '../pages/Suscripciones/SuscripcionesIndexPage'; @@ -289,6 +290,11 @@ const AppRoutes = () => { }/> + + + + }/> {/* Módulo de Radios (anidado) */} diff --git a/Frontend/src/services/Reportes/reportesService.ts b/Frontend/src/services/Reportes/reportesService.ts index 126089e..9f0aa14 100644 --- a/Frontend/src/services/Reportes/reportesService.ts +++ b/Frontend/src/services/Reportes/reportesService.ts @@ -35,9 +35,9 @@ interface GetNovedadesCanillasParams { } interface GetListadoDistMensualParams { - fechaDesde: string; // yyyy-MM-dd - fechaHasta: string; // yyyy-MM-dd - esAccionista: boolean; + fechaDesde: string; // yyyy-MM-dd + fechaHasta: string; // yyyy-MM-dd + esAccionista: boolean; } const getExistenciaPapelPdf = async (params: GetExistenciaPapelParams): Promise => { @@ -420,40 +420,63 @@ const getCanillasGananciasReporte = async (params: GetNovedadesCanillasParams): }; const getListadoDistMensualDiarios = async (params: GetListadoDistMensualParams): Promise => { - const response = await apiClient.get('/reportes/listado-distribucion-mensual/diarios', { params }); - return response.data; + const response = await apiClient.get('/reportes/listado-distribucion-mensual/diarios', { params }); + return response.data; }; const getListadoDistMensualDiariosPdf = async (params: GetListadoDistMensualParams): Promise => { - const response = await apiClient.get('/reportes/listado-distribucion-mensual/diarios/pdf', { - params, - responseType: 'blob', - }); - return response.data; + const response = await apiClient.get('/reportes/listado-distribucion-mensual/diarios/pdf', { + params, + responseType: 'blob', + }); + return response.data; }; const getListadoDistMensualPorPublicacion = async (params: GetListadoDistMensualParams): Promise => { - const response = await apiClient.get('/reportes/listado-distribucion-mensual/publicaciones', { params }); - return response.data; + const response = await apiClient.get('/reportes/listado-distribucion-mensual/publicaciones', { params }); + return response.data; }; const getListadoDistMensualPorPublicacionPdf = async (params: GetListadoDistMensualParams): Promise => { - const response = await apiClient.get('/reportes/listado-distribucion-mensual/publicaciones/pdf', { - params, - responseType: 'blob', - }); - return response.data; + const response = await apiClient.get('/reportes/listado-distribucion-mensual/publicaciones/pdf', { + params, + responseType: 'blob', + }); + return response.data; }; const getReporteFacturasPublicidadPdf = async (anio: number, mes: number): Promise<{ fileContent: Blob, fileName: string }> => { - const params = new URLSearchParams({ anio: String(anio), mes: String(mes) }); - const url = `/reportes/suscripciones/facturas-para-publicidad/pdf?${params.toString()}`; + const params = new URLSearchParams({ anio: String(anio), mes: String(mes) }); + const url = `/reportes/suscripciones/facturas-para-publicidad/pdf?${params.toString()}`; + const response = await apiClient.get(url, { + responseType: 'blob', + }); + + const contentDisposition = response.headers['content-disposition']; + let fileName = `ReportePublicidad_Suscripciones_${anio}-${String(mes).padStart(2, '0')}.pdf`; // Fallback + if (contentDisposition) { + const fileNameMatch = contentDisposition.match(/filename="(.+)"/); + if (fileNameMatch && fileNameMatch.length > 1) { + fileName = fileNameMatch[1]; + } + } + + return { fileContent: response.data, fileName: fileName }; +}; + +const getReporteDistribucionSuscripcionesPdf = async (fechaDesde: string, fechaHasta: string): Promise<{ fileContent: Blob, fileName: string }> => { + const params = new URLSearchParams({ + fechaDesde: fechaDesde, + fechaHasta: fechaHasta + }); + const url = `/reportes/suscripciones/distribucion/pdf?${params.toString()}`; + const response = await apiClient.get(url, { responseType: 'blob', }); const contentDisposition = response.headers['content-disposition']; - let fileName = `ReportePublicidad_Suscripciones_${anio}-${String(mes).padStart(2, '0')}.pdf`; // Fallback + let fileName = `ReporteDistribucion_Suscripciones_${fechaDesde}_al_${fechaHasta}.pdf`; // Fallback if (contentDisposition) { const fileNameMatch = contentDisposition.match(/filename="(.+)"/); if (fileNameMatch && fileNameMatch.length > 1) { @@ -508,6 +531,7 @@ const reportesService = { getListadoDistMensualPorPublicacion, getListadoDistMensualPorPublicacionPdf, getReporteFacturasPublicidadPdf, + getReporteDistribucionSuscripcionesPdf, }; export default reportesService; \ No newline at end of file