| | | 1 | | using System.Data; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace FAU.DataAccess.Repositories; |
| | | 6 | | |
| | | 7 | | public class IrpfRepository : IIrpfRepository |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 12 | 11 | | public IrpfRepository(ApplicationDbContext context) |
| | | 12 | | { |
| | 12 | 13 | | _context = context; |
| | 12 | 14 | | } |
| | | 15 | | |
| | | 16 | | // ── Franjas ────────────────────────────────────────────────────────────── |
| | | 17 | | |
| | | 18 | | public async Task<IEnumerable<FranjaIrpf>> GetFranjasVigentesAsync(DateTime? fecha = null) |
| | | 19 | | { |
| | 2 | 20 | | var fechaRef = fecha?.Date ?? DateTime.UtcNow.Date; |
| | 2 | 21 | | return await _context.FranjasIrpf |
| | 2 | 22 | | .Where(f => f.VigenteDesde <= fechaRef && (f.VigenteHasta == null || f.VigenteHasta >= fechaRef)) |
| | 2 | 23 | | .OrderBy(f => f.NumeroFranja) |
| | 2 | 24 | | .ToListAsync(); |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<IEnumerable<FranjaIrpf>> GetFranjasPorFechaVigenciaAsync(DateTime vigenteDesde) |
| | | 28 | | { |
| | 0 | 29 | | return await _context.FranjasIrpf |
| | 0 | 30 | | .Where(f => f.VigenteDesde.Date == vigenteDesde.Date) |
| | 0 | 31 | | .OrderBy(f => f.NumeroFranja) |
| | 0 | 32 | | .ToListAsync(); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<IEnumerable<FranjaIrpf>> GetHistorialFranjasAsync() |
| | | 36 | | { |
| | 0 | 37 | | return await _context.FranjasIrpf |
| | 0 | 38 | | .OrderByDescending(f => f.VigenteDesde) |
| | 0 | 39 | | .ThenBy(f => f.NumeroFranja) |
| | 0 | 40 | | .ToListAsync(); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task<FranjaIrpf?> GetFranjaByIdAsync(long id) |
| | | 44 | | { |
| | 4 | 45 | | return await _context.FranjasIrpf.FindAsync(id); |
| | 4 | 46 | | } |
| | | 47 | | |
| | | 48 | | public async Task<FranjaIrpf?> GetFranjaActivaByNumeroAsync(short numeroFranja) |
| | | 49 | | { |
| | 2 | 50 | | return await _context.FranjasIrpf |
| | 2 | 51 | | .Where(f => f.NumeroFranja == numeroFranja && f.VigenteHasta == null) |
| | 2 | 52 | | .FirstOrDefaultAsync(); |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<DateTime?> ObtenerSiguienteFechaVigenciaAsync(DateTime vigenteDesde) |
| | | 56 | | { |
| | 0 | 57 | | return await _context.FranjasIrpf |
| | 0 | 58 | | .Where(f => f.VigenteDesde > vigenteDesde) |
| | 0 | 59 | | .OrderBy(f => f.VigenteDesde) |
| | 0 | 60 | | .Select(f => (DateTime?)f.VigenteDesde) |
| | 0 | 61 | | .FirstOrDefaultAsync(); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<bool> ExisteFranjaAsync(short numeroFranja, DateTime vigenteDesde) |
| | | 65 | | { |
| | 2 | 66 | | return await _context.FranjasIrpf |
| | 2 | 67 | | .AnyAsync(f => f.NumeroFranja == numeroFranja && f.VigenteDesde.Date == vigenteDesde.Date); |
| | 2 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task<FranjaIrpf> CreateFranjaAsync(FranjaIrpf franja) |
| | | 71 | | { |
| | 4 | 72 | | _context.FranjasIrpf.Add(franja); |
| | 4 | 73 | | await _context.SaveChangesAsync(); |
| | 4 | 74 | | return franja; |
| | 4 | 75 | | } |
| | | 76 | | |
| | | 77 | | public async Task CreateFranjasAsync(IEnumerable<FranjaIrpf> franjas) |
| | | 78 | | { |
| | 0 | 79 | | _context.FranjasIrpf.AddRange(franjas); |
| | 0 | 80 | | await _context.SaveChangesAsync(); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | public async Task<FranjaIrpf> UpdateFranjaAsync(FranjaIrpf franja) |
| | | 84 | | { |
| | 1 | 85 | | _context.FranjasIrpf.Update(franja); |
| | 1 | 86 | | await _context.SaveChangesAsync(); |
| | 1 | 87 | | return franja; |
| | 1 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task UpdateFranjasAsync(IEnumerable<FranjaIrpf> franjas) |
| | | 91 | | { |
| | 0 | 92 | | _context.FranjasIrpf.UpdateRange(franjas); |
| | 0 | 93 | | await _context.SaveChangesAsync(); |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | public async Task DeleteFranjaAsync(long id) |
| | | 97 | | { |
| | 2 | 98 | | var franja = await _context.FranjasIrpf.FindAsync(id); |
| | 2 | 99 | | if (franja != null) |
| | | 100 | | { |
| | 1 | 101 | | _context.FranjasIrpf.Remove(franja); |
| | 1 | 102 | | await _context.SaveChangesAsync(); |
| | | 103 | | } |
| | 2 | 104 | | } |
| | | 105 | | |
| | | 106 | | public async Task<bool> EstaFranjaUsadaEnLiquidacionesCerradasAsync(long franjaId) |
| | | 107 | | { |
| | 0 | 108 | | if (!_context.Database.IsRelational()) |
| | | 109 | | { |
| | 0 | 110 | | return false; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | var conexion = _context.Database.GetDbConnection(); |
| | 0 | 114 | | var abrirConexion = conexion.State != ConnectionState.Open; |
| | | 115 | | |
| | 0 | 116 | | if (abrirConexion) |
| | | 117 | | { |
| | 0 | 118 | | await conexion.OpenAsync(); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | try |
| | | 122 | | { |
| | 0 | 123 | | var existeDetalle = await ExisteTablaAsync(conexion, "irpf_detalle_franjas"); |
| | 0 | 124 | | var existeMensual = await ExisteTablaAsync(conexion, "irpf_mensual"); |
| | 0 | 125 | | var existeLiquidaciones = await ExisteTablaAsync(conexion, "liquidaciones"); |
| | | 126 | | |
| | 0 | 127 | | if (!existeDetalle || !existeMensual || !existeLiquidaciones) |
| | | 128 | | { |
| | 0 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | return await ExisteUsoEnLiquidacionesCerradasAsync(conexion, franjaId); |
| | | 133 | | } |
| | | 134 | | finally |
| | | 135 | | { |
| | 0 | 136 | | if (abrirConexion) |
| | | 137 | | { |
| | 0 | 138 | | await conexion.CloseAsync(); |
| | | 139 | | } |
| | | 140 | | } |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | public async Task<bool> EstaFranjaUsadaEnIrpfMensualAsync(long franjaId) |
| | | 144 | | { |
| | 0 | 145 | | if (!_context.Database.IsRelational()) |
| | | 146 | | { |
| | 0 | 147 | | return false; |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | var conexion = _context.Database.GetDbConnection(); |
| | 0 | 151 | | var abrirConexion = conexion.State != ConnectionState.Open; |
| | | 152 | | |
| | 0 | 153 | | if (abrirConexion) |
| | | 154 | | { |
| | 0 | 155 | | await conexion.OpenAsync(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | try |
| | | 159 | | { |
| | 0 | 160 | | var existeDetalle = await ExisteTablaAsync(conexion, "irpf_detalle_franjas"); |
| | 0 | 161 | | if (!existeDetalle) |
| | | 162 | | { |
| | 0 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | using var comando = conexion.CreateCommand(); |
| | 0 | 167 | | comando.CommandText = @" |
| | 0 | 168 | | SELECT EXISTS ( |
| | 0 | 169 | | SELECT 1 |
| | 0 | 170 | | FROM irpf_detalle_franjas d |
| | 0 | 171 | | WHERE d.franja_irpf_id = @franjaId |
| | 0 | 172 | | );"; |
| | | 173 | | |
| | 0 | 174 | | var parametro = comando.CreateParameter(); |
| | 0 | 175 | | parametro.ParameterName = "@franjaId"; |
| | 0 | 176 | | parametro.Value = franjaId; |
| | 0 | 177 | | comando.Parameters.Add(parametro); |
| | | 178 | | |
| | 0 | 179 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 180 | | return Convert.ToBoolean(resultado); |
| | | 181 | | } |
| | | 182 | | finally |
| | | 183 | | { |
| | 0 | 184 | | if (abrirConexion) |
| | | 185 | | { |
| | 0 | 186 | | await conexion.CloseAsync(); |
| | | 187 | | } |
| | | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | private static async Task<bool> ExisteTablaAsync(System.Data.Common.DbConnection conexion, string nombreTabla) |
| | | 192 | | { |
| | 0 | 193 | | using var comando = conexion.CreateCommand(); |
| | 0 | 194 | | comando.CommandText = @" |
| | 0 | 195 | | SELECT EXISTS ( |
| | 0 | 196 | | SELECT 1 |
| | 0 | 197 | | FROM information_schema.tables |
| | 0 | 198 | | WHERE table_schema = 'public' |
| | 0 | 199 | | AND table_name = @tabla |
| | 0 | 200 | | );"; |
| | | 201 | | |
| | 0 | 202 | | var parametro = comando.CreateParameter(); |
| | 0 | 203 | | parametro.ParameterName = "@tabla"; |
| | 0 | 204 | | parametro.Value = nombreTabla; |
| | 0 | 205 | | comando.Parameters.Add(parametro); |
| | | 206 | | |
| | 0 | 207 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 208 | | return Convert.ToBoolean(resultado); |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | private static async Task<bool> ExisteUsoEnLiquidacionesCerradasAsync(System.Data.Common.DbConnection conexion, long |
| | | 212 | | { |
| | 0 | 213 | | using var comando = conexion.CreateCommand(); |
| | 0 | 214 | | comando.CommandText = @" |
| | 0 | 215 | | SELECT EXISTS ( |
| | 0 | 216 | | SELECT 1 |
| | 0 | 217 | | FROM irpf_detalle_franjas d |
| | 0 | 218 | | INNER JOIN irpf_mensual m ON m.id = d.irpf_mensual_id |
| | 0 | 219 | | INNER JOIN liquidaciones l ON l.id = m.liquidacion_id |
| | 0 | 220 | | WHERE d.franja_irpf_id = @franjaId |
| | 0 | 221 | | AND LOWER(COALESCE(l.estado, '')) IN ('cerrada', 'cerrado') |
| | 0 | 222 | | );"; |
| | | 223 | | |
| | 0 | 224 | | var parametro = comando.CreateParameter(); |
| | 0 | 225 | | parametro.ParameterName = "@franjaId"; |
| | 0 | 226 | | parametro.Value = franjaId; |
| | 0 | 227 | | comando.Parameters.Add(parametro); |
| | | 228 | | |
| | 0 | 229 | | var resultado = await comando.ExecuteScalarAsync(); |
| | 0 | 230 | | return Convert.ToBoolean(resultado); |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | } |