CRM 員工建立並分配用戶

REPORT zjp_emp_upload.
TABLES: sscrfields.
DATA:gt_excel_data TYPE TABLE OF zalsmex_tabline,
     gs_excel_data TYPE          zalsmex_tabline.
DATA: lv_filename   TYPE string,
      lt_file_table TYPE filetable,
      gv_error      TYPE flag,
      gv_fail_flag  TYPE flag,
      gv_title      TYPE lvc_title,
      lv_rc         TYPE i.
DATA: gt_zcrms0083 TYPE TABLE OF zcrms0083,
      gs_zcrms0083 TYPE zcrms0083.

DATA lt_return TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.
DATA lt_output TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.

DATA: centraldataperson TYPE bapibus1006_central_person.
DATA: addressdata       TYPE bapibus1006_address.
DATA: it_telefondata TYPE STANDARD TABLE OF bapiadtel,
      wa_telefondata TYPE                   bapiadtel.
DATA: it_email TYPE STANDARD TABLE OF bapiadsmtp,
      wa_email TYPE                   bapiadsmtp.
DATA: centraldata       TYPE bapibus1006_central.
DATA: l_bpartner     TYPE bu_partner,
      l_partner_guid TYPE bapibus1006_head-partnguid,
      l_guid_16      TYPE bu_partner_guid.

*****************************alv
DATA: g_program   TYPE syrepid VALUE sy-repid,
      gs_layout   TYPE slis_layout_alv,
      gt_fieldcat TYPE slis_t_fieldcat_alv,
      gs_fieldcat TYPE slis_fieldcat_alv,
      gt_event    TYPE slis_t_event,
      gs_event    TYPE slis_alv_event.
DATA lr_grid TYPE REF TO cl_gui_alv_grid.
*----------------------------------------------------------------------*
* MARCO DEFINE
*----------------------------------------------------------------------*

DEFINE add_fieldcat.
  clear gs_fieldcat.
  gs_fieldcat-fieldname  = &1 .
  gs_fieldcat-seltext_m  = &2 .
  gs_fieldcat-outputlen  = &3 .
  append gs_fieldcat to gt_fieldcat.
END-OF-DEFINITION.

SELECTION-SCREEN: FUNCTION KEY 1.

PARAMETERS : f_name LIKE rlgrap-filename.
*PARAMETERS : f_land LIKE adrc-country DEFAULT 'JP' OBLIGATORY.

INITIALIZATION.

  DATA: functxt TYPE smp_dyntxt.
  functxt-icon_id   = icon_export.
  functxt-quickinfo = 'Template Export'.
  functxt-icon_text = 'Template Export'.
  sscrfields-functxt_01 = functxt.

AT SELECTION-SCREEN OUTPUT.

START-OF-SELECTION.

  IF f_name IS INITIAL.
    MESSAGE 'Filename is null' TYPE 'S' DISPLAY LIKE 'E' .
    RETURN.
  ENDIF.
  PERFORM frm_upload_excel_file.
  CHECK gv_error IS INITIAL.
  PERFORM frm_make_alv_fieldcat.
  PERFORM frm_set_alv_layout.
  PERFORM frm_display_data
    TABLES
      gt_zcrms0083
    USING
      gv_title 'ZEMP_STATUS' 'ZEMP_UCOMM'.

AT SELECTION-SCREEN .

*下載模板
  CASE sscrfields-ucomm.
    WHEN 'FC01'.
      CALL FUNCTION 'ZCRM_DOWNLOAD_TEMPLATE'
        EXPORTING
          iv_objid    = 'ZJP_EMP'
          iv_filename = 'TVS Employee Upload Template'
          iv_flag     = 'X'.
    WHEN OTHERS.
  ENDCASE.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR f_name.
* Call popup for selecting the file
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    CHANGING
      file_table              = lt_file_table
      rc                      = lv_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  READ TABLE lt_file_table INTO lv_filename INDEX 1 .
  IF sy-subrc <> 0 .
    MESSAGE 'Fail to get FileName' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ELSE.
    f_name = lv_filename.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  FRM_UPLOAD_EXCEL_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_upload_excel_file .


  FIELD-SYMBOLS:  <fs_excel>   TYPE any,
                  <fs_field>   TYPE any,
                  <fs_extract> TYPE any.
  DATA: lv_count TYPE i VALUE 2,
        lv_pos   TYPE i,
        lv_str   TYPE string,
        lv_cnt   TYPE i,
        lv_edmk  TYPE string,
        lv_type  TYPE string.

*  DATA: lt_crm0007 LIKE TABLE OF gw_crm0007.
  REFRESH gt_excel_data.
  CALL FUNCTION 'ZEXCEL_UPLOAD'
    EXPORTING
      filename                = f_name
      i_begin_col             = '1' "開始的列
      i_begin_row             = '5' "開始的行
      i_end_col               = '20' "結束的列
      i_end_row               = '50000'
    TABLES
      intern                  = gt_excel_data
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  IF sy-subrc <> 0 .
    MESSAGE 'Upload Failed' TYPE 'S' DISPLAY LIKE 'E' .
    RETURN .
  ENDIF.
*解析excel導入的數據 寫進內表


  PERFORM frm_make_gt_upload TABLES gt_zcrms0083.
*  CHECK gv_error IS INITIAL.
*  PERFORM process_data.

  IF gt_zcrms0083[] IS INITIAL.
    MESSAGE 'Excel date is null' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ENDIF.


ENDFORM.                    " FRM_UPLOAD_EXCEL_FILE
*&---------------------------------------------------------------------*
*&      Form  FRM_MAKE_GT_UPLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_make_gt_upload TABLES cv_tab TYPE table.

  DATA lv_fieldvalue TYPE fieldvalue.  "字符值
  DATA lv_fldname TYPE fieldname.   "字段名
  DATA lo_exception TYPE REF TO cx_root.
  DATA lv_col TYPE i.
  DATA lr_line TYPE REF TO data.
  FIELD-SYMBOLS: <fs_field>,<line>.
  CLEAR gv_error.
  SORT gt_excel_data BY row col.

  CREATE DATA lr_line LIKE LINE OF cv_tab.
  ASSIGN lr_line->* TO <line>.

  TRY .
      LOOP AT gt_excel_data INTO gs_excel_data.
        AT NEW row.
          CLEAR <line>.
        ENDAT.
        lv_fieldvalue = gs_excel_data-value.
        CONDENSE lv_fieldvalue NO-GAPS.

        ASSIGN COMPONENT gs_excel_data-col OF STRUCTURE <line> TO <fs_field>.
        IF sy-subrc = 0.
          <fs_field> = lv_fieldvalue.
        ENDIF.
        AT END OF row.
          APPEND <line> TO cv_tab.
        ENDAT.
      ENDLOOP.
    CATCH cx_root INTO lo_exception.

      CALL METHOD lo_exception->if_message~get_text
        RECEIVING
          result = gv_error.

      CONCATENATE gv_error ' Please Check import data'
              INTO gv_error.
      MESSAGE gv_error TYPE 'S' DISPLAY LIKE 'E'.
      RETURN.
  ENDTRY.

ENDFORM.                    " FRM_MAKE_GT_UPLOAD
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  FRM_MAKE_ALV_FIELDCAT
*&---------------------------------------------------------------------*
*       設置ALV fieldcat
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_make_alv_fieldcat .
  add_fieldcat:
     'ICON'   'ICON'        5,
     'PARTNER'   'Business Partner'        18,
     'TITLE'      'Tittle'        11,
     'NAME_LAST'      'Last Name'    16,
     'NAME_FIRST'    'First Name'    16,
     'TEL_NUMBER'      'Tel Number'      18,
     'MOB_NUMBER'        'Mob Number'        18,
     'E_MAIL'        'Email'        28,
     'USERNAME'        'SAP User ID'        16,
     'COUNTRY'     'Country'        8,
     'REGION'     'Region'        8,
     'CITY1'     'City'        16,
     'POST_CODE1'     'Postal Code'        16,
     'MESSAGE'        'Error Message'        50.


ENDFORM.                    " FRM_MAKE_ALV_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_ALV_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_set_alv_layout .

  CLEAR: gv_title,gs_layout.
  gs_layout-zebra = 'X'.



ENDFORM.                    " FRM_SET_ALV_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_display_data TABLES it_tab USING  iv_title TYPE lvc_title
                                           iv_pf_status TYPE slis_formname
                                           iv_user_comm TYPE slis_formname .

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = iv_pf_status
      i_callback_user_command  = iv_user_comm
*     I_CALLBACK_TOP_OF_PAGE   = ' '
*     I_GRID_SETTINGS          =
      is_layout                = gs_layout
      it_fieldcat              = gt_fieldcat
      i_grid_title             = iv_title
*     IT_SORT                  =
*     IT_FILTER                =
*     I_SAVE                   = ' '
*     IS_VARIANT               =
    TABLES
      t_outtab                 = it_tab[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
ENDFORM.                    " FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
*&      Form  UCOMM_UPLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IV_UCOMM     text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM zemp_ucomm  USING iv_ucomm LIKE sy-ucomm
                         rs_selfield TYPE slis_selfield.
  CASE iv_ucomm.
    WHEN 'BACK' OR 'CANCEL' .
      LEAVE TO SCREEN 0 .
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'SAVE'.
      PERFORM process_data.

    WHEN OTHERS.
  ENDCASE.
ENDFORM.                    "UCOMM_UPLOAD
*&---------------------------------------------------------------------*
*&      Form  STATUS_UPLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
FORM zemp_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZEMP_STATUS' .
ENDFORM.                    "STATUS_UPLOAD
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_data .
  LOOP AT gt_zcrms0083 INTO gs_zcrms0083.
    CLEAR centraldata.
    centraldata-title_key    = gs_zcrms0083-title.                      "TITLE

    CLEAR: lt_return .
    CLEAR:centraldataperson.
    centraldataperson-firstname    = gs_zcrms0083-name_first.                     "First Name
    centraldataperson-lastname    = gs_zcrms0083-name_last.                      "Last Name

    CLEAR:addressdata.
    addressdata-postl_cod1        = gs_zcrms0083-post_code1          .          "postal
    addressdata-country        = gs_zcrms0083-country .                   "county
    addressdata-city            = gs_zcrms0083-city1            .                "city
    addressdata-region        = gs_zcrms0083-region .

    CLEAR:it_telefondata,wa_telefondata.
    wa_telefondata-telephone         = gs_zcrms0083-tel_number.
    wa_telefondata-r_3_user = '1'.
*    wa_telefondata-country = f_land.                                               "telephone
    APPEND wa_telefondata TO it_telefondata.
    wa_telefondata-telephone         = gs_zcrms0083-mob_number.
    wa_telefondata-r_3_user = '3'.
*    wa_telefondata-country = f_land.                                               "MOBphone
    APPEND wa_telefondata TO it_telefondata.

    CLEAR:it_email,wa_email.
    wa_email-e_mail = gs_zcrms0083-e_mail.                                       "E-Mail
    APPEND wa_email TO it_email.

    CLEAR:lt_return,l_bpartner.
    CALL FUNCTION 'BAPI_BUPA_CREATE_FROM_DATA'
      EXPORTING
        partnercategory   = '1'
        partnergroup      = 'Z002'
        centraldata       = centraldata
        centraldataperson = centraldataperson
        addressdata       = addressdata
*       DUPLICATE_MESSAGE_TYPE             =
*       ACCEPT_ERROR      = ' '
      IMPORTING
        businesspartner   = l_bpartner
      TABLES
        telefondata       = it_telefondata
        e_maildata        = it_email
        return            = lt_return.

    READ TABLE lt_return WITH KEY type = 'E'.
    IF sy-subrc <> 0.
      CLEAR lt_return.
      CALL FUNCTION 'BUPA_ROLE_ADD_2'
        EXPORTING
          iv_partner     = l_bpartner
*         IV_PARTNER_GUID       =
*         IV_PARTNERROLECATEGORY       =
*         iv_x_all_partnerroles =
          iv_partnerrole = 'BUP003'
*         IV_DIFFTYPEVALUE      =
*         IV_VALIDFROM   =
*         IV_VALIDTO     = '99991231'
*         IV_X_SAVE      = 'X'
*         IV_CREATE_CP   =
        TABLES
          et_return      = lt_return.

      READ TABLE lt_return WITH KEY type = 'E'.
      IF sy-subrc <> 0.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.

        gs_zcrms0083-icon = '@5B@'.
        gs_zcrms0083-partner = l_bpartner.

        CLEAR: l_partner_guid,l_guid_16.
        CALL FUNCTION 'BAPI_BUPA_GET_NUMBERS'
          EXPORTING
            businesspartner        = l_bpartner
          IMPORTING
            businesspartnerguidout = l_partner_guid.

        l_guid_16 = l_partner_guid.
        CALL FUNCTION 'BP_BUPA_CREATECENTRALPERSON'
          EXPORTING
            iv_bu_partner_guid      = l_guid_16
*           IV_NAME                 =
*           IV_EMPLOYEE_ID          =
            iv_user_id              = gs_zcrms0083-username
*           IV_NO_COMMIT            = ' '
*           IV_ASYNCHRON            = ' '
*           IV_BUFFER_MODE          = ' '
*           IS_BUS000               =
*       IMPORTING
*           EV_PERSON_ID            =
          EXCEPTIONS
            buffer_mode_not_allowed = 1
            OTHERS                  = 2.
        IF sy-subrc <> 0.
          gv_fail_flag = 'X'.
          gs_zcrms0083-icon = '@5C@'.
          gs_zcrms0083-message = 'Assign Username Failed'.
        ENDIF.
      ELSE.
        gv_fail_flag = 'X'.
        gs_zcrms0083-icon = '@5C@'.
        gs_zcrms0083-message = lt_return-message.
      ENDIF.
    ELSE.
      gv_fail_flag = 'X'.
      gs_zcrms0083-icon = '@5C@'.
      gs_zcrms0083-message = lt_return-message.
    ENDIF.

    MODIFY gt_zcrms0083 FROM gs_zcrms0083.
  ENDLOOP.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.

  CALL METHOD lr_grid->refresh_table_display
    EXCEPTIONS
      finished = 1
      OTHERS   = 2.

  IF gv_fail_flag IS INITIAL.
    MESSAGE 'All Record Create Successfully' TYPE 'S'.
  ENDIF.
ENDFORM.
相關文章
相關標籤/搜索