Tuesday, November 26, 2013

infotype_upload

*&---------------------------------------------------------------------*
*& Report  ZHR0006_UPLOAD
*&
*&---------------------------------------------------------------------*

REPORT  ZHR0006_UPLOAD.
tables: PA0006.
TYPE-POOLS: SLIS.
*---Global Data declarations
*include ZHR0006_UPLOAD2_top.

TYPES: BEGIN OF ty_pa0006,
        PERNR TYPE  PERNR_D,
        SUBTY TYPE  SUBTY,
        OBJPS TYPE  OBJPS,
        SPRPS TYPE  SPRPS,
        ENDDA TYPE  ENDDA,
        BEGDA TYPE  BEGDA,
        SEQNR TYPE  SEQNR,
        AEDTM TYPE  AEDTM,
        UNAME TYPE  AENAM,
        HISTO TYPE  HISTO,
        ITXEX TYPE  ITXEX,
        REFEX TYPE  REFEX,
        ORDEX TYPE  ORDEX,
        ITBLD TYPE  ITBLD,
        PREAS TYPE  PREAS,
        FLAG1 TYPE  NUSED,
        FLAG2 TYPE  NUSED,
        FLAG3 TYPE  NUSED,
        FLAG4 TYPE  NUSED,
        RESE1 TYPE  NUSED2,
        RESE2 TYPE  NUSED2,
        GRPVL TYPE  PCCE_GPVAL,
        ANSSA TYPE  ANSSA,
        NAME2 TYPE  PAD_CONAM,
        STRAS TYPE  PAD_STRAS,
        ORT01 TYPE  PAD_ORT01,
        ORT02 TYPE  PAD_ORT02,
        PSTLZ TYPE  PSTLZ_HR,
        LAND1 TYPE  LAND1,
        TELNR TYPE  TELNR,
        ENTKM TYPE  ENTKM,
        WKWNG TYPE  WKWNG,
        BUSRT TYPE  BUSRT,
        LOCAT TYPE  PAD_LOCAT,
        ADR03 TYPE  AD_STRSPP1,
        ADR04 TYPE  AD_STRSPP2,
        STATE TYPE  REGIO,
        HSNMR TYPE  PAD_HSNMR,
        POSTA TYPE  PAD_POSTA,
        BLDNG TYPE  AD_BLD_10,
        FLOOR TYPE  AD_FLOOR,
        STRDS TYPE  STRDS,
        ENTK2 TYPE  ENTKM,
        COM01 TYPE  COMKY,
        NUM01 TYPE  COMNR,
        COM02 TYPE  COMKY,
        NUM02 TYPE  COMNR,
        COM03 TYPE  COMKY,
        NUM03 TYPE  COMNR,
        COM04 TYPE  COMKY,
        NUM04 TYPE  COMNR,
        COM05 TYPE  COMKY,
        NUM05 TYPE  COMNR,
        COM06 TYPE  COMKY,
        NUM06 TYPE  COMNR,
        INDRL TYPE  P22J_INDRL,
        COUNC TYPE  COUNC,
        RCTVC TYPE  P22J_RCTVC,
        OR2KK TYPE  P22J_ORKK2,
        CONKK TYPE  P22J_PCNKK,
        OR1KK TYPE  P22J_ORKK1,
        RAILW TYPE  RAILW,
      END OF ty_pa0006.


data : wa_pa0006 type ty_pa0006.          "pa0006. "ty_pa0006.
data : wa_pa0006_t type p0006.
DATA : it_pa0006 type table of ty_pa0006. "pa0006. " ty_pa0006
*data : wl_pa0006 type ty_pa0006.
data : la_pa0006 type table of ty_pa0006.
data : number type num8.

*-----------ALV FIELD CATLOG----------------------------------
DATA : IT_FCAT TYPE  SLIS_T_FIELDCAT_ALV,
       WA_FCAT TYPE LINE OF SLIS_T_FIELDCAT_ALV.

DATA:IS_LAY TYPE SLIS_LAYOUT_ALV.
*--------------------------------------------------------------
*-------------------store unsucessful records------------------

DATA: lo_gui TYPE REF TO cl_gui_frontend_services,
lv_title TYPE string,
lv_folder TYPE string,
lv_dir TYPE string,
v_filetype TYPE string,
v_filename TYPE string,
lv_filename type string.

DATA : WE_PA0006 TYPE TY_PA0006.
data : er_pa0006 type table of ty_pa0006.


*-----------------------------------------------------------

DATA: P_RETURN TYPE BAPIRETURN1.


*----------------------------------------------------------
data : g_file type ibipparms-path.


PARAMETERS : P_FILE TYPE RLGRAP-FILENAME. "STRING.
PARAMETER u_file like RLGRAP-FILENAME.


at SELECTION-SCREEN on VALUE-REQUEST FOR  p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      PROGRAM_NAME  = SYST-CPROG
      DYNPRO_NUMBER = SYST-DYNNR
      FIELD_NAME    = 'p_file'
    IMPORTING
      FILE_NAME     = g_file.
  p_file = g_file.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR u_file.
  PERFORM f_browse CHANGING u_file.


*PERFORM FILL_FIELD_CATALOG.

*-----------------------------------------------------------

START-OF-SELECTION.

*---This logic is for executing Excel file from desktop
  data : l_intern    type alsmex_tabline occurs 0 with header line.
  data : l_index     type i.
  data : l_start_col type i value '1',
         l_start_row type i value '1',
         l_end_col   type i value '256',
         l_end_row   type i value '65536'.

  if p_file is not initial.
    field-symbols : .
    call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = p_file
        i_begin_col             = l_start_col
        i_begin_row             = l_start_row
        i_end_col               = l_end_col
        i_end_row               = l_end_row
      TABLES
        intern                  = l_intern
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        others                  = 3.

    if l_intern[] is initial.
*      message  i010(z1).
      exit.
    else.
      sort l_intern by row col.
      loop at l_intern.
        move l_intern-col to l_index.
        assign component l_index of structure wa_pa0006 to .
        if sy-subrc = 0. " Incase there are more xls columns than fields
          move l_intern-value to .
        endif.
        at end of row.
          append wa_pa0006 to it_pa0006.
          clear wa_pa0006.
        endat.
      endloop.
    endif.

  endif.

*  write:/'upload the data in to internal table'.
*----------------------------------------------------------


  LOOP AT IT_pa0006 INTO WA_pa0006.
    MOVE-CORRESPONDING wa_pa0006 to wa_pa0006_t.
*----------------------------------------------------------

    CALL FUNCTION 'HR_EMPLOYEE_ENQUEUE'
      EXPORTING
        number       = WA_pa0006-PERNR
      IMPORTING
        RETURN       = P_RETURN
        LOCKING_USER = SY-UNAME.

*------------------------------------------------------------


*------------------------------------------------------------
if WA_pa0006-SUBTY LT 4 and WA_pa0006-SUBTY <> 0.
    CALL FUNCTION 'HR_INFOTYPE_OPERATION'
      EXPORTING
       infty                   = '0006'
       number                  = WA_pa0006-PERNR
       SUBTYPE                 = WA_pa0006-SUBTY
       OBJECTID                = WA_pa0006-OBJPS
       LOCKINDICATOR           = WA_pa0006-SPRPS
       VALIDITYEND             = WA_pa0006-ENDDA
       VALIDITYBEGIN           = WA_pa0006-BEGDA
       RECORDNUMBER            = WA_pa0006-SEQNR
        record                 = WA_pa0006_t
*    operation                 = insert
        operation              = 'INS'
*   TCLAS                      = 'A'
        DIALOG_MODE            = '0'
*    NOCOMMIT                  = 'X'
*   VIEW_IDENTIFIER            =
*   SECONDARY_RECORD           =
     IMPORTING
        RETURN                 = P_RETURN
*   KEY                        =
                       .

    IF SY-SUBRC eq 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      message s000(zms) with 'database updated'.

      IF P_RETURN-TYPE EQ 'E'." and wa_pa0006_t-pernr eq 0 .
        APPEND WA_PA0006 TO er_pa0006.
      ENDIF.
      IF P_RETURN-TYPE <> 'E'." and wa_pa0006_t-pernr <> 0.
        APPEND WA_PA0006 TO la_pa0006.
      ENDIF.


    ENDIF.
    else.
       APPEND WA_PA0006 TO er_pa0006.
    endif.

*----------------------------------------------------------

    CALL FUNCTION 'HR_EMPLOYEE_DEQUEUE'
    EXPORTING
      number        = WA_pa0006-PERNR
*   IMPORTING
*     RETURN        =
                        .
*------------------------------------------------------------
    clear wa_pa0006.
  endloop.

  IF ER_PA0006[] IS NOT INITIAL.

    "This part of code to add column name in the downloaded file.

    data : begin of int_head occurs 0,
    Filed1(20) type c,                     " Header Data
    end of int_head.

    PERFORM INT_HEADS.


    v_filetype = '.xls'. "I just manipulate the file name using XLS file type.
    v_filename = u_file.
    CONCATENATE u_file v_filetype INTO lv_filename.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                = lv_filename
        filetype                = 'ASC'
        write_field_separator   = 'X'
      TABLES
        data_tab                = ER_PA0006
        FIELDNAMES              = int_head
      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
        OTHERS                  = 22.

    IF sy-subrc <> 0.

    ENDIF.

  ENDIF.

  IF LA_PA0006[] IS NOT INITIAL.

    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
      EXPORTING
        I_PROGRAM_NAME               = SY-REPID
*   I_INTERNAL_TABNAME           =
        I_STRUCTURE_NAME             = 'P0006'
*   I_CLIENT_NEVER_DISPLAY       = 'X'
*   I_INCLNAME                   =
*   I_BYPASSING_BUFFER           =
*   I_BUFFER_ACTIVE              =
       CHANGING
         ct_fieldcat                  = IT_FCAT
* EXCEPTIONS
*   INCONSISTENT_INTERFACE       = 1
*   PROGRAM_ERROR                = 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.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
       I_CALLBACK_PROGRAM                = SY-REPID
       IS_LAYOUT                         = IS_LAY
       IT_FIELDCAT                       = IT_FCAT
      TABLES
        t_outtab                          = LA_PA0006
* 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.

  ENDIF.




*&---------------------------------------------------------------------*
*&      Form  f_browse
*&---------------------------------------------------------------------*

form f_browse CHANGING fc_file.
  DATA: lo_gui TYPE REF TO cl_gui_frontend_services,
  lv_title TYPE string,
  lv_folder TYPE string,
  lv_dir TYPE string.

  CREATE OBJECT lo_gui.
  lv_title = 'Define download location'.
  lv_folder = 'C:'.
  CALL METHOD lo_gui->directory_browse
    EXPORTING
      window_title    = lv_title
      initial_folder  = lv_folder
    CHANGING
      selected_folder = lv_dir.
  fc_file = lv_dir.

endform.                    "f_browse



*&---------------------------------------------------------------------*
*&      Form  INT_HEADS
*&---------------------------------------------------------------------*

FORM INT_HEADS .
  int_head-Filed1 = 'PERNR'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'SUBTY'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'OBJPS'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'SPRPS'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ENDDA'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'BEGDA'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'SEQNR'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'AEDTM'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'UNAME'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'HISTO'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ITXEX'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'REFEX'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ORDEX'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ITBLD'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'PREAS'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'FLAG1'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'FLAG2'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'FLAG3'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'FLAG4'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'RESE1'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'RESE2'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'GRPVL'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ANSSA'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NAME2'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'STRAS'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ORT01'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ORT02'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'PSTLZ'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'LAND1'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'TELNR'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ENTKM'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'WKWNG'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'BUSRT'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'LOCAT'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ADR03'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ADR04'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'STATE'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'HSNMR'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'POSTA'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'BLDNG'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'FLOOR'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'STRDS'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'ENTK2'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COM01'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NUM01'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COM02'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NUM02'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COM03'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NUM03'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COM04'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NUM04'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COM05'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NUM05'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COM06'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'NUM06'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'INDRL'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'COUNC'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'RCTVC'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'OR2KK'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'CONKK'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'OR1KK'.
  APPEND int_head.
  CLEAR int_head.

  int_head-Filed1 = 'RAILW'.
  APPEND int_head.
  CLEAR int_head.

ENDFORM.                    " INT_HEADS

No comments: