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

'<EditorStateRuleAttribute("Disable LastName", "LastName", ViewType.DetailView, "IsNull(Contacts) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable FirstName", "FirstName", ViewType.DetailView, "IsNull(Contacts) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable Topic", "Topic", ViewType.DetailView, "IsNull(Contacts) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable Customers when is not empty", "Customers", ViewType.DetailView, "IsNull(Customers) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable Contacts when is not empty_1", "Contacts", ViewType.DetailView, "IsNull(Contacts) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable LeadStatus when is  Qualified = 5", "LeadStatus", ViewType.DetailView, "LeadStatus='Qualified'", EditorState.Disabled)> _

<DefaultClassOptions()> _
<NavigationItem("CRM")> _
<DefaultProperty("Topic")> _
<Appearance("Hide CRM_Leads_CRM_Opportunity_ListView Tab when LeadStatus<>eLeadStatus.Qualified", AppearanceItemType.ViewItem, "LeadStatus<>5", _
    TargetItems:="CRM_Opportunity", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable LastName", TargetItems:="LastName", Criteria:="IsNull(Contacts) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable FirstName", TargetItems:="FirstName", Criteria:="IsNull(Contacts) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable Topic", TargetItems:="Topic", Criteria:="IsNull(Contacts) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable Customers when is not empty", TargetItems:="Customers", Criteria:="IsNull(Customers) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable Contacts when is not empty_1", TargetItems:="Contacts", Criteria:="IsNull(Contacts) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable LeadStatus when is  Qualified = 5", TargetItems:="LeadStatus", Criteria:="LeadStatus='Qualified'", Enabled:=False, Context:="DetailView")> _
Public Class CRM_Leads
    Inherits BaseObject
    '-- Személyes adatok
    Private _Topic As String ' Megjelenési név
    Private _FirstName As String ' Keresztnév
    Private _LastName As String ' Vezetéknév
    Private _CRM_FirstMeet As CRM_FirstMeet ' Hol találkozott először
    Private _CompanyName As String ' Cég neve
    Private _JobTitle As String ' Beosztás
    Private _OfficePhone As String ' Munkahelyi telefon
    Private _Fax As String ' Fax
    Private _MobilePhone As String ' Mobil szám
    Private _HomePhone As String ' Otthoni szám
    Private _OtherPhone As String ' Egyéb telefonszám
    Private _WEB As String 'WEB szájt
    Private _EMail As String ' Emil
    '-- Részletes adatok
    Private _AnnualRevenue As Double 'Forgalom
    Private _NumberOfEmployees As Long 'Dolgozók száma
    Private _LeadSource As eLeadSource ' Lead forrás
    '-- Elsődleges cím adatok
    Private _Street1 As String 'Utca, házszám
    Private _Street2 As String 'Utca házszám
    Private _City As String 'Város
    Private _Country As Country 'Ország
    Private _State As String 'Megye, állam stb.
    Private _ZIP As String 'Postai irányítószám
    '-- Adminisztrációs adatok
    Private _PreferredContactMethod As ePreferredContactMethod ' Kapcsolattartás módja
    Private _LeadStatus As eLeadStatus ' Lead státusza
    Private _HRPerson As HRPerson 'A felelős személy, aki ezzel a LEAD-dal foglalkozik

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

    '-- Egyéb belső változók
    Private _InProcess As Boolean = False
    Private _Customers As Customers
    Private _Contacts As Contacts

    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
    End Sub
    Sub CreateTopic()
        If _InProcess Then
        Else
            _InProcess = True
            If CompanyName <> "" Then
                Topic = CompanyName
            End If
            If FirstName <> "" And LastName <> "" Then
                Topic = LastName & " " & FirstName
            End If
            _InProcess = False
        End If
    End Sub
    Property Topic As String
        Get
            Return _Topic
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Topic", _Topic, value)
        End Set
    End Property
    <RuleRequiredField("CRM_Leads-FirstName", DefaultContexts.Save)> _
    Property FirstName As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("FirstName", _FirstName, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                CreateTopic()
            End If
        End Set
    End Property
    <RuleRequiredField("CRM_Leads-LastName", DefaultContexts.Save)> _
    Property LastName As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("LastName", _LastName, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                CreateTopic()
            End If
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)> _
    <DataSourceCriteria("Active=True")> _
    Property CRM_FirstMeet As CRM_FirstMeet
        Get
            Return _CRM_FirstMeet
        End Get
        Set(ByVal value As CRM_FirstMeet)
            SetPropertyValue("CRM_FirstMeet", _CRM_FirstMeet, value)
        End Set
    End Property
    <RuleRequiredField("", "aQualifiedLeads_Contexts")> _
    Property CompanyName As String
        Get
            Return _CompanyName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("CompanyName", _CompanyName, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                CreateTopic()
            End If
        End Set
    End Property
    Property JobTitle As String
        Get
            Return _JobTitle
        End Get
        Set(ByVal value As String)
            SetPropertyValue("JobTitle", _JobTitle, value)
        End Set
    End Property
    Property OfficePhone As String
        Get
            Return _OfficePhone
        End Get
        Set(ByVal value As String)
            SetPropertyValue("OfficePhone", _OfficePhone, value)
        End Set
    End Property
    Property Fax As String
        Get
            Return _Fax
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Fax", _Fax, value)
        End Set
    End Property
    <RuleRequiredField("CRM_Leads-MobilePhone", "aQualifiedLeads_Contexts", TargetCriteria:="isnull(EMail)=True")> _
    Property MobilePhone As String
        Get
            Return _MobilePhone
        End Get
        Set(ByVal value As String)
            SetPropertyValue("MobilePhone", _MobilePhone, value)
        End Set
    End Property
    Property HomePhone As String
        Get
            Return _HomePhone
        End Get
        Set(ByVal value As String)
            SetPropertyValue("HomePhone", _HomePhone, value)
        End Set
    End Property
    Property OtherPhone As String
        Get
            Return _OtherPhone
        End Get
        Set(ByVal value As String)
            SetPropertyValue("OtherPhone", _OtherPhone, value)
        End Set
    End Property
    Property WEB As String
        Get
            Return _WEB
        End Get
        Set(ByVal value As String)
            SetPropertyValue("WEB", _WEB, value)
        End Set
    End Property
    <RuleRequiredField("CRM_Leads-EMail", "aQualifiedLeads_Contexts", TargetCriteria:="isnull(MobilePhone)=True")> _
    Property EMail As String
        Get
            Return _EMail
        End Get
        Set(ByVal value As String)
            SetPropertyValue("EMail", _EMail, value)
        End Set
    End Property
    Property AnnualRevenue As Double
        Get
            Return _AnnualRevenue
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AnnualRevenue", _AnnualRevenue, value)
        End Set
    End Property
    Property NumberOfEmployees As Long
        Get
            Return _NumberOfEmployees
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("NumberOfEmployees", _NumberOfEmployees, value)
        End Set
    End Property
    Property LeadSource As eLeadSource
        Get
            Return _LeadSource
        End Get
        Set(ByVal value As eLeadSource)
            SetPropertyValue("LeadSource", _LeadSource, value)
        End Set
    End Property
    <RuleRequiredField("", "aQualifiedLeads_Contexts")> _
    Property Street1 As String
        Get
            Return _Street1
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Street1", _Street1, value)
        End Set
    End Property
    Property Street2 As String
        Get
            Return _Street2
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Street2", _Street2, value)
        End Set
    End Property
    <RuleRequiredField("", "aQualifiedLeads_Contexts")> _
    Property City As String
        Get
            Return _City
        End Get
        Set(ByVal value As String)
            SetPropertyValue("City", _City, value)
        End Set
    End Property
    <RuleRequiredField("", "aQualifiedLeads_Contexts")> _
    Property Country As Country
        Get
            Return _Country
        End Get
        Set(ByVal value As Country)
            SetPropertyValue("Country", _Country, value)
        End Set
    End Property
    Property State As String
        Get
            Return _State
        End Get
        Set(ByVal value As String)
            SetPropertyValue("State", _State, value)
        End Set
    End Property
    <RuleRequiredField("", "aQualifiedLeads_Contexts")> _
    Property ZIP As String
        Get
            Return _ZIP
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ZIP", _ZIP, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Dim _ZIPHU As ZIPHU = Session.FindObject(Of ZIPHU)(CriteriaOperator.Parse("Zip=?", Me.ZIP))
                If _ZIPHU IsNot Nothing Then
                    Me.Country = TryCast(Session.FindObject(Of CountryExtend)(CriteriaOperator.Parse("ShortName=?", "Magyarország")), Country)
                    Me.City = _ZIPHU.City
                    Me.State = _ZIPHU.County
                Else
                    Me.Country = Nothing
                    Me.City = "Nem magyarországi irányítószám"
                    Me.State = "Nem magyarországi irányítószám"
                End If
            End If
        End Set
    End Property

    Property PreferredContactMethod As ePreferredContactMethod
        Get
            Return _PreferredContactMethod
        End Get
        Set(ByVal value As ePreferredContactMethod)
            SetPropertyValue("PreferredContactMethod", _PreferredContactMethod, value)
        End Set
    End Property
    Property LeadStatus As eLeadStatus
        Get
            Return _LeadStatus
        End Get
        Set(ByVal value As eLeadStatus)
            SetPropertyValue("LeadStatus", _LeadStatus, 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
    <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
    <ImmediatePostData()> _
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("Customers", _Customers, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False And Customers IsNot Nothing Then
                CompanyName = Customers.FullName
                WEB = Customers.WebSite
                If Customers.Address1 IsNot Nothing Then
                    Street1 = Customers.Address1.Street
                    ' Street2 = Customers.Address2.Street
                    City = Customers.Address1.City
                    Country = Customers.Address1.Country
                    State = Customers.Address1.StateProvince
                    ZIP = Customers.Address1.ZipPostal
                    'Contacts = Nothing
                End If
            End If
        End Set
    End Property
    <ImmediatePostData()> _
     <DataSourceCriteria("Customers='@This.Customers'")> _
    Property Contacts As Contacts
        Get
            Return _Contacts
        End Get
        Set(value As Contacts)
            SetPropertyValue("Contacts", _Contacts, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False And Contacts IsNot Nothing Then
                LastName = Contacts.LastName
                FirstName = Contacts.FirstName
                Dim _PhoneNumber As PhoneNumber
                For Each _PhoneNumber In Contacts.PhoneNumbers
                    If _PhoneNumber.PhoneType = "OfficePhone" Then
                        OfficePhone = _PhoneNumber.Number
                    End If
                    If _PhoneNumber.PhoneType = "Fax" Then
                        Fax = _PhoneNumber.Number
                    End If
                    If _PhoneNumber.PhoneType = "MobilePhone" Then
                        MobilePhone = _PhoneNumber.Number
                    End If
                    If _PhoneNumber.PhoneType = "HomePhone" Then
                        HomePhone = _PhoneNumber.Number
                    End If
                    If _PhoneNumber.PhoneType = "OtherPhone" Then
                        OtherPhone = _PhoneNumber.Number
                    End If
                Next
                EMail = Contacts.Email
                If Contacts.Customers IsNot Nothing And Customers Is Nothing Then
                    Customers = Contacts.Customers
                End If
            End If
        End Set
    End Property
    '-- Activities
    <VisibleInListView(False)> _
    <Association("CRM_Leads-CRM_Leads_Activity", GetType(CRM_Leads_Activity))> _
    ReadOnly Property CRM_Leads_Activity As XPCollection(Of CRM_Leads_Activity)
        Get
            Return GetCollection(Of CRM_Leads_Activity)("CRM_Leads_Activity")
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property CRM_Opportunity As XPCollection(Of CRM_Opportunities)
        Get
            Return New XPCollection(Of CRM_Opportunities)(Session, CriteriaOperator.Parse("CRM_Leads.Oid=?", Me.Oid))
        End Get
    End Property
    '-- Megjegyzések
    <VisibleInListView(False)> _
    <Association("CRM_Leads-CRMNote", GetType(CRMNote))> _
    ReadOnly Property CRMNote As XPCollection(Of CRMNote)
        Get
            Return GetCollection(Of CRMNote)("CRMNote")
        End Get
    End Property
    '------------ReadOnly-----------------
    <Association("CRM_Leads-CRM_LeadsDocumentation", GetType(CRM_LeadsDocumentation))> _
    ReadOnly Property CRM_LeadsDocumentation As XPCollection(Of CRM_LeadsDocumentation)
        Get
            Return GetCollection(Of CRM_LeadsDocumentation)("CRM_LeadsDocumentation")
        End Get
    End Property
End Class
