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 System.Reflection
Imports DevExpress.Data.Filtering

Public Class Support_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "CRM_Opportunities_ListView_Support" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "Contracts_ListView_Support" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "PCIGroup_ListView_Support" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid = ?", SecuritySystem.CurrentUserId))
            Frame.GetController(Of PCIVC).aRepairPCIGroup.Active.SetItemValue("", False)
            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("BySubcontractorPCI") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("BySubcontractorPCI") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("BySubcontractorPCI") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "OfferOutHeader_ListView_Support" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            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)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OfferOutHeader_ListView_Support" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            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)
        End If
    End Sub
    Private Sub aCreateSupportUserRoles_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateSupportUserRoles.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CS As New CollectionSource(_onew, GetType(SupportRoles))

            e.View = Application.CreateListView("SupportRoles_ListView", _CS, False)
        Catch ex As Exception
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim sf As StackFrame = New StackFrame()
            Dim _ErrorLog As ErrorLog = _onew.CreateObject(Of ErrorLog)()
            _ErrorLog.Description = ex.Message
            _ErrorLog.SubName = TryCast(sf.GetMethod(), MethodBase).Name
            _onew.CommitChanges()
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aCreateSupportUserRoles_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateSupportUserRoles.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _SupportRoles As SupportRoles = TryCast(e.PopupWindow.View.SelectedObjects(0), SupportRoles)

            For Each _User As DevExpress.Persistent.BaseImpl.User In View.SelectedObjects
                Dim _SupportUserRoles As SupportUserRoles = _ocur.CreateObject(Of SupportUserRoles)()
                _SupportUserRoles.User = _User
                _SupportUserRoles.SupportRoles = _ocur.GetObject(_SupportRoles)
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim sf As StackFrame = New StackFrame()
            Dim _ErrorLog As ErrorLog = _onew.CreateObject(Of ErrorLog)()
            _ErrorLog.Description = ex.Message
            _ErrorLog.SubName = TryCast(sf.GetMethod(), MethodBase).Name
            _onew.CommitChanges()
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
End Class
