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

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
    {

        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.AfterApplyCustomerSupplier += new MercatorUi.Engine.Gescom.BillingEngine.AfterApplyCustomerSupplierEventHandler(BillingEngine_AfterApplyCustomerSupplier);
        }

        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.AfterApplyCustomerSupplier -= new MercatorUi.Engine.Gescom.BillingEngine.AfterApplyCustomerSupplierEventHandler(BillingEngine_AfterApplyCustomerSupplier);
        }

        void BillingEngine_AfterApplyCustomerSupplier(object sender, MercatorUi.Engine.Gescom.BillingEngine.AfterApplyCustomerSupplierEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (billingEngine.BillingForm == null) // le billingEngine n'est pas lié à une billingForm. On n'est probablement dans un traîtement automatique
                return;
            if (!billingEngine.IsNew) // on ne traîte pas les anciens documents repris en modification
                return;
            DataSet ds = Api.Zselect(Globals.RepData, "select count(id) as nbr_comm from pieds_v (NOLOCK) where (id_cli=@id_cli) and (n_lignes>0) and (type=3)", new MercatorSqlParam("@id_cli", e.IdCliOrFou, SqlDbType.Char));
            if (ds == null)
                return;
            int nbr_comm = Convert.ToInt32(ds.Tables[0].Rows[0]["nbr_comm"]);
            if (nbr_comm == 0)
                return; // pas de commande pour ce client -> on ne poursuit pas la procédure
            if (!Dialogs.AnswerYesNo(_Divers.Iif_langue(Globals.Langue, "Order(s) existing for this customer : ", "Bestande bestellingen voor deze klant : ", "Commande(s) en cours pour ce client : ") + nbr_comm.ToString() + "\r\n" + _Divers.Iif_langue(Globals.Langue, "Browse these orders ?", "Deze bestellingen bladeren ?", "Parcourir ces commandes ?"), billingEngine.BillingForm))
                return; // l'utilisateur ne veut pas voir les commandes -> on ne poursuit pas la procédure
            string reqSql = string.Format("select pieds_v.id,journal,piece,reference,type,date,heure,tot_bas_dv,tot_bas_fb,tot_ttc_dv,tot_ttc_fb,n_dec,id_dev,imprime,cubic as flag_appl,niveau_bo,c_nom,devises.nom as dev_nom "
                                        + "from pieds_v (NOLOCK) inner join cli (NOLOCK) on (pieds_v.id_cli=cli.c_id) "
                                        + "inner join devises(NOLOCK) on (pieds_v.id_dev=devises.id) where (pieds_v.type=3) and (pieds_v.id_cli='{0}') order by pieds_v.date", Api.UnquoteSql(e.IdCliOrFou));
            Globals.Main.ShowHistWithReqSql("V", reqSql, false, billingEngine.BillingForm);
        }
    }
}