Skip to end of metadata
Go to start of metadata

Author: Swarna Ramya
Submitted: Oct 15,2007.

Description:

Example for BAPI_GOODSMVT_CREATE(To Post Goods Movement).

The following is an abap program making used of the BAPI function BAPI_GOODSMVT_CREATE to do Goods Receipts for Purchase Order after importing the data from an external system.

*BAPI TO Upload Inventory Data
*

*GMCODE Table T158G - 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
*

Domain: KZBEW - Movement Indicator
*      Goods movement w/o reference
*  B - Goods movement for purchase order
*  F - Goods movement for production order
*  L - Goods movement for delivery note
*  K - Goods movement for kanban requirement (WM - internal only)
*  O - Subsequent adjustment of "material-provided" consumption
*  W - Subsequent adjustment of proportion/product unit material

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zbapi_goodsmovement.

PARAMETERS: p-file LIKE rlgrap-filename DEFAULT
                                 'c:\sapdata\TEST.txt'.
PARAMETERS: e-file LIKE rlgrap-filename DEFAULT
                                 'c:\sapdata\gdsmvterror.txt'.

PARAMETERS: xpost LIKE sy-datum DEFAULT sy-datum.

DATA: BEGIN OF gmhead.
        INCLUDE STRUCTURE bapi2017_gm_head_01.
DATA: END OF gmhead.

DATA: BEGIN OF gmcode.
        INCLUDE STRUCTURE bapi2017_gm_code.
DATA: END OF gmcode.

DATA: BEGIN OF mthead.
        INCLUDE STRUCTURE bapi2017_gm_head_ret.
DATA: END OF mthead.

DATA: BEGIN OF itab OCCURS 100.
        INCLUDE STRUCTURE bapi2017_gm_item_create.
DATA: END OF itab.

DATA: BEGIN OF errmsg OCCURS 10.
        INCLUDE STRUCTURE bapiret2.
DATA: END OF errmsg.

DATA: wmenge LIKE iseg-menge,
      errflag.

DATA: BEGIN OF pcitab OCCURS 100,
        ext_doc(10),           "External Document Number
        mvt_type(3),           "Movement Type
        doc_date(8),           "Document Date
        post_date(8),          "Posting Date
        plant(4),              "Plant
        material(18),          "Material Number
        qty(13),               "Quantity
        recv_loc(4),           "Receiving Location
        issue_loc(4),          "Issuing Location
        pur_doc(10),           "Purchase Document No
        po_item(3),            "Purchase Document Item No
        del_no(10),            "Delivery Purchase Order Number
        del_item(3),           "Delivery Item
        prod_doc(10),          "Production Document No
        scrap_reason(10),      "Scrap Reason
        upd_sta(1),            "Update Status
      END OF pcitab.

CALL FUNCTION 'WS_UPLOAD'
  EXPORTING
    filename = p-file
    filetype = 'DAT'
  TABLES
    data_tab = pcitab.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  EXIT.
ENDIF.

gmhead-pstng_date = sy-datum.
gmhead-doc_date = sy-datum.
gmhead-pr_uname = sy-uname.
gmcode-gm_code = '01'.   "01 - MB01 - Goods Receipts for Purchase Order

LOOP AT pcitab.
  itab-move_type  = pcitab-mvt_type.
  itab-mvt_ind    = 'B'.
  itab-plant      = pcitab-plant.
  itab-material   = pcitab-material.
  itab-entry_qnt  = pcitab-qty.
  itab-move_stloc = pcitab-recv_loc.
  itab-stge_loc   = pcitab-issue_loc.
  itab-po_number  = pcitab-pur_doc.
  itab-po_item    = pcitab-po_item.
  CONCATENATE pcitab-del_no pcitab-del_item INTO itab-item_text.
  itab-move_reas  = pcitab-scrap_reason.

  APPEND itab.
ENDLOOP.

LOOP AT itab.
  WRITE:/ itab-material, itab-plant, itab-stge_loc,
          itab-move_type, itab-entry_qnt, itab-entry_uom,
          itab-entry_uom_iso, itab-po_number, itab-po_item,
                                              pcitab-ext_doc.
ENDLOOP.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
  EXPORTING
    goodsmvt_header  = gmhead
    goodsmvt_code    = gmcode
  IMPORTING
    goodsmvt_headret = mthead
  TABLES
    goodsmvt_item    = itab
    return           = errmsg.
CLEAR errflag.
LOOP AT errmsg.
  IF errmsg-type EQ 'E'.
    WRITE:/'Error in function', errmsg-message.
    errflag = 'X'.
  ELSE.
    WRITE:/ errmsg-message.
  ENDIF.
ENDLOOP.

IF errflag IS INITIAL.
  COMMIT WORK AND WAIT.
  IF sy-subrc NE 0.
    WRITE:/ 'Error in updating'.
    EXIT.
  ELSE.
    WRITE:/ mthead-mat_doc, mthead-doc_year.
    PERFORM upd_sta.
  ENDIF.
ENDIF.

*---------------------------------------------------------------------
*       FORM UPD_STA                                                  *
*---------------------------------------------------------------------
FORM upd_sta.
  LOOP AT pcitab.
    pcitab-upd_sta = 'X'.
    MODIFY pcitab.
  ENDLOOP.

  CALL FUNCTION 'WS_DOWNLOAD'
    EXPORTING
      filename = p-file
      filetype = 'DAT'
    TABLES
      data_tab = pcitab.

ENDFORM.                    "upd_sta