| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using FAU.DataAccess.Repositories; |
| | | 8 | | using FAU.Entidades; |
| | | 9 | | using FAU.Logica.DTOs; |
| | | 10 | | |
| | | 11 | | namespace FAU.Logica.Services; |
| | | 12 | | |
| | | 13 | | public class ReporteDeficitService : IReporteDeficitService |
| | | 14 | | { |
| | | 15 | | private readonly IReporteDeficitRepository _repo; |
| | | 16 | | |
| | 7 | 17 | | public ReporteDeficitService(IReporteDeficitRepository repo) |
| | | 18 | | { |
| | 7 | 19 | | _repo = repo; |
| | 7 | 20 | | } |
| | | 21 | | |
| | | 22 | | // ─── Por Persona ───────────────────────────────────────────────────────── |
| | | 23 | | |
| | | 24 | | public async Task<ReporteDeficitPorPersonaDto> ObtenerDeficitPorPersonaAsync( |
| | | 25 | | long periodoId, CancellationToken ct = default) |
| | | 26 | | { |
| | 4 | 27 | | var filas = (await _repo.ObtenerPlanillaAjusteConRelacionAsync(periodoId, ct)).ToList(); |
| | | 28 | | |
| | 4 | 29 | | var situaciones = filas |
| | 4 | 30 | | .GroupBy(f => f.SituacionNombre) |
| | 2 | 31 | | .OrderBy(g => g.Key) |
| | 2 | 32 | | .Select(gSit => BuildGrupoSituacion(gSit.Key, gSit)) |
| | 4 | 33 | | .ToList(); |
| | | 34 | | |
| | 4 | 35 | | return new ReporteDeficitPorPersonaDto |
| | 4 | 36 | | { |
| | 4 | 37 | | Situaciones = situaciones, |
| | 2 | 38 | | TotalSolicitado = situaciones.Sum(s => s.TotalSolicitado), |
| | 2 | 39 | | TotalAplicado = situaciones.Sum(s => s.TotalAplicado), |
| | 2 | 40 | | TotalDeficit = situaciones.Sum(s => s.TotalDeficit) |
| | 4 | 41 | | }; |
| | 4 | 42 | | } |
| | | 43 | | |
| | | 44 | | private static DeficitGrupoSituacionDto BuildGrupoSituacion( |
| | | 45 | | string situacion, IEnumerable<PlanillaAjusteConRelacionRow> filas) |
| | | 46 | | { |
| | 2 | 47 | | var unidades = filas |
| | 4 | 48 | | .GroupBy(f => f.UnidadNombre) |
| | 2 | 49 | | .OrderBy(g => g.Key) |
| | 2 | 50 | | .Select(gUni => BuildGrupoUnidad(gUni.Key, gUni)) |
| | 2 | 51 | | .ToList(); |
| | | 52 | | |
| | 2 | 53 | | return new DeficitGrupoSituacionDto |
| | 2 | 54 | | { |
| | 2 | 55 | | Situacion = situacion, |
| | 2 | 56 | | Unidades = unidades, |
| | 2 | 57 | | TotalSolicitado = unidades.Sum(u => u.TotalSolicitado), |
| | 2 | 58 | | TotalAplicado = unidades.Sum(u => u.TotalAplicado), |
| | 2 | 59 | | TotalDeficit = unidades.Sum(u => u.TotalDeficit) |
| | 2 | 60 | | }; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private static DeficitGrupoUnidadDto BuildGrupoUnidad( |
| | | 64 | | string unidad, IEnumerable<PlanillaAjusteConRelacionRow> filas) |
| | | 65 | | { |
| | 2 | 66 | | var companias = filas |
| | 4 | 67 | | .GroupBy(f => f.CompaniaNombre) |
| | 2 | 68 | | .OrderBy(g => g.Key) |
| | 2 | 69 | | .Select(gCia => BuildGrupoCompania(gCia.Key, gCia)) |
| | 2 | 70 | | .ToList(); |
| | | 71 | | |
| | 2 | 72 | | return new DeficitGrupoUnidadDto |
| | 2 | 73 | | { |
| | 2 | 74 | | Unidad = unidad, |
| | 2 | 75 | | Companias = companias, |
| | 2 | 76 | | TotalSolicitado = companias.Sum(c => c.TotalSolicitado), |
| | 2 | 77 | | TotalAplicado = companias.Sum(c => c.TotalAplicado), |
| | 2 | 78 | | TotalDeficit = companias.Sum(c => c.TotalDeficit) |
| | 2 | 79 | | }; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private static DeficitGrupoCompaniaDto BuildGrupoCompania( |
| | | 83 | | string compania, IEnumerable<PlanillaAjusteConRelacionRow> filas) |
| | | 84 | | { |
| | 2 | 85 | | var personas = filas |
| | 4 | 86 | | .GroupBy(f => new { f.PersonaId, f.Cedula, f.PersonaNombre }) |
| | 3 | 87 | | .OrderBy(g => g.Key.PersonaNombre) |
| | 3 | 88 | | .Select(gPer => BuildPersona(gPer.Key.PersonaId, gPer.Key.Cedula, gPer.Key.PersonaNombre, gPer)) |
| | 2 | 89 | | .ToList(); |
| | | 90 | | |
| | 2 | 91 | | return new DeficitGrupoCompaniaDto |
| | 2 | 92 | | { |
| | 2 | 93 | | Compania = compania, |
| | 2 | 94 | | Personas = personas, |
| | 3 | 95 | | TotalSolicitado = personas.Sum(p => p.TotalSolicitado), |
| | 3 | 96 | | TotalAplicado = personas.Sum(p => p.TotalAplicado), |
| | 3 | 97 | | TotalDeficit = personas.Sum(p => p.TotalDeficit) |
| | 2 | 98 | | }; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private static DeficitPersonaDto BuildPersona( |
| | | 102 | | long personaId, string cedula, string nombre, IEnumerable<PlanillaAjusteConRelacionRow> filas) |
| | | 103 | | { |
| | 3 | 104 | | var acreedores = filas |
| | 4 | 105 | | .OrderBy(f => f.AcreedorNombre) |
| | 4 | 106 | | .Select(f => new DeficitAcreedorFilaDto |
| | 4 | 107 | | { |
| | 4 | 108 | | AcreedorId = f.AcreedorId, |
| | 4 | 109 | | AcreedorCodigo = f.AcreedorCodigo, |
| | 4 | 110 | | AcreedorNombre = f.AcreedorNombre, |
| | 4 | 111 | | MontoSolicitado = f.MontoSolicitado, |
| | 4 | 112 | | MontoAplicado = f.MontoAplicado, |
| | 4 | 113 | | MontoDeficit = f.MontoNoAplicado |
| | 4 | 114 | | }) |
| | 3 | 115 | | .ToList(); |
| | | 116 | | |
| | 3 | 117 | | return new DeficitPersonaDto |
| | 3 | 118 | | { |
| | 3 | 119 | | PersonaId = personaId, |
| | 3 | 120 | | Cedula = cedula, |
| | 3 | 121 | | Nombre = nombre, |
| | 3 | 122 | | Acreedores = acreedores, |
| | 4 | 123 | | TotalSolicitado = acreedores.Sum(a => a.MontoSolicitado), |
| | 4 | 124 | | TotalAplicado = acreedores.Sum(a => a.MontoAplicado), |
| | 4 | 125 | | TotalDeficit = acreedores.Sum(a => a.MontoDeficit) |
| | 3 | 126 | | }; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public async Task<byte[]> GenerarCsvDeficitPorPersonaAsync( |
| | | 130 | | long periodoId, CancellationToken ct = default) |
| | | 131 | | { |
| | 2 | 132 | | var dto = await ObtenerDeficitPorPersonaAsync(periodoId, ct); |
| | 2 | 133 | | var sb = new StringBuilder(); |
| | 2 | 134 | | sb.AppendLine("Situación;Unidad;Compañía;Cédula;Nombre;Acreedor;Monto Solicitado;Monto Aplicado;Déficit"); |
| | | 135 | | |
| | 6 | 136 | | foreach (var gSit in dto.Situaciones) |
| | | 137 | | { |
| | 4 | 138 | | foreach (var gUni in gSit.Unidades) |
| | | 139 | | { |
| | 4 | 140 | | foreach (var gCia in gUni.Companias) |
| | | 141 | | { |
| | 4 | 142 | | foreach (var persona in gCia.Personas) |
| | | 143 | | { |
| | 4 | 144 | | foreach (var acreedor in persona.Acreedores) |
| | | 145 | | { |
| | 1 | 146 | | sb.AppendLine(string.Join(";", |
| | 1 | 147 | | gSit.Situacion, |
| | 1 | 148 | | gUni.Unidad, |
| | 1 | 149 | | gCia.Compania, |
| | 1 | 150 | | persona.Cedula, |
| | 1 | 151 | | persona.Nombre, |
| | 1 | 152 | | acreedor.AcreedorNombre, |
| | 1 | 153 | | FormatMonto(acreedor.MontoSolicitado), |
| | 1 | 154 | | FormatMonto(acreedor.MontoAplicado), |
| | 1 | 155 | | FormatMonto(acreedor.MontoDeficit))); |
| | | 156 | | } |
| | | 157 | | |
| | 1 | 158 | | sb.AppendLine(string.Join(";", |
| | 1 | 159 | | gSit.Situacion, |
| | 1 | 160 | | gUni.Unidad, |
| | 1 | 161 | | gCia.Compania, |
| | 1 | 162 | | persona.Cedula, |
| | 1 | 163 | | persona.Nombre, |
| | 1 | 164 | | "TOTAL PERSONA", |
| | 1 | 165 | | FormatMonto(persona.TotalSolicitado), |
| | 1 | 166 | | FormatMonto(persona.TotalAplicado), |
| | 1 | 167 | | FormatMonto(persona.TotalDeficit))); |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | 2 | 173 | | sb.AppendLine(string.Join(";", |
| | 2 | 174 | | "", "", "", "", "", |
| | 2 | 175 | | "TOTAL GENERAL", |
| | 2 | 176 | | FormatMonto(dto.TotalSolicitado), |
| | 2 | 177 | | FormatMonto(dto.TotalAplicado), |
| | 2 | 178 | | FormatMonto(dto.TotalDeficit))); |
| | | 179 | | |
| | 2 | 180 | | return Encoding.UTF8.GetBytes(sb.ToString()); |
| | 2 | 181 | | } |
| | | 182 | | |
| | | 183 | | // ─── Por Acreedor ───────────────────────────────────────────────────────── |
| | | 184 | | |
| | | 185 | | public async Task<ReporteDeficitPorAcreedorDto> ObtenerDeficitPorAcreedorAsync( |
| | | 186 | | long periodoId, CancellationToken ct = default) |
| | | 187 | | { |
| | 3 | 188 | | var filas = (await _repo.ObtenerPlanillaAjusteConRelacionAsync(periodoId, ct)).ToList(); |
| | | 189 | | |
| | 3 | 190 | | var acreedores = filas |
| | 2 | 191 | | .GroupBy(f => new { f.AcreedorId, f.AcreedorCodigo, f.AcreedorNombre }) |
| | 2 | 192 | | .OrderBy(g => g.Key.AcreedorNombre) |
| | 3 | 193 | | .Select(gAcr => |
| | 3 | 194 | | { |
| | 2 | 195 | | var personas = gAcr |
| | 2 | 196 | | .OrderBy(f => f.PersonaNombre) |
| | 2 | 197 | | .Select(f => new DeficitPersonaAcreedorFilaDto |
| | 2 | 198 | | { |
| | 2 | 199 | | Cedula = f.Cedula, |
| | 2 | 200 | | Nombre = f.PersonaNombre, |
| | 2 | 201 | | Estado = f.Estado, |
| | 2 | 202 | | Solicitado = f.MontoSolicitado, |
| | 2 | 203 | | Descontado = f.MontoAplicado, |
| | 2 | 204 | | Deficit = f.MontoNoAplicado, |
| | 2 | 205 | | Diferencia = f.MontoSolicitado - f.MontoAplicado, |
| | 2 | 206 | | Observaciones = f.Observaciones |
| | 2 | 207 | | }) |
| | 2 | 208 | | .ToList(); |
| | 3 | 209 | | |
| | 2 | 210 | | return new DeficitGrupoAcreedorDto |
| | 2 | 211 | | { |
| | 2 | 212 | | AcreedorId = gAcr.Key.AcreedorId, |
| | 2 | 213 | | AcreedorCodigo = gAcr.Key.AcreedorCodigo, |
| | 2 | 214 | | AcreedorNombre = gAcr.Key.AcreedorNombre, |
| | 2 | 215 | | Personas = personas, |
| | 2 | 216 | | TotalSolicitado = personas.Sum(p => p.Solicitado), |
| | 2 | 217 | | TotalDescontado = personas.Sum(p => p.Descontado), |
| | 2 | 218 | | TotalDeficit = personas.Sum(p => p.Deficit), |
| | 2 | 219 | | TotalDiferencia = personas.Sum(p => p.Diferencia) |
| | 2 | 220 | | }; |
| | 3 | 221 | | }) |
| | 3 | 222 | | .ToList(); |
| | | 223 | | |
| | 3 | 224 | | return new ReporteDeficitPorAcreedorDto |
| | 3 | 225 | | { |
| | 3 | 226 | | Acreedores = acreedores, |
| | 2 | 227 | | TotalSolicitado = acreedores.Sum(a => a.TotalSolicitado), |
| | 2 | 228 | | TotalDescontado = acreedores.Sum(a => a.TotalDescontado), |
| | 2 | 229 | | TotalDeficit = acreedores.Sum(a => a.TotalDeficit), |
| | 2 | 230 | | TotalDiferencia = acreedores.Sum(a => a.TotalDiferencia) |
| | 3 | 231 | | }; |
| | 3 | 232 | | } |
| | | 233 | | |
| | | 234 | | public async Task<byte[]> GenerarCsvDeficitPorAcreedorAsync( |
| | | 235 | | long periodoId, CancellationToken ct = default) |
| | | 236 | | { |
| | 1 | 237 | | var dto = await ObtenerDeficitPorAcreedorAsync(periodoId, ct); |
| | 1 | 238 | | var sb = new StringBuilder(); |
| | | 239 | | // Columnas en orden legacy: Solicit. → Déficit → Diferencia → Descont. → Obs. (SUM45037) |
| | 1 | 240 | | sb.AppendLine("Acreedor;Código Acreedor;Cédula;Nombre;Estado;Solicitado;Déficit;Diferencia;Descontado;Observacio |
| | | 241 | | |
| | 2 | 242 | | foreach (var gAcr in dto.Acreedores) |
| | | 243 | | { |
| | 0 | 244 | | foreach (var persona in gAcr.Personas) |
| | | 245 | | { |
| | 0 | 246 | | sb.AppendLine(string.Join(";", |
| | 0 | 247 | | gAcr.AcreedorNombre, |
| | 0 | 248 | | gAcr.AcreedorCodigo, |
| | 0 | 249 | | persona.Cedula, |
| | 0 | 250 | | persona.Nombre, |
| | 0 | 251 | | persona.Estado, |
| | 0 | 252 | | FormatMonto(persona.Solicitado), |
| | 0 | 253 | | FormatMonto(persona.Deficit), |
| | 0 | 254 | | FormatMonto(persona.Diferencia), |
| | 0 | 255 | | FormatMonto(persona.Descontado), |
| | 0 | 256 | | persona.Observaciones ?? "")); |
| | | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | sb.AppendLine(string.Join(";", |
| | 0 | 260 | | gAcr.AcreedorNombre, |
| | 0 | 261 | | gAcr.AcreedorCodigo, |
| | 0 | 262 | | "", "SUBTOTAL", "", |
| | 0 | 263 | | FormatMonto(gAcr.TotalSolicitado), |
| | 0 | 264 | | FormatMonto(gAcr.TotalDeficit), |
| | 0 | 265 | | FormatMonto(gAcr.TotalDiferencia), |
| | 0 | 266 | | FormatMonto(gAcr.TotalDescontado), |
| | 0 | 267 | | "")); |
| | | 268 | | } |
| | | 269 | | |
| | 1 | 270 | | sb.AppendLine(string.Join(";", |
| | 1 | 271 | | "", "", "", "TOTAL GENERAL", "", |
| | 1 | 272 | | FormatMonto(dto.TotalSolicitado), |
| | 1 | 273 | | FormatMonto(dto.TotalDeficit), |
| | 1 | 274 | | FormatMonto(dto.TotalDiferencia), |
| | 1 | 275 | | FormatMonto(dto.TotalDescontado), |
| | 1 | 276 | | "")); |
| | | 277 | | |
| | 1 | 278 | | return Encoding.UTF8.GetBytes(sb.ToString()); |
| | 1 | 279 | | } |
| | | 280 | | |
| | | 281 | | private static string FormatMonto(decimal monto) => |
| | 16 | 282 | | monto.ToString("F2", CultureInfo.InvariantCulture); |
| | | 283 | | } |