Test Reportes con Razor y Puppeteer
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 28m23s
All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 28m23s
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user