批量修改物料主档

ZSAPFIELDT

 

 

ZT002 

  ZPLMFIELD

 

 ZSAPFIELD

 

 ZPLMFIELD、ZSAPFIELD 导入附件

链接:https://pan.baidu.com/s/16W4Cd6-_Ad2JLBbXPYfYpg 

提取码:twa9

************************************************************************
* 程 序 名:ZMMR021
* 程序描述:批量修改物料主档
* 事务代码:ZMM021
************************************************************************
* 修改日志
************************************************************************
* 日期     版本 修改人       描述
* -------- ---- ------------ -------------------------------------------
* 20230101 1.0  Amell        创建程序

REPORT  ZMMR021  NO STANDARD PAGE HEADING MESSAGE-ID 00.

************************************************************************
*     Data Definition
************************************************************************
TABLES : mara,sscrfields,zsapfield.

TYPES:BEGIN OF ty_zsapfield,
        check   TYPE c,
        idkopos TYPE i.
    INCLUDE TYPE zsapfieldt.
TYPES  END OF ty_zsapfield.


TYPES:BEGIN OF ty_field,
        tabname      TYPE tabname,
        fieldname    TYPE fieldname, "新栏位名称
        fieldname2   TYPE fieldname, "原始栏位名称
        keyflag      TYPE keyflag,
        statm        TYPE statm, "view 维护记录
        langu        TYPE c, "栏位存在多语系标识
        logo         TYPE c, "栏位检查标识
        col          TYPE zkcd_ex_row_n, "列编号
        headertxt    TYPE char100, "栏位说明
        funtablename TYPE tabname, "funtable
        funfieldname TYPE fieldname, "funfield
      END OF ty_field,
      BEGIN OF ty_extendsettings,"PLM 扩展字段栏位名
        propertycode TYPE c LENGTH 200, "PLM 扩展字段
        extendname   TYPE c LENGTH 200, "PLM 扩展字段名称
        tabname      TYPE c LENGTH 20, "PLM 表名
        fieldname    TYPE c LENGTH 20, "PLM 修改的栏位
        values       TYPE c LENGTH 200, "PLM 修改的栏位值
        matnr        TYPE matnr, "PLM物料
      END OF ty_extendsettings,
      BEGIN OF ty_title, "csv 上传表头
        value(3000),
      END OF ty_title.


"扩展栏位
DATA:BEGIN OF gt_expfield OCCURS 0,
       tabname   TYPE tabname,
       fieldname TYPE fieldname, "栏位名称
       position  TYPE position, "栏位位置
       keyflag   TYPE keyflag, "主键
       leng      TYPE ddleng, "长度
     END OF gt_expfield.

"扩展栏位值
DATA:BEGIN OF gt_expvalue OCCURS 0,
       tabname   TYPE tabname,
       fieldname TYPE fieldname, "栏位名称
       value     TYPE string, "
     END OF gt_expvalue.

"多语系栏位
DATA:BEGIN OF gt_t002 OCCURS 0,
       spras      TYPE spras, "语系编号
       laiso      TYPE laiso, "语系代码
       sptxt      TYPE sptxt, "语系说明
       tabname    TYPE tabname, "
       fieldname  TYPE fieldname, "新栏位名称
       fieldname2 TYPE fieldname, "原始栏位名称
     END OF gt_t002.

"监视视图存放表
DATA:BEGIN OF gt_dd03l OCCURS 0,
       tabname   TYPE tabname,
       fieldname TYPE fieldname,
     END OF  gt_dd03l.

*--->动态表变量
FIELD-SYMBOLS:<gt_table_d> TYPE STANDARD TABLE,
              <gt_mathead>,
              <gt_mara>,<gt_marax>,
              <gt_mara2>,<gt_marax2>,
              <gt_marc>,<gt_marcx>,
              <gt_mpop>,<gt_mpopx>,
              <gt_mvke>,<gt_mvkex>,
              <gt_mbew>,<gt_mbewx>,
              <gt_mard>,<gt_mardx>,
              <g_wa>,<g_field>.


DATA : gt_bapimathead     LIKE bapimathead,
       gt_bapi_mara       LIKE bapi_mara,
       gt_bapi_marax      LIKE bapi_marax,
       gt_bapi_marc       LIKE bapi_marc,
       gt_bapi_marcx      LIKE bapi_marcx,
       gt_bapi_mpop       LIKE bapi_mpop,
       gt_bapi_mpopx      LIKE bapi_mpopx,
       gt_bapi_mard       LIKE bapi_mard,
       gt_bapi_mardx      LIKE bapi_mardx,
       gt_bapi_mbew       LIKE bapi_mbew,
       gt_bapi_mbewx      LIKE bapi_mbewx,
       gt_bapi_mvke       LIKE bapi_mvke,
       gt_bapi_mvkex      LIKE bapi_mvkex,
       gt_bapiret2        LIKE bapiret2,     " Error Log
       gt_bapi_te_mara2   TYPE bapi_te_mara2
                                 OCCURS 0 WITH HEADER LINE,
       gt_bapi_te_mara2x  TYPE bapi_te_mara2x
                               OCCURS 0 WITH HEADER LINE,
       gt_bapi_te_marc    TYPE bapi_te_marc,
       gt_bapi_te_marcx   TYPE bapi_te_marcx,
       gt_extensionin     TYPE STANDARD TABLE OF bapiparex,
       gs_extensionin     LIKE LINE OF gt_extensionin,
       gt_extensioninx    TYPE STANDARD TABLE OF bapiparexx,
       gs_extensioninx    LIKE LINE OF gt_extensioninx,
       gt_bapi_makt       LIKE bapi_makt  OCCURS 1 WITH HEADER LINE,
       gt_bapi_matreturn2 LIKE bapi_matreturn2 OCCURS 1 WITH
                        HEADER LINE,
       gt_bapi_marm       LIKE bapi_marm  OCCURS 1 WITH HEADER LINE,
       gt_bapi_marmx      LIKE bapi_marmx OCCURS 1 WITH HEADER LINE,
       gt_bapi_mean       LIKE bapi_mean OCCURS 1 WITH HEADER LINE,
       gt_bapi_mlan       LIKE bapi_mlan  OCCURS 1 WITH HEADER LINE,
       gt_returnmessages  LIKE bapi_matreturn2 OCCURS 0
                                           WITH HEADER LINE.


DATA: gt_table_d        TYPE REF TO data,
      gt_structure      TYPE lvc_t_fcat,
      gt_field          TYPE STANDARD TABLE OF ty_field,
      wa_field          TYPE ty_field,
      gt_extendsettings TYPE TABLE OF ty_extendsettings,
      wa_extendsettings TYPE ty_extendsettings,
      gt_zsapfield      TYPE TABLE OF ty_zsapfield,
      gt_zplmfield      TYPE STANDARD TABLE OF zplmfield,
      gt_title1         TYPE TABLE OF ty_title, "csv 上传中第一行表头
      gt_title2         TYPE TABLE OF ty_title, "csv 上传中第二行表头
      gt_title3         TYPE TABLE OF ty_title, "csv 上传中第三行表头
      gt_title5         TYPE TABLE OF ty_title, "csv 上传中第五行表头
      gt_title6         TYPE TABLE OF ty_title, "csv 上传中第六行表头
      gt_value          TYPE TABLE OF ty_title. "存储一行 csv 的表身值

DATA:g_success(5),
     g_fail(5).

DATA:  lr_fieldname TYPE RANGE OF dd03l-fieldname.

************************************************************************
* Data Definitions
************************************************************************
TYPE-POOLS: slis.

DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      wa_layout   TYPE slis_layout_alv.


************************************************************************
*     Selection-Screen
************************************************************************
*     Remark
SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE TEXT-001.

*     上載檔案路徑及名稱
PARAMETER: p_file LIKE rlgrap-filename OBLIGATORY  DEFAULT 'Z:\'.


*Remark
*SELECTION-SCREEN SKIP 1.
*SELECTION-SCREEN BEGIN OF LINE.
*SELECTION-SCREEN COMMENT (70) TEXT-002.
*SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK blk.


SELECTION-SCREEN: FUNCTION KEY 1,FUNCTION KEY 2,
                  FUNCTION KEY 3,FUNCTION KEY 4,FUNCTION KEY 5.

INITIALIZATION.
  sscrfields-functxt_01 = '下载导入模板'."'DISPLAY'.


AT SELECTION-SCREEN.

  CASE sscrfields-ucomm.
    WHEN 'FC01'.  "DISPLAY
      PERFORM get_field.
*    WHEN 'FC02'.  "
*    WHEN 'ONLI'. "F8
    WHEN OTHERS.
  ENDCASE.

*----------------------------------------------------------------------*
* At selection screen output
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      static        = 'X'
    CHANGING
      file_name     = p_file
    EXCEPTIONS
      mask_too_long = 1
      OTHERS        = 2.

AT SELECTION-SCREEN OUTPUT.


************************************************************************
*     Start-of-selection
************************************************************************
START-OF-SELECTION.
  PERFORM upload_data.
  PERFORM display_data.

  DEFINE alphain.  "補0
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' "只適用純數值的前補零OR去零
      EXPORTING
        input         = &1
     IMPORTING
        output        = &1.
  END-OF-DEFINITION.
*&---------------------------------------------------------------------*
*&      Form  UPLOAD_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM upload_data .
  DATA : lv_filename TYPE string, "路径名称
         lv_message  TYPE string, "信息
         lv_row      TYPE i , "内表标题列数
         lv_row2      TYPE i . "导入值列数

  DATA:ls_extendsettings TYPE ty_extendsettings.

  DATA: BEGIN OF lt_line OCCURS 0,
          text TYPE string,
        END OF lt_line.

  CLEAR:gt_field[].


  IF p_file = space.
    MESSAGE e000 WITH 'Please input file path for upload function'.
    STOP.
  ELSE.
    lv_filename = p_file.
  ENDIF.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lv_filename
      filetype                = 'DAT'
      codepage                = '8404'
*     HAS_FIELD_SEPARATOR     = 'X'
    TABLES
      data_tab                = lt_line
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc <> 0.
    MESSAGE s000(zmsg) WITH '上傳失敗' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
  IF lt_line[] IS INITIAL.
    MESSAGE s000(zmsg) WITH '上傳檔案中無資料'
    DISPLAY LIKE 'E'.
    STOP.
  ENDIF.


  LOOP AT lt_line.
    CASE sy-tabix.
      WHEN 1.
        SPLIT lt_line-text AT ',' INTO TABLE gt_title1.
      WHEN 2.
        SPLIT lt_line-text AT ',' INTO TABLE gt_title2.
      WHEN 3.
        SPLIT lt_line-text AT ',' INTO TABLE gt_title3.
      WHEN 4.
        CHECK 1 = 2.
      WHEN 5.
        SPLIT lt_line-text AT ',' INTO TABLE gt_title5.
      WHEN 6.
        SPLIT lt_line-text AT ',' INTO TABLE gt_title6.

        "获取上传csv 标题,并检查是否有主键
        PERFORM gain_title_check CHANGING lv_row.

        "--->sap view 与 PLM 的对应关系
        SELECT *
          FROM zplmfield
          INTO CORRESPONDING FIELDS OF TABLE gt_zplmfield.


        READ TABLE gt_field INTO wa_field WITH KEY logo = '' keyflag = 'X'.
        IF sy-subrc = 0.
          MESSAGE wa_field-tabname && '-' && wa_field-tabname &&
          '上传档案中缺少主键,请补充完整后在导入' TYPE 'E'.
        ENDIF.

*--> dynamic table structure and generate ALV fieldcat
        PERFORM create_structure USING 2.

*--> Create dynamic table
        PERFORM create_dynamic_table.

        lr_fieldname[] = VALUE #(
        ( sign = 'I' option = 'EQ' low  = 'WERKS'  high = '' )
        ( sign = 'I' option = 'EQ' low  = 'BWKEY'  high = '' )
        ( sign = 'I' option = 'EQ' low  = 'VKORG'  high = '' ) ).

*---->监视视图维护状态
        SELECT tabname fieldname
           INTO TABLE gt_dd03l
           FROM dd03l
           WHERE fieldname = 'PSTAT'.

*---->获取扩展字段栏位
        SELECT tabname  fieldname position keyflag leng
          INTO TABLE gt_expfield
          FROM dd03l
          WHERE tabname IN ('BAPI_TE_MARA2')
          AND     rollname <> ''.

        SORT: gt_expfield BY tabname position ASCENDING.

*---->连接PLM DB
        PERFORM connection_plm_db.

*---->创建 PLM DB sql
        PERFORM plmupdate_data USING ls_extendsettings lv_message.

      WHEN OTHERS.

        "csv 中的一行表身赋值到内表,对表栏位进行检查
        SPLIT lt_line-text AT ',' INTO TABLE gt_value.

*----> 连接PLM DB
        PERFORM connection_plm_db.

*---->防止导入的数据最后一列是空数据值时,那么标题列和表身列做对比,
        "若相等时,则 内表 gt_value 增加一行
        DESCRIBE TABLE gt_value LINES lv_row2.
        IF lv_row2 <= lv_row.
          gt_value[] = VALUE #( BASE gt_value ( VALUE = '' ) ).
*          APPEND gt_value.
        ENDIF.


*----> input data to dynamic table
        PERFORM process_data2 USING lv_row.
    ENDCASE.

  ENDLOOP.

ENDFORM.                    " UPLOAD_DATA


*&---------------------------------------------------------------------*
*&      Form  CLEAR_BAPI_DATA
*&---------------------------------------------------------------------*
FORM clear_bapi_data .
  CLEAR: gt_bapimathead,
                    gt_bapi_mara,gt_bapi_marax,
                    gt_bapi_marc,gt_bapi_marcx,
                    gt_bapi_mpop,gt_bapi_mpopx,
                    gt_bapi_mard,gt_bapi_mardx,
                    gt_bapi_mbew,gt_bapi_mbewx,
                    gt_bapi_mvke,gt_bapi_mvkex,
                    gt_bapiret2,
                    gt_bapi_te_mara2,gt_bapi_te_mara2x,
                    gt_bapi_te_marc,gt_bapi_te_marcx,
                    gt_extensionin,gs_extensionin,
                    gt_extensioninx,gs_extensioninx.

  REFRESH:
           gt_returnmessages,
           gt_extensionin, gt_extensioninx.

ENDFORM.                    " CLEAR_BAPI_DATA

*&---------------------------------------------------------------------*
*&      Form  CALL_BAPI
*&---------------------------------------------------------------------*
FORM call_bapi CHANGING out_flag out_id out_number out_message.

  ASSIGN COMPONENT 'MATERIAL_LONG' OF STRUCTURE <gt_mathead>
        TO <g_field>.


*---->赋值
  IF gt_bapi_mean IS NOT INITIAL.

    SELECT SINGLE meins INTO gt_bapi_mean-unit
      FROM mara
      WHERE matnr = <g_field>.

    SELECT SINGLE isocode INTO gt_bapi_mean-unit_iso
    FROM t006  WHERE msehi = gt_bapi_mean-unit.

    gt_bapi_marm-alt_unit = gt_bapi_mean-unit.
    gt_bapi_marmx-alt_unit = gt_bapi_mean-unit.

    APPEND gt_bapi_mean.
  ENDIF.

  IF gt_bapi_marm IS NOT INITIAL.
    APPEND gt_bapi_marm.
  ENDIF.

  IF gt_bapi_marmx IS NOT INITIAL.
    APPEND gt_bapi_marmx.
  ENDIF.

  CLEAR gt_bapiret2.
  CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
    EXPORTING
      headdata             = <gt_mathead>
      clientdata           = <gt_mara>
      clientdatax          = <gt_marax>
      plantdata            = <gt_marc>
      plantdatax           = <gt_marcx>
      forecastparameters   = <gt_mpop>
      forecastparametersx  = <gt_mpopx>
      salesdata            = <gt_mvke>
      salesdatax           = <gt_mvkex>
      valuationdata        = <gt_mbew>
      valuationdatax       = <gt_mbewx>
      storagelocationdata  = <gt_mard>
      storagelocationdatax = <gt_mardx>
    IMPORTING
      return               = gt_bapiret2
    TABLES
      materialdescription  = gt_bapi_makt
      unitsofmeasure       = gt_bapi_marm
      unitsofmeasurex      = gt_bapi_marmx
      internationalartnos  = gt_bapi_mean
      taxclassifications   = gt_bapi_mlan
      returnmessages       = gt_returnmessages
      extensionin          = gt_extensionin
      extensioninx         = gt_extensioninx.

  IF gt_bapiret2-type = 'S' AND gt_bapiret2-number = '356'.
    out_flag = 'S'.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    g_success = g_success + 1.
    CONCATENATE '料號:' <g_field>
    '更新成功' INTO out_message.
    CALL FUNCTION 'DEQUEUE_ALL'
      EXPORTING
        _synchron = 'X'.

*----->PLM DB sql 成功提交,否则回滚.
    EXEC SQL.
      commit
    ENDEXEC.
  ELSE.
    out_flag = 'F'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    g_fail = g_fail + 1.
    out_id = gt_bapiret2-id.
    out_number = gt_bapiret2-number.
    CONCATENATE '料號:' <g_field>
    '更新失敗-' gt_bapiret2-id gt_bapiret2-number gt_bapiret2-message
    INTO out_message.

*----->PLM DB sql 成功提交,否则回滚.
    EXEC SQL.
      rollback
    ENDEXEC.

  ENDIF.

*--->关闭PLM DB 连接
  PERFORM close_plm_db.
ENDFORM.                    " CALL_BAPI



*&---------------------------------------------------------------------*
*& Form GET_FIELD
*&---------------------------------------------------------------------*
*& text 获取需要变更的栏位值
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM get_field .

  DATA:l_number  TYPE i, "内表行
       l_number2 TYPE i, "内表行2
       l_answer  TYPE c, "弹框 ID号
*       l_table   TYPE tabname16, "表格名称
       l_statm   TYPE statm. "主档维护记录


  DATA: gt_dfies  TYPE TABLE OF dfies."F4IF_INT_TABLE 选中值

  DATA:lt_zsapfield TYPE TABLE OF ty_zsapfield.
  DATA:lt_zsapfield2 TYPE TABLE OF ddshretval.

  "--->sap view 与 function 的对应关系
  SELECT *
    FROM zsapfield
    INTO CORRESPONDING FIELDS OF TABLE gt_zsapfield.


  LOOP  AT gt_zsapfield INTO DATA(ls_zsapfield).
    ls_zsapfield-idkopos = sy-tabix.
    MODIFY gt_zsapfield FROM ls_zsapfield.
  ENDLOOP.

  lt_zsapfield[] = gt_zsapfield[].

  SORT lt_zsapfield BY funstatm.

  DELETE lt_zsapfield
  WHERE sapfieldname = 'MATNR'
  OR       sapfieldname = 'WERKS'
  OR       sapfieldname = 'VKORG'.


  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
*     ddic_structure   = ' '
      "若不是在结构(zsapfield)在内表(value_tab)中的字段不能填写在这里,否则选择会失效,
      retfield         = 'KOPOS'
*     pvalkey          = ' '
      dynpprog         = sy-repid
      dynpnr           = sy-dynnr
*     dynprofield      = 'PXXX' "回写到屏幕栏中,
      stepl            = 2 "是否显示复选框
      window_title     = '请选择需要修改的栏位'
      value_org        = 'S'
      multiple_choice  = 'X' "多项选择,用于select-options
      display          = 'F'  "c则只能显示,不能选择
      callback_program = sy-repid
    TABLES
      value_tab        = lt_zsapfield
      return_tab       = lt_zsapfield2
    EXCEPTIONS
      parameter_error  = 1
      no_values_found  = 2
      OTHERS           = 3.

  IF sy-subrc = 0.
    "确认需要修改view 栏位
    LOOP AT lt_zsapfield2 INTO DATA(ls_fieldid).
      READ TABLE gt_zsapfield INTO ls_zsapfield WITH KEY
                         kopos = ls_fieldid-fieldval.
      IF sy-subrc = 0.
        ls_zsapfield-check = 'X'.
        MODIFY gt_zsapfield FROM ls_zsapfield INDEX sy-tabix.
      ENDIF.
    ENDLOOP.
    SORT gt_zsapfield BY saptablename sapfieldname.

    DELETE gt_zsapfield WHERE check <> 'X'.

*--->检查是否有重复值

    DESCRIBE TABLE gt_zsapfield LINES l_number.

    DELETE ADJACENT DUPLICATES FROM gt_zsapfield
                COMPARING saptablename sapfieldname.

    DESCRIBE TABLE gt_zsapfield LINES l_number2.
    IF l_number <> l_number2.
      CALL FUNCTION 'POPUP_TO_CONFIRM'
        EXPORTING
          text_question         = '您选择的栏位有重复,是否重新选择' "提示消息
          icon_button_1         = '' "是
          icon_button_2         = '' "否
          default_button        = '1'
          display_cancel_button = 'X'
          start_column          = 25
          start_row             = 6
        IMPORTING
          answer                = l_answer
        EXCEPTIONS
          text_not_found        = 1
          OTHERS                = 2.
    ELSE.
      CLEAR:l_answer.
    ENDIF.

    IF l_answer = '1' OR l_answer = 'A'."是 或 取消
      LEAVE LIST-PROCESSING.
*    ELSEIF l_answer = 'A'."取消
*      LEAVE LIST-PROCESSING.
    ELSEIF l_answer = '2' OR l_answer IS INITIAL." 否
      CLEAR:gt_field[].
      SORT gt_zsapfield BY funstatm.

      LOOP AT gt_zsapfield INTO ls_zsapfield.

        "主键
        READ TABLE gt_field WITH KEY
                           tabname = ls_zsapfield-saptablename
                           TRANSPORTING NO FIELDS.

        IF sy-subrc <> 0.
          SELECT *
           APPENDING CORRESPONDING FIELDS OF TABLE gt_field
           FROM dd03l
           WHERE tabname = ls_zsapfield-saptablename
           AND     keyflag = 'X'
           AND     fieldname <> 'MATNR'
           AND     fieldname <> 'MANDT'.
        ENDIF.

        l_statm = ls_zsapfield-funstatm.

        "非主键
        READ TABLE gt_field INTO wa_field WITH KEY
                           tabname = ls_zsapfield-saptablename
                           fieldname = ls_zsapfield-sapfieldname.
        IF sy-subrc <> 0.
          SELECT *
           APPENDING CORRESPONDING FIELDS OF TABLE gt_field
           FROM dd03l
           WHERE tabname = ls_zsapfield-saptablename
           AND     fieldname = ls_zsapfield-sapfieldname.
        ENDIF.

        "组合栏位
        CASE ls_zsapfield-sapfieldname.
          WHEN 'MSTAV'.
            "集团配销链生效日期
            SELECT *
            APPENDING CORRESPONDING FIELDS OF TABLE gt_field
            FROM dd03l
            WHERE tabname = ls_zsapfield-saptablename
            AND     fieldname = 'MSTDV'.
          WHEN 'VMSTA'.
            "工厂配销链生效日期
            SELECT *
            APPENDING CORRESPONDING FIELDS OF TABLE gt_field
            FROM dd03l
            WHERE tabname = ls_zsapfield-saptablename
            AND     fieldname = 'VMSTD'.
          WHEN 'EAN11'.
            "EAN類別
            SELECT *
            APPENDING CORRESPONDING FIELDS OF TABLE gt_field
            FROM dd03l
            WHERE tabname = ls_zsapfield-saptablename
            AND     fieldname = 'NUMTP'.
        ENDCASE.

        "修改视图标识
        wa_field-statm = l_statm.
        MODIFY gt_field FROM wa_field TRANSPORTING statm
                                WHERE statm = ''.
        CLEAR:wa_field.
      ENDLOOP.



      READ TABLE gt_field INTO wa_field WITH KEY fieldname = 'SPRAS'.
      IF sy-subrc = 0.
        CLEAR:gt_t002[].
        LOOP AT gt_field INTO wa_field WHERE fieldname = 'SPRAS' .
          "动态或取当前表的语系
          PERFORM dynamic_sql USING wa_field-tabname 'D'.
        ENDLOOP.
      ENDIF.

      PERFORM download.

    ENDIF.
  ELSE.
    MESSAGE '程序错误' TYPE 'E'.
  ENDIF.

ENDFORM.


FORM download .
  CONSTANTS: c_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
  FIELD-SYMBOLS <fs> TYPE any.
  DATA: lt_title  TYPE truxs_t_text_data.

  DATA:l_fpath1 TYPE string."下载路径

  CLEAR:l_fpath1.
*--> dynamic table structure and generate ALV fieldcat
  PERFORM create_structure USING 1.

*--> Create dynamic table
  PERFORM create_dynamic_table.

*--> input data to dynamic table
  PERFORM process_data_d.

*-->获取下载路径
  PERFORM get_fpath USING l_fpath1.

* 将内表数据转换成逗号分割符CSV文件
  CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
    EXPORTING
      i_field_seperator    = ','  " Comma seperator
    TABLES
      i_tab_sap_data       = <gt_table_d>
    CHANGING
      i_tab_converted_data = lt_title
    EXCEPTIONS
      conversion_failed    = 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.

  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
*     bin_filesize            =
      filename                = l_fpath1
      filetype                = 'DAT'
      codepage                = '8404'
*     ignore_cerr             = ABAP_TRUE
*     replacement             = '#'
    CHANGING
      data_tab                = lt_title
    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
      not_supported_by_gui    = 22
      error_no_gui            = 23
      OTHERS                  = 24.

  IF sy-subrc = 0.
    MESSAGE s001 WITH '模板下载完成,请到对应目录获取模板, '.
  ELSE.
    MESSAGE s001 WITH '下载失败,请检查路径!'.
  ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form CREATE_STRUCTURE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM create_structure USING p_id.
  DATA: wa_structure TYPE lvc_s_fcat,
        wa_fieldcat  TYPE slis_fieldcat_alv,
        l_ftxt(30)   TYPE c.

  DATA:lt_field TYPE TABLE OF ty_field,
       lt_t002  LIKE  gt_t002 OCCURS 0.

  DATA: l_count TYPE i,
        l_line  TYPE i,
        l_line2 TYPE i.

  CLEAR:gt_fieldcat[],gt_structure[],l_line,l_line2.

  "语系栏位
  DESCRIBE TABLE gt_t002  LINES l_line2.

  IF gt_t002[] IS NOT INITIAL.
    DELETE gt_field WHERE fieldname = 'SPRAS'.
  ENDIF.

  IF gt_field[] IS NOT INITIAL.
    DESCRIBE TABLE gt_field  LINES l_line.
    ADD 2 TO l_line.
  ENDIF.

  IF p_id = 2.
    lt_field[] = gt_field[].

    ADD 1 TO l_count.
    wa_structure-fieldname = 'FLAG'.
    wa_structure-col_pos   = l_count.
    wa_fieldcat-col_pos    = l_count.
    wa_fieldcat-fieldname  = wa_structure-fieldname.
    wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
    wa_fieldcat-seltext_l = '错误标识'.
    wa_fieldcat-ddictxt = 'L'.
    wa_fieldcat-inttype = 'C'.
    wa_structure-outputlen = wa_fieldcat-outputlen = '1'.
*       wa_fieldcat-decimals_out = '6'.
    APPEND wa_structure TO gt_structure.
    CLEAR wa_structure.
    APPEND wa_fieldcat TO gt_fieldcat.
    CLEAR wa_fieldcat.

    SORT lt_field BY col ASCENDING.
    LOOP AT lt_field INTO DATA(ls_field) WHERE logo = 'X'.

      ADD 1 TO l_count.
      wa_structure-fieldname = ls_field-col && '_' &&
                                            ls_field-fieldname.
      wa_structure-col_pos   = l_count.
      wa_fieldcat-col_pos    = l_count.
      wa_fieldcat-fieldname  = wa_structure-fieldname.
      wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
      wa_fieldcat-seltext_l = ls_field-headertxt.
      wa_fieldcat-ddictxt = 'L'.
      wa_fieldcat-inttype = 'C'.
      wa_structure-outputlen = wa_fieldcat-outputlen = '100'.
*       wa_fieldcat-decimals_out = '6'.
      APPEND wa_structure TO gt_structure.
      CLEAR wa_structure.
      APPEND wa_fieldcat TO gt_fieldcat.
      CLEAR wa_fieldcat.
    ENDLOOP.

    ADD 1 TO l_count.
    wa_structure-fieldname = 'IMPORT_LOG'.
    wa_structure-col_pos   = l_count.
    wa_fieldcat-col_pos    = l_count.
    wa_fieldcat-fieldname  = wa_structure-fieldname.
    wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
    wa_fieldcat-seltext_l = '导入错误日志'.
    wa_fieldcat-ddictxt = 'L'.
    wa_fieldcat-inttype = 'C'.
    wa_structure-outputlen = wa_fieldcat-outputlen = '200'.
*       wa_fieldcat-decimals_out = '6'.
    APPEND wa_structure TO gt_structure.
    CLEAR wa_structure.
    APPEND wa_fieldcat TO gt_fieldcat.
    CLEAR wa_fieldcat.


    ADD 1 TO l_count.
    wa_structure-fieldname = 'ID_LOG'.
    wa_structure-col_pos   = l_count.
    wa_fieldcat-col_pos    = l_count.
    wa_fieldcat-fieldname  = wa_structure-fieldname.
    wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
    wa_fieldcat-seltext_l = 'ID'.
    wa_fieldcat-ddictxt = 'L'.
    wa_fieldcat-inttype = 'C'.
    wa_structure-outputlen = wa_fieldcat-outputlen = '10'.
*       wa_fieldcat-decimals_out = '6'.
    APPEND wa_structure TO gt_structure.
    CLEAR wa_structure.
    APPEND wa_fieldcat TO gt_fieldcat.
    CLEAR wa_fieldcat.

    ADD 1 TO l_count.
    wa_structure-fieldname = 'NUMBER'.
    wa_structure-col_pos   = l_count.
    wa_fieldcat-col_pos    = l_count.
    wa_fieldcat-fieldname  = wa_structure-fieldname.
    wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
    wa_fieldcat-seltext_l = 'Msg.No'.
    wa_fieldcat-ddictxt = 'L'.
    wa_fieldcat-inttype = 'C'.
    wa_structure-outputlen = wa_fieldcat-outputlen = '20'.
*       wa_fieldcat-decimals_out = '6'.
    APPEND wa_structure TO gt_structure.
    CLEAR wa_structure.
    APPEND wa_fieldcat TO gt_fieldcat.
    CLEAR wa_fieldcat.

    ADD 1 TO l_count.
    wa_structure-fieldname = 'MESSAGE'.
    wa_structure-col_pos   = l_count.
    wa_fieldcat-col_pos    = l_count.
    wa_fieldcat-fieldname  = wa_structure-fieldname.
    wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
    wa_fieldcat-seltext_l = 'Message'.
    wa_fieldcat-ddictxt = 'L'.
    wa_fieldcat-inttype = 'C'.
    wa_structure-outputlen = wa_fieldcat-outputlen = '200'.
*       wa_fieldcat-decimals_out = '6'.
    APPEND wa_structure TO gt_structure.
    CLEAR wa_structure.
    APPEND wa_fieldcat TO gt_fieldcat.
    CLEAR wa_fieldcat.
  ELSEIF p_id = 1.

    ADD l_line2 TO l_line.

    "排除已经多语系栏位
    lt_t002[] = gt_t002[].
    DELETE ADJACENT DUPLICATES FROM lt_t002 COMPARING fieldname2.
    DESCRIBE TABLE lt_t002  LINES l_line2.
    l_line = l_line - l_line2.

    "构建动态表栏位
    DO l_line TIMES.
      ADD 1 TO l_count.
      wa_structure-fieldname = 'FIELD' && l_count .
      wa_structure-col_pos   = l_count.
      wa_fieldcat-col_pos    = l_count.
      wa_fieldcat-fieldname  = wa_structure-fieldname.
      wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
      wa_fieldcat-seltext_l = '栏位' && l_count.
      wa_fieldcat-ddictxt = 'L'.
      wa_fieldcat-inttype = 'C'.
      wa_structure-outputlen = wa_fieldcat-outputlen = '40'.
*        wa_fieldcat-decimals_out = '6'.
      APPEND wa_structure TO gt_structure.
      CLEAR wa_structure.
      APPEND wa_fieldcat TO gt_fieldcat.
      CLEAR wa_fieldcat.
    ENDDO.
  ENDIF.
ENDFORM.

FORM create_dynamic_table .

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = gt_structure
    IMPORTING
      ep_table        = gt_table_d.

  ASSIGN gt_table_d->* TO <gt_table_d>.
ENDFORM.

FORM process_data_d .
  DATA: l_new_line   TYPE REF TO data,
        l_comp_line  TYPE REF TO data,
        ls_zsapfield TYPE ty_zsapfield.

  DATA:l_doid      TYPE i , "序号
       l_index     TYPE sy-tabix,
       l_scrtext_l TYPE string.


  CREATE DATA l_new_line LIKE LINE OF <gt_table_d>.
  CREATE DATA l_comp_line LIKE LINE OF <gt_table_d>.
  ASSIGN l_new_line->* TO <g_wa>.


  CLEAR:l_doid.
  DO 6 TIMES.
    l_doid = l_doid + 1.

    CLEAR:l_index.
    LOOP AT gt_field INTO DATA(ls_field).
      CLEAR:l_scrtext_l.
      IF l_index IS INITIAL.
        l_index = l_index + 2.
      ELSEIF ls_field-langu = '' ."多语系栏位索引不加 1 loop 中在赋值前已 + 1
        l_index = l_index + 1.
      ENDIF.

      IF l_index = 2 AND l_doid = 1.
        ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = '栏位说明'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = '物料'.

        "获取栏位说明
        PERFORM field_text  USING ls_field-tabname ls_field-fieldname
                                CHANGING ls_zsapfield l_scrtext_l.

        "----非有多语系栏位
        IF ls_field-langu = '' .
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <g_wa> TO <g_field>.
          IF  ls_zsapfield-sapfieldtext IS NOT INITIAL.
            <g_field> = ls_zsapfield-sapfieldtext.
          ELSE.
            <g_field> = l_scrtext_l.
          ENDIF.
        ENDIF.

        LOOP AT gt_t002
          WHERE tabname = ls_field-tabname
          AND     fieldname2 = ls_field-fieldname.
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD' && l_index  OF
          STRUCTURE <g_wa> TO <g_field>.
          IF l_scrtext_l IS NOT INITIAL.
            <g_field> = l_scrtext_l && '_' && gt_t002-sptxt.
          ELSE.
            <g_field> = ls_zsapfield-sapfieldtext && '_' &&
                              gt_t002-sptxt.
          ENDIF.
        ENDLOOP.

      ELSEIF l_index = 2 AND l_doid = 2.

        ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = '是否必填(*必填)'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = '*'.

        "----非有多语系栏位
        IF ls_field-langu = '' .
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <g_wa> TO <g_field>.
          IF ls_field-keyflag = 'X'.
            <g_field> = '*'.
          ELSE.
            <g_field> = ''.
          ENDIF.
        ENDIF.

        LOOP AT gt_t002
          WHERE tabname = ls_field-tabname
          AND     fieldname2 = ls_field-fieldname.
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD' && l_index  OF
          STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ''.
        ENDLOOP.

      ELSEIF l_index = 2 AND l_doid = 3.

        ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = 'View'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = ''.

        "----非有多语系栏位
        IF ls_field-langu = '' .
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ls_field-statm.
        ENDIF.

        LOOP AT gt_t002
          WHERE tabname = ls_field-tabname
          AND     fieldname2 = ls_field-fieldname.
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD' && l_index  OF
          STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ls_field-statm.
        ENDLOOP.

      ELSEIF l_index = 2 AND l_doid = 4.

        ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = 'View说明'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = ''.

        "----非有多语系栏位
        IF ls_field-langu = '' .
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <g_wa> TO <g_field>.
          READ TABLE gt_zsapfield INTO ls_zsapfield WITH KEY
                          funstatm = ls_field-statm.
          IF sy-subrc = 0.
            <g_field> = ls_zsapfield-sttxt.
          ENDIF.
        ENDIF.

        LOOP AT gt_t002
          WHERE tabname = ls_field-tabname
          AND     fieldname2 = ls_field-fieldname.
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD' && l_index  OF
          STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ls_zsapfield-sttxt.
        ENDLOOP.
      ELSEIF l_index = 2 AND l_doid = 5.

        ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = '表名'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = 'MARA'.

        "----非有多语系栏位
        IF ls_field-langu = '' .
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ls_field-tabname.
        ENDIF.

        LOOP AT gt_t002
          WHERE tabname = ls_field-tabname
          AND     fieldname2 = ls_field-fieldname.
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD' && l_index  OF
          STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ls_field-tabname.
        ENDLOOP.
      ELSEIF l_index = 2 AND l_doid = 6.

        ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = '栏位名称'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <g_wa> TO <g_field>.
        <g_field> = 'MATNR'.

        "----非有多语系栏位
        IF ls_field-langu = '' .
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <g_wa> TO <g_field>.
          <g_field> = ls_field-fieldname.
        ENDIF.

        LOOP AT gt_t002
          WHERE tabname = ls_field-tabname
          AND     fieldname2 = ls_field-fieldname.
          l_index = l_index + 1.
          ASSIGN COMPONENT 'FIELD' && l_index  OF
          STRUCTURE <g_wa> TO <g_field>.
*          <g_field> = gt_field-fieldname && '_' && gt_t002-laiso.
          <g_field> = gt_t002-fieldname.
        ENDLOOP.
      ELSE.
        ASSIGN COMPONENT 'FIELD' && l_index  OF
        STRUCTURE <g_wa> TO <g_field>.

        CASE l_doid.
          WHEN '1'.

            "获取栏位说明
            PERFORM field_text  USING ls_field-tabname ls_field-fieldname
                                    CHANGING ls_zsapfield l_scrtext_l.

            "----非有多语系栏位
            IF ls_field-langu = '' .
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
              IF  ls_zsapfield-sapfieldtext IS NOT INITIAL.
                <g_field> = ls_zsapfield-sapfieldtext.
              ELSE.
                <g_field> = l_scrtext_l.
              ENDIF.
            ENDIF.

            LOOP AT gt_t002
              WHERE tabname = ls_field-tabname
              AND     fieldname2 = ls_field-fieldname.
              l_index = l_index + 1.
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
              IF l_scrtext_l IS NOT INITIAL.
                <g_field> = l_scrtext_l && '_' && gt_t002-sptxt.
              ELSE.
                <g_field> = ls_zsapfield-sapfieldtext && '_' &&
                                  gt_t002-sptxt.
              ENDIF.
            ENDLOOP.
          WHEN '2'.
            "----非有多语系栏位
            IF ls_field-langu = '' .
              IF ls_field-keyflag = 'X'.
                <g_field> = '*'.
              ELSE.
                <g_field> = ''.
              ENDIF.
            ENDIF.

            LOOP AT gt_t002
              WHERE tabname = ls_field-tabname
              AND     fieldname2 = ls_field-fieldname.
              l_index = l_index + 1.
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
              <g_field> = ''.
            ENDLOOP.
          WHEN '3'.
            "----非有多语系栏位
            IF ls_field-langu = '' .
              <g_field> = ls_field-statm.
            ENDIF.

            LOOP AT gt_t002
              WHERE tabname = ls_field-tabname
              AND     fieldname2 = ls_field-fieldname.
              l_index = l_index + 1.
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
              <g_field> = ls_field-statm.
            ENDLOOP.
          WHEN '4'.
            READ TABLE gt_zsapfield INTO ls_zsapfield WITH KEY
                               funstatm = ls_field-statm.
            IF sy-subrc = 0 AND ls_field-langu <> 'X' .
              <g_field> = ls_zsapfield-sttxt.
            ENDIF.

            LOOP AT gt_t002
              WHERE tabname = ls_field-tabname
              AND     fieldname2 = ls_field-fieldname.
              l_index = l_index + 1.
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
              <g_field> = ls_zsapfield-sttxt.
            ENDLOOP.
          WHEN '5'.
            "----非有多语系栏位
            IF ls_field-langu = '' .
              <g_field> = ls_field-tabname.
            ENDIF.

            LOOP AT gt_t002
              WHERE tabname = ls_field-tabname
              AND     fieldname2 = ls_field-fieldname.
              l_index = l_index + 1.
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
              <g_field> = ls_field-tabname.
            ENDLOOP.
          WHEN '6'.
            "----非有多语系栏位
            IF ls_field-langu = '' .
              <g_field> = ls_field-fieldname.
            ENDIF.

            LOOP AT gt_t002
              WHERE tabname = ls_field-tabname
              AND     fieldname2 = ls_field-fieldname.
              l_index = l_index + 1.
              ASSIGN COMPONENT 'FIELD' && l_index  OF
              STRUCTURE <g_wa> TO <g_field>.
*              <g_field> = gt_field-fieldname && '_' && gt_t002-laiso.
              <g_field> = gt_t002-fieldname.
            ENDLOOP.
        ENDCASE.
      ENDIF.
    ENDLOOP.
    APPEND <g_wa> TO <gt_table_d>.CLEAR:<g_wa>.
  ENDDO.

ENDFORM.
FORM process_data2 USING VALUE(lv_row).

  DATA: ls_field  TYPE ty_field,
        ls_field2 TYPE ty_field.

  DATA: l_new_line  TYPE REF TO data,
        l_comp_line TYPE REF TO data.

  DATA: lv_index   TYPE zkcd_ex_row_n , "列编号
        lv_where   TYPE string, "动态where条件
        lv_values  TYPE string, "动态where条件值
        lv_message TYPE string, "信息
        lv_matnr   TYPE matnr, "物料
        lv_pstat   TYPE pstat_d, "视图维护状态
        lv_ean11   TYPE mara-ean11, "产品条码
        lv_numtp   TYPE mara-numtp. "EAN类别

*--->错误信息
  DATA:lv_flag     TYPE c,
       lv_id       TYPE c LENGTH 10, "错误类型
       lv_number   TYPE c LENGTH 20 , "错误编号
       lv_message2 TYPE c LENGTH 200 . "错误日日志

  CREATE DATA l_new_line LIKE gt_bapimathead.
  ASSIGN l_new_line->* TO <gt_mathead>.
  CREATE DATA l_new_line LIKE gt_bapi_mara.
  ASSIGN l_new_line->* TO <gt_mara>.
  CREATE DATA l_new_line LIKE gt_bapi_marax.
  ASSIGN l_new_line->* TO <gt_marax>.
  CREATE DATA l_new_line LIKE gt_bapi_te_mara2.
  ASSIGN l_new_line->* TO <gt_mara2>.
  CREATE DATA l_new_line LIKE gt_bapi_te_mara2x.
  ASSIGN l_new_line->* TO <gt_marax2>.
  CREATE DATA l_new_line LIKE gt_bapi_marc.
  ASSIGN l_new_line->* TO <gt_marc>.
  CREATE DATA l_new_line LIKE gt_bapi_marcx.
  ASSIGN l_new_line->* TO <gt_marcx>.
  CREATE DATA l_new_line LIKE gt_bapi_mpop.
  ASSIGN l_new_line->* TO <gt_mpop>.
  CREATE DATA l_new_line LIKE gt_bapi_mpopx.
  ASSIGN l_new_line->* TO <gt_mpopx>.
  CREATE DATA l_new_line LIKE gt_bapi_mvke.
  ASSIGN l_new_line->* TO <gt_mvke>.
  CREATE DATA l_new_line LIKE gt_bapi_mvkex.
  ASSIGN l_new_line->* TO <gt_mvkex>.
  CREATE DATA l_new_line LIKE gt_bapi_mbew.
  ASSIGN l_new_line->* TO <gt_mbew>.
  CREATE DATA l_new_line LIKE gt_bapi_mbewx.
  ASSIGN l_new_line->* TO <gt_mbewx>.
  CREATE DATA l_new_line LIKE gt_bapi_mard.
  ASSIGN l_new_line->* TO <gt_mard>.
  CREATE DATA l_new_line LIKE gt_bapi_mardx.
  ASSIGN l_new_line->* TO <gt_mardx>.

  CREATE DATA l_new_line LIKE LINE OF <gt_table_d>.
  CREATE DATA l_comp_line LIKE LINE OF <gt_table_d>.
  ASSIGN l_new_line->* TO <g_wa>.


  DATA: BEGIN OF lt_mara OCCURS 0,
          matnr TYPE matnr, "物料
          mbrsh TYPE mbrsh, "业务类别
          mtart TYPE mtart, "物料类型
        END OF lt_mara.


  CLEAR: lv_ean11, lv_numtp.


*---->将导入的数据分配到各个view中

  LOOP AT gt_value INTO DATA(ls_value).

    lv_index = sy-tabix.

    CHECK NOT lv_index = '00001'.


    READ TABLE gt_field INTO ls_field WITH KEY
                       col = lv_index.
    IF sy-subrc = 0 AND
        ls_value-value IS INITIAL AND
        ls_field-keyflag IS NOT INITIAL.

      MESSAGE '主键栏位为空' && lv_index && '' &&
                     '栏位' && ls_field-fieldname TYPE 'E'.
      STOP.
    ELSEIF sy-subrc = 0 AND lv_index = '00002'.
      ASSIGN COMPONENT ls_field-col && '_MATNR' OF
                  STRUCTURE <g_wa> TO <g_field>.
      <g_field> = lv_matnr = ls_value-value.
      CLEAR:lv_message,lt_mara.

      "查询物料是否存在
      SELECT SINGLE matnr mbrsh mtart INTO lt_mara
        FROM mara WHERE matnr = lv_matnr.
      IF sy-subrc = 0.
        ASSIGN COMPONENT 'MATERIAL_LONG' OF STRUCTURE <gt_mathead>
        TO <g_field>. <g_field> = lt_mara-matnr.

        ASSIGN COMPONENT 'MATERIAL_EXTERNAL' OF STRUCTURE <gt_mathead>
        TO <g_field>. <g_field> = lt_mara-matnr.

        ASSIGN COMPONENT 'IND_SECTOR' OF STRUCTURE <gt_mathead>
       TO <g_field>. <g_field> = lt_mara-mbrsh.

        ASSIGN COMPONENT 'MATL_TYPE' OF STRUCTURE <gt_mathead>
       TO <g_field>. <g_field> = lt_mara-mtart.
      ELSE.
        CONCATENATE lv_message lv_matnr '物料不存在'  INTO lv_message.
      ENDIF.
    ENDIF.

*--->向PLM、或SAP不同view中提供主键栏位值
    IF ls_field-keyflag <> 'X'.
      CLEAR:ls_field2.
      LOOP AT gt_field INTO ls_field2
        WHERE tabname = ls_field-tabname
            AND  keyflag = 'X'
            AND fieldname2 IN lr_fieldname.
        EXIT.
      ENDLOOP.

      READ TABLE gt_value INTO DATA(ls_value2) INDEX ls_field2-col.
      IF sy-subrc <> 0.
        CLEAR:ls_value2.
      ENDIF.
    ENDIF.


*---->动态 where 条件
    IF lv_index <> '00002' AND
        ls_value2 IS NOT INITIAL .

      CONCATENATE '''' ls_value2-value '''' INTO lv_values.

      CONCATENATE ls_field2-fieldname2 'EQ' lv_values
      INTO lv_where SEPARATED BY space.

      CONCATENATE '''' lv_matnr '''' INTO lv_values.
      CONCATENATE lv_where 'AND' 'MATNR EQ ' lv_values
      INTO lv_where SEPARATED BY space.

    ELSEIF lv_index <> '00002' AND
               ls_field-keyflag = 'X' .

      CONCATENATE '''' ls_value-value '''' INTO lv_values.

      CONCATENATE ls_field-fieldname2 'EQ' lv_values
      INTO lv_where SEPARATED BY space.

      CONCATENATE '''' lv_matnr '''' INTO lv_values.
      CONCATENATE lv_where 'AND' 'MATNR EQ ' lv_values
      INTO lv_where SEPARATED BY space.

    ELSE.

      CONCATENATE '''' lv_matnr '''' INTO lv_values.

      CONCATENATE  'MATNR EQ' lv_values
      INTO lv_where SEPARATED BY space.

    ENDIF.

    "检查视图是否存在
    READ TABLE gt_dd03l WITH KEY
                       tabname = ls_field-tabname.
    IF sy-subrc = 0 AND
        ls_field-col <> '00002' AND
        ls_field-statm <> 'V'.

      CLEAR:lv_pstat.
      "缺少 view-------------------------
      SELECT SINGLE pstat INTO lv_pstat
        FROM (ls_field-tabname)
        WHERE (lv_where).
      IF lv_pstat NS ls_field-statm.
        CONCATENATE lv_message lv_matnr '物料不存在' ls_field-statm '视图'
        INTO lv_message.
      ELSE.
        CASE ls_field-statm.
          WHEN 'K'."基本view
            ASSIGN COMPONENT 'BASIC_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
*          WHEN 'V'."销售view
*            ASSIGN COMPONENT 'SALES_VIEW' OF STRUCTURE <gt_mathead>
*            TO <g_field>. <g_field> = 'X'.
          WHEN 'E'."採購檢視
            ASSIGN COMPONENT 'PURCHASE_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
          WHEN 'D'."物料需求計畫 (MRP) 檢視
            ASSIGN COMPONENT 'MRP_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
          WHEN 'P'."預測檢視
            ASSIGN COMPONENT 'FORECAST_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
          WHEN 'A'."工作排程檢視
            ASSIGN COMPONENT 'WORK_SCHED_VIEW' OF STRUCTURE
            <gt_mathead> TO <g_field>. <g_field> = 'X'.
*            WHEN ''."生產資源/工具 (PRT) 檢視
*             ASSIGN COMPONENT 'PRT_VIEW' OF STRUCTURE <gt_mathead>
*             TO <g_field>. <g_field> = 'X'.
          WHEN 'L'."儲存檢視
            ASSIGN COMPONENT 'STORAGE_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
*            WHEN ''."倉庫管理檢視
*          ASSIGN COMPONENT 'WAREHOUSE_VIEW' OF STRUCTURE <gt_mathead>
*             TO <g_field>. <g_field> = 'X'.
          WHEN 'Q'."品質管理檢視
            ASSIGN COMPONENT 'QUALITY_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
          WHEN 'B'."會計檢視
            ASSIGN COMPONENT 'ACCOUNT_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
          WHEN 'G'."成本計算檢視
            ASSIGN COMPONENT 'COST_VIEW' OF STRUCTURE <gt_mathead>
            TO <g_field>. <g_field> = 'X'.
        ENDCASE.
      ENDIF.

    ELSEIF ls_field-col <> '00002'.
      CASE ls_field-statm.
        WHEN 'V'."销售view
          SELECT SINGLE matnr INTO lv_pstat
          FROM (ls_field-tabname)
          WHERE (lv_where).
          IF  sy-subrc = 0.
            ASSIGN COMPONENT 'SALES_VIEW' OF STRUCTURE <gt_mathead>
             TO <g_field>. <g_field> = 'X'.
          ELSE.
            CONCATENATE lv_message lv_matnr '物料不存在销售视图'
            INTO lv_message.
          ENDIF.
      ENDCASE.
    ENDIF.

*--->特殊栏位处理
    IF ls_field-fieldname = 'EAN11'.
      lv_ean11 = ls_value-value.
    ELSEIF ls_field-fieldname = 'NUMTP'.
      lv_numtp  = ls_value-value.
    ELSEIF ls_value-value IS INITIAL.

*--->必填栏位检查(检查导入资料是否空)
      PERFORM field_vakue_check
           USING ls_field lv_message lv_ean11 lv_numtp 1.
    ENDIF.

*--->向各个结构view中写入资料
    IF lv_message IS INITIAL.
      PERFORM insert_str_data
                      USING ls_field ls_value-value
                                 ls_value2-value lv_matnr
                      CHANGING lv_message.
    ENDIF.

    ASSIGN COMPONENT ls_field-col && '_' && ls_field-fieldname
    OF STRUCTURE <g_wa> TO <g_field>. <g_field> = ls_value-value.


    AT LAST.
      lv_row = lv_row + 1.
      IF lv_index > lv_row.
        CONCATENATE  lv_message '上传栏位超出第' lv_row '列,栏位值中不能包含英文逗号'
        '''' ',' '''' ',请检查后在上传' INTO lv_message.
      ENDIF.
      IF <g_wa> IS NOT INITIAL.
*--->更新数据
        PERFORM expend_data USING lv_message.
        IF lv_message IS INITIAL.
          PERFORM call_bapi CHANGING lv_flag lv_id lv_number lv_message2.
        ENDIF.

        ASSIGN COMPONENT 'FLAG' OF STRUCTURE
        <g_wa> TO <g_field>.<g_field> = lv_flag.

        ASSIGN COMPONENT 'IMPORT_LOG' OF STRUCTURE
        <g_wa> TO <g_field>.<g_field> = lv_message.

        ASSIGN COMPONENT 'ID_LOG' OF STRUCTURE
        <g_wa> TO <g_field>.<g_field> = lv_id.

        ASSIGN COMPONENT 'NUMBER' OF STRUCTURE
        <g_wa> TO <g_field>.<g_field> = lv_number.

        ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE
        <g_wa> TO <g_field>.<g_field> = lv_message2.

        APPEND <g_wa> TO <gt_table_d>.CLEAR:<g_wa>.

        CLEAR:lv_flag, lv_id ,lv_number,lv_message2.
        PERFORM clear_bapi_data.
      ENDIF.
    ENDAT.
  ENDLOOP.

  IF <gt_table_d>[] IS INITIAL.
    MESSAGE '导入数据为空' TYPE 'E'.
    STOP.
  ENDIF.

ENDFORM.

FORM get_fpath USING p_fpath1.
  DATA: lv_local_file_path TYPE dbmsgora-filename,
        lv_pure_filename   TYPE sdbah-actid,
        lv_pure_extension  TYPE sdbad-funct,
        lv_file_suffix     TYPE string.

  DATA: lv_filename TYPE string,
        lv_path     TYPE string.

*  l_path = 'Z:\'.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = '请填选择下载路径及文件名'
      file_filter          = '*.csv'
    CHANGING
      filename             = lv_filename
      path                 = lv_path
      fullpath             = p_fpath1
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.

  IF lv_filename IS INITIAL AND p_fpath1 IS NOT INITIAL.
    MESSAGE '选择路径时,请填写文件名称.' TYPE 'E'.
    STOP.
  ELSEIF p_fpath1 IS INITIAL.
    STOP.
  ENDIF.

  lv_local_file_path = p_fpath1.
  CALL FUNCTION 'SPLIT_FILENAME'        "函数:拆分文件绝对路径
    EXPORTING
      long_filename  = lv_local_file_path    "文件绝对路径   C://DOC/TEST.TXT
    IMPORTING
      pure_filename  = lv_pure_filename      "文件名称(不带后缀) C://DOC/TEST
      pure_extension = lv_pure_extension.    "后缀名TXT

  "选择文件有后缀时,去除后缀
  IF lv_pure_extension IS NOT INITIAL.
    CONCATENATE '.' lv_pure_extension INTO lv_file_suffix.
    SHIFT p_fpath1 RIGHT DELETING TRAILING lv_file_suffix.
  ENDIF.

  CONCATENATE p_fpath1 '.csv' INTO p_fpath1.

  CONDENSE p_fpath1 NO-GAPS.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form DYNAMIC_SQL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> GT_FIELD_TABNAME
*&---------------------------------------------------------------------*
FORM dynamic_sql  USING  p_tabname VALUE(lv_del).

  DATA: l_sql TYPE string."动态sql

  DATA:lt_t002 LIKE TABLE OF gt_t002,
       ls_t002 LIKE gt_t002.

  DATA:lv_laiso TYPE t002-laiso.


*   "系统语言码转换 标准语言码(iso 639)

  SELECT t002~spras t002~laiso t002t~sptxt
    INTO CORRESPONDING FIELDS OF TABLE lt_t002
    FROM t002t
    INNER JOIN t002 ON t002~spras = t002t~sprsl
    INNER JOIN zt002 ON zt002~laiso = t002~laiso
    WHERE t002t~spras = sy-langu.

  IF sy-subrc = 0.
    ls_t002-tabname = p_tabname.
    MODIFY lt_t002 FROM ls_t002 TRANSPORTING tabname
                          WHERE tabname = space.

    SELECT b~spras,b~laiso,b~sptxt,b~tabname,
      ( a~fieldname && '_' && b~laiso ) AS fieldname,
      a~fieldname AS fieldname2
      FROM dd03l AS a
      INNER JOIN @lt_t002 AS b ON b~tabname = a~tabname
      WHERE a~keyflag <> 'X'
      APPENDING CORRESPONDING FIELDS OF TABLE @gt_t002.
  ENDIF.

  lt_t002[] = gt_t002[].
  DELETE ADJACENT DUPLICATES FROM lt_t002 COMPARING fieldname2.
  LOOP AT lt_t002 INTO ls_t002.

    wa_field-langu = 'X'.
    MODIFY gt_field FROM wa_field TRANSPORTING langu
                 WHERE tabname = ls_t002-tabname
                      AND fieldname = ls_t002-fieldname2.
    IF sy-subrc <> 0 AND lv_del = 'D'.
      DELETE gt_t002 WHERE fieldname2 = ls_t002-fieldname2
                                 AND tabname = ls_t002-tabname.
    ENDIF.
    CLEAR:wa_field,ls_t002.
  ENDLOOP.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form INSERT_STR_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> GT_FIELD
*&---------------------------------------------------------------------*
FORM insert_str_data
          USING  ls_field TYPE ty_field in_value
                      VALUE(in_value2) in_matnr
                      CHANGING lv_message.

  DATA:lv_field TYPE dd03l-fieldname."栏位名称

  CASE ls_field-funtablename.
    WHEN  'BAPI_MARA'."基础资料

      IF ( in_value IS INITIAL OR in_value = '0' ) AND
            ls_field-fieldname2 = 'MSTDV'.
        in_value = '00000000'.
      ENDIF.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mara> TO <g_field>.
      <g_field> = in_value.

      "补充前导0
      CASE ls_field-fieldname2.
        WHEN 'SPART' OR 'LABOR' OR 'EXTWG' OR 'MSTAV'.
          alphain <g_field> .
          in_value = <g_field>.
      ENDCASE.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_marax> TO <g_field>.

      IF ls_field-keyflag IS INITIAL.
        <g_field> = 'X'.
      ELSE.
        <g_field> = in_value.
      ENDIF.

    WHEN  'BAPI_TE_MARA2'."基础资料
      READ TABLE gt_expvalue WITH KEY
                          value = in_matnr
                          fieldname = 'MATERIAL'.
      IF sy-subrc <> 0.
        gt_expvalue-tabname = 'BAPI_TE_MARA2'."表格
        gt_expvalue-fieldname = 'MATERIAL'."栏位
        gt_expvalue-value = in_matnr."栏位值
        APPEND gt_expvalue.CLEAR:gt_expvalue.
      ENDIF.

      gt_expvalue-tabname = ls_field-funtablename."表格
      gt_expvalue-fieldname = ls_field-funfieldname."栏位
      gt_expvalue-value = in_value."栏位值
      APPEND gt_expvalue.CLEAR:gt_expvalue.

    WHEN 'BAPI_MARC'."MRP、采购

      IF ( in_value IS INITIAL OR in_value = '0' ) AND
            ls_field-fieldname2 = 'MMSTD'.
        in_value = '00000000'.
      ENDIF.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
       <gt_marc> TO <g_field>.
      <g_field> = in_value.

      "补充前导0
      CASE ls_field-fieldname2.
        WHEN 'FHORI' OR 'DISPO' OR 'MTVFP' .
          alphain <g_field> .
          in_value = <g_field>.
      ENDCASE.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_marcx> TO <g_field>.

      IF ls_field-keyflag IS INITIAL.
        <g_field> = 'X'.
      ELSE.
        <g_field> = in_value.
      ENDIF.

    WHEN 'BAPI_MPOP'."预测
      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mpop> TO <g_field>.
      <g_field> = in_value.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mpopx> TO <g_field>.

      IF ls_field-keyflag IS INITIAL.
        <g_field> = 'X'.
      ELSE.
        <g_field> = in_value.
      ENDIF.

    WHEN 'BAPI_MVKE'."销售
      IF   ( in_value IS INITIAL OR in_value = '0' ) AND
            ( ls_field-fieldname2 = 'MSTDV' OR
              ls_field-fieldname2 = 'VMSTD').
        in_value = '00000000'.
      ENDIF.


      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
       <gt_mvke> TO <g_field>.
      <g_field> = in_value.

      "补充前导0
      CASE ls_field-fieldname2.
        WHEN 'KTGRM' OR 'VTWEG' OR 'TRAGR' OR 'LADGR'  OR
          'MVGR1'  OR 'MVGR2'  OR 'MVGR3' OR 'MVGR4'  OR
          'MVGR5'  OR 'PROVG'  OR 'MSTAV' OR 'MTVFP'.
          alphain <g_field> .
          in_value = <g_field>.
      ENDCASE.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mvkex> TO <g_field>.

      IF ls_field-keyflag IS INITIAL.
        <g_field> = 'X'.
      ELSE.
        <g_field> = in_value.
      ENDIF.

    WHEN 'BAPI_MBEW'."成本

      IF   ( in_value IS INITIAL OR in_value = '0' ) AND
            ( ls_field-fieldname2 = 'ZPLD1' OR
              ls_field-fieldname2 = 'ZPLD2' OR
              ls_field-fieldname2 = 'ZPLD3').
        in_value = '00000000'.
      ENDIF.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mbew> TO <g_field>.
      <g_field> = in_value.

      "补充前导0
      CASE ls_field-fieldname2.
        WHEN 'PRCTR' .
          alphain <g_field> .
          in_value = <g_field>.
      ENDCASE.
      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mbewx> TO <g_field>.
      <g_field> = 'X'.
    WHEN 'BAPI_MARD'."存储
      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mard> TO <g_field>.
      <g_field> = in_value.

      ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
      <gt_mardx> TO <g_field>.

      IF ls_field-keyflag IS INITIAL.
        <g_field> = 'X'.
      ELSE.
        <g_field> = in_value.
      ENDIF.

    WHEN 'BAPI_MAKT'."物料说明
      READ TABLE gt_t002 INTO DATA(ls_t002) WITH KEY
                         tabname  = ls_field-tabname
                         fieldname = ls_field-fieldname.
      IF sy-subrc = 0.
*        gt_bapi_makt-langu = ls_t002-spras.
        gt_bapi_makt-langu_iso = ls_t002-laiso.
      ELSE.
        gt_bapi_makt-langu = sy-langu.
      ENDIF.

      gt_bapi_makt-matl_desc = in_value.
      APPEND gt_bapi_makt.

    WHEN 'BAPI_MARM'."计量单位
*           ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
*           <gt_mpop> TO <g_field>.
*           <g_field> = in_value.
*
*           ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
*           <gt_mpopx> TO <g_field>.
*
*           IF ls_field-keyflag IS INITIAL.
*             <g_field> = 'X'.
*           ELSE.
*             <g_field> = in_value.
*           ENDIF.
*
    WHEN 'BAPI_MEAN'."商品代码

      CASE ls_field-fieldname2.
        WHEN 'EAN11'."产品条码
          gt_bapi_marm-ean_upc = gt_bapi_mean-ean_upc = in_value.
          gt_bapi_marmx-ean_upc = 'X'.
        WHEN 'NUMTP'."EAN類別
          gt_bapi_marm-ean_cat = gt_bapi_mean-ean_cat = in_value.
          gt_bapi_marmx-ean_cat = 'X'.
        WHEN OTHERS.
          CONCATENATE  lv_message '因修改栏位已超出程序修改范围请联系开发人员'
          INTO lv_message.
      ENDCASE.
      gt_bapi_mean-del_flag = 'X'.
    WHEN 'BAPI_MLAN'."税码
*           ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
*           <gt_mpop> TO <g_field>.
*           <g_field> = in_value.

      "补充前导0
*          CASE ls_field-fieldname2.
*            WHEN 'PRCTR' .
*                alphain <g_field> .
*                in_value = <g_field>.
*          ENDCASE.

*           ASSIGN COMPONENT ls_field-funfieldname OF STRUCTURE
*           <gt_mpopx> TO <g_field>.
*
*           IF ls_field-keyflag IS INITIAL.
*             <g_field> = 'X'.
*           ELSE.
*             <g_field> = in_value.
*           ENDIF.
*
  ENDCASE.



*--->生成 PLM sql
  READ TABLE gt_zplmfield INTO DATA(ls_zplmfield) WITH KEY
                     saptablename = ls_field-tabname
                     sapfieldname = ls_field-fieldname.
  IF sy-subrc = 0.
    "--->通过工厂、销售组织、评价范围等主键查找PLM栏位
    CONCATENATE ls_zplmfield-plmfieldname '_'  in_value2 INTO lv_field.
    READ TABLE gt_extendsettings INTO DATA(ls_extendsettings) WITH KEY
                       propertycode = lv_field.
    IF sy-subrc <> 0.
      "--->非工厂、销售组织、评价范围等主键查找PLM栏位
      READ TABLE gt_extendsettings INTO ls_extendsettings WITH KEY
                         propertycode = ls_zplmfield-plmfieldname.
    ENDIF.

    IF ls_extendsettings IS INITIAL.
      ls_extendsettings-propertycode = ls_zplmfield-plmfieldname.
    ENDIF.
    "PLM 表名 、PLM 修改的栏位、PLM 修改的栏位值
    ls_extendsettings-tabname = ls_zplmfield-plmtablename.
    ls_extendsettings-fieldname = ls_zplmfield-plmfieldname.
    ls_extendsettings-values = in_value.
    ls_extendsettings-matnr = in_matnr."物料


*---->创建 PLM DB sql
    PERFORM plmupdate_data USING ls_extendsettings lv_message.


    CLEAR:ls_extendsettings.
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form EXPEND_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM expend_data USING in_message.
  DATA:l_sleng  TYPE i, "栏位长度
       l_slengx TYPE i. "栏位长度

  "查询本次修改的栏位是否有SAP客制字段栏位,无不做检查
  READ TABLE gt_field WITH KEY  funtablename = 'BAPI_TE_MARA2'
                     TRANSPORTING NO FIELDS.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  LOOP AT gt_expfield WHERE tabname = 'BAPI_TE_MARA2'.

    AT NEW tabname.
      gs_extensionin-structure = 'BAPI_TE_MARA2'.
      gs_extensioninx-structure = 'BAPI_TE_MARA2X'.
    ENDAT.

    CLEAR:gt_expvalue.
    READ TABLE gt_expvalue  WITH KEY
                       tabname = gt_expfield-tabname
                       fieldname  = gt_expfield-fieldname.

    IF  gt_expfield-keyflag IS NOT INITIAL AND
                gt_expvalue-value IS  INITIAL.
      CONCATENATE in_message
      gt_expfield-tabname '-' gt_expfield-fieldname
      '主键栏位为空' INTO in_message.
      EXIT.
    ELSEIF sy-subrc = 0 AND
        gt_expfield-keyflag IS NOT INITIAL.

*       gs_extensionin-valuepart1+0(40)  = t_basic-matnr.
      gs_extensionin-valuepart1+l_sleng(gt_expfield-leng)
                 = gt_expvalue-value.

      gs_extensioninx-valuepart1+l_slengx(gt_expfield-leng)
                = gt_expvalue-value.

      l_sleng = l_sleng + gt_expfield-leng.
      l_slengx = l_slengx + gt_expfield-leng.

    ELSEIF sy-subrc = 0.
      gs_extensionin-valuepart1+l_sleng(gt_expfield-leng)
                = gt_expvalue-value.
      gs_extensioninx-valuepart1+l_slengx(gt_expfield-leng) = 'X'.

      l_sleng = l_sleng + gt_expfield-leng.
      l_slengx = l_slengx + 1.
    ELSE.
      gs_extensionin-valuepart1+l_sleng(gt_expfield-leng) = ''.
      gs_extensioninx-valuepart1+l_slengx(gt_expfield-leng) = ''.

      l_sleng = l_sleng + gt_expfield-leng.
      l_slengx = l_slengx + 1.
    ENDIF.

  ENDLOOP.

  APPEND: gs_extensionin TO gt_extensionin,
                gs_extensioninx TO gt_extensioninx.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM display_data .

**--> ALV Layout Setting
  wa_layout-colwidth_optimize = 'X'. " 最佳欄寬
  wa_layout-zebra = 'X'.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = wa_layout
      it_fieldcat        = gt_fieldcat
*     i_callback_pf_status_set = 'ALV_PF_STATUS'
*     i_callback_user_command  = 'ALV_USER_COMMAND'
      i_save             = 'A'
    TABLES
      t_outtab           = <gt_table_d>. "gt_arbpl_per."gt_aufk.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FIELD_VAKUE_CHECK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM field_vakue_check
       USING ls_field TYPE ty_field
                  in_meg in_ean11 in_numtp in_id .

  IF in_id = '1'.
*-->必填栏位检查
    IF ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) OR "重量单位.
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'MATKL' ) OR "物料群组
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'MEINS' ) OR "重量单位
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) OR "
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) OR "
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) OR "
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) OR "
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) OR "
        ( ls_field-tabname = 'MARA' AND
          ls_field-fieldname = 'GEWEI' ) . "

      CONCATENATE in_meg ls_field-headertxt '不能为空 '
      ls_field-tabname '-' ls_field-fieldname INTO in_meg.

    ENDIF.
  ELSEIF in_id = '2'.
*-->产品条码
    IF in_ean11 IS NOT INITIAL AND in_numtp IS INITIAL.
      CONCATENATE in_meg '未填写产品条码单位,NUMTP栏位未填写'
      INTO in_meg.
    ELSEIF  in_ean11 IS  INITIAL AND in_numtp IS NOT INITIAL .
      CONCATENATE in_meg '产品条码单位,单位不为空' in_numtp
      INTO in_meg.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FIELD_TEXT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> LS_ZSAPFIELD
*&      --> LS_FIELD_TABNAME
*&      --> LS_FIELD_FIELDNAME
*&      <-- L_SCRTEXT_L
*&---------------------------------------------------------------------*
FORM field_text  USING   lv_tabname
                          lv_fieldname
                 CHANGING ls_zsapfield TYPE  ty_zsapfield l_scrtext_l.

  READ TABLE gt_zsapfield INTO ls_zsapfield WITH KEY
                             saptablename = lv_tabname
                             sapfieldname  = lv_fieldname.
  IF sy-subrc <> 0.
    SELECT SINGLE dd04t~scrtext_l INTO l_scrtext_l
     FROM dd03l
     INNER JOIN dd04t ON dd04t~rollname = dd03l~rollname
     WHERE dd03l~tabname = lv_tabname
     AND     dd03l~fieldname = lv_fieldname
     AND     dd04t~ddlanguage = sy-langu.
    CLEAR:ls_zsapfield.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form PLMUPDATE_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM plmupdate_data USING ls_extendsettings TYPE ty_extendsettings
                CHANGING lv_text.

  DATA:exc_ref TYPE REF TO cx_sy_native_sql_error.

  TRY .
      CASE ls_extendsettings-tabname.
        WHEN 'MAT_MATERIALVERS'.
          "---->PLM 非扩展字段栏位
          CASE ls_extendsettings-fieldname.
            WHEN 'SPEC'.
              "规格
              EXEC SQL.
                update MAT_MaterialVersion set SPEC = :ls_extendsettings-values
                   where code = :ls_extendsettings-matnr
                   and MAT_MaterialVersion.IsEffect='1';
              ENDEXEC.

            WHEN 'ENGLISHNAME'.
              "英文说明
              EXEC SQL.
                update MAT_MaterialVersion
                   set ENGLISHNAME = :ls_extendsettings-values
                   where code = :ls_extendsettings-matnr
                   and MAT_MaterialVersion.IsEffect='1';
              ENDEXEC.

            WHEN 'NAME'.
              "简体说明
              EXEC SQL.
                update MAT_MaterialVersion set NAME = :ls_extendsettings-values
                   where code = :ls_extendsettings-matnr
                   and MAT_MaterialVersion.IsEffect='1';
              ENDEXEC.
          ENDCASE.

        WHEN 'MAT_EXTEND'.
          "---->PLM 扩展字段栏位
          EXEC SQL.
            update MAT_Extend
            set PropertyValue= :ls_extendsettings-values
            where MAT_Extend.SettingsId IN
              ( select SettingsId from PS_ExtendSettings ps
               where ps.propertycode= :ls_extendsettings-propertycode )
             and ObjectId IN
              ( select MaterialVerId from MAT_MaterialVersion as mat
              where mat.Code= :ls_extendsettings-matnr
               and mat.IsEffect='1' )
          ENDEXEC.
        WHEN OTHERS.
          "PLM 扩展字段栏位、PLM扩展字段名称
          EXEC SQL PERFORMING append_data.
            select distinct propertycode,ExtendName
               INTO :wa_extendsettings
              from PS_ExtendSettings;
          ENDEXEC.
      ENDCASE.
    CATCH cx_sy_native_sql_error INTO exc_ref.
      lv_text = exc_ref->get_text( ).
  ENDTRY.

  IF NOT lv_text IS INITIAL.
    EXEC SQL.
      rollback
    ENDEXEC.
  ELSE.
*----> 已经调整到了 PERFORM call_bapi 中
*    EXEC SQL.
*      commit
*    ENDEXEC.
  ENDIF.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form APPEND_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM append_data .
  APPEND wa_extendsettings TO gt_extendsettings.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CONNECTION_PLM_DB
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM connection_plm_db .
  DATA:lv_connr TYPE c LENGTH 8.

  "指定PLM DB 数据库存
  IF sy-mandt = '888'.
    lv_connr = 'PLM'.
  ELSE.
    lv_connr = 'PLMTEST'.
  ENDIF.

  "连接PLM DB
  EXEC SQL.
    CONNECT TO :lv_connr
  ENDEXEC.


  EXEC SQL.
    SET CONNECTION :lv_connr
  ENDEXEC.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form CLOSE_PLM_DB
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM close_plm_db .

  DATA:lv_connr TYPE c LENGTH 8.

  "指定PLM DB 数据库存
  IF sy-mandt = '888'.
    lv_connr = 'PLM'.
  ELSE.
    lv_connr = 'PLMTEST'.
  ENDIF.

  "关闭连接
  EXEC SQL.
    DISCONNECT :lv_connr
  ENDEXEC.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GAIN_TITLE_CHECK
*&---------------------------------------------------------------------*
*& text 获取上传csv 标题,并检查是否有主键
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM gain_title_check  CHANGING lv_row.

  DATA:" lv_row  TYPE i , "内表行
        lv_doid TYPE i VALUE 1. "计数变量

  DATA: lt_field  TYPE TABLE OF ty_field.

  "获取内表行数
  DESCRIBE TABLE gt_title6 LINES lv_row.

  "因上传csv的标题中第一列是无效列,固循环的列次数 - 1
  lv_row = lv_row - 1.

  DO lv_row TIMES.
    lv_doid = lv_doid + 1.

    READ TABLE gt_title1 INTO DATA(ls_title1) INDEX lv_doid.
    READ TABLE gt_title2 INTO DATA(ls_title2) INDEX lv_doid.
    READ TABLE gt_title3 INTO DATA(ls_title3) INDEX lv_doid.
    READ TABLE gt_title5 INTO DATA(ls_title5) INDEX lv_doid.
    READ TABLE gt_title6 INTO DATA(ls_title6) INDEX lv_doid.

    "获取主键
    READ TABLE gt_field WITH KEY
                       tabname = ls_title5-value
                       TRANSPORTING NO FIELDS.
    IF sy-subrc <> 0.
      SELECT tabname fieldname fieldname AS fieldname2 keyflag
           APPENDING CORRESPONDING FIELDS OF TABLE gt_field
           FROM dd03l
           WHERE tabname = ls_title5-value
           AND     keyflag = 'X'
           AND     fieldname <> 'MATNR'
           AND     fieldname <> 'MANDT'.
      READ TABLE gt_field INTO DATA(wa_field) WITH KEY
                         fieldname = 'SPRAS'.
      IF sy-subrc = 0.
        PERFORM dynamic_sql USING wa_field-tabname ''.
        DELETE gt_field  WHERE fieldname = 'SPRAS'.
      ENDIF.
    ENDIF.

    "上传栏位检查
*--->检查上传表名与栏位名称关系是否正确
    READ TABLE gt_field WITH KEY
                       tabname = ls_title5-value
                       fieldname = ls_title6-value
                       TRANSPORTING NO FIELDS.
    IF sy-subrc <> 0.
      SELECT  tabname fieldname fieldname AS fieldname2 keyflag
           APPENDING CORRESPONDING FIELDS OF TABLE gt_field
           FROM dd03l
           WHERE tabname = ls_title5-value
           AND     fieldname = ls_title6-value.
      IF sy-subrc <> 0.
        "若是在系统查询不到该栏位,那么默认为是多语系栏位,,查询不到则为异常
        READ TABLE gt_t002 WITH KEY
                           tabname = ls_title5-value
                           fieldname = ls_title6-value.
*                           TRANSPORTING NO FIELDS.
        IF sy-subrc = 0.
          wa_field-tabname = ls_title5-value.
          wa_field-fieldname = ls_title6-value.
          wa_field-fieldname2 = gt_t002-fieldname2.
          wa_field-keyflag = ls_title2-value.
          wa_field-statm = ls_title3-value.
          wa_field-headertxt = ls_title1-value.
          APPEND wa_field TO gt_field.CLEAR:wa_field.
        ELSEIF sy-subrc <> 0 AND lv_doid = 2.
          wa_field-tabname = 'MARA'.
          wa_field-fieldname = 'MATNR'.
          wa_field-fieldname2 = 'MATNR'.
          wa_field-keyflag = 'X'.
          wa_field-statm = ls_title3-value.
          wa_field-headertxt = ls_title1-value.
          APPEND wa_field TO gt_field.CLEAR:wa_field.
        ELSE.
          MESSAGE '上传栏位异常,表与栏位间关系异常' && ls_title5-value &&
                          ls_title6-value TYPE 'E'.
          STOP.
        ENDIF.
      ENDIF.
    ENDIF.

*--->检查上传栏位是否重复
    READ TABLE lt_field INTO DATA(ls_field) WITH KEY
                       tabname = ls_title5-value
                       fieldname = ls_title6-value.
    IF sy-subrc = 0.
      MESSAGE lv_doid && '列与'  && ls_field-col && '列栏位重复' &&
                     ls_title5-value && '-' && ls_title6-value TYPE 'E'.
      SORT.
    ELSE.
      ls_field-tabname = ls_title5-value.
      ls_field-fieldname = ls_title6-value.
      ls_field-col = lv_doid.
      APPEND ls_field TO lt_field.CLEAR:ls_field.
    ENDIF.

*--->logo 栏位用于检查上传栏位与系统栏位是否不一致,判断修改的栏位是否缺少主键
    IF  ls_title1-value IS INITIAL AND ls_title6-value IS NOT INITIAL.
      MESSAGE '上传表头格式错误,请删除无效列' && lv_doid && '' TYPE 'E'.
      STOP.
    ELSEIF  lv_doid <> 2 AND ls_title3-value IS INITIAL .
      MESSAGE '上传表头格式错误,' && lv_doid && '列未填写view 栏位,请填写后再上传' TYPE 'E'.
      STOP.
    ELSEIF  lv_doid = 2 AND ls_title6-value <> 'MATNR'.
      MESSAGE '上传表头格式错误,不要删除模板的第一列' TYPE 'E'.
      STOP.
    ENDIF.

*--->检查 view 与栏位间关系是否正确
    READ TABLE gt_field INTO wa_field WITH KEY
                       tabname = ls_title5-value
                       fieldname = ls_title6-value.
    IF sy-subrc = 0.
      CLEAR:zsapfield.
      SELECT SINGLE * INTO zsapfield
      FROM zsapfield
      WHERE saptablename = ls_title5-value
      AND     sapfieldname = wa_field-fieldname2
      AND     funstatm = ls_title3-value.
      IF sy-subrc <> 0 AND
            ls_title6-value <> 'MATNR' AND
            ls_title5-value <> 'MARA'.
        MESSAGE '导入了非法view,检查view栏位是否正确' && lv_doid && '' TYPE 'E'.
        STOP.
      ENDIF.

      wa_field-funtablename = zsapfield-funtablename.
      wa_field-funfieldname = zsapfield-funfieldname.
      wa_field-logo = 'X'.
      wa_field-col = lv_doid.
      wa_field-statm = ls_title3-value.
      wa_field-headertxt = ls_title1-value.
      MODIFY gt_field FROM wa_field TRANSPORTING
                   funtablename funfieldname logo
                   col statm headertxt
                   WHERE tabname = ls_title5-value
                       AND fieldname = ls_title6-value.
    ELSE.
      MESSAGE '导入了非法view,检查view栏位是否正确' && lv_doid && '' TYPE 'E'.
      STOP.
    ENDIF.

    SELECT SINGLE * FROM zsapfield
    WHERE saptablename = ls_title5-value
    AND     sapfieldname = ls_title6-value
    AND     funstatm = ls_title3-value.
    IF sy-subrc <> 0 AND
        ls_title6-value <> 'MATNR' AND
        ls_title5-value <> 'MARA'.
      READ TABLE gt_t002 WITH KEY
                         tabname = ls_title5-value
                         fieldname = ls_title6-value
                         TRANSPORTING NO FIELDS.
      IF sy-subrc <> 0.
        MESSAGE '导入了非法view,检查view栏位是否正确' && lv_doid && '' TYPE 'E'.
        STOP.
      ENDIF.
    ENDIF.

    CLEAR:ls_title1,ls_title2,ls_title3,ls_title5,ls_title6.
  ENDDO.

ENDFORM.

 

posted on 2023-01-05 17:50  淡淡-祥  阅读(194)  评论(0编辑  收藏  举报