大家好,欢迎来到IT知识分享网。
文章目录
1. Introduce
The bapi is used cross company.
2. code
" 采购订单创建交货单BAPI使用的参数定义 DATA: L_DELIVERY TYPE VBELN_VL, L_NUM_DELIVERIES TYPE VBNUM, LT_STOCK_TRANS_ITEMS TYPE STANDARD TABLE OF BAPIDLVREFTOSTO, LT_DELIVERIES TYPE STANDARD TABLE OF BAPISHPDELIVNUMB, LT_CREATED_ITEMS TYPE STANDARD TABLE OF BAPIDLVITEMCREATED, LT_RETURN TYPE STANDARD TABLE OF BAPIRET2, WA_RETURN LIKE BAPIRET2, LT_PROT TYPE STANDARD TABLE OF PROTT, LT_BAPIOBDLVITEMCHG LIKE STANDARD TABLE OF BAPIOBDLVITEMCHG WITH HEADER LINE, LT_BAPIOBDLVITEMCHG_C LIKE STANDARD TABLE OF BAPIOBDLVITEMCTRLCHG WITH HEADER LINE, LT_ITEM_DATA_SPL TYPE STANDARD TABLE OF /SPE/BAPIOBDLVITEMCHG. DATA: WA_HEADER_DATA TYPE BAPIOBDLVHDRCHG, WA_HEADER_CONTROL TYPE BAPIOBDLVHDRCTRLCHG, WA_ITEM_DATA_SPL TYPE /SPE/BAPIOBDLVITEMCHG, WA_STOCK_TRANS_ITEMS TYPE BAPIDLVREFTOSTO, WA_CREATED_ITEMS TYPE BAPIDLVITEMCREATED, WA_DELIVERIES TYPE BAPISHPDELIVNUMB, WA_VBKOK TYPE VBKOK, L_EF_ERROR_ANY_0 TYPE XFELD. "第一步:根据采购订单创建交货单
SORT IMP_TAB by EBELN EBELP.
LOOP at IMP_TAB.
SELECT SINGLE meins INTO lv_meins
FROM mara
WHERE matnr = IMP_TAB-MATNR.
WA_STOCK_TRANS_ITEMS-DLV_QTY = IMP_TAB-MENGE. "采购订单数量 WA_STOCK_TRANS_ITEMS-REF_DOC = IMP_TAB-EBELN. "采购凭证号
WA_STOCK_TRANS_ITEMS-REF_ITEM = IMP_TAB-EBELP. "采购凭证的项目编号 WA_STOCK_TRANS_ITEMS-SALES_UNIT = lv_meins. "销售单位
APPEND WA_STOCK_TRANS_ITEMS TO LT_STOCK_TRANS_ITEMS.
CLEAR:WA_STOCK_TRANS_ITEMS,lv_meins.
ENDLOOP.
"根据采购订单创建交货单 CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_STO' * EXPORTING * SHIP_POINT = * DUE_DATE = * DEBUG_FLG = IMPORTING DELIVERY = L_DELIVERY NUM_DELIVERIES = L_NUM_DELIVERIES TABLES STOCK_TRANS_ITEMS = LT_STOCK_TRANS_ITEMS * SERIAL_NUMBERS = * EXTENSION_IN = DELIVERIES = LT_DELIVERIES CREATED_ITEMS = LT_CREATED_ITEMS * EXTENSION_OUT = RETURN = LT_RETURN. "执行结果判断
CLEAR WA_RETURN.
LOOP AT LT_RETURN INTO WA_RETURN WHERE TYPE = 'E' OR ( TYPE = 'W' AND NUMBER = '030').
EXIT.
ENDLOOP.
IF SY-SUBRC <> 0 AND LINES( LT_STOCK_TRANS_ITEMS ) = LINES( LT_CREATED_ITEMS ) .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
EXP_DATA-VBELN = L_DELIVERY."交货单号 CONCATENATE EXP_DATA-MSG '交货单DN创建成功->' INTO EXP_DATA-MSG. "更新日志
CLEAR:GW_ZTMES003.
LOOP at GT_ZTMES003 INTO GW_ZTMES003.
GW_ZTMES003-VBELN = L_DELIVERY."交货单号 GW_ZTMES003-ID1 = 'S'. GW_ZTMES003-MSG1 = '交货单DN创建成功'. MODIFY GT_ZTMES003 FROM GW_ZTMES003. CLEAR:GW_ZTMES003. ENDLOOP. MODIFY ZTMES003 FROM TABLE GT_ZTMES003[]. "第二步:更新交货单库位
WA_HEADER_DATA-DELIV_NUMB = L_DELIVERY.
WA_HEADER_CONTROL-DELIV_NUMB = L_DELIVERY.
CLEAR:GW_ZSMES006.
READ TABLE IMP_TAB INTO GW_ZSMES006 INDEX 1.
LOOP AT LT_CREATED_ITEMS INTO WA_CREATED_ITEMS.
LT_BAPIOBDLVITEMCHG-DELIV_NUMB = L_DELIVERY.
LT_BAPIOBDLVITEMCHG-DELIV_ITEM = WA_CREATED_ITEMS-DELIV_ITEM. "交货单行项目. LT_BAPIOBDLVITEMCHG-FACT_UNIT_NOM = 1. LT_BAPIOBDLVITEMCHG-FACT_UNIT_DENOM = 1. LT_BAPIOBDLVITEMCHG-MATERIAL = WA_CREATED_ITEMS-MATERIAL. APPEND LT_BAPIOBDLVITEMCHG. CLEAR LT_BAPIOBDLVITEMCHG. LT_BAPIOBDLVITEMCHG_C-DELIV_NUMB = L_DELIVERY. LT_BAPIOBDLVITEMCHG_C-DELIV_ITEM = WA_CREATED_ITEMS-DELIV_ITEM. "交货项目
APPEND LT_BAPIOBDLVITEMCHG_C.
CLEAR LT_BAPIOBDLVITEMCHG_C.
WA_ITEM_DATA_SPL-DELIV_NUMB = L_DELIVERY. "交货 WA_ITEM_DATA_SPL-DELIV_ITEM = WA_CREATED_ITEMS-DELIV_ITEM. "交货项目
WA_ITEM_DATA_SPL-PICK_DENIAL = 'X'. "拣配拒绝标记 WA_ITEM_DATA_SPL-STGE_LOC = GW_ZSMES006-OLGORT."库存地点 都相同
APPEND WA_ITEM_DATA_SPL TO LT_ITEM_DATA_SPL.
ENDLOOP.
"交货单库位更新 CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE' EXPORTING HEADER_DATA = WA_HEADER_DATA HEADER_CONTROL = WA_HEADER_CONTROL DELIVERY = L_DELIVERY TABLES ITEM_DATA = LT_BAPIOBDLVITEMCHG[] ITEM_CONTROL = LT_BAPIOBDLVITEMCHG_C[] RETURN = LT_RETURN ITEM_DATA_SPL = LT_ITEM_DATA_SPL . READ TABLE LT_RETURN INTO WA_RETURN WITH KEY TYPE = 'E'. IF SY-SUBRC <> 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING WAIT = 'X'. CONCATENATE EXP_DATA-MSG 'DN库位更新成功->' INTO EXP_DATA-MSG. "更新日志
CLEAR:GW_ZTMES003.
LOOP at GT_ZTMES003 INTO GW_ZTMES003.
GW_ZTMES003-ID2 = 'S'.
GW_ZTMES003-MSG2 = 'DN库位更新成功' .
MODIFY GT_ZTMES003 FROM GW_ZTMES003.
CLEAR:GW_ZTMES003.
ENDLOOP.
MODIFY ZTMES003 FROM TABLE GT_ZTMES003[].
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
CONCATENATE EXP_DATA-MSG 'DN库位更新失败!' INTO EXP_DATA-MSG.
"更新日志 CLEAR:GW_ZTMES003. LOOP at GT_ZTMES003 INTO GW_ZTMES003. GW_ZTMES003-ID2 = 'E'. CONCATENATE 'DN库位更新失败:' WA_RETURN-MESSAGE INTO GW_ZTMES003-MSG2. MODIFY GT_ZTMES003 FROM GW_ZTMES003. CLEAR:GW_ZTMES003. ENDLOOP. MODIFY ZTMES003 FROM TABLE GT_ZTMES003[]. "删除交货单
CLEAR:WA_HEADER_DATA,WA_HEADER_CONTROL,LT_BAPIOBDLVITEMCHG[],
LT_BAPIOBDLVITEMCHG_C[],LT_RETURN, LT_ITEM_DATA_SPL.
WA_HEADER_DATA-DELIV_NUMB = L_DELIVERY.
WA_HEADER_CONTROL-DELIV_NUMB = L_DELIVERY.
WA_HEADER_CONTROL-DLV_DEL = 'X' ."删除 CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE' EXPORTING HEADER_DATA = WA_HEADER_DATA HEADER_CONTROL = WA_HEADER_CONTROL DELIVERY = L_DELIVERY TABLES ITEM_DATA = LT_BAPIOBDLVITEMCHG[] ITEM_CONTROL = LT_BAPIOBDLVITEMCHG_C[] RETURN = LT_RETURN ITEM_DATA_SPL = LT_ITEM_DATA_SPL . READ TABLE LT_RETURN INTO WA_RETURN WITH KEY TYPE = 'E'. IF SY-SUBRC <> 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING WAIT = 'X'. ENDIF. endif. ELSE. "创建交货单出错
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
CONCATENATE EXP_DATA-MSG '交货单DN创建失败:' WA_RETURN-MESSAGE INTO EXP_DATA-MSG.
"更新日志 CLEAR: GW_ZTMES003. LOOP at GT_ZTMES003 INTO GW_ZTMES003. GW_ZTMES003-ID1 = 'E'. CONCATENATE '交货单DN创建失败:' WA_RETURN-MESSAGE INTO GW_ZTMES003-MSG1. MODIFY GT_ZTMES003 FROM GW_ZTMES003. CLEAR:GW_ZTMES003. ENDLOOP. MODIFY ZTMES003 FROM TABLE GT_ZTMES003[]. EXIT. ENDIF. CLEAR:GW_ZTMES003. READ TABLE GT_ZTMES003 INTO GW_ZTMES003 INDEX 1. "检查交货单数量
lv_dn = L_DELIVERY.
add_pre_zero:lv_dn.
IF GW_ZTMES003-ID1 = 'S' or GW_ZTMES003-ID2 = 'S'.
SELECT sum( LFIMG ) into lv_LFIMG
from LIPS
where VBELN = lv_dn.
if lv_menge <> lv_LFIMG.
"删除交货单 CLEAR:WA_HEADER_DATA,WA_HEADER_CONTROL,LT_BAPIOBDLVITEMCHG[], LT_BAPIOBDLVITEMCHG_C[],LT_RETURN, LT_ITEM_DATA_SPL. WA_HEADER_DATA-DELIV_NUMB = L_DELIVERY. WA_HEADER_CONTROL-DELIV_NUMB = L_DELIVERY. WA_HEADER_CONTROL-DLV_DEL = 'X' ."删除
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
HEADER_DATA = WA_HEADER_DATA
HEADER_CONTROL = WA_HEADER_CONTROL
DELIVERY = L_DELIVERY
TABLES
ITEM_DATA = LT_BAPIOBDLVITEMCHG[]
ITEM_CONTROL = LT_BAPIOBDLVITEMCHG_C[]
RETURN = LT_RETURN
ITEM_DATA_SPL = LT_ITEM_DATA_SPL .
READ TABLE LT_RETURN INTO WA_RETURN WITH KEY TYPE = 'E'.
IF SY-SUBRC <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ENDIF.
"更新日志 CLEAR:GW_ZTMES003. LOOP at GT_ZTMES003 INTO GW_ZTMES003. GW_ZTMES003-ID2 = 'E'. CONCATENATE EXP_DATA-MSG 'SAP创建的交货单数量不正确' INTO GW_ZTMES003-MSG2. MODIFY GT_ZTMES003 FROM GW_ZTMES003. CLEAR:GW_ZTMES003. ENDLOOP. MODIFY ZTMES003 FROM TABLE GT_ZTMES003[]. EXP_DATA-ID = 'E'. EXP_DATA-MSG = 'SAP创建的交货单数量不正确,需重传'. endif. endif. CLEAR:GW_ZTMES003. READ TABLE GT_ZTMES003 INTO GW_ZTMES003 INDEX 1. IF GW_ZTMES003-ID1 = 'E' or GW_ZTMES003-ID2 = 'E'. EXIT. ENDIF. "第三步:交货单交货过账产生物料凭证
WA_VBKOK-WADAT_IST = SY-DATUM."过账日期 WA_VBKOK-VBELN_VL = L_DELIVERY. "交货单
WA_VBKOK-WABUC = 'X'. "自动过帐货物移动 CALL FUNCTION 'WS_DELIVERY_UPDATE' EXPORTING VBKOK_WA = WA_VBKOK DELIVERY = L_DELIVERY UPDATE_PICKING = 'X' IF_DATABASE_UPDATE = '1' "COMMIT = 'X' 异步更新
IF_ERROR_MESSAGES_SEND_0 = 'X'
IMPORTING
EF_ERROR_ANY_0 = L_EF_ERROR_ANY_0
TABLES
PROT = LT_PROT
EXCEPTIONS
ERROR_MESSAGE = 1
OTHERS = 2.
"结果判断 IF SY-SUBRC = 0 AND LT_PROT[] IS INITIAL. COMMIT WORK AND WAIT. SELECT SINGLE MAX( VBELN ) INTO GW_ZTMES003-VBELN3 "过账凭证
FROM VBFA
WHERE VBELV = WA_VBKOK-VBELN_VL "交货单 AND VBTYP_N = 'R' AND PLMIN = '+'. EXP_DATA-MBLNR3 = GW_ZTMES003-VBELN3. CONCATENATE EXP_DATA-MSG 'DN发货成功。' INTO EXP_DATA-MSG. "更新日志
CLEAR: GW_ZTMES003.
LOOP at GT_ZTMES003 INTO GW_ZTMES003.
GW_ZTMES003-ID3 = 'S'.
GW_ZTMES003-MSG3 = 'DN发货成功' .
MODIFY GT_ZTMES003 FROM GW_ZTMES003.
CLEAR:GW_ZTMES003.
ENDLOOP.
MODIFY ZTMES003 FROM TABLE GT_ZTMES003[].
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
CONCATENATE EXP_DATA-MSG 'DN发货失败!' INTO EXP_DATA-MSG.
"更新日志 CLEAR: GW_ZTMES003. LOOP at GT_ZTMES003 INTO GW_ZTMES003. GW_ZTMES003-ID3 = 'E'. GW_ZTMES003-MSG3 = 'DN发货失败' . MODIFY GT_ZTMES003 FROM GW_ZTMES003. CLEAR:GW_ZTMES003. ENDLOOP. MODIFY ZTMES003 FROM TABLE GT_ZTMES003[]. "删除交货单
CLEAR:WA_HEADER_DATA,WA_HEADER_CONTROL,LT_BAPIOBDLVITEMCHG[],
LT_BAPIOBDLVITEMCHG_C[],LT_RETURN, LT_ITEM_DATA_SPL.
WA_HEADER_DATA-DELIV_NUMB = L_DELIVERY.
WA_HEADER_CONTROL-DELIV_NUMB = L_DELIVERY.
WA_HEADER_CONTROL-DLV_DEL = 'X' ."删除
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
HEADER_DATA = WA_HEADER_DATA
HEADER_CONTROL = WA_HEADER_CONTROL
DELIVERY = L_DELIVERY
TABLES
ITEM_DATA = LT_BAPIOBDLVITEMCHG[]
ITEM_CONTROL = LT_BAPIOBDLVITEMCHG_C[]
RETURN = LT_RETURN
ITEM_DATA_SPL = LT_ITEM_DATA_SPL .
READ TABLE LT_RETURN INTO WA_RETURN WITH KEY TYPE = 'E'.
IF SY-SUBRC <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ENDIF.
EXIT.
ENDIF.
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/10461.html