本人做的接口有点复杂,这里直接写重点了:分为三步,第一步调拨单创建交货单,第二步根据生成的交货单进行拣配,交货单拣配成功后发货过账
** 创建交货单变量
DATA: ship_point TYPE tvst-vstel, "装运点/接收点
num_deliveries TYPE vbnum, "创建的凭证数
lt_trans_items LIKE bapidlvreftosto OCCURS 0 WITH HEADER LINE, "传输交货单对应的item
deliveries TYPE STANDARD TABLE OF bapishpdelivnumb, "交货号
extension_out TYPE STANDARD TABLE OF bapiparex, "BAPI 参数 ExtensionIn/ExtensionOut 的参考结构
lt_created_items LIKE TABLE OF bapidlvitemcreated, "发送个bapi生产DN的主要数据
ls_created_items LIKE bapidlvitemcreated,
serial_numbers LIKE bapidlvserialnumber OCCURS 0 WITH HEADER LINE , "序列号
lt_return1 TYPE STANDARD TABLE OF bapiret2, "返回参数
lf_vbeln TYPE vbeln_vl. "生成交货单号后返回的凭证
LOOP AT it_item INTO ls_item.
lt_trans_items-ref_doc = ls_item-ebeln. "参考凭证(调拨单)
lt_trans_items-ref_item = ls_item-posnr. "参考项(调拨单行项目)
lt_trans_items-dlv_qty = ls_item-menge. "实际已交货数量(按销售单位)
lt_trans_items-sales_unit = ls_item-meins. "销售单位
lt_trans_items-sales_unit_iso = ls_item-meins. "销售单位 的ISO代码
APPEND lt_trans_items.
ENDLOOP.
**调用bapi创建交货单
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_STO'
EXPORTING
ship_point = ship_point " 装运点
IMPORTING
delivery = lf_vbeln " 生成的交货单
num_deliveries = num_deliveries
TABLES
stock_trans_items = lt_trans_items " 传输的内表数据
serial_numbers = serial_numbers
deliveries = deliveries
created_items = lt_created_items
extension_out = extension_out
return = lt_return1. " 调用BAPI时反馈的消息内表
IF lf_vbeln IS NOT INITIAL. "不为空说明创建成功
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' "提交事务
EXPORTING
wait = 'X'.
WAIT UP TO 1 SECONDS. "等待一秒
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'."事务回滚
ENDIF
** 交货单拣配变量
DATA: vbkok_wa LIKE vbkok.
DATA: vbpok_tab LIKE vbpok OCCURS 0 WITH HEADER LINE, "要拣配的数据
prot LIKE prott OCCURS 0 WITH HEADER LINE. "拣配返回消息
DATA ls_lips TYPE lips.
DATA lt_lips TYPE TABLE OF lips OCCURS 0 WITH HEADER LINE.
DATA: ls_header_data TYPE bapiobdlvhdrchg. "更改外向交货拣配数据表头等级
DATA: ls_header_control TYPE bapiobdlvhdrctrlchg. "外向交货标题级别控制数据
DATA: lt_batch_item TYPE STANDARD TABLE OF bapiobdlvitemchg. "更改外向交货拣配数据项目等级
DATA: ls_batch_item TYPE bapiobdlvitemchg.
DATA: lt_batch_control TYPE STANDARD TABLE OF bapiobdlvitemctrlchg. "外向交货项目级别控制数据
DATA: ls_batch_control TYPE bapiobdlvitemctrlchg.
DATA: lv_newitem TYPE posnr_vl. "行项目
DATA: lt_return2 TYPE STANDARD TABLE OF bapiret2. "返回参数内表
** 抬头数据
ls_header_data-deliv_numb = lf_vbeln. "交货单号(这里用的是调拨单生成的交货单)
ls_header_control-deliv_numb = lf_vbeln. "交货单号
** 获取交货单数据 (这里的数据其实可以在上面BAPI的表参数找到,但是原接口的逻辑有些复杂所以取Lips的了)
SELECT * INTO TABLE lt_lips
FROM lips
WHERE vbeln = lf_vbeln
AND lfimg <> 0.
LOOP AT lt_lips INTO ls_lips .
ls_batch_item-deliv_numb = lf_vbeln. "修改DN号
ls_batch_item-deliv_item = ls_lips-posnr. "被修改DN号行项目
ls_batch_item-material = ls_lips-matnr. "物料
ls_batch_item-dlv_qty = ls_lips-lfimg. "交货数量
ls_batch_item-batch = 'DS9999'. "批次
ls_batch_item-sales_unit = ls_lips-vrkme. "被拆分DN销售单位
ls_batch_item-sales_unit_iso = ls_lips-meins. "被拆分DN基本单位
ls_batch_item-fact_unit_nom = ls_lips-umvkz. "销售数量转换成SKU的分子(因子)
ls_batch_item-fact_unit_denom = ls_lips-umvkn. "销售数量转换为 SKU 的值(分母)
APPEND ls_batch_item TO lt_batch_item .
" Set Mofify flag
CLEAR ls_batch_control.
ls_batch_control-deliv_numb = lw_credn_log-lf_vbeln. "修改DN号
ls_batch_control-deliv_item = ls_lips-posnr. "修改DN Item
ls_batch_control-chg_delqty = 'X'. "数量修改标志
APPEND ls_batch_control TO lt_batch_control.
ENDLOOP
" 调用BAPI修改交货单
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
header_data = ls_header_data
header_control = ls_header_control
delivery = lf_vbeln
TABLES
item_data = lt_batch_item
item_control = lt_batch_control
return = lt_return2
EXCEPTIONS
error_message = 1
OTHERS = 2.
READ TABLE LT_RETURN2 INTO DATA(LW_RETURN2) WITH KEY TYPE = 'E'.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.
" 整理要拣配的数据
LOOP AT lt_lips.
CLEAR vbpok_tab.
vbpok_tab-vbeln_vl = lt_lips-vbeln. "交货单
vbpok_tab-posnr_vl = lt_lips-posnr. "交货单行项目
vbpok_tab-vbeln = lt_lips-vgbel. "销售订单
vbpok_tab-posnn = lt_lips-vgpos. "销售订单行项目
vbpok_tab-matnr = lt_lips-matnr. "物料
vbpok_tab-pikmg = lt_lips-lfimg. "实际已交货数量(按销售单位)
vbpok_tab-vrkme = lt_lips-vrkme. "销售单位
vbpok_tab-umvkz = 1.
vbpok_tab-umvkn = 1.
APPEND vbpok_tab.
ENDLOOP.
vbkok_wa = lf_vbeln. "交货单号
" 调用函数开始拣配
CALL FUNCTION 'SD_DELIVERY_UPDATE_PICKING_1'
EXPORTING
vbkok_wa = vbkok_wa
TABLES
vbpok_tab = vbpok_tab
prot = prot.
IF PORT[] IS INITIAL
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
WAIT UP TO 1 SECONDS.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENFI.
** 交货单交货单变量
DATA: i_header_data TYPE bapiobdlvhdrcon,
i_header_control TYPE bapiobdlvhdrctrlcon.
DATA: lw_return TYPE bapiret2,
lt_return3 TYPE TABLE OF bapiret2.
i_header_data-deliv_numb = lf_vbeln. "交货单号
i_header_control-deliv_numb = lf_vbeln. "交货单号
i_header_control-post_gi_flg = 'X'.
i_header_control-deliv_date_flg = 'X'.
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CONFIRM_DEC'
EXPORTING
header_data = i_header_data
header_control = i_header_control
delivery = lf_vbeln
TABLES
return = lt_return3.
READ TABLE LT_RETURN INTO LW_RETURN WITH KEY TYPE = 'E'.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
WAIT UP TO 1 SECONDS.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.