MM MIGO BAPI BAPI_GOODSMVT_CREATE 使用汇总

本文汇总了SAP中常见的移动类型与其对应的GOODSMVT_CODE,包括冻结库存、解冻库存、收货、退货、生产退料、生产订单收货等业务场景,帮助理解并查找自定义移动类型的GOODSMVT_CODE。
摘要由CSDN通过智能技术生成

**********GOODSMVT_CODE取值含义*******
*01     MB01
*02     MB31
*03     MB1A "发
*04     MB1B "转储
*05     MB1C "其它收货
*06     MB11
*07     MB04

经常会遇到一些自定义的移动类型,但是并不知道对应的goodsmvt_code是多少。可以用如下方法进行查找

首先去T158B中根据移动类型找到对应的事务代码:如图

首先去T158B中根据移动类型找到对应的事务代码:如图

  然后再将对应的事务代码去T158G 中找code,如图:

 根据移动类型获取BAPI的code

* Assign Code to Transaction for Goods Movement
  SELECT SINGLE g~gmcode FROM t158b AS b
  INNER JOIN t158g AS g ON g~tcode = b~tcode
  INTO l_code
  WHERE b~bwart = is_head-bwart.

 1.移动类型 344 冻结库存

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '04'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
  LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.   " 从
  LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
  LW_GOODSMVT_ITEM-MOVE_TYPE       = '344'.
  LW_GOODSMVT_ITEM-ENTRY_QNT       = LS_ITEM-QTY .
  LW_GOODSMVT_ITEM-MOVE_PLANT      = LS_ITEM-WERKS .   " 到
  LW_GOODSMVT_ITEM-MOVE_STLOC      = LS_ITEM-LGORT .
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

2. 移动类型 343 解冻库存

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '04'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
  LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.   " 从
  LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
  LW_GOODSMVT_ITEM-MOVE_TYPE       = '343'.
  LW_GOODSMVT_ITEM-ENTRY_QNT       = LS_ITEM-QTY .
  LW_GOODSMVT_ITEM-MOVE_PLANT      = LS_ITEM-WERKS .   " 到
  LW_GOODSMVT_ITEM-MOVE_STLOC      = LS_ITEM-LGORT .
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

3. 移动类型 101 收货,161退货(161退货时移动移动类型也填101,成功后会自动变成161)

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
 
  LV_GOODSMVT_CODE = '01'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
   LW_GOODSMVT_ITEM-PO_NUMBER =    GS_ITEM-EBELN.         "采购凭证号
   LW_GOODSMVT_ITEM-ITEM_TEXT =    GS_ITEM-WMSNO.         "项目文本
   LW_GOODSMVT_ITEM-PO_ITEM   =    GS_ITEM-ZEILE.         "采购凭证的项目编号
   LW_GOODSMVT_ITEM-PLANT     =    GS_ITEM-WERKS.         "工厂
   LW_GOODSMVT_ITEM-STGE_LOC  =    GS_ITEM-LGORT.         "库存地点
   LW_GOODSMVT_ITEM-BATCH     =    GS_ITEM-CHARG.         "批次号
   LW_GOODSMVT_ITEM-ENTRY_QNT =    GS_ITEM-ERFMG.         "以录入项单位表示的数量
   LW_GOODSMVT_ITEM-MVT_IND   =    'B'.            "移动标识  ’B‘为采购收货  ’F ' 生产收货
   LW_GOODSMVT_ITEM-MOVE_TYPE =    '101'.                 "移动类型
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
 
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE     "MB01 按采购订单的货物移动
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

4. 移动类型 Z21 生产退料

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
 
  LV_GOODSMVT_CODE = '03'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
  LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.   " 从
  LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
  LW_GOODSMVT_ITEM-MOVE_TYPE       = 'Z21'.
  LW_GOODSMVT_ITEM-ENTRY_QNT       = LS_ITEM-QTY .
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
 
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

5. 移动类型 101 生产订单收货

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '02'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL   =    LS_ITEM-MATNR.         "物料编号
  LW_GOODSMVT_ITEM-PLANT      =    LS_ITEM-WERKS.         "工厂
  LW_GOODSMVT_ITEM-STGE_LOC   =    LS_ITEM-LGORT.         "库存地点
  LW_GOODSMVT_ITEM-BATCH      =    LS_ITEM-CHARG.         "批次号
  LW_GOODSMVT_ITEM-ENTRY_QNT  =    LS_ITEM-ERFMG.         "以录入项单位表示的数量
  LW_GOODSMVT_ITEM-MOVE_STLOC =    LS_ITEM-UMLGO.         "收货库存地点
  LW_GOODSMVT_ITEM-MOVE_BATCH =    LS_ITEM-UMCHA.         "收货批次
  LW_GOODSMVT_ITEM-STCK_TYPE  = '2' . "质检状态
  LW_GOODSMVT_ITEM-MVT_IND    =    'F'.             "移动标识  ’B‘为采购收货  ’F ' 生产收货
  LW_GOODSMVT_ITEM-MOVE_TYPE  =    '101'.                 "移动类型
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

6. 移动类型 501 无源单入库

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '05'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
  LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.   " 从
  LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
  LW_GOODSMVT_ITEM-MOVE_TYPE       = '501'.
  LW_GOODSMVT_ITEM-VENDOR          =  LS_ITEM-LIFNR.         "供应商编码
  LW_GOODSMVT_ITEM-ENTRY_QNT       =  LS_ITEM-QTY .
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

7.移动类型 321 状态转移

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
 
  LV_GOODSMVT_CODE = '04'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        =    LS_ITEM-MATNR.         "物料编号
  LW_GOODSMVT_ITEM-PLANT           =    LS_ITEM-WERKS.         "发出工厂
  LW_GOODSMVT_ITEM-STGE_LOC        =    LS_ITEM-LGORT.         "发出库存地点
  LW_GOODSMVT_ITEM-SPEC_STOCK      =    LS_ITEM-UMSOK.         "特殊库存标识
  LW_GOODSMVT_ITEM-SALES_ORD       =    LS_ITEM-KDAUF.         "发出销售订单
  LW_GOODSMVT_ITEM-S_ORD_ITEM      =    LS_ITEM-KDPOS.         "发出销售订单行项目
  LW_GOODSMVT_ITEM-BATCH           =    LS_ITEM-CHARG.         "发出批次号
  LW_GOODSMVT_ITEM-ENTRY_QNT       =    LS_ITEM-ERFMG.         "以录入项单位表示的数量
  LW_GOODSMVT_ITEM-VAL_SALES_ORD   =    LS_ITEM-MAT_KDAUF.     "接收销售订单号
  LW_GOODSMVT_ITEM-VAL_S_ORD_ITEM  =    LS_ITEM-MAT_KDPOS.     "接收销售订单行项目
  LW_GOODSMVT_ITEM-MOVE_STLOC      =    LS_ITEM-UMLGO.         "接收收货库存地点
  LW_GOODSMVT_ITEM-MOVE_BATCH      =    LS_ITEM-UMCHA.         "接收收货批次
  LW_GOODSMVT_ITEM-MOVE_TYPE =    '321'.                 "移动类型
* 如果生产订单不为空
        SELECT
          SINGLE
           *
          INTO @DATA(LS_MSEG)
          FROM MSEG
          WHERE MBLNR = @LS_ITEM-MBLNR
          AND BWART  = '101'
          AND AUFNR = @LV_AUFNR.
        IF SY-SUBRC = 0 .
         LW_GOODSMVT_ITEM-REF_DOC_YR = LS_MSEG-MJAHR .
         LW_GOODSMVT_ITEM-REF_DOC =  LS_MSEG-MBLNR .
         LW_GOODSMVT_ITEM-REF_DOC_IT =  LS_MSEG-ZEILE .
        ENDIF .
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
 
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

8. 移动类型 541 

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '04'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
      LW_GOODSMVT_ITEM-PLANT      =  LS_ITEM-WERKS.
      LW_GOODSMVT_ITEM-STGE_LOC   =  LS_ITEM-LGORT.
      LW_GOODSMVT_ITEM-MOVE_TYPE  = '541'.
      LW_GOODSMVT_ITEM-ENTRY_QNT  = LS_ITEM-ERFMG .
      LW_GOODSMVT_ITEM-BATCH      = LS_ITEM-CHARG.
      LW_GOODSMVT_ITEM-ITEM_TEXT  = LS_ITEM-EBELN .
      LW_GOODSMVT_ITEM-MOVE_PLANT = LS_ITEM-UMWRK .
      LW_GOODSMVT_ITEM-MOVE_BATCH = LS_ITEM-CHARG .
      LW_GOODSMVT_ITEM-VENDOR     = LS_ITEM-LIFNR.
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

9.移动类型 551

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
 
  LV_GOODSMVT_CODE = '03'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
  LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.   " 从
  LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
  LW_GOODSMVT_ITEM-MOVE_TYPE       = '551'.
  LW_GOODSMVT_ITEM-ENTRY_QNT       = LS_ITEM-QTY .
  LW_GOODSMVT_ITEM-BATCH           = LS_ITEM-CHARG.
  LW_GOODSMVT_ITEM-STCK_TYPE       = LS_ITEM-INSMK .
  LW_GOODSMVT_ITEM-COSTCENTER      = LS_ITEM-KOSTL.
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
 
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

10. 移动类型 105 收货

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '01'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
      LW_GOODSMVT_ITEM-MATERIAL       = LS_MSEG-MATNR.
      LW_GOODSMVT_ITEM-PLANT          =  LS_ITEM-WERKS.
      LW_GOODSMVT_ITEM-STGE_LOC       = LS_ITEM-LGORT.
      LW_GOODSMVT_ITEM-MOVE_TYPE      = '105'.
      LW_GOODSMVT_ITEM-SPEC_STOCK     = LS_MSEG-SOBKZ .
      LW_GOODSMVT_ITEM-VAL_SALES_ORD  = LS_MSEG-MAT_KDAUF .
      LW_GOODSMVT_ITEM-VAL_S_ORD_ITEM = LS_MSEG-MAT_KDPOS .
      LW_GOODSMVT_ITEM-ENTRY_QNT      = LS_ITEM-ERFMG .
      LW_GOODSMVT_ITEM-BATCH          = LS_ITEM-CHARG.
      LW_GOODSMVT_ITEM-REF_DOC_YR     = LS_MSEG-MJAHR .   "参考年度
      LW_GOODSMVT_ITEM-REF_DOC        = LS_MSEG-MBLNR    .    "参考凭证
      LW_GOODSMVT_ITEM-REF_DOC_IT     = LS_MSEG-ZEILE .    "参考行项目信息
      LW_GOODSMVT_ITEM-MVT_IND        = 'B' .
      LW_GOODSMVT_ITEM-PO_NUMBER      = LS_MSEG-EBELN .
      LW_GOODSMVT_ITEM-PO_ITEM        = LS_MSEG-EBELP .
      LW_GOODSMVT_ITEM-GR_RCPT        = LS_MSEG-WEMPF .    "收货方
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

11. 移动类型 261

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
 
  LV_GOODSMVT_CODE = '04'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
  LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
  LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.   " 从
  LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
  LW_GOODSMVT_ITEM-MOVE_TYPE       = '261'.
  LW_GOODSMVT_ITEM-reserv_no       = ls_item-rsnum.  "预留号
  LW_GOODSMVT_ITEM-res_item        = ls_item-rspos.  "预留行号
  LW_GOODSMVT_ITEM-orderid         = ls_item-aufnr.
  LW_GOODSMVT_ITEM-plant           = ls_item-werks. "工厂
  LW_GOODSMVT_ITEM-stge_loc        = ls_item-lgort. "库存地点
  LW_GOODSMVT_ITEM-batch           = ls_item-charg. "批次号
  LW_GOODSMVT_ITEM-entry_qnt       = ls_item-erfmg. "数量
  LW_GOODSMVT_ITEM-sales_ord       = ls_item-kdauf. "销售订单
  LW_GOODSMVT_ITEM-s_ord_item      = ls_item-kdpos. "销售订单行项目
  IF ls_item-kdauf IS NOT INITIAL AND ls_item-kdpos IS NOT INITIAL.
  LW_GOODSMVT_ITEM-spec_stock      = 'E'.
  ENDIF.
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
 
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

 12 .移动类型 311 413 411

  "bapi 参数
  DATA:LW_GOODSMVT_HEADER  TYPE          BAPI2017_GM_HEAD_01,
       LV_GOODSMVT_CODE    TYPE          BAPI2017_GM_CODE,
       LT_GOODSMVT_ITEM    TYPE TABLE OF BAPI2017_GM_ITEM_CREATE,
       LW_GOODSMVT_ITEM    TYPE          BAPI2017_GM_ITEM_CREATE,
       LT_RETURN           TYPE TABLE OF BAPIRET2,
       LW_RETURN           TYPE          BAPIRET2,
       LV_MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
       LV_MATDOCUMENTYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR.
  LV_GOODSMVT_CODE = '04'.
  "准备抬头数据
  LW_GOODSMVT_HEADER-PSTNG_DATE    = SY-DATUM.
  LW_GOODSMVT_HEADER-DOC_DATE      = SY-DATUM.             "凭证中的凭证日期
  LW_GOODSMVT_HEADER-PR_UNAME      = SY-UNAME.              "用户名
 
  "行项目数据
      LW_GOODSMVT_ITEM-MATERIAL        = ''物料号
      LW_GOODSMVT_ITEM-PLANT           =  LS_ITEM-WERKS.
      LW_GOODSMVT_ITEM-STGE_LOC        =  LS_ITEM-LGORT.
      LW_GOODSMVT_ITEM-MOVE_TYPE       =  LS_HEAD-BWART.
      LW_GOODSMVT_ITEM-SPEC_STOCK      =  LS_ITEM-SOBKZ .
      LW_GOODSMVT_ITEM-ENTRY_QNT       =  LS_ITEM-ERFMG .
      LW_GOODSMVT_ITEM-BATCH           =  LS_ITEM-CHARG.
      LW_GOODSMVT_ITEM-MOVE_PLANT      = LS_ITEM-UMWRK .
      LW_GOODSMVT_ITEM-MOVE_STLOC      = LS_ITEM-UMLGO .
      LW_GOODSMVT_ITEM-MOVE_BATCH      = LS_ITEM-UMCHA .
      LW_GOODSMVT_ITEM-RESERV_NO       =  LS_ITEM-RSNUM.
      LW_GOODSMVT_ITEM-RES_ITEM        =   LS_ITEM-RSPOS.
      LW_GOODSMVT_ITEM-ITEM_TEXT       = LS_ITEM-SGTXT .
      IF LW_GOODSMVT_ITEM-MOVE_TYPE    = '311'.
      LW_GOODSMVT_ITEM-VAL_SALES_ORD   = LS_ITEM-MAT_KDAUF .  "发出的销售订单
      LW_GOODSMVT_ITEM-VAL_S_ORD_ITEM  = LS_ITEM-MAT_KDPOS .
      LW_GOODSMVT_ITEM-SALES_ORD       = LS_ITEM-KDAUF .     "收到的销售订单
      LW_GOODSMVT_ITEM-S_ORD_ITEM      = LS_ITEM-KDPOS .
      ENDIF.
      IF LW_GOODSMVT_ITEM-MOVE_TYPE    = '413'.
      LW_GOODSMVT_ITEM-VAL_SALES_ORD   = LS_ITEM-KDAUF .  "收到的销售订单
      LW_GOODSMVT_ITEM-VAL_S_ORD_ITEM  = LS_ITEM-KDPOS .
      ENDIF.
      IF LW_GOODSMVT_ITEM-MOVE_TYPE    = '411'.
      LW_GOODSMVT_ITEM-VAL_SALES_ORD   = LS_ITEM-MAT_KDAUF .  "发出的销售订单
      LW_GOODSMVT_ITEM-VAL_S_ORD_ITEM  = LS_ITEM-MAT_KDPOS .
      ENDIF.
  APPEND LW_GOODSMVT_ITEM TO LT_GOODSMVT_ITEM .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = LW_GOODSMVT_HEADER
      GOODSMVT_CODE    = LV_GOODSMVT_CODE
    IMPORTING
      MATERIALDOCUMENT = LV_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = LV_MATDOCUMENTYEAR
    TABLES
      GOODSMVT_ITEM    = LT_GOODSMVT_ITEM
      RETURN           = LT_RETURN
    .

13.561

  DATA: ls_goodsmvt_header TYPE bapi2017_gm_head_01,
        ls_goodsmvt_code   TYPE bapi2017_gm_code.
  DATA: mat_doc   TYPE bapi2017_gm_head_ret-mat_doc.
  DATA: doc_year  TYPE bapi2017_gm_head_ret-doc_year.
  DATA: lw_flag   TYPE char1.
  DATA: lw_lgort  TYPE mseg-lgort.
  DATA: ls_goodsmvt_item TYPE bapi2017_gm_item_create,
        lt_goodsmvt_item TYPE TABLE OF bapi2017_gm_item_create.
  DATA: ls_return TYPE bapiret2,
        lt_return TYPE TABLE OF bapiret2.

  CLEAR: ls_goodsmvt_header, ls_goodsmvt_code,
  mat_doc, doc_year,ls_goodsmvt_item,lt_goodsmvt_item,
  ls_return,lt_return,lw_flag.

*   表头
  ls_goodsmvt_header-pstng_date = is_mkpf-budat.  "过账日期
  ls_goodsmvt_header-doc_date   = is_mkpf-bldat.  "凭证日期
  ls_goodsmvt_code-gm_code   = '05'.          "03 发货

  LOOP AT it_mseg INTO DATA(is_mseg)."只从内表中取值,不更改表中内容
*  物料转换内码
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = is_mseg-matnr
      IMPORTING
        output       = is_mseg-matnr
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.

*   订单转换内码
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = is_mseg-aufnr
      IMPORTING
        output = is_mseg-aufnr.

*   无价值库对应关系
    CLEAR:lw_lgort.
    CASE is_mseg-lgort.
      WHEN '7010'.
        lw_lgort = 'X010'.
      WHEN '7020'.
        lw_lgort = 'X020'.
      WHEN '7030'.
        lw_lgort = 'X030'.
      WHEN '7040'.
        lw_lgort = 'X040'.
      WHEN '7050'.
        lw_lgort = 'X050'.
      WHEN '7060'.
        lw_lgort = 'X060'.
      WHEN '7070'.
        lw_lgort = 'X070'.
      WHEN '7080'.
        lw_lgort = 'X080'.
      WHEN OTHERS.
    ENDCASE.

    ls_goodsmvt_item-plant     =  '213X'.                   "工厂.
    ls_goodsmvt_item-material  = is_mseg-matnr.        "物料
    ls_goodsmvt_item-entry_qnt = is_mseg-menge.             "数量
    ls_goodsmvt_item-entry_uom = is_mseg-meins.             "单位
    ls_goodsmvt_item-orderid   = is_mseg-aufnr.             "订单号
    ls_goodsmvt_item-stge_loc  = lw_lgort.                  "库存地点
    ls_goodsmvt_item-move_type = '561'.                     "移动类型
    APPEND ls_goodsmvt_item TO lt_goodsmvt_item.
    CLEAR: ls_goodsmvt_item.

    CLEAR:is_mseg.
  ENDLOOP.

  " 执行BAPI
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = ls_goodsmvt_header
      goodsmvt_code    = ls_goodsmvt_code
    IMPORTING
      materialdocument = mat_doc
      matdocumentyear  = doc_year
    TABLES
      goodsmvt_item    = lt_goodsmvt_item
      return           = lt_return.

  LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'."返回数据检查
    lw_flag = 'X'.
    EXIT.
  ENDLOOP.

  IF lw_flag = 'X'.    "错误标识
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    IF mat_doc IS NOT INITIAL AND doc_year IS NOT INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    ENDIF.
  ENDIF.

14.531

DATA: ls_header TYPE bapi2017_gm_head_01,
        ls_code   TYPE bapi2017_gm_code,
        lv_redoc  TYPE bapi2017_gm_head_ret-mat_doc,     "返回的凭证号码
        lt_item   TYPE STANDARD TABLE OF bapi2017_gm_item_create,
        ls_item   TYPE bapi2017_gm_item_create,
        lt_return TYPE STANDARD TABLE OF bapiret2,
        ls_return TYPE bapiret2.

  LOOP AT gt_table INTO gs_table.
    ls_header-pstng_date = gs_table-zrq.
    ls_header-doc_date = sy-datum.

    "分配事务代码--过账码
    ls_code-gm_code = '05'.

    ls_item-material   = gs_table-matnr.             "料号
    ls_item-plant      = gs_table-werks.                 "工厂
    ls_item-stge_loc   = gs_table-lgort.              "库存地点
    ls_item-move_type  = '531'.                    "移动类型(库存管理)
    ls_item-orderid    =   gs_table-aufnr.             "工单
*    ls_item-reserv_no  = gs_table-rsnum.             "预留/相关需求的编号 RESB~RSNUM
*    ls_item-res_item   = gs_table-rspos.              "预留/相关需求的项目编号 RESB~RSPOS
    ls_item-entry_qnt  = gs_table-menge_bc.           "发料数量
    ls_item-entry_uom  = gs_table-meins.             "计量单位

    APPEND ls_item TO lt_item.

    CLEAR gs_table.
  ENDLOOP.

  "BAPI
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = ls_header
      goodsmvt_code    = ls_code
    IMPORTING
      materialdocument = lv_redoc
    TABLES
      goodsmvt_item    = lt_item
      return           = lt_return.

  IF ( line_exists( lt_return[ type = 'A' ] ) ) OR ( line_exists( lt_return[ type = 'E' ] ) ).
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    DATA: lt_msg_tab TYPE esp1_message_tab_type,
          wa_msg_tab LIKE LINE OF lt_msg_tab.

    LOOP AT lt_return INTO ls_return WHERE type = 'A' OR type = 'E'.
      wa_msg_tab-msgid  = ls_return-id.
      wa_msg_tab-msgty  = ls_return-type.
      wa_msg_tab-msgno  = ls_return-number.
      wa_msg_tab-msgv1  = ls_return-message_v1.
      wa_msg_tab-msgv2  = ls_return-message_v2.
      wa_msg_tab-msgv3  = ls_return-message_v3.
      wa_msg_tab-msgv4  = ls_return-message_v4.
      wa_msg_tab-lineno = ls_return-row.
      APPEND wa_msg_tab TO lt_msg_tab.
    ENDLOOP.

    CALL FUNCTION 'C14Z_MESSAGES_SHOW_AS_POPUP'
      TABLES
        i_message_tab = lt_msg_tab.

  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    CLEAR gs_table.
    gs_table-mblnr = lv_redoc.
    MODIFY gt_table FROM gs_table TRANSPORTING mblnr WHERE mblnr IS INITIAL.
    MESSAGE '凭证创建成功' TYPE 'S'.
  ENDIF.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值