如开发基于SAP银行的回单进行企业现金流量表的预测功能
promot :查询SAP中基于银行回单的现金流量预测 ABAP代码
REPORT z_cash_flow_forecast.* 获取当前日期
DATA: date TYPE dats.
date = sy-datum.* 获取最近6个月的银行回单
SELECT febep~belnr, febep~budat, febep~xblnr, febep~lifnr, febep~werks,febep~bukrs, febep~kostl, febep~prctr, febep~hkont, febep~shkzg,febep~dmbtr, bseg~sgtxtINTO TABLE @DATA(receipts)FROM febepINNER JOIN bseg ON febep~belnr = bseg~belnr AND febep~ebeln = bseg~ebelnWHERE febep~budat >= date - 180AND febep~shkzg = 'H'AND febep~dmbtr <> 0.* 按月份和公司代码分组,并计算每个月的现金流量
SELECT RIGHT(febep~budat, 6) AS month,febep~bukrs,SUM(febep~dmbtr) AS amountINTO TABLE @DATA(cash_flows)FROM febepWHERE febep~budat >= date - 180AND febep~shkzg = 'H'AND febep~dmbtr <> 0GROUP BY month, febep~bukrs.* 获取公司代码描述
SELECT bukrs, butxtINTO TABLE @DATA(company_codes)FROM t001.* 计算现金流量预测
TYPES: BEGIN OF cash_flow_forecast,month TYPE char6,bukrs TYPE bukrs,days TYPE i,amount TYPE dmbtr,END OF cash_flow_forecast.DATA: cash_flow_forecasts TYPE TABLE OF cash_flow_forecast WITH HEADER LINE.LOOP AT company_codes INTO DATA(company_code).DO 3 TIMES.DATA: days TYPE i,forecast_date TYPE dats,forecast_month TYPE char6,forecast_amount TYPE dmbtr.CASE sy-index.WHEN 1.days = 60.WHEN 2.days = 120.WHEN 3.days = 240.ENDCASE.forecast_date = date + days.forecast_month = RIGHT( sy-datum + days, 6 ).SELECT SUM(febep~dmbtr)INTO @forecast_amountFROM febepWHERE febep~bukrs = company_code-bukrsAND febep~budat BETWEEN date AND forecast_dateAND febep~shkzg = 'H'AND febep~dmbtr <> 0.cash_flow_forecasts-month = forecast_month.cash_flow_forecasts-bukrs = company_code-bukrs.cash_flow_forecasts-days = days.cash_flow_forecasts-amount = forecast_amount.APPEND cash_flow_forecasts.ENDDO.
ENDLOOP.* 输出结果
WRITE: / '月份', 20 '公司代码', 30 '预测天数', 40 '现金流量'.
假设此业务所有的现金都基于银行回单设计,
此代码将基于SAP FEBEP银行回单表进行拆解,现金部分 批量上传导入银行回单表;
将ABAP代码转为 java类( 如netweaver代码实现方式)
import java.sql.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;public class CashFlowForecast {private static final String URL = "jdbc:sap://host:port/?autocommit=false&reconnect=true";private static final String USERNAME = "username";private static final String PASSWORD = "password";public static void main(String[] args) {Connection conn = null;Statement stmt = null;ResultSet rs = null;try {conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);stmt = conn.createStatement();// 获取当前日期LocalDate date = LocalDate.now();// 获取最近6个月的银行回单List<Receipt> receipts = new ArrayList<>();String receiptSql = "SELECT febep.belnr, febep.budat, febep.xblnr, febep.lifnr, febep.werks, " +"febep.bukrs, febep.kostl, febep.prctr, febep.hkont, febep.shkzg, febep.dmbtr, bseg.sgtxt " +"FROM febep INNER JOIN bseg ON febep.belnr = bseg.belnr AND febep.ebeln = bseg.ebeln " +"WHERE febep.budat >= '" + date.minusMonths(6) + "' " +"AND febep.shkzg = 'H' " +"AND febep.dmbtr <> 0";rs = stmt.executeQuery(receiptSql);while (rs.next()) {String belnr = rs.getString("belnr");LocalDate budat = rs.getDate("budat").toLocalDate();String xblnr = rs.getString("xblnr");String lifnr = rs.getString("lifnr");String werks = rs.getString("werks");String bukrs = rs.getString("bukrs");String kostl = rs.getString("kostl");String prctr = rs.getString("prctr");String hkont = rs.getString("hkont");String shkzg = rs.getString("shkzg");double dmbtr = rs.getDouble("dmbtr");String sgtxt = rs.getString("sgtxt");receipts.add(new Receipt(belnr, budat, xblnr, lifnr, werks, bukrs, kostl, prctr, hkont, shkzg, dmbtr, sgtxt));}// 按月份和公司代码分组,并计算每个月的现金流量List<CashFlow> cashFlows = new ArrayList<>();String cashFlowSql = "SELECT RIGHT(febep.budat, 6) AS month, febep.bukrs, SUM(febep.dmbtr) AS amount " +"FROM febep " +"WHERE febep.budat >= '" + date.minusMonths(6) + "' " +"AND febep.shkzg = 'H' " +"AND febep.dmbtr <> 0 " +"GROUP BY month, febep.bukrs";rs = stmt.executeQuery(cashFlowSql);while (rs.next()) {String month = rs.getString("month");String bukrs = rs.getString