期初物料主数据批量导入程序

*&---------------------------------------------------------------------*
*& Responsibility
*&---------------------------------------------------------------------*
* Program Name:ZMMC001
* Date written:
* Author's name:
* Last update:
* Program title:
* Project Name:
* Version:
*&---------------------------------------------------------------------*
* Description: (Incl. Related Function Area and System)
*&---------------------------------------------------------------------*
*
*&---------------------------------------------------------------------*
* Change History
*&---------------------------------------------------------------------*
*     Date   |   Programmer   |   Corr. #   |   Description
*            |                |             |
REPORT zmdmc001.
*-----------------------------------------------------------------------
*  数据定义
*&---------------------------------------------------------------------*
TABLES sscrfields.
INCLUDE zs4_common.
CONSTANTS:cn_flag TYPE char1 VALUE 'X'.
DATA smp_dyntxt TYPE smp_dyntxt.
*"模板下载
DATA: gv_objid_xls           TYPE wwwdatatab-objid VALUE 'ZCOM001',
      gv_export_filename_xls TYPE string VALUE '物料主数据期初批量导入模板'.
DATA: gs_layout   TYPE lvc_s_layo,
      gs_settings TYPE lvc_s_glay.
DATA: gt_fcat TYPE lvc_t_fcat,
      gs_fcat TYPE lvc_s_fcat.
DATA: gt_sort TYPE lvc_t_sort,
      gs_sort TYPE lvc_s_sort.
DATA  lw_grid TYPE REF TO cl_gui_alv_grid.
DATA: gv_struct TYPE dd02l-tabname.
DATA: gt_alv TYPE TABLE OF zsmdm001_alv,
      gs_alv TYPE zsmdm001_alv.
DATA: gt_upload_base TYPE TABLE OF zsmdm001_base,
      gs_upload_base TYPE zsmdm001_base,
      gt_upload_sd   TYPE TABLE OF zsmdm001_sd,
      gs_upload_sd   TYPE zsmdm001_sd,
      gt_upload_pp   TYPE TABLE OF zsmdm001_pp,
      gs_upload_pp   TYPE zsmdm001_pp,
      gt_upload_pur  TYPE TABLE OF zsmdm001_pur,
      gs_upload_pur  TYPE zsmdm001_pur,
      gt_upload_sto  TYPE TABLE OF zsmdm001_sto,
      gs_upload_sto  TYPE zsmdm001_sto,
      gt_upload_fi   TYPE TABLE OF zsmdm001_fi,
      gs_upload_fi   TYPE zsmdm001_fi,
      gt_upload_qm   TYPE TABLE OF zsmdm001_qm,
      gs_upload_qm   TYPE zsmdm001_qm.
DATA: gs_head    TYPE bapimathead,
      gt_makt    TYPE STANDARD TABLE OF bapi_makt WITH HEADER LINE,
      gt_unit    TYPE STANDARD TABLE OF bapi_marm WITH HEADER LINE, "单位
      gt_unitx   TYPE TABLE OF bapi_marmx WITH HEADER LINE, "单位
      gt_mlan    TYPE TABLE OF bapi_mlan WITH HEADER LINE,
      gs_mara    TYPE bapi_mara,
      gs_marc    TYPE bapi_marc,
      gs_sales   TYPE bapi_mvke,
      gs_mbew    TYPE bapi_mbew,
      gs_marax   TYPE bapi_marax,
      gs_marcx   TYPE bapi_marcx,
      gs_salesx  TYPE bapi_mvkex,
      gs_mbewx   TYPE bapi_mbewx,
      gt_return  TYPE STANDARD TABLE OF bapi_matreturn2 WITH HEADER LINE,
      gs_ret     TYPE bapiret2,
      gs_bapi    TYPE bapi_te_mara,
      gs_bapix   TYPE bapi_te_marax,
      gt_extend  TYPE STANDARD TABLE OF bapiparex WITH HEADER LINE,
      gt_extendx TYPE STANDARD TABLE OF bapiparexx WITH HEADER LINE.
*-----------------------------------------------------------------------
*  选择屏幕
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS:p_1 RADIOBUTTON GROUP rg1 MODIF ID m1 USER-COMMAND uc DEFAULT 'X', "批量创建
           p_2 RADIOBUTTON GROUP rg1 MODIF ID m1. "批量修改
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
PARAMETERS: p_3 TYPE char1 AS CHECKBOX MODIF ID m1 USER-COMMAND uc2,
            p_4 TYPE char1 AS CHECKBOX MODIF ID m2 USER-COMMAND uc2,
            p_5 TYPE char1 AS CHECKBOX MODIF ID m2 USER-COMMAND uc2,
            p_6 TYPE char1 AS CHECKBOX MODIF ID m2 USER-COMMAND uc2,
            p_7 TYPE char1 AS CHECKBOX MODIF ID m2 USER-COMMAND uc2,
            p_8 TYPE char1 AS CHECKBOX MODIF ID m2 USER-COMMAND uc2,
            p_9 TYPE char1 AS CHECKBOX MODIF ID m2 USER-COMMAND uc2.
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-003.
PARAMETERS: p_upload TYPE rlgrap-filename MODIF ID m4.
SELECTION-SCREEN END OF BLOCK b3.

SELECTION-SCREEN COMMENT /1(30) TEXT-004.
SELECTION-SCREEN COMMENT /1(40) TEXT-005.

*-----------------------------------------------------------------------
* 屏幕初始化,设置图标和文本
*&---------------------------------------------------------------------*
INITIALIZATION.
  smp_dyntxt-icon_id   = icon_table_settings .
  smp_dyntxt-quickinfo = 'Preselected Carrier'.
  smp_dyntxt-icon_text = '下载导入模板'.
  sscrfields-functxt_01 = smp_dyntxt.

*-----------------------------------------------------------------------
* 文本框选择
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upload.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'P_UPLOAD'
    IMPORTING
      file_name  = p_upload.

*-----------------------------------------------------------------------
* 选择屏幕下载事件
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.

  CASE sscrfields-ucomm.

    WHEN 'FC01'.
      PERFORM frm_download_xls_template USING  gv_objid_xls
                                               gc_file_type_xls
                                               gv_export_filename_xls.
    WHEN 'UC2'.
      IF p_3 = 'X'.
        IF p_4 = 'X'
          OR p_5 = 'X'
           OR p_6 = 'X'
             OR p_7 = 'X'
               OR p_8 = 'X'
                 OR p_9 = 'X'.
          MESSAGE e001(zmdmc001).
        ENDIF.
      ENDIF.
  ENDCASE.

*-----------------------------------------------------------------------
* 程序入口
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  IF p_3 IS INITIAL
     AND p_4 IS INITIAL
      AND p_5 IS INITIAL
        AND p_6 IS INITIAL
          AND p_7 IS INITIAL
            AND p_8 IS INITIAL
              AND p_9 IS INITIAL.
    MESSAGE s003(zmdmc001) DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.


  IF p_upload IS INITIAL.
    MESSAGE s002(zmdmc001) DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  PERFORM frm_upload_excel.
  PERFORM frm_mantain_data..
  IF p_3 = 'X'.
    PERFORM frm_create_material.
  ELSE.
    PERFORM frm_add_material_view.
  ENDIF.

  PERFORM frm_show_result.
*&---------------------------------------------------------------------*
*& 选择屏幕输入检查
*&---------------------------------------------------------------------*
FORM frm_input_check.




ENDFORM.
*&---------------------------------------------------------------------*
*& 从EXCEL里导入数据
*&---------------------------------------------------------------------*
FORM frm_upload_excel .

  DATA: lt_tab TYPE TABLE OF alsmex_tabline WITH HEADER LINE,
        ls_tab TYPE alsmex_tabline.
  DATA: l_str   TYPE string.
  DATA: l_no    TYPE i VALUE 1,
        l_char  TYPE char4 VALUE '0001',
        l_tabix LIKE sy-tabix.
  DATA: ls_upload_base TYPE zsmdm001_base.
  DATA: l_new_row TYPE kcd_ex_row_n.


  FIELD-SYMBOLS:<fs> TYPE any.
*->基本视图导入
  IF p_3 = 'X'.
*从EXCEL中上载数据
    CLEAR lt_tab[].

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 255
        i_end_row   = 65536
        sheet_name  = '基本视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT lt_tab-col OF STRUCTURE gs_upload_base TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_base TO gt_upload_base.
        CLEAR:gs_upload_base,
              l_tabix.
      ENDAT.
    ENDLOOP.
  ENDIF.

  IF p_4 = 'X'.
    CLEAR lt_tab[].

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 50
        i_end_row   = 3000
        sheet_name  = '销售视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT sy-tabix OF STRUCTURE gs_upload_sd TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_sd TO gt_upload_sd.
        CLEAR gs_upload_sd.
      ENDAT.
    ENDLOOP.
  ENDIF.


  IF p_5 = 'X'.
    CLEAR lt_tab[].

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 50
        i_end_row   = 3000
        sheet_name  = '生产视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT lt_tab-col OF STRUCTURE gs_upload_pp TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_pp TO gt_upload_pp.
        CLEAR gs_upload_pp.
      ENDAT.
    ENDLOOP.
  ENDIF.

  IF p_6 = 'X'.
    CLEAR lt_tab[].

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 50
        i_end_row   = 3000
        sheet_name  = '采购视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT lt_tab-col OF STRUCTURE gs_upload_pur TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_pur TO gt_upload_pur.
        CLEAR gs_upload_pur.
      ENDAT.
    ENDLOOP.
  ENDIF.

  IF p_7 = 'X'.
    CLEAR lt_tab[].

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 50
        i_end_row   = 3000
        sheet_name  = '存储视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT lt_tab-col OF STRUCTURE gs_upload_sto TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_sto TO gt_upload_sto.
        CLEAR gs_upload_sto.
      ENDAT.
    ENDLOOP.
  ENDIF.


  IF p_8 = 'X'.
    CLEAR lt_tab[].

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 50
        i_end_row   = 3000
        sheet_name  = '财务视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT lt_tab-col OF STRUCTURE gs_upload_fi TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_fi TO gt_upload_fi.
        CLEAR gs_upload_fi.
      ENDAT.
    ENDLOOP.
  ENDIF.

  IF p_9 = 'X'.

    CLEAR lt_tab[].
    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename    = p_upload
        i_begin_col = 2
        i_begin_row = 7
        i_end_col   = 50
        i_end_row   = 3000
        sheet_name  = '质量视图'
      TABLES
        intern      = lt_tab[].

    LOOP AT lt_tab.
      CONDENSE lt_tab-value.
      ASSIGN COMPONENT lt_tab-col OF STRUCTURE gs_upload_qm TO <fs>.         "动态方法将值传到相应的内表
      IF <fs> IS ASSIGNED.
        MOVE lt_tab-value TO <fs>.
        UNASSIGN <fs>.
      ENDIF.
      AT END OF row.
        APPEND gs_upload_qm TO gt_upload_qm.
        CLEAR gs_upload_qm.
      ENDAT.
    ENDLOOP.
  ENDIF.


ENDFORM.
*&---------------------------------------------------------------------*
*& 创建物料主数据
*&---------------------------------------------------------------------*
FORM frm_create_material .

  DATA:l_str   TYPE string,
       l_meins TYPE meins.

  LOOP AT gt_alv INTO gs_alv.
*->判断导入视图的种类

    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = gs_alv-matnr
      IMPORTING
        output       = gs_head-material
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.


    SELECT COUNT(*) FROM mara WHERE matnr = gs_alv-matnr.
    IF sy-subrc EQ 0.
      gs_alv-base_info = TEXT-006.
      CONTINUE.
    ENDIF.

    READ TABLE gt_upload_base INTO gs_upload_base WITH KEY matnr = gs_alv-matnr.

    gs_head-ind_sector = gs_upload_base-mbrsh.
    gs_head-matl_type  = gs_upload_base-mtart.
    gs_head-basic_view = 'X'.

    gs_mara-matl_group     = gs_upload_base-matkl.
*->单位转换
    PERFORM frm_unit_change USING gs_upload_base-meins CHANGING l_meins.
    gs_mara-base_uom       = l_meins.
    gs_mara-base_uom_iso   = l_meins.
    gs_mara-po_unit        = l_meins.
    gs_mara-old_mat_no     = gs_upload_base-bismt.
    gs_mara-net_weight     = gs_upload_base-ntgew.
    gs_mara-unit_of_wt     = gs_upload_base-gewei.
    gs_mara-unit_of_wt_iso = gs_upload_base-gewei.
    gs_mara-std_descr      = gs_upload_base-ean11.

    gs_marax-matl_group = cn_flag.
    gs_marax-po_unit    = cn_flag.
    gs_marax-base_uom   = cn_flag.
    gs_marax-base_uom_iso   = cn_flag.
    gs_marax-old_mat_no      = cn_flag.
    gs_marax-net_weight     = cn_flag.
    gs_marax-unit_of_wt     = cn_flag.
    gs_marax-unit_of_wt_iso = cn_flag.
    gs_marax-std_descr  = cn_flag.

    gt_makt-langu = '1'.
    gt_makt-langu_iso = 'ZH'.
    gt_makt-matl_desc = gs_upload_base-maktx.
    APPEND gt_makt.

    "单位
*    CLEAR gt_unit[].
*    gt_unit-gross_wt   = gs_upload_base-brgew.
*    gt_unit-unit_of_wt = gs_upload_base-gewei.
**    gt_unit-gross_wt = wa_y_material-t_marm_gross_wt.
**    gt_unit-unit_of_wt = wa_y_material-t_marm_unit_of_wt.
**    gt_unit-volume = wa_y_material-t_marm_volume.
*    gt_unit-volumeunit = l_meins.
*    APPEND gt_unit.
*
*    CLEAR gt_unitx[].
*    gt_unitx-gross_wt   = 'X'.
*    gt_unitx-unit_of_wt = 'X'.
*    gt_unitx-volumeunit = 'X'.
*    APPEND gt_unitx.

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        headdata            = gs_head
        clientdata          = gs_mara
        clientdatax         = gs_marax
        plantdata           = gs_marc
        plantdatax          = gs_marcx
        valuationdata       = gs_mbew
        valuationdatax      = gs_mbewx
      IMPORTING
        return              = gs_ret
      TABLES
        materialdescription = gt_makt
        unitsofmeasure      = gt_unit[]
        unitsofmeasurex     = gt_unitx[]
        returnmessages      = gt_return
        extensionin         = gt_extend[]
        extensioninx        = gt_extendx[].

    READ TABLE gt_return WITH KEY type = 'E'.
    IF sy-subrc NE 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
      gs_alv-base_info = 'X'.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      CLEAR l_str.
      LOOP AT gt_return WHERE type = 'E'.
        IF l_str IS INITIAL.
          l_str = gt_return-message.
        ELSE.
          CONCATENATE l_str gt_return-message '/' INTO l_str.
        ENDIF.
      ENDLOOP.
      gs_alv-message = l_str.
    ENDIF.
    MODIFY gt_alv FROM gs_alv.
    CLEAR:gt_return[],
          l_str.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& 返回输出结构
*&---------------------------------------------------------------------*
FORM frm_show_result .

  gs_layout-cwidth_opt = 'X'.
  gs_layout-sel_mode   = 'D'."只能选择单行
  gv_struct = 'ZSMDM001_ALV'.

  PERFORM frm_get_fieldcat_via_fm   USING    gv_struct
                                    CHANGING gt_fcat.

  LOOP AT gt_fcat INTO DATA(wa_fcat).
    CASE wa_fcat-fieldname.
      WHEN 'SEL'.
        wa_fcat-checkbox = 'X'.
        wa_fcat-edit = 'X'.
    ENDCASE.
    MODIFY gt_fcat FROM wa_fcat INDEX sy-tabix.
  ENDLOOP.

  DATA:gt_events TYPE  slis_t_event,
       ls_event  TYPE  slis_alv_event.

  DATA:l_grid_settings TYPE lvc_s_glay.
  l_grid_settings-edt_cll_cb = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'PF_STATUS_SET'
      i_callback_user_command  = 'USER_COMMAND'
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fcat[]
      i_save                   = 'A'
      it_events                = gt_events
      i_grid_settings          = l_grid_settings
    TABLES
      t_outtab                 = gt_alv[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.


ENDFORM.

*&---------------------------------------------------------------------*
*       设置GUI状态(CPOY SAPLKKBL)
*----------------------------------------------------------------------*
FORM pf_status_set USING extab TYPE slis_t_extab.
  SET PF-STATUS 'ZMDMC001_STATUS'.
ENDFORM.

*&---------------------------------------------------------------------*
*&      FORM  USER_COMMAND
*&---------------------------------------------------------------------*
*       处理ALV指令
*----------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield.

  DATA : lt_row TYPE  lvc_t_roid,    "内表
         ls_row TYPE  lvc_s_roid.    "工作区
  DATA:  alv_grid TYPE REF TO cl_gui_alv_grid.

  CASE r_ucomm.
    WHEN '&F03' OR '&F05' OR '&F12'.
      LEAVE TO SCREEN 0.
  ENDCASE.

  rs_selfield-refresh = 'X'.

ENDFORM.
*&---------------------------------------------------------------------*
*&补充物料视图
*&---------------------------------------------------------------------*
FORM frm_add_material_view .

  DATA:l_str TYPE string.


  LOOP AT gt_alv INTO gs_alv.
*->判断导入视图的种类
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = gs_alv-matnr
      IMPORTING
        output       = gs_head-material
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.


    SELECT COUNT(*) FROM mara WHERE matnr = gs_alv-matnr.
    IF sy-subrc EQ 0.
      gs_alv-base_info = TEXT-006.
      CONTINUE.
    ENDIF.

    SELECT SINGLE * FROM mara WHERE matnr = @gs_head-material INTO @DATA(wa_mara).

*->基础相关
    gs_head-ind_sector   = wa_mara-mbrsh."行业领域
    gs_head-matl_type    = wa_mara-mtart.

    IF p_4 = 'X'."销售视图
      gs_head-sales_view = 'X'.

      READ TABLE gt_upload_sd INTO gs_upload_sd WITH KEY matnr = gs_alv-matnr.
      gs_sales-sales_org        =  gs_upload_sd-vkorg.
      gs_sales-distr_chan       =  gs_upload_sd-vtweg.
      gs_sales-delyg_plnt       =  gs_upload_sd-werks.
      gs_sales-sales_unit       =  gs_upload_sd-vrkme.
      gs_sales-sales_unit_iso   =  gs_upload_sd-vrkme.
      gs_sales-delyg_plnt       =  gs_upload_sd-dwerk.
      gs_sales-matl_stats       =  gs_upload_sd-versg.
      gs_sales-acct_assgt       =  gs_upload_sd-ktgrm.
      gs_sales-item_cat         =  gs_upload_sd-mtpos.
      gs_salesx-sales_org        =  gs_upload_sd-vkorg.
      gs_salesx-distr_chan       =  gs_upload_sd-vtweg.
      gs_salesx-delyg_plnt       =  cn_flag.
      gs_salesx-sales_unit       =  cn_flag.
      gs_salesx-sales_unit_iso   =  cn_flag.
      gs_salesx-delyg_plnt       =  cn_flag.
      gs_salesx-matl_stats       =  cn_flag.
      gs_salesx-acct_assgt       =  cn_flag.
      gs_salesx-item_cat         =  cn_flag.

*->可用性检查
      gs_marc-plant         = gs_upload_sd-werks."工厂
      gs_marc-availcheck    = gs_upload_sd-mtvfp."差异码
      gs_marcx-plant        = gs_upload_sd-werks.
      gs_marcx-availcheck   = cn_flag."差异码

      gt_mlan-depcountry         = gs_upload_sd-aland.
      gt_mlan-tax_type_1         = gs_upload_sd-tatyp.
      gt_mlan-taxclass_1         = gs_upload_sd-taxkm.
      APPEND gt_mlan.
    ENDIF.

    IF p_5 = 'X'.
      gs_head-mrp_view        = 'X'.
      gs_head-work_sched_view = 'X'.

      READ TABLE gt_upload_pp INTO gs_upload_pp WITH KEY matnr = gs_alv-matnr.
      gs_marc-plant      =  gs_upload_pp-werks. "工厂
      gs_marc-mrp_type   =  gs_upload_pp-dismm."MRP类型
      gs_marc-mrp_ctrler =  gs_upload_pp-dispo.
      gs_marc-lotsizekey =  gs_upload_pp-disls.
      gs_marc-minlotsize =  gs_upload_pp-bstmi.
      gs_marc-maxlotsize =  gs_upload_pp-bstma.
      gs_marc-mrp_group  =  gs_upload_pp-disgr.
      gs_marc-reorder_pt =  gs_upload_pp-minbe.
      gs_marc-proc_type  =  gs_upload_pp-beskz.
      gs_marc-sm_key     =  gs_upload_pp-fhori.
      gs_marc-spproctype =  gs_upload_pp-sobsl.
      gs_marc-inhseprodt =  gs_upload_pp-dzeit.
      gs_marc-plnd_delry =  gs_upload_pp-plifz.
      gs_marc-gr_pr_time =  gs_upload_pp-webaz.
      gs_marc-backflush  =  gs_upload_pp-rgekz.
      gs_marc-safety_stk =  gs_upload_pp-eisbe.
      gs_marc-min_safety_stk = gs_upload_pp-eislo.
      gs_marc-iss_st_loc = gs_upload_pp-lgpro."生产存储地点
      gs_marc-sloc_exprc = gs_upload_pp-lgfsb."外部采购存储地点
      gs_marc-plan_strgp = gs_upload_pp-strgr."策略组
      gs_marc-consummode = gs_upload_pp-vrmod."消耗模式
      gs_marc-fwd_cons   = gs_upload_pp-vint2."向前消耗期间
      gs_marc-availcheck = gs_upload_pp-mtvfp."可用性检查
      gs_marc-dep_req_id = gs_upload_pp-sbdkz."独立/集中
      gs_marc-production_scheduler = gs_upload_pp-fevor."生产管理员
      gs_marc-prodprof   = gs_upload_pp-sfcpf."生产计划参数文件
      gs_marc-unlimited  = gs_upload_pp-ueetk."无限制过量交货
      gs_marc-batch_mgmt = gs_upload_pp-xchpf."批次需求管理
      gs_marc-under_tol  = gs_upload_pp-uneto."不足交货允差
      gs_marc-over_tol   = gs_upload_pp-ueeto."过度交货允差

      gs_marcx-plant      =  gs_upload_pp-werks. "工厂
      gs_marcx-mrp_type   =  cn_flag."MRP类型
      gs_marcx-mrp_ctrler =  cn_flag.
      gs_marcx-lotsizekey =  cn_flag.
      gs_marcx-minlotsize =  cn_flag.
      gs_marcx-maxlotsize =  cn_flag.
      gs_marcx-mrp_group  =  cn_flag.
      gs_marcx-reorder_pt =  cn_flag.
      gs_marcx-proc_type  =  cn_flag.
      gs_marcx-sm_key     =  cn_flag.
      gs_marcx-spproctype =  cn_flag.
      gs_marcx-inhseprodt =  cn_flag.
      gs_marcx-plnd_delry =  cn_flag.
      gs_marcx-gr_pr_time =  cn_flag.
      gs_marcx-backflush  =  cn_flag.
      gs_marcx-safety_stk =  cn_flag.
      gs_marcx-min_safety_stk = cn_flag.
      gs_marcx-iss_st_loc = cn_flag.
      gs_marcx-sloc_exprc = cn_flag.
      gs_marcx-plan_strgp = cn_flag.
      gs_marcx-consummode = cn_flag.
      gs_marcx-fwd_cons   = cn_flag.
      gs_marcx-availcheck = cn_flag.
      gs_marcx-dep_req_id = cn_flag.
      gs_marcx-production_scheduler = cn_flag.
      gs_marcx-prodprof   = cn_flag.
      gs_marcx-unlimited  = cn_flag.
      gs_marcx-batch_mgmt = cn_flag.
      gs_marcx-under_tol  = cn_flag.
      gs_marcx-over_tol   = cn_flag.
    ENDIF.

*->采购视图
    IF p_6 = 'X'.
      gs_head-purchase_view = cn_flag.
      gs_head-mrp_view = cn_flag.

      gs_marc-plant         = gs_upload_pur-werks.
      gs_marc-pur_group     = gs_upload_pur-ekgrp.
      gs_mara-po_unit       = gs_upload_pur-bstme.
      gs_mara-po_unit_iso   = gs_upload_pur-bstme.
      gs_marcx-plant        = gs_upload_pur-werks.
      gs_marcx-pur_group    = cn_flag.
      gs_marax-po_unit      = cn_flag.
      gs_marax-po_unit_iso  = cn_flag.
*      gs_mara-QUAL_DIK           = gs_upload_pur-
    ENDIF.

*->仓储视图
    IF p_7 = 'X'.
      gs_head-storage_view = 'X'.
      gs_head-warehouse_view = 'X'.

      gs_mara-batch_mgmt = gs_upload_sto-xchpf."是否启用批次管理
      gs_mara-minremlife = gs_upload_sto-mhdrz."最小货价寿命
      gs_mara-shelf_life = gs_upload_sto-mhdhb."最大货价寿命

      gs_marax-batch_mgmt = cn_flag.
      gs_marax-minremlife = cn_flag.
      gs_marax-shelf_life = cn_flag.
    ENDIF.

*->财务视图
    IF p_8  = 'X'.
      gs_head-account_view   = 'X'.
      gs_head-cost_view = 'X'.
*->会计视图相关
      gs_mbew-val_area     = gs_upload_fi-bwkey. "估价范围
      gs_mbew-val_type     = gs_upload_fi-bwtar."评估类型
      gs_mbew-val_class    = gs_upload_fi-bklas."评估类
      gs_mbew-val_cat      = gs_upload_fi-bwtty."评估类别
      gs_mbew-ml_settle    = gs_upload_fi-mlast."物料价格确定
      gs_mbew-price_ctrl   = gs_upload_fi-vprsv. "价格控制指示符
      gs_mbew-price_unit   = gs_upload_fi-peinh."价格单位
      gs_mbew-vm_p_stock   = gs_upload_fi-qklas."项目库存的评估分类
      gs_mbew-orig_group   = gs_upload_fi-hrkft."作为成本要素自组的原始组
      gs_mbew-plndprice1   = gs_upload_fi-zplp1."未来计划价格1
      gs_mbew-plndprdate1  = gs_upload_fi-zpld1."未来计划价格1的生效时间
      gs_mbewx-val_area    = gs_upload_fi-bwkey. "估价范围
      gs_mbewx-val_type    = gs_upload_fi-bwtar."评估类型
      gs_mbewx-val_class   = cn_flag."评估类
      gs_mbewx-val_cat     = cn_flag."评估类别
      gs_mbewx-ml_settle   = cn_flag."物料价格确定
      gs_mbewx-price_ctrl  = cn_flag. "价格控制指示符
      gs_mbewx-price_unit  = cn_flag."价格单位
      gs_mbewx-vm_p_stock  = cn_flag."项目库存的评估分类
      gs_mbewx-orig_group  = cn_flag."作为成本要素自组的原始组
      gs_mbewx-plndprice1  = cn_flag."未来计划价格1
      gs_mbewx-plndprdate1 = cn_flag."未来计划价格1的生效时间

      gs_marc-plant         = gs_upload_fi-bwkey."工厂
      gs_marc-variance_key  = gs_upload_fi-awsls."差异码
      gs_marcx-plant        = gs_upload_fi-bwkey.
      gs_marcx-variance_key = cn_flag."差异码

    ENDIF.


*->质量视图
    IF p_9 = 'X'.
      gs_head-quality_view = cn_flag.

      gs_marc-plant     = gs_upload_qm-werks.
      gs_marc-insp_int  = gs_upload_qm-prfrq.
      gs_marcx-plant    = gs_upload_qm-werks.
      gs_marcx-insp_int = cn_flag.

    ENDIF.

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        headdata            = gs_head
        salesdata           = gs_sales
        salesdatax          = gs_salesx
        clientdata          = gs_mara
        clientdatax         = gs_marax
        plantdata           = gs_marc
        plantdatax          = gs_marcx
        valuationdata       = gs_mbew
        valuationdatax      = gs_mbewx
      IMPORTING
        return              = gs_ret
      TABLES
        materialdescription = gt_makt
        unitsofmeasure      = gt_unit[]
        unitsofmeasurex     = gt_unitx[]
        taxclassifications  = gt_mlan[]
        returnmessages      = gt_return
        extensionin         = gt_extend[]
        extensioninx        = gt_extendx[].

    READ TABLE gt_return WITH KEY type = 'E'.
    IF sy-subrc NE 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
      IF p_3 = 'X'.
        gs_alv-base_info = cn_flag.
      ENDIF.
      IF p_4 = 'X'.
        gs_alv-sd_info = cn_flag.
      ENDIF.
      IF p_5 = 'X'.
        gs_alv-pp_info = cn_flag.
      ENDIF.
      IF p_6 = 'X'.
        gs_alv-pur_info = cn_flag.
      ENDIF.
      IF p_7 = 'X'.
        gs_alv-st_info = cn_flag.
      ENDIF.
      IF p_8 = 'X'.
        gs_alv-fi_info = cn_flag.
      ENDIF.
      IF p_9 = 'X'.
        gs_alv-qm_info = cn_flag.
      ENDIF.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      LOOP AT gt_return WHERE type = 'E'.
        IF l_str IS INITIAL.
          l_str = gt_return-message.
        ELSE.
          CONCATENATE l_str gt_return-message '/' INTO l_str.
        ENDIF.
      ENDLOOP.
      gs_alv-message = l_str.
    ENDIF.
    gs_alv-itemno = sy-tabix.
    MODIFY gt_alv FROM gs_alv.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*&处理数据
*&---------------------------------------------------------------------*
FORM frm_mantain_data .

  LOOP AT gt_upload_base INTO gs_upload_base.
    gs_alv-matnr = gs_upload_base-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  LOOP AT gt_upload_sd INTO gs_upload_sd.
    gs_alv-matnr = gs_upload_sd-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  LOOP AT gt_upload_pp INTO gs_upload_pp.
    gs_alv-matnr = gs_upload_pp-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  LOOP AT gt_upload_qm INTO gs_upload_qm.
    gs_alv-matnr = gs_upload_qm-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  LOOP AT gt_upload_fi INTO gs_upload_fi.
    gs_alv-matnr = gs_upload_fi-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  LOOP AT gt_upload_pur INTO gs_upload_pur.
    gs_alv-matnr = gs_upload_pur-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  LOOP AT gt_upload_sto INTO gs_upload_sto.
    gs_alv-matnr = gs_upload_sto-matnr.
    APPEND gs_alv TO gt_alv.
  ENDLOOP.

  SORT gt_alv BY matnr.
  DELETE ADJACENT DUPLICATES FROM gt_alv.

ENDFORM.
*&---------------------------------------------------------------------*
*&单位转换
*&---------------------------------------------------------------------*
FORM frm_unit_change USING unit1 CHANGING unit2.

  SELECT SINGLE msehi INTO unit2 FROM t006a WHERE spras = 1 AND mseh3 = unit1.
  IF sy-subrc <> 0.
    unit2 = unit1.
  ENDIF.

ENDFORM.

 S/4 项目上写的物料主数据批导,初步测试可用,写得比较着急,懒得整理了,以后完善。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值