*&---------------------------------------------------------------------*
*& Report ZDEMO_MIGO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zdemo_migo.

PARAMETERS p_ebeln TYPE ebeln.
PARAMETER p_sernr TYPE objk-sernr.



DATA:lt_ekko LIKE TABLE OF ekko.
DATA:ls_ekko LIKE ekko.
DATA:lt_ekpo LIKE TABLE OF ekpo.
DATA:ls_ekpo LIKE LINE OF lt_ekpo.

DATA:ls_goodsmvt_header LIKE bapi2017_gm_head_01.
DATA:lt_goodsmvt_item LIKE TABLE OF bapi2017_gm_item_create,
ls_goodsmvt_item LIKE LINE OF lt_goodsmvt_item,
lt_return LIKE TABLE OF bapiret2,
ls_return LIKE LINE OF lt_return.
DATA:lv_materialdocument TYPE bapi2017_gm_head_ret-mat_doc,
lv_matdocumentyear TYPE bapi2017_gm_head_ret-doc_year.
DATA:lt_goodsmvt_serialnumber LIKE TABLE OF bapi2017_gm_serialnumber.
DATA:ls_goodsmvt_serialnumber LIKE bapi2017_gm_serialnumber.
DATA:lv_goodsmvt_code LIKE bapi2017_gm_code VALUE '01'.

DATA:lv_lines TYPE int4.
DATA:lv_item_line TYPE int4.

FIELD-SYMBOLS LIKE LINE OF lt_goodsmvt_item.


SELECT SINGLE * FROM ekko INTO ls_ekko
WHERE ebeln = p_ebeln.

SELECT * FROM ekpo INTO TABLE lt_ekpo
WHERE ebeln = p_ebeln.


*&header
CLEAR ls_goodsmvt_header.
ls_goodsmvt_header-pstng_date = sy-datum.
ls_goodsmvt_header-doc_date = sy-datum.
ls_goodsmvt_header-ref_doc_no = ls_ekko-ebeln."PO
ls_goodsmvt_header-pr_uname = sy-uname.
ls_goodsmvt_header-header_txt = ls_ekko-ebeln.

*& item
CLEAR lt_goodsmvt_item[].
CLEAR lt_goodsmvt_serialnumber[].

LOOP AT lt_ekpo INTO ls_ekpo WHERE ebeln = ls_ekko-ebeln.
CLEAR ls_goodsmvt_item.

ls_goodsmvt_item-vendor = ls_ekko-lifnr.
ls_goodsmvt_item-material = ls_ekpo-matnr.
ls_goodsmvt_item-plant = ls_ekpo-werks.
ls_goodsmvt_item-stge_loc = ls_ekpo-lgort.
ls_goodsmvt_item-move_type = '101'." 正常po 收货'101'.

ls_goodsmvt_item-entry_qnt = ls_ekpo-menge.
ls_goodsmvt_item-entry_uom = ls_ekpo-meins.
ls_goodsmvt_item-po_number = ls_ekpo-ebeln.
ls_goodsmvt_item-po_item = ls_ekpo-ebelp.
ls_goodsmvt_item-mvt_ind = 'B'."采购订单

READ TABLE lt_goodsmvt_item ASSIGNING
WITH KEY material = ls_goodsmvt_item-material
plant = ls_goodsmvt_item-plant
stge_loc = ls_goodsmvt_item-stge_loc
move_type = ls_goodsmvt_item-move_type
move_plant = ls_goodsmvt_item-move_plant
move_stloc = ls_goodsmvt_item-move_stloc.

IF sy-subrc = 0.
-entry_qnt = -entry_qnt 1.
ELSE.
APPEND ls_goodsmvt_item TO lt_goodsmvt_item.
ENDIF.


DESCRIBE TABLE lt_goodsmvt_item LINES lv_item_line.

ls_goodsmvt_serialnumber-matdoc_itm = lv_item_line.
ls_goodsmvt_serialnumber-serialno = p_sernr.
APPEND ls_goodsmvt_serialnumber TO lt_goodsmvt_serialnumber.

CLEAR ls_ekpo.
ENDLOOP.

*& call bapi

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_goodsmvt_header
goodsmvt_code = lv_goodsmvt_code
IMPORTING
materialdocument = lv_materialdocument
TABLES
goodsmvt_item = lt_goodsmvt_item
goodsmvt_serialnumber = lt_goodsmvt_serialnumber
return = lt_return.


WRITE lv_materialdocument.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.