SAP-ABAP-BAPI_GOODSMVT_CREATE创建物料凭证bapi的各种情况如何赋值

前言,对于BAPI_GOODSMVT_CREATE来说,赋值难的地方基本只有两个地方不明确,第一个地方就是import中的GOODSMVT_CODE字段,针对不同的事务代码输入不同的code,具体如下。

01 - MB01 - Goods Receipts for Purchase Order

02 - MB31 - Goods Receipts for Prod Order

03 - MB1A - Goods Issue

04 - MB1B - Transfer Posting

05 - MB1C - Enter Other Goods Receipt

06 - MB11

07 - MB04

要是实在这7个都用不了,或者自己懒,不想换,那其实也可以在表T158G中加个08-MIGO,然后这个GOODSMVT_CODE字段全部赋值08就行了。

SAP-ABAP-BAPI_GOODSMVT_CREATE创建物料凭证bapi的各种情况如何赋值_第1张图片

第二个赋值难的地方是表goodsmvt_item-MVT_IND,这个具体应该如何赋值如下

SAP-ABAP-BAPI_GOODSMVT_CREATE创建物料凭证bapi的各种情况如何赋值_第2张图片

下面举例各种业务场景如何调用这个BAPI

定义就参考MSEG表就行了

1.采购订单101入库到非限制库存


  DATA LV_PEINH TYPE EKPO-PEINH .
  CLEAR:GS_GOODS_MVT_T.
  READ TABLE GT_GOODS_MVT INDEX 1 INTO GS_GOODS_MVT_T .
** BAPI HEADER
  CLEAR gw_gmhead.
  gw_gmhead-pstng_date = GS_GOODS_MVT_T-VERAB.
  gw_gmhead-doc_date   = sy-datum.
  gw_gmhead-REF_DOC_NO = sy-datum.
  gs_code-gm_code      =  '01' . "01 - MB01 - Goods Receipts for Purchase Order

** BAPI ITEM
  CLEAR: gt_gmitem[],gt_gmitem.
  LOOP AT GT_GOODS_MVT INTO  GS_GOODS_MVT.
    CLEAR gt_gmitem.
    gt_gmitem-material          = GS_GOODS_MVT-matnr.
    gt_gmitem-material_external = GS_GOODS_MVT-matnr.
    gt_gmitem-stge_loc          = GS_GOODS_MVT-lgort.
    gt_gmitem-batch             = GS_GOODS_MVT-charg.
    gt_gmitem-plant             = GS_GOODS_MVT-werks.
    gt_gmitem-move_mat          = GS_GOODS_MVT-matnr.
    gt_gmitem-move_mat_external = GS_GOODS_MVT-matnr.
    gt_gmitem-move_plant        = GS_GOODS_MVT-werks.
    gt_gmitem-move_stloc        = GS_GOODS_MVT-lgort.
    gt_gmitem-move_batch        = GS_GOODS_MVT-charg.
    gt_gmitem-PO_NUMBER         = GS_GOODS_MVT-EBELN.
    gt_gmitem-PO_ITEM           = GS_GOODS_MVT-EBELP.
    gt_gmitem-MVT_IND           = 'B'. "B - Goods movement for purchase order
    gt_gmitem-move_type         = '101'."移动类型101
    SELECT SINGLE EKPO~PEINH FROM EKPO INTO LV_PEINH WHERE EKPO~EBELN = GS_GOODS_MVT-EBELN AND EKPO~EBELP = GS_GOODS_MVT-EBELP .
      IF LV_PEINH <> 0.
         GS_GOODS_MVT-MENGE     = GS_GOODS_MVT-MENGE / LV_PEINH .
      ENDIF.
    gt_gmitem-entry_qnt         = GS_GOODS_MVT-MENGE .
    Gt_gmitem-entry_uom         = GS_GOODS_MVT-meins.
*    gt_gmitem-reserv_no        = GS_GOODS_MVT-rsnum.  "预留
*    gt_gmitem-res_item         = GS_GOODS_MVT-rspos.   "预留行项目
    gt_gmitem-ref_doc           = GS_GOODS_MVT-mblnr.
    gt_gmitem-ref_doc_yr        = GS_GOODS_MVT-mjahr.
    gt_gmitem-ref_doc_it        = GS_GOODS_MVT-zeile.
*    gt_gmitem-costcenter       = GS_GOODS_MVT-costcenter. "Cost Center
    gt_gmitem-prod_date         = g_post_date.  "Date of Manufacture 生产日期
    APPEND gt_gmitem.
  ENDLOOP.

如果想入到质检库存需要添加赋值

STCK_TYPE = 'X' .

2.344非限制库存转为冻结库存


  DATA LV_PEINH TYPE EKPO-PEINH .
  CLEAR:GS_GOODS_MVT_T.
  READ TABLE GT_GOODS_MVT INDEX 1 INTO GS_GOODS_MVT_T .
** BAPI HEADER
  CLEAR gw_gmhead.
  gw_gmhead-pstng_date = GS_GOODS_MVT_T-VERAB.
  gw_gmhead-doc_date   = sy-datum.
  gw_gmhead-REF_DOC_NO = sy-datum.
  gs_code-gm_code      =  '04' . "04 - MB1B - Transfer Posting

** BAPI ITEM
  CLEAR: gt_gmitem[],gt_gmitem.
  LOOP AT GT_GOODS_MVT INTO  GS_GOODS_MVT.
    CLEAR gt_gmitem.
    gt_gmitem-material          = GS_GOODS_MVT-matnr.
    gt_gmitem-material_external = GS_GOODS_MVT-matnr.
    gt_gmitem-stge_loc          = GS_GOODS_MVT-lgort.
    gt_gmitem-batch             = GS_GOODS_MVT-charg.
    gt_gmitem-plant             = GS_GOODS_MVT-werks.
    gt_gmitem-move_mat          = GS_GOODS_MVT-matnr.
    gt_gmitem-move_mat_external = GS_GOODS_MVT-matnr.
    gt_gmitem-move_plant        = GS_GOODS_MVT-werks.
    gt_gmitem-move_stloc        = GS_GOODS_MVT-lgort.
    gt_gmitem-move_batch        = GS_GOODS_MVT-charg.
    gt_gmitem-PO_NUMBER         = GS_GOODS_MVT-EBELN.
    gt_gmitem-PO_ITEM           = GS_GOODS_MVT-EBELP.
    gt_gmitem-MVT_IND           = ''. "B - Goods movement for purchase order
    gt_gmitem-move_type         = '344'."移动类型101
    SELECT SINGLE EKPO~PEINH FROM EKPO INTO LV_PEINH WHERE EKPO~EBELN = GS_GOODS_MVT-EBELN AND EKPO~EBELP = GS_GOODS_MVT-EBELP .
      IF LV_PEINH <> 0.
         GS_GOODS_MVT-MENGE     = GS_GOODS_MVT-MENGE / LV_PEINH .
      ENDIF.
    gt_gmitem-entry_qnt         = GS_GOODS_MVT-MENGE .
    Gt_gmitem-entry_uom         = GS_GOODS_MVT-meins.
*    gt_gmitem-reserv_no        = GS_GOODS_MVT-rsnum.  "预留
*    gt_gmitem-res_item         = GS_GOODS_MVT-rspos.   "预留行项目
    gt_gmitem-ref_doc           = GS_GOODS_MVT-mblnr.
    gt_gmitem-ref_doc_yr        = GS_GOODS_MVT-mjahr.
    gt_gmitem-ref_doc_it        = GS_GOODS_MVT-zeile.
*    gt_gmitem-costcenter       = GS_GOODS_MVT-costcenter. "Cost Center
    gt_gmitem-prod_date         = g_post_date.  "Date of Manufacture 生产日期
    APPEND gt_gmitem.
  ENDLOOP.

3.343冻结库存转为非限制库存


  DATA LV_PEINH TYPE EKPO-PEINH .
  CLEAR:GS_GOODS_MVT_T.
  READ TABLE GT_GOODS_MVT INDEX 1 INTO GS_GOODS_MVT_T .
** BAPI HEADER
  CLEAR gw_gmhead.
  gw_gmhead-pstng_date = GS_GOODS_MVT_T-VERAB.
  gw_gmhead-doc_date   = sy-datum.
  gw_gmhead-REF_DOC_NO = sy-datum.
  gs_code-gm_code      = '04' . "04 - MB1B - Transfer Posting

** BAPI ITEM
  CLEAR: gt_gmitem[],gt_gmitem.
  LOOP AT GT_GOODS_MVT INTO  GS_GOODS_MVT.
    CLEAR gt_gmitem.
    gt_gmitem-material          = GS_GOODS_MVT-matnr.
    gt_gmitem-material_external = GS_GOODS_MVT-matnr.
    gt_gmitem-stge_loc          = GS_GOODS_MVT-lgort.
    gt_gmitem-batch             = GS_GOODS_MVT-charg.
    gt_gmitem-plant             = GS_GOODS_MVT-werks.
    gt_gmitem-move_mat          = GS_GOODS_MVT-matnr.
    gt_gmitem-move_mat_external = GS_GOODS_MVT-matnr.
    gt_gmitem-move_plant        = GS_GOODS_MVT-werks.
    gt_gmitem-move_stloc        = GS_GOODS_MVT-lgort.
    gt_gmitem-move_batch        = GS_GOODS_MVT-charg.
    gt_gmitem-PO_NUMBER         = GS_GOODS_MVT-EBELN.
    gt_gmitem-PO_ITEM           = GS_GOODS_MVT-EBELP.
    gt_gmitem-MVT_IND           = ''. "B - Goods movement for purchase order
    gt_gmitem-move_type         = '343'."移动类型101
    SELECT SINGLE EKPO~PEINH FROM EKPO INTO LV_PEINH WHERE EKPO~EBELN = GS_GOODS_MVT-EBELN AND EKPO~EBELP = GS_GOODS_MVT-EBELP .
      IF LV_PEINH <> 0.
         GS_GOODS_MVT-MENGE     = GS_GOODS_MVT-MENGE / LV_PEINH .
      ENDIF.
    gt_gmitem-entry_qnt         = GS_GOODS_MVT-MENGE .
    Gt_gmitem-entry_uom         = GS_GOODS_MVT-meins.
*    gt_gmitem-reserv_no        = GS_GOODS_MVT-rsnum.  "预留
*    gt_gmitem-res_item         = GS_GOODS_MVT-rspos.   "预留行项目
    gt_gmitem-ref_doc           = GS_GOODS_MVT-mblnr.
    gt_gmitem-ref_doc_yr        = GS_GOODS_MVT-mjahr.
    gt_gmitem-ref_doc_it        = GS_GOODS_MVT-zeile.
*    gt_gmitem-costcenter       = GS_GOODS_MVT-costcenter. "Cost Center
    gt_gmitem-prod_date         = g_post_date.  "Date of Manufacture 生产日期
    APPEND gt_gmitem.
  ENDLOOP.

4.261工单投料


  DATA LV_PEINH TYPE EKPO-PEINH .
  CLEAR:GS_GOODS_MVT_T.
  READ TABLE GT_GOODS_MVT INDEX 1 INTO GS_GOODS_MVT_T .
** BAPI HEADER
  CLEAR gw_gmhead.
  gw_gmhead-pstng_date = GS_GOODS_MVT_T-VERAB.
  gw_gmhead-doc_date   = sy-datum.
  gw_gmhead-REF_DOC_NO = sy-datum.
  gs_code-gm_code      =  '03' . ""03 - MB1A - Goods Issue.

** BAPI ITEM
  CLEAR: gt_gmitem[].
  LOOP AT GT_GOODS_MVT INTO  GS_GOODS_MVT.
    CLEAR gt_gmitem.
    gt_gmitem-material          = GS_GOODS_MVT-matnr.
    gt_gmitem-material_external = GS_GOODS_MVT-matnr.
    gt_gmitem-stge_loc          = GS_GOODS_MVT-lgort.
    gt_gmitem-batch             = GS_GOODS_MVT-charg.
    gt_gmitem-plant             = GS_GOODS_MVT-werks.
    gt_gmitem-move_mat          = GS_GOODS_MVT-matnr.
    gt_gmitem-move_mat_external = GS_GOODS_MVT-matnr.
    gt_gmitem-move_plant        = GS_GOODS_MVT-werks.
    gt_gmitem-move_stloc        = GS_GOODS_MVT-lgort.
    gt_gmitem-move_batch        = GS_GOODS_MVT-charg.
    gt_gmitem-ORDERID           = GS_GOODS_MVT-AUFNR.
*    gt_gmitem-PO_NUMBER        = GS_GOODS_MVT-EBELN. "采购订单
*    gt_gmitem-PO_ITEM          = GS_GOODS_MVT-EBELP.
    gt_gmitem-MVT_IND           = ''. "
    gt_gmitem-move_type         = '261'."移动类型261
    gt_gmitem-entry_qnt         = GS_GOODS_MVT-MENGE .
    Gt_gmitem-entry_uom         = GS_GOODS_MVT-meins.
*    gt_gmitem-reserv_no        = GS_GOODS_MVT-rsnum.  "预留
*    gt_gmitem-res_item         = GS_GOODS_MVT-rspos.   "预留行项目
    gt_gmitem-ref_doc           = GS_GOODS_MVT-mblnr.
    gt_gmitem-ref_doc_yr        = GS_GOODS_MVT-mjahr.
    gt_gmitem-ref_doc_it        = GS_GOODS_MVT-zeile.
*    gt_gmitem-costcenter       = GS_GOODS_MVT-costcenter. "Cost Center
    gt_gmitem-prod_date         = g_post_date.  "Date of Manufacture 生产日期
    APPEND gt_gmitem.
  ENDLOOP.

5.551报废

6.201成本中心发料

bapi调用及返回消息处理,注意不要用sy-subrc做判断,要判断返回消息是不是空


  DATA: lt_retn LIKE bapiret2 OCCURS 0 WITH HEADER LINE.

  CLEAR: lt_retn[], lt_retn.
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = gw_gmhead
      goodsmvt_code    = gs_code
    IMPORTING
      materialdocument = g_mblnr
      matdocumentyear  = g_mjahr
    TABLES
      goodsmvt_item    = gt_gmitem
      return           = lt_retn.
  IF lt_retn IS NOT  INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    LOOP AT lt_retn.
      GS_RETURN-MESSA = GS_RETURN-MESSA && lt_retn-MESSAGE && '.' .
    ENDLOOP.
    GS_RETURN-MTYPE = 'E' .
  ELSE .
      CLEAR: lt_retn[], lt_retn.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait   = 'X'
        IMPORTING
          return = lt_retn.
      LOOP AT lt_retn WHERE type = 'E' OR type = 'A'.
        GS_RETURN-MESSA = GS_RETURN-MESSA && lt_retn-MESSAGE && '.' .
        GS_RETURN-MTYPE = 'E' .
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDLOOP.
      IF sy-subrc NE 0.
        GS_RETURN-MESSA = 'SUCCESS' .
        GS_RETURN-MTYPE = 'S' .
        DO 5 TIMES.
          SELECT * FROM MSEG INTO TABLE GT_MSEG WHERE  MBLNR = g_mblnr AND MJAHR = g_mjahr .
          IF GT_MSEG[] IS NOT INITIAL.
            EXIT .
          ELSE .
            WAIT UP TO 1 SECONDS .
          ENDIF.
        ENDDO.
        LOOP AT GT_GOODS_MVT ASSIGNING .
          READ TABLE GT_MSEG INTO GS_MSEG INDEX SY-TABIX .
          -MTYPE = GS_RETURN-MTYPE .
          -MESSAGE = GS_RETURN-MESSA .
          -MBLNR = GS_MSEG-MBLNR .
          -MJAHR = GS_MSEG-MJAHR .
          -ZEILE = GS_MSEG-ZEILE .
          -CHARG = GS_MSEG-CHARG .
        ENDLOOP.


      ENDIF.
  ENDIF.

你可能感兴趣的:(ABAP随笔,sap,abap,GOODSMVT_CREATE,移动类型,生成物料凭证)