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.XtraGrid.Views.Grid
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.Persistent.BaseImpl
Imports Microsoft.Office.Interop
Imports DevExpress.ExpressApp.Reports
Imports System.IO
Imports System.Runtime.InteropServices
Imports DevExpress.Data.Filtering
Imports DevExpress.XtraReports.UI
Imports DevExpress.ExpressApp.ReportsV2
Imports DevExpress.Persistent.Base.ReportsV2

Public Class InvoiceVC
    Inherits Nuvolar.Module.InvoiceVC
    Dim _WaitFormClass As New WaitFormClass

    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of Nuvolar.Module.InvoiceVC)().Actions.Item("aStornoInvoice").Active.SetItemValue("", False)
        If View.Id = "InvoiceHeader_ListView" Then
            AddHandler DoWork, AddressOf _WaitFormClass.DoWork
            AddHandler InitWork, AddressOf _WaitFormClass.Initwork
            AddHandler FinalWork, AddressOf _WaitFormClass.FinalWork
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        If View.Id = "InvoiceHeader_ListView" Then
            RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
            RemoveHandler InitWork, AddressOf _WaitFormClass.Initwork
            RemoveHandler FinalWork, AddressOf _WaitFormClass.FinalWork
        End If
        MyBase.OnDeactivated()
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "InvoiceHeader_ListView" Then
            Dim _editor As ListEditor = (CType(View, ListView)).Editor
            Dim _gridView As GridView = (CType(_editor, GridListEditor)).GridView
            AddHandler _gridView.SelectionChanged, AddressOf GridView_SelectionChanged
        End If

    End Sub
    Public Sub GridView_SelectionChanged(ByVal sender As Object, ByVal e As DevExpress.Data.SelectionChangedEventArgs)
        Dim _GridView As GridView = TryCast(sender, GridView)
        Dim _InvoiceHeader As InvoiceHeader
        If View Is Nothing Then Exit Sub
        If View.SelectedObjects.Count = 1 Then
            _InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)
            If Not _InvoiceHeader Is Nothing Then
                If View.Id = "InvoiceHeader_ListView" Then
                    If _InvoiceHeader.IsStorno = False Then
                        Frame.GetController(Of Nuvolar.Module.InvoiceVC)().Actions.Item("aStornoInvoice").Active.SetItemValue("", True)
                    Else
                        Frame.GetController(Of Nuvolar.Module.InvoiceVC)().Actions.Item("aStornoInvoice").Active.SetItemValue("", False)
                    End If
                    If _InvoiceHeader.IsEditable = False Then
                        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
                    Else
                        Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
                    End If
                End If
            End If
        Else
            Frame.GetController(Of Nuvolar.Module.InvoiceVC)().Actions.Item("aStornoInvoice").Active.SetItemValue("", False)
        End If
    End Sub
    Private Sub CreatePDFAndSendToDefaultMailClient()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader = TryCast(View.SelectedObjects(0), InvoiceHeader)

        Dim _OutlookApp As New Microsoft.Office.Interop.Outlook.Application()
        Dim _OutlookNameSpace As Microsoft.Office.Interop.Outlook.NameSpace
        Dim _mail As Outlook.MailItem = Nothing
        Dim _mailRecipients As Outlook.Recipients = Nothing
        Dim _mailRecipient As Outlook.Recipient = Nothing
        Dim _mailAttachments As Outlook.Attachments = Nothing
        Dim _mailAttachment As Outlook.Attachment = Nothing
        _OutlookNameSpace = _OutlookApp.GetNamespace("MAPI")
        _OutlookNameSpace.Logon(, , False, True)

        If _InvoiceHeader IsNot Nothing Then
            If _InvoiceHeader.InvoiceType.ReportDataV2 IsNot Nothing Then
                Dim _XtraReport As XtraReport = ReportDataProvider.ReportsStorage.LoadReport(_InvoiceHeader.InvoiceType.ReportDataV2)
                Dim dataSource As Object = TryCast(_XtraReport.DataSource, ISupportCriteria)
                dataSource.Criteria = CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHeader.Oid)

                Frame.GetController(Of ReportsV2.ReportServiceController).SetupBeforePrint(_XtraReport, Nothing, Nothing, False, Nothing, False)
                Dim _FileName As String = Path.GetTempPath & CorrectFileName(_InvoiceHeader.DocumentNumber) & ".pdf"
                Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

                _XtraReport.ExportToPdf(_StreamFile)
                _StreamFile.Close()

                Try
                    _mail = _OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)
                    _mail.Subject = "SZÁMLA"

                    _mailAttachments = _mail.Attachments
                    _mailAttachment = _mailAttachments.Add(_FileName)

                    _mailRecipients = _mail.Recipients

                    For Each _Address As String In _InvoiceHeader.Customers.EMail_Invoice.Split(";")
                        If LTrim(RTrim(_Address)) <> "" Then
                            _mailRecipient = _mailRecipients.Add(_Address)
                        End If
                    Next

                    _mailRecipient.Resolve()

                    If (_mailRecipient.Resolved) Then
                        '_mail.Send()
                        _mail.Display(True)
                    Else
                        System.Windows.Forms.MessageBox.Show(
                            "There is no such record in your address book.")
                    End If
                Catch ex As Exception
                    System.Windows.Forms.MessageBox.Show(ex.Message,
                        "An exception is occured in the code of add-in.")
                Finally
                    If Not IsNothing(_mailRecipient) Then Marshal.ReleaseComObject(_mailRecipient)
                    If Not IsNothing(_mailRecipients) Then Marshal.ReleaseComObject(_mailRecipients)
                    If Not IsNothing(_mail) Then Marshal.ReleaseComObject(_mail)
                End Try
            End If
        End If
    End Sub

    Private Sub aGeneratePDFandSendDefaultMailClient_Invoice_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGeneratePDFandSendDefaultMailClient_Invoice.Execute
        Me.CreatePDFAndSendToDefaultMailClient()
    End Sub
End Class
