| | | 1 | | using System.Globalization; |
| | | 2 | | using System.IO.Compression; |
| | | 3 | | using System.Reflection; |
| | | 4 | | using FAU.DataAccess; |
| | | 5 | | using FAU.Entidades; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using QuestPDF.Fluent; |
| | | 8 | | using QuestPDF.Helpers; |
| | | 9 | | using QuestPDF.Infrastructure; |
| | | 10 | | |
| | | 11 | | namespace FAU.Logica.Services; |
| | | 12 | | |
| | | 13 | | public class AguinaldoReciboService : IAguinaldoReciboService |
| | | 14 | | { |
| | | 15 | | private readonly ApplicationDbContext _context; |
| | | 16 | | |
| | 0 | 17 | | public AguinaldoReciboService(ApplicationDbContext context) |
| | | 18 | | { |
| | 0 | 19 | | _context = context; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | // ── SEMESTRAL ──────────────────────────────────────────────────────────── |
| | | 23 | | |
| | | 24 | | public async Task<(byte[] Pdf, string FileName)> GenerarPdfSemestralAsync(long personaId, int anio, short semestre) |
| | | 25 | | { |
| | 0 | 26 | | var (aguinaldo, relacion) = await CargarDatosSemestralAsync(personaId, anio, semestre); |
| | 0 | 27 | | var (fechaIngreso, fechaUltAsc) = await ObtenerFechasRelacionAsync(personaId, relacion.FechaInicio); |
| | 0 | 28 | | var mes = semestre == 1 ? 6 : 12; |
| | 0 | 29 | | var fileName = $"aguinaldo_{anio}_{mes:D2}_{relacion.Persona.Cedula}.pdf"; |
| | 0 | 30 | | return (GenerarPdfSemestral(aguinaldo, relacion, fechaIngreso, fechaUltAsc), fileName); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<(Stream Zip, string FileName)> GenerarLoteSemestralZipAsync(int anio, short semestre, IEnumerable< |
| | | 34 | | { |
| | 0 | 35 | | var query = _context.Aguinaldos |
| | 0 | 36 | | .Where(a => a.Anio == anio && a.Semestre == semestre); |
| | | 37 | | |
| | 0 | 38 | | if (personaIds != null) |
| | | 39 | | { |
| | 0 | 40 | | var ids = personaIds.ToList(); |
| | 0 | 41 | | query = query.Where(a => ids.Contains(a.PersonaId)); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | var aguinaldos = await query.ToListAsync(); |
| | | 45 | | var personaIdsUsados = aguinaldos.Select(a => a.PersonaId).ToList(); |
| | 0 | 46 | | var relacionesPorPersona = await CargarRelacionesPorPersonaAsync(personaIdsUsados); |
| | 0 | 47 | | var fechasPorPersona = await ObtenerFechasRelacionesBatchAsync(personaIdsUsados, relacionesPorPersona); |
| | | 48 | | |
| | 0 | 49 | | var mes = semestre == 1 ? 6 : 12; |
| | 0 | 50 | | var memoryStream = new MemoryStream(); |
| | 0 | 51 | | using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: true)) |
| | | 52 | | { |
| | 0 | 53 | | foreach (var aguinaldo in aguinaldos) |
| | | 54 | | { |
| | 0 | 55 | | if (!relacionesPorPersona.TryGetValue(aguinaldo.PersonaId, out var relacion)) |
| | | 56 | | continue; |
| | | 57 | | |
| | 0 | 58 | | fechasPorPersona.TryGetValue(aguinaldo.PersonaId, out var fechas); |
| | 0 | 59 | | var pdfBytes = GenerarPdfSemestral(aguinaldo, relacion, fechas.FechaIngreso, fechas.FechaUltAscenso); |
| | 0 | 60 | | var entryName = $"aguinaldo_{anio}_{mes:D2}_{relacion.Persona.Cedula}.pdf"; |
| | 0 | 61 | | var entry = zip.CreateEntry(entryName, CompressionLevel.Fastest); |
| | 0 | 62 | | using var entryStream = entry.Open(); |
| | 0 | 63 | | await entryStream.WriteAsync(pdfBytes); |
| | 0 | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | memoryStream.Position = 0; |
| | 0 | 68 | | return (memoryStream, $"aguinaldos_{anio}_{mes:D2}.zip"); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | // ── BAJA ───────────────────────────────────────────────────────────────── |
| | | 72 | | |
| | | 73 | | public async Task<(byte[] Pdf, string FileName)> GenerarPdfBajaAsync(long aguinaldoBajaId) |
| | | 74 | | { |
| | 0 | 75 | | var baja = await CargarAguinaldoBajaAsync(aguinaldoBajaId); |
| | 0 | 76 | | var relacion = await CargarRelacionParaBajaAsync(baja.PersonaId); |
| | 0 | 77 | | var (fechaIngreso, fechaUltAsc) = relacion != null |
| | 0 | 78 | | ? await ObtenerFechasRelacionAsync(baja.PersonaId, relacion.FechaInicio) |
| | 0 | 79 | | : ((DateTime?)null, (DateTime?)null); |
| | 0 | 80 | | var fileName = $"aguinaldo_{baja.Anio}_{baja.Mes:D2}_{baja.Cedula}.pdf"; |
| | 0 | 81 | | return (GenerarPdfBaja(baja, relacion, fechaIngreso, fechaUltAsc), fileName); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | public async Task<(Stream Zip, string FileName)> GenerarLoteBajaZipAsync(IEnumerable<long> aguinaldoBajaIds) |
| | | 85 | | { |
| | 0 | 86 | | var ids = aguinaldoBajaIds.ToList(); |
| | 0 | 87 | | var bajas = await _context.AguinaldosBaja |
| | 0 | 88 | | .Include(b => b.Items) |
| | 0 | 89 | | .Where(b => ids.Contains(b.Id)) |
| | 0 | 90 | | .ToListAsync(); |
| | | 91 | | |
| | | 92 | | var personaIdsUsados = bajas.Select(b => b.PersonaId).ToList(); |
| | 0 | 93 | | var relacionesPorPersona = await CargarRelacionesParaBajaAsync(personaIdsUsados); |
| | 0 | 94 | | var fechasPorPersona = await ObtenerFechasRelacionesBatchAsync(personaIdsUsados, relacionesPorPersona); |
| | | 95 | | |
| | 0 | 96 | | var memoryStream = new MemoryStream(); |
| | 0 | 97 | | using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: true)) |
| | | 98 | | { |
| | 0 | 99 | | foreach (var baja in bajas) |
| | | 100 | | { |
| | 0 | 101 | | relacionesPorPersona.TryGetValue(baja.PersonaId, out var relacion); |
| | 0 | 102 | | fechasPorPersona.TryGetValue(baja.PersonaId, out var fechas); |
| | 0 | 103 | | var pdfBytes = GenerarPdfBaja(baja, relacion, fechas.FechaIngreso, fechas.FechaUltAscenso); |
| | 0 | 104 | | var entryName = $"aguinaldo_{baja.Anio}_{baja.Mes:D2}_{baja.Cedula}.pdf"; |
| | 0 | 105 | | var entry = zip.CreateEntry(entryName, CompressionLevel.Fastest); |
| | 0 | 106 | | using var entryStream = entry.Open(); |
| | 0 | 107 | | await entryStream.WriteAsync(pdfBytes); |
| | 0 | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | memoryStream.Position = 0; |
| | 0 | 112 | | var zipName = bajas.Count > 0 |
| | 0 | 113 | | ? $"aguinaldos_{bajas[0].Anio}_{bajas[0].Mes:D2}.zip" |
| | 0 | 114 | | : "aguinaldos_baja.zip"; |
| | 0 | 115 | | return (memoryStream, zipName); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | // ── DATA LOADING ────────────────────────────────────────────────────────── |
| | | 119 | | |
| | | 120 | | private async Task<(Aguinaldo aguinaldo, RelacionLaboral relacion)> CargarDatosSemestralAsync( |
| | | 121 | | long personaId, int anio, short semestre) |
| | | 122 | | { |
| | 0 | 123 | | var aguinaldo = await _context.Aguinaldos |
| | 0 | 124 | | .FirstOrDefaultAsync(a => a.PersonaId == personaId && a.Anio == anio && a.Semestre == semestre) |
| | 0 | 125 | | ?? throw new KeyNotFoundException( |
| | 0 | 126 | | $"No se encontró aguinaldo para persona {personaId}, año {anio}, semestre {semestre}."); |
| | | 127 | | |
| | 0 | 128 | | var relacion = await CargarRelacionActivaPorPersonaIdAsync(personaId); |
| | 0 | 129 | | return (aguinaldo, relacion); |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | private async Task<RelacionLaboral> CargarRelacionActivaPorPersonaIdAsync(long personaId) |
| | | 133 | | { |
| | 0 | 134 | | return await _context.RelacionesLaborales |
| | 0 | 135 | | .Include(rl => rl.Persona) |
| | 0 | 136 | | .Include(rl => rl.Grado) |
| | 0 | 137 | | .Include(rl => rl.Escalafon) |
| | 0 | 138 | | .Include(rl => rl.Unidad) |
| | 0 | 139 | | .Where(rl => rl.PersonaId == personaId && rl.Estado == EstadoRelacion.Activo) |
| | 0 | 140 | | .FirstOrDefaultAsync() |
| | 0 | 141 | | ?? throw new KeyNotFoundException($"No se encontró relación laboral activa para persona {personaId}."); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | private async Task<Dictionary<long, RelacionLaboral>> CargarRelacionesPorPersonaAsync(List<long> personaIds) |
| | | 145 | | { |
| | 0 | 146 | | var relaciones = await _context.RelacionesLaborales |
| | 0 | 147 | | .Include(rl => rl.Persona) |
| | 0 | 148 | | .Include(rl => rl.Grado) |
| | 0 | 149 | | .Include(rl => rl.Escalafon) |
| | 0 | 150 | | .Include(rl => rl.Unidad) |
| | 0 | 151 | | .Where(rl => personaIds.Contains(rl.PersonaId) && rl.Estado == EstadoRelacion.Activo) |
| | 0 | 152 | | .ToListAsync(); |
| | | 153 | | |
| | | 154 | | return relaciones.ToDictionary(rl => rl.PersonaId); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | // Para bajas: activa si existe, sino la más reciente inactiva (mayor FechaFin) |
| | | 158 | | private async Task<RelacionLaboral?> CargarRelacionParaBajaAsync(long personaId) |
| | | 159 | | { |
| | 0 | 160 | | var activa = await _context.RelacionesLaborales |
| | 0 | 161 | | .Include(rl => rl.Persona) |
| | 0 | 162 | | .Include(rl => rl.Grado) |
| | 0 | 163 | | .Include(rl => rl.Escalafon) |
| | 0 | 164 | | .Include(rl => rl.Unidad) |
| | 0 | 165 | | .Where(rl => rl.PersonaId == personaId && rl.Estado == EstadoRelacion.Activo) |
| | 0 | 166 | | .FirstOrDefaultAsync(); |
| | | 167 | | |
| | 0 | 168 | | if (activa != null) return activa; |
| | | 169 | | |
| | 0 | 170 | | return await _context.RelacionesLaborales |
| | 0 | 171 | | .Include(rl => rl.Persona) |
| | 0 | 172 | | .Include(rl => rl.Grado) |
| | 0 | 173 | | .Include(rl => rl.Escalafon) |
| | 0 | 174 | | .Include(rl => rl.Unidad) |
| | 0 | 175 | | .Where(rl => rl.PersonaId == personaId) |
| | 0 | 176 | | .OrderByDescending(rl => rl.FechaFin) |
| | 0 | 177 | | .FirstOrDefaultAsync(); |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | // Batch para bajas: activa si existe, sino la más reciente inactiva por persona |
| | | 181 | | private async Task<Dictionary<long, RelacionLaboral>> CargarRelacionesParaBajaAsync(List<long> personaIds) |
| | | 182 | | { |
| | 0 | 183 | | if (personaIds.Count == 0) |
| | 0 | 184 | | return new Dictionary<long, RelacionLaboral>(); |
| | | 185 | | |
| | 0 | 186 | | var activas = await _context.RelacionesLaborales |
| | 0 | 187 | | .Include(rl => rl.Persona) |
| | 0 | 188 | | .Include(rl => rl.Grado) |
| | 0 | 189 | | .Include(rl => rl.Escalafon) |
| | 0 | 190 | | .Include(rl => rl.Unidad) |
| | 0 | 191 | | .Where(rl => personaIds.Contains(rl.PersonaId) && rl.Estado == EstadoRelacion.Activo) |
| | 0 | 192 | | .ToListAsync(); |
| | | 193 | | |
| | | 194 | | var result = activas.ToDictionary(rl => rl.PersonaId); |
| | | 195 | | |
| | 0 | 196 | | var sinActiva = personaIds.Except(result.Keys).ToList(); |
| | 0 | 197 | | if (sinActiva.Count > 0) |
| | | 198 | | { |
| | 0 | 199 | | var inactivas = await _context.RelacionesLaborales |
| | 0 | 200 | | .Include(rl => rl.Persona) |
| | 0 | 201 | | .Include(rl => rl.Grado) |
| | 0 | 202 | | .Include(rl => rl.Escalafon) |
| | 0 | 203 | | .Include(rl => rl.Unidad) |
| | 0 | 204 | | .Where(rl => sinActiva.Contains(rl.PersonaId)) |
| | 0 | 205 | | .ToListAsync(); |
| | | 206 | | |
| | | 207 | | foreach (var rl in inactivas.GroupBy(r => r.PersonaId) |
| | | 208 | | .Select(g => g.OrderByDescending(r => r.FechaFin).First())) |
| | 0 | 209 | | result[rl.PersonaId] = rl; |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | return result; |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | private async Task<AguinaldoBaja> CargarAguinaldoBajaAsync(long aguinaldoBajaId) |
| | | 216 | | { |
| | 0 | 217 | | return await _context.AguinaldosBaja |
| | 0 | 218 | | .Include(b => b.Items) |
| | 0 | 219 | | .FirstOrDefaultAsync(b => b.Id == aguinaldoBajaId) |
| | 0 | 220 | | ?? throw new KeyNotFoundException($"No se encontró aguinaldo por baja con id {aguinaldoBajaId}."); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | // Devuelve (fechaIngreso=primera relación, fechaUltAscenso=activa si es posterior) |
| | | 224 | | private async Task<(DateTime? FechaIngreso, DateTime? FechaUltAscenso)> ObtenerFechasRelacionAsync( |
| | | 225 | | long personaId, DateTime fechaInicioActivo) |
| | | 226 | | { |
| | 0 | 227 | | var minFecha = await _context.RelacionesLaborales |
| | 0 | 228 | | .Where(rl => rl.PersonaId == personaId) |
| | 0 | 229 | | .MinAsync(rl => rl.FechaInicio); |
| | | 230 | | |
| | 0 | 231 | | return (minFecha, fechaInicioActivo > minFecha ? fechaInicioActivo : null); |
| | 0 | 232 | | } |
| | | 233 | | |
| | | 234 | | private async Task<Dictionary<long, (DateTime? FechaIngreso, DateTime? FechaUltAscenso)>> ObtenerFechasRelacionesBat |
| | | 235 | | List<long> personaIds, |
| | | 236 | | Dictionary<long, RelacionLaboral> relacionesActivas) |
| | | 237 | | { |
| | 0 | 238 | | if (personaIds.Count == 0) |
| | 0 | 239 | | return new Dictionary<long, (DateTime?, DateTime?)>(); |
| | | 240 | | |
| | 0 | 241 | | var todasFechas = await _context.RelacionesLaborales |
| | 0 | 242 | | .Where(rl => personaIds.Contains(rl.PersonaId)) |
| | 0 | 243 | | .Select(rl => new { rl.PersonaId, rl.FechaInicio }) |
| | 0 | 244 | | .ToListAsync(); |
| | | 245 | | |
| | 0 | 246 | | var minPorPersona = todasFechas |
| | | 247 | | .GroupBy(r => r.PersonaId) |
| | | 248 | | .ToDictionary(g => g.Key, g => g.Min(r => r.FechaInicio)); |
| | | 249 | | |
| | 0 | 250 | | return personaIds.ToDictionary( |
| | | 251 | | id => id, |
| | 0 | 252 | | id => |
| | 0 | 253 | | { |
| | 0 | 254 | | var activo = relacionesActivas.GetValueOrDefault(id); |
| | 0 | 255 | | if (activo == null) |
| | 0 | 256 | | return ((DateTime?)null, (DateTime?)null); |
| | 0 | 257 | | |
| | 0 | 258 | | var minFecha = minPorPersona.GetValueOrDefault(id, activo.FechaInicio); |
| | 0 | 259 | | DateTime? ultAsc = activo.FechaInicio > minFecha ? activo.FechaInicio : null; |
| | 0 | 260 | | return ((DateTime?)minFecha, ultAsc); |
| | 0 | 261 | | }); |
| | 0 | 262 | | } |
| | | 263 | | |
| | | 264 | | // ── PDF BUILDERS ────────────────────────────────────────────────────────── |
| | | 265 | | |
| | | 266 | | private static byte[] GenerarPdfSemestral( |
| | | 267 | | Aguinaldo ag, RelacionLaboral relacion, |
| | | 268 | | DateTime? fechaIngreso, DateTime? fechaUltAscenso) |
| | | 269 | | { |
| | 0 | 270 | | var ci = CultureInfo.GetCultureInfo("es-UY"); |
| | 0 | 271 | | var mes = ag.Semestre == 1 ? 6 : 12; |
| | 0 | 272 | | var nombreMes = ci.TextInfo.ToTitleCase(ci.DateTimeFormat.GetMonthName(mes)); |
| | 0 | 273 | | var tituloPeriodo = $"MES DE : {nombreMes} {ag.Anio}"; |
| | | 274 | | |
| | 0 | 275 | | var nominal = ag.Nominal ?? 0m; |
| | 0 | 276 | | var montepio = ag.Montepio ?? 0m; |
| | 0 | 277 | | var sanidad = ag.Sanidad ?? 0m; |
| | 0 | 278 | | var totalDescuentos = ag.TotalDescuentos ?? 0m; |
| | 0 | 279 | | var liquido = ag.Liquido ?? 0m; |
| | 0 | 280 | | var redondeo = ag.Redondeo; |
| | | 281 | | |
| | 0 | 282 | | var haberes = new List<(string Desc, decimal Monto)> |
| | 0 | 283 | | { |
| | 0 | 284 | | ("AGUINALDO MILITAR", nominal) |
| | 0 | 285 | | }; |
| | | 286 | | |
| | 0 | 287 | | var descuentos = new List<(string Desc, decimal Monto)> |
| | 0 | 288 | | { |
| | 0 | 289 | | ("MONTEPÍO", montepio), |
| | 0 | 290 | | ("D.N.S.FF.AA.", sanidad) |
| | 0 | 291 | | }; |
| | | 292 | | |
| | 0 | 293 | | return GenerarPdfAguinaldo( |
| | 0 | 294 | | tituloPeriodo, relacion, |
| | 0 | 295 | | haberes, descuentos, |
| | 0 | 296 | | totalHaberes: nominal, |
| | 0 | 297 | | totalDescuentos: totalDescuentos, |
| | 0 | 298 | | redondeo: redondeo, |
| | 0 | 299 | | aCobrar: liquido, |
| | 0 | 300 | | fechaIngreso: fechaIngreso, |
| | 0 | 301 | | fechaUltAscenso: fechaUltAscenso); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private static byte[] GenerarPdfBaja( |
| | | 305 | | AguinaldoBaja baja, RelacionLaboral? relacion, |
| | | 306 | | DateTime? fechaIngreso, DateTime? fechaUltAscenso) |
| | | 307 | | { |
| | 0 | 308 | | var ci = CultureInfo.GetCultureInfo("es-UY"); |
| | 0 | 309 | | var nombreMes = ci.TextInfo.ToTitleCase(ci.DateTimeFormat.GetMonthName(baja.Mes)); |
| | 0 | 310 | | var tituloPeriodo = $"MES DE : {nombreMes} {baja.Anio}"; |
| | | 311 | | |
| | 0 | 312 | | var haberes = baja.Items |
| | 0 | 313 | | .Where(i => i.Tipo == "haber") |
| | 0 | 314 | | .Select(i => (i.NombreConcepto, Math.Abs(i.Importe))) |
| | 0 | 315 | | .ToList(); |
| | | 316 | | |
| | 0 | 317 | | var descuentos = baja.Items |
| | 0 | 318 | | .Where(i => i.Tipo == "descuento") |
| | 0 | 319 | | .Select(i => (i.NombreConcepto, Math.Abs(i.Importe))) |
| | 0 | 320 | | .ToList(); |
| | | 321 | | |
| | 0 | 322 | | if (!haberes.Any()) |
| | 0 | 323 | | haberes = new List<(string, decimal)> { ("AGUINALDO MILITAR", baja.Nominal) }; |
| | 0 | 324 | | if (!descuentos.Any()) |
| | 0 | 325 | | descuentos = new List<(string, decimal)> |
| | 0 | 326 | | { |
| | 0 | 327 | | ("MONTEPÍO", baja.Montepio), |
| | 0 | 328 | | ("D.N.S.FF.AA.", baja.Sanidad) |
| | 0 | 329 | | }; |
| | | 330 | | |
| | 0 | 331 | | return GenerarPdfAguinaldo( |
| | 0 | 332 | | tituloPeriodo, relacion, |
| | 0 | 333 | | haberes, descuentos, |
| | 0 | 334 | | totalHaberes: baja.Nominal, |
| | 0 | 335 | | totalDescuentos: baja.TotalDescuentos, |
| | 0 | 336 | | redondeo: baja.Redondeo, |
| | 0 | 337 | | aCobrar: baja.Liquido, |
| | 0 | 338 | | fechaIngreso: fechaIngreso, |
| | 0 | 339 | | fechaUltAscenso: fechaUltAscenso, |
| | 0 | 340 | | cedula: baja.Cedula, |
| | 0 | 341 | | nombreCompleto: baja.NombreCompleto); |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | // ── CORE PDF ────────────────────────────────────────────────────────────── |
| | | 345 | | |
| | 0 | 346 | | private static readonly byte[]? _logoBytes = CargarLogo(); |
| | | 347 | | |
| | | 348 | | private static byte[]? CargarLogo() |
| | | 349 | | { |
| | 0 | 350 | | var asm = Assembly.GetExecutingAssembly(); |
| | 0 | 351 | | var nombre = asm.GetManifestResourceNames() |
| | 0 | 352 | | .FirstOrDefault(n => n.EndsWith("fau-logo.png", StringComparison.OrdinalIgnoreCase)); |
| | 0 | 353 | | if (nombre is null) return null; |
| | 0 | 354 | | using var stream = asm.GetManifestResourceStream(nombre)!; |
| | 0 | 355 | | using var ms = new MemoryStream(); |
| | 0 | 356 | | stream.CopyTo(ms); |
| | 0 | 357 | | return ms.ToArray(); |
| | 0 | 358 | | } |
| | | 359 | | |
| | | 360 | | private static byte[] GenerarPdfAguinaldo( |
| | | 361 | | string tituloPeriodo, |
| | | 362 | | RelacionLaboral? relacion, |
| | | 363 | | List<(string Desc, decimal Monto)> haberes, |
| | | 364 | | List<(string Desc, decimal Monto)> descuentos, |
| | | 365 | | decimal totalHaberes, |
| | | 366 | | decimal totalDescuentos, |
| | | 367 | | decimal redondeo, |
| | | 368 | | decimal aCobrar, |
| | | 369 | | DateTime? fechaIngreso, |
| | | 370 | | DateTime? fechaUltAscenso = null, |
| | | 371 | | string? cedula = null, |
| | | 372 | | string? nombreCompleto = null) |
| | | 373 | | { |
| | 0 | 374 | | var cedulaMostrar = cedula ?? relacion?.Persona.Cedula ?? "-"; |
| | 0 | 375 | | var gradoCodigo = relacion?.Grado.Codigo ?? "-"; |
| | 0 | 376 | | var escalafonCodigo = relacion?.Escalafon.Codigo ?? "-"; |
| | 0 | 377 | | var unidadCodigo = relacion?.Unidad.Codigo ?? "-"; |
| | | 378 | | |
| | | 379 | | string apellidoNombre; |
| | 0 | 380 | | if (nombreCompleto != null) |
| | | 381 | | { |
| | 0 | 382 | | apellidoNombre = nombreCompleto.ToUpper(); |
| | | 383 | | } |
| | 0 | 384 | | else if (relacion != null) |
| | | 385 | | { |
| | 0 | 386 | | var p = relacion.Persona; |
| | 0 | 387 | | var apellidos = string.IsNullOrWhiteSpace(p.SegundoApellido) |
| | 0 | 388 | | ? p.PrimerApellido |
| | 0 | 389 | | : $"{p.PrimerApellido} {p.SegundoApellido}"; |
| | 0 | 390 | | var nombres = string.IsNullOrWhiteSpace(p.SegundoNombre) |
| | 0 | 391 | | ? p.PrimerNombre |
| | 0 | 392 | | : $"{p.PrimerNombre} {p.SegundoNombre}"; |
| | 0 | 393 | | apellidoNombre = $"{apellidos.ToUpper()}, {nombres.ToUpper()}"; |
| | | 394 | | } |
| | | 395 | | else |
| | | 396 | | { |
| | 0 | 397 | | apellidoNombre = "-"; |
| | | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | var fechaIngresoStr = fechaIngreso.HasValue |
| | 0 | 401 | | ? fechaIngreso.Value.ToString("dd/MM/yyyy") |
| | 0 | 402 | | : string.Empty; |
| | | 403 | | |
| | 0 | 404 | | var fechaUltAscStr = fechaUltAscenso.HasValue |
| | 0 | 405 | | ? fechaUltAscenso.Value.ToString("dd/MM/yyyy") |
| | 0 | 406 | | : string.Empty; |
| | | 407 | | |
| | 0 | 408 | | return Document.Create(container => |
| | 0 | 409 | | { |
| | 0 | 410 | | container.Page(page => |
| | 0 | 411 | | { |
| | 0 | 412 | | page.Size(PageSizes.A4); |
| | 0 | 413 | | page.MarginHorizontal(20); |
| | 0 | 414 | | page.MarginTop(15); |
| | 0 | 415 | | page.MarginBottom(15); |
| | 0 | 416 | | page.DefaultTextStyle(x => x.FontSize(7.5f).FontFamily("Courier New")); |
| | 0 | 417 | | |
| | 0 | 418 | | page.Content().Column(col => |
| | 0 | 419 | | { |
| | 0 | 420 | | // ── ENCABEZADO ──────────────────────────────────────── |
| | 0 | 421 | | col.Item().PaddingBottom(4).Row(row => |
| | 0 | 422 | | { |
| | 0 | 423 | | row.RelativeItem().Row(r => |
| | 0 | 424 | | { |
| | 0 | 425 | | if (_logoBytes is not null) |
| | 0 | 426 | | r.ConstantItem(90).Image(_logoBytes).FitArea(); |
| | 0 | 427 | | r.RelativeItem().PaddingLeft(_logoBytes is not null ? 6 : 0) |
| | 0 | 428 | | .Column(c => |
| | 0 | 429 | | { |
| | 0 | 430 | | c.Item().Text("FUERZA AÉREA").Bold().FontSize(13).FontFamily("Arial"); |
| | 0 | 431 | | c.Item().Text("URUGUAYA").Bold().FontSize(13).FontFamily("Arial"); |
| | 0 | 432 | | }); |
| | 0 | 433 | | }); |
| | 0 | 434 | | row.ConstantItem(165).Column(c => |
| | 0 | 435 | | { |
| | 0 | 436 | | c.Item().Border(1).Padding(6).Column(inner => |
| | 0 | 437 | | { |
| | 0 | 438 | | inner.Item().Text($" {tituloPeriodo}"); |
| | 0 | 439 | | inner.Item().Text(" "); |
| | 0 | 440 | | }); |
| | 0 | 441 | | }); |
| | 0 | 442 | | }); |
| | 0 | 443 | | |
| | 0 | 444 | | // ── CABECERA FUNCIONARIO ────────────────────────────── |
| | 0 | 445 | | col.Item().Border(1).Row(row => |
| | 0 | 446 | | { |
| | 0 | 447 | | row.ConstantItem(85).BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 448 | | .Text("CEDULA").Bold(); |
| | 0 | 449 | | row.ConstantItem(80).BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 450 | | .AlignCenter().Text("GDO").Bold(); |
| | 0 | 451 | | row.ConstantItem(32).BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 452 | | .AlignCenter().Text("ESC").Bold(); |
| | 0 | 453 | | row.RelativeItem().BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 454 | | .Text("APELLIDO Y NOMBRE").Bold(); |
| | 0 | 455 | | row.ConstantItem(22).BorderRight(1).PaddingHorizontal(4).PaddingVertical(3) |
| | 0 | 456 | | .AlignCenter().Text("UN").Bold(); |
| | 0 | 457 | | row.ConstantItem(138).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 458 | | .AlignCenter().Text("FECHAS").Bold(); |
| | 0 | 459 | | }); |
| | 0 | 460 | | |
| | 0 | 461 | | // ── DATOS FUNCIONARIO ───────────────────────────────── |
| | 0 | 462 | | col.Item().BorderLeft(1).BorderRight(1).BorderBottom(1).Row(row => |
| | 0 | 463 | | { |
| | 0 | 464 | | row.ConstantItem(85).BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 465 | | .Text(cedulaMostrar); |
| | 0 | 466 | | row.ConstantItem(80).BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 467 | | .Text(gradoCodigo); |
| | 0 | 468 | | row.ConstantItem(32).BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 469 | | .Text(escalafonCodigo); |
| | 0 | 470 | | row.RelativeItem().BorderRight(1).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 471 | | .Text(apellidoNombre); |
| | 0 | 472 | | row.ConstantItem(22).BorderRight(1).PaddingHorizontal(4).PaddingVertical(3) |
| | 0 | 473 | | .AlignCenter().Text(unidadCodigo); |
| | 0 | 474 | | row.ConstantItem(138).PaddingHorizontal(5).PaddingVertical(3) |
| | 0 | 475 | | .Column(c => |
| | 0 | 476 | | { |
| | 0 | 477 | | if (!string.IsNullOrEmpty(fechaIngresoStr)) |
| | 0 | 478 | | c.Item().Text(t => |
| | 0 | 479 | | { |
| | 0 | 480 | | t.Span("Fecha Ingreso: ").Bold(); |
| | 0 | 481 | | t.Span(fechaIngresoStr); |
| | 0 | 482 | | }); |
| | 0 | 483 | | else |
| | 0 | 484 | | c.Item().Text(" "); |
| | 0 | 485 | | |
| | 0 | 486 | | if (!string.IsNullOrEmpty(fechaUltAscStr)) |
| | 0 | 487 | | c.Item().Text(t => |
| | 0 | 488 | | { |
| | 0 | 489 | | t.Span("Fecha Ult. Asc.: ").Bold(); |
| | 0 | 490 | | t.Span(fechaUltAscStr); |
| | 0 | 491 | | }); |
| | 0 | 492 | | }); |
| | 0 | 493 | | }); |
| | 0 | 494 | | |
| | 0 | 495 | | // ── CUERPO DE 3 COLUMNAS ────────────────────────────── |
| | 0 | 496 | | col.Item().Extend() |
| | 0 | 497 | | .BorderLeft(1).BorderRight(1).BorderTop(1) |
| | 0 | 498 | | .Row(row => |
| | 0 | 499 | | { |
| | 0 | 500 | | // Columna 1: Haberes |
| | 0 | 501 | | row.RelativeItem(40).BorderRight(1).PaddingTop(5).Column(c => |
| | 0 | 502 | | { |
| | 0 | 503 | | foreach (var item in haberes) |
| | 0 | 504 | | FilaItem(c, item.Desc, item.Monto); |
| | 0 | 505 | | |
| | 0 | 506 | | FilaSeparador(c); |
| | 0 | 507 | | FilaTotalBold(c, "TOTAL DE HABERES", totalHaberes); |
| | 0 | 508 | | }); |
| | 0 | 509 | | |
| | 0 | 510 | | // Columna 2: Descuentos |
| | 0 | 511 | | row.RelativeItem(35).BorderRight(1).PaddingTop(5).Column(c => |
| | 0 | 512 | | { |
| | 0 | 513 | | foreach (var item in descuentos.Where(d => d.Monto != 0)) |
| | 0 | 514 | | FilaItem(c, item.Desc, item.Monto); |
| | 0 | 515 | | |
| | 0 | 516 | | FilaSeparador(c); |
| | 0 | 517 | | FilaTotalBold(c, "TOTAL DESC.", totalDescuentos); |
| | 0 | 518 | | }); |
| | 0 | 519 | | |
| | 0 | 520 | | // Columna 3: Ajuste (redondeo) |
| | 0 | 521 | | row.RelativeItem(25).PaddingTop(5).Column(c => |
| | 0 | 522 | | { |
| | 0 | 523 | | if (redondeo != 0m) |
| | 0 | 524 | | { |
| | 0 | 525 | | c.Item().Row(r => |
| | 0 | 526 | | { |
| | 0 | 527 | | r.RelativeItem().PaddingHorizontal(5).Text("AJUSTE"); |
| | 0 | 528 | | r.ConstantItem(60).AlignRight().PaddingHorizontal(5) |
| | 0 | 529 | | .Text(Monto(redondeo)); |
| | 0 | 530 | | }); |
| | 0 | 531 | | } |
| | 0 | 532 | | }); |
| | 0 | 533 | | }); |
| | 0 | 534 | | }); |
| | 0 | 535 | | |
| | 0 | 536 | | // ── PIE: NOMINAL | DESCUENTOS | A COBRAR ───────────────── |
| | 0 | 537 | | page.Footer().Border(1).Row(row => |
| | 0 | 538 | | { |
| | 0 | 539 | | row.RelativeItem().BorderRight(1).Padding(5).Row(r => |
| | 0 | 540 | | { |
| | 0 | 541 | | r.RelativeItem().Text(t => t.Span("NOMINAL....: ").Bold()); |
| | 0 | 542 | | r.ConstantItem(75).AlignRight().Text(Monto(totalHaberes)); |
| | 0 | 543 | | }); |
| | 0 | 544 | | row.RelativeItem().BorderRight(1).Padding(5).Row(r => |
| | 0 | 545 | | { |
| | 0 | 546 | | r.RelativeItem().Text(t => t.Span("DESCUENTOS.: ").Bold()); |
| | 0 | 547 | | r.ConstantItem(75).AlignRight().Text(Monto(totalDescuentos)); |
| | 0 | 548 | | }); |
| | 0 | 549 | | row.RelativeItem().Padding(5).Row(r => |
| | 0 | 550 | | { |
| | 0 | 551 | | r.RelativeItem().Text(t => t.Span("A COBRAR...: ").Bold()); |
| | 0 | 552 | | r.ConstantItem(75).AlignRight().Text(Monto(aCobrar)); |
| | 0 | 553 | | }); |
| | 0 | 554 | | }); |
| | 0 | 555 | | }); |
| | 0 | 556 | | }).GeneratePdf(); |
| | | 557 | | } |
| | | 558 | | |
| | | 559 | | private static string Monto(decimal valor) => |
| | 0 | 560 | | valor.ToString("N2", CultureInfo.InvariantCulture); |
| | | 561 | | |
| | | 562 | | private static void FilaItem(ColumnDescriptor col, string descripcion, decimal monto) |
| | | 563 | | { |
| | 0 | 564 | | col.Item().Row(r => |
| | 0 | 565 | | { |
| | 0 | 566 | | r.RelativeItem().PaddingHorizontal(5).Text(descripcion); |
| | 0 | 567 | | r.ConstantItem(62).AlignRight().PaddingHorizontal(5).Text(Monto(monto)); |
| | 0 | 568 | | }); |
| | 0 | 569 | | } |
| | | 570 | | |
| | | 571 | | private static void FilaSeparador(ColumnDescriptor col) |
| | | 572 | | { |
| | 0 | 573 | | col.Item().PaddingHorizontal(5).PaddingVertical(2).LineHorizontal(0.5f); |
| | 0 | 574 | | } |
| | | 575 | | |
| | | 576 | | private static void FilaTotalBold(ColumnDescriptor col, string label, decimal total) |
| | | 577 | | { |
| | 0 | 578 | | col.Item().Row(r => |
| | 0 | 579 | | { |
| | 0 | 580 | | r.RelativeItem().PaddingHorizontal(5).Text(label).Bold(); |
| | 0 | 581 | | r.ConstantItem(62).AlignRight().PaddingHorizontal(5).Text(Monto(total)).Bold(); |
| | 0 | 582 | | }); |
| | 0 | 583 | | } |
| | | 584 | | } |