| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace FAU.Logica.Services; |
| | | 7 | | |
| | | 8 | | public class TipoBeneficioService : ITipoBeneficioService |
| | | 9 | | { |
| | | 10 | | private readonly ITipoBeneficioRepository _tipoBeneficioRepository; |
| | | 11 | | private readonly IAuditoriaService _auditoriaService; |
| | | 12 | | |
| | 30 | 13 | | public TipoBeneficioService( |
| | 30 | 14 | | ITipoBeneficioRepository tipoBeneficioRepository, |
| | 30 | 15 | | IAuditoriaService auditoriaService) |
| | | 16 | | { |
| | 30 | 17 | | _tipoBeneficioRepository = tipoBeneficioRepository; |
| | 30 | 18 | | _auditoriaService = auditoriaService; |
| | 30 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<TipoBeneficio?> ObtenerPorIdAsync(long id) |
| | | 22 | | { |
| | 1 | 23 | | return await _tipoBeneficioRepository.ObtenerPorIdAsync(id); |
| | 1 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<PagedResult<TipoBeneficioDto>> ObtenerPaginadoAsync( |
| | | 27 | | int pagina, |
| | | 28 | | int tamano, |
| | | 29 | | string? busqueda = null, |
| | | 30 | | bool? activo = null) |
| | | 31 | | { |
| | 3 | 32 | | if (pagina < 1) pagina = 1; |
| | 2 | 33 | | if (tamano < 1) tamano = 10; |
| | 3 | 34 | | if (tamano > 100) tamano = 100; |
| | | 35 | | |
| | 2 | 36 | | var (items, totalCount) = await _tipoBeneficioRepository.ObtenerPaginadoAsync(pagina, tamano, busqueda, activo); |
| | | 37 | | |
| | 2 | 38 | | var itemsDto = items.Select(MapearDto).ToList(); |
| | | 39 | | |
| | 2 | 40 | | return new PagedResult<TipoBeneficioDto> |
| | 2 | 41 | | { |
| | 2 | 42 | | Items = itemsDto, |
| | 2 | 43 | | Page = pagina, |
| | 2 | 44 | | PageSize = tamano, |
| | 2 | 45 | | TotalCount = totalCount |
| | 2 | 46 | | }; |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task<(bool Exito, TipoBeneficio? Resultado, string? Error)> CrearAsync( |
| | | 50 | | string codigo, |
| | | 51 | | string nombre, |
| | | 52 | | bool esMensual, |
| | | 53 | | bool permiteRetroactividad, |
| | | 54 | | short? plazoPresentacionDias, |
| | | 55 | | decimal? montoReferencia, |
| | | 56 | | long? catalogoItemId, |
| | | 57 | | bool activo, |
| | | 58 | | long usuarioId, |
| | | 59 | | string host) |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 8 | 63 | | var validacion = ValidarDatos(codigo, nombre, plazoPresentacionDias, montoReferencia); |
| | 8 | 64 | | if (!string.IsNullOrEmpty(validacion)) |
| | | 65 | | { |
| | 4 | 66 | | return (false, null, validacion); |
| | | 67 | | } |
| | | 68 | | |
| | 4 | 69 | | var codigoNormalizado = codigo.Trim().ToUpperInvariant(); |
| | 4 | 70 | | if (await _tipoBeneficioRepository.ExisteCodigoAsync(codigoNormalizado)) |
| | | 71 | | { |
| | 1 | 72 | | return (false, null, "Ya existe un tipo de beneficio con ese codigo"); |
| | | 73 | | } |
| | | 74 | | |
| | 3 | 75 | | var nuevoTipo = new TipoBeneficio |
| | 3 | 76 | | { |
| | 3 | 77 | | Codigo = codigoNormalizado, |
| | 3 | 78 | | Nombre = nombre.Trim(), |
| | 3 | 79 | | EsMensual = esMensual, |
| | 3 | 80 | | PermiteRetroactividad = permiteRetroactividad, |
| | 3 | 81 | | PlazoPresentacionDias = plazoPresentacionDias, |
| | 3 | 82 | | MontoReferencia = montoReferencia, |
| | 3 | 83 | | CatalogoItemId = catalogoItemId, |
| | 3 | 84 | | Activo = activo |
| | 3 | 85 | | }; |
| | | 86 | | |
| | 3 | 87 | | var creado = await _tipoBeneficioRepository.CrearAsync(nuevoTipo); |
| | 1 | 88 | | RegistrarAuditoria(usuarioId, (int)AccionEnum.CrearUsuario, host); |
| | | 89 | | |
| | 1 | 90 | | return (true, creado, null); |
| | | 91 | | } |
| | 1 | 92 | | catch (DbUpdateException ex) when (EsViolacionCodigoDuplicado(ex)) |
| | | 93 | | { |
| | 1 | 94 | | return (false, null, "Ya existe un tipo de beneficio con ese codigo"); |
| | | 95 | | } |
| | 1 | 96 | | catch (Exception ex) |
| | | 97 | | { |
| | 1 | 98 | | return (false, null, $"Error al crear tipo de beneficio: {ex.Message}"); |
| | | 99 | | } |
| | 8 | 100 | | } |
| | | 101 | | |
| | | 102 | | public async Task<(bool Exito, TipoBeneficio? Resultado, string? Error)> ActualizarAsync( |
| | | 103 | | long id, |
| | | 104 | | string? codigo, |
| | | 105 | | string? nombre, |
| | | 106 | | bool? esMensual, |
| | | 107 | | bool? permiteRetroactividad, |
| | | 108 | | short? plazoPresentacionDias, |
| | | 109 | | decimal? montoReferencia, |
| | | 110 | | long? catalogoItemId, |
| | | 111 | | long usuarioId, |
| | | 112 | | string host) |
| | | 113 | | { |
| | | 114 | | try |
| | | 115 | | { |
| | 9 | 116 | | var existente = await _tipoBeneficioRepository.ObtenerPorIdAsync(id); |
| | 8 | 117 | | if (existente == null) |
| | | 118 | | { |
| | 1 | 119 | | return (false, null, "Tipo de beneficio no encontrado"); |
| | | 120 | | } |
| | | 121 | | |
| | 7 | 122 | | if (codigo != null) |
| | | 123 | | { |
| | 4 | 124 | | if (string.IsNullOrWhiteSpace(codigo)) |
| | | 125 | | { |
| | 1 | 126 | | return (false, null, "El codigo es requerido"); |
| | | 127 | | } |
| | | 128 | | |
| | 3 | 129 | | var codigoNormalizado = codigo.Trim().ToUpperInvariant(); |
| | 3 | 130 | | if (await _tipoBeneficioRepository.ExisteCodigoAsync(codigoNormalizado, id)) |
| | | 131 | | { |
| | 1 | 132 | | return (false, null, "Ya existe un tipo de beneficio con ese codigo"); |
| | | 133 | | } |
| | | 134 | | |
| | 2 | 135 | | existente.Codigo = codigoNormalizado; |
| | 2 | 136 | | } |
| | | 137 | | |
| | 5 | 138 | | if (nombre != null) |
| | | 139 | | { |
| | 2 | 140 | | if (string.IsNullOrWhiteSpace(nombre)) |
| | | 141 | | { |
| | 1 | 142 | | return (false, null, "El nombre es requerido"); |
| | | 143 | | } |
| | | 144 | | |
| | 1 | 145 | | existente.Nombre = nombre.Trim(); |
| | | 146 | | } |
| | | 147 | | |
| | 4 | 148 | | if (plazoPresentacionDias.HasValue && plazoPresentacionDias.Value < 0) |
| | | 149 | | { |
| | 1 | 150 | | return (false, null, "El plazo de presentacion no puede ser negativo"); |
| | | 151 | | } |
| | | 152 | | |
| | 3 | 153 | | if (montoReferencia.HasValue && montoReferencia.Value < 0) |
| | | 154 | | { |
| | 1 | 155 | | return (false, null, "El monto de referencia no puede ser negativo"); |
| | | 156 | | } |
| | | 157 | | |
| | 2 | 158 | | if (esMensual.HasValue) |
| | | 159 | | { |
| | 1 | 160 | | existente.EsMensual = esMensual.Value; |
| | | 161 | | } |
| | | 162 | | |
| | 2 | 163 | | if (permiteRetroactividad.HasValue) |
| | | 164 | | { |
| | 1 | 165 | | existente.PermiteRetroactividad = permiteRetroactividad.Value; |
| | | 166 | | } |
| | | 167 | | |
| | 2 | 168 | | if (plazoPresentacionDias.HasValue) |
| | | 169 | | { |
| | 1 | 170 | | existente.PlazoPresentacionDias = plazoPresentacionDias; |
| | | 171 | | } |
| | | 172 | | |
| | 2 | 173 | | if (montoReferencia.HasValue) |
| | | 174 | | { |
| | 1 | 175 | | existente.MontoReferencia = montoReferencia; |
| | | 176 | | } |
| | | 177 | | |
| | 2 | 178 | | if (catalogoItemId.HasValue) |
| | | 179 | | { |
| | 1 | 180 | | existente.CatalogoItemId = catalogoItemId; |
| | | 181 | | } |
| | | 182 | | |
| | 2 | 183 | | var actualizado = await _tipoBeneficioRepository.ActualizarAsync(existente); |
| | 1 | 184 | | RegistrarAuditoria(usuarioId, (int)AccionEnum.ActualizarUsuario, host); |
| | | 185 | | |
| | 1 | 186 | | return (true, actualizado, null); |
| | | 187 | | } |
| | 1 | 188 | | catch (DbUpdateException ex) when (EsViolacionCodigoDuplicado(ex)) |
| | | 189 | | { |
| | 1 | 190 | | return (false, null, "Ya existe un tipo de beneficio con ese codigo"); |
| | | 191 | | } |
| | 1 | 192 | | catch (Exception ex) |
| | | 193 | | { |
| | 1 | 194 | | return (false, null, $"Error al actualizar tipo de beneficio: {ex.Message}"); |
| | | 195 | | } |
| | 9 | 196 | | } |
| | | 197 | | |
| | | 198 | | public async Task<(bool Exito, TipoBeneficio? Resultado, string? Error)> ActivarAsync(long id, long usuarioId, strin |
| | | 199 | | { |
| | 3 | 200 | | return await CambiarEstadoAsync(id, true, usuarioId, host); |
| | 3 | 201 | | } |
| | | 202 | | |
| | | 203 | | public async Task<(bool Exito, TipoBeneficio? Resultado, string? Error)> DesactivarAsync(long id, long usuarioId, st |
| | | 204 | | { |
| | 2 | 205 | | return await CambiarEstadoAsync(id, false, usuarioId, host); |
| | 2 | 206 | | } |
| | | 207 | | |
| | | 208 | | public async Task<(bool Exito, bool BajaLogica, string? Error)> EliminarAsync(long id, long usuarioId, string host) |
| | | 209 | | { |
| | | 210 | | try |
| | | 211 | | { |
| | 5 | 212 | | var existente = await _tipoBeneficioRepository.ObtenerPorIdAsync(id); |
| | 4 | 213 | | if (existente == null) |
| | | 214 | | { |
| | 1 | 215 | | return (false, false, "Tipo de beneficio no encontrado"); |
| | | 216 | | } |
| | | 217 | | |
| | 3 | 218 | | if (await _tipoBeneficioRepository.TieneLiquidacionesAbiertasAsync(id)) |
| | | 219 | | { |
| | 1 | 220 | | return (false, false, "No se puede dar de baja porque existen liquidaciones abiertas relacionadas"); |
| | | 221 | | } |
| | | 222 | | |
| | 2 | 223 | | var tieneHistorico = await _tipoBeneficioRepository.TieneHistoricoBeneficiosAsync(id); |
| | 2 | 224 | | if (tieneHistorico) |
| | | 225 | | { |
| | 1 | 226 | | existente.Activo = false; |
| | 1 | 227 | | await _tipoBeneficioRepository.ActualizarAsync(existente); |
| | 1 | 228 | | RegistrarAuditoria(usuarioId, (int)AccionEnum.EliminarUsuario, host); |
| | 1 | 229 | | return (true, true, null); |
| | | 230 | | } |
| | | 231 | | |
| | 1 | 232 | | await _tipoBeneficioRepository.EliminarAsync(id); |
| | 1 | 233 | | RegistrarAuditoria(usuarioId, (int)AccionEnum.EliminarUsuario, host); |
| | 1 | 234 | | return (true, false, null); |
| | | 235 | | } |
| | 1 | 236 | | catch (Exception ex) |
| | | 237 | | { |
| | 1 | 238 | | return (false, false, $"Error al eliminar tipo de beneficio: {ex.Message}"); |
| | | 239 | | } |
| | 5 | 240 | | } |
| | | 241 | | |
| | | 242 | | private async Task<(bool Exito, TipoBeneficio? Resultado, string? Error)> CambiarEstadoAsync( |
| | | 243 | | long id, |
| | | 244 | | bool nuevoEstado, |
| | | 245 | | long usuarioId, |
| | | 246 | | string host) |
| | | 247 | | { |
| | | 248 | | try |
| | | 249 | | { |
| | 5 | 250 | | var existente = await _tipoBeneficioRepository.ObtenerPorIdAsync(id); |
| | 4 | 251 | | if (existente == null) |
| | | 252 | | { |
| | 1 | 253 | | return (false, null, "Tipo de beneficio no encontrado"); |
| | | 254 | | } |
| | | 255 | | |
| | 3 | 256 | | if (await _tipoBeneficioRepository.TieneLiquidacionesAbiertasAsync(id)) |
| | | 257 | | { |
| | 1 | 258 | | return (false, null, "No se puede cambiar el estado porque existen liquidaciones abiertas relacionadas") |
| | | 259 | | } |
| | | 260 | | |
| | 2 | 261 | | existente.Activo = nuevoEstado; |
| | 2 | 262 | | var actualizado = await _tipoBeneficioRepository.ActualizarAsync(existente); |
| | 2 | 263 | | RegistrarAuditoria(usuarioId, (int)AccionEnum.ActualizarUsuario, host); |
| | | 264 | | |
| | 2 | 265 | | return (true, actualizado, null); |
| | | 266 | | } |
| | 1 | 267 | | catch (Exception ex) |
| | | 268 | | { |
| | 1 | 269 | | return (false, null, $"Error al cambiar estado del tipo de beneficio: {ex.Message}"); |
| | | 270 | | } |
| | 5 | 271 | | } |
| | | 272 | | |
| | | 273 | | private void RegistrarAuditoria(long usuarioId, int accionId, string host) |
| | | 274 | | { |
| | 6 | 275 | | _auditoriaService.LogAuditoria(usuarioId, accionId, (int)ContextoEnum.Sistema, host); |
| | 6 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static string? ValidarDatos(string codigo, string nombre, short? plazoPresentacionDias, decimal? montoRefere |
| | | 279 | | { |
| | 8 | 280 | | if (string.IsNullOrWhiteSpace(codigo)) |
| | | 281 | | { |
| | 1 | 282 | | return "El codigo es requerido"; |
| | | 283 | | } |
| | | 284 | | |
| | 7 | 285 | | if (string.IsNullOrWhiteSpace(nombre)) |
| | | 286 | | { |
| | 1 | 287 | | return "El nombre es requerido"; |
| | | 288 | | } |
| | | 289 | | |
| | 6 | 290 | | if (plazoPresentacionDias.HasValue && plazoPresentacionDias.Value < 0) |
| | | 291 | | { |
| | 1 | 292 | | return "El plazo de presentacion no puede ser negativo"; |
| | | 293 | | } |
| | | 294 | | |
| | 5 | 295 | | if (montoReferencia.HasValue && montoReferencia.Value < 0) |
| | | 296 | | { |
| | 1 | 297 | | return "El monto de referencia no puede ser negativo"; |
| | | 298 | | } |
| | | 299 | | |
| | 4 | 300 | | return null; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private static bool EsViolacionCodigoDuplicado(DbUpdateException ex) |
| | | 304 | | { |
| | 2 | 305 | | var mensaje = ex.InnerException?.Message ?? ex.Message; |
| | 2 | 306 | | return mensaje.Contains("ux_tipos_beneficio_codigo_upper", StringComparison.OrdinalIgnoreCase) |
| | 2 | 307 | | || mensaje.Contains("tipos_beneficio_codigo_key", StringComparison.OrdinalIgnoreCase) |
| | 2 | 308 | | || mensaje.Contains("duplicate key", StringComparison.OrdinalIgnoreCase); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | private static TipoBeneficioDto MapearDto(TipoBeneficio item) |
| | | 312 | | { |
| | 1 | 313 | | return new TipoBeneficioDto |
| | 1 | 314 | | { |
| | 1 | 315 | | Id = item.Id, |
| | 1 | 316 | | Codigo = item.Codigo, |
| | 1 | 317 | | Nombre = item.Nombre, |
| | 1 | 318 | | EsMensual = item.EsMensual, |
| | 1 | 319 | | PermiteRetroactividad = item.PermiteRetroactividad, |
| | 1 | 320 | | PlazoPresentacionDias = item.PlazoPresentacionDias, |
| | 1 | 321 | | MontoReferencia = item.MontoReferencia, |
| | 1 | 322 | | CatalogoItemId = item.CatalogoItemId, |
| | 1 | 323 | | Activo = item.Activo |
| | 1 | 324 | | }; |
| | | 325 | | } |
| | | 326 | | } |