SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

BAPI_GOODSMVT_CREATE to post Goods Movement



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ММ
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Oct 27, 2007 3:14 pm    Post subject: BAPI_GOODSMVT_CREATE to post Goods Movement Reply with quote

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.

Code:
*
* 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
*
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'
* IMPORTING
*   FILELENGTH                    =
  tables
    data_tab                      = pcitab
* EXCEPTIONS
*   FILE_OPEN_ERROR               = 1
*   FILE_READ_ERROR               = 2
*   NO_BATCH                      = 3
*   GUI_REFUSE_FILETRANSFER       = 4
*   INVALID_TYPE                  = 5
*   OTHERS                        = 6
          .
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
*   TESTRUN                     = ' '
* IMPORTING
    goodsmvt_headret            = mthead
*   MATERIALDOCUMENT            =
*   MATDOCUMENTYEAR             =
  tables
    goodsmvt_item               = itab
*   GOODSMVT_SERIALNUMBER       =
    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'
* IMPORTING
*   FILELENGTH                    =
    tables
      data_tab                      = pcitab
* EXCEPTIONS
*   FILE_OPEN_ERROR               = 1
*   FILE_READ_ERROR               = 2
*   NO_BATCH                      = 3
*   GUI_REFUSE_FILETRANSFER       = 4
*   INVALID_TYPE                  = 5
*   OTHERS                        = 6
            .

endform.

*--- End of Program


Example 2
Code:

DATA: h_goodsmvt_header LIKE bapi2017_gm_head_01.
DATA: h_goodsmvt_code LIKE bapi2017_gm_code.
DATA: h_goodsmvt_item LIKE bapi2017_gm_item_create
OCCURS 0 WITH HEADER LINE.
DATA: h_goodsmvt_serialnumber LIKE bapi2017_gm_serialnumber
OCCURS 0 WITH HEADER LINE.

*Header data
h_goodsmvt_header-pstng_date = pstng_date.
h_goodsmvt_header-doc_date = doc_date.
h_goodsmvt_header-header_txt = header TEXT
h_goodsmvt_header-ref_doc_no = ref DOC #

*GM_Code 01: Goods receipt for purchase order'.
H_GOODSMVT_CODE = '01'.

*LOOP on item materials
loop AT p_order_goodsmvt WHERE NOT material IS INITIAL.
  h_goodsmvt_item-orderid = p_orderid.
  h_goodsmvt_item-mvt_ind = p_order_goodsmvt-mvt_ind.
  h_goodsmvt_item-move_type = p_order_goodsmvt-move_type.
  h_goodsmvt_item-plant = p_plant.
  h_goodsmvt_item-stge_loc = p_stge_loc.
  h_goodsmvt_item-customer = p_order_goodsmvt-customer.
  h_goodsmvt_item-po_number = p_order_goodsmvt-po_number.
  h_goodsmvt_item-po_item = p_order_goodsmvt-po_item.

*  IF the THERE is AN amount IN the position text fill it into
 * amount_lc
  if p_order_goodsmvt-item_text = '0.01'.
    h_goodsmvt_item-amount_lc = p_order_goodsmvt-item_text.
  ENDIF.

  h_goodsmvt_item-spec_stock = p_order_goodsmvt-spec_stock.

  CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
    EXPORTING
      input        = p_order_goodsmvt-material
    IMPORTING
      output       = h_goodsmvt_item-material
    EXCEPTIONS
      length_error = 1
      OTHERS       = 2.
  IF sy-subrc 0.
    h_goodsmvt_item-material = p_order_goodsmvt-material.
  ENDIF.
  h_goodsmvt_item-entry_qnt = p_order_goodsmvt-entry_qnt.
  h_goodsmvt_item-entry_uom = p_order_goodsmvt-entry_uom.
  h_goodsmvt_item-entry_uom_iso = p_order_goodsmvt-entry_uom_iso.
  APPEND h_goodsmvt_item.

*  to populate the serial numbers in case the part is serialized
  v_matitem = v_matitem + 1.
*  SHIFT p_order_goodsmvt-serialno LEFT DELETING LEADING '0'.
  IF NOT p_order_goodsmvt-serialno IS INITIAL.
    h_goodsmvt_serialnumber-matdoc_itm = v_matitem.
    h_goodsmvt_serialnumber-serialno = p_order_goodsmvt-serialno.
    APPEND h_goodsmvt_serialnumber.
  ENDIF.

ENDLOOP.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
  EXPORTING
    goodsmvt_header       = h_goodsmvt_header
    goodsmvt_code         = h_goodsmvt_code
  TABLES
    goodsmvt_item         = h_goodsmvt_item
    goodsmvt_serialnumber = h_goodsmvt_serialnumber
    return                = return_material.

IF return_material[] IS INITIAL.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = h_x.
ENDIF.


Example 3
Поступление материала к заказу на поставку (Движение 101).

Code:
* Data for BAPI_GOODSMVT_CREATE
DATA: l_gmvt_header TYPE bapi2017_gm_head_01,
      l_code TYPE bapi2017_gm_code,
      l_gmvt_items LIKE bapi2017_gm_item_create,
      lt_gmvt_items LIKE bapi2017_gm_item_create
        OCCURS 0 WITH HEADER LINE.

DATA: lt_return TYPE bapiret2 OCCURS 0 WITH HEADER LINE,
      error_occured.

l_code = '01'.
l_gmvt_header-pstng_date = '20061205'.
l_gmvt_header-doc_date = '20061123'.
l_gmvt_header-ref_doc_no = '3104699801'.

CLEAR l_gmvt_items.
l_gmvt_items-material = 'FA00013'.
l_gmvt_items-plant = 'RU04'.
l_gmvt_items-stge_loc = '0001'.
l_gmvt_items-move_type = '101'.
l_gmvt_items-entry_qnt = '1.000'.
l_gmvt_items-entry_uom = 'ST'.
l_gmvt_items-po_number = '3104699801'.
l_gmvt_items-po_item = '00010'.
l_gmvt_items-mvt_ind = 'B'.
APPEND l_gmvt_items TO lt_gmvt_items.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
     EXPORTING
          goodsmvt_header = l_gmvt_header
          goodsmvt_code   = l_code
     TABLES
          goodsmvt_item   = lt_gmvt_items
          return          = lt_return.

IF lt_return[] IS INITIAL.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
            wait = 'X'.
ENDIF.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ММ All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.