| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class BeneficioSocialService : IBeneficioSocialService |
| | | 8 | | { |
| | | 9 | | private const string EstadoActivo = "activo"; |
| | | 10 | | private const string EstadoInactivo = "inactivo"; |
| | | 11 | | private const string EstadoPendienteDoc = "pendiente_doc"; |
| | | 12 | | |
| | | 13 | | private readonly IBeneficioSocialRepository _beneficioSocialRepository; |
| | | 14 | | private readonly IAuditoriaService _auditoriaService; |
| | | 15 | | private readonly IPeriodoGuard _periodoGuard; |
| | | 16 | | private readonly IStorageService _storage; |
| | | 17 | | |
| | 10 | 18 | | public BeneficioSocialService( |
| | 10 | 19 | | IBeneficioSocialRepository beneficioSocialRepository, |
| | 10 | 20 | | IAuditoriaService auditoriaService, |
| | 10 | 21 | | IPeriodoGuard periodoGuard, |
| | 10 | 22 | | IStorageService storage) |
| | | 23 | | { |
| | 10 | 24 | | _beneficioSocialRepository = beneficioSocialRepository; |
| | 10 | 25 | | _auditoriaService = auditoriaService; |
| | 10 | 26 | | _periodoGuard = periodoGuard; |
| | 10 | 27 | | _storage = storage; |
| | 10 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<BeneficioSocialDto?> ObtenerPorIdAsync(long id) |
| | | 31 | | { |
| | 2 | 32 | | var entidad = await _beneficioSocialRepository.ObtenerPorIdAsync(id); |
| | 2 | 33 | | return entidad == null ? null : MapearDto(entidad); |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task<PagedResult<BeneficioSocialDto>> ObtenerPaginadoAsync(int pagina, int tamano, long? personaId = nu |
| | | 37 | | { |
| | 2 | 38 | | if (pagina < 1) pagina = 1; |
| | 1 | 39 | | if (tamano < 1) tamano = 10; |
| | 2 | 40 | | if (tamano > 100) tamano = 100; |
| | | 41 | | |
| | 1 | 42 | | var (items, totalCount) = await _beneficioSocialRepository.ObtenerPaginadoAsync(pagina, tamano, personaId, estad |
| | | 43 | | |
| | 1 | 44 | | return new PagedResult<BeneficioSocialDto> |
| | 1 | 45 | | { |
| | 1 | 46 | | Items = items.Select(item => MapearDto(item)).ToList(), |
| | 1 | 47 | | Page = pagina, |
| | 1 | 48 | | PageSize = tamano, |
| | 1 | 49 | | TotalCount = totalCount |
| | 1 | 50 | | }; |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<(bool Exito, BeneficioSocialDto? Resultado, string? Error)> CrearAsync(CrearBeneficioSocialRequest |
| | | 54 | | { |
| | 3 | 55 | | await _periodoGuard.VerificarPeriodoAbiertoAsync(); |
| | | 56 | | try |
| | | 57 | | { |
| | 3 | 58 | | var tipo = await _beneficioSocialRepository.ObtenerTipoBeneficioAsync(request.TipoBeneficioId); |
| | 3 | 59 | | if (tipo == null || !tipo.Activo) |
| | | 60 | | { |
| | 0 | 61 | | return (false, null, "Tipo de beneficio no encontrado o inactivo"); |
| | | 62 | | } |
| | | 63 | | |
| | 3 | 64 | | var categoriaEsperada = ConvertirCategoriaRequest(DeterminarCategoria(tipo)); |
| | 3 | 65 | | if (request.CategoriaBeneficio != categoriaEsperada) |
| | | 66 | | { |
| | 0 | 67 | | return (false, null, "La categoria declarada no coincide con el tipo de beneficio seleccionado"); |
| | | 68 | | } |
| | | 69 | | |
| | 3 | 70 | | var personaActiva = await _beneficioSocialRepository.ExistePersonaActivaAsync(request.PersonaId, request.Vig |
| | 3 | 71 | | if (!personaActiva) |
| | | 72 | | { |
| | 1 | 73 | | return (false, null, "La persona no tiene relacion laboral activa para la vigencia indicada"); |
| | | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | var validacion = ValidarReglasPorTipo(tipo, request, esActualizacion: false); |
| | 2 | 77 | | if (!string.IsNullOrEmpty(validacion)) |
| | | 78 | | { |
| | 0 | 79 | | return (false, null, validacion); |
| | | 80 | | } |
| | | 81 | | |
| | 2 | 82 | | var claveEvento = ConstruirClaveEvento(tipo, request.DatosTipo.FechaEvento, request.VigenteDesde); |
| | 2 | 83 | | if (EsBeneficioUnicaVez(tipo) && await _beneficioSocialRepository.ExisteDuplicadoEventoAsync(request.Persona |
| | | 84 | | { |
| | 1 | 85 | | return (false, null, "Existe un beneficio duplicado para un evento de unica vez"); |
| | | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | var retroactividadPendiente = CalcularRetroactividadPendiente(request.VigenteDesde); |
| | 1 | 89 | | if (retroactividadPendiente && !tipo.PermiteRetroactividad) |
| | | 90 | | { |
| | 0 | 91 | | return (false, null, "El tipo de beneficio no permite retroactividad para la vigencia indicada"); |
| | | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | var estado = request.DatosTipo.PendienteDocumentacion ? EstadoPendienteDoc : EstadoActivo; |
| | 1 | 95 | | var entidad = new BeneficioSocial |
| | 1 | 96 | | { |
| | 1 | 97 | | PersonaId = request.PersonaId, |
| | 1 | 98 | | TipoBeneficioId = request.TipoBeneficioId, |
| | 1 | 99 | | Cantidad = (short)Math.Max(request.Cantidad ?? 1, (short)0), |
| | 1 | 100 | | VigenteDesde = request.VigenteDesde.Date, |
| | 1 | 101 | | VigenteHasta = request.VigenteHasta?.Date, |
| | 1 | 102 | | Estado = estado, |
| | 1 | 103 | | DocumentoRespaldo = request.DocumentoRespaldo?.Trim(), |
| | 1 | 104 | | Observaciones = ConstruirObservaciones(request.Observaciones, request.DatosTipo.JustificacionRetroactivo |
| | 1 | 105 | | CreadoPor = usuarioId, |
| | 1 | 106 | | FechaEvento = request.DatosTipo.FechaEvento?.Date, |
| | 1 | 107 | | ClaveEvento = claveEvento, |
| | 1 | 108 | | RetroactividadPendiente = retroactividadPendiente |
| | 1 | 109 | | }; |
| | | 110 | | |
| | 1 | 111 | | var creado = await _beneficioSocialRepository.CrearAsync(entidad); |
| | | 112 | | |
| | 1 | 113 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 114 | | usuarioId, |
| | 1 | 115 | | AccionEnum.CrearBeneficio, |
| | 1 | 116 | | ContextoEnum.Beneficios, |
| | 1 | 117 | | host: host, |
| | 1 | 118 | | entidad: nameof(BeneficioSocial), |
| | 1 | 119 | | entidadId: creado.Id, |
| | 1 | 120 | | detalle: new { operacion = "crear", id = creado.Id, personaId = creado.PersonaId, tipoBeneficioId = crea |
| | | 121 | | |
| | 1 | 122 | | var recargado = await _beneficioSocialRepository.ObtenerPorIdAsync(creado.Id); |
| | 1 | 123 | | return (true, recargado == null ? MapearDto(creado, tipo) : MapearDto(recargado), null); |
| | | 124 | | } |
| | 0 | 125 | | catch (Exception ex) |
| | | 126 | | { |
| | 0 | 127 | | return (false, null, $"Error al crear beneficio social: {ex.Message}"); |
| | | 128 | | } |
| | 3 | 129 | | } |
| | | 130 | | |
| | | 131 | | public async Task<(bool Exito, BeneficioSocialDto? Resultado, string? Error)> ActualizarAsync(long id, ActualizarBen |
| | | 132 | | { |
| | | 133 | | try |
| | | 134 | | { |
| | 2 | 135 | | var entidad = await _beneficioSocialRepository.ObtenerPorIdAsync(id); |
| | 2 | 136 | | if (entidad == null) |
| | | 137 | | { |
| | 1 | 138 | | return (false, null, "Beneficio social no encontrado"); |
| | | 139 | | } |
| | | 140 | | |
| | 1 | 141 | | var tipo = entidad.TipoBeneficio ?? await _beneficioSocialRepository.ObtenerTipoBeneficioAsync(entidad.TipoB |
| | 1 | 142 | | if (tipo == null || !tipo.Activo) |
| | | 143 | | { |
| | 0 | 144 | | return (false, null, "Tipo de beneficio no encontrado o inactivo"); |
| | | 145 | | } |
| | | 146 | | |
| | 1 | 147 | | var vigenteDesde = request.VigenteDesde?.Date ?? entidad.VigenteDesde.Date; |
| | 1 | 148 | | var fechaEvento = request.FechaEvento?.Date ?? entidad.FechaEvento; |
| | | 149 | | |
| | 1 | 150 | | if (!await _beneficioSocialRepository.ExistePersonaActivaAsync(entidad.PersonaId, vigenteDesde)) |
| | | 151 | | { |
| | 0 | 152 | | return (false, null, "La persona no tiene relacion laboral activa para la vigencia indicada"); |
| | | 153 | | } |
| | | 154 | | |
| | 1 | 155 | | var requestConsolidado = new CrearBeneficioSocialRequest |
| | 1 | 156 | | { |
| | 1 | 157 | | PersonaId = entidad.PersonaId, |
| | 1 | 158 | | TipoBeneficioId = entidad.TipoBeneficioId, |
| | 1 | 159 | | Cantidad = request.Cantidad ?? entidad.Cantidad, |
| | 1 | 160 | | VigenteDesde = vigenteDesde, |
| | 1 | 161 | | VigenteHasta = request.VigenteHasta ?? entidad.VigenteHasta, |
| | 1 | 162 | | DocumentoRespaldo = request.DocumentoRespaldo ?? entidad.DocumentoRespaldo, |
| | 1 | 163 | | Observaciones = request.Observaciones ?? entidad.Observaciones, |
| | 1 | 164 | | CategoriaBeneficio = ConvertirCategoriaRequest(DeterminarCategoria(tipo)), |
| | 1 | 165 | | DatosTipo = new DatosTipoBeneficioRequest |
| | 1 | 166 | | { |
| | 1 | 167 | | FechaEvento = fechaEvento, |
| | 1 | 168 | | EstadoCivilUnion = request.EstadoCivilUnion, |
| | 1 | 169 | | TieneHijosACargo = request.TieneHijosACargo, |
| | 1 | 170 | | CantidadHijosMenores = request.CantidadHijosMenores, |
| | 1 | 171 | | CantidadHijosDiscapacidad = request.CantidadHijosDiscapacidad, |
| | 1 | 172 | | PrimaSolidariaFamiliarTipo = request.PrimaSolidariaFamiliarTipo, |
| | 1 | 173 | | TieneJustificacionRetroactivo = request.TieneJustificacionRetroactivo ?? false, |
| | 1 | 174 | | JustificacionRetroactivo = request.JustificacionRetroactivo, |
| | 1 | 175 | | PendienteDocumentacion = request.PendienteDocumentacion ?? (entidad.Estado == EstadoPendienteDoc) |
| | 1 | 176 | | } |
| | 1 | 177 | | }; |
| | | 178 | | |
| | 1 | 179 | | var validacion = ValidarReglasPorTipo(tipo, requestConsolidado, esActualizacion: true); |
| | 1 | 180 | | if (!string.IsNullOrEmpty(validacion)) |
| | | 181 | | { |
| | 0 | 182 | | return (false, null, validacion); |
| | | 183 | | } |
| | | 184 | | |
| | 1 | 185 | | var claveEvento = ConstruirClaveEvento(tipo, fechaEvento, vigenteDesde); |
| | 1 | 186 | | if (EsBeneficioUnicaVez(tipo) |
| | 1 | 187 | | && await _beneficioSocialRepository.ExisteDuplicadoEventoAsync(entidad.PersonaId, entidad.TipoBeneficioI |
| | | 188 | | { |
| | 0 | 189 | | return (false, null, "Existe un beneficio duplicado para un evento de unica vez"); |
| | | 190 | | } |
| | | 191 | | |
| | 1 | 192 | | var retroactividadPendiente = CalcularRetroactividadPendiente(vigenteDesde); |
| | 1 | 193 | | if (retroactividadPendiente && !tipo.PermiteRetroactividad) |
| | | 194 | | { |
| | 0 | 195 | | return (false, null, "El tipo de beneficio no permite retroactividad para la vigencia indicada"); |
| | | 196 | | } |
| | | 197 | | |
| | 1 | 198 | | entidad.Cantidad = requestConsolidado.Cantidad ?? entidad.Cantidad; |
| | 1 | 199 | | entidad.VigenteDesde = vigenteDesde; |
| | 1 | 200 | | entidad.VigenteHasta = requestConsolidado.VigenteHasta?.Date; |
| | 1 | 201 | | entidad.DocumentoRespaldo = requestConsolidado.DocumentoRespaldo?.Trim(); |
| | 1 | 202 | | entidad.Observaciones = ConstruirObservaciones(requestConsolidado.Observaciones, requestConsolidado.DatosTip |
| | 1 | 203 | | entidad.Estado = requestConsolidado.DatosTipo.PendienteDocumentacion ? EstadoPendienteDoc : EstadoActivo; |
| | 1 | 204 | | entidad.FechaEvento = fechaEvento; |
| | 1 | 205 | | entidad.ClaveEvento = claveEvento; |
| | 1 | 206 | | entidad.RetroactividadPendiente = retroactividadPendiente; |
| | | 207 | | |
| | 1 | 208 | | var actualizado = await _beneficioSocialRepository.ActualizarAsync(entidad); |
| | | 209 | | |
| | 1 | 210 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 211 | | usuarioId, |
| | 1 | 212 | | AccionEnum.ActualizarBeneficio, |
| | 1 | 213 | | ContextoEnum.Beneficios, |
| | 1 | 214 | | host: host, |
| | 1 | 215 | | entidad: nameof(BeneficioSocial), |
| | 1 | 216 | | entidadId: actualizado.Id, |
| | 1 | 217 | | detalle: new { operacion = "actualizar", id = actualizado.Id, personaId = actualizado.PersonaId, tipoBen |
| | | 218 | | |
| | 1 | 219 | | return (true, MapearDto(actualizado, tipo), null); |
| | | 220 | | } |
| | 0 | 221 | | catch (Exception ex) |
| | | 222 | | { |
| | 0 | 223 | | return (false, null, $"Error al actualizar beneficio social: {ex.Message}"); |
| | | 224 | | } |
| | 2 | 225 | | } |
| | | 226 | | |
| | | 227 | | public async Task<(bool Exito, bool BajaLogica, string? Error)> EliminarAsync(long id, long usuarioId, string host) |
| | | 228 | | { |
| | | 229 | | try |
| | | 230 | | { |
| | 2 | 231 | | var entidad = await _beneficioSocialRepository.ObtenerPorIdAsync(id); |
| | 2 | 232 | | if (entidad == null) |
| | | 233 | | { |
| | 0 | 234 | | return (false, false, "Beneficio social no encontrado"); |
| | | 235 | | } |
| | | 236 | | |
| | 2 | 237 | | if (entidad.Estado == EstadoInactivo) |
| | | 238 | | { |
| | 1 | 239 | | return (true, true, null); |
| | | 240 | | } |
| | | 241 | | |
| | 1 | 242 | | entidad.Estado = EstadoInactivo; |
| | 1 | 243 | | entidad.VigenteHasta ??= DateTime.UtcNow.Date; |
| | 1 | 244 | | await _beneficioSocialRepository.ActualizarAsync(entidad); |
| | | 245 | | |
| | 1 | 246 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 247 | | usuarioId, |
| | 1 | 248 | | AccionEnum.EliminarBeneficio, |
| | 1 | 249 | | ContextoEnum.Beneficios, |
| | 1 | 250 | | host: host, |
| | 1 | 251 | | entidad: nameof(BeneficioSocial), |
| | 1 | 252 | | entidadId: entidad.Id, |
| | 1 | 253 | | detalle: new { operacion = "eliminar", id = entidad.Id, personaId = entidad.PersonaId, tipoBeneficioId = |
| | | 254 | | |
| | 1 | 255 | | return (true, true, null); |
| | | 256 | | } |
| | 0 | 257 | | catch (Exception ex) |
| | | 258 | | { |
| | 0 | 259 | | return (false, false, $"Error al eliminar beneficio social: {ex.Message}"); |
| | | 260 | | } |
| | 2 | 261 | | } |
| | | 262 | | |
| | | 263 | | private static string? ValidarReglasPorTipo(TipoBeneficio tipo, CrearBeneficioSocialRequest request, bool esActualiz |
| | | 264 | | { |
| | 3 | 265 | | var datosTipo = request.DatosTipo ?? new DatosTipoBeneficioRequest(); |
| | | 266 | | |
| | 3 | 267 | | if (request.Cantidad.HasValue && request.Cantidad.Value < 0) |
| | | 268 | | { |
| | 0 | 269 | | return "La cantidad no puede ser negativa"; |
| | | 270 | | } |
| | | 271 | | |
| | 3 | 272 | | if (request.VigenteHasta.HasValue && request.VigenteHasta.Value.Date < request.VigenteDesde.Date) |
| | | 273 | | { |
| | 0 | 274 | | return "La vigencia hasta no puede ser anterior a la vigencia desde"; |
| | | 275 | | } |
| | | 276 | | |
| | 3 | 277 | | var categoria = DeterminarCategoria(tipo); |
| | | 278 | | switch (categoria) |
| | | 279 | | { |
| | | 280 | | case CategoriaTipoBeneficio.AsignacionFamiliar: |
| | 0 | 281 | | var hijosMenores = datosTipo.CantidadHijosMenores ?? 0; |
| | 0 | 282 | | var hijosDiscapacidad = datosTipo.CantidadHijosDiscapacidad ?? 0; |
| | 0 | 283 | | if (hijosMenores + hijosDiscapacidad <= 0) |
| | | 284 | | { |
| | 0 | 285 | | return "Debe indicar al menos un hijo menor o con discapacidad para asignacion familiar"; |
| | | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | if (string.IsNullOrWhiteSpace(request.DocumentoRespaldo)) |
| | | 289 | | { |
| | 0 | 290 | | return "La documentacion probatoria es requerida para asignacion familiar"; |
| | | 291 | | } |
| | | 292 | | break; |
| | | 293 | | |
| | | 294 | | case CategoriaTipoBeneficio.HogarConstituido: |
| | 0 | 295 | | if (string.IsNullOrWhiteSpace(datosTipo.EstadoCivilUnion)) |
| | | 296 | | { |
| | 0 | 297 | | return "Debe indicar estado civil o union concubinaria para hogar constituido"; |
| | | 298 | | } |
| | | 299 | | break; |
| | | 300 | | |
| | | 301 | | case CategoriaTipoBeneficio.PrimaSolidariaFamiliar: |
| | 1 | 302 | | if (string.IsNullOrWhiteSpace(datosTipo.PrimaSolidariaFamiliarTipo)) |
| | | 303 | | { |
| | 0 | 304 | | return "Debe indicar el tipo de prima solidaria familiar"; |
| | | 305 | | } |
| | | 306 | | break; |
| | | 307 | | |
| | | 308 | | case CategoriaTipoBeneficio.PrimaNacimiento: |
| | 1 | 309 | | if (!datosTipo.FechaEvento.HasValue) |
| | | 310 | | { |
| | 0 | 311 | | return "Debe indicar la fecha de nacimiento para prima por nacimiento"; |
| | | 312 | | } |
| | | 313 | | |
| | 1 | 314 | | if ((request.VigenteDesde.Date - datosTipo.FechaEvento.Value.Date).TotalDays > 60) |
| | | 315 | | { |
| | 0 | 316 | | return "La solicitud de prima por nacimiento debe presentarse dentro de los 60 días del nacimiento. |
| | | 317 | | } |
| | | 318 | | break; |
| | | 319 | | |
| | | 320 | | case CategoriaTipoBeneficio.PrimaMatrimonio: |
| | 1 | 321 | | if (!datosTipo.FechaEvento.HasValue) |
| | | 322 | | { |
| | 0 | 323 | | return "Debe indicar la fecha de matrimonio o union"; |
| | | 324 | | } |
| | | 325 | | |
| | 1 | 326 | | if ((request.VigenteDesde.Date - datosTipo.FechaEvento.Value.Date).TotalDays > 60) |
| | | 327 | | { |
| | 0 | 328 | | return "La solicitud de prima por matrimonio debe presentarse dentro de los 60 días del matrimonio. |
| | | 329 | | } |
| | | 330 | | break; |
| | | 331 | | |
| | | 332 | | case CategoriaTipoBeneficio.General: |
| | 0 | 333 | | if (!esActualizacion && request.Cantidad.GetValueOrDefault(1) <= 0) |
| | | 334 | | { |
| | 0 | 335 | | return "La cantidad debe ser mayor a cero"; |
| | | 336 | | } |
| | | 337 | | break; |
| | | 338 | | } |
| | | 339 | | |
| | 3 | 340 | | return null; |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | private static CategoriaBeneficioRequest ConvertirCategoriaRequest(CategoriaTipoBeneficio categoria) |
| | | 344 | | { |
| | 4 | 345 | | return categoria switch |
| | 4 | 346 | | { |
| | 0 | 347 | | CategoriaTipoBeneficio.AsignacionFamiliar => CategoriaBeneficioRequest.AsignacionFamiliar, |
| | 0 | 348 | | CategoriaTipoBeneficio.HogarConstituido => CategoriaBeneficioRequest.HogarConstituido, |
| | 1 | 349 | | CategoriaTipoBeneficio.PrimaSolidariaFamiliar => CategoriaBeneficioRequest.PrimaSolidariaFamiliar, |
| | 2 | 350 | | CategoriaTipoBeneficio.PrimaNacimiento => CategoriaBeneficioRequest.PrimaNacimiento, |
| | 1 | 351 | | CategoriaTipoBeneficio.PrimaMatrimonio => CategoriaBeneficioRequest.PrimaMatrimonio, |
| | 0 | 352 | | _ => CategoriaBeneficioRequest.General |
| | 4 | 353 | | }; |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private static string ConstruirObservaciones(string? observaciones, string? justificacionRetroactivo) |
| | | 357 | | { |
| | 2 | 358 | | var observacionNormalizada = observaciones?.Trim(); |
| | 2 | 359 | | var justificacionNormalizada = justificacionRetroactivo?.Trim(); |
| | | 360 | | |
| | 2 | 361 | | if (string.IsNullOrWhiteSpace(justificacionNormalizada)) |
| | | 362 | | { |
| | 2 | 363 | | return observacionNormalizada ?? string.Empty; |
| | | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | if (string.IsNullOrWhiteSpace(observacionNormalizada)) |
| | | 367 | | { |
| | 0 | 368 | | return $"Justificacion retroactivo: {justificacionNormalizada}"; |
| | | 369 | | } |
| | | 370 | | |
| | 0 | 371 | | return $"{observacionNormalizada} | Justificacion retroactivo: {justificacionNormalizada}"; |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | private static bool RequiereJustificacionFueraPlazo(DateTime fechaEvento, DateTime vigenteDesde) |
| | | 375 | | { |
| | 0 | 376 | | return (vigenteDesde.Date - fechaEvento.Date).TotalDays > 90; |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | private static bool CalcularRetroactividadPendiente(DateTime vigenteDesde) |
| | | 380 | | { |
| | 2 | 381 | | var primerDiaMes = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); |
| | 2 | 382 | | return vigenteDesde.Date < primerDiaMes; |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | private static bool EsBeneficioUnicaVez(TipoBeneficio tipo) |
| | | 386 | | { |
| | 3 | 387 | | var categoria = DeterminarCategoria(tipo); |
| | 3 | 388 | | return categoria == CategoriaTipoBeneficio.HogarConstituido |
| | 3 | 389 | | || categoria == CategoriaTipoBeneficio.PrimaNacimiento |
| | 3 | 390 | | || categoria == CategoriaTipoBeneficio.PrimaMatrimonio; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | private static string ConstruirClaveEvento(TipoBeneficio tipo, DateTime? fechaEvento, DateTime vigenteDesde) |
| | | 394 | | { |
| | 3 | 395 | | var categoria = DeterminarCategoria(tipo); |
| | | 396 | | |
| | 3 | 397 | | return categoria switch |
| | 3 | 398 | | { |
| | 1 | 399 | | CategoriaTipoBeneficio.PrimaNacimiento => fechaEvento?.Date.ToString("yyyy-MM-dd") ?? vigenteDesde.Date.ToSt |
| | 1 | 400 | | CategoriaTipoBeneficio.PrimaMatrimonio => fechaEvento?.Date.ToString("yyyy-MM-dd") ?? vigenteDesde.Date.ToSt |
| | 0 | 401 | | CategoriaTipoBeneficio.HogarConstituido => "UNICA", |
| | 1 | 402 | | _ => vigenteDesde.Date.ToString("yyyy-MM") |
| | 3 | 403 | | }; |
| | | 404 | | } |
| | | 405 | | |
| | | 406 | | private static CategoriaTipoBeneficio DeterminarCategoria(TipoBeneficio tipo) |
| | | 407 | | { |
| | 13 | 408 | | var codigo = (tipo.Codigo ?? string.Empty).Trim().ToUpperInvariant(); |
| | 13 | 409 | | var nombre = (tipo.Nombre ?? string.Empty).Trim().ToUpperInvariant(); |
| | | 410 | | |
| | 13 | 411 | | if (codigo.StartsWith("300") || codigo.StartsWith("301") || codigo.StartsWith("302") || codigo.StartsWith("303") |
| | 13 | 412 | | || codigo.StartsWith("304") || codigo.StartsWith("305") || codigo.StartsWith("306") || codigo.StartsWith("30 |
| | 13 | 413 | | || codigo.StartsWith("308") || codigo.StartsWith("309") || codigo.StartsWith("310") |
| | 13 | 414 | | || nombre.Contains("ASIGNACION FAMILIAR")) |
| | | 415 | | { |
| | 0 | 416 | | return CategoriaTipoBeneficio.AsignacionFamiliar; |
| | | 417 | | } |
| | | 418 | | |
| | 13 | 419 | | if (codigo == "320" || nombre.Contains("HOGAR")) |
| | | 420 | | { |
| | 0 | 421 | | return CategoriaTipoBeneficio.HogarConstituido; |
| | | 422 | | } |
| | | 423 | | |
| | 13 | 424 | | if (codigo == "330" || nombre.Contains("SOLIDARIA") || nombre.Contains("PSF")) |
| | | 425 | | { |
| | 4 | 426 | | return CategoriaTipoBeneficio.PrimaSolidariaFamiliar; |
| | | 427 | | } |
| | | 428 | | |
| | 9 | 429 | | if (codigo == "340" || nombre.Contains("NACIMIENTO")) |
| | | 430 | | { |
| | 5 | 431 | | return CategoriaTipoBeneficio.PrimaNacimiento; |
| | | 432 | | } |
| | | 433 | | |
| | 4 | 434 | | if (codigo == "350" || nombre.Contains("MATRIMONIO") || nombre.Contains("UNION")) |
| | | 435 | | { |
| | 4 | 436 | | return CategoriaTipoBeneficio.PrimaMatrimonio; |
| | | 437 | | } |
| | | 438 | | |
| | 0 | 439 | | return CategoriaTipoBeneficio.General; |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | private static BeneficioSocialDto MapearDto(BeneficioSocial entidad, TipoBeneficio? tipo = null) |
| | | 443 | | { |
| | 4 | 444 | | var tipoBeneficio = tipo ?? entidad.TipoBeneficio; |
| | | 445 | | |
| | 4 | 446 | | return new BeneficioSocialDto |
| | 4 | 447 | | { |
| | 4 | 448 | | Id = entidad.Id, |
| | 4 | 449 | | PersonaId = entidad.PersonaId, |
| | 4 | 450 | | TipoBeneficioId = entidad.TipoBeneficioId, |
| | 4 | 451 | | TipoBeneficioCodigo = tipoBeneficio?.Codigo ?? string.Empty, |
| | 4 | 452 | | TipoBeneficioNombre = tipoBeneficio?.Nombre ?? string.Empty, |
| | 4 | 453 | | Cantidad = entidad.Cantidad, |
| | 4 | 454 | | VigenteDesde = entidad.VigenteDesde, |
| | 4 | 455 | | VigenteHasta = entidad.VigenteHasta, |
| | 4 | 456 | | Estado = entidad.Estado, |
| | 4 | 457 | | DocumentoRespaldo = entidad.DocumentoRespaldo, |
| | 4 | 458 | | Observaciones = entidad.Observaciones, |
| | 4 | 459 | | FechaEvento = entidad.FechaEvento, |
| | 4 | 460 | | ClaveEvento = entidad.ClaveEvento, |
| | 4 | 461 | | RetroactividadPendiente = entidad.RetroactividadPendiente |
| | 4 | 462 | | }; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | private enum CategoriaTipoBeneficio |
| | | 466 | | { |
| | | 467 | | General = 0, |
| | | 468 | | AsignacionFamiliar = 1, |
| | | 469 | | HogarConstituido = 2, |
| | | 470 | | PrimaSolidariaFamiliar = 3, |
| | | 471 | | PrimaNacimiento = 4, |
| | | 472 | | PrimaMatrimonio = 5 |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | public async Task<string> SubirDocumentoAsync(long beneficioId, Stream contenido, string nombreOriginal, string cont |
| | | 476 | | { |
| | 0 | 477 | | var entidad = await _beneficioSocialRepository.ObtenerPorIdAsync(beneficioId) |
| | 0 | 478 | | ?? throw new ArgumentException($"Beneficio social {beneficioId} no encontrado."); |
| | | 479 | | |
| | 0 | 480 | | var ext = Path.GetExtension(nombreOriginal); |
| | 0 | 481 | | var nombreSanitizado = Path.GetFileNameWithoutExtension(nombreOriginal) |
| | 0 | 482 | | .Replace(" ", "_").Replace("..", "") + ext; |
| | 0 | 483 | | var rutaRelativa = $"beneficios/{beneficioId}/{nombreSanitizado}"; |
| | | 484 | | |
| | 0 | 485 | | await _storage.GuardarAsync(contenido, rutaRelativa, contentType); |
| | 0 | 486 | | entidad.DocumentoRespaldo = rutaRelativa; |
| | 0 | 487 | | await _beneficioSocialRepository.ActualizarAsync(entidad); |
| | | 488 | | |
| | 0 | 489 | | return rutaRelativa; |
| | 0 | 490 | | } |
| | | 491 | | } |