feat(contables): cierre mensual de cuenta corriente de distribuidor

Permite congelar el saldo de un distribuidor por empresa a una fecha de
corte y bloquear modificaciones retroactivas sobre el período cerrado.
El saldo se calcula sumando movimientos en rango (sin tocar cue_Saldos).
Incluye reapertura controlada exclusivamente por SuperAdmin, reporte con
saldo inicial, atajo "Desde último cierre", y auditoría del ciclo de
vida _H. Permisos CC001/CC002/CC003. Middleware global mapea bloqueos
por período cerrado a HTTP 409.
This commit is contained in:
2026-05-07 12:03:26 -03:00
parent 7e274ef114
commit 24eaf18fd9
62 changed files with 2813 additions and 162 deletions

View File

@@ -20,6 +20,7 @@ namespace GestionIntegral.Api.Services.Contables
private readonly ICanillaRepository _canillaRepo;
private readonly IEmpresaRepository _empresaRepo;
private readonly ISaldoRepository _saldoRepo;
private readonly IPeriodoCerradoValidator _periodoCerrado;
private readonly DbConnectionFactory _connectionFactory;
private readonly ILogger<NotaCreditoDebitoService> _logger;
@@ -29,6 +30,7 @@ namespace GestionIntegral.Api.Services.Contables
ICanillaRepository canillaRepo,
IEmpresaRepository empresaRepo,
ISaldoRepository saldoRepo,
IPeriodoCerradoValidator periodoCerrado,
DbConnectionFactory connectionFactory,
ILogger<NotaCreditoDebitoService> logger)
{
@@ -37,6 +39,7 @@ namespace GestionIntegral.Api.Services.Contables
_canillaRepo = canillaRepo;
_empresaRepo = empresaRepo;
_saldoRepo = saldoRepo;
_periodoCerrado = periodoCerrado;
_connectionFactory = connectionFactory;
_logger = logger;
}
@@ -111,6 +114,11 @@ namespace GestionIntegral.Api.Services.Contables
if (await _empresaRepo.GetByIdAsync(createDto.IdEmpresa) == null)
return (null, "La empresa especificada no existe.");
// Bloqueo por período cerrado (sólo aplica si Destino="Distribuidores"; el validator filtra internamente).
var bloqueoCrear = await _periodoCerrado.EstaCerradoAsync(createDto.Destino, createDto.IdDestino, createDto.IdEmpresa, createDto.Fecha);
if (bloqueoCrear.EstaCerrado)
throw new BloqueoPorPeriodoCerradoException(bloqueoCrear.IdCierre!.Value, bloqueoCrear.FechaCorte!.Value);
var nuevaNota = new NotaCreditoDebito
{
Destino = createDto.Destino,
@@ -187,6 +195,14 @@ namespace GestionIntegral.Api.Services.Contables
return (false, "Nota no encontrada.");
}
// Bloqueo por período cerrado sobre la fecha original (el DTO de update no permite cambiar Fecha).
var bloqueoUpd = await _periodoCerrado.EstaCerradoAsync(notaExistente.Destino, notaExistente.IdDestino, notaExistente.IdEmpresa, notaExistente.Fecha);
if (bloqueoUpd.EstaCerrado)
{
transaction.Rollback();
throw new BloqueoPorPeriodoCerradoException(bloqueoUpd.IdCierre!.Value, bloqueoUpd.FechaCorte!.Value);
}
decimal impactoOriginalSaldo = notaExistente.Tipo == "Credito" ? -notaExistente.Monto : notaExistente.Monto;
decimal impactoNuevoSaldo = notaExistente.Tipo == "Credito" ? -updateDto.Monto : updateDto.Monto;
decimal diferenciaAjusteSaldo = impactoNuevoSaldo - impactoOriginalSaldo;
@@ -252,6 +268,14 @@ namespace GestionIntegral.Api.Services.Contables
return (false, "Nota no encontrada.");
}
// Bloqueo por período cerrado: no se puede eliminar una nota cuya fecha cae en un cierre vigente.
var bloqueoDel = await _periodoCerrado.EstaCerradoAsync(notaExistente.Destino, notaExistente.IdDestino, notaExistente.IdEmpresa, notaExistente.Fecha);
if (bloqueoDel.EstaCerrado)
{
transaction.Rollback();
throw new BloqueoPorPeriodoCerradoException(bloqueoDel.IdCierre!.Value, bloqueoDel.FechaCorte!.Value);
}
decimal montoReversion = notaExistente.Tipo == "Credito" ? notaExistente.Monto : -notaExistente.Monto;
var eliminado = await _notaRepo.DeleteAsync(idNota, idUsuario, transaction);