Aim: Using dynamic table concept show storage location wise stock report
*&---------------------------------------------------------------------*
*& Report ZEL_SL_WISE_STOCK_REP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zel_sl_wise_stock_rep.
TYPE-POOLS : slis.
TABLES : mara, mard, mseg, mkpf.
"Data type declaration
TYPES : BEGIN OF tp_makt, "material description
matnr TYPE mara-matnr, "Material
maktx TYPE makt-maktx, "Material Description
END OF tp_makt.
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_mard, "This hold current date stock details
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, "link b/w company code and plant
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, "Plant master table
werks TYPE t001w-werks, "Plant
name2 TYPE t001w-name2, "Name
END OF tp_t001w.
TYPES : BEGIN OF tp_mbew, "To get the stock value
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
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
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,
wa_fcat TYPE lvc_s_fcat,
it_fcatalog TYPE STANDARD TABLE OF slis_fieldcat_alv,
wa_fcatalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
DATA : it_dyn_tab TYPE REF TO data,
wa_newline TYPE REF TO data.
DATA : lv_labst TYPE mard-labst,
lv_trame TYPE marc-trame,
lv_temp_stock TYPE marc-trame,
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,
lgort 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 OBLIGATORY NO INTERVALS NO-EXTENSION.
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.
START-OF-SELECTION.
IF s_budat-low = sy-datum.
PERFORM get_data.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ELSE.
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
INTO TABLE it_mard
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
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.
ENDIF.
IF NOT it_mard IS INITIAL.
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_mard-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-lgort = wa_mard-lgort.
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. "Transit stock
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'. "Standard Price
wa_final-stval = wa_mbew-stprs.
WHEN 'V'. "Moving Avg. Price
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, wa_mbew, wa_marc, wa_mslb, wa_t001w, wa_makt.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FC
*&---------------------------------------------------------------------*
FORM fill_fc .
DATA : lv_cout TYPE numc2.
it_finalcp = it_final.
SORT it_finalcp BY lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING lgort.
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.
PERFORM fill_field_catalog USING : wa_finalcp-lgort '<GT_TABLE>' wa_finalcp-lgort '17' 'MEINS'.
wa_tempfc-lgort = wa_finalcp-lgort.
APPEND : wa_tempfc TO it_tempfc.
ENDLOOP.
IF s_budat-low = sy-datum.
PERFORM fill_field_catalog USING : 'TRAME' '<GT_TABLE>' 'In Transit Stock' '17' 'MEINS'.
ENDIF.
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'.
ENDFORM. " FILL_FC
*&---------------------------------------------------------------------*
*& Form FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM fill_field_catalog USING fd_nm tb_nm sl_txt op_ln q_fn.
CLEAR wa_fcat.
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-qfieldname.
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 lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_finalcp1 COMPARING lgort.
"???????????????????????????????????????????????????????????????????????????
"Include Transit Stock
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 lgort = wa_finalcp1-lgort.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY lgort = wa_finalcp1-lgort.
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_trame = lv_trame + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
ASSIGN COMPONENT wa_tempfc-lgort OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
ENDIF.
IF NOT lv_trame IS INITIAL.
ASSIGN COMPONENT 'TRAME' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_trame.
ENDIF.
lv_labst_temp = lv_labst_temp + lv_labst + lv_trame.
lv_labst = lv_labst + lv_trame.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
CLEAR : lv_labst, wa_finalcp1, lv_stval, lv_trame.
ENDLOOP.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
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.
"End of main loop
LOOP AT it_fcat INTO wa_fcat.
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-qfieldname.
* IF NOT wa_fcatalog-qfieldname IS INITIAL.
* wa_fcatalog-qtabname = 'MARA'.
* ENDIF.
APPEND wa_fcatalog TO it_fcatalog.
CLEAR : wa_fcatalog, wa_fcat.
ENDLOOP.
IF NOT <gt_table> IS INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fcatalog
TABLES
t_outtab = <gt_table>.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
*& 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_lgort TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_lgort 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_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_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.
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_lgort[] = it_store[].
* SORT it_store_temp_matnr BY matnr.
SORT it_store_temp_lgort BY matnr lgort.
SORT it_mbewh BY joint DESCENDING.
DELETE it_store_temp_lgort WHERE lgort = ''.
* DELETE ADJACENT DUPLICATES FROM it_store_temp_matnr COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_store_temp_lgort COMPARING matnr lgort.
LOOP AT it_store_temp_lgort INTO wa_store_temp_lgort.
wa_final-matnr = wa_store_temp_lgort-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_lgort-matnr AND
lgort = wa_store_temp_lgort-lgort.
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.
wa_final-lgort = wa_store-lgort.
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_SL_WISE_STOCK_REP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zel_sl_wise_stock_rep.
TYPE-POOLS : slis.
TABLES : mara, mard, mseg, mkpf.
"Data type declaration
TYPES : BEGIN OF tp_makt,
matnr TYPE mara-matnr, "Material
maktx TYPE makt-maktx, "Material Description
END OF tp_makt.
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_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
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
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,
wa_fcat TYPE lvc_s_fcat,
it_fcatalog TYPE STANDARD TABLE OF slis_fieldcat_alv,
wa_fcatalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
DATA : it_dyn_tab TYPE REF TO data,
wa_newline TYPE REF TO data.
DATA : lv_labst TYPE mard-labst,
lv_trame TYPE marc-trame,
lv_temp_stock TYPE marc-trame,
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,
lgort 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 OBLIGATORY NO INTERVALS NO-EXTENSION.
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.
START-OF-SELECTION.
IF s_budat-low = sy-datum.
PERFORM get_data.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ELSE.
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
INTO TABLE it_mard
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
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.
ENDIF.
IF NOT it_mard IS INITIAL.
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_mard-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-lgort = wa_mard-lgort.
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. "Transit stock
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, wa_mbew, wa_marc, wa_mslb, wa_t001w, wa_makt.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FC
*&---------------------------------------------------------------------*
FORM fill_fc .
DATA : lv_cout TYPE numc2.
it_finalcp = it_final.
SORT it_finalcp BY lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING lgort.
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.
PERFORM fill_field_catalog USING : wa_finalcp-lgort '<GT_TABLE>' wa_finalcp-lgort '17' 'MEINS'.
wa_tempfc-lgort = wa_finalcp-lgort.
APPEND : wa_tempfc TO it_tempfc.
ENDLOOP.
IF s_budat-low = sy-datum.
PERFORM fill_field_catalog USING : 'TRAME' '<GT_TABLE>' 'In Transit Stock' '17' 'MEINS'.
ENDIF.
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'.
ENDFORM. " FILL_FC
*&---------------------------------------------------------------------*
*& Form FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM fill_field_catalog USING fd_nm tb_nm sl_txt op_ln q_fn.
CLEAR wa_fcat.
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-qfieldname.
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 lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_finalcp1 COMPARING lgort.
"???????????????????????????????????????????????????????????????????????????
"Include Transit Stock
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 lgort = wa_finalcp1-lgort.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY lgort = wa_finalcp1-lgort.
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_trame = lv_trame + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
ASSIGN COMPONENT wa_tempfc-lgort OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
ENDIF.
IF NOT lv_trame IS INITIAL.
ASSIGN COMPONENT 'TRAME' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_trame.
ENDIF.
lv_labst_temp = lv_labst_temp + lv_labst + lv_trame.
lv_labst = lv_labst + lv_trame.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
CLEAR : lv_labst, wa_finalcp1, lv_stval, lv_trame.
ENDLOOP.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
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.
"End of main loop
LOOP AT it_fcat INTO wa_fcat.
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-qfieldname.
* IF NOT wa_fcatalog-qfieldname IS INITIAL.
* wa_fcatalog-qtabname = 'MARA'.
* ENDIF.
APPEND wa_fcatalog TO it_fcatalog.
CLEAR : wa_fcatalog, wa_fcat.
ENDLOOP.
IF NOT <gt_table> IS INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fcatalog
TABLES
t_outtab = <gt_table>.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
*& 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_lgort TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_lgort 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_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_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.
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_lgort[] = it_store[].
* SORT it_store_temp_matnr BY matnr.
SORT it_store_temp_lgort BY matnr lgort.
SORT it_mbewh BY joint DESCENDING.
DELETE it_store_temp_lgort WHERE lgort = ''.
* DELETE ADJACENT DUPLICATES FROM it_store_temp_matnr COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_store_temp_lgort COMPARING matnr lgort.
LOOP AT it_store_temp_lgort INTO wa_store_temp_lgort.
wa_final-matnr = wa_store_temp_lgort-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_lgort-matnr AND
lgort = wa_store_temp_lgort-lgort.
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.
wa_final-lgort = wa_store-lgort.
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_SL_WISE_STOCK_REP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zel_sl_wise_stock_rep.
TYPE-POOLS : slis.
TABLES : mara, mard, mseg, mkpf.
"Data type declaration
TYPES : BEGIN OF tp_makt, "material description
matnr TYPE mara-matnr, "Material
maktx TYPE makt-maktx, "Material Description
END OF tp_makt.
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_mard, "This hold current date stock details
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, "link b/w company code and plant
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, "Plant master table
werks TYPE t001w-werks, "Plant
name2 TYPE t001w-name2, "Name
END OF tp_t001w.
TYPES : BEGIN OF tp_mbew, "To get the stock value
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
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
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,
wa_fcat TYPE lvc_s_fcat,
it_fcatalog TYPE STANDARD TABLE OF slis_fieldcat_alv,
wa_fcatalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
DATA : it_dyn_tab TYPE REF TO data,
wa_newline TYPE REF TO data.
DATA : lv_labst TYPE mard-labst,
lv_trame TYPE marc-trame,
lv_temp_stock TYPE marc-trame,
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,
lgort 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 OBLIGATORY NO INTERVALS NO-EXTENSION.
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.
START-OF-SELECTION.
IF s_budat-low = sy-datum.
PERFORM get_data.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ELSE.
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
INTO TABLE it_mard
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
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.
ENDIF.
IF NOT it_mard IS INITIAL.
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_mard-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-lgort = wa_mard-lgort.
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. "Transit stock
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'. "Standard Price
wa_final-stval = wa_mbew-stprs.
WHEN 'V'. "Moving Avg. Price
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, wa_mbew, wa_marc, wa_mslb, wa_t001w, wa_makt.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FC
*&---------------------------------------------------------------------*
FORM fill_fc .
DATA : lv_cout TYPE numc2.
it_finalcp = it_final.
SORT it_finalcp BY lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING lgort.
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.
PERFORM fill_field_catalog USING : wa_finalcp-lgort '<GT_TABLE>' wa_finalcp-lgort '17' 'MEINS'.
wa_tempfc-lgort = wa_finalcp-lgort.
APPEND : wa_tempfc TO it_tempfc.
ENDLOOP.
IF s_budat-low = sy-datum.
PERFORM fill_field_catalog USING : 'TRAME' '<GT_TABLE>' 'In Transit Stock' '17' 'MEINS'.
ENDIF.
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'.
ENDFORM. " FILL_FC
*&---------------------------------------------------------------------*
*& Form FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM fill_field_catalog USING fd_nm tb_nm sl_txt op_ln q_fn.
CLEAR wa_fcat.
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-qfieldname.
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 lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_finalcp1 COMPARING lgort.
"???????????????????????????????????????????????????????????????????????????
"Include Transit Stock
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 lgort = wa_finalcp1-lgort.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY lgort = wa_finalcp1-lgort.
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_trame = lv_trame + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
ASSIGN COMPONENT wa_tempfc-lgort OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
ENDIF.
IF NOT lv_trame IS INITIAL.
ASSIGN COMPONENT 'TRAME' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_trame.
ENDIF.
lv_labst_temp = lv_labst_temp + lv_labst + lv_trame.
lv_labst = lv_labst + lv_trame.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
CLEAR : lv_labst, wa_finalcp1, lv_stval, lv_trame.
ENDLOOP.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
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.
"End of main loop
LOOP AT it_fcat INTO wa_fcat.
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-qfieldname.
* IF NOT wa_fcatalog-qfieldname IS INITIAL.
* wa_fcatalog-qtabname = 'MARA'.
* ENDIF.
APPEND wa_fcatalog TO it_fcatalog.
CLEAR : wa_fcatalog, wa_fcat.
ENDLOOP.
IF NOT <gt_table> IS INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fcatalog
TABLES
t_outtab = <gt_table>.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
*& 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_lgort TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_lgort 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_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_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.
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_lgort[] = it_store[].
* SORT it_store_temp_matnr BY matnr.
SORT it_store_temp_lgort BY matnr lgort.
SORT it_mbewh BY joint DESCENDING.
DELETE it_store_temp_lgort WHERE lgort = ''.
* DELETE ADJACENT DUPLICATES FROM it_store_temp_matnr COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_store_temp_lgort COMPARING matnr lgort.
LOOP AT it_store_temp_lgort INTO wa_store_temp_lgort.
wa_final-matnr = wa_store_temp_lgort-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_lgort-matnr AND
lgort = wa_store_temp_lgort-lgort.
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.
wa_final-lgort = wa_store-lgort.
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_SL_WISE_STOCK_REP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zel_sl_wise_stock_rep.
TYPE-POOLS : slis.
TABLES : mara, mard, mseg, mkpf.
"Data type declaration
TYPES : BEGIN OF tp_makt,
matnr TYPE mara-matnr, "Material
maktx TYPE makt-maktx, "Material Description
END OF tp_makt.
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_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
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
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,
wa_fcat TYPE lvc_s_fcat,
it_fcatalog TYPE STANDARD TABLE OF slis_fieldcat_alv,
wa_fcatalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
DATA : it_dyn_tab TYPE REF TO data,
wa_newline TYPE REF TO data.
DATA : lv_labst TYPE mard-labst,
lv_trame TYPE marc-trame,
lv_temp_stock TYPE marc-trame,
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,
lgort 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 OBLIGATORY NO INTERVALS NO-EXTENSION.
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.
START-OF-SELECTION.
IF s_budat-low = sy-datum.
PERFORM get_data.
PERFORM fill_fc.
PERFORM create_dynamic_it.
ELSE.
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
INTO TABLE it_mard
FROM mard
AS a
INNER JOIN mara
AS b ON
a~matnr = b~matnr
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.
ENDIF.
IF NOT it_mard IS INITIAL.
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_mard-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-lgort = wa_mard-lgort.
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. "Transit stock
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, wa_mbew, wa_marc, wa_mslb, wa_t001w, wa_makt.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form FILL_FC
*&---------------------------------------------------------------------*
FORM fill_fc .
DATA : lv_cout TYPE numc2.
it_finalcp = it_final.
SORT it_finalcp BY lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING lgort.
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.
PERFORM fill_field_catalog USING : wa_finalcp-lgort '<GT_TABLE>' wa_finalcp-lgort '17' 'MEINS'.
wa_tempfc-lgort = wa_finalcp-lgort.
APPEND : wa_tempfc TO it_tempfc.
ENDLOOP.
IF s_budat-low = sy-datum.
PERFORM fill_field_catalog USING : 'TRAME' '<GT_TABLE>' 'In Transit Stock' '17' 'MEINS'.
ENDIF.
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'.
ENDFORM. " FILL_FC
*&---------------------------------------------------------------------*
*& Form FILL_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM fill_field_catalog USING fd_nm tb_nm sl_txt op_ln q_fn.
CLEAR wa_fcat.
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-qfieldname.
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 lgort.
DELETE ADJACENT DUPLICATES FROM it_finalcp COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_finalcp1 COMPARING lgort.
"???????????????????????????????????????????????????????????????????????????
"Include Transit Stock
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 lgort = wa_finalcp1-lgort.
READ TABLE it_tempfc INTO wa_tempfc WITH KEY lgort = wa_finalcp1-lgort.
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_trame = lv_trame + wa_final-trame. "Transit Stock
ENDIF.
lv_stval = wa_final-stval.
CLEAR : wa_final.
ENDLOOP.
IF NOT lv_labst IS INITIAL.
ASSIGN COMPONENT wa_tempfc-lgort OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst.
ENDIF.
IF NOT lv_trame IS INITIAL.
ASSIGN COMPONENT 'TRAME' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_trame.
ENDIF.
lv_labst_temp = lv_labst_temp + lv_labst + lv_trame.
lv_labst = lv_labst + lv_trame.
lv_stval = lv_labst * lv_stval.
lv_stval_temp = lv_stval_temp + lv_stval.
CLEAR : lv_labst, wa_finalcp1, lv_stval, lv_trame.
ENDLOOP.
IF NOT lv_labst_temp IS INITIAL.
ASSIGN COMPONENT 'TOTST' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_labst_temp.
ENDIF.
ASSIGN COMPONENT 'STVAL' OF STRUCTURE <fs_dyntable> TO <l_field>.
<l_field> = lv_stval_temp.
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.
"End of main loop
LOOP AT it_fcat INTO wa_fcat.
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-qfieldname.
* IF NOT wa_fcatalog-qfieldname IS INITIAL.
* wa_fcatalog-qtabname = 'MARA'.
* ENDIF.
APPEND wa_fcatalog TO it_fcatalog.
CLEAR : wa_fcatalog, wa_fcat.
ENDLOOP.
IF NOT <gt_table> IS INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fcatalog
TABLES
t_outtab = <gt_table>.
ELSE.
MESSAGE 'No Value Found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " CREATE_DYNAMIC_IT
*&---------------------------------------------------------------------*
*& 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_lgort TYPE STANDARD TABLE OF zmseg_stock_rep,
wa_store_temp_lgort 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_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_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.
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_lgort[] = it_store[].
* SORT it_store_temp_matnr BY matnr.
SORT it_store_temp_lgort BY matnr lgort.
SORT it_mbewh BY joint DESCENDING.
DELETE it_store_temp_lgort WHERE lgort = ''.
* DELETE ADJACENT DUPLICATES FROM it_store_temp_matnr COMPARING matnr.
DELETE ADJACENT DUPLICATES FROM it_store_temp_lgort COMPARING matnr lgort.
LOOP AT it_store_temp_lgort INTO wa_store_temp_lgort.
wa_final-matnr = wa_store_temp_lgort-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_lgort-matnr AND
lgort = wa_store_temp_lgort-lgort.
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.
wa_final-lgort = wa_store-lgort.
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