FUNCTION ZRFC_WMS_DELIVERY_DELETE.
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" ITAB_DETAIL STRUCTURE ZVBPOK OPTIONAL
*" ITAB_RETURN STRUCTURE ZINTERFACE_RETURN OPTIONAL
*" ET_LOG STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
DATA: WA_HDATA LIKE BAPIOBDLVHDRCHG,
WA_HCONT LIKE BAPIOBDLVHDRCTRLCHG,
D_DELIVY LIKE BAPIOBDLVHDRCHG-DELIV_NUMB,
ITEMCTRL LIKE BAPIOBDLVITEMCTRLCHG OCCURS 0 WITH HEADER LINE,
ITEMDATA LIKE BAPIOBDLVITEMCHG OCCURS 0 WITH HEADER LINE,
IT_BAPIRET2 LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE.
DATA: ITAB_HEADER LIKE ZVBKOK OCCURS 0 WITH HEADER LINE,
ITH LIKE ITAB_HEADER ,
TBL_ITEMS LIKE ITAB_DETAIL,
IP_VBELN TYPE VBELN,
VIP TYPE VBELN,
IP_POSNR TYPE POSNR.
*--------------------CHA JWY_20170811
SORT ITAB_DETAIL.
*--------------------CHA JWY_20170811
LOOP AT ITAB_DETAIL.
MOVE ITAB_DETAIL-VBELN TO ITAB_HEADER-VBELN.
MOVE ITAB_DETAIL-VBELN TO ITAB_HEADER-WMS_NO.
MOVE ITAB_DETAIL-VBELN TO IP_VBELN.
PERFORM F_ADD_ZERO USING IP_VBELN.
MOVE ITAB_DETAIL-POSNR TO IP_POSNR.
PERFORM F_ADD_ZERO USING IP_POSNR.
SELECT SINGLE VBELN INTO VIP FROM LIPS WHERE VBELN = IP_VBELN AND POSNR = IP_POSNR.
IF SY-SUBRC = 0.
COLLECT ITAB_HEADER.
ELSE.
ITAB_RETURN-BILLNO = ITAB_DETAIL-VBELN.
ITAB_RETURN-SAPNO = ITAB_DETAIL-POSNR.
ITAB_RETURN-SUBRC = -1.
ITAB_RETURN-MSG = '交货项目不存在'.
APPEND ITAB_RETURN.
ENDIF.
ENDLOOP.
*--------------------CHA JWY_20170811
SORT ITAB_HEADER.
SORT ITAB_DETAIL.
*--------------------CHA JWY_20170811
LOOP AT ITAB_HEADER INTO ITH.
CLEAR: WA_HDATA, WA_HCONT, D_DELIVY, IT_BAPIRET2.
WA_HDATA-DELIV_NUMB = ITH-VBELN.
WA_HCONT-DELIV_NUMB = ITH-VBELN.
LOOP AT ITAB_DETAIL INTO TBL_ITEMS WHERE VBELN = ITH-VBELN .
ITEMCTRL-DELIV_NUMB = ITH-VBELN.
ITEMCTRL-DELIV_ITEM = TBL_ITEMS-POSNR.
ITEMCTRL-DEL_ITEM = 'X'.
APPEND ITEMCTRL.
ITEMDATA-DELIV_NUMB = ITH-VBELN.
ITEMDATA-DELIV_ITEM = TBL_ITEMS-POSNR.
ITEMDATA-FACT_UNIT_NOM = 1.
ITEMDATA-FACT_UNIT_DENOM = 1.
APPEND ITEMDATA.
ENDLOOP.
CLEAR IT_BAPIRET2[].
* *---DELETING DELIVERY DOC (VL02N)
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
HEADER_DATA = WA_HDATA
HEADER_CONTROL = WA_HCONT
DELIVERY = WA_HDATA-DELIV_NUMB
TABLES
ITEM_CONTROL = ITEMCTRL
ITEM_DATA = ITEMDATA
RETURN = IT_BAPIRET2.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
MOVE SY-SUBRC TO ITAB_RETURN-SUBRC.
IF SY-SUBRC = 0.
APPEND LINES OF IT_BAPIRET2 TO ET_LOG.
ITAB_RETURN-MSG = '成功'.
ELSE.
ITAB_RETURN-MSG = '失败'.
ENDIF.
*--------------------CHA JWY_20170811
SORT ITEMDATA.
*--------------------CHA JWY_20170811
LOOP AT ITEMDATA.
ITAB_RETURN-BILLNO = ITEMDATA-DELIV_NUMB.
ITAB_RETURN-SAPNO = ITEMDATA-DELIV_ITEM.
APPEND ITAB_RETURN.
ENDLOOP.
CLEAR VIP.
SELECT SINGLE VBELN INTO VIP FROM LIPS WHERE VBELN = WA_HDATA-DELIV_NUMB.
IF SY-SUBRC > 0. "删除空交货单
WA_HCONT-DLV_DEL = 'X'.
CLEAR IT_BAPIRET2[].
*---Deleting delivery doc (VL02n)
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
HEADER_DATA = WA_HDATA
HEADER_CONTROL = WA_HCONT
DELIVERY = WA_HDATA-DELIV_NUMB
TABLES
RETURN = IT_BAPIRET2.
*---commit
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ENDIF.
ENDLOOP.
ENDFUNCTION.