写一段生成财务凭证得ABAP代码
时间: 2023-09-12 13:05:29 浏览: 123
### 回答1:
首先,我们需要在SAP系统中创建一个ABAP程序,然后再编写代码。下面是一个简单的生成财务凭证的代码示例:
```
REPORT ZFIN_VOUCHER_CREATE.
DATA: BEGIN OF VOUCHER_HEADER OCCURS 0,
POSTING_DATE TYPE D,
COMPANY_CODE TYPE BUKRS,
DOCUMENT_TYPE TYPE BLART,
END OF VOUCHER_HEADER.
DATA: BEGIN OF VOUCHER_ITEM OCCURS 0,
ACCOUNT_TYPE TYPE SAKNR,
AMOUNT TYPE BCD,
TEXT TYPE BKTXT,
END OF VOUCHER_ITEM.
START-OF-SELECTION.
VOUCHER_HEADER-POSTING_DATE = SY-DATUM.
VOUCHER_HEADER-COMPANY_CODE = '1000'.
VOUCHER_HEADER-DOCUMENT_TYPE = 'SA'.
VOUCHER_ITEM-ACCOUNT_TYPE = '1100'.
VOUCHER_ITEM-AMOUNT = 100.
VOUCHER_ITEM-TEXT = '生成财务凭证测试'.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
HEADER_TXT = VOUCHER_HEADER
ITEM_TXT = VOUCHER_ITEM
IMPORTING
DOCUMENT_HEADER = VOUCHER_HEADER
DOCUMENT_ITEM = VOUCHER_ITEM.
END-OF-SELECTION.
```
注意:这只是一个简单的代码示例,在生产环境中需要根据实际需求进行相应的修改。
### 回答2:
生成财务凭证的ABAP代码如下:
```
REPORT Z_GENERATE_FINANCIAL_DOC.
DATA: lv_doc_number TYPE i,
lv_doc_date TYPE sy-datum,
lv_acc_document TYPE bnka_belnr,
lt_items TYPE TABLE OF bapiache09b,
ls_item TYPE bapiache09b,
lt_gl_accounts TYPE TABLE OF bapiache09,
ls_gl_account TYPE bapiache09,
ls_header TYPE bapi_accountgl09,
lt_messages TYPE TABLE OF bapiret2.
* Set financial document header data
ls_header-companycode = '1000'.
ls_header-fiscalyr = '2022'.
ls_header-fiscalvarn = '01'.
ls_header-documenttype = 'RV'.
ls_header-postingdate = '20220101'.
ls_header-documentdate = '20220101'.
ls_header-header_tx_area = '001'.
* Add financial document items
CLEAR ls_item.
ls_item-itemno_acc = '001'.
ls_item-gl_account = '100000'.
ls_item-glaccount_s = 'S'.
ls_item-itemtext = '收入金额'。
ls_item-itemamount = 10000.
APPEND ls_item TO lt_items.
CLEAR ls_item.
ls_item-itemno_acc = '002'.
ls_item-gl_account = '200000'.
ls_item-glaccount_s = 'S'.
ls_item-itemtext = '支出金额'。
ls_item-itemamount = 5000.
APPEND ls_item TO lt_items.
* Populate general ledger accounts table
CLEAR ls_gl_account.
ls_gl_account-itemno_acc = '001'.
ls_gl_account-gl_account = '100000'.
ls_gl_account-glaccount_s = 'S'.
APPEND ls_gl_account TO lt_gl_accounts.
CLEAR ls_gl_account.
ls_gl_account-itemno_acc = '002'.
ls_gl_account-gl_account = '200000'.
ls_gl_account-glaccount_s = 'S'.
APPEND ls_gl_account TO lt_gl_accounts.
* Call BAPI function module to create financial document
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
accountingdocument = lv_acc_document
header = ls_header
TABLES
return = lt_messages
item = lt_items
itemgl = lt_gl_accounts.
* Check and process any error messages
IF lt_messages IS NOT INITIAL.
LOOP AT lt_messages INTO DATA(ls_message).
WRITE: / 'Error Message:', ls_message-message.
ENDLOOP.
ELSE.
WRITE: / 'Financial document', lv_acc_document, 'created successfully.'.
ENDIF.
```
以上代码用于生成一个具有财务凭证号、财务凭证日期、会计凭证号等信息的财务凭证。通过设置头部数据和添加财务凭证项目的方式,将数据传递给BAPI_ACC_DOCUMENT_POST函数模块,创建财务凭证。同时,还会检查并处理可能的错误消息。如果没有错误,将输出"Financial document {lv_acc_document} created successfully."的提示信息。
### 回答3:
生成财务凭证的ABAP代码如下:
DATA: lt_bkpf TYPE STANDARD TABLE OF bkpf, "财务凭证头表数据类型
lt_bseg TYPE STANDARD TABLE OF bseg. "财务凭证行项目表数据类型
DATA: ls_bkpf TYPE bkpf, "财务凭证头表数据结构
ls_bseg TYPE bseg. "财务凭证行项目表数据结构
* 初始化数据
CLEAR ls_bkpf.
CLEAR ls_bseg.
* 填充财务凭证头表数据
ls_bkpf-bukrs = '公司代码'. "公司代码
ls_bkpf-blart = '凭证类型'. "凭证类型
ls_bkpf-budat = sy-datum. "凭证日期
ls_bkpf-monat = sy-datum+4(2). "会计期间
ls_bkpf-brnch = '分公司'. "分公司
ls_bkpf-waers = '货币代码'. "货币代码
* 填充财务凭证行项目数据
ls_bseg-bukrs = '公司代码'. "公司代码
ls_bseg-hkont = '科目代码'. "科目代码
ls_bseg-dmbtr = '金额'. "金额
APPEND ls_bkpf TO lt_bkpf. "将凭证头表数据插入表中
APPEND ls_bseg TO lt_bseg. "将凭证行项目数据插入表中
* 生成财务凭证
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = ls_bkpf "财务凭证头表数据
TABLES
accountgl = lt_bseg. "财务凭证行项目表数据
* 检查生成结果
READ TABLE lt_bkpf INDEX 1 INTO ls_bkpf.
IF sy-subrc = 0.
WRITE: '生成财务凭证成功, 凭证号:', ls_bkpf-docum.
ELSE.
WRITE: '生成财务凭证失败'.
ENDIF.
阅读全文