| | | 1 | | namespace FAU.Logica.Services; |
| | | 2 | | |
| | | 3 | | using FAU.DataAccess.Repositories; |
| | | 4 | | using FAU.Entidades; |
| | | 5 | | using FAU.Logica.DTOs; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Servicio IRPF - orquesta cálculo + persistencia + auditoría. |
| | | 9 | | /// </summary> |
| | | 10 | | public class IrpfService : IIrpfService |
| | | 11 | | { |
| | | 12 | | private readonly IrpfCalculator _calculator; |
| | | 13 | | private readonly IIrpfRepository _repository; |
| | | 14 | | private readonly IAuditoriaService _auditoriaService; |
| | | 15 | | private readonly ICurrentUserContext _currentUser; |
| | | 16 | | |
| | 2 | 17 | | public IrpfService( |
| | 2 | 18 | | IrpfCalculator calculator, |
| | 2 | 19 | | IIrpfRepository repository, |
| | 2 | 20 | | IAuditoriaService auditoriaService, |
| | 2 | 21 | | ICurrentUserContext currentUser) |
| | | 22 | | { |
| | 2 | 23 | | _calculator = calculator; |
| | 2 | 24 | | _repository = repository; |
| | 2 | 25 | | _auditoriaService = auditoriaService; |
| | 2 | 26 | | _currentUser = currentUser; |
| | 2 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Calcula IRPF y lo persiste en BD con auditoría. |
| | | 31 | | /// </summary> |
| | | 32 | | public async Task<(bool Exito, long? IdIrpfMensual, string? Error)> CalcularYPersistirAsync( |
| | | 33 | | long liquidacionId, |
| | | 34 | | long personaId, |
| | | 35 | | CalcularIrpfRequest request) |
| | | 36 | | { |
| | | 37 | | try |
| | | 38 | | { |
| | | 39 | | // 1. Verificar si ya existe |
| | 2 | 40 | | if (await _repository.ExisteIrpfMensualAsync(liquidacionId, personaId)) |
| | 1 | 41 | | return (false, null, "Ya existe un IRPF calculado para esta liquidación y persona"); |
| | | 42 | | |
| | | 43 | | // 2. Calcular |
| | 1 | 44 | | var resultadoCalculo = _calculator.Calcular(request); |
| | | 45 | | |
| | | 46 | | // 3. Mapear resultado a entidad IrpfMensual |
| | 1 | 47 | | var irpfMensual = new IrpfMensual |
| | 1 | 48 | | { |
| | 1 | 49 | | LiquidacionId = liquidacionId, |
| | 1 | 50 | | PersonaId = personaId, |
| | 1 | 51 | | IngresosGravados = resultadoCalculo.IngresoGravado, |
| | 1 | 52 | | DeduccionAportes = resultadoCalculo.Deducciones.Aportes, |
| | 1 | 53 | | DeduccionHijos = resultadoCalculo.Deducciones.Hijos, |
| | 1 | 54 | | DeduccionConyuge = resultadoCalculo.Deducciones.Conyuge, |
| | 1 | 55 | | DeduccionProfesional = resultadoCalculo.Deducciones.Profesional, |
| | 1 | 56 | | TasaDeduccionProfesional = resultadoCalculo.TasaDeduccionProfesional, |
| | 1 | 57 | | TotalDeducciones = resultadoCalculo.TotalDeducciones, |
| | 1 | 58 | | BaseImponibleNeta = resultadoCalculo.BaseImponibleNeta, |
| | 1 | 59 | | TotalIrpf = resultadoCalculo.TotalIrpf |
| | 1 | 60 | | }; |
| | | 61 | | |
| | | 62 | | // 4. Mapear detalles por franja |
| | 1 | 63 | | var detalleFranjas = resultadoCalculo.DetallePorFranja |
| | 2 | 64 | | .Select(d => new IrpfDetalleFranjas |
| | 2 | 65 | | { |
| | 2 | 66 | | FranjaIrpfId = d.FranjaId, |
| | 2 | 67 | | BaseEnFranja = d.BaseFranja, |
| | 2 | 68 | | Tasa = d.Tasa, |
| | 2 | 69 | | Impuesto = d.Impuesto |
| | 2 | 70 | | }) |
| | 1 | 71 | | .ToList(); |
| | | 72 | | |
| | | 73 | | // 5. Persistir |
| | 1 | 74 | | var irpfCreado = await _repository.CrearIrpfMensualAsync(irpfMensual, detalleFranjas); |
| | | 75 | | |
| | | 76 | | // 6. Auditar (crear entidad BitacoraAuditoria) |
| | 1 | 77 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 78 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 79 | | accion: AccionEnum.CrearIrpf, |
| | 1 | 80 | | contexto: ContextoEnum.IRPF, |
| | 1 | 81 | | entidad: "irpf_mensual", |
| | 1 | 82 | | entidadId: irpfCreado.Id, |
| | 1 | 83 | | detalle: $"LiquidacionId={liquidacionId}, PersonaId={personaId}, TotalIrpf={irpfCreado.TotalIrpf}"); |
| | | 84 | | |
| | 1 | 85 | | return (true, irpfCreado.Id, null); |
| | | 86 | | } |
| | 0 | 87 | | catch (Exception ex) |
| | | 88 | | { |
| | 0 | 89 | | return (false, null, $"Error al calcular IRPF: {ex.Message}"); |
| | | 90 | | } |
| | 2 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Obtiene un IRPF por ID con sus detalles. |
| | | 95 | | /// </summary> |
| | | 96 | | public async Task<IrpfMensual?> ObtenerPorIdAsync(long id) |
| | | 97 | | { |
| | 0 | 98 | | return await _repository.ObtenerIrpfMensualPorIdAsync(id); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Actualiza un IRPF (retroactividad, etc). |
| | | 103 | | /// Recalcula y reemplaza valores y detalles por franja. |
| | | 104 | | /// </summary> |
| | | 105 | | public async Task<(bool Exito, long? IdIrpfMensual, string? Error)> ActualizarAsync( |
| | | 106 | | long id, |
| | | 107 | | CalcularIrpfRequest request) |
| | | 108 | | { |
| | | 109 | | try |
| | | 110 | | { |
| | | 111 | | // 1. Obtener IRPF existente |
| | 0 | 112 | | var irpfExistente = await _repository.ObtenerIrpfMensualPorIdAsync(id); |
| | 0 | 113 | | if (irpfExistente == null) |
| | | 114 | | { |
| | 0 | 115 | | return (false, null, "IRPF no encontrado"); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // 2. Guardar datos antiguos para auditoría |
| | 0 | 119 | | var datosAntiguos = $"TotalIrpf={irpfExistente.TotalIrpf}, BaseImponibleNeta={irpfExistente.BaseImponibleNet |
| | | 120 | | |
| | | 121 | | // 3. Recalcular |
| | 0 | 122 | | var resultadoCalculo = _calculator.Calcular(request); |
| | | 123 | | |
| | | 124 | | // 4. Actualizar entidad IrpfMensual |
| | 0 | 125 | | irpfExistente.IngresosGravados = resultadoCalculo.IngresoGravado; |
| | 0 | 126 | | irpfExistente.DeduccionAportes = resultadoCalculo.Deducciones.Aportes; |
| | 0 | 127 | | irpfExistente.DeduccionHijos = resultadoCalculo.Deducciones.Hijos; |
| | 0 | 128 | | irpfExistente.DeduccionConyuge = resultadoCalculo.Deducciones.Conyuge; |
| | 0 | 129 | | irpfExistente.DeduccionProfesional = resultadoCalculo.Deducciones.Profesional; |
| | 0 | 130 | | irpfExistente.TasaDeduccionProfesional = resultadoCalculo.TasaDeduccionProfesional; |
| | 0 | 131 | | irpfExistente.TotalDeducciones = resultadoCalculo.TotalDeducciones; |
| | 0 | 132 | | irpfExistente.BaseImponibleNeta = resultadoCalculo.BaseImponibleNeta; |
| | 0 | 133 | | irpfExistente.TotalIrpf = resultadoCalculo.TotalIrpf; |
| | | 134 | | |
| | | 135 | | // 5. Mapear nuevos detalles |
| | 0 | 136 | | var nuevoDetalle = resultadoCalculo.DetallePorFranja |
| | 0 | 137 | | .Select(d => new IrpfDetalleFranjas |
| | 0 | 138 | | { |
| | 0 | 139 | | FranjaIrpfId = d.FranjaId, |
| | 0 | 140 | | BaseEnFranja = d.BaseFranja, |
| | 0 | 141 | | Tasa = d.Tasa, |
| | 0 | 142 | | Impuesto = d.Impuesto |
| | 0 | 143 | | }) |
| | 0 | 144 | | .ToList(); |
| | | 145 | | |
| | | 146 | | // 6. Persistir |
| | 0 | 147 | | var irpfActualizado = await _repository.ActualizarIrpfMensualAsync(irpfExistente, nuevoDetalle); |
| | | 148 | | |
| | | 149 | | // 7. Auditar |
| | 0 | 150 | | var datosNuevos = $"TotalIrpf={irpfActualizado.TotalIrpf}, BaseImponibleNeta={irpfActualizado.BaseImponibleN |
| | 0 | 151 | | await _auditoriaService.LogAuditoriaAsync( |
| | 0 | 152 | | usuarioId: _currentUser.UserId ?? 0, |
| | 0 | 153 | | accion: AccionEnum.ActualizarIrpf, |
| | 0 | 154 | | contexto: ContextoEnum.IRPF, |
| | 0 | 155 | | entidad: "irpf_mensual", |
| | 0 | 156 | | entidadId: irpfActualizado.Id, |
| | 0 | 157 | | detalle: new { Anterior = datosAntiguos, Nuevo = datosNuevos }); |
| | | 158 | | |
| | 0 | 159 | | return (true, irpfActualizado.Id, null); |
| | | 160 | | } |
| | 0 | 161 | | catch (Exception ex) |
| | | 162 | | { |
| | 0 | 163 | | return (false, null, $"Error al actualizar IRPF: {ex.Message}"); |
| | | 164 | | } |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Obtiene IRPF paginado para una liquidación. |
| | | 169 | | /// </summary> |
| | | 170 | | public async Task<(IEnumerable<IrpfMensual> Items, int TotalCount)> ObtenerPaginadoAsync( |
| | | 171 | | long liquidacionId, |
| | | 172 | | int pagina, |
| | | 173 | | int tamano) |
| | | 174 | | { |
| | 0 | 175 | | return await _repository.ObtenerIrpfsMensualPorLiquidacionAsync(liquidacionId, pagina, tamano); |
| | 0 | 176 | | } |
| | | 177 | | } |
| | | 178 | | |