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 FarPoint.Win.Spread
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports System.IO
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Public Class InventoryVC
    Inherits Nuvolar.Module.InventoryVC

    Private _FpSpread As FpSpread
    Dim _WaitFormClass As New WaitFormClass

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.Initwork
        AddHandler FinalWork, AddressOf _WaitFormClass.Finalwork

        If View.Id = "InventoryHeader_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)

            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing

            Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)
            AddHandler _InventoryHeader.Changed, AddressOf InventoryHeader_DetailView_SetWorksheetTemplateXLS_Changed

        End If
        If View.Id = "InventoryParameters_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing


        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.Initwork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.Finalwork

        If View.Id = "InventoryHeader_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)

            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
        End If
        If View.Id = "InventoryParameters_DetailView" Then
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf InventoryHeader_DetailView_SaveAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "InventoryHeader_DetailView" Or View.Id = "InventoryParameters_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _ExcelControlViewItem As ViewItem
            If _DetailView IsNot Nothing Then
                If _DetailView.GetItems(Of ExcelPropertyEditor).Count > 0 Then
                    _ExcelControlViewItem = _DetailView.GetItems(Of ExcelPropertyEditor)(0)
                    If _ExcelControlViewItem IsNot Nothing Then
                        AddHandler _ExcelControlViewItem.ControlCreated, AddressOf ExcelControlViewItem_ControlCreated
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub InventoryHeader_DetailView_SaveAction_Executing(sender As Object, e As CancelEventArgs)
        Dim _FileName As String = Path.GetTempPath & "InSpread.xml"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        If View.Id = "InventoryHeader_DetailView" Then
            Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)
            If _InventoryHeader IsNot Nothing Then
                If _FpSpread IsNot Nothing Then
                    If Not String.IsNullOrEmpty(_InventoryHeader.DocumentNumber) Then
                        _FpSpread.Save(_StreamFile, False)
                        _StreamFile.Close()
                        Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                        _InventoryHeader.ExcelFile = New FileData(_ocur.Session)
                        _InventoryHeader.ExcelFile.LoadFromStream("Excel worksheet.xls", _StreamFile2)
                        _InventoryHeader.Save()
                        _StreamFile2.Close()
                    End If
                End If
            End If
        End If
        If View.Id = "InventoryParameters_DetailView" Then
            Dim _InventoryParameters As InventoryParameters = TryCast(View.SelectedObjects(0), InventoryParameters)
            If _InventoryParameters IsNot Nothing Then
                If _FpSpread IsNot Nothing Then
                    _FpSpread.Save(_StreamFile, False)
                    _StreamFile.Close()
                    Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                    _InventoryParameters.ExcelFile = New FileData(_ocur.Session)
                    _InventoryParameters.ExcelFile.LoadFromStream("Excel worksheet.xls", _StreamFile2)
                    _InventoryParameters.Save()
                    _StreamFile2.Close()
                End If
            End If
        End If

    End Sub
    Private Sub ExcelControlViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim itemControl As Object = (CType(sender, ViewItem)).Control

        _FpSpread = TryCast(itemControl, ExcelUserControl).FpSpreadControl

        Dim _FileName As String = Path.GetTempPath & "OutSpread.xml"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        If View.Id = "InventoryHeader_DetailView" Then
            Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If Not _ocur.Session.IsNewObject(_InventoryHeader) Then
                If _InventoryHeader IsNot Nothing Then
                    If _InventoryHeader.ExcelFile IsNot Nothing Then
                        _InventoryHeader.ExcelFile.SaveToStream(_StreamFile)
                        _StreamFile.Close()
                        _FpSpread.Open(_FileName)
                    End If
                End If
            End If
        End If
        If View.Id = "InventoryParameters_DetailView" Then
            Dim _InventoryParameters As InventoryParameters = TryCast(View.SelectedObjects(0), InventoryParameters)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If Not _ocur.Session.IsNewObject(_InventoryParameters) Then
                If _InventoryParameters IsNot Nothing Then
                    If _InventoryParameters.ExcelFile IsNot Nothing Then
                        _InventoryParameters.ExcelFile.SaveToStream(_StreamFile)
                        _StreamFile.Close()
                        _FpSpread.Open(_FileName)
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub InventoryHeader_DetailView_SetWorksheetTemplateXLS_Changed(sender As Object, e As DevExpress.Xpo.ObjectChangeEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)

        If _InventoryHeader.ExcelFile Is Nothing Then
            If _InventoryHeader.InventoryParameters.ExcelFile IsNot Nothing Then

                _InventoryHeader.ExcelFile = _InventoryHeader.InventoryParameters.ExcelFile
                If _InventoryHeader.ExcelFile IsNot Nothing Then

                    Dim _FileName As String = Path.GetTempPath & "Temp.xls"
                    Dim _StreamFile As FileStream = Nothing
                    _StreamFile = File.Create(_FileName)

                    _InventoryHeader.ExcelFile.SaveToStream(_StreamFile)
                    If TryCast(View, DetailView).GetItems(Of ExcelPropertyEditor)(0).ExcelUserControl IsNot Nothing Then
                        _FpSpread = TryCast(View, DetailView).GetItems(Of ExcelPropertyEditor)(0).ExcelUserControl.FpSpreadControl
                        _StreamFile.Close()
                        _FpSpread.OpenExcel(_FileName)
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub aInventoryParameters_CreateProductsListInSpread_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInventoryParameters_CreateProductsListInSpread.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Products_Collection As New XPCollection(Of Products)(_ocur.Session, CriteriaOperator.Parse("DangerClass<>0"))
        _Products_Collection.Sorting.Add(New SortProperty("ShortName", DB.SortingDirection.Ascending))

        If _FpSpread IsNot Nothing Then
            _FpSpread.Reset()

            _FpSpread.ActiveSheet.ColumnCount = 11
            _FpSpread.ActiveSheet.RowCount = 4 + _Products_Collection.Count
            _FpSpread.AllowDragFill = True
            _FpSpread.AllowColumnMove = True
            _FpSpread.AllowRowMove = True

            LoadXLSHeader()

            _FpSpread.ActiveSheet.Columns(1).AllowAutoFilter = True
            '_FpSpread.ActiveSheet.GroupBarInfo.Visible = True
            '_FpSpread.ActiveSheet.AllowGroup = True

            Dim _RowIndex As Integer = 4 '5. sor
            For Each _Products As Products In _Products_Collection
                _FpSpread.ActiveSheet.Cells(_RowIndex, 0).Text = _Products.Oid.ToString
                _FpSpread.ActiveSheet.Cells(_RowIndex, 1).Text = _Products.ShortName.Replace(vbNewLine, " ")

                _RowIndex += 1
            Next

            _FpSpread.Refresh()
        End If
    End Sub
    Private Sub LoadXLSHeader()
        _FpSpread.ActiveSheet.AddSpanCell(0, 0, 1, 11)
        With _FpSpread.ActiveSheet.Cells(0, 0)
            .Text = "A/2 adatlap: ÜZEMADATOK-VESZÉLYES ANYAGOK"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Center
        End With

        _FpSpread.ActiveSheet.AddSpanCell(1, 0, 2, 1)
        With _FpSpread.ActiveSheet.Cells(1, 0)
            .Text = "Termék OID"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
            .Row.Height = 40
        End With

        _FpSpread.ActiveSheet.AddSpanCell(1, 1, 2, 1)
        With _FpSpread.ActiveSheet.Cells(1, 1)
            .Text = "A nem nevesített veszélyes anyag megnevezése (az 1. melléklet 2. táblázat alapján)"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With

        _FpSpread.ActiveSheet.AddSpanCell(1, 2, 1, 4)
        With _FpSpread.ActiveSheet.Cells(1, 2)
            .Text = "Nemzetközileg elfogadott egyértelmű azonosítása (CAS-szám, szükség szerint IUPAC név, kereskedelmi megnevezés, empirikus formula)"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With

        _FpSpread.ActiveSheet.AddSpanCell(1, 6, 1, 3)
        With _FpSpread.ActiveSheet.Cells(1, 6)
            .Text = "A veszélyes anyag 1. melléklet 2. táblázat 1. oszlopa szerinti veszélyességi osztályba sorolása az R mondatok és az ADR szerinti osztályozás feltüntetésével"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With

        _FpSpread.ActiveSheet.AddSpanCell(1, 9, 2, 1)
        With _FpSpread.ActiveSheet.Cells(1, 9)
            .Text = "Jelen lévő maximális mennyisége (tonna)"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With

        _FpSpread.ActiveSheet.AddSpanCell(1, 10, 2, 1)
        With _FpSpread.ActiveSheet.Cells(1, 10)
            .Text = "A besorolásnál figyelembe vett küszöbmennyiség (tonna)"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With

        With _FpSpread.ActiveSheet.Cells(2, 2)
            .Text = "CAS szám"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
            .Row.Height = 40
        End With
        With _FpSpread.ActiveSheet.Cells(2, 3)
            .Text = "IUPAC név"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With
        With _FpSpread.ActiveSheet.Cells(2, 4)
            .Text = "kereskedelmi megnevezés"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With
        With _FpSpread.ActiveSheet.Cells(2, 5)
            .Text = "empirikus formula"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With
        With _FpSpread.ActiveSheet.Cells(2, 6)
            .Text = "R mondatok"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With
        With _FpSpread.ActiveSheet.Cells(2, 7)
            .Text = "ADR osztály"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With
        With _FpSpread.ActiveSheet.Cells(2, 8)
            .Text = "Az 1. melléklet 2. táblázat 1. oszlopa szerinti osztályba sorolás"
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Justify
        End With

        For ik = 0 To 5
            With _FpSpread.ActiveSheet.Cells(3, ik)
                .Text = ik.ToString + "."
                .HorizontalAlignment = CellHorizontalAlignment.Center
                .VerticalAlignment = CellVerticalAlignment.Center
            End With
        Next

        For ik = 9 To 10
            With _FpSpread.ActiveSheet.Cells(3, ik)
                .Text = (ik - 2).ToString + "."
                .HorizontalAlignment = CellHorizontalAlignment.Center
                .VerticalAlignment = CellVerticalAlignment.Center
            End With
        Next

        _FpSpread.ActiveSheet.AddSpanCell(3, 6, 1, 3)
        With _FpSpread.ActiveSheet.Cells(3, 6)
            .Text = "6."
            .HorizontalAlignment = CellHorizontalAlignment.Center
            .VerticalAlignment = CellVerticalAlignment.Center
        End With

    End Sub
    Private Sub aInventoryHeader_LoadSpreadFromParameters_Execute_1(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInventoryHeader_LoadSpreadFromParameters.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)

        If _InventoryHeader IsNot Nothing Then
            If _InventoryHeader.InventoryParameters IsNot Nothing Then
                If _InventoryHeader.InventoryParameters.ExcelFile IsNot Nothing Then
                    _InventoryHeader.ExcelFile = _InventoryHeader.InventoryParameters.ExcelFile
                End If
            End If
        End If

        _ocur.CommitChanges()

        Dim _FileName As String = Path.GetTempPath & "InSpread.xls"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        _InventoryHeader.ExcelFile.SaveToStream(_StreamFile)
        _StreamFile.Close()

        _FpSpread.Open(_FileName)
        _FpSpread.Refresh()

    End Sub
    Private Sub aInventoryHeader_UpdateInventorySpread_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInventoryHeader_UpdateInventorySpread.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InventoryHeader As InventoryHeader = TryCast(View.SelectedObjects(0), InventoryHeader)

        If _FpSpread IsNot Nothing Then
            _WaitFormClass.Initwork(1, 1, _FpSpread.ActiveSheet.RowCount - 1)
            For ik = 4 To _FpSpread.ActiveSheet.RowCount - 1
                _WaitFormClass.DoWork()
                Dim _InventorySC As InventorySC = _ocur.FindObject(Of InventorySC)(CriteriaOperator.Parse("InventoryHeader=? AND Products.Oid=?", _InventoryHeader, _FpSpread.ActiveSheet.Cells(ik, 0).Text))
                If _InventorySC IsNot Nothing Then
                    Select Case UCase(_InventorySC.Products.Units.ShortName)
                        Case "KG"
                            _FpSpread.ActiveSheet.Cells(ik, 9).Value = Format(_InventorySC.QTT / 1000, "#,##0.0000")
                        Case "T"
                            _FpSpread.ActiveSheet.Cells(ik, 9).Value = Format(_InventorySC.QTT, "#,##0.0000")
                        Case "L", "I"
                            _FpSpread.ActiveSheet.Cells(ik, 9).Value = Format(_InventorySC.QTT / 1000, "#,##0.0000")
                        Case Else
                            _FpSpread.ActiveSheet.Cells(ik, 9).Value = Format(0, "#,##0.0000")
                    End Select
                End If
            Next
            _FpSpread.Refresh()
            _Waitformclass.FinalWork()
        End If
    End Sub
End Class
