using NFine.Data.Extensions; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using NFine.Code; using NFine.Repository; using System.Data.Common; using NFine.Domain._03_Entity.SRM; using ICS.Application.Entity; using Newtonsoft.Json; using System.Configuration; using System.Data.SqlClient; using ICS.Data; using System.Net; using System.IO; using Newtonsoft.Json.Linq; using NFine.Domain._03_Entity.WMS; namespace NFine.Application.WMS { public class ICSToSAPApp : RepositoryFactory { //销售出库过账SAP public string DisPatchToSAP(string DisPatchCodeList, string WorkPoint) { string msg = ""; try { string DocNOList = ""; foreach (string DocNO in DisPatchCodeList.TrimEnd(',').Split(',')) { DocNOList += "'" + DocNO + "',"; } #region SAP(三层结构) string IsSuccess = ""; SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ Client = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ(); SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ1 Info = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ1(); List headlist = new List(); SAPCallBackDispatchVPN.ZWMS_DO_HEAD head = new SAPCallBackDispatchVPN.ZWMS_DO_HEAD(); string sql = @"select DISTINCT SDNCode from ICSSDN where SDNCode IN ({0}) AND WorkPoint='{1}'"; sql = string.Format(sql, DocNOList.TrimEnd(','), WorkPoint); DataTable Sapdt = SqlHelper.GetDataTableBySql(sql); foreach (DataRow dr in Sapdt.Rows) { head.VBELN = dr["SDNCode"].ToString(); headlist.Add(head); } Info.T_HEAD = headlist.ToArray(); List ItemList = new List(); List ItemLineList = new List(); Info.T_RETURN = new SAPCallBackDispatchVPN.ZWMS_DO_RETURN[1]; sql = @" select A.SDNCode,A.InvCode,A.SAPSequence,A.Sequence,A.Quantity,A.SDNQuantity ,ISNULL(B.BatchCode,'') AS BatchCode from ICSSDN A LEFT JOIN ICSExtension B ON B.ID=A.ExtensionID AND B.WorkPoint=A.WorkPoint WHERE SDNCode IN ({0}) AND A.WorkPoint='{1}'"; sql = string.Format(sql, DocNOList.TrimEnd(','), WorkPoint); Sapdt = SqlHelper.GetDataTableBySql(sql); foreach (DataRow dr in Sapdt.Rows) { if (Convert.ToDecimal(dr["Quantity"].ToString()) == Convert.ToDecimal(dr["SDNQuantity"].ToString())) { SAPCallBackDispatchVPN.ZWMS_DO_GERNR ItemLine = new SAPCallBackDispatchVPN.ZWMS_DO_GERNR(); SAPCallBackDispatchVPN.ZWMS_DO_ITEM Item = new SAPCallBackDispatchVPN.ZWMS_DO_ITEM(); Item.VBELN = dr["SDNCode"].ToString(); Item.POSNR = dr["SAPSequence"].ToString(); Item.LFIMG = System.Decimal.Round(Convert.ToDecimal(dr["Quantity"].ToString()), 3); ItemList.Add(Item); if (dr["BatchCode"].ToString() != "") { ItemLine.VBELN = dr["SDNCode"].ToString(); ItemLine.POSNR = dr["SAPSequence"].ToString(); ItemLine.GERNR = dr["BatchCode"].ToString(); ItemLineList.Add(ItemLine); } } } if (ItemList.Count > 0) { Info.T_ITEM = ItemList.ToArray(); if (ItemLineList.Count > 0) { Info.T_GERNR = ItemLineList.ToArray(); } SAPCallBackDispatchVPN.ZWMS_SK_DO_GZResponse result = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZResponse(); result = Client.CallZWMS_SK_DO_GZ(Info); foreach (SAPCallBackDispatchVPN.ZWMS_DO_RETURN resultItem in result.T_RETURN) { if (resultItem.ZFLG == "N") { IsSuccess = "N"; msg += resultItem.ZMESS + "/r/n"; } } if (msg == "") { string updatesql = @"Update ICSSDN set EATTRIBUTE1='已过账' where SDNCode in ({0}) AND WorkPoint='{1}'"; updatesql = string.Format(updatesql, DocNOList.TrimEnd(','), WorkPoint); if (SqlHelper.CmdExecuteNonQueryLi(updatesql) > 0) { msg = ""; } else { msg = "待过账状态回写失败!"; } } } #endregion return msg; } catch (Exception ex) { msg = ex.Message; return msg; } } public static string HttpPost(string url, string body) { try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*"; request.ContentType = "application/json; charset=utf-8"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { throw new Exception(ex.Message); } } } }