본문 바로가기

SAP ABAP

역할 일괄 복사 프로그램

모듈 : BC
기능 설명 : 역할 일괄 복사 프로그램
사용 예시 : 신규 회사 오픈 등으로 기존에 있는 롤을 이름만 바꿔서 새로 생성해야 할 경우 사용


1. 소스코드

***********************************************************************
* Report            : ZBC_MASS_ROLE_COPY                              *
* Module/Sub-Module : BC                                              *
* Description       : Mass Role Copy                                  *
***********************************************************************
*                          MODIFICATION LOG                           *
*                                                                     *
* DATE       AUTHORS            DESCRIPTION                           *
* ---------- ------------------ ------------------------------------- *
*             YHJ               Initial Release                       *
***********************************************************************

REPORT ZBC_MASS_ROLE_COPY.


*----------------------------------------------------------------------*
* TYPE-POOLS
*----------------------------------------------------------------------*
TYPE-POOLS: SLIS.


*----------------------------------------------------------------------*
* TABLES
*----------------------------------------------------------------------*
TABLES: AGR_TEXTS, AGR_1252.


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

DATA: BEGIN OF GT_DATA OCCURS 0,
        SOURCE_ROLE   TYPE AGR_NAME,
        NEW_ROLE      TYPE AGR_NAME,
        CHG_DESC      TYPE AGR_TITLE,
      END OF GT_DATA.

DATA: BEGIN OF GT_DATA_ALV OCCURS 0,
        SIGN(4)       TYPE C,
        SOURCE_ROLE   TYPE AGR_NAME,
        NEW_ROLE      TYPE AGR_NAME,
        MSG           TYPE STRING,
        NO            TYPE N,
      END OF GT_DATA_ALV.

DATA: GT_AGR_DEF  LIKE TABLE OF AGR_DEFINE WITH HEADER LINE.

DATA: GT_LINE(4)  TYPE N.

DATA: LT_MESSAGES TYPE SPROT_U_TAB.

DATA: LD_SUBRC    TYPE SYSUBRC,
      LD_NR_MSGS  TYPE SYTABIX.

DATA: CHECK_FLAG  TYPE C.

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

  SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE t002.
    PARAMETERS: P_FILE LIKE RLGRAP-FILENAME OBLIGATORY.
  SELECTION-SCREEN END OF BLOCK b2.

  SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE t003.

    PARAMETERS: R_DESC AS CHECKBOX DEFAULT 'X'.

    PARAMETERS: R_CPUSR AS CHECKBOX.

  SELECTION-SCREEN END OF BLOCK b3.



  SELECTION-SCREEN BEGIN OF BLOCK b4 WITH FRAME TITLE t011.

    SELECTION-SCREEN BEGIN OF LINE.
      PARAMETERS: R_BUKTXT AS CHECKBOX.
      SELECTION-SCREEN COMMENT (20) t012 FOR FIELD R_BUKTXT.
      PARAMETERS: R_BUKCOD(4) TYPE C.
      SELECTION-SCREEN COMMENT (2) t004 FOR FIELD R_BUKCOD.
    SELECTION-SCREEN END OF LINE.

    SELECTION-SCREEN BEGIN OF LINE.
      PARAMETERS: R_EKOTXT AS CHECKBOX.
      SELECTION-SCREEN COMMENT (20) t013 FOR FIELD R_EKOTXT.
      PARAMETERS: R_EKOCOD(4) TYPE C.
      SELECTION-SCREEN COMMENT (2) t004 FOR FIELD R_EKOCOD.
    SELECTION-SCREEN END OF LINE.

    SELECTION-SCREEN BEGIN OF LINE.
      PARAMETERS: R_VKOTXT AS CHECKBOX.
      SELECTION-SCREEN COMMENT (20) t014 FOR FIELD R_VKOTXT.
      PARAMETERS: R_VKOCOD(4) TYPE C.
      SELECTION-SCREEN COMMENT (2) t004 FOR FIELD R_VKOCOD.
    SELECTION-SCREEN END OF LINE.

    SELECTION-SCREEN BEGIN OF LINE.
      PARAMETERS: R_WERTXT AS CHECKBOX.
      SELECTION-SCREEN COMMENT (20) t015 FOR FIELD R_WERTXT.

      PARAMETERS: R_WERKS1(4) TYPE C.
      PARAMETERS: R_WERKS2(4) TYPE C.
      PARAMETERS: R_WERKS3(4) TYPE C.
      SELECTION-SCREEN COMMENT (2) t004 FOR FIELD R_WERKS1.

    SELECTION-SCREEN END OF LINE.


    SELECTION-SCREEN BEGIN OF LINE.
      SELECTION-SCREEN COMMENT (70) t005.
    SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN END OF BLOCK b4.


SELECTION-SCREEN END OF BLOCK b1.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  PERFORM FIND_FOLDER USING P_FILE.

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

  MOVE 'Mass Role Copy' TO t001.
  MOVE 'Excel File' TO t002.
  MOVE 'Change Option' TO t003.
  MOVE ' ' TO t004.
  MOVE 'Mass Role Copy' TO t005.
  
  MOVE 'Organizational Levels Change' TO t011.
  MOVE ' Company Code Change ($BUKRS)' TO t012.
  MOVE ' Purchasing org. Change ($EKORG)' TO t013.
  MOVE ' Sales org. Change ($VKORG)' TO t014.
  MOVE ' Plant Change ($WERKS)' TO t015.



*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*-----------------------------
AT SELECTION-SCREEN OUTPUT.
  PERFORM MODIFY_SCREEN.


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

  PERFORM UPLOAD_XLS.
  PERFORM CHECK_FILE.
  PERFORM CHECK_PARAM.

  LOOP AT GT_DATA.

    CLEAR: CHECK_FLAG.

    MOVE-CORRESPONDING GT_DATA TO GT_DATA_ALV.

    PERFORM ROLE_COPY.
    IF R_DESC = 'X'   AND CHECK_FLAG = 'S'. PERFORM ROLE_CHG_DESC.  ENDIF.
    IF R_BUKTXT = 'X' AND CHECK_FLAG = 'S'. PERFORM ROLE_CHG_BUKRS. ENDIF.
    IF R_EKOTXT = 'X' AND CHECK_FLAG = 'S'. PERFORM ROLE_CHG_EKORG. ENDIF.
    IF R_VKOTXT = 'X' AND CHECK_FLAG = 'S'. PERFORM ROLE_CHG_VKORG. ENDIF.
    IF R_WERTXT = 'X' AND CHECK_FLAG = 'S'. PERFORM ROLE_CHG_WERKS. ENDIF.

    APPEND GT_DATA_ALV.

  ENDLOOP.

  PERFORM RESULT_ALV.


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



*&--------------------------------------------------------------------*
*& ZBC_MASS_ROLE_COPY_F01
*&--------------------------------------------------------------------*

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

  DATA: XLS_DATA1  LIKE TABLE OF ALSMEX_TABLINE WITH HEADER LINE,
        FNAME      TYPE STRING.

  DATA: BEGIN OF LT_INDEX OCCURS 0 ,
          FNAME    TYPE CHAR20  ,
          POS      TYPE NUM4    ,
        END OF LT_INDEX.

  FIELD-SYMBOLS: <LFS>.


  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        FILENAME                = P_FILE
        I_BEGIN_COL             = 1
        I_BEGIN_ROW             = 1
        I_END_COL               = 100
        I_END_ROW               = 9999
      TABLES
        INTERN                  = XLS_DATA1
      EXCEPTIONS
        INCONSISTENT_PARAMETERS = 1
        UPLOAD_OLE              = 2
        OTHERS                  = 3.

*** COLUMNS
  LOOP AT XLS_DATA1 WHERE ROW = 1.
    LT_INDEX-FNAME = XLS_DATA1-VALUE.
    LT_INDEX-POS   = XLS_DATA1-COL.
    APPEND LT_INDEX.
  ENDLOOP.

  DELETE  XLS_DATA1 WHERE ROW = 1.

  LOOP AT XLS_DATA1.
    CLEAR LT_INDEX.
    READ TABLE LT_INDEX WITH KEY POS = XLS_DATA1-COL.

    CONCATENATE 'GT_DATA-' LT_INDEX-FNAME  INTO FNAME.
    ASSIGN  (FNAME) TO <LFS>.
    <LFS>  = XLS_DATA1-VALUE.

    AT END OF ROW.
      APPEND GT_DATA.
      CLEAR: GT_DATA.
    ENDAT.
  ENDLOOP.



ENDFORM.

*&--------------------------------------------------------------------*
*&      Form  FIND_FOLDER
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*  -->  p1        text*  <--  p2        text
*---------------------------------------------------------------------*
FORM FIND_FOLDER USING PV_FILE.

  CLEAR PV_FILE.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      PROGRAM_NAME  = ''
      DYNPRO_NUMBER = ''
      FIELD_NAME    = 'FILENAME'
    IMPORTING
      FILE_NAME     = PV_FILE.

ENDFORM.                    " FIND_FOLDER

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

  DATA: LT_MSG        TYPE STRING.

  FIELD-SYMBOLS: <msg> TYPE SPROT_U.

* Copy procedure

  IF R_CPUSR = 'X'.
    CALL FUNCTION 'PRGN_COPY_AGR'
      EXPORTING
        source_agr                     = GT_DATA-SOURCE_ROLE
        target_agr                     = GT_DATA-NEW_ROLE
        hr_assignments                 = 'X'
        user_assinement                = 'X'
        pers_objects                   = 'X'
        display_messages               = SPACE
      IMPORTING
        messages                       = LT_MESSAGES
      EXCEPTIONS
        flag_not_existing              = 1
        action_cancelled               = 2
        enqueue_failure                = 3
        no_recording                   = 4
        no_authority_for_tcodes_insert = 5
        no_authority_for_object_insert = 6
        no_authority_for_srole_insert  = 7
        no_authority_for_user_insert   = 8
        hr_incomplete                  = 9
        dist_incomplete                = 10
        OTHERS                         = 11.

  ELSE.
    CALL FUNCTION 'PRGN_COPY_AGR'
      EXPORTING
        source_agr                     = GT_DATA-SOURCE_ROLE
        target_agr                     = GT_DATA-NEW_ROLE
        hr_assignments                 = 'X'
        user_assinement                = ' '
        pers_objects                   = 'X'
        display_messages               = SPACE
      IMPORTING
        messages                       = LT_MESSAGES
      EXCEPTIONS
        flag_not_existing              = 1
        action_cancelled               = 2
        enqueue_failure                = 3
        no_recording                   = 4
        no_authority_for_tcodes_insert = 5
        no_authority_for_object_insert = 6
        no_authority_for_srole_insert  = 7
        no_authority_for_user_insert   = 8
        hr_incomplete                  = 9
        dist_incomplete                = 10
        OTHERS                         = 11.
  ENDIF.

  LD_SUBRC = SY-SUBRC.

  CASE LD_SUBRC.
    WHEN 0.
*      DESCRIBE TABLE LT_MESSAGES LINES LD_NR_MSGS.
*      IF ld_nr_msgs GT 1.
**       Role copied with additional information or warnings
*        PERFORM display_messages IN PROGRAM saplprgn_auth
*                                 USING lt_messages space.
*      ENDIF.
*      MESSAGE s407(s#).

      CHECK_FLAG = 'S'.
      GT_DATA_ALV-SIGN = '@5B@'.
      GT_DATA_ALV-NO  = 4.

      IF R_CPUSR = 'X'.
        GT_DATA_ALV-MSG = 'Role Copied Success (With User)'.
      ELSE.
        GT_DATA_ALV-MSG = 'Role Copied Success (Without User)'.
      ENDIF.

      COMMIT WORK AND WAIT.

      DESCRIBE TABLE LT_MESSAGES LINES LD_NR_MSGS.
      IF ld_nr_msgs GT 1.

        LOOP AT LT_MESSAGES ASSIGNING <msg>.
          CALL FUNCTION 'MESSAGE_TEXT_BUILD'
            EXPORTING
              MSGID = <msg>-AG
              MSGNR = <msg>-MSGNR
              MSGV1 = <msg>-VAR1
              MSGV2 = <msg>-VAR2
              MSGV3 = <msg>-VAR3
              MSGV4 = <msg>-VAR4
            IMPORTING
              MESSAGE_TEXT_OUTPUT = LT_MSG.

          CHECK_FLAG = 'F'.
          GT_DATA_ALV-SIGN = '@5D@'.
          GT_DATA_ALV-NO  = 3.
          GT_DATA_ALV-MSG = LT_MSG.
        ENDLOOP.

      ENDIF.

    WHEN 1.
*     No copy: Flag COLL_AGR in table AGR_FLAGS does not exist.
*      MESSAGE s431(s#) WITH GT_DATA-SOURCE_ROLE DISPLAY LIKE 'E'.

      CONCATENATE 'Type of role' GT_DATA-SOURCE_ROLE 'is undetermined' INTO LT_MSG SEPARATED BY SPACE.
      GT_DATA_ALV-SIGN = '@5C@'.
      GT_DATA_ALV-NO  = 1.
      GT_DATA_ALV-MSG = LT_MSG.

    WHEN OTHERS.
*      IF NOT lt_messages IS INITIAL.
*        PERFORM display_messages(saplprgn_auth) USING lt_messages space.
*      ELSE.
*        MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno DISPLAY LIKE 'E'
*                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*      ENDIF.

      IF lt_messages IS NOT INITIAL.

        LOOP AT LT_MESSAGES ASSIGNING <msg>.
          CALL FUNCTION 'MESSAGE_TEXT_BUILD'
            EXPORTING
              MSGID = <msg>-AG
              MSGNR = <msg>-MSGNR
              MSGV1 = <msg>-VAR1
              MSGV2 = <msg>-VAR2
              MSGV3 = <msg>-VAR3
              MSGV4 = <msg>-VAR4
            IMPORTING
              MESSAGE_TEXT_OUTPUT = LT_MSG.

          CHECK_FLAG = 'F'.
          GT_DATA_ALV-SIGN = '@5C@'.
          GT_DATA_ALV-NO  = 1.
          GT_DATA_ALV-MSG = LT_MSG.
        ENDLOOP.

      ELSE.

        CALL FUNCTION 'MESSAGE_TEXT_BUILD'
          EXPORTING
            MSGID = SY-MSGID
            MSGNR = SY-MSGNO
            MSGV1 = SY-MSGV1
            MSGV2 = SY-MSGV2
            MSGV3 = SY-MSGV3
            MSGV4 = SY-MSGV4
          IMPORTING
            MESSAGE_TEXT_OUTPUT = LT_MSG.

        CHECK_FLAG = 'F'.
        GT_DATA_ALV-SIGN = '@5C@'.
        GT_DATA_ALV-NO  = 1.
        GT_DATA_ALV-MSG = LT_MSG.

      ENDIF.

  ENDCASE.

ENDFORM.

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

  DATA: LT_MSG        TYPE STRING.

  DATA: LT_AGR_TEXTS  LIKE TABLE OF AGR_TEXTS WITH HEADER LINE.

*### 해당 언어에 설명이 있는지 없는지 확인하기 위해 AGR_TEXTS 테이블 조회
  SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_TEXTS
    FROM AGR_TEXTS
    WHERE AGR_NAME = GT_DATA-NEW_ROLE
      AND SPRAS    = SY-LANGU
      AND LINE     = 0.

*### MODIFY를 사용해, 해당 언어의 설명이 없으면 Insert, 있다면 Update 한다.
  REFRESH: LT_AGR_TEXTS. CLEAR: LT_AGR_TEXTS.

  LT_AGR_TEXTS-AGR_NAME = GT_DATA-NEW_ROLE.
  LT_AGR_TEXTS-SPRAS    = SY-LANGU.
  LT_AGR_TEXTS-LINE     = 0.
  LT_AGR_TEXTS-TEXT     = GT_DATA-CHG_DESC.
  APPEND LT_AGR_TEXTS.

  LOOP AT LT_AGR_TEXTS.
    MODIFY AGR_TEXTS FROM TABLE LT_AGR_TEXTS.
  ENDLOOP.

  "### MODIFY 실패시 오류메세지 출력을 위한 로직
  IF SY-SUBRC <> 0.
    READ TABLE GT_DATA_ALV WITH KEY NEW_ROLE = GT_DATA-NEW_ROLE .

    CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        MSGID = SY-MSGID
        MSGNR = SY-MSGNO
        MSGV1 = SY-MSGV1
        MSGV2 = SY-MSGV2
        MSGV3 = SY-MSGV3
        MSGV4 = SY-MSGV4
      IMPORTING
        MESSAGE_TEXT_OUTPUT = LT_MSG.

    CHECK_FLAG = 'F'.
    GT_DATA_ALV-SIGN = '@5C@'.
    GT_DATA_ALV-NO  = 1.
    GT_DATA_ALV-MSG = LT_MSG.
  ENDIF.

ENDFORM.

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

  DATA: LT_MSG      TYPE STRING.
  DATA: LT_LINE     TYPE N.
  DATA: TEMP_CNT(6) TYPE N.

  DATA: LT_AGR_ROW LIKE TABLE OF AGR_1252   WITH HEADER LINE,
        LT_AGR_CHG LIKE TABLE OF AGR_1252   WITH HEADER LINE.

*### 롤 조직레벨에 BUKRS 가 있는 지 조회
  SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
    FROM AGR_1252
    WHERE AGR_NAME = GT_DATA-NEW_ROLE
      AND VARBL    = '$BUKRS'.

*### 롤 조직레벨에 BUKRS 가 없다면 LT_LINE은 0값.
*### 롤에 회사코드 조직레벨이 없다면 변경 없이 스킵한다.
  LT_LINE = LINES( LT_AGR_ROW ).

* ### 해당 롤에 조직레벨 값이 있다면, 기존 조직레벨 값은 전체 삭제 후, 신규 값으로 새로 생성한다.
  IF LT_LINE >= 1.

    DELETE FROM AGR_1252 WHERE AGR_NAME = GT_DATA-NEW_ROLE
                           AND VARBL    = '$BUKRS'.

    CLEAR: LT_AGR_ROW, LT_AGR_CHG. REFRESH: LT_AGR_ROW, LT_AGR_CHG.

    SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
      FROM AGR_1252
      WHERE AGR_NAME = GT_DATA-NEW_ROLE.

* ### 비어있는 COUNTER 구멍을 찾는 로직
    TEMP_CNT = 1.
    DO 1000 TIMES.
      READ TABLE LT_AGR_ROW WITH KEY COUNTER = TEMP_CNT.
      IF SY-SUBRC = 0. TEMP_CNT = TEMP_CNT + 1.
      ELSE. EXIT. ENDIF. "### 비어있는 COUNTER 를 찾았다면, 해당 반복문 탈출 후, 새 조직레벨값을 기록한다.
    ENDDO.

* ### 새 조직레벨 값 입력
    LT_AGR_CHG-MANDT    = SY-MANDT.
    LT_AGR_CHG-AGR_NAME = GT_DATA-NEW_ROLE.
    LT_AGR_CHG-COUNTER  = TEMP_CNT.
    LT_AGR_CHG-VARBL    = '$BUKRS'.
    LT_AGR_CHG-LOW      = R_BUKCOD.
    APPEND LT_AGR_CHG.

    LOOP AT LT_AGR_CHG.
      MODIFY AGR_1252 FROM TABLE LT_AGR_CHG.
    ENDLOOP.

* ### 조직레벨 값 변경이 설공적으로 이루어졌다면, ALV 테이블에 메세지와 상태를 기록한다.
    IF SY-SUBRC <> 0.
      READ TABLE GT_DATA_ALV WITH KEY NEW_ROLE = GT_DATA-NEW_ROLE .

      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          MSGID = SY-MSGID
          MSGNR = SY-MSGNO
          MSGV1 = SY-MSGV1
          MSGV2 = SY-MSGV2
          MSGV3 = SY-MSGV3
          MSGV4 = SY-MSGV4
        IMPORTING
          MESSAGE_TEXT_OUTPUT = LT_MSG.

      CHECK_FLAG = 'F'.
      GT_DATA_ALV-SIGN = '@5C@'.
      GT_DATA_ALV-NO  = 1.
      GT_DATA_ALV-MSG = LT_MSG.
    ENDIF.

  ENDIF.

ENDFORM.

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

  DATA: LT_MSG      TYPE STRING.
  DATA: LT_LINE     TYPE N.
  DATA: TEMP_CNT(6) TYPE N.

  DATA: LT_AGR_ROW LIKE TABLE OF AGR_1252   WITH HEADER LINE,
        LT_AGR_CHG LIKE TABLE OF AGR_1252   WITH HEADER LINE.

*### 롤 조직레벨에 BUKRS 가 있는 지 조회
  SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
    FROM AGR_1252
    WHERE AGR_NAME = GT_DATA-NEW_ROLE
      AND VARBL    = '$EKORG'.

*### 롤 조직레벨에 BUKRS 가 없다면 LT_LINE은 0값.
*### 롤에 회사코드 조직레벨이 없다면 변경 없이 스킵한다.
  LT_LINE = LINES( LT_AGR_ROW ).

* ### 해당 롤에 조직레벨 값이 있다면, 기존 조직레벨 값은 전체 삭제 후, 신규 값으로 새로 생성한다.
  IF LT_LINE >= 1.

    DELETE FROM AGR_1252 WHERE AGR_NAME = GT_DATA-NEW_ROLE
                           AND VARBL    = '$EKORG'.

    CLEAR: LT_AGR_ROW, LT_AGR_CHG. REFRESH: LT_AGR_ROW, LT_AGR_CHG.

    SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
      FROM AGR_1252
      WHERE AGR_NAME = GT_DATA-NEW_ROLE.

* ### 비어있는 COUNTER 구멍을 찾는 로직
    TEMP_CNT = 1.
    DO 1000 TIMES.
      READ TABLE LT_AGR_ROW WITH KEY COUNTER = TEMP_CNT.
      IF SY-SUBRC = 0. TEMP_CNT = TEMP_CNT + 1.
      ELSE. EXIT. ENDIF. "### 비어있는 COUNTER 를 찾았다면, 해당 반복문 탈출 후, 새 조직레벨값을 기록한다.
    ENDDO.

* ### 새 조직레벨 값 입력
    LT_AGR_CHG-MANDT    = SY-MANDT.
    LT_AGR_CHG-AGR_NAME = GT_DATA-NEW_ROLE.
    LT_AGR_CHG-COUNTER  = TEMP_CNT.
    LT_AGR_CHG-VARBL    = '$EKORG'.
    LT_AGR_CHG-LOW      = R_EKOCOD.
    APPEND LT_AGR_CHG.

    LOOP AT LT_AGR_CHG.
      MODIFY AGR_1252 FROM TABLE LT_AGR_CHG.
    ENDLOOP.

* ### 조직레벨 값 변경이 설공적으로 이루어졌다면, ALV 테이블에 메세지와 상태를 기록한다.
    IF SY-SUBRC <> 0.
      READ TABLE GT_DATA_ALV WITH KEY NEW_ROLE = GT_DATA-NEW_ROLE .

      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          MSGID = SY-MSGID
          MSGNR = SY-MSGNO
          MSGV1 = SY-MSGV1
          MSGV2 = SY-MSGV2
          MSGV3 = SY-MSGV3
          MSGV4 = SY-MSGV4
        IMPORTING
          MESSAGE_TEXT_OUTPUT = LT_MSG.

      CHECK_FLAG = 'F'.
      GT_DATA_ALV-SIGN = '@5C@'.
      GT_DATA_ALV-NO  = 1.
      GT_DATA_ALV-MSG = LT_MSG.
    ENDIF.

  ENDIF.

ENDFORM.

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

  DATA: LT_MSG      TYPE STRING.
  DATA: LT_LINE     TYPE N.
  DATA: TEMP_CNT(6) TYPE N.

  DATA: LT_AGR_ROW LIKE TABLE OF AGR_1252   WITH HEADER LINE,
        LT_AGR_CHG LIKE TABLE OF AGR_1252   WITH HEADER LINE.

*### 롤 조직레벨에 BUKRS 가 있는 지 조회
  SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
    FROM AGR_1252
    WHERE AGR_NAME = GT_DATA-NEW_ROLE
      AND VARBL    = '$VKORG'.

*### 롤 조직레벨에 BUKRS 가 없다면 LT_LINE은 0값.
*### 롤에 회사코드 조직레벨이 없다면 변경 없이 스킵한다.
  LT_LINE = LINES( LT_AGR_ROW ).

* ### 해당 롤에 조직레벨 값이 있다면, 기존 조직레벨 값은 전체 삭제 후, 신규 값으로 새로 생성한다.
  IF LT_LINE >= 1.

    DELETE FROM AGR_1252 WHERE AGR_NAME = GT_DATA-NEW_ROLE
                           AND VARBL    = '$VKORG'.

    CLEAR: LT_AGR_ROW, LT_AGR_CHG. REFRESH: LT_AGR_ROW, LT_AGR_CHG.

    SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
      FROM AGR_1252
      WHERE AGR_NAME = GT_DATA-NEW_ROLE.

* ### 비어있는 COUNTER 구멍을 찾는 로직
    TEMP_CNT = 1.
    DO 1000 TIMES.
      READ TABLE LT_AGR_ROW WITH KEY COUNTER = TEMP_CNT.
      IF SY-SUBRC = 0. TEMP_CNT = TEMP_CNT + 1.
      ELSE. EXIT. ENDIF. "### 비어있는 COUNTER 를 찾았다면, 해당 반복문 탈출 후, 새 조직레벨값을 기록한다.
    ENDDO.

* ### 새 조직레벨 값 입력
    LT_AGR_CHG-MANDT    = SY-MANDT.
    LT_AGR_CHG-AGR_NAME = GT_DATA-NEW_ROLE.
    LT_AGR_CHG-COUNTER  = TEMP_CNT.
    LT_AGR_CHG-VARBL    = '$VKORG'.
    LT_AGR_CHG-LOW      = R_VKOCOD.
    APPEND LT_AGR_CHG.

    LOOP AT LT_AGR_CHG.
      MODIFY AGR_1252 FROM TABLE LT_AGR_CHG.
    ENDLOOP.

* ### 조직레벨 값 변경이 설공적으로 이루어졌다면, ALV 테이블에 메세지와 상태를 기록한다.
    IF SY-SUBRC <> 0.
      READ TABLE GT_DATA_ALV WITH KEY NEW_ROLE = GT_DATA-NEW_ROLE .

      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          MSGID = SY-MSGID
          MSGNR = SY-MSGNO
          MSGV1 = SY-MSGV1
          MSGV2 = SY-MSGV2
          MSGV3 = SY-MSGV3
          MSGV4 = SY-MSGV4
        IMPORTING
          MESSAGE_TEXT_OUTPUT = LT_MSG.

      CHECK_FLAG = 'F'.
      GT_DATA_ALV-SIGN = '@5C@'.
      GT_DATA_ALV-NO  = 1.
      GT_DATA_ALV-MSG = LT_MSG.
    ENDIF.

  ENDIF.

ENDFORM.

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

  DATA: LT_MSG      TYPE STRING.
  DATA: LT_LINE     TYPE N.
  DATA: TEMP_CNT(6) TYPE N.
  DATA: TEMP_WERKS  TYPE N.

  DATA: LT_AGR_ROW LIKE TABLE OF AGR_1252   WITH HEADER LINE,
        LT_AGR_CHG LIKE TABLE OF AGR_1252   WITH HEADER LINE.

*### 롤 조직레벨에 BUKRS 가 있는 지 조회
  SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
    FROM AGR_1252
    WHERE AGR_NAME = GT_DATA-NEW_ROLE
      AND VARBL    = '$WERKS'.

*### 롤 조직레벨에 BUKRS 가 없다면 LT_LINE은 0값.
*### 롤에 회사코드 조직레벨이 없다면 변경 없이 스킵한다.
  LT_LINE = LINES( LT_AGR_ROW ).

* ### 해당 롤에 조직레벨 값이 있다면, 기존 조직레벨 값은 전체 삭제 후, 신규 값으로 새로 생성한다.
  IF LT_LINE >= 1.

    DELETE FROM AGR_1252 WHERE AGR_NAME = GT_DATA-NEW_ROLE
                           AND VARBL    = '$WERKS'.

    CLEAR: LT_AGR_ROW, LT_AGR_CHG. REFRESH: LT_AGR_ROW, LT_AGR_CHG.

*    SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
*      FROM AGR_1252
*      WHERE AGR_NAME = GT_DATA-NEW_ROLE.


* ### 입력된 플랜트(WERKS) 갯수를 확인한다.
    TEMP_WERKS = 0.
    IF R_WERKS1 IS NOT INITIAL. TEMP_WERKS = 1. ENDIF.
    IF R_WERKS2 IS NOT INITIAL. TEMP_WERKS = 2. ENDIF.
    IF R_WERKS3 IS NOT INITIAL. TEMP_WERKS = 3. ENDIF.

* ### 입력된 플랜트 갯수 만큼, 권한조직값 업데이트를 실행한다.
    DO TEMP_WERKS TIMES.

      SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_AGR_ROW
        FROM AGR_1252
        WHERE AGR_NAME = GT_DATA-NEW_ROLE.

*     ### 비어있는 COUNTER 구멍을 찾는 로직
      TEMP_CNT = 1.
      DO 1000 TIMES.
        READ TABLE LT_AGR_ROW WITH KEY COUNTER = TEMP_CNT.
        IF SY-SUBRC = 0. TEMP_CNT = TEMP_CNT + 1.
        ELSE. EXIT. ENDIF. "### 비어있는 COUNTER 를 찾았다면, 해당 반복문 탈출 후, 새 조직레벨값을 기록한다.
      ENDDO.

      CLEAR: LT_AGR_CHG. REFRESH: LT_AGR_CHG.

* ### 새 조직레벨 값을 입력하기 전, 현재 반복문 단계의 플랜트값을 조정.
*     ### 새 조직레벨 값 입력
      LT_AGR_CHG-MANDT    = SY-MANDT.
      LT_AGR_CHG-AGR_NAME = GT_DATA-NEW_ROLE.
      LT_AGR_CHG-COUNTER  = TEMP_CNT.
      LT_AGR_CHG-VARBL    = '$WERKS'.

      "### DO 반복문 순서에 따라 들어가는 플랜트의 값이 달라진다.
      "### 첫번째 반복일때 R_WERKS1, 두번째 반복일때 R_WERKS2, 세번째 반복일때 R_WERKS3.
      IF SY-INDEX = 1. LT_AGR_CHG-LOW      = R_WERKS1. ENDIF.
      IF SY-INDEX = 2. LT_AGR_CHG-LOW      = R_WERKS2. ENDIF.
      IF SY-INDEX = 3. LT_AGR_CHG-LOW      = R_WERKS3. ENDIF.
      APPEND LT_AGR_CHG.

      LOOP AT LT_AGR_CHG.
        MODIFY AGR_1252 FROM TABLE LT_AGR_CHG.
      ENDLOOP.

*     ### 조직레벨 값 변경이 설공적으로 이루어졌다면, ALV 테이블에 메세지와 상태를 기록한다.
      IF SY-SUBRC <> 0.
        READ TABLE GT_DATA_ALV WITH KEY NEW_ROLE = GT_DATA-NEW_ROLE .

        CALL FUNCTION 'MESSAGE_TEXT_BUILD'
          EXPORTING
            MSGID = SY-MSGID
            MSGNR = SY-MSGNO
            MSGV1 = SY-MSGV1
            MSGV2 = SY-MSGV2
            MSGV3 = SY-MSGV3
            MSGV4 = SY-MSGV4
          IMPORTING
            MESSAGE_TEXT_OUTPUT = LT_MSG.

        CHECK_FLAG = 'F'.
        GT_DATA_ALV-SIGN = '@5C@'.
        GT_DATA_ALV-NO  = 1.
        GT_DATA_ALV-MSG = LT_MSG.
      ENDIF.

    ENDDO.

  ENDIF.

ENDFORM.

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

  DATA: ERR_MSG TYPE STRING.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_AGR_DEF
    FROM AGR_DEFINE
    WHERE AGR_NAME LIKE 'Z%'.

  LOOP AT GT_DATA.

*### 시스템에 복사할 롤의 소스 롤이 있는지 체크하는 로직
    READ TABLE GT_AGR_DEF WITH KEY AGR_NAME = GT_DATA-SOURCE_ROLE.
    IF SY-SUBRC <> 0.
      CONCATENATE '시스템에 원본 ROLE' GT_DATA-SOURCE_ROLE '이 없으므로, 복사를 수행할 수 없습니다.' INTO ERR_MSG SEPARATED BY SPACE.
      MESSAGE ERR_MSG TYPE 'E'.
      EXIT.
    ENDIF.

  ENDLOOP.

ENDFORM.

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

*### 회사코드 변경에 체크를 했지만, 변경할 회사코드값이 입력되지 않은 경우, 에러메시지 출력
  IF R_BUKTXT = 'X'.
    IF R_BUKCOD IS INITIAL.
      MESSAGE '변경될 회사코드가 입력되지 않았습니다' TYPE 'E'.
      CALL SELECTION-SCREEN 1000.
    ENDIF.
  ENDIF.

  IF R_EKOTXT = 'X'.
    IF R_EKOCOD IS INITIAL.
      MESSAGE '변경될 구매조직이 입력되지 않았습니다' TYPE 'E'.
      CALL SELECTION-SCREEN 1000.
    ENDIF.
  ENDIF.

  IF R_VKOTXT = 'X'.
    IF R_VKOCOD IS INITIAL.
      MESSAGE '변경될 영업조직이 입력되지 않았습니다' TYPE 'E'.
      CALL SELECTION-SCREEN 1000.
    ENDIF.
  ENDIF.

  IF R_WERTXT = 'X'.
    IF R_WERKS1 IS INITIAL.

      IF R_WERKS2 IS NOT INITIAL OR R_WERKS3 IS NOT INITIAL.
        MESSAGE '변경할 플랜트는 앞에 칸부터 순서대로 입력해주시기 바랍니다' TYPE 'E'.
        CALL SELECTION-SCREEN 1000.
      ENDIF.

      MESSAGE '변경될 플랜트가 입력되지 않았습니다' TYPE 'E'.
      CALL SELECTION-SCREEN 1000.
    ELSE.
      IF R_WERKS3 IS NOT INITIAL AND R_WERKS2 IS INITIAL.
        MESSAGE '변경할 플랜트는 앞에 칸부터 순서대로 입력해주시기 바랍니다' TYPE 'E'.
        CALL SELECTION-SCREEN 1000.
      ENDIF.
    ENDIF.
  ENDIF.


ENDFORM.

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

  DATA: LT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.


  SORT GT_DATA_ALV BY NO SOURCE_ROLE.

  REFRESH: LT_FIELDCAT. CLEAR: LT_FIELDCAT.

  LT_FIELDCAT-FIELDNAME  = 'SIGN'  .
  LT_FIELDCAT-COL_POS    = 1.
  LT_FIELDCAT-SELTEXT_M  = ''.
  LT_FIELDCAT-KEY        = 'X'.
  LT_FIELDCAT-JUST        = 'C'.
  LT_FIELDCAT-OUTPUTLEN   = 3.
  APPEND LT_FIELDCAT TO LT_FIELDCAT. CLEAR  LT_FIELDCAT.

  LT_FIELDCAT-FIELDNAME  = 'SOURCE_ROLE'  .
  LT_FIELDCAT-COL_POS    = 2.
  LT_FIELDCAT-SELTEXT_M  = 'Source Role'.
  LT_FIELDCAT-OUTPUTLEN   = 20.
  APPEND LT_FIELDCAT TO LT_FIELDCAT. CLEAR  LT_FIELDCAT.

  LT_FIELDCAT-FIELDNAME  = 'NEW_ROLE'  .
  LT_FIELDCAT-COL_POS    = 3.
  LT_FIELDCAT-SELTEXT_M  = 'Copy Role'.
  LT_FIELDCAT-OUTPUTLEN   = 20.
  APPEND LT_FIELDCAT TO LT_FIELDCAT. CLEAR  LT_FIELDCAT.

  LT_FIELDCAT-FIELDNAME  = 'MSG'  .
  LT_FIELDCAT-COL_POS    = 4.
  LT_FIELDCAT-SELTEXT_M  = 'Message'.
  LT_FIELDCAT-OUTPUTLEN   = 80.
  APPEND LT_FIELDCAT TO LT_FIELDCAT. CLEAR  LT_FIELDCAT.

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

ENDFORM.

*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                              *
*-------------------------------------------------------------------*
* 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.

* Title
  WA_HEADER-TYP  = 'H'.
  WA_HEADER-INFO = 'Role 복사 완료'.
  APPEND WA_HEADER TO T_HEADER.
  CLEAR WA_HEADER.

  CLEAR: GT_LINE.
  LOOP AT GT_DATA_ALV WHERE SIGN = '@5B@'.
    GT_LINE = GT_LINE + 1. ENDLOOP.

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

* Total No. of Records Selected
  CONCATENATE '총 복사된 Role 수: ' GT_LINE 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.

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

  %_P_FILE_%_APP_%-TEXT   = 'Base Excel File'.
  %_R_DESC_%_APP_%-TEXT   = ' Role 설명 변경'.

  %_R_CPUSR_%_APP_%-TEXT   = ' Role 복사 시, 유저도 같이 복사'.


*  IF R_BUKTXT = 'X'.
*    LOOP AT SCREEN.
*      IF SCREEN-NAME = 'R_BUKCOD'.
*        SCREEN-INPUT = '1'.
*      ENDIF.
*      MODIFY SCREEN.
*    ENDLOOP.
*  ELSE.
*    LOOP AT SCREEN.
*      IF SCREEN-NAME = 'R_BUKCOD'.
*        SCREEN-INPUT = '0'.
*      ENDIF.
*      MODIFY SCREEN.
*    ENDLOOP.
*  ENDIF.


ENDFORM.

 


2. 프로그램 사용 엑셀 양식

SOURCE_ROLENEW_ROLECHG_DESC

ZSOURCE1 ZNEW1 Basic Role