using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;

namespace MyNameSpace
{
    public class Class1 : MercatorUi.Interfaces.IExec
    {

        public void Main()
        {
            using (MercatorUi.Engine.Cpta.BookingEngine bookingEngine = MercatorUi.Engine.Cpta.BookingEngine.InitNew(1, "VENc")) // 1 = vente, le second paramètre = journal
            {
                if (!string.IsNullOrEmpty(bookingEngine.LastError))
                {
                    Dialogs.Stop("Init BookingEngine : " + bookingEngine.LastError);
                    return;
                }
                if (!bookingEngine.ApplyCustomerSupplier("MERCATOR"))
                {
                    Dialogs.Stop("ApplyCustomerSupplier : " + bookingEngine.LastError);
                    return;
                }

                // informations de PIEDS_C
                bookingEngine.PiedsCRecord.REGIME = MercatorDatabase.RegimesEnum.Normal; // ligne facultative car le régime est initialisé par ApplyCustomerSupplier
                bookingEngine.PiedsCRecord.DATE = new DateTime(2012, 1, 15);
                bookingEngine.PeriodFromDate(bookingEngine.PiedsCRecord.DATE); // adaptation de la période en fonction de la date
                bookingEngine.PiedsCRecord.ECHEANCE = Api.AddDate(bookingEngine.PiedsCRecord.DATE, bookingEngine.CliRecord.C_PAIEM); // calcul de la date d'échéance en fonction du délai de paiement repris dans la fiche client
                bookingEngine.PiedsCRecord.REFERENCE = "Ecriture générée par code";
                bookingEngine.PiedsCRecord.HEADERTOTAL = 1210;

                // ajout des lignes
                MercatorUi.Sig.Sig sig_gen = MercatorUi.Sig._SigsStatic.SigByModule(MercatorUi.Sig._SigEnum.GEN); // sera nécessaire pour indiquer qu'on va insérer un compte général
                string id_tva = MercatorController.xFunctions.xLookUpString("TVACODES", "CODE_F", "21", "ID"); // on doit manipuler uniquement des ID de code TVA

                int n = bookingEngine.AppendLine(); // initialise une nouvelle ligne et renvoie le n° de la ligne créée
                if (!bookingEngine.InsertAccount(sig_gen, "700000", bookingEngine.LIGNES_C.Rows[n]))
                {
                    Dialogs.Stop("InsertAccount 1 : " + bookingEngine.LastError);
                    return;
                }
                bookingEngine.LignesCRecords[n].TOT_DV = 1100;
                bookingEngine.LignesCRecords[n].SIGNE = "C"; // ligne facultative car le signe est initialisé par AppendLine
                bookingEngine.LignesCRecords[n].ID_TVA = id_tva;
                bookingEngine.LignesCRecords[n].TVA_DV = 231;

                n = bookingEngine.AppendLine(); // initialise une nouvelle ligne et renvoie le n° de la ligne créée
                if (!bookingEngine.InsertAccount(sig_gen, "700010", bookingEngine.LIGNES_C.Rows[n]))
                {
                    Dialogs.Stop("InsertAccount 2 : " + bookingEngine.LastError);
                    return;
                }
                bookingEngine.LignesCRecords[n].TOT_DV = 100;
                bookingEngine.LignesCRecords[n].SIGNE = "D";
                bookingEngine.LignesCRecords[n].ID_TVA = id_tva;
                bookingEngine.LignesCRecords[n].TVA_DV = 21;


                // sauvegarde
                if (!bookingEngine.Save())
                {
                    Dialogs.Stop("Save : " + bookingEngine.LastError);
                    return;
                }
                Dialogs.Stop(string.Format("L'écriture a été sauvegardée sous le numéro {0}.", bookingEngine.SavedNumber));
            }
        }
    }
}