FUNCTION zfm_storage.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IV_HEAD) TYPE ZSMB1A_HEAD OPTIONAL
*" VALUE(IV_ITEM) TYPE ZSMB1A_TAB OPTIONAL
*" EXPORTING
*" VALUE(EV_DOC) TYPE BAPI2017_GM_HEAD_RET-MAT_DOC
*" TABLES
*" ET_RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
DATA: gdsmt_header LIKE bapi2017_gm_head_01.
DATA: gdsmt_code LIKE bapi2017_gm_code.
DATA: gdsmt_item LIKE bapi2017_gm_item_create OCCURS 0 WITH HEADER
LINE.
DATA: return LIKE bapiret2 OCCURS 0.
DATA it_tab_edit.
DATA wa_item TYPE zsmb1a_item.
DATA wa_equi TYPE zsequi_itab.
DATA gdsmt_snum LIKE bapi2017_gm_serialnumber OCCURS 0 WITH HEADER
LINE.
DATA: BEGIN OF mthead.
INCLUDE STRUCTURE bapi2017_gm_head_ret.
DATA: END OF mthead.
DATA: mat_doc LIKE bapi2017_gm_head_ret-mat_doc.
DATA FLAG TYPE NUMC4.
CLEAR: gdsmt_header, gdsmt_code, gdsmt_item, gdsmt_item[], return.
* Setup BAPI header data.
IF sy-mandt = '300'.
gdsmt_header-pstng_date = '20121222'.
gdsmt_header-doc_date = '20121122'.
ELSE.
gdsmt_header-pstng_date = sy-datum.
gdsmt_header-doc_date = sy-datum.
ENDIF.
gdsmt_code-gm_code = '03'. "03 - MB1A - Goods Receipts for Purchase
*Order
* Write 101 movement to table.
*loop at it_tab_edit into itab.
CLEAR:FLAG, wa_item.
REFRESH :gdsmt_item,gdsmt_snum.
CLEAR:GDSMT_ITEM,GDSMT_SNUM.
LOOP AT iv_item INTO wa_item.
FLAG = SY-TABIX.
* IF wa_item-material IS NOT INITIAL AND iv_equi[] IS NOT INITIAL.
* CLEAR wa_equi.
* LOOP AT iv_equi INTO wa_equi WHERE material = wa_item-material.
* gdsmt_snum-MATDOC_ITM = FLAG.
* gdsmt_snum-SERIALNO = WA_EQUI-SERIALNO.
* CLEAR WA_EQUI.
* endloop.
* ENDIF.
MOVE '282' TO gdsmt_item-move_type.
MOVE wa_item-material TO gdsmt_item-material.
MOVE '1' TO gdsmt_item-entry_qnt.
* MOVE 'EA' TO gdsmt_item-entry_uom.
MOVE wa_item-plant TO gdsmt_item-plant.
MOVE wa_item-stge_loc TO gdsmt_item-stge_loc.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = iv_head-network
IMPORTING
output = iv_head-network.
MOVE iv_head-network TO gdsmt_item-network.
MOVE iv_head-activity TO gdsmt_item-activity.
MOVE ' ' TO gdsmt_item-mvt_ind.
* move 'X' to gdsmt_item-no_more_gr. "是否標識完成收貨
APPEND gdsmt_item.
MOVE FLAG TO gdsmt_snum-MATDOC_ITM.
MOVE WA_ITEM-SERIALNO TO GDSMT_SNUM-SERIALNO.
APPEND GDSMT_SNUM.
CLEAR:GDSMT_ITEM,GDSMT_SNUM.
CLEAR wa_item.
*endloop.
ENDLOOP.
IF NOT gdsmt_item[] IS INITIAL.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = gdsmt_header
goodsmvt_code = gdsmt_code
IMPORTING
goodsmvt_headret = mthead
materialdocument = mat_doc
TABLES
goodsmvt_item = gdsmt_item
goodsmvt_serialnumber = gdsmt_snum
return = et_return.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
ENDIF.
IF mat_doc IS NOT INITIAL.
ev_doc = mat_doc.
ENDIF.
ENDFUNCTION.