Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Utils

Public Class CRM_LeadsVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of CRM_LeadsVC)().aCRMTaskCompleted.Active.SetItemValue("", False)
        Frame.GetController(Of CRM_LeadsVC)().aCRMCreateTaskwithActivity.Active.SetItemValue("", False)
        Frame.GetController(Of CRM_LeadsVC)().aEdit_CRM_Leads_Activity.Active.SetItemValue("", False)
        Frame.GetController(Of CRM_LeadsVC)().aOpenCRM_Opportunities.Active.SetItemValue("", False)

        If View.Id = "CRM_Leads_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Leads_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CRM_LeadsVC)().aCRMCreateTaskwithActivity.Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Opportunities_DetailView" Then
            '--------Ha egy saját cég van, akkor az automatikusan beáll (mint a gerely)--------------->>>
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _CustomersFrom As XPCollection = _ocur.CreateCollection(GetType(CustomersFrom), CriteriaOperator.Parse("1=1"))

            If _CustomersFrom.Count = 1 Then
                Dim _CRM_Opportunities As CRM_Opportunities = TryCast(View.SelectedObjects(0), CRM_Opportunities)
                _CRM_Opportunities.CustomersFrom = _CustomersFrom(0)
            End If
            '-----------------------------------------------------------------------------------------<<<
        End If
        If View.Id = "CRM_Leads_Activity_Task_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ObjectMethodActionsViewController).Actions("Task.MarkCompleted").Active.SetItemValue("", False)
            Frame.GetController(Of CRM_LeadsVC)().aCRMTaskCompleted.Active.SetItemValue("", True)
            Frame.GetController(Of CRM_LeadsVC)().aOpenCRM_Opportunities.Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_CRM_Leads_Activity_ListView" Then
            Frame.GetController(Of CRM_LeadsVC)().aEdit_CRM_Leads_Activity.Active.SetItemValue("", True)
            'Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)

        End If
        If View.Id = "CRM_Leads_CRM_Opportunity_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)

        End If
        If View.Id = "CRM_Leads_Activity_CRM_Leads_Activity_Task_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ObjectMethodActionsViewController).Actions("Task.MarkCompleted").Active.SetItemValue("", False)
            Frame.GetController(Of CRM_LeadsVC)().aCRMTaskCompleted.Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_CRMNote_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Leads_CRM_LeadsDocumentation_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "CRM_Leads_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_CRM_Leads_Activity_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_CRM_Opportunity_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_Activity_CRM_Leads_Activity_Task_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CRM_LeadsVC)().aCRMTaskCompleted.Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Leads_Activity_Task_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ObjectMethodActionsViewController).Actions("Task.MarkCompleted").Active.SetItemValue("", True)
            Frame.GetController(Of CRM_LeadsVC)().aCRMTaskCompleted.Active.SetItemValue("", False)
            Frame.GetController(Of CRM_LeadsVC)().aOpenCRM_Opportunities.Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Leads_CRMNote_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Leads_CRM_LeadsDocumentation_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
        End If
    End Sub
#End Region
    Private Sub aQualifiedLeads_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aQualifiedLeads.CustomizePopupWindowParams
        Dim _onew = Application.CreateObjectSpace
        Dim _CRM_Leads_Qualification As CRM_Leads_Qualification
        _CRM_Leads_Qualification = _onew.CreateObject(Of CRM_Leads_Qualification)()
        e.View = Application.CreateDetailView(_onew, "CRM_Leads_Qualification_DetailView", False, _CRM_Leads_Qualification)
    End Sub
    Private Sub aQualifiedLeads_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aQualifiedLeads.Execute
        Dim _CRM_Leads_Qualification As CRM_Leads_Qualification = TryCast(e.PopupWindow.View.SelectedObjects(0), CRM_Leads_Qualification)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRM_Leads As CRM_Leads = TryCast(View.SelectedObjects(0), CRM_Leads)
        Dim _CustomersGLParameters As CustomersGLParameters = _ocur.FindObject(Of CustomersGLParameters)(CriteriaOperator.Parse("DefaultforBusiness=True"))
        Dim _CRM_Opportunities As CRM_Opportunities
        Dim _Customers As Customers = _ocur.FindObject(Of Customers)(CriteriaOperator.Parse("Address1.ZipPostal=? AND Address1.City=? AND Address1.Street=?", _CRM_Leads.ZIP, _CRM_Leads.City, _CRM_Leads.Street1))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("Email=?", _CRM_Leads.EMail))
        Dim _Address1 As Address
        Dim _CRM_Opportunities_Probability As CRM_Opportunities_Probability = _ocur.FindObject(Of CRM_Opportunities_Probability)(CriteriaOperator.Parse("Probability<=30 and Probability>0"))

        If _CustomersGLParameters Is Nothing Then
            _ocur.Rollback()
            Throw New Exception("*Nincs beállítva főkönyvi kategória !")
            Exit Sub
        End If
        If _CRM_Opportunities_Probability Is Nothing Then
            _ocur.Rollback()
            Throw New Exception("*Nincs beállítva CRM valószínűség törzs !")
            Exit Sub
        End If


        If _CRM_Leads_Qualification.Customers Then
            If _Customers Is Nothing Then
                _Customers = _ocur.CreateObject(Of Customers)()
                _Address1 = _ocur.CreateObject(Of Address)()

                _Address1.ZipPostal = _CRM_Leads.ZIP
                _Address1.Country = _CRM_Leads.Country
                _Address1.City = _CRM_Leads.City
                _Address1.Street = _CRM_Leads.Street1

                _Customers.Address1 = _Address1
            Else
                _Customers.Address1.ZipPostal = _CRM_Leads.ZIP
                _Customers.Address1.Country = _CRM_Leads.Country
                _Customers.Address1.City = _CRM_Leads.City
                _Customers.Address1.Street = _CRM_Leads.Street1
            End If

            _Customers.Name = _CRM_Leads.CompanyName
            _Customers.FullName = _CRM_Leads.CompanyName
            _Customers.CustomersGLParameters = _CustomersGLParameters
            _Customers.CRM_Leads = _CRM_Leads
            _CRM_Leads.LeadStatus = eLeadStatus.Qualified
            If _CRM_Leads_Qualification.CreateOpportunities Then
                _CRM_Opportunities = _ocur.CreateObject(Of CRM_Opportunities)()
                _CRM_Opportunities.Customers = _Customers
                _CRM_Opportunities.CRM_Leads = _CRM_Leads
                _CRM_Opportunities.CRM_Opportunities_Probability = _CRM_Opportunities_Probability
                _CRM_Opportunities.Probability = _CRM_Opportunities_Probability.Probability

            End If
        End If

        If _CRM_Leads_Qualification.Contact Then
            If _Contacts Is Nothing Then
                _Contacts = _ocur.CreateObject(Of Contacts)()
            End If
            _Contacts.FirstName = _CRM_Leads.FirstName
            _Contacts.LastName = _CRM_Leads.LastName
            _Contacts.Email = _CRM_Leads.EMail

            If _CRM_Leads.OfficePhone <> "" Then    'Telefonszámok 
                Dim _OfficePhone As DevExpress.Persistent.BaseImpl.PhoneNumber = _ocur.CreateObject(Of DevExpress.Persistent.BaseImpl.PhoneNumber)()
                _OfficePhone.PhoneType = "OfficePhone"
                _OfficePhone.Number = _CRM_Leads.OfficePhone
                _Contacts.PhoneNumbers.Add(_OfficePhone)
            End If

            If _CRM_Leads.Fax <> "" Then
                Dim _Fax As DevExpress.Persistent.BaseImpl.PhoneNumber = _ocur.CreateObject(Of DevExpress.Persistent.BaseImpl.PhoneNumber)()
                _Fax.PhoneType = "Fax"
                _Fax.Number = _CRM_Leads.Fax
                _Contacts.PhoneNumbers.Add(_Fax)
            End If
            If _CRM_Leads.MobilePhone <> "" Then
                Dim _MobilePhone As DevExpress.Persistent.BaseImpl.PhoneNumber = _ocur.CreateObject(Of DevExpress.Persistent.BaseImpl.PhoneNumber)()
                _MobilePhone.PhoneType = "MobilePhone"
                _MobilePhone.Number = _CRM_Leads.MobilePhone
                _Contacts.PhoneNumbers.Add(_MobilePhone)
            End If
            If _CRM_Leads.HomePhone <> "" Then
                Dim _HomePhone As DevExpress.Persistent.BaseImpl.PhoneNumber = _ocur.CreateObject(Of DevExpress.Persistent.BaseImpl.PhoneNumber)()
                _HomePhone.PhoneType = "HomePhone"
                _HomePhone.Number = _CRM_Leads.HomePhone
                _Contacts.PhoneNumbers.Add(_HomePhone)
            End If
            If _CRM_Leads.OtherPhone <> "" Then
                Dim _OtherPhone As DevExpress.Persistent.BaseImpl.PhoneNumber = _ocur.CreateObject(Of DevExpress.Persistent.BaseImpl.PhoneNumber)()
                _OtherPhone.PhoneType = "OtherPhone"
                _OtherPhone.Number = _CRM_Leads.OtherPhone
                _Contacts.PhoneNumbers.Add(_OtherPhone)
            End If
            _Contacts.CRM_Leads = _CRM_Leads
            _CRM_Leads.LeadStatus = eLeadStatus.Qualified

            If _CRM_Opportunities IsNot Nothing Then
                _CRM_Opportunities.Contacts = _Contacts
            End If
        End If
        _ocur.CommitChanges()
        If _CRM_Leads_Qualification.CreateOpportunities Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Opportunities_DetailView", True, _onew.GetObject(_CRM_Opportunities))
            'e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow
        End If
        View.Close()
    End Sub
    '-- Subs --
    Private Sub aDisQualifiedLeads_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aDisQualifiedLeads.CustomizePopupWindowParams
        Try
            Dim _CRM_Leads As CRM_Leads = TryCast(View.SelectedObjects(0), CRM_Leads)
            If _CRM_Leads.CRM_Opportunity.Count = 0 Then Throw New UserFriendlyException("Nem lehet visszaminõsíteni a regisztrációt, mert létezik CRM ügye!")
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CRM_Leads_DisQualification As CRM_Leads_DisQualification
            _CRM_Leads_DisQualification = _onew.CreateObject(Of CRM_Leads_DisQualification)()
            e.View = Application.CreateDetailView(_onew, "CRM_Leads_DisQualification_DetailView", False, _CRM_Leads_DisQualification)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aDisQualifiedLeads_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aDisQualifiedLeads.Execute
        Dim _CRM_Leads_DisQualification As CRM_Leads_DisQualification = TryCast(e.PopupWindow.View.SelectedObjects(0), CRM_Leads_DisQualification)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRM_Leads As CRM_Leads = TryCast(View.SelectedObjects(0), CRM_Leads)
        _CRM_Leads.LeadStatus = _CRM_Leads_DisQualification.DisQualificationLeadStatus
    End Sub
    Private Sub aCreate_CRM_Leads_Activity_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreate_CRM_Leads_Activity.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
            Dim _CRM_Opportunities As CRM_Opportunities = TryCast(_PropertyCollectionSource.MasterObject, CRM_Opportunities)
            Dim _CRM_Leads As CRM_Leads = TryCast(_PropertyCollectionSource.MasterObject, CRM_Leads)

            Dim _CRM_Leads_Activity As CRM_Leads_Activity = _onew.CreateObject(Of CRM_Leads_Activity)()

            If _CRM_Leads IsNot Nothing Then
                _CRM_Leads_Activity.CRM_Leads = _onew.GetObject(_CRM_Leads)
                _CRM_Leads_Activity.HRPerson = _onew.GetObject(_CRM_Leads.HRPerson)

            ElseIf _CRM_Opportunities IsNot Nothing Then
                _CRM_Leads_Activity.CRM_Leads = _onew.GetObject(_CRM_Opportunities.CRM_Leads)
                _CRM_Leads_Activity.HRPerson = _onew.GetObject(_CRM_Opportunities.HRPerson)
                _CRM_Leads_Activity.CRM_Opportunities = _onew.GetObject(_CRM_Opportunities)
            End If

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Leads_Activity_DetailView", True, _CRM_Leads_Activity)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aCreate_CRM_Leads_Activity_Task_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreate_CRM_Leads_Activity_Task.Execute
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        Dim _CRM_Leads_Activity As CRM_Leads_Activity = TryCast(_PropertyCollectionSource.MasterObject, CRM_Leads_Activity)

        If _CRM_Leads_Activity IsNot Nothing Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = _onew.CreateObject(Of CRM_Leads_Activity_Task)()

            _CRM_Leads_Activity_Task.CRM_Leads_Activity = _onew.GetObject(_CRM_Leads_Activity)

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Leads_Activity_Task_DetailView", True, _CRM_Leads_Activity_Task)
        Else
            Throw New Exception("*Nincs kiválasztott aktivitás !")
        End If
    End Sub
    Private Sub aCRMTaskCompleted_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCRMTaskCompleted.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        If View.SelectedObjects.Count > 0 Then
            Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = TryCast(View.SelectedObjects(0), CRM_Leads_Activity_Task)

            If _CRM_Leads_Activity_Task IsNot Nothing Then
                If _CRM_Leads_Activity_Task.Status <> General.TaskStatus.Completed Then
                    Dim _CRM_Leads_Actvity_Completed_Popup As CRM_Leads_Actvity_Completed_Popup = _onew.CreateObject(Of CRM_Leads_Actvity_Completed_Popup)()

                    e.View = Application.CreateDetailView(_onew, "CRM_Leads_Actvity_Completed_Popup_DetailView", False, _CRM_Leads_Actvity_Completed_Popup)
                Else
                    _onew.Rollback()
                    Throw New Exception("*A feladat már befejezett státuszban van!")
                End If
            End If
        Else
            _onew.Rollback()
            Throw New Exception("*Nincs kiválasztott sor!")
        End If
    End Sub
    Private Sub aCRMTaskCompleted_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCRMTaskCompleted.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRM_Leads_Actvity_Completed_Popup As CRM_Leads_Actvity_Completed_Popup = TryCast(e.PopupWindow.View.SelectedObjects(0), CRM_Leads_Actvity_Completed_Popup)
        Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = TryCast(View.SelectedObjects(0), CRM_Leads_Activity_Task)
        Dim _CRM_Leads_Activity As CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()

        If _CRM_Leads_Activity_Task.CRM_Leads_Activity IsNot Nothing Then
            _CRM_Leads_Activity.CRM_Leads = _CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Leads
            _CRM_Leads_Activity.CRM_Opportunities = _CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Opportunities
            _CRM_Leads_Activity.Description = _CRM_Leads_Actvity_Completed_Popup.Description & vbNewLine & _CRM_Leads_Activity_Task.CRM_Leads_Activity.Description
            _CRM_Leads_Activity.EventType = _CRM_Leads_Activity_Task.CRM_Leads_Activity.EventType
            _CRM_Leads_Activity.Status = _CRM_Leads_Activity_Task.CRM_Leads_Activity.Status
            _CRM_Leads_Activity.Subject = _CRM_Leads_Activity_Task.CRM_Leads_Activity.Subject
        Else
            _CRM_Leads_Activity.CRM_Leads = _CRM_Leads_Activity_Task.CRM_Opportunities.CRM_Leads
            _CRM_Leads_Activity.CRM_Opportunities = _CRM_Leads_Activity_Task.CRM_Opportunities
            _CRM_Leads_Activity.Description = _CRM_Leads_Actvity_Completed_Popup.Description & vbNewLine & _CRM_Leads_Activity_Task.CRM_Leads_Activity.Description
            _CRM_Leads_Activity.EventType = eEventType.eBusinessDirectory
            _CRM_Leads_Activity.Status = _CRM_Leads_Activity_Task.Status
            _CRM_Leads_Activity.Subject = _CRM_Leads_Activity_Task.Subject
        End If
      
        _CRM_Leads_Activity.UserCreated = _ocur.Session.GetObjectByKey(Of User)(_ocur.Session.GetKeyValue(SecuritySystem.CurrentUser))

        _CRM_Leads_Activity_Task.Results = _CRM_Leads_Actvity_Completed_Popup.Description
        _CRM_Leads_Activity_Task.Status = General.TaskStatus.Completed

        _ocur.CommitChanges()

        View.Refresh()
    End Sub
    Private Sub aCRMCreateTaskwithActivity_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCRMCreateTaskwithActivity.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _CRM_Leads_Activity_Task_Popup As CRM_Leads_Activity_Task_Popup = _onew.CreateObject(Of CRM_Leads_Activity_Task_Popup)()

        e.View = Application.CreateDetailView(_onew, "CRM_Leads_Activity_Task_Popup_DetailView", False, _CRM_Leads_Activity_Task_Popup)
    End Sub
    Private Sub aCRMCreateTaskwithActivity_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCRMCreateTaskwithActivity.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRM_Leads_Activity_Task_Popup As CRM_Leads_Activity_Task_Popup = TryCast(e.PopupWindow.View.SelectedObjects(0), CRM_Leads_Activity_Task_Popup)
        Dim _CRM_Leads As CRM_Leads = TryCast(View.SelectedObjects(0), CRM_Leads)
        Dim _CRM_Opportunities As CRM_Opportunities = TryCast(View.SelectedObjects(0), CRM_Opportunities)

        If _CRM_Leads IsNot Nothing Then
            Dim _CRM_Leads_Activity As CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()
            Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = _ocur.CreateObject(Of CRM_Leads_Activity_Task)()

            _CRM_Leads_Activity.CRM_Leads = _CRM_Leads
            _CRM_Leads_Activity.CRM_Opportunities = _CRM_Opportunities
            _CRM_Leads_Activity.CRM_Leads_Activity_Task.Add(_CRM_Leads_Activity_Task)
            _CRM_Leads_Activity.Description = _CRM_Leads_Activity_Task_Popup.Description
            _CRM_Leads_Activity.EventType = _CRM_Leads_Activity_Task_Popup.EventType
            _CRM_Leads_Activity.HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity_Task_Popup.HRPerson.Oid))
            _CRM_Leads_Activity.Subject = _CRM_Leads_Activity_Task_Popup.Subject

            _CRM_Leads_Activity_Task.CRM_Leads_Activity = _CRM_Leads_Activity
            _CRM_Leads_Activity_Task.Description = _CRM_Leads_Activity_Task_Popup.Description
            _CRM_Leads_Activity_Task.DueDate = _CRM_Leads_Activity_Task_Popup.DueDate
            _CRM_Leads_Activity_Task.HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity_Task_Popup.HRPerson.Oid))
            _CRM_Leads_Activity_Task.Subject = _CRM_Leads_Activity_Task_Popup.Subject
            _CRM_Leads_Activity_Task.StartDate = GetSQLTime(_ocur.Session)

            _ocur.CommitChanges()
        ElseIf _CRM_Opportunities IsNot Nothing Then
            Dim _CRM_Leads_Activity As CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()
            Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = _ocur.CreateObject(Of CRM_Leads_Activity_Task)()

            If _CRM_Opportunities.CRM_Leads Is Nothing Then
                Dim _CRM_Leads_New As CRM_Leads = _ocur.CreateObject(Of CRM_Leads)()

                _CRM_Leads_New.LeadStatus = eLeadStatus.Qualified
                _CRM_Leads_New.Customers = _ocur.GetObject(_CRM_Opportunities.Customers)
                _CRM_Opportunities.Customers.CRM_Leads = _CRM_Leads_New
                _CRM_Leads_New.CompanyName = _CRM_Leads_New.Customers.FullName
                _CRM_Leads_New.Topic = _CRM_Leads_New.Customers.FullName
                _CRM_Leads_New.EMail = "-"
                _CRM_Leads_New.MobilePhone = "-"
                _CRM_Leads_New.LastName = "-"
                _CRM_Leads_New.FirstName = "-"
                _CRM_Leads_New.CRM_FirstMeet = _ocur.FindObject(Of CRM_FirstMeet)(CriteriaOperator.Parse("DefaultforBusiness=True"))

                _CRM_Leads_Activity.CRM_Leads = _CRM_Leads_New
                _CRM_Opportunities.CRM_Leads = _CRM_Leads_New
            Else
                _CRM_Leads_Activity.CRM_Leads = _CRM_Opportunities.CRM_Leads
            End If
            _CRM_Leads_Activity.CRM_Opportunities = _CRM_Opportunities
            _CRM_Leads_Activity.Description = _CRM_Leads_Activity_Task_Popup.Description
            _CRM_Leads_Activity.EventType = _CRM_Leads_Activity_Task_Popup.EventType
            _CRM_Leads_Activity.HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity_Task_Popup.HRPerson.Oid))
            _CRM_Leads_Activity.Subject = _CRM_Leads_Activity_Task_Popup.Subject

            _CRM_Leads_Activity.CRM_Leads_Activity_Task.Add(_CRM_Leads_Activity_Task)

            _CRM_Leads_Activity_Task.CRM_Leads_Activity = _CRM_Leads_Activity
            _CRM_Leads_Activity_Task.Description = _CRM_Leads_Activity_Task_Popup.Description
            _CRM_Leads_Activity_Task.DueDate = _CRM_Leads_Activity_Task_Popup.DueDate
            _CRM_Leads_Activity_Task.HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity_Task_Popup.HRPerson.Oid))
            _CRM_Leads_Activity_Task.Subject = _CRM_Leads_Activity_Task_Popup.Subject
            _CRM_Leads_Activity_Task.StartDate = GetSQLTime(_ocur.Session)

            _ocur.CommitChanges()
        End If
        View.Refresh()
    End Sub
    Private Sub aEdit_CRM_Leads_Activity_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aEdit_CRM_Leads_Activity.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CRM_Leads_Activity As CRM_Leads_Activity = TryCast(View.SelectedObjects(0), CRM_Leads_Activity)
        If _CRM_Leads_Activity IsNot Nothing Then
            If _CRM_Leads_Activity.EventType <> eEventType.eEMailIn And _CRM_Leads_Activity.EventType <> eEventType.eEmailOut Then
                e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Leads_Activity_DetailView", True, _onew.GetObject(_CRM_Leads_Activity))
            Else
                Throw New Exception("*Email-el kapcsolatos aktivitást nem lehet szerkeszteni!")
            End If
        End If
    End Sub
    Private Sub aOpenCRM_Opportunities_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenCRM_Opportunities.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = TryCast(View.SelectedObjects(0), CRM_Leads_Activity_Task)

        If _CRM_Leads_Activity_Task IsNot Nothing Then
            If _CRM_Leads_Activity_Task.CRM_Leads_Activity IsNot Nothing Then
                If _CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Opportunities IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Opportunities_DetailView", True, _onew.GetObject(_CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Opportunities))
                    ' e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow
                ElseIf _CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Leads IsNot Nothing Then
                    If _CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Leads.LeadStatus <> eLeadStatus.Qualified Then
                        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Leads_DetailView", True, _onew.GetObject(_CRM_Leads_Activity_Task.CRM_Leads_Activity.CRM_Leads))
                        ' e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow
                    End If
                End If
            Else
                If _CRM_Leads_Activity_Task.CRM_Opportunities IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "CRM_Opportunities_DetailView", True, _onew.GetObject(_CRM_Leads_Activity_Task.CRM_Opportunities))
                End If
            End If
        End If
    End Sub

    Public Sub aCRM_LeadsDocumentation_ChangeGroup_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCRM_LeadsDocumentation_ChangeGroup.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = _onew.CreateObject(Of ChangeFileAttachment_PopUp)()
            e.View = Application.CreateDetailView(_onew, "ChangeFileAttachment_PopUp_DetailView", True, _ChangeFileAttachment_PopUp)
            e.View.Tag = e.Action.Id
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Hiba")
        End Try
    End Sub
    Public Sub aCRM_LeadsDocumentation_ChangeGroup_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCRM_LeadsDocumentation_ChangeGroup.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ChangeFileAttachment_PopUp As ChangeFileAttachment_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ChangeFileAttachment_PopUp)
            For Each _CRM_LeadsDocumentation As CRM_LeadsDocumentation In View.SelectedObjects
                If _ChangeFileAttachment_PopUp.ChangeStatus = True Then
                    _CRM_LeadsDocumentation.Status = _ChangeFileAttachment_PopUp.Status
                End If
                If _ChangeFileAttachment_PopUp.ChangeRowGroup1 = True Then
                    _CRM_LeadsDocumentation.RowGroup1 = _ChangeFileAttachment_PopUp.RowGroup1
                End If
                If _ChangeFileAttachment_PopUp.ChangeRowGroup2 = True Then
                    _CRM_LeadsDocumentation.RowGroup2 = _ChangeFileAttachment_PopUp.RowGroup2
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Hiba")
        End Try
    End Sub
    Private Sub aCRM_LeadsDocumentation_Remove_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aCRM_LeadsDocumentation_Remove.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)

            Dim _CRM_Opportunities As CRM_Opportunities = TryCast(_PropertyCollectionSource.MasterObject, CRM_Opportunities)

            For Each _CRM_LeadsDocumentation As CRM_LeadsDocumentation In e.SelectedObjects
                If _CRM_Opportunities IsNot Nothing Then
                    _CRM_Opportunities.CRM_Leads.CRM_LeadsDocumentation.Remove(_CRM_LeadsDocumentation)
                End If
            Next
            _ocur.CommitChanges()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aCRM_Leads_Activity_Task_OpenBusinessDirectory_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aCRM_Leads_Activity_Task_OpenBusinessDirectory.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task = TryCast(View.SelectedObjects(0), CRM_Leads_Activity_Task)
        Dim _BusinessDirectory As BusinessDirectory = _ocur.FindObject(Of BusinessDirectory)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity_Task.MasterKey))

        If _BusinessDirectory IsNot Nothing Then
            If Not _BusinessDirectory.IsFolder Then
                Dim _FileName As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetTempPath, _BusinessDirectory.FileData.FileName)
                Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create)

                _BusinessDirectory.FileData.SaveToStream(_StreamFile)
                _StreamFile.Close()
                Process.Start(_FileName)
            End If
        End If
    End Sub
End Class
