原文:Small Fire · BAPI_GOODSMVT_CREATE 解析及示例 · SAP 业务 子专栏 · Small Fire 技术笔记

Inventory Management 使用的 BAPI 解析

使用到的 BAPI

There are two BAPIs for posting Goods Movements:

  • BAPI_GOODSMVT_CREATE (universal BAPI for posting Goods Movements)
  • BAPI_GOODSMVT_CANCEL (only for reverting Goods Movements)

And two in combination with for the transactional handling:

  • BAPI_TRANSACTION_COMMIT (Commit the posting, general)
  • BAPI_TRANSACTION_ROLLBACK (Rollback the posting, general)

参数

关于要创建的物料凭证的以下信息传递到 BAPI

  • A structure with the header data
  • A structure with the code for the movement
  • A table with the item data
  • A table with the serial numbers
  • The posting is made by the function module MB_CREATE_GOODS_MOVEMENT
  • Messages are returned in the Return parameter
  • BAPI 还提供了测试运行功能,传入 TESTRUN = 'X'

参数规则

  • Material number 18-character with leading zeros
  • Batches with uppercase letters
  • 必输:PSTNG_DATE field & DOC_DATE field (import structure GOODSMVT_HEADER)

Goods Movement Code

GM_CODETCODEDescription
01MB01Goods receipt for purchase order
02MB31Goods receipt for production order
03MB1AGoods issue
04MB1BTransfer posting
05MB1COther goods receipt
06MB11Reversal of goods movements
07MB04Subsequent adjustment with regard to a subcontract order

根据 Goods Movement Code 输入 movement indicator (ITEM-MVT_IND)

  • GM_Code 01 : B
  • GM_Code 02 : F
  • Others GM_Code : blank.

Example Code for calling the GM BAPI to post a 561 Goods Movement

*&---------------------------------------------------------------------*
*& Report  Z_BAPI_GDSMVT
*&---------------------------------------------------------------------*
REPORT  z_bapi_gdsmvt.
DATA: ls_mmdochdr LIKE bapi2017_gm_head_01,
      lt_gm       TYPE STANDARD TABLE OF bapi2017_gm_item_create,
      ls_gm       LIKE bapi2017_gm_item_create,
      lt_ret      TYPE STANDARD TABLE OF bapiret2,
      ls_ret      LIKE bapiret2,
      ls_hdr      LIKE bapi2017_gm_head_ret,
      ls_ser      LIKE bapi2017_gm_serialnumber,
      lt_ser      LIKE  STANDARD TABLE OF bapi2017_gm_serialnumber.
PARAMETERS: p_pstdat LIKE bapi2017_gm_head_01-pstng_date,
            p_docdat LIKE bapi2017_gm_head_01-doc_date,
            p_matnr  LIKE bapi2017_gm_item_create-material,
            p_plant  LIKE bapi2017_gm_item_create-plant,
            p_sloc   LIKE bapi2017_gm_item_create-stge_loc,
            p_quant  LIKE bapi2017_gm_item_create-entry_qnt,
            p_batch  LIKE bapi2017_gm_item_create-batch.
START-OF-SELECTION.
* Prepare Data for Goods Movement
  ls_mmdochdr-pstng_date = p_pstdat.
  ls_mmdochdr-doc_date = p_docdat.

  ls_gm-move_type = '561'.
  ls_gm-material = p_matnr.
  ls_gm-plant = p_plant.
  ls_gm-stge_loc = p_sloc.
  ls_gm-entry_qnt = p_quant.
  ls_gm-batch = p_batch.
  APPEND ls_gm TO lt_gm.
  CLEAR ls_gm.
* Call BAPI
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = ls_mmdochdr
      goodsmvt_code    = '05'
    IMPORTING
      goodsmvt_headret = ls_hdr
    TABLES
      goodsmvt_item    = lt_gm
      return           = lt_ret.
* If no error, commit
  IF lt_ret IS INITIAL.
    WRITE: 'Material Document posted:', ls_hdr-mat_doc, ' ', ls_hdr-doc_year.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
*   Alternative COMMIT WORK.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
*   Alternative ROLLBACK WORK.
    WRITE: 'Error during posting Material document:', /.
    LOOP AT lt_ret INTO ls_ret.
      WRITE: ls_ret-type,ls_ret-id,ls_ret-number,ls_ret-message,ls_ret-log_no,
             ls_ret-log_msg_no,ls_ret-message_v1,ls_ret-message_v2,ls_ret-message_v3,
             ls_ret-message_v4.
    ENDLOOP.
  ENDIF.

循环调用 BAPI 示例

*&---------------------------------------------------------------------*
*& Report  ZBAPI_GDSMVT
*&---------------------------------------------------------------------*
REPORT  zbapi_gdsmvt.
* DATA: ...
* PARAMETERS: ...
START-OF-SELECTION.
* Prepare data for first Goods Movement
* Call BAPI to create Goods Movement
CALL FUNCTION 'BAPI_GOODSMVT_CREATE' DESTINATION 'NONE'
* If no error, commit
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' DESTINATION 'NONE'
    EXPORTING
      wait = 'X'.
* ELSE
* Error handling
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' DESTINATION 'NONE'.
* Close RFC connection
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = 'NONE'.
* Prepare data for next Goods Movement
* Call BAPI to create Goods Movement
CALL FUNCTION 'BAPI_GOODSMVT_CREATE' DESTINATION 'NONE'
* If no error, commit
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' DESTINATION 'NONE'
    EXPORTING
      wait = 'X'.
* ELSE
* Error handling
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' DESTINATION 'NONE'.
* Close RFC connection
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = 'NONE'.

特殊库存类型需要参数

Special stock (e.g. sales order, project, vendor etc.)

Sales Order Stock

  • SPEC_STOCK (Special stock indicator for Sales order stock)
  • SALES_ORD & S_ORD_ITEM
  • VAL_SALES_ORD & VAL_S_ORD_ITEM

Project Stock

  • SPEC_STOCK (Special stock indicator for Project stock)
  • WBS_ELEM & VAL_WBS_ELEM

BAPI_GOODSMVT_CANCEL 冲销物料凭证

参考