*&---------------------------------------------------------------------*
*& Report ZPPR0036
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zppr0036.
TABLES:mara,t001w.
TABLES:sscrfields,icon,rlgrap.
DATA:gt_fieldcat TYPE lvc_t_fcat,
gw_fieldcat TYPE lvc_s_fcat,
gv_repid TYPE sy-repid,
gs_layout TYPE lvc_s_layo,
gt_sort TYPE lvc_t_sort,
gw_sort TYPE lvc_s_sort.
TYPES:BEGIN OF tys_out,
bapi_mtype TYPE bapi_mtype,
bapi_msg TYPE bapi_msg,
matnr TYPE matnr,
werks TYPE mast-werks,
stlan TYPE mast-stlan,
stlal TYPE stko-stlal,
datuv TYPE stko-datuv,
valid_to TYPE stko-valid_to,
stlst TYPE stlst,
sttxt TYPE t415t-sttxt,
sel(1),
END OF tys_out.
DATA:gt_out TYPE TABLE OF tys_out.
PARAMETERS:p_werks TYPE t001w-werks OBLIGATORY DEFAULT '1000'.
SELECT-OPTIONS:s_matnr FOR mara-matnr.
START-OF-SELECTION.
PERFORM frm_get_data.
PERFORM frm_display_alv.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_get_data .
SELECT mast~matnr,
mast~stlan,
stko~stlal,
mast~werks,
stko~datuv,
stko~valid_to,
stko~stlst,
t415t~sttxt
INTO CORRESPONDING FIELDS OF TABLE @gt_out
FROM stko INNER JOIN mast ON stko~stlnr = mast~stlnr AND stko~stlal = mast~stlal
LEFT JOIN t415t ON t415t~stlst = stko~stlst AND t415t~spras = @sy-langu
WHERE mast~matnr IN @s_matnr AND
mast~werks EQ @p_werks AND
stko~stlty = 'M'.
SORT gt_out BY werks matnr stlan stlal DESCENDING datuv DESCENDING.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_display_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
DATA:g_repid TYPE sy-repid.
g_repid = sy-repid.
"g_layout-zebra = 'X' .
gs_layout-zebra = 'X'.
gs_layout-cwidth_opt = 'X'.
gs_layout-box_fname = 'SEL'.
CLEAR gt_fieldcat[].
PERFORM frm_generate_field_catalogu.
gw_sort-fieldname = 'MATNR'.
gw_sort-down = 'X'.
APPEND gw_sort TO gt_sort.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF'
i_callback_user_command = 'USER_COM'
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat
it_sort_lvc = gt_sort
TABLES
t_outtab = gt_out
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_PF
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM set_pf USING extab TYPE slis_t_extab.
SET TITLEBAR 'TITLE'.
SET PF-STATUS 'STANDARD_FULLSCREEN'.
ENDFORM. "SET_PF
*&---------------------------------------------------------------------*
*& Form user_com
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM user_com USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
DATA: gd_repid LIKE sy-repid,
ref_grid TYPE REF TO cl_gui_alv_grid.
DATA:lv_answer.
IF ref_grid IS INITIAL.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ref_grid.
ENDIF.
IF NOT ref_grid IS INITIAL.
CALL METHOD ref_grid->check_changed_data.
ENDIF.
CASE r_ucomm.
WHEN '&BACK' OR '&EXIT' OR '&EXIT'.
LEAVE TO SCREEN 0.
WHEN '&UPDATE'.
READ TABLE gt_out INTO DATA(lw_out) WITH KEY sel = 'X'.
IF sy-subrc NE 0.
MESSAGE '请选择需要操作的数据' TYPE 'E'.
ENDIF.
PERFORM frm_update_date.
WHEN OTHERS.
ENDCASE.
rs_selfield-refresh = 'X'.
ENDFORM. "USER_COM
*&---------------------------------------------------------------------*
*& Form FRM_GENERATE_FIELD_CATALOGU
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_generate_field_catalogu.
DEFINE def_modify_fieldcat.
&1-scrtext_l = &2.
&1-scrtext_m = &2.
&1-reptext = &2.
&1-scrtext_s = &2.
END-OF-DEFINITION.
TRY.
cl_salv_table=>factory( IMPORTING r_salv_table = DATA(salv_table)
CHANGING t_table = gt_out ).
gt_fieldcat = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
r_columns = salv_table->get_columns( )
r_aggregations = salv_table->get_aggregations( ) ).
CATCH cx_root.
ENDTRY.
LOOP AT gt_fieldcat ASSIGNING FIELD-SYMBOL(<fs_filedcat>).
CASE <fs_filedcat>-fieldname.
WHEN 'SEL'.
<fs_filedcat>-no_out = 'X'.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_UPDATE_DATE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_update_date .
TYPES:BEGIN OF tys_out,
matnr TYPE matnr,
werks TYPE mast-werks,
stlan TYPE mast-stlan,
END OF tys_out.
DATA:lt_out TYPE TABLE OF tys_out.
LOOP AT gt_out INTO DATA(gw_out) WHERE sel = 'X'.
lt_out[] = VALUE #( BASE lt_out ( matnr = gw_out-matnr
werks = gw_out-werks
stlan = gw_out-stlan ) ).
ENDLOOP.
DATA(lt_out_temp) = gt_out[].
LOOP AT lt_out_temp INTO DATA(lw_out_temp).
READ TABLE lt_out INTO DATA(lw_out) WITH KEY matnr = lw_out_temp-matnr
werks = lw_out_temp-werks
stlan = lw_out_temp-stlan .
IF sy-subrc NE 0.
DELETE lt_out_temp.
CONTINUE.
ENDIF.
ENDLOOP.
DATA:lv_material TYPE csap_mbom-matnr,
lv_plant TYPE csap_mbom-werks,
lv_bom_usage TYPE csap_mbom-stlan, "BOM用途
lv_valid_from TYPE csap_mbom-datuv, "有效起始日期
lv_alternative TYPE csap_mbom-stlal. "备选物料清单
DATA:lv_warning TYPE capiflag-flwarning, "出错标识
ls_mbom TYPE csap_mbom, "BOM key信息
ls_stko_i TYPE stko_api01, "BOM头部信息-输入
ls_stko_o TYPE stko_api02. "BOM头部信息-输出
DATA:lt_stpo TYPE TABLE OF stpo_api03,
ls_stpo TYPE stpo_api03.
DATA:lv_line TYPE i.
DATA:lv_string TYPE string.
DATA:lv_bapi_mtype TYPE bapi_mtype,
lv_bapi_msg TYPE bapi_msg.
DATA:
lv_datum TYPE csap_mbom-datuv,
lv_base_quan TYPE p DECIMALS 3,
lv_bmeng TYPE p DECIMALS 3,
lt_stpo_read TYPE TABLE OF stpo_api02,
ls_stpo_read TYPE stpo_api02,
lt_stko_read TYPE TABLE OF stko_api02,
ls_stko_read TYPE stko_api02.
DATA:lv_valid_to TYPE csap_mbom-datuv,
lv_aennr TYPE csap_mbom-aennr.
SORT lt_out_temp BY werks matnr stlan stlal DESCENDING .
LOOP AT lt_out_temp INTO DATA(ls_out) GROUP BY
( matnr = ls_out-matnr
werks = ls_out-werks
stlan = ls_out-stlan
size = GROUP SIZE
index = GROUP INDEX )
ASCENDING ASSIGNING FIELD-SYMBOL(<fs_group>).
CLEAR:lv_line.
lv_line = 0.
LOOP AT GROUP <fs_group> ASSIGNING FIELD-SYMBOL(<ls_group>).
CLEAR:lv_material,lv_plant,lv_bom_usage,ls_stko_i,lv_alternative,lv_string,lv_bapi_mtype,lv_bapi_msg.
REFRESH:lt_stpo_read,lt_stko_read.
lv_line = lv_line + 1.
IF lv_line = '1'.
lv_bom_usage = <ls_group>-stlan.
ELSE.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = <ls_group>-datuv
IMPORTING
date_external = lv_valid_from
EXCEPTIONS
date_internal_is_invalid = 1
OTHERS = 2.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = <ls_group>-valid_to
IMPORTING
date_external = lv_valid_to
EXCEPTIONS
date_internal_is_invalid = 1
OTHERS = 2.
lv_material = <ls_group>-matnr.
lv_plant = <ls_group>-werks.
lv_bom_usage = <ls_group>-stlan.
lv_alternative = <ls_group>-stlal.
* IF lv_alternative = '00'.
* lv_alternative = '0'.
* ENDIF.
CALL FUNCTION 'CSAP_MAT_BOM_READ'
EXPORTING
material = lv_material
plant = lv_plant
bom_usage = lv_bom_usage
alternative = lv_alternative
valid_from = lv_valid_from
valid_to = lv_valid_to
* IMPORTING
* FL_WARNING =
TABLES
t_stpo = lt_stpo_read
t_stko = lt_stko_read
EXCEPTIONS
error = 1
OTHERS = 2.
READ TABLE lt_stko_read INTO ls_stko_read INDEX 1.
MOVE-CORRESPONDING ls_stko_read TO ls_stko_i.
ls_stko_i-bom_status = '02'.
LOOP AT lt_stpo_read INTO ls_stpo_read.
MOVE-CORRESPONDING ls_stpo_read TO ls_stpo.
APPEND ls_stpo TO lt_stpo.
ENDLOOP.
CALL FUNCTION 'CSAP_MAT_BOM_MAINTAIN'
EXPORTING
material = lv_material
plant = lv_plant
bom_usage = lv_bom_usage
alternative = lv_alternative
change_no = lv_aennr
valid_from = lv_valid_from
i_stko = ls_stko_i
fl_commit_and_wait = 'X'
fl_default_values = 'X'
"fl_complete = 'X'
IMPORTING
fl_warning = lv_warning
o_stko = ls_stko_o
* TABLES
* t_stpo = lt_stpo
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc EQ 0.
lv_bapi_mtype = 'S'.
lv_bapi_msg = '修改成功'.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ELSE.
lv_bapi_mtype = 'E'.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = sy-msgid
msgnr = sy-msgno
msgv1 = sy-msgv1
msgv2 = sy-msgv2
msgv3 = sy-msgv3
msgv4 = sy-msgv4
IMPORTING
message_text_output = lv_string.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
lv_bapi_msg = lv_string .
ENDIF.
LOOP AT gt_out ASSIGNING FIELD-SYMBOL(<fs_out>) WHERE matnr = <ls_group>-matnr AND
werks = <ls_group>-werks AND
stlan = <ls_group>-stlan AND
stlal = <ls_group>-stlal.
IF lv_bapi_mtype = 'S'.
<fs_out>-stlst = '02'.
<fs_out>-sttxt = '未激活'.
ENDIF.
<fs_out>-bapi_mtype = lv_bapi_mtype.
<fs_out>-bapi_msg = lv_bapi_msg.
ENDLOOP.
ENDIF.
ENDLOOP.
ENDLOOP.
ENDFORM.