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.Filtering
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Public Class GLRowsDivideVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of GLRowsDivideVC).aFindPCI.Active.SetItemValue("", False)
        Frame.GetController(Of GLRowsDivideVC).aToTable_GLRowsDivide.Active.SetItemValue("", False)
        Frame.GetController(Of GLRowsDivideVC).aToTableD_GLRowsDivide.Active.SetItemValue("", False)
        If View.Id Like "GLRowsDivideH_GLRows_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id Like "GLRowsDivideH_GLRowsDivideR_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id Like "GLRowsDivideH_DetailView*" Then
            Frame.GetController(Of GLRowsDivideVC).aFindPCI.Active.SetItemValue("", True)
            Frame.GetController(Of GLRowsDivideVC).aToTable_GLRowsDivide.Active.SetItemValue("", True)
            Frame.GetController(Of GLRowsDivideVC).aToTableD_GLRowsDivide.Active.SetItemValue("", True)
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf GLRowsDivideH_DetailView_AcceptAction_Executing
        End If
        If View.Id = "GLRowsDivideH_GLRows_ListView_ConnectionDivide" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "GLRowsDivideH_GLRowsDivideR_ListView_ConnectionDivide" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
    End Sub
    Sub GLRowsDivideH_DetailView_AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, "DialogOk_Contexts")
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id Like "GLRowsDivideH_GLRows_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id Like "GLRowsDivideH_GLRowsDivideR_ListView*" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id Like "GLRowsDivideH_DetailView*" Then
            Frame.GetController(Of GLRowsDivideVC).aFindPCI.Active.SetItemValue("", False)
            Frame.GetController(Of GLRowsDivideVC).aToTable_GLRowsDivide.Active.SetItemValue("", False)
            Frame.GetController(Of GLRowsDivideVC).aToTableD_GLRowsDivide.Active.SetItemValue("", True)
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf GLRowsDivideH_DetailView_AcceptAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        MyBase.OnViewControlsCreated()
        If View.Id = "GLRowsDivideH_DetailView" Then
            Dim _GLRowsDivideH As GLRowsDivideH = TryCast(View.SelectedObjects(0), GLRowsDivideH)
            If _GLRowsDivideH.GLRowsBase IsNot Nothing Then
                If _GLRowsDivideH.GLRowsBase.Customer IsNot Nothing Then
                    _GLRowsDivideH.row_Customers = _ocur.FindObject(Of Customers)(CriteriaOperator.Parse("Oid=?", _GLRowsDivideH.GLRowsBase.Customer.Oid))
                    _GLRowsDivideH.row_CashType = _GLRowsDivideH.GLRowsBase.CashType
                    _GLRowsDivideH.row_PartnerType = _GLRowsDivideH.GLRowsBase.PartnerType
                    _GLRowsDivideH.row_ConnectInfo = _GLRowsDivideH.GLRowsBase.ConnectInfo
                    _GLRowsDivideH.row_Controlling = _GLRowsDivideH.GLRowsBase.Controlling
                    _GLRowsDivideH.row_CostHolder = _GLRowsDivideH.GLRowsBase.CostHolder
                    _GLRowsDivideH.row_CostPlace = _GLRowsDivideH.GLRowsBase.CostPlace
                    _GLRowsDivideH.row_UniqueObjects = _GLRowsDivideH.GLRowsBase.UniqueObjects
                    _GLRowsDivideH.row_JobNumberObjects = _GLRowsDivideH.GLRowsBase.JobNumberObjects
                    _GLRowsDivideH.row_NoteRow = _GLRowsDivideH.GLRowsBase.NoteRows

                End If
            End If

        End If
    End Sub
    Private Sub aFindPCI_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aFindPCI.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _GLRowsDivideH As GLRowsDivideH = TryCast(View.SelectedObjects(0), GLRowsDivideH)
        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(PCIGroup))



        If _GLRowsDivideH IsNot Nothing Then
            If _GLRowsDivideH.row_Customers IsNot Nothing Then
                _CollectionSource.Criteria.Clear()
                _CollectionSource.BeginUpdateCriteria()
                _CollectionSource.Criteria("1") = CriteriaOperator.Parse("Customers.Oid=?", _GLRowsDivideH.row_Customers.Oid)
                _CollectionSource.Criteria("2") = CriteriaOperator.Parse("PartnerType=?", _GLRowsDivideH.row_PartnerType)
                _CollectionSource.Criteria("3") = CriteriaOperator.Parse("CustomersFrom.Oid=?", _GLRowsDivideH.GLRowsBase.GLHeader.CustomersFrom.Oid)
                _CollectionSource.Criteria("4") = CriteriaOperator.Parse("(BallanceHUF<>0 or BallanceDEV<>0)")
                _CollectionSource.Criteria("5") = CriteriaOperator.Parse("(CurrencyName_Account.ShortName=? or CurrencyName_Account.ShortName='HUF')", _GLRowsDivideH.GLRowsBase.LiquidAssets.CurrencyName.ShortName)
                _CollectionSource.EndUpdateCriteria()

                e.View = Application.CreateListView("PCIGroup_ListView_Find", _CollectionSource, True)
            Else
                _CollectionSource.Criteria.Clear()
                _CollectionSource.BeginUpdateCriteria()
                _CollectionSource.Criteria("1") = CriteriaOperator.Parse("PartnerType=?", _GLRowsDivideH.row_PartnerType)
                _CollectionSource.Criteria("2") = CriteriaOperator.Parse("CustomersFrom.Oid=?", _GLRowsDivideH.GLRowsBase.GLHeader.CustomersFrom.Oid)
                _CollectionSource.EndUpdateCriteria()

                e.View = Application.CreateListView("PCIGroup_ListView_Find", _CollectionSource, True)
            End If
        Else
            'Ez - elméletileg - nem fordulhat elő.
        End If
    End Sub

    Private Sub aFindPCI_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aFindPCI.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGroup As PCIGroup
        Dim _GLRowsDivideH As GLRowsDivideH = TryCast(View.SelectedObjects(0), GLRowsDivideH)
        Dim _GLRowsDivideR As GLRowsDivideR
        For Each _PCIGroup In e.PopupWindow.View.SelectedObjects
            _GLRowsDivideR = _ocur.CreateObject(Of GLRowsDivideR)()
            _GLRowsDivideR.GLRowsDivideH = _GLRowsDivideH
            _GLRowsDivideR.CashType = eCashType.ePartner
            _GLRowsDivideR.ChartOfAccounts = Nothing
            _GLRowsDivideR.Customers = _ocur.GetObject(_PCIGroup.Customers)
            _GLRowsDivideR.ConnectInfo = _PCIGroup.ConnectInfo
            _GLRowsDivideR.AmountDEV = Math.Abs(_PCIGroup.BallanceDEV)
            _GLRowsDivideR.AmountHUF = Math.Abs(_PCIGroup.BallanceHUF)
            _GLRowsDivideR.Save()

        Next
        _ocur.CommitChanges()
        View.Refresh()
    End Sub

    Private Sub aToTable_GLRowsDivide_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_GLRowsDivide.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRowsDivideH As GLRowsDivideH = TryCast(View.SelectedObjects(0), GLRowsDivideH)
        Dim _GLRowsDivideR As GLRowsDivideR
        If _GLRowsDivideH IsNot Nothing Then

            If _GLRowsDivideH.GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLMixed Then
                If _GLRowsDivideH.row_CreditChartOfAccounts Is _GLRowsDivideH.row_DebitChartOfAccounts Then
                    Throw New Exception("*Nem lehet ugyan az a tartozik és a követel főkönyvi szám !")
                End If
            End If


            _GLRowsDivideR = _ocur.CreateObject(Of GLRowsDivideR)()
            _GLRowsDivideR.GLRowsDivideH = _GLRowsDivideH
            _GLRowsDivideR.CashType = _GLRowsDivideH.row_CashType
            If _GLRowsDivideH.row_CashType = eCashType.eNormal Then
                _GLRowsDivideR.ChartOfAccounts = _GLRowsDivideH.row_ChartOfAccounts
            Else
                _GLRowsDivideR.ChartOfAccounts = Nothing
            End If
            If _GLRowsDivideH.GLRowsBase.GLHeader.GLDocumentType = eGLDocumentType.eGLMixed Then
                _GLRowsDivideR.DebitChartOfAccounts = _GLRowsDivideH.row_DebitChartOfAccounts
                _GLRowsDivideR.CreditChartOfAccounts = _GLRowsDivideH.row_CreditChartOfAccounts
            End If

            _GLRowsDivideR.AmountHUF = _GLRowsDivideH.row_AmountHUF
            _GLRowsDivideR.AmountDEV = _GLRowsDivideH.row_AmountDEV
            _GLRowsDivideR.ConnectInfo = _GLRowsDivideH.row_ConnectInfo
            _GLRowsDivideR.Customers = _GLRowsDivideH.row_Customers
            _GLRowsDivideR.NoteRows = _GLRowsDivideH.row_NoteRow

            '-- Jelölő adatok
            _GLRowsDivideR.Controlling = _GLRowsDivideH.row_Controlling
            _GLRowsDivideR.CostHolder = _GLRowsDivideH.row_CostHolder
            _GLRowsDivideR.CostPlace = _GLRowsDivideH.row_CostPlace
            _GLRowsDivideR.JobNumberObjects = _GLRowsDivideH.row_JobNumberObjects
            _GLRowsDivideR.UniqueObjects = _GLRowsDivideH.row_UniqueObjects

            '--
            _GLRowsDivideR.Save()

            View.Refresh()

            _GLRowsDivideH.row_AmountDEV = _GLRowsDivideH.AmountDiffDEV
            _GLRowsDivideH.row_AmountHUF = _GLRowsDivideH.AmountDiffHUF
        End If

    End Sub

    Private Sub aToTableD_GLRowsDivide_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_GLRowsDivide.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLRowsDivideH As GLRowsDivideH = TryCast(View.SelectedObjects(0), GLRowsDivideH)
        'Törli a táblázatban lévő sort
        Dim _GLRowsDivideR_ListEditor As ListPropertyEditor
        Dim _GLRowsDivideR As GLRowsDivideR
        Dim i As Long
        _GLRowsDivideR_ListEditor = TryCast(View, DetailView).FindItem("GLRowsDivideR")

        _GLRowsDivideR = TryCast(_GLRowsDivideR_ListEditor.ListView.CurrentObject, GLRowsDivideR)
        If _GLRowsDivideR IsNot Nothing Then
            _GLRowsDivideR.Delete()
            View.Refresh()
        End If


    End Sub
End Class
