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

Public Class ContractProduct_VC
    Inherits DevExpress.ExpressApp.ViewController
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)

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

        Frame.GetController(Of ContractProduct_VC)().aDown_ContractProductRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractProduct_VC)().aUp_ContractProductRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractProduct_VC)().aNewContractProductHeader.Active.SetItemValue("", False)
        Frame.GetController(Of ContractProduct_VC)().aFinalizeContractProduct.Active.SetItemValue("", False)
        Frame.GetController(Of ContractProduct_VC)().aSendBackToEditContractProduct.Active.SetItemValue("", False)

        If View.Id Like "ContractProductHeader_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ContractProduct_VC)().aNewContractProductHeader.Active.SetItemValue("", True)
            Frame.GetController(Of ContractProduct_VC)().aFinalizeContractProduct.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().DeleteAction.TargetObjectsCriteria = "State=0 AND (DocumentNumber = '' OR IsNull(DocumentNumber)=True)"
            Frame.GetController(Of ContractProduct_VC)().aSendBackToEditContractProduct.Active.SetItemValue("", True)
        End If

        If View.Id Like "ContractProductHeader_DetailView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ContractProduct_VC)().aFinalizeContractProduct.Active.SetItemValue("", True)
            Frame.GetController(Of ContractProduct_VC)().aSendBackToEditContractProduct.Active.SetItemValue("", True)
        End If

        If View.Id = "ContractProductHeader_ContractProductRows_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 ContractProduct_VC)().aDown_ContractProductRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractProduct_VC)().aUp_ContractProductRows.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "ContractTemplate_ListView_PopUp" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "ContractProductRowsInOut_ListView" Then
            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.DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "ContractProductLINQ_ListView_ContractProductsRowsCustomersGroup_Linq" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "ContractProductLINQ_ListView_ContractProductsRowsGroup_Linq" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "ContractProductRows_ListView_Invoice" Then
            Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aMergeContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aOpenRegistryInvoiceFromContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aOpenCOCFromContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aDivideContractRows.Active.SetItemValue("", False)
        End If
        If View.Id = "ContractProductRows_ListView_Invoice_IsProforma" Then
            Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aMergeContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aOpenRegistryInvoiceFromContractRows.Active.SetItemValue("", False) 'Iktatás betekintése
            Frame.GetController(Of ContractsVC)().aOpenInvoiceFromContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aOpenCOCFromContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aDivideContractRows.Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ContractProductHeader_ContractProductRows_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 ContractProduct_VC)().aDown_ContractProductRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractProduct_VC)().aUp_ContractProductRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id Like "ContractProductHeader_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ContractProduct_VC)().aNewContractProductHeader.Active.SetItemValue("", False)
            Frame.GetController(Of ContractProduct_VC)().aSendBackToEditContractProduct.Active.SetItemValue("", False)
        End If

        If View.Id Like "ContractProductHeader_DetailView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ContractProduct_VC)().aSendBackToEditContractProduct.Active.SetItemValue("", False)
        End If
        If View.Id = "ContractTemplate_ListView_PopUp" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "ContractProductRowsInOut_ListView" Then
            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.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "ContractProductLINQ_ListView_ContractProductsRowsCustomersGroup_Linq" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "ContractProductLINQ_ListView_ContractProductsRowsGroup_Linq" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "ContractProductRows_ListView_Invoice" Then
            Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aMergeContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aOpenRegistryInvoiceFromContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aOpenCOCFromContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aDivideContractRows.Active.SetItemValue("", True)
        End If
        If View.Id = "ContractProductRows_ListView_Invoice_IsProforma" Then
            Frame.GetController(Of ContractsVC)().aGenerateInvoicefromContractsRows.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aMergeContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aOpenRegistryInvoiceFromContractRows.Active.SetItemValue("", True) 'Iktatás betekintése
            Frame.GetController(Of ContractsVC)().aOpenInvoiceFromContractRows.Active.SetItemValue("", False)
            Frame.GetController(Of ContractsVC)().aOpenCOCFromContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractsVC)().aDivideContractRows.Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aContractProductHeaderNew_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _DetailViewName As String = ""
        Dim _ContractProductHeader As ContractProductHeader = _onew.CreateObject(Of ContractProductHeader)()
        _ContractProductHeader.CustomersFrom = _onew.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("IsDefault=?", True))
        If View.Id = "ContractProductHeader_ListView_Receivables" Then
            _DetailViewName = "ContractProductHeader_DetailView_Receivables"
            _ContractProductHeader.ContractTemplate = _onew.FindObject(Of ContractTemplate)(CriteriaOperator.Parse("CustomersFrom=? AND PartnerType=? AND IsProducts=True", _ContractProductHeader.CustomersFrom, ePartnerType.eReceivables))
        End If
        If View.Id = "ContractProductHeader_ListView_Payabels" Then
            _DetailViewName = "ContractProductHeader_DetailView_Payables"
            _ContractProductHeader.ContractTemplate = _onew.FindObject(Of ContractTemplate)(CriteriaOperator.Parse("CustomersFrom=? AND PartnerType=?  AND IsProducts=True", _ContractProductHeader.CustomersFrom, ePartnerType.ePayabels))
        End If
        If _DetailViewName <> "" Then
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _DetailViewName, True, _ContractProductHeader)
        Else
            _onew.Rollback()
            '--------------------------------- TESZT MIATT ------------------------------->>
            Throw New Exception("*Az eladni kívánt mennyiség meghaladja maximálisan eladható mennyiséget!")
            '--------------------------------- TESZT MIATT -------------------------------<<
        End If
    End Sub
    Private Sub aTotable_ConProd_Payabels_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotable_ConProd_Payabels.Execute
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractProductHeader As ContractProductHeader = CType(View.SelectedObjects(0), ContractProductHeader)
        Dim _ContractProductRows As ContractProductRows = _ocur.CreateObject(Of ContractProductRows)()
        Dim _ContractProductRowsEvent As ContractProductRowsEvent = _ocur.CreateObject(Of ContractProductRowsEvent)()

        _ContractProductRows.ContractProductHeader = _ContractProductHeader

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


        _ContractProductRows.AddRows(_ContractProductHeader)

        _ContractProductRowsEvent.ContractProductRowsIn = _ContractProductRows
        _ContractProductRowsEvent.QTT_In = _ContractProductHeader.row_QTT
        _ContractProductRowsEvent.DateExecution = _ContractProductRows.DateExecution


        _ocur.CommitChanges()

        _ContractProductHeader.row_ContractProductRows = Nothing
        _ContractProductHeader.row_NettoPriceDEV = 0
        _ContractProductHeader.row_NettoPriceHUF = 0
        _ContractProductHeader.row_Products = Nothing
        _ContractProductHeader.row_QTT = 0
        _ContractProductHeader.row_RowGroup1 = ""
        _ContractProductHeader.row_RowGroup2 = ""
        _ContractProductHeader.row_Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
        _ContractProductHeader.row_VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))


        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    Private Sub aTotableD_ConProd_Payabels_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotableD_ConProd_Payabels.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractProductHeader As ContractProductHeader = View.SelectedObjects(0)
        Dim _ContractProductHeader_ListEditor As ListPropertyEditor


        _ContractProductHeader_ListEditor = TryCast(View, DetailView).FindItem("ContractProductRows")
        For Each _ContractProductRows As ContractProductRows In _ContractProductHeader_ListEditor.ListView.SelectedObjects
            Dim _ContractProductRowsEvent As ContractProductRowsEvent = _ocur.CreateObject(Of ContractProductRowsEvent)()
            _ContractProductRowsEvent.ContractProductRowsIn = _ContractProductRows
            _ContractProductRowsEvent.QTT_In = _ContractProductRows.QTT * (-1)
            _ContractProductRowsEvent.DateExecution = _ContractProductRows.DateExecution

            _ContractProductRows.RemoveRows()
        Next

        _ocur.CommitChanges()
        View.Refresh()
    End Sub
    Private Sub aBackToRowEdit_ConProd_Payabels_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_ConProd_Payabels.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractProductHeader As ContractProductHeader = View.SelectedObjects(0) 'Kijelölt elem fejlécének betöltése

        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("ContractProductRows") 'Sorok összeszedése
        If _ListView.ListView.SelectedObjects.Count > 0 Then

            Dim _ContractProductRows As ContractProductRows = _ListView.ListView.SelectedObjects(0)  'Régi sor változóba töltése

            _ContractProductHeader.row_Products = _ContractProductRows.Products
            _ContractProductHeader.row_NettoPriceDEV = _ContractProductRows.NettoPriceDEV
            _ContractProductHeader.row_NettoPriceHUF = _ContractProductRows.NettoPriceHUF
            _ContractProductHeader.row_QTT = _ContractProductRows.QTT
            _ContractProductHeader.row_RowGroup1 = _ContractProductRows.RowGroup1
            _ContractProductHeader.row_RowGroup2 = _ContractProductRows.RowGroup2
            _ContractProductHeader.row_Units = _ContractProductRows.Units
            _ContractProductHeader.row_VAT = _ContractProductRows.VAT
            _ContractProductHeader.row_APCSheetByProducts = _ContractProductRows.APCSheetByProducts

        End If
    End Sub
    Private Sub aTotable_ConProd_Receivables_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotable_ConProd_Receivables.Execute
        '    Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractProductHeader As ContractProductHeader = CType(View.SelectedObjects(0), ContractProductHeader)
        Dim _ContractProductRows_IN As ContractProductRows = _ocur.FindObject(Of ContractProductRows)(CriteriaOperator.Parse("Oid=?", _ContractProductHeader.row_ContractProductRows.Oid))
        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("ContractProductRows") 'Sorok összeszedése
        Dim _ContractProductRowsCollection As XPCollection = _ListView.ListView.ObjectSpace.CreateCollection(GetType(ContractProductRows), CriteriaOperator.Parse("ContractProductHeader=?", _ContractProductHeader))
        Dim _ContractProductRows As ContractProductRows
        Dim _MatchLine As Boolean = False

        If _ContractProductHeader.row_QTT <= (_ContractProductRows_IN.QTT - _ContractProductRows_IN.QTT_Out) Then
            For Each _ContractProductRows_List As ContractProductRows In _ContractProductRowsCollection
                If _ContractProductRows_List.Products.Oid = _ContractProductHeader.row_ContractProductRows.Products.Oid Then
                    If _ContractProductHeader.CurrencyName.ShortName = "HUF" Then
                        If _ContractProductRows_List.NettoPriceHUF = _ContractProductHeader.row_NettoPriceHUF Then
                            _MatchLine = True
                        End If
                    Else
                        If _ContractProductRows_List.NettoPriceDEV = _ContractProductHeader.row_NettoPriceDEV Then
                            _MatchLine = True
                        End If
                    End If
                End If
            Next
            If _MatchLine Then
                _ContractProductRows = _ocur.FindObject(Of ContractProductRows)(CriteriaOperator.Parse("ContractProductHeader=? AND Products=? AND NettoPriceHUF=? AND NettoPriceDEV=?", _
                                                            _ContractProductHeader, _ContractProductHeader.row_ContractProductRows.Products, _ContractProductHeader.row_NettoPriceHUF, _ContractProductHeader.row_NettoPriceDEV))
                _ContractProductRows.QTT += _ContractProductHeader.row_QTT
                _ContractProductRows_IN.QTT_Out += _ContractProductHeader.row_QTT
            Else
                _ContractProductRows = _ocur.CreateObject(Of ContractProductRows)()

                _ContractProductRows.ContractProductHeader = _ContractProductHeader
                _ContractProductRows.AddRows(_ContractProductHeader)
                _ContractProductRows_IN.QTT_Out += _ContractProductRows.QTT

            End If

            Dim _ContractProductRowsInOut As ContractProductRowsInOut = _ocur.CreateObject(Of ContractProductRowsInOut)()
            Dim _ContractProductRowsEvent As ContractProductRowsEvent = _ocur.CreateObject(Of ContractProductRowsEvent)()

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


            _ContractProductRowsInOut.QTT = _ContractProductHeader.row_QTT
            _ContractProductRowsInOut.ContractProductRowsIn = _ContractProductRows_IN
            _ContractProductRowsInOut.ContractProductRowsOut = _ContractProductRows

            _ContractProductRowsEvent.ContractProductRowsOut = _ContractProductRows
            _ContractProductRowsEvent.ContractProductRowsIn = _ContractProductHeader.row_ContractProductRows
            _ContractProductRowsEvent.QTT_Out = _ContractProductHeader.row_QTT
            _ContractProductRowsEvent.QTT_In = 0
            _ContractProductRowsEvent.DateExecution = _ContractProductRows.DateExecution

            _ContractProductHeader.row_ContractProductRows = Nothing
            _ContractProductHeader.row_NettoPriceDEV = 0
            _ContractProductHeader.row_NettoPriceHUF = 0
            _ContractProductHeader.row_Products = Nothing
            _ContractProductHeader.row_QTT = 0
            _ContractProductHeader.row_RowGroup1 = ""
            _ContractProductHeader.row_RowGroup2 = ""
            _ContractProductHeader.row_Units = Nothing
            _ContractProductHeader.row_VAT = Nothing

            _ocur.CommitChanges()

            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Else
            _ContractProductHeader.row_QTT = _ContractProductHeader.row_ContractProductRows.QTT - _ContractProductHeader.row_ContractProductRows.QTT_Out
            '--------------------------------- TESZT MIATT ------------------------------->>
            Throw New Exception("*Az eladni kívánt mennyiség meghaladja maximálisan eladható mennyiséget!")
            '--------------------------------- TESZT MIATT -------------------------------<<
        End If

        '   Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    Private Sub aBackToRowEdit_ConProd_Receivables_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_ConProd_Receivables.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractProductHeader As ContractProductHeader = View.SelectedObjects(0) 'Kijelölt elem fejlécének betöltése

        Dim _ListView As ListPropertyEditor = TryCast(View, DetailView).FindItem("ContractProductRows") 'Sorok összeszedése
        If _ListView.ListView.SelectedObjects.Count > 0 Then

            Dim _ContractProductRows As ContractProductRows = _ListView.ListView.SelectedObjects(0)  'Régi sor változóba töltése

            _ContractProductHeader.row_Products = _ContractProductRows.Products
            _ContractProductHeader.row_ContractProductRows = TryCast(_ocur.FindObject(Of ContractProductRowsInOut)(CriteriaOperator.Parse("ContractProductRowsOut=?", _ContractProductRows)), ContractProductRowsInOut).ContractProductRowsIn
            _ContractProductHeader.row_NettoPriceDEV = _ContractProductRows.NettoPriceDEV
            _ContractProductHeader.row_NettoPriceHUF = _ContractProductRows.NettoPriceHUF
            _ContractProductHeader.row_QTT = _ContractProductRows.QTT
            _ContractProductHeader.row_RowGroup1 = _ContractProductRows.RowGroup1
            _ContractProductHeader.row_RowGroup2 = _ContractProductRows.RowGroup2
            _ContractProductHeader.row_Units = _ContractProductRows.Units
            _ContractProductHeader.row_VAT = _ContractProductRows.VAT

            View.Refresh()
        End If
    End Sub
    Private Sub aDown_ContractProductRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_ContractProductRows.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractProductHeader As ContractProductHeader = TryCast(View.CurrentObject, ContractProductRows).ContractProductHeader
        Dim _ContractProductRowsSelected As ContractProductRows = Nothing
        Dim _ContractProductRowsPlus As ContractProductRows = Nothing
        Dim _ContractProductRows As ContractProductRows = Nothing

        Try
            _ContractProductRowsSelected = TryCast(View.SelectedObjects(0), ContractProductRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            '--------------------------------- TESZT MIATT ------------------------------->>
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
            '--------------------------------- TESZT MIATT -------------------------------<<
        End Try

        If _ContractProductRowsSelected.RowIndex < _ContractProductHeader.ContractProductRows.Count - 1 Then
            For Each _ContractProductRows In _ContractProductHeader.ContractProductRows
                If _ContractProductRows.RowIndex = _ContractProductRowsSelected.RowIndex + 1 Then
                    _ContractProductRowsPlus = _ContractProductRows
                    Exit For
                End If
            Next

            If _ContractProductRowsPlus IsNot Nothing Then
                _ContractProductRowsSelected.RowIndex += 1
                _ContractProductRowsPlus.RowIndex -= 1
            End If

            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aUp_ContractProductRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_ContractProductRows.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractProductHeader As ContractProductHeader = TryCast(View.SelectedObjects(0), ContractProductRows).ContractProductHeader
        Dim _ContractProductRowsSelected As ContractProductRows = Nothing
        Dim _ContractProductRowsMinus As ContractProductRows = Nothing
        Dim _ContractProductRows As ContractProductRows = Nothing

        Try
            _ContractProductRowsSelected = TryCast(View.SelectedObjects(0), ContractProductRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _ContractProductRowsSelected.RowIndex > 0 Then
            For Each _ContractProductRows In _ContractProductHeader.ContractProductRows
                If _ContractProductRows.RowIndex = _ContractProductRowsSelected.RowIndex - 1 Then
                    _ContractProductRowsMinus = _ContractProductRows
                    Exit For
                End If
            Next

            If _ContractProductRowsMinus IsNot Nothing Then
                _ContractProductRowsSelected.RowIndex -= 1
                _ContractProductRowsMinus.RowIndex += 1
            End If

            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aNewContractProductHeader_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aNewContractProductHeader.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As New CollectionSource(_onew, GetType(ContractTemplate))

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Clear()
        _CS.Criteria("First") = CriteriaOperator.Parse("IsProducts=True")
        _CS.EndUpdateCriteria()

        e.View = Application.CreateListView("ContractTemplate_ListView_PopUp", _CS, False)

    End Sub
    Private Sub aNewContractProductHeader_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aNewContractProductHeader.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        If e.PopupWindow.View.SelectedObjects.Count > 0 Then
            Dim _ContractTemplate As ContractTemplate = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractTemplate)

            Dim _DetailViewName As String = ""

            If _ContractTemplate IsNot Nothing Or e.PopupWindow.View.SelectedObjects.Count = 1 Then
                Dim _ContractProductHeader As ContractProductHeader = _onew.CreateObject(Of ContractProductHeader)()
                _ContractProductHeader.CustomersFrom = _onew.FindObject(Of CustomersFrom)(CriteriaOperator.Parse("IsDefault=?", True))
                _ContractProductHeader.ContractTemplate = _onew.GetObjectByKey(Of ContractTemplate)(_ContractTemplate.Oid)

                If _ContractTemplate.PartnerType = ePartnerType.eReceivables Then
                    _DetailViewName = "ContractProductHeader_DetailView_Receivables"
                End If
                If _ContractTemplate.PartnerType = ePartnerType.ePayabels Then
                    _DetailViewName = "ContractProductHeader_DetailView_Payables"
                End If
                If _DetailViewName <> "" Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _DetailViewName, True, _ContractProductHeader)
                    e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow
                Else
                    _onew.Rollback()
                    ' --------------------------------- TESZT MIATT ------------------------------->>
                    Throw New Exception("*Váratlan hiba történt! A művelet megszakadt! " & vbNewLine & _
                                        "Valószínüleg hibásan került be a rendszerbe egy vagy több szerződési sablon!" & vbNewLine & _
                                        "Kérem ellenőrizze a szerződési sablonokat!")
                    '    --------------------------------- TESZT MIATT -------------------------------<<
                End If
            Else
                _onew.Rollback()
                '   --------------------------------- TESZT MIATT ------------------------------->>
                Throw New Exception("*Egynél több sor van kiválasztva!")
                '    --------------------------------- TESZT MIATT -------------------------------<<
            End If
        Else
            _onew.Rollback()
            '   --------------------------------- TESZT MIATT ------------------------------->>
            Throw New Exception("*Nincs sor kiválasztva!")
            '    --------------------------------- TESZT MIATT -------------------------------<<
        End If
    End Sub
    Private Sub aFinalizeContractProduct_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalizeContractProduct.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractProductHeader As ContractProductHeader = TryCast(View.SelectedObjects(0), ContractProductHeader)
        If _ContractProductHeader IsNot Nothing Then
            Dim _CustomersNGCollection As CustomersNGCollection = Nothing

            Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
            If _ContractProductHeader.ContractTemplate.PartnerType = ePartnerType.eReceivables Then
                _CustomersNGCollection = _ocur.FindObject(Of CustomersNGCollection)(CriteriaOperator.Parse("Customers=? AND ContractTemplate=?", _
                                                        _ContractProductHeader.Customers, _ContractProductHeader.ContractTemplate))
            End If

            _ContractProductHeader.State = State.Ready

            If _ContractProductHeader.DocumentNumber = "" Then
                If _CustomersNGCollection IsNot Nothing Then
                    _ContractProductHeader.DocumentNumber = _CustomersNGCollection.NumberGenerator.GetNewNumber(_CustomersNGCollection.NumberGenerator)
                Else
                    _ContractProductHeader.DocumentNumber = _ContractProductHeader.ContractTemplate.NumberGenerator.GetNewNumber(_ContractProductHeader.ContractTemplate.NumberGenerator)
                End If
            End If

            _ocur.CommitChanges()
            Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aSendBackToEditContractProduct_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSendBackToEditContractProduct.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractProductHeader As ContractProductHeader = TryCast(View.SelectedObjects(0), ContractProductHeader)
        If _ContractProductHeader IsNot Nothing Then
            _ContractProductHeader.State = State.Preparation
            _ocur.CommitChanges()
        End If
    End Sub
    Private Sub aTotableD_ConProd_Receivables_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aTotableD_ConProd_Receivables.CustomizePopupWindowParams
        Dim _ContractProductHeader_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("ContractProductRows")
        If _ContractProductHeader_ListEditor.ListView.SelectedObjects.Count > 0 Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CS As New CollectionSource(_onew, GetType(ContractProductRowsInOut))
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria("First") = CriteriaOperator.Parse("1=2") 'CriteriaOperator.Parse("ContractProductRowsOut=?", _ContractProductRows)
            _CS.EndUpdateCriteria()

            Dim _ContractProductRows As ContractProductRows = _ContractProductHeader_ListEditor.ListView.SelectedObjects(0)
            Dim _ContractProductRowsInOut_List As XPCollection = _onew.CreateCollection(GetType(ContractProductRowsInOut), CriteriaOperator.Parse("ContractProductRowsOut=?", _onew.GetObject(_ContractProductRows)))

            For Each _ContractProductRowsInOut As ContractProductRowsInOut In _ContractProductRowsInOut_List

                _CS.Add(_ContractProductRowsInOut)
            Next

            e.View = Application.CreateListView("ContractProductRowsInOut_ListView", _CS, False)
        Else
            '--------------------------------- TESZT MIATT ------------------------------->>
            Throw New Exception("*Nincs kiválasztva egy sor sem!")
            '--------------------------------- TESZT MIATT -------------------------------<<
        End If
    End Sub
    Private Sub aTotableD_ConProd_Receivables_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aTotableD_ConProd_Receivables.Execute
        If e.PopupWindow.View.SelectedObjects.Count > 0 Then
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ContractProductHeader As ContractProductHeader = TryCast(View.SelectedObjects(0), ContractProductHeader)
            Dim _ContractProductHeader_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("ContractProductRows")
            Dim _ContractProductRows As ContractProductRows = _ContractProductHeader_ListEditor.ListView.SelectedObjects(0)

            For Each _ContractProductRowsInOut In e.PopupWindow.View.SelectedObjects
                Dim _ContractProductRowsEvent As ContractProductRowsEvent = _ocur.CreateObject(Of ContractProductRowsEvent)()
                Dim _ContractProductRowsInOut_Ocur As ContractProductRowsInOut = _ocur.GetObjectByKey(Of ContractProductRowsInOut)(TryCast(_ContractProductRowsInOut, ContractProductRowsInOut).Oid)
                _ContractProductRowsInOut_Ocur.ContractProductRowsIn.QTT_Out -= _ContractProductRowsInOut_Ocur.QTT
                _ContractProductRows.QTT -= _ContractProductRowsInOut_Ocur.QTT

                _ContractProductRowsEvent.ContractProductRowsIn = _ContractProductRowsInOut_Ocur.ContractProductRowsIn
                _ContractProductRowsEvent.ContractProductRowsOut = _ContractProductRows
                _ContractProductRowsEvent.QTT_In = 0
                _ContractProductRowsEvent.QTT_Out = _ContractProductRows.QTT

                _ContractProductRowsInOut_Ocur.RemoveRows()
                If _ContractProductRows.QTT = 0 Then
                    _ContractProductRows.RemoveRows()
                End If
            Next

            _ocur.CommitChanges()
        Else
            '--------------------------------- TESZT MIATT ------------------------------->>
            Throw New Exception("*Nincs kiválasztva egy sor sem! A törlés nem hajtahtó végre!")
            '--------------------------------- TESZT MIATT -------------------------------<<
        End If
        View.Refresh()
    End Sub

    Private Sub aPrintContractProduct_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aPrintContractProduct.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractProductHeader As ContractProductHeader = TryCast(View.SelectedObjects(0), ContractProductHeader)
        Try
            If _ContractProductHeader IsNot Nothing Then
                Dim _ReportData As DevExpress.Persistent.BaseImpl.ReportData = _ContractProductHeader.ContractTemplate.ReportData

                If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!")

                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("ContractProductHeader.Oid=?", _ContractProductHeader.Oid), True)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
