1. 程式人生 > >SAP介面開發-根據生產訂單編號獲取物料資訊

SAP介面開發-根據生產訂單編號獲取物料資訊

根據生產訂單編號獲取物料資訊需要呼叫 BAPI_PRODORD_GET_DETAIL函式,呼叫介面用C#語言實現的原始碼如下所示:

        //2018年10月19號新增

        /// <summary>
        /// 根據生產訂單獲取物料資訊
        /// </summary>
        /// <param name="prd"></param>
        /// <param name="OrderNum">生產訂單號</param>
        /// <param name="errMsg">返回錯誤資訊</param>
        /// <returns></returns>
        public static int GetMatDetailByOrder(RfcDestination prd, string OrderNums, out List<MaterialDetail> lstM, out string errMsg)
        {
            errMsg = "";
            lstM = new List<MaterialDetail>();
            MaterialDetail md1;
            try
            {
                //RfcRepository SapRfcRepository = prd.Repository;
                //IRfcFunction function = SapRfcRepository.CreateFunction("BAPI_PRODORD_GET_DETAIL");
                //function.SetValue("NUMBER", OrderNums[0]);//生產訂單號
                //function.SetValue("ORDER_OBJECTS", "X");//預設
                //RfcSessionManager.BeginContext(prd);
                //function.Invoke(prd);
                //RfcSessionManager.EndContext(prd);
                //IRfcTable iTable = function.GetTable("COMPONENT");
                RfcRepository SapRfcRepository = prd.Repository;

                IRfcFunction function = SapRfcRepository.CreateFunction("BAPI_PRODORD_GET_DETAIL");
                IRfcStructure stru = function.GetStructure("ORDER_OBJECTS");
                IRfcTable pTable = null;
                IRfcTable mTable = null;

                stru.SetValue("COMPONENTS", "1");
                mTable = function.GetTable("COMPONENT");

                function.SetValue("ORDER_OBJECTS", "X");

                OrderNums = OrderNums.PadLeft(12, '0');
                function.SetValue("NUMBER", OrderNums);//ORDER_NUMBER
                function.Invoke(prd);

                IRfcStructure Return = function.GetStructure("RETURN");
                if (mTable.RowCount > 0)
                {
                    for (int j = 0; j < mTable.RowCount; j++)
                    {
                        #region
                        //iTable.CurrentIndex = j;
                        //DataRow dr = dt.NewRow();
                        //dr["Item_Num"] = iTable.GetString("ITEM_NUMBER").ToString();
                        //dr["Mterial_Description"] = iTable.GetString("MATERIAL_DESCRIPTION").ToString();
                        //dr["Commited_Quantity"] = iTable.GetString("COMMITED_QUANTITY").ToString();
                        //dr["Shortage"] = iTable.GetString("SHORTAGE").ToString();
                        //dr["Material_Externsion"] = iTable.GetString("MATERIAL_EXTERNSION").ToString();
                        //dr["Req_Segment"] = iTable.GetString("REQ_SEGMENT").ToString();
                        //dr["Stock_Segment"] = iTable.GetString("STOCK_SEGMENT").ToString();
                        //dt.Rows.Add(dr);
                        #endregion
                        md1 = new MaterialDetail();
                        mTable.CurrentIndex = j;
                        md1.ORDERID = mTable.GetValue("ORDER_NUMBER").ToString(); //訂單號
                        md1.MATERIAL = mTable.GetValue("MATERIAL").ToString();//物料編號
                        md1.MATERIAL = md1.MATERIAL.TrimStart('0');
                        md1.ENTRY_QNT = mTable.GetValue("ENTRY_QUANTITY").ToString();
                        md1.ENTRY_QNT = Math.Round(decimal.Parse(md1.ENTRY_QNT)).ToString();
                        md1.PLANT = mTable.GetValue("PROD_PLANT").ToString();//工廠
                        md1.MATL_DESC = mTable.GetValue("MATERIAL_DESCRIPTION").ToString();//
                        md1.WITHDRAWN_QUANTITY = mTable.GetValue("WITHDRAWN_QUANTITY").ToString();//
                        md1.WITHDRAWN_QUANTITY = Math.Round(decimal.Parse(md1.WITHDRAWN_QUANTITY)).ToString();
                        md1.RESERV_NO = mTable.GetValue("RESERVATION_NUMBER").ToString();//
                        md1.RES_ITEM = mTable.GetValue("RESERVATION_ITEM").ToString();//
                        md1.ORDER_ITNO = mTable.GetValue("ITEM_NUMBER").ToString();
                        md1.ORDERID = mTable.GetValue("ORDER_NUMBER").ToString();
                        md1.ENTRY_UOM = mTable.GetValue("ENTRY_UOM").ToString();
                        md1.MOVE_TYPE = mTable.GetValue("MOVEMENT_TYPE").ToString();
                        //if (md1.MOVE_TYPE == "261")
                        lstM.Add(md1);
                    }
                }
                return 0;
            }
            catch (System.Exception ex)
            {
                errMsg = ex.Message;
                return -1;
            }
        }