FUNCTION ZCRM0190_RFC.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(IM_VBLEN) TYPE VBELN_VL OPTIONAL
*" EXPORTING
*" VALUE(MSG) TYPE MSG
*" VALUE(STATUS) TYPE CHAR1
*"----------------------------------------------------------------------
DATA:LS_VBELN TYPE LIPS-VBELN.
DATA:IT_LIPS TYPE STANDARD TABLE OF LIPS,
WA_LIPS TYPE LIPS.
DATA:IT_MSKU TYPE STANDARD TABLE OF MSKU,
WA_MSKU TYPE MSKU.
DATA:LFIMG_SY TYPE LFIMG, "剩余数量
LS_KULAB_SY TYPE MSKU-KULAB, "非限制库存剩余
LS_KUNNR TYPE LIKP-KUNAG. "售达方
"拣配
DATA:IT_VBPOK TYPE STANDARD TABLE OF VBPOK,
WA_VBPOK TYPE VBPOK,
IT_PROTT TYPE STANDARD TABLE OF PROTT,
WA_PROTT TYPE PROTT,
WA_VBKOK TYPE VBKOK.
*BAPI 使用定义
"发货过账
DATA: HEADER_DATA TYPE BAPIOBDLVHDRCON,
HEADER_CONTROL TYPE BAPIOBDLVHDRCTRLCON,
DELIVERY TYPE BAPIOBDLVHDRCON-DELIV_NUMB,
ITEM TYPE STANDARD TABLE OF BAPIOBDLVITEMCON,
WA_ITEM TYPE BAPIOBDLVITEMCON,
HEADER_DEADLINES TYPE STANDARD TABLE OF BAPIDLVDEADLN,
WA_HEADER_DEADLINES TYPE BAPIDLVDEADLN,
RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE.
"批次拆分
DATA :LT_HEADER_DATA LIKE BAPIOBDLVHDRCHG .
DATA :LT_HEADER_CONTROL LIKE BAPIOBDLVHDRCTRLCHG .
DATA :I_DELIVERY_NO LIKE BAPIOBDLVHDRCHG-DELIV_NUMB .
DATA :DELIV_ITEM LIKE BAPIOBDLVITEMCHG-DELIV_ITEM .
DATA :TECHN_CONTROL LIKE BAPIDLVCONTROL .
DATA :ITEM_DATA LIKE BAPIOBDLVITEMCHG OCCURS 0 WITH HEADER LINE .
DATA :ITEM_CONTROL LIKE BAPIOBDLVITEMCTRLCHG OCCURS 0 WITH HEADER LINE .
DATA :ITEM_SERIAL_NO LIKE BAPIDLVITMSERNO OCCURS 0 WITH HEADER LINE .
DATA: BATCH_FLAG TYPE C. "拆分批次成功标识
DATA :STR TYPE STRING .
DATA:IM_KOSTK TYPE VBUK-KOSTK. "拣配状态
DATA:LFSTK_FLAG TYPE C. "是否发货
*交货单批次拆分
*交货单拣配
*交货单过账
"初始交货单拆分行项目
CLEAR DELIV_ITEM .
DELIV_ITEM = '900000' .
DATA IT_TIMES TYPE STRING.
"参数补充前导0
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = IM_VBLEN
IMPORTING
OUTPUT = LS_VBELN.
*拣配状态
SELECT SINGLE KOSTK INTO IM_KOSTK FROM VBUK WHERE VBELN = LS_VBELN.
IF LFSTK_FLAG = 'C'.
LFSTK_FLAG = 'S'.
ELSE.
"售达方
SELECT SINGLE KUNAG INTO LS_KUNNR FROM LIKP WHERE VBELN = LS_VBELN.
*客户寄售库存
SELECT SINGLE WERKS INTO CORRESPONDING FIELDS OF WA_LIPS
FROM LIPS WHERE VBELN = LS_VBELN.
SELECT A~MATNR A~WERKS A~CHARG A~KULAB A~KUNNR A~SOBKZ A~KUEIN
INTO CORRESPONDING FIELDS OF TABLE IT_MSKU FROM MSKU AS A
INNER JOIN MARA AS B ON A~MATNR = B~MATNR
INNER JOIN MCHA AS C ON A~MATNR = C~MATNR AND A~WERKS = C~WERKS AND
A~CHARG = C~CHARG
WHERE A~WERKS = WA_LIPS-WERKS
AND A~KUNNR = LS_KUNNR.
CLEAR: WA_LIPS.
SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_LIPS FROM LIPS
WHERE LIPS~VBELN = LS_VBELN.
LOOP AT IT_LIPS INTO WA_LIPS.
LFIMG_SY = WA_LIPS-LFIMG. "实际交货数量
"批次拆分原行项目 数量修改
ITEM_DATA-DELIV_NUMB = LS_VBELN. "交货单
ITEM_DATA-DELIV_ITEM = WA_LIPS-POSNR. "批次拆分前数量 '0010'
ITEM_DATA-HIERARITEM = WA_LIPS-VGPOS. "销售订单行项目
ITEM_DATA-USEHIERITM = '1'."使用层次项目
ITEM_DATA-MATERIAL = WA_LIPS-MATNR. "物料号
ITEM_DATA-DLV_QTY = 0. "修改原交货单行项目 数量 ,过账清0
ITEM_DATA-FACT_UNIT_NOM = 1. "销售数量转换成SKU的分子(因子)
ITEM_DATA-FACT_UNIT_DENOM = 1. "销售数量转换为 SKU 的值(除数)
APPEND ITEM_DATA.
CLEAR ITEM_DATA.
"交货单原行项目修改标识
ITEM_CONTROL-DELIV_NUMB = LS_VBELN.
ITEM_CONTROL-DELIV_ITEM = WA_LIPS-POSNR.
ITEM_CONTROL-CHG_DELQTY = 'X'."修改标识
APPEND ITEM_CONTROL.
CLEAR ITEM_CONTROL.
"
"拣配行项目
WA_VBPOK-VBELN_VL = LS_VBELN."交货单
WA_VBPOK-POSNR_VL = WA_LIPS-POSNR."交货单行项目
WA_VBPOK-VBELN = WA_LIPS-VGBEL."销售订单
WA_VBPOK-POSNN = WA_LIPS-VGPOS."销售订单行项目
WA_VBPOK-MATNR = WA_LIPS-MATNR.
WA_VBPOK-CHARG = WA_LIPS-CHARG. "批次
WA_VBPOK-WERKS = WA_LIPS-WERKS. "工厂
WA_VBPOK-LFIMG = WA_LIPS-LFIMG. "实际已交货量(按销售单位)
WA_VBPOK-LGMNG = WA_LIPS-LGMNG. "仓库保管交货量
APPEND WA_VBPOK TO IT_VBPOK.
CLEAR:WA_VBPOK .
*
LOOP AT IT_MSKU INTO WA_MSKU WHERE WERKS = WA_LIPS-WERKS
AND MATNR = WA_LIPS-MATNR AND KULAB <> 0.
IF LFIMG_SY <> 0.
"批次拆分行项目
DELIV_ITEM = DELIV_ITEM + 1 .
ITEM_DATA-DELIV_NUMB = LS_VBELN.
ITEM_DATA-DELIV_ITEM = DELIV_ITEM . "拆分批次后行项目 900001
ITEM_DATA-HIERARITEM = WA_LIPS-VGPOS. "'销售订单行项目
ITEM_DATA-USEHIERITM = '1' .
ITEM_DATA-MATERIAL = WA_LIPS-MATNR. "物料
ITEM_DATA-BATCH = WA_MSKU-CHARG. "批次
ITEM_DATA-FACT_UNIT_NOM = 1 .
ITEM_DATA-FACT_UNIT_DENOM = 1 .
IF LFIMG_SY > WA_MSKU-KULAB.
ITEM_DATA-DLV_QTY = WA_MSKU-KULAB.
LFIMG_SY = LFIMG_SY - WA_MSKU-KULAB. "剩余数量
WA_MSKU-KULAB = 0.
ELSEIF LFIMG_SY < WA_MSKU-KULAB.
ITEM_DATA-DLV_QTY = LFIMG_SY.
WA_MSKU-KULAB = WA_MSKU-KULAB - LFIMG_SY.
LFIMG_SY = 0 . "剩余数量
ELSE.
ITEM_DATA-DLV_QTY = WA_MSKU-KULAB.
LFIMG_SY = 0.
WA_MSKU-KULAB = 0.
ENDIF.
MODIFY IT_MSKU FROM WA_MSKU. "更新客户库存 去除已使用的库存
ITEM_CONTROL-DELIV_NUMB = LS_VBELN.
ITEM_CONTROL-DELIV_ITEM = DELIV_ITEM.
ITEM_CONTROL-CHG_DELQTY = 'X' . "交货单数量修改标识
APPEND ITEM_CONTROL .
CLEAR ITEM_CONTROL.
"拣配行项目
WA_VBPOK-VBELN_VL = LS_VBELN.
WA_VBPOK-POSNR_VL = DELIV_ITEM.
WA_VBPOK-VBELN = WA_LIPS-VGBEL.
WA_VBPOK-POSNN = WA_LIPS-VGPOS.
WA_VBPOK-MATNR = WA_LIPS-MATNR.
WA_VBPOK-CHARG = WA_LIPS-CHARG.
WA_VBPOK-WERKS = WA_LIPS-WERKS.
WA_VBPOK-LFIMG = ITEM_DATA-DLV_QTY .
WA_VBPOK-LGMNG = ITEM_DATA-DLV_QTY .
APPEND WA_VBPOK TO IT_VBPOK.
APPEND ITEM_DATA .
CLEAR:ITEM_DATA .
CLEAR:WA_MSKU,WA_ITEM,WA_VBPOK.
ELSE.
EXIT.
ENDIF.
ENDLOOP.
IF LFIMG_SY <> 0 .
STATUS = 'E'.
MSG = '非限制库存不满足实际交货数量!'.
RETURN.
ENDIF.
CLEAR:WA_LIPS.
ENDLOOP.
IF STATUS NE 'E'.
*拆分批次
I_DELIVERY_NO = LS_VBELN.
LT_HEADER_DATA-DELIV_NUMB = LS_VBELN.
TECHN_CONTROL-UPD_IND = 'U' .
LT_HEADER_CONTROL-DELIV_NUMB = LS_VBELN.
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
HEADER_DATA = LT_HEADER_DATA
HEADER_CONTROL = LT_HEADER_CONTROL
DELIVERY = I_DELIVERY_NO
TECHN_CONTROL = TECHN_CONTROL
TABLES
ITEM_DATA = ITEM_DATA
ITEM_CONTROL = ITEM_CONTROL
RETURN = RETURN.
READ TABLE RETURN WITH KEY TYPE = 'E' .
IF SY-SUBRC NE 0 .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
MSG = '批次拆分成功'.
*拣配
CLEAR:WA_VBKOK.
WA_VBKOK-VBELN_VL = LS_VBELN.
CALL FUNCTION 'SD_DELIVERY_UPDATE_PICKING'
EXPORTING
VBKOK_WA = WA_VBKOK
SYNCHRON = 'X'
IF_ERROR_MESSAGES_SEND = ''
TABLES
VBPOK_TAB = IT_VBPOK
PROT = IT_PROTT.
READ TABLE IT_PROTT INTO WA_PROTT WITH KEY MSGTY = 'E' .
IF SY-SUBRC NE 0 .
STR = '拣配成功'.
CONCATENATE MSG '/' STR INTO MSG.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
LFSTK_FLAG = 'S'.
ELSE.
LOOP AT IT_PROTT INTO WA_PROTT WHERE MSGTY CA 'AE' .
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
MSGID = WA_PROTT-MSGID
MSGNR = WA_PROTT-MSGNO
MSGV1 = WA_PROTT-MSGV1
MSGV2 = WA_PROTT-MSGV2
MSGV3 = WA_PROTT-MSGV3
MSGV4 = WA_PROTT-MSGV4
IMPORTING
MESSAGE_TEXT_OUTPUT = STR.
IF MSG IS INITIAL .
MOVE STR TO MSG .
ELSE.
CONCATENATE MSG '/' STR INTO MSG .
ENDIF .
ENDLOOP .
STATUS = 'E'.
ENDIF.
ELSE.
STATUS = 'E'.
MSG = '交货单拆分批次失败'.
ENDIF.
ENDIF.
ENDIF.
"过账发货
IF LFSTK_FLAG = 'S'..
"抬头
HEADER_DATA-DELIV_NUMB = LS_VBELN. "交货单号
HEADER_CONTROL-DELIV_NUMB = LS_VBELN. "交货单号
HEADER_CONTROL-POST_GI_FLG = 'X'. "过账状态
"DELIVERY = LS_VBELN. "交货单号
CLEAR: RETURN[].
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CONFIRM_DEC'
EXPORTING
HEADER_DATA = HEADER_DATA
HEADER_CONTROL = HEADER_CONTROL
DELIVERY = LS_VBELN
TABLES
RETURN = RETURN.
READ TABLE RETURN WITH KEY TYPE = 'E' .
IF SY-SUBRC NE 0 .
STATUS = 'S'.
STR = '发货成功'.
CONCATENATE MSG '/' STR INTO MSG.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ELSE.
STATUS = RETURN-TYPE .
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
MSGID = RETURN-ID
MSGNR = RETURN-NUMBER
MSGV1 = RETURN-MESSAGE_V1
MSGV2 = RETURN-MESSAGE_V2
MSGV3 = RETURN-MESSAGE_V3
MSGV4 = RETURN-MESSAGE_V4
IMPORTING
MESSAGE_TEXT_OUTPUT = STR.
CONCATENATE MSG '/' STR INTO MSG.
ENDIF.
ENDIF.
ENDFUNCTION.