ABAP會計憑證生成介面
阿新 • • 發佈:2021-12-23
FUNCTION zfifm057.
*"----------------------------------------------------------------------
*"*"本地介面:
*" EXPORTING
*" VALUE(HLYNUMBER) TYPE CHAR50
*" VALUE(TRANSACTIONNUMBER) TYPE CHAR50
*" VALUE(BELNR) TYPE BELNR_D
*" VALUE(ZMSGTYP) TYPE CHAR01
*" VALUE(ZMSGTXT) TYPE CHAR200
*" TABLES
*" IT_HEAD STRUCTURE ZFIFM057_HEAD
*" IT_ITEM STRUCTURE ZFIFM057_ITEM
*"----------------------------------------------------------------------
DATA:wa_documentheader TYPE bapiache09, "待傳憑證抬頭 工作區
wa_accountgl TYPE bapiacgl09, "總帳科目項 工作區
it_accountgl TYPE STANDARD TABLE OF bapiacgl09, "總帳科目項內表
wa_accountreceivable TYPE bapiacar09, "客戶專案 工作區
it_accountreceivable TYPE STANDARD TABLE OF bapiacar09, "客戶專案 內表
wa_accountpayable TYPE bapiacap09, "供應商專案 工作區
it_accountpayable TYPE STANDARD TABLE OF bapiacap09, "供應商專案內表
wa_currencyamount TYPE bapiaccr09, "貨幣專案 工作區
it_currencyamount TYPE STANDARD TABLE OF bapiaccr09, "貨幣專案 內表
wa_extension2 TYPE bapiparex, "參考結構 工作區
it_extension2 TYPE STANDARD TABLE OF bapiparex, "參考結構 內表
wa_return TYPE bapiret2, "返回引數 工作區
it_return TYPE STANDARD TABLE OF bapiret2,
l_obj_key TYPE bapiache09-obj_key.
DATA: wa_zexten LIKE zsfi001.
DATA: l_itemno TYPE char10 VALUE '0000000000'."行專案
DATA: lt_log_head TYPE TABLE OF ztwf004 WITH HEADER LINE,
lt_log_item TYPE TABLE OF ztwf005 WITH HEADER LINE.
CHECK it_head IS NOT INITIAL.
READ TABLE it_head INTO DATA(ls_head) INDEX 1 .
* wa_documentheader-ref_doc_no_long = ls_head-xblnr.
wa_documentheader-comp_code = ls_head-bukrs. "公司程式碼
wa_documentheader-doc_type = 'SA'. "憑證型別
wa_documentheader-doc_date = ls_head-bldat. "憑證中的憑證日期
wa_documentheader-pstng_date = ls_head-budat. "憑證中的記帳日期
wa_documentheader-ref_doc_no = ls_head-xblnr. "參考憑證號
wa_documentheader-header_txt = ls_head-bktxt. "憑證抬頭文字
wa_documentheader-username = sy-uname. "使用者
LOOP AT it_item INTO DATA(ls_item).
ADD 1 TO l_itemno.
IF ls_item-koart = 'S' OR ( ls_item-lifnr IS INITIAL AND ls_item-kunnr IS INITIAL ).
"總賬相關的行專案資訊
wa_accountgl-itemno_acc = l_itemno . "憑證行專案
wa_accountgl-costcenter = ls_item-kostl . "成本中心
wa_accountgl-profit_ctr = ls_item-prctr . "利潤中心
wa_accountgl-gl_account = ls_item-hkont. "會計科目
wa_accountgl-item_text = ls_item-sgtxt. "專案行文字
wa_accountgl-alloc_nmbr = ls_item-zuonr. "分配號
IF ls_item-aufnr IS NOT INITIAL.
wa_accountgl-orderid = ls_item-aufnr. "訂單號
ENDIF.
APPEND wa_accountgl TO it_accountgl .
ENDIF.
IF ls_item-koart = 'K' OR ls_item-lifnr IS NOT INITIAL."供應商
wa_accountpayable-itemno_acc = l_itemno . "憑證行專案
wa_accountpayable-vendor_no = ls_item-lifnr. "供應商
wa_accountpayable-alloc_nmbr = ls_item-zuonr. "分配號
wa_accountpayable-item_text = ls_item-sgtxt. "專案行文字
APPEND wa_accountpayable TO it_accountpayable.
ENDIF.
IF ls_item-koart = 'D' OR ls_item-kunnr IS NOT INITIAL."客戶.
wa_accountreceivable-itemno_acc = l_itemno . "憑證行專案
wa_accountreceivable-customer = ls_item-kunnr.
wa_accountreceivable-gl_account = ls_item-hkont.
wa_accountreceivable-profit_ctr = ls_item-prctr . "利潤中心
wa_accountreceivable-item_text = ls_item-sgtxt. "專案行文字
wa_accountreceivable-bline_date = ls_item-zfbdt. "到期日期計算的起算日期
APPEND wa_accountreceivable TO it_accountreceivable.
ENDIF.
"貨幣行專案資訊
IF ls_item-shkzg = 'H'.
ls_item-cny_amount = ls_item-cny_amount * ( -1 ).
ls_item-dmbtr = ls_item-dmbtr * ( -1 ).
ENDIF.
wa_currencyamount-itemno_acc = l_itemno .
wa_currencyamount-amt_doccur = ls_item-cny_amount. "原幣金額
wa_currencyamount-currency = ls_head-waers.
IF ls_head-hwaer IS NOT INITIAL.
wa_currencyamount-currency = ls_head-hwaer.
wa_currencyamount-amt_doccur = ls_item-dmbtr. "本位幣金額
ENDIF.
wa_currencyamount-exch_rate = ls_item-kursf. "匯率
APPEND wa_currencyamount TO it_currencyamount.
wa_zexten-posnr = l_itemno.
CASE ls_head-zbdlx.
WHEN '出差費用報銷單' OR '個人報銷'.
IF ls_head-zspjd = 'FA'. "往來憑證
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '40'.
ELSE.
wa_zexten-bschl = '31'.
ENDIF.
ENDIF.
IF ls_head-zspjd = 'CA'."付款憑證
"如果實際付款金額為0時,則不產生憑證
IF line_exists( it_currencyamount[ amt_doccur = 0 ] ).
hlynumber = ls_head-hlynumber.
transactionnumber = ls_head-transactionnumber.
zmsgtyp = 'E'.
zmsgtxt = '實際付款金額為0!'.
RETURN.
ENDIF.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '25'.
ELSE.
wa_zexten-bschl = '50'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ENDIF.
ENDIF.
WHEN '借款單' .
IF ls_head-zfylx = '日常借款'.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '25'.
ELSE.
wa_zexten-bschl = '50'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ENDIF.
ENDIF.
IF ls_head-zfylx = '房租押金' OR ls_head-zfylx = '保證金退款'.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '29'.
wa_zexten-umskz = ls_item-umskz. "特殊總賬標識
ELSE.
wa_zexten-bschl = '50'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ENDIF.
ENDIF.
WHEN '還款單'.
IF ls_head-zfylx = '日常借款還款'.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '40'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ELSE.
wa_zexten-bschl = '31'.
ENDIF.
ENDIF.
IF ls_head-zfylx = '房租押金還款' OR ls_head-zfylx = '保證金退款還款'.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '40'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ELSE.
wa_zexten-bschl = '39'.
wa_zexten-umskz = ls_item-umskz. "特殊總賬標識
ENDIF.
ENDIF.
WHEN '培訓付款單' OR '會議付款單' OR '對公付款'.
IF ls_head-zspjd = 'FA' AND ls_head-zsfdp = ''.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '25'.
ELSE.
wa_zexten-bschl = '50'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ENDIF.
ELSEIF ls_head-zspjd = 'FA' AND ls_head-zsfdp = 'X'.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '40'.
ELSE.
wa_zexten-bschl = '31'.
ENDIF.
ELSEIF ls_head-zspjd = 'CA' .
"如果實際付款金額為0時,則不產生憑證
IF line_exists( it_currencyamount[ amt_doccur = 0 ] ).
hlynumber = ls_head-hlynumber.
transactionnumber = ls_head-transactionnumber.
zmsgtyp = 'E'.
zmsgtxt = '實際付款金額為0!'.
RETURN.
ENDIF.
IF ls_item-shkzg = 'S'.
wa_zexten-bschl = '25'.
ELSE.
wa_zexten-bschl = '50'.
wa_zexten-zzcf = ls_item-zzcf. "現金流量項
ENDIF.
ENDIF.
WHEN OTHERS.
ENDCASE.
wa_extension2-structure = 'ZSFI001'.
wa_extension2-valuepart1 = wa_zexten.
APPEND wa_extension2 TO it_extension2.
ENDLOOP.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = wa_documentheader
IMPORTING
obj_key = l_obj_key
TABLES
accountgl = it_accountgl
accountreceivable = it_accountreceivable
accountpayable = it_accountpayable
currencyamount = it_currencyamount
return = it_return
extension2 = it_extension2.
READ TABLE it_return INTO wa_return WITH KEY type = 'E'.
IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT it_return INTO wa_return WHERE ( type = 'E' OR type = 'A').
zmsgtxt = zmsgtxt && wa_return-message.
ENDLOOP.
zmsgtyp = 'E'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
zmsgtyp = 'S'.
belnr = l_obj_key(10).
ENDIF.
hlynumber = ls_head-hlynumber.
transactionnumber = ls_head-transactionnumber.
WAIT UP TO '0.5' SECONDS.
UPDATE bkpf SET xblnr_alt = ls_head-xblnr_alt WHERE bukrs = ls_head-bukrs AND belnr = l_obj_key(10) AND gjahr = ls_head-budat(4).
IF sy-subrc = 0.
COMMIT WORK .
ELSE.
ROLLBACK WORK.
ENDIF.
"日誌儲存
MOVE-CORRESPONDING ls_head TO lt_log_head.
lt_log_head-belnr = l_obj_key(10).
lt_log_head-ztime = sy-datum && sy-uzeit.
lt_log_head-zhlyl = ls_head-hlynumber.
lt_log_head-zhlym = ls_head-transactionnumber.
APPEND lt_log_head.
CLEAR : l_itemno.
LOOP AT it_item INTO ls_item.
ADD 1 TO l_itemno.
MOVE-CORRESPONDING ls_item TO lt_log_item.
lt_log_item-ztime = sy-datum && sy-uzeit.
lt_log_item-belnr = l_obj_key(10).
lt_log_item-buzei = l_itemno.
ENDLOOP.
MODIFY ztwf004 FROM TABLE lt_log_head.
IF sy-subrc = 0.
COMMIT WORK .
ELSE.
ROLLBACK WORK.
ENDIF.
MODIFY ztwf005 FROM TABLE lt_log_item.
IF sy-subrc = 0.
COMMIT WORK .
ELSE.
ROLLBACK WORK.
ENDIF.
ENDFUNCTION.