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

namespace TestDeliveriesInvoicing
{
    public class TestDeliveriesInvoicing
    {

        public void Exec()
        {
            DataSet ds = Api.Zselect(MercatorUi.Globals.RepData, "select * from sequenc where journal='Blivr'" + "\r\n" + "select * from sequenc where journal='Factu'");
            if (ds == null)
                return;
            if (ds.Tables[0].Rows.Count == 0)
            {
                MercatorUi.Dialogs.Stop("Séquence de livraisons non trouvée !");
                return;
            }
            if (ds.Tables[1].Rows.Count == 0)
            {
                MercatorUi.Dialogs.Stop("Séquence de factures non trouvée !");
                return;
            }
            Api.TrimEndDataSet(ds);

            MercatorUi.Forms.Gescom.GescomDialogs.GescomAskDeliveriesInvoicing.GescomAskDeliveriesInvoicingRet ret = new MercatorUi.Forms.Gescom.GescomDialogs.GescomAskDeliveriesInvoicing.GescomAskDeliveriesInvoicingRet(
                ds.Tables[0].Rows[0] // sequencOrig
                , ds.Tables[1].Rows[0] // sequencDest
                , string.Format("(pieds_v.date>={0}) and (pieds_v.date<{1})", Api.DateTimeSqlString(new DateTime(DateTime.Today.Year, DateTime.Today.Month - 1, 1)), Api.DateTimeSqlString(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1))) // whereClause
                , true // groupLines
                , false // groupCustNoVat
                , false // invoiceAccount
                , true // withQZero
                , false // dateAllLines
                , false // noComment
                , 100 // minimum)
                );
            // ret contient toutes les sélections habituellement faites dans la boîte de dialogue     

            int n_fact = MercatorUi.Forms.Gescom.GescomProcedures.Procedures.DeliveriesInvoicing(MercatorUi.Engine.Gescom.Billing.TypeVAEnum.V, ret, true);

            MercatorUi.Dialogs.Stop(string.Format("Facture(s) générée(s) automatiquement : {0}", n_fact));
        }

    }
}