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

namespace Booking
{
    public class Customizer : MercatorUi.ICustomizers.IBookingEngineCreated, MercatorUi.ICustomizers.IBookingEngineClosed
    {

        public void BookingEngineCreated(MercatorUi.Engine.Cpta.BookingEngine BookingEngine)
        {
            BookingEngine.DuringSave += new MercatorUi.Engine.Cpta.BookingEngine.DuringSaveEventHandler(BookingEngine_DuringSave);
        }

        public void BookingEngineClosed(MercatorUi.Engine.Cpta.BookingEngine BookingEngine)
        {
            BookingEngine.DuringSave -= new MercatorUi.Engine.Cpta.BookingEngine.DuringSaveEventHandler(BookingEngine_DuringSave);
        }

        void BookingEngine_DuringSave(object sender, MercatorUi.Engine.Cpta.BookingEngine.DuringSaveEventArgs e)
        {
            e.SqlCommand.CommandText = "declare @id_gescom char(15), @bloq_paiem bit \r\n"
                                     + "select @id_gescom=id_gescom from #lignes_c_tmp where id_fou is not null \r\n"
                                     + "select @bloq_paiem=bloq_paiem from PIEDS_A where (journal=left(@id_gescom,5)) and (piece=right(@id_gescom,10)) \r\n" // @bloq_paiem zal leeg zijn indien het gescom-document niet bestaat
                                     + "if @bloq_paiem=1 \r\n"
                                     + "    update #lignes_c_tmp set nivpaiem=7 where id_fou is not null";
            if (!Api.SqlExec(e.SqlCommand))
                e.CancelSave = true;
        }
    }
}