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.ExpressApp.Win.Editors
Imports DevExpress.ExpressApp.Scheduler.Win
Imports DevExpress.XtraScheduler
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Persistent.Base.General
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl

Public Class SchedulerVC
    Inherits DevExpress.ExpressApp.ViewController
    Private scheduler As SchedulerControl
    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "Contacts_CRM_Leads_Activity_ListView" Then
            ' Access the current List View 
            Dim currentView As ListView = CType(View, ListView)
            ' Access the View's List Editor as a SchedulerListEditor 
            Dim listEditor As SchedulerListEditor = CType(currentView.Editor, SchedulerListEditor)
            '  Access the LOist Editor's ScheudlerControl   
            scheduler = listEditor.SchedulerControl

            ' Set the desired time to be visible 
            If Not scheduler Is Nothing Then
                AddHandler scheduler.Storage.AppointmentInserting, AddressOf AppointmentInserting
            End If
        End If

    End Sub
    Sub AppointmentInserting(sender As Object, e As DevExpress.XtraScheduler.PersistentObjectCancelEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Appointment As Appointment
        Dim _CRM_Leads_Activity As CRM_Leads_Activity
        Dim _Contacts As Contacts = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Contacts)
        Dim _CRM_Leads As CRM_Leads
        Dim currentUser As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))

        If _Contacts IsNot Nothing Then
            _Appointment = TryCast(e.Object, Appointment)

            If _Contacts.CRM_Leads Is Nothing Then
                _CRM_Leads = _ocur.CreateObject(Of CRM_Leads)()
                _CRM_Leads.Contacts = _Contacts
                _CRM_Leads.FirstName = _Contacts.FirstName
                _CRM_Leads.LastName = _Contacts.LastName
                _CRM_Leads.HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", currentUser))
                _CRM_Leads.CRM_FirstMeet = _ocur.FindObject(Of CRM_FirstMeet)(CriteriaOperator.Parse("DefaultforBusiness=True"))
                _CRM_Leads.LeadStatus = eLeadStatus.Qualified
            End If
            _CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()
            _CRM_Leads_Activity.CRM_Leads = _Contacts.CRM_Leads
            _CRM_Leads_Activity.EventType = eEventType.eMeeting
            _CRM_Leads_Activity.Subject = _Appointment.Subject
            _CRM_Leads_Activity.StartOn = _Appointment.Start
            _CRM_Leads_Activity.EndOn = _Appointment.End
        End If
        _ocur.CommitChanges()

    End Sub
End Class
