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.ExpressApp.SystemModule

Public Class PricingActionVC
    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()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

    End Sub
    Private Sub aGeneratePricingAction_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGeneratePricingAction.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _PricingActionHeader As PricingActionHeader = _uow.GetObjectByKey(Of PricingActionHeader)(TryCast(View.SelectedObjects(0), PricingActionHeader).Oid)
        Dim _PricingActionRows As PricingActionRows
        Dim I As Long = 0
        _uow.Delete(_PricingActionHeader.PricingActionRows)

        Dim _Products_Collection As New XPCollection(Of Products)(_uow)

        _Products_Collection.Criteria = _uow.ParseCriteria(_PricingActionHeader.Criterion)

        RaiseEvent InitWork(1, 1, _Products_Collection.Count)
        For Each _Products In _Products_Collection
            RaiseEvent DoWork()
            _PricingActionRows = New PricingActionRows(_uow)
            _PricingActionRows.PricingActionHeader = _PricingActionHeader
            _PricingActionRows.Products = _Products
            _PricingActionRows.QTT_From = 1
            _PricingActionRows.QTT_To = 999999
            _PricingActionRows.IsAdditionalDiscount = False
            _PricingActionRows.ListPriceHUF = _Products.ListPriceOutHUF
            _PricingActionRows.ListPriceDEV = _Products.ListPriceOutDEV
            I += 1
            If I Mod 1000 = 0 Then _uow.CommitChanges()
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork

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

    Private Sub aSetPricingActionGroups_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSetPricingActionGroups.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _PricingActionRowsSet_PopUp As PricingActionRowsSet_PopUp = _onew.CreateObject(Of PricingActionRowsSet_PopUp)()

        e.View = Application.CreateDetailView(_onew, "PricingActionRowsSet_PopUp_DetailView", False, _PricingActionRowsSet_PopUp)
    End Sub
    Private Sub aSetPricingActionGroups_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSetPricingActionGroups.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _PricingActionRowsSet_PopUp As PricingActionRowsSet_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), PricingActionRowsSet_PopUp)

            If _PricingActionRowsSet_PopUp Is Nothing Then Throw New Exception("*Nem várt hiba történt, a folyamat megszakadt!")

            Dim _ListPriceHUFPercent As Double = _PricingActionRowsSet_PopUp.ListPriceHUFPercent
            Dim _ListPriceHUFPercent_Change As Boolean = _PricingActionRowsSet_PopUp.ListPriceHUFPercent_Change
            Dim _IsAdditionalDiscount As Boolean = _PricingActionRowsSet_PopUp.IsAdditionalDiscount
            Dim _IsAdditionalDiscount_Change As Boolean = _PricingActionRowsSet_PopUp.IsAdditionalDiscount_Change

            RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)

            For Each _PricingActionRows As PricingActionRows In View.SelectedObjects
                If _ListPriceHUFPercent_Change Then
                    If _ListPriceHUFPercent = 0 Then
                        _PricingActionRows.ListPriceHUF = _PricingActionRows.Products.ListPriceOutHUF
                    Else
                        _PricingActionRows.ListPriceHUF = _PricingActionRows.Products.ListPriceOutHUF - (_PricingActionRows.Products.ListPriceOutHUF * (_ListPriceHUFPercent / 100))
                        _PricingActionRows.ListPriceDEV = _PricingActionRows.Products.ListPriceOutDEV - (_PricingActionRows.Products.ListPriceOutDEV * (_ListPriceHUFPercent / 100))
                    End If
                End If
                If _IsAdditionalDiscount_Change Then
                    _PricingActionRows.IsAdditionalDiscount = _IsAdditionalDiscount
                End If
                RaiseEvent DoWork()
            Next
            RaiseEvent FinalWork
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

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