mm 物料 主档 修改 运费群组 BAPI_MATERIAL_SAVEDATA

来源:互联网 发布:怎么用服务器ip做域名 编辑:程序博客网 时间:2024/05/17 02:54
*&---------------------------------------------------------------------*
*& Report ZAPP01R1101 批量修改物料主檔資料
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  zamm01r1003 message-id zapp01..

type-pools : slis.    "引用ALV 要加入這語法

tables:mara,marc,makt,mast,stpo.

*------------------------------------------------------------
data: it_fieldcat type  slis_t_fieldcat_alv. "field catalog
data: it_fieldcat1 type  slis_t_fieldcat_alv. "field catalog

data: is_variant  like disvariant.         "ALV 儲存配置
data: is_layout   type slis_layout_alv.    "ALV 格式設定 *
data: it_events   type slis_t_event.       "ALV事件
data: wa_events   like line of it_events.
data: it_list_commentary type slis_t_listheader,"ALV TOP/END OF PAGE資訊
      wa_list_commentary like line of it_list_commentary.
data: g_save(1)       type c.
data: g_exit(1)      type c.

data: g_repid like sy-repid.
*DATA: gt_events       TYPE slis_t_event.
data: gt_excluding  type slis_t_extab.
data: gt_event_exit type slis_t_event_exit.

data: col    type lvc_s_scol,
      coltab type lvc_t_scol,
      color  type lvc_s_colo.
*-----------------------------------------------------------
data:i_exl like standard table of zalsmex_tabline1 with header line .
data:it_return like bapiret2  occurs 0  with header line.

data:wa_return     like bapiret2.
data:wa_headdata   like bapimathead.

data:wa_plantdata  like bapi_marc.
data:wa_plantdatax like bapi_marcx.

data:wa_bapi_mara   like bapi_mara.
data:wa_bapi_marax  like bapi_marax.

data:wa_bapi_marm   like table of bapi_marm with header line.
data:wa_bapi_marmx  like table of bapi_marmx with header line.

data:wa_tmcnv like tmcnv.

data : begin of i_tab occurs 0,
      matnr like marc-matnr,  "料號
      werks like marc-werks,  "工廠
      mfrgr like marc-mfrgr,  "運費群組
end of i_tab.

data:g_count(7type n.
data: g_alvcnt(7type n.
data: g_totcnt(8type n.

*輸入條件

selection-screen begin of block blk1 with frame title text-001.

parameters: p_path like rlgrap-filename obligatory. "上傳BAPI的文件名稱
selection-screen end of block blk1.


* 說明部分
selection-screen begin of block comm3 with frame title text-009.
selection-screen comment /01(78text-c10.
selection-screen comment /01(78text-c11.
selection-screen comment /01(78text-c12.
selection-screen comment /01(78text-c13.
selection-screen comment /01(78text-c14.

selection-screen skip 1.
selection-screen comment /01(78text-c20.
selection-screen comment /01(78text-c21.
selection-screen skip 1.

selection-screen end of  block comm3.

initialization.


at selection-screen output.

  loop at screen.
*    if screen-name = 'P_LESS'.
**      screen-input = 0.   " disable *
*      screen-active = 0.
*      modify screen.
*    endif.

  endloop.


at selection-screen on value-request for p_path.
  perform get_filename.                  "Choose upload filename


at selection-screen.
*  PERFORM check_screen.


start-of-selection.
  perform get_init_variant .    "取得ALV 配置呈現變量 *
  perform define_fieldcat.      "定義ALV欄位設定 *

  perform get_img_material.      "取得 IMG  料號前導零設定

  perform upload_data.          "EXCEL 資料上傳
  perform check_authority.      "檢查權限

  if it_return[] is not initial"檢查權限有異常資料
    perform alv_display using 'CHK'.
  else.
    perform call_bapi_change.   "變更料號裝配廢品率
    if it_return[] is not initial.  "料號變更有異常資料
      perform alv_display using 'UPD'.
    else.

      message i000(zapp01) with text-s01.
    endif.
  endif.

end-of-selection.


  define append_selections.
    &4-sign = 'I'.
    &4-option = &1.
    &4-low = &2.
    &4-high = &3.
    append &4.
  end-of-definition.


*&---------------------------------------------------------------------*
*&      Form  get_layout_variant
*&---------------------------------------------------------------------*
*  取得 使用者 在show ALV 配置設定 *
*----------------------------------------------------------------------*
form get_init_variant .
  clear is_variant.

  is_variant-report = sy-repid.
  call function 'REUSE_ALV_VARIANT_DEFAULT_GET'
    exporting
      i_save     = 'A'
    changing
      cs_variant = is_variant
    exceptions
      not_found  = 2.
endform.                    "initialize_variant


*&---------------------------------------------------------------------*
*&      Form  get_t141 .
*&---------------------------------------------------------------------*
*       *"取得 IMG  料號前導零設定
*----------------------------------------------------------------------*
form get_img_material.

  "取得料號編碼 IMG 設定是否有前導零
  "TMCNV-LEXFLAG = ' '  --> 若料號號碼為數值存檔時前面會補零

  clear:wa_tmcnv.
  select single * into wa_tmcnv from tmcnv
    where convid = 'MATCONV'.

endform.                                                    "get_t141

*&---------------------------------------------------------------------*
*&      Form  check_authority.
*&---------------------------------------------------------------------*
*       檢查權限
*----------------------------------------------------------------------*
form check_authority.
  data: l_tabix like sy-tabix.
  data: l_count type p.

  refresh:it_return.

  describe table i_tab lines sy-tfill. " 取得 總筆數 *
  g_totcnt = sy-tfill.

  loop at i_tab.
    l_tabix = sy-tabix.

    "物料主檔:工廠
    authority-check object 'M_MATE_WRK'
             id 'ACTVT' field '02'
             id 'WERKS' field i_tab-werks.

    if sy-subrc <> 0.
      it_return-type = 'E'.
      concatenate  text-e06  i_tab-matnr text-e04
      into it_return-message.
      append it_return.  clear:it_return.
    endif.

    "--------------------------------------------------------
    " 產生進度比率小時鐘 ------------------------------------
    l_count = l_tabix / g_totcnt * 100.
    perform progress_indicator using '2-1' l_count text-t03.
    " -------------------------------------------------------

  endloop.

  sort it_return by type message.
  delete adjacent duplicates from it_return comparing type message.
  describe table it_return lines sy-tfill. " 取得 總筆數 *
  g_alvcnt = sy-tfill.

endform.                    "get_mat_status

*&---------------------------------------------------------------------*
*&      Form  call_bapi_change
*&---------------------------------------------------------------------*
*       變更料號主檔資料
*----------------------------------------------------------------------*
form call_bapi_change.
  data: l_count type p,
        g_meins like mara-meins,
        g_gewei like t006i-isocode.
  data: l_tabix like sy-tabix.

  refresh:it_return.


  loop at i_tab.
    clear:wa_headdata,wa_bapi_mara,wa_bapi_marax,
          wa_bapi_marm,wa_bapi_marmx.
    l_tabix = sy-tabix.


    wa_headdata-material     = i_tab-matnr.
    wa_headdata-purchase_view = 'X'.


    wa_plantdata-plant       = i_tab-werks.
    wa_plantdata-matfrgtgrp       = i_tab-mfrgr.
    "-------------------------------------------
    wa_plantdatax-plant      = i_tab-werks.
    wa_plantdatax-matfrgtgrp = 'X'.


    "呼叫 BAPI 進行修改料號主檔資料 -----------
    call function 'BAPI_MATERIAL_SAVEDATA'
      exporting
       headdata                   = wa_headdata
       clientdata                 = wa_bapi_mara
       clientdatax                = wa_bapi_marax
       plantdata                  = wa_plantdata
       plantdatax                 = wa_plantdatax
*   FORECASTPARAMETERS         =
*   FORECASTPARAMETERSX        =
*   PLANNINGDATA               =
*   PLANNINGDATAX              =
*   STORAGELOCATIONDATA        =
*   STORAGELOCATIONDATAX       =
*   VALUATIONDATA              =
*   VALUATIONDATAX             =
*   WAREHOUSENUMBERDATA        =
*   WAREHOUSENUMBERDATAX       =
*   SALESDATA                  =
*   SALESDATAX                 =
*   STORAGETYPEDATA            =
*   STORAGETYPEDATAX           =
*   FLAG_ONLINE                = ' '
*   FLAG_CAD_CALL              = ' '
*   NO_DEQUEUE                 = ' '
*   NO_ROLLBACK_WORK           = ' '
      importing
       return                     = wa_return
* tables
**   MATERIALDESCRIPTION        =
*   unitsofmeasure              = wa_bapi_marm
*   unitsofmeasurex             = wa_bapi_marmx
*   INTERNATIONALARTNOS        =
*   MATERIALLONGTEXT           =
*   TAXCLASSIFICATIONS         =
*   RETURNMESSAGES             =
*   PRTDATA                    =
*   PRTDATAX                   =
*   EXTENSIONIN                =
*   EXTENSIONINX               =
              .

    if sy-subrc = 0.
      call function 'BAPI_TRANSACTION_COMMIT'
*               EXPORTING
*                 WAIT          =
*               IMPORTING
*                 RETURN        = RETURN
              .
    else.
      call function 'BAPI_TRANSACTION_ROLLBACK'.
    endif.
    refresh: wa_bapi_marm,wa_bapi_marm[],wa_bapi_marmx,wa_bapi_marmx[].
    "--------------------------------------------------------
    if wa_return-type = 'E'.    "更新失敗
      move-corresponding wa_return to it_return.
      concatenate i_tab-matnr it_return-message into it_return-message.
      append it_return.
      g_alvcnt = g_alvcnt + 1"計算總筆數, show 在 ALV Title
    endif.

    " 產生進度比率小時鐘 ------------------------------------
    l_count = l_tabix / g_totcnt * 100.
    perform progress_indicator using '2-2' l_count text-t04.
    " -------------------------------------------------------

  endloop.


endform.                    "get_request_data

*&---------------------------------------------------------------------*
*&      Form  define_fieldcat
*&---------------------------------------------------------------------*
*       Field Catalog
*----------------------------------------------------------------------*
form define_fieldcat.
  data:wa_fieldcat type slis_t_fieldcat_alv with header line.
  clear:it_fieldcat,it_fieldcat[],wa_fieldcat,wa_fieldcat[].

  "從SAP DDIC 抓出 ALV 欄位標準設定  ---------------------------------------
  call function 'REUSE_ALV_FIELDCATALOG_MERGE'
    exporting
      i_program_name         = sy-repid
      i_internal_tabname     = 'IT_RETURN'   " IT_LIST 一定要大寫
      i_inclname             = sy-repid
    changing
      ct_fieldcat            = it_fieldcat
    exceptions "
      inconsistent_interface = 1
      program_error          = 2
      others                 = 3.

endform.                    "define_fieldcat


*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      Form  set_layout
*&---------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
form set_layout.
  is_layout-info_fieldname   = 'ROW_COLOR'.   "設定ALV 一整列顏色
  is_layout-coltab_fieldname = 'CELL_COLOR'.  "設定ALV  欄(cell) 顏色

endform.                    "set_layout


*&---------------------------------------------------------------------*
*&      Form  alv_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form alv_display  using l_mode.
  data:l_headdesc type lvc_title.

  if l_mode = 'CHK'.
    shift g_alvcnt left deleting leading '0' .
    concatenate text-601 g_alvcnt into l_headdesc.
  else.
    shift g_alvcnt left deleting leading '0' .
    concatenate text-602 g_alvcnt into l_headdesc.

  endif.


  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program       = sy-repid
*      i_callback_pf_status_set = 'ALV_STATUS'
*      i_callback_user_command  = 'USER_COMMAND'
      i_grid_title             = l_headdesc
      is_layout                = is_layout  "ALV 格式設定 *
      i_save                   = ' '
      is_variant               = is_variant "傳入 使用者 在show ALV 配置設定
      it_events                = it_events
      it_fieldcat              = it_fieldcat "傳入ALV Field Catalog *
      i_default                = 'X'
    tables
      t_outtab                 = it_return
    exceptions
      program_error            = 1.


endform.                    "alv_display



*&---------------------------------------------------------------------*
*&      Form  alv_status
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->EXTAB      text
*----------------------------------------------------------------------*
form alv_status using  extab type slis_t_extab.
  refresh extab.
  set pf-status 'ALV_STATUS' excluding extab.
endform.                    "alv_status


*&---------------------------------------------------------------------*
*&      Form  progress_indicator
*&---------------------------------------------------------------------*
*       產生進度比率小時鐘
*----------------------------------------------------------------------*
*      -->
*----------------------------------------------------------------------*
form progress_indicator using   l_step  vn_test vc_desc.
  data: vc_num(4), vc_text(30).

  vc_num = vn_test.
  concatenate ' Process->' l_step vc_num '%  ' vc_desc into vc_text.
  call function 'SAPGUI_PROGRESS_INDICATOR'
    exporting
      percentage = vn_test
      text       = vc_text
    exceptions
      others     = 1.
endform.                  "progress_indicator

*&---------------------------------------------------------------------*
*&      Form  get_filename
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form get_filename .
  call function 'WS_FILENAME_GET'
    exporting
      def_filename     = '*.xls'
      def_path         = 'C:\'
      mask             = ',*.xls.'
      mode             = 'O'
      title            = text-t01    "請選擇上傳的Excel文件
    importing
      filename         = p_path
    exceptions
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      others           = 5.
  if sy-subrc <> 0.
    "上傳的EXCEL路徑及檔名不正確,請重新輸入!
    message s000 with text-e01.
    stop.
  endif.
endform.                    "get_filename

*&---------------------------------------------------------------------*
*&      Form  upload_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form upload_data.
  clear:i_tab,i_tab[],i_exl,i_exl[].
  data:l_count(7type n.

  clear:l_count.
  refresh:i_tab.

  call function 'ZALSM_EXCEL_TO_INTERNAL_TABL1'
    exporting
      filename                = p_path
      i_begin_col             = 1
      i_begin_row             = 1
      i_end_col               = 3
      i_end_row               = 65535
    tables
      intern                  = i_exl
    exceptions
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.

  if sy-subrc = 0 or i_exl[] is initial..
    sort i_exl by row col.

    loop at i_exl where row >= 2.
      condense i_exl-value.
      case i_exl-col.
        when 1.
          i_tab-matnr = i_exl-value.
          if wa_tmcnv-lexflag = ''.   "IMG 設定有前導零
            call function 'CONVERSION_EXIT_ALPHA_INPUT'   "補前導零
              exporting
                input  = i_tab-matnr
              importing
                output = i_tab-matnr.
          endif.
        when 2.
          i_tab-werks = i_exl-value.

        when 3.
          i_tab-mfrgr = i_exl-value.

      endcase.

      at end of row .
        append i_tab.    clear i_tab.
        l_count = l_count + 1.
      endat.
    endloop.

  else.
    "無資料,請仔細檢查上傳的數據和文件名!
    message i000 with text-e02.
    stop.
  endif.

  if g_count > 65535.
    message i026(zapp01) with l_count.
    stop.
  endif.

  free:i_exl.
endform.                    "upload_data
0 0
原创粉丝点击