Monday, 26 March 2012

Downloading Employee LOG details

REPORT  ZR_D200_HRAS111_EMPREPORT.

TABLES PERNR.

INFOTYPES: 0000,0001,0002,0006,0008.

RANGES:R_PERNR FOR PERNR-PERNR,
       R_INFTY FOR P0008-INFTY.

DATA IT_LOG TYPE PLDOC_KEY_TAB.
DATA WA_LOG TYPE PLDOC_KEY.

DATA IT_INFTY_B TYPE PRELP_TAB.
DATA WA_INFTY_B TYPE PRELP.

DATA IT_INFTY_A TYPE PRELP_TAB.
DATA WA_INFTY_A TYPE PRELP.

DATA IT_FIELDS LIKE HRINFTYLOG_FIELDS OCCURS 0 WITH HEADER LINE.

DATA:BEGIN OF ITAB OCCURS 0,
      FNAME(6) TYPE C,
      OLD(10)  TYPE C,
      NEW(10)  TYPE C,
      PERNR TYPE PERNR,
      NACHN TYPE NACHN,
      HIRE  TYPE PREL-BEGDA,
      WERKS TYPE P0001-WERKS,
      BTRTL TYPE P0001-BTRTL,
      STRAS TYPE P0006-STRAS,
      ORT01 TYPE P0006-ORT01,
      ORT02 TYPE P0006-ORT02,
      PSTLZ TYPE P0006-PSTLZ,
      TRFAR TYPE P0008-TRFAR,
      TRFGB TYPE P0008-TRFGB,
      TRFGR TYPE P0008-TRFGR,
      ANSAL TYPE P0008-ANSAL,
     END OF ITAB.

DATA: D_PERNR TYPE PERNR,
      D_NACHN TYPE NACHN,
      D_HIRE  TYPE PREL-BEGDA,
      D_WERKS TYPE P0001-WERKS,
      D_BTRTL TYPE P0001-BTRTL,
      D_STRAS TYPE P0006-STRAS,
      D_ORT01 TYPE P0006-ORT01,
      D_ORT02 TYPE P0006-ORT02,
      D_PSTLZ TYPE P0006-PSTLZ,
      D_TRFAR TYPE P0008-TRFAR,
      D_TRFGB TYPE P0008-TRFGB,
      D_TRFGR TYPE P0008-TRFGR,
      D_ANSAL TYPE P0008-ANSAL.

DATA IT_HIFI TYPE PHIFI OCCURS 0 WITH HEADER LINE.

DATA IT_UNAME TYPE UNAME_RANGE OCCURS 0 WITH HEADER LINE.

PARAMETER P_FNAME TYPE STRING OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FNAME.

CALL METHOD CL_RSAN_UT_FILES=>F4
  EXPORTING
    I_APPLSERV              = SPACE
    I_TITLE                 = 'CHOOSE FILE FOR DOWN LOAD'
    I_GUI_EXTENSION         = '.TXT'
    I_GUI_EXT_FILTER        = SPACE
*    I_GUI_INITIAL_DIRECTORY =
*    I_APPLSERV_LOGICAL      = 'X'
*    I_APPLSERV_AL11         = 'X'
*  IMPORTING
*    E_CANCELED              =
*    E_LOGICAL_FILE          =
*    E_AL11_CHOSEN           =
  CHANGING
    C_FILE_NAME             = P_FNAME
*  EXCEPTIONS
*    FAILED                  = 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.


START-OF-SELECTION.

GET PERNR.

  RP-READ-ALL-TIME-ITY PN-BEGDA PN-ENDDA.

  CALL FUNCTION 'RP_HIRE_FIRE'
   EXPORTING
     BEG             = PN-BEGDA
     END             = PN-ENDDA
   IMPORTING
*     FIRE_DATE       =
     HIRE_DATE       = D_HIRE
    TABLES
      PPHIFI          = IT_HIFI
      PP0000          = P0000
      PP0001          = P0001
            .


  REFRESH R_PERNR[].

  R_PERNR-SIGN = 'I'.
  R_PERNR-OPTION = 'EQ'.
  R_PERNR-LOW = PERNR-PERNR.

  APPEND R_PERNR.

  REFRESH R_INFTY.

  R_INFTY-SIGN = 'I'.
  R_INFTY-OPTION = 'EQ'.
  R_INFTY-LOW = '0000'.

  APPEND R_INFTY.
  CLEAR R_INFTY.

  R_INFTY-SIGN = 'I'.
  R_INFTY-OPTION = 'EQ'.
  R_INFTY-LOW = '0001'.

  APPEND R_INFTY.
  CLEAR R_INFTY.

  R_INFTY-SIGN = 'I'.
  R_INFTY-OPTION = 'EQ'.
  R_INFTY-LOW = '0006'.

  APPEND R_INFTY.
  CLEAR R_INFTY.

  R_INFTY-SIGN = 'I'.
  R_INFTY-OPTION = 'EQ'.
  R_INFTY-LOW = '0008'.

  APPEND R_INFTY.
  CLEAR R_INFTY.

  IT_UNAME-SIGN = 'I'.
  IT_UNAME-OPTION = 'EQ'.
  IT_UNAME-LOW = 'SAPUSER'.

  APPEND IT_UNAME.
  CLEAR IT_UNAME.


  CALL FUNCTION 'HR_INFOTYPE_LOG_GET_LIST'
   EXPORTING
     TCLAS                    = 'A'
     BEGDA                    = PN-BEGDA
     ENDDA                    = PN-ENDDA
*   AUTH_CHECK               = 'X'
*   USE_ARCHIVE              = ' '
* IMPORTING
*   SUBRC                    =
   TABLES
     PERNR_TAB                = R_PERNR[]
     INFTY_TAB                = R_INFTY[]
     INFTY_LOGG_KEY_TAB       = IT_LOG
*   DATUM_TAB                =
   UNAME_TAB                = IT_UNAME
            .


  LOOP AT IT_LOG INTO WA_LOG.

    CALL FUNCTION 'HR_INFOTYPE_LOG_GET_DETAIL'
      EXPORTING
        LOGGED_INFOTYPE        = WA_LOG
*   AUTH_CHECK             = 'X'
*   USE_ARCHIVE            = ' '
* IMPORTING
*   SUBRC                  =
      TABLES
        INFTY_TAB_BEFORE       = IT_INFTY_B
        INFTY_TAB_AFTER        = IT_INFTY_A
        FIELDS                 = IT_FIELDS
              .

    MOVE:       PERNR-PERNR TO D_PERNR,
                P0002-NACHN TO D_NACHN,
                P0001-WERKS TO D_WERKS,
                P0001-BTRTL TO D_BTRTL,
                P0006-STRAS TO D_STRAS,
                P0006-ORT01 TO D_ORT01,
                P0006-ORT02 TO D_ORT02,
                P0006-PSTLZ TO D_PSTLZ,
                P0008-TRFAR TO D_TRFAR,
                P0008-TRFGB TO D_TRFGB,
                P0008-TRFGR TO D_TRFGR,
                P0008-ANSAL TO D_ANSAL.

    LOOP AT IT_FIELDS.

      IF IT_FIELDS-FNAME = 'PERSK' OR IT_FIELDS-FNAME = 'STAT2'
          OR IT_FIELDS-FNAME = 'ORT01' OR IT_FIELDS-FNAME = 'TRFGB'.

        IF IT_FIELDS-OLDDT NE IT_FIELDS-NEWDT.

          MOVE: IT_FIELDS-FNAME TO ITAB-FNAME,
                IT_FIELDS-OLDDT TO ITAB-OLD,
                IT_FIELDS-NEWDT TO ITAB-NEW,
                D_PERNR TO ITAB-PERNR,
                D_NACHN TO ITAB-NACHN,
                D_HIRE  TO ITAB-HIRE,
                D_WERKS TO ITAB-WERKS,
                D_BTRTL TO ITAB-BTRTL,
                D_STRAS TO ITAB-STRAS,
                D_ORT01 TO ITAB-ORT01,
                D_ORT02 TO ITAB-ORT02,
                D_PSTLZ TO ITAB-PSTLZ,
                D_TRFAR TO ITAB-TRFAR,
                D_TRFGB TO ITAB-TRFGB,
                D_TRFGR TO ITAB-TRFGR,
                D_ANSAL TO ITAB-ANSAL.

          APPEND ITAB.
          CLEAR ITAB.

        ENDIF.
      ENDIF.

    ENDLOOP.

  ENDLOOP.

END-OF-SELECTION.

 CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
  EXPORTING
*    BIN_FILESIZE              =
    FILENAME                  = P_FNAME
    FILETYPE                  = 'ASC'
    APPEND                    = SPACE
    WRITE_FIELD_SEPARATOR     = 'X'
*    HEADER                    = '00'
*    TRUNC_TRAILING_BLANKS     = SPACE
*    WRITE_LF                  = 'X'
*    COL_SELECT                = SPACE
*    COL_SELECT_MASK           = SPACE
    DAT_MODE                  = 'X'
*    CONFIRM_OVERWRITE         = SPACE
*    NO_AUTH_CHECK             = SPACE
*    CODEPAGE                  = SPACE
*    IGNORE_CERR               = ABAP_TRUE
*    REPLACEMENT               = '#'
*    WRITE_BOM                 = SPACE
*    TRUNC_TRAILING_BLANKS_EOL = 'X'
*    WK1_N_FORMAT              = SPACE
*    WK1_N_SIZE                = SPACE
*    WK1_T_FORMAT              = SPACE
*    WK1_T_SIZE                = SPACE
*  IMPORTING
*    FILELENGTH                =
  CHANGING
    DATA_TAB                  = ITAB[]
*  EXCEPTIONS
*    FILE_WRITE_ERROR          = 1
*    NO_BATCH                  = 2
*    GUI_REFUSE_FILETRANSFER   = 3
*    INVALID_TYPE              = 4
*    NO_AUTHORITY              = 5
*    UNKNOWN_ERROR             = 6
*    HEADER_NOT_ALLOWED        = 7
*    SEPARATOR_NOT_ALLOWED     = 8
*    FILESIZE_NOT_ALLOWED      = 9
*    HEADER_TOO_LONG           = 10
*    DP_ERROR_CREATE           = 11
*    DP_ERROR_SEND             = 12
*    DP_ERROR_WRITE            = 13
*    UNKNOWN_DP_ERROR          = 14
*    ACCESS_DENIED             = 15
*    DP_OUT_OF_MEMORY          = 16
*    DISK_FULL                 = 17
*    DP_TIMEOUT                = 18
*    FILE_NOT_FOUND            = 19
*    DATAPROVIDER_EXCEPTION    = 20
*    CONTROL_FLUSH_ERROR       = 21
*    NOT_SUPPORTED_BY_GUI      = 22
*    ERROR_NO_GUI              = 23
*    others                    = 24
        .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.