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

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

            if (!billingEngine.LIGNES.Columns.Contains("stock_xxx"))
            {
                billingEngine.LIGNES.Columns.Add("stock_xxx", typeof(string));
                billingEngine.LIGNES.Columns.Add("stock_yyy", typeof(string));

                bool billingEngineWasModified = billingEngine.IsModified;

                //Uitlezen van de STOCK-velden die zich niet in de LIGNES_V bevinden
                DataSet ds = Api.Zselect(Globals.RepData, "select s_id, s_xxx, s_yyy from STOCK (NOLOCK) "
                                                        + "where exists(select * from LIGNES_V (NOLOCK) where (id=@id) and (journal=@journal) and (piece=@piece) and (id_article=s_id))",
                                new MercatorSqlParam("@id", billingEngine.Id, SqlDbType.Char),
                                new MercatorSqlParam("@journal", billingEngine.Journal, SqlDbType.Char),
                                new MercatorSqlParam("@piece", billingEngine.Piece));
                if (ds != null)
                {
                    Api.TrimEndDataTable(ds.Tables[0]);
                    foreach (DataRow billingLine in billingEngine.LIGNES.Rows)
                    {
                        DataRow[] stock = ds.Tables[0].Select(string.Format("s_id='{0}'", Api.UnquoteSql(billingLine["id_article"].ToString())));
                        InsertStockInBillingLine((stock.Length > 0 ? stock[0] : null), billingLine, billingEngine);
                    }
                }
                if (!billingEngineWasModified)
                    billingEngine.LIGNES.AcceptChanges();
            }
        }

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

        void BillingEngine_AfterInsertItem(object sender, MercatorUi.Engine.Gescom.BillingEngine.AfterInsertItemEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            InsertStockInBillingLine(e.DataRowStock, e.DataRowLignes, billingEngine);
        }

        void InsertStockInBillingLine(DataRow stock, DataRow billingLine, MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            if (stock == null)
            {
                billingLine["stock_xxx"] = "";
                billingLine["stock_yyy"] = "";
            }
            else
            {
                billingLine["stock_xxx"] = stock["s_xxx"];
                billingLine["stock_yyy"] = stock["s_yyy"];
            }
        }
    }
}