一、利用此BAPI生成预制凭证,首先要在如下用户出口做一个简单增强:
ACBAPI01 会计核算: 对 BAPI 界面的客户增强功能
对应的出口函数是:EXIT_SAPLACC4_001
相应的代码如下,核心内容是要让凭证抬头中的status_new值为2,即可生成预制凭证:
READ TABLE extension WITH KEY field1 = 'BAPI-PARK'.
IF sy-subrc = 0 .
MOVE 2 TO t_acchd-status_new. "to mark this is Park Document
ENDIF.
*此部分内容为BAPI标准参数中没有的字段,四个Field字段都是char250,如果字段太多,
可利用处理字符串拆分的方法进行
LOOP AT extension.
READ TABLE t_accit WITH KEY posnr = extension-field1.
IF sy-subrc = 0.
t_accit-bschl = extension-field2. "记帐码
t_accit-rstgr = extension-field3. "原因代码
t_accit-umskz = extension-field4. "特殊总帐标识
MODIFY t_accit INDEX sy-tabix.
ENDIF.
*在自己的程序或接口代码中调用BAPI,测试程序如下:
*&---------------------------------------------------------------------*
*& Report ZT_TEST_PARK_DOCUMENT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zt_test_park_document1 NO STANDARD PAGE HEADING.
TABLES: bkpf,
bseg,
bsec,
bset,
bsez.
DATA: header LIKE bapiache08,
accountgl LIKE STANDARD TABLE OF bapiacgl08 WITH HEADER LINE,
currencyamount LIKE STANDARD TABLE OF bapiaccr08 WITH HEADER LINE,
extension1 LIKE STANDARD TABLE OF bapiextc WITH HEADER LINE,
return LIKE STANDARD TABLE OF bapiret2 WITH HEADER LINE.
DATA: loc_cnt TYPE posnr_acc,
loc_amt TYPE bapiwrbtr.
DATA: obj_key LIKE bapiache02-obj_key,
obj_type LIKE bapiache02-obj_type,
obj_sys LIKE bapiache02-obj_sys.
START-OF-SELECTION.
header-username = sy-uname.
header-comp_code = '1200'.
header-fisc_year = sy-datum+0(4).
header-doc_date = sy-datum.
header-pstng_date = sy-datum.
header-fis_period = sy-datum+4(2).
header-doc_type = 'SA'.
accountgl-itemno_acc = '1'.
accountgl-gl_account = '6602070000 ' .
accountgl-costcenter = '1200AA01'.
accountgl-item_text = 'Test Bapi SGTXT'.
accountgl-alloc_nmbr = 'Test Bapi ZUONR'.
APPEND accountgl.
CLEAR accountgl.
extension1-field1 = '1'."Item No
extension1-field2 = '40'."记帐码
APPEND extension1.
loc_cnt = 1.
loc_cnt = loc_cnt + 1.
accountgl-itemno_acc = loc_cnt.
accountgl-gl_account = '1001010000' .
* accountgl-comp_code = '1200'.
* accountgl-pstng_date = sy-datum.
* accountgl-doc_type = 'SA'.
* accountgl-fisc_year = sy-datum+0(4).
* accountgl-fis_period = sy-datum+4(2).
* accountgl-costcenter = ‘13100’.
accountgl-item_text = 'Test Bapi SGTXT'.
accountgl-alloc_nmbr = 'Test Bapi ZUONR'.
APPEND accountgl.
CLEAR accountgl.
extension1-field1 = '2'. "Item No
extension1-field2 = '50'. "记帐码
extension1-field3 = '999'."原因代码
APPEND extension1.
loc_amt = 1111.
currencyamount-itemno_acc = '1'.
currencyamount-currency = 'CNY'.
currencyamount-amt_doccur = loc_amt. "( For Debit / ‘S’)
APPEND currencyamount.
CLEAR currencyamount.
currencyamount-itemno_acc = '2'.
currencyamount-currency = 'CNY'.
currencyamount-amt_doccur = ( -1 ) * ( loc_amt ). " ( For credit /‘H’)
APPEND currencyamount.
CLEAR currencyamount.
extension1-field1 = 'BAPI-PARK'.
APPEND extension1.
CALL FUNCTION 'BAPI_ACC_GL_POSTING_POST'
EXPORTING
documentheader = header
IMPORTING
obj_type = obj_type
obj_key = obj_key
obj_sys = obj_sys
TABLES
accountgl = accountgl
currencyamount = currencyamount
return = return
extension1 = extension1.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
二、前一个BAPI只能生成总帐类的预制凭证,有一定局限性,如果涉及AP、AR时应该用以此BAPI来生成预制凭证。
用此BAPI生成预制凭证时,应先处理如下BADI.
ACC_DOCUMENT在调用科目接口前更改外部凭证(利用T-code:SE18处理)
在BAPI的Change方法中增加如下代码:
DATA:ls_exte TYPE bapiparex,
ls_accit TYPE accit,
l_string TYPE string.
FIELD-SYMBOLS:<fs_posnr> TYPE ANY,
<fs_field> TYPE ANY,
<fs_value1> TYPE ANY,
<fs_value2> TYPE ANY.
READ TABLE c_extension2 INTO ls_exte WITH KEY structure = 'PARK'.
IF sy-subrc = 0.
MOVE '2' TO c_acchd-status_new.
DELETE c_extension2 INDEX sy-tabix.
ENDIF.
LOOP AT c_extension2 INTO ls_exte.
ASSIGN COMPONENT 1 OF STRUCTURE ls_exte TO <fs_field>.
ASSIGN COMPONENT 2 OF STRUCTURE ls_exte TO <fs_posnr>.
ASSIGN COMPONENT 3 OF STRUCTURE ls_exte TO <fs_value1>.
READ TABLE c_accit INTO ls_accit WITH KEY posnr = <fs_posnr>.
IF sy-subrc IS INITIAL.
CONCATENATE 'LS_ACCIT-' ls_exte-structure INTO l_string.
ASSIGN (l_string) TO <fs_value2>.
<fs_value2> = <fs_value1>.
MODIFY c_accit FROM ls_accit INDEX sy-tabix.
ENDIF.
ENDLOOP.
该段代码分两个部分,前一部分是为了生成预制凭证用的,后一部分
是针对BAPI标准参数中没有的字段。
调用BAPI的代码如下:
call function 'BAPI_ACC_DOCUMENT_POST'
exporting
documentheader = header
importing
obj_type = obj_type
obj_key = obj_key
obj_sys = obj_sys
tables
accountgl = accountgl
accountreceivable = accountreceivable
accountpayable = accountpayable
currencyamount = currencyamount
return = return
extension2 = extension2.
read table return with key type = 'S'
id = 'RW'
number = '605'.
if sy-subrc = 0.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = 'X'.
endif.
备注:如果要生成预制凭证,一定要在表参数extension2中
增加一条特殊记录,例如可以如下:
extension2-structure = 'PARK'.
extension2-valuepart1 = '1'.
extension2-valuepart2 = 'PARK'.
append extension2.
clear extension2.