Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Text
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports System.Collections.Generic
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Templates
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Model.NodeGenerators
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.Persistent.BaseImpl
Imports System.ComponentModel

' For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
Partial Public Class InvoiceFactoringVC
    Inherits ViewController
    Private _SpreadsheetControl As SpreadsheetControl
    Public Sub New()
        InitializeComponent()
        RegisterActions(components)
        ' Target required Views (via the TargetXXX properties) and create their Actions.
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ' Perform various tasks depending on the target View.
        If View.Id = "InvoiceFactoringHeader_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _SpreadXML_ViewItem As ViewItem = _DetailView.FindItem("SpreadXML")
            If _SpreadXML_ViewItem IsNot Nothing Then
                AddHandler _SpreadXML_ViewItem.ControlCreated, AddressOf _SpreadXML_ViewItem_ControlCreated

                AddHandler Frame.GetController(Of ModificationsController).SaveAction.Executing, AddressOf SaveAction_Executing
                AddHandler Frame.GetController(Of ModificationsController).SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            End If
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        ' Access and customize the target View control.
    End Sub
    Protected Overrides Sub OnDeactivated()
        ' Unsubscribe from previously subscribed events and release other references and resources.
        MyBase.OnDeactivated()
    End Sub
    Private Sub _SpreadXML_ViewItem_ControlCreated(sender As Object, e As EventArgs)
        'Dim _InvoiceFactoringBank As InvoiceFactoringBank = TryCast(View.SelectedObjects(0), InvoiceFactoringBank)
        'Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        'Dim _SpreadUserControl As SpreadUserControl = TryCast(_ItemControl, SpreadUserControl)

        '_SpreadsheetControl = TryCast(_ItemControl, SpreadUserControl).SpreadsheetControl1

        'If _InvoiceFactoringBank IsNot Nothing Then
        '    If _InvoiceFactoringBank.SpreadFile IsNot Nothing Then
        '        Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), _InvoiceFactoringBank.SpreadFile.FileName)
        '        Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
        '        _InvoiceFactoringBank.SpreadFile.SaveToStream(_Stream)
        '        _Stream.Close()

        '        Dim _StreamFile = New System.IO.FileStream(_FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        '        Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
        '        Dim workbook As DevExpress.Spreadsheet.IWorkbook = _SpreadsheetControl.Document
        '        _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)
        '        workbook.LoadDocument(_StreamFile, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
        '        _StreamFile.Close()
        '    End If
        'End If
        'Dim _FactoringSpreadCustomFunction As New FactoringSpreadCustomFunction
        'Dim _workbook As DevExpress.Spreadsheet.IWorkbook = _SpreadsheetControl.Document

        'If (Not _workbook.CustomFunctions.Contains(_FactoringSpreadCustomFunction.Name)) Then
        '    _workbook.CustomFunctions.Add(_FactoringSpreadCustomFunction)
        'End If

    End Sub
    Private Sub SaveAction_Executing(sender As Object, e As CancelEventArgs)
        'Dim _InvoiceFactoringBank As InvoiceFactoringBank = TryCast(View.SelectedObjects(0), InvoiceFactoringBank)
        'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        'If _SpreadsheetControl IsNot Nothing Then

        '    Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "FACTORING.XML")
        '    Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
        '    _SpreadsheetControl.Document.SaveDocument(_Stream, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
        '    _Stream.Close()

        '    Dim _StreamFile = New System.IO.FileStream(_FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        '    Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
        '    _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)

        '    Dim _FileData As FileData
        '    _FileData = _ocur.CreateObject(Of FileData)()

        '    _FileData.LoadFromStream("FACTORING.XML", _StreamFile)

        '    _InvoiceFactoringBank.SpreadFile = _FileData
        'End If
    End Sub
End Class
