欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

SAP 采购信息记录创建

试了很多网上采购信息记录的创建,各种函数的 都没有完美的,最后还是决定使用BDC。

把功能封装成函数 方便调用。

其中关键点:交货日期和单价重新定义了变量。

 

 

 

 

FUNCTION Y_TEST_FM_BP006.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  TABLES
*"      TT_INPUT STRUCTURE  ZBP003
*"      TT_RETURN STRUCTURE  BAPIRET1
*"----------------------------------------------------------------------

  LOOP AT TT_INPUT.

    "首先判断采购信息记录是否已创建
    DATA:ZAPLFZ TYPE STRING.
    DATA:ZNETPR TYPE STRING.
    ZAPLFZ = TT_INPUT-APLFZ.
    ZNETPR = TT_INPUT-NETPR.
    TT_INPUT-MATNR  = |{ TT_INPUT-MATNR ALPHA = IN WIDTH = 18 }| .
    TT_INPUT-LIFNR  = |{ TT_INPUT-LIFNR ALPHA = IN }|.
    "获取交货计划天数
    SELECT SINGLE PLIFZ
    INTO @DATA(LV_PLIFZ)
          FROM MARC
          WHERE MATNR = @TT_INPUT-MATNR
          AND WERKS = @TT_INPUT-WERKS.
    "若系统中已维护交货计划天数,则使用系统中数据
    IF LV_PLIFZ IS NOT INITIAL.
      TT_INPUT-APLFZ = LV_PLIFZ.
      CLEAR LV_PLIFZ.
    ENDIF.

    SELECT * INTO TABLE @DATA(IT_EINE_NEW)
          FROM EINA AS A INNER JOIN EINE AS B ON A~INFNR = B~INFNR
          WHERE A~LIFNR = @TT_INPUT-LIFNR
          AND A~MATNR = @TT_INPUT-MATNR
          AND B~WERKS = @TT_INPUT-WERKS
          AND B~EKORG = @TT_INPUT-EKORG
          AND B~ESOKZ = @TT_INPUT-ESOKZ.

    IF IT_EINE_NEW[] IS INITIAL. "新建

      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0100'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'RM06I-NORMB'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '/00'.
      PERFORM BDC_FIELD       USING 'EINA-LIFNR'
            TT_INPUT-LIFNR.
      PERFORM BDC_FIELD       USING 'EINA-MATNR'
            TT_INPUT-MATNR.
      PERFORM BDC_FIELD       USING 'EINE-EKORG'
            TT_INPUT-EKORG.
      PERFORM BDC_FIELD       USING 'EINE-WERKS'
            TT_INPUT-WERKS.

      IF TT_INPUT-ESOKZ = '0'.
        PERFORM BDC_FIELD       USING 'RM06I-NORMB'
              'X'.
      ELSEIF TT_INPUT-ESOKZ = '2'.
        PERFORM BDC_FIELD       USING 'RM06I-KONSI'
              'X'.
      ENDIF.

      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0101'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'EINA-MAHN1'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '/00'.
      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0102'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'EINE-MWSKZ'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '/00'.
      PERFORM BDC_FIELD       USING 'EINE-APLFZ'
            ZAPLFZ.
      PERFORM BDC_FIELD       USING 'EINE-NORBM'
            '1'.
      PERFORM BDC_FIELD       USING 'EINE-WEBRE'
            'X'.
      PERFORM BDC_FIELD       USING 'EINE-MWSKZ'
*            'J7'.
      TT_INPUT-MWSKZ.
      PERFORM BDC_FIELD       USING 'EINE-IPRKZ'
            'D'.
      PERFORM BDC_FIELD       USING 'EINE-NETPR'
*            '13'.
      ZNETPR.
      PERFORM BDC_FIELD       USING 'EINE-WAERS'
*            'CNY'.
      TT_INPUT-WAERS.
      PERFORM BDC_FIELD       USING 'EINE-PEINH'
            '1'.
      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0105'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'EINE-ANGNR'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '=KO'.
      PERFORM BDC_DYNPRO      USING 'SAPMV13A' '1017'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'RV13A-DATBI(01)'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '=SICH'.
      PERFORM BDC_FIELD       USING 'RV13A-DATAB(01)'
            TT_INPUT-DATEB.
      PERFORM BDC_FIELD       USING 'RV13A-DATBI(01)'
            TT_INPUT-DATEE.
      CALL TRANSACTION 'ME11' USING BDCDATA
            MODE 'N'
            UPDATE 'L'
            MESSAGES INTO MESSTAB.
      COMMIT WORK AND WAIT.
      CLEAR EE.
      LOOP AT MESSTAB WHERE MSGTYP = 'E'.
        EE = 'X'.
        CLEAR MESSTAB.
      ENDLOOP.
      CLEAR: MESSTAB,ZNETPR,ZAPLFZ.
      IF EE NE 'X'.
        READ TABLE MESSTAB WITH KEY MSGTYP = 'S' DYNAME = 'SAPMM06I'.
        TT_RETURN-ID = MESSTAB-MSGV1.
        TT_RETURN-MESSAGE = |采购信息记录{ TT_RETURN-ID }创建成功!|.
        TT_RETURN-TYPE = 'S'.
        APPEND TT_RETURN.
        CLEAR: TT_RETURN.
      ELSE.
        CLEAR ITAB_100[].
        READ TABLE MESSTAB WITH KEY MSGTYP = 'E'.
        SELECT *
        INTO TABLE ITAB_100
        FROM T100
        WHERE SPRSL = MESSTAB-MSGSPRA
        AND ARBGB = MESSTAB-MSGID
        AND MSGNR = MESSTAB-MSGNR.
        IF SY-SUBRC = 0.
          READ TABLE ITAB_100 INDEX 1.
          L_MSTRING = ITAB_100-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 NO-GAPS.
          CONCATENATE '创建失败:' L_MSTRING INTO L_MSTRING.
          TT_RETURN-MESSAGE = L_MSTRING.
          TT_RETURN-TYPE = 'E'.
          APPEND TT_RETURN.
          CLEAR TT_RETURN.
        ENDIF.
      ENDIF.
    ELSEIF IT_EINE_NEW[] IS NOT INITIAL. "修改
      CLEAR: BDCDATA[],MESSTAB[].
      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0100'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'EINA-LIFNR'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '/00'.
      PERFORM BDC_FIELD       USING 'EINA-LIFNR'
            TT_INPUT-LIFNR.
      PERFORM BDC_FIELD       USING 'EINA-MATNR'
            TT_INPUT-MATNR.
      PERFORM BDC_FIELD       USING 'EINE-EKORG'
            TT_INPUT-EKORG.
      PERFORM BDC_FIELD       USING 'EINE-WERKS'
            TT_INPUT-WERKS.
      IF TT_INPUT-ESOKZ = '0'.
        PERFORM BDC_FIELD       USING 'RM06I-NORMB'
              'X'.
      ELSEIF TT_INPUT-ESOKZ = '2'.
        PERFORM BDC_FIELD       USING 'RM06I-KONSI'
              'X'.
      ENDIF.
      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0101'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'EINA-MAHN1'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '/00'.
      PERFORM BDC_DYNPRO      USING 'SAPMM06I' '0102'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'EINE-MWSKZ'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '=KO'.
      PERFORM BDC_FIELD       USING 'EINE-APLFZ'
            ZAPLFZ.
      PERFORM BDC_FIELD       USING 'EINE-NORBM'
            '1'.
      PERFORM BDC_FIELD       USING 'EINE-WEBRE'
            'X'.
      PERFORM BDC_FIELD       USING 'EINE-MWSKZ'
            TT_INPUT-MWSKZ.
      PERFORM BDC_FIELD       USING 'EINE-IPRKZ'
            'D'.
      PERFORM BDC_DYNPRO      USING 'SAPLV14A' '0102'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'VAKE-DATAB(01)'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '=NEWD'.
      PERFORM BDC_DYNPRO      USING 'SAPMV13A' '1017'.
      PERFORM BDC_FIELD       USING 'BDC_CURSOR'
            'RV13A-DATBI(01)'.
      PERFORM BDC_FIELD       USING 'BDC_OKCODE'
            '=SICH'.
      PERFORM BDC_FIELD       USING 'KONP-KBETR(01)'
            ZNETPR.
      PERFORM BDC_FIELD       USING 'RV13A-DATAB(01)'
            TT_INPUT-DATEB.
      PERFORM BDC_FIELD       USING 'RV13A-DATBI(01)'
            TT_INPUT-DATEE.
      CALL TRANSACTION 'ME12' USING BDCDATA
            MODE 'N'
            UPDATE 'L'
            MESSAGES INTO MESSTAB.
      COMMIT WORK AND WAIT.
      CLEAR EE.
      LOOP AT MESSTAB WHERE MSGTYP = 'E'.
        EE = 'X'.
        CLEAR MESSTAB.
      ENDLOOP.
      CLEAR: MESSTAB,ZNETPR,ZAPLFZ.
      IF EE NE 'X'.
        READ TABLE MESSTAB WITH KEY MSGTYP = 'S' DYNAME = 'SAPMM06I'.
        TT_RETURN-ID = MESSTAB-MSGV1.
        TT_RETURN-MESSAGE = '更新成功!'.
        TT_RETURN-TYPE = 'S'.
        APPEND TT_RETURN.
        CLEAR TT_RETURN.
      ELSE.
        CLEAR ITAB_100[].
        READ TABLE MESSTAB WITH KEY MSGTYP = 'E'.
        SELECT * INTO TABLE ITAB_100 FROM T100 WHERE SPRSL = MESSTAB-MSGSPRA AND ARBGB = MESSTAB-MSGID AND MSGNR = MESSTAB-MSGNR.
        IF SY-SUBRC = 0.
          READ TABLE ITAB_100 INDEX 1.
          L_MSTRING = ITAB_100-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 NO-GAPS.
          CONCATENATE '更新失败:' L_MSTRING INTO L_MSTRING.
          TT_RETURN-MESSAGE = L_MSTRING.
          TT_RETURN-TYPE = 'E'.
          APPEND TT_RETURN.
          CLEAR TT_RETURN.
        ENDIF.
      ENDIF.
    ENDIF.

  ENDLOOP.



ENDFUNCTION.

 

FUNCTION-POOL ZKING006.                     "MESSAGE-ID ..

* INCLUDE LZKING006D...                      " Local class definition

DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
DATA:   MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA:   E_GROUP_OPENED.
DATA: EE TYPE C.
DATA: ITAB_100 TYPE STANDARD TABLE OF T100 WITH HEADER LINE.
DATA: L_MSTRING TYPE STRING.
***INCLUDE LZKING006F01.
*----------------------------------------------------------------------*

FORM BDC_FIELD USING FNAM FVAL.
  CLEAR BDCDATA.
  BDCDATA-FNAM = FNAM.
  BDCDATA-FVAL = FVAL.
  APPEND BDCDATA.
ENDFORM.

FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.

 

posted @ 2021-11-04 17:23  萧静默  阅读(972)  评论(0编辑  收藏  举报