using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Configuration;
using SAP.Middleware.Connector;
///
/// SapApi xuwei add 2020-07-15
///
public class SapApi
{
//SAP服务器配置参数
public static string appServerHost = ConfigurationManager.AppSettings["SapAppServerHost"].ToString();
public static string systemNumber = ConfigurationManager.AppSettings["SapSystemNumber"].ToString();
public static string user = ConfigurationManager.AppSettings["SapUser"].ToString();
public static string password = ConfigurationManager.AppSettings["SapPassword"].ToString();
public static string client = ConfigurationManager.AppSettings["SapClient"].ToString();
///
/// 读取库存单价接口(获取库存数量)
///
/// 工厂代号
/// 物料编号
/// 库存地点
/// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断
/// 消息文本
///
public static DataTable ZMMFM037(string WERKS, List matnrs, string LGORT, out string ZTYPE, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM037");
//输入参数
IRfcTable imTable = rfcApi.GetTable("TAB_IN");
foreach (string MATNR in matnrs)
{
imTable.Append();
imTable.SetValue("WERKS", WERKS);
imTable.SetValue("MATNR", MATNR);
imTable.SetValue("LGORT", LGORT);
}
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZTYPE = rfcApi.GetValue("ZTYPE").ToString();
ZMSG = rfcApi.GetValue("ZMSG").ToString();
IRfcTable table = rfcApi.GetTable("TAB_OUT");
DataTable dt = GetDataTableFromRFCTable(table);
//string[] columns = new string[] { "WERKS", "MATNR", "MEINS", "VBELN", "POSNR", "CHARG", "LGORT", "MENGE", "VERPR", "WAERS" };
//DataTable dt = new DataTable();
//foreach (string clmn in columns)
//{
// dt.Columns.Add(clmn);
//}
//for (int i = 0; i < table.RowCount; i++)
//{
// DataRow dr = dt.NewRow();
// foreach (string clmn in columns)
// {
// dr[clmn] = table.GetString(clmn);
// }
// dt.Rows.Add(dr);
//}
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// 读取库存单价接口(获取库存数量)
///
/// 工厂代号
/// 物料编号
/// 库存地点
/// 消息文本
///
public static DataTable ZMM_WMS016(string WERKS, List matnrs, string LGORT, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMM_WMS016");
//输入参数
IRfcTable imTable = rfcApi.GetTable("IT_INPUT");
foreach (string MATNR in matnrs)
{
imTable.Append();
imTable.SetValue("WERKS", WERKS);
imTable.SetValue("MATNR", MATNR);
imTable.SetValue("LGORT", LGORT);
}
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZMSG = rfcApi.GetValue("E_MSG").ToString();
IRfcTable table = rfcApi.GetTable("IT_OUTPUT");
DataTable dt = GetDataTableFromRFCTable(table);
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// 读取库存单价接口(获取库存数量)
///
/// 工厂代号
/// 库存地点
/// 消息文本
///
public static DataTable ZMM_WMS016(string WERKS, DataTable dtInventory, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMM_WMS016");
//输入参数
IRfcTable imTable = rfcApi.GetTable("IT_INPUT");
foreach (DataRow row in dtInventory.Rows)
{
imTable.Append();
imTable.SetValue("WERKS", WERKS);
imTable.SetValue("MATNR", row["IDNRK"]);
imTable.SetValue("LGORT", row["POSITION"]);
}
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZMSG = rfcApi.GetValue("E_MSG").ToString();
IRfcTable table = rfcApi.GetTable("IT_OUTPUT");
DataTable dt = GetDataTableFromRFCTable(table);
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// 查询PO的相关信息(获取订单达成数量)
///
/// 采购凭证号
/// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断
/// 消息文本
///
public static DataTable ZMMFM_MES_POCX(string IN_EBELN, out string ZTYPE, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM_MES_POCX");
//输入参数
rfcApi.SetValue("IN_EKORG", 1000);
rfcApi.SetValue("IN_EBELN", IN_EBELN);
rfcApi.SetValue("IN_AEDAT_FROM", "20220101");
rfcApi.SetValue("IN_AEDAT_TO", "20301231");
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZTYPE = rfcApi.GetValue("ZTYPE").ToString();
ZMSG = rfcApi.GetValue("ZMSG").ToString();
IRfcTable table = rfcApi.GetTable("GT_OUT");
//EBELN 采购凭证编号
//EBELP 采购凭证的项目编号
//MENGE 采购订单数量
//MEINS 订单计量单位
//ZYSSL 已收/ 退数量数量
//ZWSSL 未收/ 退数量
//ELIKZ 交货已完成标识
//LIFNR 供应商编码
//EKGRP 采购组
//MATNR 物料编码
//string[] columns = new string[] { "EBELN", "EBELP", "MENGE", "MEINS", "ZYSSL", "ZWSSL", "ELIKZ", "LIFNR", "EKGRP", "MATNR" };
DataTable dt = GetDataTableFromRFCTable(table);
//foreach (string clmn in columns)
//{
// dt.Columns.Add(clmn);
//}
//for (int i = 0; i < table.RowCount; i++)
//{
// DataRow dr = dt.NewRow();
// foreach (string clmn in columns)
// {
// dr[clmn] = table.ro.GetString(clmn);
// }
// dt.Rows.Add(dr);
//}
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// 物料主数据接口(同步最小包装数用)
///
/// 物料凭证编号
/// 创建时间
/// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断
/// 消息文本
///
public static DataTable ZMMFM054(out string ZTYPE, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM054");
//输入参数
rfcApi.SetValue("IN_ALL", 'Y');
rfcApi.SetValue("IN_WERKS", 5020);
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZTYPE = rfcApi.GetValue("ZTYPE").ToString();
ZMSG = rfcApi.GetValue("ZMSG").ToString();
IRfcTable table = rfcApi.GetTable("OUT_TABLE");
DataTable dt = GetDataTableFromRFCTable(table);
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// 东科与SAP系统之线边仓库存领用接口(库存过账)
///
/// 过帐日期
/// 东科单据号
/// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断
/// 消息文本
///
public static DataTable ZMMFM045(string BLDAT, string ZDKNO, DataTable dtTable, out string ZTYPE, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM045");
//BLDAT DATS 8 0 必填 凭证日期 格式: yyyymmdd
//BUDAT DATS 8 0 必填 过帐日期 格式: yyyymmdd
//WERKS CHAR 4 0 必填 工厂 5020
//LGORT_FM CHAR 4 0 必填 发出库位
//LGORT_TO CHAR 4 0 必填 接收库位
//MATNR CHAR 40 0 必填 物料编号
//BWART CHAR 3 0 必填 移动类型(库存管理) 目前只允许填:311
//MEINS UNIT 3 0 必填 基本计量单位
//MENGE QUAN 13 3 必填 数量
//ZDKNO CHAR 40 0 必填 东科单据号
//输入参数
IRfcTable imTable = rfcApi.GetTable("IT_ITEM");
foreach (DataRow dr in dtTable.Rows)
{
imTable.Append();
imTable.SetValue("BLDAT", BLDAT);
imTable.SetValue("BUDAT", BLDAT);
imTable.SetValue("WERKS", "5000");
imTable.SetValue("LGORT_FM", dr["LGORT_FM"]);
imTable.SetValue("LGORT_TO", dr["TOLOGT"]);
imTable.SetValue("MATNR", dr["MATNR"]);
imTable.SetValue("CHARG", dr["CHARG"] + "");
imTable.SetValue("BWART", "311");
imTable.SetValue("MEINS", dr["MEINS"]);
imTable.SetValue("MENGE", dr["MENGE"]);
imTable.SetValue("ZDKNO", ZDKNO);
}
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZTYPE = rfcApi.GetValue("ZTYPE").ToString();
ZMSG = rfcApi.GetValue("ZMSG").ToString();
IRfcTable table = rfcApi.GetTable("ET_RETURN");
DataTable dt = GetDataTableFromRFCTable(table);
//string[] columns = new string[] { "BLDAT", "WERKS", "MBLNR", "MJAHR", "MSGTY", "MSGTX" };
//DataTable dt = new DataTable();
//foreach (string clmn in columns)
//{
// dt.Columns.Add(clmn);
//}
//for (int i = 0; i < table.RowCount; i++)
//{
// DataRow dr = dt.NewRow();
// foreach (string clmn in columns)
// {
// dr[clmn] = table.GetString(clmn);
// }
// dt.Rows.Add(dr);
//}
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// 冲销凭证接口(撤销过账)
///
/// 物料凭证编号
/// 创建时间
/// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断
/// 消息文本
///
public static void ZMMFM038(string MBLNR, DateTime createTime, out string ZTYPE, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM038");
//输入参数
rfcApi.SetValue("MBLNR", MBLNR);
rfcApi.SetValue("MJAHR", createTime.ToString("yyyy"));
rfcApi.SetValue("BUDAT", createTime.ToString("yyyyMMdd"));
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZTYPE = rfcApi.GetValue("ZTYPE").ToString();
ZMSG = rfcApi.GetValue("ZMSG").ToString();
rfcDest = null;
rfcRep = null;
}
///
/// 测试用,用于把线边仓的货,全挪走
///
/// 过帐日期
/// 东科单据号
/// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断
/// 消息文本
///
public static DataTable ZMMFM045_TEST(string BLDAT, string ZDKNO, DataTable dtTable, out string ZTYPE, out string ZMSG)
{
RfcConfigParameters rfcPara = new RfcConfigParameters();
rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost);
rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber);
rfcPara.Add(RfcConfigParameters.User, user);
rfcPara.Add(RfcConfigParameters.Password, password);
rfcPara.Add(RfcConfigParameters.Client, client);
rfcPara.Add(RfcConfigParameters.Name, "CON");
rfcPara.Add(RfcConfigParameters.Language, "ZH");
rfcPara.Add(RfcConfigParameters.PoolSize, "5");
rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60");
RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara);
RfcRepository rfcRep = rfcDest.Repository;
//接口API
IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM045");
//BLDAT DATS 8 0 必填 凭证日期 格式: yyyymmdd
//BUDAT DATS 8 0 必填 过帐日期 格式: yyyymmdd
//WERKS CHAR 4 0 必填 工厂 5020
//LGORT_FM CHAR 4 0 必填 发出库位
//LGORT_TO CHAR 4 0 必填 接收库位
//MATNR CHAR 40 0 必填 物料编号
//BWART CHAR 3 0 必填 移动类型(库存管理) 目前只允许填:311
//MEINS UNIT 3 0 必填 基本计量单位
//MENGE QUAN 13 3 必填 数量
//ZDKNO CHAR 40 0 必填 东科单据号
//输入参数
IRfcTable imTable = rfcApi.GetTable("IT_ITEM");
foreach (DataRow dr in dtTable.Rows)
{
imTable.Append();
imTable.SetValue("BLDAT", BLDAT);
imTable.SetValue("BUDAT", BLDAT);
imTable.SetValue("WERKS", "5020");
imTable.SetValue("LGORT_FM", "2205");
imTable.SetValue("LGORT_TO", "1103");
imTable.SetValue("MATNR", dr["MATNR"]);
imTable.SetValue("CHARG", dr["CHARG"] + "");
imTable.SetValue("BWART", "311");
imTable.SetValue("MEINS", dr["MEINS"]);
imTable.SetValue("MENGE", dr["MENGE"]);
imTable.SetValue("ZDKNO", ZDKNO);
}
//调用接口
rfcApi.Invoke(rfcDest);
//获取输出
ZTYPE = rfcApi.GetValue("ZTYPE").ToString();
ZMSG = rfcApi.GetValue("ZMSG").ToString();
IRfcTable table = rfcApi.GetTable("ET_RETURN");
DataTable dt = GetDataTableFromRFCTable(table);
rfcDest = null;
rfcRep = null;
return dt;
}
///
/// IRfcTable转DataTable
///
///
///
private static DataTable GetDataTableFromRFCTable(IRfcTable myrfcTable)
{
DataTable loTable = new DataTable();
int liElement = 0;
for (liElement = 0; liElement <= myrfcTable.ElementCount - 1; liElement++)
{
RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement);
loTable.Columns.Add(metadata.Name);
}
foreach (IRfcStructure Row in myrfcTable)
{
DataRow ldr = loTable.NewRow();
for (liElement = 0; liElement <= myrfcTable.ElementCount - 1; liElement++)
{
RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement);
ldr[metadata.Name] = Row.GetString(metadata.Name);
}
loTable.Rows.Add(ldr);
}
return loTable;
}
}