SAP發料介面實現(Java版)
阿新 • • 發佈:2018-11-23
發料介面原始碼如下:
public Map<String, Map<Integer, Map<String, Object>>> sendOutStorageMtrReq(List<Map<String, Object>> rows) throws Throwable{ ProdOrdGetDetail prodSap = new ProdOrdGetDetail(); Map<String,Map<String, List<Map<String, Object>>>> pbomDetailMap=new HashMap<>(); Map<String, Map<Integer, Map<String, Object>>> returnMap = new HashMap<>(); Map<Integer, Map<String, Object>> returnMap_1 = new HashMap<Integer, Map<String, Object>>(); Map<Integer, Map<String, Object>> returnMap_2 = new HashMap<Integer, Map<String, Object>>(); Map<String, Object> subMap = null; SapBapiInvoker sap = null; JCoDestination destination = null; JCoFunction function = null; JCoTable returntable = null; JCoTable goodsmvt_item = null; JCoTable goodsmvt_items = null; try { //根據生產訂單號來查詢SAP中的BOM明細 生產訂單號只有一個 Map<String, List<Map<String, Object>>> pbomMap = prodSap.queryPbom(rows.get(0).get("workOrderNo").toString()); //將查詢結果放MAP裡 生產訂單作為KEY pbomDetailMap.put(rows.get(0).get("workOrderNo").toString(), pbomMap); if(pbomMap.isEmpty()){ subMap = new HashMap<String, Object>(); subMap.put("type", "E"); subMap.put("message", "未獲取到訂單:" + rows.get(0).get("workOrderNo") + "BOM資訊!"); returnMap_1.put(0, subMap); returnMap.put("returntable", returnMap_1); return returnMap; } sap = new SapBapiInvoker(); destination = sap.connectSAPserver(propPath); function = destination.getRepository().getFunction("Z_SAP_PP_MTRRSM"); if(function == null){ throw new RuntimeException("Z_SAP_PP_MTRRSM not found in SAP."); } goodsmvt_item = function.getTableParameterList().getTable("GOODSMVT_ITEM"); for(Map<String, Object> row : rows){ Map<String, List<Map<String, Object>>> pbomItmMap = pbomDetailMap.get(rows.get(0).get("workOrderNo").toString()); String mtrErpCod = row.get("matnr").toString(); if(!pbomItmMap.containsKey(mtrFormat(mtrErpCod))){ continue; } String mtr = mtrFormat(mtrErpCod); List<Map<String, Object>> pbomItmList = pbomItmMap.get(mtr); String reservNo = ""; String resItem = ""; for (Map<String, Object> map : pbomItmList) { reservNo = map.get("RESERVATION_NUMBER").toString(); resItem = map.get("RESERVATION_ITEM").toString(); } goodsmvt_item.appendRow(); String orderid = this.autoGenericCode(row.get("workOrderNo").toString(), 12);//12位 不足12位補0 goodsmvt_item.setValue("ORDERID", orderid);//批次號 goodsmvt_item.setValue("MATERIAL", mtr);//物料編號 goodsmvt_item.setValue("ENTRY_QNT", Double.valueOf(row.get("qty").toString()));//領料數量 goodsmvt_item.setValue("ENTRY_UOM", row.get("qtyunit"));//物料的單位 goodsmvt_item.setValue("PLANT", row.get("werks"));//工廠 goodsmvt_item.setValue("STGE_LOC", row.get("lineLgortSapCod"));//庫存地 線邊倉的對應SAP編號 goodsmvt_item.setValue("MOVE_TYPE", 261);//移動型別 goodsmvt_item.setValue("RESERV_NO", reservNo); goodsmvt_item.setValue("RES_ITEM", resItem); } function.execute(destination); goodsmvt_items = function.getTableParameterList().getTable("GOODSMVT_ITEMS"); for(int i=0; i<goodsmvt_items.getNumRows(); i++){ subMap = new HashMap<String, Object>(); for(int j=0; j<goodsmvt_items.getMetaData().getFieldCount(); j++){ subMap.put(goodsmvt_items.getMetaData().getName(j).toLowerCase(), goodsmvt_items.getString(j)); } returnMap_1.put(i, subMap); goodsmvt_items.nextRow(); } if(!returnMap_1.isEmpty()){ returnMap.put("goodsmvt_items", returnMap_1); } returntable = function.getTableParameterList().getTable("RETURN"); //失敗時才會有,而且是一條,也就是i=0,放到returnMap的第一個(下標0) 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)); } returnMap_2.put(i, subMap); returntable.nextRow(); } if(!returnMap_2.isEmpty()){ returnMap.put("returntable", returnMap_2); } } finally { goodsmvt_items = null; goodsmvt_item = null; returntable = null; function = null; destination = null; sap = null; } return returnMap; }