ABAP:BP 供应商创建修改BAPI和供应商银行信息创建修改BAPI

供应商创建及BAPI:cl_md_bp_maintain=>maintain

供应商银行信息创建BAPI:BAPI_BUPA_BANKDETAIL_ADD

供应商银行信息更新BAPI:BAPI_BUPA_BANKDETAIL_CHANGE

FORM frm_create_bp .
  DATA: lt_zhrt006 TYPE TABLE OF zhrt006,
        ls_zhrt006 LIKE LINE OF lt_zhrt006.
  DATA: lv_id TYPE sysuuid_x16 .

  DATA: exc_ref             TYPE REF TO cx_root,
        lt_partnerguid_list TYPE bu_partner_guid_t,
        ls_partnerguid_list LIKE LINE OF lt_partnerguid_list.

  DATA: lt_return TYPE  bapiretm.

  DATA: lv_bpartnerguid TYPE bu_partner_guid_bapi,
        lv_partnerguid  TYPE but000-partner_guid.

  "BP数据
  DATA: lt_data      TYPE cvis_ei_extern_t,
        ls_data      TYPE cvis_ei_extern,
        ls_partner   TYPE bus_ei_extern,
        ls_common    TYPE bus_ei_bupa_central,
        ls_roles     TYPE bus_ei_bupa_roles,
        lt_roles     TYPE bus_ei_bupa_roles_t,
        ls_bank      TYPE bus_ei_bupa_bankdetail,
        lt_bank      TYPE bus_ei_bupa_bankdetail_t,
        ls_industry  TYPE bus_ei_bupa_industrysector,
        lt_industry  TYPE bus_ei_bupa_industrysector_t,
        lt_phone     TYPE bus_ei_bupa_telephone_t,
        ls_phone     TYPE bus_ei_bupa_telephone,
        lt_fax       TYPE bus_ei_bupa_fax_t,
        ls_fax       TYPE bus_ei_bupa_fax,
        lt_smtp      TYPE bus_ei_bupa_smtp_t,
        ls_smtp      TYPE bus_ei_bupa_smtp,
        ls_addresses TYPE bus_ei_bupa_address,
        lt_addresses TYPE bus_ei_bupa_address_t,
        ls_bpheader  TYPE bus_ei_header.

  "供应商数据
  DATA: lt_company TYPE vmds_ei_company_t,
        ls_company TYPE vmds_ei_company,
        ls_purchas TYPE vmds_ei_purchasing,
        lt_purchas TYPE vmds_ei_purchasing_t,
        ls_vendor  TYPE vmds_ei_extern,
        ls_header  TYPE vmds_ei_header.
  DATA: ls_bankdetail TYPE bapibus1006_bankdetail.
  DATA: ls_bankdetail_x TYPE bapibus1006_bankdetail_x,
        lv_type         TYPE char1.

  DATA: lt_return2 TYPE TABLE OF bapiret2.

  CLEAR:lt_zhrt006[].
  LOOP AT gt_out ASSIGNING <fs_out> WHERE sel = 'X' AND color NE 'C510'.
    CLEAR: lv_bpartnerguid, lv_partnerguid, ls_header, ls_vendor, ls_bpheader, ls_common,
           ls_roles, lt_roles, lt_roles[], ls_phone, lt_phone, lt_phone[], ls_smtp, lt_smtp, lt_smtp[],
           ls_addresses, lt_addresses, lt_addresses[], ls_bank, lt_bank, lt_bank[], ls_company, lt_company, lt_company[],
           ls_data, lt_data, lt_data[].

    SELECT SINGLE partner_guid FROM but000 INTO lv_partnerguid WHERE partner = <fs_out>-lifnr .

    CASE 'X' .
      WHEN rb_c .               "创建
        TRY .
            lv_bpartnerguid = cl_uuid_factory=>create_system_uuid( )->create_uuid_x16( ).
          CATCH cx_uuid_error INTO exc_ref.
            "返回消息
            <fs_out>-zcode = 'E'.
            <fs_out>-zmsgl  = exc_ref->get_text( ).
            <fs_out>-color = 'C610' .

            CLEAR: lv_id .
            APPEND INITIAL LINE TO lt_zhrt006 ASSIGNING FIELD-SYMBOL(<fs_zhrt006>) .

            TRY.
                CALL METHOD cl_system_uuid=>if_system_uuid_static~create_uuid_c32
                  RECEIVING
                    uuid = lv_id.

              CATCH cx_uuid_error .
                MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
            ENDTRY.

            <fs_zhrt006>-uuid  = lv_id .
            <fs_zhrt006>-lifnr = <fs_out>-lifnr .
            <fs_zhrt006>-pernr = <fs_out>-pernr .
            <fs_zhrt006>-sname = <fs_out>-sname .
            <fs_zhrt006>-begda = <fs_out>-begda .
            <fs_zhrt006>-endda = <fs_out>-endda .
            <fs_zhrt006>-ztask = 'C' .
            <fs_zhrt006>-zcode = <fs_out>-zcode .
            <fs_zhrt006>-zmsgl = <fs_out>-zmsgl .
            <fs_zhrt006>-zdate = sy-datum .
            <fs_zhrt006>-ztime = sy-uzeit .
            <fs_zhrt006>-zname = sy-uname .

            CONTINUE .
        ENDTRY.

        ls_header-object_task = 'I' .
        ls_data-ensure_create-create_vendor = 'X'.

      WHEN rb_m OR rb_d .       "修改 冻结
        TRY .
            cl_system_uuid=>if_system_uuid_static~convert_uuid_x16(
            EXPORTING uuid = lv_partnerguid
            IMPORTING uuid_c32 = lv_bpartnerguid
            ).
          CATCH cx_uuid_error INTO DATA(exc_ref1).

        ENDTRY.

        ls_header-object_task = 'U'.
    ENDCASE .

    ls_header-object_instance-lifnr = <fs_out>-lifnr .
    ls_vendor-header = ls_header .

    IF rb_d = 'X' .    "冻结
      ls_vendor-central_data-central-data-sperr = 'X' .
      ls_vendor-central_data-central-datax-sperr = abap_true.
    ENDIF .

    "header
    ls_bpheader-object_task                   = 'M'.
    ls_bpheader-object_instance-bpartner      = <fs_out>-lifnr.          "BP号
    ls_bpheader-object_instance-bpartnerguid  = lv_bpartnerguid.         "BP GUID

    "common
    ls_common-data-bp_control-category  = '2'.                           "业务伙伴类别
    ls_common-data-bp_control-grouping  = <fs_out>-group.                "业务伙伴分组

    ls_common-data-bp_centraldata-partnerlanguageiso  = 'ZH'.            "语言
    ls_common-data-bp_centraldata-searchterm1         = <fs_out>-pernr.  "搜索项1
    ls_common-data-bp_centraldata-searchterm2         = <fs_out>-zbmen.  "搜索项2
    ls_common-data-bp_centraldata-title_key           = '0003'.          "称谓

    ls_common-datax-bp_centraldata-partnerlanguageiso = abap_true.
    ls_common-datax-bp_centraldata-searchterm1        = abap_true.
    ls_common-datax-bp_centraldata-searchterm2        = abap_true.
    ls_common-datax-bp_centraldata-title_key          = abap_true.

    ls_common-data-bp_organization-name1  = <fs_out>-sname.              "名称
    ls_common-datax-bp_organization-name1 = abap_true.

    ls_roles-task       = 'M'.
    ls_roles-data_key   = 'FLVN00'.                                      "角色类别
    APPEND ls_roles TO lt_roles.
    CLEAR: ls_roles.

    ls_roles-task       = 'M'.
    ls_roles-data_key   = 'FLVN01'.                                      "角色类别
    APPEND ls_roles TO lt_roles.
    CLEAR: ls_roles.

    "phone
    ls_phone-contact-task = 'M'.
    ls_phone-contact-data-telephone   = <fs_out>-telf1.                  "电话号码

    ls_phone-contact-datax-telephone  = abap_true.
    APPEND ls_phone TO lt_phone.
    CLEAR: ls_phone.

    "email
    ls_smtp-contact-task = 'M'.
    ls_smtp-contact-data-e_mail   = <fs_out>-telbx.                       "邮箱

    ls_smtp-contact-datax-e_mail  = abap_true.
    APPEND ls_smtp TO lt_smtp.
    CLEAR: ls_smtp.

    "addr
    ls_addresses-data-postal-data-country     = <fs_out>-land1.          "国家代码
    ls_addresses-data-postal-data-languiso    = 'ZH'.                    "语言
    ls_addresses-data-postal-data-street      = <fs_out>-locat.          "地址
    ls_addresses-data-postal-data-city        = <fs_out>-locat.          "城市
    ls_addresses-data-postal-data-postl_cod1  = '000000' .               "邮编

    ls_addresses-data-postal-datax-country    = abap_true.
    ls_addresses-data-postal-datax-langu_iso  = abap_true.
    ls_addresses-data-postal-datax-street     = abap_true.
    ls_addresses-data-postal-datax-city       = abap_true.
    ls_addresses-data-postal-datax-postl_cod1 = abap_true.
    APPEND ls_addresses TO lt_addresses.
    CLEAR: ls_addresses.

    "bank
    IF <fs_out>-bankl IS NOT INITIAL .
      ls_bank-task = 'M'.
      ls_bank-data-bank_ctry      = <fs_out>-banks.
      ls_bank-data-bank_key       = <fs_out>-bankl.
      ls_bank-data-bank_acct      = <fs_out>-bankn.
      ls_bank-data-accountholder  = <fs_out>-emfsl.
      ls_bank-data-bank_ref       = <fs_out>-bkref.
      ls_bank-data-bankdetailvalidfrom = <fs_out>-begda_yh. "ADD BY ZJ 20230803 银行信息有效起始日期
      ls_bank-data-bankdetailvalidto = <fs_out>-endda_yh. "ADD BY ZJ 20230803 银行信息有效结束日期

      ls_bank-datax-bank_ctry     = abap_true.
      ls_bank-datax-bank_key      = abap_true.
      ls_bank-datax-bank_acct     = abap_true.
      ls_bank-datax-bank_ref      = abap_true.
      ls_bank-datax-accountholder = abap_true.
      ls_bank-datax-bankdetailvalidfrom = abap_true. "ADD BY ZJ 20230803 银行信息有效起始日期
      ls_bank-datax-bankdetailvalidto = abap_true. "ADD BY ZJ 20230803 银行信息有效结束日期
      APPEND ls_bank TO lt_bank.
      CLEAR: ls_bank.
    ENDIF .

    ls_partner-header                                 = ls_bpheader.
    ls_partner-central_data-common                    = ls_common.
    ls_partner-central_data-role-roles                = lt_roles.
    ls_partner-central_data-bankdetail-bankdetails    = lt_bank.
    ls_partner-central_data-communication-phone-phone = lt_phone.
    ls_partner-central_data-communication-smtp-smtp   = lt_smtp.
    ls_partner-central_data-address-addresses         = lt_addresses.

    "company
    ls_company-task = 'M'.
    ls_company-data_key   = <fs_out>-bukrs.
    ls_company-data-akont = <fs_out>-akont.
    ls_company-data-zterm = <fs_out>-zterm.

    ls_company-datax-akont = abap_true.
    ls_company-datax-zterm = abap_true.
    APPEND ls_company TO lt_company.
    CLEAR: ls_company.

    ls_vendor-company_data-company = lt_company.
    CLEAR: ls_company.

    ls_data-partner = ls_partner.
    ls_data-vendor  = ls_vendor.
    APPEND ls_data TO lt_data.

    "维护
    cl_md_bp_maintain=>maintain(
      EXPORTING
        i_data     = lt_data
      IMPORTING
        e_return   = lt_return
    ).

    READ TABLE lt_return INTO DATA(ls_return) INDEX 1.
    DATA(lt_msg) = ls_return-object_msg.
    LOOP AT lt_msg INTO DATA(ls_msg) WHERE type CA 'AEX'.
      EXIT.
    ENDLOOP.
    IF sy-subrc <> 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = abap_true.

**********ADD BY ZJ 20230807 S 更新银行信息
      "判断表中有没有供应商的银行信息
      CLEAR:ls_bankdetail,ls_bankdetail_x,lv_type,lt_return2.
      SELECT SINGLE bkvid
        FROM but0bk
        INTO @DATA(lv_bkvid)
       WHERE partner = @<fs_out>-lifnr
         AND bankl = @<fs_out>-bankl
         AND bankn = @<fs_out>-bankn
         AND bkref = @<fs_out>-bkref.
      IF sy-subrc EQ 0.
        lv_type = 'U'.
      ELSE.
        lv_type = 'I'.
      ENDIF.

      ls_bankdetail-bank_ctry = <fs_out>-land1."国家
      ls_bankdetail-bank_key = <fs_out>-bankl."银行代码
      ls_bankdetail-bank_acct = <fs_out>-bankn."账号
      ls_bankdetail-bank_ref = <fs_out>-bkref."参考
      ls_bankdetail-bankdetailvalidfrom = <fs_out>-begda_yh."有效起始日期
      ls_bankdetail-bankdetailvalidto = <fs_out>-endda_yh."有效截至日期

      "插入银行信息
      IF lv_type EQ 'I'.
        CALL FUNCTION 'BAPI_BUPA_BANKDETAIL_ADD'
          EXPORTING
            businesspartner = <fs_out>-lifnr
            bankdetaildata  = ls_bankdetail
          TABLES
            return          = lt_return2.
      ELSE.
        "修改银行信息的日期
        ls_bankdetail_x-bankdetailvalidfrom = abap_true.
        ls_bankdetail_x-bankdetailvalidto = abap_true.
        CALL FUNCTION 'BAPI_BUPA_BANKDETAIL_CHANGE'
          EXPORTING
            businesspartner  = <fs_out>-lifnr
            bankdetailid     = lv_bkvid
            bankdetaildata   = ls_bankdetail
            bankdetaildata_x = ls_bankdetail_x
          TABLES
            return           = lt_return2.
      ENDIF.

      CLEAR:lv_bkvid.
      LOOP AT lt_return2 TRANSPORTING NO FIELDS WHERE type CA 'AEX'.
        EXIT.
      ENDLOOP.
      IF sy-subrc = 0.

        <fs_out>-zcode = 'E'.
        <fs_out>-zmsgl = '供应商创建成功,银行信息更新失败' .
        <fs_out>-color = 'C610' .

        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = abap_true.

        <fs_out>-zcode = 'S'.
        <fs_out>-zmsgl = '成功' .
        <fs_out>-color = 'C510' .
        CLEAR: lv_id .
        APPEND INITIAL LINE TO lt_zhrt006 ASSIGNING <fs_zhrt006> .

        TRY.
            CALL METHOD cl_system_uuid=>if_system_uuid_static~create_uuid_c32
              RECEIVING
                uuid = lv_id.

          CATCH cx_uuid_error .
            MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDTRY.

        <fs_zhrt006>-uuid  = lv_id .
        <fs_zhrt006>-lifnr = <fs_out>-lifnr .
        <fs_zhrt006>-pernr = <fs_out>-pernr .
        <fs_zhrt006>-sname = <fs_out>-sname .
        <fs_zhrt006>-begda = <fs_out>-begda .
        <fs_zhrt006>-endda = <fs_out>-endda .
        IF rb_c = 'X' .
          <fs_zhrt006>-ztask = 'C' .
        ELSEIF rb_m = 'X' .
          <fs_zhrt006>-ztask = 'M' .
        ELSEIF rb_d = 'X' .
          <fs_zhrt006>-ztask = 'D' .
        ENDIF .
        <fs_zhrt006>-zcode = <fs_out>-zcode .
        <fs_zhrt006>-zmsgl = <fs_out>-zmsgl .
        <fs_zhrt006>-zdate = sy-datum .
        <fs_zhrt006>-ztime = sy-uzeit .
        <fs_zhrt006>-zname = sy-uname .
      ENDIF.
**********ADD BY ZJ 20230807 E 更新银行信息





    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      <fs_out>-zcode = 'E'.
      LOOP AT lt_msg INTO ls_msg WHERE type CA 'AEX'.
        <fs_out>-zmsgl = <fs_out>-zmsgl && ls_msg-message && '/'.
      ENDLOOP.
      <fs_out>-color = 'C610' .

      CLEAR: lv_id .
      APPEND INITIAL LINE TO lt_zhrt006 ASSIGNING <fs_zhrt006> .

      TRY.
          CALL METHOD cl_system_uuid=>if_system_uuid_static~create_uuid_c32
            RECEIVING
              uuid = lv_id.

        CATCH cx_uuid_error .
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDTRY.

      <fs_zhrt006>-uuid  = lv_id .
      <fs_zhrt006>-lifnr = <fs_out>-lifnr .
      <fs_zhrt006>-pernr = <fs_out>-pernr .
      <fs_zhrt006>-sname = <fs_out>-sname .
      <fs_zhrt006>-begda = <fs_out>-begda .
      <fs_zhrt006>-endda = <fs_out>-endda .
      IF rb_c = 'X' .
        <fs_zhrt006>-ztask = 'C' .
      ELSEIF rb_m = 'X' .
        <fs_zhrt006>-ztask = 'M' .
      ELSEIF rb_d = 'X' .
        <fs_zhrt006>-ztask = 'D' .
      ENDIF .
      <fs_zhrt006>-zcode = <fs_out>-zcode .
      <fs_zhrt006>-zmsgl = <fs_out>-zmsgl .
      <fs_zhrt006>-zdate = sy-datum .
      <fs_zhrt006>-ztime = sy-uzeit .
      <fs_zhrt006>-zname = sy-uname .
    ENDIF.
  ENDLOOP .

  IF lt_zhrt006[] IS NOT INITIAL .
    MODIFY zhrt006 FROM TABLE lt_zhrt006 .
    COMMIT WORK .
  ENDIF .

  MESSAGE: '操作完成' TYPE 'S' .
ENDFORM.

 

posted @ 2023-08-07 14:42  阿胖的阿多  阅读(1617)  评论(0编辑  收藏  举报