物料主数据批导

举报
雨绸缪 发表于 2023/10/30 16:05:57 2023/10/30
【摘要】 前言为了实现物料的客制化批导,首先要找到相关的 BAPI 或者录制 BDC,然后定义好批导文件,一般是 EXCEL,这样才方便用户整理需要导入的数据。核心 BAPI – BAPI_MATERIAL_SAVEDATA,调用语法如下:**调用BAPI创建物料/修改主数据 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' EXPORTI...

前言

为了实现物料的客制化批导,首先要找到相关的 BAPI 或者录制 BDC,然后定义好批导文件,一般是 EXCEL,这样才方便用户整理需要导入的数据。

核心 BAPI – BAPI_MATERIAL_SAVEDATA,调用语法如下:

**调用BAPI创建物料/修改主数据
        CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
          EXPORTING
            headdata           = header
            clientdata         = w_mara
            clientdatax        = w_marax
            plantdata          = w_marc
            plantdatax         = w_marcx
            salesdata          = w_mvke
            salesdatax         = w_mvkex
            valuationdata  = w_mbew
            valuationdatax = w_mbewx
            STORAGELOCATIONDATA  = W_MARD
            STORAGELOCATIONDATAX = W_MARDX
          TABLES
            materialdescription = gt_makt  
            unitsofmeasure      = gt_marm  
            unitsofmeasurex     = gt_marmx  
            materiallongtext    = gt_mltx  
            internationalartnos = gt_mean
            taxclassifications = t_mlan
            returnmessages     = return.
  • 头信息 BAPI 结构:bapimathead
  • 基本视图 BAPI 结构:bapi_marabapi_marax
  • 物料描述 BAPI 结构:bapi_makt
  • 工厂视图 BAPPI 结构:bapi_marc
  • 税分类 BAPI 结构:bapi_mlan
  • UPC-CODE/EAN11:bapi_mean
  • 单位结构:bapi_marmbapi_marmx
  • 扩展结构:BAPIPAREX
  • 返回信息结构:BAPIRET2

image.png

填充头文件

  BAPIMATHEAD-MATERIAL = IT_MATMASTER-MATNR.
  BAPIMATHEAD-IND_SECTOR = IT_MATMASTER-Mbrsh.
  BAPIMATHEAD-MATL_TYPE = IT_MATMASTER-Mtart.
  BAPIMATHEAD-BASIC_VIEW = 'X'.
  BAPIMATHEAD-PURCHASE_VIEW = 'X'.
  BAPIMATHEAD-ACCOUNT_VIEW = 'X'.

测试文件

REPORT Z8_BAPI.

**********************************************************************
* STRUCTURE DECLARATIONS *
**********************************************************************

TABLES: BAPIMATHEAD, "Headerdata
        BAPI_MARA, "Clientdata
        BAPI_MARAX, "Clientdatax
        BAPI_MARC, "Plantdata
        BAPI_MARCX, "Plantdatax
        BAPI_MAKT, "Material description
        BAPIRET2. "Return messages

DATA: V_FILE TYPE STRING.

DATA:
BEGIN OF LSMW_MATERIAL_MASTER,
  MATNR(018) TYPE C, "Material number
  MTART(004) TYPE C, "Material type
  MBRSH(001) TYPE C, "Industry sector
  WERKS(004) TYPE C, "Plant
  MAKTX(040) TYPE C, "Material description
  DISMM(002) TYPE C, "Extra Field Added In the Program as itsrequired
  MEINS(003) TYPE C, "Base unit of measure
  MATKL(009) TYPE C, "Material group
  SPART(002) TYPE C, "Division
  LABOR(003) TYPE C, "Lab/office
  PRDHA(018) TYPE C, "Product hierarchy
  MSTAE(002) TYPE C, "X-plant matl status
  MTPOS_MARA(004) TYPE C, "Gen item cat group
  BRGEW(017) TYPE C, "Gross weight
  GEWEI(003) TYPE C, "Weight unit
  NTGEW(017) TYPE C, "Net weight
  GROES(032) TYPE C, "Size/Dimensions
  MAGRV(004) TYPE C, "Matl grp pack matls
  BISMT(018) TYPE C, "Old material number
  WRKST(048) TYPE C, "Basic material
  PROFL(003) TYPE C, "DG indicator profile
  KZUMW(001) TYPE C, "Environmentally rlvt
  BSTME(003) TYPE C, "Order unit
  VABME(001) TYPE C,
  EKGRP(003) TYPE C, "Purchasing group
  XCHPF(001) TYPE C, "Batch management
  EKWSL(004) TYPE C, "Purchasing key value
  WEBAZ(003) TYPE C, "GR processing time
  MFRPN(040) TYPE C, "Manufacturer part number
  MFRNR(010) TYPE C, "Manufacturer number
  VPRSV(001) TYPE C, "Price control indicator
  STPRS(015) TYPE C, "Standard price
  BWPRH(014) TYPE C, "Commercial price1
  BKLAS(004) TYPE C, "Valuation class
END OF LSMW_MATERIAL_MASTER.

**********************************************************************
* INTERNAL TABLE DECLARATIONS *
**********************************************************************
*to store the input data

DATA: BEGIN OF it_matmaster OCCURS 0.
  INCLUDE STRUCTURE LSMW_MATERIAL_MASTER.
DATA:END OF it_matmaster.

*for material description
DATA:BEGIN OF IT_MATERIALDESC OCCURS 0.
  INCLUDE STRUCTURE BAPI_MAKT .
DATA:END OF IT_MATERIALDESC.

*to return messages
DATA:BEGIN OF IT_RETURN OCCURS 0.
  INCLUDE STRUCTURE BAPIRET2.
DATA:END OF IT_RETURN.

************************************************************************
*SELECTION SCREEN
************************************************************************

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002.
  PARAMETERS:P_FILE LIKE RLGRAP-FILENAME OBLIGATORY.
SELECTION-SCREEN END OF BLOCK B1 .

************************************************************************
AT SELECTION SCREEN
************************************************************************

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      PROGRAM_NAME = SYST-CPROG
      DYNPRO_NUMBER = SYST-DYNNR
      FIELD_NAME = 'P_FILE'
    IMPORTING
      FILE_NAME = P_FILE.

**********************************************************************
* TO UPLOAD THE DATA *
**********************************************************************

START-OF-SELECTION.

  V_FILE = P_FILE.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = V_FILE
      FILETYPE = 'ASC'
      HAS_FIELD_SEPARATOR = 'X'
      HEADER_LENGTH = 0
      READ_BY_LINE = 'X'
      DAT_MODE = ' '
    IMPORTING
      FILELENGTH =
      HEADER =
    TABLES
      data_tab = IT_MATMASTER
EXCEPTIONS
    FILE_OPEN_ERROR = 1
    FILE_READ_ERROR = 2
    NO_BATCH = 3
    GUI_REFUSE_FILETRANSFER = 4
    INVALID_TYPE = 5
    NO_AUTHORITY = 6
    UNKNOWN_ERROR = 7
    BAD_DATA_FORMAT = 8
    HEADER_NOT_ALLOWED = 9
    SEPARATOR_NOT_ALLOWED = 10
    HEADER_TOO_LONG = 11
    UNKNOWN_DP_ERROR = 12
    ACCESS_DENIED = 13
    DP_OUT_OF_MEMORY = 14
    DISK_FULL = 15
    DP_TIMEOUT = 16
    OTHERS = 17.

IF sy-subrc <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

**********************************************************************
* DATA POPULATIONS *
**********************************************************************

LOOP AT IT_MATMASTER.

  BAPIMATHEAD-MATERIAL = IT_MATMASTER-MATNR.
  BAPIMATHEAD-IND_SECTOR = IT_MATMASTER-Mbrsh.
  BAPIMATHEAD-MATL_TYPE = IT_MATMASTER-Mtart.
  BAPIMATHEAD-BASIC_VIEW = 'X'.
  BAPIMATHEAD-PURCHASE_VIEW = 'X'.
  BAPIMATHEAD-ACCOUNT_VIEW = 'X'.
  BAPI_MARA-MATL_GROUP = IT_MATMASTER-MATKL.
  BAPI_MARA-OLD_MAT_NO = IT_MATMASTER-BISMT.
  BAPI_MARA-BASE_UOM = IT_MATMASTER-MEINS.
  BAPI_MARA-BASIC_MATL = IT_MATMASTER-WRKST.
  BAPI_MARA-MFR_NO = IT_MATMASTER-MFRNR.
  BAPI_MARAX-MATL_GROUP = 'X'.
  BAPI_MARAX-OLD_MAT_NO = 'X'.
  BAPI_MARAX-BASE_UOM = 'X'.
  BAPI_MARAX-BASIC_MATL = 'X'.
  BAPI_MARAX-MFR_NO = 'X'.
  BAPI_MARC-PLANT = IT_MATMASTER-WERKS.
  BAPI_MARC-PUR_GROUP = IT_MATMASTER-EKGRP.
  BAPI_MARCX-PLANT = IT_MATMASTER-WERKS.
  BAPI_MARCX-PUR_GROUP = 'X'.
  IT_MATERIALDESC-LANGU = 'EN'.
  IT_MATERIALDESC-MATL_DESC = IT_MATMASTER-MAKTX.
  
  APPEND IT_materialdesc.

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
  EXPORTING
    headdata = BAPIMATHEAD
    CLIENTDATA = BAPI_MARA
    CLIENTDATAX = BAPI_MARAX
    PLANTDATA = BAPI_MARC
    PLANTDATAX = BAPI_MARCX
    FORECASTPARAMETERS =
    FORECASTPARAMETERSX =
    PLANNINGDATA =
    PLANNINGDATAX =
    STORAGELOCATIONDATA =
    STORAGELOCATIONDATAX =
    VALUATIONDATA =
    VALUATIONDATAX =
    WAREHOUSENUMBERDATA =
    WAREHOUSENUMBERDATAX =
    SALESDATA =
    SALESDATAX =
    STORAGETYPEDATA =
    STORAGETYPEDATAX =
    FLAG_ONLINE = ' '
    FLAG_CAD_CALL = ' '
  IMPORTING
    RETURN = IT_RETURN
  TABLES
    MATERIALDESCRIPTION = IT_MATERIALDESC
    UNITSOFMEASURE =
    UNITSOFMEASUREX =
    INTERNATIONALARTNOS =
    MATERIALLONGTEXT =
    TAXCLASSIFICATIONS =
    RETURNMESSAGES =
    PRTDATA =
    PRTDATAX =
    EXTENSIONIN =
    EXTENSIONINX =
  .

  IT_RETURN-LOG_NO,
  IT_RETURN-LOG_MSG_NO,
  IT_RETURN-MESSAGE_V1,
  IT_RETURN-MESSAGE_V2,
  IT_RETURN-MESSAGE_V3,
  IT_RETURN-MESSAGE_V4,
  IT_RETURN-PARAMETER,
  IT_RETURN-ROW,
  IT_RETURN-FIELD,
  IT_RETURN-SYSTEM.
  
    " IT_RETURN-TYPE is 'E' in case of error, else 'S'.
    IF IT_RETURN-TYPE = 'E'.
      WRITE:/ IT_RETURN-TYPE,
          IT_RETURN-ID,
          IT_RETURN-NUMBER,
          IT_RETURN-MESSAGE.
          
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          
    ELSEIF IT_RETURN-TYPE = 'S'.
        " Commit to release the locks
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    ENDIF.
ENDLOOP.


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。