| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using FAU.DataAccess.Repositories; |
| | | 3 | | using FAU.Entidades; |
| | | 4 | | using FAU.Logica.DTOs; |
| | | 5 | | using FAU.Logica.Validadores; |
| | | 6 | | |
| | | 7 | | namespace FAU.Logica.Services; |
| | | 8 | | |
| | | 9 | | public class CompPersonal048018Service : ICompPersonal048018Service |
| | | 10 | | { |
| | | 11 | | private readonly ICompPersonal048018Repository _repositorio; |
| | | 12 | | private readonly IPersonalRepository _repositorioPersonal; |
| | | 13 | | private readonly IAuditoriaService _auditoria; |
| | | 14 | | |
| | 12 | 15 | | public CompPersonal048018Service( |
| | 12 | 16 | | ICompPersonal048018Repository repositorio, |
| | 12 | 17 | | IPersonalRepository repositorioPersonal, |
| | 12 | 18 | | IAuditoriaService auditoria) |
| | | 19 | | { |
| | 12 | 20 | | _repositorio = repositorio; |
| | 12 | 21 | | _repositorioPersonal = repositorioPersonal; |
| | 12 | 22 | | _auditoria = auditoria; |
| | 12 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<PagedResult<CompPersonal048018Dto>> ObtenerPaginadoAsync( |
| | | 26 | | int pagina, |
| | | 27 | | int tamano, |
| | | 28 | | string? busqueda, |
| | | 29 | | string? estado) |
| | | 30 | | { |
| | 0 | 31 | | pagina = Math.Max(1, pagina); |
| | 0 | 32 | | tamano = Math.Clamp(tamano, 1, 100); |
| | | 33 | | |
| | 0 | 34 | | var (items, total) = await _repositorio.ObtenerPaginadoAsync(pagina, tamano, busqueda, estado); |
| | | 35 | | |
| | 0 | 36 | | return new PagedResult<CompPersonal048018Dto> |
| | 0 | 37 | | { |
| | 0 | 38 | | Items = items.Select(MapearDto), |
| | 0 | 39 | | Page = pagina, |
| | 0 | 40 | | PageSize = tamano, |
| | 0 | 41 | | TotalCount = total |
| | 0 | 42 | | }; |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<CompPersonal048018Dto?> ObtenerPorIdAsync(long id) |
| | | 46 | | { |
| | 0 | 47 | | var entidad = await _repositorio.ObtenerPorIdAsync(id); |
| | 0 | 48 | | return entidad is null ? null : MapearDto(entidad); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<CompPersonal048018Dto> CrearAsync( |
| | | 52 | | CrearCompPersonal048018Request request, |
| | | 53 | | long usuarioId, |
| | | 54 | | string? host) |
| | | 55 | | { |
| | 14 | 56 | | ValidarImporte(request.Importe); |
| | 13 | 57 | | var cedula = NormalizarCedula(request.CedulaTitular); |
| | 13 | 58 | | var relacion = await _repositorioPersonal.GetByCedulaAsync(cedula) |
| | 13 | 59 | | ?? throw new ArgumentException($"No se encontró personal activo con cédula {cedula}."); |
| | | 60 | | |
| | 12 | 61 | | var existente = await _repositorio.ObtenerActivoPorPersonaAsync(relacion.PersonaId); |
| | 12 | 62 | | if (existente is not null) |
| | 1 | 63 | | throw new InvalidOperationException( |
| | 1 | 64 | | "La persona ya tiene una Compensación Personal (048.018) activa. Dela de baja antes de cargar una nueva. |
| | | 65 | | |
| | 11 | 66 | | var entidad = new CompPersonal048018 |
| | 11 | 67 | | { |
| | 11 | 68 | | PersonaId = relacion.PersonaId, |
| | 11 | 69 | | Importe = RedondearImporte(request.Importe), |
| | 11 | 70 | | Estado = "activo", |
| | 11 | 71 | | VigenteDesde = (request.VigenteDesde ?? DateTime.Today).Date, |
| | 11 | 72 | | CreadoPor = UsuarioValido(usuarioId), |
| | 11 | 73 | | CreadoEn = DateTime.Now |
| | 11 | 74 | | }; |
| | | 75 | | |
| | 11 | 76 | | await _repositorio.CrearAsync(entidad); |
| | 11 | 77 | | await _auditoria.LogAuditoriaAsync( |
| | 11 | 78 | | usuarioId, |
| | 11 | 79 | | AccionEnum.CrearCompPersonal, |
| | 11 | 80 | | ContextoEnum.Compensaciones, |
| | 11 | 81 | | host, |
| | 11 | 82 | | nameof(CompPersonal048018), |
| | 11 | 83 | | entidad.Id, |
| | 11 | 84 | | new |
| | 11 | 85 | | { |
| | 11 | 86 | | entidad.PersonaId, |
| | 11 | 87 | | Cedula = cedula, |
| | 11 | 88 | | entidad.Importe, |
| | 11 | 89 | | entidad.VigenteDesde |
| | 11 | 90 | | }); |
| | | 91 | | |
| | 11 | 92 | | var completo = await _repositorio.ObtenerPorIdAsync(entidad.Id) |
| | 11 | 93 | | ?? throw new InvalidOperationException("No fue posible recuperar la Compensación Personal creada."); |
| | 11 | 94 | | return MapearDto(completo); |
| | 11 | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task<CompPersonal048018Dto> ActualizarAsync( |
| | | 98 | | long id, |
| | | 99 | | ActualizarCompPersonal048018Request request, |
| | | 100 | | long usuarioId, |
| | | 101 | | string? host) |
| | | 102 | | { |
| | 5 | 103 | | ValidarImporte(request.Importe); |
| | 4 | 104 | | var entidad = await _repositorio.ObtenerPorIdAsync(id) |
| | 4 | 105 | | ?? throw new ArgumentException($"Compensación Personal {id} no encontrada."); |
| | 4 | 106 | | if (entidad.Estado != "activo") |
| | 1 | 107 | | throw new InvalidOperationException( |
| | 1 | 108 | | "No se puede editar una Compensación Personal dada de baja. Cargue una nueva."); |
| | | 109 | | |
| | 3 | 110 | | var importeAnterior = entidad.Importe; |
| | 3 | 111 | | var vigenteDesdeAnterior = entidad.VigenteDesde; |
| | 3 | 112 | | entidad.Importe = RedondearImporte(request.Importe); |
| | 3 | 113 | | if (request.VigenteDesde.HasValue) |
| | 1 | 114 | | entidad.VigenteDesde = request.VigenteDesde.Value.Date; |
| | 3 | 115 | | await _repositorio.ActualizarAsync(entidad); |
| | | 116 | | |
| | 3 | 117 | | await _auditoria.LogAuditoriaAsync( |
| | 3 | 118 | | usuarioId, |
| | 3 | 119 | | AccionEnum.ActualizarCompPersonal, |
| | 3 | 120 | | ContextoEnum.Compensaciones, |
| | 3 | 121 | | host, |
| | 3 | 122 | | nameof(CompPersonal048018), |
| | 3 | 123 | | entidad.Id, |
| | 3 | 124 | | new |
| | 3 | 125 | | { |
| | 3 | 126 | | entidad.PersonaId, |
| | 3 | 127 | | ImporteAnterior = importeAnterior, |
| | 3 | 128 | | ImporteNuevo = entidad.Importe, |
| | 3 | 129 | | VigenteDesdeAnterior = vigenteDesdeAnterior, |
| | 3 | 130 | | VigenteDesdeNuevo = entidad.VigenteDesde |
| | 3 | 131 | | }); |
| | | 132 | | |
| | 3 | 133 | | return MapearDto(entidad); |
| | 3 | 134 | | } |
| | | 135 | | |
| | | 136 | | public async Task DarBajaAsync(long id, long usuarioId, string? host) |
| | | 137 | | { |
| | 5 | 138 | | var entidad = await _repositorio.ObtenerPorIdAsync(id) |
| | 5 | 139 | | ?? throw new ArgumentException($"Compensación Personal {id} no encontrada."); |
| | 5 | 140 | | if (entidad.Estado != "activo") |
| | 1 | 141 | | throw new InvalidOperationException("La Compensación Personal ya está dada de baja."); |
| | | 142 | | |
| | 4 | 143 | | entidad.Estado = "anulado"; |
| | 4 | 144 | | entidad.VigenteHasta ??= DateTime.Today.Date; |
| | 4 | 145 | | await _repositorio.ActualizarAsync(entidad); |
| | | 146 | | |
| | 4 | 147 | | await _auditoria.LogAuditoriaAsync( |
| | 4 | 148 | | usuarioId, |
| | 4 | 149 | | AccionEnum.DarBajaCompPersonal, |
| | 4 | 150 | | ContextoEnum.Compensaciones, |
| | 4 | 151 | | host, |
| | 4 | 152 | | nameof(CompPersonal048018), |
| | 4 | 153 | | entidad.Id, |
| | 4 | 154 | | new |
| | 4 | 155 | | { |
| | 4 | 156 | | entidad.PersonaId, |
| | 4 | 157 | | entidad.Importe, |
| | 4 | 158 | | entidad.VigenteHasta |
| | 4 | 159 | | }); |
| | 4 | 160 | | } |
| | | 161 | | |
| | | 162 | | private static string NormalizarCedula(string cedula) |
| | | 163 | | { |
| | 13 | 164 | | var normalizada = CedulaValidator.Limpiar(cedula ?? string.Empty); |
| | 13 | 165 | | if (!Regex.IsMatch(normalizada, @"^\d{7,8}$")) |
| | 0 | 166 | | throw new ArgumentException("La cédula debe contener 7 u 8 dígitos."); |
| | 13 | 167 | | return normalizada; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | private static decimal RedondearImporte(decimal importe) => |
| | 14 | 171 | | Math.Round(importe, 2, MidpointRounding.AwayFromZero); |
| | | 172 | | |
| | | 173 | | private static void ValidarImporte(decimal importe) |
| | | 174 | | { |
| | 19 | 175 | | if (importe <= 0) |
| | 2 | 176 | | throw new ArgumentException("El importe debe ser mayor a cero."); |
| | 17 | 177 | | } |
| | | 178 | | |
| | 11 | 179 | | private static long? UsuarioValido(long usuarioId) => usuarioId > 0 ? usuarioId : null; |
| | | 180 | | |
| | 14 | 181 | | private static CompPersonal048018Dto MapearDto(CompPersonal048018 entidad) => new() |
| | 14 | 182 | | { |
| | 14 | 183 | | Id = entidad.Id, |
| | 14 | 184 | | PersonaId = entidad.PersonaId, |
| | 14 | 185 | | Cedula = entidad.Persona.Cedula, |
| | 14 | 186 | | NombreCompleto = entidad.Persona.NombreCompleto, |
| | 14 | 187 | | Importe = entidad.Importe, |
| | 14 | 188 | | Estado = entidad.Estado, |
| | 14 | 189 | | VigenteDesde = entidad.VigenteDesde, |
| | 14 | 190 | | VigenteHasta = entidad.VigenteHasta, |
| | 14 | 191 | | CreadoEn = entidad.CreadoEn |
| | 14 | 192 | | }; |
| | | 193 | | } |