6f320d38b5
- Completato report x ordini - gestione integrata con stored
155 lines
6.2 KiB
C#
155 lines
6.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Microsoft.Reporting.NETCore;
|
|
using NLog;
|
|
using System.Data;
|
|
using ToDataTable;
|
|
using WebDoorCreator.Core.ReportViewer;
|
|
using WebDoorCreator.Data.Services;
|
|
|
|
namespace WebDoorCreator.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class ReportController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ReportController(IConfiguration configuration, WebDoorCreatorService DataService)
|
|
{
|
|
Log.Info("Starting ReportController");
|
|
_configuration = configuration;
|
|
WDCService = DataService;
|
|
Log.Info("Avviato ReportController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
// GET: api/Alive
|
|
[HttpGet]
|
|
public string Get()
|
|
{
|
|
return "OK";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce report dato ordine e formato
|
|
/// </summary>
|
|
/// <param name="OrderId">ID univoco ordine</param>
|
|
/// <param name="Format">Formato: PDF/HTML/DOCX/XLSX</param>
|
|
/// <returns></returns>
|
|
[HttpGet("GetOrderReport")]
|
|
public async Task<IActionResult> GetOrderReport(int OrderId, string Format)
|
|
{
|
|
// dato il servizio è impostato il file rdlc e la struttura data DS da passare
|
|
await Task.Delay(1);
|
|
// inizializzo parametri report di default
|
|
Core.ReportViewer.Report.RenderParams currParams = new Core.ReportViewer.Report.RenderParams(Format);
|
|
currParams.FileName= $"DCA-Order-{OrderId:00000000}";
|
|
|
|
// restituisco oggetto ReportOrder specifico
|
|
return await OrderReportGet(OrderId, currParams);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private WebDoorCreatorService WDCService { get; set; } = null!;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Restituisce filestream del report DEMO
|
|
/// </summary>
|
|
/// <param name="FileName">Nome file desiderato x download</param>
|
|
/// <param name="RenderFormat"></param>
|
|
/// <param name="FileExt"></param>
|
|
/// <param name="MimeType"></param>
|
|
/// <returns></returns>
|
|
private IActionResult ReportDemoGet(int OrderId, Core.ReportViewer.Report.RenderParams CurrParams)
|
|
{
|
|
using (var report = new LocalReport())
|
|
{
|
|
// setup parametri
|
|
List<ReportParameter> RepParams = new List<ReportParameter>();
|
|
RepParams.Add(new ReportParameter("Title", "Invoice 4/2020"));
|
|
// setup DsReport
|
|
Dictionary<string, DataTable> DsDict = new Dictionary<string, DataTable>();
|
|
Random rnd = new Random();
|
|
decimal WidgetPrice = 204.99m;
|
|
decimal GizmoPrice = 2.41m;
|
|
var items = new[] {
|
|
new ReportItem { Description = "Widget 6000", Price = WidgetPrice, Qty = rnd.Next(1,5) },
|
|
new ReportItem { Description = "Gizmo MAX", Price = GizmoPrice, Qty = rnd.Next(10,50) },
|
|
new ReportItem { Description = "Pippo", Price = 1.2m, Qty = rnd.Next(3,9) },
|
|
new ReportItem { Description = "Paperino", Price = 1.74m, Qty = rnd.Next(5,15) }
|
|
}.ToList();
|
|
var totaQty = items.Sum(x => x.Qty);
|
|
var avgPrice = items.Sum(x => x.Qty * x.Price) / totaQty;
|
|
items.Add(new ReportItem() { Description = "Total", Qty = totaQty, Price = Math.Round(avgPrice, 2) });
|
|
|
|
DataTable tabDati = items.ToDataTable();
|
|
tabDati.TableName = "Items";
|
|
// aggiungo dizionario
|
|
DsDict.Add("Items", tabDati);
|
|
|
|
// carico dati nel report...
|
|
Core.ReportViewer.Report.Load(report, "Report.rdlc", RepParams, DsDict);
|
|
var rawData = report.Render(CurrParams.RenderFormat);
|
|
return File(rawData, CurrParams.MimeType, $"{CurrParams.FileName}.{CurrParams.Extension}");
|
|
}
|
|
}
|
|
#endif
|
|
/// <summary>
|
|
/// Restituisce filestream del report richiesto
|
|
/// </summary>
|
|
/// <param name="OrderId">ID Ordine</param>
|
|
/// <param name="CurrParams">Parametri rendering report</param>
|
|
/// <returns></returns>
|
|
private async Task<IActionResult> OrderReportGet(int OrderId, Core.ReportViewer.Report.RenderParams CurrParams)
|
|
{
|
|
using (var report = new LocalReport())
|
|
{
|
|
// setup parametri
|
|
List<ReportParameter> RepParams = new List<ReportParameter>();
|
|
RepParams.Add(new ReportParameter("OrderID", $"{OrderId}"));
|
|
// setup DsReport
|
|
Dictionary<string, DataTable> DsDict = new Dictionary<string, DataTable>();
|
|
// recupero tab dati config...
|
|
var confList = await WDCService.ConfigGetAll();
|
|
DataTable tabDatiConf = confList.ToDataTable();
|
|
tabDatiConf.TableName = "DataSetConfig";
|
|
// aggiungo a dizionario
|
|
DsDict.Add("DataSetConfig", tabDatiConf);
|
|
|
|
// recupero dati veri del report...
|
|
var repDetailData = await WDCService.PreRepOrderGetByKey(OrderId);
|
|
DataTable tabDati = repDetailData.ToDataTable();
|
|
tabDati.TableName = "DataSetOrderReportExpl";
|
|
// aggiungo dizionario
|
|
DsDict.Add("DataSetOrderReportExpl", tabDati);
|
|
|
|
// carico dati nel report...
|
|
Core.ReportViewer.Report.Load(report, "ReportOrder.rdlc", RepParams, DsDict);
|
|
var rawData = report.Render(CurrParams.RenderFormat);
|
|
return File(rawData, CurrParams.MimeType, $"{CurrParams.FileName}.{CurrParams.Extension}");
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |