Backend API:
- Recargos por Zona (`dist_RecargoZona`):
  - CRUD completo (Modelos, DTOs, Repositorio, Servicio, Controlador).
  - Endpoints anidados bajo `/publicaciones/{idPublicacion}/recargos`.
  - Lógica de negocio para vigencias (cierre/reapertura de períodos).
  - Auditoría en `dist_RecargoZona_H`.
- Porcentajes de Pago Distribuidores (`dist_PorcPago`):
  - CRUD completo (Modelos, DTOs, Repositorio, Servicio, Controlador).
  - Endpoints anidados bajo `/publicaciones/{idPublicacion}/porcentajespago`.
  - Lógica de negocio para vigencias.
  - Auditoría en `dist_PorcPago_H`.
- Porcentajes/Montos Pago Canillitas (`dist_PorcMonPagoCanilla`):
  - CRUD completo (Modelos, DTOs, Repositorio, Servicio, Controlador).
  - Endpoints anidados bajo `/publicaciones/{idPublicacion}/porcentajesmoncanilla`.
  - Lógica de negocio para vigencias.
  - Auditoría en `dist_PorcMonPagoCanilla_H`.
- Secciones de Publicación (`dist_dtPubliSecciones`):
  - CRUD completo (Modelos, DTOs, Repositorio, Servicio, Controlador).
  - Endpoints anidados bajo `/publicaciones/{idPublicacion}/secciones`.
  - Auditoría en `dist_dtPubliSecciones_H`.
- Entradas/Salidas Distribuidores (`dist_EntradasSalidas`):
  - Implementado backend (Modelos, DTOs, Repositorio, Servicio, Controlador).
  - Lógica para determinar precios/recargos/porcentajes aplicables.
  - Cálculo de monto y afectación de saldos de distribuidores en `cue_Saldos`.
  - Auditoría en `dist_EntradasSalidas_H`.
- Correcciones de Mapeo Dapper:
  - Aplicados alias explícitos en repositorios de RecargoZona, PorcPago, PorcMonCanilla, PubliSeccion,
    Canilla, Distribuidor y Precio para asegurar mapeo correcto de IDs y columnas.
Frontend React:
- Recargos por Zona:
  - `recargoZonaService.ts`.
  - `RecargoZonaFormModal.tsx` para crear/editar períodos de recargos.
  - `GestionarRecargosPublicacionPage.tsx` para listar y gestionar recargos por publicación.
- Porcentajes de Pago Distribuidores:
  - `porcPagoService.ts`.
  - `PorcPagoFormModal.tsx`.
  - `GestionarPorcentajesPagoPage.tsx`.
- Porcentajes/Montos Pago Canillitas:
  - `porcMonCanillaService.ts`.
  - `PorcMonCanillaFormModal.tsx`.
  - `GestionarPorcMonCanillaPage.tsx`.
- Secciones de Publicación:
  - `publiSeccionService.ts`.
  - `PubliSeccionFormModal.tsx`.
  - `GestionarSeccionesPublicacionPage.tsx`.
- Navegación:
  - Actualizadas rutas y menús para acceder a la gestión de recargos, porcentajes (dist. y canillita) y secciones desde la vista de una publicación.
- Layout:
  - Uso consistente de `Box` con Flexbox en lugar de `Grid` en nuevos modales y páginas para evitar errores de tipo.
		
	
		
			
				
	
	
		
			257 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			257 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Dapper;
 | |
| using GestionIntegral.Api.Models.Distribucion;
 | |
| using Microsoft.Extensions.Logging;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Data;
 | |
| using System.Linq;
 | |
| using System.Threading.Tasks;
 | |
| 
 | |
| namespace GestionIntegral.Api.Data.Repositories.Distribucion
 | |
| {
 | |
|     public class PorcMonCanillaRepository : IPorcMonCanillaRepository
 | |
|     {
 | |
|         private readonly DbConnectionFactory _cf;
 | |
|         private readonly ILogger<PorcMonCanillaRepository> _log;
 | |
| 
 | |
|         public PorcMonCanillaRepository(DbConnectionFactory connectionFactory, ILogger<PorcMonCanillaRepository> logger)
 | |
|         {
 | |
|             _cf = connectionFactory;
 | |
|             _log = logger;
 | |
|         }
 | |
| 
 | |
|         public async Task<IEnumerable<(PorcMonCanilla Item, string NomApeCanilla)>> GetByPublicacionIdAsync(int idPublicacion)
 | |
|         {
 | |
|             const string sql = @"
 | |
|                 SELECT 
 | |
|                     pmc.Id_PorcMon AS IdPorcMon, pmc.Id_Publicacion AS IdPublicacion, pmc.Id_Canilla AS IdCanilla,
 | |
|                     pmc.VigenciaD, pmc.VigenciaH, pmc.PorcMon, pmc.EsPorcentaje,
 | |
|                     c.NomApe AS NomApeCanilla
 | |
|                 FROM dbo.dist_PorcMonPagoCanilla pmc
 | |
|                 INNER JOIN dbo.dist_dtCanillas c ON pmc.Id_Canilla = c.Id_Canilla
 | |
|                 WHERE pmc.Id_Publicacion = @IdPublicacionParam
 | |
|                 ORDER BY c.NomApe, pmc.VigenciaD DESC";
 | |
|             try
 | |
|             {
 | |
|                 using var connection = _cf.CreateConnection();
 | |
|                 return await connection.QueryAsync<PorcMonCanilla, string, (PorcMonCanilla, string)>(
 | |
|                     sql,
 | |
|                     (item, nomApe) => (item, nomApe),
 | |
|                     new { IdPublicacionParam = idPublicacion },
 | |
|                     splitOn: "NomApeCanilla"
 | |
|                 );
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _log.LogError(ex, "Error al obtener PorcMonCanilla por Publicacion ID: {IdPublicacion}", idPublicacion);
 | |
|                 return Enumerable.Empty<(PorcMonCanilla, string)>();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async Task<PorcMonCanilla?> GetByIdAsync(int idPorcMon)
 | |
|         {
 | |
|             const string sql = @"
 | |
|                 SELECT 
 | |
|                     Id_PorcMon AS IdPorcMon, Id_Publicacion AS IdPublicacion, Id_Canilla AS IdCanilla,
 | |
|                     VigenciaD, VigenciaH, PorcMon, EsPorcentaje
 | |
|                 FROM dbo.dist_PorcMonPagoCanilla 
 | |
|                 WHERE Id_PorcMon = @IdPorcMonParam";
 | |
|             try
 | |
|             {
 | |
|                 using var connection = _cf.CreateConnection();
 | |
|                 return await connection.QuerySingleOrDefaultAsync<PorcMonCanilla>(sql, new { IdPorcMonParam = idPorcMon });
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                  _log.LogError(ex, "Error al obtener PorcMonCanilla por ID: {IdPorcMon}", idPorcMon);
 | |
|                 return null;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async Task<PorcMonCanilla?> GetActiveByPublicacionCanillaAndDateAsync(int idPublicacion, int idCanilla, DateTime fecha, IDbTransaction? transaction = null)
 | |
|         {
 | |
|             const string sql = @"
 | |
|                 SELECT TOP 1 
 | |
|                     Id_PorcMon AS IdPorcMon, Id_Publicacion AS IdPublicacion, Id_Canilla AS IdCanilla,
 | |
|                     VigenciaD, VigenciaH, PorcMon, EsPorcentaje
 | |
|                 FROM dbo.dist_PorcMonPagoCanilla
 | |
|                 WHERE Id_Publicacion = @IdPublicacionParam AND Id_Canilla = @IdCanillaParam
 | |
|                   AND VigenciaD <= @FechaParam
 | |
|                   AND (VigenciaH IS NULL OR VigenciaH >= @FechaParam)
 | |
|                 ORDER BY VigenciaD DESC;";
 | |
|             
 | |
|             var cn = transaction?.Connection ?? _cf.CreateConnection();
 | |
|             bool ownConnection = transaction == null;
 | |
|             PorcMonCanilla? result = null;
 | |
|             try
 | |
|             {
 | |
|                 if (ownConnection && cn.State == ConnectionState.Closed && cn is System.Data.Common.DbConnection dbConn) await dbConn.OpenAsync();
 | |
|                 result = await cn.QuerySingleOrDefaultAsync<PorcMonCanilla>(sql, 
 | |
|                     new { IdPublicacionParam = idPublicacion, IdCanillaParam = idCanilla, FechaParam = fecha.Date }, 
 | |
|                     transaction);
 | |
|             }
 | |
|             finally
 | |
|             {
 | |
|                 if (ownConnection && cn.State == ConnectionState.Open && cn is System.Data.Common.DbConnection dbConnClose) await dbConnClose.CloseAsync();
 | |
|                 if (ownConnection) (cn as IDisposable)?.Dispose();
 | |
|             }
 | |
|             return result;
 | |
|         }
 | |
| 
 | |
|         public async Task<PorcMonCanilla?> GetPreviousActiveAsync(int idPublicacion, int idCanilla, DateTime vigenciaDNuevo, IDbTransaction transaction)
 | |
|         {
 | |
|             const string sql = @"
 | |
|                 SELECT TOP 1 
 | |
|                     Id_PorcMon AS IdPorcMon, Id_Publicacion AS IdPublicacion, Id_Canilla AS IdCanilla,
 | |
|                     VigenciaD, VigenciaH, PorcMon, EsPorcentaje
 | |
|                 FROM dbo.dist_PorcMonPagoCanilla
 | |
|                 WHERE Id_Publicacion = @IdPublicacionParam AND Id_Canilla = @IdCanillaParam
 | |
|                   AND VigenciaD < @VigenciaDNuevoParam
 | |
|                   AND VigenciaH IS NULL
 | |
|                 ORDER BY VigenciaD DESC;";
 | |
|             return await transaction.Connection!.QuerySingleOrDefaultAsync<PorcMonCanilla>(sql, 
 | |
|                 new { IdPublicacionParam = idPublicacion, IdCanillaParam = idCanilla, VigenciaDNuevoParam = vigenciaDNuevo.Date }, 
 | |
|                 transaction);
 | |
|         }
 | |
| 
 | |
|         public async Task<PorcMonCanilla?> CreateAsync(PorcMonCanilla nuevoItem, int idUsuario, IDbTransaction transaction)
 | |
|         {
 | |
|             const string sqlInsert = @"
 | |
|                 INSERT INTO dbo.dist_PorcMonPagoCanilla (Id_Publicacion, Id_Canilla, VigenciaD, VigenciaH, PorcMon, EsPorcentaje)
 | |
|                 OUTPUT INSERTED.Id_PorcMon AS IdPorcMon, INSERTED.Id_Publicacion AS IdPublicacion, INSERTED.Id_Canilla AS IdCanilla,
 | |
|                        INSERTED.VigenciaD, INSERTED.VigenciaH, INSERTED.PorcMon, INSERTED.EsPorcentaje
 | |
|                 VALUES (@IdPublicacion, @IdCanilla, @VigenciaD, @VigenciaH, @PorcMon, @EsPorcentaje);";
 | |
|             const string sqlInsertHistorico = @"
 | |
|                 INSERT INTO dbo.dist_PorcMonPagoCanilla_H (Id_PorcMon, Id_Publicacion, Id_Canilla, VigenciaD, VigenciaH, PorcMon, EsPorcentaje, Id_Usuario, FechaMod, TipoMod)
 | |
|                 VALUES (@IdPorcMonParam, @IdPublicacionParam, @IdCanillaParam, @VigenciaDParam, @VigenciaHParam, @PorcMonParam, @EsPorcentajeParam, @IdUsuarioParam, @FechaModParam, @TipoModParam);";
 | |
| 
 | |
|             var inserted = await transaction.Connection!.QuerySingleAsync<PorcMonCanilla>(sqlInsert, nuevoItem, transaction);
 | |
|             if (inserted == null || inserted.IdPorcMon == 0) throw new DataException("Error al crear PorcMonCanilla o ID no generado.");
 | |
| 
 | |
|             await transaction.Connection!.ExecuteAsync(sqlInsertHistorico, new
 | |
|             {
 | |
|                 IdPorcMonParam = inserted.IdPorcMon,
 | |
|                 IdPublicacionParam = inserted.IdPublicacion,
 | |
|                 IdCanillaParam = inserted.IdCanilla,
 | |
|                 VigenciaDParam = inserted.VigenciaD,
 | |
|                 VigenciaHParam = inserted.VigenciaH,
 | |
|                 PorcMonParam = inserted.PorcMon,
 | |
|                 EsPorcentajeParam = inserted.EsPorcentaje,
 | |
|                 IdUsuarioParam = idUsuario,
 | |
|                 FechaModParam = DateTime.Now,
 | |
|                 TipoModParam = "Creado"
 | |
|             }, transaction);
 | |
|             return inserted;
 | |
|         }
 | |
| 
 | |
|         public async Task<bool> UpdateAsync(PorcMonCanilla itemAActualizar, int idUsuario, IDbTransaction transaction)
 | |
|         {
 | |
|             var actual = await transaction.Connection!.QuerySingleOrDefaultAsync<PorcMonCanilla>(
 | |
|                 @"SELECT Id_PorcMon AS IdPorcMon, Id_Publicacion AS IdPublicacion, Id_Canilla AS IdCanilla, 
 | |
|                          VigenciaD, VigenciaH, PorcMon, EsPorcentaje
 | |
|                   FROM dbo.dist_PorcMonPagoCanilla WHERE Id_PorcMon = @IdPorcMonParam",
 | |
|                 new { IdPorcMonParam = itemAActualizar.IdPorcMon }, transaction);
 | |
|             if (actual == null) throw new KeyNotFoundException("Registro PorcMonCanilla no encontrado.");
 | |
| 
 | |
|             const string sqlUpdate = @"
 | |
|                 UPDATE dbo.dist_PorcMonPagoCanilla SET
 | |
|                     PorcMon = @PorcMon, EsPorcentaje = @EsPorcentaje, VigenciaH = @VigenciaH
 | |
|                 WHERE Id_PorcMon = @IdPorcMon;";
 | |
|             const string sqlInsertHistorico = @"
 | |
|                 INSERT INTO dbo.dist_PorcMonPagoCanilla_H (Id_PorcMon, Id_Publicacion, Id_Canilla, VigenciaD, VigenciaH, PorcMon, EsPorcentaje, Id_Usuario, FechaMod, TipoMod)
 | |
|                 VALUES (@IdPorcMonParam, @IdPublicacionParam, @IdCanillaParam, @VigenciaDParam, @VigenciaHParam, @PorcMonParam, @EsPorcentajeParam, @IdUsuarioParam, @FechaModParam, @TipoModParam);";
 | |
| 
 | |
|             await transaction.Connection!.ExecuteAsync(sqlInsertHistorico, new
 | |
|             {
 | |
|                 IdPorcMonParam = actual.IdPorcMon,
 | |
|                 IdPublicacionParam = actual.IdPublicacion,
 | |
|                 IdCanillaParam = actual.IdCanilla,
 | |
|                 VigenciaDParam = actual.VigenciaD,
 | |
|                 VigenciaHParam = actual.VigenciaH, // Valor ANTERIOR
 | |
|                 PorcMonParam = actual.PorcMon, // Valor ANTERIOR
 | |
|                 EsPorcentajeParam = actual.EsPorcentaje, // Valor ANTERIOR
 | |
|                 IdUsuarioParam = idUsuario,
 | |
|                 FechaModParam = DateTime.Now,
 | |
|                 TipoModParam = "Actualizado"
 | |
|             }, transaction);
 | |
| 
 | |
|             var rowsAffected = await transaction.Connection!.ExecuteAsync(sqlUpdate, itemAActualizar, transaction);
 | |
|             return rowsAffected == 1;
 | |
|         }
 | |
| 
 | |
|         public async Task<bool> DeleteAsync(int idPorcMon, int idUsuario, IDbTransaction transaction)
 | |
|         {
 | |
|             var actual = await transaction.Connection!.QuerySingleOrDefaultAsync<PorcMonCanilla>(
 | |
|                @"SELECT Id_PorcMon AS IdPorcMon, Id_Publicacion AS IdPublicacion, Id_Canilla AS IdCanilla, 
 | |
|                         VigenciaD, VigenciaH, PorcMon, EsPorcentaje
 | |
|                  FROM dbo.dist_PorcMonPagoCanilla WHERE Id_PorcMon = @IdPorcMonParam", 
 | |
|                new { IdPorcMonParam = idPorcMon }, transaction);
 | |
|             if (actual == null) throw new KeyNotFoundException("Registro PorcMonCanilla no encontrado para eliminar.");
 | |
| 
 | |
|             const string sqlDelete = "DELETE FROM dbo.dist_PorcMonPagoCanilla WHERE Id_PorcMon = @IdPorcMonParam";
 | |
|              const string sqlInsertHistorico = @"
 | |
|                 INSERT INTO dbo.dist_PorcMonPagoCanilla_H (Id_PorcMon, Id_Publicacion, Id_Canilla, VigenciaD, VigenciaH, PorcMon, EsPorcentaje, Id_Usuario, FechaMod, TipoMod)
 | |
|                 VALUES (@IdPorcMonParam, @IdPublicacionParam, @IdCanillaParam, @VigenciaDParam, @VigenciaHParam, @PorcMonParam, @EsPorcentajeParam, @IdUsuarioParam, @FechaModParam, @TipoModParam);";
 | |
| 
 | |
|             await transaction.Connection!.ExecuteAsync(sqlInsertHistorico, new
 | |
|             {
 | |
|                 IdPorcMonParam = actual.IdPorcMon,
 | |
|                 IdPublicacionParam = actual.IdPublicacion,
 | |
|                 IdCanillaParam = actual.IdCanilla,
 | |
|                 VigenciaDParam = actual.VigenciaD,
 | |
|                 VigenciaHParam = actual.VigenciaH,
 | |
|                 PorcMonParam = actual.PorcMon,
 | |
|                 EsPorcentajeParam = actual.EsPorcentaje,
 | |
|                 IdUsuarioParam = idUsuario,
 | |
|                 FechaModParam = DateTime.Now,
 | |
|                 TipoModParam = "Eliminado"
 | |
|             }, transaction);
 | |
| 
 | |
|             var rowsAffected = await transaction.Connection!.ExecuteAsync(sqlDelete, new { IdPorcMonParam = idPorcMon }, transaction);
 | |
|             return rowsAffected == 1;
 | |
|         }
 | |
| 
 | |
|         public async Task<bool> DeleteByPublicacionIdAsync(int idPublicacion, int idUsuarioAuditoria, IDbTransaction transaction)
 | |
|         {
 | |
|             const string selectSql = @"
 | |
|                 SELECT Id_PorcMon AS IdPorcMon, Id_Publicacion AS IdPublicacion, Id_Canilla AS IdCanilla, 
 | |
|                        VigenciaD, VigenciaH, PorcMon, EsPorcentaje 
 | |
|                 FROM dbo.dist_PorcMonPagoCanilla WHERE Id_Publicacion = @IdPublicacionParam";
 | |
|             var itemsToDelete = await transaction.Connection!.QueryAsync<PorcMonCanilla>(selectSql, new { IdPublicacionParam = idPublicacion }, transaction);
 | |
| 
 | |
|             if (itemsToDelete.Any())
 | |
|             {
 | |
|                 const string insertHistoricoSql = @"
 | |
|                     INSERT INTO dbo.dist_PorcMonPagoCanilla_H (Id_PorcMon, Id_Publicacion, Id_Canilla, VigenciaD, VigenciaH, PorcMon, EsPorcentaje, Id_Usuario, FechaMod, TipoMod)
 | |
|                     VALUES (@IdPorcMonParam, @IdPublicacionParam, @IdCanillaParam, @VigenciaDParam, @VigenciaHParam, @PorcMonParam, @EsPorcentajeParam, @IdUsuarioParam, @FechaModParam, @TipoModParam);";
 | |
|                 foreach (var item in itemsToDelete)
 | |
|                 {
 | |
|                     await transaction.Connection!.ExecuteAsync(insertHistoricoSql, new
 | |
|                     {
 | |
|                         IdPorcMonParam = item.IdPorcMon,
 | |
|                         IdPublicacionParam = item.IdPublicacion,
 | |
|                         IdCanillaParam = item.IdCanilla,
 | |
|                         VigenciaDParam = item.VigenciaD,
 | |
|                         VigenciaHParam = item.VigenciaH,
 | |
|                         PorcMonParam = item.PorcMon,
 | |
|                         EsPorcentajeParam = item.EsPorcentaje,
 | |
|                         IdUsuarioParam = idUsuarioAuditoria,
 | |
|                         FechaModParam = DateTime.Now,
 | |
|                         TipoModParam = "Eliminado (Cascada)"
 | |
|                     }, transaction);
 | |
|                 }
 | |
|             }
 | |
|             const string deleteSql = "DELETE FROM dbo.dist_PorcMonPagoCanilla WHERE Id_Publicacion = @IdPublicacionParam";
 | |
|             try
 | |
|             {
 | |
|                 await transaction.Connection!.ExecuteAsync(deleteSql, new { IdPublicacionParam = idPublicacion }, transaction: transaction);
 | |
|                 return true;
 | |
|             }
 | |
|             catch (System.Exception ex)
 | |
|             {
 | |
|                 _log.LogError(ex, "Error al eliminar PorcMonCanilla por IdPublicacion: {idPublicacion}", idPublicacion);
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| } |