Files
GestionIntegralWeb/Backend/GestionIntegral.Api/Controllers/Reportes/PdfTemplates/ConsumoBobinasSeccionDocument.cs
dmolinari 229eb937f5
All checks were successful
Optimized Build and Deploy / remote-build-and-deploy (push) Successful in 7m55s
QuestPdf Implementado en la totalidad de reportes.
2025-06-24 12:52:37 -03:00

139 lines
6.4 KiB
C#

using GestionIntegral.Api.Dtos.Reportes;
using GestionIntegral.Api.Dtos.Reportes.ViewModels;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using System.Collections.Generic;
using System.Linq;
namespace GestionIntegral.Api.Controllers.Reportes.PdfTemplates
{
public class ConsumoBobinasSeccionDocument : IDocument
{
public ConsumoBobinasSeccionViewModel Model { get; }
public ConsumoBobinasSeccionDocument(ConsumoBobinasSeccionViewModel model)
{
Model = model;
}
public DocumentMetadata GetMetadata() => DocumentMetadata.Default;
public void Compose(IDocumentContainer container)
{
container.Page(page =>
{
page.Margin(1.5f, Unit.Centimetre);
page.DefaultTextStyle(x => x.FontFamily("Arial").FontSize(9));
page.Header().Element(ComposeHeader);
page.Content().Element(ComposeContent);
page.Footer().AlignCenter().Text(x => { x.Span("Página "); x.CurrentPageNumber(); });
});
}
void ComposeHeader(IContainer container)
{
container.Column(column =>
{
column.Spacing(5);
column.Item().AlignCenter().Text("Reporte de Consumo de Bobinas por Secciones").SemiBold().FontSize(14);
string subTitle = Model.EsConsolidado ? "Consolidado" : $"Planta: {Model.NombrePlanta}";
column.Item().AlignCenter().Text(subTitle).FontSize(12);
column.Item().PaddingTop(5, Unit.Millimetre).Row(row =>
{
row.RelativeItem().Text(text => { text.Span("Fecha del Reporte: ").SemiBold(); text.Span(Model.FechaReporte); });
row.RelativeItem().AlignRight().Text(text => { text.Span("Periodo: ").SemiBold(); text.Span($"{Model.FechaDesde} - {Model.FechaHasta}"); });
});
// Encabezado de la tabla principal
column.Item().PaddingTop(10).BorderBottom(1.5f).BorderColor(Colors.Black).Padding(4).Row(row =>
{
row.RelativeItem(1.5f).Text("Publicación").SemiBold();
row.RelativeItem(1.5f).Text("Sección").SemiBold();
row.RelativeItem(3).Text("Bobina").SemiBold();
row.RelativeItem(1).AlignRight().Text("Cant. Bobinas").SemiBold();
row.RelativeItem(1).AlignRight().Text("Kilos").SemiBold();
});
});
}
void ComposeContent(IContainer container)
{
container.Column(column =>
{
var gruposPorPublicacion = Model.Detalles.GroupBy(p => p.NombrePublicacion);
foreach (var grupoPub in gruposPorPublicacion)
{
column.Item().Element(c => ComposePublicacionSection(c, grupoPub));
}
// Fila de Totales Generales al final
var totalGeneralBobinas = Model.Detalles.Sum(d => d.CantidadBobinas);
var totalGeneralKilos = Model.Detalles.Sum(d => d.TotalKilos);
column.Item().PaddingTop(10).Row(row =>
{
row.RelativeItem(6).AlignRight().Text("Total General").ExtraBold().FontSize(12);
row.RelativeItem(1).AlignRight().Text(totalGeneralBobinas.ToString("N0")).ExtraBold().FontSize(12);
row.RelativeItem(1).AlignRight().Text(totalGeneralKilos.ToString("N0")).ExtraBold().FontSize(12);
});
});
}
// --- COMPONENTE PARA UNA PUBLICACIÓN ENTERA ---
void ComposePublicacionSection(IContainer container, IGrouping<string, ConsumoBobinasSeccionDto> grupoPub)
{
container.Row(row =>
{
// Columna 1: Nombre de la Publicación
row.RelativeItem(1.5f).BorderBottom(1).BorderLeft(1).BorderRight(1).BorderColor(Colors.Grey.Medium).Padding(3).Text(grupoPub.Key).SemiBold();
// Columna 2: Contiene todas las sub-tablas de secciones
row.RelativeItem(6.5f).Column(seccionesColumn =>
{
var gruposPorSeccion = grupoPub.GroupBy(s => s.NombreSeccion);
foreach(var grupoSec in gruposPorSeccion)
{
seccionesColumn.Item().Element(c => ComposeSeccionTable(c, grupoSec));
}
});
});
}
// --- COMPONENTE PARA LA TABLA DE UNA SECCIÓN ---
void ComposeSeccionTable(IContainer container, IGrouping<string, ConsumoBobinasSeccionDto> grupoSec)
{
container.Table(table =>
{
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn(1.5f); // Sección
columns.RelativeColumn(3); // Bobina
columns.RelativeColumn(1); // Cantidad
columns.RelativeColumn(1); // Kilos
});
// Filas de detalle
foreach (var detalle in grupoSec.OrderBy(d => d.NombreBobina))
{
table.Cell().Border(1).BorderColor(Colors.Grey.Medium).Padding(3).Text(grupoSec.Key);
table.Cell().Border(1).BorderColor(Colors.Grey.Medium).Padding(3).Text(detalle.NombreBobina);
table.Cell().Border(1).BorderColor(Colors.Grey.Medium).Padding(3).AlignRight().Text(detalle.CantidadBobinas.ToString("N0"));
table.Cell().Border(1).BorderColor(Colors.Grey.Medium).Padding(3).AlignRight().Text(detalle.TotalKilos.ToString("N0"));
}
// Fila de total de la sección
var totalCantSeccion = grupoSec.Sum(d => d.CantidadBobinas);
var totalKilosSeccion = grupoSec.Sum(d => d.TotalKilos);
table.Cell().ColumnSpan(2).Border(1).BorderColor(Colors.Grey.Medium).AlignRight().Padding(3).Text("Total Sección").SemiBold();
table.Cell().Border(1).BorderColor(Colors.Grey.Medium).Padding(3).AlignRight().Text(t => t.Span(totalCantSeccion.ToString("N0")).SemiBold());
table.Cell().Border(1).BorderColor(Colors.Grey.Medium).Padding(3).AlignRight().Text(t => t.Span(totalKilosSeccion.ToString("N0")).SemiBold());
});
}
}
}