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