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.Xpo

' For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
Partial Public Class StorageMoveEventsVC
    Inherits 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()
        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.
    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 aRecalcStorageMoveEvents_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aRecalcStorageMoveEvents.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _StorageMoveEvents As StorageMoveEvents

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _StorageMoveEvents In View.SelectedObjects
            RaiseEvent DoWork()
            RecalcStorageMoveEvents(_uow, _StorageMoveEvents.StorageInRows.StorageInHeader.Storage, _StorageMoveEvents.StorageInRows.Products)
        Next
        RaiseEvent FinalWork
    End Sub

    Private Sub RecalcStorageMoveEvents(_uow As UnitOfWork, _Storage As Storage, _Products As Products)
        Dim _FullIndex As Long = 1
        Dim _Next As Double = 0
        Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(_uow, CriteriaOperator.Parse("IsNull(StorageInRows.GCRecord)=True and IsNull(StorageInRows.StorageInHeader.GCRecord)=True and StorageInRows.Storage.Oid=? and StorageInRows.Products.Oid=?", _Storage.Oid, _Products.Oid))
        Dim _StorageMoveEvents As StorageMoveEvents


        _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("GetDate(ExecutionDate)", DB.SortingDirection.Ascending))
        _StorageMoveEvents_Collection.Sorting.Add(New SortProperty("StorageInRows.RowIndex", DB.SortingDirection.Ascending))


        For Each _StorageMoveEvents In _StorageMoveEvents_Collection
            _StorageMoveEvents.QTT_Free_Before = _Next
            _StorageMoveEvents.QTT_Free_After = _StorageMoveEvents.QTT_Free_Before + _StorageMoveEvents.QTT
            _StorageMoveEvents.FullIndex = _FullIndex
            _FullIndex += 1
            _Next = _StorageMoveEvents.QTT_Free_After
        Next
        _uow.CommitChanges()

    End Sub

    Private Sub aRecalcStorageMoveEvents_StorageComputed_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aRecalcStorageMoveEvents_StorageComputed.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _StorageComputed As StorageComputed

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _StorageComputed In View.SelectedObjects
            RaiseEvent DoWork()
            RecalcStorageMoveEvents(_uow, _StorageComputed.Storage, _StorageComputed.Products)
        Next
        RaiseEvent FinalWork
    End Sub
End Class
