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

<DefaultClassOptions()> _
<NavigationItem(False)> _
<CreatableItem(False)> _
<NonPersistent()> _
Public Class OperationLog_ViewLogs_PopUp
    Inherits BaseObject

    Private _WorkPlace As WorkPlace
    Private _FromDate As Date
    Private _FromDateHour As Long
    Private _FromDateMin As Long
    Private _ToDate As Date
    Private _ToDateHour As Long
    Private _ToDateMin As Long
    Private _CollectionSource As CollectionSource
    Private _CustomersFrom As CustomersFrom

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        ToDate = GetSQLTime(Session).Date
        ToDateHour = GetSQLTime(Session).Hour
        ToDateMin = GetSQLTime(Session).Minute
        FromDate = ToDate.AddDays(-7)
        FromDateHour = ToDateHour
        FromDateMin = ToDateMin
    End Sub

    <ImmediatePostData(True), 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 FromDate As Date
        Get
            Return _FromDate
        End Get
        Set(value As Date)
            SetPropertyValue("FromDate", _FromDate, value)
        End Set
    End Property
    <RuleRange("OperationLog_ViewLogs_PopUp - FromDateHour", DefaultContexts.Save, 0, 23)> _
    Property FromDateHour As Long
        Get
            Return _FromDateHour
        End Get
        Set(value As Long)
            SetPropertyValue("FromDateHour", _FromDateHour, value)
        End Set
    End Property
    <RuleRange("OperationLog_ViewLogs_PopUp - FromDateMin", DefaultContexts.Save, 0, 59)> _
    Property FromDateMin As Long
        Get
            Return _FromDateMin
        End Get
        Set(value As Long)
            SetPropertyValue("FromDateMin", _FromDateMin, value)
        End Set
    End Property
    Property ToDate As Date
        Get
            Return _ToDate
        End Get
        Set(value As Date)
            SetPropertyValue("ToDate", _ToDate, value)
        End Set
    End Property
    <RuleRange("OperationLog_ViewLogs_PopUp - ToDateHour", DefaultContexts.Save, 0, 23)> _
    Property ToDateHour As Long
        Get
            Return _ToDateHour
        End Get
        Set(value As Long)
            SetPropertyValue("ToDateHour", _ToDateHour, value)
        End Set
    End Property
    <RuleRange("OperationLog_ViewLogs_PopUp - ToDateMin", DefaultContexts.Save, 0, 59)> _
    Property ToDateMin As Long
        Get
            Return _ToDateMin
        End Get
        Set(value As Long)
            SetPropertyValue("ToDateMin", _ToDateMin, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    Private ReadOnly Property WorkPlace_Collection As XPCollection(Of WorkPlace)
        Get
            Dim _User As User = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
            Dim _Contacts As Contacts = Session.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _Subcontractor As Subcontractor = Session.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _Contacts.Customers))
            Dim _SubcontractorCustomers As SubcontractorCustomers
            Dim _Criteria As String = "1=2"
            If _Subcontractor IsNot Nothing Then
                If _Subcontractor.SubcontractorCustomers.Count > 0 Then
                    For Each _SubcontractorCustomers In _Subcontractor.SubcontractorCustomers
                        If _Criteria = "1=2" Then
                            _Criteria = "Customers.Oid in ('" & _SubcontractorCustomers.Customers.Oid.ToString & "'"
                        Else
                            _Criteria += ",'" & _SubcontractorCustomers.Customers.Oid.ToString & "'"
                        End If
                    Next
                End If
                If _Criteria <> "1=2" Then _Criteria += ")"
            Else
                _Criteria = "Customers.Oid='" + _Contacts.Customers.Oid.ToString + "' AND CustomersFrom.Oid='" + CustomersFrom.Oid.ToString + "'"
            End If

            Dim _WorkPlace_Collection As New XPCollection(Of WorkPlace)(Session, CriteriaOperator.Parse(_Criteria))
            Return _WorkPlace_Collection
        End Get
    End Property
End Class
