使用BAPI_ACC_DOCUMENT_POST 与 BADI_ACC_DOCUMENT 创建固定资产凭证
一、有图有真相,先看创建的效果:
二、创建增强结构:
POSNR 1 Types POSNR_ACC NUMC 10 0 会计凭证行项目编号
RSTGR 1 Types RSTGR CHAR 3 0 付款原因代码
BSCHL 1 Types BSCHL CHAR 2 0 记帐代码
ZZSPART 1 Types SPART CHAR 2 0 品牌
ZZANLN1 1 Types ZZANLN1 CHAR 12 0 收入辅助资产号码
ANBWA 1 Types ANBWA CHAR 3 0 资产业务类型
ANLN1 1 Types ANLN1 CHAR 12 0 主资产号
ANLN2 1 Types ANLN2 CHAR 4 0 资产次级编号
ZFBDT 1 Types DZFBDT DATS 8 0 用于到期日计算的基准日期
XNEGP 1 Types XNEGP CHAR 1 0 标识: 反记帐
三、实施 BADI_ACC_DOCUMENT ,代码可以直接从系统自带的DEMO中复制:
METHOD IF_EX_ACC_DOCUMENT~CHANGE.
DATA: WA_EXTENSION TYPE BAPIPAREX,
EXT_VALUE(960) TYPE C,
WA_ACCIT TYPE ACCIT,
L_REF TYPE REF TO DATA.
FIELD-SYMBOLS:
SORT C_EXTENSION2 BY STRUCTURE.
LOOP AT C_EXTENSION2 INTO WA_EXTENSION.
AT NEW STRUCTURE.
CREATE DATA L_REF TYPE (WA_EXTENSION-STRUCTURE).
ASSIGN L_REF->* TO
ENDAT.
CONCATENATE WA_EXTENSION-VALUEPART1 WA_EXTENSION-VALUEPART2
WA_EXTENSION-VALUEPART3 WA_EXTENSION-VALUEPART4
INTO EXT_VALUE.
MOVE EXT_VALUE TO
ASSIGN COMPONENT 'POSNR' OF STRUCTURE
CHECK
READ TABLE C_ACCIT WITH KEY POSNR =
INTO WA_ACCIT.
IF SY-SUBRC IS INITIAL.
MOVE-CORRESPONDING
MODIFY C_ACCIT FROM WA_ACCIT INDEX SY-TABIX.
ENDIF.
ENDLOOP.
ENDMETHOD.
四、完整的测试代码:
DATA: LW_DOCUMENTHEADER LIKE BAPIACHE09,
LW_CUSTOMERCPD LIKE BAPIACPA09,
LW_CONTRACTHEADER LIKE BAPIACCAHD.
DATA: LV_OBJ_TYPE LIKE BAPIACHE09-OBJ_TYPE,
LV_OBJ_KEY LIKE BAPIACHE09-OBJ_KEY,
LV_OBJ_SYS LIKE BAPIACHE09-OBJ_SYS.
DATA: LW_ACCOUNTGL LIKE BAPIACGL09,
LW_ACCOUNTRECEIVABLE LIKE BAPIACAR09,
LW_ACCOUNTPAYABLE LIKE BAPIACAP09,
LW_ACCOUNTTAX LIKE BAPIACTX09,
LW_CURRENCYAMOUNT LIKE BAPIACCR09,
LW_CRITERIA LIKE BAPIACKEC9,
LW_VALUEFIELD LIKE BAPIACKEV9,
LW_EXTENSION1 LIKE BAPIACEXTC,
LW_RETURN LIKE BAPIRET2,
LW_PAYMENTCARD LIKE BAPIACPC09,
LW_CONTRACTITEM LIKE BAPIACCAIT,
LW_EXTENSION2 LIKE BAPIPAREX,
LW_REALESTATE LIKE BAPIACRE09,
LW_ACCOUNTWT LIKE BAPIACWT09.
DATA: LT_ACCOUNTGL LIKE STANDARD TABLE OF BAPIACGL09,
LT_ACCOUNTRECEIVABLE LIKE STANDARD TABLE OF BAPIACAR09,
LT_ACCOUNTPAYABLE LIKE STANDARD TABLE OF BAPIACAP09,
LT_ACCOUNTTAX LIKE STANDARD TABLE OF BAPIACTX09,
LT_CURRENCYAMOUNT LIKE STANDARD TABLE OF BAPIACCR09,
LT_CRITERIA LIKE STANDARD TABLE OF BAPIACKEC9,
LT_VALUEFIELD LIKE STANDARD TABLE OF BAPIACKEV9,
LT_EXTENSION1 LIKE STANDARD TABLE OF BAPIACEXTC,
LT_RETURN LIKE STANDARD TABLE OF BAPIRET2,
LT_PAYMENTCARD LIKE STANDARD TABLE OF BAPIACPC09,
LT_CONTRACTITEM LIKE STANDARD TABLE OF BAPIACCAIT,
LT_EXTENSION2 LIKE STANDARD TABLE OF BAPIPAREX,
LT_REALESTATE LIKE STANDARD TABLE OF BAPIACRE09,
LT_ACCOUNTWT LIKE STANDARD TABLE OF BAPIACWT09.
DATA: WA_ZEXTEN LIKE ZEXTEN.
LW_DOCUMENTHEADER-USERNAME = SY-UNAME. "用户
LW_DOCUMENTHEADER-HEADER_TXT = ''. "抬头文本
LW_DOCUMENTHEADER-COMP_CODE = '1000'. "公司代码
LW_DOCUMENTHEADER-DOC_DATE = SY-DATUM. "凭证日期
LW_DOCUMENTHEADER-PSTNG_DATE = SY-DATUM. "记账日期
LW_DOCUMENTHEADER-DOC_TYPE = 'AA'. "凭证类型
LW_DOCUMENTHEADER-BUS_ACT = 'RFBU'. "业务事务 RFBU RMWE
LW_DOCUMENTHEADER-OBJ_TYPE = 'BKPFF'. "参考交易
CLEAR: LW_ACCOUNTGL.
LW_ACCOUNTGL-ITEMNO_ACC = 10.
LW_ACCOUNTGL-GL_ACCOUNT = '1601050000'.
LW_ACCOUNTGL-ACCT_TYPE = 'A'.
LW_ACCOUNTGL-ASSET_NO = '000200000003'.
LW_ACCOUNTGL-SUB_NUMBER = '0000'.
LW_ACCOUNTGL-ASVAL_DATE = SY-DATUM.
LW_ACCOUNTGL-QUANTITY = 2.
LW_ACCOUNTGL-BASE_UOM = 'M2'.
APPEND LW_ACCOUNTGL TO LT_ACCOUNTGL.
CLEAR: LW_ACCOUNTGL.
LW_ACCOUNTGL-ITEMNO_ACC = 20.
LW_ACCOUNTGL-GL_ACCOUNT = '3101010000'.
*LW_ACCOUNTGL-ACCT_TYPE = 'S'.
APPEND LW_ACCOUNTGL TO LT_ACCOUNTGL.
CLEAR: LW_CURRENCYAMOUNT.
LW_CURRENCYAMOUNT-ITEMNO_ACC = 10.
LW_CURRENCYAMOUNT-CURRENCY = 'CNY'.
LW_CURRENCYAMOUNT-AMT_DOCCUR = '100'.
APPEND LW_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.
CLEAR: LW_CURRENCYAMOUNT.
LW_CURRENCYAMOUNT-ITEMNO_ACC = 20.
LW_CURRENCYAMOUNT-CURRENCY = 'CNY'.
LW_CURRENCYAMOUNT-AMT_DOCCUR = '-100'.
APPEND LW_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.
CLEAR: WA_ZEXTEN.
WA_ZEXTEN-POSNR = 10. "行号
WA_ZEXTEN-RSTGR = ''. "
WA_ZEXTEN-BSCHL = '70'. "
WA_ZEXTEN-ZZSPART = ''. "
WA_ZEXTEN-ZZANLN1 = ''. "
WA_ZEXTEN-ANBWA = '100'. "
WA_ZEXTEN-ANLN1 = '000200000003'. "
WA_ZEXTEN-ANLN2 = '0000'. "
WA_ZEXTEN-ZFBDT = ''. "
WA_ZEXTEN-XNEGP = ''. "
LW_EXTENSION2-STRUCTURE = 'ZEXTEN'.
LW_EXTENSION2-VALUEPART1 = WA_ZEXTEN.
APPEND LW_EXTENSION2 TO LT_EXTENSION2.
CLEAR: WA_ZEXTEN.
WA_ZEXTEN-POSNR = 20. "行号
WA_ZEXTEN-RSTGR = ''. "
WA_ZEXTEN-BSCHL = '50'. "
WA_ZEXTEN-ZZSPART = ''. "
WA_ZEXTEN-ZZANLN1 = ''. "
WA_ZEXTEN-ANBWA = ''. "
WA_ZEXTEN-ANLN1 = ''. "
WA_ZEXTEN-ANLN2 = ''. "
WA_ZEXTEN-ZFBDT = ''. "
WA_ZEXTEN-XNEGP = ''. "
LW_EXTENSION2-STRUCTURE = 'ZEXTEN'.
LW_EXTENSION2-VALUEPART1 = WA_ZEXTEN.
APPEND LW_EXTENSION2 TO LT_EXTENSION2.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
DOCUMENTHEADER = LW_DOCUMENTHEADER
CUSTOMERCPD = LW_CUSTOMERCPD
CONTRACTHEADER = LW_CONTRACTHEADER
IMPORTING
OBJ_TYPE = LV_OBJ_TYPE
OBJ_KEY = LV_OBJ_KEY
OBJ_SYS = LV_OBJ_SYS
TABLES
ACCOUNTGL = LT_ACCOUNTGL
ACCOUNTRECEIVABLE = LT_ACCOUNTRECEIVABLE
ACCOUNTPAYABLE = LT_ACCOUNTPAYABLE
ACCOUNTTAX = LT_ACCOUNTTAX
CURRENCYAMOUNT = LT_CURRENCYAMOUNT
CRITERIA = LT_CRITERIA
VALUEFIELD = LT_VALUEFIELD
EXTENSION1 = LT_EXTENSION1
RETURN = LT_RETURN
PAYMENTCARD = LT_PAYMENTCARD
CONTRACTITEM = LT_CONTRACTITEM
EXTENSION2 = LT_EXTENSION2
REALESTATE = LT_REALESTATE
ACCOUNTWT = LT_ACCOUNTWT.
IF LV_OBJ_KEY = '$'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ENDIF.
LOOP AT LT_RETURN INTO LW_RETURN.
WRITE:/ LW_RETURN-TYPE,
LW_RETURN-ID,
LW_RETURN-NUMBER,
LW_RETURN-MESSAGE.
ENDLOOP.
五、参考资料:
http://blog.sina.com.cn/s/blog_3f2c03e30100mk24.html
http://blog.csdn.net/bxy5511/article/details/6456588