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.AfterInsertItem += new MercatorUi.Engine.Gescom.BillingEngine.AfterInsertItemEventHandler(AfterInsertItem);
BillingEngine.BeforePayment += new EventHandler(BeforePayment);
BillingEngine.AfterSave += new EventHandler(AfterSave);
}
public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
{
BillingEngine.AfterInsertItem -= new MercatorUi.Engine.Gescom.BillingEngine.AfterInsertItemEventHandler(AfterInsertItem);
BillingEngine.BeforePayment -= new EventHandler(BeforePayment);
BillingEngine.AfterSave -= new EventHandler(AfterSave);
}
void AfterInsertItem(object sender, MercatorUi.Engine.Gescom.BillingEngine.AfterInsertItemEventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
if (e.DataRowLignes["S_CLE1"].ToString().Trim() == "UTILBONCAD")
{
string id_boncad = Dialogs.AskString("Veuillez scanner le code-barres", "", true, 35);
if (id_boncad != "")
{
DataRow[] drcad = billingEngine.LIGNES.Select("id_boncad='" + id_boncad.Trim() + "'");
int nbBoncad = 0;
if (drcad != null)
{
foreach (DataRow drcad2 in drcad)
{
nbBoncad++;
}
}
if (nbBoncad == 0)
{
string reqSqlcad = "select count(*) as nbUtil, sum(q) as q, sum(pu) as pu from lignes_v where id_boncad=@id_boncad";
DataSet dscad = Api.Zselect(MercatorUi.Globals.RepData, reqSqlcad, new MercatorSqlParam("@id_boncad", id_boncad, SqlDbType.Char));
if (dscad != null)
{
if (Convert.ToInt16(dscad.Tables[0].Rows[0]["nbUtil"]) == 0)
{
Dialogs.Stop("Ce bon cadeau n'existe pas !");
e.CancelInsertItem = true;
}
else
{
if (Convert.ToInt16(dscad.Tables[0].Rows[0]["nbUtil"]) > 1)
{
Dialogs.Stop("Ce bon cadeau a déjà été utilisé !");
e.CancelInsertItem = true;
}
else
{
e.DataRowLignes["Q"] = Convert.ToDouble(dscad.Tables[0].Rows[0]["q"]) * -1;
e.DataRowLignes["PU"] = Convert.ToDouble(dscad.Tables[0].Rows[0]["pu"]);
e.DataRowLignes["ID_BONCAD"] = id_boncad.Trim();
}
}
}
else
{
e.CancelInsertItem = true;
}
}
else
{
Dialogs.Stop("Ce bon cadeau a déjà été utilisé au sein de cette vente !");
e.CancelInsertItem = true;
}
}
else
{
Dialogs.Stop("Code-barres vide !");
e.CancelInsertItem = true;
}
}
}
void BeforePayment(object sender, EventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
DataRow[] drBonCad = billingEngine.LIGNES.Select("s_cle1='BONCAD'");
if (drBonCad.Length != 0)
{
int nLigne = 0;
foreach (DataRow drBC in billingEngine.LIGNES.Rows)
{
if (drBC["S_CLE1"].ToString() == "BONCAD")
{
billingEngine.LIGNES.Rows[nLigne]["id_boncad"] = billingEngine.PIEDS["id"].ToString() + billingEngine.LIGNES.Rows[nLigne]["dl_id"].ToString();
}
nLigne++;
}
}
}
void AfterSave(object sender, EventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
DataRow[] drBonCad = billingEngine.LIGNES.Select("s_cle1='BONCAD'");
if (drBonCad.Length != 0)
{
DataSet ds = new DataSet();
DataTable dt_lignes;
DataTable dt_pieds;
foreach (DataRow drBC in billingEngine.LIGNES.Rows)
{
if (drBC["S_CLE1"].ToString() == "BONCAD")
{
ds = new DataSet();
dt_lignes = new DataTable();
dt_lignes = billingEngine.LIGNES.Clone();
dt_lignes.ImportRow(drBC);
dt_pieds = new DataTable();
dt_pieds = billingEngine.PIEDS.Table.Clone();
dt_pieds.ImportRow(billingEngine.PIEDS);
ds.Tables.Add(dt_lignes);
ds.Tables.Add(dt_pieds);
ds.Tables.Add(Api.DataTableFromDico(MercatorUi.Globals.ParamIdentif, "ParamIdentif"));
List<MercatorUi.Reporting.OutputDescriptor> listOutputDescriptors = new List<MercatorUi.Reporting.OutputDescriptor>();
listOutputDescriptors.Add(new MercatorUi.Reporting.OutputDescriptorPrint());
MercatorUi.Reporting.ReportingStatic.Reporting.RunReport("BonCadeau", @"<MainDir\BonCadeau.repx", ds, listOutputDescriptors);
}
}
}
}
}
}