Files
SanchoToro/GestionGrupoSanchoToro/Informes/Informes.vb
2026-01-23 12:45:41 +01:00

137 lines
5.1 KiB
VB.net

Imports System.IO
Imports bdGrupoSanchoToro
Imports bdGrupoSanchoToro.db
Imports DevExpress.Xpf.Core
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports tsEFCore8.Extensiones
Imports Microsoft.EntityFrameworkCore
Imports System.Drawing.Printing
Imports System.ComponentModel
Imports DevExpress.Xpf.Printing
Public Class Informes
Private Shared NumeroCopias As Integer
Friend Shared Sub ImprimirPlantilla(bd As tscGrupoSanchoToro, Plantilla As Byte(), Datasource As Object, ImpresoraPredeterminada As Boolean, Copias As Integer)
Try
Dim xr As New XtraReport
xr.LoadLayoutFromXml(New MemoryStream(Plantilla), True)
ImprimeInforme(xr, ImpresoraPredeterminada, Copias, bd.AhoraMySql, Datasource)
Catch ex As Exception
DXMessageBox.Show(ex.Message, "Atención", MessageBoxButton.OK, MessageBoxImage.Error)
End Try
End Sub
'Friend Shared Sub ImprimirPlantilla(bd As tscGrupoSanchoToro, idPlantilla As Integer, Datasource As Object, ImpresoraPredeterminada As Boolean, Copias As Integer)
' Try
' If idPlantilla = 0 Then Throw New Exception("Seleccione primero la plantilla a imprimir")
' Dim xr As New XtraReport
' Dim pl = bd.plantillas.AsNoTracking.SingleOrDefault(Function(x) x.idPlantilla = idPlantilla)
' 'Dim pl = bd.plantillas.Where(Function(x) x.idPlantilla = idPlantilla).First
' 'Dim pl As plantillas = (From p In bd.plantillas Where p.idPlantilla = idPlantilla).First
' Dim s As String = System.Text.Encoding.UTF8.GetString(pl.idFicheroNavigation.Fichero)
' Using sw As New StreamWriter(New MemoryStream())
' sw.Write(s)
' sw.Flush()
' xr = XtraReport.FromStream(sw.BaseStream, True)
' End Using
' Try
' Dim pr As DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").First
' pr.Value = bd.AhoraMySql
' pr.Visible = False
' Catch ex As Exception
' End Try
' ' xr.DataSource = Datasource
' ImprimeInforme(xr, ImpresoraPredeterminada, Copias, bd.AhoraMySql, Datasource)
' Catch ex As Exception
' DXMessageBox.Show(ex.Message, "Atención", MessageBoxButton.OK, MessageBoxImage.Error)
' End Try
'End Sub
Friend Shared Function GuardaInformeEnPdf(bd As tscGrupoSanchoToro, idPlantilla As Integer, Datasource As Object) As Byte()
Try
If idPlantilla = 0 Then Throw New Exception("Seleccione primero la plantilla a imprimir")
Dim xr As New XtraReport
Dim pl = bd.plantillas.AsNoTracking.Where(Function(x) x.idPlantilla = idPlantilla).First
Dim s As String = System.Text.Encoding.UTF8.GetString(pl.idFicheroNavigation.Fichero)
Using sw As New StreamWriter(New MemoryStream())
sw.Write(s)
sw.Flush()
xr = XtraReport.FromStream(sw.BaseStream, True)
End Using
Try
Dim pr As DevExpress.XtraReports.Parameters.Parameter = (From p As DevExpress.XtraReports.Parameters.Parameter In xr.Parameters Where p.Name = "Fecha").First
pr.Value = bd.AhoraMySql
pr.Visible = False
Catch ex As Exception
End Try
xr.DataSource = Datasource
Return GuardaInformeEnPdf(xr)
Catch ex As Exception
Throw New Exception(ex.Message, ex)
'DXMessageBox.Show(ex.Message, "Atención", MessageBoxButton.OK, MessageBoxImage.Error)
End Try
End Function
Friend Shared Sub ImprimeInforme(xr As XtraReport, ImpresoraPredeterminada As Boolean, Copias As Integer, Fecha As Date, Datos As Object)
Try
For Each pr In xr.Parameters
pr.Visible = False
If pr.Name = "Fecha" Then
pr.Value = Fecha
End If
Next
xr.DataSource = Datos
xr.CreateDocument(True)
If ImpresoraPredeterminada Then
If Copias < 2 Then
NumeroCopias = 1
xr.Print()
Else
NumeroCopias = Copias
AddHandler xr.PrintingSystem.StartPrint, AddressOf xr_StartPring
xr.Print()
End If
Else
PrintHelper.Print(xr)
'xr.PrintDialog()
End If
Catch ex As Exception
DXMessageBox.Show(ex.Message, "Error")
End Try
End Sub
Private Shared Sub xr_StartPring(sender As Object, e As PrintDocumentEventArgs)
e.PrintDocument.PrinterSettings.Copies = NumeroCopias
End Sub
Friend Shared Function GuardaInformeEnPdf(xr As XtraReport) As Byte()
For Each pr In xr.Parameters
pr.Visible = False
Next
xr.CreateDocument(True)
Dim st As New MemoryStream
xr.ExportToPdf(st)
Return st.ToArray
End Function
End Class