OA系统调用此接口,传输费用报销流程的凭证信息到SAP,生成费用报销类型SAP凭证。
调用标准的BABI方法实现。
1、首先先介绍一下实现会计凭证生成的BAPI,参考链接。
2、增强操作在另一篇文章(SAP接口 财务凭证集成_借款)在此不再赘述。
3、数据导入和导出,此流程生成的凭证存在多借多贷的情况,所以需要定义两个结构来接收相关参数。
4、代码实现
FUNCTION ZFIIP018 .
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(IV_BUKRS) TYPE BUKRS OPTIONAL
*" VALUE(IV_RSTGR) TYPE RSTGR OPTIONAL
*" VALUE(IV_LIFNR) TYPE LIFNR OPTIONAL
*" VALUE(IV_WAERS) TYPE WAERS OPTIONAL
*" VALUE(IV_BLART) TYPE BLART OPTIONAL
*" VALUE(IV_BSCHLH) TYPE BSCHL OPTIONAL
*" VALUE(IV_SHBKZK) TYPE UMSKZ OPTIONAL
*" VALUE(IV_RACCTH) TYPE RACCT OPTIONAL
*" VALUE(IV_BUDAT) TYPE BUDAT OPTIONAL
*" VALUE(IV_AC_AMOUNT) TYPE WRBTR OPTIONAL
*" VALUE(IV_DT_AMOUNT) TYPE WRBTR OPTIONAL
*" VALUE(IV_WRBTR) TYPE WRBTR OPTIONAL
*" VALUE(IV_KOSTL) TYPE KOSTL OPTIONAL
*" VALUE(IV_PRCTR) TYPE PRCTR OPTIONAL
*" VALUE(IV_TYPE) TYPE CHAR1 OPTIONAL
*" VALUE(IV_NAME) TYPE USNAM OPTIONAL
*" VALUE(IV_BXTXTH) TYPE TEXT34 OPTIONAL
*" VALUE(IV_USNAM) TYPE USNAM OPTIONAL
*" EXPORTING
*" VALUE(EV_MSGTY) TYPE BAPI_MTYPE
*" VALUE(EV_MESSAGE) TYPE BAPI_MSG
*" VALUE(EV_MESBELNR) TYPE BAPI_MSG
*" TABLES
*" IV_INPUT STRUCTURE ZSFIIP015 OPTIONAL
*" IV_INRSTGR STRUCTURE ZSFIIP018
*"----------------------------------------------------------------------
*凭证生成规则:
*" VALUE(IV_BUKRS) 公司代码 固定值
*" VALUE(IV_RSTGR) 原因代码 选择
*" VALUE(IV_LIFNR) 会计科目-借 A:无;B:员工供应商编码;C:无
*" VALUE(IV_WAERS) 货币码 默认CNY,可选择修改
*" VALUE(IV_BLART) 凭证类型 默认SA,可选择修改
*" VALUE(IV_BSCHLH) 记账码-贷 A:50;B:50(银行付款时);31(抵扣借支时);;C:50(银行付款时);特别总账标识不为空:39,否则为:31(供应商往来时)
*" VALUE(IV_SHBKZK) 特别总账标识-贷 A:无;B:无;C:记账码-贷=39时,OA传值【特别总账标识】,必选,默认A;记账码-贷=31时,无
*" VALUE(IV_RACCTH) 会计科目-贷 A:付款银行编码;B:付款银行编码 / 员工供应商编码;C:付款银行编码 / 供应商编码;
*" VALUE(IV_BUDAT) 过账日期 出纳付款日期
*" VALUE(IV_AC_AMOUNT) 预付款 A:无;B:无;C:预付款金额
*" VALUE(IV_DT_AMOUNT) 抵扣金额 A:无;B:抵扣金额;C:无
*" VALUE(IV_WRBTR) 金额 应付款
*" VALUE(IV_KOSTL) 成本中心 传值
*" VALUE(IV_PRCTR) 利润中心 固定值
*" VALUE(IV_TYPE) 业务类型 固定值:A:费用报销,B;个人借支报销;C:供应商往来报销
*" VALUE(IV_NAME) 费用归属人姓名
*" VALUE(IV_BXTXTH) 报销内容-贷 限定17个汉字以内
*" VALUE(IV_USNAM) 输入者
*" SGTXT 行项目文本 费用归属人姓名+“报销”+费用说明
*" BKTXT 凭证抬头文本 费用归属人姓名+“报销”
*" BSCHLS 40
*"TABLES:
*"IV_INPUT(ABC)
*:RACCTS 会计科目-借
*:BXTXT 采购内容
*:WRBTR 金额
*:KOSTL 成本中心
*"IV_INRSTGR
*:RSTGR 原因代码-贷
*:WRBTR 金额
*:BXTXT 采购内容
*"----------------------------------------------------------------------
TYPES: BEGIN OF ty_data.
INCLUDE TYPE ZSFIIP015.
TYPES: END OF ty_data.
TYPES: BEGIN OF ty_rstgr."原因代码
INCLUDE TYPE ZSFIIP018.
TYPES: END OF ty_rstgr.
DATA: lw_documentheader TYPE bapiache09, "表头内容
lt_accountgl TYPE STANDARD TABLE OF bapiacgl09, "总帐科目项
lw_accountgl TYPE bapiacgl09, "总帐科目项工作区
lt_accountpayable TYPE STANDARD TABLE OF bapiacap09, "供应商项目
lw_accountpayable TYPE bapiacap09, "供应商项目工作区
* lt_accountreceivable TYPE STANDARD TABLE OF bapiacar09, "客户项目
* lW_accountreceivable TYPE bapiacar09, "客户项目
lt_currencyamount TYPE STANDARD TABLE OF bapiaccr09, "货币项目
lw_currencyamount TYPE bapiaccr09, "货币项目工作区
lt_ext2 TYPE STANDARD TABLE OF bapiparex WITH HEADER LINE,"扩展字段(记账码,原因代码)
lw_ext2 TYPE ZSFIIP019,"扩展字段工作区
* lt_order_info TYPE STANDARD TABLE OF zfit_order_info,
lv_itemno_acc TYPE posnr_acc, "会计凭证行项目号
lt_return TYPE STANDARD TABLE OF bapiret2, "返回参数
lw_return TYPE bapiret2, "返回参数工作区
* ls_no_acc_rule TYPE zfit_no_acc_rule,
* lv_order_no TYPE zfit_order_info-order_no,
lv_key TYPE bapiache09-obj_key,
it_data TYPE TABLE OF ty_data, "借方结构
wa_data TYPE ty_data,
wa_rstgr TYPE ty_rstgr. "付款原因代码
"CONSTANTS: cn_bus_act TYPE glvor VALUE 'RFBU',"交易类型
" cn_status_s TYPE char4 VALUE 'S000',
" cn_status_e TYPE char4 VALUE 'E0M1',
" cn_type_s TYPE c VALUE 'S',
" cn_type_e TYPE c VALUE 'E',
" cn_koart_k TYPE c VALUE 'K',
" cn_koart_d TYPE c VALUE 'D',
" cn_koart_s TYPE c VALUE 'S',
" cn_shkzg_h TYPE c VALUE 'H',
" cn_shkzg_s TYPE c VALUE 'S',
" cn_blart_z5 TYPE bkpf-blart VALUE 'Z5',
" cn_bschl_50 TYPE bschl VALUE '50',
" cn_bschl_40 TYPE bschl VALUE '40'.
DATA: l_itemno TYPE char10 VALUE '0000000000'."行项目
DATA: ls_currencyamount TYPE LINE OF bapiaccr09_tab.
DATA: l_wrbtr_sum TYPE bapidoccur,
l_tax_sum TYPE bapidoccur.
DATA: v_tabix TYPE sy-tabix,
IV_SGTXT TYPE sgtxt,"项目行文本
IV_BKTXT TYPE bktxt."凭证抬头文本.
*IV_BUKRS = '1100'.
*IV_RSTGR = 'B5'.
*IV_WAERS = 'CNY'.
*IV_LIFNR = '600001'.
*IV_BSCHLH = '31'.
*IV_SHBKZK ='A'.
*IV_BLART = 'SA'.
*IV_RACCTH = '1002010106'.
*IV_BUDAT ='20200503'.
*IV_AC_AMOUNT = 3200.
*IV_DT_AMOUNT = 2000.
*IV_WRBTR = 0.
*IV_PRCTR = 'PC1100'.
*IV_TYPE = 'B'.
*IV_NAME = '刘定'.
*IV_BXTXTH = '测试B'.
*IV_USNAM = 'FICO003'.
*
*wa_data-raccts = '8001020100'.
*wa_data-bxtxt = '办公费'.
*wa_data-wrbtr = 1000.
*wa_data-kostl = '11S00202'.
*APPEND wa_data TO IV_INPUT .
*CLEAR wa_data .
***
*WA_DATA-RACCTS = '2221010201'.
*WA_DATA-BXTXT = '材料进项1'.
*WA_DATA-WRBTR = 1000.
*append WA_DATA to iv_input .
*clear WA_DATA .
*
*WA_DATA-RACCTS = '2221010202'.
*WA_DATA-BXTXT = '材料进项2'.
*WA_DATA-WRBTR = 6000.
*append WA_DATA to iv_input .
*clear WA_DATA .
IF IV_TYPE ='A'.
IV_AC_AMOUNT = 0."预付款
IV_DT_AMOUNT = 0."抵扣金额
ELSEIF IV_TYPE ='B'.
IV_AC_AMOUNT = 0.
ELSE.
IV_DT_AMOUNT = 0.
ENDIF.
IF IV_BSCHLH <> '39'.
CLEAR IV_SHBKZK.
ENDIF.
*加上p_in的前导零
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = IV_LIFNR
IMPORTING
output = IV_LIFNR.
IF IV_BUDAT IS INITIAL.
IV_BUDAT = SY-DATUM.
ENDIF.
IF IV_NAME IS NOT INITIAL.
CONCATENATE IV_NAME '报销' IV_BXTXTH INTO IV_BKTXT.
ENDIF.
*->填充总账信息
DATA ls_accountgl TYPE bapiacgl09.
* 分配字段写入“收付款单号+行项目号”
* 抬头信息
lw_documentheader-doc_date = IV_BUDAT."凭证中的凭证日期
lw_documentheader-pstng_date = IV_BUDAT."凭证中的记账日期
lw_documentheader-fisc_year = IV_BUDAT+0(4)."记账财年
lw_documentheader-fis_period = IV_BUDAT+4(2)."记账月份
lw_documentheader-doc_type = IV_BLART."凭证类型
lw_documentheader-comp_code = IV_BUKRS."公司代码
lw_documentheader-header_txt = IV_BKTXT."凭证抬头
* lw_documentheader-bus_act = cn_bus_act. "'RFBU'. “交易类型
lw_documentheader-username = IV_USNAM."输入者
*********************---借方---*****************************
LOOP AT IV_INPUT INTO wa_data."循环科目号(借)结构
IF wa_data-raccts CP '22210102*'. "判断是否为进项税科目号
IV_SGTXT = wa_data-bxtxt.
ELSE.
CONCATENATE IV_NAME '报销' wa_data-bxtxt INTO IV_SGTXT.
ENDIF.
ADD 1 TO l_itemno.
"总账相关的行项目信息
lw_accountgl-itemno_acc = l_itemno . "凭证行项目
lw_accountgl-gl_account = wa_data-raccts. "会计科目(借)
lw_accountgl-item_text = IV_SGTXT. "项目行文本
lw_accountgl-fis_period = IV_BUDAT+4(2). "记账月份
lw_accountgl-pstng_date = IV_BUDAT. "凭证中的记账日期
lw_accountgl-value_date = IV_BUDAT. "起息日
lw_accountgl-costcenter = wa_data-kostl . "成本中心
lw_accountgl-profit_ctr = IV_PRCTR . "利润中心
APPEND lw_accountgl TO lt_accountgl .
CLEAR lw_accountgl .
"货币金额
lw_currencyamount-itemno_acc = l_itemno."凭证行项目
lw_currencyamount-currency = IV_WAERS."货币码
lw_currencyamount-amt_doccur = wa_data-wrbtr."金额
APPEND lw_currencyamount TO lt_currencyamount.
CLEAR:lw_currencyamount.
"----------- extension2 --------------------
"记账码&原因代码
lw_ext2-posnr = l_itemno. "凭证行项目
lw_ext2-bschl = '40'. "记账码(自动判断记账码)
* lw_ext2-rstgr = IV_RSTGR. "凭证行项目原因代码
IF lw_ext2 IS NOT INITIAL .
lt_ext2-structure = 'ZSFIIP019'.
lt_ext2-valuepart1 = lw_ext2.
APPEND lt_ext2 TO lt_ext2.
CLEAR lt_ext2.
CLEAR lw_ext2.
ENDIF.
CLEAR wa_data.
ENDLOOP.
******************---贷方---*************************
IF IV_TYPE ='B' AND IV_DT_AMOUNT <> 0."抵扣金额
IV_DT_AMOUNT = - IV_DT_AMOUNT.
ADD 1 TO l_itemno.
"供应商
lw_accountpayable-itemno_acc = l_itemno. "凭证行项目
lw_accountpayable-vendor_no = IV_LIFNR."员工供应商编码
lw_accountpayable-bline_date = IV_BUDAT."到期日
lw_accountpayable-item_text = IV_BKTXT."项目行文本(同凭证抬头)
APPEND lw_accountpayable TO lt_accountpayable.
CLEAR lw_accountpayable.
"货币金额
lw_currencyamount-itemno_acc = l_itemno."凭证行项目
lw_currencyamount-currency = IV_WAERS."货币码
lw_currencyamount-amt_doccur = IV_DT_AMOUNT."抵扣金额
APPEND lw_currencyamount TO lt_currencyamount.
CLEAR:lw_currencyamount.
"----------- extension2 --------------------
"记账码&原因代码
lw_ext2-posnr = l_itemno. "凭证行项目
lw_ext2-bschl = '31'. "记账码
IF lw_ext2 IS NOT INITIAL .
lt_ext2-structure = 'ZSFIIP019'.
lt_ext2-valuepart1 = lw_ext2.
APPEND lt_ext2 TO lt_ext2.
CLEAR lt_ext2.
CLEAR lw_ext2.
ENDIF.
ENDIF.
IF IV_TYPE ='C' AND IV_AC_AMOUNT <> 0."预付款金额
IV_AC_AMOUNT = - IV_AC_AMOUNT.
ADD 1 TO l_itemno.
"供应商
lw_accountpayable-itemno_acc = l_itemno. "凭证行项目
lw_accountpayable-vendor_no = IV_LIFNR."员工供应商编码
IF IV_SHBKZK IS NOT INITIAL.
lw_accountpayable-sp_gl_ind = IV_SHBKZK."特别总账标识
ENDIF.
lw_accountpayable-bline_date = IV_BUDAT."到期日
lw_accountpayable-item_text = IV_BKTXT."项目行文本(同凭证抬头)
APPEND lw_accountpayable TO lt_accountpayable.
CLEAR lw_accountpayable.
"货币金额
lw_currencyamount-itemno_acc = l_itemno."凭证行项目
lw_currencyamount-currency = IV_WAERS."货币码
lw_currencyamount-amt_doccur = IV_AC_AMOUNT."抵扣金额
APPEND lw_currencyamount TO lt_currencyamount.
CLEAR:lw_currencyamount.
"----------- extension2 --------------------
"记账码&原因代码
lw_ext2-posnr = l_itemno. "凭证行项目
IF IV_SHBKZK IS NOT INITIAL.
lw_ext2-bschl = '39'. "记账码
ELSE.
lw_ext2-bschl = '31'. "记账码
ENDIF.
IF lw_ext2 IS NOT INITIAL .
lt_ext2-structure = 'ZSFIIP019'.
lt_ext2-valuepart1 = lw_ext2.
APPEND lt_ext2 TO lt_ext2.
CLEAR lt_ext2.
CLEAR lw_ext2.
ENDIF.
ENDIF.
*IF IV_SHBKZK IS NOT INITIAL."特别总账标识
* IV_WRBTR = - IV_WRBTR.
* ADD 1 TO l_itemno.
* "供应商
* lw_accountpayable-itemno_acc = l_itemno. "凭证行项目
* lw_accountpayable-vendor_no = IV_LIFNR."员工供应商编码
*
* lw_accountpayable-bline_date = SY-DATUM.
* lw_accountpayable-item_text = IV_BKTXT."项目行文本(同凭证抬头)
* APPEND lw_accountpayable TO lt_accountpayable.
* CLEAR lw_accountpayable.
* "货币金额
* lw_currencyamount-itemno_acc = l_itemno."凭证行项目
* lw_currencyamount-currency = IV_WAERS."货币码
* lw_currencyamount-amt_doccur = IV_WRBTR."应付款金额
* APPEND lw_currencyamount TO lt_currencyamount.
* CLEAR:lw_currencyamount.
* "----------- extension2 --------------------
* "记账码&原因代码
* lw_ext2-posnr = l_itemno. "凭证行项目
* lw_ext2-bschl = '39'. "记账码
* IF lw_ext2 IS NOT INITIAL .
* lt_ext2-structure = 'ZSFIIP019'.
* lt_ext2-valuepart1 = lw_ext2.
* APPEND lt_ext2 TO lt_ext2.
* CLEAR lt_ext2.
* CLEAR lw_ext2.
* ENDIF.
* ENDIF.
IF IV_INRSTGR[] IS NOT INITIAL .
LOOP AT IV_INRSTGR INTO wa_rstgr."循环科目号(借)结构
*IF IV_WRBTR <> 0."应付款是否为零
CONCATENATE IV_NAME '报销' wa_rstgr-bxtxt INTO IV_SGTXT.
ADD 1 TO l_itemno.
"总账相关的行项目信息
lw_accountgl-itemno_acc = l_itemno. "凭证行项目
lw_accountgl-gl_account = IV_RACCTH. "会计科目(贷)
lw_accountgl-item_text = IV_SGTXT. "项目行文本(同凭证抬头)
APPEND lw_accountgl TO lt_accountgl .
CLEAR lw_accountgl .
"货币金额
lw_currencyamount-itemno_acc = l_itemno."凭证行项目
lw_currencyamount-currency = IV_WAERS."货币码
lw_currencyamount-amt_doccur = - wa_rstgr-WRBTR.."应付款金额
APPEND lw_currencyamount TO lt_currencyamount.
CLEAR:lw_currencyamount.
"----------- extension2 --------------------
"记账码&原因代码
lw_ext2-posnr = l_itemno. "凭证行项目
lw_ext2-bschl = '50'. "记账码
lw_ext2-rstgr = wa_rstgr-RSTGR. "凭证行项目原因代码
IF lw_ext2 IS NOT INITIAL .
lt_ext2-structure = 'ZSFIIP019'.
lt_ext2-valuepart1 = lw_ext2.
APPEND lt_ext2 TO lt_ext2.
CLEAR lt_ext2.
CLEAR lw_ext2.
ENDIF.
ENDLOOP.
ENDIF.
* ENDIF.
* 调用即可:check 可以防止跳号
CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
EXPORTING
documentheader = lw_documentheader
TABLES
accountgl = lt_accountgl[]
currencyamount = lt_currencyamount[]
return = lt_return[].
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = lw_documentheader
IMPORTING
* OBJ_TYPE =
obj_key = lv_key
* OBJ_SYS =
TABLES
accountgl = lt_accountgl[]
* accountreceivable = lt_accountreceivable[]
accountpayable = lt_accountpayable[]
currencyamount = lt_currencyamount[]
return = lt_return[]
extension2 = lt_ext2[].
READ TABLE lt_return INTO lw_return INDEX 1.
IF lw_return-type = 'S' .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
EV_MESBELNR = lv_key+0(10).
* os_key-bukrs = lv_key+10(4).
* os_key-gjahr = lv_key+14(4).
ev_msgty = 'S'.
EV_MESSAGE = lw_return-message.
ELSE.
LOOP AT lt_return INTO lw_return WHERE type = 'E'.
EV_MSGTY = 'E'.
IF EV_MESSAGE IS INITIAL.
EV_MESSAGE = lw_return-message.
ELSE.
CONCATENATE EV_MESSAGE '/' lw_return-message INTO EV_MESSAGE.
ENDIF.
ENDLOOP.
ENDIF.
CLEAR:l_itemno,
lt_accountgl[],
lt_accountpayable[],
lt_currencyamount[],
lt_return[],
lt_ext2[].
ENDFUNCTION.