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;
}