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.Xpo
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Persistent.BaseImpl



Public Class RecipeExecutionSimpleVC
    Inherits DevExpress.ExpressApp.ViewController

    Dim _RecipeExecutionFrom_Collection As New ArrayList
    Dim _RecipeExecutionTo_Collection As New ArrayList
    Dim _VaildData As Boolean = True

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "RecipeTo_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "RecipeFrom_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "RecipeTo_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "RecipeFrom_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aRecipeExecutionFrom_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aRecipeExecutionFrom.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RecipeExecutionSimple As RecipeExecutionSimple = TryCast(View.SelectedObjects(0), RecipeExecutionSimple)
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(RecipeExecutionSimplePopupFrom))
        _RecipeExecutionFrom_Collection.Clear()
        Dim _RecipeExecutionSimplePopupFrom As RecipeExecutionSimplePopupFrom

        For Each _RecipeFrom In _RecipeExecutionSimple.Recipe.RecipeFrom
            _RecipeExecutionSimplePopupFrom = _onew.CreateObject(Of RecipeExecutionSimplePopupFrom)()

            _RecipeExecutionSimplePopupFrom.RecipeFrom = _onew.GetObject(_RecipeFrom)
            _RecipeExecutionSimplePopupFrom.StorageComputed = _onew.FindObject(Of StorageComputed) _
                        (CriteriaOperator.Parse("Products=? And Storage=?", _onew.GetObject(_RecipeFrom.Products), _onew.GetObject(_RecipeFrom.Storage)))

            If _RecipeExecutionSimplePopupFrom.StorageComputed Is Nothing Then
                _VaildData = False
            Else
                _RecipeExecutionSimplePopupFrom.QTTNeed = _RecipeExecutionSimple.QTT * _RecipeFrom.QTT
                _RecipeExecutionSimplePopupFrom.QTTDifferent = _RecipeExecutionSimplePopupFrom.StorageComputed.QTTFree - _RecipeExecutionSimplePopupFrom.QTTNeed
                If _RecipeExecutionSimplePopupFrom.QTTDifferent < 0 Then
                    _VaildData = False
                End If
                _RecipeExecutionFrom_Collection.Add(_RecipeExecutionSimplePopupFrom)
                _CS.Add(_RecipeExecutionSimplePopupFrom)
            End If
        Next

        e.View = Application.CreateListView("RecipeExecutionSimplePopupFrom_ListView", _CS, False)
    End Sub
    Private Sub aRecipeExecutionFrom_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aRecipeExecutionFrom.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
        Try

            If _VaildData = False Then Throw New UserFriendlyException("Nem kivitelezhető a gyártás, mert egy vagy több alapanyag nem áll rendelkezésre!")

            '--==Zárolás==--
            '____________________________________________________________________________________________________________________
            If Not LockingModule.LockObject(_uow_lock, GetType(StorageComputed), "-", _User) Then
                Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("TableName=?", GetType(StorageComputed).Name + "-" + "-"))

                Dim _Prompt As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\StorageComputed", "CanNotStorageInByLocking")
                Dim _Title As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxError")
                MsgBox(String.Format(_Prompt, _LockingObject.User.FullName, _LockingObject.ObjectCreated), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, _Title)

                Exit Sub
            Else
                Dim _RecipeExecutionSimple As RecipeExecutionSimple = TryCast(View.SelectedObjects(0), RecipeExecutionSimple)
                '----------------------------------------------------------
                'Raktárak feltérképezése, kitárolási fejléc(ek) összerakása
                '----------------------------------------------------------
                Dim _StorageOutHeader_Collection As New ArrayList '<== Kitároláso fejlécEK kollekciója, annyi kitárolási fejléc, AHÁNY KÜLÖNBÖZŐ raktár van!!!

                For Each _RecipeFrom As RecipeFrom In _RecipeExecutionSimple.Recipe.RecipeFrom
                    If _StorageOutHeader_Collection.Count = 0 Then
                        Dim _StorageOutHeader As New StorageOutHeader(_ocur.Session)
                        With _StorageOutHeader
                            .Storage = _ocur.GetObjectByKey(Of Storage)(_RecipeFrom.Storage.Oid)
                            .CustomersFrom = _ocur.GetObjectByKey(Of CustomersFrom)(_RecipeExecutionSimple.CustomersFrom.Oid)
                            .RecipeExecutionSimple = _RecipeExecutionSimple
                            .Customers = _ocur.GetObjectByKey(Of Customers)(TryCast(.Storage.CustomersFrom, Customers).Oid)
                            .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                        End With
                        _StorageOutHeader_Collection.Add(_StorageOutHeader)
                    Else
                        For Each _StorageOutHeaderExist As StorageOutHeader In _StorageOutHeader_Collection
                            Dim _NewStorage As Boolean = False
                            If _StorageOutHeaderExist.Storage.Oid <> _RecipeFrom.Storage.Oid Then
                                _NewStorage = True
                            End If
                            If _NewStorage Then
                                Dim _StorageOutHeader As New StorageOutHeader(_ocur.Session)
                                With _StorageOutHeader
                                    .Storage = _ocur.GetObjectByKey(Of Storage)(_RecipeFrom.Storage.Oid)
                                    .Customers = _ocur.GetObjectByKey(Of CustomersFrom)(_RecipeExecutionSimple.CustomersFrom.Oid)
                                    .RecipeExecutionSimple = _RecipeExecutionSimple
                                    .Customers = _ocur.GetObjectByKey(Of Customers)(TryCast(.Storage.CustomersFrom, Customers))
                                    .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                                End With
                                _StorageOutHeader_Collection.Add(_StorageOutHeader)
                            End If
                            Exit For
                        Next
                    End If
                Next

                '----------------------------------------------------------
                'Kitárolási sorok kitárolási fejlécekzeh rendelése, StorageComputed toszingálás
                '----------------------------------------------------------
                For Each _StorageOutHeader As StorageOutHeader In _StorageOutHeader_Collection
                    For Each _RecipeFrom As RecipeFrom In _RecipeExecutionSimple.Recipe.RecipeFrom
                        If _RecipeFrom.Storage.Oid = _StorageOutHeader.Storage.Oid Then
                            Dim _StorageOutRows As New StorageOutRows(_ocur.Session)
                            With _StorageOutRows
                                .StorageOutHeader = _StorageOutHeader
                                .RowIndex = _StorageOutHeader.StorageOutRows.Count
                                .Products = _ocur.GetObjectByKey(Of Products)(_RecipeFrom.Products.Oid)
                            End With

                            Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                           CriteriaOperator.Parse("StorageInHeader.Storage=? and StorageInHeader.IsEditable=False and StorageInHeader.IsStorno=False and Products=?", _
                           _StorageOutHeader.Storage, _ocur.GetObjectByKey(Of Products)(_RecipeFrom.Products.Oid)))
                            _StorageInRows_Collection.Sorting.Add(New SortProperty("StorageInHeader.CreateDate", DB.SortingDirection.Ascending))

                            Dim _QTT_Mod As Double = _RecipeFrom.QTT * _RecipeExecutionSimple.QTT
                            For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                                Dim _QTT_Free As Double = _StorageInRows.QTT - _StorageInRows.QTT_Out - _StorageInRows.QTT_Out_Will
                                If _QTT_Free > 0 Then
                                    If _QTT_Free < _QTT_Mod Then
                                        _StorageInRows.QTT_Out_Will += _QTT_Free
                                        _StorageOutRows.QTT_Will += _QTT_Free
                                        _QTT_Mod -= _QTT_Free
                                        '------- FIFO sor beszúrása
                                        Dim _StorageOutRowsInRows As StorageOutRowsInRows = _ocur.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                                                                    _StorageInRows, _StorageOutRows), True)
                                        If _StorageOutRowsInRows IsNot Nothing Then
                                            _StorageOutRowsInRows.QTT += _QTT_Free
                                        Else
                                            _StorageOutRowsInRows = New StorageOutRowsInRows(_ocur.Session)
                                            _StorageOutRowsInRows.StorageInRows = _StorageInRows
                                            _StorageOutRowsInRows.StorageOutRows = _StorageOutRows
                                            _StorageOutRowsInRows.QTT = _QTT_Free
                                        End If

                                    Else
                                        _StorageInRows.QTT_Out_Will += _QTT_Mod
                                        _StorageOutRows.QTT_Will += _QTT_Mod
                                        '------- FIFO sor beszúrása
                                        Dim _StorageOutRowsInRows As StorageOutRowsInRows = _ocur.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                                                                _StorageInRows, _StorageOutRows), True)
                                        If _StorageOutRowsInRows IsNot Nothing Then
                                            _StorageOutRowsInRows.QTT += _QTT_Mod
                                        Else
                                            _StorageOutRowsInRows = New StorageOutRowsInRows(_ocur.Session)
                                            _StorageOutRowsInRows.StorageInRows = _StorageInRows
                                            _StorageOutRowsInRows.StorageOutRows = _StorageOutRows
                                            _StorageOutRowsInRows.QTT = _QTT_Mod
                                        End If

                                        Exit For
                                    End If
                                End If
                            Next

                            Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", _
                                                                                                _StorageOutHeader.Storage, _ocur.GetObjectByKey(Of Products)(_RecipeFrom.Products.Oid)), True)
                            Dim _QTT_ReservedOut As Double = _StorageComputed.QTTReservedOut
                            Dim _QTT_Will_Out As Double = 0
                            Dim _StorageOutRows_Collection_R As New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                                                    CriteriaOperator.Parse("QTT_Will>0 and StorageOutHeader.Storage=? and Products=?", _StorageOutHeader.Storage, _ocur.GetObjectByKey(Of Products)(_RecipeFrom.Products.Oid)))
                            For Each _StorageOutRows_R In _StorageOutRows_Collection_R
                                _QTT_Will_Out += _StorageOutRows_R.QTT_Will
                            Next

                            _StorageComputed.QTTFree = _StorageComputed.QTTFree - (_QTT_Will_Out - _QTT_ReservedOut)
                            _StorageComputed.QTTReservedOut = _QTT_Will_Out
                        End If
                    Next
                Next

                '----------------------------------------------------------
                'Kitárolás(ok) véglegesítése, mozgástábla, StorageComputed baszogatás
                '----------------------------------------------------------
                For Each _StorageOutHeader As StorageOutHeader In _StorageOutHeader_Collection

                    _StorageOutHeader.IsEditable = False
                    _StorageOutHeader.IsFinalized = True

                    For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                        Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                          CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))

                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _StorageOutRowsInRows.QTT > 0 Then
                                Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                                                                                    CriteriaOperator.Parse("StorageInRows=?", _StorageOutRowsInRows.StorageInRows))
                                _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("ObjectCreated", DB.SortingDirection.Descending))

                                _StorageOutRowsInRows.StorageInRows.QTT_Out += _StorageOutRowsInRows.QTT
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _StorageOutRowsInRows.QTT

                                Dim _StorageMoveEvents As New StorageMoveEvents(_ocur.Session)
                                _StorageMoveEvents.StorageInRows = _StorageOutRowsInRows.StorageInRows
                                _StorageMoveEvents.StorageOutRows = _StorageOutRowsInRows.StorageOutRows
                                If _StorageMoveEvents_Collection.Count > 0 Then
                                    _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents_Collection(0).QTT_Free_After
                                Else
                                    _StorageMoveEvents.QTT_Free_Before = 0
                                End If
                                _StorageMoveEvents.QTT = (-1) * _StorageOutRowsInRows.QTT
                                _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
                                _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageOutFinal
                                _StorageMoveEvents.ExecutionDate = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate
                            End If
                        Next

                        _StorageOutRows.QTT += _StorageOutRows.QTT_Will

                        Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                                    _StorageOutRows.Products, _StorageOutRows.StorageOutHeader.Storage), True)
                        _StorageComputed.QTTReservedOut -= _StorageOutRows.QTT_Will
                        _StorageOutRows.QTT_Will = 0
                    Next
                Next


                '----------------------------------------------------------
                'Betárolás raktár(ak)ba
                '----------------------------------------------------------
                Dim _StorageInHeader_Collection As New ArrayList

                For Each _RecipeTo As RecipeTo In _RecipeExecutionSimple.Recipe.RecipeTo
                    If _StorageInHeader_Collection.Count = 0 Then
                        Dim _StorageInHeader As New StorageInHeader(_ocur.Session)
                        With _StorageInHeader
                            .Storage = _ocur.GetObjectByKey(Of Storage)(_RecipeTo.Storage.Oid)
                            .RecipeExecutionSimple = _RecipeExecutionSimple
                            .IsEditable = False
                            .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                        End With
                        _StorageInHeader_Collection.Add(_StorageInHeader)
                    Else
                        For Each _StorageInHeaderExist As StorageInHeader In _StorageInHeader_Collection
                            Dim _NewStorage As Boolean = False
                            If _StorageInHeaderExist.Storage.Oid <> _RecipeTo.Storage.Oid Then
                                _NewStorage = True
                            End If
                            If _NewStorage Then
                                Dim _StorageInHeader As New StorageInHeader(_ocur.Session)
                                With _StorageInHeader
                                    .Storage = _onew.GetObjectByKey(Of Storage)(_RecipeTo.Storage.Oid)
                                    .RecipeExecutionSimple = _RecipeExecutionSimple
                                    .IsEditable = False
                                    .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                                End With
                                _StorageInHeader_Collection.Add(_StorageInHeader)
                                Exit For
                            End If
                        Next
                    End If
                Next

                For Each _RecipeTo As RecipeTo In _RecipeExecutionSimple.Recipe.RecipeTo
                    For Each _StorageInHeader As StorageInHeader In _StorageInHeader_Collection
                        If _RecipeTo.Storage.Oid = _StorageInHeader.Storage.Oid Then

                            Dim _StorageInRows As New StorageInRows(_ocur.Session)
                            With _StorageInRows
                                .StorageInHeader = _StorageInHeader
                                .Products = _RecipeTo.Products
                                .QTT_Will = _RecipeTo.QTT * _RecipeExecutionSimple.QTT
                                .RowIndex = _StorageInHeader.StorageInRows.Count
                                .AveragePriceInHUF = _RecipeTo.ListPriceHUF
                            End With

                            Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                                                    _RecipeTo.Products, _StorageInHeader.Storage))
                            If _StorageComputed Is Nothing Then
                                _StorageComputed = New StorageComputed(_ocur.Session)
                                With _StorageComputed
                                    .Storage = _StorageInHeader.Storage
                                    .Products = _RecipeTo.Products
                                    .QTTReservedIn = _RecipeTo.QTT * _RecipeExecutionSimple.QTT
                                End With
                            Else
                                _StorageComputed.QTTReservedIn += _RecipeTo.QTT * _RecipeExecutionSimple.QTT
                            End If
                        End If
                    Next
                Next

                '----------------------------------------------------------
                'Betárolás(ok) véglegeítése
                '----------------------------------------------------------
                For Each _StorageInHeader As StorageInHeader In _StorageInHeader_Collection

                    _StorageInHeader.IsEditable = False
                    _StorageInHeader.IsFinalized = True

                    For Each _StorageInRows As StorageInRows In _StorageInHeader.StorageInRows
                        Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                _ocur.GetObject(_StorageInRows.Products), _ocur.GetObject(_StorageInHeader.Storage)))
                        Dim _StorageMoveEvents As New StorageMoveEvents(_ocur.Session)
                        If _StorageInRows.QTT_Will > 0 Then
                            _StorageInRows.QTT = _StorageInRows.QTT_Will
                            _StorageComputed.QTTFree += _StorageInRows.QTT_Will
                            _StorageComputed.QTTReservedIn -= _StorageInRows.QTT_Will

                            _StorageMoveEvents.StorageInRows = _ocur.GetObject(_StorageInRows)
                            _StorageMoveEvents.QTT = _StorageInRows.QTT_Will
                            _StorageMoveEvents.QTT_Free_After = _StorageComputed.QTTFree
                            _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents.QTT_Free_After - _StorageMoveEvents.QTT
                            _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageInFinal
                            _StorageMoveEvents.ExecutionDate = _StorageInRows.StorageInHeader.CreateDate

                            _StorageInRows.QTT_Will = 0
                        End If
                    Next
                Next

                _ocur.CommitChanges()

                '----------------------------------------------------------
                'Nyomtatványok
                '----------------------------------------------------------
                For Each _StorageOutHeader As StorageOutHeader In _StorageOutHeader_Collection
                    Dim _ReportData As ReportData = _onew.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageOutHeader.Storage.ReportOut.Oid), True)
                    If _ReportData IsNot Nothing Then
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.DocumentNumber=?", _StorageOutHeader.DocumentNumber), True)
                    End If
                Next


                For Each _StorageInHeader As StorageInHeader In _StorageInHeader_Collection
                    Dim _ReportData As ReportData = _onew.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageInHeader.Storage.ReportIn.Oid), True)
                    If _ReportData IsNot Nothing Then
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("StorageInHeader.DocumentNumber =?", _StorageInHeader.DocumentNumber), True)
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
    Private Sub aRecipeExecutionTo_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aRecipeExecutionTo.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RecipeExecutionSimple As RecipeExecutionSimple = TryCast(View.SelectedObjects(0), RecipeExecutionSimple)
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(RecipeExecutionSimplePopupTo))
        _RecipeExecutionTo_Collection.Clear()
        Dim _RecipeExecutionSimplePopupTo As RecipeExecutionSimplePopupTo

        For Each _RecipeTo In _RecipeExecutionSimple.Recipe.RecipeTo
            _RecipeExecutionSimplePopupTo = _onew.CreateObject(Of RecipeExecutionSimplePopupTo)()

            _RecipeExecutionSimplePopupTo.RecipeTo = _onew.GetObject(_RecipeTo)
            _RecipeExecutionSimplePopupTo.StorageComputed = _onew.FindObject(Of StorageComputed) _
                        (CriteriaOperator.Parse("Products=? And Storage=?", _onew.GetObject(_RecipeTo.Products), _onew.GetObject(_RecipeTo.Storage)))
            _RecipeExecutionSimplePopupTo.QTTNeed = _RecipeExecutionSimple.QTT * _RecipeTo.QTT
            _RecipeExecutionSimplePopupTo.QTTDifferent = _RecipeExecutionSimplePopupTo.StorageComputed.QTTFree - _RecipeExecutionSimplePopupTo.QTTNeed
            If _RecipeExecutionSimplePopupTo.QTTDifferent < 0 Then
                _VaildData = False
            End If
            _RecipeExecutionTo_Collection.Add(_RecipeExecutionSimplePopupTo)
            _CS.Add(_RecipeExecutionSimplePopupTo)
        Next

        e.View = Application.CreateListView("RecipeExecutionSimplePopupTo_ListView", _CS, False)
    End Sub
    Private Sub aRecipeExecutionTo_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aRecipeExecutionTo.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
        Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
        Try

            If _VaildData = False Then Throw New UserFriendlyException("Nem kivitelezhető a gyártás, mert egy vagy több alapanyag nem áll rendelkezésre!")

            '--==Zárolás==--
            '____________________________________________________________________________________________________________________
            If Not LockingModule.LockObject(_uow_lock, GetType(StorageComputed), "-", _User) Then
                Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("TableName=?", GetType(StorageComputed).Name + "-" + "-"))

                Dim _Prompt As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions\StorageComputed", "CanNotStorageInByLocking")
                Dim _Title As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxError")
                MsgBox(String.Format(_Prompt, _LockingObject.User.FullName, _LockingObject.ObjectCreated), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, _Title)

                Exit Sub
            Else
                Dim _RecipeExecutionSimple As RecipeExecutionSimple = TryCast(View.SelectedObjects(0), RecipeExecutionSimple)
                '----------------------------------------------------------
                'Raktárak feltérképezése, kitárolási fejléc(ek) összerakása
                '----------------------------------------------------------
                Dim _StorageOutHeader_Collection As New ArrayList '<== Kitároláso fejlécEK kollekciója, annyi kitárolási fejléc, AHÁNY KÜLÖNBÖZŐ raktár van!!!

                For Each _RecipeTo As RecipeTo In _RecipeExecutionSimple.Recipe.RecipeTo
                    If _StorageOutHeader_Collection.Count = 0 Then
                        Dim _StorageOutHeader As New StorageOutHeader(_ocur.Session)
                        With _StorageOutHeader
                            .Storage = _ocur.GetObjectByKey(Of Storage)(_RecipeTo.Storage.Oid)
                            .CustomersFrom = _ocur.GetObjectByKey(Of CustomersFrom)(_RecipeExecutionSimple.CustomersFrom.Oid)
                            .RecipeExecutionSimple = _RecipeExecutionSimple
                            .Customers = _ocur.GetObjectByKey(Of Customers)(TryCast(.Storage.CustomersFrom, Customers).Oid)
                            .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                        End With
                        _StorageOutHeader_Collection.Add(_StorageOutHeader)
                    Else
                        For Each _StorageOutHeaderExist As StorageOutHeader In _StorageOutHeader_Collection
                            Dim _NewStorage As Boolean = False
                            If _StorageOutHeaderExist.Storage.Oid <> _RecipeTo.Storage.Oid Then
                                _NewStorage = True
                            End If
                            If _NewStorage Then
                                Dim _StorageOutHeader As New StorageOutHeader(_ocur.Session)
                                With _StorageOutHeader
                                    .Storage = _ocur.GetObjectByKey(Of Storage)(_RecipeTo.Storage.Oid)
                                    .Customers = _ocur.GetObjectByKey(Of CustomersFrom)(_RecipeExecutionSimple.CustomersFrom.Oid)
                                    .RecipeExecutionSimple = _RecipeExecutionSimple
                                    .Customers = _ocur.GetObjectByKey(Of Customers)(TryCast(.Storage.CustomersFrom, Customers))
                                    .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                                End With
                                _StorageOutHeader_Collection.Add(_StorageOutHeader)
                            End If
                            Exit For
                        Next
                    End If
                Next

                '----------------------------------------------------------
                'Kitárolási sorok kitárolási fejlécekzeh rendelése, StorageComputed toszingálás
                '----------------------------------------------------------
                For Each _StorageOutHeader As StorageOutHeader In _StorageOutHeader_Collection
                    For Each _RecipeTo As RecipeTo In _RecipeExecutionSimple.Recipe.RecipeTo
                        If _RecipeTo.Storage.Oid = _StorageOutHeader.Storage.Oid Then
                            Dim _StorageOutRows As New StorageOutRows(_ocur.Session)
                            With _StorageOutRows
                                .StorageOutHeader = _StorageOutHeader
                                .RowIndex = _StorageOutHeader.StorageOutRows.Count
                                .Products = _ocur.GetObjectByKey(Of Products)(_RecipeTo.Products.Oid)
                            End With

                            Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                           CriteriaOperator.Parse("StorageInHeader.Storage=? and StorageInHeader.IsEditable=False and StorageInHeader.IsStorno=False and Products=?", _
                           _StorageOutHeader.Storage, _ocur.GetObjectByKey(Of Products)(_RecipeTo.Products.Oid)))
                            _StorageInRows_Collection.Sorting.Add(New SortProperty("StorageInHeader.CreateDate", DB.SortingDirection.Ascending))

                            Dim _QTT_Mod As Double = _RecipeTo.QTT * _RecipeExecutionSimple.QTT
                            For Each _StorageInRows As StorageInRows In _StorageInRows_Collection
                                Dim _QTT_Free As Double = _StorageInRows.QTT - _StorageInRows.QTT_Out - _StorageInRows.QTT_Out_Will
                                If _QTT_Free > 0 Then
                                    If _QTT_Free < _QTT_Mod Then
                                        _StorageInRows.QTT_Out_Will += _QTT_Free
                                        _StorageOutRows.QTT_Will += _QTT_Free
                                        _QTT_Mod -= _QTT_Free
                                        '------- FIFO sor beszúrása
                                        Dim _StorageOutRowsInRows As StorageOutRowsInRows = _ocur.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                                                                    _StorageInRows, _StorageOutRows), True)
                                        If _StorageOutRowsInRows IsNot Nothing Then
                                            _StorageOutRowsInRows.QTT += _QTT_Free
                                        Else
                                            _StorageOutRowsInRows = New StorageOutRowsInRows(_ocur.Session)
                                            _StorageOutRowsInRows.StorageInRows = _StorageInRows
                                            _StorageOutRowsInRows.StorageOutRows = _StorageOutRows
                                            _StorageOutRowsInRows.QTT = _QTT_Free
                                        End If

                                    Else
                                        _StorageInRows.QTT_Out_Will += _QTT_Mod
                                        _StorageOutRows.QTT_Will += _QTT_Mod
                                        '------- FIFO sor beszúrása
                                        Dim _StorageOutRowsInRows As StorageOutRowsInRows = _ocur.FindObject(Of StorageOutRowsInRows)(CriteriaOperator.Parse("StorageInRows=? and StorageOutRows=?", _
                                                                                                                                _StorageInRows, _StorageOutRows), True)
                                        If _StorageOutRowsInRows IsNot Nothing Then
                                            _StorageOutRowsInRows.QTT += _QTT_Mod
                                        Else
                                            _StorageOutRowsInRows = New StorageOutRowsInRows(_ocur.Session)
                                            _StorageOutRowsInRows.StorageInRows = _StorageInRows
                                            _StorageOutRowsInRows.StorageOutRows = _StorageOutRows
                                            _StorageOutRowsInRows.QTT = _QTT_Mod
                                        End If

                                        Exit For
                                    End If
                                End If
                            Next

                            Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? and Products=?", _
                                                                                                _StorageOutHeader.Storage, _ocur.GetObjectByKey(Of Products)(_RecipeTo.Products.Oid)), True)
                            Dim _QTT_ReservedOut As Double = _StorageComputed.QTTReservedOut
                            Dim _QTT_Will_Out As Double = 0
                            Dim _StorageOutRows_Collection_R As New XPCollection(Of StorageOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                                                    CriteriaOperator.Parse("QTT_Will>0 and StorageOutHeader.Storage=? and Products=?", _StorageOutHeader.Storage, _ocur.GetObjectByKey(Of Products)(_RecipeTo.Products.Oid)))
                            For Each _StorageOutRows_R In _StorageOutRows_Collection_R
                                _QTT_Will_Out += _StorageOutRows_R.QTT_Will
                            Next

                            _StorageComputed.QTTFree = _StorageComputed.QTTFree - (_QTT_Will_Out - _QTT_ReservedOut)
                            _StorageComputed.QTTReservedOut = _QTT_Will_Out
                        End If
                    Next
                Next

                '----------------------------------------------------------
                'Kitárolás(ok) véglegesítése, mozgástábla, StorageComputed baszogatás
                '----------------------------------------------------------
                For Each _StorageOutHeader As StorageOutHeader In _StorageOutHeader_Collection

                    _StorageOutHeader.IsEditable = False
                    _StorageOutHeader.IsFinalized = True

                    For Each _StorageOutRows As StorageOutRows In _StorageOutHeader.StorageOutRows
                        Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                          CriteriaOperator.Parse("StorageOutRows=?", _StorageOutRows))

                        For Each _StorageOutRowsInRows In _StorageOutRowsInRows_Collection
                            If _StorageOutRowsInRows.QTT > 0 Then
                                Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, _
                                                                                    CriteriaOperator.Parse("StorageInRows=?", _StorageOutRowsInRows.StorageInRows))
                                _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("ObjectCreated", DB.SortingDirection.Descending))

                                _StorageOutRowsInRows.StorageInRows.QTT_Out += _StorageOutRowsInRows.QTT
                                _StorageOutRowsInRows.StorageInRows.QTT_Out_Will -= _StorageOutRowsInRows.QTT

                                Dim _StorageMoveEvents As New StorageMoveEvents(_ocur.Session)
                                _StorageMoveEvents.StorageInRows = _StorageOutRowsInRows.StorageInRows
                                _StorageMoveEvents.StorageOutRows = _StorageOutRowsInRows.StorageOutRows
                                If _StorageMoveEvents_Collection.Count > 0 Then
                                    _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents_Collection(0).QTT_Free_After
                                Else
                                    _StorageMoveEvents.QTT_Free_Before = 0
                                End If
                                _StorageMoveEvents.QTT = (-1) * _StorageOutRowsInRows.QTT
                                _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
                                _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageOutFinal
                                _StorageMoveEvents.ExecutionDate = _StorageOutRowsInRows.StorageOutRows.StorageOutHeader.CreateDate
                            End If
                        Next

                        _StorageOutRows.QTT += _StorageOutRows.QTT_Will

                        Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                                    _StorageOutRows.Products, _StorageOutRows.StorageOutHeader.Storage), True)
                        _StorageComputed.QTTReservedOut -= _StorageOutRows.QTT_Will
                        _StorageOutRows.QTT_Will = 0
                    Next
                Next


                '----------------------------------------------------------
                'Betárolás raktár(ak)ba
                '----------------------------------------------------------
                Dim _StorageInHeader_Collection As New ArrayList

                For Each _RecipeFrom As RecipeFrom In _RecipeExecutionSimple.Recipe.RecipeFrom
                    If _StorageInHeader_Collection.Count = 0 Then
                        Dim _StorageInHeader As New StorageInHeader(_ocur.Session)
                        With _StorageInHeader
                            .Storage = _ocur.GetObjectByKey(Of Storage)(_RecipeFrom.Storage.Oid)
                            .RecipeExecutionSimple = _RecipeExecutionSimple
                            .IsEditable = False
                            .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                        End With
                        _StorageInHeader_Collection.Add(_StorageInHeader)
                    Else
                        For Each _StorageInHeaderExist As StorageInHeader In _StorageInHeader_Collection
                            Dim _NewStorage As Boolean = False
                            If _StorageInHeaderExist.Storage.Oid <> _RecipeFrom.Storage.Oid Then
                                _NewStorage = True
                            End If
                            If _NewStorage Then
                                Dim _StorageInHeader As New StorageInHeader(_ocur.Session)
                                With _StorageInHeader
                                    .Storage = _onew.GetObjectByKey(Of Storage)(_RecipeFrom.Storage.Oid)
                                    .RecipeExecutionSimple = _RecipeExecutionSimple
                                    .IsEditable = False
                                    .StorageMoveType = _ocur.GetObject(_RecipeExecutionSimple.RecipeExecutionSimpleType.StorageMoveType)
                                End With
                                _StorageInHeader_Collection.Add(_StorageInHeader)
                                Exit For
                            End If
                        Next
                    End If
                Next

                For Each _RecipeFrom As RecipeFrom In _RecipeExecutionSimple.Recipe.RecipeFrom
                    For Each _StorageInHeader As StorageInHeader In _StorageInHeader_Collection
                        If _RecipeFrom.Storage.Oid = _StorageInHeader.Storage.Oid Then

                            Dim _StorageInRows As New StorageInRows(_ocur.Session)
                            With _StorageInRows
                                .StorageInHeader = _StorageInHeader
                                .Products = _RecipeFrom.Products
                                .QTT_Will = _RecipeFrom.QTT * _RecipeExecutionSimple.QTT
                                .RowIndex = _StorageInHeader.StorageInRows.Count
                                .AveragePriceInHUF = _RecipeFrom.ListPriceHUF
                            End With

                            Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                                                    _RecipeFrom.Products, _StorageInHeader.Storage))
                            If _StorageComputed Is Nothing Then
                                _StorageComputed = New StorageComputed(_ocur.Session)
                                With _StorageComputed
                                    .Storage = _StorageInHeader.Storage
                                    .Products = _RecipeFrom.Products
                                    .QTTReservedIn = _RecipeFrom.QTT * _RecipeExecutionSimple.QTT
                                End With
                            Else
                                _StorageComputed.QTTReservedIn += _RecipeFrom.QTT * _RecipeExecutionSimple.QTT
                            End If
                        End If
                    Next
                Next

                '----------------------------------------------------------
                'Betárolás(ok) véglegeítése
                '----------------------------------------------------------
                For Each _StorageInHeader As StorageInHeader In _StorageInHeader_Collection

                    _StorageInHeader.IsEditable = False
                    _StorageInHeader.IsFinalized = True

                    For Each _StorageInRows As StorageInRows In _StorageInHeader.StorageInRows
                        Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _
                                                                                _ocur.GetObject(_StorageInRows.Products), _ocur.GetObject(_StorageInHeader.Storage)))
                        Dim _StorageMoveEvents As New StorageMoveEvents(_ocur.Session)
                        If _StorageInRows.QTT_Will > 0 Then
                            _StorageInRows.QTT = _StorageInRows.QTT_Will
                            _StorageComputed.QTTFree += _StorageInRows.QTT_Will
                            _StorageComputed.QTTReservedIn -= _StorageInRows.QTT_Will

                            _StorageMoveEvents.StorageInRows = _ocur.GetObject(_StorageInRows)
                            _StorageMoveEvents.QTT = _StorageInRows.QTT_Will
                            _StorageMoveEvents.QTT_Free_After = _StorageComputed.QTTFree
                            _StorageMoveEvents.QTT_Free_Before = _StorageMoveEvents.QTT_Free_After - _StorageMoveEvents.QTT
                            _StorageMoveEvents.MoveEventsType = eMoveEventsType.StorageInFinal
                            _StorageMoveEvents.ExecutionDate = _StorageInRows.StorageInHeader.CreateDate

                            _StorageInRows.QTT_Will = 0
                        End If
                    Next
                Next

                _ocur.CommitChanges()

                '----------------------------------------------------------
                'Nyomtatványok
                '----------------------------------------------------------
                For Each _StorageOutHeader As StorageOutHeader In _StorageOutHeader_Collection
                    Dim _ReportData As ReportData = _onew.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageOutHeader.Storage.ReportOut.Oid), True)
                    If _ReportData IsNot Nothing Then
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("StorageOutRows.StorageOutHeader.DocumentNumber=?", _StorageOutHeader.DocumentNumber), True)
                    End If
                Next


                For Each _StorageInHeader As StorageInHeader In _StorageInHeader_Collection
                    Dim _ReportData As ReportData = _onew.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _StorageInHeader.Storage.ReportIn.Oid), True)
                    If _ReportData IsNot Nothing Then
                        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("StorageInHeader.DocumentNumber =?", _StorageInHeader.DocumentNumber), True)
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            LockingModule.UnLockObject(_uow_lock, GetType(StorageComputed), "-", _User)
        End Try
    End Sub
End Class
