物料憑證過賬BAPI
一 測試
*&---------------------------------------------------------------------*
*& Report ZLXF12
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZLXF12.
DATA: ls_goodsmvt_header TYPE bapi2017_gm_head_01,
ls_goodsmvt_code TYPE bapi2017_gm_code.
DATA: ls_goodsmvt_item LIKE bapi2017_gm_item_create,
lt_goodsmvt_item LIKE TABLE OF bapi2017_gm_item_create.
DATA: lv_materialdocument TYPE bapi2017_gm_head_ret-mat_doc.
DATA: lv_matdocumentyear TYPE bapi2017_gm_head_ret-doc_year.
DATA: ls_return TYPE bapiret2,
lt_return TYPE TABLE OF bapiret2.
DATA: lv_msg TYPE string.
DATA: lv_message TYPE string.
ls_goodsmvt_header-pstng_date = '20170630'." 憑證中的過帳日期
ls_goodsmvt_header-doc_date = '20170630'."憑證中的憑證日期
ls_goodsmvt_header-header_txt = 'bapi create ' ."憑證擡頭文字
ls_goodsmvt_code-gm_code = '05'.
* LOOP AT gt_item INTO gs_item WHERE zsfsl > 0 AND mblnr IS INITIAL.
ls_goodsmvt_item-material = '10.130.000001'."物料號
ls_goodsmvt_item-plant = '1000'."工廠
ls_goodsmvt_item-stge_loc = '1001'."庫存地點
ls_goodsmvt_item-move_type = '511'."移動型別
* ls_goodsmvt_item-item_text = gs_head-zlldj."領料單號
* ls_goodsmvt_item-gr_rcpt = gs_item-zxuha."序號
* ls_goodsmvt_item-orderid = gs_item-aufnr."生產訂單號
ls_goodsmvt_item-entry_qnt = 520.
ls_goodsmvt_item-entry_uom = 'ST' .
* ls_goodsmvt_item-quantity = gs_item-zsfsl.
APPEND ls_goodsmvt_item TO lt_goodsmvt_item.
CLEAR: ls_goodsmvt_item.
* ENDLOOP.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_goodsmvt_header
goodsmvt_code = ls_goodsmvt_code
IMPORTING
materialdocument = lv_materialdocument
matdocumentyear = lv_matdocumentyear
TABLES
goodsmvt_item = lt_goodsmvt_item
return = lt_return.
IF lv_materialdocument IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
IF sy-subrc EQ 0.
COMMIT WORK.
* MESSAGE s000 WITH '過賬成功,憑證號' lv_materialdocument.
MESSAGE lv_materialdocument TYPE 'S' .
LEAVE TO SCREEN 0.
ELSE.
ROLLBACK WORK.
* MESSAGE s000 WITH '過賬成功,憑證號' lv_materialdocument '資料儲存失敗!'.
MESSAGE '資料儲存失敗!' TYPE 'W' .
LEAVE TO SCREEN 0.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT lt_return INTO ls_return WHERE type EQ 'A' OR type EQ 'E'.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = ls_return-id
msgnr = ls_return-number
msgv1 = ls_return-message_v1
msgv2 = ls_return-message_v2
msgv3 = ls_return-message_v3
msgv4 = ls_return-message_v4
IMPORTING
message_text_output = lv_msg.
IF lv_message IS INITIAL.
lv_message = lv_msg.
ELSE.
CONCATENATE lv_msg '/' lv_message INTO lv_message.
ENDIF.
CLEAR: ls_return.
ENDLOOP.
* MESSAGE i000 WITH lv_msg DISPLAY LIKE 'E'.
MESSAGE lv_msg TYPE 'W' .
ENDIF.
二 委外 ==================================================
FORM frm_post .
DATA: lt_zmmt009c LIKE TABLE OF zmmt009c,
ls_zmmt009c LIKE zmmt009c.
DATA: lt_zmmt009b LIKE TABLE OF zmmt009b,
ls_zmmt009b LIKE zmmt009b.
DATA: lt_009b LIKE TABLE OF zmmt009b.
DATA: ls_goodsmvt_header TYPE bapi2017_gm_head_01,
ls_goodsmvt_code TYPE bapi2017_gm_code.
DATA: ls_goodsmvt_item LIKE bapi2017_gm_item_create,
lt_goodsmvt_item LIKE TABLE OF bapi2017_gm_item_create.
DATA: lv_materialdocument TYPE bapi2017_gm_head_ret-mat_doc.
DATA: lv_matdocumentyear TYPE bapi2017_gm_head_ret-doc_year.
DATA: ls_return TYPE bapiret2,
lt_return TYPE TABLE OF bapiret2.
DATA: lv_msg TYPE string.
DATA: lv_message TYPE string.
ls_goodsmvt_header-pstng_date = sy-datum." 憑證中的過帳日期
ls_goodsmvt_header-doc_date = sy-datum."憑證中的憑證日期
ls_goodsmvt_header-header_txt = gs_head-zwwck."憑證擡頭文字
ls_goodsmvt_code-gm_code = '04'.
LOOP AT gt_item INTO gs_item WHERE zgzsl > 0 AND mblnr IS INITIAL.
ls_goodsmvt_item-material = gs_item-matnr."物料號
ls_goodsmvt_item-plant = gs_head-werks."工廠
ls_goodsmvt_item-stge_loc = gs_item-lgort."庫存地點
ls_goodsmvt_item-move_type = '541'."移動型別
* ls_goodsmvt_item-orderid = gs_item-aufnr."生產訂單號
ls_goodsmvt_item-vendor = gs_item-lifnr.
ls_goodsmvt_item-gr_rcpt = gs_item-ebeln.
ls_goodsmvt_item-unload_pt = gs_item-ebelp.
ls_goodsmvt_item-item_text = gs_item-zhxmh."序號
ls_goodsmvt_item-entry_qnt = gs_item-zgzsl.
ls_goodsmvt_item-entry_uom = gs_item-meins.
ls_goodsmvt_item-val_type = gs_item-bwtar.
APPEND ls_goodsmvt_item TO lt_goodsmvt_item.
CLEAR: ls_goodsmvt_item.
ENDLOOP.
IF lt_goodsmvt_item[] IS INITIAL.
MESSAGE s000 WITH '沒有符合條件的過賬資料,請檢查' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_goodsmvt_header
goodsmvt_code = ls_goodsmvt_code
IMPORTING
materialdocument = lv_materialdocument
matdocumentyear = lv_matdocumentyear
TABLES
goodsmvt_item = lt_goodsmvt_item
return = lt_return.
IF lv_materialdocument IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
LOOP AT gt_item INTO gs_item WHERE mblnr IS INITIAL.
IF gs_item-zgzsl > 0.
gs_item-mblnr = lv_materialdocument.
gs_item-mjahr = lv_matdocumentyear.
ELSE.
gs_item-loekz = 'X'.
ENDIF.
gs_item-zhxgd = sy-datum.
gs_item-zhxgr = sy-uname.
MODIFY gt_item FROM gs_item.
CLEAR: gs_item.
ENDLOOP.
SELECT *
INTO TABLE lt_zmmt009b
FROM zmmt009b
FOR ALL ENTRIES IN gt_item
WHERE lifnr = gt_item-lifnr
AND matnr = gt_item-matnr
AND werks = gt_item-werks.
SORT lt_zmmt009b BY lifnr matnr werks.
LOOP AT gt_item INTO gs_item.
MOVE-CORRESPONDING gs_item TO ls_zmmt009c.
APPEND ls_zmmt009c TO lt_zmmt009c.
IF gs_item-loekz IS INITIAL.
CLEAR: ls_zmmt009b.
READ TABLE lt_zmmt009b INTO ls_zmmt009b
WITH KEY lifnr = gs_item-lifnr
matnr = gs_item-matnr
werks = gs_item-werks
BINARY SEARCH.
IF sy-subrc EQ 0.
ls_zmmt009b-zdfkc_ye = ls_zmmt009b-zdfkc_ye + gs_item-zbddf.
ls_zmmt009b-zhxgd = sy-datum.
ls_zmmt009b-zhxgr = sy-uname.
* APPEND ls_zmmt009b TO lt_009b.
MODIFY lt_zmmt009b FROM ls_zmmt009b INDEX sy-tabix.
ELSE.
MOVE-CORRESPONDING gs_item TO ls_zmmt009b.
ls_zmmt009b-zdfkc_ye = gs_item-zbddf.
ls_zmmt009b-zhxgd = sy-datum.
ls_zmmt009b-zhxgr = sy-uname.
* APPEND ls_zmmt009b TO lt_zmmt009b.
COLLECT ls_zmmt009b INTO lt_009b.
ENDIF.
ENDIF.
CLEAR: ls_zmmt009c,gs_item.
ENDLOOP.
IF lt_009b[] IS NOT INITIAL.
APPEND LINES OF lt_009b TO lt_zmmt009b.
ENDIF.
CALL FUNCTION 'DEQUEUE_ALL'.
MODIFY zmmt009c FROM TABLE lt_zmmt009c.
IF sy-subrc EQ 0.
MODIFY zmmt009b FROM TABLE lt_zmmt009b.
COMMIT WORK.
MESSAGE s000 WITH '過賬成功,憑證號' lv_materialdocument.
LEAVE TO SCREEN 0.
ELSE.
ROLLBACK WORK.
MESSAGE s000 WITH '過賬成功,憑證號' lv_materialdocument '資料儲存失敗!'.
LEAVE TO SCREEN 0.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
CALL FUNCTION 'DEQUEUE_ALL'.
LOOP AT lt_return INTO ls_return WHERE type EQ 'A' OR type EQ 'E'.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = ls_return-id
msgnr = ls_return-number
msgv1 = ls_return-message_v1
msgv2 = ls_return-message_v2
msgv3 = ls_return-message_v3
msgv4 = ls_return-message_v4
IMPORTING
message_text_output = lv_msg.
IF lv_message IS INITIAL.
lv_message = lv_msg.
ELSE.
CONCATENATE lv_msg '/' lv_message INTO lv_message.
ENDIF.
CLEAR: ls_return.
ENDLOOP.
MESSAGE s000 WITH lv_msg DISPLAY LIKE 'E'.
ENDIF.
ENDFORM.