前面几篇博客文章介绍 BAPI_ACC_DOCUMENT_POST
的使用,如果需要用代码完成类似 F-04 过账并清账的操作,可以顺序调用下面的三个函数:
- POSTING_INTERFACE_START
- POSTING_INTERFACE_CLEARING
- POSTING_INTERFACE_END
这三个函数在内部也是调用 BDC,但对于开发者来说,只需要给定参数,简化了编码的工作。
为了便于理解,假定我们需要对下面的会计凭证进行清账:
应付账款是未清项目,现在要对应付账款进行清账。先给出完整的代码,后面再对要点进行解释:
REPORT z_post_clearing.
DEFINE populate_ftpost.
clear ls_ftpost.
ls_ftpost-stype = &1.
ls_ftpost-count = &2.
ls_ftpost-fnam = &3.
ls_ftpost-fval = &4.
append ls_ftpost to lt_ftpost.
END-OF-DEFINITION.
START-OF-SELECTION.
PERFORM frm_post_clearing.
FORM frm_post_clearing.
" ftclear 是清账的数据的表参数
DATA:
lt_ftclear TYPE STANDARD TABLE OF ftclear,
ls_ftclear LIKE LINE OF lt_ftclear.
" ftpost 是过账的抬头和行项目的表参数
DATA:
lt_ftpost TYPE STANDARD TABLE OF ftpost,
ls_ftpost LIKE LINE OF lt_ftpost.
DATA:
lt_blntab LIKE TABLE OF blntab WITH HEADER LINE,
lt_fttax LIKE TABLE OF fttax WITH HEADER LINE.
" populate ftclear table parameter
CLEAR ls_ftclear.
ls_ftclear-agkoa = 'K'. " k for vendor
ls_ftclear-agbuk = 'Z900'.
ls_ftclear-selfd = 'BELNR'.
ls_ftclear-xnops = 'X'.
ls_ftclear-selvon = '1900000001'.
APPEND ls_ftclear TO lt_ftclear.
" populate ftpost table paramter
" 分为header和line item,line item按行填充
" 首先填写header (K)
populate_ftpost 'K' 1 'BKPF-BUKRS' 'Z900'. " company code
populate_ftpost 'K' 1 'BKPF-BLART' 'KA'. " document type
populate_ftpost 'K' 1 'BKPF-BLDAT' sy-datum. " posting date
populate_ftpost 'K' 1 'BKPF-BUDAT' sy-datum.
populate_ftpost 'K' 1 'BKPF-WAERS' 'CNY'. " currency
populate_ftpost 'K' 1 'BKPF-BKTXT' 'Clearing 1900000001'. " header text
" 按行填写行项目的字段 (P)
populate_ftpost 'P' 1 'RF05A-NEWBS' '50'. " posting key
populate_ftpost 'P' 1 'BSEG-HKONT' '10010100'. " G/L account
populate_ftpost 'P' 1 'BSEG-SGTXT' 'Clearing vendor invoice'.
populate_ftpost 'P' 1 'BSEG-WRBTR' '1700'. " amount
CALL FUNCTION 'POSTING_INTERFACE_START'
EXPORTING
i_client = sy-mandt
i_function = 'C'
i_mode = 'N'
i_keep = 'X'
i_update = 'S'
i_user = sy-uname.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'POSTING_INTERFACE_CLEARING'
EXPORTING
i_auglv = 'AUSGZAHL'
i_tcode = 'FB05'
i_sgfunct = 'C'
IMPORTING
e_msgid = sy-msgid
e_msgno = sy-msgno
e_msgty = sy-msgty
e_msgv1 = sy-msgv1
e_msgv2 = sy-msgv2
e_msgv3 = sy-msgv3
e_msgv4 = sy-msgv4
TABLES
t_blntab = lt_blntab
t_ftclear = lt_ftclear
t_ftpost = lt_ftpost
t_fttax = lt_fttax
EXCEPTIONS
clearing_procedure_invalid = 1
clearing_procedure_missing = 2
table_t041a_empty = 3
transaction_code_invalid = 4
amount_format_error = 5
too_many_line_items = 6
company_code_invalid = 7
screen_not_found = 8
no_authorization = 9
OTHERS = 10.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" keep track of document number
READ TABLE lt_blntab INDEX 1.
CALL FUNCTION 'POSTING_INTERFACE_END'
EXCEPTIONS
session_not_processable = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WRITE: lt_blntab-bukrs, lt_blntab-belnr, lt_blntab-gjahr, ' was posted successfully'.
ENDFORM.
POSTING_INTERFACE_START
函数CALL FUNCTION 'POSTING_INTERFACE_START'
EXPORTING
i_client = sy-mandt
i_function = 'C'
i_mode = 'N'
i_keep = 'X'
i_update = 'S'
i_user = sy-uname.
这个函数主要给出 client, user id 以及后续 BDC 的一些参数。注意参数中 i_mode
参数,这个参数的含义与 BDC 模式的含义相同:
POSTING_INTERFACE_CLEARING
函数这个函数式程序处理的主体,调用代码如下:
CALL FUNCTION 'POSTING_INTERFACE_CLEARING'
EXPORTING
i_auglv = 'AUSGZAHL'
i_tcode = 'FB05'
i_sgfunct = 'C'
TABLES
t_blntab = lt_blntab
t_ftclear = lt_ftclear
t_ftpost = lt_ftpost
t_fttax = lt_fttax .
auglv
参数auglv
参数表示清账过程,数据来自 T041A
表,比如上面的 AUSGZAHL
表示付款。
FTCLEAR
表参数FTCLEAR
表参数是要要被清账的数据。对于本篇的会计凭证来说,我们要对应付账款(供应商)进行清账,所以需要填写 FTCLEAR
表参数如下:
" ftclear 是清账的数据的表参数
DATA:
lt_ftclear TYPE STANDARD TABLE OF ftclear,
ls_ftclear LIKE LINE OF lt_ftclear.
" populate ftclear table parameter
CLEAR ls_ftclear.
ls_ftclear-agkoa = 'K'. " k for vendor
ls_ftclear-agbuk = 'Z900'.
ls_ftclear-selfd = 'BELNR'.
ls_ftclear-xnops = 'X'. " No Special G/L items
ls_ftclear-selvon = '1900000001'.
APPEND ls_ftclear TO lt_ftclear.
可以看到,清账以会计凭证号码 1900000001 为基础,这是清账的一种方法而已。
FTPOST
表参数FTPOST
表参数表示要过账的数据,包括 document header 和 line items。对于本次的过账凭证来说,最后生成的会计凭证为:
DR: 应付账款 1700
CR: 现金 (10010100)1700
贷方现金这一行就是过账数据,我们需要将 document header 和现金这一行填写到 PTPOST
参数中,为减少代码量,先定义一个宏。
DEFINE populate_ftpost.
clear ls_ftpost.
ls_ftpost-stype = &1.
ls_ftpost-count = &2.
ls_ftpost-fnam = &3.
ls_ftpost-fval = &4.
append ls_ftpost to lt_ftpost.
END-OF-DEFINITION.
" ftpost 是过账的抬头和行项目的表参数
DATA:
lt_ftpost TYPE STANDARD TABLE OF ftpost,
ls_ftpost LIKE LINE OF lt_ftpost.
" populate ftpost table paramter
" 分为header和line item,line item按行填充
" 首先填写header (K)
populate_ftpost 'K' 1 'BKPF-BUKRS' 'Z900'. " company code
populate_ftpost 'K' 1 'BKPF-BLART' 'KA'. " document type
populate_ftpost 'K' 1 'BKPF-BLDAT' sy-datum. " posting date
populate_ftpost 'K' 1 'BKPF-BUDAT' sy-datum.
populate_ftpost 'K' 1 'BKPF-WAERS' 'CNY'. " currency
populate_ftpost 'K' 1 'BKPF-BKTXT' 'Clearing 1900000001'. " header text
" 按行填写行项目的字段 (P)
populate_ftpost 'P' 1 'RF05A-NEWBS' '50'. " posting key
populate_ftpost 'P' 1 'BSEG-HKONT' '10010100'. " G/L account
populate_ftpost 'P' 1 'BSEG-SGTXT' 'Clearing vendor invoice'.
populate_ftpost 'P' 1 'BSEG-WRBTR' '1700'. " amount
在填充参数的时候,ftpost-stype
为 K 表示 document header, P 表示 行项目。另外,我们可以看到,在一张凭证中 ftpost-count
用于关联 document header 和 line item。
blntab
表参数blntab
表参数用于记录生成的会计凭证号码信息:
DATA: lt_blntab LIKE TABLE OF blntab WITH HEADER LINE.
" keep track of document number
READ TABLE lt_blntab INDEX 1.
WRITE: lt_blntab-bukrs, lt_blntab-belnr, lt_blntab-gjahr, ' was posted successfully'.
如果 clearing 函数成功,调用函数 POSTING_INTERFACE_END
call function 'POSTING_INTERFACE_END'
exporting
i_bdcimmed = 'X'
exceptions
session_not_processable = 1
others = 2.