Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Reports
Imports System.IO
Imports DevExpress.XtraReports.UI
Imports DevExpress.ExpressApp.ReportsV2

Public Class XAFPrintHelper
    Private _Frame As Frame
    Private _ReportData As ReportData
    Private _ReportDataV2 As ReportDataV2
    Private _CriteriaOperator As CriteriaOperator
    Private _XPOObjectSpace As Xpo.XPObjectSpace
    Sub New(XPOObjectSpace As Xpo.XPObjectSpace, Frame As Frame, ReportData As ReportData, CriteriaOperator As CriteriaOperator)
        _Frame = Frame
        _ReportData = ReportData
        _CriteriaOperator = CriteriaOperator
        _XPOObjectSpace = XPOObjectSpace
    End Sub
    Sub New(XPOObjectSpace As Xpo.XPObjectSpace, Frame As Frame, ReportDataV2 As ReportDataV2, CriteriaOperator As CriteriaOperator)
        _Frame = Frame
        _ReportDataV2 = ReportDataV2
        _CriteriaOperator = CriteriaOperator
        _XPOObjectSpace = XPOObjectSpace
    End Sub

    Public Sub ShowPreview()
        _Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, _CriteriaOperator, True)
    End Sub
    Public Sub PrintToCurrentPrinter()
        _Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, _CriteriaOperator, True)
    End Sub
    Public Overridable Sub ShowPreviewV2()

        If _ReportDataV2 Is Nothing Then
            MsgBox("Nincs beállítva nyomtatvány", vbOK + vbExclamation, "F I G Y E L E M !")
            Exit Sub
        End If

        Dim _XtraReport As XtraReport = ReportDataProvider.ReportsStorage.LoadReport(_ReportDataV2)
        Dim dataSource As Object = TryCast(_XtraReport.DataSource, DevExpress.Persistent.Base.ReportsV2.ISupportCriteria)
        dataSource.Criteria = _CriteriaOperator
        _Frame.GetController(Of ReportsV2.ReportServiceController).SetupBeforePrint(_XtraReport, Nothing, Nothing, False, Nothing, False)
        _XtraReport.ShowPreview()
        'Dim handle As String = ReportDataProvider.ReportsStorage.GetReportContainerHandle(_ReportDataV2)
        '_Frame.GetController(Of ReportsV2.ReportServiceController).ShowPreview(handle, _CriteriaOperator)
    End Sub
    Public Overridable Sub ShowPreviewV2PDF()

        If _ReportDataV2 Is Nothing Then
            MsgBox("Nincs beállítva nyomtatvány", vbOK + vbExclamation, "F I G Y E L E M !")
            Exit Sub
        End If

        Dim _XtraReport As XtraReport = ReportDataProvider.ReportsStorage.LoadReport(_ReportDataV2)
        Dim dataSource As Object = TryCast(_XtraReport.DataSource, DevExpress.Persistent.Base.ReportsV2.ISupportCriteria)
        Dim _PDFExportOptions As New DevExpress.XtraPrinting.PdfExportOptions

        dataSource.Criteria = _CriteriaOperator
        _Frame.GetController(Of ReportsV2.ReportServiceController).SetupBeforePrint(_XtraReport, Nothing, Nothing, False, Nothing, False)

        _PDFExportOptions.ShowPrintDialogOnOpen = False

        _XtraReport.ExportToPdf("c:\temp\kutya.pdf", _PDFExportOptions)
        Process.Start("c:\temp\kutya.pdf")
    End Sub
    Public Overridable Function GeneratePDF() As MemoryStream
        Dim _XafReport As XafReport = _ReportData.LoadReport(_XPOObjectSpace)
        Dim _MemoryStream As New MemoryStream
        Dim _PDFExportOptions As New DevExpress.XtraPrinting.PdfExportOptions
        _PDFExportOptions.ShowPrintDialogOnOpen = False

        _XafReport.FilterString = _CriteriaOperator.ToString
        _XafReport.ExportToPdf(_MemoryStream, _PDFExportOptions)
        _MemoryStream.Seek(0, System.IO.SeekOrigin.Begin)

        Return _MemoryStream

    End Function
End Class
