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()> _
<NonPersistent()> _
<CreatableItem(False)> _
<NavigationItem(False)> _
<Appearance("Disable CustomersFrom in Popup", "ViewItem", targetitems:="CustomersFrom", criteria:="isnull(CustomersFrom)=False", enabled:=False)> _
<Appearance("Disable WorkSheet_Header_Collection in Popup, when WorkSheet_Header_Collection.Count=0", "ViewItem", targetitems:="WorkSheet_Header_Collection", criteria:="WorkSheet_Header_Collection.Count=0", visibility:=Editors.ViewItemVisibility.Hide)> _
Public Class OpenWorkSheet_SubcontractorPopUp
    Inherits BaseObject
    Private _CustomersFrom As CustomersFrom
    Private _WorkSheetType As WorkSheetType
    Private _DateCreated As Date
    Private _Subcontractor As Subcontractor
    Private _InvoiceState As eInvoiceState 'Számlázási állapot
    Private _WorkState As WorkState 'Munkafolyamat
    Private _Question As Question
    Private _Description As String
    Private _WorkPlace As WorkPlace
    Private _WorkHour As Double
    Private _WorkDescription As String
    Private _WorkUsedMaterialDescription As String
    Private _WorkDescriptionOther As String
    Private _WorkTravelKm As Long

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Dim _User As User = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = Session.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Subcontractor = Session.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _Contacts.Customers))
    End Sub
    <ImmediatePostData()> _
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Me.WorkPlace = Nothing
            End If
        End Set
    End Property
    <DataSourceProperty("WorkSheetType_Collection")> _
    Property WorkSheetType As WorkSheetType
        Get
            Return _WorkSheetType
        End Get
        Set(value As WorkSheetType)
            SetPropertyValue("WorkSheetType", _WorkSheetType, value)
        End Set
    End Property
    Property DateCreated As Date
        Get
            Return _DateCreated
        End Get
        Set(value As Date)
            SetPropertyValue("DateCreated", _DateCreated, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    Property Subcontractor As Subcontractor
        Get
            Return _Subcontractor
        End Get
        Set(value As Subcontractor)
            SetPropertyValue("Subcontractor", _Subcontractor, value)
        End Set
    End Property
    Property InvoiceState As eInvoiceState
        Get
            Return _InvoiceState
        End Get
        Set(value As eInvoiceState)
            SetPropertyValue("InvoiceState", _InvoiceState, value)
        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
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    Property Question As Question
        Get
            Return _Question
        End Get
        Set(value As Question)
            SetPropertyValue("Question", _Question, value)
        End Set
    End Property
    <Size(-1)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <DataSourceProperty("WorkPlace_Collection")> _
    Property WorkPlace As WorkPlace
        Get
            Return _WorkPlace
        End Get
        Set(value As WorkPlace)
            SetPropertyValue("WorkPlace", _WorkPlace, value)
        End Set
    End Property
    Property WorkHour As Double
        Get
            Return _WorkHour
        End Get
        Set(value As Double)
            SetPropertyValue("WorkHour", _WorkHour, 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 WorkTravelKm As Long
        Get
            Return _WorkTravelKm
        End Get
        Set(value As Long)
            SetPropertyValue("WorkTravelKm", _WorkTravelKm, value)
        End Set
    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=?", Me.Subcontractor.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 WorkSheetType_Collection As XPCollection(Of WorkSheetType)
        Get
            Dim _WorkSheetType_Collection_Back As New XPCollection(Of WorkSheetType)(Session, CriteriaOperator.Parse("Oid<>Oid"))
            For Each _ActWorkSheetType As SubcontractorWorksheetType In Me.Subcontractor.SubcontractorWorksheetType
                If Me.CustomersFrom IsNot Nothing Then
                    If _ActWorkSheetType.WorkSheetType.CustomersFrom.Oid = Me.CustomersFrom.Oid Then
                        _WorkSheetType_Collection_Back.Add(_ActWorkSheetType.WorkSheetType)
                    End If
                End If
            Next
            Return _WorkSheetType_Collection_Back
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property WorkSheet_Header_Collection As XPCollection(Of WorkSheet_Header)
        Get
            If Question IsNot Nothing AndAlso Subcontractor IsNot Nothing Then
                Return New XPCollection(Of WorkSheet_Header)(Session, CriteriaOperator.Parse("Question.Oid=? AND SubcontractorCustomers.Oid=?", Question.Oid, Subcontractor.Customers.Oid))
            Else
                Return Nothing
            End If
        End Get
    End Property
End Class
