| | | 1 | | using FAU.DataAccess; |
| | | 2 | | using FAU.DataAccess.Repositories; |
| | | 3 | | using FAU.Entidades; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace FAU.Logica.Services; |
| | | 7 | | |
| | | 8 | | public class DescuentoPersonalService : IDescuentoPersonalService |
| | | 9 | | { |
| | | 10 | | private readonly IDescuentoPersonalRepository _repo; |
| | | 11 | | private readonly IPersonalRepository _personalRepo; |
| | | 12 | | private readonly IPeriodosRepository _periodosRepo; |
| | | 13 | | private readonly ApplicationDbContext _context; |
| | | 14 | | |
| | 9 | 15 | | public DescuentoPersonalService( |
| | 9 | 16 | | IDescuentoPersonalRepository repo, |
| | 9 | 17 | | IPersonalRepository personalRepo, |
| | 9 | 18 | | IPeriodosRepository periodosRepo, |
| | 9 | 19 | | ApplicationDbContext context) |
| | | 20 | | { |
| | 9 | 21 | | _repo = repo; |
| | 9 | 22 | | _personalRepo = personalRepo; |
| | 9 | 23 | | _periodosRepo = periodosRepo; |
| | 9 | 24 | | _context = context; |
| | 9 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<DescuentoPersonalPeriodo> CrearAsync( |
| | | 28 | | long periodoId, string cedulaTitular, int codigoConcepto, |
| | | 29 | | decimal importe, string? observaciones, long usuarioId) |
| | | 30 | | { |
| | 4 | 31 | | if (importe <= 0) |
| | 1 | 32 | | throw new ArgumentException("El importe debe ser mayor a cero."); |
| | | 33 | | |
| | 3 | 34 | | var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular) |
| | 3 | 35 | | ?? throw new ArgumentException($"Funcionario {cedulaTitular} no encontrado."); |
| | | 36 | | |
| | 3 | 37 | | var periodo = await _periodosRepo.ObtenerPorIdAsync(periodoId) |
| | 3 | 38 | | ?? throw new ArgumentException($"Período {periodoId} no encontrado."); |
| | | 39 | | |
| | 3 | 40 | | if (periodo.Estado != "ABIERTA" && periodo.Estado != "LISTA_CALCULO") |
| | 1 | 41 | | throw new InvalidOperationException($"El período está en estado {periodo.Estado} y no admite modificaciones. |
| | | 42 | | |
| | 2 | 43 | | var catalogoItem = await _context.CatalogoItems |
| | 2 | 44 | | .FirstOrDefaultAsync(c => c.Codigo == codigoConcepto && c.Vigente) |
| | 2 | 45 | | ?? throw new ArgumentException($"Concepto con código {codigoConcepto} no encontrado o no vigente."); |
| | | 46 | | |
| | 2 | 47 | | if (catalogoItem.Tipo != "descuento_personal") |
| | 1 | 48 | | throw new InvalidOperationException($"El concepto {codigoConcepto} (tipo '{catalogoItem.Tipo}') no es un des |
| | | 49 | | |
| | 1 | 50 | | var descuento = new DescuentoPersonalPeriodo |
| | 1 | 51 | | { |
| | 1 | 52 | | PeriodoId = periodoId, |
| | 1 | 53 | | PersonaId = relacion.PersonaId, |
| | 1 | 54 | | CatalogoItemId = catalogoItem.Id, |
| | 1 | 55 | | Importe = importe, |
| | 1 | 56 | | Estado = "borrador", |
| | 1 | 57 | | Observaciones = observaciones, |
| | 1 | 58 | | UsuarioCreacionId = usuarioId |
| | 1 | 59 | | }; |
| | | 60 | | |
| | 1 | 61 | | return await _repo.CreateAsync(descuento); |
| | 1 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<DescuentoPersonalPeriodo> EditarAsync(long id, decimal importe, string? observaciones) |
| | | 65 | | { |
| | 2 | 66 | | if (importe <= 0) |
| | 0 | 67 | | throw new ArgumentException("El importe debe ser mayor a cero."); |
| | | 68 | | |
| | 2 | 69 | | var d = await _repo.GetByIdAsync(id) |
| | 2 | 70 | | ?? throw new ArgumentException($"Descuento {id} no encontrado."); |
| | | 71 | | |
| | 2 | 72 | | if (d.Estado != "borrador") |
| | 1 | 73 | | throw new InvalidOperationException("Solo se pueden editar descuentos en estado borrador."); |
| | | 74 | | |
| | 1 | 75 | | d.Importe = importe; |
| | 1 | 76 | | d.Observaciones = observaciones; |
| | 1 | 77 | | return await _repo.UpdateAsync(d); |
| | 1 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task EliminarAsync(long id) |
| | | 81 | | { |
| | 2 | 82 | | var d = await _repo.GetByIdAsync(id) |
| | 2 | 83 | | ?? throw new ArgumentException($"Descuento {id} no encontrado."); |
| | | 84 | | |
| | 2 | 85 | | if (d.Estado != "borrador") |
| | 1 | 86 | | throw new InvalidOperationException("Solo se pueden eliminar descuentos en estado borrador."); |
| | | 87 | | |
| | 1 | 88 | | await _repo.DeleteAsync(id); |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | public async Task<IEnumerable<DescuentoPersonalPeriodo>> ListarAsync( |
| | | 92 | | long periodoId, string? cedulaTitular, int? codigoConcepto, string? estado) |
| | | 93 | | { |
| | 0 | 94 | | long? personaId = null; |
| | 0 | 95 | | if (!string.IsNullOrEmpty(cedulaTitular)) |
| | | 96 | | { |
| | 0 | 97 | | var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular); |
| | 0 | 98 | | personaId = relacion?.PersonaId; |
| | | 99 | | } |
| | 0 | 100 | | return await _repo.GetByPeriodoAsync(periodoId, personaId, codigoConcepto, estado); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public Task<int> ConfirmarBorradoresAsync(long periodoId) => |
| | 1 | 104 | | _repo.ConfirmarBorradoresAsync(periodoId); |
| | | 105 | | } |