BOM创建和修改

BOM创建和修改
FUNCTION z_rfc_plm_007.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(P_MODE) TYPE  CHAR1 DEFAULT 'N'
*"     VALUE(P_SYSTEM) TYPE  CHAR4 DEFAULT 'PLM'
*"     VALUE(P_UNAME) TYPE  UNAME DEFAULT SY-UNAME
*"     VALUE(P_NAME1) TYPE  CHAR12 OPTIONAL
*"  TABLES
*"      IT_PLM007 STRUCTURE  ZRFCS_PLM007 OPTIONAL
*"----------------------------------------------------------------------
  DATA:it_bomgroup  LIKE bapi1080_bgr_c OCCURS 0 WITH HEADER LINE,
       it_variants  LIKE bapi1080_bom_c OCCURS 0 WITH HEADER LINE,
       it_items     LIKE bapi1080_itm_c OCCURS 0 WITH HEADER LINE,
       it_matrel    LIKE bapi1080_mbm_c OCCURS 0 WITH HEADER LINE,
       it_itemas    LIKE bapi1080_rel_itm_bom_c OCCURS 0 WITH HEADER LINE,
       it_return    LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
       it_subitems  LIKE bapi1080_sui_c OCCURS 0 WITH HEADER LINE,
       it_subitemas LIKE bapi1080_rel_sui_itm_c OCCURS 0 WITH HEADER LINE.
  DATA: lv_meins    TYPE mara-meins,
        lv_potx1    TYPE potx1,
        lv_zmessage TYPE char255,
        lv_potx2    TYPE potx2,
        lv_matnr    TYPE char18,
        lv_end      TYPE char1,
        lv_begin    TYPE char1,
        lv_ztype    TYPE char1,
        lv_len      TYPE p,
        lv_tabixu   TYPE sy-tabix.
  DATA: json_ser TYPE REF TO cl_trex_json_serializer,
        json_des TYPE REF TO cl_trex_json_deserializer.
  DATA: jsonstr TYPE string.
  DATA: lt_zrfc_logs TYPE TABLE OF zrfc_logs,
        ls_zrfc_logs TYPE zrfc_logs.
  DATA: lv_tabix TYPE sy-tabix.
  DATA: date(10) TYPE c.
  DATA t_stpo TYPE STANDARD TABLE OF stpo_api02 WITH HEADER LINE.
  DATA t_stpo2 TYPE STANDARD TABLE OF stpo_api02 WITH HEADER LINE.
  CREATE OBJECT json_ser
    EXPORTING
      data = it_plm007[].
  CALL METHOD json_ser->serialize.
  CALL METHOD json_ser->get_data
    RECEIVING
      rval = jsonstr.

  ls_zrfc_logs-funcname   = 'Z_RFC_PLM_007'.
  ls_zrfc_logs-zsystem    = p_system.
  ls_zrfc_logs-uname      = p_uname.
  ls_zrfc_logs-zname1     = p_name1.
  ls_zrfc_logs-erdat      = sy-datum.
  ls_zrfc_logs-uzeit      = sy-uzeit.
*  ls_zrfc_logs-ep_type    = ep_type.
*  ls_zrfc_logs-ep_message = ep_message.
  ls_zrfc_logs-jsonstr1   = jsonstr.

  MODIFY zrfc_logs FROM ls_zrfc_logs.
  IF sy-subrc EQ 0.
    COMMIT WORK AND WAIT.
  ELSE.
    ROLLBACK WORK.

  ENDIF.

  SORT it_plm007[] BY matnr werks stlan.
  LOOP AT it_plm007.
    CLEAR: lv_begin,lv_end.
    AT NEW stlal.
      lv_begin = 'X'.
    ENDAT.
    AT END OF stlal.
      lv_end = 'X'.
    ENDAT.
    CLEAR: lv_matnr.
    lv_matnr = |{ it_plm007-matnr ALPHA = IN }|. "增加前置0
    it_plm007-stlal = |{ it_plm007-stlal ALPHA = IN }|. "增加前置0
    it_plm007-posnr = |{ it_plm007-posnr ALPHA = IN }|.
    it_plm007-matnr = lv_matnr.

    IF lv_begin = 'X'.
      SELECT COUNT(*)
        FROM mast
       WHERE werks EQ it_plm007-werks "工厂
         AND matnr EQ it_plm007-matnr "物料
         AND stlan EQ it_plm007-stlan "BOM用途
         AND stlal EQ it_plm007-stlal."可选的BOM
      IF sy-subrc EQ 0.
*已存在BOM,则修改
        lv_ztype = 'U'.
      ELSE.
*不存在BOM,则创建
        lv_ztype = 'C'.
      ENDIF.
    ENDIF.

    IF lv_ztype EQ 'U'.
*已存在BOM,则修改
      IF lv_begin EQ 'X'.
        date = it_plm007-datuv.
        CLEAR: t_stpo , t_stpo[].


        CASE sy-mandt.
          WHEN 886.
            CALL FUNCTION 'CSAP_MAT_BOM_OPEN'
              DESTINATION 'S4PCLNT886'
              EXPORTING
                material    = it_plm007-matnr
                plant       = it_plm007-werks
                bom_usage   = it_plm007-stlan
                alternative = it_plm007-stlal "'01'
                valid_from  = date
              TABLES
                t_stpo      = t_stpo[]
              EXCEPTIONS
                error       = 1
                OTHERS      = 2.
          WHEN 520.
            CALL FUNCTION 'CSAP_MAT_BOM_OPEN'
              DESTINATION 'S4DCLNT520'
              EXPORTING
                material    = it_plm007-matnr
                plant       = it_plm007-werks
                bom_usage   = it_plm007-stlan
                alternative = it_plm007-stlal "'01'
                valid_from  = date
              TABLES
                t_stpo      = t_stpo[]
              EXCEPTIONS
                error       = 1
                OTHERS      = 2.
          WHEN 668.
            CALL FUNCTION 'CSAP_MAT_BOM_OPEN'
              DESTINATION 'S4DCLNT668'
              EXPORTING
                material    = it_plm007-matnr
                plant       = it_plm007-werks
                bom_usage   = it_plm007-stlan
                alternative = it_plm007-stlal "'01'
                valid_from  = date
              TABLES
                t_stpo      = t_stpo[]
              EXCEPTIONS
                error       = 1
                OTHERS      = 2.
          WHEN OTHERS.
            CALL FUNCTION 'CSAP_MAT_BOM_OPEN'
              EXPORTING
                material    = it_plm007-matnr
                plant       = it_plm007-werks
                bom_usage   = it_plm007-stlan
                alternative = it_plm007-stlal "'01'
                valid_from  = date
              TABLES
                t_stpo      = t_stpo[]
              EXCEPTIONS
                error       = 1
                OTHERS      = 2.
        ENDCASE.
        LOOP AT t_stpo ASSIGNING FIELD-SYMBOL(<ls_stpo>).
          CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
            EXPORTING
              input  = <ls_stpo>-component
            IMPORTING
              output = <ls_stpo>-component.
        ENDLOOP.
        IF t_stpo[] IS INITIAL.
          it_plm007-ztype = 'E'.
          it_plm007-zmessage = '打开BOM失败'.
        ENDIF.
      ENDIF.
*      READ TABLE t_stpo INTO DATA(ls_stpo) WITH KEY item_no = it_plm007-posnr
*                                                  component = it_plm007-idnrk.
*      IF sy-subrc EQ 0.
*        lv_tabix = sy-tabix.
*        LOOP AT t_stpo FROM sy-tabix.
*          IF t_stpo-item_no NE it_plm007-posnr OR t_stpo-component NE it_plm007-idnrk.
*            EXIT.
*          ENDIF.
*
*          t_stpo-item_no           = |{ it_plm007-posnr ALPHA = IN }|."it_plm007-posnr."'0010'.BOM项目号
*          t_stpo-item_categ          = it_plm007-postp.
*          t_stpo-component         = it_plm007-idnrk.
*          CLEAR:lv_potx1,lv_potx2.
*          lv_len = strlen( it_plm007-potx1 ).
*          IF lv_len <= 40.
*            lv_potx1 = it_plm007-potx1.
*          ELSE.
*            lv_potx1 = it_plm007-potx1+0(40).
*            lv_potx2 = it_plm007-potx1+40(40).
*          ENDIF.
*          t_stpo-item_text1        = lv_potx1. "BOM项目文本1
*          t_stpo-item_text2        = lv_potx2. "BOM项目文本2
*          t_stpo-comp_qty          = it_plm007-menge. "组件数量
*          CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
*            EXPORTING
*              input          = it_plm007-meins
*            IMPORTING
*              output         = t_stpo-comp_unit
*            EXCEPTIONS
*              unit_not_found = 1
*              OTHERS         = 2.
*          IF t_stpo-comp_unit CA '**'.
*            t_stpo-comp_unit = it_plm007-meins.
*          ENDIF.
*
**          t_stpo-comp_unit         = it_plm007-meins."组件基本计量单位
*          t_stpo-comp_scrap        = it_plm007-ausch. "组件报废百分比
*          t_stpo-mat_provis        = it_plm007-beikz."物料供应标识符
*          t_stpo-ai_strateg        = it_plm007-alpst. "替代项目:策略
*          t_stpo-ai_prio           = it_plm007-alprf. "替代项目:评比定单
*          t_stpo-ai_group          = it_plm007-alpgr. "替代项目:组
*          t_stpo-usage_prob        = it_plm007-ewahr. "使用可能性按%
*          t_stpo-follow_grp        = it_plm007-nfgrp. "后继组
*          t_stpo-discon_grp        = it_plm007-nfeag. "中止组
*          t_stpo-rel_cost          = it_plm007-sanka."成本核算标识相关
**        t_stpo-CO_PRODUCT = it_plm007-KZKUP1.
*          t_stpo-valid_from        = sy-datum."it_plm007-datuv."it_plm007-datuv. "有效起始日期
*          t_stpo-change_no         = it_plm007-aennr. "更改编号
*          MODIFY t_stpo[] FROM t_stpo INDEX lv_tabix.
*          CLEAR: t_stpo.
*        ENDLOOP.
*      ELSE.
*        READ TABLE t_stpo INTO DATA(ls_stpo2) WITH KEY item_no = it_plm007-posnr.
*        IF sy-subrc EQ 0.
*          lv_tabix = sy-tabix.
*          LOOP AT t_stpo FROM sy-tabix.
*            IF t_stpo-item_no NE it_plm007-posnr OR t_stpo-component NE it_plm007-idnrk.
*              EXIT.
*            ENDIF.
*
*            t_stpo-item_no           = |{ it_plm007-posnr ALPHA = IN }|."it_plm007-posnr."'0010'.BOM项目号
*            t_stpo-item_categ          = it_plm007-postp.
*            t_stpo-component         = it_plm007-idnrk.
*            CLEAR:lv_potx1,lv_potx2.
*            lv_len = strlen( it_plm007-potx1 ).
*            IF lv_len <= 40.
*              lv_potx1 = it_plm007-potx1.
*            ELSE.
*              lv_potx1 = it_plm007-potx1+0(40).
*              lv_potx2 = it_plm007-potx1+40(40).
*            ENDIF.
*            t_stpo-item_text1        = lv_potx1. "BOM项目文本1
*            t_stpo-item_text2        = lv_potx2. "BOM项目文本2
*            t_stpo-comp_qty          = it_plm007-menge. "组件数量
*            CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
*              EXPORTING
*                input          = it_plm007-meins
*              IMPORTING
*                output         = t_stpo-comp_unit
*              EXCEPTIONS
*                unit_not_found = 1
*                OTHERS         = 2.
*            IF t_stpo-comp_unit CA '**'.
*              t_stpo-comp_unit = it_plm007-meins.
*            ENDIF.
*
**          t_stpo-comp_unit         = it_plm007-meins."组件基本计量单位
*            t_stpo-comp_scrap        = it_plm007-ausch. "组件报废百分比
*            t_stpo-mat_provis        = it_plm007-beikz."物料供应标识符
*            t_stpo-ai_strateg        = it_plm007-alpst. "替代项目:策略
*            t_stpo-ai_prio           = it_plm007-alprf. "替代项目:评比定单
*            t_stpo-ai_group          = it_plm007-alpgr. "替代项目:组
*            t_stpo-usage_prob        = it_plm007-ewahr. "使用可能性按%
*            t_stpo-follow_grp        = it_plm007-nfgrp. "后继组
*            t_stpo-discon_grp        = it_plm007-nfeag. "中止组
*            t_stpo-rel_cost          = it_plm007-sanka."成本核算标识相关
**        t_stpo-CO_PRODUCT = it_plm007-KZKUP1.
*            t_stpo-valid_from        = sy-datum."it_plm007-datuv."it_plm007-datuv. "有效起始日期
*            t_stpo-change_no         = it_plm007-aennr. "更改编号
*            MODIFY t_stpo[] FROM t_stpo INDEX lv_tabix.
*            CLEAR: t_stpo.
*          ENDLOOP.
*        ELSE.
*
*          t_stpo-item_no           = |{ it_plm007-posnr ALPHA = IN }|."it_plm007-posnr."'0010'.BOM项目号
*          t_stpo-item_categ          = it_plm007-postp.
*          t_stpo-component         = it_plm007-idnrk.
*          CLEAR:lv_potx1,lv_potx2.
*          lv_len = strlen( it_plm007-potx1 ).
*          IF lv_len <= 40.
*            lv_potx1 = it_plm007-potx1.
*          ELSE.
*            lv_potx1 = it_plm007-potx1+0(40).
*            lv_potx2 = it_plm007-potx1+40(40).
*          ENDIF.
*          t_stpo-item_text1        = lv_potx1. "BOM项目文本1
*          t_stpo-item_text2        = lv_potx2. "BOM项目文本2
*          t_stpo-comp_qty          = it_plm007-menge. "组件数量
*          CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
*            EXPORTING
*              input          = it_plm007-meins
*            IMPORTING
*              output         = t_stpo-comp_unit
*            EXCEPTIONS
*              unit_not_found = 1
*              OTHERS         = 2.
*          IF t_stpo-comp_unit CA '**' OR t_stpo-comp_unit EQ ''.
*            t_stpo-comp_unit = it_plm007-meins.
*          ENDIF.
**          t_stpo-comp_unit         = it_plm007-meins."组件基本计量单位
*          t_stpo-comp_scrap        = it_plm007-ausch. "组件报废百分比
*          t_stpo-mat_provis        = it_plm007-beikz."物料供应标识符
*          t_stpo-ai_strateg        = it_plm007-alpst. "替代项目:策略
*          t_stpo-ai_prio           = it_plm007-alprf. "替代项目:评比定单
*          t_stpo-ai_group          = it_plm007-alpgr. "替代项目:组
*          t_stpo-usage_prob        = it_plm007-ewahr. "使用可能性按%
*          t_stpo-follow_grp        = it_plm007-nfgrp. "后继组
*          t_stpo-discon_grp        = it_plm007-nfeag. "中止组
*          t_stpo-rel_cost          = it_plm007-sanka."成本核算标识相关
**        t_stpo-CO_PRODUCT = it_plm007-KZKUP1.
*          t_stpo-valid_from        = sy-datum."it_plm007-datuv."it_plm007-datuv. "有效起始日期
*          t_stpo-change_no         = it_plm007-aennr. "更改编号
*          APPEND t_stpo TO t_stpo[].
*          CLEAR: t_stpo.
*        ENDIF.
*      ENDIF.

          t_stpo2-item_no           = |{ it_plm007-posnr ALPHA = IN }|."it_plm007-posnr."'0010'.BOM项目号
          t_stpo2-item_categ          = it_plm007-postp.
          t_stpo2-component         = it_plm007-idnrk.
          CLEAR:lv_potx1,lv_potx2.
          lv_len = strlen( it_plm007-potx1 ).
          IF lv_len <= 40.
            lv_potx1 = it_plm007-potx1.
          ELSE.
            lv_potx1 = it_plm007-potx1+0(40).
            lv_potx2 = it_plm007-potx1+40(40).
          ENDIF.
          t_stpo2-item_text1        = lv_potx1. "BOM项目文本1
          t_stpo2-item_text2        = lv_potx2. "BOM项目文本2
          t_stpo2-comp_qty          = it_plm007-menge. "组件数量
          CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
            EXPORTING
              input          = it_plm007-meins
            IMPORTING
              output         = t_stpo2-comp_unit
            EXCEPTIONS
              unit_not_found = 1
              OTHERS         = 2.
          IF t_stpo2-comp_unit CA '**' OR t_stpo2-comp_unit EQ ''.
            t_stpo2-comp_unit = it_plm007-meins.
          ENDIF.
*          t_stpo2-comp_unit         = it_plm007-meins."组件基本计量单位
          t_stpo2-comp_scrap        = it_plm007-ausch. "组件报废百分比
          t_stpo2-mat_provis        = it_plm007-beikz."物料供应标识符
          t_stpo2-ai_strateg        = it_plm007-alpst. "替代项目:策略
          t_stpo2-ai_prio           = it_plm007-alprf. "替代项目:评比定单
          t_stpo2-ai_group          = it_plm007-alpgr. "替代项目:组
          t_stpo2-usage_prob        = it_plm007-ewahr. "使用可能性按%
          t_stpo2-follow_grp        = it_plm007-nfgrp. "后继组
          t_stpo2-discon_grp        = it_plm007-nfeag. "中止组
          t_stpo2-rel_cost          = it_plm007-sanka."成本核算标识相关
*        t_stpo2-CO_PRODUCT = it_plm007-KZKUP1.
          t_stpo2-valid_from        = sy-datum."it_plm007-datuv."it_plm007-datuv. "有效起始日期
          t_stpo2-change_no         = it_plm007-aennr. "更改编号
          APPEND t_stpo2 TO t_stpo2[].
          CLEAR: t_stpo2.
      IF lv_end EQ 'X'.


        LOOP AT t_stpo.
          CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
            EXPORTING
              input  = t_stpo-component
            IMPORTING
              output = t_stpo-component.
          t_stpo-fldelete = 'X'.
          CASE sy-mandt.
            WHEN 886.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                DESTINATION 'S4PCLNT886'
                EXPORTING
                  i_stpo = t_stpo
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
            WHEN 520.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                DESTINATION 'S4DCLNT520'
                EXPORTING
                  i_stpo = t_stpo
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
            WHEN 668.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                DESTINATION 'S4DCLNT668'
                EXPORTING
                  i_stpo = t_stpo
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
            WHEN OTHERS.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                EXPORTING
                  i_stpo = t_stpo
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
          ENDCASE.
          IF sy-subrc NE 0.
            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 = lv_zmessage.
            it_plm007-zmessage = |{ it_plm007-zmessage }{ lv_zmessage }|.
            it_plm007-ztype = 'E'.
            ROLLBACK WORK.
            EXIT.
          ELSE.
            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 = lv_zmessage.
            it_plm007-zmessage = |{ it_plm007-zmessage }{ lv_zmessage }|.
          ENDIF.
          CLEAR: lv_zmessage,t_stpo.
        ENDLOOP.

        LOOP AT t_stpo2.
          CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
            EXPORTING
              input  = t_stpo2-component
            IMPORTING
              output = t_stpo2-component.
          CASE sy-mandt.
            WHEN 886.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                DESTINATION 'S4PCLNT886'
                EXPORTING
                  i_stpo = t_stpo2
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
            WHEN 520.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                DESTINATION 'S4DCLNT520'
                EXPORTING
                  i_stpo = t_stpo2
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
            WHEN 668.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                DESTINATION 'S4DCLNT668'
                EXPORTING
                  i_stpo = t_stpo2
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
            WHEN OTHERS.
              CALL FUNCTION 'CSAP_BOM_ITEM_MAINTAIN' "维护BOM
                EXPORTING
                  i_stpo = t_stpo2
                EXCEPTIONS
                  error  = 1
                  OTHERS = 2.
          ENDCASE.




          IF sy-subrc NE 0.
            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 = lv_zmessage.
            it_plm007-zmessage = |{ it_plm007-zmessage }{ lv_zmessage }|.
            it_plm007-ztype = 'E'.
            ROLLBACK WORK.
            EXIT.
          ELSE.
            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 = lv_zmessage.
            it_plm007-zmessage = |{ it_plm007-zmessage }{ lv_zmessage }|.
          ENDIF.
          CLEAR: lv_zmessage,t_stpo2.
        ENDLOOP.

        CASE sy-mandt.
          WHEN 886.
            CALL FUNCTION 'CSAP_MAT_BOM_CLOSE' "关闭BOM
              DESTINATION 'S4PCLNT886'
              EXCEPTIONS
                error  = 1
                OTHERS = 2.
          WHEN 520.
            CALL FUNCTION 'CSAP_MAT_BOM_CLOSE' "关闭BOM
              DESTINATION 'S4DCLNT520'
              EXCEPTIONS
                error  = 1
                OTHERS = 2.
          WHEN 668.
            CALL FUNCTION 'CSAP_MAT_BOM_CLOSE' "关闭BOM
              DESTINATION 'S4DCLNT668'
              EXCEPTIONS
                error  = 1
                OTHERS = 2.
          WHEN OTHERS.
            CALL FUNCTION 'CSAP_MAT_BOM_CLOSE' "关闭BOM
              EXCEPTIONS
                error  = 1
                OTHERS = 2.
        ENDCASE.



        IF sy-subrc = 0.
          IF it_plm007-ztype NE 'E'.
            it_plm007-ztype = 'S'.
          ENDIF.
          it_plm007-zmessage = it_plm007-zmessage && '已关闭BOM'.
*            UPDATE ZBOM SET STATUS = 'S' WHERE MATNR = GS_ITAB-MATNR AND WERKS = GS_ITAB-WERKS.
        ENDIF.
      ENDIF.
    ENDIF.

    IF lv_ztype EQ 'C'.
*不存在BOM,则创建
      IF lv_begin = 'X'.


        CLEAR  it_bomgroup.
        it_bomgroup-bom_group_identification = 'BAPI_SMP_COL1'.
        it_bomgroup-object_type = 'BGR'.
        it_bomgroup-object_id = 'SIMPLE1'.
        it_bomgroup-bom_usage = it_plm007-stlan. "BOM用途
*      it_bomgroup-bom_group = it_plm007-stlan. "BOM分组
        it_bomgroup-ltxt_lang = sy-langu. "语言
        it_bomgroup-technical_type = ''. "技术类型
        it_bomgroup-created_in_plant = it_plm007-werks. "生成BOM 或 可选/变式的工厂
        it_bomgroup-bom_text = it_plm007-stktx." ''. "BOM文本
        APPEND it_bomgroup.

        CLEAR it_variants.
        it_variants-bom_group_identification = 'BAPI_SMP_COL1'.
        it_variants-object_type = 'BOM'.
        it_variants-object_id = 'SIMPLE1'.
        it_variants-alternative_bom = it_plm007-stlal."'01'. "可选的BOM
        it_variants-bom_status = it_plm007-stlst."'01'. "BOM状态
        it_variants-base_qty = it_plm007-bmeng. "BOM基本数量
        CLEAR: lv_meins.
        SELECT SINGLE meins INTO lv_meins FROM mara WHERE matnr EQ it_plm007-matnr.
        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
          EXPORTING
            input          = lv_meins
          IMPORTING
            output         = it_variants-base_unit
          EXCEPTIONS
            unit_not_found = 1
            OTHERS         = 2.
        IF it_variants-base_unit CA '**' OR it_variants-base_unit EQ ''.
          it_variants-base_unit = lv_meins."BOM基本单位
        ENDIF.

*        it_variants-base_unit  = lv_meins. "it_plm007-meins.
        it_variants-alt_text = it_plm007-stktx. "可选BOM文本
        it_variants-valid_from_date = it_plm007-datuv. "有效起始日期 AENNR
*        it_variants-change_no = it_plm007-aennr. "更改编号
        it_variants-function = 'NEW'.
        APPEND it_variants.

        CLEAR it_matrel.
        it_matrel-bom_group_identification = 'BAPI_SMP_COL1'.
        it_matrel-material = it_plm007-matnr. "物料编号,18字符
        it_matrel-plant = it_plm007-werks.
        it_matrel-bom_usage = it_plm007-stlan. "BOM用途
        it_matrel-alternative_bom = it_plm007-stlal. "可选的BOM(备选物料清单)
*      it_matrel-alternative_bom = '01'.
        it_matrel-material_long = it_plm007-matnr.
        APPEND it_matrel.

        CLEAR it_itemas.
        it_itemas-bom_group_identification = 'BAPI_SMP_COL1'.
        it_itemas-sub_object_type = 'ITM'.
        it_itemas-sub_object_id = 'SIMPLE1'.
        it_itemas-super_object_type = 'BOM'.
        it_itemas-super_object_id = 'SIMPLE1'.
        it_itemas-valid_from_date = it_plm007-datuv.
        it_itemas-function = 'NEW'.
        APPEND it_itemas.

      ENDIF.


      CLEAR it_items.
      it_items-bom_group_identification = 'BAPI_SMP_COL1'.
      it_items-object_type       = 'ITM'.
      it_items-object_id         = 'SIMPLE1'.
      it_items-item_no           = |{ it_plm007-posnr ALPHA = IN }|."'0010'.BOM项目号
      it_items-item_cat          = it_plm007-postp.
      it_items-component         = it_plm007-idnrk.
      CLEAR:lv_potx1,lv_potx2.
      lv_len = strlen( it_plm007-potx1 ).
      IF lv_len <= 40.
        lv_potx1 = it_plm007-potx1.
      ELSE.
        lv_potx1 = it_plm007-potx1+0(40).
        lv_potx2 = it_plm007-potx1+40(40).
      ENDIF.
      it_items-item_text1        = lv_potx1. "BOM项目文本1
      it_items-item_text2        = lv_potx2. "BOM项目文本2
      it_items-comp_qty          = it_plm007-menge. "组件数量
      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input          = it_plm007-meins
        IMPORTING
          output         = it_items-comp_unit
        EXCEPTIONS
          unit_not_found = 1
          OTHERS         = 2.
      IF it_items-comp_unit CA '**'.
        it_items-comp_unit = it_plm007-meins."BOM基本单位
      ENDIF.
*      it_items-comp_unit         = it_plm007-meins."组件基本计量单位
      it_items-comp_scrap        = it_plm007-ausch. "组件报废百分比
      it_items-mat_provision     = it_plm007-beikz."物料供应标识符
      it_items-alt_item_strategy = it_plm007-alpst. "替代项目:策略
      it_items-alt_item_prio     = it_plm007-alprf. "替代项目:评比定单
      it_items-alt_item_group    = it_plm007-alpgr. "替代项目:组
      it_items-usage_prob        = it_plm007-ewahr. "使用可能性按%
      it_items-follow_up_group   = it_plm007-nfgrp. "后继组
      it_items-discon_group      = it_plm007-nfeag. "中止组
*        IT_ITEMS-CO_PRODUCT = it_plm007-KZKUP1.
      it_items-valid_from_date   = it_plm007-datuv. "有效起始日期
*      it_items-change_no = it_plm007-aennr. "更改编号
      APPEND it_items.



      IF lv_end EQ 'X'.
        CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
          EXPORTING
*           TESTRUN           = ' '
            all_error         = 'X'
          TABLES
            bomgroup          = it_bomgroup
            variants          = it_variants
            items             = it_items
*           subitems          =
            materialrelations = it_matrel
            itemassignments   = it_itemas
*           subitemassignments =
*           texts             =
            return            = it_return.
        READ TABLE it_return WITH KEY type = 'E'.
        IF sy-subrc EQ 0.
          it_plm007-ztype = 'E'.
          ROLLBACK WORK.
        ELSE.
          READ TABLE it_return WITH KEY type = 'A'.
          IF sy-subrc EQ 0.
            it_plm007-ztype = 'E'.
            ROLLBACK WORK.
          ELSE.
            it_plm007-ztype = 'S'.
            COMMIT WORK AND WAIT.
          ENDIF.
        ENDIF.
        LOOP AT it_return.
          it_plm007-zmessage = |{ it_plm007-zmessage }{ it_return-message }|.
        ENDLOOP.
*        MOVE-CORRESPONDING it_plm007 TO it_re_007.
*        APPEND it_re_007.
        CLEAR: it_return[],it_itemas[],it_matrel[],it_items[],it_variants[],it_bomgroup[].
      ENDIF.

    ENDIF.
    MODIFY it_plm007.
    CLEAR it_plm007.
  ENDLOOP.



  DELETE it_plm007[] WHERE ztype EQ ''.


ENDFUNCTION.
View Code

    修改的做法:打开BOM后,先删除原本所有的行项目,再新增。再保存关闭BOM。有使用ABAP链接(SM59创建)

  创建:直接创建

上一篇:Teancenter BOM提取SQL脚本


下一篇:BOM 浏览器对象模型