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

Public Class StorageOperation_VC
    Inherits DevExpress.ExpressApp.ViewController

    Dim _WaitFormClass As New WaitFormClass
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of StorageOperation_VC)().aResetStorageForTest.Active.SetItemValue("", False)

        If View.Id = "ResetObjects_DetailView" Then
            Frame.GetController(Of StorageOperation_VC)().aResetStorageForTest.Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "ResetObjects_DetailView" Then
            Frame.GetController(Of StorageOperation_VC)().aResetStorageForTest.Active.SetItemValue("", False)
        End If
    End Sub

    Private Sub aResetStorageForTest_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aResetStorageForTest.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        Dim _StorageOutHeader_Collection As New XPCollection(Of StorageOutHeader)(_uow)
        Dim _StorageOutRows_Collection As New XPCollection(Of StorageOutRows)(_uow)
        Dim _StorageInHeader_Collection As New XPCollection(Of StorageInHeader)(_uow)
        Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_uow)
        Dim _StorageComputed_Collection As New XPCollection(Of StorageComputed)(_uow)
        Dim _StorageMoveEvents_Collection As New XPCollection(Of StorageMoveEvents)(_uow)
        Dim _StorageOutRowsInRows_Collection As New XPCollection(Of StorageOutRowsInRows)(_uow)
        Dim _OrderInHeader_Collection As New XPCollection(Of OrderInHeader)(_uow)
        Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(_uow)
        Dim _DeliveryNoteOutHeader_Collection As New XPCollection(Of DeliveryNoteOutHeader)(_uow)
        Dim _DeliveryNoteOutRows_Collection As New XPCollection(Of DeliveryNoteOutRows)(_uow)
        Dim _DeliveryNoteInHeader_Collection As New XPCollection(Of DeliveryNoteInHeader)(_uow)
        Dim _DeliveryNoteInRows_Collection As New XPCollection(Of DeliveryNoteInRows)(_uow)
        Dim _StorageComputedLocations_Collection As New XPCollection(Of StorageComputedLocations)(_uow)
        Dim _StoragePDACollectionHeader_Collection As New XPCollection(Of StoragePDACollectionHeader)(_uow)
        Dim _StoragePDACollectionRows_Collection As New XPCollection(Of StoragePDACollectionRows)(_uow)

        _WaitFormClass.InitWork(1, 1, 16)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageComputed_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageInHeader_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageInRows_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageMoveEvents_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageOutHeader_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageOutRows_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageOutRowsInRows_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_OrderInHeader_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_OrderInRows_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_DeliveryNoteInHeader_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_DeliveryNoteInRows_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_DeliveryNoteOutHeader_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_DeliveryNoteOutRows_Collection)
        _WaitFormClass.DoWork()
        _uow.Delete(_StorageComputedLocations_Collection)
        '_WaitFormClass.DoWork()
        '_uow.Delete(_StoragePDACollectionHeader_Collection)
        '_WaitFormClass.DoWork()
        '_uow.Delete(_StoragePDACollectionRows_Collection)
        _WaitFormClass.Finalwork
        _uow.CommitChanges()

        Dim _Products_Collection As New XPCollection(Of Products)(_uow)
        Dim _Storage As Storage = _uow.FindObject(Of Storage)(CriteriaOperator.Parse("CustomersFrom.IsDefault=True"))
        '_Storage = _uow.FindObject(Of Storage)(CriteriaOperator.Parse("ShortName=?", "Input anyag raktár"))
        Dim _StorageInHeader As New StorageInHeader(_uow)
        Dim _Rnd As New Random

        _StorageInHeader.Storage = _Storage
        _StorageInHeader.DocumentNumber = _Storage.NumberGeneratorIn.GetNewNumber(_Storage.NumberGeneratorIn)
        _StorageInHeader.StorageMoveType = _uow.FindObject(Of StorageMoveType)(CriteriaOperator.Parse("MoveType=?", eMoveType.NormalInput))

        _WaitFormClass.InitWork(1, 1, 10)
        For ik = 1 To 100
            _WaitFormClass.DoWork()
            Dim _StorageInRows As New StorageInRows(_uow)
            Dim _StorageComputed As StorageComputed

            _StorageInRows.StorageInHeader = _StorageInHeader
            _StorageInRows.Products = _Products_Collection(_Rnd.Next(0, _Products_Collection.Count))
            _StorageInRows.QTT_Will = _Rnd.Next(1, 1000)
            _StorageInRows.RowIndex = _StorageInHeader.StorageInRows.Count

            _StorageComputed = _uow.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _StorageInRows.Products, _Storage))
            If _StorageComputed Is Nothing Then
                _StorageComputed = New StorageComputed(_uow)
                _StorageComputed.Storage = _Storage
                _StorageComputed.Products = _StorageInRows.Products
            End If
            _StorageComputed.QTTReservedIn += _StorageInRows.QTT_Will
        Next
        _WaitFormClass.Finalwork

        _uow.CommitChanges()

        _WaitFormClass.InitWork(1, 1, 3)
        For ik = 0 To 3
            _WaitFormClass.DoWork()
            Dim _StorageInRows_ReNew As New StorageInRows(_uow)
            Dim _StorageInHeader_ReNew As New StorageInHeader(_uow)

            _StorageInRows_ReNew.StorageInHeader = _StorageInHeader_ReNew
            _StorageInRows_ReNew.Products = _StorageInHeader.StorageInRows(ik).Products
            _StorageInRows_ReNew.QTT_Will = _Rnd.Next(1, 1000)
            _StorageInRows_ReNew.RowIndex = _StorageInHeader_ReNew.StorageInRows.Count

            _StorageInHeader_ReNew.Storage = _StorageInHeader.Storage
            _StorageInHeader_ReNew.DocumentNumber = _StorageInHeader.Storage.NumberGeneratorIn.GetNewNumber(_Storage.NumberGeneratorIn)
            _StorageInHeader_ReNew.StorageMoveType = _StorageInHeader.StorageMoveType

            Dim _StorageComputed_ReNew As StorageComputed = _uow.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Products=? and Storage=?", _StorageInRows_ReNew.Products, _StorageInHeader_ReNew.Storage))
            If _StorageComputed_ReNew Is Nothing Then
                _StorageComputed_ReNew = New StorageComputed(_uow)
                _StorageComputed_ReNew.Storage = _Storage
                _StorageComputed_ReNew.Products = _StorageInRows_ReNew.Products
            End If
            _StorageComputed_ReNew.QTTReservedIn += _StorageInRows_ReNew.QTT_Will
        Next ik
        _WaitFormClass.Finalwork

        _uow.CommitChanges()

        Dim _OrderInHeader As New OrderInHeader(_uow)
        Dim _Customers_Collection As New XPCollection(Of Customers)(_uow)
        Dim _Contacts As Contacts = Nothing

        _OrderInHeader.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_StorageInHeader.Storage.CustomersFrom.Oid)
        _OrderInHeader.Customers = _Customers_Collection(_Rnd.Next(0, _Customers_Collection.Count))

        If _Contacts Is Nothing Then
            _Contacts = New Contacts(_uow)
            _Contacts.FirstName = "Tamás"
            _Contacts.LastName = "Teszt"
        End If
        _OrderInHeader.Contacts = _Contacts
        _OrderInHeader.OrderInParameters = _uow.FindObject(Of OrderInParameters)(CriteriaOperator.Parse("CustomersFrom=?", _OrderInHeader.CustomersFrom))
        _OrderInHeader.DocumentNumber = _OrderInHeader.OrderInParameters.NumberGenerator.GetNewNumber(_OrderInHeader.OrderInParameters.NumberGenerator)
        _OrderInHeader.PaymentOption = _uow.FindObject(Of PaymentOption)(CriteriaOperator.Parse("DefaultForBusinnes=True"))
        _OrderInHeader.ShipmentOption = _uow.FindObject(Of ShipmentOption)(CriteriaOperator.Parse("1=1"))

        Dim _StorageOutHeader As New StorageOutHeader(_uow)
        _StorageOutHeader.CustomersFrom = _uow.GetObjectByKey(Of CustomersFrom)(_StorageInHeader.Storage.CustomersFrom.Oid)
        _StorageOutHeader.Customers = _Customers_Collection(_Rnd.Next(0, _Customers_Collection.Count))
        _StorageOutHeader.Storage = _StorageInHeader.Storage
        _StorageOutHeader.DocumentNumber = _Storage.NumberGeneratorOut.GetNewNumber(_Storage.NumberGeneratorOut)

        _StorageOutHeader.StorageMoveType = _uow.FindObject(Of StorageMoveType)(CriteriaOperator.Parse("Storage=? AND MoveType=?", _StorageOutHeader.Storage, eMoveType.Sales))
        _uow.CommitChanges()


    End Sub
End Class
