SAP 批量获取物料特性值(Classification)

2023-10-13 18:40:42 浏览数 (3)

代码语言:javascript复制
REPORT zmm009.

TYPE-POOLS: slis.
TABLES : mara.

TYPES : BEGIN OF ltype_mara,
          matnr TYPE mara-matnr,
        END OF ltype_mara.

TYPES : BEGIN OF ltype_cabn,
          atnam TYPE cabn-atnam,
          atbez TYPE cabnt-atbez,
        END OF ltype_cabn.

TYPES : BEGIN OF ltype_itab,
          objek TYPE inob-objek, "物料代码
          atinn TYPE cabn-atinn, "内部特性
          normt TYPE mara-normt,
          groes TYPE mara-groes,
          maktx TYPE makt-maktx,
          atnam TYPE cabn-atnam, "特性名称
          atwrt TYPE ausp-atwrt, "特性值
        END OF ltype_itab.

TYPES : BEGIN OF ltype_tab,
          objek TYPE inob-objek, "物料代码
          atinn TYPE cabn-atinn, "内部特性
          atnam TYPE cabn-atnam, "特性名称
          atflv TYPE ausp-atflv, "特性值
        END OF ltype_tab.

TYPES : BEGIN OF ltype_cawnt,
          atinn TYPE cawnt-atinn,
          atwtb TYPE cawnt-atwtb,
        END OF ltype_cawnt.

TYPES : BEGIN OF ltype_makt,
          matnr TYPE mara-matnr,
          normt TYPE mara-normt,
          groes TYPE mara-groes,
          maktx TYPE makt-maktx,
        END OF ltype_makt.

DATA : lt_makt TYPE TABLE OF ltype_makt,
       ls_makt LIKE LINE OF lt_makt.

DATA : lt_cawnt TYPE TABLE OF ltype_cawnt,
       ls_cawnt LIKE LINE OF lt_cawnt.

DATA : class_value TYPE cha_class_view-sollwert.

DATA : lt_ausp TYPE TABLE OF ltype_tab,
       ls_ausp LIKE LINE OF lt_ausp.

DATA : lt_tab TYPE TABLE OF ltype_itab,
       ls_tab LIKE LINE OF lt_tab.

DATA : lt_mara TYPE TABLE OF ltype_mara,
       ls_mara LIKE LINE OF lt_mara.

DATA : l_object TYPE kssk-objek,
       l_class  TYPE klah-class,
       l_matty  TYPE atwrt,
       lt_class TYPE TABLE OF sclass,
       ls_class LIKE LINE OF lt_class,
       lt_objda TYPE TABLE OF clobjdat,
       ls_objda LIKE LINE OF lt_objda.

DATA : lt_fieldcat TYPE slis_t_fieldcat_alv.
DATA : it_ls_fieldcat TYPE slis_fieldcat_alv.
DATA : sla         TYPE slis_layout_alv,
       ivariant    LIKE disvariant,
       i_repid     LIKE sy-repid,
       i_excluding TYPE slis_t_extab.

DATA: dy_table TYPE REF TO data,
      dy_wa    TYPE REF TO data,
      it_str   TYPE lvc_t_fcat,  "是Table Type
      wa_str   TYPE lvc_s_fcat.  "是一个Structure  用于存储即将构建的动态内表结构

FIELD-SYMBOLS: <dyn_table> TYPE table,  "是一个标志,可以理解为一个指针,将来创建的内表就要通过它们来访问
               <dyn_wa>    TYPE any,
               <fs>        TYPE any.

FIELD-SYMBOLS : <lt_data> TYPE table.

DATA : lt_cabn TYPE TABLE OF ltype_cabn,
       ls_cabn LIKE LINE OF lt_cabn.

SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME.

SELECT-OPTIONS : so_matnr FOR mara-matnr.

SELECTION-SCREEN END OF BLOCK blk01.

IF so_matnr IS INITIAL.

  MESSAGE 'Input Material' TYPE 'E'.

ELSE.

  PERFORM frm_get_data.

  PERFORM frm_alv_display.

ENDIF.

*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_get_data .

  SELECT inob~objek          "物料代码
       cabn~atinn            "内部特性
       cabn~atnam            "特性名称
       ausp~atwrt            "特性值
      INTO CORRESPONDING FIELDS OF TABLE lt_tab
      FROM inob
      INNER JOIN ausp ON inob~cuobj = ausp~objek
      INNER JOIN cabn ON cabn~atinn = ausp~atinn
      WHERE inob~objek IN so_matnr.

  IF lt_tab IS NOT INITIAL.

    SELECT inob~objek          "物料代码
         cabn~atinn            "内部特性
         cabn~atnam            "特性名称
         ausp~atflv            "特性值
        INTO CORRESPONDING FIELDS OF TABLE lt_ausp
        FROM inob
        INNER JOIN ausp ON inob~cuobj = ausp~objek
        INNER JOIN cabn ON cabn~atinn = ausp~atinn
        FOR ALL ENTRIES IN lt_tab
        WHERE inob~objek = lt_tab-objek.

    CLEAR lt_cawnt.
    SELECT
       atinn
       atwtb
      FROM cawnt
      INTO CORRESPONDING FIELDS OF TABLE lt_cawnt
      FOR ALL ENTRIES IN lt_tab
      WHERE atinn = lt_tab-atinn
      AND atzhl = '0001'
      AND spras = sy-langu.

    IF lt_ausp IS NOT INITIAL.

      CLEAR ls_tab.
      LOOP AT lt_tab INTO ls_tab.

        CLEAR lt_makt.
        SELECT
          t1~matnr
          t1~groes
          t1~normt
          t2~maktx
          FROM mara AS t1 INNER JOIN makt AS t2
          ON t1~matnr = t2~matnr
          INTO CORRESPONDING FIELDS OF TABLE lt_makt
          WHERE t1~matnr = ls_tab-objek
          AND t2~spras = sy-langu.

        CLEAR ls_makt.
        READ TABLE lt_makt INTO ls_makt WITH KEY matnr = ls_tab-objek.
        ls_tab-groes = ls_makt-groes.
        ls_tab-normt = ls_makt-normt.
        ls_tab-maktx = ls_makt-maktx.

        IF ls_tab-atwrt = '#'.

          CLEAR ls_cawnt.
          READ TABLE lt_cawnt INTO ls_cawnt WITH KEY atinn = ls_tab-atinn.
          IF sy-subrc = 0.
            ls_tab-atwrt = ls_cawnt-atwtb.
          ELSE.
            ls_tab-atwrt = 'w/o'.
          ENDIF.

        ENDIF.

        CLEAR ls_ausp.
        READ TABLE lt_ausp INTO ls_ausp WITH KEY objek = ls_tab-objek atinn = ls_tab-atinn.

        IF ls_tab-atwrt IS INITIAL.

          CALL FUNCTION 'QSS0_FLTP_TO_CHAR_CONVERSION'
            EXPORTING
              i_number_of_digits       = 2
              i_fltp_value             = ls_ausp-atflv
              i_value_not_initial_flag = 'X'
              i_screen_fieldlength     = 16
            IMPORTING
              e_char_field             = class_value.

          ls_tab-atwrt = class_value.

        ENDIF.

        MODIFY lt_tab FROM ls_tab TRANSPORTING atwrt groes normt maktx.

      ENDLOOP.

    ENDIF.

  ENDIF.

  IF lt_tab IS NOT INITIAL.

    SORT lt_tab BY objek atinn.

    DATA title(20) TYPE c OCCURS 0 WITH HEADER LINE.

    LOOP AT lt_tab INTO ls_tab.

      title = ls_tab-atnam.
      COLLECT title.

    ENDLOOP.

    wa_str-fieldname = 'BOX'."固定列 防止界面返回时报错
    wa_str-col_pos = 1.
    wa_str-inttype = 'CHAR'.
    wa_str-intlen = 2.
    APPEND wa_str TO it_str.

    wa_str-fieldname = 'OBJEK'."固定列
    wa_str-col_pos = 1.
    wa_str-inttype = 'CHAR'.
    wa_str-intlen = 18.
    APPEND wa_str TO it_str.
    CLEAR wa_str.

    wa_str-fieldname = 'ATINN'."固定列
    wa_str-col_pos = 1.
    wa_str-inttype = 'CHAR'.
    wa_str-intlen = 18.
    APPEND wa_str TO it_str.
    CLEAR wa_str.

    wa_str-fieldname = 'MAKTX'."固定列
    wa_str-col_pos = 1.
    wa_str-inttype = 'CHAR'.
    wa_str-intlen = 30.
    APPEND wa_str TO it_str.
    CLEAR wa_str.

    wa_str-fieldname = 'GROES'."固定列
    wa_str-col_pos = 1.
    wa_str-inttype = 'CHAR'.
    wa_str-intlen = 30.
    APPEND wa_str TO it_str.
    CLEAR wa_str.

    wa_str-fieldname = 'NORMT'."固定列
    wa_str-col_pos = 1.
    wa_str-inttype = 'CHAR'.
    wa_str-intlen = 30.
    APPEND wa_str TO it_str.
    CLEAR wa_str.

    REFRESH lt_fieldcat.
    PERFORM input_fieldcat  USING 'OBJEK'  'Material'  ''  '' '' '' ''."固定列描述
    PERFORM input_fieldcat  USING 'ATINN'  'Class number'  ''  '' '' '' ''.
    PERFORM input_fieldcat  USING 'MAKTX'  'Material Description'  ''  '' '' '' ''.
    PERFORM input_fieldcat  USING 'GROES'  'Size/dimensions'  ''  '' '' '' ''.
    PERFORM input_fieldcat  USING 'NORMT'  'Industry Standard Description'  ''  '' '' '' ''.

    LOOP AT title."标题赋值
      wa_str-fieldname = title.
      wa_str-col_pos = 1.
      wa_str-inttype = 'CHAR'.
      wa_str-intlen = 20.
      APPEND wa_str TO it_str.
      CLEAR wa_str.
      PERFORM input_fieldcat  USING title  title  ''  '' '' '' ''.
    ENDLOOP.

    "此方法用于构建动态内表,输入=构建的结构,输出=dy_table
    CALL METHOD cl_alv_table_create=>create_dynamic_table
      EXPORTING
        it_fieldcatalog = it_str
      IMPORTING
        ep_table        = dy_table.


    ASSIGN dy_table->* TO <dyn_table>.
    CREATE DATA dy_wa LIKE LINE OF <dyn_table>.
    ASSIGN dy_wa->* TO <dyn_wa>.

    DATA fieldname(20).
    LOOP AT lt_tab INTO ls_tab.
      ASSIGN COMPONENT 'OBJEK' OF STRUCTURE <dyn_wa> TO <fs>.  "分配物料,用FS字段 指向它
      <fs> = ls_tab-objek.
      ASSIGN COMPONENT 'ATINN' OF STRUCTURE <dyn_wa> TO <fs>.  "分配类型值,用FS字段 指向它
      <fs> = ls_tab-atinn.
      ASSIGN COMPONENT 'GROES' OF STRUCTURE <dyn_wa> TO <fs>.  "分配类型值,用FS字段 指向它
      <fs> = ls_tab-groes.
      ASSIGN COMPONENT 'NORMT' OF STRUCTURE <dyn_wa> TO <fs>.  "分配类型值,用FS字段 指向它
      <fs> = ls_tab-normt.
      ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <dyn_wa> TO <fs>.  "分配类型值,用FS字段 指向它
      <fs> = ls_tab-maktx.
      fieldname = ls_tab-atnam. "title对应。
      ASSIGN COMPONENT fieldname OF STRUCTURE <dyn_wa> TO <fs>. "分配结构的日期,将FS指向它
      <fs> = ls_tab-atwrt.
      AT END OF objek.
        APPEND <dyn_wa> TO <dyn_table>.
      ENDAT.

    ENDLOOP.

  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form INPUT_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*      -->P_       text
*      -->P_       text
*      -->P_       text
*      -->P_       text
*      -->P_       text
*      -->P_       text
*      -->P_       text
*&---------------------------------------------------------------------*
FORM input_fieldcat  USING  p1 p2 p3 p4 p5 p6 p7.

  it_ls_fieldcat-fieldname    = p1.
  it_ls_fieldcat-seltext_l    = p2.

  APPEND it_ls_fieldcat TO lt_fieldcat.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_alv_display .

  DATA:
    gs_layout TYPE slis_layout_alv,
    g_repid   LIKE sy-repid,
    gt_events TYPE slis_t_event,
    gt_sort   TYPE slis_t_sortinfo_alv.

  gs_layout-colwidth_optimize = 'X'. " 自动列宽
  gs_layout-zebra             = 'X'. " 斑马线显示输出
  gs_layout-get_selinfos      = 'X'.
  gs_layout-box_fieldname     = 'BOX'.

  PERFORM frm_alv_name CHANGING lt_fieldcat.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = lt_fieldcat
      is_layout          = gs_layout
      it_sort            = gt_sort
      i_save             = 'A'
    TABLES
      t_outtab           = <dyn_table>.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_NAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_alv_name CHANGING lt_fieldcat TYPE slis_t_fieldcat_alv .

  DATA : ls_fieldcat LIKE LINE OF lt_fieldcat.

  DELETE lt_fieldcat WHERE  fieldname = 'ATINN'.

  LOOP AT lt_fieldcat INTO ls_fieldcat WHERE fieldname NE'OBJEK'.

    IF  ls_fieldcat-fieldname NE 'MAKTX'.
      IF ls_fieldcat-fieldname NE 'GROES'.
        IF ls_fieldcat-fieldname NE 'NORMT'.

          CLEAR lt_cabn[].
          SELECT
            t1~atnam
            t2~atbez
            FROM cabn AS t1
            INNER JOIN cabnt AS t2 ON t1~atinn = t2~atinn AND t1~adzhl = t2~adzhl
            INTO CORRESPONDING FIELDS OF TABLE lt_cabn
            WHERE t1~atnam = ls_fieldcat-fieldname
            AND t2~spras = 'EN'.

          CLEAR ls_cabn.
          READ TABLE lt_cabn INTO ls_cabn INDEX 1 .
          ls_fieldcat-seltext_l = ls_cabn-atbez.

          MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING seltext_l.

        ENDIF.
      ENDIF.
    ENDIF.

  ENDLOOP.

ENDFORM.

0 人点赞