ABAP动态内表ALV

  我们经常遇到用户对报表栏位动态显示的需求,比如FI/CO报表里,用户希望根据选择屏幕输入不同的会计期间,来对比不同期间的数据,这种通过普通内表是无法构造美观的报表的。下面这支程式,是用来展现一台笔记本电脑各主要部件价格。其中部件栏位允许用户扩充,扩充的方式是请用户上传excel,抓取表头存到数据库里。此外,还支持按物料组汇总功能(报表二)(点击报表一汇总按钮)。主要功能说明,请看重点注释。


Code listing for: ZSDQ1002E

Description: Finish Good Pricing Report

*&---------------------------------------------------------------------*
*& Report  ZSDQ1002E
*&
*&---------------------------------------------------------------------*
*& Request :Cost Saving Project
*& Author : 居士爱吃泡面
*& Creation Date : 2014/03/11
*& Purpose: Finish Good Pricing Report
*&---------------------------------------------------------------------*

REPORT ZSDQ1002E NO STANDARD PAGE HEADING.

TABLES: MARC.

TYPE-POOLS: SLIS.

DATA: GW_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DATA: GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: GW_LAYOUT TYPE SLIS_LAYOUT_ALV.

DATA: IT_FIELDCAT_LVC TYPE LVC_T_FCAT,
      WA_FIELDCAT_LVC TYPE LVC_S_FCAT.

DATA: GRID TYPE REF TO CL_GUI_ALV_GRID.

DATA: DYN_TABLE TYPE REF TO DATA,
      DYN_LINE  TYPE REF TO DATA.

FIELD-SYMBOLS: <FS_FIELDCAT> LIKE LINE OF GT_FIELDCAT,
               <LINE> TYPE STANDARD TABLE,
               <WA> TYPE ANY,
               <FS_FIELD> TYPE ANY,
               <FS_FIELD1> TYPE ANY,
               <FS_FIELD2> TYPE ANY,
               <FS_FIELD3> TYPE ANY.

DATA: BEGIN OF WA_OUTPUT1,
  PROJECT LIKE ZCOT018-NORMT,
  WERKS LIKE MARC-WERKS,
  MATNR LIKE MARA-MATNR,
  MAKTX LIKE MAKT-MAKTX,
  IDNRK LIKE STPO-IDNRK,
  KDMAT LIKE ZSDQ1-KDMAT,
  MAKTX1 LIKE MAKT-MAKTX,
  ALPGR LIKE STPO-ALPGR,
  MENGE LIKE STPO-MENGE,
  EWAHR LIKE STPO-EWAHR,
  MENGE1 LIKE STPO-MENGE,
  MATKL LIKE ZSDQ1-MATKL,
  PRTYPE LIKE ZSDQ1-PRTYPE,
  KBETR LIKE ZSDQ1-KBETR,
  KBETR1 LIKE ZSDQ1-KBETR,
  END OF WA_OUTPUT1.

DATA: WA_ZSDQ1 TYPE ZSDQ1,
      WA_OUTPUT3 TYPE ZSDQ1002E_S.

DATA: IT_ZSDQ1 LIKE TABLE OF WA_ZSDQ1,
      IT_OUTPUT1 LIKE TABLE OF WA_OUTPUT1,
      IT_OUTPUT2 LIKE TABLE OF WA_OUTPUT1 WITH HEADER LINE,
      IT_OUTPUT3 LIKE TABLE OF WA_OUTPUT3.

FIELD-SYMBOLS: <FS1> LIKE WA_OUTPUT1.

DATA: G_MATNR LIKE MARC-MATNR,
      G_MAKTX LIKE MAKT-MAKTX,
      G_PROJECT(5).

DATA: BEGIN OF WA_MATNR,
  MATNR LIKE MARA-MATNR,
  END OF WA_MATNR.

DATA: BEGIN OF WA_ERROR,
  MATNR LIKE MARA-MATNR,
  IDNRK LIKE STPO-IDNRK,
  END OF WA_ERROR.

DATA: IT_MATNR LIKE TABLE OF WA_MATNR WITH HEADER LINE,
      IT_ERROR LIKE TABLE OF WA_ERROR WITH HEADER LINE,
      IT_HEAD LIKE TABLE OF ZSDQ7 WITH HEADER LINE.

DATA: I(5) TYPE C VALUE '1'.
DEFINE ADD_FIELDCAT.
  CLEAR:GW_FIELDCAT.
  GW_FIELDCAT-COL_POS = I.
  GW_FIELDCAT-TABNAME = 'it_output1'.
  GW_FIELDCAT-FIELDNAME = &1.
  GW_FIELDCAT-SELTEXT_M = &2.
  GW_FIELDCAT-OUTPUTLEN = &3.
  GW_FIELDCAT-EMPHASIZE = &4.
  APPEND GW_FIELDCAT TO GT_FIELDCAT.
  I = I + 1.
END-OF-DEFINITION.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002.
SELECT-OPTIONS: S_MATNR FOR MARC-MATNR.
PARAMETERS: P_WERKS LIKE MARC-WERKS DEFAULT 'CN06' OBLIGATORY,
            P_DATUV LIKE STKO-DATUV OBLIGATORY DEFAULT SY-DATUM,
            P_DATBI LIKE ZSDQ1-DATBI OBLIGATORY DEFAULT SY-DATUM,
            P_KURSF LIKE BKPF-KURSF.
SELECTION-SCREEN END OF BLOCK B1.

SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_FILE LIKE RLGRAP-FILENAME .
SELECTION-SCREEN END OF BLOCK B2.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      MASK      = ''
      STATIC    = 'X'
    CHANGING
      FILE_NAME = P_FILE.

START-OF-SELECTION.

  IF S_MATNR IS INITIAL AND P_FILE IS INITIAL.
    MESSAGE 'Input Material' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

  IF P_FILE IS NOT INITIAL.
    PERFORM GET_UPLOAD_HEAD.
  ELSE.
    PERFORM GET_LBG_PRICE.
  ENDIF.


*&---------------------------------------------------------------------*
*&      Form  GET_UPLOAD_HEAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM GET_UPLOAD_HEAD.
  DATA: L_INTERN LIKE TABLE OF ALSMEX_TABLINE WITH HEADER LINE,
        ZNO TYPE I VALUE 1.
*将上传EXCEL数据表头导入到内表
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      FILENAME                = P_FILE
      I_BEGIN_COL             = '1'
      I_BEGIN_ROW             = '1'
      I_END_COL               = '200'
      I_END_ROW               = '1'
    TABLES
      INTERN                  = L_INTERN
    EXCEPTIONS
      INCONSISTENT_PARAMETERS = 1
      UPLOAD_OLE              = 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.

  LOOP AT L_INTERN.
    CONDENSE L_INTERN-VALUE.
    IT_HEAD-MATKL = L_INTERN-VALUE.
    IT_HEAD-ZNUMBER = ZNO.
    APPEND IT_HEAD.
    ZNO = ZNO + 1.
  ENDLOOP.

*删除旧版表头,写入新版表头(栏位+编号)到自定义表
  DELETE FROM ZSDQ7.
  INSERT ZSDQ7 FROM TABLE IT_HEAD.
  IF SY-SUBRC EQ 0.
    MESSAGE 'Update header Sucess !' TYPE 'S'.
  ENDIF.

ENDFORM. "GET_UPLOAD_HEAD

*&---------------------------------------------------------------------*
*&      Form  get_lbg_price
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM GET_LBG_PRICE.
  LOOP AT S_MATNR.
    IT_MATNR-MATNR = S_MATNR-LOW.
    APPEND IT_MATNR.

    CLEAR : G_MATNR, G_PROJECT, G_MAKTX, WA_OUTPUT1.

    G_MATNR = S_MATNR-LOW.
    SHIFT G_MATNR LEFT DELETING LEADING '0'.
    G_PROJECT = G_MATNR(5).

    SELECT SINGLE MAKTX INTO G_MAKTX
      FROM MAKT
      WHERE MATNR = G_MATNR AND SPRAS = SY-LANGU.

    PERFORM GET_BOM_LIST USING S_MATNR-LOW 100 1 .
  ENDLOOP.


  IF IT_OUTPUT1 IS NOT INITIAL.
*抓取LBG价格信息
    SELECT *
      INTO CORRESPONDING FIELDS OF TABLE IT_ZSDQ1
      FROM ZSDQ1
      FOR ALL ENTRIES IN IT_OUTPUT1
      WHERE MATNR = IT_OUTPUT1-IDNRK AND TYPE = 'L'
       AND DATAB <= P_DATBI AND DATBI => P_DATBI.
  ELSE.
    MESSAGE 'No BOM data find !' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.


*得到原材物料组及计算原材价格
  LOOP AT IT_OUTPUT1 ASSIGNING <FS1>.
    CLEAR WA_ERROR.
    READ TABLE IT_ZSDQ1 INTO WA_ZSDQ1 WITH KEY MATNR = <FS1>-IDNRK.
    IF SY-SUBRC EQ 0.
      <FS1>-MATKL = WA_ZSDQ1-MATKL.
      <FS1>-PRTYPE = WA_ZSDQ1-PRTYPE.
      <FS1>-KDMAT = WA_ZSDQ1-KDMAT.
      <FS1>-KBETR = WA_ZSDQ1-KBETR.
      <FS1>-KBETR1 = <FS1>-MENGE * <FS1>-KBETR * <FS1>-EWAHR / 100.
    ELSE.
      WA_ERROR-IDNRK = <FS1>-IDNRK.
      WA_ERROR-MATNR = <FS1>-MATNR.
      APPEND WA_ERROR TO IT_ERROR.
    ENDIF.
  ENDLOOP.

  SORT IT_ERROR BY MATNR IDNRK.
  DELETE ADJACENT DUPLICATES FROM IT_ERROR.

  IF IT_ERROR[] IS NOT INITIAL.
    WRITE: 'The fllowing material price need maintain: '.
    ULINE.
    LOOP AT IT_ERROR INTO WA_ERROR.
      WRITE:/ WA_ERROR-MATNR, WA_ERROR-IDNRK.
    ENDLOOP.
  ELSE.
    PERFORM DISPLAY_ALV_OUTPUT1.
  ENDIF.
ENDFORM. "get_lbg_price
*&---------------------------------------------------------------------*
*&      Form  GET_BOM_LIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_BOM_LIST USING U_MATNR TYPE MARA-MATNR
                        U_EWAHR TYPE STPO-EWAHR
                        U_MENGE TYPE STPO-MENGE.

*递归的方式展BOM到底阶,计算BOM里原材的数量(根据替代比和用量)
  DATA: LW_STB TYPE STPOX.
  DATA: LT_STB TYPE TABLE OF STPOX,
        L_EWAHR TYPE STPO-EWAHR,
        L_MENGE TYPE STPO-MENGE.

  CLEAR:LW_STB,LT_STB[].
  CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
    EXPORTING
      CAPID                 = 'PP01'
      DATUV                 = P_DATUV
      MTNRV                 = U_MATNR
      WERKS                 = P_WERKS
    TABLES
      STB                   = LT_STB
    EXCEPTIONS
      ALT_NOT_FOUND         = 1
      CALL_INVALID          = 2
      MATERIAL_NOT_FOUND    = 3
      MISSING_AUTHORIZATION = 4
      NO_BOM_FOUND          = 5
      NO_PLANT_DATA         = 6
      NO_SUITABLE_BOM_FOUND = 7
      CONVERSION_ERROR      = 8
      OTHERS                = 9.
*已经到底阶了
  IF SY-SUBRC = 5 OR SY-SUBRC = 7.
    IF WA_OUTPUT1 IS NOT INITIAL.
      APPEND WA_OUTPUT1 TO IT_OUTPUT1.
    ENDIF.
  ENDIF.

  LOOP AT LT_STB INTO LW_STB.
    IF U_EWAHR IS INITIAL.
      L_EWAHR = LW_STB-EWAHR.
    ELSE.
      IF LW_STB-ALPGR IS NOT INITIAL.
        L_EWAHR = LW_STB-EWAHR * U_EWAHR / 100.
      ELSE.
        L_EWAHR = U_EWAHR.
      ENDIF.
    ENDIF.

    L_MENGE = U_MENGE * LW_STB-MENGE.

    CLEAR WA_OUTPUT1.
    WA_OUTPUT1-PROJECT = G_PROJECT.
    WA_OUTPUT1-WERKS = P_WERKS.
    WA_OUTPUT1-MATNR = G_MATNR.
    WA_OUTPUT1-MAKTX = G_MAKTX.
    WA_OUTPUT1-IDNRK = LW_STB-IDNRK.
    WA_OUTPUT1-MAKTX1 = LW_STB-OJTXP.
    WA_OUTPUT1-MENGE = L_MENGE.
    WA_OUTPUT1-MENGE1 = L_MENGE * L_EWAHR / 100.
    WA_OUTPUT1-EWAHR = L_EWAHR.
    WA_OUTPUT1-ALPGR = LW_STB-ALPGR.

    PERFORM GET_BOM_LIST USING LW_STB-IDNRK L_EWAHR L_MENGE.
  ENDLOOP.


ENDFORM. " GET_BOM_LIST
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_OUTPUT1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DISPLAY_ALV_OUTPUT1 .
  ADD_FIELDCAT 'PROJECT' 'Model' 18 'C500'.
  ADD_FIELDCAT 'WERKS' 'Plant' 4 'C500'.
  ADD_FIELDCAT 'MATNR' 'Material' 18 'C500'.
  ADD_FIELDCAT 'MAKTX' 'Desc.' 40 'C500'.
  ADD_FIELDCAT 'IDNRK' 'Component' 18 'C500'.
  ADD_FIELDCAT 'KDMAT' 'Cust. Material' 35 'C500'.
  ADD_FIELDCAT 'MAKTX1' 'Desc.' 40 'C500'.
  ADD_FIELDCAT 'ALPGR' 'AltItemGroup' 2 'C500'.
  ADD_FIELDCAT 'MENGE1' 'Usage' 10 'C500'.
  ADD_FIELDCAT 'MATKL' 'Material Group' 9 'C500'.
  ADD_FIELDCAT 'PRTYPE' 'pur. type' 10 'C500'.
  ADD_FIELDCAT 'KBETR' 'Unit Price' 15 'C500'.
  ADD_FIELDCAT 'KBETR1' 'Amount' 15 'C500'.

  GW_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM       = SY-REPID
      IS_LAYOUT                = GW_LAYOUT
      IT_FIELDCAT              = GT_FIELDCAT
      I_DEFAULT                = 'X'
      I_SAVE                   = 'A'
      I_CALLBACK_PF_STATUS_SET = 'ALV_STATUS_SET'
      I_CALLBACK_USER_COMMAND  = 'ALV_USER_COMMAND'
    TABLES
      T_OUTTAB                 = IT_OUTPUT1
    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. " DISPLAY_ALV_OUTPUT1


*&---------------------------------------------------------------------*
*&      Form  alv_status_set
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PT_EXTAB   text
*----------------------------------------------------------------------*
FORM ALV_STATUS_SET USING PT_EXTAB TYPE SLIS_T_EXTAB.

  SET PF-STATUS 'ZSTATUS' EXCLUDING PT_EXTAB.

  CHECK GRID IS INITIAL.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      E_GRID = GRID.
ENDFORM. "alv_status_set


*&---------------------------------------------------------------------*
*&      Form  alv_user_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PA_UCOMM     text
*      -->PS_SELFIELD  text
*----------------------------------------------------------------------*
FORM ALV_USER_COMMAND USING PA_UCOMM TYPE SY-UCOMM
                                PS_SELFIELD TYPE SLIS_SELFIELD.

  CASE PA_UCOMM.
    WHEN '&UPDATE'.
      PERFORM UPDATE_TO_TABLE.
    WHEN '&COLLECT'.
      PERFORM COLLECT_PART_PRICE.
  ENDCASE.

  PS_SELFIELD-REFRESH = 'X'.

ENDFORM. "alv_user_command
*&---------------------------------------------------------------------*
*&      Form  UPDATE_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM UPDATE_TO_TABLE .
  DATA: WA_ZCOT017 TYPE ZCOT017.
  DATA: IT_ZCOT017 LIKE TABLE OF WA_ZCOT017 WITH HEADER LINE.

*原材价格信息更新到自定义表在zcot017
  LOOP AT IT_OUTPUT1 INTO WA_OUTPUT1.
    IT_ZCOT017-WERKS = P_WERKS.
    IT_ZCOT017-MATNR = WA_OUTPUT1-MATNR.
    IT_ZCOT017-URZZT = WA_OUTPUT1-PRTYPE.
    IT_ZCOT017-BLDAT = SY-DATUM.
    IT_ZCOT017-ZBUSGP = 'LBG'.
    IT_ZCOT017-BUALT = WA_OUTPUT1-KBETR.
    COLLECT IT_ZCOT017.
  ENDLOOP.

  IF IT_ZCOT017[] IS NOT INITIAL.
    MODIFY ZCOT017 FROM TABLE IT_ZCOT017.
    IF SY-SUBRC EQ 0.
      MESSAGE 'Update To Table Success' TYPE 'S'.
    ENDIF.
  ELSE.
    MESSAGE 'No data To Update' TYPE 'E' DISPLAY LIKE 'E'.
  ENDIF.
ENDFORM. " UPDATE_TABLE
*&---------------------------------------------------------------------*
*&      Form  COLLECT_PART_PRICE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM COLLECT_PART_PRICE.
CLEAR IT_HEAD[].
  SELECT * INTO CORRESPONDING FIELDS OF TABLE
    IT_HEAD FROM ZSDQ7.

  SORT IT_HEAD[] BY ZNUMBER.
  IF it_head[] IS INITIAL.
    MESSAGE 'Please upload excel header fist !' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

  PERFORM BUILD_DYNAMIC_FIELD.
  PERFORM CREATE_ITAB_DYNAMICALLY.
  PERFORM GET_DATA.
  PERFORM DISPLAY_ALV_OUTPUT2.
ENDFORM. " COLLECT_PART_PRICE


*&---------------------------------------------------------------------*
*&      Form  display_alv_output3
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM DISPLAY_ALV_OUTPUT2.
  DATA: IS_LAYOUT_LVC TYPE LVC_S_LAYO.
  IS_LAYOUT_LVC-CWIDTH_OPT = 'X'.

  DATA: L_CHAR LIKE ZSDQ7-MATKL,
        L_TABIX LIKE SY-TABIX.

  LOOP AT IT_HEAD.
    L_CHAR = IT_HEAD-MATKL.
    TRANSLATE L_CHAR TO UPPER CASE.
    READ TABLE IT_FIELDCAT_LVC INTO WA_FIELDCAT_LVC WITH KEY SCRTEXT_M = L_CHAR.
    L_TABIX = SY-TABIX.
    IF SY-SUBRC EQ 0.
      WA_FIELDCAT_LVC-SCRTEXT_M = IT_HEAD-MATKL.
      MODIFY IT_FIELDCAT_LVC FROM WA_FIELDCAT_LVC INDEX L_TABIX.
    ENDIF.
  ENDLOOP.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IT_FIELDCAT_LVC    = IT_FIELDCAT_LVC
      IS_LAYOUT_LVC      = IS_LAYOUT_LVC
      I_DEFAULT          = 'X'
      I_SAVE             = 'A'
    TABLES
      T_OUTTAB           = <LINE>
    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. "display_alv_output3

*&---------------------------------------------------------------------*
*&      Form  build_dynamic_field
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM BUILD_DYNAMIC_FIELD.
  CLEAR: WA_FIELDCAT_LVC, IT_FIELDCAT_LVC[].

  DATA: N(3) TYPE N VALUE 1,
        L_MATKL LIKE ZSDQ1-MATKL,
        L_FIELDNAME(20),
        POS(5) TYPE C VALUE '1'.

  DEFINE GET_FIELDCAT_LVC.
    WA_FIELDCAT_LVC-COL_POS   = POS.
*动态扩充BUALT栏位
    IF &1 = 'BUALT'.
      CONCATENATE 'BUALT' N INTO L_FIELDNAME.
      WA_FIELDCAT_LVC-FIELDNAME = L_FIELDNAME.
      N = N + 1.
    ELSE.
      WA_FIELDCAT_LVC-FIELDNAME = &1.
    ENDIF.
    WA_FIELDCAT_LVC-REF_TABLE   = &2.
    WA_FIELDCAT_LVC-REF_FIELD   = &3.
    WA_FIELDCAT_LVC-SCRTEXT_M   = IT_HEAD-MATKL.
    WA_FIELDCAT_LVC-COLDDICTXT   = 'M'.
    WA_FIELDCAT_LVC-NO_OUT = ''.
    APPEND WA_FIELDCAT_LVC TO IT_FIELDCAT_LVC.
    POS = POS + 1.
  END-OF-DEFINITION.

*根据上传表头构建动态内表Fieldcat
  LOOP AT IT_HEAD.
    TRANSLATE IT_HEAD-MATKL TO UPPER CASE.
    CASE IT_HEAD-MATKL.
      WHEN 'MODEL'.
        GET_FIELDCAT_LVC 'NORMT' 'ZSDQ1002E_S' 'NORMT'.
      WHEN 'QUOTATION DATE'.
        GET_FIELDCAT_LVC 'BLDAT' 'ZSDQ1002E_S' 'BLDAT'.
      WHEN 'LENOVO PN'.
        GET_FIELDCAT_LVC 'KDMAT' 'ZSDQ1002E_S' 'KDMAT'.
      WHEN 'LCFC PN'.
        GET_FIELDCAT_LVC 'MATNR' 'ZSDQ1002E_S' 'MATNR'.
      WHEN 'DESC.'.
        GET_FIELDCAT_LVC 'MAKTX' 'ZSDQ1002E_S' 'MAKTX'.
      WHEN 'TOTAL ($)'.
        GET_FIELDCAT_LVC 'ZTOTAL' 'ZSDQ1002E_S' 'ZTOTAL'.
      WHEN 'EXCHANGE RATE'.
        GET_FIELDCAT_LVC 'KURSF' 'ZSDQ1002E_S' 'KURSF'.
      WHEN 'WEEE'.
        GET_FIELDCAT_LVC 'ZWEEE' 'ZSDQ1002E_S' 'ZWEEE'.
      WHEN 'TOTAL'.
        GET_FIELDCAT_LVC 'ZTOTAL1' 'ZSDQ1002E_S' 'ZTOTAL1'.
      WHEN 'PACKING TOTAL'.
        GET_FIELDCAT_LVC 'ZPACKING1' 'ZSDQ1002E_S' 'ZPACKING1'.
      WHEN OTHERS.
        GET_FIELDCAT_LVC 'BUALT' 'ZSDQ1002E_S' 'ZCPU'.
    ENDCASE.
  ENDLOOP.
ENDFORM. "build_dynamic_field
*&---------------------------------------------------------------------*
*&      Form  CREATE_ITAB_DYNAMICALLY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CREATE_ITAB_DYNAMICALLY .
*根据构建的Fieldcat创建动态内表
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = IT_FIELDCAT_LVC
    IMPORTING
      EP_TABLE        = DYN_TABLE.

  ASSIGN DYN_TABLE->* TO <LINE>.
  CREATE DATA DYN_LINE LIKE LINE OF <LINE> .
  ASSIGN DYN_LINE->* TO <WA> .
ENDFORM. " CREATE_ITAB_DYNAMICALLY
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA .
  CLEAR: IT_OUTPUT2[], IT_OUTPUT3[]..
  IT_OUTPUT2[] = IT_OUTPUT1[].
  SORT IT_OUTPUT2 BY WERKS PROJECT MATNR MATKL.
  DELETE ADJACENT DUPLICATES FROM IT_OUTPUT2
              COMPARING WERKS PROJECT MATNR MATKL.

  DATA: L_PRICE LIKE ZSDQ1-KBETR,
        L_MATNR LIKE MARA-MATNR,
        L_PACKING LIKE ZSDQ1-KBETR,
        L_TOTAL_PRICE LIKE ZSDQ1-KBETR,
        L_TOTAL_PRICE1 LIKE ZSDQ1-KBETR.

  DATA: L_KNUMH LIKE A921-KNUMH,
        L_KBETR LIKE KONP-KBETR,
        L_KPEIN LIKE KONP-KPEIN,
        L_ZWEEE TYPE BUALT,
        L_KDMAT LIKE KNMT-KDMAT.

*首先判断Field是否存在,再将栏位地址赋给&3,将&4的值赋给&3
  DEFINE ASSIGN_FIELD.
    READ TABLE IT_FIELDCAT_LVC INTO WA_FIELDCAT_LVC WITH KEY FIELDNAME = &1.
    IF SY-SUBRC = 0.
      ASSIGN COMPONENT WA_FIELDCAT_LVC-FIELDNAME OF STRUCTURE &2 TO &3.
      &3 = &4.
    ENDIF.
  END-OF-DEFINITION.

  DEFINE ASSIN_FILED_MATKL.
    READ TABLE IT_FIELDCAT_LVC INTO WA_FIELDCAT_LVC WITH KEY SCRTEXT_M = &1.
    IF SY-SUBRC = 0.
      ASSIGN COMPONENT WA_FIELDCAT_LVC-FIELDNAME OF STRUCTURE &2 TO &3.
      &3 = &4.
    ENDIF.
  END-OF-DEFINITION.

  DEFINE GET_FILED_MATKL.
    READ TABLE IT_FIELDCAT_LVC INTO WA_FIELDCAT_LVC WITH KEY SCRTEXT_M = &1.
    IF SY-SUBRC = 0.
      ASSIGN COMPONENT WA_FIELDCAT_LVC-FIELDNAME OF STRUCTURE &2 TO &3.
    ENDIF.
  END-OF-DEFINITION.

  IF P_KURSF IS NOT INITIAL.
    SELECT SINGLE KNUMH INTO L_KNUMH
      FROM A921
      WHERE KAPPL = 'V' AND KSCHL = 'ZTP2'
        AND VKORG = 'CN07' AND VTWEG = '01'
        AND SPART = '01' AND KUNNR = 'L1LNVCN1'
        AND DATBI > P_DATBI AND DATAB < P_DATBI.
    IF SY-SUBRC EQ 0.
      SELECT SINGLE KBETR KPEIN INTO (L_KBETR, L_KPEIN)
        FROM KONP
        WHERE KAPPL = 'V' AND KSCHL = 'ZTP2'
        AND KNUMH = L_KNUMH.
      IF L_KPEIN NE 0.
        L_ZWEEE = L_KBETR / L_KPEIN.
      ENDIF.
    ENDIF.
  ELSE.
    L_ZWEEE = 0.
  ENDIF.

  LOOP AT IT_MATNR.
    CLEAR : <WA>, L_TOTAL_PRICE, L_TOTAL_PRICE1.

    LOOP AT IT_OUTPUT2 WHERE MATNR = IT_MATNR-MATNR.
      ASSIGN_FIELD 'NORMT' <WA> <FS_FIELD> IT_OUTPUT2-PROJECT.
      ASSIGN_FIELD 'BLDAT' <WA> <FS_FIELD> SY-DATUM.
      ASSIGN_FIELD 'MATNR' <WA> <FS_FIELD> IT_OUTPUT2-MATNR.
      ASSIGN_FIELD 'MAKTX' <WA> <FS_FIELD> IT_OUTPUT2-MAKTX.

      CLEAR L_PRICE.
      LOOP AT IT_OUTPUT1 INTO WA_OUTPUT1
           WHERE WERKS = IT_OUTPUT2-WERKS
             AND PROJECT = IT_OUTPUT2-PROJECT
             AND MATNR = IT_OUTPUT2-MATNR
             AND MATKL = IT_OUTPUT2-MATKL.
        L_PRICE = L_PRICE + WA_OUTPUT1-KBETR1.
      ENDLOOP.

      TRANSLATE IT_OUTPUT2-MATKL TO UPPER CASE.

      ASSIN_FILED_MATKL IT_OUTPUT2-MATKL <WA> <FS_FIELD> L_PRICE.
      IF SY-SUBRC EQ 0.
        L_TOTAL_PRICE = L_TOTAL_PRICE + L_PRICE.
      ENDIF.
    ENDLOOP.

    SELECT SINGLE KDMAT INTO L_KDMAT
      FROM KNMT
      WHERE MATNR = IT_MATNR-MATNR.

    ASSIGN_FIELD 'KDMAT' <WA> <FS_FIELD> L_KDMAT.
    ASSIGN_FIELD 'ZWEEE' <WA> <FS_FIELD> L_ZWEEE.
    ASSIGN_FIELD 'KURSF' <WA> <FS_FIELD> P_KURSF.
    ASSIGN_FIELD 'ZTOTAL' <WA> <FS_FIELD> L_TOTAL_PRICE.
    GET_FILED_MATKL 'MANUAL' <WA> <FS_FIELD1>.
    GET_FILED_MATKL 'PACKING' <WA> <FS_FIELD2>.
    GET_FILED_MATKL 'CD' <WA> <FS_FIELD3>.

    L_PACKING = <FS_FIELD1> + <FS_FIELD2> + <FS_FIELD3>.
    ASSIGN_FIELD 'ZPACKING1' <WA> <FS_FIELD> L_PACKING.

    IF P_KURSF IS INITIAL.
      ASSIGN_FIELD 'ZTOTAL1' <WA> <FS_FIELD> L_TOTAL_PRICE.
    ELSE.
      L_TOTAL_PRICE1 =
      L_TOTAL_PRICE * P_KURSF + L_ZWEEE.
      ASSIGN_FIELD 'ZTOTAL1' <WA> <FS_FIELD> L_TOTAL_PRICE1.
    ENDIF.
    APPEND <WA> TO <LINE>.
  ENDLOOP.

ENDFORM. " GET_DATA


*Selection texts
*----------------------------------------------------------
* P_DATBI         Price Valid on
* P_DATUV         BOM Valid on
* P_FILE         File Path
* P_KURSF         Exchange Rate
* P_WERKS         Plant
* S_MATNR         Material


*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
*   Input Material

总结一下: 1.第一步根据动态栏位需求创建所需要的Fieldcat     2.根据Fieldcat创建动态内表,并实例化  3.根据Filecat-name取栏位指定到指针变量,以取值或者修改值

你可能感兴趣的:(ABAP动态内表ALV)