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

Public Class BallanceVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _User As User

        If _ocur IsNot Nothing Then
            _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        End If

        If View.Id = "BallanceHeader_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "' or 'SysAdmin'='" & SecuritySystem.CurrentUserName & "'"
        End If
        If View.Id = "BallanceHeader_DetailView" Then
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "' or 'SysAdmin'='" & SecuritySystem.CurrentUserName & "'"
            Frame.GetController(Of BallanceVC).aGenerateBallanceReport.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "' or 'SysAdmin'='" & SecuritySystem.CurrentUserName & "'"
        End If

        If View.Id = "TrialBalanceHeader_DetailView_BallanceHeader_Current" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _BallanceHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), BallanceHeader)
            If _BallanceHeader IsNot Nothing Then
                View.CurrentObject = _ocur.GetObject(_BallanceHeader.TrialBalanceHeader)
            End If
        End If
        If View.Id = "TrialBalanceHeader_DetailView_BallanceHeader_Previous" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)

            Dim _BallanceHeader = TryCast(TryCast(Frame, NestedFrame).ViewItem.View.SelectedObjects(0), BallanceHeader)
            If _BallanceHeader IsNot Nothing Then
                View.CurrentObject = _ocur.GetObject(_BallanceHeader.TrialBalanceHeaderBefore)
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "BallanceHeader_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aGenerateBallanceReport_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateBallanceReport.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _BallanceHeader As BallanceHeader = TryCast(View.SelectedObjects(0), BallanceHeader)

        If _BallanceHeader IsNot Nothing Then
            If _BallanceHeader.TrialBalanceHeader IsNot Nothing Then Frame.GetController(Of SISBusiness.Module.TrialBalanceVC).LoadTrialBalanceNew(_ocur, _BallanceHeader.TrialBalanceHeader)
            If _BallanceHeader.TrialBalanceHeaderBefore IsNot Nothing Then Frame.GetController(Of SISBusiness.Module.TrialBalanceVC).LoadTrialBalanceNew(_ocur, _BallanceHeader.TrialBalanceHeaderBefore)
        End If
    End Sub
End Class
