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.XtraReports.UI
Imports DevExpress.XtraReports.Parameters
Imports DevExpress.ExpressApp.Reports
Imports DevExpress.Persistent.BaseImpl
Imports System.IO

Public Class OffersOutPrintVC
    Inherits DevExpress.ExpressApp.ViewController
    Private _ReportServiceController As ReportServiceController
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "OfferOutHeader_DetailView" Then
            Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrint.Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Opportunities_OfferOutHeader_ListView" Then
            Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrint.Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_ListView_Support" Then
            Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrint.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrintPDF2.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrintPDF.Active.SetItemValue("", False)
        End If
        If View.Id = "OfferOutHeader_ListView_Subcontractor" Then
            Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrint.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrintPDF2.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrintPDF.Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OfferOutHeader_DetailView" Then
            Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrint.Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Opportunities_OfferOutHeader_ListView" Then
            Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutPrintVC).aOffersOutPrint.Active.SetItemValue("", False)
        End If
    End Sub
    Public Sub aOffersOutPrint_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOffersOutPrint.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader
        Dim _ReportData As ReportData

        If e.SelectedObjects.Count > 0 Then
            _OfferOutHeader = TryCast(e.SelectedObjects(0), OfferOutHeader)

            If _OfferOutHeader IsNot Nothing Then
                If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem IsNot Nothing Then
                    If _OfferOutHeader.IsEditable = True Then
                        _OfferOutHeader.RecalculateOfferOut(_OfferOutHeader, _ocur)
                        _ocur.CommitChanges()
                    End If
                    If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem.Data = "0" Then
                        _ReportData = _OfferOutHeader.OfferOutParameters.ReportData

                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("OfferOutHeader=?", _OfferOutHeader), True)
                    End If
                    If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem.Data = "1" Then
                        _ReportData = _OfferOutHeader.OfferOutParameters.ReportData1
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("OfferOutHeader=?", _OfferOutHeader), True)
                    End If
                Else
                    Throw New Exception("*Nincs nyomtatvány típus kiválasztva!")
                End If
            End If
        End If
    End Sub
    Public Sub aOfferOutPrintChoice_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SingleChoiceActionExecuteEventArgs) Handles aOfferOutPrintChoice.Execute
        If e.SelectedObjects.Count > 0 Then
            Dim _OfferOutHeader As OfferOutHeader = TryCast(e.SelectedObjects(0), OfferOutHeader)
            Dim _ReportData As ReportData = Nothing

            If _OfferOutHeader IsNot Nothing Then
                Select Case e.SelectedChoiceActionItem.Data
                    Case 0 'Standard
                        _ReportData = _OfferOutHeader.OfferOutParameters.ReportData
                    Case 1 'Contract
                        _ReportData = _OfferOutHeader.OfferOutParameters.ReportData1
                    Case 2 'Other
                        _ReportData = _OfferOutHeader.OfferOutParameters.ReportData2
                End Select
                'If _ReportData IsNot Nothing Then
                '    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("OfferOutHeader=?", _OfferOutHeader),true)
                'End If
            End If
        End If
    End Sub

    Private Sub aOffersOutPrintPDF_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOffersOutPrintPDF.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _ReportData As ReportData = Nothing

        If _OfferOutHeader IsNot Nothing Then
            If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem IsNot Nothing Then
                If _OfferOutHeader.IsEditable = True Then
                    _OfferOutHeader.RecalculateOfferOut(_OfferOutHeader, _ocur)
                    _ocur.CommitChanges()
                End If
                If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem.Data = "0" Then
                    _ReportData = _OfferOutHeader.OfferOutParameters.ReportData
                End If
                If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem.Data = "1" Then
                    _ReportData = _OfferOutHeader.OfferOutParameters.ReportData1
                End If
            Else
                Throw New Exception("*Nincs nyomtatvány típus kiválasztva!")
            End If

            If _ReportData IsNot Nothing Then
                Dim _XafReport As XafReport = _ReportData.LoadReport(_ocur)
                Dim _FilterString As String = "[OfferOutHeader.Oid]='" & _OfferOutHeader.Oid.ToString & "'"
                _XafReport.FilterString = _FilterString
                Dim _FileName As String = Path.GetTempPath & CorrectFileName(_OfferOutHeader.DocumentNumber) & ".pdf"
                Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

                _XafReport.ExportToPdf(_StreamFile)
                _StreamFile.Close()

                _StreamFile = New FileStream(_FileName, FileMode.Open, FileAccess.Read)
                Dim _StreamReader As New StreamReader(_StreamFile)
                _StreamFile.Seek(0, SeekOrigin.Begin)
                _OfferOutHeader.PDF_Original = New FileData(_ocur.Session)
                _OfferOutHeader.PDF_Original.LoadFromStream(_FileName, _StreamFile)
                _OfferOutHeader.PDF_Original.FileName = CorrectFileName(_OfferOutHeader.DocumentNumber) & ".pdf"

                _StreamReader.Close()
                _StreamFile.Close()

                _ocur.CommitChanges()

                If File.Exists(_FileName) Then File.Delete(_FileName)
            End If
        End If
    End Sub

    
End Class
