diff --git a/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs b/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs index a5e0dc9..e8d8748 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs +++ b/admin.net.pro/Admin.NET/Vistar.Application/SapService/SapService.cs @@ -595,9 +595,176 @@ public class SapService : IDynamicApiController, ITransient return itemElements; } -} + + /// + /// SAP采购申请创建修改 + /// + /// + /// + public async Task SapPurchasingRequisitionApi(SapPurchasingRequisitionInput input) + { + // 创建SOAP XML请求 + var soapEnvelope = new XDocument( + new XDeclaration("1.0", "utf-8", "yes"), + new XElement(XName.Get("Envelope", "http://schemas.xmlsoap.org/soap/envelope/"), + new XElement(XName.Get("Header", "http://schemas.xmlsoap.org/soap/envelope/")), + new XElement(XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/"), + new XElement(XName.Get("Zmmfm013", "urn:sap-com:document:sap:soap:functions:mc-style"), + new XElement("IsReq", + new XElement("Reqkeyid", input.Reqkeyid), + new XElement("Businessid", input.Businessid), + new XElement("Messageid", input.Messageid), + new XElement("Sndprn", input.Sndprn), + new XElement("Rcvprn", input.Rcvprn), + new XElement("Requser", input.Requser), + new XElement("Note1", input.Note1), + new XElement("Note2", input.Note2), + new XElement("Note3", input.Note3) + ), + new XElement("ItData", + new XElement("item", + new XElement("Zwbid", input.Zwbid), + new XElement("Banfn", input.Banfn), + new XElement("Bsart", input.Bsart), + new XElement("Zoperate", input.Zoperate), + new XElement("Zttby1", input.Zttby1), + new XElement("Zttby2", input.Zttby2), + new XElement("Zttby3", input.Zttby3), + new XElement("Item", + // 使用循环生成 元素 + PurchasingRequisitionItem(input.Item) + ) + ) + ) + ) + ) + ) + ); + + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true, + ClientCertificateOptions = ClientCertificateOption.Manual + }; + try + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + // 设置基本身份验证信息 + var username = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); + var password = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapPassword); + var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}")); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); + // 将 XML 内容转换为字符串并设置请求内容类型为 text/xml + var content = new StringContent(soapEnvelope.ToString(), Encoding.UTF8, "text/xml"); + content.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); + httpClient.DefaultRequestHeaders.Add("Accept-Language", "zh"); + // 设置 SAP Web 服务的 URL + var url = "https://vhjqeds4ci.sap.vistar-eq.com:44300/sap/bc/srt/rfc/sap/zmmfm013/120/zmmfm013/zmmfm013"; + + // 发起 POST 请求到 SAP Web 服务 + var response = await httpClient.PostAsync(url, content); + + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsStringAsync(); + XDocument doc = XDocument.Parse(result); + var item = doc.Descendants("item").FirstOrDefault(); + var code = item?.Element("Code")?.Value; + var msg = item?.Element("Msg")?.Value; + var EtRetinfo= doc.Descendants("EtRetinfo").FirstOrDefault(); + var EtRetinfoItem= EtRetinfo.Descendants("item").FirstOrDefault(); + var Banfn= EtRetinfoItem?.Element("Banfn")?.Value; + var EtRetinfoMsg = EtRetinfoItem?.Element("Msg")?.Value; + + if (item == null) + { + var ES_RETItem = doc.Descendants("ES_RET").FirstOrDefault(); + var ES_RETcode = ES_RETItem?.Element("CODE")?.Value; + var ES_RETmsg = ES_RETItem?.Element("MSG")?.Value; + var outputES_RET = new SapOutput() + { + parameter = soapEnvelope.ToString(), + code = ES_RETcode, + msg = ES_RETmsg, + result = result + }; + return outputES_RET; + } + var output = new SapOutput() + { + parameter = soapEnvelope.ToString(), + code = code, + msg = msg, + result = result, + banfn= Banfn + }; + return output; + } + else + { + var result = await response.Content.ReadAsStringAsync(); + var output = new SapOutput() + { + parameter = soapEnvelope.ToString(), + code = "失败", + msg = "调用失败,状态码: " + response.StatusCode, + result = result + }; + return output; + } + } + } + catch (Exception ex) + { + // 记录错误日志 + Console.WriteLine("发生错误: " + ex.Message); + var output = new SapOutput() + { + code = "失败", + msg = "发生错误" + ex.Message, + result = "发生错误" + ex + }; + return output; + } + + } + /// + /// 生成采购申请 item 元素 + /// + /// + /// + public List PurchasingRequisitionItem(List items) + { + var itemElements = new List(); + foreach (var item in items) + { + var element = new XElement("item", + new XElement("Bnfpo", item.Bnfpo), + new XElement("Zzjjbs", item.Zzjjbs), + new XElement("Knttp", item.Knttp), + new XElement("Pstyp", item.Pstyp), + new XElement("Matnr", item.Matnr), + new XElement("Txz01", item.Txz01), + new XElement("Menge", item.Menge), + new XElement("Meins", item.Meins), + new XElement("Lfdat", item.Lfdat), + new XElement("Matkl", item.Matkl), + new XElement("Werks", item.Werks), + new XElement("Lgort", item.Lgort), + new XElement("Ekgrp", item.Ekgrp), + new XElement("Ekorg", item.Ekorg), + new XElement("Zzpspnr", item.Zzpspnr), + new XElement("Afnam", item.Afnam), + new XElement("Loekz", item.Loekz) + ); + itemElements.Add(element); + } + return itemElements; + } +} \ No newline at end of file diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Util/SapOutput.cs b/admin.net.pro/Admin.NET/Vistar.Application/Util/SapOutput.cs index 37daf0c..aea7200 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/Util/SapOutput.cs +++ b/admin.net.pro/Admin.NET/Vistar.Application/Util/SapOutput.cs @@ -21,4 +21,5 @@ public class SapOutput public string code { get; set; } public string msg { get; set; } public string result { get; set; } + public string? banfn { get; set; } }