SAP的BOM删除和维护

1、删除BOM函数
 CSAP_MAT_BOM_DELETE

2、删除BOM(DELETE)方法
参见sap的标准程序RCBOMDEL

3、BOM新增函数
BAPI_MATERIAL_BOM_GROUP_CREATE
*&---------------------------------------------------------------------*
*& Report  ZTS_I_COPY_COSTBOM
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTS_I_COPY_COSTBOM.

TABLES: MAST,MARA,MARC,STKO,STPO,AFKO,AFPO,RESB,T100.

DATA: BEGIN OF IT_TAB OCCURS 0,
        AUFNR LIKE AFKO-AUFNR,
        GSTRI LIKE AFKO-GSTRI,
        GSUZI LIKE AFKO-GSUZI,
        RSNUM LIKE AFKO-RSNUM,
        GAMNG LIKE AFKO-GAMNG,
        MATNR LIKE AFPO-MATNR,
        WERKS LIKE AUFK-WERKS,
        OBJNR LIKE AUFK-OBJNR,
      END OF IT_TAB.

DATA: BEGIN OF IT_SORT OCCURS 0,
        GSTRI LIKE AFKO-GSTRI,
        GSUZI LIKE AFKO-GSUZI,
        AUFNR LIKE AFKO-AUFNR,
        RSNUM LIKE AFKO-RSNUM,
        GAMNG LIKE AFKO-GAMNG,
        MATNR LIKE AFPO-MATNR,
        WERKS LIKE AUFK-WERKS,
        OBJNR LIKE AUFK-OBJNR,
      END OF IT_SORT.

DATA: BEGIN OF IT_TAB2 OCCURS 0,
        AUFNR LIKE AFKO-AUFNR,
        RSNUM LIKE AFKO-RSNUM,
        MATNR LIKE MARA-MATNR,
        WERKS LIKE MARC-WERKS,
        GAMNG LIKE AFKO-GAMNG,
        FLAG(1),
        MSG(100),
      END OF IT_TAB2.

DATA: BEGIN OF IT_ITEM OCCURS 0 ,
        RSNUM LIKE AFKO-RSNUM,
        MATNR LIKE RESB-MATNR,
        BDMNG LIKE RESB-BDMNG,
        MEINS LIKE RESB-MEINS,
        POSTP LIKE RESB-POSTP,
        WERKS LIKE MARC-WERKS,
        LVORM LIKE MARC-LVORM,
        MMSTA LIKE MARC-MMSTA,
        MSTAE LIKE MARA-MSTAE,
      END OF IT_ITEM .

DATA: IT_ITEM2 LIKE IT_ITEM OCCURS 0 WITH HEADER LINE .
DATA: BEGIN OF IT_DEL OCCURS 0 ,
        MATNR LIKE MARA-MATNR ,
        WERKS LIKE MARC-WERKS ,
      END OF IT_DEL .

DATA: BEGIN OF ITAB_STKO OCCURS 0,
        STLNR LIKE STKO-STLNR,
        STLAL LIKE STKO-STLAL,
        STLST LIKE STKO-STLST,
        LKENZ LIKE STKO-LKENZ,
        DATUV LIKE STKO-DATUV,
     END OF ITAB_STKO.
* BDCDATA
DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
* BAPI DATA STRUCTURE
DATA: EX_HEADDATA       LIKE BAPIMATHEAD,
      EX_PLANTDATA      LIKE BAPI_MARC,
      EX_PLANTDATAX     LIKE BAPI_MARCX,
      EX_CLIENTDATA     LIKE BAPI_MARA,
      EX_CLIENTDATAX    LIKE BAPI_MARAX,
      IM_RETURN         LIKE  BAPIRET2.

DATA: G_OK TYPE I,         "SUCCESS
      G_NO TYPE I.         "FAIL

SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: S_WERKS FOR MAST-WERKS OBLIGATORY NO INTERVALS.
SELECT-OPTIONS: S_AUFNR FOR AFKO-AUFNR .
SELECT-OPTIONS: S_MATNR FOR AFKO-PLNBEZ .
SELECT-OPTIONS: S_GSTRI FOR AFKO-GSTRI.
SELECTION-SCREEN END OF BLOCK BLK1.


*---------------------------------------------------------------------*
*        INITIALIZATION                                               *
*---------------------------------------------------------------------*
INITIALIZATION.


*---------------------------------------------------------------------*
*        START-OF-SELECTION                                           *
*---------------------------------------------------------------------*
START-OF-SELECTION.
* 取需要拷贝的工单
  PERFORM FRM_GET_ORDER.
  IF NOT IT_DEL[] IS INITIAL.
    PERFORM FRM_DEL_COSTBOM.
  ENDIF.
  IF NOT IT_ITEM2[] IS INITIAL.
    PERFORM FRM_CHANGE_MAT_STATUS.
  ENDIF.
  PERFORM FRM_COPY_COSTBOM.
*  PERFORM FRM_BAPI_CS01.
  IF NOT IT_ITEM2[] IS INITIAL.
    PERFORM FRM_RETURN_MAT_STATUS.
  ENDIF.
*---------------------------------------------------------------------*
*        END-OF-SELECTION                                             *
*---------------------------------------------------------------------*
END-OF-SELECTION.
* 输出记日志到屏幕
  PERFORM FRM_WRITE_LOG.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_ORDER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_GET_ORDER .
  DATA: BEGIN OF IT_TEMP OCCURS 0,
          MATNR LIKE MARA-MATNR ,
          WERKS LIKE MARC-WERKS ,
        END OF IT_TEMP .
  DATA: LV_KZKFG LIKE MARA-KZKFG . "可配置物料
  DATA: R_PAIS TYPE RANGE OF JEST-STAT,
        R_PAIS_STAT LIKE LINE OF R_PAIS.
  DATA: R_PAIS2 TYPE RANGE OF JEST-STAT,
        R_PAIS2_STAT LIKE LINE OF R_PAIS2.
  DATA: R_PAIS3 TYPE RANGE OF AUFK-AUART,
        R_PAIS3_AUART LIKE LINE OF R_PAIS3.
  DATA: BEGIN OF GT_JEST OCCURS 0.
  DATA: OBJNR LIKE JEST-OBJNR,
        STAT  LIKE JEST-STAT,
        INACT LIKE JEST-INACT.
  DATA END OF GT_JEST.
  DATA: LV_LINE LIKE BSVX-STTXT .
  DATA: LV_MSTAE LIKE MARA-MSTAE.
  DATA: LV_LVORM LIKE MARA-LVORM.

*
** 工单状态range定义
*  CLEAR: R_PAIS_STAT ,R_PAIS2_STAT  .
*  R_PAIS_STAT-SIGN   = 'I'.
*  R_PAIS_STAT-OPTION = 'EQ'.
*  R_PAIS_STAT-LOW    = 'I0012'.
*  APPEND R_PAIS_STAT TO R_PAIS.
*
*  R_PAIS_STAT-SIGN   = 'I'.
*  R_PAIS_STAT-OPTION = 'EQ'.
*  R_PAIS_STAT-LOW    = 'I0321'.
*  APPEND R_PAIS_STAT TO R_PAIS.
*
*  R_PAIS2_STAT-SIGN   = 'I'.
*  R_PAIS2_STAT-OPTION = 'EQ'.
*  R_PAIS2_STAT-LOW    = 'I0074'.
*  APPEND R_PAIS2_STAT TO R_PAIS2.
*
*  R_PAIS2_STAT-SIGN   = 'I'.
*  R_PAIS2_STAT-OPTION = 'EQ'.
*  R_PAIS2_STAT-LOW    = 'I0321'.
*  APPEND R_PAIS2_STAT TO R_PAIS2.
* 工单类型定义
  CLEAR R_PAIS3_AUART.
  R_PAIS3_AUART-SIGN   = 'I'.
  R_PAIS3_AUART-OPTION = 'EQ'.
  R_PAIS3_AUART-LOW    = 'PP01'.
  APPEND R_PAIS3_AUART TO R_PAIS3.
  R_PAIS3_AUART-SIGN   = 'I'.
  R_PAIS3_AUART-OPTION = 'EQ'.
  R_PAIS3_AUART-LOW    = 'ZPP1'.
  APPEND R_PAIS3_AUART TO R_PAIS3.
  R_PAIS3_AUART-SIGN   = 'I'.
  R_PAIS3_AUART-OPTION = 'EQ'.
  R_PAIS3_AUART-LOW    = 'ZPP3'.
  APPEND R_PAIS3_AUART TO R_PAIS3.
  R_PAIS3_AUART-SIGN   = 'I'.
  R_PAIS3_AUART-OPTION = 'EQ'.
  R_PAIS3_AUART-LOW    = 'ZPP4'.
  APPEND R_PAIS3_AUART TO R_PAIS3.
  R_PAIS3_AUART-SIGN   = 'I'.
  R_PAIS3_AUART-OPTION = 'EQ'.
  R_PAIS3_AUART-LOW    = 'ZPP5'.
  APPEND R_PAIS3_AUART TO R_PAIS3.


  SELECT A~AUFNR A~GSTRI A~GSUZI A~RSNUM A~GAMNG C~MATNR B~WERKS B~OBJNR
    INTO CORRESPONDING FIELDS OF TABLE IT_TAB
    FROM AFKO AS A
    INNER JOIN AUFK AS B ON A~AUFNR = B~AUFNR
    INNER JOIN AFPO AS C ON A~AUFNR = C~AUFNR
*    INNER JOIN JEST AS C ON B~OBJNR = C~OBJNR
    WHERE A~AUFNR IN S_AUFNR
      AND A~GSTRI IN S_GSTRI
      AND C~MATNR IN S_MATNR
      AND B~WERKS IN S_WERKS
      AND B~AUART IN R_PAIS3
      AND B~LOEKZ NE 'X'.

  LOOP AT IT_TAB.
    CLEAR LV_LINE.
    CALL FUNCTION 'STATUS_TEXT_EDIT'
      EXPORTING
*   CLIENT                  = SY-MANDT
*   FLG_USER_STAT           = ' '
        OBJNR                   = IT_TAB-OBJNR
        ONLY_ACTIVE             = 'X'
        SPRAS                   = '1'
*   BYPASS_BUFFER           = ' '
     IMPORTING
*   ANW_STAT_EXISTING       =
*   E_STSMA                 =
       LINE                    =  LV_LINE
*   USER_LINE               =
*   STONR                   =
     EXCEPTIONS
       OBJECT_NOT_FOUND        = 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.

    ELSE.
      IF ( LV_LINE CS 'DLV' AND LV_LINE CS 'GMPS' ) OR
         ( LV_LINE CS 'PDLV' AND LV_LINE CS 'GMPS' ).
        IT_TEMP-MATNR = IT_TAB-MATNR .
        IT_TEMP-WERKS = IT_TAB-WERKS .
        APPEND IT_TEMP.
        CLEAR : IT_TAB,IT_TEMP .
      ELSE.
        DELETE IT_TAB.
      ENDIF.
    ENDIF.

    CLEAR IT_TAB.
  ENDLOOP.

*  LOOP AT IT_TAB.
*    IT_TEMP-MATNR = IT_TAB-PLNBEZ .
*    IT_TEMP-WERKS = IT_TAB-WERKS .
*    APPEND IT_TEMP.
*    CLEAR : IT_TAB,IT_TEMP .
*  ENDLOOP.
* 删除重复的bom
* 没有激活的无效BOM
*  DATA: LV_TIX LIKE SY-TABIX .
  SORT IT_TEMP BY MATNR WERKS .
  DELETE ADJACENT DUPLICATES FROM IT_TEMP COMPARING MATNR WERKS.
*  LOOP AT IT_TEMP .
*    LV_TIX = SY-TABIX.
*    CLEAR ITAB_STKO.
*    REFRESH ITAB_STKO.
*    CLEAR MAST.
*    SELECT SINGLE * FROM MAST WHERE MATNR EQ IT_TEMP-MATNR
*                                AND WERKS EQ IT_TEMP-WERKS.
*    IF SY-SUBRC EQ 0.
*      SELECT STLNR STLAL STLST LKENZ DATUV
*        FROM STKO
*        INTO CORRESPONDING FIELDS OF TABLE ITAB_STKO
*       WHERE STLNR = MAST-STLNR
*         AND STLAL = MAST-STLAL
*         AND DATUV <= SY-DATUM
*        ORDER BY DATUV DESCENDING.
*
*      READ TABLE ITAB_STKO INDEX 1.
*
*      IF ITAB_STKO-STLST <> '01' OR ITAB_STKO-LKENZ <> SPACE.
*        "BOM未激活或者已经删除
*        DELETE IT_TEMP INDEX LV_TIX.
*      ENDIF.
*    ENDIF.

*  ENDLOOP.
* 可配置的bom不需要
  LOOP AT IT_TEMP.
    SELECT SINGLE KZKFG MSTAE LVORM
      INTO (LV_KZKFG,LV_MSTAE,LV_LVORM)
      FROM MARA
      WHERE MATNR EQ IT_TEMP-MATNR.
    IF LV_KZKFG EQ 'X' OR LV_MSTAE NE ' ' OR LV_LVORM NE ' '.
      DELETE IT_TEMP .
    ELSE.
      SELECT SINGLE * FROM MAST WHERE WERKS EQ IT_TEMP-WERKS
                                  AND MATNR EQ IT_TEMP-MATNR
                                  AND STLAN EQ '6'.
      IF SY-SUBRC EQ 0.
        APPEND IT_TEMP TO IT_DEL.
      ENDIF.
    ENDIF.
    CLEAR MARC.
    SELECT SINGLE * FROM MARC WHERE MATNR EQ IT_TEMP-MATNR
                               AND WERKS EQ IT_TEMP-WERKS.
    IF MARC-MMSTA NE ' ' OR MARC-LVORM NE ' '.
      DELETE IT_TEMP .
    ENDIF.

    CLEAR IT_TEMP.

  ENDLOOP.

  LOOP AT IT_TAB.
    MOVE-CORRESPONDING IT_TAB TO IT_SORT.
    APPEND IT_SORT.
    CLEAR: IT_TAB,IT_SORT.
  ENDLOOP.

  SORT IT_SORT BY GSTRI DESCENDING GSUZI DESCENDING .

  LOOP AT IT_TEMP.
    READ TABLE IT_SORT WITH KEY MATNR = IT_TEMP-MATNR
                                WERKS = IT_TEMP-WERKS.
    IF SY-SUBRC EQ 0.
      IT_TAB2-AUFNR = IT_SORT-AUFNR .
      IT_TAB2-RSNUM = IT_SORT-RSNUM .
      IT_TAB2-MATNR = IT_SORT-MATNR.
      IT_TAB2-WERKS = IT_SORT-WERKS .
      IT_TAB2-GAMNG = IT_SORT-GAMNG .
      APPEND IT_TAB2.
      CLEAR IT_TAB2.
    ENDIF.
    CLEAR:IT_SORT, IT_TEMP.
  ENDLOOP.

  LOOP AT IT_TAB2.

    SELECT A~RSNUM A~MATNR A~BDMNG A~MEINS A~POSTP B~WERKS B~LVORM B~MMSTA C~MSTAE
     APPENDING CORRESPONDING FIELDS OF TABLE IT_ITEM
      FROM RESB AS A
      INNER JOIN MARC AS B ON A~MATNR = B~MATNR
                          AND A~WERKS = B~WERKS
      INNER JOIN MARA AS C ON A~MATNR = C~MATNR
     WHERE A~RSNUM EQ IT_TAB2-RSNUM
       AND A~WERKS EQ IT_TAB2-WERKS
       AND A~BDMNG NE ' '
       AND C~MTART NE 'LEER'.                               "剔除69
*       AND B~LVORM NE 'X'
*       AND B~MMSTA EQ ' '
*       AND C~MSTAE EQ ' '.

    CLEAR IT_TAB2.
  ENDLOOP.

  LOOP AT IT_ITEM .
    IF IT_ITEM-LVORM EQ 'X' OR IT_ITEM-MMSTA NE ' ' OR IT_ITEM-MSTAE NE ' '.
      APPEND IT_ITEM TO IT_ITEM2 .
    ENDIF.
    CLEAR IT_ITEM.
  ENDLOOP.

 


ENDFORM.                    " FRM_GET_ORDER
*&---------------------------------------------------------------------*
*&      Form  FRM_COPY_COSTBOM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_COPY_COSTBOM .
  DATA:L_OPT LIKE CTU_PARAMS.
  DATA:   L_MSTRING(200)  TYPE C.
  DATA:   MESSAGE1(200)  TYPE C,
          MESSAGE2(200)  TYPE C.
  DATA: LV_FLAG(1).
  DATA: LV_HEDQTY(13).
  DATA: LV_ITEMQTY(13).


  L_OPT-DISMODE = 'N'.
  L_OPT-UPDMODE = 'S'.
  L_OPT-CATTMODE = ''.
  L_OPT-DEFSIZE = 'X'.
*  L_OPT-NOBINPT = 'X'.

  LOOP AT IT_TAB2.

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

    LV_HEDQTY = IT_TAB2-GAMNG.

    PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0100'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                  '/00'.
    PERFORM BDC_FIELD       USING 'RC29N-MATNR'
                                  IT_TAB2-MATNR.
    PERFORM BDC_FIELD       USING 'RC29N-WERKS'
                                  IT_TAB2-WERKS.
    PERFORM BDC_FIELD       USING 'RC29N-STLAN'
                                  '6'.
*    PERFORM BDC_FIELD       USING 'RC29N-AENNR'
*                                  ''.

    PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0110'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                  '/00'.
    PERFORM BDC_FIELD       USING 'RC29K-BMENG'
                                   LV_HEDQTY.

    PERFORM BDC_FIELD       USING 'RC29K-STLST'
                                    '1'.

    PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0111'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                  '/00'.


    LOOP AT IT_ITEM WHERE RSNUM = IT_TAB2-RSNUM.
      LV_ITEMQTY = IT_ITEM-BDMNG.


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

      IF LV_FLAG = ' '.

        LV_FLAG = 'X'.

        PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0140'.
        PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                      '/00'.
        PERFORM BDC_FIELD       USING 'RC29P-IDNRK(01)'
                                       IT_ITEM-MATNR.
        PERFORM BDC_FIELD       USING 'RC29P-MENGE(01)'
                                      LV_ITEMQTY.
        PERFORM BDC_FIELD       USING 'RC29P-POSTP(01)'
                                      IT_ITEM-POSTP.

        IF IT_ITEM-POSTP EQ 'L'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0130'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0131'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.

        ELSEIF IT_ITEM-POSTP EQ 'N'.
          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0130'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0131'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0133'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.
          PERFORM BDC_FIELD       USING  'RC29P-EKORG' ' '.


        ENDIF.

        PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0140'.
        PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '=FCNP'.
      ELSE.

        PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0140'.
        PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                      '/00'.
        PERFORM BDC_FIELD       USING 'RC29P-IDNRK(02)'
                                       IT_ITEM-MATNR.
        PERFORM BDC_FIELD       USING 'RC29P-MENGE(02)'
                                      LV_ITEMQTY.
        PERFORM BDC_FIELD       USING 'RC29P-POSTP(02)'
                                      IT_ITEM-POSTP.

        IF IT_ITEM-POSTP EQ 'L'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0130'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0131'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.


        ELSEIF IT_ITEM-POSTP EQ 'N'.
          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0130'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.

          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0131'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.


          PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0133'.
          PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '/00'.
          PERFORM BDC_FIELD       USING  'RC29P-EKORG' ' '.


        ENDIF.

        PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0140'.
        PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                        '=FCNP'.


      ENDIF.

      CLEAR IT_ITEM.
    ENDLOOP.

    PERFORM BDC_DYNPRO      USING 'SAPLCSDI' '0140'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                  '=FCBU'.

*CALL CS01 CREATE COST BOM
    CALL TRANSACTION 'CS01' USING BDCDATA
                            OPTIONS FROM L_OPT
                            MESSAGES INTO MESSTAB.

* BDC SUCCESS
    IF SY-SUBRC = 0.
      IT_TAB2-FLAG = '0'.
      IT_TAB2-MSG = '创建成本BOM成功'.
      ADD 1 TO G_OK.
* BDC ERROR
    ELSE.
      LOOP AT MESSTAB WHERE MSGTYP = 'S'
                         OR MSGTYP = 'I'
                         OR MSGTYP = 'E'
                         OR MSGTYP = 'A'.
      ENDLOOP.
      IF NOT MESSTAB IS INITIAL.
        CLEAR: MESSAGE1,
               MESSAGE2.
        LOOP AT MESSTAB.
          SELECT SINGLE * FROM T100 WHERE SPRSL = MESSTAB-MSGSPRA
                                    AND   ARBGB = MESSTAB-MSGID
                                    AND   MSGNR = MESSTAB-MSGNR.
          IF SY-SUBRC = 0.
            L_MSTRING = T100-TEXT.
            IF L_MSTRING CS '&1'.
              REPLACE '&1' WITH MESSTAB-MSGV1 INTO L_MSTRING.
              REPLACE '&2' WITH MESSTAB-MSGV2 INTO L_MSTRING.
              REPLACE '&3' WITH MESSTAB-MSGV3 INTO L_MSTRING.
              REPLACE '&4' WITH MESSTAB-MSGV4 INTO L_MSTRING.
            ELSE.
              REPLACE '&' WITH MESSTAB-MSGV1 INTO L_MSTRING.
              REPLACE '&' WITH MESSTAB-MSGV2 INTO L_MSTRING.
              REPLACE '&' WITH MESSTAB-MSGV3 INTO L_MSTRING.
              REPLACE '&' WITH MESSTAB-MSGV4 INTO L_MSTRING.
            ENDIF.
            CONDENSE L_MSTRING.
            CONCATENATE MESSAGE1 L_MSTRING ';' INTO MESSAGE1.
          ELSE.
            CONCATENATE MESSAGE2 MESSTAB ';' INTO MESSAGE2.
          ENDIF.
          CLEAR: MESSTAB,
                 L_MSTRING.
        ENDLOOP.
        CONCATENATE MESSAGE1 MESSAGE2 INTO IT_TAB2-MSG.
        IT_TAB2-FLAG = '1'.
        ADD 1 TO G_NO.
      ENDIF.

    ENDIF.

    MODIFY IT_TAB2.

    CLEAR:LV_FLAG,IT_TAB2,LV_HEDQTY,LV_ITEMQTY.

    REFRESH BDCDATA.CLEAR BDCDATA.
    REFRESH MESSTAB.CLEAR MESSTAB.
  ENDLOOP.

 

 

ENDFORM.                    " FRM_COPY_COSTBOM
*&---------------------------------------------------------------------*
*&      Form  FRM_WRITE_LOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_WRITE_LOG .

*BDC RESULT
  FORMAT COLOR 5 ON.
  WRITE:/,(40) '运行结果' CENTERED.
  FORMAT COLOR OFF.

  FORMAT COLOR 6 ON.
  WRITE:/ '创建成本bom成功:',G_OK,'条'.
  FORMAT COLOR OFF.

  SKIP.

  FORMAT COLOR 6 ON.
  WRITE:/ '创建成本bom失败:',G_NO,'条'.
  FORMAT COLOR OFF.

  SORT IT_TAB2 BY FLAG .
  LOOP AT IT_TAB2 .

    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        INPUT        = IT_TAB2-MATNR
      IMPORTING
        OUTPUT       = IT_TAB2-MATNR
      EXCEPTIONS
        LENGTH_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.


    WRITE:/1(10) IT_TAB2-AUFNR,
            (10) IT_TAB2-RSNUM,
            (18) IT_TAB2-MATNR,
            (04) IT_TAB2-WERKS,
           (100) IT_TAB2-MSG.

  ENDLOOP.

ENDFORM.                    " FRM_WRITE_LOG

*&---------------------------------------------------------------------*
*&      Form  bdc_dynpro
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0644   text
*      -->P_0645   text
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.                    "BDC_DYNPRO

*&---------------------------------------------------------------------*
*&      Form  bdc_field
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0639   text
*      -->P_0640   text
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
**  IF FVAL <> NODATA.
  CLEAR BDCDATA.
  BDCDATA-FNAM = FNAM.
  BDCDATA-FVAL = FVAL.
  APPEND BDCDATA.
**  ENDIF.
ENDFORM.                    "BDC_FIELD
*&---------------------------------------------------------------------*
*&      Form  FRM_CHANGE_MAT_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_CHANGE_MAT_STATUS .

  LOOP AT IT_ITEM2.
    CLEAR: EX_HEADDATA,EX_CLIENTDATA,EX_CLIENTDATAX, EX_PLANTDATA, EX_PLANTDATAX, IM_RETURN.

    MOVE: IT_ITEM2-MATNR TO EX_HEADDATA-MATERIAL,
          'X'            TO EX_HEADDATA-BASIC_VIEW,
          'X'            TO EX_HEADDATA-MRP_VIEW.

    IF IT_ITEM2-LVORM EQ 'X'.
      MOVE: ' ' TO EX_CLIENTDATA-DEL_FLAG.
      MOVE: 'X' TO EX_CLIENTDATAX-DEL_FLAG.
    ENDIF.

    IF IT_ITEM2-MSTAE NE ' '.
      MOVE: ' ' TO EX_CLIENTDATA-PUR_STATUS.
      MOVE: 'X' TO EX_CLIENTDATAX-PUR_STATUS.
    ENDIF.

    IF IT_ITEM2-MMSTA NE ' '.
      MOVE: IT_ITEM2-WERKS     TO EX_PLANTDATA-PLANT,
            ' '     TO EX_PLANTDATA-PUR_STATUS.

      MOVE: IT_ITEM2-WERKS TO EX_PLANTDATAX-PLANT,
            'X'           TO EX_PLANTDATAX-PUR_STATUS.
    ENDIF.

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        HEADDATA    = EX_HEADDATA
        CLIENTDATA  = EX_CLIENTDATA
        CLIENTDATAX = EX_CLIENTDATAX
        PLANTDATA   = EX_PLANTDATA
        PLANTDATAX  = EX_PLANTDATAX
      IMPORTING
        RETURN      = IM_RETURN.


    IF IM_RETURN-TYPE = 'E'.

    ELSE.
      COMMIT WORK AND WAIT.
      WAIT UP TO 5 SECONDS.

    ENDIF.

  ENDLOOP.

 

ENDFORM.                    " FRM_CHANGE_MAT_STATUS
*&---------------------------------------------------------------------*
*&      Form  FRM_RETURN_MAT_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_RETURN_MAT_STATUS .
  LOOP AT IT_ITEM2.
    CLEAR: EX_HEADDATA,EX_CLIENTDATA,EX_CLIENTDATAX, EX_PLANTDATA, EX_PLANTDATAX, IM_RETURN.

    MOVE: IT_ITEM2-MATNR TO EX_HEADDATA-MATERIAL,
          'X'            TO EX_HEADDATA-BASIC_VIEW,
          'X'            TO EX_HEADDATA-MRP_VIEW.

    IF IT_ITEM2-LVORM EQ 'X'.
      MOVE: 'X' TO EX_CLIENTDATA-DEL_FLAG.
      MOVE: 'X' TO EX_CLIENTDATAX-DEL_FLAG.
    ENDIF.

    IF IT_ITEM2-MMSTA NE ' '.
      MOVE: IT_ITEM2-MMSTA TO EX_CLIENTDATA-PUR_STATUS.
      MOVE: 'X' TO EX_CLIENTDATAX-PUR_STATUS.
    ENDIF.

    IF IT_ITEM2-MMSTA NE ' '.
      MOVE: IT_ITEM2-WERKS     TO EX_PLANTDATA-PLANT,
            IT_ITEM2-MMSTA     TO EX_PLANTDATA-PUR_STATUS.

      MOVE: IT_ITEM2-WERKS TO EX_PLANTDATAX-PLANT,
            'X'           TO EX_PLANTDATAX-PUR_STATUS.
    ENDIF.

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        HEADDATA    = EX_HEADDATA
        CLIENTDATA  = EX_CLIENTDATA
        CLIENTDATAX = EX_CLIENTDATAX
        PLANTDATA   = EX_PLANTDATA
        PLANTDATAX  = EX_PLANTDATAX
      IMPORTING
        RETURN      = IM_RETURN.


    IF IM_RETURN-TYPE = 'E'.

    ELSE.
      COMMIT WORK AND WAIT.

    ENDIF.

  ENDLOOP.
ENDFORM.                    " FRM_RETURN_MAT_STATUS
*&---------------------------------------------------------------------*
*&      Form  FRM_DEL_COSTBOM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_DEL_COSTBOM .
  DATA: LV_TEMP LIKE CAPIFLAG-FLWARNING.

  LOOP AT IT_DEL.

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

    CALL FUNCTION 'CSAP_MAT_BOM_DELETE'
      EXPORTING
        MATERIAL   = IT_DEL-MATNR
        PLANT      = IT_DEL-WERKS
        BOM_USAGE  = '6'
      IMPORTING
        FL_WARNING = LV_TEMP
      EXCEPTIONS
        ERROR      = 1
        OTHERS     = 2.

    IF SY-SUBRC EQ 0.
      COMMIT WORK AND WAIT.
      WAIT UP TO 3 SECONDS.
    ENDIF.

  ENDLOOP.

ENDFORM.                    " FRM_DEL_COSTBOM
*&---------------------------------------------------------------------*
*&      Form  frm_bapi_cs01
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_BAPI_CS01 .
  DATA: ES_MAST LIKE BAPIBOMMST .
  DATA: ES_STZU LIKE BAPIBOMSTZ .
  DATA: IM_RET LIKE BAPIRET2 .
  DATA: IS_STKO LIKE BAPIBOMSTK OCCURS 0 WITH HEADER LINE.
  DATA: IS_STPO LIKE BAPIBOMSTP OCCURS 0 WITH HEADER LINE.
  DATA: IS_STAS LIKE BAPIBOMSTA OCCURS 0 WITH HEADER LINE.
*
  DATA: LV_ITEM TYPE I.

  LOOP AT IT_TAB2.

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


    REFRESH IS_STPO . CLEAR ES_MAST . CLEAR IM_RET. CLEAR IS_STKO.
**** BAPI 参数 BOM MAST表****************************
    MOVE : '009' TO ES_MAST-OPERATION ,
           IT_TAB2-MATNR TO ES_MAST-MATERIAL,
           IT_TAB2-WERKS TO ES_MAST-PLANT,
           SY-DATUM TO ES_MAST-CREATE_DAT,
*           '01' TO ES_MAST-ALT_BOM,
           '6' TO ES_MAST-BOM_USAGE.

    MOVE :
           '009' TO IS_STKO-OPERATION,
           'M' TO IS_STKO-BOM_CATEGO,
*           '01' TO IS_STKO-ALT_BOM,
           SY-DATUM TO IS_STKO-VALID_FROM,
           IT_TAB2-GAMNG TO IS_STKO-BASE_QTY.
    APPEND IS_STKO.
    CLEAR IS_STKO.

*    LOOP AT IT_ITEM WHERE RSNUM = IT_TAB2-RSNUM.
*
**      CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
**        EXPORTING
**          INPUT  = IT_ITEM-MATNR
**        IMPORTING
**          OUTPUT = IT_ITEM-MATNR.
*
*      MOVE:
*             '009' TO IS_STPO-OPERATION,
**             IT_ITEM-MEINS TO IS_STPO-UNCOMP,
*             IT_ITEM-BDMNG TO IS_STPO-COMPON_QTY,
**              '1' TO IS_STPO-COMPON_QTY,
*             IT_ITEM-MATNR TO IS_STPO-COMPONENT,
*             IT_ITEM-POSTP TO IS_STPO-ITEM_CAT.
*
*
*      APPEND IS_STPO.
*      CLEAR IS_STPO.
*
*    ENDLOOP.

    MOVE:
           '009' TO IS_STPO-OPERATION,
*             IT_ITEM-MEINS TO IS_STPO-UNCOMP,
           '1' TO IS_STPO-COMPON_QTY,
           '000000000000002009' TO IS_STPO-COMPONENT,
           'N' TO IS_STPO-ITEM_CAT.
*             IT_ITEM-aennr TO IS_STPO-change_no,
*             LV_ITEM TO IS_STPO-BOM_ITM_NO.

    APPEND IS_STPO.
*
*      MOVE:
*             '009' TO IS_STPO-OPERATION,
**             IT_ITEM-MEINS TO IS_STPO-UNCOMP,
*             '1' TO IS_STPO-COMPON_QTY,
*             '000000000031020744' TO IS_STPO-COMPONENT,
*             'L' TO IS_STPO-ITEM_CAT.
**             IT_ITEM-aennr TO IS_STPO-change_no,
**             LV_ITEM TO IS_STPO-BOM_ITM_NO.
*
*      APPEND IS_STPO.

    CALL FUNCTION 'BAPI_BOM_UPLOAD_SAVE'
      EXPORTING
        IS_MAST   = ES_MAST
        IS_STZU   = ES_STZU
      IMPORTING
        ES_RETURN = IM_RET
      TABLES
        IT_STKO   = IS_STKO
        IT_STPO   = IS_STPO
        IT_STAS   = IS_STAS.

    COMMIT WORK.
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ELSE.

      COMMIT WORK AND WAIT .

    ENDIF.
    CLEAR IT_TAB2.

  ENDLOOP.

ENDFORM.                    " frm_bapi_cs01

posted on 2006-09-12 23:55  毛小娃  阅读(1449)  评论(0编辑  收藏  举报

导航