URUN_AGACI KONTROL RAPORU İleri&Geri

iler&geri istikamette Ürün hiyerarşisi çıkartma

*&---------------------------------------------------------------------*
*& Report  ZPP_URUN_AGACI_KONTROL_RAPORU
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zpp_urun_agaci_kontrol_raporu.

TABLES : mara, marc, stpo, mvke, stpov_alv.

TYPE-POOLS: slis.

TYPES:  BEGIN OF tys_malz,
          matnr TYPE matnr,
          werks TYPE werks_d,
        END OF tys_malz.

TYPES: BEGIN OF gty_tab ,
        dglvl LIKE stpox_alv-dglvl,
        stlnr LIKE mast-stlnr,
        stlty LIKE stpo-stlty,
        matnr LIKE mast-matnr,
        datuv LIKE stpo-datuv,
        stvkn LIKE stpo-stvkn,
        stpoz LIKE stpo-stpoz,
        zmm_matnr LIKE mara-zmm_matnr,
        maktx LIKE makt-maktx,
        posnr LIKE stpo-posnr,
        postp LIKE stpo-postp,
        menge LIKE stpo-menge,
        meins LIKE stpo-meins,
        zmm_resimno LIKE mara-zmm_resimno,
        bismt LIKE mara-bismt,
        brgew LIKE mara-brgew,
        mvgr1 LIKE mvke-mvgr1,
        mvgr2 LIKE mvke-mvgr2,
        mvgr3 LIKE mvke-mvgr3,
        mvgr4 LIKE mvke-mvgr4,
        mvgr5 LIKE mvke-mvgr5,
        mmsta LIKE marc-mmsta,
        dismm LIKE marc-dismm,
        dispo LIKE marc-dispo,
        disls LIKE marc-disls,
        beskz LIKE marc-beskz,
        lgpro LIKE marc-lgpro,
        lgfsb LIKE marc-lgfsb,
        strgr LIKE marc-strgr,
        mtvfp LIKE marc-mtvfp,
        altsl LIKE marc-altsl,
        sbdkz LIKE marc-sbdkz,
        sfcpf LIKE marc-sfcpf,
*        lgort LIKE mard-lgort, DİNAMİK OLDULAR
*        labst LIKE mard-labst, DİNAMİK OLDULAR
      END OF gty_tab.

DATA: BEGIN OF gt_tab2 OCCURS 0 ,
        dglvl LIKE stpox_alv-dglvl,
        stlnr LIKE mast-stlnr,
        stlty LIKE stpo-stlty,
        matnr LIKE mast-matnr,
        datuv LIKE stpo-datuv,
        stvkn LIKE stpo-stvkn,
        stpoz LIKE stpo-stpoz,
        zmm_matnr LIKE mara-zmm_matnr,
        maktx LIKE makt-maktx,
        posnr LIKE stpo-posnr,
        postp LIKE stpo-postp,
        menge LIKE stpo-menge,
        meins LIKE stpo-meins,
        zmm_resimno LIKE mara-zmm_resimno,
        bismt LIKE mara-bismt,
        brgew LIKE mara-brgew,
        mvgr1 LIKE mvke-mvgr1,
        mvgr2 LIKE mvke-mvgr2,
        mvgr3 LIKE mvke-mvgr3,
        mvgr4 LIKE mvke-mvgr4,
        mvgr5 LIKE mvke-mvgr5,
        mmsta LIKE marc-mmsta,
        dismm LIKE marc-dismm,
        dispo LIKE marc-dispo,
        disls LIKE marc-disls,
        beskz LIKE marc-beskz,
        lgpro LIKE marc-lgpro,
        lgfsb LIKE marc-lgfsb,
        strgr LIKE marc-strgr,
        mtvfp LIKE marc-mtvfp,
        altsl LIKE marc-altsl,
        sbdkz LIKE marc-sbdkz,
        sfcpf LIKE marc-sfcpf,
      END OF gt_tab2.


DATA: gt_tab TYPE STANDARD TABLE OF gty_tab WITH HEADER LINE.

DATA: BEGIN OF gt_depo OCCURS 0,
        matnr LIKE mard-matnr,
*        werks LIKE mard-werks,
        lgort LIKE mard-lgort,
        labst LIKE mard-labst,
      END OF gt_depo.

DATA: gv_max_depo TYPE i.

DATA gt_fieldcat TYPE TABLE OF slis_fieldcat_alv WITH HEADER LINE .
DATA gv_repid LIKE sy-repid .
DATA gt_layout TYPE slis_layout_alv.
DATA gt_list_top_of_page TYPE slis_t_listheader.
DATA gt_events   TYPE slis_t_event.

DATA: anz_stufe(11) TYPE c.
DATA:
   pm_mtnrv LIKE mara-matnr,
   pm_werks LIKE marc-werks,
   pm_stlal LIKE stko-stlal,
   pm_stlan LIKE stzu-stlan,
   pm_capid LIKE tc04-capid,
   pm_datub LIKE stpo-datuv,
   pm_datuv LIKE stko-datuv,
   pm_techs LIKE tecs-techs,
   pm_aennr LIKE aenr-aennr,
   pm_emeng LIKE stko-bmeng,
   pm_hemng(13)  TYPE n,
   pm_alvsa      LIKE rc29l-valst,
   pm_dsprf      LIKE klah-class,
   pm_prprf      LIKE klah-class,
   pm_ausch(1)   TYPE c,
   pm_ltext(1)   TYPE c,
   pm_drldt(1)   TYPE c,
   pm_dspco(1)   TYPE c,
   pm_stpst      LIKE rc29l-maxst,
   pm_altvo(1)   TYPE c,
   pm_upsaz(1)   TYPE c,
   pm_brems(1)   TYPE c,
   pm_erskz(1)   TYPE c,
   pm_erssl(1)   TYPE c,
   pm_beikz(1)   TYPE c,
   pm_bessl(1)   TYPE c,
   pm_bagrp      LIKE mara-matnr,
   pm_postp(1)   TYPE c,
   pm_ehndl(1)   TYPE c VALUE `1`,
   pm_sanko LIKE stpo-sanko,
   pm_sanfe LIKE stpo-sanfe,
   pm_sanka LIKE stpo-sanka,
   pm_sanin LIKE stpo-sanin,
   pm_sanvs LIKE stpo-sanvs,
   pm_rvrel LIKE stpo-rvrel,
   pm_schgt LIKE stpo-schgt,
   pm_stkkz LIKE stpo-stkkz,
   pm_pspnr LIKE prst-pspnr,
   pm_vbeln LIKE kdst-vbeln,
   pm_vbpos LIKE kdst-vbpos,
   stltp_in LIKE rc29a-atabr,
   mem_mngmt(1) TYPE c,
   dstst_flg LIKE csdata-xfeld.

DATA: BEGIN OF selpool1.
        INCLUDE STRUCTURE cstmat.
DATA: END OF selpool1.

DATA: BEGIN OF selpool2.
        INCLUDE STRUCTURE mc29s.
DATA: END OF selpool2.

DATA: BEGIN OF stb OCCURS 1000.
        INCLUDE STRUCTURE stpox.
DATA: END OF stb.

*     Materialkatalog
DATA: BEGIN OF matcat OCCURS 50.
        INCLUDE STRUCTURE cscmat.
DATA: END OF matcat.

DATA: BEGIN OF ltb OCCURS 0.
        INCLUDE STRUCTURE stpov.
DATA: END OF ltb.

DATA: BEGIN OF equicat  OCCURS 0.
        INCLUDE STRUCTURE cscequi.
DATA: END OF equicat .

DATA: BEGIN OF kndcat   OCCURS 0.
        INCLUDE STRUCTURE cscknd.
DATA: END OF kndcat  .


DATA: BEGIN OF prjcat   OCCURS 0.
        INCLUDE STRUCTURE cscprj.
DATA: END OF prjcat.

DATA: BEGIN OF stdcat   OCCURS 0.
        INCLUDE STRUCTURE cscstd.
DATA: END OF stdcat  .

DATA: BEGIN OF tplcat   OCCURS 0.
        INCLUDE STRUCTURE csctpl.
DATA: END OF tplcat.

DATA: dref        TYPE REF TO data,
      dref1       TYPE REF TO data,
      l_wa_name   TYPE string,
      itab_type   TYPE REF TO cl_abap_tabledescr,
      struct_type TYPE REF TO cl_abap_structdescr,
      comp_tab    TYPE cl_abap_structdescr=>component_table,
      elem_type   TYPE REF TO cl_abap_elemdescr,
      comp_fld    TYPE cl_abap_structdescr=>component.


FIELD-SYMBOLS : <lt_outtab> TYPE STANDARD TABLE,
                <ls_outtab> TYPE ANY,
                <l_fld> TYPE ANY.

FIELD-SYMBOLS : ,
                ,
                ,
                ,
                ,
                ,
                ,
                <zmm_matnr>,
                ,
                ,
                ,
                ,
                ,
                <zmm_resimno>,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                ,
                .


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001 .
PARAMETERS:     p_vkorg LIKE mvke-vkorg OBLIGATORY,
                p_vtweg LIKE mvke-vtweg OBLIGATORY.
SELECT-OPTIONS: s_werks FOR marc-werks OBLIGATORY DEFAULT `1000`,
                s_matnr FOR mara-matnr,
                s_mtart FOR mara-mtart,
                s_idnrk FOR stpo-idnrk.
PARAMETERS:     p_dglvl LIKE rc29l-maxst DEFAULT `0`.
SELECTION-SCREEN END OF BLOCK b1.
*&---------------------------------------------------------------------*
INITIALIZATION.

AT SELECTION-SCREEN OUTPUT.
  IF NOT s_idnrk[] IS INITIAL.
    CLEAR: s_matnr.
    REFRESH s_matnr[].

    LOOP AT SCREEN.
      IF screen-name = `S_MATNR-LOW` OR screen-name = `S_MATNR-HIGH`.
        screen-input = 0.
      ENDIF.

      MODIFY SCREEN.
    ENDLOOP.
  ELSE.
    LOOP AT SCREEN.
      IF screen-name = `S_MATNR-LOW` OR screen-name = `S_MATNR-HIGH`.
        screen-input = 1.
      ENDIF.

      MODIFY SCREEN.
    ENDLOOP.

  ENDIF.

START-OF-SELECTION.
  PERFORM f_get_data.

END-OF-SELECTION.
  PERFORM f_display_data.

TOP-OF-PAGE.
  PERFORM top_of_page.

TOP-OF-PAGE DURING LINE-SELECTION.
  PERFORM top_of_page.

*&---------------------------------------------------------------------*
*&      Form  F_DISPLAY_DATA
*&---------------------------------------------------------------------*
FORM f_display_data .


  PERFORM e03_eventtab_build USING gt_events[].
  PERFORM e04_comment_build USING gt_list_top_of_page[].
  PERFORM f_build_fcat.
  PERFORM f_build_layout.

  CALL FUNCTION `REUSE_ALV_GRID_DISPLAY`
    EXPORTING
      i_callback_program       = gv_repid
      i_callback_user_command  = `F_USER_COMMAND`
      i_callback_pf_status_set = `F_PF_STATUS`
      i_buffer_active          = space
      i_bypassing_buffer       = `X`
      it_fieldcat              = gt_fieldcat[]
      is_layout                = gt_layout
      it_events                = gt_events[]
      i_save                   = `A`
    TABLES
      t_outtab                 = <lt_outtab>[]  "gt_tab
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.                    " F_DISPLAY_DATA

*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
FORM f_build_layout .

  gt_layout-zebra             = `X`.
  gt_layout-colwidth_optimize = `X`.

ENDFORM.                    " BUILD_LAYOUT

*----------------------------------------------------------*
*       FORM F_USER_COMMAND                                  *
*----------------------------------------------------------*
FORM f_user_command USING r_ucomm LIKE sy-ucomm
                  rs_selfield TYPE slis_selfield.

*  CASE r_ucomm .
*    WHEN `` .
*
*  ENDCASE.
ENDFORM.                    "f_user_command

*&---------------------------------------------------------------------*
*&      FORM  F_PF_STATUS
*&---------------------------------------------------------------------*
FORM f_pf_status USING extab TYPE slis_t_extab.
  SET PF-STATUS `STANDARD`.
  REFRESH extab[].

ENDFORM.                    "f_pf_status
*---------------------------------------------------------------------*
*       FORM E03_EVENTTAB_BUILD                                       *
*---------------------------------------------------------------------*
FORM e03_eventtab_build USING e03_lt_events TYPE slis_t_event.
  DATA: ls_event TYPE slis_alv_event.

  CALL FUNCTION `REUSE_ALV_EVENTS_GET`
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = e03_lt_events.

  READ TABLE e03_lt_events WITH KEY name = slis_ev_top_of_page
                           INTO ls_event.
  IF sy-subrc = 0.
    MOVE `SAYFA_BASI` TO ls_event-form.
    APPEND ls_event TO e03_lt_events.
  ENDIF.
ENDFORM.                    "E03_EVENTTAB_BUILD


*---------------------------------------------------------------------*
*       FORM E04_COMMENT_BUILD                                        *
*---------------------------------------------------------------------*
FORM e04_comment_build USING e04_lt_top_of_page TYPE slis_t_listheader.
  DATA: ls_line TYPE slis_listheader.
  FREE : e04_lt_top_of_page.
  DATA : _tarih(10).
  DATA : _saat(10).

  CLEAR ls_line.
  ls_line-typ  = `H`.
  ls_line-key  = `RAPOR: `.
  ls_line-info = sy-title.
  APPEND ls_line TO e04_lt_top_of_page.
  CLEAR ls_line.

  ls_line-typ  = `S`.
  ls_line-key  = `KULLANICI: `.
  ls_line-info = sy-uname.
  ls_line-info+35(10) = sy-tcode.
  APPEND ls_line TO e04_lt_top_of_page.
  CLEAR ls_line.

  ls_line-typ = `S`.
  ls_line-key = `TARIH: `.
  WRITE sy-datum TO _tarih DD/MM/YYYY.
  WRITE sy-uzeit TO _saat .
  ls_line-info = _tarih.
  ls_line-info+20(10) = _saat.
  APPEND ls_line TO e04_lt_top_of_page.
  CLEAR ls_line.

ENDFORM.                    "E04_COMMENT_BUILD

*&---------------------------------------------------------------------*
*&      FORM  TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM top_of_page .
  FORMAT COLOR COL_HEADING.

  WRITE:/ sy-vline NO-GAP,` `,
          sy-vline NO-GAP.
ENDFORM.                    " TOP_OF_PAGE

*&---------------------------------------------------------------------*
*& FORM SAYFA_BASI
*&---------------------------------------------------------------------*
FORM sayfa_basi.
  CALL FUNCTION `REUSE_ALV_COMMENTARY_WRITE`
    EXPORTING
      i_logo             = ` `
      it_list_commentary = gt_list_top_of_page.
ENDFORM. " SAYFA_BASI

*&---------------------------------------------------------------------*
*&      Form  F_BUILD_FCAT
*&---------------------------------------------------------------------*
FORM f_build_fcat .

  DATA : lv_line TYPE i,
         lv_depo_no(3) TYPE n.

  gv_repid = sy-repid.

  CALL FUNCTION `REUSE_ALV_FIELDCATALOG_MERGE`
    EXPORTING
      i_program_name         = gv_repid
      i_internal_tabname     = `GT_TAB2`
      i_inclname             = gv_repid
    CHANGING
      ct_fieldcat            = gt_fieldcat[]
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.


  DESCRIBE TABLE gt_fieldcat LINES lv_line.

  DO gv_max_depo TIMES.
    lv_line = lv_line + 1.
    lv_depo_no = lv_depo_no + 1.
    CONCATENATE `DEPO_` lv_depo_no INTO gt_fieldcat-fieldname.
    gt_fieldcat-col_pos = lv_line.
    gt_fieldcat-tabname = 1.
    gt_fieldcat-datatype = `CHAR`.
    gt_fieldcat-inttype  = `C`.
    gt_fieldcat-intlen   = `4`.

*    gt_fieldcat-domname  = `LGORT`.
*    gt_fieldcat-dd_outlen  = `000004`.
    gt_fieldcat-seltext_l =
    gt_fieldcat-seltext_m =
    gt_fieldcat-seltext_s =
    gt_fieldcat-reptext_ddic = gt_fieldcat-fieldname.
    APPEND gt_fieldcat.

    CONCATENATE `MIKTAR_` lv_depo_no INTO gt_fieldcat-fieldname.
    gt_fieldcat-col_pos = lv_line.
    gt_fieldcat-tabname = 1.
    gt_fieldcat-datatype = `QUAN`.
    gt_fieldcat-inttype  = `P`.
    gt_fieldcat-intlen   = `13`.

*    gt_fieldcat-domname  = `MENG13V.
*    gt_fieldcat-dd_outlen  = `000004`.
    gt_fieldcat-seltext_l =
    gt_fieldcat-seltext_m =
    gt_fieldcat-seltext_s =
    gt_fieldcat-reptext_ddic = gt_fieldcat-fieldname.
    APPEND gt_fieldcat.

  ENDDO.


  LOOP AT gt_fieldcat.
    CASE gt_fieldcat-fieldname .
      WHEN `ZMM_MATNR` .
        gt_fieldcat-reptext_ddic = `Bileşen Malzeme Maskeli Kodu`.
        gt_fieldcat-seltext_l  = `Bileşen Malzeme Maskeli Kodu`.
        gt_fieldcat-seltext_m  = `Blşn Malz Mask. Kodu`.
        gt_fieldcat-seltext_s  = `Blşn Kodu`.
        gt_fieldcat-ddictxt    = `L`.
    ENDCASE .

    MODIFY gt_fieldcat.
  ENDLOOP .

ENDFORM.                    " F_BUILD_FCAT
*&---------------------------------------------------------------------*
*&      Form  F_GET_DATA
*&---------------------------------------------------------------------*
FORM f_get_data.

  DATA: lt_malz TYPE STANDARD TABLE OF tys_malz WITH HEADER LINE,
        lt_malz_tmp TYPE STANDARD TABLE OF tys_malz WITH HEADER LINE.

  pm_datuv = sy-datum.

  IF NOT s_idnrk[] IS INITIAL.
    SELECT mast~matnr
           marc~werks
      FROM mast
      INNER JOIN mara ON mast~matnr EQ mara~matnr
      INNER JOIN marc ON marc~matnr EQ mast~matnr
      INTO CORRESPONDING FIELDS OF TABLE lt_malz_tmp
      WHERE mast~matnr IN s_idnrk
        AND mara~mtart IN s_mtart
        AND marc~werks IN s_werks.

    SORT lt_malz_tmp.
    DELETE ADJACENT DUPLICATES FROM lt_malz_tmp.

    PERFORM f_where_used TABLES lt_malz_tmp
                                lt_malz.



  ELSE.
    SELECT mast~matnr
           marc~werks
      FROM mast
      INNER JOIN mara ON mast~matnr EQ mara~matnr
      INNER JOIN marc ON marc~matnr EQ mast~matnr
      INTO CORRESPONDING FIELDS OF TABLE lt_malz
      WHERE mast~matnr IN s_matnr
        AND mara~mtart IN s_mtart
        AND marc~werks IN s_werks.

  ENDIF.

  SORT lt_malz.
  DELETE ADJACENT DUPLICATES FROM lt_malz.

  PERFORM f_mat_bom TABLES lt_malz.
  PERFORM f_fill_gt_tab_fields.
  PERFORM f_fill_depo_table.
  PERFORM f_dyn_table.
  PERFORM f_fill_outtab.

ENDFORM.                    " F_GET_DATA

*&---------------------------------------------------------------------*
*&      Form  STUFE_AUFBEREITEN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM stufe_aufbereiten.
  anz_stufe = stb-stufe.
  TRANSLATE anz_stufe USING ` .`.
  anz_stufe+10(1) = ` `.

  IF stb-stufe < 9.
    stb-stufe = 9 - stb-stufe.
    SHIFT anz_stufe BY stb-stufe PLACES.
    stb-stufe = 9 - stb-stufe.
  ENDIF.
ENDFORM.                    "STUFE_AUFBEREITEN
*&---------------------------------------------------------------------*
*&      Form  F_CHECK_MATNR
*&---------------------------------------------------------------------*
FORM f_check_matnr USING matnr TYPE matnr.

  DATA:lv_matnr LIKE mara-matnr.

  SELECT SINGLE matnr INTO matnr
         FROM mara
         WHERE matnr = matnr.
  IF sy-subrc NE 0 .
    CALL FUNCTION `CONVERSION_EXIT_ALPHA_INPUT`
      EXPORTING
        input  = matnr
      IMPORTING
        output = lv_matnr.

    SELECT SINGLE matnr INTO matnr
           FROM mara
           WHERE matnr = lv_matnr.

    IF sy-subrc <> 0.

      IF lv_matnr IS NOT INITIAL. " boş ise ilk kayıt gelmesin
        SELECT SINGLE matnr INTO matnr
                  FROM mara
                  WHERE zmm_matnr = lv_matnr.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    " F_CHECK_MATNR
*&---------------------------------------------------------------------*
*&      Form  F_MAT_BOM
*&---------------------------------------------------------------------*
FORM f_mat_bom TABLES lt_malz TYPE STANDARD TABLE.

  DATA: wa_malz TYPE tys_malz.

  LOOP AT lt_malz INTO wa_malz.

    PERFORM f_check_matnr USING wa_malz-matnr.

    CALL FUNCTION `CS_BOM_EXPL_MAT_V2`
     EXPORTING
        altvo = pm_altvo
        aufsw = ` `
        auskz = pm_ausch
        bagrp = pm_bagrp
        beikz = pm_beikz
        bessl = pm_bessl
        brems = pm_brems
        capid = `PP01`
        datuv = pm_datuv
        drldt = pm_drldt
        ehndl = pm_ehndl
        emeng = pm_emeng
        erskz = pm_erskz
        erssl = pm_erssl
        mbwls = ` `
        mtnrv = wa_malz-matnr
        mehrs = `X`
        mmory = `1` "mem_mngmt
        postp = pm_postp
        sanko = pm_sanko
        sanfr = pm_sanfe
        sanka = pm_sanka
        sanin = pm_sanin
        sanvs = pm_sanvs
        rndkz = ``
        rvrel = pm_rvrel
        schgt = pm_schgt
        stkkz = pm_stkkz
        stlal = pm_stlal
        stlan = pm_stlan
        stpst = p_dglvl "pm_stpst
        werks = wa_malz-werks
        aumgb = `X`
     IMPORTING
        topmat = selpool1
        dstst  = dstst_flg
     TABLES
        stb    = stb
        matcat = matcat
     EXCEPTIONS
        material_not_found    = 4
        no_plant_data         = 8
        no_bom_found          = 12
        no_suitable_bom_found = 16
        alt_not_found         = 24
*d       missing_authorization = 28.
        missing_authorization = 28
        conversion_error      = 36.

    LOOP AT stb.
      gt_tab-stlnr = stb-stlnr.
      gt_tab-stlty = stb-bmtyp.
      gt_tab-matnr = wa_malz-matnr.
      gt_tab-datuv = stb-datuv.
      gt_tab-stvkn = stb-stvkn.
      gt_tab-stpoz = stb-stpoz.
      gt_tab-posnr = stb-posnr.
      gt_tab-postp = stb-postp.
      gt_tab-menge = stb-menge.
      gt_tab-meins = stb-meins.
      WRITE: stb-idnrk TO gt_tab-zmm_matnr.
      gt_tab-maktx = stb-ojtxp.

      PERFORM stufe_aufbereiten.
      WRITE anz_stufe TO gt_tab-dglvl NO-SIGN.

      APPEND gt_tab.
    ENDLOOP.

  ENDLOOP.

ENDFORM.                    " F_MAT_BOM
*&---------------------------------------------------------------------*
*&      Form  F_WHERE_USED
*&---------------------------------------------------------------------*
FORM f_where_used  TABLES lt_malz_tmp TYPE STANDARD TABLE
                          lt_malz TYPE STANDARD TABLE.

  DATA: wa_malz TYPE tys_malz,
        wa_malz2 TYPE tys_malz.

  LOOP AT lt_malz_tmp INTO wa_malz2.

    CALL FUNCTION `CS_WHERE_USED_MAT`
      EXPORTING
        datub                      = pm_datub
        datuv                      = pm_datuv
        matnr                      = wa_malz2-matnr
        postp                      = pm_postp
        stlan                      = pm_stlan
        werks                      = wa_malz2-werks
*        stltp                      = `M` " stltp_in
      IMPORTING
        topmat                     = selpool2
      TABLES
        wultb                      = ltb
        equicat                    = equicat
        kndcat                     = kndcat
        matcat                     = matcat
        stdcat                     = stdcat
        tplcat                     = tplcat
        prjcat                     = prjcat
      EXCEPTIONS
        call_invalid               = 01
        material_not_found         = 02
        no_where_used_rec_found    = 03
        no_where_used_rec_selected = 04
        no_where_used_rec_valid    = 05.

    CLEAR wa_malz.
    LOOP AT matcat.
      wa_malz-matnr = matcat-matnr.
      wa_malz-werks = wa_malz2-werks.
      APPEND wa_malz TO lt_malz.
    ENDLOOP.

  ENDLOOP.

ENDFORM.                    " F_WHERE_USED
*&---------------------------------------------------------------------*
*&      Form  F_DYN_TABLE
*&---------------------------------------------------------------------*
FORM f_dyn_table .
  DATA: lv_depo_no(3) TYPE n.

  struct_type ?=
  cl_abap_typedescr=>describe_by_name( `GTY_TAB` ).
  "elem_type   ?= cl_abap_elemdescr=>describe_by_name( `F_COUNT` ).
  comp_tab = struct_type->get_components( ).

*  LOOP AT gt_depo.
  DO gv_max_depo TIMES.
    lv_depo_no = lv_depo_no + 1.
    CONCATENATE `DEPO_` lv_depo_no INTO comp_fld-name.
    elem_type   ?= cl_abap_elemdescr=>describe_by_name( `LGORT_D` ).
    comp_fld-type = elem_type.
    APPEND comp_fld TO comp_tab.

    CONCATENATE `MIKTAR_` lv_depo_no INTO comp_fld-name.
    elem_type   ?= cl_abap_elemdescr=>describe_by_name( `LABST` ).
    comp_fld-type = elem_type.
    APPEND comp_fld TO comp_tab.
  ENDDO.
*  ENDLOOP.

  struct_type = cl_abap_structdescr=>create( comp_tab ).
  itab_type   = cl_abap_tabledescr=>create( struct_type ).

  l_wa_name = `l_WA`.
  CREATE DATA dref TYPE HANDLE itab_type.
  ASSIGN dref->* TO <lt_outtab>.
  CREATE DATA dref TYPE HANDLE struct_type.
  ASSIGN dref->* TO <ls_outtab>.


ENDFORM.                    " F_DYN_TABLE
*&---------------------------------------------------------------------*
*&      Form  F_FILL_OUTTAB
*&---------------------------------------------------------------------*
FORM f_fill_outtab .

  DATA: lv_depo_no(3) TYPE n,
        lv_depo_name(20) TYPE c.

  LOOP AT gt_tab.
    ASSIGN COMPONENT `DGLVL` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-dglvl.
    ASSIGN COMPONENT `STLNR` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-stlnr.
    ASSIGN COMPONENT `STLTY` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-stlty .
    ASSIGN COMPONENT `MATNR` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-matnr.
    ASSIGN COMPONENT `DATUV` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-datuv.
    ASSIGN COMPONENT `STVKN` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-stvkn.
    ASSIGN COMPONENT `STPOZ` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-stpoz.
    ASSIGN COMPONENT `ZMM_MATNR` OF STRUCTURE <ls_outtab>
                                           TO <zmm_matnr>.
    <zmm_matnr> = gt_tab-zmm_matnr .
    ASSIGN COMPONENT `MAKTX` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-maktx.
    ASSIGN COMPONENT `POSNR` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-posnr.
    ASSIGN COMPONENT `POSTP` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-postp.
    ASSIGN COMPONENT `MENGE` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-menge.
    ASSIGN COMPONENT `MEINS` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-meins .
    ASSIGN COMPONENT `ZMM_RESIMNO` OF STRUCTURE <ls_outtab>
                                             TO <zmm_resimno>.
    <zmm_resimno> = gt_tab-zmm_resimno.
    ASSIGN COMPONENT `BISMT` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-bismt.
    ASSIGN COMPONENT `BRGEW` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-brgew.
    ASSIGN COMPONENT `MVGR1` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mvgr1.
    ASSIGN COMPONENT `MVGR2` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mvgr2.
    ASSIGN COMPONENT `MVGR3` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mvgr3.
    ASSIGN COMPONENT `MVGR4` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mvgr4.
    ASSIGN COMPONENT `MVGR5` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mvgr5.
    ASSIGN COMPONENT `MMSTA` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mmsta.
    ASSIGN COMPONENT `DISMM` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-dismm.
    ASSIGN COMPONENT `DISPO` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-dispo.
    ASSIGN COMPONENT `DISLS` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-disls.
    ASSIGN COMPONENT `BESKZ` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-beskz.
    ASSIGN COMPONENT `LGPRO` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-lgpro.
    ASSIGN COMPONENT `LGFSB` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-lgfsb.
    ASSIGN COMPONENT `STRGR` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-strgr.
    ASSIGN COMPONENT `MTVFP` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-mtvfp.
    ASSIGN COMPONENT `ALTSL` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-altsl.
    ASSIGN COMPONENT `SBDKZ` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-sbdkz.
    ASSIGN COMPONENT `SFCPF` OF STRUCTURE <ls_outtab> TO .
    = gt_tab-sfcpf.

    PERFORM f_check_matnr USING gt_tab-zmm_matnr.
    CLEAR lv_depo_no.
    LOOP AT gt_depo WHERE matnr = gt_tab-zmm_matnr.
*                        AND werks = -werks.

      lv_depo_no = lv_depo_no + 1.
      CONCATENATE `DEPO_` lv_depo_no INTO lv_depo_name.
      ASSIGN lv_depo_name TO <l_fld>.
      ASSIGN COMPONENT lv_depo_name OF STRUCTURE <ls_outtab>
                                              TO <l_fld>.
      <l_fld> = gt_depo-lgort.


      CONCATENATE `MIKTAR_` lv_depo_no INTO lv_depo_name.
      ASSIGN lv_depo_name TO <l_fld>.
      ASSIGN COMPONENT lv_depo_name OF STRUCTURE <ls_outtab>
                                              TO <l_fld>.
      <l_fld> = gt_depo-labst.

    ENDLOOP.

    APPEND <ls_outtab> TO <lt_outtab>.
    CLEAR <ls_outtab>.
  ENDLOOP.

ENDFORM.                    " F_FILL_OUTTAB
*&---------------------------------------------------------------------*
*&      Form  F_FILL_DEPO_TABLE
*&---------------------------------------------------------------------*
FORM f_fill_depo_table .

  DATA: ls_depo LIKE LINE OF gt_depo.
  DATA: lv_cnt_depo TYPE i.

  LOOP AT gt_tab.

    PERFORM f_check_matnr USING gt_tab-zmm_matnr.

    SELECT matnr lgort labst FROM mard
      INTO CORRESPONDING FIELDS OF ls_depo
      WHERE matnr EQ gt_tab-zmm_matnr
        AND werks IN s_werks.

      COLLECT ls_depo INTO gt_depo.

    ENDSELECT.
  ENDLOOP.

  SORT gt_depo.
  DELETE ADJACENT DUPLICATES FROM gt_depo COMPARING ALL FIELDS.

  CLEAR gv_max_depo.
  LOOP AT gt_depo.
    AT NEW matnr.
      CLEAR lv_cnt_depo.
    ENDAT.
    lv_cnt_depo = lv_cnt_depo + 1.

    IF lv_cnt_depo GT gv_max_depo.
      gv_max_depo = lv_cnt_depo.
    ENDIF.

  ENDLOOP.

ENDFORM.                    " F_FILL_DEPO_TABLE
*&---------------------------------------------------------------------*
*&      Form  F_FILL_GT_TAB_FIELDS
*&---------------------------------------------------------------------*
FORM f_fill_gt_tab_fields .
  DATA: lv_matnr TYPE matnr.

  LOOP AT gt_tab.

    lv_matnr = gt_tab-zmm_matnr.
    PERFORM f_check_matnr USING lv_matnr.

    SELECT SINGLE zmm_resimno bismt brgew FROM mara
      INTO CORRESPONDING FIELDS OF gt_tab
      WHERE matnr EQ lv_matnr.

    SELECT SINGLE mmsta dismm dispo disls beskz lgpro
           lgfsb strgr mtvfp altsl sbdkz sfcpf
      FROM marc
      INTO CORRESPONDING FIELDS OF gt_tab
      WHERE matnr EQ lv_matnr
        AND werks IN s_werks.

    SELECT SINGLE mvgr1 mvgr2 mvgr3 mvgr4 mvgr5 FROM mvke
      INTO CORRESPONDING FIELDS OF gt_tab
      WHERE matnr EQ lv_matnr
        AND vkorg EQ p_vkorg
        AND vtweg EQ p_vtweg.

    MODIFY gt_tab.
  ENDLOOP.

ENDFORM.                    " F_FILL_GT_TAB_FIELDS