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:
@@ -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 ?? "-");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
|
||||
@@ -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.")]
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.")]
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user