public static void ValidateSAPData(PlanningFunctionGroup activityPlanGroup, int functionCount) { lock (m_syncObject) { if (activityPlanGroup.FunctionList.Count > 0) { IRfcFunction sapValidateCostPlanFunction = SapConnection.GetObject().CurrentDestination.Repository.CreateFunction("BAPI_ACT_PRICE_CHECK_AND_POST"); IRfcTable returnTable = null; try { ///*** --- IMPORT (SAP TAB) -------------------------------***// IRfcStructure headerInfoStructure = sapValidateCostPlanFunction.GetStructure("HEADER_INFO"); // HEADER_INFO headerInfoStructure.SetValue("CO_AREA", ((ActivityPlan)activityPlanGroup.FunctionList[0]).ControllingArea); // 3 headerInfoStructure.SetValue("FISC_YEAR", ((ActivityPlan)activityPlanGroup.FunctionList[0]).FiscalYear); // 4 headerInfoStructure.SetValue("PERIOD_FROM", ((ActivityPlan)activityPlanGroup.FunctionList[0]).PeriodFrom); // 5 headerInfoStructure.SetValue("PERIOD_TO", ((ActivityPlan)activityPlanGroup.FunctionList[0]).PeriodTo); // 6 headerInfoStructure.SetValue("VERSION", ((ActivityPlan)activityPlanGroup.FunctionList[0]).Version); // 8 headerInfoStructure.SetValue("DOC_HDR_TX", ((ActivityPlan)activityPlanGroup.FunctionList[0]).DocumentHeaderText); // 9 headerInfoStructure.SetValue("PLAN_CURRTYPE", ((ActivityPlan)activityPlanGroup.FunctionList[0]).PlanningCurrency); // 10 sapValidateCostPlanFunction.SetValue("DELTA", ((ActivityPlan)activityPlanGroup.FunctionList[0]).Delta); // 11 sapValidateCostPlanFunction.SetValue("TESTRUN", "X"); // "X" - Validate Only, " " - Post foreach (IPlanningFunction myFunction in activityPlanGroup.FunctionList) { try { /*** --- IDX_STRUCTURE -- CO Planning: Plan Activity BAPIs -------------- ***/ IRfcTable indexTable = sapValidateCostPlanFunction.GetTable("IDX_STRUCTURE"); indexTable.Append(); indexTable.SetValue("OBJECT_INDEX", ((ActivityPlan)myFunction).ObjectIndex.ToString("000000")); indexTable.SetValue("VALUE_INDEX", ((ActivityPlan)myFunction).ValueIndex.ToString("000000")); indexTable.SetValue("ATTRIB_INDEX", "000000"); /*** --- OBJECT -- CO Planning: Objects for Plan Activity BAPIs --------- ***/ IRfcTable coObjectTable = sapValidateCostPlanFunction.GetTable("OBJECT"); coObjectTable.Append(); string objectIndex = ((ActivityPlan)myFunction).ObjectIndex.ToString("000000"); string valueIndex = ((ActivityPlan)myFunction).ValueIndex.ToString("000000"); try { if (coObjectTable.GetValue("OBJECT_INDEX") != null) { if (coObjectTable.GetValue("OBJECT_INDEX").ToString() != objectIndex) { coObjectTable.SetValue("OBJECT_INDEX", ((ActivityPlan)myFunction).ObjectIndex.ToString("000000")); // Calculated } } else { coObjectTable.SetValue("OBJECT_INDEX", ((ActivityPlan)myFunction).ObjectIndex.ToString("000000")); // Calculated } } catch (Exception ex) { coObjectTable.SetValue("OBJECT_INDEX", ((ActivityPlan)myFunction).ObjectIndex.ToString("000000")); } coObjectTable.SetValue("COSTCENTER", ((ActivityPlan)myFunction).CostCenter); // 12 coObjectTable.SetValue("ACTTYPE", ((ActivityPlan)myFunction).ActivityType); // 13 /*** --- ACCOUNT_PLAN_TOTVALUE -- CO Planning: Objects for Primary Cost BAPIs --------- ***/ IRfcTable totValueTable = sapValidateCostPlanFunction.GetTable("TOT_VALUE"); totValueTable.Append(); totValueTable.SetValue("VALUE_INDEX", ((ActivityPlan)myFunction).ValueIndex.ToString("000000")); // Calculated if (((ActivityPlan)myFunction).Price != string.Empty) { totValueTable.SetValue("PRICE_FIX", ((ActivityPlan)myFunction).Price); // 2 totValueTable.SetValue("DIST_KEY_PRICE_FIX", ((ActivityPlan)myFunction).DistributionKey); totValueTable.SetValue("PRICE_UNIT", "00001"); } else { totValueTable.SetValue("PRICE_FIX", "0"); } totValueTable.SetValue("ACTVTY_QTY", ((ActivityPlan)myFunction).Quantity); // 1 totValueTable.SetValue("DIST_KEY_QUAN", ((ActivityPlan)myFunction).DistributionKey); // 7 //// ToDo totValueTable.SetValue("CURRENCY", ((ActivityPlan)myFunction).TransactionCurrency); // 14 myFunction.Updated = true; } catch (Exception ex) { myFunction.ValidationResult = ex.Message; } } } catch (Exception exp) { foreach (IPlanningFunction myFunction in activityPlanGroup.FunctionList) { myFunction.ValidationResult = exp.Message; } } try { sapValidateCostPlanFunction.Invoke(SapConnection.GetObject().CurrentDestination); } catch (Exception ex) { DialogResult r = MessageBox.Show("SAP Authorization Error: " + ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); if (r == DialogResult.OK) { } ReturnProgressDataForm.CancelProcess(); return; } returnTable = sapValidateCostPlanFunction.GetTable("RETURN"); foreach (IPlanningFunction myFunction in activityPlanGroup.FunctionList) { if (!SAPRequest.ReturnValuesList.ContainsKey(myFunction.Signature)) { SAPRequest.ReturnValuesList.Add(myFunction.Signature, myFunction); } } if (returnTable.RowCount > 0) { try { string logPath = LogFile.CheckCreateLogFolder() + "\\PWLogValOnly" + DateTime.Now.ToString("(dd-MMM-yyyy-HH-mm-ss-f)") + ".txt"; if (!File.Exists(logPath)) { using (TextWriter writer = File.CreateText(logPath)) { writer.WriteLine("VALIDATION ONLY: " + DateTime.Now.ToString("(dd-MMM-yyyy-HH-mm-ss-f)")); writer.WriteLine(" "); for (int y = 0; y <= (returnTable.RowCount - 1); y += 1) { for (int z = 0; z <= (returnTable[y].ElementCount - 1); z += 1) { string par = returnTable[y][z].Metadata.Name; string val = returnTable[y].GetString(z); string messageLine = par + " : " + val; writer.WriteLine(messageLine); } writer.WriteLine(" "); } } } } catch (Exception ex) { //MessageBox.Show(ex.Message) } for (int j = 0; j <= (returnTable.RowCount - 1); j++) { int row = Convert.ToInt32(returnTable[j].GetString("ROW")) - 1; string message = returnTable[j].GetString("MESSAGE"); if (row < 0) { row = 0; } string rType = string.Empty; string messageV1 = string.Empty; string messageV2 = string.Empty; string messageV3 = string.Empty; string messageV4 = string.Empty; string rNumber = string.Empty; rType = returnTable[j].GetString("TYPE"); messageV1 = returnTable[j].GetString("MESSAGE_V1"); messageV2 = returnTable[j].GetString("MESSAGE_V2"); for (int i = 0; i <= (activityPlanGroup.FunctionList.Count - 1); i++) { int elementLocation = SAPRequest.GetObject().TotalProcessedBySAP + i; if (elementLocation < 0) { elementLocation = 0; } string activityType = ((ActivityPlan)SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation)).ActivityType; string costCenter = ((ActivityPlan)SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation)).CostCenter; try { messageV1 = messageV1.TrimStart('0'); messageV2 = messageV2.TrimStart('0'); } catch (Exception ex) { } try { if (i == row && !string.IsNullOrEmpty(message)) { SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation).Result = message; } else if (i != row && rType == "E") { try { if (messageV1 == activityType || messageV2 == activityType) { // account for incrementing batch number SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation).Result = message; } else if (messageV1 != activityType && messageV1 != costCenter && row == 0) { rNumber = returnTable[j].GetString("NUMBER"); if (rNumber != string.Empty) { SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation).Result = message; if (ReturnProgressDataForm.OperationCancelled) { break; } } } } catch (Exception ex) { } } else if (rType == "I") { foreach (IPlanningFunction myFunction in activityPlanGroup.FunctionList) { SAPRequest.ReturnValuesList[myFunction.Signature].Result = message; if (ReturnProgressDataForm.OperationCancelled) { break; } } return; } } catch (Exception ex) { } if (ReturnProgressDataForm.OperationCancelled) { break; } } if (ReturnProgressDataForm.OperationCancelled) { break; } } foreach (IPlanningFunction myFunction in activityPlanGroup.FunctionList) { if ((SAPRequest.ReturnValuesList[myFunction.Signature].Result == null) || (SAPRequest.ReturnValuesList[myFunction.Signature].Result == string.Empty)) { SAPRequest.ReturnValuesList[myFunction.Signature].Result = "pwValidated"; } if (ReturnProgressDataForm.OperationCancelled) { break; } } } else { foreach (IPlanningFunction myFunction in activityPlanGroup.FunctionList) { SAPRequest.ReturnValuesList[myFunction.Signature].Result = "pwValidated"; if (ReturnProgressDataForm.OperationCancelled) { break; } } } } } }
/// <summary> /// Extension member for PWFunctionCollection /// This member adds a series of grouped functions, based on sorting criteria, to the List /// A group represents a grid of tabular values to be passed to SAP in a single network function call /// </summary> /// <param name="functionCollection"></param> /// <returns></returns> public static List <IFunctionGroup> GetFunctionGroups(this MatManFunctionCollection functionCollection) { List <IFunctionGroup> groupList = new List <IFunctionGroup>(); MatManCompareFunction myCompare = new MatManCompareFunction(); SortedList <string, CostPlan> loadCostPlanList = new SortedList <string, CostPlan>(myCompare); SortedList <string, ActivityPlan> loadActivityPlanList = new SortedList <string, ActivityPlan>(myCompare); // Future function Lists //#Region Build function lists from the function collection (PWFunctionCollection) using dequeue while (functionCollection.Count > 0) { IMatManFunction myFunction = functionCollection.Dequeue(); Type myType = myFunction.GetType().BaseType; if (myFunction.GetType().BaseType == typeof(IPlanningFunction)) { switch (((IPlanningFunction)myFunction).FunctionType) { case PlanningFunctionType.CostPlan: { loadCostPlanList.Add(myFunction.Signature, (CostPlan)myFunction); break; } case PlanningFunctionType.ActivityPlan: { loadActivityPlanList.Add(myFunction.Signature, (ActivityPlan)myFunction); break; } default: break; } } } //#End Region // index for all elements int functionIndex = 0; #region CostPlan grouping { var costCenterElements = from function in loadCostPlanList.Values group function by new { ControllingArea = function.ControllingArea, FiscalYear = function.FiscalYear, PeriodFrom = function.PeriodFrom, PeriodTo = function.PeriodTo, Version = function.Version, DocumentHeaderText = function.DocumentHeaderText, PlanningCurrency = function.PlanningCurrency, Delta = function.Delta } into functionGroup select functionGroup; int costGroupSize = 0; int costObjectIndex = 0; int costValueIndex = 0; int count = costCenterElements.AsEnumerable().Count(); //int i = 0; for (int i = 0; i < count; i++) { var g = costCenterElements.ElementAt(i); int groupCount = g.Count(); //int j = 0; for (int j = 0; j < groupCount;) { costGroupSize = Math.Min((Settings.Default.MaximumBatchSize - 1), (groupCount - 1)); // Build a FunctionGroup to add to the list, if greater than max group size - begin new group PlanningFunctionGroup myGroup = new PlanningFunctionGroup(PlanningFunctionType.CostPlan); // increment Object Index (Index Structure) for (int k = 0; k <= costGroupSize; k++) { if (j < groupCount) { var item = costCenterElements.ElementAt(i).ElementAt(j); costObjectIndex++; costValueIndex++; functionIndex++; // increment Value Index (Index Structure) item.ObjectIndex = costObjectIndex; item.ValueIndex = costValueIndex; item.Index = functionIndex; myGroup.FunctionList.Add(item); j++; if (j >= groupCount) { break; } } else { break; } } groupList.Add(myGroup); } } } #endregion #region ActivityPlan grouping { var activityCenterElements = from function in loadActivityPlanList.Values group function by new { CompanyCode = function.ControllingArea, FiscalYear = function.FiscalYear, PeriodFrom = function.PeriodFrom, PeriodTo = function.PeriodTo, Version = function.Version, DocumentHeaderText = function.DocumentHeaderText, PlanningCurrency = function.PlanningCurrency, Delta = function.Delta } into functionGroup select functionGroup; int activityGroupSize = 0; int activityObjectIndex = 0; int activityValueIndex = 0; int activityAttributeIndex = 0; int count = activityCenterElements.AsEnumerable().Count(); //int i = 0; for (int i = 0; i < count; i++) { var g = activityCenterElements.ElementAt(i); int groupCount = g.Count(); //int j = 0; for (int j = 0; j < groupCount;) { activityGroupSize = Math.Min((Settings.Default.MaximumBatchSize - 1), (groupCount - 1)); // Build a FunctionGroup to add to the list, if greater than max group size - begin new group PlanningFunctionGroup myGroup = new PlanningFunctionGroup(PlanningFunctionType.ActivityPlan); // increment Object Index (Index Structure) for (int k = 0; k <= activityGroupSize; k++) { if (j < groupCount) { var item = activityCenterElements.ElementAt(i).ElementAt(j); activityObjectIndex++; activityValueIndex++; activityAttributeIndex++; functionIndex++; item.ObjectIndex = activityObjectIndex; item.ValueIndex = activityValueIndex; item.AttributeIndex = activityAttributeIndex; item.Index = functionIndex; myGroup.FunctionList.Add(item); j++; if (j >= groupCount) { break; } } else { break; } } groupList.Add(myGroup); } } } #endregion return(groupList); }
public static void PostSAPData(PlanningFunctionGroup costPlanGroup, int functionCount) { lock (m_syncObject) { if (costPlanGroup.FunctionList.Count > 0) { IRfcFunction sapPostCostPlanFunction = SapConnection.GetObject().CurrentDestination.Repository.CreateFunction("BAPI_COSTACTPLN_POSTPRIMCOST"); IRfcFunction sapCommitWorkFunction = SapConnection.GetObject().CurrentDestination.Repository.CreateFunction("BAPI_TRANSACTION_COMMIT"); IRfcTable returnTable = null; try { IRfcStructure headerInfoStructure = sapPostCostPlanFunction.GetStructure("HEADERINFO"); // HEADER_INFO headerInfoStructure.SetValue("CO_AREA", ((CostPlan)costPlanGroup.FunctionList[0]).ControllingArea); // 3 headerInfoStructure.SetValue("FISC_YEAR", ((CostPlan)costPlanGroup.FunctionList[0]).FiscalYear); // 4 headerInfoStructure.SetValue("PERIOD_FROM", ((CostPlan)costPlanGroup.FunctionList[0]).PeriodFrom); // 5 headerInfoStructure.SetValue("PERIOD_TO", ((CostPlan)costPlanGroup.FunctionList[0]).PeriodTo); // 6 headerInfoStructure.SetValue("VERSION", ((CostPlan)costPlanGroup.FunctionList[0]).Version); // 8 headerInfoStructure.SetValue("DOC_HDR_TX", ((CostPlan)costPlanGroup.FunctionList[0]).DocumentHeaderText); // 9 headerInfoStructure.SetValue("PLAN_CURRTYPE", ((CostPlan)costPlanGroup.FunctionList[0]).PlanningCurrency); // 10 sapPostCostPlanFunction.SetValue("DELTA", ((CostPlan)costPlanGroup.FunctionList[0]).Delta); // 11 foreach (IPlanningFunction myFunction in costPlanGroup.FunctionList) { try { IRfcTable coObjectTable = sapPostCostPlanFunction.GetTable("COOBJECT"); // OBJECT string objectIndex = ((CostPlan)myFunction).ObjectIndex.ToString("000000"); string valueIndex = ((CostPlan)myFunction).ValueIndex.ToString("000000"); coObjectTable.Append(); try { if (coObjectTable.GetValue("OBJECT_INDEX") != null) { if (((string)coObjectTable.GetValue("OBJECT_INDEX")) != objectIndex) { coObjectTable.SetValue("OBJECT_INDEX", ((CostPlan)myFunction).ObjectIndex.ToString("000000")); // Calculated } } else { coObjectTable.SetValue("OBJECT_INDEX", ((CostPlan)myFunction).ObjectIndex.ToString("000000")); // Calculated } } catch (Exception ex) { coObjectTable.SetValue("OBJECT_INDEX", ((CostPlan)myFunction).ObjectIndex.ToString("000000")); } coObjectTable.SetValue("COSTCENTER", ((CostPlan)myFunction).CostCenter); // 11 coObjectTable.SetValue("ACTTYPE", ((CostPlan)myFunction).ActivityType); // 13 coObjectTable.SetValue("ORDERID", ((CostPlan)myFunction).OrderID); // 14 coObjectTable.SetValue("WBS_ELEMENT", ((CostPlan)myFunction).WBSElement); // 15 IRfcTable totValueTable = sapPostCostPlanFunction.GetTable("TOTVALUE"); // TOT_VALUE totValueTable.Append(); totValueTable.SetValue("VALUE_INDEX", ((CostPlan)myFunction).ValueIndex.ToString("000000")); // Calculated if (((CostPlan)myFunction).FixedInputValue != string.Empty) { totValueTable.SetValue("FIX_VALUE", ((CostPlan)myFunction).FixedInputValue); // 2 } else { totValueTable.SetValue("FIX_VALUE", "0"); } totValueTable.SetValue("DIST_KEY_FIX_VAL", ((CostPlan)myFunction).DistributionKey); // 6 totValueTable.SetValue("COST_ELEM", ((CostPlan)myFunction).CostElement); // 12 totValueTable.SetValue("FUNCTION", ((CostPlan)myFunction).FunctionalArea); // 16 totValueTable.SetValue("FUND", ((CostPlan)myFunction).Fund); // 17 totValueTable.SetValue("GRANT_NBR", ((CostPlan)myFunction).Grant); // 18 totValueTable.SetValue("TRANS_CURR", ((CostPlan)myFunction).TransactionCurrency); // 19 IRfcTable indexTable = sapPostCostPlanFunction.GetTable("INDEXSTRUCTURE"); // IDX_STRUCTURE indexTable.Append(); indexTable.SetValue("OBJECT_INDEX", ((CostPlan)myFunction).ObjectIndex.ToString("000000")); indexTable.SetValue("VALUE_INDEX", ((CostPlan)myFunction).ValueIndex.ToString("000000")); indexTable.SetValue("ATTRIB_INDEX", "000000"); myFunction.Updated = true; } catch (Exception ex) { myFunction.ValidationResult = ex.Message; } } } catch (Exception exp) { foreach (IPlanningFunction myFunction in costPlanGroup.FunctionList) { myFunction.ValidationResult = exp.Message; } } try { RfcSessionManager.BeginContext(SapConnection.GetObject().CurrentDestination); sapPostCostPlanFunction.Invoke(SapConnection.GetObject().CurrentDestination); sapCommitWorkFunction.Invoke(SapConnection.GetObject().CurrentDestination); RfcSessionManager.EndContext(SapConnection.GetObject().CurrentDestination); } catch (Exception ex) { DialogResult r = MessageBox.Show("SAP Authorization Error: " + ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); if (r == DialogResult.OK) { } ReturnProgressDataForm.CancelProcess(); return; } returnTable = sapPostCostPlanFunction.GetTable("RETURN"); foreach (IPlanningFunction myFunction in costPlanGroup.FunctionList) { if (!SAPRequest.ReturnValuesList.ContainsKey(myFunction.Signature)) { SAPRequest.ReturnValuesList.Add(myFunction.Signature, myFunction); } } if (returnTable.RowCount > 0) { try { string logPath = LogFile.CheckCreateLogFolder() + "\\PWLogValOnly" + DateTime.Now.ToString("(dd-MMM-yyyy-HH-mm-ss-f)") + ".txt"; if (!File.Exists(logPath)) { using (TextWriter writer = File.CreateText(logPath)) { writer.WriteLine("VALIDATION AND POST: " + DateTime.Now.ToString("(dd-MMM-yyyy-HH-mm-ss-f)")); writer.WriteLine(" "); for (int y = 0; y <= (returnTable.RowCount - 1); y += 1) { for (int z = 0; z <= (returnTable[y].ElementCount - 1); z += 1) { string par = returnTable[y][z].Metadata.Name; string val = returnTable[y].GetString(z); string messageLine = par + " : " + val; writer.WriteLine(messageLine); } writer.WriteLine(" "); } } } } catch (Exception ex) { //MessageBox.Show(ex.Message) } for (int j = 0; j <= (returnTable.RowCount - 1); j++) { int row = Convert.ToInt32(returnTable[j].GetString("ROW")) - 1; string message = returnTable[j].GetString("MESSAGE"); if (row < 0) { row = 0; } string rType = string.Empty; string messageV1 = string.Empty; string messageV2 = string.Empty; string messageV3 = string.Empty; string messageV4 = string.Empty; string rNumber = string.Empty; rType = returnTable[j].GetString("TYPE"); messageV1 = returnTable[j].GetString("MESSAGE_V1"); messageV2 = returnTable[j].GetString("MESSAGE_V2"); for (int i = 0; i <= (costPlanGroup.FunctionList.Count - 1); i++) { int elementLocation = SAPRequest.GetObject().TotalProcessedBySAP + i; if (elementLocation < 0) { elementLocation = 0; } string costElement = ((CostPlan)SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation)).CostElement; string costCenter = ((CostPlan)SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation)).CostCenter; try { messageV1 = messageV1.TrimStart('0'); messageV2 = messageV2.TrimStart('0'); } catch (Exception ex) { } try { if (i == row && !string.IsNullOrEmpty(message)) { SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation).Result = message; } else if (i != row && rType == "E") { try { if (messageV1 == costElement || messageV2 == costElement) { // account for incrementing batch number SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation).Result = message; } else if (messageV1 != costElement && messageV1 != costCenter && row == 0) { rNumber = returnTable[j].GetString("NUMBER"); if (rNumber != string.Empty) { SAPRequest.ReturnValuesList.Values.ElementAt(elementLocation).Result = message; if (ReturnProgressDataForm.OperationCancelled) { break; } } } } catch (Exception ex) { } } else if (rType == "I") { foreach (IPlanningFunction myFunction in costPlanGroup.FunctionList) { SAPRequest.ReturnValuesList[myFunction.Signature].Result = message; if (ReturnProgressDataForm.OperationCancelled) { break; } } return; } } catch (Exception ex) { } if (ReturnProgressDataForm.OperationCancelled) { break; } } if (ReturnProgressDataForm.OperationCancelled) { break; } } foreach (IPlanningFunction myFunction in costPlanGroup.FunctionList) { if (SAPRequest.ReturnValuesList[myFunction.Signature].Result == null | SAPRequest.ReturnValuesList[myFunction.Signature].Result == string.Empty) { SAPRequest.ReturnValuesList[myFunction.Signature].Result = "pwValidated"; } if (ReturnProgressDataForm.OperationCancelled) { break; } } } else { foreach (IPlanningFunction myFunction in costPlanGroup.FunctionList) { SAPRequest.ReturnValuesList[myFunction.Signature].Result = "pwValidated"; if (ReturnProgressDataForm.OperationCancelled) { break; } } } } } }