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

Public Class OrderInRowsOtherVC
    Inherits DevExpress.ExpressApp.ViewController

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

        If View.Id = "OrderInHeader_OrderInRowsOther_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "OrderInHeader_OrderInRowsOther_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aToTable_OrderInOther_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_OrderInOther.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInHeader As OrderInHeader = CType(View.SelectedObjects(0), OrderInHeader)

        If _OrderInHeader IsNot Nothing Then

            Dim _OrderInRowsOther As OrderInRowsOther = New OrderInRowsOther(_ocur.Session)
            _OrderInRowsOther.AddRows(_OrderInHeader)
            '  _OrderInHeader.Save()
            _ocur.CommitChanges()

            Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_o_ShortName")
            If ItemToFocus IsNot Nothing Then
                Dim ItemControl As Object
                ItemControl = ItemToFocus.Control
                ItemControl.Focus()
            End If

            _OrderInHeader.row_o_Description = ""
            _OrderInHeader.row_o_DiscountPercent = 0
            _OrderInHeader.row_o_ListPriceDEV = 0
            _OrderInHeader.row_o_ListPriceHUF = 0
            _OrderInHeader.row_o_QTT = 0
            _OrderInHeader.row_o_ShortName = ""
            _OrderInHeader.row_o_Units = Nothing
            _OrderInHeader.row_o_VAT = Nothing

            View.Refresh()
        End If
    End Sub
    Private Sub aToTableD_OrderInOther_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_OrderInOther.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInRowsOther As OrderInRowsOther
        Dim _OrderInHeader As OrderInHeader = TryCast(View.SelectedObjects(0), OrderInHeader)
        Dim _OrderInRowsOther_Del_Collection As New ArrayList 'Sz
        Dim _OrderInRowsOther_ListEditor As ListPropertyEditor

        _OrderInRowsOther_ListEditor = TryCast(View, DetailView).FindItem("OrderInRowsOther")
        For Each _OrderInRowsOther In _OrderInRowsOther_ListEditor.ListView.SelectedObjects
            If _OrderInRowsOther.QTT_Invoiced = 0 Then
                _OrderInRowsOther.RemoveRows(_OrderInHeader)
                _OrderInRowsOther_Del_Collection.Add(_OrderInRowsOther)

            End If
        Next
        _ocur.Delete(_OrderInRowsOther_Del_Collection)
        _OrderInHeader.Save()
        _ocur.CommitChanges()
        View.Refresh()
    End Sub

    Private Sub aUp_OrderInOther_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_OrderInOther.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInHeader As OrderInHeader = TryCast(e.SelectedObjects(0), OrderInRowsOther).OrderInHeader
        Dim _OrderInRowsOther_Selected As OrderInRowsOther = TryCast(e.SelectedObjects(0), OrderInRowsOther)
        Dim _OrderInRowsOther_Minus As OrderInRowsOther
        Dim _OrderInRowsOther As OrderInRowsOther

        If _OrderInRowsOther_Selected.RowIndex > 0 Then
            For Each _OrderInRowsOther In _OrderInHeader.OrderInRowsOther
                If _OrderInRowsOther.RowIndex = _OrderInRowsOther_Selected.RowIndex - 1 Then
                    _OrderInRowsOther_Minus = _OrderInRowsOther
                    Exit For
                End If
            Next

            If _OrderInRowsOther_Minus IsNot Nothing Then
                _OrderInRowsOther_Selected.RowIndex -= 1
                _OrderInRowsOther_Minus.RowIndex += 1
            End If
            _OrderInRowsOther_Minus.Save()
            _OrderInRowsOther_Selected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aDown_OrderInOther_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_OrderInOther.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInHeader As OrderInHeader = TryCast(e.SelectedObjects(0), OrderInRowsOther).OrderInHeader
        Dim _OrderInRowsOther_Selected As OrderInRowsOther = TryCast(e.SelectedObjects(0), OrderInRowsOther)
        Dim _OrderInRowsOther_Plus As OrderInRowsOther
        Dim _OrderInrowsOther As OrderInRowsOther

        If _OrderInRowsOther_Selected.RowIndex < _OrderInHeader.OrderInRowsOther.Count - 1 Then
            For Each _OrderInrowsOther In _OrderInHeader.OrderInRowsOther
                If _OrderInrowsOther.RowIndex = _OrderInRowsOther_Selected.RowIndex + 1 Then
                    _OrderInRowsOther_Plus = _OrderInrowsOther
                    Exit For
                End If
            Next

            If _OrderInRowsOther_Plus IsNot Nothing Then
                _OrderInRowsOther_Selected.RowIndex += 1
                _OrderInRowsOther_Plus.RowIndex -= 1
            End If
            _OrderInRowsOther_Plus.Save()
            _OrderInRowsOther_Selected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub

    Private Sub aInsertServicesToOrderIn_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aInsertServicesToOrderIn.CustomizePopupWindowParams
        ' Az akció a szolgáltatások listview-ját hozza fel.
        Dim _OrderInHeader As OrderInHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OrderInHeader)

        If _OrderInHeader.IsEditable Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(Services))

            e.View = Application.CreateListView("Services_ListView_Browse", _CollectionSource, False)
        Else
            '----------TESZT MIATT-----------------------------------------
            Throw New Exception("*A kimenő ajánlat végleges vagy stornó!")
        End If
    End Sub

    Private Sub aInsertServicesToOrderIn_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aInsertServicesToOrderIn.Execute
        'A popup-on kiválasztott szolgáltatásokból létrehoz kimenő árajánlati sorokat
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInHeader As OrderInHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OrderInHeader)
        Dim _Services As Services
        Dim _OrderInRowsOther_New As OrderInRowsOther

        For Each _Services In e.PopupWindow.View.SelectedObjects
            _OrderInRowsOther_New = _ocur.CreateObject(Of OrderInRowsOther)()

            _OrderInRowsOther_New.OrderInHeader = _OrderInHeader
            _OrderInRowsOther_New.ShortName = _Services.ShortName
            _OrderInRowsOther_New.CustomsTariffs = _ocur.GetObject(_Services.CustomsTariffs)
            _OrderInRowsOther_New.Description = _Services.Description
            _OrderInRowsOther_New.ListPriceDEV = _Services.ListPriceDEV
            _OrderInRowsOther_New.ListPriceHUF = _Services.ListPriceHUF
            _OrderInRowsOther_New.Units = _ocur.GetObject(_Services.Units)
            _OrderInRowsOther_New.VAT = _ocur.GetObject(_Services.VAT)
            _OrderInRowsOther_New.QTT = 1
            _OrderInRowsOther_New.RowGroup1 = _Services.RowGroup1
            _OrderInRowsOther_New.RowGroup2 = _Services.RowGroup2

            'Indexelés
            If _OrderInHeader.OrderInRowsOther.Count = 0 Then
                _OrderInRowsOther_New.RowIndex = 0
            Else
                _OrderInRowsOther_New.RowIndex = _OrderInHeader.OrderInRowsOther.Count - 1
            End If

            _OrderInRowsOther_New.Save()
        Next
        _ocur.CommitChanges()
    End Sub
End Class
