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

Public Class CustomersMeeting_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "CustomersMeeting_CustomersMeetingDocuments_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "CustomersMeeting_ListView_Support" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _User As DevExpress.Persistent.BaseImpl.User = TryCast(SecuritySystem.CurrentUser, DevExpress.Persistent.BaseImpl.User)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(DevExpress.Data.Filtering.CriteriaOperator.Parse("User = ?", _ocur.GetObject(_User)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("BySupportCM") = DevExpress.Data.Filtering.CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("BySupportCM") = DevExpress.Data.Filtering.CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("BySupportCM") = DevExpress.Data.Filtering.CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "CustomersMeeting_ListView_Subcontractor" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _User As DevExpress.Persistent.BaseImpl.User = TryCast(SecuritySystem.CurrentUser, DevExpress.Persistent.BaseImpl.User)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(DevExpress.Data.Filtering.CriteriaOperator.Parse("User = ?", _ocur.GetObject(_User)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then

                    Dim _Filter As String = "UserCreated.Oid in("

                    For Each _ContactsIn As Contacts In _Contacts.Customers.Contacts
                        _Filter += "'" + _ContactsIn.User.Oid.ToString + "',"
                    Next
                    _Filter = _Filter.Remove(_Filter.Length - 1, 1)
                    _Filter += ")"

                    TryCast(View, ListView).CollectionSource.Criteria("BySupportCM") = DevExpress.Data.Filtering.CriteriaOperator.Parse(_Filter)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("BySupportCM") = DevExpress.Data.Filtering.CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("BySupportCM") = DevExpress.Data.Filtering.CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "CustomersMeeting_DetailView_Subcontractor" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "CustomersMeeting_DetailView" Then
            Dim _CustomersMeeting As CustomersMeeting = TryCast(View.SelectedObjects(0), CustomersMeeting)
            If _CustomersMeeting IsNot Nothing Then
                If Not TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.IsNewObject(_CustomersMeeting) Then
                    If _CustomersMeeting.IsEditable Then
                        Dim _NeedCommit As Boolean = False
                        If _CustomersMeeting.ObjectCreated.Year < GetSQLTime(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session).Year Then
                            _CustomersMeeting.IsEditable = False
                            _NeedCommit = True
                        ElseIf _CustomersMeeting.ObjectCreated.Month < GetSQLTime(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session).Month Then
                            _CustomersMeeting.IsEditable = False
                            _NeedCommit = True
                        ElseIf _CustomersMeeting.ObjectCreated.Day < GetSQLTime(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session).Day Then
                            _CustomersMeeting.IsEditable = False
                            _NeedCommit = True
                        End If
                        If _NeedCommit Then TryCast(View.ObjectSpace, Xpo.XPObjectSpace).CommitChanges()
                    End If
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "CustomersMeeting_CustomersMeetingDocuments_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "CustomersMeeting_ListView_Support" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "CustomersMeeting_ListView_Subcontractor" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "CustomersMeeting_DetailView_Subcontractor" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)

        End If
    End Sub
    Private Sub aCustomersMeeting_Print_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aCustomersMeeting_Print.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CustomersMeeting As CustomersMeeting = TryCast(View.SelectedObjects(0), CustomersMeeting)
        If _CustomersMeeting IsNot Nothing Then
            If _CustomersMeeting.CustomersMeetingState IsNot Nothing Then
                Dim _ReportData As ReportData = _CustomersMeeting.CustomersMeetingState.ReportData

                If _ReportData IsNot Nothing Then
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("Oid=?", _CustomersMeeting.Oid), True)
                Else
                    MsgBox("Nincs beállítva nyomtatvány!")
                End If
            Else
                MsgBox("Nincs beállítva az egyeztetés típusa, így nem lehet nyomtatni!")
            End If
        End If
    End Sub
End Class
