
FORM frm_save_data .
DATA : l_documentheader TYPE bapiache09,
lt_accountgl TYPE TABLE OF bapiacgl09 WITH HEADER LINE,
l_accountgl TYPE bapiacgl09,
lt_currencyamount TYPE TABLE OF bapiaccr09 WITH HEADER LINE,
l_account TYPE hkont VALUE &_gl_account_5001010000,
l_itemno_acc_01 TYPE bapiacgl09-itemno_acc, "第一个项目
l_itemno_acc_02 TYPE bapiacgl09-itemno_acc. "第二个项目
DATA : l_zjzje TYPE ztfi020-zjzje.
IF lt_data[] IS INITIAL.
EXIT.
ENDIF.
*---------l_documentheader ----------------*
l_documentheader-bus_act = 'RFBU'.
l_documentheader-username = sy-uname.
l_documentheader-comp_code = p_bukrs.
l_documentheader-fisc_year = p_gjahr.
l_documentheader-fis_period = p_monat.
l_documentheader-doc_type = 'SA'.
PERFORM conversion_exit_alpha_input CHANGING p_monat.
l_documentheader-doc_date = p_gjahr && p_monat && '01'.
* 获取上月的最后一天
PERFORM rp_calc_date_in_interval USING 1 CHANGING l_documentheader-doc_date.
l_documentheader-pstng_date = l_documentheader-doc_date.
*---------l_documentheader ----------------*
LOOP AT lt_data.
*- 生产订单
IF l_accountgl-orderid IS INITIAL
AND lt_data-aufnr IS NOT INITIAL.
l_accountgl-orderid = lt_data-aufnr.
ENDIF.
*- 成本中心
IF l_accountgl-costcenter IS INITIAL
AND lt_data-kostl IS NOT INITIAL.
l_accountgl-costcenter = lt_data-kostl.
ENDIF.
*- 汇总结转金额
l_zjzje = l_zjzje + lt_data-zjzje.
* 判断科目是 5001010000 还是 6601090400
IF lt_data-account = &_gl_account_6601090400.
l_account = &_gl_account_6601090400.
ENDIF.
CLEAR lt_data.
ENDLOOP.
IF l_accountgl-orderid IS INITIAL
AND l_accountgl-costcenter IS INITIAL.
MESSAGE '生产订单与成本中心都为空!' TYPE 'E'.
ENDIF.
IF l_zjzje > 0.
l_itemno_acc_01 = '40'.
l_itemno_acc_02 = '50'.
ELSE.
l_zjzje = - l_zjzje.
l_itemno_acc_01 = '50'.
l_itemno_acc_02 = '40'.
ENDIF.
*-------------accountgl---------*
lt_accountgl-itemno_acc = l_itemno_acc_01.
lt_accountgl-orderid = l_accountgl-orderid.
lt_accountgl-costcenter = l_accountgl-costcenter.
lt_accountgl-gl_account = &_gl_account_1901040000.
APPEND lt_accountgl.
CLEAR lt_accountgl.
lt_accountgl-itemno_acc = l_itemno_acc_02.
lt_accountgl-orderid = l_accountgl-orderid.
lt_accountgl-costcenter = l_accountgl-costcenter.
lt_accountgl-gl_account = l_account.
APPEND lt_accountgl.
CLEAR lt_accountgl.
*-------------accountgl---------*
*-------------currencyamount---------*
lt_currencyamount-itemno_acc = l_itemno_acc_01.
lt_currencyamount-currency = 'CNY'.
IF l_zjzje > 0.
lt_currencyamount-amt_doccur = l_zjzje.
ELSE.
lt_currencyamount-amt_doccur = - l_zjzje.
ENDIF.
APPEND lt_currencyamount.
CLEAR lt_currencyamount.
lt_currencyamount-itemno_acc = l_itemno_acc_02.
lt_currencyamount-currency = 'CNY'.
IF l_zjzje > 0.
lt_currencyamount-amt_doccur = - l_zjzje.
ELSE.
lt_currencyamount-amt_doccur = l_zjzje.
ENDIF.
APPEND lt_currencyamount.
CLEAR lt_currencyamount.
*-------------currencyamount---------*
PERFORM frm_bapi_acc_document_post TABLES lt_accountgl lt_currencyamount USING l_documentheader.
ENDFORM.
*&---------------------------------------------------------------------*DATA : l_documentheader TYPE bapiache09,
lt_accountgl TYPE TABLE OF bapiacgl09 WITH HEADER LINE,
l_accountgl TYPE bapiacgl09,
lt_currencyamount TYPE TABLE OF bapiaccr09 WITH HEADER LINE,
l_account TYPE hkont VALUE &_gl_account_5001010000,
l_itemno_acc_01 TYPE bapiacgl09-itemno_acc, "第一个项目
l_itemno_acc_02 TYPE bapiacgl09-itemno_acc. "第二个项目
DATA : l_zjzje TYPE ztfi020-zjzje.
IF lt_data[] IS INITIAL.
EXIT.
ENDIF.
*---------l_documentheader ----------------*
l_documentheader-bus_act = 'RFBU'.
l_documentheader-username = sy-uname.
l_documentheader-comp_code = p_bukrs.
l_documentheader-fisc_year = p_gjahr.
l_documentheader-fis_period = p_monat.
l_documentheader-doc_type = 'SA'.
PERFORM conversion_exit_alpha_input CHANGING p_monat.
l_documentheader-doc_date = p_gjahr && p_monat && '01'.
* 获取上月的最后一天
PERFORM rp_calc_date_in_interval USING 1 CHANGING l_documentheader-doc_date.
l_documentheader-pstng_date = l_documentheader-doc_date.
*---------l_documentheader ----------------*
LOOP AT lt_data.
*- 生产订单
IF l_accountgl-orderid IS INITIAL
AND lt_data-aufnr IS NOT INITIAL.
l_accountgl-orderid = lt_data-aufnr.
ENDIF.
*- 成本中心
IF l_accountgl-costcenter IS INITIAL
AND lt_data-kostl IS NOT INITIAL.
l_accountgl-costcenter = lt_data-kostl.
ENDIF.
*- 汇总结转金额
l_zjzje = l_zjzje + lt_data-zjzje.
* 判断科目是 5001010000 还是 6601090400
IF lt_data-account = &_gl_account_6601090400.
l_account = &_gl_account_6601090400.
ENDIF.
CLEAR lt_data.
ENDLOOP.
IF l_accountgl-orderid IS INITIAL
AND l_accountgl-costcenter IS INITIAL.
MESSAGE '生产订单与成本中心都为空!' TYPE 'E'.
ENDIF.
IF l_zjzje > 0.
l_itemno_acc_01 = '40'.
l_itemno_acc_02 = '50'.
ELSE.
l_zjzje = - l_zjzje.
l_itemno_acc_01 = '50'.
l_itemno_acc_02 = '40'.
ENDIF.
*-------------accountgl---------*
lt_accountgl-itemno_acc = l_itemno_acc_01.
lt_accountgl-orderid = l_accountgl-orderid.
lt_accountgl-costcenter = l_accountgl-costcenter.
lt_accountgl-gl_account = &_gl_account_1901040000.
APPEND lt_accountgl.
CLEAR lt_accountgl.
lt_accountgl-itemno_acc = l_itemno_acc_02.
lt_accountgl-orderid = l_accountgl-orderid.
lt_accountgl-costcenter = l_accountgl-costcenter.
lt_accountgl-gl_account = l_account.
APPEND lt_accountgl.
CLEAR lt_accountgl.
*-------------accountgl---------*
*-------------currencyamount---------*
lt_currencyamount-itemno_acc = l_itemno_acc_01.
lt_currencyamount-currency = 'CNY'.
IF l_zjzje > 0.
lt_currencyamount-amt_doccur = l_zjzje.
ELSE.
lt_currencyamount-amt_doccur = - l_zjzje.
ENDIF.
APPEND lt_currencyamount.
CLEAR lt_currencyamount.
lt_currencyamount-itemno_acc = l_itemno_acc_02.
lt_currencyamount-currency = 'CNY'.
IF l_zjzje > 0.
lt_currencyamount-amt_doccur = - l_zjzje.
ELSE.
lt_currencyamount-amt_doccur = l_zjzje.
ENDIF.
APPEND lt_currencyamount.
CLEAR lt_currencyamount.
*-------------currencyamount---------*
PERFORM frm_bapi_acc_document_post TABLES lt_accountgl lt_currencyamount USING l_documentheader.
ENDFORM.
*& Form FRM_BAPI_ACC_DOCUMENT_POST
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
* -->P_DOCUMENTHEADER text
* -->P_ACCOUNTGL text
* -->P_CURRENCYAMOUNT text
*&---------------------------------------------------------------------*
FORM frm_bapi_acc_document_post TABLES p_accountgl STRUCTURE bapiacgl09
p_currencyamount STRUCTURE bapiaccr09
USING p_documentheader TYPE bapiache09.
DATA : lt_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
DATA : l_obj_key TYPE bapiache09-obj_key.
DATA : l_msg TYPE bapi_msg.
DATA : lt_ztfi020 TYPE TABLE OF ztfi020 WITH HEADER LINE.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = p_documentheader
IMPORTING
obj_key = l_obj_key
TABLES
accountgl = p_accountgl
currencyamount = p_currencyamount
return = lt_return.
CLEAR lt_return.
SORT lt_return BY type.
READ TABLE lt_return WITH KEY type = 'E' BINARY SEARCH.
IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT lt_return WHERE ( type = 'E' OR type = 'A' ) .
l_msg = l_msg && lt_return-message.
ENDLOOP.
l_msg = '凭证创建失败,请手工录入!' && l_msg.
MESSAGE l_msg TYPE 'E'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
l_msg = '凭证过账成功!过账凭证号:' && l_obj_key(10).
LOOP AT lt_data.
MOVE-CORRESPONDING lt_data TO lt_ztfi020.
lt_ztfi020-belnr = l_obj_key(10).
lt_ztfi020-zcxbs = ''.
APPEND lt_ztfi020.
CLEAR : lt_ztfi020,lt_data.
ENDLOOP.
* 更新数据库
* 1.上锁
PERFORM frm_ztfi020_lock.
* 2.更新数据
MODIFY ztfi020 FROM TABLE lt_ztfi020[].
IF sy-subrc = 0.
COMMIT WORK.
MESSAGE l_msg TYPE 'S'.
ELSE.
ROLLBACK WORK.
l_msg = l_msg && ',数据保存至ZTFI020表失败!请核查!'.
MESSAGE l_msg TYPE 'E'.
ENDIF.
* 3.解锁
PERFORM frm_ztfi020_unlock.
ENDIF.
ENDFORM.