SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi

BAPI函数:cmd_ei_api=>maintain_bapi

事物代码:XD01/XD02

客户主数据创建、修改、拓展功能开发

数据结构定义:

基本视图信息

SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi_第1张图片

SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi_第2张图片

公司代码信息结构:

SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi_第3张图片

销售视图信息结构:

SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi_第4张图片

客户主数据税分类信息结构:

SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi_第5张图片

代码参考

详细代码样例参考:

FUNCTION zsd_customer_maintain.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(IS_GENERAL) TYPE  ZSGENERAL OPTIONAL
*"  EXPORTING
*"     REFERENCE(KUNNR) TYPE  KUNNR
*"  TABLES
*"      IT_COMPANY_DATA STRUCTURE  ZSCOMPANY_DATA OPTIONAL
*"      IT_SALES_DATA STRUCTURE  ZSSALES_DATA OPTIONAL
*"      IT_TAX_DATA STRUCTURE  ZSTAX_DATA OPTIONAL
*"      ET_RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
  DATA: is_master_data           TYPE cmds_ei_main,
        es_error                 TYPE cvis_message,
        ls_messages              TYPE bapiret2,
        ls_return                TYPE bapiret2,
        ls_company_data          TYPE zscompany_data,
        ls_company               TYPE cmds_ei_company,
        ls_customers             TYPE cmds_ei_extern,
        ls_sales_data            TYPE zssales_data,
        ls_sales                 TYPE cmds_ei_sales,
        ls_tax_data              TYPE zstax_data,
        ls_tax                   TYPE cmds_ei_tax_ind,
        ls_banks                 TYPE cvis_ei_cvi_bankdetail,
        lv_kunnr                 TYPE knvk-kunnr,
        lt_knvk                  TYPE TABLE OF knvk,
        ls_knvk                  TYPE knvk,
        ls_adr6                  TYPE adr6,
        lv_name                  TYPE string,
        ls_contact               TYPE cmds_ei_contacts,
        gs_error                 TYPE cvis_message,
        ls_phone                 TYPE cvis_ei_phone_str,
        ls_smtp                  TYPE cvis_ei_smtp_str,
        ls_functions             TYPE cmds_ei_functions,
        es_master_data_correct   TYPE cmds_ei_main,
        es_message_correct       TYPE cvis_message,
        ls_customrers            TYPE cmds_ei_extern,
        es_master_data_defective TYPE cmds_ei_main,
        es_message_defective     TYPE cvis_message.

*--------------------------------------------------------------------*
*  基本视图信息
*--------------------------------------------------------------------*
  ls_customers-header-object_task = 'M'.
  CLEAR: lv_kunnr.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = is_general-kunnr
    IMPORTING
      output = lv_kunnr.
  IF lv_kunnr IS NOT INITIAL.
    ls_customers-header-object_instance-kunnr = lv_kunnr.
  ENDIF.

  IF is_general-ktokd IS NOT INITIAL.
    ls_customers-central_data-central-data-ktokd = is_general-ktokd. "客户帐户组
    ls_customers-central_data-central-datax-ktokd = 'X'.
  ENDIF.

  IF is_general-stcd5 IS NOT INITIAL.
    ls_customers-central_data-central-data-stcd5 = is_general-stcd5. "税号 5
    ls_customers-central_data-central-datax-stcd5 = 'X'.
  ENDIF.

  IF is_general-title IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-title = is_general-title. "称谓默认公司:0003
    ls_customers-central_data-address-postal-datax-title = 'X'.
  ENDIF.

  IF is_general-name IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-name = is_general-name. "名称 1
    ls_customers-central_data-address-postal-datax-name = 'X'.
  ENDIF.

  IF is_general-name_2 IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-name_2 = is_general-name_2. "名称 2
    ls_customers-central_data-address-postal-datax-name_2 = 'X'.
  ENDIF.

  IF is_general-name_3 IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-name_3 = is_general-name_3. "名称 3
    ls_customers-central_data-address-postal-datax-name_3 = 'X'.
  ENDIF.

  IF is_general-name_4 IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-name_4 = is_general-name_4. "名称 4
    ls_customers-central_data-address-postal-datax-name_4 = 'X'.
  ENDIF.

  IF is_general-sort1 IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-sort1 = is_general-sort1. "检索项1
    ls_customers-central_data-address-postal-datax-sort1 = 'X'.
  ENDIF.

  IF is_general-sort2 IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-sort2 = is_general-sort2. "检索项2
    ls_customers-central_data-address-postal-datax-sort2 = 'X'.
  ENDIF.

  IF is_general-street IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-street = is_general-street. "街道
    ls_customers-central_data-address-postal-datax-street = 'X'.
  ENDIF.

  IF is_general-country IS NOT INITIAL.
    ls_customers-central_data-address-postal-data-country = is_general-country. "国家键值
    ls_customers-central_data-address-postal-datax-country = 'X'.
    IF is_general-country = 'CN'.
      ls_customers-central_data-address-postal-data-langu = '1'. "语言代码
    ELSE.
      ls_customers-central_data-address-postal-data-langu = 'E'. "语言代码
    ENDIF.
    ls_customers-central_data-address-postal-datax-langu = 'X'.
  ENDIF.
  IF ls_customers-central_data-address IS NOT INITIAL.
    ls_customers-central_data-address-task = 'M'.
  ENDIF.

  IF is_general-banks IS NOT INITIAL.
    ls_banks-data_key-banks = is_general-banks. "银行国家代码
  ENDIF.

  IF is_general-bankl IS NOT INITIAL.
    ls_banks-data_key-bankl = is_general-bankl. "银行编号
  ENDIF.

  IF is_general-bankn IS NOT INITIAL.
    ls_banks-data_key-bankn = is_general-bankn. "银行帐户号码
  ENDIF.

  IF is_general-bvtyp IS NOT INITIAL.
    ls_banks-data-bvtyp = is_general-bvtyp. "合作银行类型
    ls_banks-datax-bvtyp = 'X'.
  ENDIF.

  IF is_general-bkref IS NOT INITIAL.
    ls_banks-data-bkref = is_general-bkref. "银行帐户的参考规定
    ls_banks-datax-bkref = 'X'.
  ENDIF.

  IF is_general-koinh IS NOT INITIAL.
    ls_banks-data-koinh = is_general-koinh. "帐户持有人姓名
    ls_banks-datax-koinh = 'X'.
  ENDIF.

  IF ls_banks IS NOT INITIAL.
    ls_banks-task = 'M'.
    APPEND ls_banks TO ls_customers-central_data-bankdetail-bankdetails[].
    CLEAR: ls_banks.
  ENDIF.

  "检查联系人是否有创建,如果有的话,就进行修改,如果没有,则新建
  SELECT * INTO TABLE lt_knvk
    FROM knvk
    WHERE kunnr = lv_kunnr.

  LOOP AT lt_knvk INTO ls_knvk.
    CLEAR: lv_name.
    CONCATENATE ls_knvk-name1 ls_knvk-namev INTO lv_name.
    IF lv_name = is_general-name_c.
      ls_contact-task = 'U'.
      ls_contact-data_key-parnr = ls_knvk-parnr.
      EXIT.
    ELSE.
      ls_contact-task = 'M'.
    ENDIF.
  ENDLOOP.
  IF ls_contact-task = 'M'.
    cmd_ei_api=>get_contact_number( IMPORTING ev_parnr = ls_contact-data_key-parnr
                                                es_error = gs_error ).
  ENDIF.


  IF is_general-telephone IS NOT INITIAL.
    ls_phone-contact-data-telephone = is_general-telephone.
    ls_phone-contact-datax-telephone = 'X'.
  ENDIF.

  IF is_general-extension IS NOT INITIAL.
    ls_phone-contact-data-extension = is_general-extension.
    ls_phone-contact-datax-extension = 'X'.
  ENDIF.

  IF is_general-tel_no IS NOT INITIAL.
    ls_phone-contact-data-tel_no = is_general-tel_no.
    ls_phone-contact-datax-tel_no = 'X'.
  ENDIF.

  IF ls_phone IS NOT INITIAL.
    ls_phone-contact-task = 'M'.
    APPEND ls_phone TO ls_contact-address_type_1-communication-phone-phone[].
    CLEAR: ls_phone.
  ENDIF.

  IF is_general-e_mail IS NOT INITIAL.
    READ TABLE lt_knvk INTO ls_knvk WITH KEY parnr = ls_contact-data_key-parnr.
    IF sy-subrc = 0.
      SELECT SINGLE * INTO ls_adr6
        FROM adr6
        WHERE persnumber = ls_knvk-prsnr.
      IF ls_adr6-smtp_addr IS INITIAL.
        ls_smtp-contact-task = 'I'.
      ELSE.
        ls_smtp-contact-task = 'U'.
      ENDIF.
    ENDIF.

    ls_smtp-contact-data-e_mail = is_general-e_mail.
    ls_smtp-contact-datax-e_mail = 'X'.
    APPEND ls_smtp TO ls_contact-address_type_1-communication-smtp-smtp[].
    APPEND ls_smtp TO ls_contact-address_type_3-communication-smtp-smtp[].
    CLEAR: ls_smtp.
  ENDIF.

  IF is_general-postl_cod1 IS NOT INITIAL.
    ls_contact-address_type_1-postal-data-postl_cod1 = is_general-postl_cod1.
    ls_contact-address_type_1-postal-datax-postl_cod1 = 'X'.

    ls_contact-address_type_2-postal-data-postl_cod1 = is_general-postl_cod1.
    ls_contact-address_type_2-postal-datax-postl_cod1 = 'X'.
  ENDIF.

  IF is_general-country IS NOT INITIAL.
    ls_contact-address_type_1-postal-data-country = is_general-country.
    ls_contact-address_type_1-postal-datax-country = 'X'.

    ls_contact-address_type_2-postal-data-country = is_general-country.
    ls_contact-address_type_2-postal-datax-country = 'X'.
  ENDIF.

  IF is_general-name_c IS NOT INITIAL.
    ls_contact-address_type_1-postal-data-name = is_general-name_c.
    ls_contact-address_type_1-postal-datax-name = 'X'.

    ls_contact-address_type_3-postal-data-fullname = is_general-name_c.
    ls_contact-address_type_3-postal-data-firstname = is_general-name_c.
    ls_contact-address_type_3-postal-data-lastname = is_general-name_c.
    ls_contact-address_type_3-postal-datax-fullname = 'X'.
    ls_contact-address_type_3-postal-datax-firstname = 'X'.
    ls_contact-address_type_3-postal-datax-lastname = 'X'.
  ENDIF.

  IF ls_contact IS NOT INITIAL.
    ls_contact-address_type_1-task = 'M'.
    ls_contact-address_type_2-task = 'M'.
    ls_contact-address_type_3-task = 'M'.
    APPEND ls_contact TO ls_customers-central_data-contact-contacts[].
    CLEAR: ls_contact.
  ENDIF.

  "紧急联系人
  LOOP AT lt_knvk INTO ls_knvk.
    CLEAR: lv_name.
    CONCATENATE ls_knvk-name1 ls_knvk-namev INTO lv_name.
    IF lv_name = is_general-name_jj.
      ls_contact-task = 'U'.
      ls_contact-data_key-parnr = ls_knvk-parnr.
      EXIT.
    ELSE.
      ls_contact-task = 'M'.
    ENDIF.
  ENDLOOP.
  IF ls_contact-task = 'M'.
    cmd_ei_api=>get_contact_number( IMPORTING ev_parnr = ls_contact-data_key-parnr
                                              es_error = gs_error ).
  ENDIF.

  IF is_general-telephone_jj IS NOT INITIAL.
    ls_phone-contact-data-telephone = is_general-telephone_jj.
    ls_phone-contact-datax-telephone = 'X'.
  ENDIF.

  IF is_general-extension_jj IS NOT INITIAL.
    ls_phone-contact-data-extension = is_general-extension_jj.
    ls_phone-contact-datax-extension = 'X'.
  ENDIF.

  IF is_general-tel_no_jj IS NOT INITIAL.
    ls_phone-contact-data-tel_no = is_general-tel_no_jj.
    ls_phone-contact-datax-tel_no = 'X'.
  ENDIF.

  IF ls_phone IS NOT INITIAL.
    ls_phone-contact-task = 'M'.
    APPEND ls_phone TO ls_contact-address_type_1-communication-phone-phone[].
    CLEAR: ls_phone.
  ENDIF.

  IF is_general-e_mail_jj IS NOT INITIAL.
    READ TABLE lt_knvk INTO ls_knvk WITH KEY parnr = ls_contact-data_key-parnr.
    IF sy-subrc = 0.
      SELECT SINGLE * INTO ls_adr6
        FROM adr6
        WHERE persnumber = ls_knvk-prsnr.
      IF ls_adr6-smtp_addr IS INITIAL.
        ls_smtp-contact-task = 'I'.
      ELSE.
        ls_smtp-contact-task = 'U'.
      ENDIF.
    ENDIF.

    ls_smtp-contact-data-e_mail = is_general-e_mail_jj.
    ls_smtp-contact-datax-e_mail = 'X'.
    APPEND ls_smtp TO ls_contact-address_type_1-communication-smtp-smtp[].
    APPEND ls_smtp TO ls_contact-address_type_3-communication-smtp-smtp[].
    CLEAR: ls_smtp.
  ENDIF.

  IF is_general-postl_cod1_jj IS NOT INITIAL.
    ls_contact-address_type_1-postal-data-postl_cod1 = is_general-postl_cod1_jj.
    ls_contact-address_type_1-postal-datax-postl_cod1 = 'X'.

    ls_contact-address_type_2-postal-data-postl_cod1 = is_general-postl_cod1_jj.
    ls_contact-address_type_2-postal-datax-postl_cod1 = 'X'.
  ENDIF.

  IF is_general-country IS NOT INITIAL.
    ls_contact-address_type_1-postal-data-country = is_general-country.
    ls_contact-address_type_1-postal-datax-country = 'X'.

    ls_contact-address_type_2-postal-data-country = is_general-country.
    ls_contact-address_type_2-postal-datax-country = 'X'.
  ENDIF.

  IF is_general-name_jj IS NOT INITIAL.
    ls_contact-address_type_1-postal-data-name = is_general-name_jj.
    ls_contact-address_type_1-postal-datax-name = 'X'.

    ls_contact-address_type_3-postal-data-fullname = is_general-name_jj.
    ls_contact-address_type_3-postal-data-firstname = is_general-name_jj.
    ls_contact-address_type_3-postal-data-lastname = is_general-name_jj.
    ls_contact-address_type_3-postal-datax-fullname = 'X'.
    ls_contact-address_type_3-postal-datax-firstname = 'X'.
    ls_contact-address_type_3-postal-datax-lastname = 'X'.
  ENDIF.

  IF ls_contact IS NOT INITIAL.
    ls_contact-address_type_1-task = 'M'.
    ls_contact-address_type_2-task = 'M'.
    ls_contact-address_type_3-task = 'M'.
    APPEND ls_contact TO ls_customers-central_data-contact-contacts[].
    CLEAR: ls_contact.
  ENDIF.

  " 税分类
  LOOP AT it_tax_data INTO ls_tax_data.
    ls_tax-task = 'M'.
    ls_tax-data_key-aland = ls_tax_data-aland. "发货国(由该国家发货)
    ls_tax-data_key-tatyp = ls_tax_data-tatyp. "税类别(营业税,联邦营业税,...)
    ls_tax-data-taxkd = ls_tax_data-taxkd. "客户税分类

    ls_tax-datax-taxkd = 'X'.
    APPEND ls_tax TO ls_customers-central_data-tax_ind-tax_ind[].
    CLEAR: ls_tax, ls_tax_data.
  ENDLOOP.
*--------------------------------------------------------------------*
*  销售视图信息
*--------------------------------------------------------------------*
  LOOP AT it_sales_data INTO ls_sales_data.
    ls_sales-task = 'M'.
    ls_sales-data_key-vkorg = ls_sales_data-vkorg. "销售组织
    ls_sales-data_key-vtweg = ls_sales_data-vtweg. "分销渠道
    ls_sales-data_key-spart = ls_sales_data-spart. "产品组
    ls_sales-data-bzirk = ls_sales_data-bzirk."销售地区
    ls_sales-data-vkgrp = ls_sales_data-vkgrp. "销售组
    ls_sales-data-vkbur = ls_sales_data-vkbur. "销售部门
    ls_sales-data-awahr = ls_sales_data-awahr."该项目的订单可能性
    ls_sales-data-kdgrp = ls_sales_data-kdgrp. "客户组
    ls_sales-data-waers = ls_sales_data-waers. "货币
    ls_sales-data-kalks = ls_sales_data-kalks."定价过程分配到该客户
    ls_sales-data-zterm = ls_sales_data-zterm. "付款条件代码
    ls_sales-data-mrnkz = ls_sales_data-mrnkz. "人工发票维护
    ls_sales-data-bokre = ls_sales_data-bokre. "标识:客户将接受回扣
    ls_sales-data-prfre = ls_sales_data-prfre. "定价标识相关
    ls_sales-data-ktgrd = ls_sales_data-ktgrd. "客户组的帐户分配
    ls_sales-data-inco1 = ls_sales_data-inco1. "国际贸易条款 (部分1)
    ls_sales-data-inco2 = ls_sales_data-inco2. "国际贸易条件(部分2)
    ls_sales-data-kzazu = 'X'. "订单组合标识

    ls_functions-task = 'M'.
    ls_functions-data_key-parvw = 'AG'.
    IF lv_kunnr IS NOT INITIAL.
      ls_functions-data-partner = lv_kunnr.
      ls_functions-datax-partner = 'X'.
    ENDIF.
    APPEND ls_functions TO ls_sales-functions-functions[].
    CLEAR: ls_functions.
    ls_functions-task = 'M'.
    ls_functions-data_key-parvw = 'RE'.
    IF lv_kunnr IS NOT INITIAL.
      ls_functions-data-partner = lv_kunnr.
      ls_functions-datax-partner = 'X'.
    ENDIF.
    APPEND ls_functions TO ls_sales-functions-functions[].
    CLEAR: ls_functions.
    ls_functions-task = 'M'.
    ls_functions-data_key-parvw = 'RG'.
    IF lv_kunnr IS NOT INITIAL.
      ls_functions-data-partner = lv_kunnr.
      ls_functions-datax-partner = 'X'.
    ENDIF.
    APPEND ls_functions TO ls_sales-functions-functions[].
    CLEAR: ls_functions.
    ls_functions-task = 'M'.
    ls_functions-data_key-parvw = 'WE'.
    IF lv_kunnr IS NOT INITIAL.
      ls_functions-data-partner = lv_kunnr.
      ls_functions-datax-partner = 'X'.
    ENDIF.
    APPEND ls_functions TO ls_sales-functions-functions[].
    CLEAR: ls_functions.

    ls_sales-datax-bzirk = 'X'.
    ls_sales-datax-vkgrp = 'X'.
    ls_sales-datax-vkbur = 'X'.
    ls_sales-datax-awahr = 'X'.
    ls_sales-datax-kdgrp = 'X'.
    ls_sales-datax-waers = 'X'.
    ls_sales-datax-kalks = 'X'.
    ls_sales-datax-zterm = 'X'.
    ls_sales-datax-mrnkz = 'X'.
    ls_sales-datax-bokre = 'X'.
    ls_sales-datax-prfre = 'X'.
    ls_sales-datax-ktgrd = 'X'.
    ls_sales-datax-inco1 = 'X'.
    ls_sales-datax-inco2 = 'X'.
    ls_sales-datax-kzazu = 'X'.

    APPEND ls_sales TO ls_customers-sales_data-sales[].
    CLEAR: ls_sales, ls_sales_data.
  ENDLOOP.

*--------------------------------------------------------------------*
*  公司代码视图信息
*--------------------------------------------------------------------*
  LOOP AT it_company_data INTO ls_company_data.
    ls_company-task = 'M'.
    ls_company-data_key-bukrs = ls_company_data-bukrs. "公司代码
    ls_company-data-akont = ls_company_data-akont. "统驭科目

    ls_company-datax-akont = 'X'.
    APPEND ls_company TO ls_customers-company_data-company[].
    CLEAR: ls_company, ls_company_data.
  ENDLOOP.

  APPEND ls_customers TO is_master_data-customers[].

*  CALL METHOD cmd_ei_api=>maintain
*    EXPORTING
*      iv_test_run    = space
*      is_master_data = is_master_data
*    IMPORTING
*      es_error       = es_error.

  CALL METHOD cmd_ei_api=>maintain_bapi
    EXPORTING
*     iv_test_run              = SPACE
*     iv_collect_messages      = SPACE
      is_master_data           = is_master_data
    IMPORTING
      es_master_data_correct   = es_master_data_correct
      es_message_correct       = es_message_correct
      es_master_data_defective = es_master_data_defective
      es_message_defective     = es_message_defective.


  READ TABLE es_master_data_correct-customers[] INTO ls_customrers INDEX 1.
  IF sy-subrc <> 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
*     IMPORTING
*       RETURN        =
      .
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'
*     IMPORTING
*       RETURN        =
      .
*    READ TABLE es_master_data_correct-customers[] INTO ls_customrers INDEX 1.
    kunnr = ls_customrers-header-object_instance-kunnr.

  ENDIF.

  LOOP AT es_error-messages INTO ls_messages.
    MOVE-CORRESPONDING ls_messages TO ls_return.
    APPEND ls_return TO et_return.
    CLEAR: ls_return, ls_messages.
  ENDLOOP.

  LOOP AT es_message_defective-messages INTO ls_messages..
    MOVE-CORRESPONDING ls_messages TO ls_return.
    APPEND ls_return TO et_return.
    CLEAR: ls_return, ls_messages.
  ENDLOOP.

ENDFUNCTION.

你可能感兴趣的:(sap)