1. 程式人生 > >BAPI過賬

BAPI過賬

with car test init obj mona uname 項目 .cn

REPORT ZWGX013.


* DATA LR_DOC TYPE REF TO CL_DOCUMENT.
* DATA LV_KEY TYPE BAPIACHE09-OBJ_KEY.
CLASS CL_DOCUMENT DEFINITION.

PUBLIC SECTION.
METHODS SET_HEAD
IMPORTING
HEADER_TXT TYPE BKTXT
COMP_CODE TYPE BUKRS
DOC_DATE TYPE DATS
PSTNG_DATE TYPE DATS
FISC_YEAR TYPE GJAHR
FIS_PERIOD TYPE MONAT.

METHODS ADD_ITEM
IMPORTING
ACCOUNT TYPE HKONT
TEXT TYPE SGTXT
CURRENCY TYPE WAERS
AMONT TYPE BAPIDOCCUR.

METHODS CHECK
RETURNING VALUE(FLAG) TYPE ABAP_BOOL.

METHODS POST
RETURNING VALUE(OBJKEY) TYPE BAPIACHE09-OBJ_KEY.

PRIVATE SECTION.
DATA HEADER TYPE BAPIACHE09."表頭
DATA ITEMS TYPE TABLE OF BAPIACGL09.
DATA ITEM TYPE BAPIACGL09.
DATA CITEMS TYPE TABLE OF BAPIACCR09.
DATA CITEM TYPE BAPIACCR09.
DATA ITEMNO TYPE POSNR_ACC."會計憑證行項目編號
DATA RETURNS TYPE TABLE OF BAPIRET2."BAPIRET2返回參數

ENDCLASS.


CLASS CL_DOCUMENT IMPLEMENTATION.
METHOD SET_HEAD.
HEADER-USERNAME = SY-UNAME.
HEADER-COMP_CODE = COMP_CODE.
HEADER-HEADER_TXT = HEADER_TXT.
HEADER-DOC_DATE = DOC_DATE."DOC_DATE憑證中的憑證日期
HEADER-PSTNG_DATE = PSTNG_DATE."PSTNG_DATE憑證中的過帳日期
HEADER-DOC_TYPE = ‘SA‘."憑證類型
HEADER-FISC_YEAR = FISC_YEAR."財年
HEADER-FIS_PERIOD = FIS_PERIOD."會計期間
ITEMNO = 1.
ENDMETHOD.

METHOD ADD_ITEM.

DATA: ITEM TYPE BAPIACGL09."總賬科項目
DATA: CITEM TYPE BAPIACCR09."貨幣項目

ITEM-ITEMNO_ACC = ITEMNO."ITEMNO_ACC會計憑證行項目編號
ITEM-GL_ACCOUNT = ACCOUNT."GL_ACCOUNT總賬科目
ITEM-ITEM_TEXT = TEXT."ITEM_TEXT項目文本
APPEND ITEM TO ITEMS.
CLEAR ITEM.

CITEM-ITEMNO_ACC = ITEMNO.
CITEM-CURRENCY = CURRENCY.
CITEM-AMT_DOCCUR = AMONT.
APPEND CITEM TO CITEMS.
CLEAR CITEM.

ITEMNO = ITEMNO + 1.
ENDMETHOD.

METHOD CHECK.


CALL FUNCTION ‘BAPI_ACC_DOCUMENT_CHECK‘
EXPORTING
DOCUMENTHEADER = HEADER
* CUSTOMERCPD =
* CONTRACTHEADER =
TABLES
ACCOUNTGL = ITEMS
* ACCOUNTRECEIVABLE =
* ACCOUNTPAYABLE =
* ACCOUNTTAX =
CURRENCYAMOUNT = CITEMS
* CRITERIA =
* VALUEFIELD =
* EXTENSION1 =
RETURN = RETURNS
* PAYMENTCARD =
* CONTRACTITEM =
* EXTENSION2 =
* REALESTATE =
* ACCOUNTWT =
.

READ TABLE RETURNS WITH KEY TYPE = ‘E‘ TRANSPORTING NO FIELDS.

IF SY-SUBRC = 0.
FLAG = ABAP_FALSE.
ELSE.
FLAG = ABAP_TRUE.
ENDIF .
ENDMETHOD.
* DATA LV_KEY TYPE BAPIACHE09-OBJ_KEY.
METHOD POST.
CALL FUNCTION ‘BAPI_ACC_DOCUMENT_POST‘
EXPORTING
DOCUMENTHEADER = HEADER
* CUSTOMERCPD =
* CONTRACTHEADER =
IMPORTING ""沒放開,DUMP
* OBJ_TYPE =
OBJ_KEY = OBJKEY" OBJ_KEY 字段參考關鍵
* OBJ_SYS =
TABLES
ACCOUNTGL = ITEMS
* ACCOUNTRECEIVABLE =
* ACCOUNTPAYABLE =
* ACCOUNTTAX =
CURRENCYAMOUNT = CITEMS
* CRITERIA =
* VALUEFIELD =
* EXTENSION1 =
RETURN = RETURNS
* PAYMENTCARD =
* CONTRACTITEM =
* EXTENSION2 =
* REALESTATE =
* ACCOUNTWT =
.

CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT‘
* EXPORTING
* WAIT =
* IMPORTING
* RETURN =
.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA LR_DOC TYPE REF TO CL_DOCUMENT.
"必須定義在 CREATE OBJECT LR_DOC.之前
DATA LV_KEY TYPE BAPIACHE09-OBJ_KEY.

CREATE OBJECT LR_DOC.

CALL METHOD LR_DOC->SET_HEAD
EXPORTING
HEADER_TXT = ‘TEST OO DOCUMENT POST‘
COMP_CODE = ‘0725‘
DOC_DATE = SY-DATUM
PSTNG_DATE = SY-DATUM
FISC_YEAR = SY-DATUM(4)
FIS_PERIOD = SY-DATUM+4(2).

CALL METHOD LR_DOC->ADD_ITEM
EXPORTING
ACCOUNT = ‘0010010101‘
TEXT = ‘text item 001‘
CURRENCY = ‘CNY‘
AMONT = 5.

CALL METHOD LR_DOC->ADD_ITEM
EXPORTING
ACCOUNT = ‘0010020111‘
TEXT = ‘text item 002‘
CURRENCY = ‘CNY‘
AMONT = 0 - 3.

CALL METHOD LR_DOC->ADD_ITEM
EXPORTING
ACCOUNT = ‘0010020101‘
TEXT = ‘text item 003‘
CURRENCY = ‘CNY‘
AMONT = 0 - 2.

IF LR_DOC->CHECK( ) = ABAP_TRUE.
LV_KEY = LR_DOC->POST( ).
* LV_KEY = LR_DOC->POST( ).
WAIT UP TO 1 SECONDS.
SET PARAMETER ID ‘BLN‘ FIELD LV_KEY(10).
SET PARAMETER ID ‘BUK‘ FIELD LV_KEY+10(4).
SET PARAMETER ID ‘GJR‘ FIELD LV_KEY+14(4).
CALL TRANSACTION ‘FB03‘ AND SKIP FIRST SCREEN.
ENDIF.

技術分享

BAPI過賬