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

Public Class OrderInToDisposeVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "OrderInToDispose_DetailView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OrderInToDispose_OrderInTo_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().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 = "OrderInToDispose_DetailView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OrderInToDispose_OrderInTo_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aOrderToSetCustomerOrder_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOrderToSetCustomerOrder.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _OrderInToDispose As OrderInToDispose = TryCast(View.SelectedObjects(0), OrderInToDispose)
            Dim _OrderInTo_ListEditor As DevExpress.ExpressApp.Editors.ListPropertyEditor = TryCast(View, DetailView).FindItem("OrderInTo")
            If _OrderInTo_ListEditor.View.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egyetlen sor sem, így nem lehet rendeléshez rendelni!")

            Dim _CS As New CollectionSource(_onew, GetType(OrderOutHeader))
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria("First") = CriteriaOperator.Parse("IsEditable=True and CustomersFrom=?", _onew.GetObject(_OrderInToDispose.CustomersFrom))
            _CS.EndUpdateCriteria()

            e.View = Application.CreateListView("OrderOutHeader_ListView_Select", _CS, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aOrderToSetCustomerOrder_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOrderToSetCustomerOrder.Execute
        '// A kijelölt tételeket hozzárendeli egy élő vagy egy új rendeléshez
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderOutHeader As OrderOutHeader = TryCast(e.PopupWindow.View.SelectedObjects(0), OrderOutHeader)
            Dim _OrderInTo_ListEditor As DevExpress.ExpressApp.Editors.ListPropertyEditor = TryCast(View, DetailView).FindItem("OrderInTo")
            Dim _OrderOutRows As OrderOutRows = Nothing
            Dim _OrderInTo As OrderInTo

            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
            If _OrderOutHeader IsNot Nothing Then
                For Each _OrderInTo In _OrderInTo_ListEditor.ListView.SelectedObjects
                    If _OrderInTo.OrderOutRows Is Nothing Then
                        _OrderOutRows = _ocur.CreateObject(Of OrderOutRows)()
                        _OrderOutRows.OrderOutHeader = _ocur.GetObjectByKey(Of OrderOutHeader)(_OrderOutHeader.Oid)
                        _OrderOutRows.DiscountPercent = 0
                        _OrderOutRows.ListPriceDEV = _OrderInTo.OrderInRows.Products.ListPriceInDEV
                        _OrderOutRows.ListPriceHUF = _OrderInTo.OrderInRows.Products.ListPriceInDEV
                        _OrderOutRows.NoteRows = ""
                        _OrderOutRows.NoteRowsPrivate = ""
                        _OrderOutRows.Products = _OrderInTo.OrderInRows.Products
                        _OrderOutRows.QTT = _OrderInTo.QTT_To
                        _OrderOutRows.QualityOption = _OrderInTo.OrderInRows.QualityOption
                        _OrderOutRows.RowIndex = _OrderOutRows.OrderOutHeader.OrderOutRows.Count 'Kicsit zavarosnak tűnhet... :)
                        '_OrderOutRows.ShipmentOption=_OrderOutRows
                        _OrderOutRows.SumPriceDEV = Math.Round(_OrderOutRows.ListPriceDEV * _OrderOutRows.QTT, 2, MidpointRounding.AwayFromZero)
                        _OrderOutRows.SumPriceHUF = Math.Round(_OrderOutRows.ListPriceHUF * _OrderOutRows.QTT, 2, MidpointRounding.AwayFromZero)
                        '_OrderOutRows.UrgentOption
                        _OrderInTo.OrderOutRows = _OrderOutRows
                    End If
                Next
                _ocur.CommitChanges()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        Finally
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        End Try
    End Sub

    Private Sub aOrderToIncoming_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOrderToIncoming.CustomizePopupWindowParams

        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _OrderInToDispose As OrderInToDispose = TryCast(View.SelectedObjects(0), OrderInToDispose)
            Dim _OrderInTo_ListEditor As DevExpress.ExpressApp.Editors.ListPropertyEditor = TryCast(View, DetailView).FindItem("OrderInTo")
            If _OrderInTo_ListEditor.View.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egyetlen sor sem, így nem lehet rendeléshez rendelni!")

            Dim _CS As New CollectionSource(_onew, GetType(OrderOutHeader))

            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria("First") = CriteriaOperator.Parse("IsEditable=True AND CustomersOrder=?", _
                                                           TryCast(_OrderInTo_ListEditor.View.SelectedObjects(0), OrderInTo).OrderOutRows.OrderOutHeader.CustomersOrder)
            _CS.EndUpdateCriteria()

            e.View = Application.CreateListView("OrderOutHeader_ListView", _CS, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aOrderToIncoming_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOrderToIncoming.Execute
        '// A kijelölt tételeket hozzárendeli egy olyan lezárt rendeléshez ami még nem jött meg és abbol lesz foglalva a tétel
        Try

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

    
End Class
