Test Reportes con Razor y Puppeteer
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 28m23s

This commit is contained in:
2025-06-19 14:47:43 -03:00
parent 8591945eb4
commit 975a1e6d26
11 changed files with 493 additions and 86 deletions

View File

@@ -12,6 +12,8 @@ using System.IO;
using System.Linq;
using GestionIntegral.Api.Data.Repositories.Distribucion;
using GestionIntegral.Api.Services.Distribucion;
using GestionIntegral.Api.Services.Pdf;
using GestionIntegral.Api.Dtos.Reportes.ViewModels;
namespace GestionIntegral.Api.Controllers
{
@@ -27,7 +29,7 @@ namespace GestionIntegral.Api.Controllers
private readonly IEmpresaRepository _empresaRepository;
private readonly IDistribuidorRepository _distribuidorRepository; // Para obtener el nombre del distribuidor
private readonly INovedadCanillaService _novedadCanillaService;
private readonly IPdfGeneratorService _pdfGeneratorService;
// Permisos
private const string PermisoVerReporteExistenciaPapel = "RR005";
@@ -48,7 +50,8 @@ namespace GestionIntegral.Api.Controllers
IPlantaRepository plantaRepository,
IPublicacionRepository publicacionRepository,
IEmpresaRepository empresaRepository,
IDistribuidorRepository distribuidorRepository)
IDistribuidorRepository distribuidorRepository,
IPdfGeneratorService pdfGeneratorService)
{
_reportesService = reportesService;
_novedadCanillaService = novedadCanillaService;
@@ -57,6 +60,7 @@ namespace GestionIntegral.Api.Controllers
_publicacionRepository = publicacionRepository;
_empresaRepository = empresaRepository;
_distribuidorRepository = distribuidorRepository;
_pdfGeneratorService = pdfGeneratorService;
}
private bool TienePermiso(string codAcc) => User.IsInRole("SuperAdmin") || User.HasClaim(c => c.Type == "permission" && c.Value == codAcc);
@@ -94,14 +98,14 @@ namespace GestionIntegral.Api.Controllers
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetReporteExistenciaPapelPdf(
[FromQuery] DateTime fechaDesde,
[FromQuery] DateTime fechaHasta,
[FromQuery] int? idPlanta,
[FromQuery] bool consolidado = false)
[FromQuery] DateTime fechaDesde,
[FromQuery] DateTime fechaHasta,
[FromQuery] int? idPlanta,
[FromQuery] bool consolidado = false)
{
if (!TienePermiso(PermisoVerReporteExistenciaPapel)) return Forbid();
var (data, error) = await _reportesService.ObtenerExistenciaPapelAsync(fechaDesde, fechaHasta, idPlanta, consolidado); // <--- CORREGIDO
var (data, error) = await _reportesService.ObtenerExistenciaPapelAsync(fechaDesde, fechaHasta, idPlanta, consolidado);
if (error != null)
{
@@ -114,43 +118,50 @@ namespace GestionIntegral.Api.Controllers
try
{
LocalReport report = new LocalReport();
string rdlcPath = consolidado ? "Controllers/Reportes/RDLC/ReporteExistenciaPapelConsolidado.rdlc" : "Controllers/Reportes/RDLC/ReporteExistenciaPapel.rdlc";
using (var fs = new FileStream(rdlcPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
report.LoadReportDefinition(fs);
}
report.DataSources.Add(new ReportDataSource("DSConsumoBobinas", data));
var parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("FechaDesde", fechaDesde.ToString("dd/MM/yyyy")));
parameters.Add(new ReportParameter("FechaHasta", fechaHasta.ToString("dd/MM/yyyy")));
// Determinar la plantilla y el nombre de la planta
string templatePath;
string nombrePlantaParam = "Consolidado";
if (!consolidado && idPlanta.HasValue)
if (consolidado)
{
var planta = await _plantaRepository.GetByIdAsync(idPlanta.Value);
nombrePlantaParam = planta?.Nombre ?? "N/A";
}
else if (consolidado)
{
// Para el consolidado, el RDLC ReporteExistenciaPapelConsolidado.txt NO espera NomPlanta
templatePath = Path.Combine("Controllers", "Reportes", "Templates", "ReporteExistenciaPapelConsolidado.cshtml");
}
else
{ // No consolidado pero idPlanta es NULL (aunque el servicio ya valida esto)
nombrePlantaParam = "N/A";
}
// Solo añadir NomPlanta si NO es consolidado, porque el RDLC consolidado no lo tiene.
if (!consolidado)
{
parameters.Add(new ReportParameter("NomPlanta", nombrePlantaParam));
templatePath = Path.Combine("Controllers", "Reportes", "Templates", "ReporteExistenciaPapel.cshtml");
if (idPlanta.HasValue)
{
var planta = await _plantaRepository.GetByIdAsync(idPlanta.Value);
nombrePlantaParam = planta?.Nombre ?? "N/A";
}
}
// Crear el ViewModel para la plantilla Razor
var viewModel = new ExistenciaPapelViewModel
{
Existencias = data,
NombrePlanta = nombrePlantaParam,
FechaDesde = fechaDesde.ToString("dd/MM/yyyy"),
FechaHasta = fechaHasta.ToString("dd/MM/yyyy")
};
report.SetParameters(parameters);
// Configurar opciones de PDF (márgenes, etc.)
var pdfOptions = new PdfGenerationOptions
{
Margin = new PuppeteerSharp.Media.MarginOptions
{
Top = "2cm",
Bottom = "2cm",
Left = "1cm",
Right = "1cm"
},
Format = PuppeteerSharp.Media.PaperFormat.A4,
// Podríamos agregar un encabezado/pie de página aquí si fuera necesario
// FooterTemplate = "<div style='font-size:8px; width:100%; text-align:center;'>Página <span class='pageNumber'></span> de <span class='totalPages'></span></div>"
};
// Generar el PDF
byte[] pdfBytes = await _pdfGeneratorService.GeneratePdfFromRazorTemplateAsync(templatePath, viewModel, pdfOptions);
byte[] pdfBytes = report.Render("PDF");
string fileName = $"ExistenciaPapel_{fechaDesde:yyyyMMdd}_{fechaHasta:yyyyMMdd}_{(consolidado ? "Consolidado" : $"Planta{idPlanta}")}.pdf";
return File(pdfBytes, "application/pdf", fileName);
}

View File

@@ -0,0 +1,116 @@
@using GestionIntegral.Api.Dtos.Reportes.ViewModels
@model ExistenciaPapelViewModel
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
font-family: 'Roboto', Arial, sans-serif;
font-size: 10pt;
margin: 0;
padding: 0;
}
.report-container {
width: 100%;
margin: auto;
}
.report-header {
text-align: center;
padding: 10px;
border-bottom: 2px solid #ccc;
margin-bottom: 20px;
}
.report-header h1 {
font-size: 14pt;
margin: 0 0 5px 0;
}
.report-header h2 {
font-size: 12pt;
margin: 0;
font-weight: normal;
}
.report-parameters {
margin-bottom: 20px;
padding: 10px;
border: 1px solid #eee;
background-color: #f9f9f9;
}
.report-parameters p {
margin: 0 0 5px 0;
}
.report-table {
width: 100%;
border-collapse: collapse;
font-size: 9pt;
}
.report-table th, .report-table td {
border: 1px solid #ccc;
padding: 6px;
text-align: left;
}
.report-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.text-right { text-align: right; }
.text-center { text-align: center; }
</style>
</head>
<body>
<div class="report-container">
<div class="report-header">
<h1>Reporte de Existencias de Papel</h1>
<h2>Planta: @Model.NombrePlanta</h2>
</div>
<div class="report-parameters">
<p><strong>Fecha del Reporte:</strong> @Model.FechaReporte</p>
<p><strong>Periodo Consultado:</strong> Desde @Model.FechaDesde Hasta @Model.FechaHasta</p>
</div>
<table class="report-table">
<thead>
<tr>
<th>Tipo Bobina</th>
<th class="text-right">Cant. Stock</th>
<th class="text-right">Kg. Stock</th>
<th class="text-right">Consumo Acumulado (Kg)</th>
<th class="text-right">Días Disponibles</th>
<th class="text-center">Fin Stock Estimado</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Existencias)
{
<tr>
<td>@item.TipoBobina</td>
<td class="text-right">@(item.BobinasEnStock?.ToString("N0") ?? "0")</td>
<td class="text-right">@(item.TotalKilosEnStock?.ToString("N0") ?? "0")</td>
<td class="text-right">@(item.ConsumoAcumulado?.ToString("N0") ?? "0")</td>
<td class="text-right">@(item.PromedioDiasDisponibles?.ToString("N0") ?? "N/A")</td>
<td class="text-center">@(item.FechaEstimacionFinStock?.ToString("dd/MM/yyyy") ?? "N/A")</td>
</tr>
}
</tbody>
<tfoot>
@{
var totalBobinas = Model.Existencias.Sum(e => e.BobinasEnStock ?? 0);
var totalKilos = Model.Existencias.Sum(e => e.TotalKilosEnStock ?? 0);
var totalConsumo = Model.Existencias.Sum(e => e.ConsumoAcumulado ?? 0);
}
<tr style="font-weight: bold;">
<td class="text-right">Totales</td>
<td class="text-right">@totalBobinas.ToString("N0")</td>
<td class="text-right">@totalKilos.ToString("N0")</td>
<td class="text-right">@totalConsumo.ToString("N0")</td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>

View File

@@ -0,0 +1,115 @@
@using GestionIntegral.Api.Dtos.Reportes.ViewModels
@model ExistenciaPapelViewModel
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
font-family: 'Roboto', Arial, sans-serif;
font-size: 10pt;
margin: 0;
padding: 0;
}
.report-container {
width: 100%;
margin: auto;
}
.report-header {
text-align: center;
padding: 10px;
border-bottom: 2px solid #ccc;
margin-bottom: 20px;
}
.report-header h1 {
font-size: 14pt;
margin: 0 0 5px 0;
}
.report-header h2 {
font-size: 12pt;
margin: 0;
font-weight: normal;
}
.report-parameters {
margin-bottom: 20px;
padding: 10px;
border: 1px solid #eee;
background-color: #f9f9f9;
}
.report-parameters p {
margin: 0 0 5px 0;
}
.report-table {
width: 100%;
border-collapse: collapse;
font-size: 9pt;
}
.report-table th, .report-table td {
border: 1px solid #ccc;
padding: 6px;
text-align: left;
}
.report-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.text-right { text-align: right; }
.text-center { text-align: center; }
</style>
</head>
<body>
<div class="report-container">
<div class="report-header">
<h1>Reporte de Existencias de Papel</h1>
</div>
<div class="report-parameters">
<p><strong>Fecha del Reporte:</strong> @Model.FechaReporte</p>
<p><strong>Periodo Consultado:</strong> Desde @Model.FechaDesde Hasta @Model.FechaHasta</p>
</div>
<table class="report-table">
<thead>
<tr>
<th>Tipo Bobina</th>
<th class="text-right">Cant. Stock</th>
<th class="text-right">Kg. Stock</th>
<th class="text-right">Consumo Acumulado (Kg)</th>
<th class="text-right">Días Disponibles</th>
<th class="text-center">Fin Stock Estimado</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Existencias)
{
<tr>
<td>@item.TipoBobina</td>
<td class="text-right">@(item.BobinasEnStock?.ToString("N0") ?? "0")</td>
<td class="text-right">@(item.TotalKilosEnStock?.ToString("N0") ?? "0")</td>
<td class="text-right">@(item.ConsumoAcumulado?.ToString("N0") ?? "0")</td>
<td class="text-right">@(item.PromedioDiasDisponibles?.ToString("N0") ?? "N/A")</td>
<td class="text-center">@(item.FechaEstimacionFinStock?.ToString("dd/MM/yyyy") ?? "N/A")</td>
</tr>
}
</tbody>
<tfoot>
@{
var totalBobinas = Model.Existencias.Sum(e => e.BobinasEnStock ?? 0);
var totalKilos = Model.Existencias.Sum(e => e.TotalKilosEnStock ?? 0);
var totalConsumo = Model.Existencias.Sum(e => e.ConsumoAcumulado ?? 0);
}
<tr style="font-weight: bold;">
<td class="text-right">Totales</td>
<td class="text-right">@totalBobinas.ToString("N0")</td>
<td class="text-right">@totalKilos.ToString("N0")</td>
<td class="text-right">@totalConsumo.ToString("N0")</td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>