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

Public Class RegistryRowsFinancialControllingVC
    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()
        If View.Id Like "RegistryRowsFinancialControlling_ListView_Reinvoice*" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "RegistryHeader_RegistryRowsFinancialControlling_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)

            'If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject IsNot Nothing Then
            '    Dim _RegistryHeader As RegistryHeader = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
            '    If _RegistryHeader IsNot Nothing Then
            '        Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader)
            '        'AddHandler _RegistryHeader.Changed, AddressOf RegistryHeader_RegistryRowsFinancialControlling_ListView_MasterObjectChanged

            '        AddHandler View.ObjectSpace.ObjectChanged, AddressOf _RegistryRowsFinancialControlling_ListView_ObjectChanged
            '        AddHandler View.ObjectSpace.ObjectSaved, AddressOf _RegistryRowsFinancialControlling_ListView_ObjectSaved
            '        AddHandler View.ObjectSpace.ObjectReloaded, AddressOf _RegistryRowsFinancialControlling_ListView_ObjectReloaded
            '        AddHandler View.ObjectSpace.ObjectDeleted, AddressOf _RegistryRowsFinancialControlling_ListView_ObjectDeleted
            '    End If
            'End If
        End If
        If View.Id = "RegistryHeader_ListView_Subcontractor" Then
            Frame.GetController(Of RegistryRowsFinancialControllingVC).aViewRegistryNoTask.Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id Like "RegistryRowsFinancialControlling_ListView_Reinvoice*" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "RegistryHeader_RegistryRowsFinancialControlling_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            'If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject IsNot Nothing Then
            '    Dim _RegistryHeader As RegistryHeader = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
            '    If _RegistryHeader IsNot Nothing Then
            '        RemoveHandler _RegistryHeader.Changed, AddressOf RegistryHeader_RegistryRowsFinancialControlling_ListView_MasterObjectChanged
            '    End If
            'End If
        End If
        If View.Id = "RegistryHeader_ListView_Subcontractor" Then
            Frame.GetController(Of RegistryRowsFinancialControllingVC).aViewRegistryNoTask.Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aOpenContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenContractRows.Execute
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling = TryCast(View.SelectedObjects(0), RegistryRowsFinancialControlling)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _onew.GetObject(_RegistryRowsFinancialControlling.ContractRows.Contracts))
    End Sub

    Private Sub RegistryHeader_RegistryRowsFinancialControlling_ListView_MasterObjectChanged(sender As Object, e As DevExpress.Xpo.ObjectChangeEventArgs)
        Dim _RegistryHeader As RegistryHeader = TryCast(e.Object, RegistryHeader)
        Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader)
    End Sub

    Private Sub _RegistryRowsFinancialControlling_ListView_ObjectChanged(sender As Object, e As DevExpress.ExpressApp.ObjectChangedEventArgs)
        Dim _RegistryHeader As RegistryHeader = TryCast(e.Object, RegistryHeader)
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling = TryCast(e.Object, RegistryRowsFinancialControlling)
        If _RegistryHeader IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader)
        If _RegistryRowsFinancialControlling IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryRowsFinancialControlling.RegistryHeader)
    End Sub
    Private Sub _RegistryRowsFinancialControlling_ListView_ObjectReloaded(sender As Object, e As ObjectManipulatingEventArgs)
        Dim _RegistryHeader As RegistryHeader = TryCast(e.Object, RegistryHeader)
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling = TryCast(e.Object, RegistryRowsFinancialControlling)
        If _RegistryHeader IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader)
        If _RegistryRowsFinancialControlling IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryRowsFinancialControlling.RegistryHeader)
    End Sub
    Private Sub _RegistryRowsFinancialControlling_ListView_ObjectSaved(sender As Object, e As ObjectManipulatingEventArgs)
        Dim _RegistryHeader As RegistryHeader = TryCast(e.Object, RegistryHeader)
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling = TryCast(e.Object, RegistryRowsFinancialControlling)
        If _RegistryHeader IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader)
        If _RegistryRowsFinancialControlling IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryRowsFinancialControlling.RegistryHeader)
    End Sub
    Private Sub _RegistryRowsFinancialControlling_ListView_ObjectDeleted(sender As Object, e As ObjectsManipulatingEventArgs)
        If e.Objects.Count <= 0 Then Exit Sub
        Dim _Object As Object = Nothing
        For Each _Object In e.Objects

        Next
        Dim _RegistryHeader As RegistryHeader = TryCast(_Object, RegistryHeader)
        Dim _RegistryRowsFinancial As RegistryRowsFinancial = TryCast(_Object, RegistryRowsFinancial)
        If _RegistryHeader IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader)
        If _RegistryRowsFinancial IsNot Nothing Then Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryRowsFinancial.RegistryHeader)
    End Sub

    Private Sub Action_NEW_DELETE_Hide_RegistryHeader_RegistryRowsFinancialControlling(_RegistryHeader As RegistryHeader)
        '// Mikor NE látszódjon a NEW action 
        If _RegistryHeader Is Nothing Then Exit Sub
        If _RegistryHeader.CurrentWorkflowSystemSteps Is Nothing Then Exit Sub

        Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
        If _RegistryHeader.CurrentWorkflowSystemSteps.StepFinancialType = eStepFinancialType.eCheckVATPlusControlling Then
            If _RegistryHeader.IsEditable = True Then
                If _RegistryHeader.BruttoAmountRemaining = 0 And _RegistryHeader.F_AmountBruttoHUF <> 0 Then
                    If _RegistryHeader.NettoAmountRemaining <> 0 Then
                        Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
                    End If
                End If
            End If
        End If

        '// Mikor NE látszódjon a törlés gomb
        Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        If _RegistryHeader.CurrentWorkflowSystemSteps.StepFinancialType = eStepFinancialType.eCheckVATPlusControlling Then
            If _RegistryHeader.IsEditable = True Then
                Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            End If
        End If


    End Sub
    Private Sub aViewRegistryNoTask_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistryNoTask.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As New CollectionSource(_onew, GetType(RegistryHeader))

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Clear()
        _CS.Criteria("First") = CriteriaOperator.Parse("1=2")
        _CS.EndUpdateCriteria()

        Dim _RegistryHeader_Collection As New XPCollection(Of RegistryHeader)(_onew.Session, CriteriaOperator.Parse("RegistryType.RegistryMainType='eAccount' and RegistryAcceptState='eAccept' and CurrentWorkflowSystemSteps.IsLastStep=False"))
        Dim _RegistryHeader As RegistryHeader
        Dim _RegistryTasks As RegistryTasks

        For Each _RegistryHeader In _RegistryHeader_Collection
            _RegistryTasks = _onew.FindObject(Of RegistryTasks)(CriteriaOperator.Parse("RegistryHeader=? and Status in ('InProgress','NotStarted','WaitingForSomeoneElse')", _RegistryHeader))
            If _RegistryTasks Is Nothing Then
                _CS.Add(_RegistryHeader)
            End If
        Next

        e.ShowViewParameters.CreatedView = Application.CreateListView("RegistryHeader_ListView_NoTask", _CS, True)
    End Sub
End Class
