S4 BP供应商主数据扩建公司代码,采购组织数据(CVI_EI_INBOUND_MAIN)

参考:https://blog.csdn.net/DeveloperMrMeng/article/details/131331147

以模板数据为基础直接扩充对应的公司代码和采购组织数据:

 

report ZED1_TEST1.
tables:sscrfields.

data:gt_upload type alsmex_tabline occurs 0 with header line.
field-symbols:<fs_upload> like line of gt_upload.

data:begin of gs_table,
       light        type c,
       lifnr        type lifnr,   "供应商帐户号
       bukrs1       type bukrs,   "模板公司代码
       bukrs2       type bukrs,   "扩建公司代码
       ekorg1       type ekorg,   "模板采购组织
       ekorg2       type ekorg,   "扩建采购组织
       partner_guid type  bu_partner_guid,
       message      type string,  "消息
     end of gs_table.
data gt_table like table of gs_table.
field-symbols <table> like gs_table.

*alv
data: gt_fieldcat type slis_t_fieldcat_alv,
      ls_fieldcat like line of gt_fieldcat,   "WA APPEND TO FIELD CATALOG
      g_repid     type sy-repid,
      ls_layout   type slis_layout_alv.

***bapi
data:gs_data   type cvis_ei_extern,
     gt_data   type cvis_ei_extern_t,
     gt_return type   bapiretm.

data:lv_partner_guid type bu_partner_guid.  "供应商唯一id

data: gs_partner      type bus_ei_extern,       "业务合作伙伴的复杂外部接口
      gs_header       type bus_ei_header,       "抬头
      gs_central_data type bus_ei_central_data, "中心数据
      gs_common       type bus_ei_bupa_central. "中心数据->合作伙伴数据

data: gs_vendor          type vmds_ei_extern,  "供应商
      gs_header_vmd      type vmds_ei_header,  "供应商->数据抬头
      gs_company_data    type vmds_ei_vmd_company,     "供应商->公司代码
      gt_company         type vmds_ei_company_t,
      gs_company         type vmds_ei_company,
      gs_purchasing_data type vmds_ei_vmd_purchasing,  "供应商->采购组织
      gt_purchasing      type vmds_ei_purchasing_t,
      gs_purchasing      type vmds_ei_purchasing,
      gs_function_data   type vmds_ei_vmd_functions,  "供应商->采购组织-合作伙伴角色
      gt_functions       type vmds_ei_functions_t,     "合作伙伴角色
      gs_functions       type vmds_ei_functions.

constants:lc_parvw_pi type parvw value 'PI',
          lc_parvw_oa type parvw value 'OA',
          lc_parvw_vn type parvw value 'VN'.

data:gt_lfb1 type table of lfb1,
     gs_lfb1 type lfb1,
     gt_lfm1 type table of lfm1,
     gs_lfm1 type lfm1.


selection-screen function key 1. "应用工具栏增加按钮
selection-screen begin of block blk_001 with frame title text-001 . "选择文件路径
parameters:p_file like rlgrap-filename.
selection-screen end of block blk_001.

at selection-screen on value-request for p_file.
  perform frm_select_file.

initialization.
  sscrfields-functxt_01 = '模板下载'.
  sscrfields-ucomm      = 'FC01'.

at selection-screen.
  case sscrfields-ucomm.
    when 'ONLI'.
      perform frm_check_file_exist.
    when 'FC01'.
      perform frm_generate_template.
    when others.
  endcase.


start-of-selection.
  perform check_filename.
  perform frm_upload_data. " 导入文件数据
  perform frm_handle_data.

end-of-selection .
  perform f_alv_fieldcat.
  perform f_display.



form frm_handle_data.

* Check 供应商 exist
  select partner,partner_guid,bu_group into table @data(lt_guid) from but000 for all entries in @gt_table where partner = @gt_table-lifnr.
  sort lt_guid by partner.

  loop at gt_table assigning <table>.
    read table lt_guid into data(ls_guid) with key partner = <table>-lifnr.
    if sy-subrc = 0.
      <table>-partner_guid = ls_guid-partner_guid.
    else.
      <table>-message = '供应商不存在'.
      <table>-light = '1'.
    endif.
  endloop.

  read table gt_table transporting no fields with key light = '1'.
  if sy-subrc = 0.
    return.
  endif.

*模板 供应商对应的公司代码数据
  select * into table gt_lfb1 from lfb1 for all entries in gt_table where lifnr = gt_table-lifnr and bukrs = gt_table-bukrs1.
  sort gt_lfb1 by lifnr bukrs.
*模板 供应商对应的采购组织数据
  select * into table gt_lfm1 from lfm1 for all entries in gt_table where lifnr = gt_table-lifnr and ekorg = gt_table-ekorg1.
  sort gt_lfm1 by lifnr ekorg.

  loop at gt_table assigning <table>.
    clear:gs_header,gs_partner,gs_vendor,gs_lfb1,gs_company,gt_company,gs_company_data,
          gs_purchasing,gt_functions,gs_function_data,gs_purchasing,gt_purchasing,gs_purchasing_data,
          gs_vendor,gs_data,gt_data,gt_return.

* Basic data part.
    gs_header-object_task = 'U'.
    gs_header-object_instance-bpartner = <table>-lifnr.
    read table lt_guid into ls_guid with key partner = <table>-lifnr .
    gs_header-object_instance-bpartnerguid = ls_guid-partner_guid.
    gs_partner-header = gs_header.

    gs_common-data-bp_control-category = '2'.  "BUT000-TYPE业务伙伴类别:1人员  2组织   3组
    gs_common-data-bp_control-grouping = ls_guid-bu_group.  "BUT000-BU_GROUP业务伙伴分组
    gs_central_data-common = gs_common.
    gs_partner-central_data = gs_central_data.

* Vendor data part
    gs_header_vmd-object_task = 'U'.
    gs_header_vmd-object_instance-lifnr = <table>-lifnr.
    gs_vendor-header = gs_header_vmd.

*******扩充公司代码数据
    if <table>-bukrs1 <> ''.
      read table gt_lfb1 into gs_lfb1 with key lifnr = <table>-lifnr bukrs = <table>-bukrs1 binary search.
      if sy-subrc = 0.
        gs_company-task = 'I'.  "I:新增(扩展) 'U':修改
        gs_company-data_key-bukrs = <table>-bukrs2.              "公司代码

        if gs_lfb1-akont is not initial.
          gs_company-data-akont  = gs_lfb1-akont.               "总帐中的统驭科目
          gs_company-datax-akont = 'X'.
        endif.
        if gs_lfb1-zuawa is not initial.
          gs_company-data-zuawa  = gs_lfb1-zuawa.               "根据分配号排序代码
          gs_company-datax-zuawa = 'X'.
        endif.
        if gs_lfb1-fdgrv is not initial.
          gs_company-data-fdgrv  = gs_lfb1-fdgrv.               "计划组
          gs_company-datax-fdgrv = 'X'.
        endif.
        if gs_lfb1-zterm is not initial.
          gs_company-data-zterm  = gs_lfb1-zterm.               "收付条件代码
          gs_company-datax-zterm =  'X'.
        endif.
        append gs_company to gt_company.

        gs_company_data-company = gt_company. "公司代码数据
      else.
        <table>-light = '1'.
        <table>-message = '供应商对应的模板公司代码数据不存在!'.
        continue.
      endif.
    endif.

*******扩充采购组织数据
    if <table>-ekorg1 <> ''.
      read table gt_lfm1 into gs_lfm1 with key lifnr = <table>-lifnr ekorg = <table>-ekorg1 binary search.
      if sy-subrc = 0.
        gs_purchasing-task = 'I'.
        gs_purchasing-data_key-ekorg = <table>-ekorg2.          "Purchasing organization

        if gs_lfm1-waers is not initial.
          gs_purchasing-data-waers  = gs_lfm1-waers.           "订单货币
          gs_purchasing-datax-waers =  'X'.
        endif.
        if gs_lfm1-zterm is not initial.
          gs_purchasing-data-zterm  = gs_lfm1-zterm.           "收付条件代码
          gs_purchasing-datax-zterm =  'X'.
        endif.
        if gs_lfm1-webre is not initial.      "标识:基于收货的发票验证
          if gs_lfm1-webre = 'N'.
            gs_purchasing-data-webre  = space.
            gs_purchasing-datax-webre =  'X'.
          else.
            gs_purchasing-data-webre  = gs_lfm1-webre.
            gs_purchasing-datax-webre =  'X'.
          endif.
        endif.
        if gs_lfm1-ekgrp is not initial.                  "采购组
          gs_purchasing-data-ekgrp  = gs_lfm1-ekgrp.
          gs_purchasing-datax-ekgrp =  'X'.
        endif.

        gs_functions-task = 'I'.  "合作伙伴角色
        call function 'CONVERSION_EXIT_PARVW_INPUT'
          exporting
            input  = lc_parvw_pi
          importing
            output = gs_functions-data_key-parvw.
        append gs_functions to gt_functions.

        call function 'CONVERSION_EXIT_PARVW_INPUT'
          exporting
            input  = lc_parvw_oa
          importing
            output = gs_functions-data_key-parvw.
        append gs_functions to gt_functions.

        call function 'CONVERSION_EXIT_PARVW_INPUT'
          exporting
            input  = lc_parvw_vn
          importing
            output = gs_functions-data_key-parvw.
        append gs_functions to gt_functions.

        gs_function_data-functions = gt_functions.
        gs_purchasing-functions = gs_function_data.
        append gs_purchasing to gt_purchasing.
        gs_purchasing_data-purchasing = gt_purchasing.  "合作伙伴角色->采购组织

      else.
        <table>-light = '1'.
        <table>-message = '供应商对应的模板采购组织数据不存在!'.
        continue.
      endif.
    endif.

*数据合并
    gs_vendor-company_data = gs_company_data.
    gs_vendor-purchasing_data = gs_purchasing_data.

    gs_data-vendor = gs_vendor.
    gs_data-partner = gs_partner.
    append gs_data to gt_data.

    call function 'CVI_EI_INBOUND_MAIN'
      exporting
        i_data   = gt_data
      importing
        e_return = gt_return.
    loop at gt_return into data(gs_return).
      loop at gs_return-object_msg into data(gs_msg) where type ca 'EAX'.
        concatenate <table>-message gs_msg-message into <table>-message separated by '/'.
      endloop.
    endloop.

    if <table>-message is initial.
      call function 'BAPI_TRANSACTION_COMMIT'
        exporting
          wait = abap_true.
      <table>-light = '3'.
    else.
      call function 'BAPI_TRANSACTION_ROLLBACK' .
      <table>-light = '1'.
    endif.
  endloop.

endform.

form f4_help_for_filename  changing l_file type rlgrap-filename.
  data:
    l_file_import  type string,
    l_path_initial type string,
    lt_filetable   type filetable,
    lw_filetable   like line of lt_filetable,
    l_rc           type i.

  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      file_filter             = 'EXCEL 文件 (*.XLS;*XLSX)|*.XLS;*.XLSX'
    changing
      file_table              = lt_filetable
      rc                      = l_rc
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5.
  if sy-subrc <> 0.
  endif.

  if l_rc > 0.
    read table lt_filetable into lw_filetable index 1.
    if sy-subrc = 0.
      l_file = lw_filetable-filename.
    endif.
  endif.
endform.
form frm_select_file .
  data:lv_window_title type string,
       lv_file_filter  type string value 'Excel Files(*.xls;*.xlsx)|*.xls;*.xlsx|All Files(*.*)|*.*',
       lt_filetable    type filetable with header line,
       lv_rc           type i.
***选择文件
  lv_window_title  = '文件选择'(004).
  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      window_title            = lv_window_title
*     default_extension       =
*     default_filename        =
      file_filter             = lv_file_filter
*     with_encoding           =
      initial_directory       = 'C:\'
*     multiselection          =
    changing
      file_table              = lt_filetable[]
      rc                      = lv_rc
*     user_action             =
*     file_encoding           =
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5.
  if sy-subrc = 0.
    read table lt_filetable index 1.
    if sy-subrc = 0.
      p_file = lt_filetable-filename.
    endif.
  endif.
endform.                    " FRM_SELECT_FILE
form frm_check_file_exist .
  data:lv_file   type string,
       lv_result type abap_bool.

  data:lv_name(255) type c,
       lv_path(255) type c.

  data:lv_lenth type i.

  check sy-ucomm = 'ONLI'.

  if p_file is initial.
    message '请输入文件路径' type 'E'.
  endif.

  lv_file = p_file.
  call method cl_gui_frontend_services=>file_exist
    exporting
      file                 = lv_file
    receiving
      result               = lv_result
    exceptions
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      others               = 5.
  if sy-subrc <> 0.
    message '文件不存在' type 'E'.
  endif.

***检查文件格式
  call function 'TRINT_SPLIT_FILE_AND_PATH'
    exporting
      full_name     = p_file
    importing
      stripped_name = lv_name
      file_path     = lv_path
    exceptions
      x_error       = 1
      others        = 2.
  if sy-subrc = 0.
    lv_lenth = strlen( lv_name ).
    if lv_lenth > 4.
      lv_lenth = lv_lenth - 4.
    endif.
    lv_name = lv_name+lv_lenth.
    translate lv_name to upper case.
    if lv_name ne '.XLS' and lv_name ne 'XLSX'.
      message '文件不是EXCEL格式文件,请检查' type 'E'.
    endif.
  endif.
endform.                    " FRM_CHECK_FILE_EXIST
*&---------------------------------------------------------------------*
*&      Form  FRM_GENERATE_TEMPLATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_generate_template .
  data:ls_object like wwwdatatab,
       lv_rc     type sy-subrc,
       lt_ft     type filetable,
       ls_ft     type file_table,
       lv_ua     type i.

  data:lwk_fn type rlgrap-filename.
  data:lv_objid type w3objid.

****查询模板
  lv_objid = 'ZED1_TEST1'.
  select single relid objid from wwwdata into corresponding fields of ls_object
    where relid = 'MI' and srtf2 = 0 and objid = lv_objid.                                  "smw0里对象名称
  if sy-subrc <> 0 or ls_object-objid = space .
    message s001(00) with '未知的Excel模板:ZED1_TEST1' display like 'E'. "未知的Excel模板:ZRPMM_0103
    exit.
  endif.

  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      file_filter             = cl_gui_frontend_services=>filetype_excel
    changing
      file_table              = lt_ft
      rc                      = lv_rc
      user_action             = lv_ua
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
    with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.
    check lv_ua = 0.
    read table lt_ft into ls_ft index 1.
    lwk_fn  = ls_ft-filename.
***下载模板
    call function 'DOWNLOAD_WEB_OBJECT'
      exporting
        key         = ls_object
        destination = lwk_fn
      importing
        rc          = lv_rc.
    if lv_rc <> 0.
      message s001(00) display like 'E'. "下载模板失败!
      exit.
    endif.
  endif.
endform.                    " FRM_GENERATE_TEMPLATE
form frm_upload_data.
  data:lv_no(10) type c value '1000000000'.

  define mcr_add_zero.
    if &1 is NOT INITIAL.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input         = &1
     IMPORTING
       OUTPUT        = &1.
    endif.
  end-of-definition.

  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    exporting
      filename                = p_file
      i_begin_col             = '1'
      i_begin_row             = '2'
      i_end_col               = '7'
      i_end_row               = '9999'
    tables
      intern                  = gt_upload[]
    exceptions
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.
  if sy-subrc <> 0.
* Implement suitable error handling here
  endif.

  if gt_upload[] is initial.
    message '上传文档为空' type 'I'.
    exit.
  endif.

  loop at gt_upload assigning <fs_upload>.
    translate <fs_upload>-value to upper case.
    case <fs_upload>-col.
      when '0001'.
        gs_table-lifnr = <fs_upload>-value.
        gs_table-lifnr = |{ gs_table-lifnr alpha = in }|.
      when '0002'.
        gs_table-bukrs1 = <fs_upload>-value.
      when '0003'.
        gs_table-bukrs2  = <fs_upload>-value.
      when '0004'.
        gs_table-ekorg1 = <fs_upload>-value.
      when '0005'.
        gs_table-ekorg2 = <fs_upload>-value.
      when others.
    endcase.
    at end of row.
      append gs_table to gt_table.
      clear gs_table.
    endat.
  endloop.
***check上传的数据

endform.                    " FRM_UPLOAD_DATA
form check_filename .
  data: l_s1 type string,
        l_s2 type string.

  split p_file at '.' into l_s1 l_s2.
  translate l_s2 to upper case.
  if l_s2 <> 'XLS' and l_s2 <> 'XLSX'.
    message '文件格式不对!' type 'E'.
    stop.
  endif.
endform.

form f_alv_fieldcat .
  define append_alv_cat.
    CLEAR ls_fieldcat.
    ls_fieldcat-fieldname    = &1.
    ls_fieldcat-reptext_ddic = &2.
    APPEND ls_fieldcat TO gt_fieldcat.
  end-of-definition.

  append_alv_cat: 'LIGHT' '红绿灯',
                  'LIFNR' '供应商',
                  'BUKRS1' '模板公司代码',
                  'EKORG1' '模板公司代码',
                  'BUKRS2' '扩建公司代码',
                  'EKORG2' '扩建采购组织',
                  'MESSAGE' '错误说明'.

  ls_layout-zebra     = 'X'.     "斑马线
  ls_layout-lights_fieldname  = 'LIGHT'."指定灯列名
  ls_layout-colwidth_optimize = 'X'.
endform.
*&---------------------------------------------------------------------*
*&      Form  F_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form f_display .
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_background_id    = 'ALV_BACKGROUND'
      is_layout          = ls_layout
      i_callback_program = g_repid
      it_fieldcat        = gt_fieldcat
    tables
      t_outtab           = gt_table
    exceptions
      program_error      = 1
      others             = 2.
endform.

 

posted @ 2024-08-26 10:57  肆意点  阅读(67)  评论(0编辑  收藏  举报