| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class CompensacionRepository : ICompensacionRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 48 | 10 | | public CompensacionRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 48 | 12 | | _context = context; |
| | 48 | 13 | | } |
| | | 14 | | |
| | | 15 | | // ── Tipos ──────────────────────────────────────────────────────────────── |
| | | 16 | | |
| | | 17 | | public async Task<IEnumerable<TipoCompensacion>> GetTiposAsync(bool? activo = null) |
| | | 18 | | { |
| | 4 | 19 | | var query = _context.TiposCompensacion.AsQueryable(); |
| | 4 | 20 | | if (activo.HasValue) |
| | 2 | 21 | | query = query.Where(t => t.Vigente == activo.Value); |
| | 4 | 22 | | return await query.OrderBy(t => t.Nombre).ToListAsync(); |
| | 4 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<TipoCompensacion?> GetTipoByIdAsync(long id) |
| | 2 | 26 | | => await _context.TiposCompensacion.FindAsync(id); |
| | | 27 | | |
| | | 28 | | public async Task<TipoCompensacion?> GetTipoByCodigoAsync(string codigo) |
| | 2 | 29 | | => await _context.TiposCompensacion.FirstOrDefaultAsync(t => t.Codigo == codigo); |
| | | 30 | | |
| | | 31 | | public async Task<TipoCompensacion> CreateTipoAsync(TipoCompensacion tipo) |
| | | 32 | | { |
| | 1 | 33 | | _context.TiposCompensacion.Add(tipo); |
| | 1 | 34 | | await _context.SaveChangesAsync(); |
| | 1 | 35 | | return tipo; |
| | 1 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<TipoCompensacion> UpdateTipoAsync(TipoCompensacion tipo) |
| | | 39 | | { |
| | 1 | 40 | | _context.TiposCompensacion.Update(tipo); |
| | 1 | 41 | | await _context.SaveChangesAsync(); |
| | 1 | 42 | | return tipo; |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task DeleteTipoAsync(long id) |
| | | 46 | | { |
| | 2 | 47 | | var tipo = await _context.TiposCompensacion.FindAsync(id); |
| | 2 | 48 | | if (tipo != null) |
| | | 49 | | { |
| | 1 | 50 | | _context.TiposCompensacion.Remove(tipo); |
| | 1 | 51 | | await _context.SaveChangesAsync(); |
| | | 52 | | } |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<bool> TieneConfiguracionesAsync(long tipoId) |
| | 2 | 56 | | => await _context.ConfigCompensaciones.AnyAsync(c => c.TipoCompensacionId == tipoId); |
| | | 57 | | |
| | | 58 | | public async Task<bool> TieneLotesAsync(long tipoId) |
| | 2 | 59 | | => await _context.LotesCompensacion.AnyAsync(l => l.TipoCompensacionId == tipoId); |
| | | 60 | | |
| | | 61 | | // ── Configs ────────────────────────────────────────────────────────────── |
| | | 62 | | |
| | | 63 | | public async Task<IEnumerable<ConfigCompensacion>> GetConfigsAsync( |
| | | 64 | | long? tipoId = null, long? regimenId = null, long? gradoId = null, bool? activo = null) |
| | | 65 | | { |
| | 4 | 66 | | var query = _context.ConfigCompensaciones |
| | 4 | 67 | | .Include(c => c.TipoCompensacion) |
| | 4 | 68 | | .Include(c => c.Regimen) |
| | 4 | 69 | | .Include(c => c.Grado) |
| | 4 | 70 | | .Include(c => c.Escalafon) |
| | 4 | 71 | | .AsQueryable(); |
| | | 72 | | |
| | 5 | 73 | | if (tipoId.HasValue) query = query.Where(c => c.TipoCompensacionId == tipoId.Value); |
| | 4 | 74 | | if (regimenId.HasValue) query = query.Where(c => c.RegimenId == regimenId.Value); |
| | 4 | 75 | | if (gradoId.HasValue) query = query.Where(c => c.GradoId == gradoId.Value); |
| | 6 | 76 | | if (activo.HasValue) query = query.Where(c => c.Activo == activo.Value); |
| | | 77 | | |
| | 4 | 78 | | return await query.OrderBy(c => c.TipoCompensacionId).ThenBy(c => c.VigenteDesde).ToListAsync(); |
| | 4 | 79 | | } |
| | | 80 | | |
| | | 81 | | public async Task<ConfigCompensacion?> GetConfigByIdAsync(long id) |
| | 2 | 82 | | => await _context.ConfigCompensaciones |
| | 2 | 83 | | .Include(c => c.TipoCompensacion) |
| | 2 | 84 | | .Include(c => c.Regimen) |
| | 2 | 85 | | .Include(c => c.Grado) |
| | 2 | 86 | | .Include(c => c.Escalafon) |
| | 2 | 87 | | .FirstOrDefaultAsync(c => c.Id == id); |
| | | 88 | | |
| | | 89 | | public async Task<ConfigCompensacion?> GetConfigVigenteAsync(long tipoId, DateTime periodo) |
| | | 90 | | { |
| | 5 | 91 | | return await _context.ConfigCompensaciones |
| | 5 | 92 | | .Where(c => c.TipoCompensacionId == tipoId |
| | 5 | 93 | | && c.Activo |
| | 5 | 94 | | && c.VigenteDesde <= periodo |
| | 5 | 95 | | && (c.VigenteHasta == null || c.VigenteHasta >= periodo)) |
| | 5 | 96 | | .OrderByDescending(c => c.VigenteDesde) |
| | 5 | 97 | | .FirstOrDefaultAsync(); |
| | 5 | 98 | | } |
| | | 99 | | |
| | | 100 | | public async Task<bool> EsUnicaConfigActivaAsync(long configId, long tipoId) |
| | 2 | 101 | | => !await _context.ConfigCompensaciones |
| | 2 | 102 | | .AnyAsync(c => c.TipoCompensacionId == tipoId && c.Activo && c.Id != configId); |
| | | 103 | | |
| | | 104 | | public async Task DesactivarConfigsActivasAsync(long tipoId) |
| | | 105 | | { |
| | 1 | 106 | | await _context.ConfigCompensaciones |
| | 1 | 107 | | .Where(c => c.TipoCompensacionId == tipoId && c.Activo) |
| | 1 | 108 | | .ExecuteUpdateAsync(s => s.SetProperty(c => c.Activo, false)); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | public async Task<ConfigCompensacion> CreateConfigAsync(ConfigCompensacion config) |
| | | 112 | | { |
| | 1 | 113 | | _context.ConfigCompensaciones.Add(config); |
| | 1 | 114 | | await _context.SaveChangesAsync(); |
| | 1 | 115 | | return config; |
| | 1 | 116 | | } |
| | | 117 | | |
| | | 118 | | public async Task<ConfigCompensacion> UpdateConfigAsync(ConfigCompensacion config) |
| | | 119 | | { |
| | 0 | 120 | | _context.ConfigCompensaciones.Update(config); |
| | 0 | 121 | | await _context.SaveChangesAsync(); |
| | 0 | 122 | | return config; |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | public async Task DeleteConfigAsync(long id) |
| | | 126 | | { |
| | 1 | 127 | | var config = await _context.ConfigCompensaciones.FindAsync(id); |
| | 1 | 128 | | if (config != null) |
| | | 129 | | { |
| | 1 | 130 | | _context.ConfigCompensaciones.Remove(config); |
| | 1 | 131 | | await _context.SaveChangesAsync(); |
| | | 132 | | } |
| | 1 | 133 | | } |
| | | 134 | | |
| | | 135 | | // ── Lotes ──────────────────────────────────────────────────────────────── |
| | | 136 | | |
| | | 137 | | public async Task<(IEnumerable<LoteCompensacion> Items, int TotalCount)> GetLotesPagedAsync( |
| | | 138 | | int page, int pageSize, long? tipoId = null, int? anio = null, int? mes = null, string? estado = null) |
| | | 139 | | { |
| | 0 | 140 | | var query = _context.LotesCompensacion |
| | 0 | 141 | | .Include(l => l.TipoCompensacion) |
| | 0 | 142 | | .Include(l => l.AprobadorUsuario) |
| | 0 | 143 | | .AsQueryable(); |
| | | 144 | | |
| | 0 | 145 | | if (tipoId.HasValue) query = query.Where(l => l.TipoCompensacionId == tipoId.Value); |
| | 0 | 146 | | if (anio.HasValue) query = query.Where(l => l.Periodo.Year == anio.Value); |
| | 0 | 147 | | if (mes.HasValue) query = query.Where(l => l.Periodo.Month == mes.Value); |
| | 0 | 148 | | if (!string.IsNullOrWhiteSpace(estado)) query = query.Where(l => l.Estado == estado); |
| | | 149 | | |
| | 0 | 150 | | var totalCount = await query.CountAsync(); |
| | 0 | 151 | | var items = await query |
| | 0 | 152 | | .OrderByDescending(l => l.Periodo) |
| | 0 | 153 | | .ThenBy(l => l.TipoCompensacionId) |
| | 0 | 154 | | .Skip((page - 1) * pageSize) |
| | 0 | 155 | | .Take(pageSize) |
| | 0 | 156 | | .ToListAsync(); |
| | | 157 | | |
| | | 158 | | // Batch count de items sin cargar los items completos |
| | 0 | 159 | | var ids = items.Select(l => l.Id).ToList(); |
| | 0 | 160 | | var conteos = await _context.ItemsLoteCompensacion |
| | 0 | 161 | | .Where(i => ids.Contains(i.LoteCompensacionId)) |
| | 0 | 162 | | .GroupBy(i => i.LoteCompensacionId) |
| | 0 | 163 | | .Select(g => new { LoteId = g.Key, Count = g.Count() }) |
| | 0 | 164 | | .ToDictionaryAsync(x => x.LoteId, x => x.Count); |
| | | 165 | | |
| | 0 | 166 | | foreach (var lote in items) |
| | 0 | 167 | | lote.ItemCount = conteos.TryGetValue(lote.Id, out var c) ? c : 0; |
| | | 168 | | |
| | 0 | 169 | | return (items, totalCount); |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | public async Task<LoteCompensacion?> GetLoteByIdWithItemsAsync(long id) |
| | 0 | 173 | | => await _context.LotesCompensacion |
| | 0 | 174 | | .Include(l => l.TipoCompensacion) |
| | 0 | 175 | | .Include(l => l.AprobadorUsuario) |
| | 0 | 176 | | .Include(l => l.Items) |
| | 0 | 177 | | .ThenInclude(i => i.Persona) |
| | 0 | 178 | | .Include(l => l.Items) |
| | 0 | 179 | | .ThenInclude(i => i.SnapshotGrado) |
| | 0 | 180 | | .Include(l => l.Items) |
| | 0 | 181 | | .ThenInclude(i => i.SnapshotSituacion) |
| | 0 | 182 | | .Include(l => l.Items) |
| | 0 | 183 | | .ThenInclude(i => i.SnapshotUnidad) |
| | 0 | 184 | | .FirstOrDefaultAsync(l => l.Id == id); |
| | | 185 | | |
| | | 186 | | public async Task<LoteCompensacion?> GetLoteByIdAsync(long id) |
| | 0 | 187 | | => await _context.LotesCompensacion.FindAsync(id); |
| | | 188 | | |
| | | 189 | | public async Task<bool> ExisteLoteActivoAsync(long tipoId, DateTime periodo) |
| | 4 | 190 | | => await _context.LotesCompensacion.AnyAsync(l => |
| | 4 | 191 | | l.TipoCompensacionId == tipoId && |
| | 4 | 192 | | l.Periodo.Year == periodo.Year && |
| | 4 | 193 | | l.Periodo.Month == periodo.Month && |
| | 4 | 194 | | l.Estado != "rechazado"); |
| | | 195 | | |
| | | 196 | | public async Task<LoteCompensacion> CreateLoteAsync(LoteCompensacion lote) |
| | | 197 | | { |
| | 1 | 198 | | _context.LotesCompensacion.Add(lote); |
| | 1 | 199 | | await _context.SaveChangesAsync(); |
| | 1 | 200 | | return lote; |
| | 1 | 201 | | } |
| | | 202 | | |
| | | 203 | | public async Task<LoteCompensacion> UpdateLoteAsync(LoteCompensacion lote) |
| | | 204 | | { |
| | | 205 | | // Marcar solo propiedades editables como modificadas para evitar problemas con DateTime Kind |
| | 0 | 206 | | var entry = _context.Entry(lote); |
| | | 207 | | |
| | | 208 | | // Solo marcar como modificado si el valor es diferente del original |
| | 0 | 209 | | if (entry.Property(x => x.Estado).CurrentValue != entry.Property(x => x.Estado).OriginalValue) |
| | 0 | 210 | | entry.Property(x => x.Estado).IsModified = true; |
| | | 211 | | |
| | 0 | 212 | | if (entry.Property(x => x.Fuente).CurrentValue != entry.Property(x => x.Fuente).OriginalValue) |
| | 0 | 213 | | entry.Property(x => x.Fuente).IsModified = true; |
| | | 214 | | |
| | 0 | 215 | | if (entry.Property(x => x.NombreArchivo).CurrentValue != entry.Property(x => x.NombreArchivo).OriginalValue) |
| | 0 | 216 | | entry.Property(x => x.NombreArchivo).IsModified = true; |
| | | 217 | | |
| | 0 | 218 | | if (entry.Property(x => x.HashArchivo).CurrentValue != entry.Property(x => x.HashArchivo).OriginalValue) |
| | 0 | 219 | | entry.Property(x => x.HashArchivo).IsModified = true; |
| | | 220 | | |
| | 0 | 221 | | await _context.SaveChangesAsync(); |
| | 0 | 222 | | return lote; |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | public async Task DeleteLoteAsync(LoteCompensacion lote) |
| | | 226 | | { |
| | 0 | 227 | | _context.LotesCompensacion.Remove(lote); |
| | 0 | 228 | | await _context.SaveChangesAsync(); |
| | 0 | 229 | | } |
| | | 230 | | |
| | | 231 | | public async Task DeleteAllItemsAsync(long loteId) |
| | | 232 | | { |
| | 1 | 233 | | var items = await _context.ItemsLoteCompensacion |
| | 1 | 234 | | .Where(i => i.LoteCompensacionId == loteId) |
| | 1 | 235 | | .ToListAsync(); |
| | 1 | 236 | | _context.ItemsLoteCompensacion.RemoveRange(items); |
| | 1 | 237 | | await _context.SaveChangesAsync(); |
| | 1 | 238 | | } |
| | | 239 | | |
| | | 240 | | // ── Items ──────────────────────────────────────────────────────────────── |
| | | 241 | | |
| | | 242 | | public async Task<(IEnumerable<ItemLoteCompensacion> Items, int TotalCount)> GetItemsPagedAsync( |
| | | 243 | | long loteId, int page, int pageSize) |
| | | 244 | | { |
| | 0 | 245 | | var query = _context.ItemsLoteCompensacion |
| | 0 | 246 | | .Include(i => i.Persona) |
| | 0 | 247 | | .Include(i => i.SnapshotGrado) |
| | 0 | 248 | | .Include(i => i.SnapshotSituacion) |
| | 0 | 249 | | .Include(i => i.SnapshotUnidad) |
| | 0 | 250 | | .Where(i => i.LoteCompensacionId == loteId); |
| | | 251 | | |
| | 0 | 252 | | var totalCount = await query.CountAsync(); |
| | 0 | 253 | | var items = await query |
| | 0 | 254 | | .OrderBy(i => i.Id) |
| | 0 | 255 | | .Skip((page - 1) * pageSize) |
| | 0 | 256 | | .Take(pageSize) |
| | 0 | 257 | | .ToListAsync(); |
| | | 258 | | |
| | 0 | 259 | | return (items, totalCount); |
| | 0 | 260 | | } |
| | | 261 | | |
| | | 262 | | public async Task<ItemLoteCompensacion?> GetItemByIdAsync(long id) |
| | 2 | 263 | | => await _context.ItemsLoteCompensacion |
| | 2 | 264 | | .Include(i => i.Persona) |
| | 2 | 265 | | .FirstOrDefaultAsync(i => i.Id == id); |
| | | 266 | | |
| | | 267 | | public async Task<bool> ExistePersonaEnLoteAsync(long loteId, long personaId) |
| | 2 | 268 | | => await _context.ItemsLoteCompensacion |
| | 2 | 269 | | .AnyAsync(i => i.LoteCompensacionId == loteId && i.PersonaId == personaId); |
| | | 270 | | |
| | | 271 | | public async Task<decimal> GetMontoTotalItemsAsync(long loteId) |
| | 3 | 272 | | => await _context.ItemsLoteCompensacion |
| | 3 | 273 | | .Where(i => i.LoteCompensacionId == loteId) |
| | 3 | 274 | | .SumAsync(i => i.Monto ?? 0m); |
| | | 275 | | |
| | | 276 | | public async Task<int> GetItemCountAsync(long loteId) |
| | 2 | 277 | | => await _context.ItemsLoteCompensacion |
| | 2 | 278 | | .CountAsync(i => i.LoteCompensacionId == loteId); |
| | | 279 | | |
| | | 280 | | public async Task<int> GetItemsEnErrorCountAsync(long loteId) |
| | 2 | 281 | | => await _context.ItemsLoteCompensacion |
| | 2 | 282 | | .CountAsync(i => i.LoteCompensacionId == loteId && i.Estado == "error"); |
| | | 283 | | |
| | | 284 | | public async Task<TarifaPorCategoria?> GetTarifaCategoriaAsync(long catalogoItemId, string categoria, DateTime perio |
| | 0 | 285 | | => await _context.TarifasPorCategoria |
| | 0 | 286 | | .Where(t => t.ItemId == catalogoItemId |
| | 0 | 287 | | && t.Categoria == categoria |
| | 0 | 288 | | && t.VigenteDesde <= periodo |
| | 0 | 289 | | && (t.VigenteHasta == null || t.VigenteHasta >= periodo)) |
| | 0 | 290 | | .OrderByDescending(t => t.VigenteDesde) |
| | 0 | 291 | | .FirstOrDefaultAsync(); |
| | | 292 | | |
| | | 293 | | public async Task<RemuneracionGrado?> GetTarifaGradoAsync(long catalogoItemId, long gradoId, DateTime periodo) |
| | 0 | 294 | | => await _context.RemuneracionesGrado |
| | 0 | 295 | | .Where(r => r.ItemId == catalogoItemId |
| | 0 | 296 | | && r.GradoId == gradoId |
| | 0 | 297 | | && r.VigenteDesde <= periodo |
| | 0 | 298 | | && (r.VigenteHasta == null || r.VigenteHasta >= periodo)) |
| | 0 | 299 | | .OrderByDescending(r => r.VigenteDesde) |
| | 0 | 300 | | .FirstOrDefaultAsync(); |
| | | 301 | | |
| | | 302 | | public async Task<ItemLoteCompensacion> CreateItemAsync(ItemLoteCompensacion item) |
| | | 303 | | { |
| | 1 | 304 | | _context.ItemsLoteCompensacion.Add(item); |
| | 1 | 305 | | await _context.SaveChangesAsync(); |
| | 1 | 306 | | return item; |
| | 1 | 307 | | } |
| | | 308 | | |
| | | 309 | | public async Task<ItemLoteCompensacion> UpdateItemAsync(ItemLoteCompensacion item) |
| | | 310 | | { |
| | 0 | 311 | | _context.ItemsLoteCompensacion.Update(item); |
| | 0 | 312 | | await _context.SaveChangesAsync(); |
| | 0 | 313 | | return item; |
| | 0 | 314 | | } |
| | | 315 | | |
| | | 316 | | public async Task DeleteItemAsync(ItemLoteCompensacion item) |
| | | 317 | | { |
| | 0 | 318 | | _context.ItemsLoteCompensacion.Remove(item); |
| | 0 | 319 | | await _context.SaveChangesAsync(); |
| | 0 | 320 | | } |
| | | 321 | | } |