SAP移庫介面實現(Java版)
阿新 • • 發佈:2018-11-23
SAP移庫介面Java程式碼實現:
移庫介面: public static Map<String, Object> storageMoveToSap(StorageTransfer storageTransfer) throws Throwable{ SapBapiInvoker sap = new SapBapiInvoker(); JCoDestination destination = sap.connectSAPserver(propPath); JCoFunction function = null; JCoTable returntable = null; Map<String, Object> subMap = new HashMap<String, Object>(); boolean sapSuccessFlag=false; try{ JCoContext.begin(destination); JCoRepository res=destination.getRepository(); function = res.getFunction("BAPI_GOODSMVT_CREATE"); if(function == null){ throw new RuntimeException("BAPI_GOODSMVT_CREATE not found in SAP."); } JCoStructure goodsmvt_header = function.getImportParameterList().getStructure("GOODSMVT_HEADER"); JCoStructure goodsmvt_code = function.getImportParameterList().getStructure("GOODSMVT_CODE"); String year = TimeUtil.getCurrentDate(""); goodsmvt_header.setValue("PSTNG_DATE",year);//記賬日期 goodsmvt_header.setValue("DOC_DATE",year);//憑證日期 goodsmvt_header.setValue("PR_UNAME",getSAPConnUser());//連線使用者 goodsmvt_code.setValue("GM_CODE", "04");// BAPI 貨物移動分配事務程式碼 String outWerks = storageTransfer.getOutplantErpCode(); String outLgort = storageTransfer.getOutLgortErpCode(); String inWerks = outWerks;// 同一工廠 String inLgort = storageTransfer.getInLgortErpCode(); JCoTable goodsmvt_item = function.getTableParameterList().getTable("GOODSMVT_ITEM"); goodsmvt_item.appendRow(); String matnr = AutoGenericCodeUtil.autoGenericCode(storageTransfer.getOutMatnr(), 18); goodsmvt_item.setValue("MATERIAL",matnr);//物料號 18位 goodsmvt_item.setValue("PLANT",outWerks); goodsmvt_item.setValue("STGE_LOC",outLgort); goodsmvt_item.setValue("MOVE_TYPE","311");//同一個工廠下不同庫存地移庫 固定寫死311 goodsmvt_item.setValue("ENTRY_QNT",storageTransfer.getInMatnrQty());//入庫數量 goodsmvt_item.setValue("MOVE_PLANT",inWerks); goodsmvt_item.setValue("MOVE_STLOC",inLgort); function.execute(destination); JCoStructure export = function.getExportParameterList().getStructure("GOODSMVT_HEADRET"); String MAT_DOC = export.getValue("MAT_DOC").toString(); returntable = function.getTableParameterList().getTable("RETURN"); if(StringUtils.isNotBlank(MAT_DOC)&&returntable.isEmpty()){ function=res.getFunction("BAPI_TRANSACTION_COMMIT");//提交 function.execute(destination); subMap.put("MAT_DOC", MAT_DOC); sapSuccessFlag=true; }else{ for(int i = 0; i<returntable.getNumRows(); i++){ subMap = new HashMap<String, Object>(); for(int j = 0; j<returntable.getMetaData().getFieldCount(); j++){ subMap.put(returntable.getMetaData().getName(j).toLowerCase(), returntable.getString(j)); } returntable.nextRow(); } } } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); }finally { JCoContext.end(destination); returntable = null; function = null; destination = null; sap = null; } subMap.put("sapSuccessFlag", sapSuccessFlag); return subMap; }