物料反查BOM追溯报表

REPORT zrppp002.

TABLES: mast.

TYPES: BEGIN OF typ_mat_bom,
         matnr    TYPE marc-matnr, "当前物料编码
         werks    TYPE marc-werks, "当前工厂
         matnr_up TYPE marc-matnr, "上层物料编码
         werks_up TYPE marc-werks, "上层工厂
         stlan_up TYPE mast-stlan,
         stlty    TYPE stpo-stlty,
         stlnr    TYPE stpo-stlnr,
         stlkn    TYPE stpo-stlkn,
         stpoz    TYPE stpo-stpoz,
         posnr    TYPE stpo-posnr,
         bmeng    TYPE basmn,      "上层用量
         menge    TYPE kmpmg,      "组件用量
         meins    TYPE kmpme,      "组件单位
       END OF typ_mat_bom.

DATA: gt_mat_bom TYPE TABLE OF typ_mat_bom
                 WITH NON-UNIQUE SORTED KEY mat COMPONENTS matnr werks.

DATA: BEGIN OF gs_mat_list,
        matnr TYPE marc-matnr, "当前物料编码
        werks TYPE marc-werks, "当前工厂
        bom   TYPE TABLE OF typ_mat_bom,
      END OF gs_mat_list,
      gt_mat_list LIKE TABLE OF gs_mat_list.

DATA: BEGIN OF gs_list,
        matnr             TYPE marc-matnr, "材料
        werks             TYPE marc-werks, "工厂
        mat_top           TYPE marc-matnr, "物料编码
        maktx_top         TYPE makt-maktx, "物料描述
        mat_top2          TYPE marc-matnr, "物料编码
        mat_lv1           TYPE marc-matnr, "组件编码
        maktx_lv1         TYPE makt-maktx, "组件描述
        stlan             TYPE mast-stlan, "用途
        cuob_knowledge_01 TYPE string,     "特性信息
        cuob_knowledge_02 TYPE string,     "特性信息
        indx_srtfd        TYPE indx_srtfd,
        menge             TYPE kmpmg, 
        meins             TYPE meins,
        stlty             TYPE stpo-stlty,
        stlnr             TYPE stpo-stlnr,
        stlkn             TYPE stpo-stlkn,
        stpoz             TYPE stpo-stpoz,
        posnr             TYPE stpo-posnr,
      END OF gs_list,
      gt_list LIKE TABLE OF gs_list.

DATA: gs_layout TYPE lvc_s_layo,
      gs_fcat   TYPE lvc_s_fcat,
      gt_fcat   TYPE lvc_t_fcat.

***Selection screen
SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME TITLE TEXT-t01.
  SELECT-OPTIONS:
    s_matnr FOR mast-matnr.
  PARAMETERS:
    p_werks LIKE mast-werks OBLIGATORY,
    p_datuv LIKE stpo-datuv OBLIGATORY DEFAULT sy-datum.
*    p_stlan LIKE mast-stlan OBLIGATORY DEFAULT 'P'.
  SELECT-OPTIONS:
    s_stlan FOR mast-stlan OBLIGATORY NO INTERVALS DEFAULT 'P'.
SELECTION-SCREEN END OF BLOCK blk01.

************************************************************************
* INITIALIZATION
************************************************************************
INITIALIZATION.


************************************************************************
* START-OF-SELECTION
************************************************************************
START-OF-SELECTION.

  PERFORM frm_get_data.
*  PERFORM frm_get_up_data.
  PERFORM frm_get_up_data_new.

  PERFORM frm_set_fcat.

  PERFORM frm_list_data.


*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
FORM frm_get_data .

  IF s_matnr[] IS INITIAL.
    MESSAGE s000(oo) WITH '请输入<物料编码>' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  "物料信息
  SELECT matnr,werks FROM marc
    WHERE matnr IN @s_matnr
      AND werks EQ @p_werks
    INTO TABLE @DATA(lt_marc).

  LOOP AT lt_marc ASSIGNING FIELD-SYMBOL(<fs_marc>).

    "按物料+工厂向上递归追溯BOM直至顶层
    PERFORM frm_bom_used_mat USING <fs_marc>-matnr <fs_marc>-werks.

  ENDLOOP.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_bom_used_mat
*&---------------------------------------------------------------------*
FORM frm_bom_used_mat  USING uv_matnr TYPE marc-matnr
                             uv_werks TYPE marc-werks.

  DATA: BEGIN OF ls_marc,
          matnr LIKE marc-matnr,
          werks LIKE marc-werks,
        END OF ls_marc,
        lt_marc LIKE TABLE OF ls_marc.

  DATA: lt_marc_bom LIKE TABLE OF ls_marc.

  DATA: BEGIN OF ls_used_mat,
          datub TYPE rc29l-datub,
          datuv TYPE rc29l-datuv,
          matnr TYPE rc29l-matnr,
          stlan TYPE rc29l-stlan,
          werks TYPE rc29l-werks,
          stltp TYPE rc29a-atabr,
        END OF ls_used_mat.

  DATA: lt_wultb   TYPE TABLE OF stpov,
        lt_equicat TYPE TABLE OF cscequi,
        lt_kndcat  TYPE TABLE OF cscknd,
        lt_matcat  TYPE TABLE OF cscmat,
        lt_stdcat  TYPE TABLE OF cscstd,
        lt_tplcat  TYPE TABLE OF csctpl.

  DATA: lt_mat_bom TYPE TABLE OF typ_mat_bom.

  DATA: lv_count TYPE i.

  REFRESH: lt_marc,lt_mat_bom.

  "当前物料向上展BOM
  lt_marc = VALUE #( BASE lt_marc
                     ( matnr = uv_matnr
                       werks = uv_werks ) ).


  WHILE lt_marc IS NOT INITIAL.
    lv_count = lv_count + 1.

    LOOP AT lt_marc ASSIGNING FIELD-SYMBOL(<fs_marc>).
      "已反查BOM的物料不再重复反查
      LOOP AT gt_mat_bom ASSIGNING FIELD-SYMBOL(<fs_mat_bom>) USING KEY mat WHERE matnr = <fs_marc>-matnr
                                                                              AND werks = <fs_marc>-werks.
        IF lv_count = 1.
          APPEND <fs_mat_bom> TO lt_mat_bom.
        ELSE.
          EXIT.
        ENDIF.
      ENDLOOP.
      IF sy-subrc = 0.
        CONTINUE.
      ENDIF.

      "所有都要
      ls_used_mat = VALUE #( datuv = p_datuv
                             matnr = <fs_marc>-matnr
                             werks = <fs_marc>-werks
                             stltp = '   M      '
                             ).

      REFRESH: lt_wultb,lt_equicat,lt_kndcat,lt_matcat,lt_stdcat,lt_tplcat.

      CALL FUNCTION 'CS_WHERE_USED_MAT'
        EXPORTING
          datub                      = ls_used_mat-datub
          datuv                      = ls_used_mat-datuv
          matnr                      = ls_used_mat-matnr
          stlan                      = ls_used_mat-stlan
          werks                      = ls_used_mat-werks
          stltp                      = ls_used_mat-stltp
        TABLES
          wultb                      = lt_wultb
          equicat                    = lt_equicat
          kndcat                     = lt_kndcat
          matcat                     = lt_matcat
          stdcat                     = lt_stdcat
          tplcat                     = lt_tplcat
        EXCEPTIONS
          call_invalid               = 1
          material_not_found         = 2
          no_where_used_rec_found    = 3
          no_where_used_rec_selected = 4
          no_where_used_rec_valid    = 5
          OTHERS                     = 6.

      LOOP AT lt_wultb ASSIGNING FIELD-SYMBOL(<fs_wultb>) WHERE ( stlan = '1' OR stlan = 'P' )."只处理1或者P的,计算最顶层,否则会展到销售BOM做为顶层

        "BOM层级关系
        IF lv_count = 1."待反查物料对应的上层物料清单
          lt_mat_bom = VALUE #( BASE lt_mat_bom
                                ( matnr = ls_used_mat-matnr
                                  werks = ls_used_mat-werks
                                  matnr_up = <fs_wultb>-matnr
                                  werks_up = <fs_wultb>-werks
                                  stlan_up = <fs_wultb>-stlan
                                  stlty = <fs_wultb>-stlty
                                  stlnr = <fs_wultb>-stlnr
                                  stlkn = <fs_wultb>-stlkn
                                  stpoz = <fs_wultb>-stpoz
                                  posnr = <fs_wultb>-posnr
                                  bmeng = <fs_wultb>-bmeng
                                  menge = <fs_wultb>-menge
                                  meins = <fs_wultb>-meins
                                  ) ).
        ENDIF.

        "记录当前物料及上层物料关系
        gt_mat_bom = VALUE #( BASE gt_mat_bom
                              ( matnr = ls_used_mat-matnr
                                werks = ls_used_mat-werks
                                matnr_up = <fs_wultb>-matnr
                                werks_up = <fs_wultb>-werks
                                stlan_up = <fs_wultb>-stlan
                                stlty = <fs_wultb>-stlty
                                stlnr = <fs_wultb>-stlnr
                                stlkn = <fs_wultb>-stlkn
                                stpoz = <fs_wultb>-stpoz
                                posnr = <fs_wultb>-posnr
                                bmeng = <fs_wultb>-bmeng
                                menge = <fs_wultb>-menge
                                meins = <fs_wultb>-meins
                                ) ).

        lt_marc_bom = VALUE #( BASE lt_marc_bom
                               ( matnr = <fs_wultb>-matnr
                                 werks = <fs_wultb>-werks ) ).
      ENDLOOP.
    ENDLOOP.

    REFRESH: lt_marc.
    lt_marc = lt_marc_bom.

    REFRESH: lt_marc_bom.
    "经沟通最多十几层BOM,这里超过20层BOM,退出
    IF lv_count >= 20.
      EXIT.
    ENDIF.
  ENDWHILE.

*  "当前物料反查的清单
*  SORT lt_mat_bom BY matnr werks mantr_up werks_up stlan_up.
*  DELETE ADJACENT DUPLICATES FROM lt_mat_bom COMPARING matnr werks mantr_up werks_up stlan_up.

  gt_mat_list = VALUE #( BASE gt_mat_list
                         ( matnr = uv_matnr
                           werks = uv_werks
                           bom = lt_mat_bom ) ).

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_get_up_data
*&---------------------------------------------------------------------*
FORM frm_get_up_data .

*  DATA: BEGIN OF knowledge_tab OCCURS 10.
*          INCLUDE STRUCTURE m60i.
*  DATA: END OF knowledge_tab.
*
*  DATA: xscrout       TYPE c,
*        in_date       TYPE sy-datum,
*        in_root_matnr TYPE matnr.
*
*  DATA  BEGIN OF stpo_buffer OCCURS 0.
*  INCLUDE STRUCTURE stpo.
*  DATA  END   OF stpo_buffer.
*  DATA: ls_stpo_buffer LIKE LINE OF stpo_buffer.
*
*  DATA: lt_cuob_knowledge TYPE zapst_line_char78.
*
*  LOOP AT gt_mat_list ASSIGNING FIELD-SYMBOL(<fs_mat_list>).
*
*    gs_list = VALUE #( matnr = <fs_mat_list>-matnr
*                       werks = <fs_mat_list>-werks
*                       ).
*
*    LOOP AT <fs_mat_list>-bom ASSIGNING FIELD-SYMBOL(<fs_bom>) WHERE stlan_up = p_stlan.
*      "按上层物料反查是否存在上层,不存在上层为顶层
*      READ TABLE gt_mat_bom WITH TABLE KEY mat COMPONENTS matnr = <fs_bom>-mantr_up
*                                                          werks = <fs_bom>-werks_up TRANSPORTING NO FIELDS.
*      IF sy-subrc = 0.
*        CONTINUE.
*      ELSE.
*        gs_list-mat_top = <fs_bom>-mantr_up.
*        gs_list-mat_lv1 = <fs_bom>-matnr.
*        gs_list-stlan   = p_stlan.
*
*        APPEND gs_list TO gt_list.
*      ENDIF.
*    ENDLOOP.
*  ENDLOOP.
*
*
*  IF gt_list IS NOT INITIAL.
*    "物料描述
*    SELECT matnr,maktx FROM makt
*      FOR ALL ENTRIES IN @gt_list
*      WHERE matnr = @gt_list-mat_top
*        AND spras = @sy-langu
*      INTO TABLE @DATA(lt_makt).
*
*    SELECT matnr,maktx FROM makt
*      FOR ALL ENTRIES IN @gt_list
*      WHERE matnr = @gt_list-mat_lv1
*        AND spras = @sy-langu
*      APPENDING TABLE @lt_makt.
*
*    "取物料特性参考逻辑
*    SELECT
*      a~matnr AS matnr_root,
*      a~werks AS werks_root,
*      c~matnr,
*      c~out_proba,
*      c~indx_srtfd,
*      d~menge,  
*      d~meins
*      FROM mast AS a
*      INNER JOIN stko AS b ON b~stlnr = a~stlnr
*                          AND b~stlal = a~stlal
*      INNER JOIN zapst_bom_stpo AS c ON c~stlty = b~stlty
*                                    AND c~stlnr = b~stlnr
*      LEFT JOIN stpo AS d ON c~stlty = d~stlty  "
*                         AND c~stlnr = d~stlnr
*                         AND c~stlkn = d~stlkn
*                         AND c~stpoz = d~stpoz
*      FOR ALL ENTRIES IN @gt_list
*      WHERE a~matnr = @gt_list-mat_top
*        AND a~werks = @gt_list-werks
*        AND a~stlan = @p_stlan
*        AND b~stlty = 'M'
*        AND c~matnr = @gt_list-mat_lv1
*      INTO TABLE @DATA(lt_bom_stpo).
*
*    SORT lt_bom_stpo BY matnr_root werks_root matnr.
*
*    "CTO物料
*    SELECT matnr,kzkfg FROM mara
*      FOR ALL ENTRIES IN @gt_list
*      WHERE matnr = @gt_list-mat_top
*      INTO TABLE @DATA(lt_mara).
*    SORT lt_mara BY matnr.
*  ENDIF.
*
*
*  SORT lt_makt BY matnr.
*  LOOP AT gt_list ASSIGNING FIELD-SYMBOL(<fs_list>).
*    DATA(lv_tabix) = sy-tabix.
*
*    READ TABLE lt_mara INTO DATA(ls_mara) WITH KEY matnr = <fs_list>-mat_top BINARY SEARCH.
*    IF sy-subrc NE 0.
*      CLEAR ls_mara.
*    ENDIF.
*
*    "只保留CTO
*    IF ls_mara-kzkfg NE 'X'.
*      DELETE gt_list INDEX lv_tabix.
*      CONTINUE.
*    ENDIF.
*
*    CLEAR: knowledge_tab[],in_date,in_root_matnr,xscrout,ls_stpo_buffer,lt_cuob_knowledge.
*
*    "物料描述
*    READ TABLE lt_makt ASSIGNING FIELD-SYMBOL(<fs_makt>) WITH KEY matnr = <fs_list>-mat_top BINARY SEARCH.
*    IF sy-subrc = 0.
*      <fs_list>-maktx_top = <fs_makt>-maktx.
*    ENDIF.
*
*    READ TABLE lt_makt ASSIGNING <fs_makt> WITH KEY matnr = <fs_list>-mat_lv1 BINARY SEARCH.
*    IF sy-subrc = 0.
*      <fs_list>-maktx_lv1 = <fs_makt>-maktx.
*    ENDIF.
*
*    "
*    READ TABLE lt_bom_stpo ASSIGNING FIELD-SYMBOL(<fs_bom_stpo>)
*                           WITH KEY matnr_root = <fs_list>-mat_top
*                                    werks_root = <fs_list>-werks
*                                    matnr = <fs_list>-mat_lv1 BINARY SEARCH.
*    IF sy-subrc = 0.
*      <fs_list>-out_proba_dec = <fs_bom_stpo>-out_proba.
*      <fs_list>-indx_srtfd = <fs_bom_stpo>-indx_srtfd.
*      <fs_list>-menge = <fs_bom_stpo>-menge.
*      <fs_list>-meins = <fs_bom_stpo>-meins.
*
*      IF <fs_bom_stpo>-indx_srtfd IS NOT INITIAL.
*        IMPORT
*          knowledge_tab = knowledge_tab
*          in_date = in_date
*          in_root_matnr = in_root_matnr
*          xscrout = xscrout
*          wa_stpo_buffer = ls_stpo_buffer
*         FROM DATABASE zapst_bom_indx(z1) ID <fs_bom_stpo>-indx_srtfd.
*
*        PERFORM explain_list_header IN PROGRAM zapsrp0057  " FROM explain_list_header is copied/refered from program LM60PFEX
*                           TABLES knowledge_tab
**                                  lt_cuob_knowledge
*                           USING  ls_stpo_buffer-knobj in_date
*                                  in_root_matnr
*                                  xscrout
*                           CHANGING <fs_list>-cuob_knowledge_01
*                                    <fs_list>-cuob_knowledge_02 IF FOUND. "
*
**        LOOP AT lt_cuob_knowledge ASSIGNING FIELD-SYMBOL(<fs_cuob_knowledge>).
**          <fs_list>-cuob_knowledge = <fs_list>-cuob_knowledge && ` ` && <fs_cuob_knowledge>-line.
**        ENDLOOP.
*      ENDIF.
*    ENDIF.
*  ENDLOOP.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_set_fcat
*&---------------------------------------------------------------------*
FORM frm_set_fcat .

  CLEAR: gt_fcat.
  PERFORM frm_add_fcat USING:
        'MATNR'           TEXT-f01    'MATNR'           'MARC',
        'WERKS'           TEXT-f02    'WERKS'           'MARC',
        'MENGE'           TEXT-f11    'MENGE'           'STPO',
        'MEINS'           TEXT-f12    'MEINS'           'STPO',
        'MAT_TOP'         TEXT-f03    'MATNR'           'MARC',
        'MAKTX_TOP'       TEXT-f04    'MAKTX'           'MAKT',
        'MAT_TOP2'        TEXT-f13    'MATNR'           'MARC',
        'POSNR'           TEXT-f14    'POSNR'           'STPO',
        'MAT_LV1'         TEXT-f05    'MATNR'           'MARC',
        'MAKTX_LV1'       TEXT-f06    'MAKTX'           'MAKT',
        'STLAN'           TEXT-f07    'STLAN'           'MAST',
        'OUT_PROBA_DEC'   TEXT-f08    'OUT_PROBA_DEC'   'ZAPSS_BOM_REQ_PROB_DATA',
        'CUOB_KNOWLEDGE_01'  TEXT-f09    'CUOB_KNOWLEDGE_01'  'ZAPSS_BOM_REQ_PROB_DATA',
        'CUOB_KNOWLEDGE_02'  TEXT-f10    'CUOB_KNOWLEDGE_02'  'ZAPSS_BOM_REQ_PROB_DATA'.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_add_fcat
*&---------------------------------------------------------------------*
FORM frm_add_fcat  USING VALUE(uv_fieldname)
                               uv_coltext
                         VALUE(uv_ref_field)
                         VALUE(uv_ref_table).

  CLEAR: gs_fcat.

  gs_fcat-fieldname = uv_fieldname.
  gs_fcat-scrtext_l = uv_coltext.
  gs_fcat-scrtext_m = uv_coltext.
  gs_fcat-scrtext_s = uv_coltext.
  gs_fcat-coltext   = uv_coltext.
  gs_fcat-ref_field = uv_ref_field.
  gs_fcat-ref_table = uv_ref_table.

  IF gs_fcat-fieldname = 'MENGE'.
    gs_fcat-no_zero = 'X'.
  ENDIF.

  APPEND gs_fcat TO gt_fcat.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_list_data
*&---------------------------------------------------------------------*
FORM frm_list_data .

  gs_layout-cwidth_opt  = 'X'.
  gs_layout-zebra       = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'FRM_STATUS_SET'
      i_callback_user_command  = 'FRM_USER_COMMAND'
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fcat
      i_default                = 'X'
      i_save                   = 'A'
    TABLES
      t_outtab                 = gt_list
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  frm_status_set
*&---------------------------------------------------------------------*
FORM frm_status_set  USING ut_extab TYPE slis_t_extab.

  REFRESH ut_extab.

  SET PF-STATUS 'PF_9000' EXCLUDING ut_extab.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  frm_user_command_mat
*&---------------------------------------------------------------------*
FORM frm_user_command USING u_ucomm LIKE sy-ucomm
                            us_selfield TYPE slis_selfield.

  CASE u_ucomm.
    WHEN '&IC1'.
      READ TABLE gt_list INTO gs_list INDEX us_selfield-tabindex.
      IF sy-subrc = 0.
        CALL FUNCTION 'ZREQUIREMENTS_EXPLAIN_PROB_GET'
          EXPORTING
            iv_id = gs_list-indx_srtfd.
        SET TITLEBAR 'TI_9001'.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.

  us_selfield-refresh = 'X'.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_get_up_data_new
*&---------------------------------------------------------------------*
FORM frm_get_up_data_new .

  DATA: BEGIN OF knowledge_tab OCCURS 10.
          INCLUDE STRUCTURE m60i.
  DATA: END OF knowledge_tab.

  DATA: xscrout       TYPE c,
        in_date       TYPE sy-datum,
        in_root_matnr TYPE matnr.

  DATA  BEGIN OF stpo_buffer OCCURS 0.
  INCLUDE STRUCTURE stpo.
  DATA  END   OF stpo_buffer.
  DATA: ls_stpo_buffer LIKE LINE OF stpo_buffer.

  DATA: lt_used_mat_while     TYPE TABLE OF typ_mat_bom,
        ls_used_mat_while_tmp TYPE typ_mat_bom,
        lt_used_mat_while_tmp TYPE TABLE OF typ_mat_bom.

  DATA: lt_list LIKE TABLE OF gs_list.

  DATA: lt_basic_data TYPE TABLE OF rcukb1,
        lt_sources    TYPE TABLE OF rcukn1.

  DATA: lv_count TYPE i.

  IF gt_mat_bom IS NOT INITIAL.
    "CTO物料
    SELECT matnr,kzkfg FROM mara
      FOR ALL ENTRIES IN @gt_mat_bom
      WHERE matnr = @gt_mat_bom-matnr_up
      INTO TABLE @DATA(lt_mara).
    SORT lt_mara BY matnr kzkfg.
  ENDIF.

  SORT gt_mat_bom BY matnr werks.
  LOOP AT gt_mat_list ASSIGNING FIELD-SYMBOL(<fs_mat_list>).

    gs_list = VALUE #( matnr = <fs_mat_list>-matnr
                       werks = <fs_mat_list>-werks
                       ).

    lt_used_mat_while = <fs_mat_list>-bom.

    CLEAR: lv_count.
    "当前物料进行递归,找最物料,并统计单位用量
    WHILE lt_used_mat_while IS NOT INITIAL.

      lv_count = lv_count + 1.
      REFRESH: lt_used_mat_while_tmp.

      LOOP AT lt_used_mat_while ASSIGNING FIELD-SYMBOL(<fs_used_mat_while>).

        IF lv_count = 1."当前层的用量
          IF <fs_used_mat_while>-bmeng NE 0.
            <fs_used_mat_while>-menge = <fs_used_mat_while>-menge / <fs_used_mat_while>-bmeng.
          ENDIF.
        ENDIF.

        READ TABLE gt_mat_bom WITH KEY matnr = <fs_used_mat_while>-matnr_up
                                       werks = <fs_used_mat_while>-werks_up BINARY SEARCH TRANSPORTING NO FIELDS.

        IF sy-subrc = 0.
          DATA(lv_tabix_bom) = sy-tabix.

          LOOP AT gt_mat_bom ASSIGNING FIELD-SYMBOL(<fs_mat_bom>) FROM lv_tabix_bom.
            IF <fs_mat_bom>-matnr NE <fs_used_mat_while>-matnr_up OR <fs_mat_bom>-werks NE <fs_used_mat_while>-werks_up.
              EXIT.
            ENDIF.

            IF <fs_mat_bom>-matnr CS '-CTO-S02'."S02单独处理
              READ TABLE lt_mara WITH KEY matnr = <fs_mat_bom>-matnr_up
                                          kzkfg = 'X' BINARY SEARCH TRANSPORTING NO FIELDS.
              IF sy-subrc = 0.
                "SO2获取当层
                gs_list-mat_top = <fs_mat_bom>-matnr_up.
                gs_list-mat_top2 = <fs_mat_bom>-matnr.
                gs_list-mat_lv1 = <fs_used_mat_while>-matnr.
                gs_list-stlan   = <fs_used_mat_while>-stlan_up.
                gs_list-menge   = COND #( WHEN <fs_mat_bom>-bmeng = 0
                                             THEN <fs_mat_bom>-menge
                                           ELSE <fs_mat_bom>-menge * <fs_used_mat_while>-menge / <fs_mat_bom>-bmeng ).
                gs_list-meins   = <fs_used_mat_while>-meins.

                gs_list-stlty   = <fs_used_mat_while>-stlty.
                gs_list-stlnr   = <fs_used_mat_while>-stlnr.
                gs_list-stlkn   = <fs_used_mat_while>-stlkn.
                gs_list-stpoz   = <fs_used_mat_while>-stpoz.
                gs_list-posnr   = <fs_used_mat_while>-posnr.
                APPEND gs_list TO gt_list.
              ENDIF.
            ELSE.
              ls_used_mat_while_tmp = <fs_mat_bom>.
              ls_used_mat_while_tmp-menge = COND #( WHEN <fs_mat_bom>-bmeng = 0
                                                        THEN <fs_mat_bom>-menge
                                                      ELSE <fs_mat_bom>-menge * <fs_used_mat_while>-menge / <fs_mat_bom>-bmeng ).
              gs_list-meins   = <fs_used_mat_while>-meins.

              APPEND ls_used_mat_while_tmp TO lt_used_mat_while_tmp.
            ENDIF.
          ENDLOOP.
        ELSE.
          "获取失败,已是最顶层,取显示CTO物料
          READ TABLE lt_mara WITH KEY matnr = <fs_used_mat_while>-matnr_up
                                      kzkfg = 'X' BINARY SEARCH TRANSPORTING NO FIELDS.
          IF sy-subrc = 0.
            gs_list-mat_top = <fs_used_mat_while>-matnr_up.
            gs_list-mat_top2 = <fs_used_mat_while>-matnr_up.
            gs_list-mat_lv1 = <fs_used_mat_while>-matnr.
            gs_list-stlan   = <fs_used_mat_while>-stlan_up.
            gs_list-menge   = <fs_used_mat_while>-menge.
            gs_list-meins   = <fs_used_mat_while>-meins.

            gs_list-stlty   = <fs_used_mat_while>-stlty.
            gs_list-stlnr   = <fs_used_mat_while>-stlnr.
            gs_list-stlkn   = <fs_used_mat_while>-stlkn.
            gs_list-stpoz   = <fs_used_mat_while>-stpoz.
            gs_list-posnr   = <fs_used_mat_while>-posnr.
            APPEND gs_list TO gt_list.
          ENDIF.
        ENDIF.
      ENDLOOP.

      REFRESH: lt_used_mat_while.
      lt_used_mat_while = lt_used_mat_while_tmp.

      IF lv_count >= 20.
        EXIT.
      ENDIF.
    ENDWHILE.
  ENDLOOP.

  IF gt_list IS NOT INITIAL.
    "物料描述
    SELECT matnr,maktx FROM makt
      FOR ALL ENTRIES IN @gt_list
      WHERE matnr = @gt_list-mat_top
        AND spras = @sy-langu
      INTO TABLE @DATA(lt_makt).

    SELECT matnr,maktx FROM makt
      FOR ALL ENTRIES IN @gt_list
      WHERE matnr = @gt_list-mat_lv1
        AND spras = @sy-langu
      APPENDING TABLE @lt_makt.

    SELECT stlty,stlnr,stlkn,stpoz,knobj FROM stpo
      FOR ALL ENTRIES IN @gt_list
      WHERE stlty = @gt_list-stlty
        AND stlnr = @gt_list-stlnr
        AND stlkn = @gt_list-stlkn
        AND stpoz = @gt_list-stpoz
      INTO TABLE @DATA(lt_stpo).
  ENDIF.

  "物料特性P用途参考ZAPS0070,1用途取标准
  DATA(lt_list_p) = gt_list.
  DELETE lt_list_p WHERE stlan NE 'P'.

  IF lt_list_p IS NOT INITIAL.
    "取物料特性参考逻辑
    SELECT stlty,stlnr,stlkn,stpoz,out_proba,indx_srtfd
      FROM zapst_bom_stpo
      FOR ALL ENTRIES IN @lt_list_p
      WHERE stlty = @lt_list_p-stlty
        AND stlnr = @lt_list_p-stlnr
        AND stlkn = @lt_list_p-stlkn
        AND stpoz = @lt_list_p-stpoz
      INTO TABLE @DATA(lt_bom_stpo).
  ENDIF.

  SORT lt_makt BY matnr.
  SORT lt_bom_stpo BY stlty stlnr stlkn stpoz.
  SORT lt_stpo BY stlty stlnr stlkn stpoz.
  LOOP AT gt_list ASSIGNING FIELD-SYMBOL(<fs_list>).
    DATA(lv_tabix) = sy-tabix.

    IF <fs_list>-stlan NOT IN s_stlan[].
      DELETE gt_list INDEX lv_tabix.
      CONTINUE.
    ENDIF.

    "物料描述
    READ TABLE lt_makt ASSIGNING FIELD-SYMBOL(<fs_makt>) WITH KEY matnr = <fs_list>-mat_top BINARY SEARCH.
    IF sy-subrc = 0.
      <fs_list>-maktx_top = <fs_makt>-maktx.
    ENDIF.

    READ TABLE lt_makt ASSIGNING <fs_makt> WITH KEY matnr = <fs_list>-mat_lv1 BINARY SEARCH.
    IF sy-subrc = 0.
      <fs_list>-maktx_lv1 = <fs_makt>-maktx.
    ENDIF.

    "P用途参考ZAPS0070取特性,1用途取标准
    IF <fs_list>-stlan = 'P'.
      <fs_list>-menge = <fs_list>-menge * 100.

      READ TABLE lt_bom_stpo ASSIGNING FIELD-SYMBOL(<fs_bom_stpo>) WITH KEY stlty = <fs_list>-stlty
                                                                            stlnr = <fs_list>-stlnr
                                                                            stlkn = <fs_list>-stlkn
                                                                            stpoz = <fs_list>-stpoz
                                                                            BINARY SEARCH.
      IF sy-subrc = 0.
        <fs_list>-out_proba_dec = <fs_bom_stpo>-out_proba.
        <fs_list>-indx_srtfd = <fs_bom_stpo>-indx_srtfd.

        IF <fs_bom_stpo>-indx_srtfd IS NOT INITIAL.
          IMPORT
            knowledge_tab = knowledge_tab
            in_date = in_date
            in_root_matnr = in_root_matnr
            xscrout = xscrout
            wa_stpo_buffer = ls_stpo_buffer
             FROM DATABASE zapst_bom_indx(z1) ID <fs_bom_stpo>-indx_srtfd.

          PERFORM explain_list_header IN PROGRAM zapsrp0057  " FROM explain_list_header is copied/refered from program LM60PFEX
                             TABLES knowledge_tab
                             USING  ls_stpo_buffer-knobj in_date
                                    in_root_matnr
                                    xscrout
                             CHANGING <fs_list>-cuob_knowledge_01
                                      <fs_list>-cuob_knowledge_02 IF FOUND. "Hw 498851
        ENDIF.
      ENDIF.
    ELSEIF <fs_list>-stlan = '1'.

      READ TABLE lt_stpo ASSIGNING FIELD-SYMBOL(<fs_stpo>) WITH KEY stlty = <fs_list>-stlty
                                                                    stlnr = <fs_list>-stlnr
                                                                    stlkn = <fs_list>-stlkn
                                                                    stpoz = <fs_list>-stpoz
                                                                    BINARY SEARCH.
      IF sy-subrc = 0.
        IF <fs_stpo>-knobj IS NOT INITIAL.
          REFRESH: lt_basic_data,lt_sources.

          CALL FUNCTION 'CUKD_API_INIT'.

          CALL FUNCTION 'CUKD_API_ALLOCATIONS_READ'
            EXPORTING
              allocation_number   = <fs_stpo>-knobj
              table               = 'STPO'
              with_alloc_data     = ''
              with_basic_data     = 'X'
              with_lang_dep_names = ''
              with_docus          = ''
              with_sources        = 'X'
            TABLES
              basic_data          = lt_basic_data
              sources             = lt_sources
            EXCEPTIONS
              error               = 1
              OTHERS              = 2.
          SORT lt_sources BY knnam line_no.
          "特性信息-选择
          READ TABLE lt_basic_data ASSIGNING FIELD-SYMBOL(<fs_basic_data>) WITH KEY knart = '5'.
          IF sy-subrc = 0.
            READ TABLE lt_sources WITH KEY knnam = <fs_basic_data>-knnam BINARY SEARCH TRANSPORTING NO FIELDS.
            IF sy-subrc = 0.
              DATA(lv_tabix_sources) = sy-tabix.

              LOOP AT lt_sources ASSIGNING FIELD-SYMBOL(<fs_sources>) FROM lv_tabix_sources.
                IF <fs_sources>-knnam NE <fs_basic_data>-knnam.
                  EXIT.
                ENDIF.

                <fs_list>-cuob_knowledge_01 = COND #( WHEN <fs_list>-cuob_knowledge_01 IS INITIAL
                                                        THEN <fs_sources>-line
                                                      ELSE |{ <fs_list>-cuob_knowledge_01 } { <fs_sources>-line }| ).
              ENDLOOP.
            ENDIF.
          ENDIF.

          "特性信息-过程
          READ TABLE lt_basic_data ASSIGNING <fs_basic_data> WITH KEY knart = '7'.
          IF sy-subrc = 0.
            READ TABLE lt_sources WITH KEY knnam = <fs_basic_data>-knnam BINARY SEARCH TRANSPORTING NO FIELDS.
            IF sy-subrc = 0.
              lv_tabix_sources = sy-tabix.

              LOOP AT lt_sources ASSIGNING <fs_sources> FROM lv_tabix_sources.
                IF <fs_sources>-knnam NE <fs_basic_data>-knnam.
                  EXIT.
                ENDIF.

                <fs_list>-cuob_knowledge_02 = COND #( WHEN <fs_list>-cuob_knowledge_02 IS INITIAL
                                                        THEN <fs_sources>-line
                                                      ELSE |{ <fs_list>-cuob_knowledge_02 } { <fs_sources>-line }| ).
              ENDLOOP.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.

ENDFORM.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值