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("CRM")> _
<DefaultProperty("ShortName")> _
Public Class CRM_Opportunities
    Inherits BaseObject

    Private _CRM_Opportunities_Parameters As CRM_Opportunities_Parameters
    Private _CustomersFrom As CustomersFrom
    Private _CloseDate As Date

    '--General
    Private _ShortName As String
    Private _DocumentNumber As String
    Private _Customers As Customers
    Private _Contacts As Contacts
    Private _EstimatedRevenue As Double
    Private _EstimatedCurrencyName As CurrencyName
    Private _EstimatedRevenueHUF As Double
    Private _EstimatedCloseDate As Date
    Private _Probability As Long
    Private _OpportunityRating As eOpportunityRating
    Private _CRM_Opportunities_Probability As CRM_Opportunities_Probability
    Private _JobNumberObjects As JobNumberObjects
    Private _WorkPlace As WorkPlace
    Private _MasterBusinessDirectory As BusinessDirectory

    '-- Administration
    Private _CRM_Leads As CRM_Leads
    Private _OpportunityStatus As eOpportunityStatus
    Private _HRPerson As HRPerson
    Private _WorkflowSystemSteps As WorkflowSystemSteps

    Private _UserCreated As User 'Létrehozó
    Private _ObjectCreated As Date 'Létrehozva

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()

    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 IsDeleted = False Then
            ' CRM ügyhöz dokumentum tárhely beállítása
            If MasterBusinessDirectory Is Nothing Then
                Dim _CRMBusinessDirectory As BusinessDirectory = Session.FindObject(Of BusinessDirectory)(CriteriaOperator.Parse(""))
            End If
            If Me.CRM_Opportunities_Parameters Is Nothing Then
                If Me.CustomersFrom IsNot Nothing Then
                    Dim _CRM_Opportunities_Parameters As CRM_Opportunities_Parameters = Session.FindObject(Of CRM_Opportunities_Parameters)(CriteriaOperator.Parse("IsDefault=True and CustomersFrom=?", Me.CustomersFrom))
                    If _CRM_Opportunities_Parameters IsNot Nothing Then
                        Me.CRM_Opportunities_Parameters = _CRM_Opportunities_Parameters
                    End If
                End If
            End If
            If Me.CRM_Opportunities_Parameters IsNot Nothing Then
                If Me.CRM_Opportunities_Parameters.WorkflowSystem IsNot Nothing Then
                    If Me.WorkflowSystemSteps Is Nothing Then
                        Dim _WorkflowSystemSteps As WorkflowSystemSteps = Session.FindObject(Of WorkflowSystemSteps)(CriteriaOperator.Parse("WorkflowSystem=? and StepIndex=1", Me.CRM_Opportunities_Parameters.WorkflowSystem))
                        If _WorkflowSystemSteps IsNot Nothing Then
                            Me.WorkflowSystemSteps = _WorkflowSystemSteps
                        End If
                    End If
                End If
                If Me.DocumentNumber = "" Then Me.DocumentNumber = Me.CRM_Opportunities_Parameters.NumberGenerator.GetNewNumber(Me.CRM_Opportunities_Parameters.NumberGenerator)
            End If
        End If
    End Sub

    Property CRM_Opportunities_Parameters As CRM_Opportunities_Parameters
        Get
            Return _CRM_Opportunities_Parameters
        End Get
        Set(value As CRM_Opportunities_Parameters)
            SetPropertyValue("CRM_Opportunities_Parameters", _CRM_Opportunities_Parameters, value)
        End Set
    End Property
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(ByVal value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    Property CloseDate As Date
        Get
            Return _CloseDate
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("CloseDate", _CloseDate, value)
        End Set
    End Property

    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, 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
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    <DataSourceCriteria("'@This.Customers'=Customers")> _
    Property Contacts As Contacts
        Get
            Return _Contacts
        End Get
        Set(value As Contacts)
            SetPropertyValue("Contacts", _Contacts, value)
        End Set
    End Property
    Property EstimatedRevenue As Double
        Get
            Return _EstimatedRevenue
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("EstimatedRevenue", _EstimatedRevenue, value)
        End Set
    End Property
    Property EstimatedCurrencyName As CurrencyName
        Get
            Return _EstimatedCurrencyName
        End Get
        Set(value As CurrencyName)
            SetPropertyValue("EstimatedCurrencyName", _EstimatedCurrencyName, value)
        End Set
    End Property
    Property EstimatedRevenueHUF As Double
        Get
            Return _EstimatedRevenueHUF
        End Get
        Set(value As Double)
            SetPropertyValue("EstimatedRevenueHUF", _EstimatedRevenueHUF, value)
        End Set
    End Property
    Property EstimatedCloseDate As Date
        Get
            Return _EstimatedCloseDate
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("EstimatedCloseDate", _EstimatedCloseDate, value)
        End Set
    End Property
    <RuleRange("CRM_Opportunities-Probability", DefaultContexts.Save, 1, 100)> _
    Property Probability As Long
        Get
            Return _Probability
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("Probability", _Probability, value)
        End Set
    End Property
    <RuleRequiredField("CRM_Opportunities-CRM_Opportunities_Probability", DefaultContexts.Save)> _
    Property CRM_Opportunities_Probability As CRM_Opportunities_Probability
        Get
            Return _CRM_Opportunities_Probability
        End Get
        Set(value As CRM_Opportunities_Probability)
            SetPropertyValue("CRM_Opportunities_Probability", _CRM_Opportunities_Probability, value)
            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False Then
                Probability = value.Probability
            End If
        End Set
    End Property
    Property OpportunityRating As eOpportunityRating
        Get
            Return _OpportunityRating
        End Get
        Set(ByVal value As eOpportunityRating)
            SetPropertyValue("OpportunityRating", _OpportunityRating, value)
        End Set
    End Property
    Property JobNumberObjects As JobNumberObjects
        Get
            Return _JobNumberObjects
        End Get
        Set(value As JobNumberObjects)
            SetPropertyValue("JobNumberObjects", _JobNumberObjects, value)
        End Set
    End Property
    <DataSourceCriteria("CustomersFrom='@This.CustomersFrom'")> _
    Property WorkPlace As WorkPlace
        Get
            Return _WorkPlace
        End Get
        Set(value As WorkPlace)
            SetPropertyValue("WorkPlace", _WorkPlace, value)
        End Set
    End Property
    Property MasterBusinessDirectory As BusinessDirectory
        Get
            Return _MasterBusinessDirectory
        End Get
        Set(value As BusinessDirectory)
            SetPropertyValue("MasterBusinessDirectory", _MasterBusinessDirectory, value)
        End Set
    End Property

    Property CRM_Leads As CRM_Leads
        Get
            Return _CRM_Leads
        End Get
        Set(ByVal value As CRM_Leads)
            SetPropertyValue("CRM_Leads", _CRM_Leads, value)
        End Set
    End Property
    Property OpportunityStatus As eOpportunityStatus
        Get
            Return _OpportunityStatus
        End Get
        Set(ByVal value As eOpportunityStatus)
            SetPropertyValue("OpportunityStatus", _OpportunityStatus, value)
        End Set
    End Property
    Property HRPerson As HRPerson
        Get
            Return _HRPerson
        End Get
        Set(ByVal value As HRPerson)
            SetPropertyValue("HRPerson", _HRPerson, value)
        End Set
    End Property
    <DataSourceCriteria("WorkflowSystem='@This.CRM_Opportunities_Parameters.WorkflowSystem'")> _
    Property WorkflowSystemSteps As WorkflowSystemSteps
        Get
            Return _WorkflowSystemSteps
        End Get
        Set(value As WorkflowSystemSteps)
            SetPropertyValue("WorkflowSystemSteps", _WorkflowSystemSteps, value)
        End Set
    End Property

    <NonCloneable> _
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    <NonCloneable> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property

    '-----------ReadOnly------------
    <VisibleInListView(False)> _
    <Association("OfferOutHeader-CRM_Opportunities", GetType(OfferOutHeader))> _
    ReadOnly Property OfferOutHeader As XPCollection(Of OfferOutHeader)
        Get
            Return GetCollection(Of OfferOutHeader)("OfferOutHeader")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("OfferInHeader-CRM_Opportunities", GetType(OfferInHeader))> _
        ReadOnly Property OfferInHeader As XPCollection(Of OfferInHeader)
        Get
            Return GetCollection(Of OfferInHeader)("OfferInHeader")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("CRM_Opportunities-OrderInHeader", GetType(OrderInHeader))> _
    ReadOnly Property OrderInHeader As XPCollection(Of OrderInHeader)
        Get
            Return GetCollection(Of OrderInHeader)("OrderInHeader")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("CRM_Opportunities-Contract", GetType(Contracts))> _
    ReadOnly Property Contracts As XPCollection(Of Contracts)
        Get
            Return GetCollection(Of Contracts)("Contracts")
        End Get
    End Property
    <VisibleInListView(False)> _
        ReadOnly Property DeliveryNoteOutHeader As XPCollection(Of DeliveryNoteOutHeader)
        Get
            Return New XPCollection(Of DeliveryNoteOutHeader)(Session, CriteriaOperator.Parse("Customers=? and CustomersFrom=?", Me.Customers, Me.CustomersFrom))
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property InvoiceHeader As XPCollection(Of InvoiceHeader)
        Get
            Return New XPCollection(Of InvoiceHeader)(Session, CriteriaOperator.Parse("Customers=? and CustomersFrom=?", Me.Customers, Me.CustomersFrom))
        End Get
    End Property 'Számlák
    <VisibleInListView(False)> _
    ReadOnly Property PCIDetail As XPCollection(Of PCIDetail)
        Get
            Return New XPCollection(Of PCIDetail)(Session, CriteriaOperator.Parse("PartnerType=0 and Customers=? and CustomersFrom=?", Me.Customers, Me.CustomersFrom))
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property CRM_Leads_Activity As XPCollection(Of CRM_Leads_Activity)
        Get
            Return New XPCollection(Of CRM_Leads_Activity)(Session, CriteriaOperator.Parse("CRM_Leads=?", Me.CRM_Leads))
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("CRM_Opportunities-CRM_Opportunities_Contacts_Roles", GetType(CRM_Opportunities_Contacts_Roles))> _
    ReadOnly Property CRM_Opportunities_Contacts_Roles As XPCollection(Of CRM_Opportunities_Contacts_Roles)
        Get
            Return GetCollection(Of CRM_Opportunities_Contacts_Roles)("CRM_Opportunities_Contacts_Roles")
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property CRM_Leads_Activity_Task As XPCollection(Of CRM_Leads_Activity_Task)
        Get
            Return New XPCollection(Of CRM_Leads_Activity_Task)(Session, CriteriaOperator.Parse("CRM_Leads_Activity.CRM_Leads=? or CRM_Opportunities=?", Me.CRM_Leads, Me))
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property RegistryTask As XPCollection(Of RegistryTasks)
        Get
            If Me.CustomersFrom IsNot Nothing Then
                Return New XPCollection(Of RegistryTasks)(Session, _
                    CriteriaOperator.Parse("RegistryHeader.CustomersFrom=?  and Status<>4 and RegistryHeader.RegistryType.RegistryMainType=5", Me.CustomersFrom))
            Else
                Return Nothing
            End If
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property WorkSheet_Header As XPCollection(Of WorkSheet_Header)
        Get
            Return New XPCollection(Of WorkSheet_Header)(Session, CriteriaOperator.Parse("Customers=? and CustomersFrom=?", Me.Customers, Me.CustomersFrom))
        End Get
    End Property
    <Association("CRM_Opportunities-CRM_Opportunities_Products", GetType(CRM_Opportunities_Products)), VisibleInListView(False), VisibleInLookupListView(False)>
    ReadOnly Property CRM_Opportunities_Products As XPCollection(Of CRM_Opportunities_Products)
        Get
            Return GetCollection(Of CRM_Opportunities_Products)("CRM_Opportunities_Products")
        End Get
    End Property
    <Association("CRM_Opportunities-CRM_Opportunities_Services", GetType(CRM_Opportunities_Services)), VisibleInListView(False), VisibleInLookupListView(False)>
    ReadOnly Property CRM_Opportunities_Services As XPCollection(Of CRM_Opportunities_Services)
        Get
            Return GetCollection(Of CRM_Opportunities_Services)("CRM_Opportunities_Services")
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property CRM_Opportunities_BusinessDirectory As XPCollection(Of BusinessDirectory)
        Get
            Dim _BusinessDirectory_Collection As New XPCollection(Of BusinessDirectory)(Session, CriteriaOperator.Parse("1=2"))
            If MasterBusinessDirectory IsNot Nothing Then
                If MasterBusinessDirectory.Children.Count > 0 Then
                    _BusinessDirectory_Collection.Add(MasterBusinessDirectory)
                    RecursiveBusinessDirectory(_BusinessDirectory_Collection, MasterBusinessDirectory.NestedBusinessDirectory)
                Else
                    _BusinessDirectory_Collection.Add(MasterBusinessDirectory)
                End If
            End If
            Return _BusinessDirectory_Collection
        End Get
    End Property
    <Browsable(False)> _
    ReadOnly Property MasterBusinessDirectoryCollection As XPCollection(Of BusinessDirectory)
        Get
            Dim _BusinessDirectoryReturn_Collection As XPCollection(Of BusinessDirectory) = Nothing
            Dim _BusinessDirectory_Collection As New XPCollection(Of BusinessDirectory)(Session)
            If _BusinessDirectory_Collection.Count > 0 Then
                Dim _Filter As String = "Oid in("

                For Each _BusinessDirectory As BusinessDirectory In _BusinessDirectory_Collection
                    If _BusinessDirectory.IsFolder Then _Filter += "'" + _BusinessDirectory.Oid.ToString + "',"
                Next
                _Filter = _Filter.Remove(_Filter.Length - 1, 1)
                _Filter += ")"
                If _Filter.Length > 8 Then
                    _BusinessDirectoryReturn_Collection = New XPCollection(Of BusinessDirectory)(Session, CriteriaOperator.Parse(_Filter))
                    _BusinessDirectory_Collection.Filter = CriteriaOperator.Parse("IsFolder=True")
                Else
                    _Filter = "1=2"
                End If
            End If

            Return _BusinessDirectoryReturn_Collection
        End Get
    End Property
    <VisibleInListView(False)> _
    <VisibleInLookupListView(False)> _
    ReadOnly Property CRM_Opportunities_Customers As XPCollection(Of CRM_Opportunities_Customers)
        Get
            Return New XPCollection(Of CRM_Opportunities_Customers)(Session, CriteriaOperator.Parse("CRM_Opportunities=?", Me))
        End Get
    End Property
    <VisibleInListView(False)> _
   <VisibleInLookupListView(False)> _
    ReadOnly Property CRM_Opportunities_CustomersContacts As XPCollection(Of CRM_Opportunities_CustomersContacts)
        Get
            Return New XPCollection(Of CRM_Opportunities_CustomersContacts)(Session, CriteriaOperator.Parse("CRM_Opportunities=?", Me))
        End Get
    End Property

    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property OrderInRowsOther As XPCollection(Of OrderInRowsOther)
        Get
            Return New XPCollection(Of OrderInRowsOther)(Session, CriteriaOperator.Parse("OrderInHeader.CRM_Opportunities=?", Me))
        End Get
    End Property

    Private Sub RecursiveBusinessDirectory(_BusinessDirectoryList As XPCollection(Of BusinessDirectory), _NestedBusinessDirectory As XPCollection(Of BusinessDirectory))
        For Each _BusinessDirectory As BusinessDirectory In _NestedBusinessDirectory
            If _BusinessDirectory.Children.Count > 0 Then
                _BusinessDirectoryList.Add(_BusinessDirectory)
                RecursiveBusinessDirectory(_BusinessDirectoryList, _BusinessDirectory.NestedBusinessDirectory)
            Else
                _BusinessDirectoryList.Add(_BusinessDirectory)
            End If
        Next
    End Sub


End Class