SAP ABAP 動態內表實現 ALV橫向按月份動態顯示資料

智造星發表於2020-10-28

*&---------------------------------------------------------------------*
*& Report  ZMM_PRODUCT_ORDER_CHECK
*&
*&---------------------------------------------------------------------*
*& add by leon 20200525 MA 歷史修改記錄查詢
*&
*&
*&---------------------------------------------------------------------*
 

*&---------------------------------------------------------------------*
*& Report  ZMM_MA_HISTORY
*&
*&---------------------------------------------------------------------*
*& add by leon 20200525 MA 歷史修改記錄查詢
*&
*&
*&---------------------------------------------------------------------*
REPORT ZMM_MA_HISTORY MESSAGE-ID ZMM .
TYPE-POOLS:SLIS, ABAP .
**********************************************************************
TABLES : MARA,MARC,CDHDR,CDPOS,ZMATAPPEND.
DATA: GR_TABLE     TYPE REF TO CL_SALV_TABLE.

*變數定義
TYPES : BEGIN OF TYP_ITAB ,
          MATNR    TYPE MATNR,
          WERKS    TYPE WERKS_D,
          DISMM	   TYPE DISMM,
          MMSTA    TYPE MMSTA,
          MATGR	   TYPE MATNRGROUP,
          VERSION  TYPE CHAR3,
          MA       TYPE MATNRGROUP,
          SPMON    TYPE SPMON,
          UDATE    TYPE  CDDATUM,
          ZEIAR    TYPE DZEIAR,
          MAKTX    TYPE MAKTX,
          EKGRP    TYPE EKGRP,
          ZZSCMPN  TYPE ZZSCMPN,
          USERNAME TYPE  CDUSERNAME,
*          UTIME    type  CDUZEIT,
*          TCODE    type  CDTCODE,
        END OF TYP_ITAB .

TYPES TY_ITAB TYPE TYP_ITAB  OCCURS 0 .
DATA:LT_TAB TYPE TY_ITAB .
DATA:LT_TAB1 TYPE TY_ITAB .
DATA WA_TAB TYPE LINE OF TY_ITAB .
DATA : I_MONTHS   TYPE INT4,
       D_DATE_BEG TYPE DATUM,
       D_DATE_END TYPE DATUM,
       V_SPMON    TYPE SPMON,
       V_DATE1    TYPE DATUM,
       V_DATE2    TYPE DATUM.

DATA L_OBJECTID LIKE CDPOS-OBJECTID .
DATA L_TABLEKEY LIKE  CDPOS-TABKEY .
DATA V_TABLEKEY LIKE  CDPOS-TABKEY .
DATA LT_EDITPOS	LIKE TABLE OF	CDRED WITH HEADER LINE   .
DATA WA_EDITPOS LIKE CDRED .

TYPES : BEGIN OF TYP_MON ,
          SPMON TYPE SPMON,
        END OF TYP_MON .
DATA : WA_MON TYPE TYP_MON,
       LT_MON TYPE TYP_MON OCCURS 0.
DATA V_INDEX TYPE SY-INDEX .
DATA COLUMNNAME TYPE LVC_FNAME .
DATA : SHORT_TEXT TYPE SCRTEXT_S,
       MED_TEXT   TYPE SCRTEXT_M,
       LONG_TEXT  TYPE SCRTEXT_L.
**********************************************************************
DATA LR_STRUC TYPE REF TO CL_ABAP_STRUCTDESCR.
DATA LR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR.

DATA LR_TYPE TYPE REF TO CL_ABAP_TYPEDESCR.
DATA LR_DATA  TYPE REF TO CL_ABAP_DATADESCR.

DATA LT_COMP TYPE  ABAP_COMPONENT_TAB.
DATA LS_COMP LIKE LINE OF  LT_COMP.

DATA DYN_WA TYPE REF TO DATA.
DATA DYN_TABLE TYPE REF TO DATA.

DATA L_STRING TYPE STRING.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>    TYPE ANY,
               <DYN_LINE>  TYPE ANY,
               <DYN_FIELD> ,
               <WA_FIELD1> ,
               <WA_FIELD2> ,
               <WA_FIELD3> .
DATA :CFIELD(30) .

DATA: DREF_STR    TYPE REF TO DATA,
      DREF_TAB    TYPE REF TO DATA,
      DREF_I      TYPE REF TO DATA,
      ITAB_TYPE   TYPE REF TO CL_ABAP_TABLEDESCR,
      STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR,
      ELEM_TYPE   TYPE REF TO CL_ABAP_ELEMDESCR,
      TABLE_TYPE  TYPE REF TO CL_ABAP_TABLEDESCR,
      COMP_TAB    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE WITH HEADER LINE,
      COMP_TAB1   TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE WITH HEADER LINE.
FIELD-SYMBOLS :<FS_ITAB> TYPE ANY TABLE.

*---------------------------------------------------------------------*
*       CLASS lcl_handle_events DEFINITION
*----------------------------------------------------------------------*
*       事件處理類(定義)
*----------------------------------------------------------------------*
CLASS LCL_HANDLE_EVENTS DEFINITION.
  PUBLIC SECTION.
*    CLASS-METHODS :
    METHODS:
      ON_USER_COMMAND FOR EVENT ADDED_FUNCTION OF CL_SALV_EVENTS
        IMPORTING E_SALV_FUNCTION,

      ON_DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_SALV_EVENTS_TABLE
        IMPORTING ROW COLUMN,

      ON_LINK_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE
        IMPORTING ROW COLUMN.
ENDCLASS  .              "lcl_handle_eventsDEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
*       ALV操作類(定義)
*----------------------------------------------------------------------*
CLASS LCL_ALV DEFINITION.
  PUBLIC SECTION .
    METHODS: GETDATA         "取得要顯示的資料
      RETURNING VALUE(LT_TAB) TYPE TY_ITAB,
      ALV_FULL        "全屏Grid列表處理方法
        IMPORTING VALUE(LT_TAB) TYPE TY_ITAB,
      MAIN .           "主方法
ENDCLASS .                    "lcl_alv DEFINITION

**********************************************************************
DATA: GR_EVENTS TYPE REF TO LCL_HANDLE_EVENTS.

SELECTION-SCREEN : BEGIN OF BLOCK BLK-01 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS : S_WERKS FOR MARC-WERKS  ,
                 S_MATNR FOR MARC-MATNR ,
                 S_ZEIAR FOR MARA-ZEIAR ,
                 S_EKGRP FOR MARC-EKGRP ,
                 S_SPMON FOR SY-DATUM+0(6),
                 S_DATE FOR SY-DATUM NO-DISPLAY
                 .
SELECTION-SCREEN : END OF BLOCK BLK-01 .
SELECTION-SCREEN : BEGIN OF BLOCK BLK-02 WITH FRAME TITLE TEXT-002.
PARAMETERS : P_VERTI TYPE C RADIOBUTTON GROUP RG1 USER-COMMAND UC1 DEFAULT 'X',
             P_HORIZ TYPE C RADIOBUTTON GROUP RG1
             .

SELECTION-SCREEN : END OF BLOCK BLK-02 .

INITIALIZATION .
  S_WERKS-LOW = '2000'.
  S_WERKS-HIGH = '2200'.
  S_WERKS-SIGN = 'I' .
  S_WERKS-OPTION = 'BT' .
  APPEND S_WERKS .
*  S_EKGRP-LOW = '201'.
*  S_EKGRP-SIGN = 'I'.
*  S_EKGRP-OPTION = 'EQ' .
*  append S_EKGRP .
  IF SY-DATUM+4(2) EQ '12'.
    S_SPMON-LOW+0(4) = SY-DATUM+0(4) .
    S_SPMON-LOW+4(2) = '01' .
  ELSE .
    S_SPMON-LOW+0(4) = SY-DATUM+0(4) - 1  .
    S_SPMON-LOW+4(2) = SY-DATUM+4(2) + 1  .
  ENDIF .
  S_SPMON-HIGH+0(6) = SY-DATUM+0(6) .
  S_SPMON-SIGN = 'I' .
  S_SPMON-OPTION = 'BT' .
  APPEND S_SPMON .

*----------------------------------------------------------------------*
*       CLASS lcl_handle_events IMPLEMENTATION
*----------------------------------------------------------------------*
*       事件處理類(實現)
*----------------------------------------------------------------------*
CLASS LCL_HANDLE_EVENTS IMPLEMENTATION.
*  method ON_USER_COMMAND .
*    data L_STRING type STRING.
*    concatenate 'Function is ' E_SALV_FUNCTION
*           into L_STRING
*           separated by SPACE .
*    message I000(0K) with L_STRING .
*  endmethod  .                   "on_user_command
  METHOD ON_USER_COMMAND .
    PERFORM HANDLE_USER_COMMAND USING E_SALV_FUNCTION .
  ENDMETHOD .                    "on_user_command
  METHOD ON_DOUBLE_CLICK .
    PERFORM SHOW_DOCUMENT USING ROW COLUMN .
  ENDMETHOD .

  METHOD ON_LINK_CLICK .
*perform SHOW_DOCUMENT using ROW COLUMN .
  ENDMETHOD .                      "on_user_command
ENDCLASS  .             "lcl_handle_eventsIMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
*       ALV操作類(實現)
*----------------------------------------------------------------------*
CLASS LCL_ALV IMPLEMENTATION.
*取得要顯示的資料
  METHOD GETDATA .
    DATA WA_DATE LIKE LINE OF S_DATE .
    WA_DATE-LOW+0(6) = S_SPMON-LOW+0(6).
    WA_DATE-LOW+6(2) = '01'.
    CALL FUNCTION 'DATE_GET_MONTH_LASTDAY'
      EXPORTING
        I_DATE = WA_DATE-LOW
      IMPORTING
        E_DATE = WA_DATE-HIGH.
    WA_DATE-SIGN = 'I'.
    WA_DATE-OPTION = 'BT' .
    APPEND WA_DATE TO S_DATE .
    CLEAR : LT_TAB1 . REFRESH LT_TAB1.
**********************************************************************
*& 取物料主資料
    SELECT DISTINCT MARA~MATNR
                    MARC~WERKS
                    MARC~DISMM
                    MARC~MMSTA
                    MARC~MATGR
                    MARA~ZEIAR
                    MARC~EKGRP
      FROM MARA
      INNER JOIN MARC ON MARC~MATNR = MARA~MATNR
      INTO CORRESPONDING FIELDS OF TABLE LT_TAB1
     WHERE MARA~MATNR IN S_MATNR
       AND MARC~WERKS IN S_WERKS
       AND MARC~EKGRP IN S_EKGRP
       AND MARA~ZEIAR IN S_ZEIAR
       AND MARA~MATNR IN ( SELECT DISTINCT MATNR
                             FROM ZMATAPPEND
                            WHERE MATNR = MARA~MATNR
                          )
    .
    "" 取月份
    CLEAR :D_DATE_BEG,D_DATE_END .
    D_DATE_BEG+0(6) = S_SPMON-LOW+0(6) .
    D_DATE_BEG+6(2) = '01' .
    D_DATE_END+0(6) = S_SPMON-HIGH+0(6) .
    D_DATE_END+6(2) = '01' .
    CLEAR I_MONTHS .
    CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
      EXPORTING
        BEGDA    = D_DATE_BEG
        ENDDA    = D_DATE_END
*       TAB_MODE = ' '
      IMPORTING
*       DAYS     =
*       C_WEEKS  =
        C_MONTHS = I_MONTHS
*       C_YEARS  =
*       WEEKS    =
*       MONTHS   =
*       YEARS    =
*       D_MONTHS =
*       MONTH_TAB       =
      .
    I_MONTHS = I_MONTHS + 1 .
    CLEAR :LT_TAB . REFRESH :LT_TAB .
    CLEAR: WA_MON,LT_MON,LT_MON .
    LOOP AT LT_TAB1 INTO WA_TAB .
      SELECT SINGLE MAKTX
              FROM MAKT
              INTO WA_TAB-MAKTX
             WHERE MATNR = WA_TAB-MATNR
               AND SPRAS = SY-LANGU
            .

      CLEAR : L_OBJECTID,LT_EDITPOS,L_TABLEKEY .
      REFRESH :LT_EDITPOS .
      WRITE WA_TAB-MATNR TO L_OBJECTID .
      CONCATENATE SY-MANDT
            WA_TAB-MATNR
       INTO L_TABLEKEY.
      WRITE WA_TAB-WERKS TO L_TABLEKEY+21(4).
*      concatenate SY-MANDT WA_TAB-MATNR WA_TAB-WERKS into L_TABLEKEY .
      CALL FUNCTION 'CHANGEDOCUMENT_READ'
        EXPORTING
*         ARCHIVE_HANDLE             = 0
*         CHANGENUMBER               = ' '
*         DATE_OF_CHANGE             = '00000000'
          OBJECTCLASS                = 'MATERIAL'
          OBJECTID                   = L_OBJECTID
          TABLEKEY                   = L_TABLEKEY
          TABLENAME                  = 'MARC'
*         TIME_OF_CHANGE             = '000000'
*         USERNAME                   = ' '
*         LOCAL_TIME                 = ' '
*         TIME_ZONE                  = 'UTC'
*         TABLEKEY254                = ' '
*         KEYGUID                    = ' '
*         DATE_UNTIL                 = '99991231'
*         TIME_UNTIL                 = '235959'
*         KEYGUID_STR                = ' '
*         READ_CHANGEDOCU            = ' '
*         I_PREP_UNIT                = 'X'
*         NOPLUS_ASWILDCARD_INOBJID  = ' '
*     IMPORTING
*         ET_CDRED_STR               =
        TABLES
          EDITPOS                    = LT_EDITPOS[]
        EXCEPTIONS
          NO_POSITION_FOUND          = 1
          WRONG_ACCESS_TO_ARCHIVE    = 2
          TIME_ZONE_CONVERSION_ERROR = 3
          OTHERS                     = 4.
      IF SY-SUBRC <> 0.
* Implement suitable error handling here
      ENDIF.
      DELETE LT_EDITPOS WHERE FNAME NE 'MATGR' .
      IF LT_EDITPOS[] IS INITIAL .
        APPEND WA_TAB TO LT_TAB .
      ELSE .
        CLEAR V_SPMON .
        V_SPMON = D_DATE_BEG+0(6) .
        CLEAR : WA_TAB-MA,WA_TAB-VERSION,
          WA_TAB-UDATE,
          WA_TAB-USERNAME  .
*          WA_TAB-UTIME ,
*          WA_TAB-TCODE .
        DO I_MONTHS TIMES.
          IF V_SPMON+4(2) GT 12 .
            V_SPMON+4(2) = '01' .
            V_SPMON+0(4) = V_SPMON+0(4) + 1 .
          ENDIF .
          WA_TAB-SPMON = V_SPMON .

          WA_MON-SPMON = V_SPMON .
          APPEND WA_MON TO LT_MON .

          CLEAR : V_DATE1 ,V_DATE2 .
          V_DATE1+0(6) = V_SPMON .
          V_DATE1+6(2) = '01' .
          CALL FUNCTION 'DATE_GET_MONTH_LASTDAY'
            EXPORTING
              I_DATE = V_DATE1
            IMPORTING
              E_DATE = V_DATE2.
          CLEAR WA_EDITPOS .
          LOOP AT LT_EDITPOS INTO WA_EDITPOS WHERE UDATE BETWEEN V_DATE1 AND V_DATE2 .
            WA_TAB-MA = WA_EDITPOS-F_NEW .
            WA_TAB-VERSION = WA_TAB-VERSION + 1 .
            WA_TAB-UDATE = WA_EDITPOS-UDATE .
            WA_TAB-USERNAME = WA_EDITPOS-USERNAME .
*            WA_TAB-UTIME = WA_EDITPOS-UTIME .
*            WA_TAB-TCODE = WA_EDITPOS-TCODE .
            APPEND WA_TAB TO LT_TAB .
          ENDLOOP .
          IF WA_EDITPOS IS INITIAL .
            CLEAR WA_TAB-MA .
            LOOP AT LT_EDITPOS INTO WA_EDITPOS WHERE UDATE LT V_DATE1 .  "把最後一次修改的記錄讀出來並顯示在
              WA_TAB-MA = WA_EDITPOS-F_NEW .
              WA_TAB-VERSION = WA_TAB-VERSION + 1 .
              WA_TAB-UDATE = WA_EDITPOS-UDATE .
              WA_TAB-USERNAME = WA_EDITPOS-USERNAME .
*            WA_TAB-UTIME = WA_EDITPOS-UTIME .
*            WA_TAB-TCODE = WA_EDITPOS-TCODE .
            ENDLOOP .
            IF NOT WA_TAB-MA IS INITIAL. " 當前期間內沒有修改記錄 則把上個月的記錄複製到當前月份
              APPEND WA_TAB TO LT_TAB .
            ENDIF .
          ENDIF .
          V_SPMON = V_SPMON + 1 .
        ENDDO .
      ENDIF .
      CLEAR WA_TAB .
    ENDLOOP.
    SORT LT_TAB BY
          MATNR
          WERKS
          SPMON DESCENDING
          UDATE
          MATGR
          MA
          VERSION DESCENDING .
    DELETE ADJACENT DUPLICATES FROM LT_TAB COMPARING MATNR WERKS SPMON UDATE MATGR MA .
    SORT LT_MON BY SPMON DESCENDING .
    DELETE ADJACENT DUPLICATES FROM LT_MON COMPARING SPMON .

    IF P_HORIZ = 'X'.
      PERFORM GEN_DYN_TABLE TABLES LT_TAB .
    ENDIF .

  ENDMETHOD .                   "getdata

*輸出全屏網格列表的方法
  METHOD ALV_FULL.
    DATA LR_DISPLAY TYPE REF TO CL_SALV_DISPLAY_SETTINGS .
    DATA:LR_FUNCTIONS TYPE REF TO CL_SALV_FUNCTIONS.
*         LR_EVENTS    type ref to CL_SALV_EVENTS.
    DATA: LV_REPID TYPE SYREPID.
    DATA: LR_LAYOUT TYPE REF TO CL_SALV_LAYOUT,
          LS_KEY    TYPE SALV_S_LAYOUT_KEY.
    DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.

    DATA: LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS,
          LR_COLUMN  TYPE REF TO CL_SALV_COLUMN_TABLE.
    DATA: LR_SELECTIONS TYPE REF TO CL_SALV_SELECTIONS.

    "建立例項
    IF P_HORIZ = 'X' .
      TRY .
          CL_SALV_TABLE=>FACTORY(
            EXPORTING
              LIST_DISPLAY = ABAP_FALSE
            IMPORTING
              R_SALV_TABLE = GR_TABLE
            CHANGING
              T_TABLE      = <DYN_TABLE>
          ) .
        CATCH CX_SALV_MSG .
      ENDTRY .
    ELSEIF P_VERTI = 'X'.
      TRY .
          CL_SALV_TABLE=>FACTORY(
            EXPORTING
              LIST_DISPLAY = ABAP_FALSE
            IMPORTING
              R_SALV_TABLE = GR_TABLE
            CHANGING
              T_TABLE      = LT_TAB
          ) .
        CATCH CX_SALV_MSG .
      ENDTRY .
    ENDIF .
    "設定ALV功能
    GR_TABLE->SET_SCREEN_STATUS(
    PFSTATUS      =  'SALV_STANDARD'
    REPORT        =  SY-REPID
    SET_FUNCTIONS = GR_TABLE->C_FUNCTIONS_ALL
    ).
    LR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
    LR_COLUMNS->SET_OPTIMIZE( 'X' ).

    PERFORM SET_COLUMNS_TECHNICAL USING LR_COLUMNS.

*    try.
*        LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ).
*        LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ).
*
*      catch CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
*    endtry.
*
*    try.
*        LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'MATGR' ).
*        LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ).
*
*      catch CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
*    endtry.

    LR_SELECTIONS = GR_TABLE->GET_SELECTIONS( ).
    LR_SELECTIONS->SET_SELECTION_MODE( IF_SALV_C_SELECTION_MODE=>MULTIPLE ).
    LR_LAYOUT = GR_TABLE->GET_LAYOUT( ).
    LS_KEY-REPORT = SY-REPID.
    LR_LAYOUT->SET_KEY( LS_KEY ).
    LR_LAYOUT->SET_DEFAULT( 'X' ).
    LR_EVENTS = GR_TABLE->GET_EVENT( ).

    CREATE OBJECT GR_EVENTS. "動態方法
    SET HANDLER GR_EVENTS->ON_USER_COMMAND FOR LR_EVENTS .
    SET HANDLER GR_EVENTS->ON_LINK_CLICK FOR LR_EVENTS .
    SET HANDLER GR_EVENTS->ON_DOUBLE_CLICK FOR LR_EVENTS .

    "  ALV的顯示樣式
    LR_DISPLAY = GR_TABLE->GET_DISPLAY_SETTINGS( ) .
    LR_DISPLAY->SET_STRIPED_PATTERN( 'X' ) . "設定顏色間隔顯示
    IF SY-LANGU = 'E'.
      LR_DISPLAY->SET_LIST_HEADER( 'Products MA Change Report' ) . "設定ALV的抬頭
    ELSE.
      LR_DISPLAY->SET_LIST_HEADER( 'WAC MA變化報表' ) . "設定ALV的抬頭
    ENDIF.

    LR_DISPLAY->SET_HORIZONTAL_LINES( 'X' ) .  "設定水平線是否顯示
    LR_DISPLAY->SET_VERTICAL_LINES( 'X' ) .    "設定垂直線是否顯示
*    "註冊事件  " 靜態方法
*    set handler LCL_HANDLE_EVENTS=>ON_USER_COMMAND for LR_EVENTS.
    "顯示列表
    GR_TABLE->DISPLAY( ) .

  ENDMETHOD.                   "alv_full

*主方法,整合資料
  METHOD MAIN .
    "取得要顯示的資料
    LT_TAB = ME->GETDATA( ).
    ME->ALV_FULL( LT_TAB ).
  ENDMETHOD.                    "main

ENDCLASS .                    "lcl_alvIMPLEMENTATION

*報表執行
START-OF-SELECTION .
  DATA  LR_ALV   TYPE REF TO LCL_ALV .
  CREATE OBJECT LR_ALV .
  LR_ALV->MAIN( ) .

FORM SET_COLUMNS_TECHNICAL USING IR_COLUMNS TYPE REF TO CL_SALV_COLUMNS.
  DATA: LR_COLUMN TYPE REF TO CL_SALV_COLUMN.
  DATA : LR_COLUMNS TYPE SALV_T_COLUMN_REF .
  DATA WA_COLUMN TYPE SALV_S_COLUMN_REF .
*  try .
*      LR_COLUMNS = IR_COLUMNS->GET( ) .
*    catch CX_SALV_NOT_FOUND.                            "#EC NO_HANDLER
*  endtry.
  LOOP AT LT_MON INTO WA_MON .
    CLEAR COLUMNNAME .
    CONCATENATE 'MONTH_' WA_MON-SPMON INTO COLUMNNAME .
    WRITE : WA_MON-SPMON TO SHORT_TEXT ,
            WA_MON-SPMON TO MED_TEXT,
            WA_MON-SPMON TO LONG_TEXT .
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( COLUMNNAME ).
        LR_COLUMN->SET_SHORT_TEXT( SHORT_TEXT ).
        LR_COLUMN->SET_MEDIUM_TEXT( MED_TEXT ).
        LR_COLUMN->SET_LONG_TEXT( LONG_TEXT ).
      CATCH CX_SALV_NOT_FOUND .                         "#EC NO_HANDLER
    ENDTRY .
  ENDLOOP .


  IF SY-LANGU EQ 'E'.

    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'SPMON' ).
        LR_COLUMN->SET_SHORT_TEXT( 'Period' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'Period' ).
        LR_COLUMN->SET_LONG_TEXT( 'Period' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'UDATE' ).
        LR_COLUMN->SET_SHORT_TEXT( 'UpdateDate' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'Update Date' ).
        LR_COLUMN->SET_LONG_TEXT( 'Update Date' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'VERSION' ).
        LR_COLUMN->SET_SHORT_TEXT( 'Revision' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'Revision Number' ).
        LR_COLUMN->SET_LONG_TEXT( 'Revision Number' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.

    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'ZEIAR' ).
        LR_COLUMN->SET_SHORT_TEXT( 'Business' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'Business Unit' ).
        LR_COLUMN->SET_LONG_TEXT( 'Business Unit' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MA' ).
        LR_COLUMN->SET_SHORT_TEXT( 'Revised MA' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'Revised MA' ).
        LR_COLUMN->SET_LONG_TEXT( 'Revised MA' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MATGR' ).
        LR_COLUMN->SET_SHORT_TEXT( 'MA' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'MA' ).
        LR_COLUMN->SET_LONG_TEXT( 'MA' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.

  ELSE.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'USERNAME' ).
        LR_COLUMN->SET_SHORT_TEXT( '修改賬號' ).
        LR_COLUMN->SET_MEDIUM_TEXT( '修改賬號' ).
        LR_COLUMN->SET_LONG_TEXT( '修改賬號' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.

    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'SPMON' ).
        LR_COLUMN->SET_SHORT_TEXT( '月份' ).
        LR_COLUMN->SET_MEDIUM_TEXT( '月份' ).
        LR_COLUMN->SET_LONG_TEXT( '月份' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'UDATE' ).
        LR_COLUMN->SET_SHORT_TEXT( '更新日期' ).
        LR_COLUMN->SET_MEDIUM_TEXT( '更新日期' ).
        LR_COLUMN->SET_LONG_TEXT( '更新日期' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'VERSION' ).
        LR_COLUMN->SET_SHORT_TEXT( '修改次數' ).
        LR_COLUMN->SET_MEDIUM_TEXT( '修改次數' ).
        LR_COLUMN->SET_LONG_TEXT( '修改次數' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MA' ).
        LR_COLUMN->SET_SHORT_TEXT( '修改後MA' ).
        LR_COLUMN->SET_MEDIUM_TEXT( '修改後MA' ).
        LR_COLUMN->SET_LONG_TEXT( '修改後MA' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'ZEIAR' ).
        LR_COLUMN->SET_SHORT_TEXT( '產品家族' ).
        LR_COLUMN->SET_MEDIUM_TEXT( '產品家族' ).
        LR_COLUMN->SET_LONG_TEXT( '產品家族' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
    TRY.
        LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MATGR' ).
        LR_COLUMN->SET_SHORT_TEXT( 'MA' ).
        LR_COLUMN->SET_MEDIUM_TEXT( 'MA' ).
        LR_COLUMN->SET_LONG_TEXT( 'MA' ).
      CATCH CX_SALV_NOT_FOUND.                          "#EC NO_HANDLER
    ENDTRY.
  ENDIF.

ENDFORM.

FORM GEN_DYN_TABLE TABLES GT_TAB LIKE LT_TAB.
  TYPES : BEGIN OF TYP_TAB_PART1 ,
            MATNR TYPE MATNR,
            WERKS TYPE WERKS_D,
            DISMM	TYPE DISMM,
            MMSTA TYPE MMSTA,
            MATGR	TYPE MATNRGROUP,

          END OF TYP_TAB_PART1 .
  TYPES :  BEGIN OF TYP_TAB_PART3 ,
             ZEIAR    TYPE DZEIAR,
             MAKTX    TYPE MAKTX,
             EKGRP    TYPE EKGRP,
             USERNAME TYPE  CDUSERNAME,
           END OF TYP_TAB_PART3 .
  DATA LT_TAB_PART1 TYPE SORTED TABLE OF TYP_TAB_PART1 WITH UNIQUE KEY MATNR WERKS .
  DATA LT_TAB_PART3 TYPE TYP_TAB_PART3 OCCURS 0 .
  DATA :V_MATNR TYPE MATNR,
        V_WERKS TYPE WERKS_D.
  "排序表動態排序
  DATA:LT_SORTED_TABLE TYPE ABAP_SORTORDER_TAB.
  LT_SORTED_TABLE = VALUE #( ( NAME = 'MATNR' ASTEXT = ABAP_FALSE DESCENDING = ABAP_FALSE )
                             ( NAME = 'WERKS') ).
**********************************************************************
*& 動態建立內表 內表由三部分組成  第1和第3部份 為上面的結構  中間的第二部分由分份動態生成
  "**========給現有的內表動態的加一列
  FREE :TABLE_TYPE, STRUCT_TYPE,COMP_TAB[] .
  TABLE_TYPE ?= CL_ABAP_TABLEDESCR=>DESCRIBE_BY_DATA( LT_TAB_PART1 ).
  STRUCT_TYPE ?= TABLE_TYPE->GET_TABLE_LINE_TYPE( ).
  COMP_TAB[] = STRUCT_TYPE->GET_COMPONENTS( ).
  CLEAR V_INDEX.
  DESCRIBE TABLE COMP_TAB LINES V_INDEX .
  LOOP AT LT_MON INTO WA_MON .
*    COMP_TAB-NAME = WA_MON-SPMON .
    CONCATENATE 'MONTH_' WA_MON-SPMON  INTO COMP_TAB-NAME .
    COMP_TAB-TYPE = CL_ABAP_ELEMDESCR=>GET_C( 20 ) .
    V_INDEX = V_INDEX + 1 .
    INSERT COMP_TAB INTO COMP_TAB INDEX V_INDEX .
  ENDLOOP .

  FREE :TABLE_TYPE, STRUCT_TYPE,COMP_TAB1[] .
  TABLE_TYPE ?= CL_ABAP_TABLEDESCR=>DESCRIBE_BY_DATA( LT_TAB_PART3 ) .
  STRUCT_TYPE ?= TABLE_TYPE->GET_TABLE_LINE_TYPE( ) .
  COMP_TAB1[] = STRUCT_TYPE->GET_COMPONENTS( ) .
  V_INDEX = V_INDEX + 1 .
  INSERT LINES OF COMP_TAB1 INTO COMP_TAB  INDEX V_INDEX  .

  STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( COMP_TAB[] ) .
  ITAB_TYPE   = CL_ABAP_TABLEDESCR=>CREATE( STRUCT_TYPE ) .
  CREATE DATA DREF_TAB TYPE HANDLE ITAB_TYPE  .
  ASSIGN DREF_TAB->* TO <DYN_TABLE>  ."將欄位符號指向新建立出來的內表物件
  CREATE DATA DREF_STR TYPE HANDLE STRUCT_TYPE  .
  ASSIGN DREF_STR->* TO <DYN_WA> .

  IF NOT GT_TAB[] IS INITIAL .
    LOOP AT GT_TAB INTO WA_TAB .
      MOVE-CORRESPONDING WA_TAB TO <DYN_WA> .
      APPEND <DYN_WA> TO <DYN_TABLE> .
    ENDLOOP .
    SORT <DYN_TABLE> BY (LT_SORTED_TABLE) .
    DELETE ADJACENT DUPLICATES FROM <DYN_TABLE> COMPARING ALL FIELDS .
    LOOP AT <DYN_TABLE> INTO <DYN_WA> .
      V_INDEX = SY-TABIX.
      ASSIGN COMPONENT 'MATNR' OF STRUCTURE <DYN_WA> TO <WA_FIELD1> .
      ASSIGN COMPONENT 'WERKS' OF STRUCTURE <DYN_WA> TO <WA_FIELD2> .
      ASSIGN COMPONENT 'MATGR' OF STRUCTURE <DYN_WA> TO <WA_FIELD3> .
      IF V_MATNR EQ <WA_FIELD1> AND V_WERKS EQ <WA_FIELD2>.  "如果同一物料同一工廠出現多次 刪除重複行
        DELETE <DYN_TABLE> INDEX V_INDEX  .
        CONTINUE .
      ENDIF.
      V_MATNR = <WA_FIELD1> .
      V_WERKS = <WA_FIELD2> .
      LOOP AT GT_TAB INTO WA_TAB WHERE MATNR EQ  <WA_FIELD1>
                                   AND WERKS  EQ  <WA_FIELD2>
                                   AND MATGR  EQ  <WA_FIELD3>
                                   AND SPMON  NE  '000000'.
        CLEAR CFIELD .
        CONCATENATE 'MONTH_' WA_TAB-SPMON INTO CFIELD .
        ASSIGN COMPONENT CFIELD OF STRUCTURE <DYN_WA> TO <DYN_FIELD> .
        <DYN_FIELD> = WA_TAB-MA+0(20) .
      ENDLOOP .
      MODIFY <DYN_TABLE> FROM <DYN_WA> INDEX V_INDEX.
    ENDLOOP .
  ENDIF .
**********************************************************************

ENDFORM .

FORM HANDLE_USER_COMMAND USING I_UCOMM TYPE SALV_DE_FUNCTION.
  DATA: LR_SELECTIONS TYPE REF TO CL_SALV_SELECTIONS,
        LT_ROWS       TYPE SALV_T_ROW,
        LV_ROWS       TYPE LINE OF SALV_T_ROW,
        LV_LINES      TYPE INT4.

  CASE I_UCOMM .
    WHEN 'DISP_INV'.
      LR_SELECTIONS = GR_TABLE->GET_SELECTIONS( ) .
      LT_ROWS = LR_SELECTIONS->GET_SELECTED_ROWS( ) .
      LV_LINES = LINES( LT_ROWS ) .
      IF LV_LINES = 0 .
        MESSAGE 'Please select at least one record '(M01) TYPE 'I'.
      ELSEIF LV_LINES > 1 .
        MESSAGE 'Please select only one record'(M02) TYPE 'I'.
      ELSE .
*        read table  LT_ROWS into LV_ROWS index 1..
*        read table GT_OUT_INVOICE_DETAIL into GS_OUT_INVOICE_DETAIL index LV_ROWS.
*        if SY-SUBRC = 0 and GS_OUT_INVOICE_DETAIL-VBELN is not initial.
*          set parameter id 'VF' field GS_OUT_INVOICE_DETAIL-VBELN.
*          call transaction 'VF03' and skip first screen.
*        endif.
      ENDIF .
    WHEN 'REFRESH'.

    WHEN OTHERS .
  ENDCASE .

ENDFORM.                    " handle_user_command

FORM SHOW_DOCUMENT USING PV_ROW    TYPE I
                      PV_COLUMN TYPE LVC_FNAME  .
  IF P_VERTI = 'X'.



    READ TABLE LT_TAB INTO WA_TAB INDEX PV_ROW  .


    IF SY-SUBRC = 0 AND WA_TAB-MATNR IS NOT INITIAL  .
      CASE PV_COLUMN  .
        WHEN 'MATGR' .
          SET PARAMETER ID 'MAT' FIELD WA_TAB-MATNR .
          CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN .
        WHEN 'MATNR' .
          SET PARAMETER ID 'MAT' FIELD WA_TAB-MATNR .
          SET PARAMETER ID 'BERID' FIELD WA_TAB-WERKS .
          SET PARAMETER ID 'WRK' FIELD WA_TAB-WERKS .
          CALL TRANSACTION 'MD04' AND SKIP FIRST SCREEN.
      ENDCASE .
    ENDIF .

  ELSEIF P_HORIZ = 'X' .



    READ TABLE <DYN_TABLE> INTO <DYN_WA> INDEX PV_ROW  .


*    CLEAR: <WA_FIELD1> , <WA_FIELD2> ,<WA_FIELD3> .
*
*      ASSIGN COMPONENT 'MATNR' OF STRUCTURE <DYN_WA> TO <WA_FIELD1> .
*      ASSIGN COMPONENT 'WERKS' OF STRUCTURE <DYN_WA> TO <WA_FIELD2> .
*      ASSIGN COMPONENT 'MATGR' OF STRUCTURE <DYN_WA> TO <WA_FIELD3> .

    IF SY-SUBRC = 0 AND <WA_FIELD1> IS NOT INITIAL  .
      CASE PV_COLUMN  .
        WHEN 'MATGR' .
          SET PARAMETER ID 'MAT' FIELD <WA_FIELD1> .
          CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN .
        WHEN 'MATNR' .
          SET PARAMETER ID 'MAT' FIELD <WA_FIELD1> .
          SET PARAMETER ID 'BERID' FIELD <WA_FIELD2> .
          SET PARAMETER ID 'WRK' FIELD <WA_FIELD2> .
          CALL TRANSACTION 'MD04' AND SKIP FIRST SCREEN.
      ENDCASE .
    ENDIF .


  ENDIF.


ENDFORM.

 

相關文章