*&---------------------------------------------------------------------*
*& Report YJFIR03
*& Name Jatin Singhal
* Date 05.02.15
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT yjfir03.
TYPES : BEGIN OF gs_mbew,
matnr TYPE matnr,
pstat TYPE pstat_d,
bwkey TYPE bwkey,
bklas TYPE bklas,
stprs TYPE stprs,
lfmon TYPE lfmon,
lfgja TYPE lfgja,
END OF gs_mbew.
TYPES : BEGIN OF gs_mbewh,
matnr TYPE matnr,
stprs TYPE stprs,
lfmon TYPE lfmon,
lfgja TYPE lfgja,
END OF gs_mbewh.
DATA : gv_werks TYPE werks_d,
gv_matnr TYPE matnr,
gv_ean11 TYPE ean11,
gv_mtart TYPE mtart,
gv_matkl TYPE matkl,
gv_lfmon TYPE lfmon,
gv_lfgja TYPE lfgja.
*-----------------------------
*For Testing purpose
DATA : BEGIN OF g_mbew_h1,
matnr TYPE matnr,
pstat TYPE pstat,
End of g_mbew_h1.
DATA g_mbew_h2 LIKE g_mbew_h1.
*------------------------------
DATA : g_flag1 TYPE c,
g_flag2 TYPE c. " Indicator to check data coming from MBEW and MBEWH
DATA :gt_final TYPE TABLE OF yjalv03,
gt_fcat TYPE slis_t_fieldcat_alv.
SELECTION-SCREEN : BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_werks FOR gv_werks OBLIGATORY,
s_matnr FOR gv_matnr,
s_ean11 FOR gv_ean11,
s_mtart FOR gv_mtart,
s_matkl FOR gv_matkl,
s_lfmon FOR gv_lfmon,
s_lfgja FOR gv_lfgja.
SELECTION-SCREEN : END OF BLOCK b01 .
SELECTION-SCREEN : BEGIN OF BLOCK b02 WITH FRAME TITLE text-002.
PARAMETERS : p_layout AS CHECKBOX,
p_cond AS CHECKBOX.
SELECTION-SCREEN : END OF BLOCK b02 .
AT SELECTION-SCREEN.
PERFORM field_validations.
START-OF-SELECTION.
PERFORM get_data.
END-OF-SELECTION.
PERFORM display_data.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data .
DATA: lt_mbew TYPE TABLE OF gs_mbew,
lt_mbewh TYPE TABLE OF gs_mbewh.
FIELD-SYMBOLS : <fs_final> TYPE yjalv03,
<fs_mbew> TYPE gs_mbew,
<fs_mbewh> TYPE gs_mbewh.
IF p_cond <> 'X'.
* Fetching data from MARA and MARC.
SELECT mc~werks mr~matnr normt ean11 ersda ernam mtart matkl
INTO TABLE gt_final
FROM marc AS mc INNER JOIN mara AS mr
ON mc~matnr = mr~matnr
WHERE mc~werks IN s_werks
AND mr~matnr IN s_matnr
AND ean11 IN s_ean11
AND mtart IN s_mtart
AND matkl IN s_matkl.
IF gt_final IS NOT INITIAL.
SELECT mbew~matnr mbew~pstat mbew~bwkey mbew~bklas
mbewh~stprs mbewh~lfmon mbewh~lfgja
INTO TABLE lt_mbew
FROM mbew INNER JOIN mbewh
ON mbew~matnr = mbewh~matnr
FOR ALL ENTRIES IN gt_final
WHERE mbew~matnr = gt_final-matnr
AND mbewh~lfmon IN s_lfmon
AND mbewh~lfgja IN s_lfgja.
IF lt_mbew IS INITIAL.
g_flag1 = 'X'.
RETURN.
ENDIF.
LOOP AT gt_final ASSIGNING <fs_final>.
READ TABLE lt_mbew ASSIGNING <fs_mbew> WITH KEY matnr = <fs_final>-matnr.
IF sy-subrc = 0.
<fs_final>-pstat = <fs_mbew>-pstat.
<fs_final>-bwkey = <fs_mbew>-bwkey.
<fs_final>-bklas = <fs_mbew>-bklas.
<fs_final>-stprs = <fs_mbew>-stprs.
<fs_final>-lfmon = <fs_mbew>-lfmon.
<fs_final>-lfgja = <fs_mbew>-lfgja.
ENDIF.
ENDLOOP.
ENDIF.
* Fetching data using Inner Join on four tables
ELSE.
SELECT mc~werks mr~matnr normt ean11 ersda ernam mtart matkl
mbew~pstat mbew~bwkey mbew~bklas
* mbewh~stprs mbewh~lfmon mbewh~lfgja
INTO TABLE gt_final
FROM marc AS mc
INNER JOIN mara AS mr
ON mc~matnr = mr~matnr
LEFT OUTER JOIN mbew
ON mbew~matnr = mr~matnr
* LEFT OUTER JOIN mbewh
* ON mbewh~matnr = mbew~matnr
WHERE mc~werks IN s_werks
AND mr~matnr IN s_matnr
AND ean11 IN s_ean11
AND mtart IN s_mtart
AND matkl IN s_matkl.
* AND mbewh~lfmon IN s_lfmon
* AND mbewh~lfgja IN s_lfgja.
SELECT matnr stprs lfmon lfgja
INTO TABLE lt_mbewh
FROM mbewh
FOR ALL ENTRIES IN gt_final
WHERE matnr = gt_final-matnr
AND lfmon IN s_lfmon
AND lfgja IN s_lfgja.
IF sy-subrc <> 0.
g_flag2 = 'X'.
RETURN.
ENDIF.
LOOP AT gt_final ASSIGNING <fs_final>.
READ TABLE lt_mbewh ASSIGNING <fs_mbewh> WITH KEY matnr = <fs_final>-matnr.
IF sy-subrc = 0.
<fs_final>-stprs = <fs_mbew>-stprs.
<fs_final>-lfmon = <fs_mbew>-lfmon.
<fs_final>-lfgja = <fs_mbew>-lfgja.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM display_data .
IF gt_final IS INITIAL . " Return to Selection screen if Global Table is blank
RETURN.
ELSEIF g_flag1 = 'X'.
MESSAGE text-004 TYPE 'I'.
ELSEIF g_flag2 = 'X'.
MESSAGE text-005 TYPE 'I'.
ENDIF.
DATA: l_repid TYPE syrepid,
l_title TYPE lvc_title.
* Layout structure
DATA: l_layout TYPE slis_layout_alv.
DATA: l_gridset TYPE lvc_s_glay.
l_layout-info_fieldname = 'COLCODE'.
l_repid = sy-repid.
*Prepare Field catalog for the ALV report
PERFORM setup_fieldcatalog.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = l_repid
* i_callback_pf_status_set = 'SET_STATUS'
i_callback_user_command = 'USER_COMMAND'
* i_callback_top_of_page = 'TOP_OF_PAGE'
* i_background_id = 'ALV_BACKGROUND'
i_grid_title = l_title
i_grid_settings = l_gridset
is_layout = l_layout
it_fieldcat = gt_fcat
i_save = 'A'
TABLES
t_outtab = gt_final
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.
FORM setup_fieldcatalog .
* DATA g_fcat_h TYPE slis_fieldcat_alv.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = 'YJFIR03'
i_structure_name = 'YJALV03'
CHANGING
ct_fieldcat = gt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
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. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form FIELD_VALIDATIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM field_validations .
ENDFORM. " FIELD_VALIDATIONS
*& Report YJFIR03
*& Name Jatin Singhal
* Date 05.02.15
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT yjfir03.
TYPES : BEGIN OF gs_mbew,
matnr TYPE matnr,
pstat TYPE pstat_d,
bwkey TYPE bwkey,
bklas TYPE bklas,
stprs TYPE stprs,
lfmon TYPE lfmon,
lfgja TYPE lfgja,
END OF gs_mbew.
TYPES : BEGIN OF gs_mbewh,
matnr TYPE matnr,
stprs TYPE stprs,
lfmon TYPE lfmon,
lfgja TYPE lfgja,
END OF gs_mbewh.
DATA : gv_werks TYPE werks_d,
gv_matnr TYPE matnr,
gv_ean11 TYPE ean11,
gv_mtart TYPE mtart,
gv_matkl TYPE matkl,
gv_lfmon TYPE lfmon,
gv_lfgja TYPE lfgja.
*-----------------------------
*For Testing purpose
DATA : BEGIN OF g_mbew_h1,
matnr TYPE matnr,
pstat TYPE pstat,
End of g_mbew_h1.
DATA g_mbew_h2 LIKE g_mbew_h1.
*------------------------------
DATA : g_flag1 TYPE c,
g_flag2 TYPE c. " Indicator to check data coming from MBEW and MBEWH
DATA :gt_final TYPE TABLE OF yjalv03,
gt_fcat TYPE slis_t_fieldcat_alv.
SELECTION-SCREEN : BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_werks FOR gv_werks OBLIGATORY,
s_matnr FOR gv_matnr,
s_ean11 FOR gv_ean11,
s_mtart FOR gv_mtart,
s_matkl FOR gv_matkl,
s_lfmon FOR gv_lfmon,
s_lfgja FOR gv_lfgja.
SELECTION-SCREEN : END OF BLOCK b01 .
SELECTION-SCREEN : BEGIN OF BLOCK b02 WITH FRAME TITLE text-002.
PARAMETERS : p_layout AS CHECKBOX,
p_cond AS CHECKBOX.
SELECTION-SCREEN : END OF BLOCK b02 .
AT SELECTION-SCREEN.
PERFORM field_validations.
START-OF-SELECTION.
PERFORM get_data.
END-OF-SELECTION.
PERFORM display_data.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data .
DATA: lt_mbew TYPE TABLE OF gs_mbew,
lt_mbewh TYPE TABLE OF gs_mbewh.
FIELD-SYMBOLS : <fs_final> TYPE yjalv03,
<fs_mbew> TYPE gs_mbew,
<fs_mbewh> TYPE gs_mbewh.
IF p_cond <> 'X'.
* Fetching data from MARA and MARC.
SELECT mc~werks mr~matnr normt ean11 ersda ernam mtart matkl
INTO TABLE gt_final
FROM marc AS mc INNER JOIN mara AS mr
ON mc~matnr = mr~matnr
WHERE mc~werks IN s_werks
AND mr~matnr IN s_matnr
AND ean11 IN s_ean11
AND mtart IN s_mtart
AND matkl IN s_matkl.
IF gt_final IS NOT INITIAL.
SELECT mbew~matnr mbew~pstat mbew~bwkey mbew~bklas
mbewh~stprs mbewh~lfmon mbewh~lfgja
INTO TABLE lt_mbew
FROM mbew INNER JOIN mbewh
ON mbew~matnr = mbewh~matnr
FOR ALL ENTRIES IN gt_final
WHERE mbew~matnr = gt_final-matnr
AND mbewh~lfmon IN s_lfmon
AND mbewh~lfgja IN s_lfgja.
IF lt_mbew IS INITIAL.
g_flag1 = 'X'.
RETURN.
ENDIF.
LOOP AT gt_final ASSIGNING <fs_final>.
READ TABLE lt_mbew ASSIGNING <fs_mbew> WITH KEY matnr = <fs_final>-matnr.
IF sy-subrc = 0.
<fs_final>-pstat = <fs_mbew>-pstat.
<fs_final>-bwkey = <fs_mbew>-bwkey.
<fs_final>-bklas = <fs_mbew>-bklas.
<fs_final>-stprs = <fs_mbew>-stprs.
<fs_final>-lfmon = <fs_mbew>-lfmon.
<fs_final>-lfgja = <fs_mbew>-lfgja.
ENDIF.
ENDLOOP.
ENDIF.
* Fetching data using Inner Join on four tables
ELSE.
SELECT mc~werks mr~matnr normt ean11 ersda ernam mtart matkl
mbew~pstat mbew~bwkey mbew~bklas
* mbewh~stprs mbewh~lfmon mbewh~lfgja
INTO TABLE gt_final
FROM marc AS mc
INNER JOIN mara AS mr
ON mc~matnr = mr~matnr
LEFT OUTER JOIN mbew
ON mbew~matnr = mr~matnr
* LEFT OUTER JOIN mbewh
* ON mbewh~matnr = mbew~matnr
WHERE mc~werks IN s_werks
AND mr~matnr IN s_matnr
AND ean11 IN s_ean11
AND mtart IN s_mtart
AND matkl IN s_matkl.
* AND mbewh~lfmon IN s_lfmon
* AND mbewh~lfgja IN s_lfgja.
SELECT matnr stprs lfmon lfgja
INTO TABLE lt_mbewh
FROM mbewh
FOR ALL ENTRIES IN gt_final
WHERE matnr = gt_final-matnr
AND lfmon IN s_lfmon
AND lfgja IN s_lfgja.
IF sy-subrc <> 0.
g_flag2 = 'X'.
RETURN.
ENDIF.
LOOP AT gt_final ASSIGNING <fs_final>.
READ TABLE lt_mbewh ASSIGNING <fs_mbewh> WITH KEY matnr = <fs_final>-matnr.
IF sy-subrc = 0.
<fs_final>-stprs = <fs_mbew>-stprs.
<fs_final>-lfmon = <fs_mbew>-lfmon.
<fs_final>-lfgja = <fs_mbew>-lfgja.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM display_data .
IF gt_final IS INITIAL . " Return to Selection screen if Global Table is blank
RETURN.
ELSEIF g_flag1 = 'X'.
MESSAGE text-004 TYPE 'I'.
ELSEIF g_flag2 = 'X'.
MESSAGE text-005 TYPE 'I'.
ENDIF.
DATA: l_repid TYPE syrepid,
l_title TYPE lvc_title.
* Layout structure
DATA: l_layout TYPE slis_layout_alv.
DATA: l_gridset TYPE lvc_s_glay.
l_layout-info_fieldname = 'COLCODE'.
l_repid = sy-repid.
*Prepare Field catalog for the ALV report
PERFORM setup_fieldcatalog.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = l_repid
* i_callback_pf_status_set = 'SET_STATUS'
i_callback_user_command = 'USER_COMMAND'
* i_callback_top_of_page = 'TOP_OF_PAGE'
* i_background_id = 'ALV_BACKGROUND'
i_grid_title = l_title
i_grid_settings = l_gridset
is_layout = l_layout
it_fieldcat = gt_fcat
i_save = 'A'
TABLES
t_outtab = gt_final
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.
FORM setup_fieldcatalog .
* DATA g_fcat_h TYPE slis_fieldcat_alv.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = 'YJFIR03'
i_structure_name = 'YJALV03'
CHANGING
ct_fieldcat = gt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
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. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form FIELD_VALIDATIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM field_validations .
ENDFORM. " FIELD_VALIDATIONS