SAP 物料信息创建或者修改传外部系统接口

举报
雨绸缪 发表于 2023/10/30 16:04:09 2023/10/30
【摘要】 引言在 SAP 开发中,少不了需要跟外部系统进行交互,此时就需要接口,本文介绍一下如何将 SAP 中的物料信息,传到外部系统。首先,我们要确定,每次物料信息保存会进入哪些用户出口或者 BADI,这个有很多,本文选择了 ZXMG0U02 程序,这个包含文件中有一个与 mara 对应的结构 wmara,里面包含着保存物料后的所有字段值。而这段程序也是包含在出口 EXIT_SAPLMGMU_00...

引言

在 SAP 开发中,少不了需要跟外部系统进行交互,此时就需要接口,本文介绍一下如何将 SAP 中的物料信息,传到外部系统。

首先,我们要确定,每次物料信息保存会进入哪些用户出口或者 BADI,这个有很多,本文选择了 ZXMG0U02 程序,这个包含文件中有一个与 mara 对应的结构 wmara,里面包含着保存物料后的所有字段值。

而这段程序也是包含在出口 EXIT_SAPLMGMU_001 中的,代码如下:

FUNCTION EXIT_SAPLMGMU_001.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(WMARA) LIKE  MARA STRUCTURE  MARA
*"     VALUE(WMARC) LIKE  MARC STRUCTURE  MARC
*"     VALUE(WMARD) LIKE  MARD STRUCTURE  MARD
*"     VALUE(WMBEW) LIKE  MBEW STRUCTURE  MBEW
*"     VALUE(WMLGN) LIKE  MLGN STRUCTURE  MLGN
*"     VALUE(WMLGT) LIKE  MLGT STRUCTURE  MLGT
*"     VALUE(WMVKE) LIKE  MVKE STRUCTURE  MVKE
*"     VALUE(WSTAT) LIKE  MGSTAT STRUCTURE  MGSTAT
*"     VALUE(WMFHM) LIKE  MFHM STRUCTURE  MFHM
*"     VALUE(WMPOP) LIKE  MPOP STRUCTURE  MPOP
*"  TABLES
*"      STEXT STRUCTURE  SHORT_DESC
*"      SSTEUERTAB STRUCTURE  MG03STEUER
*"      SSTEUMMTAB STRUCTURE  MG03STEUMM
*"      WMEINH STRUCTURE  SMEINH
*"      SMEAN_ME_TAB STRUCTURE  MEAN
*"  CHANGING
*"     VALUE(CMARA) LIKE  MARU STRUCTURE  MARU
*"  EXCEPTIONS
*"      APPLICATION_ERROR
*"----------------------------------------------------------------------


  INCLUDE ZXMG0U02.


ENDFUNCTION.

这个出口包含了所有物料相关的信息:基本数据、工厂数据、库存数据、销售数据等等,如图。

image.png

外部接口文档定义如下:创建/修改 物料,供应商 mapping 信息

接口文档

{
    "jsonrpc": "2.0",
    "method": "call",
    "params": {
       "args": [[123456],[{
          "data_type": "create",                                     // create(创建),write(修改)
          "code": "123456789",                                       // 唯一标识符
          "material_code": "19000000197",                            // Material Code
          "supplier_code": "500619",                                 // Supplier Code
          "statr_date": "20220131",                                  // Start Code
          "end_date": "20230131"                                     // Start Code
       }]],
      "model": "edi.material.supplier.mapping",
      "method": "sync_material_supplier_info",
      "kwargs": {
        "context": null
      },
      "id": 123456
    }
}

创建成功返回值

{
    "jsonrpc": "2.0",
    "id": 81,
    "result": []
}

创建错误返回值

{
    "jsonrpc": "2.0",
    "id": 81,
    "error": {
        "code": 200,
        "message": "Odoo Server Error",
        "data": {
            "name": "",
            "debug": "",
            "message": "",
            "arguments": [""],
            "exception_type": ""
        }
    }
}

ZXMG0U02 中调用如下的新建物料的功能模块:

 CALL FUNCTION 'ZMARA_TRANS' IN BACKGROUND TASK
      TABLES
        gt_mara_all = gt_mara_trans
        gt_return   = gt_return.

修改的功能模块如下:

CALL FUNCTION 'ZMARA_TRANS_MODIFY' IN BACKGROUND TASK
      TABLES
        gt_mara_all = gt_mara_trans
        gt_return   = gt_return.

那么如何将物料信息填充到表 gt_mara_trans 中呢,可以写如下代码:

IF wmara-matnr IS NOT INITIAL.
    gs_mara_trans-matnr = wmara-matnr.
    gs_mara_trans-matkl = wmara-matkl.
    gs_mara_trans-mtart = wmara-mtart.
    gs_mara_trans-bismt = wmara-bismt.
    gs_mara_trans-spart = wmara-spart.
    gs_mara_trans-mtpos_mara = wmvke-mtpos.
    gs_mara_trans-meins = wmara-meins.
    APPEND gs_mara_trans TO gt_mara_trans.
ENDIF.

功能模块 ZMARA_TRANS

将其定义为 RFC 函数,即在属性中设置为 Remote-Enabled Module -> Start immed.,然后再 Tables 参数中添加两个参数 gt_mara_allgt_return 两个表格接口,分别定义你需要传给外部系统的结构和接受外部接口的返回成功或者失败信息。

FUNCTION zodoo_mara_trans.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  TABLES
*"      GT_MARA_ALL STRUCTURE  ZODOO_MARA_TRANS
*"      GT_RETURN STRUCTURE  ZODOO_MA_RETURN
*"----------------------------------------------------------------------


  SORT gt_mara_all BY matnr.
  DELETE ADJACENT DUPLICATES FROM gt_mara_all COMPARING ALL FIELDS.


  IF gt_mara_all[] IS NOT INITIAL.
    DATA: lr_http_client TYPE REF TO if_http_client .
    DATA: lr_http_client1 TYPE REF TO if_http_client .

    DATA : l_error TYPE string .

    DATA: c_str TYPE string.
    DATA: opcode_usr_attr(1) TYPE x VALUE 5,
          terminal           TYPE usr41-terminal.

    DATA: lv_url         TYPE string,
          lv_result      TYPE string,
          lv_post_string TYPE string,
          lv_len         TYPE i.


    TYPES:BEGIN OF ty_params1,
            db       TYPE string,
            login    TYPE string,
            password TYPE string,
          END OF ty_params1.


    TYPES:BEGIN OF ty_message1,
            jsonrpc TYPE string,
            method  TYPE string,
            params  TYPE ty_params1,
            id      TYPE i,

          END OF ty_message1.

    DATA:ls_message1  TYPE ty_message1 .


    TYPES:BEGIN OF ty_result,
            id      TYPE i,
            jsonrpc TYPE string,
            result  TYPE i,

          END OF ty_result .

    DATA:ls_result  TYPE ty_result .

    TYPES:BEGIN OF ty_args,
            default_code    TYPE string,
            sku             TYPE string,
            sku_desc        TYPE string,
            origin          TYPE string,
            name            TYPE string,
            material_type   TYPE i,
            old_material_no TYPE string,
            item_cat_group  TYPE char04,
            short_name      TYPE string, 
            unit_size       TYPE string, 
            product_group   TYPE i,      
            from_factory    TYPE string, 
            sap_product     TYPE string, 
          END OF ty_args.

    DATA:lt_args TYPE TABLE OF ty_args .
    DATA:ls_args TYPE ty_args .

    TYPES:BEGIN OF ty_params,
            args   LIKE lt_args,
            model  TYPE string,
            method TYPE string,
            kwargs TYPE string,


          END OF ty_params.


    TYPES:BEGIN OF ty_message,
            jsonrpc TYPE string,
            method  TYPE string,
            params  TYPE ty_params,
            id      TYPE i,

          END OF ty_message.

    DATA:ls_message  TYPE ty_message .

    DATA: l_matid TYPE string .

    DATA: l_i TYPE i .
    DATA: l_str TYPE string .


    ls_message1-jsonrpc = '2.0' .
    ls_message1-method = 'call' .
    
*-->内表数据转-->JSON格式
    lv_post_string = /ui2/cl_json=>serialize( data = ls_message1 compress = abap_true pretty_name = abap_true ).


    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = lv_url
      IMPORTING
        client             = lr_http_client1
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.


    "设定调用服务
    lr_http_client1->request->set_method( if_http_request=>co_request_method_post ).

    CALL METHOD lr_http_client1->request->set_header_field
      EXPORTING
        name  = 'CONTENT-TYPE'
        value = 'application/json'.


    lv_len = strlen( lv_post_string ) .

    CALL METHOD lr_http_client1->request->set_cdata
      EXPORTING
        data   = lv_post_string
        offset = 0
        length = lv_len.

    CALL METHOD lr_http_client1->send
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2.
    CALL METHOD lr_http_client1->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3.


    CALL METHOD lr_http_client1->response->get_cookie
      EXPORTING
        name  = 'session_id'
      IMPORTING
        value = g_session.



    lv_result = lr_http_client1->response->get_cdata( ).

    WRITE :/ 'Get session ID :' .

    WRITE :/ g_session  .

    lv_url = 'http://xxx.xxx/' .

    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = lv_url
      IMPORTING
        client             = lr_http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.



    lr_http_client->request->set_cookie( name = 'session_id' value = g_session ).


    "设定调用服务
    lr_http_client->request->set_method( if_http_request=>co_request_method_post ).

    CALL METHOD lr_http_client->request->set_header_field
      EXPORTING
        name  = 'CONTENT-TYPE'
        value = 'application/json'.


    LOOP AT gt_mara_all .

      CLEAR : ls_message , lv_post_string ,lv_len ,lv_result ,ls_args ,ls_message-params-args .

      ls_message-jsonrpc = '2.0' .
      ls_message-method = 'call' .

      ls_args-default_code =  gt_mara_all-matnr   .
      ls_args-sku = gt_mara_all-zmatsku   .
      ls_args-sku_desc = gt_mara_all-zskudes .

      LS_ARGS-material_tpye = GT_MARA_ALL-MATKL   .
      ls_args-old_material_no = gt_mara_all-bismt   .
      ls_args-item_cat_group = gt_mara_all-mtpos_mara.

      ls_args-loss_rate =  0  .
      ls_args-tracking = 'lot'  .
      ls_args-type = 'product'  .
      ls_args-origin = 'na' .
      ls_args-short_name = gt_mara_all-maktx 

      APPEND ls_args TO ls_message-params-args .

      ls_message-params-model = '"product.product"' .
      ls_message-params-method = '"create"' .
      ls_message-params-kwargs = '{"context":null}' .

      ls_message-id = 1  .

*     *-->内表数据转-->JSON格式
      lv_post_string = /ui2/cl_json=>serialize( data = ls_message compress = abap_true pretty_name = abap_true ).

      REPLACE ALL OCCURRENCES OF 'defaultCode'  IN lv_post_string WITH 'default_code'.
      REPLACE ALL OCCURRENCES OF 'materialType'  IN lv_post_string WITH 'material_type'.
      REPLACE ALL OCCURRENCES OF 'oldMaterialNo'  IN lv_post_string WITH 'old_material_no'.
      REPLACE ALL OCCURRENCES OF 'itemCatGroup'  IN lv_post_string WITH 'item_cat_group'.
      REPLACE ALL OCCURRENCES OF 'skuDesc'  IN lv_post_string WITH 'sku_desc'.
      REPLACE ALL OCCURRENCES OF 'materialGroup'  IN lv_post_string WITH 'material_group'.
      REPLACE ALL OCCURRENCES OF 'materialState'  IN lv_post_string WITH 'material_state'.
      REPLACE ALL OCCURRENCES OF 'shortName'  IN lv_post_string WITH 'short_name'.
      REPLACE ALL OCCURRENCES OF 'unitSize'  IN lv_post_string WITH 'unit_size'. 
      REPLACE ALL OCCURRENCES OF 'productGroup'  IN lv_post_string WITH 'product_group'. 
      REPLACE ALL OCCURRENCES OF '"true"'  IN lv_post_string WITH 'true'.  
      REPLACE ALL OCCURRENCES OF 'fromFactory'  IN lv_post_string WITH 'from_factory'. 
      REPLACE ALL OCCURRENCES OF '"model":"\"product.product\"","method":"\"create\"","kwargs":"{\"context\":null}"' IN lv_post_string
                 WITH '"model":"product.product","method":"create","kwargs":{"context":null}' .


      lv_len = strlen( lv_post_string ) .

      CALL METHOD lr_http_client->request->set_cdata
        EXPORTING
          data   = lv_post_string
          offset = 0
          length = lv_len.


      CALL METHOD lr_http_client->send
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2.
      CALL METHOD lr_http_client->receive
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3.

      lv_result = lr_http_client->response->get_cdata( ).


      WRITE :/ 'Post MasData create:' , ls_args-default_code .
      WRITE :/ lv_result .

      CLEAR ls_result .

      CALL METHOD /ui2/cl_json=>deserialize
        EXPORTING
          json        = lv_result
          pretty_name = 'X'
        CHANGING
          data        = ls_result.


      CLEAR l_odoo_mara .
      IF ls_result-result > 0 .
       

        gt_return-zid = ls_result-result.
        gt_return-zmessage = '传输成功'.

      ELSE .
        PERFORM get_error_message   USING  lv_result
                        CHANGING l_error.

        gt_return-zmessage = l_error.
      ENDIF .

    ENDLOOP .

    IF gt_return[] IS NOT INITIAL.
      MODIFY zma_return FROM TABLE gt_return[].
    ENDIF.

    lr_http_client1->close( ).

    lr_http_client->close( ).

  ENDIF.

ENDFUNCTION.

删除 物料,供应商mapping信息

  • URL: /web/dataset/call_kw
{
    "jsonrpc": "2.0",
    "method": "call",
    "params": {
       "args": [[123456],["123456788", "123456789"]],  // SAP唯一标识符
      "model": "edi.material.supplier.mapping",
      "method": "unlink_material_supplier",
      "kwargs": {
        "context": null
      },
      "id": 123456
    }
}

总结

修改和删除的接口也可以参考新建的功能模块类似的做法,这里就不贴代码了,防止文章过长。

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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