Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.ConditionalAppearance

<DefaultClassOptions()> _
<CreatableItem(False)> _
<NavigationItem(False)> _
<Appearance("OperationalLog - Disable UserCreated all time", AppearanceItemType.ViewItem, "1=1", TargetItems:="UserCreated", Enabled:=False)> _
<Appearance("OperationalLog - Disable ObjectCreated all time", AppearanceItemType.ViewItem, "1=1", TargetItems:="ObjectCreated", Enabled:=False)> _
<Appearance("OperationalLog - Disable DocumentNumber all time", AppearanceItemType.ViewItem, "1=1", TargetItems:="DocumentNumber", Enabled:=False)> _
<Appearance("OperationalLog - Disable Save,SaveAndNew", AppearanceItemType.Action, "1=1", TargetItems:="Save;SaveAndNew", visibility:=Editors.ViewItemVisibility.Hide)> _
Public Class OperationalLog
    Inherits BaseObject

    Private _CustomersFrom As CustomersFrom
    Private _Customers As Customers
    Private _DocumentNumber As String 'Bejegyzés sorszáma
    Private _WorkPlace As WorkPlace 'Munkahely
    Private _WorkState As WorkState 'Munkafolyamat
    Private _OperationalLogParameters As OperationalLogParameters
    Private _Description As String 'Esemény leírása
    Private _WorkDescription As String 'Elvégzett munka leírása
    Private _WorkUsedMaterialDescription As String 'Felhasznált anyag leírása (nem kötelező WEB)
    Private _WorkDescriptionOther As String 'Egyéb az alvállalkozó által megadott megjegyzés (nem kötelező WEB)
    Private _ObjectCreated As Date
    Private _UserCreated As User

    Dim _LoggedUser As User
    Dim _LoggedContacts As Contacts
    Dim _LoggedSubcontractor As Subcontractor

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        WorkState = Session.FindObject(Of WorkState)(CriteriaOperator.Parse("IsOperationalLog = True"))

        _LoggedUser = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        _LoggedContacts = Session.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _LoggedUser))
        _LoggedSubcontractor = Session.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _LoggedContacts.Customers))
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        End If
        If String.IsNullOrEmpty(DocumentNumber) Then
            DocumentNumber = OperationalLogParameters.NumberGenerator.GetNewNumber(OperationalLogParameters.NumberGenerator)
        End If
    End Sub

    <RuleRequiredField("OperationalLog - CustomersFrom", DefaultContexts.Save)> _
    <ImmediatePostData()> _
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <RuleRequiredField("OperationalLog - Customers", DefaultContexts.Save)> _
    <DataSourceProperty("Customers_Collection")> _
    <ImmediatePostData()> _
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    Property DocumentNumber As String
        Get
            Return _DocumentNumber
        End Get
        Set(value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    <RuleRequiredField("OperationalLog - WorkPlace", DefaultContexts.Save)> _
    <DataSourceCriteria("CustomersFrom='@This.CustomersFrom' and Customers='@This.Customers'")> _
    <DataSourceProperty("WorkPlace_Collection")> _
    <ImmediatePostData()> _
    Property WorkPlace As WorkPlace
        Get
            Return _WorkPlace
        End Get
        Set(value As WorkPlace)
            SetPropertyValue("WorkPlace", _WorkPlace, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving And CustomersFrom IsNot Nothing Then
                OperationalLogParameters = Session.FindObject(Of OperationalLogParameters)(CriteriaOperator.Parse("WorkPlace=? AND CustomersFrom=?", value, CustomersFrom))
            End If
        End Set
    End Property
    Property WorkState As WorkState
        Get
            Return _WorkState
        End Get
        Set(value As WorkState)
            SetPropertyValue("WorkState", _WorkState, value)
        End Set
    End Property
    <RuleRequiredField("OperationalLog - OperationalLogParameters", DefaultContexts.Save)> _
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    Property OperationalLogParameters As OperationalLogParameters
        Get
            Return _OperationalLogParameters
        End Get
        Set(value As OperationalLogParameters)
            SetPropertyValue("OperationalLogParameters", _OperationalLogParameters, value)
        End Set
    End Property
    <Size(-1)> _
    <RuleRequiredField("OperationalLog - Description", DefaultContexts.Save)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <Size(-1)> _
    Property WorkDescription As String
        Get
            Return _WorkDescription
        End Get
        Set(value As String)
            SetPropertyValue("WorkDescription", _WorkDescription, value)
        End Set
    End Property
    <Size(-1)> _
    Property WorkUsedMaterialDescription As String
        Get
            Return _WorkUsedMaterialDescription
        End Get
        Set(value As String)
            SetPropertyValue("WorkUsedMaterialDescription", _WorkUsedMaterialDescription, value)
        End Set
    End Property
    <Size(-1)> _
    Property WorkDescriptionOther As String
        Get
            Return _WorkDescriptionOther
        End Get
        Set(value As String)
            SetPropertyValue("WorkDescriptionOther", _WorkDescriptionOther, value)
        End Set
    End Property
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property

    <VisibleInListView(False)> _
    <Association("OperationalLog-OperationalLogDocuments", GetType(OperationalLogDocuments))> _
    ReadOnly Property OperationalLogDocuments As XPCollection(Of OperationalLogDocuments)
        Get
            Return GetCollection(Of OperationalLogDocuments)("OperationalLogDocuments")
        End Get
    End Property
    <VisibleInListView(False), VisibleInDetailView(False), VisibleInLookupListView(False)> _
    ReadOnly Property FirstDocument As OperationalLogDocuments
        Get
            Dim _FirstDocument As OperationalLogDocuments = Nothing

            For Each _FirstDocument In Me.OperationalLogDocuments
                Exit For
            Next
            Return _FirstDocument
        End Get
    End Property
    <VisibleInListView(False)> _
    Private ReadOnly Property WorkPlace_Collection As XPCollection(Of WorkPlace)
        Get
            If Me.CustomersFrom Is Nothing Then
                Return Nothing
            End If
            Dim _WorkPlace_Collection_All As New XPCollection(Of WorkPlace)(Session, CriteriaOperator.Parse("CustomersFrom.Oid=?", Me.CustomersFrom.Oid))
            Dim _WorkPlace_Collection As New XPCollection(Of WorkPlace)(Session, CriteriaOperator.Parse("1=2"))
            Dim _WorkPlace As WorkPlace
            Dim _WorkPlace_ArrayList As New ArrayList
            Dim _SubcontractorCustomers_Collection As New XPCollection(Of SubcontractorCustomers)(Session, CriteriaOperator.Parse("Subcontractor.Oid=?", _LoggedSubcontractor.Oid))
            Dim _SubcontractorCustomers As SubcontractorCustomers

            '_SubcontractorCustomers.Customers.Contacts
            _WorkPlace_ArrayList.Clear()
            For Each _WorkPlace In _WorkPlace_Collection_All
                For Each _SubcontractorCustomers In _SubcontractorCustomers_Collection
                    If _SubcontractorCustomers.Workplace IsNot Nothing Then
                        If _WorkPlace.Oid = _SubcontractorCustomers.Workplace.Oid Then
                            If _WorkPlace_ArrayList.Contains(_WorkPlace) Then
                            Else
                                _WorkPlace_ArrayList.Add(_WorkPlace)
                            End If
                        End If
                    End If
                Next
            Next

            For Each _WorkPlace In _WorkPlace_ArrayList
                _WorkPlace_Collection.Add(_WorkPlace)
            Next

            Return _WorkPlace_Collection
        End Get
    End Property
    <VisibleInListView(False)> _
    Private ReadOnly Property Customers_Collection As XPCollection(Of Customers)
        Get
            If _LoggedSubcontractor IsNot Nothing Then
                Dim _Customers_Collection As New XPCollection(Of Customers)(Session, CriteriaOperator.Parse("1=2"))
                Dim _SubcontractorCustomers_Collection As New XPCollection(Of SubcontractorCustomers)(Session, CriteriaOperator.Parse("Subcontractor.Oid=?", _LoggedSubcontractor.Oid))

                For Each _SubcontractorCustomers As SubcontractorCustomers In _SubcontractorCustomers_Collection
                    _Customers_Collection.Add(_SubcontractorCustomers.Customers)
                Next

                Return _Customers_Collection
            Else
                Return Nothing
            End If
        End Get
    End Property
End Class
