From 13c0bf551876bd844338113f003600a8e2f73f9c Mon Sep 17 00:00:00 2001 From: f_compcontrol Date: Wed, 6 May 2026 08:41:53 +0200 Subject: [PATCH] GBS now working! --- .../Standard/SapStandardFunctions.cs | 74 ++++++++++++------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/CompControl.SapFrameworkConnector/Standard/SapStandardFunctions.cs b/CompControl.SapFrameworkConnector/Standard/SapStandardFunctions.cs index 61529d5..cd1051c 100644 --- a/CompControl.SapFrameworkConnector/Standard/SapStandardFunctions.cs +++ b/CompControl.SapFrameworkConnector/Standard/SapStandardFunctions.cs @@ -6,6 +6,8 @@ using System.Text; using static CompControl.ErpConnector.Utility.Models.SAP_CompControl_Models; using static CompControl.ErpConnector.Utility.Models.SAP_BAPI_Models; using System.Threading; +using Newtonsoft.Json; +//using System.Text.Json; namespace CompControl.SapFrameworkConnector { @@ -17,6 +19,7 @@ namespace CompControl.SapFrameworkConnector public static ApiResultBestellungen GetBestellung(string poNo, bool useDetailsAlternative = false) { ApiResultBestellungen result = new ApiResultBestellungen(); + ApiResultBestellungen resultSAG = new ApiResultBestellungen(); BAPI_PO_GETDETAIL_IMPORT import = new BAPI_PO_GETDETAIL_IMPORT() { PURCHASEORDER = poNo, ITEMS = "X" }; BAPI_PO_GETDETAIL_EXPORT details = new BAPI_PO_GETDETAIL_EXPORT(); if (useDetailsAlternative) { details = BAPI_PO_GETDETAILS1(import); _log.Debug($"using BAPI_PO_GETDETAILS-1 !!!"); } @@ -100,33 +103,54 @@ namespace CompControl.SapFrameworkConnector result.MessageTechnical = result.MessageShort = ermsg; } - // ==> MAYBE NEVER NEEDED: Lieferplan is also delivered with PO_DETAILS with sufficient rights - //if (result.GrDataItems.Count == 0) //If no Items were found take a look at SAG instead of PO - //{ - // _log.Debug($"Could not find any PO Items, will now try and search for SAG '{poNo}'"); - // try - // { - // BAPI_SAG_GETDETAIL_IMPORT sagInput = new BAPI_SAG_GETDETAIL_IMPORT(); - // sagInput.PURCHASINGDOCUMENT = poNo; - // sagInput.SHIPPING_DATA = "X"; - // sagInput.SCHEDULE_DATA = "X"; - // sagInput.ITEM_DATA = "X"; - // BAPI_SAG_GETDETAIL_EXPORT sagDetails = BAPI_SAG_GETDETAIL(sagInput); + try + { + BAPI_SAG_GETDETAIL_IMPORT sagInput = new BAPI_SAG_GETDETAIL_IMPORT(); + sagInput.PURCHASINGDOCUMENT = poNo; + sagInput.SHIPPING_DATA = "X"; + sagInput.SCHEDULE_DATA = "X"; + sagInput.ITEM_DATA = "X"; + BAPI_SAG_GETDETAIL_EXPORT sagDetails = BAPI_SAG_GETDETAIL(sagInput); - // //TODO: Add logic if RFC works! - // //foreach () - // //{ + //TODO: Add logic if RFC works! + if (sagDetails.SCHEDULE.Count > 0) //Only if any Items were returned! + { + List newGrList = new List(); + foreach (GrDataItem gr in result.GrDataItems) + { + bool atLeastOnePosFound = false; + foreach (BAPIMEOUTSCHEDULE schedule in sagDetails.SCHEDULE) + { + GrDataItem grCopy = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(gr)); //Kopierpfusch - // //} - // } - // catch (Exception ex) - // { - // string ermsg = $"GetBestellung SAG: General Error, please check LOG of ErpConnector!"; - // _log.Error(ermsg + $" => {ex}"); - // result.ErrorNumber = 900; - // result.MessageTechnical = result.MessageShort = ermsg; - // } - //} + if (schedule.ITEM_NO.Equals(gr.OrderPos)) + { + _log.Debug($"Matching SAGSCHEDULE => PosNo={schedule.ITEM_NO}, SchedLine={schedule.SCHED_LINE}, Qty={schedule.QUANTITY}, Date={schedule.DELIVERY_DATE}"); + + if (schedule.DELIVERY_DATE.Trim().Equals("")) { schedule.DELIVERY_DATE = "01.01.2199"; } + string day = schedule.DELIVERY_DATE.Substring(0, 2); + string month = schedule.DELIVERY_DATE.Substring(3, 2); + string year = schedule.DELIVERY_DATE.Substring(6, 4); + DateTime delDate = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)); + + grCopy.OrderQuantity = schedule.QUANTITY; + grCopy.Schedule = schedule.SCHED_LINE; + grCopy.ExpectedDeliveryDate = delDate; + newGrList.Add(grCopy); + } + } + } + + result.GrDataItems = newGrList; + } + } + catch (Exception ex) + { + string ermsg = $"GetBestellung SAG: General Error, please check LOG of ErpConnector!"; + _log.Error(ermsg + $" => {ex}"); + result.ErrorNumber = 900; + result.MessageTechnical = result.MessageShort = ermsg; + } _log.Debug($"Result of GetBestellung now consists of {result.GrDataItems.Count} GrDataItems and {result.ManufacturerDataItems.Count} ManufacturerDataItems...");