其他分享
首页 > 其他分享> > ABAP-PP BOM主数据批量创建

ABAP-PP BOM主数据批量创建

作者:互联网

需求:PP模块BOM主数据批量创建修改。

批量创建BOM主数据BAPI:BAPI_MATERIAL_BOM_GROUP_CREATE
批量修改BOM主数据BAPI:CSAP_MAT_BOM_MAINTAIN
CS01:创建BOM主数据
CS02:更改BOM主数据
CS03:查看BOM主数据

批量创建/修改代码如下:

*&---------------------------------------------------------------------*
*& Report ZTEST_007
*&---------------------------------------------------------------------*
*&Bom主数据批导Demo
*&---------------------------------------------------------------------*
REPORT ztest_007.

* 类型池
*----------------------------------------------------------------------*
TYPE-POOLS: slis,icon.
*----------------------------------------------------------------------*
* 数据库表声明
*----------------------------------------------------------------------*
TABLES: sscrfields. "选择屏幕上的字段

*----------------------------------------------------------------------*
* 类型声明
*----------------------------------------------------------------------*
TYPES:BEGIN OF ty_alv,
        werks   TYPE mast-werks,  "工厂
        datuv   TYPE stko-datuv,  "有效起始日期
        matnr   TYPE mast-matnr,  "父项物料编码
        bmeng   TYPE stko-bmeng,  "父项基本数量
        bmein   TYPE stko-bmein,  "父项基本计量单位
        posnr   TYPE stpo-posnr,  "BOM行项目编号
        postp   TYPE stpo-postp,  "BOM行项目类别
        idnrk   TYPE stpo-idnrk,  "子项物料编码
        menge   TYPE stpo-menge,  "子项数量
        meins   TYPE stpo-meins,  "子项计量单位
        icon    TYPE icon_d,      "红绿灯
        message TYPE bapi_msg,    "消息
      END OF ty_alv.

TYPES:BEGIN OF ty_mast,
        matnr TYPE mast-matnr,
        werks TYPE mast-werks,
        stlan TYPE mast-stlan, "BOM用途
        stlnr TYPE mast-stlnr, "物料单
        stlal TYPE mast-stlal, "备选物料清单
      END OF ty_mast.


*----------------------------------------------------------------------*
* 数据对象声明
*----------------------------------------------------------------------*
DATA: gt_out   TYPE TABLE OF ty_alv, "Excel上传的数据
      gs_out   TYPE ty_alv,
      gv_error TYPE c,
      gt_data1 TYPE TABLE OF ty_alv, "创建BOM
      gt_data2 TYPE TABLE OF ty_alv. "修改BOM

DATA:gt_mast   TYPE TABLE OF ty_mast."已有BOM

"选择界面下载按钮参数
DATA: functxt TYPE smp_dyntxt.

*----------------------------------------------------------------------*
* ALV变量
*----------------------------------------------------------------------*
DATA: gs_layout   TYPE lvc_s_layo, " ALV布局
      gt_fieldcat TYPE TABLE OF lvc_s_fcat,
      gs_fieldcat TYPE lvc_s_fcat.

*----------------------------------------------------------------------*
* FIELD-SYMBOLS声明
*----------------------------------------------------------------------*
FIELD-SYMBOLS: <fs_out> TYPE ty_alv.

*&---------------------------------------------------------------------*
*& Selection Screen/选择屏幕
*&---------------------------------------------------------------------*
*&------选择文件
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS p_path LIKE rlgrap-filename  MEMORY ID m.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-003.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(79) warning1.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(75) warning2.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(75) warning3.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(75) warning4.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN:END OF BLOCK b3.

*&---------------------------------------------------------------------*
*& INITIALIZATION/选择屏幕前初始化
*&---------------------------------------------------------------------*
INITIALIZATION.
  SELECTION-SCREEN: FUNCTION KEY 1.""在屏幕定义功能码

  "选择屏幕下载按钮
  functxt-icon_id   = icon_export.
  functxt-icon_text = '模板下载'.
  sscrfields-functxt_01 = functxt.


  warning1 = '【1】请勿对下载模板的列顺序进行变更!'.
  warning2 = '【2】请使用下载功能下载模版进行后续操作!'.
  warning3 = '【3】请使用日期格式YYYYMMDD ! 例:20220131'.
  warning4 = '【4】空白默认不修改该字段! 如需置空,请填写#'.

*&---------------------------------------------------------------------*
*& at selection-screen/选择屏幕开始
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path .
  PERFORM frm_f4_filename.

AT SELECTION-SCREEN.
  IF sscrfields-ucomm = 'FC01'.
    DATA: lv_filename TYPE string,
          lv_path     TYPE string,
          lv_fullpath TYPE string.

    PERFORM frm_file_save USING 'BOM主数据批量导入模版.xlsx'
                       CHANGING lv_filename
                                lv_path
                                lv_fullpath.
    IF lv_fullpath IS NOT INITIAL.
      PERFORM frm_download_template USING lv_fullpath .
    ENDIF.
    CLEAR sscrfields-ucomm .
  ENDIF.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN .
    IF screen-name = 'P_PATH'.
      screen-required = '2'. "假必输
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

*&---------------------------------------------------------------------*
*& Start-of-selection/开始选择屏幕
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  IF p_path IS INITIAL.
    MESSAGE s055(00) DISPLAY LIKE 'E'.
    EXIT.
  ENDIF.

  PERFORM frm_upload_data.
  IF gt_out IS INITIAL.
    MESSAGE '无数据!' TYPE 'S' DISPLAY LIKE 'E'.
    EXIT.
  ENDIF.
  PERFORM frm_check_data.
  IF gv_error IS INITIAL.
    gt_data1 = gt_out.
    gt_data2 = gt_out.
    SORT gt_data1 BY matnr werks.
    DELETE ADJACENT DUPLICATES FROM gt_data1 COMPARING matnr werks.

    SORT gt_data2 BY matnr werks.
    DELETE ADJACENT DUPLICATES FROM gt_data2 COMPARING matnr werks.
    "判断BOM是否已存在
    IF gt_data1 IS NOT INITIAL.
      SELECT  matnr   ,
              werks   ,
              stlan   ,
              stlnr   ,
              stlal
          FROM mast
          INTO TABLE @DATA(lt_mast)
          FOR ALL ENTRIES IN @gt_data1
          WHERE matnr = @gt_data1-matnr
          AND   werks = @gt_data1-werks.
      SORT lt_mast BY matnr werks.
    ENDIF.
    "删除已存在BOM
    LOOP AT gt_data1 INTO gs_out  .
      READ TABLE lt_mast INTO DATA(ls_mast) WITH KEY matnr = gs_out-matnr
                                                     werks = gs_out-werks BINARY SEARCH.
      IF  sy-subrc = 0.
        DELETE gt_data1[].
      ENDIF.
      CLEAR:gs_out.
    ENDLOOP.
    LOOP AT gt_data2 INTO gs_out  .
      READ TABLE lt_mast INTO DATA(ls_mast2) WITH KEY matnr = gs_out-matnr
                                                      werks = gs_out-werks BINARY SEARCH.
      IF  sy-subrc = 0.
        DELETE gt_data2[].
      ENDIF.
      CLEAR:gs_out.
    ENDLOOP.
    PERFORM frm_call_bapi USING 'X'.
  ENDIF.

  PERFORM frm_set_layout.
  PERFORM frm_set_fieldcat.
  PERFORM frm_display_alv.




*&---------------------------------------------------------------------*
*&      Form  set_pf_status
*&---------------------------------------------------------------------*
*       定义界面状态栏
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STATUS'. "自定义状态名称
ENDFORM. "set_pf_status

*&---------------------------------------------------------------------*
*&      Form  alv_user_command
*&---------------------------------------------------------------------*
*       响应状态触发事件
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM alv_user_command USING r_ucomm LIKE sy-ucomm
      rs_selfield TYPE slis_selfield.

  DATA:ref_grid TYPE REF TO cl_gui_alv_grid,
       ls_stbl  TYPE lvc_s_stbl.

  ls_stbl-row = 'X'.
  ls_stbl-col = 'X'.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = ref_grid.  "获取全局变量

  CALL METHOD ref_grid->check_changed_data. "获取响应事件
  rs_selfield-refresh = 'X'.                "刷新界面
  CASE r_ucomm.
    WHEN 'ZCL'.
      READ TABLE gt_out INTO gs_out WITH KEY icon = icon_red_light.
      IF sy-subrc = 0.
        CLEAR:gs_out.
        MESSAGE '上载的数据中存在错误,请先处理错误再继续维护!' TYPE 'S' DISPLAY LIKE 'E'.
        EXIT.
      ENDIF.
      READ TABLE gt_out INTO gs_out WITH KEY icon = icon_green_light.
      IF sy-subrc = 0.
        CLEAR:gs_out.
        MESSAGE '不可重复处理!' TYPE 'S' DISPLAY LIKE 'E'.
        EXIT.
      ENDIF.
      IF gt_data1 IS NOT INITIAL.
        "创建BOM
        PERFORM frm_call_bapi USING ''.
      ENDIF.
      IF gt_data2 IS NOT INITIAL.
        "修改BOM
        PERFORM frm_call_bapi2.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.

  CALL METHOD ref_grid->check_changed_data. "获取响应事件

  CALL METHOD ref_grid->refresh_table_display
    EXPORTING
      is_stable      = ls_stbl
      i_soft_refresh = 'X'
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.


ENDFORM.

*&---------------------------------------------------------------------*
*& Form FRM_CHECK_DATA
*&---------------------------------------------------------------------*
*& 数据校验
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_check_data .
  CLEAR:gv_error.
  LOOP AT gt_out ASSIGNING <fs_out>.
    IF <fs_out>-matnr IS INITIAL OR <fs_out>-werks IS INITIAL.
      <fs_out>-icon = icon_red_light.
      <fs_out>-message = '物料和工厂必填!'.
      gv_error = 'X'.
    ENDIF.
    CLEAR:<fs_out>.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_F4_FILENAME
*&---------------------------------------------------------------------*
*& 文件搜索帮助
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_f4_filename .
  CALL FUNCTION 'F4_FILENAME'
*   EXPORTING
*     PROGRAM_NAME        = SYST-CPROG
*     DYNPRO_NUMBER       = SYST-DYNNR
*     FIELD_NAME          = ' '
    IMPORTING
      file_name = p_path.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILE_SAVE
*&---------------------------------------------------------------------*
*& 保存文件弹出窗口
*&---------------------------------------------------------------------*
*&      --> P_
*&      <-- LV_FILENAME
*&      <-- LV_PATH
*&      <-- LV_FULLPATH
*&---------------------------------------------------------------------*
FORM frm_file_save  USING    VALUE(p_text)
                    CHANGING lv_filename
                             lv_path
                             lv_fullpath.
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
*     WINDOW_TITLE              =
      default_extension         = 'XLSX'
      default_file_name         = p_text
*     WITH_ENCODING             =
*     FILE_FILTER               =
*     INITIAL_DIRECTORY         =
*     PROMPT_ON_OVERWRITE       = 'X'
    CHANGING
      filename                  = lv_filename
      path                      = lv_path
      fullpath                  = lv_fullpath
*     USER_ACTION               =
*     FILE_ENCODING             =
    EXCEPTIONS
      cntl_error                = 1
      error_no_gui              = 2
      not_supported_by_gui      = 3
      invalid_default_file_name = 4
      OTHERS                    = 5.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
*& 下载模板
*&---------------------------------------------------------------------*
*&      --> LV_FULLPATH
*&---------------------------------------------------------------------*
FORM frm_download_template USING lv_fullpath.
  DATA:ls_key         TYPE wwwdatatab,
       lv_rc          TYPE sy-subrc,
       lv_destination LIKE rlgrap-filename.

  lv_destination = lv_fullpath .

  SELECT SINGLE relid
                objid
    INTO CORRESPONDING FIELDS OF ls_key
    FROM wwwdata
    WHERE srtf2 = 0
    AND relid = 'MI'
    AND objid = 'ZMM003'.
  IF sy-subrc <> 0.
    MESSAGE '模板文件 ZMM003 不存在!' TYPE 'S' DISPLAY LIKE 'E'.
    EXIT.
  ENDIF.

  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = ls_key
      destination = lv_destination
    IMPORTING
      rc          = lv_rc
* CHANGING
*     TEMP        =
    .
  IF lv_rc <> 0.
    MESSAGE '模板文件 ZMM003 下载失败,请与相关人员联系!' TYPE 'S' DISPLAY LIKE 'E'.
    EXIT.
  ELSE.
    MESSAGE '下载成功!' TYPE 'S'.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*& 上传Excel数据到内表
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_upload_data .
  DATA: lt_exc_data TYPE zalsmex_tabline OCCURS 0 WITH HEADER LINE,
        lv_index    TYPE sy-tabix.
  DATA: BEGIN OF lt_split OCCURS 0,
          field TYPE c LENGTH 50,
        END OF lt_split.

  FIELD-SYMBOLS: <fs_value>.

  CLEAR: lt_exc_data[],
         gt_out,
         gs_out.

  CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_path
      i_begin_col             = 1
      i_begin_row             = 8
      i_end_col               = 200
      i_end_row               = 50000
    TABLES
      intern                  = lt_exc_data
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  LOOP AT lt_exc_data.
    lv_index = lt_exc_data-col.

    ASSIGN COMPONENT lv_index OF STRUCTURE gs_out TO <fs_value>.
    MOVE lt_exc_data-value TO <fs_value>.

    CLEAR lt_exc_data.

    AT END OF row.
      "单位转换
      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input          = gs_out-meins
          language       = sy-langu
        IMPORTING
          output         = gs_out-meins
        EXCEPTIONS
          unit_not_found = 1
          OTHERS         = 2.
      "单位转换
      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input          = gs_out-bmein
          language       = sy-langu
        IMPORTING
          output         = gs_out-bmein
        EXCEPTIONS
          unit_not_found = 1
          OTHERS         = 2.


      "物料号
      IF gs_out-matnr IS NOT INITIAL.
        CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
          EXPORTING
            input        = gs_out-matnr
          IMPORTING
            output       = gs_out-matnr
          EXCEPTIONS
            length_error = 1
            OTHERS       = 2.
      ENDIF.
      APPEND gs_out TO gt_out.
      CLEAR gs_out.
    ENDAT.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CALL_BAPI
*&---------------------------------------------------------------------*
*& 创建BOM
*&---------------------------------------------------------------------*
*&      --> P_
*&---------------------------------------------------------------------*
FORM frm_call_bapi  USING lv_test.
  "定义BAPI变量
  DATA:lt_bomgroup          TYPE TABLE OF bapi1080_bgr_c,
       ls_bomgroup          TYPE bapi1080_bgr_c,

       lt_variants          TYPE TABLE OF bapi1080_bom_c,
       ls_variants          TYPE bapi1080_bom_c,

       lt_items             TYPE TABLE OF bapi1080_itm_c,
       ls_items             TYPE bapi1080_itm_c,

       lt_materialrelations TYPE TABLE OF bapi1080_mbm_c,
       ls_materialrelations TYPE bapi1080_mbm_c,

       lt_itemassignments   TYPE TABLE OF bapi1080_rel_itm_bom_c,
       ls_itemassignments   TYPE bapi1080_rel_itm_bom_c,

       ls_testrun           TYPE bapiflag , "模拟运行BAPI

       lt_return            TYPE TABLE OF bapiret2, "返回消息
       ls_return            TYPE bapiret2.

  DATA:lt_data  TYPE TABLE OF ty_alv,
       lv_lines TYPE i, "总行数
       lv_index TYPE i,
       lv_msg   TYPE string.

  lt_data = gt_data1.
  DESCRIBE TABLE lt_data LINES lv_lines.
  CLEAR:lv_index.

  LOOP AT lt_data INTO gs_out.
    "进度条
    lv_index = lv_index + 1.
    lv_msg  = 'BOM创建处理中...' && lv_index &&  '/' && lv_lines.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        percentage = 25
        text       = lv_msg.

    "抬头字段
    CLEAR:ls_bomgroup.
    ls_bomgroup-bom_group_identification = 'BAPI_SMP_COL1'."标识物料单组
    ls_bomgroup-object_type              = 'BGR'."BOM 组中的对象类型
    ls_bomgroup-object_id                = 'SIMPLE1'."BOM 组中对象的标识
    ls_bomgroup-technical_type           = ''."技术类型
    ls_bomgroup-ltxt_lang                = sy-langu.
    ls_bomgroup-bom_usage                = '1'."  BOM 用途
    ls_bomgroup-created_in_plant         = gs_out-werks."工厂
    ls_bomgroup-bom_text                 = ''."物料清单描述
    APPEND ls_bomgroup TO lt_bomgroup.

    CLEAR:ls_variants.
    ls_variants-bom_group_identification = 'BAPI_SMP_COL1'.
    ls_variants-object_type              = 'BOM'.
    ls_variants-object_id                = 'SIMPLE1'.
    ls_variants-alternative_bom          = '01'."备选物料清单 (备选BOM)
    ls_variants-base_qty                 = gs_out-bmeng."父项基本数量
    ls_variants-base_unit                = gs_out-bmein."父项基本计量单位
    ls_variants-valid_from_date          = gs_out-datuv."有效起始日期
    ls_variants-function                 = 'NEW'."创建

    CLEAR:ls_materialrelations.
    ls_materialrelations-bom_group_identification = 'BAPI_SMP_COL1'.
    ls_materialrelations-material                 = gs_out-matnr."父项物料编码
    ls_materialrelations-material_long            = gs_out-matnr.
    ls_materialrelations-bom_usage                = '1'."BOM用途
    ls_materialrelations-alternative_bom          = '01'."可选BOM
    ls_materialrelations-plant                    = gs_out-werks."工厂
    APPEND ls_materialrelations TO lt_materialrelations.

    "行项目字段
    LOOP AT gt_out INTO DATA(ls_data) WHERE matnr = gs_out-matnr
                                       AND   werks = gs_out-werks.
      CLEAR:ls_items.
      ls_items-bom_group_identification = 'BAPI_SMP_COL1'.
      ls_items-object_type              = 'ITM'.
      ls_items-object_id                = 'SIMPLE1'.
      "BOM行项目编号
      CALL FUNCTION 'CONVERSION_EXIT_NUMCV_INPUT'
        EXPORTING
          input  = ls_data-posnr
        IMPORTING
          output = ls_items-item_no.
      ls_items-item_cat             = ls_data-postp."BOM行项目类别
      ls_items-item_id              = ls_data-posnr.
      ls_items-component            = ls_data-idnrk."子项目物料编码
      ls_items-component_long       = ls_data-idnrk.
      ls_items-comp_qty             = ls_data-menge."子项数量
      ls_items-comp_unit            = ls_data-meins."子项计量单位
      ls_items-ltxt_lang            = sy-langu.
      ls_items-valid_from_date      = ls_data-datuv."有效起始日期
      APPEND ls_items TO lt_items.

      CLEAR:ls_itemassignments.
      ls_itemassignments-bom_group_identification = 'BAPI_SMP_COL1'.
      ls_itemassignments-sub_object_type          = 'ITM'.
      ls_itemassignments-sub_object_id            = 'SIMPLE1'.
      ls_itemassignments-super_object_type        = 'BOM'.
      ls_itemassignments-super_object_id          = 'SIMPLE1'.
      ls_itemassignments-valid_from_date          = sy-datum."有效起始日期
      ls_itemassignments-function                 = 'NEW'.
      APPEND ls_itemassignments TO lt_itemassignments.
      CLEAR:ls_data.
    ENDLOOP.

    IF lv_test = 'X'.
      ls_testrun-bapiflag = 'X'.
      CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
        EXPORTING
          testrun           = ls_testrun
          all_error         = 'X'
        TABLES
          bomgroup          = lt_bomgroup
          variants          = lt_variants
          items             = lt_items
*         SUBITEMS          =
          materialrelations = lt_materialrelations
          itemassignments   = lt_itemassignments
*         SUBITEMASSIGNMENTS       =
*         TEXTS             =
          return            = lt_return.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
        LOOP AT gt_out ASSIGNING <fs_out>.
          CONCATENATE <fs_out>-message ls_return-message INTO <fs_out>-message SEPARATED BY ';'.
          SHIFT <fs_out>-message LEFT DELETING LEADING ';'.
          <fs_out>-icon = icon_red_light.
          CLEAR:<fs_out>.
        ENDLOOP.
        CLEAR:ls_return.
      ENDLOOP.
    ELSE.
      CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
        EXPORTING
*         testrun           = ls_testrun
          all_error         = 'X'
        TABLES
          bomgroup          = lt_bomgroup
          variants          = lt_variants
          items             = lt_items
*         SUBITEMS          =
          materialrelations = lt_materialrelations
          itemassignments   = lt_itemassignments
*         SUBITEMASSIGNMENTS       =
*         TEXTS             =
          return            = lt_return.
      IF line_exists( lt_return[ type = 'E' ] ) OR line_exists( lt_return[ type = 'A' ] ).
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
          LOOP AT gt_out ASSIGNING <fs_out>.
            CONCATENATE <fs_out>-message ls_return-message INTO <fs_out>-message SEPARATED BY ';'.
            SHIFT <fs_out>-message LEFT DELETING LEADING ';'.
            <fs_out>-icon = icon_red_light.
            CLEAR:<fs_out>.
          ENDLOOP.
          CLEAR:ls_return.
        ENDLOOP.
      ELSE.
        LOOP AT gt_out ASSIGNING <fs_out>.
          <fs_out>-message = '创建成功!'.
          <fs_out>-icon = icon_green_light.
          CLEAR:<fs_out>.
        ENDLOOP.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
      ENDIF.
    ENDIF.
    REFRESH:lt_bomgroup,lt_variants,lt_items,
            lt_materialrelations,lt_itemassignments,lt_return.
    CLEAR:gs_out.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_layout .
  CLEAR gs_layout.
  gs_layout-sel_mode = 'D'.   "设置行模式"
  gs_layout-cwidth_opt = 'X'.  "优化列宽设置"
  gs_layout-zebra = 'X'.       "设置斑马线"
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_fieldcat .
  PERFORM frm_get_fieldcat USING 'ICON  ' ' 指示灯' ' ' ' ' ' '.
  PERFORM frm_get_fieldcat USING 'MESSAGE' '消息 ' ' ' ' ' ' '.
  PERFORM frm_get_fieldcat USING 'WERKS ' '工厂' 'MAST' 'WERKS ' ' '.
  PERFORM frm_get_fieldcat USING 'DATUV ' '有效起始日期' 'STKO' 'DATUV ' ' '.
  PERFORM frm_get_fieldcat USING 'MATNR ' '父项物料编码' 'MAST' 'MATNR ' ' '.
  PERFORM frm_get_fieldcat USING 'BMENG ' '父项基本数量' 'STKO' 'BMENG ' ' '.
  PERFORM frm_get_fieldcat USING 'BMEIN ' '父项基本计量单位' 'STKO' 'BMEIN ' ' '.
  PERFORM frm_get_fieldcat USING 'POSNR ' 'BOM行项目编号' 'STPO' 'POSNR ' ' '.
  PERFORM frm_get_fieldcat USING 'POSTP ' 'BOM行项目类别' 'STPO' 'POSTP ' ' '.
  PERFORM frm_get_fieldcat USING 'IDNRK ' '子项物料编码' 'STPO' 'IDNRK ' ' '.
  PERFORM frm_get_fieldcat USING 'MENGE ' '子项数量' 'STPO' 'MENGE ' ' '.
  PERFORM frm_get_fieldcat USING 'MEINS ' '子项计量单位' 'STPO' 'MEINS ' ' '.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> P_
*&      --> P_
*&      --> P_
*&      --> P_
*&      --> P_
*&---------------------------------------------------------------------*
FORM frm_get_fieldcat  USING    VALUE(p_field)
                                VALUE(p_name)
                                VALUE(p_reftable)
                                VALUE(p_reffield)
                                VALUE(p_edit).
  gs_fieldcat-just = 'C'.
  gs_fieldcat-fieldname = p_field.
  gs_fieldcat-coltext   = p_name.
  gs_fieldcat-seltext   = p_name.
  gs_fieldcat-ref_table = p_reftable.
  gs_fieldcat-ref_field = p_reffield.
  gs_fieldcat-edit      = p_edit.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR:gs_fieldcat.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
*报表展示
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid         "当前程序
      i_callback_pf_status_set = 'SET_PF_STATUS'
      i_callback_user_command  = 'ALV_USER_COMMAND'
      is_layout_lvc            = gs_layout     "界面格式"
      it_fieldcat_lvc          = gt_fieldcat    "字段属性"
    TABLES
      t_outtab                 = gt_out
    EXCEPTIONS
      program_error            = 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.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CALL_BAPI2
*&---------------------------------------------------------------------*
*& 修改BOM
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_call_bapi2 .
  DATA:lv_material    TYPE csap_mbom-matnr,
       lv_plant       TYPE csap_mbom-werks,
       lv_bom_usage   TYPE csap_mbom-stlan, "BOM用途
       lv_valid_from  TYPE csap_mbom-datuv, "有效起始日期
       lv_alternative TYPE csap_mbom-stlal, "备选物料清单
       ls_stko        TYPE stko_api01, "BOM头数据
       ls_o_stko      TYPE stko_api02, "输出HEADER
       lv_warning     TYPE capiflag-flwarning,
       lt_stpo        TYPE TABLE OF stpo_api03,
       ls_stpo        TYPE stpo_api03.

  DATA:lt_data2  TYPE TABLE OF ty_alv,
       lv_lines2 TYPE i, "总行数
       lv_index2 TYPE i,
       lv_msg2   TYPE string.

  lt_data2 = gt_data2.
  DESCRIBE TABLE lt_data2 LINES lv_lines2.
  CLEAR:lv_index2.

  IF lt_data2 IS NOT INITIAL .
    "获取子项目节点
    SELECT  mast~matnr,
            mast~werks,
            stpo~stlty,
            stpo~stlnr,
            stpo~stlkn,
            stpo~stpoz
        FROM mast
        LEFT JOIN stpo
        ON stpo~stlnr = mast~stlnr
        INTO TABLE @DATA(lt_stpo3)
        FOR ALL ENTRIES IN @lt_data2
        WHERE mast~matnr =  @lt_data2-matnr
        AND   mast~werks =  @lt_data2-werks.
    SORT lt_stpo3 BY stlnr stpoz.

    "获取备选BOM
    SELECT  matnr ,
            werks ,
            stlan ,
            stlnr ,
            stlal
        FROM mast
        INTO TABLE @DATA(lt_stlal)
        FOR ALL ENTRIES IN @lt_data2
        WHERE matnr =  @lt_data2-matnr
        AND   werks =  @lt_data2-werks.
    SORT lt_stlal BY matnr werks.
  ENDIF.

  LOOP AT lt_data2 INTO gs_out.
    "进度条
    lv_index2 = lv_index2 + 1.
    lv_msg2  = 'BOM修改处理中...' && lv_index2 &&  '/' && lv_lines2.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        percentage = 25
        text       = lv_msg2.

    "抬头数据
    lv_material = gs_out-matnr."父项物料
    lv_plant    = gs_out-werks."工厂
    lv_bom_usage = '1'."BOM用途
    lv_valid_from = gs_out-datuv."有效起始日期
    ls_stko-bom_status = '01' .
    ls_stko-base_quan  = gs_out-bmeng."父项基本数量
    ls_stko-base_unit  = gs_out-bmein."父项基本计量单位

    "行项目数据
    LOOP AT gt_out INTO DATA(ls_data) WHERE matnr = gs_out-matnr
                                        AND  werks = gs_out-werks.
      CLEAR:ls_stpo.
      CALL FUNCTION 'CONVERSION_EXIT_NUMCV_INPUT'
        EXPORTING
          input  = ls_data-posnr
        IMPORTING
          output = ls_stpo-item_no. "BOM行项目编号

      LOOP AT lt_stpo3 INTO DATA(ls_stpo3) WHERE matnr = gs_out-matnr
                                            AND  werks = gs_out-werks..
        ls_stpo-item_node = ls_stpo3-stlnr. "行项目节点
        ls_stpo-item_count = ls_stpo3-stpoz.
        DELETE lt_stpo3[].
        CLEAR:ls_stpo3.
        EXIT.
      ENDLOOP.

      ls_stpo-item_categ = ls_data-postp."BOM行项目类别
      ls_stpo-component  = ls_data-idnrk."子项目物料
      ls_stpo-comp_qty   = ls_data-menge."子项数量
      ls_stpo-comp_unit  = ls_data-meins."子项计量单位
      APPEND ls_stpo TO lt_stpo.
      CLEAR:ls_data.
    ENDLOOP.

    CLEAR:lv_alternative.
    READ TABLE lt_stlal INTO DATA(ls_stlal) WITH KEY matnr = gs_out-matnr
                                                     werks = gs_out-werks BINARY SEARCH.
    IF sy-subrc = 0.
      lv_alternative = ls_stlal-stlal."备选物料清单
      CLEAR:ls_stlal.
    ENDIF.

    CALL FUNCTION 'CSAP_MAT_BOM_MAINTAIN'
      EXPORTING
        material           = lv_material
        plant              = lv_plant
        bom_usage          = lv_bom_usage
        alternative        = lv_alternative
        valid_from         = lv_valid_from
*       CHANGE_NO          =
*       REVISION_LEVEL     =
        i_stko             = ls_stko
*       FL_NO_CHANGE_DOC   = ' '
        fl_commit_and_wait = 'X'
*       FL_CAD             = ' '
        fl_bom_create      = 'X'
        fl_new_item        = 'X'
        fl_complete        = 'X'
*       FL_DEFAULT_VALUES  = 'X'
*       FL_IDENTIFY_BY_GUID       = ' '
*       FL_RECURSIVE       = ' '
      IMPORTING
        fl_warning         = lv_warning
        o_stko             = ls_o_stko
      TABLES
        t_stpo             = lt_stpo
      EXCEPTIONS
        error              = 1
        OTHERS             = 2.
    IF sy-subrc <> 0.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      DATA:l_return(50) TYPE c.
      CLEAR:l_return.

      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          msgid               = sy-msgid
          msgnr               = sy-msgno
          msgv1               = sy-msgv1
          msgv2               = sy-msgv2
          msgv3               = sy-msgv3
          msgv4               = sy-msgv4
        IMPORTING
          message_text_output = l_return.
      LOOP AT gt_out ASSIGNING FIELD-SYMBOL(<fs_out4>) WHERE matnr = gs_out-matnr
                                                         AND werks = gs_out-werks .
        <fs_out4>-message = <fs_out4>-message && l_return.
        <fs_out4>-icon    = icon_red_light.
      ENDLOOP.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.

      LOOP AT gt_out ASSIGNING <fs_out4> WHERE matnr = gs_out-matnr
                                                   AND werks = gs_out-werks .
        <fs_out4>-message = '修改成功!'.
        <fs_out4>-icon    = icon_green_light.
      ENDLOOP.
    ENDIF.

  ENDLOOP.

ENDFORM.

 

标签:PP,gs,&---------------------------------------------------------------------
来源: https://www.cnblogs.com/1502762920-com/p/16640283.html