SAP 411移库demo BAPI BAPI_GOODSMVT_CREATE
REPORT zmm026.
TABLES:mska.
**全局定义
TYPES:BEGIN OF tp_alv,
matnr TYPE mska-matnr,
maktx TYPE makt-maktx,
werks TYPE mska-werks,
lgort TYPE mska-lgort,
vbeln TYPE mska-vbeln,
posnr TYPE mska-posnr,
kalab TYPE mska-kalab,
meins TYPE mara-meins,
lgort2 TYPE mska-lgort,
kalab2 TYPE mska-kalab,
sel,
incon TYPE char50,
msg TYPE char100,
END OF tp_alv.
DATA:gt_alv TYPE TABLE OF tp_alv,
gs_alv TYPE tp_alv.
DATA: gt_fcat TYPE lvc_t_fcat, "字段目录
gs_fcat TYPE lvc_s_fcat,
gs_layout TYPE lvc_s_layo.
**选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK b0 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS:s_werks FOR mska-werks,
s_matnr FOR mska-matnr,
s_vbeln FOR mska-vbeln,
s_lgort FOR mska-lgort.
SELECTION-SCREEN END OF BLOCK b0.
**初始化
INITIALIZATION.
**执行
START-OF-SELECTION.
PERFORM frm_getdata.
**显示
END-OF-SELECTION.
PERFORM frm_layout.
PERFORM frm_fcat.
PERFORM frm_show.
&---------------------------------------------------------------------
*& Form frm_getdata
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_getdata .
REFRESH:gt_alv.
SELECT
mska~matnr
mska~werks
mska~lgort
mska~vbeln
mska~posnr
mska~kalab
mara~meins
makt~maktx
INTO CORRESPONDING FIELDS OF TABLE gt_alv
FROM mska
JOIN mara ON mska~matnr EQ mara~matnr
JOIN makt ON mska~matnr EQ makt~matnr
AND makt~spras EQ sy-langu
WHERE mska~matnr IN s_matnr
AND mska~werks IN s_werks
AND mska~vbeln IN s_vbeln
AND mska~lgort IN s_lgort
AND mska~kalab > 0.
LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(
ENDLOOP.
SORT gt_alv BY matnr werks lgort.
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_layout
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_layout .
CLEAR gs_layout.
gs_layout-zebra = ‘X’.
gs_layout-cwidth_opt = ‘X’.
gs_layout-sel_mode = ‘A’.
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_fcat
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_fcat .
REFRESH gt_fcat.
DEFINE set_fieldcat_alv.
CLEAR gs_fcat.
gs_fcat-fieldname = &1.
gs_fcat-coltext = &2.
gs_fcat-ref_field = &3.
gs_fcat-ref_table = &4.
gs_fcat-outputlen = &5.
IF &1 = ‘KALAB’ OR &1 = ‘KALAB2’ .
gs_fcat-qfieldname = ‘MEINS’ .
ENDIF.
IF &1 = ‘VBELN’ OR &1 = ‘POSNR’.
gs_fcat-no_zero = ‘X’.
ENDIF.
IF &1 = ‘SEL’.
gs_fcat-checkbox = ‘X’.
ENDIF.
IF &1 = ‘SEL’ OR &1 = ‘LGORT2’ OR &1 = ‘KALAB2’.
gs_fcat-edit = ‘X’.
ENDIF.
APPEND gs_fcat TO gt_fcat.
END-OF-DEFINITION.
set_fieldcat_alv ‘SEL’ ‘选择’ ‘’ ‘’ ‘1’ .
set_fieldcat_alv ‘MATNR’ ‘物料’ ‘MATNR’ ‘MSKA’ ‘18’ .
set_fieldcat_alv ‘MAKTX’ ‘物料描述’ ‘’ ‘’ ‘40’ .
set_fieldcat_alv ‘WERKS’ ‘工厂’ ‘WERKS’ ‘MSKA’ ‘4’ .
set_fieldcat_alv ‘VBELN’ ‘销售订单’ ‘VBELN’ ‘MSKA’ ‘10’ .
set_fieldcat_alv ‘POSNR’ ‘行项目’ ‘POSNR’ ‘MSKA’ ‘6’ .
set_fieldcat_alv ‘LGORT’ ‘库位’ ‘LGORT’ ‘MSKA’ ‘4’ .
set_fieldcat_alv ‘KALAB’ ‘数量’ ‘’ ‘’ ‘15’ .
set_fieldcat_alv ‘MEINS’ ‘单位’ ‘MEINS’ ‘MARA’ ‘3’ .
set_fieldcat_alv ‘LGORT2’ ‘目标库位’ ‘LGORT’ ‘MSKA’ ‘4’ .
set_fieldcat_alv ‘KALAB2’ ‘移库数量’ ‘’ ‘’ ‘15’ .
set_fieldcat_alv ‘INCON’ ‘图标’ ‘’ ‘’ ‘50’ .
set_fieldcat_alv ‘MSG’ ‘处理消息’ ‘’ ‘’ ‘100’ .
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_show
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_show .
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC’
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fcat
i_callback_pf_status_set = ‘PF_STATUS’
i_callback_user_command = ‘USER_COMMAND’
it_events = lt_events
i_save = 'A'
TABLES
t_outtab = gt_alv
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA g_grid TYPE REF TO cl_gui_alv_grid.
CALL FUNCTION ‘GET_GLOBALS_FROM_SLVC_FULLSCR’
IMPORTING
e_grid = g_grid.
CALL METHOD g_grid->check_changed_data.
CASE r_ucomm.
WHEN ‘&IC1’.
READ TABLE gt_alv INTO gs_alv INDEX rs_selfield-tabindex.
IF rs_selfield-fieldname = ‘MATNR’ OR rs_selfield-fieldname = ‘WERKS’ OR rs_selfield-fieldname = ‘KALAB’.
SET PARAMETER ID ‘MAT’ FIELD gs_alv-matnr.
SET PARAMETER ID ‘WRK’ FIELD gs_alv-werks.
CALL TRANSACTION ‘MMBE’ AND SKIP FIRST SCREEN.
ENDIF.
WHEN ‘ZALL’.
LOOP AT gt_alv ASSIGNING FIELD-SYMBOL().
-sel = ‘X’.
ENDLOOP.
WHEN ‘ZSAL’.
LOOP AT gt_alv ASSIGNING .
-sel = ''.
ENDLOOP.
WHEN ‘POST’.
READ TABLE gt_alv INTO gs_alv WITH KEY sel = 'X'.
IF sy-subrc = 0.
PERFORM frm_post.
ELSE.
MESSAGE '请选择行!' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDCASE.
rs_selfield-refresh = ‘X’.
rs_selfield-col_stable = ‘X’.
rs_selfield-row_stable = ‘X’.
ENDFORM.
FORM pf_status USING rt_extab TYPE slis_t_extab.
DATA: ls_extab TYPE slis_extab,
lt_extab TYPE slis_t_extab.
REFRESH lt_extab.
SET PF-STATUS ‘PF_STATUS’ EXCLUDING lt_extab. " 定义Toolbar
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_post
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_post .
DATA goodsmvt_code LIKE bapi2017_gm_code VALUE ‘04’.
DATA goodsmvt_header LIKE bapi2017_gm_head_01.
DATA materialdocument LIKE bapi2017_gm_head_ret-mat_doc.
DATA matdocumentyear LIKE bapi2017_gm_head_ret-doc_year.
DATA goodsmvt_item TYPE bapi2017_gm_item_create OCCURS 0 WITH HEADER LINE.
DATA return TYPE bapiret2 OCCURS 0 WITH HEADER LINE.
goodsmvt_header-pstng_date = sy-datum.
goodsmvt_header-doc_date = sy-datum.
goodsmvt_header-pr_uname = sy-uname.
goodsmvt_header-header_txt = ‘批量跟单转非限制’.
LOOP AT gt_alv INTO gs_alv WHERE sel = ‘X’ AND msg = ‘’.
CLEAR:goodsmvt_item,goodsmvt_item[],return[],materialdocument,matdocumentyear.
IF gs_alv-kalab2 = '' .
gs_alv-kalab2 = gs_alv-kalab.
ENDIF.
IF gs_alv-lgort2 = '' .
gs_alv-lgort2 = gs_alv-lgort.
ENDIF.
goodsmvt_item-move_type = '411'.
goodsmvt_item-item_text = '批量跟单转非限制'.
goodsmvt_item-material = gs_alv-matnr.
goodsmvt_item-entry_qnt = gs_alv-kalab2.
goodsmvt_item-spec_stock = 'E'.
goodsmvt_item-plant = gs_alv-werks. "原工厂
goodsmvt_item-stge_loc = gs_alv-lgort. "原库位
APPEND goodsmvt_item.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = goodsmvt_header
goodsmvt_code = goodsmvt_code
IMPORTING
materialdocument = materialdocument
matdocumentyear = matdocumentyear
TABLES
goodsmvt_item = goodsmvt_item[]
return = return[].
IF materialdocument IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
LOOP AT return WHERE type = 'E'.
gs_alv-msg = gs_alv-msg && '/' && return-message.
ENDLOOP.
gs_alv-incon = icon_red_light.
MODIFY gt_alv FROM gs_alv.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
gs_alv-msg = '已处理'.
gs_alv-incon = icon_green_light.
MODIFY gt_alv FROM gs_alv.
ENDIF.
ENDLOOP.
ENDFORM.