본문 바로가기

SAP BC-프로그램

Tcode 사용 기록 검색 프로그램

모듈 : BC
기능 설명 : SAP User 별, Tcode 별 사용 기록 검색
사용 예시 : 기존 ST03N 에서 최대 한 달 별로 조회할 수 있는 Tcode 사용 이력을 한번에 조회하도록 개발

***********************************************************************
* Report            : ZBC_TCODE_HISTORY                               *
* Module/Sub-Module : BC                                              *
* Description       : User Tcode History View                         *
***********************************************************************
*                          MODIFICATION LOG                           *
*                                                                     *
* DATE       AUTHORS            DESCRIPTION                           *
* ---------- ------------------ ------------------------------------- *
*             YHJ                     Initial Release                 *
***********************************************************************

REPORT  ZBC_TCODE_HISTORY.

*----------------------------------------------------------------------*
* TYPE-POOLS
*----------------------------------------------------------------------*
TYPE-POOLS: SLIS, ICON.


*----------------------------------------------------------------------*
* TABLES
*----------------------------------------------------------------------*


*----------------------------------------------------------------------*
* DATA
*----------------------------------------------------------------------*

DATA: GT_DATA  LIKE TABLE OF SAPWLUSTCX WITH HEADER LINE,
      LT_DATA  LIKE TABLE OF SAPWLUSTCX WITH HEADER LINE,
      TEMP_TAB LIKE TABLE OF SAPWLUSTCX WITH HEADER LINE.

DATA: LV_EXCEP(1)   TYPE C,
      LV_COUNT(10)  TYPE N.

DATA: BEGIN OF LT_DATE_LIST OCCURS 0,
        SQE_DATE   LIKE SY-DATUM,
      END OF LT_DATE_LIST.

DATA: BEGIN OF TT_DATA OCCURS 0,
        SAPID      LIKE SY-UNAME,
        TCODE(40)  TYPE C,
        TSTEP(10)  TYPE I,
      END OF TT_DATA.

DATA: BEGIN OF ZT_DATA OCCURS 0,
        SAPID      LIKE SY-UNAME,
        TCODE(40)  TYPE C,
        TSTEP(10)  TYPE I,
      END OF ZT_DATA.

CONSTANTS : C1(1) value '.'.


*----------------------------------------------------------------------*
* SELECTION-SCREEN
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.

  SELECT-OPTIONS: R_USER FOR SY-UNAME.
*  SELECT-OPTIONS: R_DATE FOR SY-DATUM.

  PARAMETERS: P_SDATE LIKE SY-DATUM.
  PARAMETERS: P_EDATE LIKE SY-DATUM.

  SELECTION-SCREEN SKIP 1.

  PARAMETERS: P_ZYCBO AS CHECKBOX.
  PARAMETERS: P_ONLYT AS CHECKBOX.

  SELECTION-SCREEN SKIP 1.

  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT (60) TEXT-002.
  SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT (60) TEXT-003.
  SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN OUTPUT.
  %_R_USER_%_APP_%-TEXT   = 'SAP ID'.
  %_P_SDATE_%_APP_%-TEXT  = 'START DATE'.
  %_P_EDATE_%_APP_%-TEXT  = 'END DATE'.
  %_P_ZYCBO_%_APP_%-TEXT  = 'CBO TCode Only (Z*/Y*)'.
  %_P_ONLYT_%_APP_%-TEXT  = 'TCode Only'.

*----------------------------------------------------------------------*
* INITIALIZATION                                                       *
*----------------------------------------------------------------------*
INITIALIZATION.


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

  PERFORM DATE_CHECK.

  LOOP AT LT_DATE_LIST.

    REFRESH: GT_DATA, TEMP_TAB. CLEAR: GT_DATA, TEMP_TAB.

    CALL FUNCTION 'SAPWL_WORKLOAD_GET_STATISTIC'
      EXPORTING
        PERIODTYPE = 'M'
        STARTDATE = LT_DATE_LIST-SQE_DATE
        ONLY_APPLICATION_STATISTIC = 'X'
        INSTANCE = 'TOTAL'
      TABLES
        APPLICATION_STATISTIC = GT_DATA
      EXCEPTIONS
        UNKNOWN_PERIODTYPE = 1
        NO_DATA_FOUND = 2
        NO_SERVER_GIVEN = 3
        OTHERS = 4.

    IF SY-SUBRC = 0.

      LOOP AT R_USER.

        TEMP_TAB[] = GT_DATA[].

        DELETE TEMP_TAB WHERE ACCOUNT NS R_USER-LOW.
        APPEND LINES OF TEMP_TAB TO LT_DATA.

      ENDLOOP.
    ENDIF.

  ENDLOOP.



  IF LT_DATA[] IS NOT INITIAL.

    SORT LT_DATA[] BY ACCOUNT ENTRY_ID.
*    DELETE ADJACENT DUPLICATES FROM LT_DATA[] COMPARING ACCOUNT ENTRY_ID.

    LOOP AT LT_DATA.

      IF P_ONLYT = 'X'.
        TT_DATA-TCODE = LT_DATA-ENTRY_ID+0(40).
        TT_DATA-TSTEP = LT_DATA-COUNT.
        COLLECT TT_DATA.
      ELSE.
        TT_DATA-SAPID = LT_DATA-ACCOUNT.
        TT_DATA-TCODE = LT_DATA-ENTRY_ID+0(40).
        TT_DATA-TSTEP = LT_DATA-COUNT.
        COLLECT TT_DATA.
      ENDIF.

    ENDLOOP.

    IF P_ZYCBO = 'X'.
      LOOP AT TT_DATA.
        IF TT_DATA-TCODE CP 'Z*' OR TT_DATA-TCODE CP 'Y*'.
          APPEND TT_DATA TO ZT_DATA.
        ENDIF.
      ENDLOOP.
    ENDIF.

    IF P_ONLYT = 'X'. SORT TT_DATA[] BY TCODE. ENDIF.

    IF P_ZYCBO = 'X'. PERFORM RESULT_VIEW TABLES ZT_DATA.
    ELSE. PERFORM RESULT_VIEW TABLES TT_DATA. ENDIF.

  ELSE.
    MESSAGE 'No Data.' TYPE 'S'.
    REJECT.
  ENDIF.


*----------------------------------------------------------------------*
* END-OF-SELECTION.
*----------------------------------------------------------------------*
END-OF-SELECTION.


*&---------------------------------------------------------------------*
*& ZBC_HIST_DEL_F01
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      Form  DATE_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DATE_CHECK .

  DATA: TEMP_NUM(2) TYPE n.
  DATA: TEMP_DATE LIKE SY-DATUM.

  IF R_USER IS INITIAL.
    MESSAGE 'SAP ID is a required value.' TYPE 'S'.
    REJECT.
  ENDIF.

  IF P_SDATE IS INITIAL.
    MESSAGE '
Start date is a required value.' TYPE 'S'.
    REJECT.
  ENDIF.

  TEMP_NUM = P_SDATE+6(2) - 1.
  P_SDATE = P_SDATE - TEMP_NUM.

  IF P_EDATE IS NOT INITIAL.
    TEMP_NUM = P_EDATE+6(2) - 1.
    P_EDATE = P_EDATE - TEMP_NUM.
  ENDIF.

  IF P_SDATE > P_EDATE AND P_EDATE IS NOT INITIAL.
    MESSAGE 'The start date must be less than the end date.' TYPE 'S'.
    REJECT.
  ENDIF.

  IF P_EDATE IS INITIAL OR P_EDATE = P_SDATE.

    LT_DATE_LIST-SQE_DATE = P_SDATE. APPEND LT_DATE_LIST.

  ELSE.

    LT_DATE_LIST-SQE_DATE = P_SDATE. APPEND LT_DATE_LIST.
    TEMP_DATE = P_SDATE.

    DO.

      CALL FUNCTION 'BKK_ADD_MONTH_TO_DATE'
        EXPORTING
          MONTHS = 1
          OLDDATE = TEMP_DATE
        IMPORTING
          NEWDATE = TEMP_DATE.

      LT_DATE_LIST-SQE_DATE = TEMP_DATE. APPEND LT_DATE_LIST.

      IF TEMP_DATE = P_EDATE.
        EXIT.
      ENDIF.

    ENDDO.

  ENDIF.

ENDFORM.                    " DATE_CHECK

*&--------------------------------------------------------------------*
*&      Form  RESULT_VIEW
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*---------------------------------------------------------------------*
FORM RESULT_VIEW TABLES FT_DATA.

*DATA: LT_FIELDCAT TYPE  SLIS_T_FIELDCAT_ALV.
*
*CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
*     EXPORTING
*          I_PROGRAM_NAME         = SY-REPID
*          I_INTERNAL_TABNAME     = 'GT_LOG'
*          I_INCLNAME             = SY-REPID
*     CHANGING
*          CT_FIELDCAT            = LT_FIELDCAT
*     EXCEPTIONS
*          INCONSISTENT_INTERFACE = 1
*          PROGRAM_ERROR          = 2
*          OTHERS                 = 3.


  DATA: LT_FIELDCAT type SLIS_T_FIELDCAT_ALV with header line.

*  LT_FIELDCAT-FIELDNAME   = 'ICON'.
*  LT_FIELDCAT-SELTEXT_M   = 'RC'.
*  LT_FIELDCAT-KEY         = 'X'.
*  LT_FIELDCAT-JUST        = 'C'.
*  LT_FIELDCAT-OUTPUTLEN   = 3.
*  LT_FIELDCAT-COL_POS     = 0.
*  append LT_FIELDCAT to LT_FIELDCAT.
*  clear  LT_FIELDCAT.

  IF P_ONLYT = ''.
    LT_FIELDCAT-FIELDNAME   = 'SAPID'.
    LT_FIELDCAT-SELTEXT_M   = 'USER'.
    LT_FIELDCAT-KEY         = 'X'.
    LT_FIELDCAT-OUTPUTLEN   = 12.
    LT_FIELDCAT-COL_POS     = 1.
    APPEND LT_FIELDCAT TO LT_FIELDCAT.
    CLEAR  LT_FIELDCAT.
  ENDIF.

  LT_FIELDCAT-FIELDNAME   = 'TCODE'.
  LT_FIELDCAT-SELTEXT_M   = 'TCODE'.
  LT_FIELDCAT-OUTPUTLEN   = 40.
  LT_FIELDCAT-COL_POS     = 2.
  APPEND LT_FIELDCAT TO LT_FIELDCAT.
  CLEAR  LT_FIELDCAT.

  LT_FIELDCAT-FIELDNAME   = 'TSTEP'.
  LT_FIELDCAT-SELTEXT_M   = '# STEP'.
  LT_FIELDCAT-OUTPUTLEN   = 10.
  LT_FIELDCAT-COL_POS     = 3.
  APPEND LT_FIELDCAT TO LT_FIELDCAT.
  CLEAR  LT_FIELDCAT.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM      = SY-REPID
      I_CALLBACK_TOP_OF_PAGE  = 'TOP-OF-PAGE'
      IT_FIELDCAT             = LT_FIELDCAT[]
    TABLES
      T_OUTTAB = FT_DATA.

ENDFORM.                    " RESULT_VIEW

*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE1                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM TOP-OF-PAGE.

*ALV Header declarations
  DATA: T_HEADER  TYPE SLIS_T_LISTHEADER,
        WA_HEADER TYPE SLIS_LISTHEADER,
        T_LINE    LIKE WA_HEADER-INFO.

  DATA: TEXT_SDATE(20)   TYPE C,
        TEXT_EDATE(20)   TYPE C.

* Title
  WA_HEADER-TYP  = 'H'.
  WA_HEADER-INFO = 'User Used Tcode Histoy'.
  APPEND WA_HEADER TO T_HEADER.
  CLEAR WA_HEADER.


*## Diplay period

  CONCATENATE P_SDATE+0(4) P_SDATE+4(2) P_SDATE+6(2) INTO TEXT_SDATE SEPARATED BY C1.
  IF P_EDATE IS NOT INITIAL.
    CONCATENATE P_EDATE+0(4) P_EDATE+4(2) P_EDATE+6(2) INTO TEXT_EDATE SEPARATED BY C1. ENDIF.

  IF P_EDATE IS INITIAL.
    CONCATENATE 'Period: ' TEXT_SDATE INTO T_LINE SEPARATED BY SPACE.
  ELSE.
    CONCATENATE 'Period: ' TEXT_SDATE '~' TEXT_EDATE INTO T_LINE SEPARATED BY SPACE.
  ENDIF.

  WA_HEADER-TYP  = 'S'.
  WA_HEADER-INFO = T_LINE.
  APPEND WA_HEADER TO T_HEADER.
  CLEAR: WA_HEADER, T_LINE.

*## Total No. of Records Selected

  LV_COUNT = LINES( TT_DATA ).
  IF P_ZYCBO = 'X'. LV_COUNT = LINES( ZT_DATA ). ENDIF.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
    EXPORTING
      INPUT  = LV_COUNT
    IMPORTING
      OUTPUT = LV_COUNT.

  CONCATENATE 'Used TCode Count: ' LV_COUNT INTO T_LINE SEPARATED BY SPACE.

  WA_HEADER-TYP  = 'S'.
  WA_HEADER-INFO = T_LINE.
  APPEND WA_HEADER TO T_HEADER.
  CLEAR: WA_HEADER, T_LINE.


  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY = T_HEADER.

ENDFORM.                    "TOP-OF-PAGE.