| using iWareSAP.IDOC.Entity; | 
| using iWareSAP.Properties; | 
| using SAP.Middleware.Connector; | 
| using System; | 
| using System.Linq; | 
| using System.Collections.Generic; | 
| using UtilDll.UTIL; | 
|   | 
| namespace iWareSAP.CLIENT.Entity | 
| { | 
|     public class SapncoClient | 
|     { | 
|         private RfcDestination _rfcDestination = null; | 
|         private string _logDir; | 
|   | 
|         public SapncoClient() | 
|         { | 
|             _logDir = Resources.LogDir + @"\ATOIS"; | 
|   | 
|         } | 
|   | 
|         /// <summary> | 
|         /// Register Client | 
|         /// </summary> | 
|         public void RegisterRfcDestination() | 
|         { | 
|             try | 
|             { | 
|   | 
|                 if (_rfcDestination == null) | 
|                 { | 
|                     _rfcDestination = RfcDestinationManager.GetDestination("NCO_Destination"); | 
|                 } | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "RegisterRfcDestination", ex.Message); | 
|             } | 
|         } | 
|   | 
|   | 
|         /// <summary> | 
|         /// 调用时间 | 
|         /// </summary> | 
|         /// <param name="dateStr"></param> | 
|         /// <returns></returns> | 
|         public string InvokeRFCFunctionGetDate(string dateStr) | 
|         { | 
|             string date = string.Empty; | 
|             try | 
|             { | 
|                 IRfcFunction function = this._rfcDestination.Repository.CreateFunction("DATE_GET_WEEK"); | 
|                 function.SetParameterActive(0, true); | 
|                 function.SetValue("DATE", dateStr); | 
|                 function.Invoke(this._rfcDestination); | 
|                 date = function.GetValue("DATE").ToString(); | 
|             } | 
|             catch (RfcBaseException ex) | 
|             { | 
|                 throw ex; | 
|             } | 
|             return date; | 
|         } | 
|   | 
|                  | 
|         /// <summary>         | 
|         /// 调用Sap函数BAPI_PO_GETDETAIL1 | 
|         /// (非IDOC) | 
|         /// </summary> | 
|         /// <param name="orderCode"></param> | 
|         /// <returns></returns>         | 
|         public PurchaseOrder InvokeRFCFunctionGetPoDetail(string poNumber, out string msg) | 
|         { | 
|             try | 
|             { | 
|                 msg = ""; | 
|                 LogTextHelper.WriteLine(_logDir, "调用方法{0},poNumber={1}", "InvokeRFCFunctionGetPoDetail", poNumber); | 
|                 var function = _rfcDestination.Repository.CreateFunction("BAPI_PO_GETDETAIL"); | 
|   | 
|               | 
|   | 
|                 function.SetParameterActive(0, true); | 
|                 function.SetValue("PURCHASEORDER", poNumber); | 
|                 function.SetValue("ACCOUNT_ASSIGNMENT", "X"); | 
|   | 
|                 function.Invoke(_rfcDestination); | 
|                 var header = function.GetStructure("PO_HEADER"); | 
|                 var purchaseOrder = new PurchaseOrder(header); | 
|                 if (string.IsNullOrEmpty(purchaseOrder.PO_NUMBER)) | 
|                 { | 
|                     msg = string.Format("未找到采购单{0}", poNumber); | 
|                     LogTextHelper.WriteLine(_logDir, "msg={0}", msg); | 
|                     return null; | 
|                 } | 
|                 var details = function.GetTable("PO_ITEMS"); | 
|                | 
|                 foreach (IRfcStructure detail in details) | 
|                 { | 
|                     purchaseOrder.Materials.Add(new PurchaseOrderMaterial(detail)); | 
|                 } | 
|   | 
|                 var assignments = function.GetTable("PO_ITEM_ACCOUNT_ASSIGNMENT"); | 
|                 | 
|                 var flag = false; | 
|                 foreach (IRfcStructure assignment in assignments) | 
|                 { | 
|                     if (!flag) | 
|                     { | 
|                         purchaseOrder.SD_DOC = assignment.GetValue("SD_DOC") == null ? "" : assignment.GetValue("SD_DOC").ToString(); | 
|                         purchaseOrder.SDOC_ITEM = assignment.GetValue("SDOC_ITEM") == null ? "" : assignment.GetValue("SDOC_ITEM").ToString(); | 
|                         flag = true; | 
|                     } | 
|   | 
|                     var poItem = assignment.GetValue("PO_ITEM") == null ? "" : assignment.GetValue("PO_ITEM").ToString(); | 
|                     var m = purchaseOrder.Materials.FirstOrDefault(x => x.PO_ITEM == poItem); | 
|                     if (m != null) | 
|                     { | 
|                         m.SD_DOC = assignment.GetValue("SD_DOC") == null ? "" : assignment.GetValue("SD_DOC").ToString(); | 
|                         m.SDOC_ITEM = assignment.GetValue("SDOC_ITEM") == null ? "" : assignment.GetValue("SDOC_ITEM").ToString(); | 
|                     } | 
|                 } | 
|                 LogTextHelper.WriteLine(_logDir, "purchaseOrder={0}", purchaseOrder); | 
|                 return purchaseOrder; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 msg = ex.Message; | 
|                 LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "InvokeRFCFunctionGetPoDetail", ex.Message); | 
|                 return null; | 
|             } | 
|         } | 
|   | 
|                  | 
|         /// <summary> | 
|         /// 发送收货确认 | 
|         /// </summary>         | 
|         /// <returns></returns> | 
|         public string InvokeRFCFunctionPoGR(List<GRMaterial> grMaterials, out string msg) | 
|         { | 
|             try | 
|             { | 
|                 msg = ""; | 
|   | 
|                  | 
|                 if (grMaterials.Count <= 0) | 
|                 { | 
|                     msg = "确认物料数量不能为0"; | 
|                     return ""; | 
|                 } | 
|                 var function = _rfcDestination.Repository.CreateFunction("/SIE/CHN_MM_WMS_PO_GR"); | 
|                | 
|                 function.SetParameterActive(0, true); | 
|                 function.SetParameterActive(1, true); | 
|                 function.SetValue("I_BUDAT", DateTime.Now.ToString("yyyyMMdd")); | 
|                 var items = function.GetTable("LT_GOODSMVT_ITEM"); | 
|                 | 
|                 grMaterials.ForEach(x => | 
|                 { | 
|                     items.Append(); | 
|                     items.CurrentRow.SetValue("EBELN", x.EBELN);//采购单号 | 
|                     items.CurrentRow.SetValue("EBELP", x.EBELP);//行号 | 
|                     items.CurrentRow.SetValue("MATNR", x.MATNR);//物料号 | 
|                     items.CurrentRow.SetValue("BWART", x.BWART);//移动类型 | 
|                     items.CurrentRow.SetValue("WERKS", x.WERKS);//工厂 | 
|                     items.CurrentRow.SetValue("LGORT", x.LGORT);//存储位置 | 
|                     items.CurrentRow.SetValue("MENGE", x.MENGE);//数量 | 
|                 }); | 
|   | 
|                 var grStr = ""; | 
|                 grMaterials.ForEach(x => grStr += x.ToString() + "\r\n"); | 
|                 LogTextHelper.WriteLine(_logDir, "调用方法{0},grMaterials={1}", "InvokeRFCFunctionPoGR", grStr); | 
|                 function.Invoke(_rfcDestination); | 
|                 var materialDoc = function.GetValue("MATERIALDOCUMENT").ToString(); | 
|   | 
|                 var etReturns = function.GetTable("ET_RETURN"); | 
|                 foreach (IRfcStructure etReturn in etReturns) | 
|                 { | 
|                     var message = etReturn.GetValue("MESSAGE") == null ? "" : etReturn.GetValue("MESSAGE").ToString(); | 
|                     msg = message == "Success" || message == "Successfully" ? "" : message; | 
|                     break; | 
|                 } | 
|   | 
|                 if (string.IsNullOrEmpty(materialDoc)) | 
|                 { | 
|                     LogTextHelper.WriteLine(_logDir, "msg={0}", msg); | 
|                 } | 
|                 else | 
|                 { | 
|                     LogTextHelper.WriteLine(_logDir, "materialDoc={0}", materialDoc); | 
|                 } | 
|                 return materialDoc; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 msg = ex.Message; | 
|                 LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "InvokeRFCFunctionPoGR", ex.Message); | 
|                 return ""; | 
|             } | 
|         } | 
|          | 
|         /// <summary> | 
|         /// 发送出库确认 | 
|         /// </summary>         | 
|         /// <returns></returns> | 
|         public string InvokeRFCFunctionWmsGI(List<GIMaterial> giMaterials, out string msg) | 
|         { | 
|             try | 
|             { | 
|                 msg = ""; | 
|                  | 
|                 var function = _rfcDestination.Repository.CreateFunction("/SIE/CHN_MM_WMS_GI"); | 
|                | 
|                 function.SetParameterActive(0, true); | 
|                 function.SetParameterActive(1, true); | 
|                 var items = function.GetTable("T_MSEG"); | 
|                 giMaterials.ForEach(x => | 
|                 { | 
|                     if (x.BWART.EndsWith("E"))  | 
|                     { | 
|                         x.SOBKZ = "E"; | 
|                         x.BWART = x.BWART.Substring(0, x.BWART.Length - 1); | 
|                     } | 
|                     else if (x.BWART.EndsWith("K"))  | 
|                     { | 
|                         x.SOBKZ = "K"; | 
|                         x.BWART = x.BWART.Substring(0, x.BWART.Length - 1); | 
|   | 
|                     } | 
|                     else  | 
|                     { | 
|                         x.SOBKZ = ""; | 
|                     } | 
|   | 
|                     items.Append(); | 
|                     items.CurrentRow.SetValue("BWART", x.BWART);//移动类型 | 
|                     items.CurrentRow.SetValue("MATNR", x.MATNR);//物料号 | 
|                     items.CurrentRow.SetValue("WERKS", x.WERKS);//工厂 | 
|                     items.CurrentRow.SetValue("LGORT", x.LGORT);//存储位置 | 
|                     items.CurrentRow.SetValue("SOBKZ", x.SOBKZ);//特殊类型 | 
|                     items.CurrentRow.SetValue("LIFNR", x.LIFNR);//供应商 | 
|                     items.CurrentRow.SetValue("KDAUF", x.KDAUF);//销售订单 | 
|                     items.CurrentRow.SetValue("KDPOS", x.KDPOS);//销售订单项 | 
|                     items.CurrentRow.SetValue("ERFMG", x.ERFMG);//数量 | 
|                     items.CurrentRow.SetValue("ERFME", x.ERFME);//单位 | 
|                     items.CurrentRow.SetValue("CHARG", x.CHARG);//工作台 | 
|                     items.CurrentRow.SetValue("SERNR", x.SERNR);//序列号 | 
|                     items.CurrentRow.SetValue("AUFNR", x.AUFNR);//生产订单 | 
|                     items.CurrentRow.SetValue("KZEAR", x.KZEAR);//最终标识 | 
|                     items.CurrentRow.SetValue("RSNUM", x.RSNUM);//保留数 | 
|                     items.CurrentRow.SetValue("RSPOS", x.RSPOS);//保留项 | 
|                     items.CurrentRow.SetValue("BWTAR", x.BWTAR);//验证类型 | 
|                 }); | 
|   | 
|                 var giStr = ""; | 
|                 giMaterials.ForEach(x => giStr += x.ToString() + "\r\n"); | 
|                 LogTextHelper.WriteLine(_logDir, "调用方法{0},giMaterials={1}", "InvokeRFCFunctionWmsGI", giStr); | 
|                 function.Invoke(_rfcDestination); | 
|   | 
|                 var materialDoc = function.GetValue("MBLNR").ToString(); | 
|                 var res = function.GetValue("MJAHR").ToString(); | 
|   | 
|                 var etReturns = function.GetTable("RETURN"); | 
|   | 
|                 foreach (IRfcStructure etReturn in etReturns) | 
|                 { | 
|                     var message = etReturn.GetValue("MESSAGE") == null ? "" : etReturn.GetValue("MESSAGE").ToString(); | 
|                     msg = message == "Success" || message == "Successfully" ? "" : message; | 
|                     break; | 
|                 } | 
|   | 
|                 if (string.IsNullOrEmpty(materialDoc)) | 
|                 { | 
|                     | 
|                     LogTextHelper.WriteLine(_logDir, "msg={0}", msg); | 
|                 } | 
|                 else | 
|                 { | 
|                     LogTextHelper.WriteLine(_logDir, "materialDoc={0}, res={1}", materialDoc, res); | 
|                 } | 
|   | 
|                 return materialDoc; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 msg = ex.Message; | 
|                 LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "InvokeRFCFunctionWmsGI", ex.Message); | 
|                 return ""; | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 发送出库确认 | 
|         /// </summary>         | 
|         /// <returns></returns> | 
|         public string InvokeRFCFunctionWmsStkMov(List<MovMaterial> movMaterials, out string msg) | 
|         { | 
|             try | 
|             { | 
|                 msg = ""; | 
|   | 
|                 var function = _rfcDestination.Repository.CreateFunction("/SIE/CHN_MM_WMS_STKMOV"); | 
|                  | 
|                 function.SetParameterActive(0, true); | 
|                 function.SetParameterActive(1, true); | 
|                 var items = function.GetTable("T_MSEG"); | 
|   | 
|                 movMaterials.ForEach(x => | 
|                 { | 
|                     if (x.BWART.EndsWith("E")) | 
|                     { | 
|                         x.SOBKZ = "E"; | 
|                         x.BWART = x.BWART.Substring(0, x.BWART.Length - 1); | 
|                     } | 
|                     else if (x.BWART.EndsWith("K")) | 
|                     { | 
|                         x.SOBKZ = "K"; | 
|                         x.BWART = x.BWART.Substring(0, x.BWART.Length - 1); | 
|                     } | 
|                     else | 
|                     { | 
|                         x.SOBKZ = ""; | 
|                     } | 
|   | 
|                     items.Append(); | 
|                     items.CurrentRow.SetValue("BWART", x.BWART);//移动类型 | 
|                     items.CurrentRow.SetValue("MATNR", x.MATNR);//物料号 | 
|                     items.CurrentRow.SetValue("WERKS", x.WERKS);//工厂 | 
|                     items.CurrentRow.SetValue("LGORT", x.LGORT);//转出库位 | 
|                     items.CurrentRow.SetValue("SOBKZ", x.SOBKZ);//特殊类型 | 
|                     items.CurrentRow.SetValue("LIFNR", x.LIFNR);//供应商 | 
|                     items.CurrentRow.SetValue("KDAUF", x.KDAUF);//销售订单 | 
|                     items.CurrentRow.SetValue("KDPOS", x.KDPOS);//销售订单项 | 
|                     items.CurrentRow.SetValue("ERFMG", x.ERFMG);//数量 | 
|                     items.CurrentRow.SetValue("ERFME", x.ERFME);//单位 | 
|                     items.CurrentRow.SetValue("CHARG", x.CHARG);//批次 | 
|                     items.CurrentRow.SetValue("SERNR", x.SERNR);//序列号 | 
|                     items.CurrentRow.SetValue("UMLGO", x.UMLGO);//转入库位 | 
|                     items.CurrentRow.SetValue("BWTAR", x.BWTAR);//验证类型 | 
|                 }); | 
|   | 
|                 var movStr = ""; | 
|                 movMaterials.ForEach(x => movStr += x.ToString() + "\r\n"); | 
|                 LogTextHelper.WriteLine(_logDir, "调用方法{0},movMaterials={1}", "InvokeRFCFunctionWmsStkMov", movStr); | 
|   | 
|                 function.Invoke(_rfcDestination); | 
|                 var materialDoc = function.GetValue("MBLNR").ToString(); | 
|                 var res = function.GetValue("MJAHR").ToString(); | 
|   | 
|                 var etReturns = function.GetTable("RETURN"); | 
|   | 
|                 foreach (IRfcStructure etReturn in etReturns) | 
|                 { | 
|                     var message = etReturn.GetValue("MESSAGE") == null ? "" : etReturn.GetValue("MESSAGE").ToString(); | 
|                     msg = message == "Success" || message == "Successfully" ? "" : message; | 
|                     break; | 
|                 } | 
|   | 
|                 if (string.IsNullOrEmpty(materialDoc)) | 
|                 { | 
|                     LogTextHelper.WriteLine(_logDir, "msg={0}", msg); | 
|                 } | 
|                 else | 
|                 { | 
|                     LogTextHelper.WriteLine(_logDir, "materialDoc={0}, res={1}", materialDoc, res); | 
|                 } | 
|   | 
|                 return materialDoc; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 msg = ex.Message; | 
|                 LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "InvokeRFCFunctionWmsStkMov", ex.Message); | 
|                 return ""; | 
|             } | 
|         } | 
|   | 
|         /// <summary>         | 
|         /// 调用Sap函数/SIE/CHN_MM_WMS_REVERSEGR | 
|         /// (非IDOC) | 
|         /// </summary> | 
|         /// <param name="doc">物料凭证</param> | 
|         /// <returns></returns>         | 
|         public string InvokeRFCFunctionWmsReverser(string doc, out string msg) | 
|         { | 
|             try | 
|             { | 
|                 msg = ""; | 
|                 LogTextHelper.WriteLine(_logDir, "调用方法{0},doc={1}", "InvokeRFCFunctionWmsReverser", doc); | 
|                 var function = _rfcDestination.Repository.CreateFunction("/SIE/CHN_MM_WMS_REVERSEGR"); | 
|                 function.SetParameterActive(0, true); | 
|                 function.SetValue("MAT_DOC_NUMBER", doc); | 
|   | 
|                 function.Invoke(_rfcDestination); | 
|   | 
|                 var etReturns = function.GetTable("ET_RETURN"); | 
|   | 
|                 foreach (IRfcStructure etReturn in etReturns) | 
|                 { | 
|                     | 
|                     var message = etReturn.GetValue("POSTING_RESULT") == null ? "" : etReturn.GetValue("POSTING_RESULT").ToString(); | 
|                     msg = message.ToLower().Contains("success") ? "" : message; | 
|                     break; | 
|                 } | 
|   | 
|                 var res = string.IsNullOrEmpty(msg) ? "Success" : "Failure"; | 
|                 LogTextHelper.WriteLine(_logDir, "res={0},msg={1}", res, msg); | 
|   | 
|                 return res; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 msg = ex.Message; | 
|                 LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "InvokeRFCFunctionGetPoDetail", ex.Message); | 
|                 return null; | 
|             } | 
|         } | 
|     } | 
| } |