FUNCTION Z_SD_CREATE_DN.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IM_SHIP_TYPE) TYPE LIKP-VSART DEFAULT '01'
*" VALUE(IM_DUE_DATE) TYPE DATUM DEFAULT '99991231'
*" EXPORTING
*" VALUE(E_DN) TYPE CHAR10
*" VALUE(E_STATUS) TYPE CHAR1
*" VALUE(E_MESSAGE) TYPE CHAR40
*" TABLES
*" T_DATA STRUCTURE ZSDS0008
*" IT_RETURN STRUCTURE BAPIRET2 OPTIONAL
*" IT_DN STRUCTURE VBAK OPTIONAL
*"----------------------------------------------------------------------
DATA : LV_SHIP_POINT TYPE BAPIDLVCREATEHEADER-SHIP_POINT,
LV_DUE_DATE TYPE BAPIDLVCREATEHEADER-DUE_DATE,
LV_DELIVERY TYPE BAPISHPDELIVNUMB-DELIV_NUMB,
LT_SO_ITEMS LIKE TABLE OF BAPIDLVREFTOSALESORDER,
LS_SO_ITEM LIKE BAPIDLVREFTOSALESORDER,
LT_STO_ITEMS LIKE TABLE OF BAPIDLVREFTOSTO,
LS_STO_ITEM LIKE BAPIDLVREFTOSTO,
LT_RETURN LIKE TABLE OF BAPIRET2,
LS_RETURN LIKE BAPIRET2. DATA : LV_TYPE(). "PO / SO
DATA : LV_SUCCESS.
DATA : LV_MATNR TYPE VBAP-MATNR.
DATA : LV_VBELN TYPE VBELN.
DATA : LV_TABIX TYPE SY-TABIX.
DATA : LV_CONTINUE. DATA : LT_LIPS TYPE TABLE OF LIPS,
LS_LIPS TYPE LIPS. DATA : BEGIN OF LS_DATA,
DEL.
INCLUDE STRUCTURE ZSDS0008.
DATA : END OF LS_DATA. DATA : LT_DATA LIKE TABLE OF LS_DATA. DATA : BEGIN OF LT_VBFA OCCURS ,
VBELV TYPE VBFA-VBELV, "先前凭证
POSNV TYPE VBFA-POSNV,
VBELN TYPE VBFA-VBELN, "后续凭证
POSNR TYPE VBFA-POSNN,
END OF LT_VBFA. DATA : LV_MAX TYPE I VALUE '',
LV_COUNT TYPE I. LOOP AT T_DATA.
MOVE-CORRESPONDING T_DATA TO LS_DATA.
APPEND LS_DATA TO LT_DATA.
ENDLOOP. PERFORM FRM_DATA_CHECK TABLES LT_DATA CHANGING E_MESSAGE. IF E_MESSAGE IS NOT INITIAL.
E_STATUS = 'E'.
EXIT.
ENDIF. REFRESH : LT_STO_ITEMS,
LT_SO_ITEMS,
IT_RETURN. SORT LT_DATA BY VBELN POSNR. LV_DUE_DATE = IM_DUE_DATE. CLEAR LS_DATA.
READ TABLE LT_DATA INTO LS_DATA INDEX .
SELECT SINGLE * FROM EKKO WHERE EBELN = LS_DATA-VBELN.
IF SY-SUBRC EQ ."PO - STO.
LV_TYPE = 'PO'.
LV_SHIP_POINT = ''.
* LV_DUE_DATE = '99991230'.
ELSE.
LV_TYPE = 'SO'.
LV_SHIP_POINT = ''.
* LV_DUE_DATE = '99991230'.
ENDIF. * 判断行项目是否产生过交货单
SELECT VGBEL AS VBELV VGPOS AS POSNV VBELN POSNR
FROM LIPS
INTO TABLE LT_VBFA
WHERE VGBEL = LS_DATA-VBELN. * 控制交货单最大的项目数
DO. CLEAR LV_CONTINUE. LOOP AT LT_DATA INTO LS_DATA WHERE DEL EQ ''. * IF LV_MAX = LV_COUNT.
* CLEAR LV_COUNT.
* EXIT.
* ENDIF.
* 排除掉已经创建过交货单的行项目
READ TABLE LT_VBFA WITH KEY VBELV = LS_DATA-VBELN POSNV = LS_DATA-POSNR.
IF SY-SUBRC EQ .
E_DN = LT_VBFA-VBELN.
IT_DN-VBELN = E_DN.
COLLECT IT_DN.
CONTINUE.
ENDIF. CLEAR E_DN. ADD TO LV_COUNT. IF LV_TYPE = 'PO'.
CLEAR LS_STO_ITEM.
LS_STO_ITEM-REF_DOC = LS_DATA-VBELN.
LS_STO_ITEM-REF_ITEM = LS_DATA-POSNR.
APPEND LS_STO_ITEM TO LT_STO_ITEMS.
ELSE.
CLEAR LS_SO_ITEM.
LS_SO_ITEM-REF_DOC = LS_DATA-VBELN.
LS_SO_ITEM-REF_ITEM = LS_DATA-POSNR.
APPEND LS_SO_ITEM TO LT_SO_ITEMS.
ENDIF. LV_CONTINUE = 'X'. LS_DATA-DEL = 'X'.
MODIFY LT_DATA FROM LS_DATA TRANSPORTING DEL.
ENDLOOP. IF LV_CONTINUE EQ ''.
EXIT.
ENDIF. IF LV_DUE_DATE NE ''.
FREE MEMORY ID 'DUE_DATE'.
EXPORT LV_DUE_DATE TO MEMORY ID 'DUE_DATE'.
ENDIF. IF LT_SO_ITEMS IS NOT INITIAL.
* Call BAPI to Create DN
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_SLS'
EXPORTING
SHIP_POINT = LV_SHIP_POINT
DUE_DATE = LV_DUE_DATE
IMPORTING
DELIVERY = LV_DELIVERY
TABLES
SALES_ORDER_ITEMS = LT_SO_ITEMS
RETURN = LT_RETURN. ELSEIF LT_STO_ITEMS IS NOT INITIAL.
* Call BAPI to Create DN
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_STO'
EXPORTING
SHIP_POINT = LV_SHIP_POINT
DUE_DATE = LV_DUE_DATE
IMPORTING
DELIVERY = LV_DELIVERY
TABLES
STOCK_TRANS_ITEMS = LT_STO_ITEMS
RETURN = LT_RETURN. ENDIF. APPEND LINES OF LT_RETURN TO IT_RETURN. LOOP AT LT_RETURN TRANSPORTING NO FIELDS WHERE TYPE = 'A' OR
TYPE = 'E' OR
TYPE = 'X'.
EXIT.
ENDLOOP. IF SY-SUBRC EQ .
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. PERFORM FRM_CONVERT_MESSAGE TABLES LT_RETURN CHANGING E_MESSAGE. E_STATUS = 'E'.
EXIT.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'. E_STATUS = 'S'.
E_DN = LV_DELIVERY.
ENDIF. DO TIMES.
SELECT *
FROM LIPS
INTO TABLE LT_LIPS
WHERE VBELN = LV_DELIVERY. IF SY-SUBRC EQ .
EXIT.
ENDIF.
ENDDO. * --->请求交货数量 与交货数量不一致 20152417
IF LT_STO_ITEMS IS NOT INITIAL AND LINES( LT_STO_ITEMS ) NE LINES( LT_LIPS ).
* WAIT UP TO 1 SECONDS.
PERFORM FRM_DELETE_DELIVERY TABLES IT_RETURN USING LV_DELIVERY.
CLEAR IT_RETURN.
IT_RETURN-ID = ''.
IT_RETURN-NUMBER = ''.
IT_RETURN-TYPE = 'E'.
IT_RETURN-MESSAGE = '交货数量与请求交货数量不一致,停止交货'.
IT_RETURN-MESSAGE_V1 = '交货数量与请求交货数量不一致'.
IT_RETURN-MESSAGE_V2 = ',停止交货'.
IT_RETURN-MESSAGE_V3 = LV_DELIVERY.
IT_RETURN-MESSAGE_V4 = ''.
APPEND IT_RETURN. CLEAR: E_STATUS,E_DN.
EXIT.
ELSEIF LT_SO_ITEMS IS NOT INITIAL AND LINES( LT_SO_ITEMS ) NE LINES( LT_LIPS ).
* WAIT UP TO 1 SECONDS.
PERFORM FRM_DELETE_DELIVERY TABLES IT_RETURN USING LV_DELIVERY.
CLEAR IT_RETURN.
IT_RETURN-ID = ''.
IT_RETURN-NUMBER = ''.
IT_RETURN-TYPE = 'E'.
IT_RETURN-MESSAGE = '交货数量与请求交货数量不一致,停止交货'.
IT_RETURN-MESSAGE_V1 = '交货数量与请求交货数量不一致'.
IT_RETURN-MESSAGE_V2 = ',停止交货'.
IT_RETURN-MESSAGE_V3 = LV_DELIVERY.
IT_RETURN-MESSAGE_V4 = ''.
APPEND IT_RETURN. CLEAR: E_STATUS,E_DN.
EXIT.
ENDIF.
* <---请求交货数量 与交货数量不一致 20150427 IT_DN-VBELN = LV_DELIVERY.
APPEND IT_DN. * REFRESH : GT_DN_ITEM.
*
* LOOP AT LT_LIPS INTO LS_LIPS.
* LV_TABIX = SY-TABIX.
*
* CLEAR GS_DN_ITEM.
* GS_DN_ITEM-DELIV_NUMB = LS_LIPS-VBELN.
* GS_DN_ITEM-DELIV_ITEM = LS_LIPS-POSNR.
* GS_DN_ITEM-MATERIAL = LS_LIPS-MATNR.
* GS_DN_ITEM-DLV_QTY = LS_LIPS-LGMNG.
* GS_DN_ITEM-FACT_UNIT_NOM = LS_LIPS-UMVKZ.
* GS_DN_ITEM-FACT_UNIT_DENOM = LS_LIPS-UMVKN.
*
* READ TABLE LT_DATA INTO LS_DATA WITH KEY VBELN = LS_LIPS-VGBEL POSNR = LS_LIPS-VGPOS.
* IF SY-SUBRC EQ 0 AND LS_DATA-CHARG IS NOT INITIAL AND LS_LIPS-CHARG NE LS_DATA-CHARG.
* GS_DN_ITEM-BATCH = LS_DATA-CHARG.
* APPEND GS_DN_ITEM TO GT_DN_ITEM.
*
* LS_LIPS-CHARG = LS_DATA-CHARG.
* ELSE.
* DELETE LT_LIPS INDEX LV_TABIX.
* CONTINUE.
* ENDIF.
*
* CLEAR GS_DN_ITEM_CTR.
* GS_DN_ITEM_CTR-DELIV_NUMB = LS_LIPS-VBELN.
* GS_DN_ITEM_CTR-DELIV_ITEM = LS_LIPS-POSNR.
* GS_DN_ITEM_CTR-CHG_DELQTY = 'X'.
* APPEND GS_DN_ITEM_CTR TO GT_DN_ITEM_CTR.
*
* MODIFY LT_LIPS FROM LS_LIPS TRANSPORTING CHARG.
* ENDLOOP. * CHECK GT_DN_ITEM IS NOT INITIAL. * GS_DN_HEADER-DELIV_NUMB = LV_DELIVERY.
* GS_DN_HEADER_CTR-DELIV_NUMB = LV_DELIVERY.
* GS_DN_HEADER-SHIP_COND = IM_SHIP_COND."Ship condition
* GS_DN_HEADER_CTR-SHIP_COND_FLG = 'X'."Ship condition
* GV_DN_NUMBER = LV_DELIVERY. * Update Batch to DN
* CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
* EXPORTING
* HEADER_DATA = GS_DN_HEADER
* HEADER_CONTROL = GS_DN_HEADER_CTR
* DELIVERY = GV_DN_NUMBER
* TABLES
* ITEM_DATA = GT_DN_ITEM
* ITEM_CONTROL = GT_DN_ITEM_CTR
* RETURN = GT_RETURN. * LV_VBELN = LV_DELIVERY.
* CALL FUNCTION 'Z_SD_CHANGE_DN'
* EXPORTING
* IM_DELIVERY = LV_VBELN
* IM_SHIP_TYPE = IM_SHIP_TYPE
* TABLES
* IT_ITEM = LT_LIPS
* IT_RETURN = GT_RETURN.
*
*
* LOOP AT GT_RETURN TRANSPORTING NO FIELDS WHERE TYPE = 'A' OR
* TYPE = 'E' OR
* TYPE = 'X'.
* EXIT.
* ENDLOOP.
*
* IF SY-SUBRC EQ 0.
* CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
*
* APPEND LINES OF GT_RETURN TO IT_RETURN.
*
* PERFORM FRM_CONVERT_MESSAGE TABLES GT_RETURN CHANGING E_MESSAGE.
** Delete Delivery Note
* PERFORM FRM_DELETE_DELIVERY USING LV_VBELN.
*
* E_STATUS = 'E'.
* ELSE.
* CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
* WAIT = 'X'.
*
* E_STATUS = 'S'.
* E_DN = LV_DELIVERY.
* ENDIF.
*
* REFRESH: LT_SO_ITEMS,
* LT_STO_ITEMS,
* LT_LIPS.
*
* APPEND LINES OF GT_RETURN TO IT_RETURN.
*
* IT_DN-VBELN = LV_DELIVERY.
* APPEND IT_DN.
ENDDO. ENDFUNCTION. ZSDS0008结构:
KUNNR 类型 KUNNR
VBELN 类型 VBELN
POSNR 类型 POSNR
CHARG 类型 CHARG_D