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.Persistent.Validation
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors

Public Class StoragePDACollectionHeaderVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of StoragePDACollectionHeaderVC)().aInsertStoragePDARows.Active.SetItemValue("", False)
        Frame.GetController(Of StoragePDACollectionHeaderVC)().aDeleteStoragePDARows.Active.SetItemValue("", False)

        If View.Id = "StoragePDACollectionHeader_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "StoragePDACollectionHeader_StoragePDACollectionRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC)().aGetAuditTrail.Active.SetItemValue("", False)
        End If

        '--------------------------DetailViewZ----------------------------------------
        If View.Id = "StoragePDACollectionHeader_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of StoragePDACollectionHeaderVC)().aInsertStoragePDARows.Active.SetItemValue("", True)
            Frame.GetController(Of StoragePDACollectionHeaderVC)().aDeleteStoragePDARows.Active.SetItemValue("", True)
        End If
        If View.Id = "StorageInFromPDACollectionPopup_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "StoragePDACollectionHeader_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "StoragePDACollectionHeader_StoragePDACollectionRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC)().aGetAuditTrail.Active.SetItemValue("", True)
        End If

        '--------------------------DetailViewZ----------------------------------------
        If View.Id = "StoragePDACollectionHeader_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of StoragePDACollectionHeaderVC)().aInsertStoragePDARows.Active.SetItemValue("", False)
            Frame.GetController(Of StoragePDACollectionHeaderVC)().aDeleteStoragePDARows.Active.SetItemValue("", False)
        End If
        If View.Id = "StorageInFromPDACollectionPopup_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
    End Sub
    Private Sub AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, DefaultContexts.Save)
    End Sub

    Private Sub aInsertStoragePDARows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInsertStoragePDARows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StoragePDACollectionRows As StoragePDACollectionRows = _onew.CreateObject(Of StoragePDACollectionRows)()

        e.View = Application.CreateDetailView(_onew, "StoragePDACollectionRows_DetailView", False, _StoragePDACollectionRows)
    End Sub
    Private Sub aInsertStoragePDARows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInsertStoragePDARows.Execute

        Try
            Dim _ocur As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            Dim _StoragePDACollectionRows_PopUp As StoragePDACollectionRows = TryCast(e.PopupWindow.View.SelectedObjects(0), StoragePDACollectionRows)
            If _StoragePDACollectionRows_PopUp Is Nothing Then Throw New Exception("*")
            Dim _StoragePDACollectionRows As StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
            _StoragePDACollectionRows.StoragePDACollectionHeader = _ocur.GetObject(_StoragePDACollectionHeader)
            _StoragePDACollectionRows.Products = _ocur.GetObject(_StoragePDACollectionRows_PopUp.Products)
            _StoragePDACollectionRows.QTT = _StoragePDACollectionRows_PopUp.QTT
            _StoragePDACollectionRows.Description = _StoragePDACollectionRows_PopUp.Description

            _ocur.CommitChanges()

            Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
            If nestedFrame IsNot Nothing Then
                Dim parentView As View = nestedFrame.View
                parentView.ObjectSpace.Refresh()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try

    End Sub
    Private Sub aDeleteStoragePDARows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDeleteStoragePDARows.Execute
        Try
            Dim _StoragePDACollectionRows_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("StoragePDACollectionRows")
            If _StoragePDACollectionRows_ListEditor Is Nothing Then Throw New Exception("*Váratlan hiba történt, művelet megszakadt!")
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _StoragePDACollectionRows_DEL_Collection As New ArrayList
            For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionRows_ListEditor.ListView.SelectedObjects
                _StoragePDACollectionRows_DEL_Collection.Add(_StoragePDACollectionRows)
            Next
            _ocur.Delete(_StoragePDACollectionRows_DEL_Collection)
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub

    Private Sub aLocatePDACollectionIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aLocatePDACollectionIn.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            If _StoragePDACollectionHeader Is Nothing Then Throw New Exception("*Nincs kijelölve egyetlen PDA kollekció sem!")
            If _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eClosed Then Throw New Exception("*A kollekció már fel lett dolgozva!")
            If _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eOpen Then Throw New Exception("*A kollekció még nem lett lokalizálva!")
            Dim _StorageComputedLocations As StorageComputedLocations = Nothing

            For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionHeader.StoragePDACollectionRows
                If _StoragePDACollectionRows.StorageLocation IsNot Nothing Then
                    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed) _
                                       (CriteriaOperator.Parse("Storage=? AND Products=?", _StoragePDACollectionRows.StorageLocation.Storage, _StoragePDACollectionRows.Products))

                    If _StorageComputed Is Nothing Then Throw New Exception("*Végzetes hiba történt, a folymat megszakadt!")

                    If _StoragePDACollectionRows.Products.ProductType = eProductType.ePacketNumber Or _
                        _StoragePDACollectionRows.Products.ProductType = eProductType.eSerialNumber Then

                        _StorageComputedLocations = _ocur.CreateObject(Of StorageComputedLocations)()

                        _StorageComputedLocations.StorageComputed = _StorageComputed
                        _StorageComputedLocations.QTT = _StoragePDACollectionRows.QTT
                        _StorageComputedLocations.StorageLocation = _StoragePDACollectionRows.StorageLocation

                        _StorageComputedLocations.SerialNumber = _StoragePDACollectionRows.SerialNumber
                        _StorageComputedLocations.PacketNumber = _StoragePDACollectionRows.PacketNumber

                    Else
                        _StorageComputedLocations = _ocur.FindObject(Of StorageComputedLocations)(CriteriaOperator.Parse("StorageComputed=? AND StorageLocation=?", _
                                                                                                                   _StorageComputed, _StoragePDACollectionRows.StorageLocation))
                        If _StorageComputedLocations Is Nothing Then
                            _StorageComputedLocations = _ocur.CreateObject(Of StorageComputedLocations)()

                            _StorageComputedLocations.StorageComputed = _StorageComputed
                            _StorageComputedLocations.QTT = _StoragePDACollectionRows.QTT
                            _StorageComputedLocations.StorageLocation = _StoragePDACollectionRows.StorageLocation
                        Else
                            _StorageComputedLocations.QTT += _StoragePDACollectionRows.QTT
                        End If

                    End If

                End If
            Next
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eClosed
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aLocatePDACollectionOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aLocatePDACollectionOut.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            If _StoragePDACollectionHeader Is Nothing Then Throw New Exception("*Nincs kijelölve egyetlen PDA kollekció sem!")

            'For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionHeader.StoragePDACollectionRows
            '    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed) _
            '                               (CriteriaOperator.Parse("Storage=? AND Products=?", _StoragePDACollectionRows.StorageLocation.Storage, _StoragePDACollectionRows.Products))
            '    Dim _StorageComputedLocations As StorageComputedLocations = _ocur.FindObject(Of StorageComputedLocations)(CriteriaOperator.Parse("StorageLocation=? AND StorageComputed=?", _StoragePDACollectionRows.StorageLocation, _StorageComputed))

            '    ' _StorageComputedLocations.QTT -= _StoragePDACollectionRows.QTT
            'Next
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eClosed
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
End Class
