根据前台需要输入参数,在bapi里面传值,不同业务所需参数不同。
dome1:
"bapi结构赋值
LOOP AT lt_item INTO lw_item.
wa_documentheader-comp_code = lw_item-bukrs. "公司代码
wa_documentheader-doc_date = p_date. "凭证日期
wa_documentheader-pstng_date = p_date. "过账日期
wa_documentheader-doc_type = 'Z4'.
wa_documentheader-username = sy-uname.
wa_documentheader-fisc_year = p_date+0(4).
wa_documentheader-header_txt = '合同税率计算过账'.
wa_documentheader-fis_period = sy-datum+4(2).
CLEAR :wa_accountgl.
lv_i = lv_i + 1.
wa_accountgl-itemno_acc = lv_i. "会计凭证行项目编号
wa_accountgl-gl_account = '6051060000'. "总分类帐帐目
wa_accountgl-item_text = '合同税率计算过账'.
wa_accountgl-profit_ctr = lw_item-prctr. "利润中心
APPEND wa_accountgl TO lt_accountgl.
CLEAR wa_currency_amount.
wa_currency_amount-itemno_acc = lv_i. "行项目编号
wa_currency_amount-amt_doccur = lw_item-taxamount. "金额
wa_currency_amount-currency = 'CNY'.
APPEND wa_currency_amount TO lt_currency_amount.
CLEAR:wa_zbapi_acc01,wa_extension2.
wa_zbapi_acc01-posnr = lv_i.
wa_zbapi_acc01-xnegp = 'X'. "反记账
wa_zbapi_acc01-bschl = '40'.
wa_extension2-structure = 'ZBAPI_ACC01'.
wa_extension2-valuepart1 = wa_zbapi_acc01.
APPEND wa_extension2 TO lt_extension2.
CLEAR :wa_accountgl.
lv_i = lv_i + 1.
wa_accountgl-itemno_acc = lv_i. "会计凭证行项目编号
wa_accountgl-gl_account = lw_item-acctcode. "总分类帐帐目
wa_accountgl-item_text = '合同税率计算过账'.
wa_accountgl-profit_ctr = lw_item-prctr. "利润中心
APPEND wa_accountgl TO lt_accountgl.
CLEAR wa_currency_amount.
wa_currency_amount-itemno_acc = lv_i. "行项目编号
wa_currency_amount-amt_doccur = - lw_item-taxamount. "金额
wa_currency_amount-currency = 'CNY'.
APPEND wa_currency_amount TO lt_currency_amount.
CLEAR:wa_zbapi_acc01,wa_extension2.
wa_zbapi_acc01-posnr = lv_i.
wa_zbapi_acc01-bschl = '50'.
wa_extension2-structure = 'ZBAPI_ACC01'.
wa_extension2-valuepart1 = wa_zbapi_acc01.
APPEND wa_extension2 TO lt_extension2.
"财务凭证为999行,到900行时,进行财务凭证拆分;调用bapi生成凭证
IF lv_i >= 999.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = wa_documentheader
IMPORTING
obj_type = lv_obj_type
obj_key = lv_obj_key
obj_sys = lv_obj_sys
TABLES
accountgl = lt_accountgl
currencyamount = lt_currency_amount
return = lt_return
extension2 = lt_extension2.
CLEAR wa_return.
READ TABLE lt_return INTO wa_return WITH KEY type = 'E'.
IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. "失败
LOOP AT lt_return INTO wa_return WHERE type = 'E'.
ls_item-mesg = ls_item-mesg && '' && wa_return-message.
ENDLOOP.
ls_item-mesg = '@0A@' && '过账失败:' && ls_item-mesg.
ls_item-status = 'E'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' "成功
EXPORTING
wait = 'X'.
CONCATENATE '@08@' '过账成功' INTO ls_item-mesg.
ls_item-status = 'S'.
ENDIF.
CLEAR : lv_i,wa_documentheader,lt_accountgl,wa_accountgl,lt_currency_amount,
wa_currency_amount
,lt_extension2,lt_extension2[].
ENDIF.
ENDLOOP.
demo2:
LOOP AT gt_item INTO gs_item.
AT NEW bukrs.
CLEAR:lv_i,wa_documentheader,lt_accountgl,wa_accountgl,lt_currency_amount,
wa_currency_amount
,lt_extension2,lt_extension2[].
wa_documentheader
-comp_code = gs_item-bukrs.
READ TABLE gt_item INTO gs_item INDEX sy-tabix.
wa_documentheader
-doc_date = p_budat. "凭证日期
wa_documentheader
-pstng_date = p_budat. "过账日期
wa_documentheader
-doc_type = 'SA'.
wa_documentheader
-username = sy-uname.
wa_documentheader
-header_txt = '调整折旧费用分摊'.
ENDAT.
IF gs_item-mes IS INITIAL.
CLEAR :wa_accountgl.
lv_i
= lv_i + 1.
wa_accountgl
-itemno_acc = lv_i. "会计凭证行项目编号
wa_accountgl
-gl_account = gs_item-ktnafg. "总分类帐帐目
wa_accountgl
-costcenter = gs_item-kostl. "成本中心
wa_accountgl
-item_text = '调整折旧费用分摊'.
wa_accountgl
-profit_ctr = gs_item-bukrs. "利润中心
APPEND wa_accountgl TO lt_accountgl.
CLEAR wa_currency_amount.
wa_currency_amount
-itemno_acc = lv_i. "行项目编号
wa_currency_amount
-amt_doccur = - gs_item-nafaz. "金额
wa_currency_amount
-currency = 'CNY'.
APPEND wa_currency_amount TO lt_currency_amount.
CLEAR :wa_accountgl.
lv_i
= lv_i + 1.
wa_accountgl
-itemno_acc = lv_i. "会计凭证行项目编号
wa_accountgl
-gl_account = gs_item-hkont. "总分类帐帐目
wa_accountgl
-costcenter = gs_item-kostl1. "成本中心
wa_accountgl
-profit_ctr = gs_item-bukrs. "利润中心
wa_accountgl
-item_text = '调整折旧费用分摊'.
APPEND wa_accountgl TO lt_accountgl.
CLEAR wa_currency_amount.
wa_currency_amount
-itemno_acc = lv_i. "行项目编号
wa_currency_amount
-amt_doccur = gs_item-nafaz. "金额
wa_currency_amount
-currency = 'CNY'.
APPEND wa_currency_amount TO lt_currency_amount.
CLEAR:wa_zbapi_acc01,wa_extension2.
wa_zbapi_acc01
-posnr = lv_i.
IF gs_item-nafaz LT 0.
wa_zbapi_acc01
-xnegp = 'X'. "反记账
ENDIF.
wa_extension2
-structure = 'ZBAPI_ACC01'.
wa_extension2
-valuepart1 = wa_zbapi_acc01.
APPEND wa_extension2 TO lt_extension2.
ENDIF.
AT END OF bukrs.
"调用bapi生成凭证
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader
= wa_documentheader
IMPORTING
obj_type
= lv_obj_type
obj_key
= lv_obj_key
obj_sys
= lv_obj_sys
TABLES
accountgl
= lt_accountgl
currencyamount
= lt_currency_amount
return = lt_return
extension2
= lt_extension2.
LOOP AT gt_anlp1 ASSIGNING <f1> WHERE box EQ 'X' AND mes EQ space.
CLEAR wa_return.
READ TABLE lt_return INTO wa_return WITH KEY type = 'E'.
IF sy-subrc = 0.
CONCATENATE '@0A@' '调整失败:' wa_return-message INTO <f1>-mes.
CONCATENATE 'E' gs_anlp-sta INTO <f1>-sta.
ELSE.
CONCATENATE '@08@' '科目调整成功' lv_obj_key+0(10) INTO <f1>-mes.
CONCATENATE 'S' '' INTO <f1>-sta.
ENDIF.
<f1>
-pname = sy-uname.
<f1>
-pdate = sy-datum.
<f1>
-ptime = sy-uzeit.
<f1>
-belnr_t = lv_obj_key+0(10).
<f1>
-peraf = p_peraf.
ENDLOOP.
PERFORM frm_save_data.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDAT.
ENDLOOP.
注意:bapi结束需要
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT''.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
每生成一个凭证就保存。