Dans le calendrier du CRM, ajouter un rendez-vous depuis le menu contextuel

0000002798     -      16/02/2018

La programmation illustrée ici permet d'ajouter de façon rapide une nouvelle action dans le calendrier du CRM (rendez-vous). Elle offre la possibilité de by-passer la saisie du modèle d'action et de la fiche de signalétique (client, prospect, ...) qui devra être associée à cette action. Dans le code ci-dessous, ces 2 paramètres sont fixés en tant que constantes. Il sera simple d'adapter cela pour d'autres usages.

Il est impératif de veiller à ce que le modèle d'action utilisé soit compatible avec le calendrier. (type_pim=2)

Si, avant appel du menu contextuel, une fourchette d'heures a été sélectionnée dans le calendrier, celle-ci sera prise en compte pour fixer le début et la fin de l'action.

Le code est le suivant :

Zoom
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorExtensions;
using MercatorUi;
using DevComponents.DotNetBar;

namespace Main
{
    public class Customizer : MercatorUi.ICustomizers.IExec
    {

        public void Main(MercatorUi.ICustomizers.ExecAction Action)
        {
            if (Action == MercatorUi.ICustomizers.ExecAction.DossierOpen)
            {
                Globals.Main.BaseFormCreating += new MercatorUi.Main.BaseFormCreatingEventHandler(Main_BaseFormCreating);
            }
            else if (Action == MercatorUi.ICustomizers.ExecAction.DossierClose)
            {
                Globals.Main.BaseFormCreating -= new MercatorUi.Main.BaseFormCreatingEventHandler(Main_BaseFormCreating);
            }
        }

        void Main_BaseFormCreating(object sender, MercatorUi.Main.BaseFormCreatingEventArgs e)
        {
            if (e.Form is MercatorUi.Forms.Action.CrmCalendarForm)
            {
                MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)e.Form;
                crmCalendarForm.ShowingContextMenu += crmCalendarForm_ShowingContextMenu;
                crmCalendarForm.Disposed += crmCalendarForm_Disposed;
            }
        }

        void crmCalendarForm_Disposed(object sender, EventArgs e)
        {
            MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)sender;
            crmCalendarForm.ShowingContextMenu -= crmCalendarForm_ShowingContextMenu;
            crmCalendarForm.Disposed -= crmCalendarForm_Disposed;
        }

        void crmCalendarForm_ShowingContextMenu(object sender, MercatorUi.Forms.Action.CrmCalendarForm.ShowingContextMenuEventArgs e)
        {
            MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)sender;

            ButtonItem addCustomActionItem = new ButtonItem();
            addCustomActionItem.Text = "Ajouter Action Custom";
            addCustomActionItem.Name = "addCustomActionItem";
            addCustomActionItem.Tag = crmCalendarForm;
            addCustomActionItem.Click += AddCustomActionItem_Click;
            addCustomActionItem.Disposed += addCustomActionItem_Disposed;
            e.ContextMenuItems.Add(addCustomActionItem);
        }

        private void addCustomActionItem_Disposed(object sender, EventArgs e)
        {
            ButtonItem addCustomActionItem = (ButtonItem)sender;
            addCustomActionItem.Disposed -= addCustomActionItem_Disposed;
            addCustomActionItem.Click -= AddCustomActionItem_Click;
        }

        private const string idActTempl = ".A-85FA695"; // ID dans ACTTEMPL. Attention : TYPE_PIM doit valoir 2 pour ce layout d'action (compatible calendrier)
        private const string IdCli = "INEO1     "; // ce client doit exister

        private void AddCustomActionItem_Click(object sender, EventArgs e)
        {
            ButtonItem addCustomActionItem = (ButtonItem)sender;
            MercatorUi.Forms.Action.CrmCalendarForm crmCalendarForm = (MercatorUi.Forms.Action.CrmCalendarForm)addCustomActionItem.Tag;
            DateTime startDate = crmCalendarForm.CalendarView.DateSelectionStart.GetValueOrDefault();
            DateTime endDate = crmCalendarForm.CalendarView.DateSelectionEnd.GetValueOrDefault();
            MercatorUi.Engine.Crm.ActionEngine actionEngine = MercatorUi.Engine.Crm.ActionEngine.InitNew(MercatorUi.Sig._SigEnum.CLI, IdCli, idActTempl);
            if (actionEngine.DataSet == null)
                return;
            actionEngine.ActionsRecord.MOMENT_1 = startDate;
            actionEngine.ActionsRecord.MOMENT_2 = endDate;
            actionEngine.CalendarOwnerKey = crmCalendarForm.CalendarView.SelectedOwner;
            MercatorUi.Forms.Action.ActionForm actionFormNew = new MercatorUi.Forms.Action.ActionForm(idActTempl.Substring(1), MercatorUi.Forms.Action.ActionClasses.ActionFormMode.Normal, actionEngine, null);
            actionFormNew.Show(Globals.iw);
        }
    }
}