基本上和用 abap 调用 bapi 差不多,贴出来记录一下:
package com.hs.purchase.biz; import java.util.Vector; import com.hs.commUtil.SapRfc; import com.hs.purchase.model.SapMaterialEntity; import com.sap.mw.jco.IMetaData; import com.sap.mw.jco.JCO; import com.sap.mw.jco.JCO.Client; import com.sap.mw.jco.JCO.Repository; import java.util.Hashtable; public class SapMaterialCreator{ //........略......... /*** * 新建一个SAP物料主数据 * @param myConnection SAP连接 * @param entity 主数据的内容,注易一定要带上料号 * @return */ public boolean newSapMaterial (Client myConnection , SapMaterialEntity entity ,String msgType,String message ) { Repository repository = SapRfc.getRepository(myConnection); // material_no = String.format("%1$,018d", Integer.parseInt(material_no)); // //自动获取物料号码-暂时不用 // JCO.Function getNum = SapRfc.getFunction("BAPI_STDMATERIAL_GETINTNUMBER", myConnection); // if ( getNum != null) // { // getNum.getImportParameterList().setValue(entity.getMaterial_type(),"MATERIAL_TYPE"); // getNum.getImportParameterList().setValue("M","INDUSTRY_SECTOR"); // getNum.getImportParameterList().setValue(1,"REQUIRED_NUMBERS"); // // myConnection.execute(getNum); // // JCO.Table mTable = getNum.getTableParameterList().getTable("MATERIAL_NUMBER"); // if (mTable.getNumRows() > 0) { // mTable.setRow(0); // entity.setMaterial_no( mTable.getValue("MATERIAL").toString()); // } else { // return ""; // } // // } JCO.Function saveData = SapRfc.getFunction("BAPI_MATERIAL_SAVEDATA", myConnection); if (saveData != null) { JCO.ParameterList imports = saveData.getImportParameterList(); JCO.ParameterList exports = saveData.getExportParameterList(); JCO.ParameterList tables = saveData.getTableParameterList(); /* import structure */ JCO.Structure head_data = imports.getStructure("HEADDATA"); JCO.Structure plant_data = imports.getStructure("PLANTDATA"); JCO.Structure plant_data_x = imports.getStructure("PLANTDATAX"); JCO.Structure sales_data = imports.getStructure("SALESDATA"); JCO.Structure sales_data_x = imports.getStructure("SALESDATAX"); JCO.Structure client_data = imports.getStructure("CLIENTDATA"); JCO.Structure client_data_x = imports.getStructure("CLIENTDATAX"); JCO.Structure valuation_data = imports.getStructure("VALUATIONDATA"); JCO.Structure valuation_data_x = imports.getStructure("VALUATIONDATAX"); IMetaData structureDefinition = repository.getStructureDefinition("BAPI_TE_MARA"); JCO.Structure te_mara_data = JCO.createStructure(structureDefinition); structureDefinition = repository.getStructureDefinition("BAPI_TE_MARAX"); JCO.Structure te_mara_data_x = JCO.createStructure(structureDefinition); /* export structure */ JCO.Structure return_data = exports.getStructure("RETURN"); /* table */ JCO.Table material_description = tables.getTable("MATERIALDESCRIPTION"); JCO.Table units_of_measure = tables.getTable("UNITSOFMEASURE"); JCO.Table units_of_measure_x = tables.getTable("UNITSOFMEASUREX"); JCO.Table tax_classifications = tables.getTable("TAXCLASSIFICATIONS"); JCO.Table extensionin = tables.getTable("EXTENSIONIN"); JCO.Table extensionin_x = tables.getTable("EXTENSIONINX"); // String material_type = "FERT"; /* 建物料时要更新的视图 */ head_data.setValue("X","BASIC_VIEW" ); // 基本数据视图 head_data.setValue("X","SALES_VIEW"); // 销售视图 head_data.setValue("X","PURCHASE_VIEW"); // 采购视图 head_data.setValue("X","MRP_VIEW"); // 物料需求计划(MRP)视图 head_data.setValue("X","FORECAST_VIEW"); // 预测视图 head_data.setValue("X","WORK_SCHED_VIEW"); // 工作计划视图 head_data.setValue("X","PRT_VIEW"); // 生产资源/工具(PRT)视图 head_data.setValue("X","STORAGE_VIEW"); // 存储视图 head_data.setValue("X","WAREHOUSE_VIEW"); // 仓库管理视图 head_data.setValue("X","QUALITY_VIEW"); // 质量管理视图 head_data.setValue("X","ACCOUNT_VIEW"); // 会计视图 head_data.setValue("X","COST_VIEW"); // 成本视图 /* 特殊的物料类型,去掉相应不该有的视图 */ String material_type = entity.getMaterial_type(); if (material_type == "FERT") { head_data.setValue("PURCHASE_VIEW", ""); // 成品 去掉 采购视图 } else if (material_type == "ROH" && material_type == "ROH1" && material_type == "ROH2" && material_type == "ROH3" && material_type == "HAWA" && material_type == "Z001" && material_type == "Z002" && material_type == "Z003" && material_type == "Z004" && material_type == "Z005" && material_type == "Z006" && material_type == "Z007") { head_data.setValue("WORK_SCHED_VIEW", ""); // 工作计划视图 } else if (material_type == "UNBW") { head_data.setValue("WAREHOUSE_VIEW", ""); // 仓库管理视图 } head_data.setValue(entity.getMaterial_no(),"MATERIAL"); head_data.setValue("M","IND_SECTOR"); head_data.setValue(entity.getMaterial_type(),"MATL_TYPE"); // WERKS 4 Plant 工厂 plant_data.setValue(entity.getPlant(),"PLANT"); plant_data_x.setValue(entity.getPlant(),"PLANT"); // VKORG 4 Sales Organization 销售组织 sales_data.setValue(entity.getSales_org(),"SALES_ORG"); sales_data_x.setValue(entity.getSales_org(),"SALES_ORG"); // VTWEG 2 Distribution Channel 分销渠道 sales_data.setValue(entity.getDistr_chan(),"DISTR_CHAN"); sales_data_x.setValue(entity.getDistr_chan(),"DISTR_CHAN"); // MAKTX 40 Material Description 物料描述(短文本) material_description.appendRow(); material_description.setValue("1","LANGU"); // material_description.setValue("ZH","LANGU_ISO"); material_description.setValue(entity.getMatl_desc_zh(),"MATL_DESC"); // MAKTX 40 Material Description 物料描述(英文) material_description.appendRow(); material_description.setValue("E","LANGU"); // material_description.setValue("EN","LANGU_ISO"); material_description.setValue(entity.getMatl_desc_en(),"MATL_DESC"); units_of_measure.appendRow(); units_of_measure_x.appendRow(); // MEINS 3 Base Unit of Measure 基本计量单位 units_of_measure.setValue(entity.getAlt_unit(),"ALT_UNIT"); units_of_measure_x.setValue("X","ALT_UNIT"); // GROSS_WT BRGEW QUAN 13 3 毛重 units_of_measure.setValue(entity.getGross_wt(),"GROSS_WT"); units_of_measure_x.setValue("X","GROSS_WT"); // UNIT_OF_WT GEWEI UNIT 3 0 重量单位 units_of_measure.setValue(entity.getUnit_of_wt(),"UNIT_OF_WT"); units_of_measure_x.setValue("X","UNIT_OF_WT"); // VOLUME VOLUM QUAN 13 3 业务量(体积) units_of_measure.setValue(entity.getVolume(),"VOLUME"); units_of_measure_x.setValue("X","VOLUME"); // VOLUMEUNIT VOLEH UNIT 3 0 体积单位 units_of_measure.setValue(entity.getVolumeunit(),"VOLUMEUNIT"); units_of_measure_x.setValue("X","VOLUMEUNIT"); // MEINS 3 Base Unit of Measure 基本计量单位 client_data.setValue(entity.getAlt_unit(),"BASE_UOM"); client_data_x.setValue("X","BASE_UOM"); // MATKL 4 Material Group 物料组 client_data.setValue(entity.getMatl_group(),"MATL_GROUP"); client_data_x.setValue("X","MATL_GROUP"); // EXTWG 2 外部物料组(88) client_data.setValue(entity.getExtmatlgrp(),"EXTMATLGRP"); client_data.setValue("X","EXTMATLGRP"); // SPART 2 Division 产品组 client_data.setValue(entity.getDivision(),"DIVISION"); client_data.setValue("X","DIVISION"); // MARA-MSTAE 2 跨工厂物料状态 client_data.setValue(entity.getPur_status(),"PUR_STATUS"); client_data.setValue("X","PUR_STATUS"); // MTPOS 4 Item category group 普通项目类别组 client_data.setValue(entity.getItem_cat(),"ITEM_CAT"); client_data.setValue("X","ITEM_CAT"); // UNIT_OF_WT GEWEI UNIT 3 0 重量单位 client_data.setValue(entity.getUnit_of_wt(),"UNIT_OF_WT"); client_data_x.setValue("X","UNIT_OF_WT"); // MARA-KZKFG 是否可配置物料 te_mara_data.setValue(entity.getKzkfg(),"KZKFG"); te_mara_data_x.setValue("X","KZKFG"); te_mara_data.setValue(entity.getMaterial_no(),"MATERIAL"); te_mara_data_x.setValue(entity.getMaterial_no(),"MATERIAL"); //更新扩展字段 extensionin.appendRow(); extensionin_x.appendRow(); extensionin.setValue("BAPI_TE_MARA","STRUCTURE"); extensionin.setValue(te_mara_data.getString("MATERIAL")+te_mara_data.getString("KZKFG"),"VALUEPART1"); extensionin_x.setValue("BAPI_TE_MARAX","STRUCTURE"); extensionin_x.setValue(te_mara_data_x.getString("MATERIAL")+ te_mara_data_x.getString("KZKFG"),"VALUEPART1"); // DWERK 4 Delivering Plant 交货工厂 (自有或外部) sales_data.setValue(entity.getDelyg_plnt(),"DELYG_PLNT"); sales_data_x.setValue("X","DELYG_PLNT"); tax_classifications.appendRow(); tax_classifications.setValue("CN","DEPCOUNTRY"); tax_classifications.setValue("MWST","TAX_TYPE_1"); // TAXKM 1 Material tax type 物料的税 tax_classifications.setValue(entity.getTaxclass_1(),"TAXCLASS_1"); // VERSG 1 Matl statistics grp 物料统计组 SD视图2 sales_data.setValue(entity.getMatl_stats(),"MATL_STATS"); sales_data_x.setValue("X","MATL_STATS"); // KONDM 2 物料定价组 SD视图2 sales_data.setValue(entity.getMat_pr_grp(),"MAT_PR_GRP"); sales_data_x.setValue("X","MAT_PR_GRP"); // KTGRM 2 Acct assignment grp 科目设置组 SD视图2 sales_data.setValue(entity.getAcct_assgt(),"ACCT_ASSGT"); sales_data_x.setValue("X","ACCT_ASSGT"); // MTPOS 4 Item category group 项目类别组 SD视图2 同是否可配置相关 sales_data.setValue(entity.getItem_cat_sd(),"ITEM_CAT"); sales_data_x.setValue("X","ITEM_CAT"); // MTVFP 2 可用性检查 销售一般工厂 视图 plant_data.setValue(entity.getAvailcheck(),"AVAILCHECK"); plant_data_x.setValue("X","AVAILCHECK"); // LADGR 4 LoadingGrp 装载组 plant_data.setValue(entity.getLoadinggrp(),"LOADINGGRP"); plant_data_x.setValue("X","LOADINGGRP"); // TRAGR 4 Trans. Grp 运输组 client_data.setValue(entity.getTrans_grp(),"TRANS_GRP"); client_data_x.setValue("X","TRANS_GRP"); // EKGRP 3 采购组 plant_data.setValue(entity.getPur_group(),"PUR_GROUP"); plant_data_x.setValue("X","PUR_GROUP"); // DISMM 2 MRP Type MRP类型 plant_data.setValue(entity.getMrp_type(),"MRP_TYPE"); plant_data_x.setValue("X","MRP_TYPE"); // MINBE 13 Reorder Point 再订货点 plant_data.setValue(entity.getReorder_pt(),"REORDER_PT"); plant_data_x.setValue("X","REORDER_PT"); // DISPO 3 MRP Controller MRP 控制者 plant_data.setValue(entity.getMrp_ctrler(),"MRP_CTRLER"); plant_data_x.setValue("X","MRP_CTRLER"); // DISLS 2 Lot size 批量 (物料计划) plant_data.setValue(entity.getLotsizekey(),"LOTSIZEKEY"); plant_data_x.setValue("X","LOTSIZEKEY"); // MARC-BSTMI 最小批量大小 plant_data.setValue(entity.getMinlotsize(),"MINLOTSIZE"); plant_data_x.setValue("X","MINLOTSIZE"); // AUSSS 5 装配报废率 plant_data.setValue(entity.getAssy_scrap(),"ASSY_SCRAP"); plant_data_x.setValue("X","ASSY_SCRAP"); // BSTRF 13 ing value for purchase order quantity 舍入值 plant_data.setValue(entity.getRound_val(),"ROUND_VAL"); plant_data_x.setValue("X","ROUND_VAL"); // BESKZ 1 Procurement type 采购类型 plant_data.setValue(entity.getProc_type(),"PROC_TYPE"); plant_data_x.setValue("X","PROC_TYPE"); // SOBSL 2 Special procurement 特殊采购类型 plant_data.setValue(entity.getSpproctype(),"SPPROCTYPE"); plant_data_x.setValue("X","SPPROCTYPE"); // RGEKZ 1 Indicator: Backflush 标识:反冲 plant_data.setValue(entity.getBackflush(),"BACKFLUSH"); plant_data_x.setValue("X","BACKFLUSH"); // LGPRO 4 Prod. stor. location 生产仓储地点 plant_data.setValue(entity.getIss_st_loc(),"ISS_ST_LOC"); plant_data_x.setValue("X","ISS_ST_LOC"); // LGFSB 4 Storage loc. for EP 外部采购的缺省仓储位置 (不填) plant_data.setValue(entity.getSloc_exprc(),"SLOC_EXPRC"); plant_data_x.setValue("X","SLOC_EXPRC"); // DZEIT 3 In-house production time 自制生产时间 plant_data.setValue(entity.getInhseprodt(),"INHSEPRODT"); plant_data_x.setValue("X","INHSEPRODT"); // PLIFZ 3 Planned Deliv. Time 计划的天数内交货 plant_data.setValue(entity.getPlnd_delry(),"PLND_DELRY"); plant_data_x.setValue("X","PLND_DELRY"); // WEBAZ 3 Goods Receipt Processing Time in Days 以天计的收货处理时间 plant_data.setValue(entity.getGr_pr_time(),"GR_PR_TIME"); plant_data_x.setValue("X","GR_PR_TIME"); // FHORI 3 SchedMargin key 浮动的计划边际码 plant_data.setValue(entity.getSm_key(),"SM_KEY"); plant_data_x.setValue("X","SM_KEY"); // EISBE 13 Safety Stock 安全库存 plant_data.setValue(entity.getSafety_stk(),"SAFETY_STK"); plant_data_x.setValue("X","SAFETY_STK"); // PERKZ 1 Period Indicator 期间标识 plant_data.setValue(entity.getPeriod_ind(),"PERIOD_IND"); plant_data_x.setValue("X","PERIOD_IND"); // STRGR 2 Strategy group 计划策略组 plant_data.setValue(entity.getPlan_strgp(),"PLAN_STRGP"); plant_data_x.setValue("X","PLAN_STRGP"); // MISKZ 1 综合MRP 综合MRP标识 plant_data.setValue(entity.getMixed_mrp(),"MIXED_MRP"); plant_data_x.setValue("X","MIXED_MRP"); // // MTVFP 2 Availability check 可用性检查的检查组 // plant_data.setValue("AVAILCHECK", "02"); // plant_data_x.setValue("AVAILCHECK", "X"); // SBDKZ 1 独立/集中 plant_data.setValue(entity.getDep_req_id(),"DEP_REQ_ID"); plant_data_x.setValue("X","DEP_REQ_ID"); // ABCIN 1 周期盘点的盘点标识 plant_data.setValue(entity.getCc_ph_inv(),"CC_PH_INV"); plant_data_x.setValue("X","CC_PH_INV"); // FEVOR 3 Production scheduler 生产调度员 plant_data.setValue(entity.getProduction_scheduler(),"PRODUCTION_SCHEDULER"); plant_data_x.setValue("X","PRODUCTION_SCHEDULER"); // 评估范围 和 工厂 一致 valuation_data.setValue(entity.getPlant(),"VAL_AREA"); valuation_data_x.setValue(entity.getPlant(),"VAL_AREA"); // VPRSV 1 价格控制 valuation_data.setValue(entity.getPrice_ctrl(),"PRICE_CTRL"); valuation_data_x.setValue("X","PRICE_CTRL"); // STPRS 23 .4 Standard Price 标准价格 valuation_data.setValue(entity.getStd_price(),"STD_PRICE"); valuation_data_x.setValue("X","STD_PRICE"); // VERPR 23 .4 移动平均价 valuation_data.setValue(entity.getMoving_pr(),"MOVING_PR"); valuation_data_x.setValue("X","MOVING_PR"); // PEINH 5 .0 Per. unit price 价格的单位量 valuation_data.setValue(entity.getPrice_unit(),"PRICE_UNIT"); valuation_data_x.setValue("X","PRICE_UNIT"); // EKLR 1 withQuntityStructure 物料根据数量结构进行成本核算 valuation_data.setValue(entity.getQty_struct(),"QTY_STRUCT"); valuation_data_x.setValue("X","QTY_STRUCT"); // HKMAT 1 物料的源 valuation_data.setValue(entity.getOrig_mat(),"ORIG_MAT"); valuation_data_x.setValue("X","ORIG_MAT"); // LOSGR 13 .3 成本核算批量 plant_data.setValue(entity.getLot_size(),"LOT_SIZE"); plant_data_x.setValue("X","LOT_SIZE"); // SOBSK 2 成本核算特殊采购类型 plant_data.setValue(entity.getSpecprocty(),"SPECPROCTY"); plant_data_x.setValue("X","SPECPROCTY"); // XCHPF 1 批次管理需求的标识 client_data.setValue(entity.getBatch_mgmt(),"BATCH_MGMT"); client_data_x.setValue("X","BATCH_MGMT"); if (sales_data.getValue("SALES_ORG").toString() == "") { sales_data.clear(); sales_data_x.clear(); } //执行函数 msgType=message=""; myConnection.execute(saveData); msgType = return_data.getValue("TYPE").toString(); message = return_data.getValue("MESSAGE").toString(); return ( msgType == "" || msgType == "S" ) ; } return false; } }