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

Public Class OrderOutStockVC
    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

    Private Sub aGenerateOrderOutStock_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateOrderOutStock.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _OrderOutStockHeader As OrderOutStockHeader = _uow.GetObjectByKey(Of OrderOutStockHeader)(TryCast(View.SelectedObjects(0), OrderOutStockHeader).Oid)
        Dim _OrderOutStockRows As OrderOutStockRows
        Dim I As Long = 0
        _uow.Delete(_OrderOutStockHeader.OrderOutStockRows)

        If _OrderOutStockHeader.OrderOutStockQueryType = eOrderOutStockQueryType.eProduct Then
            Dim _Products_Collection As New XPCollection(Of Products)(_uow, CriteriaOperator.Parse("1=1"))
            Dim _Products As Products
            _Products_Collection.Criteria = _uow.ParseCriteria(_OrderOutStockHeader.Criterion)

            RaiseEvent InitWork(1, 1, _Products_Collection.Count)
            For Each _Products In _Products_Collection
                RaiseEvent DoWork()
                _OrderOutStockRows = New OrderOutStockRows(_uow)
                _OrderOutStockRows.OrderOutStockHeader = _OrderOutStockHeader
                _OrderOutStockRows.Products = _Products
                For Each _COP In _OrderOutStockRows.Products.CustomersOrderPriority
                    If _COP.RowIndex = 1 Then
                        _OrderOutStockRows.CustomersOrderPriority1 = _COP
                    End If
                    If _COP.RowIndex = 2 Then
                        _OrderOutStockRows.CustomersOrderPriority2 = _COP
                    End If
                Next
                I += 1
                If I Mod 1000 Then _uow.CommitChanges()
            Next
            _uow.CommitChanges()
            RaiseEvent FinalWork
        End If

        If _OrderOutStockHeader.OrderOutStockQueryType = eOrderOutStockQueryType.eStorageComputed Then
            Dim _StorageComputed_Collection As New XPCollection(Of StorageComputed)(_uow, CriteriaOperator.Parse("1=1"))
            Dim _StorageComputed As StorageComputed

            _StorageComputed_Collection.Criteria = _uow.ParseCriteria(_OrderOutStockHeader.Criterion)

            RaiseEvent InitWork(1, 1, _StorageComputed_Collection.Count)
            For Each _StorageComputed In _StorageComputed_Collection
                RaiseEvent DoWork()
                _OrderOutStockRows = New OrderOutStockRows(_uow)
                _OrderOutStockRows.OrderOutStockHeader = _OrderOutStockHeader
                _OrderOutStockRows.Products = _StorageComputed.Products
                _OrderOutStockRows.StorageComputed = _StorageComputed
                _OrderOutStockRows.StorageOutCirculation_CurrentYear = _uow.FindObject(Of StorageOutCirculation)(_uow.ParseCriteria("Products=? and YearExecution=?", _StorageComputed.Products, Year(GetSQLTime(_uow))))
                _OrderOutStockRows.StorageOutCirculation_CurrentYear = _uow.FindObject(Of StorageOutCirculation)(_uow.ParseCriteria("Products=? and YearExecution=?", _StorageComputed.Products, Year(GetSQLTime(_uow)) - 1))
                For Each _COP In _OrderOutStockRows.Products.CustomersOrderPriority
                    If _COP.RowIndex = 0 Then
                        _OrderOutStockRows.CustomersOrderPriority1 = _COP
                    End If
                    If _COP.RowIndex = 1 Then
                        _OrderOutStockRows.CustomersOrderPriority2 = _COP
                    End If
                Next
                I += 1
                If I Mod 1000 Then _uow.CommitChanges()
            Next
            _uow.CommitChanges()
            RaiseEvent FinalWork
        End If


        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub

    Private Sub aCreateOrderfromOrderOutStock_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateOrderfromOrderOutStock.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
    End Sub

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