SD BAPI–SD_SALESDOCUMENT_CREATE 销售订单退/换货
FUNCTION zsdfu004.
*“----------------------------------------------------------------------
"“本地接口:
*” IMPORTING
*” VALUE(I_CGSQD) TYPE CHAR20 OPTIONAL
*" VALUE(I_ZNAME) TYPE CHAR12 OPTIONAL
*" VALUE(I_ZTYPE) TYPE CHAR01 OPTIONAL
*" VALUE(I_ZYY) TYPE CHAR200 OPTIONAL
*" EXPORTING
*" VALUE(E_TYPE) TYPE BAPI_MTYPE
*" VALUE(E_MSG) TYPE BAPI_MSG
*" TABLES
*" ITEM STRUCTURE ZSD004 OPTIONAL
*"----------------------------------------------------------------------
DATA: it_header_in LIKE bapisdhd1.
DATA: it_header_inx LIKE bapisdhd1x.
DATA: it_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
DATA: it_items_in LIKE bapisditm OCCURS 0 WITH HEADER LINE. "通讯字段: 销售和分销凭证项目
DATA: it_items_in2 LIKE bapisditm OCCURS 0 WITH HEADER LINE. "通讯字段: 销售和分销凭证项目
DATA: it_items_inx LIKE bapisditmx OCCURS 0 WITH HEADER LINE. "通讯字段: 销售和分销凭证项目
DATA: it_schedules_in LIKE bapischdl OCCURS 0 WITH HEADER LINE. "维护 SD 凭证计划行的通讯字段
DATA: it_schedules_inx LIKE bapischdlx OCCURS 0 WITH HEADER LINE. "维护 SD 凭证计划行的通讯字段
DATA: it_partners LIKE bapiparnr OCCURS 0 WITH HEADER LINE. "通讯字段: SD单据合作伙伴
DATA: order_conditions_in LIKE bapicond OCCURS 0 WITH HEADER LINE.
DATA: order_conditions_inx LIKE bapicondx OCCURS 0 WITH HEADER LINE.
DATA: order_conditions_in2 LIKE bapicond OCCURS 0 WITH HEADER LINE.
DATA: order_conditions_inx2 LIKE bapicondx OCCURS 0 WITH HEADER LINE.
DATA: order_text LIKE bapisdtext OCCURS 0 WITH HEADER LINE.
DATA: lt_bapiparex LIKE bapiparex OCCURS 0 WITH HEADER LINE.
DATA: ls_zbape_vbap TYPE bape_vbap.
DATA: ls_zbape_vbapx TYPE bape_vbapx.
DATA: salesdocument1 LIKE bapivbeln-vbeln.
DATA: salesdocument2 LIKE bapivbeln-vbeln.
DATA:BEGIN OF is_vbak,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
auart TYPE vbak-auart,
kunnr TYPE vbak-kunnr,
waerk TYPE vbak-waerk,
vkorg TYPE vbak-vkorg,
vtweg TYPE vbak-vtweg,
spart TYPE vbak-spart,
END OF is_vbak.
DATA:it_vbak LIKE TABLE OF is_vbak.
DATA: BEGIN OF is_vbkd,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
bstkd TYPE vbkd-bstkd, "客户PO号
END OF is_vbkd.
DATA:it_vbkd LIKE TABLE OF is_vbkd.
DATA:it_item LIKE TABLE OF zsd004 WITH HEADER LINE.
DATA:lv_error.
DATA:lv_mess(200).
DATA:lv_type1.
DATA:lv_type2.
DATA:lv_mess1.
DATA:lv_mess2.
DATA:cs_vbak TYPE vbak.
DATA:cs_vbap TYPE vbap.
DATA:cs_vbkd TYPE vbkd.
DATA:lv_bstkd TYPE vbkd-bstkd.
DATA:lv_posnr TYPE vbap-posnr.
DATA lv_knumv LIKE vbak-knumv.
DATA:cs_konv TYPE v_konv_cds.
***1)数据检查
IF i_cgsqd = ‘’.
lv_error = ‘X’.
lv_mess = ‘缺少OA单号’.
ENDIF.
IF i_zname = ‘’.
lv_error = ‘X’.
lv_mess = lv_mess && ‘/’ && ‘缺少申请者’.
ENDIF.
IF i_ztype = ‘’.
lv_error = ‘X’.
lv_mess = lv_mess && ‘/’ && ‘缺少业务类型’.
ENDIF.
IF i_ztype = ‘T’ OR i_ztype = ‘H’.
ELSE.
lv_error = ‘X’.
lv_mess = lv_mess && ‘/’ && ‘业务类型不正确’.
ENDIF.
IF item[] IS INITIAL.
lv_error = ‘X’.
lv_mess = lv_mess && ‘/’ && ‘销售订单行为空’.
ENDIF.
IF lv_error = ‘’.
LOOP AT item.
CLEAR:it_item,is_vbak,is_vbkd.
it_item-vbeln = |{ item-vbeln ALPHA = IN }|.
it_item-posnr = |{ item-posnr ALPHA = IN }|.
it_item-matnr = item-matnr.
it_item-menge = item-menge.
APPEND it_item.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF cs_vbap FROM vbap WHERE vbeln = it_item-vbeln AND posnr = it_item-posnr.
IF sy-subrc <> 0.
lv_error = 'X'.
lv_mess = lv_mess && '/' && 'SO行:' && item-vbeln && '&' && item-posnr && '不存在'.
EXIT.
ELSE.
IF cs_vbap-matnr <> item-matnr.
lv_error = 'X'.
lv_mess = lv_mess && '/' && 'SO行:' && item-vbeln && '&' && item-posnr && '物料不一致'.
EXIT.
ELSE.
SELECT SINGLE * INTO cs_vbak FROM vbak WHERE vbeln = it_item-vbeln.
MOVE-CORRESPONDING cs_vbak TO is_vbak.
APPEND is_vbak TO it_vbak.
SELECT SINGLE * INTO cs_vbkd FROM vbkd WHERE vbeln = it_item-vbeln AND posnr = '000000'.
is_vbkd-vbeln = it_item-vbeln.
is_vbkd-posnr = it_item-posnr.
is_vbkd-bstkd = cs_vbkd-bstkd.
is_vbkd-vbeln = item-vbeln.
APPEND is_vbkd TO it_vbkd.
ENDIF.
ENDIF.
ENDLOOP.
SORT it_vbak BY auart kunnr waerk vkorg vtweg spart.
DELETE ADJACENT DUPLICATES FROM it_vbak COMPARING auart kunnr waerk vkorg vtweg spart.
IF lines( it_vbak ) > 1.
lv_error = ‘X’.
lv_mess = lv_mess && ‘订单抬头不一致(订单类型 or 客户 or 销售范围 or 币种)’.
ELSEIF lines( it_vbak ) = 1.
IF cs_vbak-auart <> ‘ZSP’.
lv_error = ‘X’.
lv_mess = lv_mess && ‘不是备件销售订单’.
ENDIF.
ENDIF.
SORT it_vbkd BY vbeln.
DELETE ADJACENT DUPLICATES FROM it_vbkd COMPARING vbeln.
LOOP AT it_vbkd INTO is_vbkd.
is_vbkd-vbeln = |{ is_vbkd-vbeln ALPHA = OUT }|.
IF sy-tabix = 1.
lv_bstkd = is_vbkd-vbeln.
ELSE.
lv_bstkd = lv_bstkd && ‘’ && ‘/’ && is_vbkd-vbeln.
ENDIF.
ENDLOOP.
lv_bstkd = lv_bstkd && ‘//’ && sy-datum+4(4).
ENDIF.
***2)创建备件退货销售订单
IF lv_error = ‘’.
CLEAR:it_header_in,it_header_inx,salesdocument1,lv_posnr.
REFRESH:it_return,it_items_in,it_items_in2,it_items_inx,it_schedules_in,it_schedules_inx,it_partners,
order_conditions_in,order_conditions_inx,order_conditions_in2,order_conditions_inx2,lt_bapiparex,order_text.
"2.1)抬头赋值
it_header_in-doc_type = 'ZRE2'. "订单类型 备件退货订单类型
it_header_in-sales_org = cs_vbak-vkorg. "销售组织
it_header_in-distr_chan = cs_vbak-vtweg. "分销渠道
it_header_in-division = cs_vbak-spart. "产品组
it_header_in-purch_no_c = 'T:' && lv_bstkd. "采购订单号
it_header_in-ref_1 = lv_bstkd. "您的参考
it_header_in-doc_date = sy-datum. "凭证日期
it_header_in-currency = cs_vbak-waerk. "币种
"2.2)合作伙伴
it_partners-partn_role = 'AG'.
it_partners-partn_numb = cs_vbak-kunnr.
it_partners-itm_number = space.
APPEND it_partners .
it_partners-partn_role = 'RG'.
it_partners-partn_numb = cs_vbak-kunnr.
it_partners-itm_number = space.
APPEND it_partners .
it_partners-partn_role = 'RE'.
it_partners-partn_numb = cs_vbak-kunnr.
it_partners-itm_number = space.
APPEND it_partners .
it_partners-partn_role = 'WE'.
it_partners-partn_numb = cs_vbak-kunnr.
it_partners-itm_number = space.
APPEND it_partners .
"2.3)行项目
"2.3.1)行项目
LOOP AT it_item.
CLEAR:it_items_in,it_items_inx.
ADD 10 TO lv_posnr.
it_item-posnr1 = lv_posnr.
MODIFY it_item TRANSPORTING posnr1.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF cs_vbap FROM vbap WHERE vbeln = it_item-vbeln AND posnr = it_item-posnr.
it_items_in-itm_number = lv_posnr. "销售订单行项目号
it_items_in-material_long = it_item-matnr. "物料
it_items_in-sales_unit = cs_vbap-vrkme. "单位
it_items_in-plant = cs_vbap-werks. "工厂
it_items_in-target_qty = it_item-menge. "数量
it_items_in-target_qu = cs_vbap-vrkme. "单位
it_items_in-purch_no_c = 'T:' && |{ it_item-vbeln ALPHA = OUT }| && '/' && |{ it_item-posnr ALPHA = OUT }|. "行项目参考
APPEND it_items_in.
IF i_ztype = 'H'.
it_items_in-purch_no_c = 'H:' && |{ it_item-vbeln ALPHA = OUT }| && '/' && |{ it_item-posnr ALPHA = OUT }|. "行项目参考
DO 10 TIMES.
SELECT COUNT(*) FROM vbkd WHERE bstkd = it_items_in-purch_no_c.
IF sy-subrc = 0.
it_items_in-purch_no_c = 'H' && it_items_in-purch_no_c.
ELSE.
EXIT.
ENDIF.
ENDDO.
APPEND it_items_in TO it_items_in2.
ENDIF.
it_items_inx-updateflag = 'M'.
it_items_inx-itm_number = lv_posnr. "销售订单行项目号
it_items_inx-material_long = 'X'. "物料
** it_items_inx-sales_unit = ‘X’. "单位
it_items_inx-plant = 'X'. "工厂
it_items_inx-target_qty = 'X'. "数量
it_items_inx-target_qu = 'X'. "单位
it_items_inx-purch_no_c = 'X'. "行项目参考
APPEND it_items_inx.
"2.3.2)增强字段
CLEAR: ls_zbape_vbap, ls_zbape_vbapx.
ls_zbape_vbap = VALUE #(
posnr = lv_posnr
zprono = cs_vbap-zprono
).
ls_zbape_vbapx = VALUE #(
posnr = lv_posnr
zprono = 'X'
).
CLEAR:lt_bapiparex.
lt_bapiparex-structure = 'BAPE_VBAP'.
lt_bapiparex+30(960) = ls_zbape_vbap.
APPEND lt_bapiparex TO lt_bapiparex.
CLEAR:lt_bapiparex.
lt_bapiparex-structure = 'BAPE_VBAPX'.
lt_bapiparex+30(960) = ls_zbape_vbapx.
APPEND lt_bapiparex.
"2.3.3)计划行
CLEAR it_schedules_in.
it_schedules_in-itm_number = lv_posnr.
it_schedules_in-sched_line = 1.
it_schedules_in-req_qty = it_item-menge. "数量
it_schedules_in-req_date = sy-datum.
APPEND it_schedules_in.
it_schedules_inx-updateflag = 'I'.
it_schedules_inx-itm_number = lv_posnr.
it_schedules_inx-sched_line = 1.
it_schedules_inx-req_qty = 'X'.
it_schedules_inx-req_date = 'X'.
APPEND it_schedules_inx.
"2.3.4)价格条件
CLEAR:lv_knumv,cs_konv,order_conditions_in.
SELECT SINGLE knumv INTO lv_knumv FROM vbak WHERE vbeln = it_item-vbeln.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF cs_konv FROM v_konv_cds WHERE knumv = lv_knumv
AND kposn = it_item-posnr
AND kinak = ''
AND kschl LIKE 'PR0%'.
order_conditions_in-itm_number = lv_posnr. "Condition item number
order_conditions_in-cond_st_no = cs_konv-stunr. "步骤
order_conditions_in-cond_count = cs_konv-zaehk. "计数
order_conditions_in-cond_type = cs_konv-kschl. "条件类型
order_conditions_in-cond_value = cs_konv-kbetr. "单价
order_conditions_in-currency = cs_konv-waers. "币种
order_conditions_in-cond_p_unt = cs_konv-kpein. "价格单位
order_conditions_in-cond_unit = cs_konv-kmein. "数量单位
APPEND order_conditions_in.
order_conditions_inx-updateflag = 'U'.
order_conditions_inx-itm_number = lv_posnr. "Condition item number
order_conditions_inx-cond_st_no = cs_konv-stunr. "步骤
order_conditions_inX-cond_count = cs_konv-zaehk. "计数
order_conditions_inx-cond_type = cs_konv-kschl."条件类型
order_conditions_inx-cond_value = 'X'. "单价
order_conditions_inx-currency = 'X'. "币种
order_conditions_inx-cond_p_unt = 'X'. "价格单位
APPEND order_conditions_inx.
IF i_ztype = 'H'.
APPEND LINES OF order_conditions_in TO order_conditions_in2.
APPEND LINES OF order_conditions_inx TO order_conditions_inx2.
ENDIF.
"2.3.5)项目文本
CLEAR:order_text.
order_text-doc_number = ''.
order_text-itm_number = lv_posnr.
order_text-text_id = '0001'.
order_text-langu = sy-langu.
order_text-text_line = '退货,原订单:' && it_item-vbeln && '//' && it_item-posnr.
APPEND order_text.
ENDLOOP.
"2.4 掉BAPI
CALL FUNCTION ‘SD_SALESDOCUMENT_CREATE’
EXPORTING
sales_header_in = it_header_in
sales_header_inx = it_header_inx
IMPORTING
salesdocument_ex = salesdocument1
TABLES
return = it_return
sales_items_in = it_items_in
sales_items_inx = it_items_inx
sales_partners = it_partners
sales_schedules_in = it_schedules_in
sales_schedules_inx = it_schedules_inx
sales_conditions_in = order_conditions_in
sales_conditions_inx = order_conditions_inx
sales_text = order_text
extensionin = lt_bapiparex.
DELETE it_return WHERE id = ‘V4’ AND number = ‘219’.
IF salesdocument1 IS INITIAL.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
lv_error = 'X'.
lv_type1 = 'E'.
LOOP AT it_return WHERE type CA 'AE'.
IF sy-tabix = 1.
lv_mess = '创建退货订单失败:' && it_return-message.
ELSE.
lv_mess = lv_mess && ',' && it_return-message.
ENDIF.
ENDLOOP.
lv_mess1 = lv_mess.
LOOP AT it_item.
CLEAR it_item-posnr1.
MODIFY it_item TRANSPORTING posnr1.
ENDLOOP.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = ‘X’.
lv_type1 = 'S'.
DO 1000 TIMES.
SELECT COUNT(*) FROM lips WHERE vgbel = salesdocument1.
IF sy-subrc = 0.
EXIT.
ENDIF.
ENDDO.
LOOP AT it_item.
it_item-vbeln1 = salesdocument1.
SELECT SINGLE * INTO @DATA(lw_lips) FROM lips WHERE vgbel = @it_item-vbeln1 AND vgpos = @it_item-posnr1.
IF sy-subrc = 0.
it_item-vbeln3 = lw_lips-vbeln.
it_item-posnr3 = lw_lips-posnr.
ENDIF.
MODIFY it_item TRANSPORTING vbeln1 vbeln3 posnr3.
ENDLOOP.
ENDIF.
ENDIF.
***3)创建备件换货销售订单
"大部分数据用创建退货销售订单的数据
IF i_ztype = ‘H’.
IF lv_error = ‘’.
CLEAR:salesdocument2.
REFRESH:it_return.
"3.1)抬头赋值
it_header_in-doc_type = 'ZSP'. "订单类型 备件退货订单类型
it_header_in-purch_no_c = 'H:' && lv_bstkd. "采购订单号
DO 10 TIMES.
SELECT COUNT(*) FROM vbkd WHERE bstkd = it_header_in-purch_no_c.
IF sy-subrc = 0.
it_header_in-purch_no_c = 'H' && it_header_in-purch_no_c.
ELSE.
EXIT.
ENDIF.
ENDDO.
"3.2项目文本
LOOP AT order_text.
REPLACE '退货' WITH '换货' INTO order_text-text_line.
MODIFY order_text TRANSPORTING text_line.
ENDLOOP.
"3.3 掉BAPI
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
EXPORTING
sales_header_in = it_header_in
sales_header_inx = it_header_inx
IMPORTING
salesdocument_ex = salesdocument2
TABLES
return = it_return
sales_items_in = it_items_in2
sales_items_inx = it_items_inx
sales_partners = it_partners
sales_schedules_in = it_schedules_in
sales_schedules_inx = it_schedules_inx
sales_conditions_in = order_conditions_in2
sales_conditions_inx = order_conditions_inx2
sales_text = order_text
extensionin = lt_bapiparex.
DELETE it_return WHERE id = 'V4' AND number = '219'.
IF salesdocument2 IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
lv_error = 'X'.
lv_type2 = 'E'.
LOOP AT it_return WHERE type CA 'AE'.
IF sy-tabix = 1.
lv_mess = '创建换货订单失败:' && it_return-message.
ELSE.
lv_mess = lv_mess && ',' && it_return-message.
ENDIF.
ENDLOOP.
lv_mess2 = lv_mess.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
lv_type2 = 'S'.
LOOP AT it_item.
it_item-vbeln2 = salesdocument2.
it_item-posnr2 = it_item-posnr1.
MODIFY it_item TRANSPORTING vbeln2 posnr2.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
IF lv_error = ‘’.
e_type = ‘S’.
e_msg = ‘处理成功’.
ELSE.
IF salesdocument1 <> ‘’.
e_type = ‘I’.
e_msg = ‘退货订单创建成功:’ && salesdocument1 && ‘//换货订单创建失败,’ && lv_mess.
ELSE.
e_type = ‘E’.
e_msg = ‘处理失败,’ && lv_mess.
ENDIF.
ENDIF.
***4)更新接口返回表item
LOOP AT item.
item-vbeln = |{ item-vbeln ALPHA = IN }|.
item-posnr = |{ item-posnr ALPHA = IN }|.
READ TABLE it_item WITH KEY vbeln = item-vbeln
posnr = item-posnr.
IF sy-subrc = 0.
item-vbeln1 = it_item-vbeln1.
item-posnr1 = it_item-posnr1.
item-vbeln3 = it_item-vbeln3.
item-posnr3 = it_item-posnr3.
item-vbeln2 = it_item-vbeln2.
item-posnr2 = it_item-posnr2.
ENDIF.
MODIFY item TRANSPORTING vbeln1 posnr1 vbeln3 posnr3 vbeln2 posnr2.
ENDLOOP.
***5)更新sap接口记录表
DATA:lt_07 TYPE TABLE OF ztsd007 WITH HEADER LINE.
DATA:lv_xh TYPE ztsd007-xh.
DATA:lv_xhitem TYPE ztsd007-xhitem.
DATA:lv_date TYPE sy-datum.
DATA:lv_time TYPE sy-uzeit.
CLEAR:lv_xh,lv_xhitem,lt_07[].
lv_date = sy-datum.
lv_time = sy-uzeit.
SELECT MAX( xh ) INTO lv_xh FROM ztsd007.
ADD 1 TO lv_xh.
IF item[] IS INITIAL.
CLEAR lt_07.
lt_07-xh = lv_xh.
lt_07-xhitem = 1.
lt_07-cgsqd = i_cgsqd.
lt_07-zname = i_zname.
lt_07-ztype = i_ztype.
lt_07-zdate = lv_date.
lt_07-ztime = lv_time.
lt_07-ztype3 = e_type.
lt_07-mess3 = e_msg.
lt_07-zyy = i_zyy.
APPEND lt_07.
ELSE.
LOOP AT it_item.
CLEAR lt_07.
ADD 1 TO lv_xhitem.
lt_07-xh = lv_xh.
lt_07-xhitem = lv_xhitem.
lt_07-cgsqd = i_cgsqd.
lt_07-zname = i_zname.
lt_07-ztype = i_ztype.
lt_07-zdate = lv_date.
lt_07-ztime = lv_time.
lt_07-ztype3 = e_type.
lt_07-mess3 = e_msg.
lt_07-zyy = i_zyy.
lt_07-vbeln = it_item-vbeln.
lt_07-posnr = it_item-posnr.
lt_07-matnr = it_item-matnr.
lt_07-menge = it_item-menge.
lt_07-ztype1 = lv_type1.
lt_07-mess1 = lv_mess1.
lt_07-vbeln1 = it_item-vbeln1.
lt_07-posnr1 = it_item-posnr1.
lt_07-vbeln3 = it_item-vbeln3.
lt_07-posnr3 = it_item-posnr3.
lt_07-ztype2 = lv_type2.
lt_07-mess2 = lv_mess2.
lt_07-vbeln2 = it_item-vbeln2.
lt_07-posnr2 = it_item-posnr2.
APPEND lt_07.
ENDLOOP.
ENDIF.
IF lt_07[] IS NOT INITIAL.
MODIFY ztsd007 FROM TABLE lt_07.
COMMIT WORK.
ENDIF.
ENDFUNCTION.