using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MercatorApi;
using MercatorUi;
using MercatorExtensions;
namespace Billing
{
public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
{
public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
{
BillingEngine.AfterInsertItem += AfterInsertItem;
BillingEngine.BeforePayment += BeforePayment;
BillingEngine.AfterSave += AfterSave;
}
public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
{
BillingEngine.AfterInsertItem -= AfterInsertItem;
BillingEngine.BeforePayment -= BeforePayment;
BillingEngine.AfterSave -= AfterSave;
}
void AfterInsertItem(object sender, MercatorUi.Engine.Gescom.BillingEngine.AfterInsertItemEventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
if (e.LignesVRecord.S_CLE1 == "UTILBONVAL")
{
string id_bonval = Dialogs.AskString("Veuillez scanner le code barre", "", true, 35).Trim();
if (id_bonval != "")
{
int nbBon = billingEngine.LignesVRecords.Where(l => l.ID_BONVAL == id_bonval).Count();
if (nbBon == 0)
{
string reqSql = "select count(*) as nbUtil, sum(q) as q, sum(pu) as pu from lignes_v where id_bonval=@id_bonval";
DataSet ds = Api.Zselect(MercatorUi.Globals.RepData, reqSql, new MercatorSqlParam("@id_bonval", id_bonval, SqlDbType.Char));
if (ds != null)
{
if (Convert.ToInt16(ds.Tables[0].Rows[0]["nbUtil"]) == 0)
{
Dialogs.Stop("Ce bon à valoir n'existe pas !");
e.CancelInsertItem = true;
}
else if (Convert.ToInt16(ds.Tables[0].Rows[0]["nbUtil"]) > 1)
{
Dialogs.Stop("Ce bon à valoir a déjà été utilisé !");
e.CancelInsertItem = true;
}
else
{
e.LignesVRecord.Q = Convert.ToDouble(ds.Tables[0].Rows[0]["q"]) * -1;
e.LignesVRecord.PU = Convert.ToDouble(ds.Tables[0].Rows[0]["pu"]);
e.LignesVRecord.ID_BONVAL = id_bonval;
}
}
else
{
e.CancelInsertItem = true;
}
}
else
{
Dialogs.Stop("Ce bon à valoir 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;
if (billingEngine.PiedsVRecord.TOT_TTC_FB.CompareTo(0, Globals.DEC_DEV_B) < 0)
{
MercatorUi.Sig.Sig sigStock = MercatorUi.Sig._SigsStatic.SigByModule(MercatorUi.Sig._SigEnum.STOCK);
string reqSql = string.Format("select {0} from stock (NOLOCK) where s_cle1 ='BONVAL'", sigStock.FieldsListForSelectRtrimNoBytes);
DataSet dsStock = Api.Zselect(Globals.RepData, reqSql);
if ((dsStock == null) || (dsStock.Tables[0].Rows.Count == 0))
{
Dialogs.Stop("Article non trouvé !");
}
else
{
DataRow drStock = dsStock.Tables[0].Rows[0];
int nLigne = billingEngine.AppendLine();
if (nLigne < 0)
{
Dialogs.Stop("AppendLine BillingEngine : " + billingEngine.LastError + (Api.LastError == "" ? " - " + Api.LastError : ""));
}
else if (!billingEngine.InsertItem(drStock, billingEngine.LIGNES.Rows[nLigne]))
{
Dialogs.Stop("InsertItem BillingEngine : " + billingEngine.LastError + (Api.LastError == "" ? " - " + Api.LastError : ""));
}
else
{
billingEngine.LignesVRecords[nLigne].PU = Math.Abs(billingEngine.PiedsVRecord.TOT_TTC_FB);
billingEngine.LignesVRecords[nLigne].ID_BONVAL = billingEngine.PiedsVRecord.ID + billingEngine.LignesVRecords[nLigne].DL_ID;
billingEngine.UpdateAmounts();
}
}
}
}
void AfterSave(object sender, EventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
if (billingEngine.LignesVRecords.Where(l => l.S_CLE1 == "BONVAL").Count() != 0)
{
DataSet ds;
foreach (DataRow drBC in billingEngine.LIGNES.Select("S_CLE1='BONVAL'"))
{
ds = new DataSet();
DataTable dt_lignes = billingEngine.LIGNES.Clone();
dt_lignes.ImportRow(drBC);
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"));
/* Edit Layout
//string reportFileName = Globals.MainDir + "BonValoir.repx";
string reportFileName = @"<MainDir\BonValoir.repx";
string reportLayout = MercatorUi.Reporting.ReportingStatic.Reporting.EditLayout(Api.JustStem(reportFileName), Api.FileToStr(reportFileName, Encoding.UTF8), ds);
Api.StrToFile(reportLayout, reportFileName, Encoding.UTF8);
*/
List<MercatorUi.Reporting.OutputDescriptor> listOutputDescriptors = new List<MercatorUi.Reporting.OutputDescriptor>();
// Preview
//listOutputDescriptors.Add(new MercatorUi.Reporting.OutputDescriptorPreview ());
//
listOutputDescriptors.Add(new MercatorUi.Reporting.OutputDescriptorPrint());
//MercatorUi.Reporting.ReportingStatic.Reporting.RunReport("BonValoir", Globals.MainDir + "BonValoir.repx", ds, listOutputDescriptors);
MercatorUi.Reporting.ReportingStatic.Reporting.RunReport("BonValoir", @"<MainDir\BonValoir.repx", ds, listOutputDescriptors);
}
}
}
}
}