public class SapClass
{
/// <summary>
///
/// </summary>
/// <param name="fphm">發票號碼</param>
/// <param name="fprq">發票開票日期</param>
/// <param name="fkf">付款方</param>
/// <param name="fxqd">分銷渠道 </param>
public DataTable nco(string fphm, DateTime fprq, string fkf, char fxqd)
{
//MyBackendConfig config = new MyBackendConfig();
IDestinationConfiguration ID = new MyBackendConfig();
RfcDestinationManager.RegisterDestinationConfiguration(ID);
RfcDestination prd = RfcDestinationManager.GetDestination(PlatRunning.Global.AppSettings.GetValue("SapName"));//SAP系統標識
DataTable dt = nco(prd, fphm, fprq, fkf, fxqd);
RfcDestinationManager.UnregisterDestinationConfiguration(ID);
return dt;
}
/// <summary>
/// SAPTOJS
/// </summary>
/// <param name="prd"></param>
/// <param name="fphm"></param>
/// <param name="fprq"></param>
/// <param name="fkf"></param>
/// <param name="fxqd"></param>
private DataTable nco(RfcDestination prd, string fphm, DateTime fprq, string fkf, char fxqd)
{
RfcRepository repo = null;
try
{
repo = prd.Repository;
}
catch (Exception ex)
{
throw (ex);
}
IRfcFunction companyBapi = repo.CreateFunction("ZSD_SY_FUC_SAPTOJS"); //調用函數名
//設置Import的參數
companyBapi.SetValue("VBELN", fphm);
companyBapi.SetValue("ERDAT", fprq);
companyBapi.SetValue("KUNRG", fkf);
companyBapi.SetValue("ZBJ", fxqd);
companyBapi.Invoke(prd); //執行函數
IRfcTable table = companyBapi.GetTable("IT_JS_EXPORT"); //獲取相應的內表IT_JS_EXPORT
//提前實例化一個空的表結構處理
DataTable dt = CreateSapToJSTable();
for (int i = 0; i < table.Count; i++)
{
table.CurrentIndex = i;
DataRow dr = dt.NewRow();
dr["fxqd"] = table[i].GetString("VTWEG");
dr["fphm"] = table[i].GetString("VBELN");
dr["fphh"] = table[i].GetString("POSNR");
dr["fpcjsj"] = table[i].GetString("ERDAT");
dr["khdm"] = table[i].GetString("KUNRG");
dr["khmc"] = table[i].GetString("NAME");
dr["khdzch"] = table[i].GetString("STRAS");
dr["sapddh"] = table[i].GetString("AUBEL");
dr["khddh"] = table[i].GetString("BSTKD");
dr["sapwlh"] = table[i].GetString("MATNR");
dr["khwlh"] = table[i].GetString("KDMAT");
dr["spmc"] = table[i].GetString("ARKTX");
dr["kpm"] = table[i].GetString("TEXT");
dr["jldw"] = table[i].GetString("VRKME");
dr["sl"] = table[i].GetString("FKIMG");
dr["bb"] = table[i].GetString("WAERK");
dr["dj"] = table[i].GetString("KBETR");
dr["jgdw"] = table[i].GetString("KPEIN");
dr["zj"] = table[i].GetString("NETWR");
dr["shl"] = table[i].GetString("MWSK1");
dr["se"] = table[i].GetString("MWSBP");
dr["myxz"] = table[i].GetString("KTGRM");
dt.Rows.Add(dr);
}
return dt;
}
/// <summary>
///
/// </summary>
/// <param name="sapfph">sap發票號</param>
/// <param name="qtfph">生成發票號</param>
public DataTable nco(string sapfph, string qtfph)
{
//MyBackendConfig config = new MyBackendConfig();
IDestinationConfiguration ID = new MyBackendConfig();
RfcDestinationManager.RegisterDestinationConfiguration(ID);
RfcDestination prd = RfcDestinationManager.GetDestination(PlatRunning.Global.AppSettings.GetValue("SapName"));//SAP系統標識
DataTable dt = nco(prd, sapfph, qtfph);
RfcDestinationManager.UnregisterDestinationConfiguration(ID);
return dt;
}
/// <summary>
/// JSTOSAP
/// </summary>
/// <param name="prd"></param>
/// <param name="sapfph"></param>
/// <param name="qtfph"></param>
private DataTable nco(RfcDestination prd, string sapfph, string qtfph)
{
DataTable dt = WriteTableToSAP(prd, sapfph, qtfph);
return dt;
}
#region 寫回數據到SAP
private DataTable WriteTableToSAP(RfcDestination rfcDest, string sapfph, string qtfph)
{
RfcRepository repo = rfcDest.Repository;
IRfcFunction funBapi = repo.CreateFunction("ZSD_SY_FUC_JSTOSAP");
//調用函數名
IRfcTable tblROF = funBapi.GetTable("IT_JSTOSAP");
tblROF.Clear();
tblROF.Insert();
tblROF.CurrentRow.SetValue("VBELN", sapfph);
tblROF.CurrentRow.SetValue("JSVBELN", qtfph);
funBapi.SetValue("IT_JSTOSAP", tblROF);
funBapi.Invoke(rfcDest);
//引用回傳結果
IRfcTable ReturnTable = funBapi.GetTable("IT_RETURN");
DataTable dtReturn = ReturnMessage();
for (int i = 0; i < ReturnTable.Count; i++)
{
ReturnTable.CurrentIndex = i;
DataRow dr = dtReturn.NewRow();
dr["isSuccess"] = ReturnTable[i].GetString("TYPE");
dr["ReturnMessage"] = ReturnTable[i].GetString("MESSAGE");
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
#endregion
#region 接口參數
public class MyBackendConfig : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(String destinationName)
{
if ("DEV".Equals(destinationName))
{
string strtemp = string.Empty;
RfcConfigParameters parms = new RfcConfigParameters();
parms.Add(RfcConfigParameters.AppServerHost, "192.168.0.122");//SAP主機IP
parms.Add(RfcConfigParameters.SystemNumber, "00");//SAP實例
parms.Add(RfcConfigParameters.User, "SYRFC");//用戶名
parms.Add(RfcConfigParameters.Password, "123456"); //密碼
parms.Add(RfcConfigParameters.Client, "710");// Client
parms.Add(RfcConfigParameters.Language, "ZH"); //登陸語言
parms.Add(RfcConfigParameters.PoolSize, "5");
parms.Add(RfcConfigParameters.MaxPoolSize, "10");
parms.Add(RfcConfigParameters.IdleTimeout, "60");
return parms;
}
else
return null;
}
public bool ChangeEventsSupported()
{
return false;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
}
#endregion
public DataTable CreateSapToJSTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("fxqd"); //分銷渠道
dt.Columns.Add("fphm");//發票號碼
dt.Columns.Add("fphh");//發票行號
dt.Columns.Add("fpcjsj");//發票創建時間
dt.Columns.Add("khdm");//客戶代碼
dt.Columns.Add("khmc");//客戶名稱
dt.Columns.Add("khdzch");//客戶地址(中文)
dt.Columns.Add("sapddh");//sap訂單號
dt.Columns.Add("khddh");//客戶訂單號
dt.Columns.Add("sapwlh");//sap物料號
dt.Columns.Add("khwlh");//客戶物料號
dt.Columns.Add("spmc");//商品名稱
dt.Columns.Add("kpm");//開票名
dt.Columns.Add("jldw");//計量單位
dt.Columns.Add("sl");//數量
dt.Columns.Add("bb");//幣別
dt.Columns.Add("dj");//含稅單價
dt.Columns.Add("jgdw");//價格單位
dt.Columns.Add("zj");//總價
dt.Columns.Add("shl");//稅率
dt.Columns.Add("se");//稅額
dt.Columns.Add("myxz");//貿易性質
return dt;
}
public DataTable ReturnMessage()
{
DataTable dt = new DataTable();
dt.Columns.Add("isSuccess"); //成功與否
dt.Columns.Add("ReturnMessage");//Message
return dt;
}
}