1. 程式人生 > >SAP發料介面實現(C#版)

SAP發料介面實現(C#版)


        //發料介面
        /// <summary>
        /// 根據發料單號獲取發料單內所包含的物料資訊,根據生產訂單的編號,發料至生產訂單
        /// </summary>
        /// <param name="prd"></param>
        /// <param name="OrderNum">發料單號</param>
        /// <param name="errMsg">錯誤資訊</param>
        /// <returns></returns>
        public static int SendOutStorageMtrReq(RfcDestination prd, List<MaterialDetail> list, out string errMsg)
        {
            errMsg = "";
            try
            {
                RfcRepository SapRfcRepository = prd.Repository;
                IRfcFunction function = SapRfcRepository.CreateFunction("Z_SAP_PP_MTRRSM");
                //IRfcStructure strCode = function.GetStructure("GOODSMVT_CODE");
                //strCode.SetValue("GM_CODE", "03");
                //IRfcStructure strHeader = function.GetStructure("GOODSMVT_HEADER");
                //ln.Write("1", DateTime.Today.ToString("yyyy-MM-dd"));
                //strHeader.SetValue("PSTNG_DATE", DateTime.Today.ToString("yyyy-MM-dd"));
                //strHeader.SetValue("DOC_DATE", DateTime.Today.ToString("yyyy-MM-dd"));
                //IRfcFunction functioncmt = SapRfcRepository.CreateFunction("BAPI_TRANSACTION_COMMIT");
                RfcSessionManager.BeginContext(prd);
                //functioncmt.SetValue("WAIT", "X");
                IRfcTable iTable = function.GetTable("GOODSMVT_ITEM");//獲取表結構
                for (int i = 0; i < list.Count; i++)
                {
                    iTable.Append();
                    iTable.SetValue("ORDERID", list[i].ORDERID);//生產批次號 
                    iTable.SetValue("MATERIAL", list[i].MATERIAL);//物料編號
                    iTable.SetValue("ENTRY_QNT", list[i].ENTRY_QNT);//領料數量
                    iTable.SetValue("ENTRY_UOM", list[i].ENTRY_UOM);//物料的單位
                    iTable.SetValue("PLANT", list[i].PLANT);//工廠
                    iTable.SetValue("STGE_LOC", list[i].STGE_LOC);//庫存地  線邊倉的對應SAP編號
                    iTable.SetValue("MOVE_TYPE", "261");//移動型別
                    iTable.SetValue("RESERV_NO", list[i].RESERV_NO);
                    iTable.SetValue("RES_ITEM", list[i].RES_ITEM);
                }
                function.Invoke(prd);
                //functioncmt.Invoke(prd);
                RfcSessionManager.EndContext(prd);
                IRfcTable Return = function.GetTable("RETURN");
                if (Return.RowCount > 0)
                {
                    if (Return.GetString("TYPE").ToString().Trim() == "I")
                    {
                        //Suess.Text = RETURN.GetString("MESSAGE").ToString();
                        errMsg = "憑證號:" + function.GetString("NUMBER").Trim();
                        prd = null;
                        return 0;
                    }
                    else if (Return.GetString("TYPE").ToString().Trim() == "E")
                    {
                        errMsg = Return.GetString("MESSAGE").ToString();
                        prd = null;
                        return -1;
                    }
                    else
                        return -1;
                }
                else
                {
                    IRfcStructure strReturn = function.GetStructure("GOODSMVT_HEADRET");
                    errMsg = strReturn.GetValue("MAT_DOC").ToString();
                    return 0;
                }
            }
            catch (System.Exception ex)
            {
                errMsg = ex.Message;
                return -1;
            }
        }