欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

动态ALV表实例-移动类型汇总

TABLES:MSEG,MAKT.
"定义结构
TYPES:BEGIN OF TY_DATA,
        MJAHR      LIKE MSEG-MJAHR, "物料凭证的年份
        MBLNR      LIKE MSEG-MBLNR, "物料凭证
        BWART      LIKE MSEG-BWART, "移动类型
        XAUTO      LIKE MSEG-XAUTO, "是否自动创建
        MATNR      LIKE MSEG-MATNR, "物料编码
        MAKTX      LIKE MAKT-MAKTX, "物料描述
        WERKS      LIKE MSEG-WERKS, "工厂
        LGORT      LIKE MSEG-LGORT, "库位
        CHARG      LIKE MSEG-CHARG, "批次
        MENGE      LIKE MSEG-MENGE, "基本单位数量
        MEINS      LIKE MSEG-MEINS, "基本单位
        ERFMG      LIKE MSEG-ERFMG, "录入数量
        ERFME      LIKE MSEG-ERFME, "录入单位
        BUDAT_MKPF LIKE MSEG-BUDAT_MKPF, "过账日期
        CPUDT_MKPF LIKE MSEG-CPUDT_MKPF, "输入日期
        CPUTM_MKPF LIKE MSEG-CPUTM_MKPF, "输入时间
        USNAM_MKPF LIKE MSEG-USNAM_MKPF, "用户名
      END OF TY_DATA.
TYPES:BEGIN OF TY_DATA1,
*        MJAHR      LIKE MSEG-MJAHR, "物料凭证的年份
*        MBLNR      LIKE MSEG-MBLNR, "物料凭证
        WERKS LIKE MSEG-WERKS, "工厂
        LGORT LIKE MSEG-LGORT, "库位
        MATNR LIKE MSEG-MATNR, "物料编码
        MAKTX LIKE MAKT-MAKTX, "物料描述
        BWART LIKE MSEG-BWART, "移动类型
*        XAUTO      LIKE MSEG-XAUTO, "是否自动创建
*        CHARG      LIKE MSEG-CHARG, "批次
        MENGE LIKE MSEG-MENGE, "基本单位数量
        MEINS LIKE MSEG-MEINS, "基本单位
*        ERFMG      LIKE MSEG-ERFMG, "录入数量
*        ERFME      LIKE MSEG-ERFME, "录入单位
*        BUDAT_MKPF LIKE MSEG-BUDAT_MKPF, "过账日期
*        CPUDT_MKPF LIKE MSEG-CPUDT_MKPF, "输入日期
*        CPUTM_MKPF LIKE MSEG-CPUTM_MKPF, "输入时间
*        USNAM_MKPF LIKE MSEG-USNAM_MKPF, "用户名
      END OF TY_DATA1.
TYPES:BEGIN OF TY_DATA2,
*        MJAHR      LIKE MSEG-MJAHR, "物料凭证的年份
*        MBLNR      LIKE MSEG-MBLNR, "物料凭证
*        WERKS      LIKE MSEG-WERKS, "工厂
*        LGORT      LIKE MSEG-LGORT, "库位
*        MATNR      LIKE MSEG-MATNR, "物料编码
*        MAKTX      LIKE MAKT-MAKTX, "物料描述
        BWART LIKE MSEG-BWART, "移动类型
*        XAUTO      LIKE MSEG-XAUTO, "是否自动创建
*        CHARG      LIKE MSEG-CHARG, "批次
*        MENGE      LIKE MSEG-MENGE, "基本单位数量
*        MEINS      LIKE MSEG-MEINS, "基本单位
*        ERFMG      LIKE MSEG-ERFMG, "录入数量
*        ERFME      LIKE MSEG-ERFME, "录入单位
*        BUDAT_MKPF LIKE MSEG-BUDAT_MKPF, "过账日期
*        CPUDT_MKPF LIKE MSEG-CPUDT_MKPF, "输入日期
*        CPUTM_MKPF LIKE MSEG-CPUTM_MKPF, "输入时间
*        USNAM_MKPF LIKE MSEG-USNAM_MKPF, "用户名
      END OF TY_DATA2.
DATA:IT_DATA TYPE STANDARD TABLE OF TY_DATA,
     WA_DATA TYPE TY_DATA.
DATA:IT_DATA1 TYPE STANDARD TABLE OF TY_DATA1,
     WA_DATA1 TYPE TY_DATA1.
DATA:IT_DATA2 TYPE STANDARD TABLE OF TY_DATA2,
     WA_DATA2 TYPE TY_DATA2.
DATA:N TYPE I.
DATA: IT_TABLE     TYPE REF TO DATA,
      IT_STRUCTURE TYPE LVC_T_FCAT,
      WA_STRUCTURE TYPE LVC_S_FCAT.
DATA: WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
      IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
      WA_LAYOUT   TYPE LVC_S_LAYO.
DATA: G_TITLE    TYPE LVC_TITLE.
"FIELD SYMBOLS
FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>,
               <DYN_FIELD>.
"定义屏幕
SELECTION-SCREEN BEGIN OF BLOCK BLK01.
SELECT-OPTIONS:S_WERKS FOR MSEG-WERKS,"工厂
               S_LGORT FOR MSEG-LGORT,"库位
               S_MATNR FOR MSEG-MATNR,"物料
               S_BUDAT FOR MSEG-BUDAT_MKPF,"过账日期
               S_CPUDT FOR MSEG-CPUDT_MKPF,"输入日期
               S_BWART FOR MSEG-BWART,"移动类型
               S_USNAM FOR MSEG-USNAM_MKPF."username
SELECTION-SCREEN END OF BLOCK BLK01.
"主程序

START-OF-SELECTION.
  PERFORM GET_DATA."取数
  PERFORM CREAT_STRUCTURE."创建内表结构
  PERFORM CREAT_TABLE."按照结构定义内表
  PERFORM DATA_TO_TABLE."写数据进内表
  PERFORM FRM_INIT_LAYOUT.
  PERFORM FRM_SHOW_ALV."内表数据展示
FORM GET_DATA.
  SELECT
  MSEG~MJAHR,
  MSEG~MBLNR,
  MSEG~BWART,
  MSEG~XAUTO,
  MSEG~MATNR,
  MAKT~MAKTX,
  MSEG~WERKS,
  MSEG~LGORT,
  MSEG~CHARG,
  CASE  MSEG~SHKZG
   WHEN 'H' THEN ( 0 - MSEG~MENGE )
  ELSE MSEG~MENGE
    END AS MENGE,
  MSEG~MEINS,
  CASE  MSEG~SHKZG
   WHEN 'H' THEN ( 0 - MSEG~ERFMG )
  ELSE MSEG~ERFMG
    END AS ERFMG,
  MSEG~ERFME,
  MSEG~BUDAT_MKPF,
  MSEG~CPUDT_MKPF,
  MSEG~CPUTM_MKPF,
  MSEG~USNAM_MKPF
  FROM MSEG
  LEFT JOIN MAKT ON MAKT~MATNR = MSEG~MATNR
  INTO CORRESPONDING FIELDS OF TABLE @IT_DATA
  WHERE MSEG~WERKS IN @S_WERKS
        AND MSEG~LGORT IN @S_LGORT
        AND MSEG~MATNR IN @S_MATNR
        AND MSEG~BUDAT_MKPF IN @S_BUDAT
        AND MSEG~BWART IN @S_BWART
        AND MSEG~USNAM_MKPF IN @S_USNAM
        AND MSEG~CPUDT_MKPF IN @S_CPUDT.
  LOOP AT IT_DATA INTO WA_DATA.
    MOVE-CORRESPONDING WA_DATA TO WA_DATA1.
    COLLECT WA_DATA1 INTO IT_DATA1.
    CLEAR:WA_DATA,WA_DATA1.
  ENDLOOP.


ENDFORM.
FORM CREAT_STRUCTURE.
  DATA: L_LINES TYPE I.
  LOOP AT IT_DATA1 INTO WA_DATA1.
    MOVE-CORRESPONDING WA_DATA1 TO WA_DATA2.
    APPEND WA_DATA2 TO IT_DATA2.
  ENDLOOP.

  SORT IT_DATA2 BY BWART.
  DELETE ADJACENT DUPLICATES FROM IT_DATA2 COMPARING BWART.
  DESCRIBE TABLE IT_DATA2 LINES N.
  L_LINES = 5.


* 创建静态结构部分
  PERFORM FRM_ALV_FCAT_SET USING:
    1   'WERKS' TEXT-007 'WERKS'  'MSEG'  SPACE   SPACE,
    2   'LGORT' TEXT-008 'LGORT'  'MSEG'  SPACE   SPACE,
    3   'MATNR' TEXT-008 'MATNR'  'MAKT'  SPACE   SPACE,
    4   'MAKTX' TEXT-008 'MAKTX'  'MAKT'  SPACE   SPACE.
*    5   'BUDAT_MKPF' TEXT-008 'BUDAT_MKPF'  'MSEG'  SPACE   SPACE,
*    6   'CPUDT_MKPF' TEXT-008 'CPUDT_MKPF'  'MSEG'  SPACE   SPACE.

  LOOP AT IT_DATA2 INTO WA_DATA2.
*    DO N TIMES.
    CLEAR WA_STRUCTURE.
*字段名称
    WA_STRUCTURE-FIELDNAME = WA_DATA2-BWART.
*列名称
*    CONCATENATE '移动类型' WA_DATA2-BWART INTO WA_STRUCTURE-SCRTEXT_L.
    WA_STRUCTURE-SCRTEXT_L = WA_DATA2-BWART.
    WA_STRUCTURE-SCRTEXT_M = WA_STRUCTURE-SCRTEXT_L.
    WA_STRUCTURE-SCRTEXT_S = WA_STRUCTURE-SCRTEXT_L.
    WA_STRUCTURE-COLTEXT   = WA_STRUCTURE-SCRTEXT_L.
    WA_STRUCTURE-NO_ZERO   = 'X'.
    WA_STRUCTURE-DO_SUM    = 'X'.

    WA_STRUCTURE-REF_FIELD = 'MENGE'.
    WA_STRUCTURE-REF_TABLE = 'EKPO'.

    WA_STRUCTURE-COL_POS    = L_LINES.
    APPEND WA_STRUCTURE TO IT_STRUCTURE.

    L_LINES = L_LINES + 1.
  ENDLOOP.
  WA_STRUCTURE-FIELDNAME = 'SUM'.
  WA_STRUCTURE-SCRTEXT_L = '行总计'.
  WA_STRUCTURE-SCRTEXT_M = WA_STRUCTURE-SCRTEXT_L.
  WA_STRUCTURE-SCRTEXT_S = WA_STRUCTURE-SCRTEXT_L.
  WA_STRUCTURE-COLTEXT   = WA_STRUCTURE-SCRTEXT_L.
  WA_STRUCTURE-NO_ZERO   = 'X'.
  WA_STRUCTURE-DO_SUM    = 'X'.
*  WA_STRUCTURE-REF_FIELD = 'MENGE'.
*  WA_STRUCTURE-REF_TABLE = 'EKPO'.
  WA_STRUCTURE-COL_POS   = L_LINES.
  APPEND WA_STRUCTURE TO IT_STRUCTURE.

ENDFORM.
FORM CREAT_TABLE.
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = IT_STRUCTURE
    IMPORTING
      EP_TABLE        = IT_TABLE.

  ASSIGN IT_TABLE->* TO <DYN_TABLE>.
ENDFORM.
FORM DATA_TO_TABLE.
  DATA:WA_NEW_LINE TYPE REF TO DATA.
  CREATE DATA WA_NEW_LINE LIKE LINE OF <DYN_TABLE>.
  ASSIGN WA_NEW_LINE->* TO <DYN_WA>.

  DATA: L_SUM(7) TYPE P DECIMALS 3.

  DATA:L_FIELDNAME TYPE LVC_S_FCAT-FIELDNAME.
  DATA:LW_ERFMG TYPE TY_DATA1.
  SORT IT_DATA1 BY WERKS LGORT MATNR.
  LOOP AT IT_DATA1 INTO WA_DATA1.
    CLEAR:L_FIELDNAME.
    L_FIELDNAME = WA_DATA1-BWART.
    ASSIGN COMPONENT L_FIELDNAME OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
    IF WA_DATA1-MENGE IS NOT INITIAL.
      <DYN_FIELD> = WA_DATA1-MENGE.
      L_SUM = L_SUM + WA_DATA1-MENGE.
    ENDIF.
    LW_ERFMG = WA_DATA1.
    AT END OF MATNR.
      ASSIGN COMPONENT 'WERKS' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
      <DYN_FIELD> = LW_ERFMG-WERKS.
      ASSIGN COMPONENT 'LGORT' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
      <DYN_FIELD> = LW_ERFMG-LGORT.
      ASSIGN COMPONENT 'MATNR' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
      <DYN_FIELD> = LW_ERFMG-MATNR.
      ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
      <DYN_FIELD> = LW_ERFMG-MAKTX.
*      ASSIGN COMPONENT 'BUDAT_MKPF' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
*      <DYN_FIELD> = LW_ERFMG-BUDAT_MKPF.
*      ASSIGN COMPONENT 'CPUDT_MKPF' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
*      <DYN_FIELD> = LW_ERFMG-CPUDT_MKPF.
      ASSIGN COMPONENT 'SUM' OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
      <DYN_FIELD> = L_SUM.
      APPEND <DYN_WA> TO <DYN_TABLE>.
      CLEAR: L_SUM, <DYN_WA>.
    ENDAT.

    CLEAR: LW_ERFMG, WA_DATA1.



  ENDLOOP.


*  DELETE <DYN_TABLE> WHERE MATNR IS INITIAL.


ENDFORM.
*设置layout属性
FORM FRM_INIT_LAYOUT .
  G_TITLE = TEXT-006.

  CLEAR WA_LAYOUT.
  WA_LAYOUT-CWIDTH_OPT = 'X'.
ENDFORM.                    " FRM_INIT_LAYOUT
FORM FRM_SHOW_ALV.
*  * ALV显示函数
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IS_LAYOUT_LVC      = WA_LAYOUT
      IT_FIELDCAT_LVC    = IT_STRUCTURE
      I_GRID_TITLE       = G_TITLE
*     i_callback_pf_status_set = 'PF_STATUS_SET'
*     IT_EXCLUDING       =       "系统自带STATUS图标控制内表
      I_SAVE             = 'A'
*     i_callback_pf_status_set = 'PF_STATUS_SET'
*     i_callback_user_command  = 'FRM_USER_COMMAND'
    TABLES
      T_OUTTAB           = <DYN_TABLE>
    EXCEPTIONS
      PROGRAM_ERROR      = 1
      OTHERS             = 2.
  IF SY-SUBRC <> 0.
*    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.




ENDFORM.
FORM FRM_ALV_FCAT_SET USING PU_POS    TYPE I
                            PU_FNAME  TYPE C
                            PU_FTEXT  TYPE C
                            PU_RFIELD TYPE C
                            PU_RTABLE TYPE C
                            PU_QNAME  TYPE C
                            PU_EDIT   TYPE C.
  CLEAR WA_STRUCTURE.
  WA_STRUCTURE-COL_POS    = PU_POS.
  WA_STRUCTURE-FIELDNAME  = PU_FNAME.
  WA_STRUCTURE-SCRTEXT_L  = PU_FTEXT.
  WA_STRUCTURE-SCRTEXT_M  = PU_FTEXT.
  WA_STRUCTURE-SCRTEXT_S  = PU_FTEXT.
  WA_STRUCTURE-REF_FIELD  = PU_RFIELD.
  WA_STRUCTURE-REF_TABLE  = PU_RTABLE.
  WA_STRUCTURE-QFIELDNAME = PU_QNAME.
  WA_STRUCTURE-EDIT       = PU_EDIT.
  WA_STRUCTURE-KEY = 'X'.
  APPEND WA_STRUCTURE TO IT_STRUCTURE.
ENDFORM.                    " FRM_ALV_FCAT_S

 

posted @ 2019-07-11 17:01  萧静默  阅读(416)  评论(0编辑  收藏  举报