using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorController;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace MercatorUi.MovableControls.ButtonsCodes
{
    public static class Script
    {

        public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)clickedButton.Form;
            string journal = Dialogs.AskJournal("Quel type de document ?", MercatorUi._Dialogs.AskPieceEnum.V, 3, true, "").Journal;
            if (!string.IsNullOrEmpty(journal))
            {
                MercatorUi.Engine.Gescom.BillingEngine billingEngine = MercatorUi.Engine.Gescom.BillingEngine.InitNew(MercatorUi.Engine.Gescom.Billing.TypeVAEnum.V, 3, journal);
                billingEngine.ApplyCustomerSupplier(sigForm.CliRecord.C_IDCOMPTA);
                billingEngine.ApplyCliLiv(sigForm.CliRecord.C_ID);
                MercatorUi.Forms.Billing.BillingForm billingForm = Globals.Main.ShowBillingNewInThread(billingEngine);
                billingForm.FullInitialized += billingForm_fullInitialized;
            }
        }
        private static void billingForm_fullInitialized(object sender, EventArgs e)
        {
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)sender;
            billingForm.FullInitialized -= billingForm_fullInitialized;
            List<Control> controls = billingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.LinesEditor));
            if (controls.Count > 0)
            {
                MercatorUi.Forms.Billing.BillingObjects.LinesEditor ln = (MercatorUi.Forms.Billing.BillingObjects.LinesEditor)controls[0];
                ln.Focus();
            }
        }
    }
}