| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using FAU.Logica.DTOs; |
| | | 7 | | |
| | | 8 | | namespace FAU.Logica.Calculators |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Calculador de déficit que aplica descuentos personales respetando el mínimo legal (35% del bruto). |
| | | 12 | | /// Implementación simple: ordena por PrioridadDeficit asc y aplica hasta agotar el disponible. |
| | | 13 | | /// </summary> |
| | | 14 | | public class DeficitCalculator : IDeficitCalculator |
| | | 15 | | { |
| | 0 | 16 | | private const decimal PorcentajeRetencionMinima = 0.35M; |
| | | 17 | | |
| | | 18 | | public Task<IEnumerable<DescuentoAplicacionResult>> AplicarAsync( |
| | | 19 | | decimal nominalBruto, |
| | | 20 | | decimal descuentosLegales, |
| | | 21 | | IEnumerable<DescuentoPersonalInput> descuentosConfirmados, |
| | | 22 | | CancellationToken ct = default) |
| | | 23 | | { |
| | 16 | 24 | | if (descuentosConfirmados == null) |
| | 0 | 25 | | return Task.FromResult(Enumerable.Empty<DescuentoAplicacionResult>()); |
| | | 26 | | |
| | | 27 | | // Monto que debe quedar para el afiliado: 35% del nominal bruto |
| | 16 | 28 | | var minimo = Math.Round(nominalBruto * PorcentajeRetencionMinima, 2); |
| | | 29 | | |
| | | 30 | | // Disponible para descuentos personales = nominalBruto - descuentosLegales - minimo |
| | 16 | 31 | | var disponibleTotal = Math.Round(nominalBruto - descuentosLegales - minimo, 2); |
| | | 32 | | |
| | 16 | 33 | | var resultados = new List<DescuentoAplicacionResult>(); |
| | | 34 | | |
| | 16 | 35 | | if (disponibleTotal <= 0) |
| | | 36 | | { |
| | | 37 | | // No hay espacio para descuentos personales |
| | 13 | 38 | | foreach (var d in descuentosConfirmados.OrderBy(x => x.PrioridadDeficit)) |
| | | 39 | | { |
| | 3 | 40 | | resultados.Add(new DescuentoAplicacionResult( |
| | 3 | 41 | | d.DescuentoPeriodoId, |
| | 3 | 42 | | d.PersonaId, |
| | 3 | 43 | | d.AcreedorId, |
| | 3 | 44 | | d.MontoSolicitado, |
| | 3 | 45 | | 0m, |
| | 3 | 46 | | d.MontoSolicitado, |
| | 3 | 47 | | "deficit", |
| | 3 | 48 | | "No hay disponible según mínima retención (35%)" |
| | 3 | 49 | | )); |
| | | 50 | | } |
| | | 51 | | |
| | 2 | 52 | | return Task.FromResult<IEnumerable<DescuentoAplicacionResult>>(resultados); |
| | | 53 | | } |
| | | 54 | | |
| | 14 | 55 | | decimal restante = disponibleTotal; |
| | | 56 | | |
| | 190 | 57 | | foreach (var d in descuentosConfirmados.OrderBy(x => x.PrioridadDeficit)) |
| | | 58 | | { |
| | 39 | 59 | | if (ct.IsCancellationRequested) break; |
| | | 60 | | |
| | 37 | 61 | | var aplicar = Math.Min(d.MontoSolicitado, Math.Round(Math.Max(0, restante), 2)); |
| | | 62 | | |
| | 37 | 63 | | var estado = aplicar == d.MontoSolicitado ? "aplicado" |
| | 37 | 64 | | : aplicar > 0 ? "parcial" : "deficit"; |
| | | 65 | | |
| | 37 | 66 | | var motivo = aplicar == 0 ? "deficit" : null; |
| | | 67 | | |
| | 37 | 68 | | resultados.Add(new DescuentoAplicacionResult( |
| | 37 | 69 | | d.DescuentoPeriodoId, |
| | 37 | 70 | | d.PersonaId, |
| | 37 | 71 | | d.AcreedorId, |
| | 37 | 72 | | d.MontoSolicitado, |
| | 37 | 73 | | aplicar, |
| | 37 | 74 | | d.MontoSolicitado - aplicar, |
| | 37 | 75 | | estado, |
| | 37 | 76 | | motivo |
| | 37 | 77 | | )); |
| | | 78 | | |
| | 37 | 79 | | restante -= aplicar; |
| | 58 | 80 | | if (restante <= 0) restante = 0; |
| | | 81 | | } |
| | | 82 | | |
| | 14 | 83 | | return Task.FromResult<IEnumerable<DescuentoAplicacionResult>>(resultados); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |