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
Imports System.Net.Mail

Public Enum eMeetPlace
    eCustomersOffice = 0
    eOurOffice = 1
    eOtherPlace = 2
End Enum
<DefaultClassOptions()> _
<NavigationItem(False)> _
<CreatableItem(False)> _
<Appearance("Disable CustomersMeeting - ObjectCreated all times", AppearanceItemType.ViewItem, "1=1", TargetItems:="ObjectCreated", Enabled:=False)> _
<Appearance("Disable CustomersMeeting - UserCreated all times", AppearanceItemType.ViewItem, "1=1", TargetItems:="UserCreated", Enabled:=False)> _
<Appearance("Disable CustomersMeeting - Months all times", AppearanceItemType.ViewItem, "1=1", TargetItems:="Months", Enabled:=False)> _
<Appearance("Disable CustomersMeeting - DocumentNumber all times", AppearanceItemType.ViewItem, "1=1", TargetItems:="DocumentNumber", Enabled:=False)> _
<Appearance("Disable CustomersMeeting - all properties, when IsEditable=False", AppearanceItemType.ViewItem, "IsEditable=False", TargetItems:="*", Enabled:=False)> _
Public Class CustomersMeeting
    Inherits BaseObject
    Private _MeetPlace As eMeetPlace
    Private _CustomersMeetingType As CustomersMeetingType
    Private _Customers As Customers
    Private _DocumentNumber As String
    Private _DateExecution As Date
    Private _ShortName As String
    Private _Description As String
    Private _UserCreated As User
    Private _ObjectCreated As Date
    Private _IsEditable As Boolean
    Private _Months As eMonths
    Private _EmailHasBeenSent As Boolean

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        DateExecution = Now
        IsEditable = True
        EmailHasBeenSent = False
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If Session.IsNewObject(Me) Then
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
            ObjectCreated = Now

            If String.IsNullOrEmpty(DocumentNumber) And NumberGenerator IsNot Nothing Then
                DocumentNumber = NumberGenerator.GetNewNumber(NumberGenerator, Year(Now).ToString)
            End If
            Months = ObjectCreated.Month

            If EmailHasBeenSent = False Then
                Try
                    Dim _WebParameters As WebParameters = Session.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
                    If _WebParameters Is Nothing Then Throw New Exception("*Nincsnek e-mail paraméterek beállítva!")
                    If _WebParameters.SMTPHost Is Nothing Then Throw New Exception("*Nincs SMTP server beállítva!")
                    If _WebParameters.SMTPUserName = "" Then Throw New Exception("*Nincs felhasználói név beállítva!")
                    If _WebParameters.SMTPPort = 0 Then Throw New Exception("*Nincs SMTP port beállítva!")


                    Dim _MailMessage As New MailMessage
                    Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                    _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                    If Customers.Contacts.Count > 0 Then
                        For Each _Contacts As Contacts In Customers.Contacts
                            If Not String.IsNullOrEmpty(_Contacts.Email) Then _MailMessage.To.Add(_Contacts.Email)
                        Next
                    ElseIf Not String.IsNullOrEmpty(Customers.Email) Then
                        _MailMessage.To.Add(Customers.Email)
                    Else
                        Throw New Exception("*Nincs email cím beállítva!")
                    End If

                    _MailMessage.Subject = "Új egyeztetés"
                    _MailMessage.IsBodyHtml = True
                    _MailMessage.Body = String.Format("{0} számmal új egyeztetés készült az Ön részére", DocumentNumber)

                    _SmtpClient.Port = _WebParameters.SMTPPort
                    _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                    _SmtpClient.EnableSsl = _WebParameters.EnableSsl

                    _SmtpClient.Send(_MailMessage)

                    EmailHasBeenSent = True
                Catch ex As Exception
                    Dim sf As StackFrame = New StackFrame()
                    Dim _uow As New UnitOfWork(Session.DataLayer)
                    Dim _ErrorLog As New ErrorLog(_uow)
                    _ErrorLog.Description = ex.Message
                    _ErrorLog.SubName = TryCast(sf.GetMethod(), System.Reflection.MethodBase).Name
                    _uow.CommitTransaction()
                End Try
            End If
        End If
    End Sub

    Property MeetPlace As eMeetPlace
        Get
            Return _MeetPlace
        End Get
        Set(value As eMeetPlace)
            SetPropertyValue("MeetPlace", _MeetPlace, value)
        End Set
    End Property
    Property CustomersMeetingState As CustomersMeetingType 'ELBASZTAM a nevét! R.
        Get
            Return _CustomersMeetingType
        End Get
        Set(value As CustomersMeetingType)
            SetPropertyValue("CustomersMeetingType", _CustomersMeetingType, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                If String.IsNullOrEmpty(ShortName) Then _
                    ShortName = value.ShortName
            End If
        End Set
    End Property
    <RuleRequiredField("CustomersMeeting - Customers", DefaultContexts.Save)> _
    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
    Property DateExecution As Date
        Get
            Return _DateExecution
        End Get
        Set(value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
        End Set
    End Property
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <Size(-1)> _
    <RuleRequiredField("CustomersMeeting - Description", DefaultContexts.Save)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            SetPropertyValue("Description", _Description, 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
    <VisibleInListView(False)> _
    Property IsEditable As Boolean
        Get
            Return _IsEditable
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsEditable", _IsEditable, value)
        End Set
    End Property
    Property Months As eMonths
        Get
            Return _Months
        End Get
        Set(value As eMonths)
            SetPropertyValue("Months", _Months, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    <VisibleInLookupListView(False)> _
    Property EmailHasBeenSent As Boolean
        Get
            Return _EmailHasBeenSent
        End Get
        Set(value As Boolean)
            SetPropertyValue("EmailHasBeenSent", _EmailHasBeenSent, value)
        End Set
    End Property

    <VisibleInListView(False)> _
    <Association("CustomersMeeting-CustomersMeetingDocuments", GetType(CustomersMeetingDocuments))> _
    ReadOnly Property CustomersMeetingDocuments As XPCollection(Of CustomersMeetingDocuments)
        Get
            Return GetCollection(Of CustomersMeetingDocuments)("CustomersMeetingDocuments")
        End Get
    End Property
    <Browsable(False)> _
    ReadOnly Property NumberGenerator As NumberGenerator
        Get
            Return TryCast(Session.FindObject(Of SupportNG)(CriteriaOperator.Parse("SupportType=2")), SupportNG).NumberGenerator
        End Get
    End Property

    <Browsable(False)> _
    ReadOnly Property CustomersMeetingSubContractorCustomers As XPCollection(Of Customers)
        Get
            If TryCast(SecuritySystem.CurrentUser, User) Is Nothing Then Return Nothing
            Dim _SubContractorCustomers_Collection As New XPCollection(Of Customers)(Session, CriteriaOperator.Parse("1=2"))
            Dim _User As User = Session.GetObjectByKey(Of User)(TryCast(SecuritySystem.CurrentUser, User).Oid)
            Dim _Contacts As Contacts = Session.FindObject(Of Contacts)(DevExpress.Data.Filtering.CriteriaOperator.Parse("User = ?", _User))
            If _Contacts.Customers IsNot Nothing Then
                Dim _Subcontractor As Subcontractor = Session.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _Contacts.Customers))
                If _Subcontractor IsNot Nothing Then
                    For Each _SubcontractorCustomers As SubcontractorCustomers In _Subcontractor.SubcontractorCustomers
                        _SubContractorCustomers_Collection.Add(_SubcontractorCustomers.Customers)
                    Next
                End If

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