BAPI_GOODSMVT_CREATE --mb01,mb1A

本文介绍了一种通过批量文件上传的方式实现SAP系统中货物移动记录的创建过程。主要涵盖了不同类型的货物移动(如采购订单收货、生产订单收货等),并详细展示了如何通过ABAP编程接口进行数据准备、上传及错误处理。该方法适用于自动化处理大量货物移动数据。
* 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 
BAPI_GOODSMVT_CREATE是SAP系统标准函数,可用于采购订单收货创建等操作。要使用BAPI_GOODSMVT_CREATE实现退货PO入库,以下是一般的步骤和示例代码: ### 数据准备 在调用BAPI之前,需要准备好必要的数据,如移动类型、物料、数量、工厂、库存地点等。对于退货PO入库,移动类型通常是特定的退货移动类型。 ### 代码示例 ```abap REPORT z_return_po_goods_receipt. DATA: lt_goodsmvt_item TYPE TABLE OF bapigoodsmvtitem, ls_goodsmvt_item TYPE bapigoodsmvtitem, lt_goodsmvt_header TYPE TABLE OF bapigoodsmvtheader, ls_goodsmvt_header TYPE bapigoodsmvtheader, lt_return TYPE TABLE OF bapiret2, lv_mat_doc TYPE bapi2017_gm_head_ret-matdoc, lv_mat_doc_year TYPE bapi2017_gm_head_ret-matyear. * 填充抬头数据 ls_goodsmvt_header-pstng_date = sy-datum. " 过账日期 ls_goodsmvt_header-doc_date = sy-datum. " 凭证日期 APPEND ls_goodsmvt_header TO lt_goodsmvt_header. * 填充项目数据 ls_goodsmvt_item-move_type = '122'. " 退货移动类型,根据实际情况调整 ls_goodsmvt_item-material = 'MATERIAL_NUMBER'. " 物料编号 ls_goodsmvt_item-plant = 'PLANT_CODE'. " 工厂代码 ls_goodsmvt_item-stge_loc = 'STORAGE_LOCATION'. " 库存地点 ls_goodsmvt_item-entry_qnt = '10'. " 数量 ls_goodsmvt_item-po_number = 'PO_NUMBER'. " 采购订单号 ls_goodsmvt_item-po_item = 'PO_ITEM_NUMBER'. " 采购订单项目号 APPEND ls_goodsmvt_item TO lt_goodsmvt_item. * 调用BAPI CALL FUNCTION 'BAPI_GOODSMVT_CREATE' EXPORTING goodsmvt_header = ls_goodsmvt_header IMPORTING materialdocument = lv_mat_doc matdocumentyear = lv_mat_doc_year TABLES goodsmvt_item = lt_goodsmvt_item return = lt_return. * 检查返回消息 LOOP AT lt_return INTO DATA(ls_return) WHERE type CA 'EAX'. WRITE:/ '错误:', ls_return-message. EXIT. ENDLOOP. IF sy-subrc <> 0. WRITE:/ '物料凭证:', lv_mat_doc, ' 年度:', lv_mat_doc_year. ENDIF. ``` ### 代码解释 1. **数据声明**:声明了必要的内表和结构体,用于存储抬头数据、项目数据和返回消息。 2. **填充抬头数据**:设置过账日期和凭证日期。 3. **填充项目数据**:设置移动类型、物料编号、工厂、库存地点、数量、采购订单号和项目号等。 4. **调用BAPI**:调用BAPI_GOODSMVT_CREATE函数执行货物移动。 5. **检查返回消息**:检查返回消息,如果有错误消息则输出错误信息,否则输出物料凭证号和年度。 ### 注意事项 - 移动类型`122`是示例,实际使用时需要根据业务需求选择正确的移动类型。 - 物料编号、工厂、库存地点、采购订单号和项目号等数据需要根据实际情况进行替换。 - 确保用户有足够的权限执行货物移动操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值