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

Public Class StorageComputedVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        '------------------------- LISTVIEWS --------------------------------------------------------------------------------------------------------------------------------------
        If View.Id Like "StorageComputed_LookupListView" Then '-- StorageComputed csak programból állítható elő !!!
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", False)
            'Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If

        '------------------------- DETAILVIEWS ------------------------------------------------------------------------------------------------------------------------------------
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        '----------------- LISTVIEWS ----------------------------------------------------------------------------------------------------------------------------
        If View.Id Like "StorageComputed_LookupListView" Then '-- StorageComputed csak programból állítható elő !!!
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", True)
            'Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        '----------------- DETAILVIEWS --------------------------------------------------------------------------------------------------------------------------

        '----------------- OTHERVIEWS ---------------------------------------------------------------------------------------------------------------------------

        If View.Id Like "Storage*Header_*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ModificationsController)().Active.SetItemValue("", True)
        End If
        If View.Id Like "Products*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aViewStorageMoveInfo_StorageComputed_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewStorageMoveInfo_StorageComputed.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StorageComputed As StorageComputed = TryCast(View.SelectedObjects(0), StorageComputed)
        Dim _StorageMoveInfo As StorageMoveInfo
        If _StorageComputed IsNot Nothing Then
            _StorageMoveInfo = _onew.CreateObject(Of StorageMoveInfo)()
            _StorageMoveInfo.Products = _onew.GetObject(_StorageComputed.Products)
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "StorageMoveInfo_DetailView", False, _StorageMoveInfo)
        End If
    End Sub

    Private Sub aViewStorageMoveInfo_StorageOutCirculation_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewStorageMoveInfo_StorageOutCirculation.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StorageOutCirculation As StorageOutCirculation = TryCast(View.SelectedObjects(0), StorageOutCirculation)
        Dim _StorageMoveInfo As StorageMoveInfo
        If _StorageOutCirculation IsNot Nothing Then
            _StorageMoveInfo = _onew.CreateObject(Of StorageMoveInfo)()
            _StorageMoveInfo.Products = _onew.GetObject(_StorageOutCirculation.Products)
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "StorageMoveInfo_DetailView", False, _StorageMoveInfo)
        End If
    End Sub

    Private Sub aGenerateStorageComputedPrice_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateStorageComputedPrice.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StorageComputed As StorageComputed

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _StorageComputed In View.SelectedObjects
            RaiseEvent DoWork()
            _StorageComputed.RecalculateAveragePrice()
            _StorageComputed.RecalculateMinimumPrice()
            _StorageComputed.RecalculateSumFIFOPriceInHUFFree()
        Next
        _ocur.CommitChanges()
        RaiseEvent FinalWork

    End Sub
    Private Sub aChangeGroup_StorageComputed_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aChangeGroup_StorageComputed.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeGroup_StorageComputed_PopUp As New ChangeGroup_StorageComputed_PopUp(_onew.Session)

            e.View = Application.CreateDetailView(_onew, "ChangeGroup_StorageComputed_PopUp_DetailView", True, _ChangeGroup_StorageComputed_PopUp)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aChangeGroup_StorageComputed_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aChangeGroup_StorageComputed.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _ChangeGroup_StorageComputed_PopUp As ChangeGroup_StorageComputed_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ChangeGroup_StorageComputed_PopUp)

            For Each _StorageComputed As StorageComputed In View.SelectedObjects
                If _ChangeGroup_StorageComputed_PopUp.Change_PricingMinimum Then
                    _StorageComputed.PricingMinimum = _ocur.GetObject(_ChangeGroup_StorageComputed_PopUp.PricingMinimum)
                End If
            Next

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aStorageComputed_Generate_QTT_FIFO_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aStorageComputed_Generate_QTT_FIFO.Execute
        Try
            GLOBAL_HAS_Audit = False
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
            For Each _StorageComputed As StorageComputed In View.SelectedObjects
                RaiseEvent DoWork()
                Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_ocur.Session, CriteriaOperator.Parse("Storage=? AND Products=? AND StorageInHeader.IsStorno=False AND StorageInHeader.IsEditable=False", _
                                                                                                           _StorageComputed.Storage, _StorageComputed.Products))
                '  If _StorageInRows_Collection.Count > 0 Then
                Dim _SIR_QTT As Double = 0
                Dim _SIR_OTT_Out As Double = 0

                For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                    _SIR_QTT += _StorageInRows.QTT
                    _SIR_OTT_Out += _StorageInRows.QTT_Out
                Next

                _StorageComputed.QTT_FIFO = _SIR_QTT - _SIR_OTT_Out
                'End If
            Next
            _ocur.CommitChanges()
        Catch ex As NullReferenceException
            MsgBox("Már megint valami NULL!", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        Finally
            GLOBAL_HAS_Audit = True
            RaiseEvent FinalWork
        End Try
    End Sub
End Class

