| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace FAU.Logica.Validadores; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Validador para campos de compensación del personal |
| | | 8 | | /// </summary> |
| | | 9 | | public static class CompensacionValidator |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Valida que un valor sea numérico (entero o decimal) |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="valor">Valor a validar</param> |
| | | 15 | | /// <returns>True si es válido o null/vacío</returns> |
| | | 16 | | public static bool ValidarNumerico(string? valor) |
| | | 17 | | { |
| | 0 | 18 | | if (string.IsNullOrWhiteSpace(valor)) |
| | 0 | 19 | | return true; // Es opcional |
| | | 20 | | |
| | | 21 | | // Permitir números enteros y decimales (con coma o punto) |
| | 0 | 22 | | return Regex.IsMatch(valor.Trim(), @"^-?\d+([.,]\d+)?$"); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Valida que sea un número decimal válido y positivo |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="valor">Valor a validar</param> |
| | | 29 | | /// <returns>True si es válido o null/vacío</returns> |
| | | 30 | | public static bool ValidarDecimalPositivo(string? valor) |
| | | 31 | | { |
| | 4 | 32 | | if (string.IsNullOrWhiteSpace(valor)) |
| | 4 | 33 | | return true; // Es opcional |
| | | 34 | | |
| | 0 | 35 | | var valorTrimmed = valor.Trim(); |
| | | 36 | | |
| | | 37 | | // Reemplazar coma por punto para estandarizar |
| | 0 | 38 | | valorTrimmed = valorTrimmed.Replace(',', '.'); |
| | | 39 | | |
| | 0 | 40 | | if (!decimal.TryParse(valorTrimmed, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var numero |
| | 0 | 41 | | return false; |
| | | 42 | | |
| | 0 | 43 | | return numero >= 0; // Debe ser positivo |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Obtiene un mensaje de error si la Prima Técnica es inválida |
| | | 48 | | /// </summary> |
| | | 49 | | public static string? ObtenerErrorPrimaTecnica(string? valor) |
| | | 50 | | { |
| | 2 | 51 | | if (!ValidarDecimalPositivo(valor)) |
| | | 52 | | { |
| | 0 | 53 | | return $"Prima Técnica debe ser un número decimal positivo válido. Valor ingresado: {valor}"; |
| | | 54 | | } |
| | 2 | 55 | | return null; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Obtiene un mensaje de error si la Prima Solidaria Familiar es inválida |
| | | 60 | | /// </summary> |
| | | 61 | | public static string? ObtenerErrorPrimaSolidaria(string? valor) |
| | | 62 | | { |
| | 2 | 63 | | if (!ValidarDecimalPositivo(valor)) |
| | | 64 | | { |
| | 0 | 65 | | return $"Prima Solidaria Familiar debe ser un número decimal positivo válido. Valor ingresado: {valor}"; |
| | | 66 | | } |
| | 2 | 67 | | return null; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Obtiene un mensaje de error si el Riesgo de Vuelo es inválido |
| | | 72 | | /// </summary> |
| | | 73 | | public static string? ObtenerErrorRiesgoVuelo(decimal? valor) |
| | | 74 | | { |
| | 2 | 75 | | if (valor.HasValue && valor.Value < 0) |
| | | 76 | | { |
| | 0 | 77 | | return $"Riesgo de Vuelo debe ser un número positivo. Valor ingresado: {valor}"; |
| | | 78 | | } |
| | 2 | 79 | | return null; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Obtiene un mensaje de error si los Años Inactivos son inválidos |
| | | 84 | | /// </summary> |
| | | 85 | | public static string? ObtenerErrorAniosInactivos(int? valor) |
| | | 86 | | { |
| | 2 | 87 | | if (valor.HasValue && valor.Value < 0) |
| | | 88 | | { |
| | 0 | 89 | | return $"Años Inactivos debe ser un número positivo. Valor ingresado: {valor}"; |
| | | 90 | | } |
| | 2 | 91 | | return null; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Valida todos los campos de compensación y retorna el primer error encontrado |
| | | 96 | | /// </summary> |
| | | 97 | | public static string? ValidarCompensaciones(decimal? riesgoVuelo, string? primaTecnica, |
| | | 98 | | string? primaSolidaria, int? aniosInactivos) |
| | | 99 | | { |
| | 2 | 100 | | var error = ObtenerErrorRiesgoVuelo(riesgoVuelo); |
| | 2 | 101 | | if (error != null) return error; |
| | | 102 | | |
| | 2 | 103 | | error = ObtenerErrorPrimaTecnica(primaTecnica); |
| | 2 | 104 | | if (error != null) return error; |
| | | 105 | | |
| | 2 | 106 | | error = ObtenerErrorPrimaSolidaria(primaSolidaria); |
| | 2 | 107 | | if (error != null) return error; |
| | | 108 | | |
| | 2 | 109 | | error = ObtenerErrorAniosInactivos(aniosInactivos); |
| | 2 | 110 | | if (error != null) return error; |
| | | 111 | | |
| | 2 | 112 | | return null; |
| | | 113 | | } |
| | | 114 | | } |