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.PivotGrid.Win
Imports DevExpress.XtraPivotGrid
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo

Public Class ProjectPlanVC
    Inherits SISBusiness.Module.ProjectPlanVC
    Private _PivotGridListEditor As PivotGridListEditor
    Private _PivotGrdidControl As PivotGridControl
    Private _Criteria As String = ""
    Dim _WaitFormClass As New WaitFormClass
    Public Sub New()
        MyBase.New()

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

    End Sub

    Private Sub aBindPlan_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBindPlan.Execute
        Dim _form As New ProjectPlanForm

        _form.StartForm(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of ProjectPlanVC).aProjectPlanViewFactPivot.Active.SetItemValue("", False)

        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.Initwork
        AddHandler FinalWork, AddressOf _WaitFormClass.Finalwork

        If View.Id = "ProjectPlan_ProjectPlanRows_ListView_Pivot" Or View.Id = "ProjectPlan_ProjectPlanRows_ListView_Pivot_Controlling" Then
            Frame.GetController(Of ProjectPlanVC).aProjectPlanViewFactPivot.Active.SetItemValue("", True)
        End If

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If _PivotGrdidControl IsNot Nothing Then
            RemoveHandler _PivotGrdidControl.CellClick, AddressOf PivotGrdidControl_CellClick
        End If
        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.Initwork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.Finalwork
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "ProjectPlan_ProjectPlanRows_ListView_Pivot" Or View.Id = "ProjectPlan_ProjectPlanRows_ListView_Pivot_Controlling" Then
            _PivotGridListEditor = TryCast(TryCast(View, ListView).Editor, PivotGridListEditor)

            If _PivotGridListEditor IsNot Nothing Then
                _PivotGrdidControl = _PivotGridListEditor.PivotGridControl
                AddHandler _PivotGrdidControl.CellClick, AddressOf PivotGrdidControl_CellClick
            End If

        End If
    End Sub

    Private Sub PivotGrdidControl_CellClick(sender As Object, e As PivotCellEventArgs)
        Dim _RowPivotGridField As PivotGridField
        Dim _ColumnPivotGridField As PivotGridField
        Dim s As String
        If e.RowValueType = PivotGridValueType.Value Then
            If e.ColumnValueType = PivotGridValueType.Value Then
                _Criteria = ""
                For Each _RowPivotGridField In e.GetRowFields
                    s = CStr(_PivotGrdidControl.GetFieldValue(_RowPivotGridField, e.RowIndex))
                    If LTrim(RTrim(s)) <> "" Then
                        If _Criteria = "" Then
                            _Criteria = _RowPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        Else
                            _Criteria = _Criteria & " and " & _RowPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        End If
                    End If
                Next

                For Each _ColumnPivotGridField In e.GetColumnFields
                    s = CStr(_PivotGrdidControl.GetFieldValue(_ColumnPivotGridField, e.ColumnIndex))
                    If LTrim(RTrim(s)) <> "" Then
                        If _Criteria = "" Then
                            _Criteria = _ColumnPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        Else
                            _Criteria = _Criteria & " and " & _ColumnPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        End If
                    End If
                Next
            ElseIf e.ColumnValueType = PivotGridValueType.GrandTotal Then
                _Criteria = ""
                For Each _RowPivotGridField In e.GetRowFields
                    s = CStr(_PivotGrdidControl.GetFieldValue(_RowPivotGridField, e.RowIndex))
                    If LTrim(RTrim(s)) <> "" Then
                        If _Criteria = "" Then
                            _Criteria = _RowPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        Else
                            _Criteria = _Criteria & " and " & _RowPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        End If
                    End If
                Next

                For Each _ColumnPivotGridField In e.GetColumnFields
                    s = CStr(_PivotGrdidControl.GetFieldValue(_ColumnPivotGridField, e.ColumnIndex))
                    If LTrim(RTrim(s)) <> "" Then
                        If _Criteria = "" Then
                            _Criteria = _ColumnPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        Else
                            _Criteria = _Criteria & " and " & _ColumnPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        End If
                    End If
                Next
            End If
        End If
    End Sub
    Private Sub aProjectPlanViewFactPivot_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanViewFactPivot.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProjectPlan As ProjectPlan = Nothing

        If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource) IsNot Nothing Then
            _ProjectPlan = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, ProjectPlan)
        Else
            If TryCast(View, ListView).CollectionSource.List.Count > 0 Then
                _ProjectPlan = TryCast(TryCast(View, ListView).CollectionSource.List(0), ProjectPlanRows).ProjectPlan
            End If
        End If

        If _ProjectPlan Is Nothing Then Exit Sub

        If LTrim(RTrim(_Criteria <> "")) Then

            Dim _ProjectPlanRows As ProjectPlanRows = _onew.FindObject(Of ProjectPlanRows)(CriteriaOperator.Parse(_Criteria))

            If _ProjectPlanRows IsNot Nothing Then

                Dim _GLRows_Controlling As New CollectionSource(_onew, GetType(GLRows))


                '_GLRows.GLHeader.DateExecution.Date

                _GLRows_Controlling.BeginUpdateCriteria()
                _GLRows_Controlling.Criteria("Plan1") = CriteriaOperator.Parse("(DebitChartOfAccounts.AccountNumber=? or CreditChartOfAccounts.AccountNumber=?)", _ProjectPlanRows.AccountNumber, _ProjectPlanRows.AccountNumber)
                If _ProjectPlanRows.Controlling Is Nothing Then
                    _GLRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("isnull(Controlling)=True")
                Else
                    _GLRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("Controlling=?", _ProjectPlanRows.Controlling)
                End If
                If _ProjectPlanRows.CostHolder Is Nothing Then
                    _GLRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("isnull(CostHolder)=True")
                Else
                    _GLRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("CostHolder=?", _ProjectPlanRows.CostHolder)
                End If
                If _ProjectPlanRows.CostPlace Is Nothing Then
                    _GLRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("isnull(CostPlace)=True")
                Else
                    _GLRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("CostPlace=?", _ProjectPlanRows.CostPlace)
                End If
                If _ProjectPlanRows.JobNumberObjects Is Nothing Then
                    _GLRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("isnull(JobNumberObjects)=True")
                Else
                    _GLRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("JobNumberObjects=?", _ProjectPlanRows.JobNumberObjects)
                End If
                If _ProjectPlanRows.UniqueObjects Is Nothing Then
                    _GLRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("isnull(UniqueObjects)=True")
                Else
                    _GLRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("UniqueObjects=?", _ProjectPlanRows.UniqueObjects)
                End If
                _GLRows_Controlling.Criteria("DateExecution") = CriteriaOperator.Parse("GetYear(GLHeader.DateExecution)=? and GetMonth(GLHeader.DateExecution)=? and GLHeader.IsEditable=False and GLHeader.CustomersFrom=?", _ProjectPlanRows.DateYear, _ProjectPlanRows.DateMonth, _ProjectPlanRows.ProjectPlan.CustomersFrom)
                _GLRows_Controlling.EndUpdateCriteria()


                e.ShowViewParameters.CreatedView = Application.CreateListView("GLRows_ListView_ProjectPlan", _GLRows_Controlling, True)
            End If
        End If
    End Sub

    Private Sub aProjectPlanEditPlan_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanEditPlan.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProjectPlan As ProjectPlan = Nothing

        If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource) IsNot Nothing Then
            _ProjectPlan = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, ProjectPlan)
        Else
            If TryCast(View, ListView).CollectionSource.List.Count > 0 Then
                _ProjectPlan = TryCast(TryCast(View, ListView).CollectionSource.List(0), ProjectPlanRows).ProjectPlan
            End If
        End If

        If _ProjectPlan Is Nothing Then Exit Sub

        If LTrim(RTrim(_Criteria <> "")) Then
            Dim _ProjectPlanRows_Collection As New CollectionSource(_onew, GetType(ProjectPlanRows))
            _ProjectPlanRows_Collection.BeginUpdateCriteria()
            _ProjectPlanRows_Collection.Criteria("Criteris") = CriteriaOperator.Parse(_Criteria)
            _ProjectPlanRows_Collection.EndUpdateCriteria()

            e.ShowViewParameters.CreatedView = Application.CreateListView("ProjectPlanRows_ListView_Controlling", _ProjectPlanRows_Collection, True)
        End If

    End Sub

    Private Sub aProjectPlanViewContractsPivot_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanViewContractsPivot.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProjectPlan As ProjectPlan = Nothing

        If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource) IsNot Nothing Then
            _ProjectPlan = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, ProjectPlan)
        Else
            If TryCast(View, ListView).CollectionSource.List.Count > 0 Then
                _ProjectPlan = TryCast(TryCast(View, ListView).CollectionSource.List(0), ProjectPlanRows).ProjectPlan
            End If
        End If

        If _ProjectPlan Is Nothing Then Exit Sub

        If LTrim(RTrim(_Criteria <> "")) Then

            Dim _ProjectPlanRows As ProjectPlanRows = _onew.FindObject(Of ProjectPlanRows)(CriteriaOperator.Parse(_Criteria))

            If _ProjectPlanRows IsNot Nothing Then

                Dim _ContractRows_Controlling As New CollectionSource(_onew, GetType(ContractRows))


                '_ContractRows.GLHeader.DateExecution.Date

                _ContractRows_Controlling.BeginUpdateCriteria()

                If _ProjectPlanRows.Controlling Is Nothing Then
                    _ContractRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("isnull(Controlling)=True")
                Else
                    _ContractRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("Controlling=?", _ProjectPlanRows.Controlling)
                End If
                If _ProjectPlanRows.CostHolder Is Nothing Then
                    _ContractRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("isnull(CostHolder)=True")
                Else
                    _ContractRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("CostHolder=?", _ProjectPlanRows.CostHolder)
                End If
                If _ProjectPlanRows.CostPlace Is Nothing Then
                    _ContractRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("isnull(CostPlace)=True")
                Else
                    _ContractRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("CostPlace=?", _ProjectPlanRows.CostPlace)
                End If
                If _ProjectPlanRows.JobNumberObjects Is Nothing Then
                    _ContractRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("isnull(JobNumberObjects)=True")
                Else
                    _ContractRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("JobNumberObjects=?", _ProjectPlanRows.JobNumberObjects)
                End If
                If _ProjectPlanRows.UniqueObjects Is Nothing Then
                    _ContractRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("isnull(UniqueObjects)=True")
                Else
                    _ContractRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("UniqueObjects=?", _ProjectPlanRows.UniqueObjects)
                End If
                _ContractRows_Controlling.Criteria("DateExecution") = CriteriaOperator.Parse("GetYear(DateExecution)=? and GetMonth(DateExecution)=? and Contracts.CustomersFrom=?", _ProjectPlanRows.DateYear, _ProjectPlanRows.DateMonth, _ProjectPlanRows.ProjectPlan.CustomersFrom)
                _ContractRows_Controlling.EndUpdateCriteria()


                e.ShowViewParameters.CreatedView = Application.CreateListView("ContractRows_ListView_ProjectPlan", _ContractRows_Controlling, True)
            End If
        End If
    End Sub
End Class
