report z_sd_kna_part_price_m.
"根据客户+物料 创建价格
*data:gs_head like zstru_sd_price_condition.
types: begin of ty_head,
matnr type matnr,
kunnr type kunnr,
waers type waers, "货币码
cond_value type bapicurext, "货币金额
valid_from type sy-datum, "开始日期
valid_to type sy-datum, "截止日期
end of ty_head.
data gs_head type ty_head.
data gt_head type table of ty_head.
"等级数量
types: begin of ty_litem,
matnr type matnr,
kunnr type kunnr,
waers type waers, "货币码
cond_value type bapicurext, "货币金额
menge type kstbm, "数量
end of ty_litem.
data gs_item type ty_litem.
data gt_item type table of ty_litem.
data num type i.
data: cond_no_str type n length 9,
cond_no type c length 10. "条件号
data cond_count type i. "条件的序列号
data:la908 type a908 .
data:l_key like bapicondct-varkey.
data:lpdate type sy-datum.
data:lkunnr like kna1-kunnr,
lmatnr like mara-matnr.
data:lflg type abap_bool.
data: h_varkey(255),
h_cond_unit type meins .
data: lt_bapicondct type bapicondct occurs 0 , "
wa_bapicondct type bapicondct,
lt_bapicondhd type bapicondhd occurs 0 , "
wa_bapicondhd type bapicondhd,
lt_bapicondit type bapicondit occurs 0,
wa_bapicondit type bapicondit,
lt_bapicondqs type bapicondqs occurs 0,
wa_bapicondqs type bapicondqs,
lt_bapicondvs type bapicondvs occurs 0,
wa_bapicondvs type bapicondvs.
data:litemret like objdesc,
itemsmsg like table of objdesc,
l_message type msgtxt.
data: lt_bapiret2 like standard table of bapiret2 with header line .
data: ret type bapiret2 .
data : ev_message type msgtxt,
ev_status type c,
cond_type type c length 4 value 'ZPS1', "条件类型
table_no type n length 3 value '908', "条件表
operation type c length 4 value '009', ""004修改 _009创建 003 删除
applicatio type c value 'V', "应用类型
cond_usage type c value 'A'. "条件表用途
data: lt_bapiknumhs type bapiknumhs occurs 0 .
data: lt_mem_initial type cnd_mem_initial occurs 0 .
lflg = abap_true.
ev_status = 'S'.
ev_message = '成功'.
gs_head-matnr = 'A123'.
gs_head-kunnr = '1000000'.
gs_head-waers = 'CNY'. "货币码
gs_head-cond_value = 10. "货币金额
gs_head-valid_from = sy-datum. "开始日期
gs_head-valid_to = '99991231'. "截止日期
append gs_head to gt_head.
clear gs_head.
gs_head-matnr = 'A05'.
gs_head-kunnr = '1000000'.
gs_head-waers = 'CNY'. "货币码
gs_head-cond_value = 10. "货币金额
gs_head-valid_from = sy-datum. "开始日期
gs_head-valid_to = '99991231'. "截止日期
append gs_head to gt_head.
clear gs_head.
"-------------------------------------------------------------
gs_item-matnr = 'A05'.
gs_item-kunnr = '1000000'.
gs_item-waers = 'CNY'. "货币码
gs_item-cond_value = 10. "货币金额
gs_item-menge = 100. "数量
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gs_item-kunnr
importing
output = gs_item-kunnr.
append gs_item to gt_item.
clear gs_item.
gs_item-matnr = 'A05'.
gs_item-kunnr = '1000000'.
gs_item-waers = 'CNY'. "货币码
gs_item-cond_value = 5. "货币金额
gs_item-menge = 1000. "数量
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gs_item-kunnr
importing
output = gs_item-kunnr.
append gs_item to gt_item.
clear gs_item.
gs_item-matnr = 'A123'.
gs_item-kunnr = '1000000'.
gs_item-waers = 'CNY'. "货币码
gs_item-cond_value = 10. "货币金额
gs_item-menge = 100. "数量
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gs_item-kunnr
importing
output = gs_item-kunnr.
append gs_item to gt_item.
clear gs_item.
gs_item-matnr = 'A123'.
gs_item-kunnr = '1000000'.
gs_item-waers = 'CNY'. "货币码
gs_item-cond_value = 10. "货币金额
gs_item-menge = 100. "数量
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gs_item-kunnr
importing
output = gs_item-kunnr.
append gs_item to gt_item.
clear gs_item.
"---------------------------------------------------------------
clear: cond_count,cond_no,cond_no_str.
loop at gt_head into gs_head.
if gs_head-valid_to < gs_head-valid_from or gs_head-valid_from < sy-datum.
lflg = abap_false.
continue.
endif.
clear lt_bapicondct[].
clear wa_bapicondct.
clear lt_bapicondhd[].
clear wa_bapicondhd.
clear lt_bapicondit[].
clear wa_bapicondit.
clear: lt_bapicondqs[],wa_bapicondqs.
clear: lt_bapiret2[].
if cond_usage = 'A' and table_no = '908'.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gs_head-kunnr
importing
output = gs_head-kunnr.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gs_head-matnr
importing
output = gs_head-matnr.
concatenate gs_head-kunnr gs_head-matnr into h_varkey.
endif.
select single meins from mara
into h_cond_unit where matnr = gs_head-matnr.
clear cond_count.
cond_count = cond_count + 1.
cond_no_str = cond_no_str + 1.
cond_no = cond_no_str.
concatenate '$' cond_no into cond_no.
wa_bapicondct-table_no = table_no."'941'.
wa_bapicondct-applicatio = applicatio."'V'.
wa_bapicondct-cond_usage = cond_usage."'A'. "条件表用途 ‘A' 定价
wa_bapicondct-cond_type = cond_type."I_KSCHL. "定价条件
wa_bapicondct-operation = operation."'009'. "消息功能 : 003 DEL ; 004 MODIFY ;005 REPLACE 009 INITIAL
wa_bapicondct-varkey = h_varkey."客户+物料 具体的拼接根据业务需要
wa_bapicondct-valid_to = gs_head-valid_to.
wa_bapicondct-valid_from = gs_head-valid_from.
wa_bapicondct-cond_no = cond_no. "条件记录号 $000000001
append wa_bapicondct to lt_bapicondct.
*KONH
wa_bapicondhd-operation = operation."'009'.
wa_bapicondhd-cond_no = cond_no. "条件记录号 $000000001
wa_bapicondhd-created_by = sy-uname. "创建对象的人员名称
wa_bapicondhd-creat_date = sy-datum. "记录的创建日期
wa_bapicondhd-cond_usage = cond_usage."'A'.
wa_bapicondhd-table_no = table_no."'941'.
wa_bapicondhd-applicatio = applicatio."'V'.
wa_bapicondhd-cond_type = cond_type."I_KSCHL. "
wa_bapicondhd-varkey = h_varkey. "客户+物料 具体的拼接根据业务需要
wa_bapicondhd-valid_from = gs_head-valid_from.
wa_bapicondhd-valid_to = gs_head-valid_to.
append wa_bapicondhd to lt_bapicondhd .
*KONP
wa_bapicondit-operation = operation."'009'.
wa_bapicondit-cond_no = cond_no. "条件记录号 $000000001
wa_bapicondit-cond_count = cond_count. "条件序列号
wa_bapicondit-applicatio = applicatio."'V'.
wa_bapicondit-cond_type = cond_type."I_KSCHL. "
wa_bapicondit-scaletype = 'A'. "A:基础等级
wa_bapicondit-scalebasin = 'C'. "'C'. 存在数量等级
wa_bapicondit-scale_qty = '0'. "条件等级数量
wa_bapicondit-cond_p_unt = '1'. "条件定价单位
wa_bapicondit-cond_unit = h_cond_unit. " 物料主数据中的物料单位
wa_bapicondit-calctypcon = 'C'. "C:数量 A:百分比 ...
wa_bapicondit-cond_value = gs_head-cond_value. "货币金额
wa_bapicondit-condcurr = gs_head-waers.
append wa_bapicondit to lt_bapicondit.
*设置等级数量
loop at gt_item into gs_item where matnr eq gs_head-matnr and kunnr eq gs_head-kunnr. "数量等级金额>0
select single * into la908
from a908
where datbi > gs_head-valid_from and datab <= gs_head-valid_from and
matnr = gs_head-matnr and kunnr = gs_head-kunnr and kschl = cond_type and kappl = applicatio.
num = num + 1.
wa_bapicondqs-operation = ''.
wa_bapicondqs-cond_no = cond_no. ""条件记录号 $000000001
wa_bapicondqs-cond_count = cond_count. "条件序列号
wa_bapicondqs-line_no = num. "行等级的当前号码
wa_bapicondqs-scale_qty = gs_item-menge. "条件等级数量
wa_bapicondqs-cond_unit = h_cond_unit. "条件单位
wa_bapicondqs-currency = gs_item-cond_value. "BAPIs 的货币金额(带有 9 个小数位)
wa_bapicondqs-condcurr = gs_head-waers. "比率单位(货币或百分数)
append wa_bapicondqs to lt_bapicondqs.
clear wa_bapicondqs .
endloop.
clear: num.
if lflg = abap_true.
call function 'BAPI_PRICES_CONDITIONS'
tables
ti_bapicondct = lt_bapicondct
ti_bapicondhd = lt_bapicondhd
ti_bapicondit = lt_bapicondit
ti_bapicondqs = lt_bapicondqs
ti_bapicondvs = lt_bapicondvs
to_bapiret2 = lt_bapiret2
to_bapiknumhs = lt_bapiknumhs
to_mem_initial = lt_mem_initial
exceptions
update_error = 1
others = 2.
if sy-subrc = 0.
loop at lt_bapiret2 where type eq 'A' or type eq 'E'.
l_message = l_message && lt_bapiret2-message.
endloop.
if sy-subrc eq 0.
ev_status = 'E'.
ev_message = '客户价格更新失败:' && l_message.
litemret-object = h_varkey.
litemret-text = 'E'.
call function 'BAPI_TRANSACTION_ROLLBACK'.
else.
litemret-object = h_varkey.
litemret-text = 'S'.
call function 'BAPI_TRANSACTION_COMMIT'.
endif.
append litemret to itemsmsg.
else.
ev_status = 'E'.
ev_message = '客户价格更新失败:' && l_message.
litemret-object = h_varkey.
litemret-text = ev_status.
append litemret to itemsmsg.
endif.
else.
ev_status = 'E'.
ev_message = '客户价格更新失败:请检查条件表是否正确'.
litemret-object = h_varkey.
litemret-text = ev_status.
append litemret to itemsmsg.
endif.
call function 'BAPI_TRANSACTION_COMMIT'
importing
return = ret.
endloop.
执行结果
VK11 查看