天天看点

SAP通过JCO调用RFC

public void addVoucherFormSap()  throws Exception{

String startDate=DateUtil.convertDateToString(getDate(new Date(),-7), "yyyy-MM-dd");

String endDate=DateUtil.convertDateToString(getDate(new Date(),1), "yyyy-MM-dd");

//查找目前时间段内已对账的sap凭证,防止出现重复的问题

HQLInfo hqlInfo=new HQLInfo();

StringBuilder whereBlock=new StringBuilder();

whereBlock.append(" fsVoucherSapCheck.fdSapDate<=:endDate and fsVoucherSapCheck.fdSapDate>=:startDate");

hqlInfo.setWhereBlock(whereBlock.toString());

hqlInfo.setParameter("endDate", endDate);

hqlInfo.setParameter("startDate", startDate);

List<FsVoucherSapCheck> resultList=fsVoucherSapCheckService.findList(hqlInfo);

Map<String,FsVoucherSapCheck> valMap=new HashMap<String,FsVoucherSapCheck>();

for (FsVoucherSapCheck fsVoucherSapCheck : resultList) {

valMap.put(fsVoucherSapCheck.getFdSapNumber()+fsVoucherSapCheck.getFdNo(), fsVoucherSapCheck);

}

sapMainConnection = SAPMainVoucherGenerator.getInstance().getConnection();  //获取sap链接

fsVoucherSapCheckService.addVoucher(sapMainConnection,startDate,endDate,valMap,"main");  //获取rfc接口数据

sapOtherConnection = SAPOtherVoucherGenerator.getInstance().getConnection();  

fsVoucherSapCheckService.addVoucher(sapOtherConnection,startDate,endDate,valMap,"other");

}

SAPMainVoucherGenerator.java

public class SAPMainVoucherGenerator implements Serializable {

static private SAPMainVoucherGenerator instance;

static public SAPMainVoucherGenerator getInstance() {

if (instance == null) {

instance = new SAPMainVoucherGenerator();

}

return instance;

}

public JCO.Client connection = null;

public JCO.Client getConnection() throws Exception {

try {

if(connection==null){

JcoMainPool.getInstance();

connection = JCO.getClient(JcoMainPool.POOL_MAIN_NAME);

}

} catch (Exception ex) {

ex.printStackTrace();

throw new Exception("取得SAP连接出错!" + ex.getMessage());

}

return connection;

}

private SAPMainVoucherGenerator() {

}

}

JcoMainPool.java

public class JcoMainPool {

public static final String POOL_MAIN_NAME = "SAPMainPool";  //连接池名称,自定义

private static JcoMainPool instance;

private final Log logger = LogFactory.getLog(JcoMainPool.class);

static public JcoMainPool getInstance(){

if(instance == null){

instance = new JcoMainPool();

}

return instance;

}

private JcoMainPool() {

init();

}

private void init() {

try {

// 获取SAP连接配置信息

IFsVoucherRfcConfigService fsVoucherRfcConfigService = (IFsVoucherRfcConfigService) SpringBeanUtil

.getBean("fsVoucherRfcConfigService");

List<FsVoucherRfcConfig> rfcConfigs = fsVoucherRfcConfigService.findList(null,null);

if (ArrayUtil.isEmpty(rfcConfigs)) {

throw new KmssRuntimeException(new KmssMessage("系统未找到相关SAP连接配置信息!"));

}

FsVoucherRfcConfig rfcConfig =(FsVoucherRfcConfig) rfcConfigs.get(0);

JCO.Pool mainPool = JCO.getClientPoolManager().getPool(POOL_MAIN_NAME);

if (mainPool == null) {

int max = 10;

Properties props = new Properties();

props.put("jco.client.client", rfcConfig.getFdMainCode());  //集团代码

props.put("jco.client.user", rfcConfig.getFdSapUserName());   //访问SAP用户名

props.put("jco.client.passwd", rfcConfig.getFdSapPassword()); //访问SAP密码

props.put("jco.client.lang", "ZH");   //设置中文

props.put("jco.client.ashost", rfcConfig.getFdMainIp());  //设置IP

props.put("jco.client.sysnr", rfcConfig.getFdSapCode());

JCO.addClientPool(POOL_MAIN_NAME, max, props);  //初始化连接池

}

} catch (Exception ex) {

ex.printStackTrace();

logger.error(ex);

}

}

}

fsVoucherSapCheckService.java

 JCO.Repository sapMainRepository;

    JCO.Repository sapOtherRepository;

public void addVoucher(Client sapConnection,String starDate,String endDate,Map<String,

FsVoucherSapCheck> valMap,String type)  throws Exception{    

try {

  JCO.Function function = null;    

if("main".equals(type)){

sapMainRepository = new JCO.Repository("BUOA", sapConnection);

// 创建SAP函数实例

   function = this.createMainFunction("ZFI_DOCUMENT_READ");  //凭证信息表

}else{

sapOtherRepository = new JCO.Repository("BUOA", sapConnection);

// 创建SAP函数实例

   function = this.createOtherFunction("ZFI_DOCUMENT_READ");  //凭证信息表

}

   if (function == null) {

    logger.error("ZFI_DOCUMENT_READ"+ " not found in SAP.");

   }        

// 输入参数列表

JCO.ParameterList parameterList = function.getImportParameterList();

// 输入结构赋值

JCO.Structure paramStructure = parameterList.getStructure("SYS_INFO");  //系统信息,此处位BUOA

paramStructure.setValue("BUOA","SOURCE_SYS_ID");

// 获取表

ParameterList tableParameterList = function.getTableParameterList();  //获取表信息,可为参数表,返回表也是如此获取

Table dateRange = tableParameterList.getTable("RNG_BUDAT");  //会计凭证日期参数表

dateRange.appendRow();

dateRange.setRow(0);  //多行继续dateRange.setRow(1);

dateRange.setValue("I","SIGN");

dateRange.setValue("BT","OPTION");

dateRange.setValue(starDate,"LOW");

dateRange.setValue(endDate,"HIGH");//获取20170101-目前buoa凭证

// client 执行函数

sapConnection.execute(function);

Map<String,JSONObject> mainMap=new HashMap<String,JSONObject>();

// 获取表

Table mainTab = tableParameterList.getTable("T_BKPF");

for (int i = 0; i < mainTab.getNumRows(); i++) {

mainTab.setRow(i); // 设置游标

String sapNumber=mainTab.getString("BELNR");  //会计凭证号

if(StringUtil.isNotNull(sapNumber)&&!mainMap.containsKey(sapNumber)){

String sapDate=mainTab.getString("BUDAT");   //过账日期

String voucherDate=mainTab.getString("BLDAT");   //凭证日期

String createDate=mainTab.getString("CPUDT");   //录入日期

String sapUser=mainTab.getString("USNAM");   //用户名

String fdSapCharge=mainTab.getString("STBLG");   //冲销凭证

JSONObject obj=new JSONObject();

obj.put("sapDate", sapDate);

obj.put("voucherDate", voucherDate);

obj.put("createDate", createDate);

obj.put("sapUser", sapUser);

obj.put("fdSapCharge", fdSapCharge);

mainMap.put(sapNumber, obj);

}

}

// 获取表

Table resultTab = tableParameterList.getTable("T_BSEG");

for (int i = 0; i < resultTab.getNumRows(); i++) {

resultTab.setRow(i); // 设置游标

   FsVoucherSapCheck check=new FsVoucherSapCheck();

String fdSapNumber=resultTab.getString("BELNR");

String fdNo=resultTab.getString("BUZEI");

String key=fdSapNumber+fdNo;

   if(valMap.containsKey(key)&&valMap.get(key)!=null){

    check=valMap.get(key);

   }

   check.setFdAccountsCode(resultTab.getString("HKONT"));

   check.setFdChannel(resultTab.getString("AUFNR"));

  check.setFdAsset(resultTab.getString("ANLN1"));

  check.setFdBussArea(resultTab.getString("GSBER"));

  check.setFdCode(resultTab.getString("BUKRS"));

  check.setFdCostCenter(resultTab.getString("KOSTL"));

  check.setFdCustomer(resultTab.getString("KUNNR"));

  check.setFdFlag(resultTab.getString("SHKZG"));

  check.setFdMoney(resultTab.getString("WRBTR")!=null?Double.parseDouble(resultTab.getString("WRBTR")):0.0);

  check.setFdNo(fdNo);

  check.setFdProfitCenter(resultTab.getString("PRCTR"));

  check.setFdSapNumber(fdSapNumber);

  check.setFdSupplier(resultTab.getString("LIFNR"));

  check.setFdyear(resultTab.getString("GJAHR"));

  if(StringUtil.isNotNull(fdSapNumber)&&mainMap.containsKey(fdSapNumber)

  &&mainMap.get(fdSapNumber)!=null){

  JSONObject obj=mainMap.get(fdSapNumber);

  String fdSapDate=obj.get("sapDate")!=null?obj.get("sapDate").toString():null;

  check.setFdSapDate(fdSapDate);  //过账日期

  check.setFdVoucherTime(obj.get("voucherDate")!=null?obj.get("voucherDate").toString():null);  //凭证日期

  check.setFdCreateDate(obj.get("createDate")!=null?obj.get("createDate").toString():null);  //凭证录入时间

  check.setFdperiod(StringUtil.isNotNull(fdSapDate)?fdSapDate.substring(5, 7):"");  //会计期间

check.setFdSapUser(obj.get("sapUser")!=null?obj.get("sapUser").toString():null);  //用户

check.setFdChargeNumber(obj.get("fdSapCharge")!=null?obj.get("fdSapCharge").toString():null);

  }

  check.setFdCheckTime(DateUtil.convertDateToString(new Date(), DateUtil.PATTERN_DATETIME));

  if(valMap.containsKey(key)&&valMap.get(key)!=null){

    this.update(check);

   }else{

    this.add(check);

   }

}

} catch (Exception e) {

throw new Exception(e);

} finally {

//JCO.releaseClient(sapConnection);  //释放链接

}

}

public JCO.Function createMainFunction(String name) throws Exception {

        try {

            IFunctionTemplate ft = sapMainRepository.getFunctionTemplate(name.toUpperCase());

            if (ft == null)

                return null;

            return ft.getFunction();

        } catch (Exception ex) {

            throw new Exception(ex);

        }

    } 

public JCO.Function createOtherFunction(String name) throws Exception {

try {

IFunctionTemplate ft = sapOtherRepository.getFunctionTemplate(name.toUpperCase());

if (ft == null)

return null;

return ft.getFunction();

} catch (Exception ex) {

throw new Exception(ex);

}

}