Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.SystemModule
Imports System.Linq
Imports System.IO
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Persistent.BaseImpl

Public Module ViewAttachments
    Public Sub ViewAttachments(_ObjectSpace As Xpo.XPObjectSpace, _Frame As DevExpress.ExpressApp.Frame, _
                               _CustomersFrom As CustomersFrom, _Customers As Customers, _
                               _DocumentNumber As String, Optional _DocumentNumberProforma As String = "", _
                               Optional _GLMixed As GLMixed = Nothing)

        Dim _RegistryHeader As RegistryHeader = _ObjectSpace.FindObject(Of RegistryHeader) _
        (CriteriaOperator.Parse("CustomersFrom=? and Customers=? and DocumentNumber=?", _CustomersFrom, _Customers, _DocumentNumber))

        Dim _InvoiceHeader As InvoiceHeader = _ObjectSpace.FindObject(Of InvoiceHeader) _
        (CriteriaOperator.Parse("CustomersFrom=? and Customers=? and DocumentNumber=?", _CustomersFrom, _Customers, _DocumentNumber))

        If _InvoiceHeader Is Nothing Then
            _InvoiceHeader = _ObjectSpace.FindObject(Of InvoiceHeader)(CriteriaOperator.Parse("CustomersFrom=? and Customers=? and DocumentNumberProforma=?", _CustomersFrom, _Customers, _DocumentNumberProforma))
        End If

        If _RegistryHeader IsNot Nothing Then

            Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _RegistryHeader.DocumentFile.FileName)
            Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
            _RegistryHeader.DocumentFile.SaveToStream(_Stream)
            _Stream.Close()
            Process.Start(_FilePath)
        ElseIf _InvoiceHeader IsNot Nothing Then
            Dim _ReportData As ReportData
            Dim _ReportData_DEV As ReportData
            Dim _ReportData_DEV_EU As ReportData
            Dim _CurrencyName As CurrencyName
            _CurrencyName = _InvoiceHeader.CurrencyName
            If _InvoiceHeader IsNot Nothing Then
                _ReportData = _InvoiceHeader.InvoiceType.ReportData
                _ReportData_DEV = _InvoiceHeader.InvoiceType.ReportData_DEV
                _ReportData_DEV_EU = _InvoiceHeader.InvoiceType.ReportData_DEV_EU
                If _ReportData IsNot Nothing Or _InvoiceHeader Is Nothing Then
                    If _CurrencyName.ShortName = "HUF" Then
                        _Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader), True)
                    Else
                        _Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_DEV, CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader), True)
                    End If

                End If
            End If
        End If
        If _GLMixed IsNot Nothing Then
            If _GLMixed.DocumentFile IsNot Nothing Then
                ProcessDocument(_GLMixed.DocumentFile)
            End If
        End If
    End Sub
    Private Sub ProcessDocument(_DocumentFile As FileData)
        Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _DocumentFile.FileName)
        Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
        _DocumentFile.SaveToStream(_Stream)
        _Stream.Close()
        Process.Start(_FilePath)
    End Sub
End Module
