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

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
    {
        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.BeforeSave += BillingEngine_BeforeSave;
        }

        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.BeforeSave -= BillingEngine_BeforeSave;
        }

        private void BillingEngine_BeforeSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;

            // niet groeperen op verschillende waarden van LIGNES_V.CUSTOM_COL1 en LIGNES_V.CUSTOM_COL2
            string[] supplColumns = new string[] { "CUSTOM_COL1", "CUSTOM_COL2" };

            // bijkomende voorwaarden geschreven in de syntaxis van DataTable.Select()
            string additionalConditionsOnLignes = "(q > 0)" // enkel lijnen met positieve hoeveelheden groeperen
                + " and (id_article<>'S_ID1') and (id_article<>'S_ID2')"; // deze twee artikels nooit groeperen

            // rekening houden met PU, REMISE, REMISE2, ... -> dus enkel groeperen als PU, REMISE, REMISE2, ... identiek zijn
            bool doNotChangePuRemises = true;

            // lijnen met een commentaar in de omschrijving behouden
            bool keyComments = true;

            billingEngine.Group(supplColumns, additionalConditionsOnLignes, doNotChangePuRemises, keyComments);
        }
    }
}