Feat: Implementa Reporte de Distribución de Suscripciones y Refactoriza Gestión de Ajustes

Se introduce una nueva funcionalidad de reporte crucial para la logística y se realiza una refactorización mayor del sistema de ajustes para garantizar la correcta imputación contable.

###  Nuevas Características

- **Nuevo Reporte de Distribución de Suscripciones (RR011):**
    - Se añade un nuevo reporte en PDF que genera un listado de todas las suscripciones activas en un rango de fechas.
    - El reporte está diseñado para el equipo de reparto, incluyendo datos clave como nombre del suscriptor, dirección, teléfono, días de entrega y observaciones.
    - Se implementa el endpoint, la lógica de servicio, la consulta a la base de datos y el template de QuestPDF en el backend.
    - Se crea la página correspondiente en el frontend (React) con su selector de fechas y se añade la ruta y el enlace en el menú de reportes.

### 🔄 Refactorización Mayor

- **Asociación de Ajustes a Empresas:**
    - Se refactoriza por completo la entidad `Ajuste` para incluir una referencia obligatoria a una `IdEmpresa`.
    - **Motivo:** Corregir un error crítico en la lógica de negocio donde los ajustes de un suscriptor se aplicaban a la primera factura generada, sin importar a qué empresa correspondía el ajuste.
    - Se provee un script de migración SQL para actualizar el esquema de la base de datos (`susc_Ajustes`).
    - Se actualizan todos los DTOs, repositorios y servicios (backend) para manejar la nueva relación.
    - Se modifica el `FacturacionService` para que ahora aplique los ajustes pendientes correspondientes a cada empresa dentro de su bucle de facturación.
    - Se actualiza el formulario de creación/edición de ajustes en el frontend (React) para incluir un selector de empresa obligatorio.

### ️ Optimizaciones de Rendimiento

- **Solución de N+1 Queries:**
    - Se optimiza el método `ObtenerTodos` en `SuscriptorService` para obtener todas las formas de pago en una única consulta en lugar de una por cada suscriptor.
    - Se optimiza el método `ObtenerAjustesPorSuscriptor` en `AjusteService` para obtener todos los nombres de usuarios y empresas en dos consultas masivas, en lugar de N consultas individuales.
    - Se añade el método `GetByIdsAsync` al `IUsuarioRepository` y su implementación para soportar esta optimización.

### 🐛 Corrección de Errores

- Se corrigen múltiples errores en el script de migración de base de datos (uso de `GO` dentro de transacciones, error de "columna inválida").
- Se soluciona un error de SQL (`INSERT` statement) en `AjusteRepository` que impedía la creación de nuevos ajustes.
- Se corrige un bug en el `AjusteService` que causaba que el nombre de la empresa apareciera como "N/A" en la UI debido a un mapeo incompleto en el método optimizado.
- Se corrige la lógica de generación de emails en `FacturacionService` para mostrar correctamente el nombre de la empresa en cada sección del resumen de cuenta.
This commit is contained in:
2025-08-09 17:39:21 -03:00
parent 899e0a173f
commit 21c5c1d7d9
35 changed files with 856 additions and 158 deletions

View File

@@ -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 ?? "-");
}
});
}
}
}

View File

@@ -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<IActionResult> 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.");
}
}
}
}

View File

@@ -46,5 +46,6 @@ namespace GestionIntegral.Api.Data.Repositories.Reportes
Task<IEnumerable<ListadoDistCanMensualDiariosDto>> GetReporteMensualDiariosAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista);
Task<IEnumerable<ListadoDistCanMensualPubDto>> GetReporteMensualPorPublicacionAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista);
Task<IEnumerable<FacturasParaReporteDto>> GetDatosReportePublicidadAsync(string periodo);
Task<IEnumerable<DistribucionSuscripcionDto>> GetDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta);
}
}

View File

@@ -592,5 +592,45 @@ namespace GestionIntegral.Api.Data.Repositories.Reportes
return Enumerable.Empty<FacturasParaReporteDto>();
}
}
public async Task<IEnumerable<DistribucionSuscripcionDto>> 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<DistribucionSuscripcionDto>(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<DistribucionSuscripcionDto>();
}
}
}
}

View File

@@ -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<Ajuste?> 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<Ajuste>(sqlBuilder.ToString(), parameters);
}
public async Task<IEnumerable<Ajuste>> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, DateTime fechaHasta, IDbTransaction transaction)
public async Task<IEnumerable<Ajuste>> 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<Ajuste>(sql, new { idSuscriptor, FechaHasta = fechaHasta }, transaction);
return await transaction.Connection.QueryAsync<Ajuste>(sql, new { idSuscriptor, idEmpresa, FechaHasta = fechaHasta }, transaction);
}
public async Task<bool> MarcarAjustesComoAplicadosAsync(IEnumerable<int> 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)
{

View File

@@ -191,6 +191,40 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones
return await connection.QuerySingleOrDefaultAsync<string>(sql);
}
public async Task<IEnumerable<(Factura Factura, string NombreEmpresa)>> 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<Factura, string, (Factura, string)>(
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<IEnumerable<Factura>> GetFacturasPagadasPendientesDeFacturar(string periodo)
{
// Consulta simplificada pero robusta.

View File

@@ -15,7 +15,7 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones
Task<bool> UpdateAsync(Ajuste ajuste, IDbTransaction transaction);
Task<bool> AnularAjusteAsync(int idAjuste, int idUsuario, IDbTransaction transaction);
Task<IEnumerable<Ajuste>> GetAjustesPorSuscriptorAsync(int idSuscriptor, DateTime? fechaDesde, DateTime? fechaHasta);
Task<IEnumerable<Ajuste>> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, DateTime fechaHasta, IDbTransaction transaction);
Task<IEnumerable<Ajuste>> GetAjustesPendientesHastaFechaAsync(int idSuscriptor, int idEmpresa, DateTime fechaHasta, IDbTransaction transaction);
Task<bool> MarcarAjustesComoAplicadosAsync(IEnumerable<int> idsAjustes, int idFactura, IDbTransaction transaction);
Task<IEnumerable<Ajuste>> GetAjustesPorIdFacturaAsync(int idFactura);
}

View File

@@ -9,6 +9,7 @@ namespace GestionIntegral.Api.Data.Repositories.Suscripciones
Task<IEnumerable<Factura>> GetByPeriodoAsync(string periodo);
Task<Factura?> GetBySuscriptorYPeriodoAsync(int idSuscriptor, string periodo, IDbTransaction transaction);
Task<IEnumerable<Factura>> GetListBySuscriptorYPeriodoAsync(int idSuscriptor, string periodo);
Task<IEnumerable<(Factura Factura, string NombreEmpresa)>> GetFacturasConEmpresaAsync(int idSuscriptor, string periodo);
Task<Factura?> CreateAsync(Factura nuevaFactura, IDbTransaction transaction);
Task<bool> UpdateEstadoPagoAsync(int idFactura, string nuevoEstadoPago, IDbTransaction transaction);
Task<bool> UpdateNumeroFacturaAsync(int idFactura, string numeroFactura, IDbTransaction transaction);

View File

@@ -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<IEnumerable<Usuario>> GetAllAsync(string? userFilter, string? nombreFilter);
Task<Usuario?> GetByIdAsync(int id);
Task<IEnumerable<Usuario>> GetByIdsAsync(IEnumerable<int> ids);
Task<Usuario?> GetByUsernameAsync(string username); // Ya existe en IAuthRepository, pero lo duplicamos para cohesión del CRUD
Task<Usuario?> CreateAsync(Usuario nuevoUsuario, int idUsuarioCreador, IDbTransaction transaction);
Task<bool> UpdateAsync(Usuario usuarioAActualizar, int idUsuarioModificador, IDbTransaction transaction);
@@ -17,7 +20,6 @@ namespace GestionIntegral.Api.Data.Repositories.Usuarios
// Task<bool> DeleteAsync(int id, int idUsuarioModificador, IDbTransaction transaction);
Task<bool> SetPasswordAsync(int userId, string newHash, string newSalt, bool debeCambiarClave, int idUsuarioModificador, IDbTransaction transaction);
Task<bool> UserExistsAsync(string username, int? excludeId = null);
// Para el DTO de listado
Task<IEnumerable<(Usuario Usuario, string NombrePerfil)>> GetAllWithProfileNameAsync(string? userFilter, string? nombreFilter);
Task<(Usuario? Usuario, string? NombrePerfil)> GetByIdWithProfileNameAsync(int id);
Task<IEnumerable<UsuarioHistorialDto>> GetHistorialByUsuarioIdAsync(int idUsuarioAfectado, DateTime? fechaDesde, DateTime? fechaHasta);

View File

@@ -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<Usuario?> 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<IEnumerable<Usuario>> GetByIdsAsync(IEnumerable<int> 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<Usuario>();
}
// 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<Usuario>(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<Usuario>();
}
}
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<Usuario?> GetByUsernameAsync(string username)
{
// Esta es la misma que en AuthRepository, si se unifican, se puede eliminar una.

View File

@@ -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; }
}
}

View File

@@ -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<DistribucionSuscripcionDto> Suscripciones { get; set; } = Enumerable.Empty<DistribucionSuscripcionDto>();
}
public class GrupoEmpresa
{
public string NombreEmpresa { get; set; } = string.Empty;
public IEnumerable<GrupoPublicacion> Publicaciones { get; set; } = Enumerable.Empty<GrupoPublicacion>();
}
public class DistribucionSuscripcionesViewModel
{
public IEnumerable<GrupoEmpresa> 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<DistribucionSuscripcionDto> 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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -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; }

View File

@@ -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.")]

View File

@@ -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;

View File

@@ -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.")]

View File

@@ -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; }

View File

@@ -61,17 +61,11 @@ namespace GestionIntegral.Api.Services.Reportes
IEnumerable<SaldoDto> Saldos,
string? Error
)> ObtenerReporteCuentasDistribuidorAsync(int idDistribuidor, int idEmpresa, DateTime fechaDesde, DateTime fechaHasta);
Task<(IEnumerable<ListadoDistribucionDistSimpleDto> Simple, IEnumerable<ListadoDistribucionDistPromedioDiaDto> Promedios, string? Error)> ObtenerListadoDistribucionDistribuidoresAsync(int idDistribuidor, int idPublicacion, DateTime fechaDesde, DateTime fechaHasta);
Task<(
IEnumerable<LiquidacionCanillaDetalleDto> Detalles,
IEnumerable<LiquidacionCanillaGananciaDto> Ganancias,
string? Error
)> ObtenerDatosTicketLiquidacionAsync(DateTime fecha, int idCanilla);
Task<(IEnumerable<LiquidacionCanillaDetalleDto> Detalles, IEnumerable<LiquidacionCanillaGananciaDto> Ganancias, string? Error)> ObtenerDatosTicketLiquidacionAsync(DateTime fecha, int idCanilla);
Task<(IEnumerable<ListadoDistCanMensualDiariosDto> Data, string? Error)> ObtenerReporteMensualDiariosAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista);
Task<(IEnumerable<ListadoDistCanMensualPubDto> Data, string? Error)> ObtenerReporteMensualPorPublicacionAsync(DateTime fechaDesde, DateTime fechaHasta, bool esAccionista);
Task<(IEnumerable<FacturasParaReporteDto> Data, string? Error)> ObtenerFacturasParaReportePublicidad(int anio, int mes);
Task<(IEnumerable<DistribucionSuscripcionDto> Data, string? Error)> ObtenerReporteDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta);
}
}

View File

@@ -550,5 +550,23 @@ namespace GestionIntegral.Api.Services.Reportes
return (new List<FacturasParaReporteDto>(), "Error interno al generar el reporte.");
}
}
public async Task<(IEnumerable<DistribucionSuscripcionDto> Data, string? Error)> ObtenerReporteDistribucionSuscripcionesAsync(DateTime fechaDesde, DateTime fechaHasta)
{
if (fechaDesde > fechaHasta)
{
return (Enumerable.Empty<DistribucionSuscripcionDto>(), "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<DistribucionSuscripcionDto>(), "Error interno al generar el reporte.");
}
}
}
}

View File

@@ -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<AjusteService> _logger;
@@ -19,12 +21,14 @@ namespace GestionIntegral.Api.Services.Suscripciones
IAjusteRepository ajusteRepository,
ISuscriptorRepository suscriptorRepository,
IUsuarioRepository usuarioRepository,
IEmpresaRepository empresaRepository,
DbConnectionFactory connectionFactory,
ILogger<AjusteService> 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<IEnumerable<AjusteDto>> 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<AjusteDto>();
}
// 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;

View File

@@ -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<DebitoAutomaticoService> _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<List<(Factura Factura, Suscriptor Suscriptor)>> 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<ProcesamientoLoteResponseDto> 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"

View File

@@ -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($"<h4 style='margin-top: 20px; margin-bottom: 10px; color: #34515e;'>Resumen de Suscripción</h4>");
// Título mejorado para claridad
resumenHtml.Append($"<h4 style='margin-top: 20px; margin-bottom: 10px; color: #34515e;'>Resumen para {nombreEmpresa}</h4>");
resumenHtml.Append("<table style='width: 100%; border-collapse: collapse; font-size: 0.9em;'>");
// 1. Mostrar Detalles de Suscripciones
foreach (var detalle in detalles)
{
resumenHtml.Append($"<tr><td style='padding: 5px; border-bottom: 1px solid #eee;'>{detalle.Descripcion}</td><td style='padding: 5px; border-bottom: 1px solid #eee; text-align: right;'>${detalle.ImporteNeto:N2}</td></tr>");
}
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($"<tr><td style='padding: 5px; border-bottom: 1px solid #eee; font-style: italic;'>Ajuste: {ajuste.Motivo}</td><td style='padding: 5px; border-bottom: 1px solid #eee; text-align: right; color: {colorMonto}; font-style: italic;'>{signo} ${ajuste.Monto:N2}</td></tr>");
}
}
resumenHtml.Append($"<tr style='font-weight: bold;'><td style='padding: 5px;'>Subtotal</td><td style='padding: 5px; text-align: right;'>${factura.ImporteFinal:N2}</td></tr>");
resumenHtml.Append("</table>");
@@ -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);

View File

@@ -51,10 +51,42 @@ namespace GestionIntegral.Api.Services.Suscripciones
public async Task<IEnumerable<SuscriptorDto>> 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<SuscriptorDto>();
}
// 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<SuscriptorDto?> 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);

View File

@@ -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<AjusteFormModalProps> = ({ open, onClose, onSubmit, idSuscriptor, errorMessage, clearErrorMessage, initialData }) => {
const AjusteFormModal: React.FC<AjusteFormModalProps> = ({ open, onClose, onSubmit, idSuscriptor, errorMessage, clearErrorMessage, initialData, empresas }) => {
const [formData, setFormData] = useState<AjusteFormData>({});
const [loading, setLoading] = useState(false);
const [localErrors, setLocalErrors] = useState<{ [key: string]: string | null }>({});
@@ -38,16 +38,16 @@ const AjusteFormModal: React.FC<AjusteFormModalProps> = ({ 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<AjusteFormModalProps> = ({ 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<AjusteFormModalProps> = ({ open, onClose, onSubm
return Object.keys(errors).length === 0;
};
// --- HANDLERS CON TIPADO EXPLÍCITO ---
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setFormData((prev: AjusteFormData) => ({
@@ -75,7 +75,7 @@ const AjusteFormModal: React.FC<AjusteFormModalProps> = ({ open, onClose, onSubm
if (errorMessage) clearErrorMessage();
};
const handleSelectChange = (e: SelectChangeEvent<string>) => { // Tipado como string
const handleSelectChange = (e: SelectChangeEvent<string | number>) => { // 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<AjusteFormModalProps> = ({ open, onClose, onSubm
<Box sx={modalStyle}>
<Typography variant="h6">{isEditing ? 'Editar Ajuste Manual' : 'Registrar Ajuste Manual'}</Typography>
<Box component="form" onSubmit={handleSubmit} sx={{ mt: 2 }}>
<TextField name="fechaAjuste" label="Fecha del Ajuste" type="date" value={formData.fechaAjuste || ''} onChange={handleInputChange} required fullWidth margin="dense" InputLabelProps={{ shrink: true }} error={!!localErrors.fechaAjuste} helperText={localErrors.fechaAjuste} />
<FormControl fullWidth margin="dense" required error={!!localErrors.idEmpresa}>
<InputLabel id="empresa-label">Empresa</InputLabel>
<Select
name="idEmpresa"
labelId="empresa-label"
value={formData.idEmpresa || ''}
onChange={handleSelectChange}
label="Empresa"
>
{empresas.map((empresa) => (
<MenuItem key={empresa.idEmpresa} value={empresa.idEmpresa}>
{empresa.nombre}
</MenuItem>
))}
</Select>
{localErrors.idEmpresa && <Typography color="error" variant="caption">{localErrors.idEmpresa}</Typography>}
</FormControl>
<FormControl fullWidth margin="dense" error={!!localErrors.tipoAjuste}>
<InputLabel id="tipo-ajuste-label" required>Tipo de Ajuste</InputLabel>
<Select name="tipoAjuste" labelId="tipo-ajuste-label" value={formData.tipoAjuste || ''} onChange={handleSelectChange} label="Tipo de Ajuste">
@@ -116,12 +134,16 @@ const AjusteFormModal: React.FC<AjusteFormModalProps> = ({ open, onClose, onSubm
<MenuItem value="Debito">Débito (Cargo extra al cliente)</MenuItem>
</Select>
</FormControl>
<TextField name="monto" label="Monto" type="number" value={formData.monto || ''} onChange={handleInputChange} required fullWidth margin="dense" error={!!localErrors.monto} helperText={localErrors.monto} InputProps={{ startAdornment: <InputAdornment position="start">$</InputAdornment> }} inputProps={{ step: "0.01" }} />
<TextField name="motivo" label="Motivo" value={formData.motivo || ''} onChange={handleInputChange} required fullWidth margin="dense" multiline rows={3} error={!!localErrors.motivo} helperText={localErrors.motivo} />
<Alert severity="info" sx={{ mt: 2 }}>
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 <strong>empresa seleccionada</strong> en el período correspondiente a la "Fecha del Ajuste".
</Alert>
{errorMessage && <Alert severity="error" sx={{ mt: 2 }}>{errorMessage}</Alert>}
<Box sx={{ mt: 3, display: 'flex', justifyContent: 'flex-end', gap: 1 }}>
<Button onClick={onClose} color="secondary" disabled={loading}>Cancelar</Button>
<Button type="submit" variant="contained" disabled={loading}>

View File

@@ -79,24 +79,41 @@ const SuscriptorFormModal: React.FC<SuscriptorFormModalProps> = ({ open, onClose
if (!formData.nroDocumento?.trim()) errors.nroDocumento = 'El número de documento es obligatorio.';
if (!formData.idFormaPagoPreferida) errors.idFormaPagoPreferida = 'La forma de pago es obligatoria.';
if (CBURequerido) {
if (!formData.cbu || formData.cbu.trim().length !== 22) {
errors.cbu = 'El CBU es obligatorio y debe tener 22 dígitos.';
}
} else if (formData.cbu && formData.cbu.trim().length > 0 && formData.cbu.trim().length !== 22) {
errors.cbu = 'El CBU debe tener 22 dígitos o estar vacío.';
// Validar formato de Nro de Documento (solo números)
if (formData.nroDocumento && !/^[0-9]+$/.test(formData.nroDocumento)) {
errors.nroDocumento = 'El documento solo debe contener números.';
}
// Validar formato de Email
if (formData.email && !/^\S+@\S+\.\S+$/.test(formData.email)) {
errors.email = 'El formato del email no es válido.';
}
// Validar formato y longitud de CBU
if (CBURequerido) {
if (!formData.cbu || !/^[0-9]+$/.test(formData.cbu) || formData.cbu.length !== 22) {
errors.cbu = 'El CBU es obligatorio y debe tener 22 dígitos numéricos.';
}
} else if (formData.cbu && formData.cbu.trim().length > 0 && (!/^[0-9]+$/.test(formData.cbu) || formData.cbu.length !== 26)) {
errors.cbu = 'El CBU debe tener 22 dígitos numéricos o estar vacío.';
}
setLocalErrors(errors);
return Object.keys(errors).length === 0;
};
// --- HANDLER DE INPUT MEJORADO ---
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setFormData(prev => ({ ...prev, [name]: value }));
// Prevenir entrada de caracteres no numéricos para CBU y NroDocumento
if (name === 'cbu' || name === 'nroDocumento') {
const numericValue = value.replace(/[^0-9]/g, '');
setFormData(prev => ({ ...prev, [name]: numericValue }));
} else {
setFormData(prev => ({ ...prev, [name]: value }));
}
if (localErrors[name]) setLocalErrors(prev => ({ ...prev, [name]: null }));
if (errorMessage) clearErrorMessage();
};
@@ -164,7 +181,7 @@ const SuscriptorFormModal: React.FC<SuscriptorFormModalProps> = ({ open, onClose
<MenuItem value="CUIL">CUIL</MenuItem>
</Select>
</FormControl>
<TextField name="nroDocumento" label="Nro Documento" value={formData.nroDocumento || ''} onChange={handleInputChange} required fullWidth margin="dense" sx={{ flex: 2 }} error={!!localErrors.nroDocumento} helperText={localErrors.nroDocumento} disabled={loading} />
<TextField name="nroDocumento" label="Nro Documento" value={formData.nroDocumento || ''} onChange={handleInputChange} required fullWidth margin="dense" sx={{ flex: 2 }} error={!!localErrors.nroDocumento} helperText={localErrors.nroDocumento} disabled={loading} inputProps={{ maxLength: 11 }} />
</Box>
<FormControl fullWidth margin="dense" error={!!localErrors.idFormaPagoPreferida}>
<InputLabel id="forma-pago-label" required>Forma de Pago</InputLabel>

View File

@@ -2,6 +2,8 @@ export interface AjusteDto {
idAjuste: number;
fechaAjuste: string;
idSuscriptor: number;
idEmpresa: number;
nombreEmpresa?: string;
tipoAjuste: 'Credito' | 'Debito';
monto: number;
motivo: string;

View File

@@ -1,4 +1,5 @@
export interface CreateAjusteDto {
idEmpresa: number;
fechaAjuste: string;
idSuscriptor: number;
tipoAjuste: 'Credito' | 'Debito';

View File

@@ -1,4 +1,5 @@
export interface UpdateAjusteDto {
idEmpresa: number;
fechaAjuste: string; // "yyyy-MM-dd"
tipoAjuste: 'Credito' | 'Debito';
monto: number;

View File

@@ -0,0 +1,68 @@
import React, { useState } from 'react';
import { Box, Alert, Paper } from '@mui/material';
import reporteService from '../../services/Reportes/reportesService';
import { usePermissions } from '../../hooks/usePermissions';
import axios from 'axios';
import SeleccionaReporteDistribucionSuscripciones from './SeleccionaReporteDistribucionSuscripciones';
const ReporteDistribucionSuscripcionesPage: React.FC = () => {
const [loading, setLoading] = useState(false);
const [apiError, setApiError] = useState<string | null>(null);
const { tienePermiso, isSuperAdmin } = usePermissions();
const puedeVerReporte = isSuperAdmin || tienePermiso("RR011");
const handleGenerateReport = async (params: { fechaDesde: string; fechaHasta: string; }) => {
setLoading(true);
setApiError(null);
try {
const { fileContent, fileName } = await reporteService.getReporteDistribucionSuscripcionesPdf(params.fechaDesde, params.fechaHasta);
const url = window.URL.createObjectURL(new Blob([fileContent], { type: 'application/pdf' }));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
link.parentNode?.removeChild(link);
window.URL.revokeObjectURL(url);
} catch (err: any) {
let message = 'Ocurrió un error al generar el reporte.';
if (axios.isAxiosError(err) && err.response) {
if (err.response.status === 404) {
message = "No se encontraron datos para los parámetros seleccionados.";
} else if (err.response.data instanceof Blob && err.response.data.type === "application/json") {
const errorText = await err.response.data.text();
try {
const errorJson = JSON.parse(errorText);
message = errorJson.message || message;
} catch {
message = errorText || message;
}
}
}
setApiError(message);
} finally {
setLoading(false);
}
};
if (!puedeVerReporte) {
return <Alert severity="error">No tiene permiso para ver este reporte.</Alert>;
}
return (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'flex-start', pt: 4 }}>
<Paper elevation={3} sx={{ borderRadius: '8px' }}>
<SeleccionaReporteDistribucionSuscripciones
onGenerarReporte={handleGenerateReport}
isLoading={loading}
apiErrorMessage={apiError}
/>
</Paper>
</Box>
);
};
export default ReporteDistribucionSuscripcionesPage;

View File

@@ -24,6 +24,7 @@ const allReportModules: { category: string; label: string; path: string }[] = [
{ category: 'Novedades de Canillitas', label: 'Novedades de Canillitas', path: 'novedades-canillas' },
{ category: 'Listados Distribución', label: 'Dist. Mensual Can/Acc', path: 'listado-distribucion-mensual' },
{ category: 'Suscripciones', label: 'Facturas para Publicidad', path: 'suscripciones-facturas-publicidad' },
{ category: 'Suscripciones', label: 'Distribución de Suscripciones', path: 'suscripciones-distribucion' },
];
const predefinedCategoryOrder = [

View File

@@ -0,0 +1,63 @@
import React, { useState } from 'react';
import { Box, Typography, Button, CircularProgress, Alert, TextField } from '@mui/material';
interface SeleccionaReporteProps {
onGenerarReporte: (params: { fechaDesde: string, fechaHasta: string }) => Promise<void>;
isLoading?: boolean;
apiErrorMessage?: string | null;
}
const SeleccionaReporteDistribucionSuscripciones: React.FC<SeleccionaReporteProps> = ({
onGenerarReporte,
isLoading,
apiErrorMessage
}) => {
const [fechaDesde, setFechaDesde] = useState(new Date().toISOString().split('T')[0]);
const [fechaHasta, setFechaHasta] = useState(new Date().toISOString().split('T')[0]);
const handleGenerar = () => {
onGenerarReporte({ fechaDesde, fechaHasta });
};
return (
<Box sx={{ p: 2, border: '1px solid #ccc', borderRadius: '4px', minWidth: 400 }}>
<Typography variant="h6" gutterBottom>
Reporte de Distribución de Suscripciones
</Typography>
<Typography variant="body2" color="text.secondary" sx={{mb: 2}}>
Seleccione un rango de fechas para generar el listado de suscriptores activos y sus detalles de entrega.
</Typography>
<Box sx={{ display: 'flex', gap: 2, alignItems: 'center', mb: 2 }}>
<TextField
label="Fecha Desde"
type="date"
fullWidth
value={fechaDesde}
onChange={(e) => setFechaDesde(e.target.value)}
InputLabelProps={{ shrink: true }}
disabled={isLoading}
/>
<TextField
label="Fecha Hasta"
type="date"
fullWidth
value={fechaHasta}
onChange={(e) => setFechaHasta(e.target.value)}
InputLabelProps={{ shrink: true }}
disabled={isLoading}
/>
</Box>
{apiErrorMessage && <Alert severity="error" sx={{ mt: 2 }}>{apiErrorMessage}</Alert>}
<Box sx={{ mt: 3, display: 'flex', justifyContent: 'flex-end', gap: 1 }}>
<Button onClick={handleGenerar} variant="contained" disabled={isLoading}>
{isLoading ? <CircularProgress size={24} /> : 'Generar Reporte'}
</Button>
</Box>
</Box>
);
};
export default SeleccionaReporteDistribucionSuscripciones;

View File

@@ -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<SuscriptorDto | null>(null);
const [ajustes, setAjustes] = useState<AjusteDto[]>([]);
const [empresas, setEmpresas] = useState<EmpresaDropdownDto[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(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 }}
/>
<TextField
@@ -159,7 +167,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => {
type="date"
size="small"
value={filtroFechaHasta}
onChange={handleFechaHastaChange} // <-- USAR NUEVO HANDLER
onChange={handleFechaHastaChange}
InputLabelProps={{ shrink: true }}
/>
</Box>
@@ -178,6 +186,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => {
<TableHead>
<TableRow>
<TableCell>Fecha Ajuste</TableCell>
<TableCell>Empresa</TableCell>
<TableCell>Tipo</TableCell>
<TableCell>Motivo</TableCell>
<TableCell align="right">Monto</TableCell>
@@ -188,13 +197,14 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => {
</TableHead>
<TableBody>
{loading ? (
<TableRow><TableCell colSpan={7} align="center"><CircularProgress size={24} /></TableCell></TableRow>
<TableRow><TableCell colSpan={8} align="center"><CircularProgress size={24} /></TableCell></TableRow>
) : ajustes.length === 0 ? (
<TableRow><TableCell colSpan={7} align="center">No se encontraron ajustes para los filtros seleccionados.</TableCell></TableRow>
<TableRow><TableCell colSpan={8} align="center">No se encontraron ajustes para los filtros seleccionados.</TableCell></TableRow>
) : (
ajustes.map(a => (
<TableRow key={a.idAjuste} sx={{ '& .MuiTableCell-root': { color: a.estado === 'Anulado' ? 'text.disabled' : 'inherit' }, textDecoration: a.estado === 'Anulado' ? 'line-through' : 'none' }}>
<TableCell>{formatDisplayDate(a.fechaAjuste)}</TableCell>
<TableCell>{a.nombreEmpresa || 'N/A'}</TableCell>
<TableCell>
<Chip label={a.tipoAjuste} size="small" color={a.tipoAjuste === 'Credito' ? 'success' : 'error'} />
</TableCell>
@@ -233,6 +243,7 @@ const CuentaCorrienteSuscriptorPage: React.FC = () => {
initialData={editingAjuste}
errorMessage={apiErrorMessage}
clearErrorMessage={() => setApiErrorMessage(null)}
empresas={empresas}
/>
</Box>
);

View File

@@ -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 = () => {
<ReporteFacturasPublicidadPage />
</SectionProtectedRoute>
}/>
<Route path="suscripciones-distribucion" element={
<SectionProtectedRoute requiredPermission="RR011" sectionName="Reporte Distribución de Suscripciones">
<ReporteDistribucionSuscripcionesPage />
</SectionProtectedRoute>
}/>
</Route>
{/* Módulo de Radios (anidado) */}

View File

@@ -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<Blob> => {
@@ -420,40 +420,63 @@ const getCanillasGananciasReporte = async (params: GetNovedadesCanillasParams):
};
const getListadoDistMensualDiarios = async (params: GetListadoDistMensualParams): Promise<ListadoDistCanMensualDiariosDto[]> => {
const response = await apiClient.get<ListadoDistCanMensualDiariosDto[]>('/reportes/listado-distribucion-mensual/diarios', { params });
return response.data;
const response = await apiClient.get<ListadoDistCanMensualDiariosDto[]>('/reportes/listado-distribucion-mensual/diarios', { params });
return response.data;
};
const getListadoDistMensualDiariosPdf = async (params: GetListadoDistMensualParams): Promise<Blob> => {
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<ListadoDistCanMensualPubDto[]> => {
const response = await apiClient.get<ListadoDistCanMensualPubDto[]>('/reportes/listado-distribucion-mensual/publicaciones', { params });
return response.data;
const response = await apiClient.get<ListadoDistCanMensualPubDto[]>('/reportes/listado-distribucion-mensual/publicaciones', { params });
return response.data;
};
const getListadoDistMensualPorPublicacionPdf = async (params: GetListadoDistMensualParams): Promise<Blob> => {
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;