| | | 1 | | using System.Data; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace FAU.DataAccess.Repositories; |
| | | 6 | | |
| | | 7 | | public class TipoBeneficioRepository : ITipoBeneficioRepository |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 15 | 11 | | public TipoBeneficioRepository(ApplicationDbContext context) |
| | | 12 | | { |
| | 15 | 13 | | _context = context; |
| | 15 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task<TipoBeneficio?> ObtenerPorIdAsync(long id) |
| | | 17 | | { |
| | 2 | 18 | | return await _context.TiposBeneficio |
| | 2 | 19 | | .FirstOrDefaultAsync(tb => tb.Id == id); |
| | 2 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<TipoBeneficio?> ObtenerPorCodigoAsync(string codigo) |
| | | 23 | | { |
| | 1 | 24 | | var codigoNormalizado = codigo.Trim().ToUpperInvariant(); |
| | | 25 | | |
| | 1 | 26 | | return await _context.TiposBeneficio |
| | 1 | 27 | | .FirstOrDefaultAsync(tb => tb.Codigo.ToUpper() == codigoNormalizado); |
| | 1 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<(IEnumerable<TipoBeneficio> Items, int TotalCount)> ObtenerPaginadoAsync( |
| | | 31 | | int pagina, |
| | | 32 | | int tamano, |
| | | 33 | | string? busqueda = null, |
| | | 34 | | bool? activo = null) |
| | | 35 | | { |
| | 3 | 36 | | var query = _context.TiposBeneficio.AsQueryable(); |
| | | 37 | | |
| | 3 | 38 | | if (!string.IsNullOrWhiteSpace(busqueda)) |
| | | 39 | | { |
| | 1 | 40 | | var termino = busqueda.Trim().ToLower(); |
| | 1 | 41 | | query = query.Where(tb => |
| | 1 | 42 | | tb.Codigo.ToLower().Contains(termino) || |
| | 1 | 43 | | tb.Nombre.ToLower().Contains(termino)); |
| | | 44 | | } |
| | | 45 | | |
| | 3 | 46 | | if (activo.HasValue) |
| | | 47 | | { |
| | 1 | 48 | | query = query.Where(tb => tb.Activo == activo.Value); |
| | | 49 | | } |
| | | 50 | | |
| | 3 | 51 | | var totalCount = await query.CountAsync(); |
| | | 52 | | |
| | 3 | 53 | | var items = await query |
| | 3 | 54 | | .OrderBy(tb => tb.Nombre) |
| | 3 | 55 | | .Skip((pagina - 1) * tamano) |
| | 3 | 56 | | .Take(tamano) |
| | 3 | 57 | | .ToListAsync(); |
| | | 58 | | |
| | 3 | 59 | | return (items, totalCount); |
| | 3 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task<TipoBeneficio> CrearAsync(TipoBeneficio tipoBeneficio) |
| | | 63 | | { |
| | 17 | 64 | | _context.TiposBeneficio.Add(tipoBeneficio); |
| | 17 | 65 | | await _context.SaveChangesAsync(); |
| | 17 | 66 | | return tipoBeneficio; |
| | 17 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<TipoBeneficio> ActualizarAsync(TipoBeneficio tipoBeneficio) |
| | | 70 | | { |
| | 1 | 71 | | _context.TiposBeneficio.Update(tipoBeneficio); |
| | 1 | 72 | | await _context.SaveChangesAsync(); |
| | 1 | 73 | | return tipoBeneficio; |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task EliminarAsync(long id) |
| | | 77 | | { |
| | 2 | 78 | | var entidad = await _context.TiposBeneficio.FindAsync(id); |
| | 2 | 79 | | if (entidad == null) |
| | | 80 | | { |
| | 1 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | _context.TiposBeneficio.Remove(entidad); |
| | 1 | 85 | | await _context.SaveChangesAsync(); |
| | 2 | 86 | | } |
| | | 87 | | |
| | | 88 | | public async Task<bool> ExisteCodigoAsync(string codigo, long? excluirId = null) |
| | | 89 | | { |
| | 3 | 90 | | var codigoNormalizado = codigo.Trim().ToUpperInvariant(); |
| | | 91 | | |
| | 3 | 92 | | var query = _context.TiposBeneficio |
| | 3 | 93 | | .Where(tb => tb.Codigo.ToUpper() == codigoNormalizado); |
| | | 94 | | |
| | 3 | 95 | | if (excluirId.HasValue) |
| | | 96 | | { |
| | 1 | 97 | | query = query.Where(tb => tb.Id != excluirId.Value); |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | return await query.AnyAsync(); |
| | 3 | 101 | | } |
| | | 102 | | |
| | | 103 | | public async Task<bool> ExistePorIdAsync(long id) |
| | | 104 | | { |
| | 3 | 105 | | return await _context.TiposBeneficio.AnyAsync(tb => tb.Id == id); |
| | 3 | 106 | | } |
| | | 107 | | |
| | | 108 | | public async Task<bool> TieneHistoricoBeneficiosAsync(long tipoBeneficioId) |
| | | 109 | | { |
| | 2 | 110 | | return await _context.BeneficiosSociales |
| | 2 | 111 | | .AnyAsync(bs => bs.TipoBeneficioId == tipoBeneficioId); |
| | 2 | 112 | | } |
| | | 113 | | |
| | | 114 | | public async Task<bool> TieneLiquidacionesAbiertasAsync(long tipoBeneficioId) |
| | | 115 | | { |
| | 1 | 116 | | if (!_context.Database.IsRelational()) |
| | | 117 | | { |
| | 1 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | var conexion = _context.Database.GetDbConnection(); |
| | 0 | 122 | | var abrirConexion = conexion.State != ConnectionState.Open; |
| | | 123 | | |
| | 0 | 124 | | if (abrirConexion) |
| | | 125 | | { |
| | 0 | 126 | | await conexion.OpenAsync(); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | try |
| | | 130 | | { |
| | 0 | 131 | | var existeLiquidaciones = await ExisteTablaAsync(conexion, "liquidaciones"); |
| | 0 | 132 | | if (!existeLiquidaciones) |
| | | 133 | | { |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | var tieneEstado = await ExisteColumnaAsync(conexion, "liquidaciones", "estado"); |
| | 0 | 138 | | if (!tieneEstado) |
| | | 139 | | { |
| | 0 | 140 | | return false; |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | var tieneTipoBeneficioDirecto = await ExisteColumnaAsync(conexion, "liquidaciones", "tipo_beneficio_id"); |
| | 0 | 144 | | if (tieneTipoBeneficioDirecto) |
| | | 145 | | { |
| | 0 | 146 | | return await ExisteLiquidacionDirectaAbiertaAsync(conexion, tipoBeneficioId); |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | var existeLiquidacionItems = await ExisteTablaAsync(conexion, "liquidacion_items"); |
| | 0 | 150 | | if (!existeLiquidacionItems) |
| | | 151 | | { |
| | 0 | 152 | | return false; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | var liquidacionItemsTieneTipo = await ExisteColumnaAsync(conexion, "liquidacion_items", "tipo_beneficio_id") |
| | 0 | 156 | | var liquidacionItemsTieneLiquidacionId = await ExisteColumnaAsync(conexion, "liquidacion_items", "liquidacio |
| | 0 | 157 | | var liquidacionesTieneId = await ExisteColumnaAsync(conexion, "liquidaciones", "id"); |
| | | 158 | | |
| | 0 | 159 | | if (liquidacionItemsTieneTipo && liquidacionItemsTieneLiquidacionId && liquidacionesTieneId) |
| | | 160 | | { |
| | 0 | 161 | | return await ExisteLiquidacionPorItemsAbiertaAsync(conexion, tipoBeneficioId); |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | return false; |
| | | 165 | | } |
| | | 166 | | finally |
| | | 167 | | { |
| | 0 | 168 | | if (abrirConexion) |
| | | 169 | | { |
| | 0 | 170 | | await conexion.CloseAsync(); |
| | | 171 | | } |
| | | 172 | | } |
| | 1 | 173 | | } |
| | | 174 | | |
| | | 175 | | private static async Task<bool> ExisteTablaAsync(System.Data.Common.DbConnection conexion, string nombreTabla) |
| | | 176 | | { |
| | 0 | 177 | | using var comando = conexion.CreateCommand(); |
| | 0 | 178 | | comando.CommandText = @" |
| | 0 | 179 | | SELECT EXISTS ( |
| | 0 | 180 | | SELECT 1 |
| | 0 | 181 | | FROM information_schema.tables |
| | 0 | 182 | | WHERE table_schema = 'public' |
| | 0 | 183 | | AND table_name = @tabla |
| | 0 | 184 | | );"; |
| | | 185 | | |
| | 0 | 186 | | var parametro = comando.CreateParameter(); |
| | 0 | 187 | | parametro.ParameterName = "@tabla"; |
| | 0 | 188 | | parametro.Value = nombreTabla; |
| | 0 | 189 | | comando.Parameters.Add(parametro); |
| | | 190 | | |
| | 0 | 191 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 192 | | return Convert.ToBoolean(resultado); |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | private static async Task<bool> ExisteColumnaAsync(System.Data.Common.DbConnection conexion, string nombreTabla, str |
| | | 196 | | { |
| | 0 | 197 | | using var comando = conexion.CreateCommand(); |
| | 0 | 198 | | comando.CommandText = @" |
| | 0 | 199 | | SELECT EXISTS ( |
| | 0 | 200 | | SELECT 1 |
| | 0 | 201 | | FROM information_schema.columns |
| | 0 | 202 | | WHERE table_schema = 'public' |
| | 0 | 203 | | AND table_name = @tabla |
| | 0 | 204 | | AND column_name = @columna |
| | 0 | 205 | | );"; |
| | | 206 | | |
| | 0 | 207 | | var parametroTabla = comando.CreateParameter(); |
| | 0 | 208 | | parametroTabla.ParameterName = "@tabla"; |
| | 0 | 209 | | parametroTabla.Value = nombreTabla; |
| | 0 | 210 | | comando.Parameters.Add(parametroTabla); |
| | | 211 | | |
| | 0 | 212 | | var parametroColumna = comando.CreateParameter(); |
| | 0 | 213 | | parametroColumna.ParameterName = "@columna"; |
| | 0 | 214 | | parametroColumna.Value = nombreColumna; |
| | 0 | 215 | | comando.Parameters.Add(parametroColumna); |
| | | 216 | | |
| | 0 | 217 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 218 | | return Convert.ToBoolean(resultado); |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | private static async Task<bool> ExisteLiquidacionDirectaAbiertaAsync(System.Data.Common.DbConnection conexion, long |
| | | 222 | | { |
| | 0 | 223 | | using var comando = conexion.CreateCommand(); |
| | 0 | 224 | | comando.CommandText = @" |
| | 0 | 225 | | SELECT EXISTS ( |
| | 0 | 226 | | SELECT 1 |
| | 0 | 227 | | FROM liquidaciones l |
| | 0 | 228 | | WHERE l.tipo_beneficio_id = @tipoBeneficioId |
| | 0 | 229 | | AND LOWER(COALESCE(l.estado, '')) IN ('abierta', 'abierto', 'pendiente', 'en_proceso') |
| | 0 | 230 | | );"; |
| | | 231 | | |
| | 0 | 232 | | var parametro = comando.CreateParameter(); |
| | 0 | 233 | | parametro.ParameterName = "@tipoBeneficioId"; |
| | 0 | 234 | | parametro.Value = tipoBeneficioId; |
| | 0 | 235 | | comando.Parameters.Add(parametro); |
| | | 236 | | |
| | 0 | 237 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 238 | | return Convert.ToBoolean(resultado); |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | private static async Task<bool> ExisteLiquidacionPorItemsAbiertaAsync(System.Data.Common.DbConnection conexion, long |
| | | 242 | | { |
| | 0 | 243 | | using var comando = conexion.CreateCommand(); |
| | 0 | 244 | | comando.CommandText = @" |
| | 0 | 245 | | SELECT EXISTS ( |
| | 0 | 246 | | SELECT 1 |
| | 0 | 247 | | FROM liquidacion_items li |
| | 0 | 248 | | INNER JOIN liquidaciones l ON l.id = li.liquidacion_id |
| | 0 | 249 | | WHERE li.tipo_beneficio_id = @tipoBeneficioId |
| | 0 | 250 | | AND LOWER(COALESCE(l.estado, '')) IN ('abierta', 'abierto', 'pendiente', 'en_proceso') |
| | 0 | 251 | | );"; |
| | | 252 | | |
| | 0 | 253 | | var parametro = comando.CreateParameter(); |
| | 0 | 254 | | parametro.ParameterName = "@tipoBeneficioId"; |
| | 0 | 255 | | parametro.Value = tipoBeneficioId; |
| | 0 | 256 | | comando.Parameters.Add(parametro); |
| | | 257 | | |
| | 0 | 258 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 259 | | return Convert.ToBoolean(resultado); |
| | 0 | 260 | | } |
| | | 261 | | } |