ps_head结构如下
下面是封装好的form示例
*&---------------------------------------------------------------------*
*& Form frm_modify_customer
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> LS_HEAD
*& <-- LS_RETINFO
*&---------------------------------------------------------------------*
FORM frm_modify_customer USING ps_head TYPE zsds002
CHANGING ps_retinfo TYPE zsds008.
*--& 变量声明
DATA: lt_return_map TYPE mdg_bs_bp_msgmap_t.
DATA: lt_data TYPE cvis_ei_extern_t,
lt_return TYPE bapiretm,
ls_return TYPE bapireti,
ls_retmsg TYPE LINE OF bapiretct,
lv_text TYPE string.
DATA: ls_data LIKE LINE OF lt_data,
ls_bupa_role TYPE bus_ei_bupa_roles,
ls_relation TYPE burs_ei_extern,
ls_paadr TYPE bus_ei_bupa_address,
ls_company_view TYPE cmds_ei_company.
DATA: lv_guid TYPE guid_32,
lv_pguid LIKE but000-partner_guid,
lv_partner LIKE but000-partner.
DATA: ls_functions_st TYPE cmds_ei_functions,
ls_functions_t TYPE cmds_ei_functions_t,
ls_functions TYPE cmds_ei_cmd_functions,
ls_sales_data_st TYPE cmds_ei_sales,
ls_sales_data TYPE cmds_ei_cmd_sales,
ls_customer TYPE cmds_ei_extern,
ls_customers TYPE cmds_ei_main,
ls_company_code_st TYPE cmds_ei_company,
ls_company_code TYPE cmds_ei_cmd_company,
ls_phone TYPE bus_ei_bupa_telephone,
ls_fax TYPE bus_ei_bupa_fax,
ls_mail TYPE bus_ei_bupa_smtp,
ls_cphone TYPE cvis_ei_phone_str,
ls_cfax TYPE cvis_ei_fax_str,
ls_cmail TYPE cvis_ei_smtp_str,
ls_taxnumber TYPE bus_ei_bupa_taxnumber,
lt_taxnumber TYPE bus_ei_bupa_taxnumber_t,
ls_tax_ind TYPE cmds_ei_tax_ind,
ls_line TYPE tline,
ls_ret TYPE bapiret2,
lt_bankdetails TYPE bus_ei_bupa_bankdetail_t,
ls_bankdetails TYPE bus_ei_bupa_bankdetail,
lv_partner_guid TYPE but000-partner_guid,
ls_profile TYPE ukm_ei_bp_cms,
ls_segments TYPE ukm_ei_bp_cms_sgm.
DATA: lv_name_org1 TYPE but000-name_org1,
lv_name_org2 TYPE but000-name_org2.
DATA: lv_street TYPE adrc-street,
lv_str_suppl3 TYPE adrc-str_suppl3,
lv_location TYPE adrc-location.
DATA: lv_len TYPE i,
lv_spras TYPE kna1-spras.
" 合作伙伴名称
lv_partner = ps_head-partner.
lv_partner = |{ lv_partner ALPHA = IN }|.
" 操作标识 I 新建 U 更新 M 修改/新建
ls_data-partner-header-object_task = gc_m.
" bp guid
SELECT SINGLE
partner_guid
INTO lv_partner_guid
FROM but000
WHERE partner = lv_partner.
" bp guid
IF sy-subrc <> 0.
TRY.
CALL METHOD cl_system_uuid=>create_uuid_c32_static
RECEIVING
uuid = lv_guid.
CATCH cx_uuid_error .
ENDTRY.
ELSE.
lv_guid = lv_partner_guid.
ENDIF.
ls_data-partner-header-object_instance-bpartnerguid = lv_guid.
*--- BP基础数据------------------------------*
" 合作伙伴类别 3: 组 2: 组织
ls_data-partner-central_data-common-data-bp_control-category = '2'.
" 客户分组
ls_data-partner-central_data-common-data-bp_control-grouping = ps_head-bu_group.
" 标题 0001 女士 0002 先生 0003 公司
ls_data-partner-central_data-common-data-bp_centraldata-title_key = ps_head-title.
ls_data-partner-central_data-common-datax-bp_centraldata-title_key = gc_x.
" 搜索项(检索项)
ls_data-partner-central_data-common-data-bp_centraldata-searchterm1 = ps_head-bu_sort1.
ls_data-partner-central_data-common-datax-bp_centraldata-searchterm1 = gc_x.
" 公司名称(客户名称)
" 第1到40位存入BUT000-NAME_ORG1中,第41-80位存入BUT000-NAME_ORG2中
IF strlen( ps_head-name_org ) > 40.
lv_name_org1 = ps_head-name_org+0(40).
lv_len = strlen( ps_head-name_org ) MOD 40.
lv_name_org2 = ps_head-name_org+40(lv_len).
ELSE.
lv_name_org1 = ps_head-name_org.
ENDIF.
ls_data-partner-central_data-common-data-bp_organization-name1 = lv_name_org1.
ls_data-partner-central_data-common-data-bp_organization-name2 = lv_name_org2.
ls_data-partner-central_data-common-datax-bp_organization-name1 = gc_x.
ls_data-partner-central_data-common-datax-bp_organization-name2 = gc_x.
" 客户冻结
ls_data-partner-central_data-common-data-bp_centraldata-centralblock = ps_head-xblck.
ls_data-partner-central_data-common-datax-bp_centraldata-centralblock = gc_x.
*--- BP地址 -----------------------------*
" 地址操作标识
ls_paadr-task = gc_m.
" 地址 guid
SELECT SINGLE
address_guid
FROM but020
WHERE partner = @lv_partner
INTO @DATA(lv_addguid).
IF sy-subrc <> 0.
TRY.
CLEAR lv_guid.
CALL METHOD cl_system_uuid=>create_uuid_c32_static
RECEIVING
uuid = lv_guid.
CATCH cx_uuid_error .
ENDTRY.
ls_paadr-data_key-guid = lv_guid.
ELSE.
ls_paadr-data_key-guid = lv_addguid.
ENDIF.
" 街道
" 第1到60位存入ADRC-STREET中,第61-100位存入ADRC-STR_SUPPL3中,第101-120位存入ADRC-LOCATION中
IF strlen( ps_head-stras ) <= 60.
lv_street = ps_head-stras.
ELSEIF strlen( ps_head-stras ) > 60 AND strlen( ps_head-stras ) <= 100.
lv_street = ps_head-stras+0(60).
lv_len = strlen( ps_head-stras ) MOD 60.
lv_str_suppl3 = ps_head-stras+60(lv_len).
ELSE.
lv_street = ps_head-stras+0(60).
lv_str_suppl3 = ps_head-stras+60(40).
lv_len = strlen( ps_head-stras ) MOD 100.
lv_location = ps_head-stras+100(lv_len).
ENDIF.
ls_paadr-data-postal-data-street = lv_street.
ls_paadr-data-postal-data-str_suppl3 = lv_str_suppl3.
ls_paadr-data-postal-data-location = lv_location.
ls_paadr-data-postal-datax-street = gc_x.
ls_paadr-data-postal-datax-str_suppl3 = gc_x.
ls_paadr-data-postal-datax-location = gc_x.
" 邮政编码
ls_paadr-data-postal-data-postl_cod1 = ps_head-pstlz.
ls_paadr-data-postal-datax-postl_cod1 = gc_x.
" 城市
ls_paadr-data-postal-data-city = ps_head-ort01.
ls_paadr-data-postal-datax-city = gc_x.
" 国家
ls_paadr-data-postal-data-country = ps_head-land1.
ls_paadr-data-postal-datax-country = gc_x.
" 地区(省份)
ls_paadr-data-postal-data-region = ps_head-regio.
ls_paadr-data-postal-datax-region = gc_x.
" 语言 (外部传入2位外码转换为1位内码)
CALL FUNCTION 'CONVERSION_EXIT_ISOLA_INPUT'
EXPORTING
input = ps_head-spras
IMPORTING
output = lv_spras.
ls_paadr-data-postal-data-langu = lv_spras."语言代码
ls_paadr-data-postal-datax-langu = gc_x.
" 电话
ls_phone-contact-task = gc_m.
ls_phone-contact-data-telephone = ps_head-tel_number.
ls_phone-contact-datax-telephone = gc_x.
APPEND ls_phone TO ls_paadr-data-communication-phone-phone.
" 传真
ls_fax-contact-task = gc_m.
ls_fax-contact-data-fax = ps_head-fax_number. "传真
ls_fax-contact-datax-fax = gc_x.
APPEND ls_fax TO ls_paadr-data-communication-fax-fax.
" 邮箱
ls_mail-contact-task = gc_m.
ls_mail-contact-data-e_mail = ps_head-smtp_addr.
ls_mail-contact-datax-e_mail = gc_x.
APPEND ls_mail TO ls_paadr-data-communication-smtp-smtp.
ls_paadr-currently_valid = abap_true.
APPEND ls_paadr TO ls_data-partner-central_data-address-addresses.
*-------------- Customer / Header ---------------------------------*
" 客户抬头操作标识
IF lv_partner_guid IS INITIAL AND ps_head-zoperate = 'I'. " BP中没有合作伙伴则新建
ls_data-ensure_create-create_customer = abap_true.
ls_data-customer-header-object_task = 'I'.
ELSE.
IF ps_head-partner IS NOT INITIAL AND ps_head-zoperate = 'I'. " BP中有合作伙伴但没有客户则新建
ls_data-ensure_create-create_customer = abap_true.
ls_data-customer-header-object_task = 'I'.
ELSE. " BP中有合作伙伴且有客户则更新
ls_data-customer-header-object_task = 'U'.
ENDIF.
ENDIF.
" 客户编码
ls_data-customer-header-object_instance-kunnr = lv_partner.
" 税号信息
ls_taxnumber-task = gc_m.
ls_taxnumber-data_key-taxtype = ps_head-taxtype.
ls_taxnumber-data_key-taxnumxl = ps_head-taxnumxl.
APPEND ls_taxnumber TO lt_taxnumber .
ls_data-partner-central_data-taxnumber-current_state = 'X' .
ls_data-partner-central_data-taxnumber-taxnumbers = lt_taxnumber .
*----& 角色
IF ps_head-sales[] IS NOT INITIAL.
ls_bupa_role-task = gc_m.
ls_bupa_role-data_key = 'FLCU01'. " FI 客户
APPEND ls_bupa_role TO ls_data-partner-central_data-role-roles.
ENDIF.
IF ps_head-company[] IS NOT INITIAL.
ls_bupa_role-task = gc_m.
ls_bupa_role-data_key = 'FLCU00'. " 客户
APPEND ls_bupa_role TO ls_data-partner-central_data-role-roles.
ENDIF.
*----& 银行段
IF ps_head-bank[] IS NOT INITIAL.
" 检查银行更新/新建
SELECT
bkvid, " 银行明细标识
partner " 合作伙伴编号
FROM but0bk
WHERE
partner = @lv_partner
INTO TABLE @DATA(lt_but0bk).
LOOP AT ps_head-bank INTO DATA(ls_bank).
READ TABLE lt_but0bk TRANSPORTING NO FIELDS WITH KEY bkvid = ls_bank-bkvid.
IF sy-subrc = 0.
ls_bankdetails-task = gc_u.
ELSE.
ls_bankdetails-task = gc_i.
ENDIF.
" 序号
ls_bankdetails-data_key = ls_bank-bkvid.
" 银行国家
ls_bankdetails-data-bank_ctry = ls_bank-banks.
ls_bankdetails-datax-bank_ctry = gc_x.
" 银行代码
ls_bankdetails-data-bank_key = ls_bank-bankl.
ls_bankdetails-datax-bank_key = gc_x.
" 银行名称
ls_bankdetails-data-bankaccountname = ls_bank-banka.
ls_bankdetails-datax-bankaccountname = gc_x.
" 银行账户,超过18位时填入参考字段
ls_bankdetails-data-bank_acct = ls_bank-bankn.
ls_bankdetails-datax-bank_acct = gc_x.
ls_bankdetails-data-bank_ref = ls_bank-bkref.
ls_bankdetails-datax-bank_ref = gc_x.
" 是否默认
ls_bankdetails-data-coll_auth = ls_bank-xezer.
ls_bankdetails-datax-coll_auth = gc_x.
APPEND ls_bankdetails TO lt_bankdetails.
PERFORM frm_create_bank USING ls_bankdetails .
ENDLOOP.
ls_data-partner-central_data-bankdetail-bankdetails = lt_bankdetails .
ENDIF.
*----& 销售段
LOOP AT ps_head-sales INTO DATA(ls_sales).
ls_sales_data_st-task = gc_m.
ls_sales_data_st-data_key-spart = ls_sales-spart."产品组
ls_sales_data_st-data_key-vkorg = ls_sales-vkorg."销售组织
ls_sales_data_st-data_key-vtweg = ls_sales-vtweg."分销渠道
" 销售地区
ls_sales_data_st-data-bzirk = ls_sales-bzirk.
ls_sales_data_st-datax-bzirk = gc_x.
" 销售办事处
ls_sales_data_st-data-vkbur = ls_sales-vkbur.
ls_sales_data_st-datax-vkbur = gc_x.
" 销售组
ls_sales_data_st-data-vkgrp = ls_sales-kdgrp.
ls_sales_data_st-datax-vkgrp = gc_x.
" 货币
ls_sales_data_st-data-waers = ls_sales-waers.
ls_sales_data_st-datax-waers = gc_x.
" 定价过程
ls_sales_data_st-data-kalks = ls_sales-kalks.
ls_sales_data_st-datax-kalks = gc_x.
" 装运条件 默认01
IF ls_sales-vsbed IS INITIAL.
ls_sales-vsbed = '01'.
ENDIF.
ls_sales_data_st-data-vsbed = ls_sales-vsbed.
ls_sales_data_st-datax-vsbed = gc_x.
" 与 POD 处理相关
ls_sales_data_st-data-podkz = ls_sales-podkz .
ls_sales_data_st-datax-podkz = gc_x.
" 国际贸易条款1
* IF ls_sales-inco1 IS NOT INITIAL.
ls_sales_data_st-data-inco1 = ls_sales-inco1.
ls_sales_data_st-datax-inco1 = gc_x.
" 国际贸易条款位置 1
ls_sales_data_st-data-inco2_l = ls_sales-inco2_l.
ls_sales_data_st-datax-inco2_l = gc_x.
" 付款条件代码
ls_sales_data_st-data-zterm = ls_sales-zterm.
ls_sales_data_st-datax-zterm = gc_x.
" 客户的科目分配组
ls_sales_data_st-data-ktgrd = ls_sales-ktgrd.
ls_sales_data_st-datax-ktgrd = gc_x.
" 销售范围订单冻结
ls_sales_data_st-data-aufsd = ls_sales-aufsd.
ls_sales_data_st-datax-aufsd = gc_x.
*------& 销售段->合作伙伴职能段
LOOP AT ls_sales-partner INTO DATA(ls_partner).
" 合作伙伴外码转内码
CALL FUNCTION 'CONVERSION_EXIT_PARVW_INPUT'
EXPORTING
input = ls_partner-parvw
IMPORTING
output = ls_partner-parvw.
ls_functions_st-data_key-parvw = ls_partner-parvw.
ls_functions_st-data_key-parza = ls_partner-parza.
ls_functions_st-data-partner = ls_partner-kunn2.
ls_functions_st-datax-partner = gc_x.
APPEND ls_functions_st TO ls_functions_t.
ENDLOOP.
ls_functions-functions = ls_functions_t.
ls_sales_data_st-functions = ls_functions.
IF ls_sales-partner IS NOT INITIAL.
ls_sales_data_st-functions-current_state = gc_x.
ENDIF.
APPEND ls_sales_data_st TO ls_sales_data-sales.
CLEAR: ls_sales_data_st,ls_functions_t[],ls_functions.
ENDLOOP.
ls_data-customer-sales_data = ls_sales_data.
*----税段
LOOP AT ps_head-tax INTO DATA(ls_tax).
ls_tax_ind-task = gc_m.
ls_tax_ind-data_key-tatyp = ls_tax-tatyp." 税类别
ls_tax_ind-data_key-aland = ls_tax-aland." 国家
" 客户税分类
ls_tax_ind-data-taxkd = ls_tax-takld.
ls_tax_ind-datax-taxkd = gc_x.
APPEND ls_tax_ind TO ls_data-customer-central_data-tax_ind-tax_ind.
ENDLOOP.
*----公司视图段
LOOP AT ps_head-company INTO DATA(ls_company).
ls_company_view-task = gc_m.
ls_company_view-data_key = ls_company-bukrs. " 公司代码
" 统驭科目
ls_company_view-data-akont = ls_company-akont.
ls_company_view-datax-akont = gc_x.
" 付款条件
ls_company_view-data-zterm = ls_company-zterm.
ls_company_view-datax-zterm = gc_x.
" 排序代码
ls_company_view-data-zuawa = ls_company-zuawa.
ls_company_view-datax-zuawa = gc_x.
" 对公司代码过帐冻结
ls_company_view-data-sperr = ls_company-sperr.
ls_company_view-datax-sperr = gc_x.
" K3客户编码
ls_company_view-data-altkn = ls_company-altkn.
ls_company_view-datax-altkn = gc_x.
APPEND ls_company_view TO ls_data-customer-company_data-company.
ENDLOOP.
" 校验BP数据
CALL METHOD cl_md_bp_maintain=>validate_single
EXPORTING
i_data = ls_data "lt_data
IMPORTING
et_return_map = lt_return_map. "lt_return.
APPEND ls_data TO lt_data.
" 创建BP
IF lt_return_map IS INITIAL.
CALL METHOD cl_md_bp_maintain=>maintain
EXPORTING
i_data = lt_data
IMPORTING
e_return = lt_return.
" 异常返回校验
LOOP AT lt_return INTO ls_return.
LOOP AT ls_return-object_msg INTO ls_retmsg WHERE type CA 'AE'.
CLEAR lv_text.
MESSAGE ID ls_retmsg-id TYPE 'S' NUMBER ls_retmsg-number INTO lv_text
WITH ls_retmsg-message_v1 ls_retmsg-message_v2 ls_retmsg-message_v3 ls_retmsg-message_v4.
CONCATENATE ps_retinfo-message lv_text INTO ps_retinfo-message.
ENDLOOP.
ENDLOOP.
IF ps_retinfo-message IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
IF ps_head-partner IS INITIAL.
ps_retinfo-kunnr = sy-msgv1.
ELSE.
ps_retinfo-kunnr = ps_head-partner.
ENDIF.
ps_retinfo-kunnr = |{ ps_retinfo-kunnr ALPHA = IN }|.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ELSE.
LOOP AT lt_return_map INTO DATA(ls_return_map) WHERE type CA 'AE'.
CLEAR lv_text.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = ls_return_map-id
msgnr = ls_return_map-number
msgv1 = ls_return_map-message_v1
msgv2 = ls_return_map-message_v2
msgv3 = ls_return_map-message_v3
msgv4 = ls_return_map-message_v4
IMPORTING
message_text_output = lv_text.
CONCATENATE ps_retinfo-message lv_text INTO ps_retinfo-message.
ENDLOOP.
ENDIF.
IF ps_retinfo-message IS NOT INITIAL.
ps_retinfo-code = gc_e.
ps_retinfo-message = TEXT-m09 && ps_retinfo-message.
ELSE.
ps_retinfo-code = gc_s.
ps_retinfo-message = TEXT-m08.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_create_bank
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> LS_BANKDETAILS
*&---------------------------------------------------------------------*
FORM frm_create_bank USING ps_bank_detail TYPE bus_ei_bupa_bankdetail.
DATA: ls_return TYPE bapiret2,
lv_ret_bank_ctry TYPE bapi1011_key-bank_ctry,
lv_ret_bank_key TYPE bapi1011_key-bank_key,
lv_bank_ctry TYPE bapi1011_key-bank_ctry,
lv_bank_key TYPE bapi1011_key-bank_key,
ls_bank_address TYPE bapi1011_address,
ls_bank_addressx TYPE bapi1011_addressx.
CLEAR: ls_return,lv_ret_bank_ctry,lv_ret_bank_key .
lv_bank_key = ps_bank_detail-data-bank_key.
lv_bank_ctry = ps_bank_detail-data-bank_ctry.
ls_bank_address-bank_name = ps_bank_detail-data-bankaccountname.
ls_bank_addressx-bank_name = 'X' .
SELECT COUNT(*)
FROM bnka
WHERE banks = lv_bank_ctry
AND bankl = lv_bank_key.
IF sy-subrc <> 0 . "银行不存在
CALL FUNCTION 'BAPI_BANK_CREATE'
EXPORTING
bank_ctry = lv_bank_ctry
bank_key = lv_bank_key
bank_address = ls_bank_address
IMPORTING
return = ls_return
bankcountry = lv_ret_bank_ctry
bankkey = lv_ret_bank_key.
IF lv_bank_key IS NOT INITIAL AND lv_ret_bank_ctry IS NOT INITIAL .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
WAIT UP TO 1 SECONDS .
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.
ENDIF .
ENDFORM.