237 lines
9.5 KiB
C#
237 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Text;
|
|
using SteamWare;
|
|
using Microsoft.Reporting.WinForms;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing.Printing;
|
|
|
|
/// <summary>
|
|
/// Classe che si occupa di stampare report da reportViewer via printer remota
|
|
/// </summary>
|
|
public class reportPrinter
|
|
{
|
|
|
|
#region area codice da non modificare
|
|
|
|
private int m_currentPageIndex;
|
|
private IList<Stream> m_streams;
|
|
/// <summary>
|
|
/// ciclo da fornire al renderizzatore dei report, per salvare 1 immagine da ogni pagina del report
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="fileNameExtension"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="willSeek"></param>
|
|
/// <returns></returns>
|
|
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
|
{
|
|
Stream stream = new FileStream(SteamWare.IO.fileMover.getFilePath(@".\temp\" + name + "." + fileNameExtension), FileMode.Create);
|
|
m_streams.Add(stream);
|
|
return stream;
|
|
}
|
|
/// <summary>
|
|
/// Handler per PrintPageEvents
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="ev"></param>
|
|
private void PrintPage(object sender, PrintPageEventArgs ev)
|
|
{
|
|
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
|
|
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
|
|
m_currentPageIndex++;
|
|
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
|
|
}
|
|
/// <summary>
|
|
/// funzione di stampa...
|
|
/// </summary>
|
|
private void Print(string printerName)
|
|
{
|
|
//const string printerName = "Microsoft Office Document Image Writer";
|
|
//const string printerName = "Brother HL-2170W series";
|
|
if (m_streams == null || m_streams.Count == 0)
|
|
return;
|
|
PrintDocument printDoc = new PrintDocument();
|
|
printDoc.PrinterSettings.PrinterName = printerName;
|
|
if (!printDoc.PrinterSettings.IsValid)
|
|
{
|
|
logger.lg.scriviLog(String.Format("Can't find printer \"{0}\".", printerName), tipoLog.ERROR);
|
|
return;
|
|
}
|
|
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
|
printDoc.Print();
|
|
}
|
|
/// <summary>
|
|
/// Export del report come EMF (Enhanced Metafile) file.
|
|
/// </summary>
|
|
/// <param name="report"></param>
|
|
private void Export(LocalReport report, string deviceInfo)
|
|
{
|
|
Warning[] warnings;
|
|
m_streams = new List<Stream>();
|
|
report.Render("Image", deviceInfo, CreateStream, out warnings);
|
|
foreach (Stream stream in m_streams) stream.Position = 0;
|
|
}
|
|
/// <summary>
|
|
/// dispone l'applicazione e rilascia le risorse
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if (m_streams != null)
|
|
{
|
|
foreach (Stream stream in m_streams)
|
|
stream.Close();
|
|
m_streams = null;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region area codice da modificare
|
|
|
|
/// <summary>
|
|
/// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
|
|
/// </summary>
|
|
/// <param name="tipoReport">report ammessi: ElencoMacchine / RichiestaIntervento</param>
|
|
/// <param name="periodoAnalizzato">oggetto che contiene data inizio e data fine dell'analisi richiesta per il report</param>
|
|
/// <returns>tabella dati</returns>
|
|
private DataTable caricaDati(reportRichiesto tipoReport, string idxObj)
|
|
{
|
|
DataTable tab = new DataTable();
|
|
switch (tipoReport)
|
|
{
|
|
case reportRichiesto.ElencoMacchine:
|
|
tab = (DataTable)TA_app.obj.taElencoImpMacch.GetData();
|
|
break;
|
|
case reportRichiesto.RichiestaIntervento:
|
|
tab = (DataTable)TA_app.obj.taInterventiMtz.getByIdx(Convert.ToInt32(idxObj));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return tab;
|
|
}
|
|
/// <summary>
|
|
/// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
|
|
/// </summary>
|
|
/// <param name="tipoReport">report ammessi: ElencoInterventi / MTBF_MTTR</param>
|
|
/// <param name="periodoAnalizzato">oggetto che contiene data inizio e data fine dell'analisi richiesta per il report</param>
|
|
/// <returns>tabella dati</returns>
|
|
private DataTable caricaDati(reportRichiesto tipoReport, intervalloDate periodoAnalizzato, string username)
|
|
{
|
|
DataTable tab = new DataTable();
|
|
switch (tipoReport)
|
|
{
|
|
case reportRichiesto.ElencoInterventi:
|
|
tab = (DataTable)TA_app.obj.taElencoIntFilt.getByIntervDateUsernameFilt(periodoAnalizzato.inizio, periodoAnalizzato.fine, username);
|
|
break;
|
|
case reportRichiesto.MTBF_MTTR:
|
|
tab = (DataTable)TA_app.obj.taElenco_MTBF_MTTR.GetData(periodoAnalizzato.inizio, periodoAnalizzato.fine, username);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return tab;
|
|
}
|
|
/// <summary>
|
|
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante
|
|
/// </summary>
|
|
/// <param name="tipoReport">report ammessi: ElencoMacchine / RichiestaIntervento</param>
|
|
/// <param name="printerName">nome completo stampante (rispetto al server)</param>
|
|
/// <param name="parametro">numIntMtz (codice numero richiesta)</param>
|
|
public void printReport(reportRichiesto tipoReport, string printerName, string parametro)
|
|
{
|
|
LocalReport report = new LocalReport();
|
|
devInfoParam deviceInfo = new devInfoParam("EMF", "21cm", "29.7cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
|
|
switch (tipoReport)
|
|
{
|
|
case reportRichiesto.ElencoMacchine:
|
|
report.ReportPath = string.Format(@".\{0}\MacchineReparti.rdlc", memLayer.ML.CRS("ReportBasePath"));
|
|
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, parametro)));
|
|
deviceInfo = new devInfoParam("EMF", "21cm", "29.7cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
|
|
break;
|
|
case reportRichiesto.RichiestaIntervento:
|
|
report.ReportPath = string.Format(@".\{0}\RichiestaIntervento.rdlc", memLayer.ML.CRS("ReportBasePath"));
|
|
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, parametro)));
|
|
deviceInfo = new devInfoParam("EMF", "29.7cm", "21cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
doEmfPrint(printerName, report, deviceInfo.xmlParam);
|
|
}
|
|
/// <summary>
|
|
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante
|
|
/// </summary>
|
|
/// <param name="tipoReport">report ammessi: ElencoInterventi / MTBF_MTTR</param>
|
|
/// <param name="printerName">nome completo stampante (rispetto al server)</param>
|
|
/// <param name="periodoAnalizzato">oggetto che contiene data inizio e data fine dell'analisi richiesta per il report</param>
|
|
public void printReport(reportRichiesto tipoReport, string printerName, intervalloDate periodoAnalizzato, string username)
|
|
{
|
|
LocalReport report = new LocalReport();
|
|
devInfoParam deviceInfo = new devInfoParam("EMF", "21cm", "29.7cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
|
|
switch (tipoReport)
|
|
{
|
|
case reportRichiesto.ElencoInterventi:
|
|
report.ReportPath = string.Format(@".\{0}\ReportInterventi.rdlc", memLayer.ML.CRS("ReportBasePath"));
|
|
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, periodoAnalizzato, username)));
|
|
deviceInfo = new devInfoParam("EMF", "29.7cm", "21cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
|
|
break;
|
|
case reportRichiesto.MTBF_MTTR:
|
|
report.ReportPath = string.Format(@".\{0}\Report_MTBF_MTTR.rdlc", memLayer.ML.CRS("ReportBasePath"));
|
|
report.DataSources.Add(new ReportDataSource("WebGim", caricaDati(tipoReport, periodoAnalizzato, username)));
|
|
deviceInfo = new devInfoParam("EMF", "29.7cm", "21cm", "0.2cm", "0.2cm", "0.2cm", "0.2cm");
|
|
// calcolo le ore totali equivalenti...
|
|
ReportParameter[] OreTotali = { new ReportParameter("OreTotali", WebGimUtils.obj.oreEquivalenti(periodoAnalizzato).ToString()) };
|
|
report.SetParameters(OreTotali);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
doEmfPrint(printerName, report, deviceInfo.xmlParam);
|
|
}
|
|
/// <summary>
|
|
/// esegue print vero e proprio
|
|
/// </summary>
|
|
/// <param name="printerName"></param>
|
|
/// <param name="report"></param>
|
|
/// <param name="deviceInfo"></param>
|
|
private void doEmfPrint(string printerName, LocalReport report, string deviceInfo)
|
|
{
|
|
// export in EMF
|
|
Export(report, deviceInfo);
|
|
m_currentPageIndex = 0;
|
|
// stampo
|
|
Print(printerName);
|
|
// do dispose?
|
|
Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// oggetto protected
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
protected reportPrinter()
|
|
{
|
|
}
|
|
/// <summary>
|
|
/// singleton pubblico
|
|
/// </summary>
|
|
public static reportPrinter obj = new reportPrinter();
|
|
|
|
#endregion
|
|
}
|
|
/// <summary>
|
|
/// tipologia di report ammessi alla stampa...
|
|
/// </summary>
|
|
public enum reportRichiesto
|
|
{
|
|
ElencoInterventi,
|
|
ElencoMacchine,
|
|
MTBF_MTTR,
|
|
RichiestaIntervento
|
|
}
|