SAP PP ECN CSAP_MAT_BOM_MAINTAIN

发布于:2024-12-23 ⋅ 阅读:(14) ⋅ 点赞:(0)

刚开始的时候ECN总是加不上,

参考kimi给出的案例

点击链接查看和 Kimi 智能助手的对话 https://kimi.moonshot.cn/share/cth1ipmqvl7f04qkggdg

效果 加上了

FUNCTION ZPBOM_PLM2SAP.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  EXPORTING
*"     VALUE(OUTPUT) TYPE  ZSM8_SALESORDER_CREATE_OUTPUT
*"  TABLES
*"      INPUT_PBOM STRUCTURE  ZPBOM
*"      OUTPUT_MESSAGE STRUCTURE  ZMAT_MESSAGE
*"----------------------------------------------------------------------
*  新增 input_bom
*  修改 input_bom
*  删除 ECN  日期

  TABLES: MAST,STPO,STKO,STPU.
  DATA: TEMP_MAT     TYPE MATNR,
        TEMP_WERKS   TYPE WERKS_D,
        TEMP_STLNR   LIKE MAST-STLNR, "物料单号(bom号)
        RETURN(220)  TYPE C,
        ISFIND(1)    TYPE C,
        FROMDATE(10) TYPE C.
  DATA:TEMP_MATNR TYPE MATNR.
  DATA: BEGIN OF ZLOG,
          MATNR             TYPE MATNR,
          WERKS             TYPE WERKS_D,
          INTERFACENAME(60) TYPE C,
          LOGDATE           TYPE DATUM,
          LOGTIME           TYPE UZEIT,
          MAKTX             TYPE MAKTX,
          MEINS             TYPE MEINS,
          MBRSH             TYPE MBRSH,
          LVORM             TYPE LVORM,
          MTART             TYPE MTART,
          SPART             TYPE SPART,
          MATKL             TYPE MATKL,
          BISMT             TYPE BISMT,
          LGFSB             TYPE LGFSB,
          EORIGINNO(40)     TYPE C,
          CREATER(20)       TYPE C,
          MESSAGETYPE(1)    TYPE C,
          MESSAGE(220)      TYPE C,
        END OF ZLOG.

  DATA: BEGIN OF SUBMATNR_COUNT,"用于记录在同一层级下相同子物料,相同数量,出现的次数。
          SUBMATNR TYPE MATNR,
          MENGE    TYPE KMPMG,
          COUNT    TYPE I,
        END OF SUBMATNR_COUNT.

  DATA:SAP_SUBMATNR_COUNT LIKE SUBMATNR_COUNT,
       PLM_SUBMATNR_COUNT LIKE SUBMATNR_COUNT.

  DATA: NO1 TYPE I,
        NO2 TYPE I.

  DATA:I_STKO LIKE STKO_API01.

  DATA:BEGIN OF GS_INPUTPBOM,
         MATNR   TYPE MATNR,
         WERKS   TYPE WERKS_D,
         IDNRK   TYPE IDNRK,
         KMPMG   TYPE KMPMG,
         XUHAO   TYPE I,
         MEINS   TYPE MEINS,
         SORTP   TYPE SORTP,
         LOGDATE TYPE DATUM,
         LOGTIME TYPE UZEIT,
       END OF GS_INPUTPBOM.

  DATA:GT_STPO_INPUT LIKE TABLE OF STPO_API03 WITH HEADER LINE.

  DATA: SAP_STPO LIKE TABLE OF STPO WITH HEADER LINE.
  DATA: PDATAV LIKE  CSAP_MBOM-DATUV.

*1. 根据父物料字段,对输入BOM表排序:
  SORT INPUT_PBOM BY STLAN WERKS MATNR IDNRK KMPMG.

*2.将plm的bom格式转化为SAP能处理的单层bom,并处理(行项目新建或修改和删除的处理)

  CLEAR TEMP_MAT.
  CLEAR TEMP_WERKS.
  I_STKO-BASE_QUAN = 1.
  I_STKO-BOM_STATUS = '01'.

  LOOP AT INPUT_PBOM.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        INPUT  = INPUT_PBOM-MATNR
      IMPORTING
        OUTPUT = INPUT_PBOM-MATNR.

    TRANSLATE INPUT_PBOM-MATNR TO UPPER CASE.

*    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        INPUT  = INPUT_PBOM-IDNRK
      IMPORTING
        OUTPUT = INPUT_PBOM-IDNRK.

    TRANSLATE INPUT_PBOM-IDNRK TO UPPER CASE.

    CLEAR GS_INPUTPBOM.

*2.1   当从一个小bom切换到另一个小bom时的处理;
*2.1.1 可以是第一个bom的第一条记录,
*2.1.2 或者是另开始一个小bom数据
    IF ( TEMP_MAT <> INPUT_PBOM-MATNR OR TEMP_WERKS <> INPUT_PBOM-WERKS ).

      "2.1.1当是第一个bom的第一条数据时
      IF ( TEMP_MAT IS INITIAL ) AND ( TEMP_WERKS IS INITIAL ).
        TEMP_MAT = INPUT_PBOM-MATNR.
        TEMP_WERKS = INPUT_PBOM-WERKS.

        CLEAR PLM_SUBMATNR_COUNT.
        PLM_SUBMATNR_COUNT-SUBMATNR = INPUT_PBOM-IDNRK.
        PLM_SUBMATNR_COUNT-MENGE = INPUT_PBOM-KMPMG.
        PLM_SUBMATNR_COUNT-COUNT = PLM_SUBMATNR_COUNT-COUNT + 1.

        "保存传入的数据到zinputpbom透视表中,作为记录。
        GS_INPUTPBOM-MATNR = INPUT_PBOM-MATNR.
        GS_INPUTPBOM-WERKS = INPUT_PBOM-WERKS.
        GS_INPUTPBOM-IDNRK = INPUT_PBOM-IDNRK.
        GS_INPUTPBOM-KMPMG = INPUT_PBOM-KMPMG.
        GS_INPUTPBOM-XUHAO = PLM_SUBMATNR_COUNT-COUNT.
        GS_INPUTPBOM-MEINS = INPUT_PBOM-MEINS.
        GS_INPUTPBOM-SORTP = INPUT_PBOM-SORTP.
        GS_INPUTPBOM-LOGDATE = SY-DATUM.
        GS_INPUTPBOM-LOGTIME = SY-UZEIT.

        CLEAR TEMP_MATNR.
        SELECT SINGLE MATNR INTO TEMP_MATNR FROM ZINPUTPBOM
        WHERE MATNR = INPUT_PBOM-MATNR
        AND WERKS = INPUT_PBOM-WERKS
        AND IDNRK = INPUT_PBOM-IDNRK
        AND KMPMG = INPUT_PBOM-KMPMG
        AND XUHAO = PLM_SUBMATNR_COUNT-COUNT.

        IF TEMP_MATNR IS INITIAL.
          INSERT INTO ZINPUTPBOM VALUES GS_INPUTPBOM.
        ELSE.
          UPDATE ZINPUTPBOM SET SORTP = GS_INPUTPBOM-SORTP LOGDATE = SY-DATUM  LOGTIME = SY-UZEIT
          WHERE MATNR = INPUT_PBOM-MATNR
          AND WERKS = INPUT_PBOM-WERKS
          AND IDNRK = INPUT_PBOM-IDNRK
          AND KMPMG = INPUT_PBOM-KMPMG
          AND XUHAO = PLM_SUBMATNR_COUNT-COUNT.
        ENDIF.
        COMMIT WORK AND WAIT.

        "准备行项目数据
        CLEAR TEMP_STLNR.
        SELECT SINGLE STLNR INTO TEMP_STLNR FROM MAST WHERE MATNR = TEMP_MAT AND WERKS = TEMP_WERKS AND STLAN = '1'.
        IF TEMP_STLNR IS INITIAL.  "sap原数据没有bom时的处理
          CLEAR GT_STPO_INPUT.
          IF INPUT_PBOM-STVKN IS NOT INITIAL.
            GT_STPO_INPUT-ITEM_NODE = INPUT_PBOM-STVKN.
          ENDIF.
          GT_STPO_INPUT-CHG_NO_TO = INPUT_PBOM-AENRA.
          GT_STPO_INPUT-VALID_TO = INPUT_PBOM-DATUB+0(4) && '-' && INPUT_PBOM-DATUB+4(2) && '-' && INPUT_PBOM-DATUB+6(2).
          GT_STPO_INPUT-REL_PROD = 'X'."标识 与生产相关项目
          GT_STPO_INPUT-AI_GROUP = 'A'."替代组
          GT_STPO_INPUT-AI_PRIO = '1'."优先级
          GT_STPO_INPUT-AI_STRATEG = '01'."策略
          GT_STPO_INPUT-USAGE_PROB = '100'."使用比例
          GT_STPO_INPUT-IDENTIFIER = '1'."标识
          GT_STPO_INPUT-ITEM_NO = INPUT_PBOM-POSNR.     "项目号 默认9000
          GT_STPO_INPUT-ITEM_CATEG = INPUT_PBOM-POSTP.  "项目类别ICT 默认L
          GT_STPO_INPUT-COMPONENT = INPUT_PBOM-IDNRK.   "组件:即子物料
          GT_STPO_INPUT-COMP_QTY = INPUT_PBOM-KMPMG.    "数量
          GT_STPO_INPUT-COMP_UNIT = INPUT_PBOM-MEINS.   "单位
          GT_STPO_INPUT-REL_COST = 'X'.                 "成本核算标准相关
          GT_STPO_INPUT-SORTSTRING = INPUT_PBOM-SORTP.  "排序字符串
          GT_STPO_INPUT-CHANGE_NO = INPUT_PBOM-AENNR.
          PDATAV = INPUT_PBOM-DATUV+0(4) && '-' && INPUT_PBOM-DATUV+4(2) && '-' && INPUT_PBOM-DATUV+6(2) .
          GT_STPO_INPUT-VALID_FROM = PDATAV.

*        gt_stpo_input-comp_scrap = ."部件废品用百分比表示(BTCI)
*        gt_stpo_input-issue_loc = ."生产订单的发货地点
          APPEND GT_STPO_INPUT.

        ELSE.                     "sap原数据有bom时的处理,指定bom中要更改的具体条目
          CLEAR GT_STPO_INPUT.
          IF INPUT_PBOM-STVKN IS NOT INITIAL.
            GT_STPO_INPUT-ITEM_NODE = INPUT_PBOM-STVKN.
          ENDIF.
          GT_STPO_INPUT-CHG_NO_TO = INPUT_PBOM-AENRA.
          GT_STPO_INPUT-VALID_TO = INPUT_PBOM-DATUB+0(4) && '-' && INPUT_PBOM-DATUB+4(2) && '-' && INPUT_PBOM-DATUB+6(2).
          GT_STPO_INPUT-REL_PROD = 'X'."标识 与生产相关项目
          GT_STPO_INPUT-AI_GROUP = 'A'."替代组
          GT_STPO_INPUT-AI_PRIO = '1'."优先级
          GT_STPO_INPUT-AI_STRATEG = '01'."策略
          GT_STPO_INPUT-USAGE_PROB = '100'."使用比例
          GT_STPO_INPUT-IDENTIFIER = '1'."标识
          GT_STPO_INPUT-ITEM_NO = INPUT_PBOM-POSNR.     "项目号 默认9000
          GT_STPO_INPUT-ITEM_CATEG = INPUT_PBOM-POSTP.  "项目类别ICT 默认L
          GT_STPO_INPUT-COMPONENT = INPUT_PBOM-IDNRK.   "组件:即子物料
          GT_STPO_INPUT-COMP_QTY = INPUT_PBOM-KMPMG.    "数量
          GT_STPO_INPUT-COMP_UNIT = INPUT_PBOM-MEINS.   "单位
          GT_STPO_INPUT-REL_COST = 'X'.                 "成本核算标准相关
          GT_STPO_INPUT-SORTSTRING = INPUT_PBOM-SORTP.  "排序字符串
          GT_STPO_INPUT-CHANGE_NO = INPUT_PBOM-AENNR.
          PDATAV = INPUT_PBOM-DATUV+0(4) && '-' && INPUT_PBOM-DATUV+4(2) && '-' && INPUT_PBOM-DATUV+6(2) .
          GT_STPO_INPUT-VALID_FROM = PDATAV.
*         gt_stpo_input-comp_scrap = ."部件废品用百分比表示(BTCI)
*         gt_stpo_input-issue_loc = ."生产订单的发货地点

          CLEAR SAP_STPO.
          CLEAR SAP_STPO[].
          SELECT * INTO CORRESPONDING FIELDS OF TABLE  SAP_STPO FROM STPO
          WHERE STLNR = TEMP_STLNR AND STLTY = 'M' AND IDNRK = INPUT_PBOM-IDNRK AND MENGE = INPUT_PBOM-KMPMG ORDER BY STLKN STPOZ.
          IF LINES( SAP_STPO[] ) >= PLM_SUBMATNR_COUNT-COUNT.
            CLEAR NO1.
            LOOP AT SAP_STPO.
              NO1 = NO1 + 1.
              IF NO1 = PLM_SUBMATNR_COUNT-COUNT.
                GT_STPO_INPUT-BOM_NO = SAP_STPO-STLNR.
                GT_STPO_INPUT-ITEM_NODE = SAP_STPO-STLKN.
                GT_STPO_INPUT-ITEM_COUNT = SAP_STPO-STPOZ.
                IF ( SAP_STPO-SORTF IS NOT INITIAL ) AND ( INPUT_PBOM-SORTP IS INITIAL ).
                  GT_STPO_INPUT-SORTSTRING = SAP_STPO-SORTF.  "排序字符串
                ENDIF.
                EXIT.
              ENDIF.
            ENDLOOP.
          ENDIF.
          APPEND GT_STPO_INPUT.
        ENDIF.

      ELSE.
        "2.1.2 当完成一个小bom,处理该层小bom的数据同步。进入下一个小bom数据的准备
        CLEAR SAP_STPO.
        CLEAR SAP_STPO[].
        CLEAR SAP_SUBMATNR_COUNT.

*        SELECT * INTO CORRESPONDING FIELDS OF TABLE sap_stpo FROM stpo WHERE stlnr = temp_stlnr AND stlty = 'M' ORDER BY idnrk MENGE STLKN STPOZ.
*
*        LOOP AT sap_stpo."比较输入bom数据和SAPbom数据,判断需要删除的行项目(即输入无,而SAP有的行项目):这里是用物料号做的比较,是否ok?
*          IF sap_stpo-IDNRK = sap_submatnr_count-submatnr AND sap_stpo-MENGE = sap_submatnr_count-MENGE.
*            sap_submatnr_count-COUNT = sap_submatnr_count-COUNT + 1.
*          ELSE.
*            CLEAR sap_submatnr_count.
*            sap_submatnr_count-submatnr = sap_stpo-idnrk.
*            sap_submatnr_count-MENGE = sap_stpo-MENGE.
*            sap_submatnr_count-COUNT = sap_submatnr_count-COUNT + 1.
*          ENDIF.
* 删除用ECN处理
*          isfind = '0'.
*          CLEAR NO2.
*          LOOP AT gt_stpo_input.
*            IF sap_stpo-IDNRK = gt_stpo_input-COMPONENT AND sap_stpo-MENGE = gt_stpo_input-comp_qty.
*              NO2 = NO2 + 1.
*              IF NO2 = sap_submatnr_count-COUNT.
*                isfind = '1'.
*                EXIT.
*              ENDIF.
*            ENDIF.
*          ENDLOOP.
*          IF isfind = '0'."对要删除的行项目做标记,待处理
*            gt_stpo_input-bom_no = sap_stpo-STLNR.
*            gt_stpo_input-ITEM_NODE = sap_stpo-STLKN.
*            gt_stpo_input-ITEM_COUNT = sap_stpo-STPOZ.
*            gt_stpo_input-fldelete = 'X'.
*            APPEND gt_stpo_input.
*          ENDIF.
*        ENDLOOP.

        "下面执行小bom的数据同步

        CALL FUNCTION 'CSAP_MAT_BOM_MAINTAIN'
          EXPORTING
            MATERIAL      = TEMP_MAT
            PLANT         = TEMP_WERKS
            BOM_USAGE     = '1'   "物料清单用途 默认1
            CHANGE_NO     = INPUT_PBOM-AENNR
            VALID_FROM    = PDATAV
            I_STKO        = I_STKO
            FL_BOM_CREATE = 'X'
            FL_NEW_ITEM   = 'X'
            FL_COMPLETE   = 'x'
          TABLES
            T_STPO        = GT_STPO_INPUT
          EXCEPTIONS
            ERROR         = 1
            OTHERS        = 2.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            WAIT = 'X'.

        "BOM同步成功与否的记录

        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 = RETURN.

        IF SY-SUBRC <> 0.
          CLEAR OUTPUT_MESSAGE.
          RETURN = RETURN+12(10).
          OUTPUT_MESSAGE-MESSAGETYPE = 'E'.
        ELSE.
          CLEAR OUTPUT_MESSAGE.
          OUTPUT_MESSAGE-MESSAGETYPE = 'S'.
        ENDIF.
        OUTPUT_MESSAGE-MESSAGE = RETURN.

        OUTPUT_MESSAGE-MATNR = TEMP_MAT.

        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
          EXPORTING
            INPUT  = OUTPUT_MESSAGE-MATNR
          IMPORTING
            OUTPUT = OUTPUT_MESSAGE-MATNR.

        OUTPUT_MESSAGE-WERKS = TEMP_WERKS.

        APPEND OUTPUT_MESSAGE.

        ZLOG-MATNR = OUTPUT_MESSAGE-MATNR.
        ZLOG-WERKS = OUTPUT_MESSAGE-WERKS.
        ZLOG-MESSAGETYPE = OUTPUT_MESSAGE-MESSAGETYPE.
        ZLOG-MESSAGE = OUTPUT_MESSAGE-MESSAGE.
        ZLOG-LOGDATE = SY-DATUM.
        ZLOG-LOGTIME = SY-UZEIT.
        ZLOG-INTERFACENAME = 'ZPBOM_PLM2SAP'.

        INSERT INTO ZLOGOFMATNRORBOM  VALUES ZLOG.
        COMMIT WORK AND WAIT.

        CLEAR ZLOG.

        CLEAR GT_STPO_INPUT[].


        "进入下一个小bom数据的准备
        TEMP_MAT = INPUT_PBOM-MATNR.
        TEMP_WERKS = INPUT_PBOM-WERKS.

        CLEAR PLM_SUBMATNR_COUNT.
        PLM_SUBMATNR_COUNT-SUBMATNR = INPUT_PBOM-IDNRK.
        PLM_SUBMATNR_COUNT-MENGE = INPUT_PBOM-KMPMG.
        PLM_SUBMATNR_COUNT-COUNT = PLM_SUBMATNR_COUNT-COUNT + 1.

        "保存传入的数据到zinputpbom透视表中
        GS_INPUTPBOM-MATNR = INPUT_PBOM-MATNR.
        GS_INPUTPBOM-WERKS = INPUT_PBOM-WERKS.
        GS_INPUTPBOM-IDNRK = INPUT_PBOM-IDNRK.
        GS_INPUTPBOM-KMPMG = INPUT_PBOM-KMPMG.
        GS_INPUTPBOM-XUHAO = PLM_SUBMATNR_COUNT-COUNT.
        GS_INPUTPBOM-MEINS = INPUT_PBOM-MEINS.
        GS_INPUTPBOM-SORTP = INPUT_PBOM-SORTP.
        GS_INPUTPBOM-LOGDATE = SY-DATUM.
        GS_INPUTPBOM-LOGTIME = SY-UZEIT.

        CLEAR TEMP_MATNR.
        SELECT SINGLE MATNR INTO TEMP_MATNR FROM ZINPUTPBOM
        WHERE MATNR = INPUT_PBOM-MATNR
        AND WERKS = INPUT_PBOM-WERKS
        AND IDNRK = INPUT_PBOM-IDNRK
        AND KMPMG = INPUT_PBOM-KMPMG
        AND XUHAO = PLM_SUBMATNR_COUNT-COUNT.

        IF TEMP_MATNR IS INITIAL.
          INSERT INTO ZINPUTPBOM VALUES GS_INPUTPBOM.
        ELSE.
          UPDATE ZINPUTPBOM SET SORTP = GS_INPUTPBOM-SORTP LOGDATE = SY-DATUM  LOGTIME = SY-UZEIT
          WHERE MATNR = INPUT_PBOM-MATNR
          AND WERKS = INPUT_PBOM-WERKS
          AND IDNRK = INPUT_PBOM-IDNRK
          AND KMPMG = INPUT_PBOM-KMPMG
          AND XUHAO = PLM_SUBMATNR_COUNT-COUNT.
        ENDIF.
        COMMIT WORK AND WAIT.

        "准备行项目数据
        CLEAR TEMP_STLNR.
        SELECT SINGLE STLNR INTO TEMP_STLNR FROM MAST WHERE MATNR = TEMP_MAT AND WERKS = TEMP_WERKS AND STLAN = '1'.
        IF TEMP_STLNR IS INITIAL.
          CLEAR GT_STPO_INPUT.
          IF INPUT_PBOM-STVKN IS NOT INITIAL.
            GT_STPO_INPUT-ITEM_NODE = INPUT_PBOM-STVKN.
          ENDIF.
          GT_STPO_INPUT-CHG_NO_TO = INPUT_PBOM-AENRA.
          GT_STPO_INPUT-VALID_TO = INPUT_PBOM-DATUB+0(4) && '-' && INPUT_PBOM-DATUB+4(2) && '-' && INPUT_PBOM-DATUB+6(2).
          GT_STPO_INPUT-ITEM_NO = INPUT_PBOM-POSNR.     "项目号 默认9000
          GT_STPO_INPUT-ITEM_CATEG = INPUT_PBOM-POSTP.  "项目类别ICT 默认L
          GT_STPO_INPUT-COMPONENT = INPUT_PBOM-IDNRK.   "组件:即子物料
          GT_STPO_INPUT-COMP_QTY = INPUT_PBOM-KMPMG.    "数量
          GT_STPO_INPUT-COMP_UNIT = INPUT_PBOM-MEINS.   "单位
          GT_STPO_INPUT-REL_COST = 'X'.                 "成本核算标准相关
          GT_STPO_INPUT-SORTSTRING = INPUT_PBOM-SORTP.  "排序字符串
          GT_STPO_INPUT-CHANGE_NO = INPUT_PBOM-AENNR.
          PDATAV = INPUT_PBOM-DATUV+0(4) && '-' && INPUT_PBOM-DATUV+4(2) && '-' && INPUT_PBOM-DATUV+6(2) .
          GT_STPO_INPUT-VALID_FROM = PDATAV.
*        gt_stpo_input-comp_scrap = ."部件废品用百分比表示(BTCI)
*        gt_stpo_input-issue_loc = ."生产订单的发货地点
          APPEND GT_STPO_INPUT.

        ELSE.
          CLEAR GT_STPO_INPUT.
          IF INPUT_PBOM-STVKN IS NOT INITIAL.
            GT_STPO_INPUT-ITEM_NODE = INPUT_PBOM-STVKN.
          ENDIF.
          GT_STPO_INPUT-CHG_NO_TO = INPUT_PBOM-AENRA.
          GT_STPO_INPUT-VALID_TO = INPUT_PBOM-DATUB+0(4) && '-' && INPUT_PBOM-DATUB+4(2) && '-' && INPUT_PBOM-DATUB+6(2).
          GT_STPO_INPUT-ITEM_NO = INPUT_PBOM-POSNR.     "项目号 默认9000
          GT_STPO_INPUT-ITEM_CATEG = INPUT_PBOM-POSTP.  "项目类别ICT 默认L
          GT_STPO_INPUT-COMPONENT = INPUT_PBOM-IDNRK.   "组件:即子物料
          GT_STPO_INPUT-COMP_QTY = INPUT_PBOM-KMPMG.    "数量
          GT_STPO_INPUT-COMP_UNIT = INPUT_PBOM-MEINS.   "单位
          GT_STPO_INPUT-REL_COST = 'X'.                 "成本核算标准相关
          GT_STPO_INPUT-SORTSTRING = INPUT_PBOM-SORTP.  "排序字符串
          GT_STPO_INPUT-CHANGE_NO = INPUT_PBOM-AENNR.
          PDATAV = INPUT_PBOM-DATUV+0(4) && '-' && INPUT_PBOM-DATUV+4(2) && '-' && INPUT_PBOM-DATUV+6(2) .
          GT_STPO_INPUT-VALID_FROM = PDATAV.
*        gt_stpo_input-comp_scrap = ."部件废品用百分比表示(BTCI)
*        gt_stpo_input-issue_loc = ."生产订单的发货地点

          CLEAR SAP_STPO.
          CLEAR SAP_STPO[].
          SELECT * INTO CORRESPONDING FIELDS OF TABLE  SAP_STPO FROM STPO
          WHERE STLNR = TEMP_STLNR AND STLTY = 'M' AND IDNRK = INPUT_PBOM-IDNRK AND MENGE = INPUT_PBOM-KMPMG ORDER BY STLKN STPOZ.
          IF LINES( SAP_STPO[] ) >= PLM_SUBMATNR_COUNT-COUNT.
            CLEAR NO1.
            LOOP AT SAP_STPO.
              NO1 = NO1 + 1.
              IF NO1 = PLM_SUBMATNR_COUNT-COUNT.
                GT_STPO_INPUT-BOM_NO = SAP_STPO-STLNR.
                GT_STPO_INPUT-ITEM_NODE = SAP_STPO-STLKN.
                GT_STPO_INPUT-ITEM_COUNT = SAP_STPO-STPOZ.
                IF ( SAP_STPO-SORTF IS NOT INITIAL ) AND ( INPUT_PBOM-SORTP IS INITIAL ).
                  GT_STPO_INPUT-SORTSTRING = SAP_STPO-SORTF.  "排序字符串
                ENDIF.
                EXIT.
              ENDIF.
            ENDLOOP.
          ENDIF.
          APPEND GT_STPO_INPUT.
        ENDIF.
      ENDIF.

*2.2  某一小bom内行项目数据的处理:添加该行行项目。
    ELSE.
      IF INPUT_PBOM-IDNRK = PLM_SUBMATNR_COUNT-SUBMATNR AND INPUT_PBOM-KMPMG = PLM_SUBMATNR_COUNT-MENGE.
        PLM_SUBMATNR_COUNT-COUNT = PLM_SUBMATNR_COUNT-COUNT + 1.
      ELSE.
        CLEAR PLM_SUBMATNR_COUNT.
        PLM_SUBMATNR_COUNT-SUBMATNR = INPUT_PBOM-IDNRK.
        PLM_SUBMATNR_COUNT-MENGE = INPUT_PBOM-KMPMG.
        PLM_SUBMATNR_COUNT-COUNT = PLM_SUBMATNR_COUNT-COUNT + 1.
      ENDIF.

      "保存传入的数据到zinputpbom透视表中
      GS_INPUTPBOM-MATNR = INPUT_PBOM-MATNR.
      GS_INPUTPBOM-WERKS = INPUT_PBOM-WERKS.
      GS_INPUTPBOM-IDNRK = INPUT_PBOM-IDNRK.
      GS_INPUTPBOM-KMPMG = INPUT_PBOM-KMPMG.
      GS_INPUTPBOM-XUHAO = PLM_SUBMATNR_COUNT-COUNT.
      GS_INPUTPBOM-MEINS = INPUT_PBOM-MEINS.
      GS_INPUTPBOM-SORTP = INPUT_PBOM-SORTP.
      GS_INPUTPBOM-LOGDATE = SY-DATUM.
      GS_INPUTPBOM-LOGTIME = SY-UZEIT.

      CLEAR TEMP_MATNR.
      SELECT SINGLE MATNR INTO TEMP_MATNR FROM ZINPUTPBOM
      WHERE MATNR = INPUT_PBOM-MATNR
      AND WERKS = INPUT_PBOM-WERKS
      AND IDNRK = INPUT_PBOM-IDNRK
      AND KMPMG = INPUT_PBOM-KMPMG
      AND XUHAO = PLM_SUBMATNR_COUNT-COUNT.

      IF TEMP_MATNR IS INITIAL.
        INSERT INTO ZINPUTPBOM VALUES GS_INPUTPBOM.
      ELSE.
        UPDATE ZINPUTPBOM SET SORTP = GS_INPUTPBOM-SORTP LOGDATE = SY-DATUM  LOGTIME = SY-UZEIT
        WHERE MATNR = INPUT_PBOM-MATNR
        AND WERKS = INPUT_PBOM-WERKS
        AND IDNRK = INPUT_PBOM-IDNRK
        AND KMPMG = INPUT_PBOM-KMPMG
        AND XUHAO = PLM_SUBMATNR_COUNT-COUNT.
      ENDIF.
      COMMIT WORK AND WAIT.

      "准备行项目数据
      CLEAR TEMP_STLNR.
      SELECT SINGLE STLNR INTO TEMP_STLNR FROM MAST WHERE MATNR = TEMP_MAT AND WERKS = TEMP_WERKS AND STLAN = '1'.
      IF TEMP_STLNR IS INITIAL.
        CLEAR GT_STPO_INPUT.
        GT_STPO_INPUT-REL_PROD = 'X'."标识 与生产相关项目
        GT_STPO_INPUT-AI_GROUP = 'A'."替代组
        GT_STPO_INPUT-AI_PRIO = '1'."优先级
        GT_STPO_INPUT-AI_STRATEG = '01'."策略
        GT_STPO_INPUT-USAGE_PROB = '100'."使用比例
        GT_STPO_INPUT-IDENTIFIER = '1'."标识
        GT_STPO_INPUT-ITEM_NO = INPUT_PBOM-POSNR.     "项目号 默认9000
        GT_STPO_INPUT-ITEM_CATEG = INPUT_PBOM-POSTP.  "项目类别ICT 默认L
        GT_STPO_INPUT-COMPONENT = INPUT_PBOM-IDNRK.   "组件:即子物料
        GT_STPO_INPUT-COMP_QTY = INPUT_PBOM-KMPMG.    "数量
        GT_STPO_INPUT-COMP_UNIT = INPUT_PBOM-MEINS.   "单位
        GT_STPO_INPUT-REL_COST = 'X'.                 "成本核算标准相关
        GT_STPO_INPUT-SORTSTRING = INPUT_PBOM-SORTP.  "排序字符串
        PDATAV = INPUT_PBOM-DATUV+0(4) && '-' && INPUT_PBOM-DATUV+4(2) && '-' && INPUT_PBOM-DATUV+6(2) .
        GT_STPO_INPUT-VALID_FROM = PDATAV.
*        gt_stpo_input-comp_scrap = ."部件废品用百分比表示(BTCI)
*        gt_stpo_input-issue_loc = ."生产订单的发货地点
        APPEND GT_STPO_INPUT.

      ELSE.
        CLEAR GT_STPO_INPUT.
        IF INPUT_PBOM-STVKN IS NOT INITIAL.
          GT_STPO_INPUT-ITEM_NODE = INPUT_PBOM-STVKN.
        ENDIF.
        GT_STPO_INPUT-CHG_NO_TO = INPUT_PBOM-AENRA.
        GT_STPO_INPUT-VALID_TO = INPUT_PBOM-DATUB+0(4) && '-' && INPUT_PBOM-DATUB+4(2) && '-' && INPUT_PBOM-DATUB+6(2).
        GT_STPO_INPUT-REL_PROD = 'X'."标识 与生产相关项目
        GT_STPO_INPUT-AI_GROUP = 'A'."替代组
        GT_STPO_INPUT-AI_PRIO = '1'."优先级
        GT_STPO_INPUT-AI_STRATEG = '01'."策略
        GT_STPO_INPUT-USAGE_PROB = '100'."使用比例
        GT_STPO_INPUT-IDENTIFIER = '1'."标识
        GT_STPO_INPUT-ITEM_NO = INPUT_PBOM-POSNR.     "项目号 默认9000
        GT_STPO_INPUT-ITEM_CATEG = INPUT_PBOM-POSTP.  "项目类别ICT 默认L
        GT_STPO_INPUT-COMPONENT = INPUT_PBOM-IDNRK.   "组件:即子物料
        GT_STPO_INPUT-COMP_QTY = INPUT_PBOM-KMPMG.    "数量
        GT_STPO_INPUT-COMP_UNIT = INPUT_PBOM-MEINS.   "单位
        GT_STPO_INPUT-REL_COST = 'X'.                 "成本核算标准相关
        GT_STPO_INPUT-SORTSTRING = INPUT_PBOM-SORTP.  "排序字符串
        PDATAV = INPUT_PBOM-DATUV+0(4) && '-' && INPUT_PBOM-DATUV+4(2) && '-' && INPUT_PBOM-DATUV+6(2) .
        GT_STPO_INPUT-VALID_FROM = PDATAV.
*        gt_stpo_input-comp_scrap = ."部件废品用百分比表示(BTCI)
*        gt_stpo_input-issue_loc = ."生产订单的发货地点

        CLEAR SAP_STPO.
        CLEAR SAP_STPO[].
        SELECT * INTO CORRESPONDING FIELDS OF TABLE  SAP_STPO FROM STPO
        WHERE STLNR = TEMP_STLNR AND STLTY = 'M' AND IDNRK = INPUT_PBOM-IDNRK AND MENGE = INPUT_PBOM-KMPMG ORDER BY STLKN STPOZ.
        IF LINES( SAP_STPO[] ) >= PLM_SUBMATNR_COUNT-COUNT.
          CLEAR NO1.
          LOOP AT SAP_STPO.
            NO1 = NO1 + 1.
            IF NO1 = PLM_SUBMATNR_COUNT-COUNT.
              GT_STPO_INPUT-BOM_NO = SAP_STPO-STLNR.
              GT_STPO_INPUT-ITEM_NODE = SAP_STPO-STLKN.
              GT_STPO_INPUT-ITEM_COUNT = SAP_STPO-STPOZ.
              IF ( SAP_STPO-SORTF IS NOT INITIAL ) AND ( INPUT_PBOM-SORTP IS INITIAL ).
                GT_STPO_INPUT-SORTSTRING = SAP_STPO-SORTF.  "排序字符串
              ENDIF.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.
        APPEND GT_STPO_INPUT.
      ENDIF.
    ENDIF.
  ENDLOOP.

*3.  所有bom项跑完了,对最后一个小bom的处理
  CLEAR SAP_STPO.
  CLEAR SAP_STPO[].
  CLEAR SAP_SUBMATNR_COUNT.

*  SELECT * INTO CORRESPONDING FIELDS OF TABLE sap_stpo FROM stpo WHERE stlnr = temp_stlnr AND stlty = 'M' ORDER BY idnrk MENGE STLKN STPOZ.
*
*  LOOP AT sap_stpo."比较输入bom数据和SAPbom数据,判断需要删除的行项目(即输入无,而SAP有的行项目):这里是用物料号做的比较,是否ok?
*    IF sap_stpo-IDNRK = sap_submatnr_count-submatnr AND sap_stpo-MENGE = sap_submatnr_count-MENGE.
*      sap_submatnr_count-COUNT = sap_submatnr_count-COUNT + 1.
*    ELSE.
*      CLEAR sap_submatnr_count.
*      sap_submatnr_count-submatnr = sap_stpo-idnrk.
*      sap_submatnr_count-MENGE = sap_stpo-MENGE.
*      sap_submatnr_count-COUNT = sap_submatnr_count-COUNT + 1.
*    ENDIF.
*
*    isfind = '0'.
*    CLEAR NO2.
*    LOOP AT gt_stpo_input.
*      IF sap_stpo-IDNRK = gt_stpo_input-COMPONENT AND sap_stpo-MENGE = gt_stpo_input-comp_qty.
*        NO2 = NO2 + 1.
*        IF NO2 = sap_submatnr_count-COUNT.
*          isfind = '1'.
*          EXIT.
*        ENDIF.
*      ENDIF.
*    ENDLOOP.
*    IF isfind = '0'.
*      gt_stpo_input-bom_no = sap_stpo-STLNR.
*      gt_stpo_input-ITEM_NODE = sap_stpo-STLKN.
*      gt_stpo_input-ITEM_COUNT = sap_stpo-STPOZ.
*      gt_stpo_input-fldelete = 'X'.
*      APPEND gt_stpo_input.
*    ENDIF.
*  ENDLOOP.

  CALL FUNCTION 'CSAP_MAT_BOM_MAINTAIN'
    EXPORTING
      MATERIAL      = TEMP_MAT
      PLANT         = TEMP_WERKS
      BOM_USAGE     = '1'   "物料清单用途 默认1
      CHANGE_NO     = INPUT_PBOM-AENNR
      VALID_FROM    = PDATAV
      I_STKO        = I_STKO
      FL_BOM_CREATE = 'X'
      FL_NEW_ITEM   = 'X'
      FL_COMPLETE   = 'x'
    TABLES
      T_STPO        = GT_STPO_INPUT
    EXCEPTIONS
      ERROR         = 1
      OTHERS        = 2.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      WAIT = 'X'.

  "BOM同步成功与否的记录

  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 = RETURN.

  IF SY-MSGTY = 'S'.
    CLEAR OUTPUT_MESSAGE.
    OUTPUT_MESSAGE-MESSAGETYPE = 'S'.
  ELSE.
    CLEAR OUTPUT_MESSAGE.
*    RETURN = RETURN+12(10).
    OUTPUT_MESSAGE-MESSAGETYPE = 'E'.
  ENDIF.



  OUTPUT_MESSAGE-MESSAGE = RETURN.
  OUTPUT-RETURN-MSGTY = OUTPUT_MESSAGE-MESSAGETYPE.
  OUTPUT-RETURN-MSGTX = OUTPUT_MESSAGE-MESSAGE.

  OUTPUT_MESSAGE-MATNR = TEMP_MAT.

  CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
    EXPORTING
      INPUT  = OUTPUT_MESSAGE-MATNR
    IMPORTING
      OUTPUT = OUTPUT_MESSAGE-MATNR.


  OUTPUT_MESSAGE-WERKS = TEMP_WERKS.
  APPEND OUTPUT_MESSAGE.

  ZLOG-MATNR = OUTPUT_MESSAGE-MATNR.
  ZLOG-WERKS = OUTPUT_MESSAGE-WERKS.
  ZLOG-MESSAGETYPE = OUTPUT_MESSAGE-MESSAGETYPE.
  ZLOG-MESSAGE = OUTPUT_MESSAGE-MESSAGE.
  ZLOG-LOGDATE = SY-DATUM.
  ZLOG-LOGTIME = SY-UZEIT.
  ZLOG-INTERFACENAME = 'ZPBOM_PLM2SAP'.

  INSERT INTO ZLOGOFMATNRORBOM  VALUES ZLOG.
  COMMIT WORK AND WAIT.

  CLEAR GT_STPO_INPUT[].

ENDFUNCTION.