private void BillingEngine_BeforeSave(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeSaveEventArgs e)
{
MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
DataTable dtArticles = new DataTable();
dtArticles.Columns.Add("s_id", typeof(string));
foreach(DataRow dr in billingEngine.LIGNES.Select("id_article<>''"))
dtArticles.Rows.Add(new object[1] { dr["id_article"] });
using (MercatorSqlConnection conn = new MercatorSqlConnection(MercatorUi.Globals.RepData, true))
{
if (!conn.IsConnected)
{
e.CancelSave = true; // connectie geweigerd door de SQL-server -> we stoppen het bewaren
return;
}
if (!Api.BulkDataTable(dtArticles, "#art_tmp", conn.Connection, null, "STOCK"))
{
e.CancelSave = true; //error tijdens de bulk van de DataTable -> we stoppen het bewaren
return;
}
using (MercatorSqlCommand cmd = new MercatorSqlCommand("", conn))
{
cmd.CommandText = "select s_id,s_xxx,s_yyy from STOCK inner join #art_tmp on (stock.s_id=#art_tmp.s_id) \r\n"
+ "drop table #art_tmp";
DataSet ds = Api.Zselect(cmd);
if (ds == null)
{
e.CancelSave = true; // de query hierboven geeft een fout -> we stoppen het bewaren
return;
}
Api.TrimEndDataTable(ds.Tables[0]);
foreach (DataRow dr in billingEngine.LIGNES.Select("id_article<>''"))
{
DataRow[] foundRows = ds.Tables[0].Select(string.Format("s_id='{0}'", Api.UnquoteSql(dr["id_article")))):
if (foundRows.Length > 0) // zal altijd het geval zijn, wanneer alle artikelen bestaan
{
dr["xxx"] = foundRows[0]["s_xxx"];
dr["yyy"] = foundRows[0]["s_yyy"];
}
}
}
}
}