Aim: To get dynamically branch wise stock details based on given input
*&---------------------------------------------------------------------*
*& Report ZEL_BRANCH_WISE_STOCK_REP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zel_branch_wise_stock_rep.
TYPE-POOLS : slis.
TABLES : mara, mard, mseg, mkpf.
"Data type declaration
TYPES : BEGIN OF tp_mara,
matnr TYPE mara-matnr, "Material
mtart TYPE mara-mtart, "Material Type
maktx TYPE makt-maktx, "Material Description
END OF tp_mara.
TYPES : BEGIN OF tp_makt,
matnr TYPE mara-matnr, "Material
maktx TYPE makt-maktx, "Material Description
END OF tp_makt.
TYPES : BEGIN OF tp_mard,
matnr TYPE mard-matnr, "Material
werks TYPE mard-werks, "Plant
lgort TYPE mard-lgort, "Storage Location
labst TYPE mard-labst, "Valuated Unrestricted-Use Stock
insme TYPE mard-insme, "Stock in Quality Inspection
einme TYPE mard-einme, "Total Stock of All Restricted Batches
speme TYPE mard-speme, "Blocked Stock
retme TYPE mard-retme, "Blocked Stock Returns
mtart TYPE mara-mtart, "Material Type
END OF tp_mard.
TYPES : BEGIN OF tp_t001k,
bwkey TYPE t001k-bwkey, "Valuation Area(To featch the respective plant)
bukrs TYPE t001k-bukrs, "Company Code
END OF tp_t001k.
TYPES : BEGIN OF tp_t001w,
werks TYPE t001w-werks, "Plant
name2 TYPE t001w-name2, "Name
END OF tp_t001w.
TYPES : BEGIN OF tp_mbew,
matnr TYPE mbew-matnr,
bwkey TYPE mbew-bwkey,
vprsv TYPE mbew-vprsv, "Price Control Indicator
verpr TYPE mbew-verpr, "Moving Average Price/Periodic Unit Price
stprs TYPE mbew-stprs, "Standard price
END OF tp_mbew.
TYPES : BEGIN OF tp_mbewh,
matnr TYPE mbewh-matnr,
bwkey TYPE mbewh-bwkey,
lfgja TYPE mbewh-lfgja,
lfmon TYPE mbewh-lfmon,
vprsv TYPE mbewh-vprsv, "Price Control Indicator
verpr TYPE mbewh-verpr, "Moving Average Price/Periodic Unit Price
stprs TYPE mbewh-stprs, "Standard price
joint(6) TYPE n,
END OF tp_mbewh.
TYPES : BEGIN OF tp_mslb,
matnr TYPE mslb-matnr,
werks TYPE mslb-werks,
sobkz TYPE mslb-sobkz,
lblab TYPE mslb-lblab,
END OF tp_mslb.
TYPES : BEGIN OF tp_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
trame TYPE marc-trame, "Stock in transit
END OF tp_marc.
TYPES : BEGIN OF tp_final,
matnr TYPE mard-matnr, "Material
maktx TYPE makt-maktx, "Material Description
werks TYPE t001w-werks, "Plant
name2 TYPE t001w-name2, "Name
labst TYPE mard-labst, "Valuated Unrestricted-Use Stock
insme TYPE mard-insme, "Stock in Quality Inspection
einme TYPE mard-einme, "Total Stock of All Restricted Batches
speme TYPE mard-speme, "Blocked Stock
retme TYPE mard-retme, "Blocked Stock Returns
lblab TYPE mslb-lblab, "vendor Stock
trame TYPE marc-trame, "Stock in transit
totst TYPE mard-labst, "Total Stock
stval TYPE mbew-stprs, "Stock Value
END OF tp_final.
DATA : it_fcat TYPE STANDARD TABLE OF lvc_s_fcat,
it_fcatalog TYPE STANDARD TABLE OF slis_fieldcat_alv,
wa_fcat TYPE lvc_s_fcat,
wa_fcatalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv,
it_evt TYPE slis_t_event,
wa_evt TYPE slis_alv_event,
it_fcat1 TYPE STANDARD TABLE OF lvc_s_fcat, "For temp
wa_fcat1 TYPE lvc_s_fcat, "For temp
it_fcat2 TYPE STANDARD TABLE OF lvc_s_fcat, "For temp
wa_fcat2 TYPE lvc_s_fcat. "For temp
DATA : it_dyn_tab TYPE REF TO data,
wa_newline TYPE REF TO data.
DATA : lv_labst TYPE mard-labst,
lv_labst_temp TYPE mard-labst,
lv_stval TYPE zlv_stval, "Stock Value.
lv_stval_temp TYPE zlv_stval. "Stock Value.
FIELD-SYMBOLS : <gt_table> TYPE STANDARD TABLE,
<fs_dyntable>,
<fs_fldval> TYPE ANY,
<l_field> TYPE ANY.
TYPES : BEGIN OF tp_tempfc,
werks TYPE char8,
name2 TYPE char8,
END OF tp_tempfc.
"internal table declaration
DATA : it_mara TYPE STANDARD TABLE OF tp_mara,
wa_mara TYPE tp_mara.
DATA : it_makt TYPE STANDARD TABLE OF tp_makt,
wa_makt TYPE tp_makt.
DATA : it_mard TYPE STANDARD TABLE OF tp_mard,
wa_mard TYPE tp_mard.
DATA : it_marc TYPE STANDARD TABLE OF tp_marc,
wa_marc TYPE tp_marc.
DATA : it_mslb TYPE STANDARD TABLE OF tp_mslb,
wa_mslb TYPE tp_mslb.
DATA : it_mbew TYPE STANDARD TABLE OF tp_mbew, "For current date stock value calculation
wa_mbew TYPE tp_mbew.
DATA : it_mbewh TYPE STANDARD TABLE OF tp_mbewh, "For previous current date stock value calculation
wa_mbewh TYPE tp_mbewh.
DATA : it_t001k TYPE STANDARD TABLE OF tp_t001k,
wa_t001k TYPE tp_t001k.
DATA : it_t001w TYPE STANDARD TABLE OF tp_t001w,
wa_t001w TYPE tp_t001w.
DATA : it_t001w_temp TYPE STANDARD TABLE OF tp_t001w,
wa_t001w_temp TYPE tp_t001w.
DATA : it_final TYPE STANDARD TABLE OF tp_final,
wa_final TYPE tp_final.
DATA : it_finalcp TYPE STANDARD TABLE OF tp_final,
wa_finalcp TYPE tp_final.
DATA : it_finalcp1 TYPE STANDARD TABLE OF tp_final,
wa_finalcp1 TYPE tp_final.
DATA : it_tempfc TYPE STANDARD TABLE OF tp_tempfc,
wa_tempfc TYPE tp_tempfc.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : p_burks TYPE t001k-bukrs DEFAULT '1000' OBLIGATORY.
SELECT-OPTIONS : s_werks FOR mseg-werks.
SELECT-OPTIONS : s_lgort FOR mard-lgort.
SELECT-OPTIONS : s_matnr FOR mard-matnr.
SELECT-OPTIONS : s_mtart FOR mara-mtart.
SELECT-OPTIONS : s_budat FOR mkpf-budat DEFAULT sy-datum OBLIGATORY NO INTERVALS NO-EXTENSION.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS : r_ts1 RADIOBUTTON GROUP g1. "Don't include in transit stock
PARAMETERS : r_ts2 RADIOBUTTON GROUP g1. "Include in transit stock
PARAMETERS : r_ts3 RADIOBUTTON GROUP g1. "Display in transit sotck only
SELECTION-SCREEN END OF BLOCK b2.
START-OF-SELECTION.
IF s_budat-low = sy-datum.
PERFORM get_data.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ELSE.
IF r_ts2 = 'X' OR r_ts3 = 'X'.
MESSAGE text-003 TYPE 'S' DISPLAY LIKE 'W'.
ENDIF.
PERFORM get_data_ufm.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ENDIF.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
FORM get_data .
SELECT bwkey
bukrs
FROM t001k
INTO TABLE it_t001k
WHERE bukrs = p_burks.
IF NOT it_t001k IS INITIAL.
SELECT werks
name2
FROM t001w
INTO TABLE it_t001w
FOR ALL ENTRIES IN it_t001k
WHERE werks = it_t001k-bwkey AND werks IN s_werks.
ENDIF.
IF NOT it_t001w IS INITIAL.
SELECT a~matnr
a~werks
a~lgort
a~labst
a~insme
a~einme
a~speme
a~retme
b~mtart
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
INTO TABLE it_mard
FOR ALL ENTRIES IN it_t001w
WHERE werks = it_t001w-werks AND lgort IN s_lgort AND a~matnr IN s_matnr AND b~mtart IN s_mtart.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
IF NOT it_mard IS INITIAL.
SELECT matnr
maktx
FROM makt
INTO TABLE it_makt
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND spras = sy-langu.
SELECT matnr
bwkey
vprsv
verpr
stprs
FROM mbew
INTO TABLE it_mbew
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND bwkey = it_mard-werks.
SELECT matnr
werks
sobkz
lblab
FROM mslb
INTO TABLE it_mslb
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND werks = it_mard-werks AND sobkz = 'O'.
SELECT matnr
werks
trame
FROM marc
INTO TABLE it_marc
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND werks = it_mard-werks.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
LOOP AT it_mard INTO wa_mard.
wa_final-matnr = wa_mard-matnr.
READ TABLE it_makt INTO wa_makt WITH KEY matnr = wa_final-matnr.
IF sy-subrc = 0.
wa_final-maktx = wa_makt-maktx.
ENDIF.
wa_final-werks = wa_mard-werks.
READ TABLE it_t001w INTO wa_t001w WITH KEY werks = wa_final-werks.
IF sy-subrc = 0.
wa_final-name2 = wa_t001w-name2.
ENDIF.
wa_final-labst = wa_mard-labst.
wa_final-insme = wa_mard-insme.
wa_final-einme = wa_mard-einme.
wa_final-speme = wa_mard-speme.
wa_final-retme = wa_mard-retme.
LOOP AT it_mslb INTO wa_mslb WHERE matnr = wa_final-matnr AND werks = wa_final-werks.
wa_final-lblab = wa_final-lblab + wa_mslb-lblab.
CLEAR : wa_mslb.
ENDLOOP.
READ TABLE it_marc INTO wa_marc WITH KEY matnr = wa_final-matnr
werks = wa_final-werks.
IF sy-subrc = 0.
wa_final-trame = wa_marc-trame.
ENDIF.
READ TABLE it_mbew INTO wa_mbew WITH KEY matnr = wa_final-matnr
bwkey = wa_final-werks.
IF sy-subrc = 0.
IF NOT wa_mbew-vprsv IS INITIAL.
CASE wa_mbew-vprsv.
WHEN 'S'.
wa_final-stval = wa_mbew-stprs.
WHEN 'V'.
wa_final-stval = wa_mbew-verpr.
ENDCASE.
ELSEIF NOT wa_mbew-verpr IS INITIAL.
wa_final-stval = wa_mbew-verpr.
ELSEIF NOT wa_mbew-stprs IS INITIAL.
wa_final-stval = wa_mbew-stprs.
ENDIF.
ENDIF.
APPEND : wa_final TO it_final.
CLEAR : wa_final.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FC
*&---------------------------------------------------------------------*
FORM fill_fc .
DATA : lv_cout TYPE numc2.
it_finalcp = it_final.
SORT it_finalcp BY werks.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING werks.
PERFORM fill_field_catalog USING : 'MATNR' '<GT_TABLE>' 'Material Number' '18' space,
'MAKTX' '<GT_TABLE>' 'Material Description' '40' space.
LOOP AT it_finalcp INTO wa_finalcp.
CLEAR : wa_tempfc.
lv_cout = lv_cout + 1.
CONCATENATE 'WERKS' lv_cout INTO wa_tempfc-werks.
CONCATENATE 'NAME2' lv_cout INTO wa_tempfc-name2.
PERFORM fill_field_catalog USING : wa_tempfc-werks '<GT_TABLE>' wa_finalcp-werks '17' 'MEINS',
wa_tempfc-name2 '<GT_TABLE>' wa_finalcp-name2 '16' space.
wa_tempfc-name2 = wa_finalcp-werks.
APPEND : wa_tempfc TO it_tempfc.
ENDLOOP.
PERFORM fill_field_catalog USING : 'TOTST' '<GT_TABLE>' 'Total Stock' '17' 'MEINS',
'STVAL' '<GT_TABLE>' 'Stock Value' '33' space.
REFRESH : it_finalcp.
wa_layout-zebra = 'X'.
wa_layout-no_colhead = 'X' .
ENDFORM. " FILL_FC
*&---------------------------------------------------------------------*
*& Form FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM fill_field_catalog USING fd_nm tb_nm sl_txt op_ln q_fn.
DATA : lv TYPE numc2.
STATICS pos LIKE sy-index VALUE 0. " static variable
pos = pos + 1.
CLEAR wa_fcat.
IF fd_nm CS 'NAME'.
MOVE 2 TO wa_fcat-row_pos.
pos = pos - 1.
MOVE pos TO wa_fcat-col_pos.
ELSE.
MOVE 1 TO wa_fcat-row_pos.
MOVE pos TO wa_fcat-col_pos.
ENDIF.
MOVE fd_nm TO wa_fcat-fieldname.
MOVE tb_nm TO wa_fcat-tabname.
MOVE sl_txt TO wa_fcat-seltext.
MOVE op_ln TO wa_fcat-outputlen.
MOVE op_ln TO wa_fcat-outputlen.
* MOVE q_fn TO wa_fcat-ref_field.
APPEND wa_fcat TO it_fcat.
CLEAR : wa_fcat.
ENDFORM. " FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
*& Form CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
FORM create_dynamic_it .
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = it_dyn_tab.
ASSIGN it_dyn_tab->* TO <gt_table>.
CREATE DATA wa_newline LIKE LINE OF <gt_table>.
ASSIGN wa_newline->* TO <fs_dyntable>.
it_finalcp = it_final.
it_finalcp1 = it_final.
SORT it_finalcp BY matnr.
SORT it_finalcp1 BY werks.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_finalcp1 COMPARING werks.
"???????????????????????????????????????????????????????????????????????????
IF r_ts1 = 'X'. "Don't Include Transit Stock only
LOOP AT it_finalcp INTO wa_finalcp.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-maktx.
LOOP AT it_finalcp1 INTO wa_finalcp1.
LOOP AT it_final INTO wa_final WHERE matnr = wa_finalcp-matnr AND werks = wa_finalcp1-werks.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY name2 = wa_finalcp1-werks.
IF sy-subrc = 0.
lv_labst = lv_labst + wa_final-labst. "Valuated Unrestricted-Use Stock
lv_labst = lv_labst + wa_final-insme. "Stock in Quality Inspection
lv_labst = lv_labst + wa_final-einme. "Total Stock of All Restricted Batches
lv_labst = lv_labst + wa_final-speme. "Blocked Stock
lv_labst = lv_labst + wa_final-retme. "Blocked Stock Returns
lv_labst = lv_labst + wa_final-lblab. "Vendor stock
"lv_labst = lv_labst + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
ASSIGN COMPONENT wa_tempfc-werks OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
lv_labst_temp = lv_labst_temp + lv_labst.
ENDIF.
CLEAR : lv_labst, wa_finalcp1, lv_stval.
ENDLOOP.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
IF NOT lv_labst_temp IS INITIAL.
INSERT <fs_dyntable> INTO TABLE <gt_table>.
ENDIF.
CLEAR : <fs_dyntable>.
CLEAR : lv_labst, wa_final, wa_finalcp, wa_finalcp1, lv_labst_temp, lv_stval_temp.
ENDLOOP.
"???????????????????????????????????????????????????????????????????????????
ELSEIF r_ts2 = 'X'. "Include Transit Stock only
LOOP AT it_finalcp INTO wa_finalcp.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-maktx.
LOOP AT it_finalcp1 INTO wa_finalcp1.
LOOP AT it_final INTO wa_final WHERE matnr = wa_finalcp-matnr AND werks = wa_finalcp1-werks.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY name2 = wa_finalcp1-werks.
IF sy-subrc = 0.
lv_labst = lv_labst + wa_final-labst. "Valuated Unrestricted-Use Stock
lv_labst = lv_labst + wa_final-insme. "Stock in Quality Inspection
lv_labst = lv_labst + wa_final-einme. "Total Stock of All Restricted Batches
lv_labst = lv_labst + wa_final-speme. "Blocked Stock
lv_labst = lv_labst + wa_final-retme. "Blocked Stock Returns
lv_labst = lv_labst + wa_final-lblab. "Vendor stock
lv_labst = lv_labst + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
ASSIGN COMPONENT wa_tempfc-werks OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
lv_labst_temp = lv_labst_temp + lv_labst.
ENDIF.
CLEAR : lv_labst, wa_finalcp1, lv_stval.
ENDLOOP.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
IF NOT lv_labst_temp IS INITIAL.
INSERT <fs_dyntable> INTO TABLE <gt_table>.
ENDIF.
CLEAR : <fs_dyntable>.
CLEAR : lv_labst, wa_final, wa_finalcp, wa_finalcp1, lv_labst_temp, lv_stval_temp.
ENDLOOP.
"???????????????????????????????????????????????????????????????????????????
ELSEIF r_ts3 = 'X'. "Display Transit Stock only
LOOP AT it_finalcp INTO wa_finalcp.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-maktx.
LOOP AT it_finalcp1 INTO wa_finalcp1.
LOOP AT it_final INTO wa_final WHERE matnr = wa_finalcp-matnr AND werks = wa_finalcp1-werks.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY name2 = wa_finalcp1-werks.
IF sy-subrc = 0.
"lv_labst = lv_labst + wa_final-labst. "Valuated Unrestricted-Use Stock
"lv_labst = lv_labst + wa_final-insme. "Stock in Quality Inspection
"lv_labst = lv_labst + wa_final-einme. "Total Stock of All Restricted Batches
"lv_labst = lv_labst + wa_final-speme. "Blocked Stock
"lv_labst = lv_labst + wa_final-retme. "Blocked Stock Returns
"lv_labst = lv_labst + wa_final-lblab. "Vendor stock
lv_labst = lv_labst + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
ASSIGN COMPONENT wa_tempfc-werks OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
lv_labst_temp = lv_labst_temp + lv_labst.
ENDIF.
CLEAR : lv_labst, wa_finalcp1, lv_stval.
ENDLOOP.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
IF NOT lv_labst_temp IS INITIAL.
INSERT <fs_dyntable> INTO TABLE <gt_table>.
ENDIF.
CLEAR : <fs_dyntable>.
CLEAR : lv_labst, wa_final, wa_finalcp, wa_finalcp1, lv_labst_temp, lv_stval_temp.
ENDLOOP.
ENDIF.
LOOP AT it_fcat INTO wa_fcat WHERE row_pos = 1.
wa_fcatalog-fieldname = wa_fcat-fieldname.
wa_fcatalog-tabname = wa_fcat-tabname.
wa_fcatalog-outputlen = wa_fcat-outputlen.
wa_fcatalog-seltext_l = wa_fcat-seltext.
wa_fcatalog-qfieldname = wa_fcat-ref_field.
* IF NOT wa_fcatalog-qfieldname IS INITIAL.
* wa_fcatalog-qtabname = wa_fcat-ref_table.
* wa_fcatalog-inttype = wa_fcat-inttype.
* ENDIF.
APPEND wa_fcatalog TO it_fcatalog.
CLEAR : wa_fcatalog, wa_fcat.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
et_events = it_evt.
READ TABLE it_evt INTO wa_evt
WITH KEY name = slis_ev_top_of_page .
wa_evt-form = slis_ev_top_of_page .
MODIFY it_evt FROM wa_evt INDEX sy-tabix .
IF NOT <gt_table> IS INITIAL.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fcatalog
it_events = it_evt
TABLES
t_outtab = <gt_table>.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
*& Form top_of_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM top_of_page .
DATA : lv_count TYPE i, "numc4
lv_count1 TYPE i,
lv_lcount TYPE i,
lv_lcount_2 TYPE i,
lv_loop TYPE i.
ULINE.
FORMAT COLOR 7.
"sy-vline
WRITE : / sy-vline, 02 'Material',
20 sy-vline,21 'Material Description'.
lv_lcount = 62.
it_fcat1[] = it_fcat[].
DELETE it_fcat1 WHERE row_pos = 2.
DESCRIBE TABLE it_fcat1 LINES lv_count.
lv_count1 = lv_count.
lv_count = lv_count - 1.
LOOP AT it_fcat1 INTO wa_fcat1 WHERE fieldname CS 'WERKS'.
lv_loop = lv_loop + 1.
ENDLOOP.
IF sy-subrc = 0.
IF lv_loop <> 1. "if plant is multiple
CLEAR : lv_loop.
LOOP AT it_fcat1 INTO wa_fcat1 WHERE row_pos = 1.
lv_loop = lv_loop + 1.
IF sy-tabix => 3.
IF sy-tabix = 3.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSE.
IF lv_count = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSEIF lv_count1 = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
* lv_lcount = lv_lcount + 14.
lv_lcount_2 = lv_lcount + 33.
WRITE : AT lv_lcount_2 sy-vline.
ELSE.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR : lv_lcount, lv_lcount_2, lv_count, lv_loop.
it_fcat2[] = it_fcat[].
DELETE it_fcat2 WHERE row_pos = 1.
DESCRIBE TABLE it_fcat2 LINES lv_count.
* lv_lcount = 62.
FORMAT COLOR 4.
ULINE.
LOOP AT it_fcat2 INTO wa_fcat2 WHERE row_pos = 2.
lv_loop = lv_loop + 1.
IF sy-tabix = 1.
WRITE : AT 1 sy-vline.
lv_lcount = lv_lcount + 20.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 42.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : sy-vline.
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 18.
ELSE.
IF lv_count = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 17.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 18.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 34.
WRITE : AT lv_lcount_2 sy-vline.
ELSE.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 18.
ENDIF.
ENDIF.
ENDLOOP.
ELSE. "if plant is single
CLEAR : lv_loop.
LOOP AT it_fcat1 INTO wa_fcat1 WHERE row_pos = 1.
lv_loop = lv_loop + 1.
IF sy-tabix => 3.
IF sy-tabix = 3.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSE.
IF lv_count = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSEIF lv_count1 = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
* lv_lcount = lv_lcount + 14.
lv_lcount_2 = lv_lcount + 33.
WRITE : AT lv_lcount_2 sy-vline.
ELSE.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR : lv_lcount, lv_lcount_2, lv_count, lv_loop.
it_fcat2[] = it_fcat[].
DELETE it_fcat2 WHERE row_pos = 1.
DESCRIBE TABLE it_fcat2 LINES lv_count.
* lv_lcount = 62.
FORMAT COLOR 4.
ULINE.
LOOP AT it_fcat2 INTO wa_fcat2 WHERE row_pos = 2.
WRITE : AT 1 sy-vline.
lv_lcount = lv_lcount + 20.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 42.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : sy-vline.
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 17.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 18.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 34.
WRITE : AT lv_lcount_2 sy-vline.
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM. "top_of_page
*&---------------------------------------------------------------------*
*& Form GET_DATA_UFM
*&---------------------------------------------------------------------*
FORM get_data_ufm .
DATA : it_stock TYPE STANDARD TABLE OF zmmbe_all_data,
wa_stock TYPE zmmbe_all_data.
DATA : it_stock_temp TYPE STANDARD TABLE OF zmmbe_all_data,
wa_stock_temp TYPE zmmbe_all_data.
DATA : it_store TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store TYPE zmseg_stock_rep.
DATA : it_store_temp TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp TYPE zmseg_stock_rep.
DATA : it_store_temp_matnr TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_matnr TYPE zmseg_stock_rep.
DATA : it_store_temp_werks TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_werks TYPE zmseg_stock_rep.
DATA : s_matnr_fm LIKE STANDARD TABLE OF bapi2017_gm_material_ra,
s_matnr_fm_wa LIKE bapi2017_gm_material_ra,
s_werks_fm LIKE STANDARD TABLE OF bapi2017_gm_plant_ra,
s_werks_fm_wa LIKE bapi2017_gm_plant_ra,
s_lgort_fm LIKE bapi2017_gm_stge_loc_ra OCCURS 0,
s_budat_fm LIKE bapi2017_gm_pstng_date_ra OCCURS 0.
DATA : lv_lc TYPE numc4,
lv_menge TYPE menge_d,
lv_menge_tot TYPE menge_d,
lv_matnr TYPE mara,
lv_temp1 TYPE numc2,
lv_temp2 TYPE numc4,
lv_joint(6) TYPE n.
DATA : lv_fyear TYPE mbewh-lfgja,
lv_period TYPE mbewh-lfmon.
"To calculate correct Financial year and period
CALL FUNCTION 'FTI_FISCAL_YEAR_MONTH_GET'
EXPORTING
i_bukrs = p_burks
i_budat = s_budat-low* I_DZTERM = FTIS_DATUM-INITIAL
* I_GJAHR = FTIS_GJAHR-INITIAL
IMPORTING
e_gjahr = lv_fyear
e_monat = lv_period.
CONCATENATE lv_period lv_fyear INTO lv_joint.
"Stock As on date
s_budat-sign = 'I'.
s_budat-option = 'BT'.
s_budat-high = s_budat-low.
s_budat-low = '19000101'.
REFRESH : s_budat.
APPEND : s_budat.
IF NOT s_matnr IS INITIAL OR NOT s_mtart IS INITIAL.
SELECT a~matnr
a~mtart
b~maktx
INTO TABLE it_mara
FROM mara
AS a
INNER JOIN makt
AS b ON
a~matnr = b~matnr
WHERE a~matnr IN s_matnr AND a~mtart IN s_mtart.
LOOP AT it_mara INTO wa_mara.
s_matnr_fm_wa-sign = 'I'.
s_matnr_fm_wa-option = 'EQ'.
s_matnr_fm_wa-low = wa_mara-matnr.
APPEND : s_matnr_fm_wa TO s_matnr_fm.
CLEAR : wa_mara.
ENDLOOP.
IF s_matnr_fm IS INITIAL.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
SELECT bwkey
bukrs
FROM t001k
INTO TABLE it_t001k
WHERE bukrs = p_burks.
IF NOT it_t001k IS INITIAL.
SELECT werks
name2
FROM t001w
INTO TABLE it_t001w
FOR ALL ENTRIES IN it_t001k
WHERE werks = it_t001k-bwkey AND werks IN s_werks.
it_t001w_temp[] = it_t001w[].
SORT it_t001w_temp BY werks.
DELETE ADJACENT DUPLICATES FROM it_t001w_temp COMPARING werks.
LOOP AT it_t001w_temp INTO wa_t001w_temp.
s_werks_fm_wa-sign = 'I'.
s_werks_fm_wa-option = 'EQ'.
s_werks_fm_wa-low = wa_t001w_temp-werks.
APPEND : s_werks_fm_wa TO s_werks_fm.
CLEAR : wa_t001w_temp.
ENDLOOP.
ENDIF.
SELECT a~matnr
a~werks
a~lgort
a~labst
a~insme
a~einme
a~speme
a~retme
b~mtart
INTO TABLE it_mard
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
WHERE werks IN s_werks_fm AND lgort IN s_lgort AND a~matnr IN s_matnr_fm AND b~mtart IN s_mtart.
IF it_mard IS INITIAL.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
* s_matnr_fm[] = s_matnr[].
* s_werks_fm[] = s_werks[].
s_lgort_fm[] = s_lgort[].
s_budat_fm[] = s_budat[].
"For stock
CALL FUNCTION 'ZMM_GET_MAT_STK_ALL_DATA'
EXPORTING
p_lgbst = 'X'
TABLES
material_ra = s_matnr_fm
plant_ra = s_werks_fm
stge_loc_ra = s_lgort_fm
pstng_date_ra = s_budat_fm
it_bestand = it_stock_temp
it_fm_mseg = it_store_temp
EXCEPTIONS
selectone = 1
spl_stock = 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.
it_stock[] = it_stock_temp[].
it_store[] = it_store_temp[].
REFRESH : it_stock_temp, it_store_temp.
"For Vendor stock
CALL FUNCTION 'ZMM_GET_MAT_STK_ALL_DATA'
EXPORTING
p_sbbst = 'X'
p_sobkz = 'O'
TABLES
material_ra = s_matnr_fm
plant_ra = s_werks_fm
stge_loc_ra = s_lgort_fm
pstng_date_ra = s_budat_fm
it_bestand = it_stock_temp
it_fm_mseg = it_store_temp
EXCEPTIONS
selectone = 1
spl_stock = 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.
* "Assign temp table (it_stock_temp) value into main table (it_stock)
* LOOP AT it_stock_temp INTO wa_stock_temp.
* wa_stock = wa_stock_temp.
* APPEND : wa_stock to it_stock.
* CLEAR : wa_stock_temp, wa_stock.
* ENDLOOP.
"Assign temp table (it_store_temp) value into main table (it_store)
LOOP AT it_store_temp INTO wa_store_temp.
wa_store = wa_store_temp.
APPEND : wa_store TO it_store.
CLEAR : wa_store_temp, wa_store.
ENDLOOP.
IF it_mara IS INITIAL.
SELECT a~matnr
a~mtart
b~maktx
INTO TABLE it_mara
FROM mara
AS a
INNER JOIN makt
AS b ON
a~matnr = b~matnr
FOR ALL ENTRIES IN it_store
WHERE a~matnr = it_store-matnr.
ENDIF.
IF NOT it_store IS INITIAL.
SELECT matnr
bwkey
lfgja
lfmon
vprsv
verpr
stprs
FROM mbewh
INTO TABLE it_mbewh
FOR ALL ENTRIES IN it_store
WHERE matnr = it_store-matnr AND bwkey = it_store-werks." AND lfgja = lv_fyear.
ENDIF.
LOOP AT it_mbewh INTO wa_mbewh.
lv_temp1 = wa_mbewh-lfmon.
lv_temp2 = wa_mbewh-lfgja.
CONCATENATE lv_temp1 lv_temp2 INTO wa_mbewh-joint.
MODIFY it_mbewh FROM wa_mbewh INDEX sy-tabix TRANSPORTING joint.
CLEAR : wa_mbewh, lv_temp1, lv_temp2.
ENDLOOP.
it_store_temp_matnr[] = it_store[].
it_store_temp_werks[] = it_store[].
* SORT it_store_temp_matnr BY matnr.
SORT it_store_temp_werks BY matnr werks.
SORT it_mbewh BY joint DESCENDING.
* DELETE ADJACENT DUPLICATES FROM it_store_temp_matnr COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_store_temp_werks COMPARING matnr werks.
LOOP AT it_store_temp_werks INTO wa_store_temp_werks.
wa_final-matnr = wa_store_temp_werks-matnr.
READ TABLE it_mara INTO wa_mara WITH KEY matnr = wa_final-matnr.
IF sy-subrc = 0.
wa_final-maktx = wa_mara-maktx.
ENDIF.
LOOP AT it_store INTO wa_store WHERE matnr = wa_store_temp_werks-matnr AND
werks = wa_store_temp_werks-werks.
IF wa_store-shkzg = 'S'.
lv_menge = lv_menge + wa_store-menge.
ELSEIF wa_store-shkzg = 'H'.
lv_menge = lv_menge - wa_store-menge.
ENDIF.
ENDLOOP.
wa_final-labst = lv_menge.
wa_final-werks = wa_store-werks.
READ TABLE it_t001w INTO wa_t001w WITH KEY werks = wa_final-werks.
IF sy-subrc = 0.
wa_final-name2 = wa_t001w-name2.
ENDIF.
LOOP AT it_mbewh INTO wa_mbewh WHERE matnr = wa_final-matnr AND bwkey = wa_final-werks AND
joint <= lv_joint.
IF NOT wa_mbewh-vprsv IS INITIAL.
CASE wa_mbewh-vprsv.
WHEN 'S'.
wa_final-stval = wa_mbewh-stprs.
WHEN 'V'.
wa_final-stval = wa_mbewh-verpr.
ENDCASE.
ELSEIF NOT wa_mbewh-verpr IS INITIAL.
wa_final-stval = wa_mbewh-verpr.
ELSEIF NOT wa_mbewh-stprs IS INITIAL.
wa_final-stval = wa_mbewh-stprs.
ENDIF.
EXIT. "After getting first value exit the loop.
ENDLOOP.
APPEND : wa_final TO it_final.
CLEAR : lv_menge, wa_store.
ENDLOOP.
ENDFORM. " GET_DATA_UFM
*&---------------------------------------------------------------------*
*& Report ZEL_BRANCH_WISE_STOCK_REP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zel_branch_wise_stock_rep.
TYPE-POOLS : slis.
TABLES : mara, mard, mseg, mkpf.
"Data type declaration
TYPES : BEGIN OF tp_mara,
matnr TYPE mara-matnr, "Material
mtart TYPE mara-mtart, "Material Type
maktx TYPE makt-maktx, "Material Description
END OF tp_mara.
TYPES : BEGIN OF tp_makt,
matnr TYPE mara-matnr, "Material
maktx TYPE makt-maktx, "Material Description
END OF tp_makt.
TYPES : BEGIN OF tp_mard,
matnr TYPE mard-matnr, "Material
werks TYPE mard-werks, "Plant
lgort TYPE mard-lgort, "Storage Location
labst TYPE mard-labst, "Valuated Unrestricted-Use Stock
insme TYPE mard-insme, "Stock in Quality Inspection
einme TYPE mard-einme, "Total Stock of All Restricted Batches
speme TYPE mard-speme, "Blocked Stock
retme TYPE mard-retme, "Blocked Stock Returns
mtart TYPE mara-mtart, "Material Type
END OF tp_mard.
TYPES : BEGIN OF tp_t001k,
bwkey TYPE t001k-bwkey, "Valuation Area(To featch the respective plant)
bukrs TYPE t001k-bukrs, "Company Code
END OF tp_t001k.
TYPES : BEGIN OF tp_t001w,
werks TYPE t001w-werks, "Plant
name2 TYPE t001w-name2, "Name
END OF tp_t001w.
TYPES : BEGIN OF tp_mbew,
matnr TYPE mbew-matnr,
bwkey TYPE mbew-bwkey,
vprsv TYPE mbew-vprsv, "Price Control Indicator
verpr TYPE mbew-verpr, "Moving Average Price/Periodic Unit Price
stprs TYPE mbew-stprs, "Standard price
END OF tp_mbew.
TYPES : BEGIN OF tp_mbewh,
matnr TYPE mbewh-matnr,
bwkey TYPE mbewh-bwkey,
lfgja TYPE mbewh-lfgja,
lfmon TYPE mbewh-lfmon,
vprsv TYPE mbewh-vprsv, "Price Control Indicator
verpr TYPE mbewh-verpr, "Moving Average Price/Periodic Unit Price
stprs TYPE mbewh-stprs, "Standard price
joint(6) TYPE n,
END OF tp_mbewh.
TYPES : BEGIN OF tp_mslb,
matnr TYPE mslb-matnr,
werks TYPE mslb-werks,
sobkz TYPE mslb-sobkz,
lblab TYPE mslb-lblab,
END OF tp_mslb.
TYPES : BEGIN OF tp_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
trame TYPE marc-trame, "Stock in transit
END OF tp_marc.
TYPES : BEGIN OF tp_final,
matnr TYPE mard-matnr, "Material
maktx TYPE makt-maktx, "Material Description
werks TYPE t001w-werks, "Plant
name2 TYPE t001w-name2, "Name
labst TYPE mard-labst, "Valuated Unrestricted-Use Stock
insme TYPE mard-insme, "Stock in Quality Inspection
einme TYPE mard-einme, "Total Stock of All Restricted Batches
speme TYPE mard-speme, "Blocked Stock
retme TYPE mard-retme, "Blocked Stock Returns
lblab TYPE mslb-lblab, "vendor Stock
trame TYPE marc-trame, "Stock in transit
totst TYPE mard-labst, "Total Stock
stval TYPE mbew-stprs, "Stock Value
END OF tp_final.
DATA : it_fcat TYPE STANDARD TABLE OF lvc_s_fcat,
it_fcatalog TYPE STANDARD TABLE OF slis_fieldcat_alv,
wa_fcat TYPE lvc_s_fcat,
wa_fcatalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv,
it_evt TYPE slis_t_event,
wa_evt TYPE slis_alv_event,
it_fcat1 TYPE STANDARD TABLE OF lvc_s_fcat, "For temp
wa_fcat1 TYPE lvc_s_fcat, "For temp
it_fcat2 TYPE STANDARD TABLE OF lvc_s_fcat, "For temp
wa_fcat2 TYPE lvc_s_fcat. "For temp
DATA : it_dyn_tab TYPE REF TO data,
wa_newline TYPE REF TO data.
DATA : lv_labst TYPE mard-labst,
lv_labst_temp TYPE mard-labst,
lv_stval TYPE zlv_stval, "Stock Value.
lv_stval_temp TYPE zlv_stval. "Stock Value.
FIELD-SYMBOLS : <gt_table> TYPE STANDARD TABLE,
<fs_dyntable>,
<fs_fldval> TYPE ANY,
<l_field> TYPE ANY.
TYPES : BEGIN OF tp_tempfc,
werks TYPE char8,
name2 TYPE char8,
END OF tp_tempfc.
"internal table declaration
DATA : it_mara TYPE STANDARD TABLE OF tp_mara,
wa_mara TYPE tp_mara.
DATA : it_makt TYPE STANDARD TABLE OF tp_makt,
wa_makt TYPE tp_makt.
DATA : it_mard TYPE STANDARD TABLE OF tp_mard,
wa_mard TYPE tp_mard.
DATA : it_marc TYPE STANDARD TABLE OF tp_marc,
wa_marc TYPE tp_marc.
DATA : it_mslb TYPE STANDARD TABLE OF tp_mslb,
wa_mslb TYPE tp_mslb.
DATA : it_mbew TYPE STANDARD TABLE OF tp_mbew, "For current date stock value calculation
wa_mbew TYPE tp_mbew.
DATA : it_mbewh TYPE STANDARD TABLE OF tp_mbewh, "For previous current date stock value calculation
wa_mbewh TYPE tp_mbewh.
DATA : it_t001k TYPE STANDARD TABLE OF tp_t001k,
wa_t001k TYPE tp_t001k.
DATA : it_t001w TYPE STANDARD TABLE OF tp_t001w,
wa_t001w TYPE tp_t001w.
DATA : it_t001w_temp TYPE STANDARD TABLE OF tp_t001w,
wa_t001w_temp TYPE tp_t001w.
DATA : it_final TYPE STANDARD TABLE OF tp_final,
wa_final TYPE tp_final.
DATA : it_finalcp TYPE STANDARD TABLE OF tp_final,
wa_finalcp TYPE tp_final.
DATA : it_finalcp1 TYPE STANDARD TABLE OF tp_final,
wa_finalcp1 TYPE tp_final.
DATA : it_tempfc TYPE STANDARD TABLE OF tp_tempfc,
wa_tempfc TYPE tp_tempfc.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : p_burks TYPE t001k-bukrs DEFAULT '1000' OBLIGATORY.
SELECT-OPTIONS : s_werks FOR mseg-werks.
SELECT-OPTIONS : s_lgort FOR mard-lgort.
SELECT-OPTIONS : s_matnr FOR mard-matnr.
SELECT-OPTIONS : s_mtart FOR mara-mtart.
SELECT-OPTIONS : s_budat FOR mkpf-budat DEFAULT sy-datum OBLIGATORY NO INTERVALS NO-EXTENSION.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS : r_ts1 RADIOBUTTON GROUP g1. "Don't include in transit stock
PARAMETERS : r_ts2 RADIOBUTTON GROUP g1. "Include in transit stock
PARAMETERS : r_ts3 RADIOBUTTON GROUP g1. "Display in transit sotck only
SELECTION-SCREEN END OF BLOCK b2.
START-OF-SELECTION.
IF s_budat-low = sy-datum.
PERFORM get_data.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ELSE.
IF r_ts2 = 'X' OR r_ts3 = 'X'.
MESSAGE text-003 TYPE 'S' DISPLAY LIKE 'W'.
ENDIF.
PERFORM get_data_ufm.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ENDIF.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
FORM get_data .
SELECT bwkey
bukrs
FROM t001k
INTO TABLE it_t001k
WHERE bukrs = p_burks.
IF NOT it_t001k IS INITIAL.
SELECT werks
name2
FROM t001w
INTO TABLE it_t001w
FOR ALL ENTRIES IN it_t001k
WHERE werks = it_t001k-bwkey AND werks IN s_werks.
ENDIF.
IF NOT it_t001w IS INITIAL.
SELECT a~matnr
a~werks
a~lgort
a~labst
a~insme
a~einme
a~speme
a~retme
b~mtart
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
INTO TABLE it_mard
FOR ALL ENTRIES IN it_t001w
WHERE werks = it_t001w-werks AND lgort IN s_lgort AND a~matnr IN s_matnr AND b~mtart IN s_mtart.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
IF NOT it_mard IS INITIAL.
SELECT matnr
maktx
FROM makt
INTO TABLE it_makt
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND spras = sy-langu.
SELECT matnr
bwkey
vprsv
verpr
stprs
FROM mbew
INTO TABLE it_mbew
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND bwkey = it_mard-werks.
SELECT matnr
werks
sobkz
lblab
FROM mslb
INTO TABLE it_mslb
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND werks = it_mard-werks AND sobkz = 'O'.
SELECT matnr
werks
trame
FROM marc
INTO TABLE it_marc
FOR ALL ENTRIES IN it_mard
WHERE matnr = it_mard-matnr AND werks = it_mard-werks.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
LOOP AT it_mard INTO wa_mard.
wa_final-matnr = wa_mard-matnr.
READ TABLE it_makt INTO wa_makt WITH KEY matnr = wa_final-matnr.
IF sy-subrc = 0.
wa_final-maktx = wa_makt-maktx.
ENDIF.
wa_final-werks = wa_mard-werks.
READ TABLE it_t001w INTO wa_t001w WITH KEY werks = wa_final-werks.
IF sy-subrc = 0.
wa_final-name2 = wa_t001w-name2.
ENDIF.
wa_final-labst = wa_mard-labst.
wa_final-insme = wa_mard-insme.
wa_final-einme = wa_mard-einme.
wa_final-speme = wa_mard-speme.
wa_final-retme = wa_mard-retme.
LOOP AT it_mslb INTO wa_mslb WHERE matnr = wa_final-matnr AND werks = wa_final-werks.
wa_final-lblab = wa_final-lblab + wa_mslb-lblab.
CLEAR : wa_mslb.
ENDLOOP.
READ TABLE it_marc INTO wa_marc WITH KEY matnr = wa_final-matnr
werks = wa_final-werks.
IF sy-subrc = 0.
wa_final-trame = wa_marc-trame.
ENDIF.
READ TABLE it_mbew INTO wa_mbew WITH KEY matnr = wa_final-matnr
bwkey = wa_final-werks.
IF sy-subrc = 0.
IF NOT wa_mbew-vprsv IS INITIAL.
CASE wa_mbew-vprsv.
WHEN 'S'.
wa_final-stval = wa_mbew-stprs.
WHEN 'V'.
wa_final-stval = wa_mbew-verpr.
ENDCASE.
ELSEIF NOT wa_mbew-verpr IS INITIAL.
wa_final-stval = wa_mbew-verpr.
ELSEIF NOT wa_mbew-stprs IS INITIAL.
wa_final-stval = wa_mbew-stprs.
ENDIF.
ENDIF.
APPEND : wa_final TO it_final.
CLEAR : wa_final.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FC
*&---------------------------------------------------------------------*
FORM fill_fc .
DATA : lv_cout TYPE numc2.
it_finalcp = it_final.
SORT it_finalcp BY werks.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING werks.
PERFORM fill_field_catalog USING : 'MATNR' '<GT_TABLE>' 'Material Number' '18' space,
'MAKTX' '<GT_TABLE>' 'Material Description' '40' space.
LOOP AT it_finalcp INTO wa_finalcp.
CLEAR : wa_tempfc.
lv_cout = lv_cout + 1.
CONCATENATE 'WERKS' lv_cout INTO wa_tempfc-werks.
CONCATENATE 'NAME2' lv_cout INTO wa_tempfc-name2.
PERFORM fill_field_catalog USING : wa_tempfc-werks '<GT_TABLE>' wa_finalcp-werks '17' 'MEINS',
wa_tempfc-name2 '<GT_TABLE>' wa_finalcp-name2 '16' space.
wa_tempfc-name2 = wa_finalcp-werks.
APPEND : wa_tempfc TO it_tempfc.
ENDLOOP.
PERFORM fill_field_catalog USING : 'TOTST' '<GT_TABLE>' 'Total Stock' '17' 'MEINS',
'STVAL' '<GT_TABLE>' 'Stock Value' '33' space.
REFRESH : it_finalcp.
wa_layout-zebra = 'X'.
wa_layout-no_colhead = 'X' .
ENDFORM. " FILL_FC
*&---------------------------------------------------------------------*
*& Form FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM fill_field_catalog USING fd_nm tb_nm sl_txt op_ln q_fn.
DATA : lv TYPE numc2.
STATICS pos LIKE sy-index VALUE 0. " static variable
pos = pos + 1.
CLEAR wa_fcat.
IF fd_nm CS 'NAME'.
MOVE 2 TO wa_fcat-row_pos.
pos = pos - 1.
MOVE pos TO wa_fcat-col_pos.
ELSE.
MOVE 1 TO wa_fcat-row_pos.
MOVE pos TO wa_fcat-col_pos.
ENDIF.
MOVE fd_nm TO wa_fcat-fieldname.
MOVE tb_nm TO wa_fcat-tabname.
MOVE sl_txt TO wa_fcat-seltext.
MOVE op_ln TO wa_fcat-outputlen.
MOVE op_ln TO wa_fcat-outputlen.
* MOVE q_fn TO wa_fcat-ref_field.
APPEND wa_fcat TO it_fcat.
CLEAR : wa_fcat.
ENDFORM. " FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
*& Form CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
FORM create_dynamic_it .
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = it_dyn_tab.
ASSIGN it_dyn_tab->* TO <gt_table>.
CREATE DATA wa_newline LIKE LINE OF <gt_table>.
ASSIGN wa_newline->* TO <fs_dyntable>.
it_finalcp = it_final.
it_finalcp1 = it_final.
SORT it_finalcp BY matnr.
SORT it_finalcp1 BY werks.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_finalcp1 COMPARING werks.
"???????????????????????????????????????????????????????????????????????????
IF r_ts1 = 'X'. "Don't Include Transit Stock only
LOOP AT it_finalcp INTO wa_finalcp.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-maktx.
LOOP AT it_finalcp1 INTO wa_finalcp1.
LOOP AT it_final INTO wa_final WHERE matnr = wa_finalcp-matnr AND werks = wa_finalcp1-werks.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY name2 = wa_finalcp1-werks.
IF sy-subrc = 0.
lv_labst = lv_labst + wa_final-labst. "Valuated Unrestricted-Use Stock
lv_labst = lv_labst + wa_final-insme. "Stock in Quality Inspection
lv_labst = lv_labst + wa_final-einme. "Total Stock of All Restricted Batches
lv_labst = lv_labst + wa_final-speme. "Blocked Stock
lv_labst = lv_labst + wa_final-retme. "Blocked Stock Returns
lv_labst = lv_labst + wa_final-lblab. "Vendor stock
"lv_labst = lv_labst + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
ASSIGN COMPONENT wa_tempfc-werks OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
lv_labst_temp = lv_labst_temp + lv_labst.
ENDIF.
CLEAR : lv_labst, wa_finalcp1, lv_stval.
ENDLOOP.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
IF NOT lv_labst_temp IS INITIAL.
INSERT <fs_dyntable> INTO TABLE <gt_table>.
ENDIF.
CLEAR : <fs_dyntable>.
CLEAR : lv_labst, wa_final, wa_finalcp, wa_finalcp1, lv_labst_temp, lv_stval_temp.
ENDLOOP.
"???????????????????????????????????????????????????????????????????????????
ELSEIF r_ts2 = 'X'. "Include Transit Stock only
LOOP AT it_finalcp INTO wa_finalcp.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-maktx.
LOOP AT it_finalcp1 INTO wa_finalcp1.
LOOP AT it_final INTO wa_final WHERE matnr = wa_finalcp-matnr AND werks = wa_finalcp1-werks.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY name2 = wa_finalcp1-werks.
IF sy-subrc = 0.
lv_labst = lv_labst + wa_final-labst. "Valuated Unrestricted-Use Stock
lv_labst = lv_labst + wa_final-insme. "Stock in Quality Inspection
lv_labst = lv_labst + wa_final-einme. "Total Stock of All Restricted Batches
lv_labst = lv_labst + wa_final-speme. "Blocked Stock
lv_labst = lv_labst + wa_final-retme. "Blocked Stock Returns
lv_labst = lv_labst + wa_final-lblab. "Vendor stock
lv_labst = lv_labst + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
ASSIGN COMPONENT wa_tempfc-werks OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
lv_labst_temp = lv_labst_temp + lv_labst.
ENDIF.
CLEAR : lv_labst, wa_finalcp1, lv_stval.
ENDLOOP.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
IF NOT lv_labst_temp IS INITIAL.
INSERT <fs_dyntable> INTO TABLE <gt_table>.
ENDIF.
CLEAR : <fs_dyntable>.
CLEAR : lv_labst, wa_final, wa_finalcp, wa_finalcp1, lv_labst_temp, lv_stval_temp.
ENDLOOP.
"???????????????????????????????????????????????????????????????????????????
ELSEIF r_ts3 = 'X'. "Display Transit Stock only
LOOP AT it_finalcp INTO wa_finalcp.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-matnr.
ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = wa_finalcp-maktx.
LOOP AT it_finalcp1 INTO wa_finalcp1.
LOOP AT it_final INTO wa_final WHERE matnr = wa_finalcp-matnr AND werks = wa_finalcp1-werks.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY name2 = wa_finalcp1-werks.
IF sy-subrc = 0.
"lv_labst = lv_labst + wa_final-labst. "Valuated Unrestricted-Use Stock
"lv_labst = lv_labst + wa_final-insme. "Stock in Quality Inspection
"lv_labst = lv_labst + wa_final-einme. "Total Stock of All Restricted Batches
"lv_labst = lv_labst + wa_final-speme. "Blocked Stock
"lv_labst = lv_labst + wa_final-retme. "Blocked Stock Returns
"lv_labst = lv_labst + wa_final-lblab. "Vendor stock
lv_labst = lv_labst + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
ASSIGN COMPONENT wa_tempfc-werks OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
lv_labst_temp = lv_labst_temp + lv_labst.
ENDIF.
CLEAR : lv_labst, wa_finalcp1, lv_stval.
ENDLOOP.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
IF NOT lv_labst_temp IS INITIAL.
INSERT <fs_dyntable> INTO TABLE <gt_table>.
ENDIF.
CLEAR : <fs_dyntable>.
CLEAR : lv_labst, wa_final, wa_finalcp, wa_finalcp1, lv_labst_temp, lv_stval_temp.
ENDLOOP.
ENDIF.
LOOP AT it_fcat INTO wa_fcat WHERE row_pos = 1.
wa_fcatalog-fieldname = wa_fcat-fieldname.
wa_fcatalog-tabname = wa_fcat-tabname.
wa_fcatalog-outputlen = wa_fcat-outputlen.
wa_fcatalog-seltext_l = wa_fcat-seltext.
wa_fcatalog-qfieldname = wa_fcat-ref_field.
* IF NOT wa_fcatalog-qfieldname IS INITIAL.
* wa_fcatalog-qtabname = wa_fcat-ref_table.
* wa_fcatalog-inttype = wa_fcat-inttype.
* ENDIF.
APPEND wa_fcatalog TO it_fcatalog.
CLEAR : wa_fcatalog, wa_fcat.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
et_events = it_evt.
READ TABLE it_evt INTO wa_evt
WITH KEY name = slis_ev_top_of_page .
wa_evt-form = slis_ev_top_of_page .
MODIFY it_evt FROM wa_evt INDEX sy-tabix .
IF NOT <gt_table> IS INITIAL.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fcatalog
it_events = it_evt
TABLES
t_outtab = <gt_table>.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
*& Form top_of_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM top_of_page .
DATA : lv_count TYPE i, "numc4
lv_count1 TYPE i,
lv_lcount TYPE i,
lv_lcount_2 TYPE i,
lv_loop TYPE i.
ULINE.
FORMAT COLOR 7.
"sy-vline
WRITE : / sy-vline, 02 'Material',
20 sy-vline,21 'Material Description'.
lv_lcount = 62.
it_fcat1[] = it_fcat[].
DELETE it_fcat1 WHERE row_pos = 2.
DESCRIBE TABLE it_fcat1 LINES lv_count.
lv_count1 = lv_count.
lv_count = lv_count - 1.
LOOP AT it_fcat1 INTO wa_fcat1 WHERE fieldname CS 'WERKS'.
lv_loop = lv_loop + 1.
ENDLOOP.
IF sy-subrc = 0.
IF lv_loop <> 1. "if plant is multiple
CLEAR : lv_loop.
LOOP AT it_fcat1 INTO wa_fcat1 WHERE row_pos = 1.
lv_loop = lv_loop + 1.
IF sy-tabix => 3.
IF sy-tabix = 3.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSE.
IF lv_count = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSEIF lv_count1 = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
* lv_lcount = lv_lcount + 14.
lv_lcount_2 = lv_lcount + 33.
WRITE : AT lv_lcount_2 sy-vline.
ELSE.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR : lv_lcount, lv_lcount_2, lv_count, lv_loop.
it_fcat2[] = it_fcat[].
DELETE it_fcat2 WHERE row_pos = 1.
DESCRIBE TABLE it_fcat2 LINES lv_count.
* lv_lcount = 62.
FORMAT COLOR 4.
ULINE.
LOOP AT it_fcat2 INTO wa_fcat2 WHERE row_pos = 2.
lv_loop = lv_loop + 1.
IF sy-tabix = 1.
WRITE : AT 1 sy-vline.
lv_lcount = lv_lcount + 20.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 42.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : sy-vline.
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 18.
ELSE.
IF lv_count = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 17.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 18.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 34.
WRITE : AT lv_lcount_2 sy-vline.
ELSE.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 18.
ENDIF.
ENDIF.
ENDLOOP.
ELSE. "if plant is single
CLEAR : lv_loop.
LOOP AT it_fcat1 INTO wa_fcat1 WHERE row_pos = 1.
lv_loop = lv_loop + 1.
IF sy-tabix => 3.
IF sy-tabix = 3.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSE.
IF lv_count = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ELSEIF lv_count1 = lv_loop.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
* lv_lcount = lv_lcount + 14.
lv_lcount_2 = lv_lcount + 33.
WRITE : AT lv_lcount_2 sy-vline.
ELSE.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat1-seltext.
lv_lcount = lv_lcount + 18.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR : lv_lcount, lv_lcount_2, lv_count, lv_loop.
it_fcat2[] = it_fcat[].
DELETE it_fcat2 WHERE row_pos = 1.
DESCRIBE TABLE it_fcat2 LINES lv_count.
* lv_lcount = 62.
FORMAT COLOR 4.
ULINE.
LOOP AT it_fcat2 INTO wa_fcat2 WHERE row_pos = 2.
WRITE : AT 1 sy-vline.
lv_lcount = lv_lcount + 20.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 42.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 8.
lv_lcount_2 = lv_lcount - 1."test
WRITE : sy-vline.
WRITE : AT lv_lcount_2 sy-vline, AT lv_lcount wa_fcat2-seltext.
lv_lcount = lv_lcount + 17.
WRITE : AT lv_lcount sy-vline.
lv_lcount = lv_lcount + 18.
WRITE : AT lv_lcount sy-vline.
lv_lcount_2 = lv_lcount + 34.
WRITE : AT lv_lcount_2 sy-vline.
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM. "top_of_page
*&---------------------------------------------------------------------*
*& Form GET_DATA_UFM
*&---------------------------------------------------------------------*
FORM get_data_ufm .
DATA : it_stock TYPE STANDARD TABLE OF zmmbe_all_data,
wa_stock TYPE zmmbe_all_data.
DATA : it_stock_temp TYPE STANDARD TABLE OF zmmbe_all_data,
wa_stock_temp TYPE zmmbe_all_data.
DATA : it_store TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store TYPE zmseg_stock_rep.
DATA : it_store_temp TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp TYPE zmseg_stock_rep.
DATA : it_store_temp_matnr TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_matnr TYPE zmseg_stock_rep.
DATA : it_store_temp_werks TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_werks TYPE zmseg_stock_rep.
DATA : s_matnr_fm LIKE STANDARD TABLE OF bapi2017_gm_material_ra,
s_matnr_fm_wa LIKE bapi2017_gm_material_ra,
s_werks_fm LIKE STANDARD TABLE OF bapi2017_gm_plant_ra,
s_werks_fm_wa LIKE bapi2017_gm_plant_ra,
s_lgort_fm LIKE bapi2017_gm_stge_loc_ra OCCURS 0,
s_budat_fm LIKE bapi2017_gm_pstng_date_ra OCCURS 0.
DATA : lv_lc TYPE numc4,
lv_menge TYPE menge_d,
lv_menge_tot TYPE menge_d,
lv_matnr TYPE mara,
lv_temp1 TYPE numc2,
lv_temp2 TYPE numc4,
lv_joint(6) TYPE n.
DATA : lv_fyear TYPE mbewh-lfgja,
lv_period TYPE mbewh-lfmon.
"To calculate correct Financial year and period
CALL FUNCTION 'FTI_FISCAL_YEAR_MONTH_GET'
EXPORTING
i_bukrs = p_burks
i_budat = s_budat-low* I_DZTERM = FTIS_DATUM-INITIAL
* I_GJAHR = FTIS_GJAHR-INITIAL
IMPORTING
e_gjahr = lv_fyear
e_monat = lv_period.
CONCATENATE lv_period lv_fyear INTO lv_joint.
"Stock As on date
s_budat-sign = 'I'.
s_budat-option = 'BT'.
s_budat-high = s_budat-low.
s_budat-low = '19000101'.
REFRESH : s_budat.
APPEND : s_budat.
IF NOT s_matnr IS INITIAL OR NOT s_mtart IS INITIAL.
SELECT a~matnr
a~mtart
b~maktx
INTO TABLE it_mara
FROM mara
AS a
INNER JOIN makt
AS b ON
a~matnr = b~matnr
WHERE a~matnr IN s_matnr AND a~mtart IN s_mtart.
LOOP AT it_mara INTO wa_mara.
s_matnr_fm_wa-sign = 'I'.
s_matnr_fm_wa-option = 'EQ'.
s_matnr_fm_wa-low = wa_mara-matnr.
APPEND : s_matnr_fm_wa TO s_matnr_fm.
CLEAR : wa_mara.
ENDLOOP.
IF s_matnr_fm IS INITIAL.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
SELECT bwkey
bukrs
FROM t001k
INTO TABLE it_t001k
WHERE bukrs = p_burks.
IF NOT it_t001k IS INITIAL.
SELECT werks
name2
FROM t001w
INTO TABLE it_t001w
FOR ALL ENTRIES IN it_t001k
WHERE werks = it_t001k-bwkey AND werks IN s_werks.
it_t001w_temp[] = it_t001w[].
SORT it_t001w_temp BY werks.
DELETE ADJACENT DUPLICATES FROM it_t001w_temp COMPARING werks.
LOOP AT it_t001w_temp INTO wa_t001w_temp.
s_werks_fm_wa-sign = 'I'.
s_werks_fm_wa-option = 'EQ'.
s_werks_fm_wa-low = wa_t001w_temp-werks.
APPEND : s_werks_fm_wa TO s_werks_fm.
CLEAR : wa_t001w_temp.
ENDLOOP.
ENDIF.
SELECT a~matnr
a~werks
a~lgort
a~labst
a~insme
a~einme
a~speme
a~retme
b~mtart
INTO TABLE it_mard
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
WHERE werks IN s_werks_fm AND lgort IN s_lgort AND a~matnr IN s_matnr_fm AND b~mtart IN s_mtart.
IF it_mard IS INITIAL.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
* s_matnr_fm[] = s_matnr[].
* s_werks_fm[] = s_werks[].
s_lgort_fm[] = s_lgort[].
s_budat_fm[] = s_budat[].
"For stock
CALL FUNCTION 'ZMM_GET_MAT_STK_ALL_DATA'
EXPORTING
p_lgbst = 'X'
TABLES
material_ra = s_matnr_fm
plant_ra = s_werks_fm
stge_loc_ra = s_lgort_fm
pstng_date_ra = s_budat_fm
it_bestand = it_stock_temp
it_fm_mseg = it_store_temp
EXCEPTIONS
selectone = 1
spl_stock = 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.
it_stock[] = it_stock_temp[].
it_store[] = it_store_temp[].
REFRESH : it_stock_temp, it_store_temp.
"For Vendor stock
CALL FUNCTION 'ZMM_GET_MAT_STK_ALL_DATA'
EXPORTING
p_sbbst = 'X'
p_sobkz = 'O'
TABLES
material_ra = s_matnr_fm
plant_ra = s_werks_fm
stge_loc_ra = s_lgort_fm
pstng_date_ra = s_budat_fm
it_bestand = it_stock_temp
it_fm_mseg = it_store_temp
EXCEPTIONS
selectone = 1
spl_stock = 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.
* "Assign temp table (it_stock_temp) value into main table (it_stock)
* LOOP AT it_stock_temp INTO wa_stock_temp.
* wa_stock = wa_stock_temp.
* APPEND : wa_stock to it_stock.
* CLEAR : wa_stock_temp, wa_stock.
* ENDLOOP.
"Assign temp table (it_store_temp) value into main table (it_store)
LOOP AT it_store_temp INTO wa_store_temp.
wa_store = wa_store_temp.
APPEND : wa_store TO it_store.
CLEAR : wa_store_temp, wa_store.
ENDLOOP.
IF it_mara IS INITIAL.
SELECT a~matnr
a~mtart
b~maktx
INTO TABLE it_mara
FROM mara
AS a
INNER JOIN makt
AS b ON
a~matnr = b~matnr
FOR ALL ENTRIES IN it_store
WHERE a~matnr = it_store-matnr.
ENDIF.
IF NOT it_store IS INITIAL.
SELECT matnr
bwkey
lfgja
lfmon
vprsv
verpr
stprs
FROM mbewh
INTO TABLE it_mbewh
FOR ALL ENTRIES IN it_store
WHERE matnr = it_store-matnr AND bwkey = it_store-werks." AND lfgja = lv_fyear.
ENDIF.
LOOP AT it_mbewh INTO wa_mbewh.
lv_temp1 = wa_mbewh-lfmon.
lv_temp2 = wa_mbewh-lfgja.
CONCATENATE lv_temp1 lv_temp2 INTO wa_mbewh-joint.
MODIFY it_mbewh FROM wa_mbewh INDEX sy-tabix TRANSPORTING joint.
CLEAR : wa_mbewh, lv_temp1, lv_temp2.
ENDLOOP.
it_store_temp_matnr[] = it_store[].
it_store_temp_werks[] = it_store[].
* SORT it_store_temp_matnr BY matnr.
SORT it_store_temp_werks BY matnr werks.
SORT it_mbewh BY joint DESCENDING.
* DELETE ADJACENT DUPLICATES FROM it_store_temp_matnr COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_store_temp_werks COMPARING matnr werks.
LOOP AT it_store_temp_werks INTO wa_store_temp_werks.
wa_final-matnr = wa_store_temp_werks-matnr.
READ TABLE it_mara INTO wa_mara WITH KEY matnr = wa_final-matnr.
IF sy-subrc = 0.
wa_final-maktx = wa_mara-maktx.
ENDIF.
LOOP AT it_store INTO wa_store WHERE matnr = wa_store_temp_werks-matnr AND
werks = wa_store_temp_werks-werks.
IF wa_store-shkzg = 'S'.
lv_menge = lv_menge + wa_store-menge.
ELSEIF wa_store-shkzg = 'H'.
lv_menge = lv_menge - wa_store-menge.
ENDIF.
ENDLOOP.
wa_final-labst = lv_menge.
wa_final-werks = wa_store-werks.
READ TABLE it_t001w INTO wa_t001w WITH KEY werks = wa_final-werks.
IF sy-subrc = 0.
wa_final-name2 = wa_t001w-name2.
ENDIF.
LOOP AT it_mbewh INTO wa_mbewh WHERE matnr = wa_final-matnr AND bwkey = wa_final-werks AND
joint <= lv_joint.
IF NOT wa_mbewh-vprsv IS INITIAL.
CASE wa_mbewh-vprsv.
WHEN 'S'.
wa_final-stval = wa_mbewh-stprs.
WHEN 'V'.
wa_final-stval = wa_mbewh-verpr.
ENDCASE.
ELSEIF NOT wa_mbewh-verpr IS INITIAL.
wa_final-stval = wa_mbewh-verpr.
ELSEIF NOT wa_mbewh-stprs IS INITIAL.
wa_final-stval = wa_mbewh-stprs.
ENDIF.
EXIT. "After getting first value exit the loop.
ENDLOOP.
APPEND : wa_final TO it_final.
CLEAR : lv_menge, wa_store.
ENDLOOP.
ENDFORM. " GET_DATA_UFM