無名经验总结与分享(SAP)            十年树木,百年树人

WMS函数组:13.WMS入库BAPI

FUNCTION ZRFC_WMS_GOODSMVT_CREATE_D.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(IT_ZMKPF002) LIKE  ZWMS_GOODSMOVE_HEAD STRUCTURE
*"        ZWMS_GOODSMOVE_HEAD
*"  EXPORTING
*"     REFERENCE(EP_SUBRC) LIKE  SY-SUBRC
*"     REFERENCE(EP_MATERIALDOCUMENT) LIKE
*"        BAPI2017_GM_HEAD_RET-MAT_DOC
*"     REFERENCE(EP_MATDOCUMENTYEAR) LIKE
*"        BAPI2017_GM_HEAD_RET-DOC_YEAR
*"  TABLES
*"      ET_LOG STRUCTURE  BAPIRET2
*"      IT_ZMKPF001 STRUCTURE  ZWMS_GOODSMOVE_ITEM
*"      IT_R STRUCTURE  BAPI2017_GM_ITEM_CREATE
*"----------------------------------------------------------------------
*GM_CODE 01: GOODS RECEIPT FOR PURCHASE ORDER
*GM_CODE 02: GOODS RECEIPT FOR PRODUCTION ORDER
*GM_CODE 03: GOODS ISSUE
*GM_CODE 04: TRANSFER POSTING
*GM_CODE 05: OTHER GOODS RECEIPTS
*GM_CODE 06: REVERSAL OF GOODS MOVEMENTS
*GM_CODE 07: SUBSEQUENT ADJUSTMENT TO A SUBCONTRACT ORDER
* DOMAIN: KZBEW - MOVEMENT INDICATOR
*      GOODS MOVEMENT W/O REFERENCE
* B - GOODS MOVEMENT FOR PURCHASE ORDER
* F - GOODS MOVEMENT FOR PRODUCTION ORDER
* L - GOODS MOVEMENT FOR DELIVERY NOTE
* K - GOODS MOVEMENT FOR KANBAN REQUIREMENT (WM - INTERNAL ONLY)
* O - SUBSEQUENT ADJUSTMENT OF "MATERIAL-PROVIDED" CONSUMPTION
* W - SUBSEQUENT ADJUSTMENT OF PROPORTION/PRODUCT UNIT MATERIAL
*GM_CODE 01: GOODS RECEIPT FOR PURCHASE ORDER
*GM_CODE 02: GOODS RECEIPT FOR PRODUCTION ORDER
*GM_CODE 03: GOODS ISSUE
*GM_CODE 04: TRANSFER POSTING
*GM_CODE 05: OTHER GOODS RECEIPTS
*GM_CODE 06: REVERSAL OF GOODS MOVEMENTS
*GM_CODE 07: SUBSEQUENT ADJUSTMENT TO A SUBCONTRACT ORDER
*"----------------------------------------------------------------------
DATA:
    GOODSMVT_CODE       LIKE  IT_ZMKPF002-GM_CODE,
    IP_GMHEAD           LIKE  BAPI2017_GM_HEAD_01     OCCURS    0   WITH HEADER  LINE,
    IP_GMCODE           LIKE  BAPI2017_GM_CODE        OCCURS    0   WITH HEADER  LINE,
    IP_MTHEAD           LIKE  BAPI2017_GM_HEAD_RET    OCCURS    0   WITH HEADER  LINE,
    IP_MITEM            LIKE  BAPI2017_GM_ITEM_CREATE OCCURS    0   WITH HEADER  LINE,
    IP_MITEM2           LIKE  BAPI2017_GM_ITEM_CREATE OCCURS    0   WITH HEADER  LINE,
    EP_GOODSMVT_HEADRET LIKE  BAPI2017_GM_HEAD_RET OCCURS    0   WITH HEADER  LINE,
    I_ERROR_MESSAGES    LIKE  ARRANG_ERR,
    IT_RESB             LIKE RESB OCCURS    0   WITH HEADER  LINE,
    IT_MARA             LIKE MARA OCCURS    0   WITH HEADER  LINE,
    IT_EKPO             LIKE EKPO OCCURS    0   WITH HEADER  LINE,
    IT_EKET             LIKE EKET OCCURS    0   WITH HEADER  LINE,
    I_LOG               LIKE  BAPIRET2        OCCURS 0 WITH HEADER LINE.
DATA :
BEGIN OF IT_MSLB OCCURS 0 ,
      MATNR     LIKE MSLB-MATNR,
      LBLAB     LIKE MSLB-LBLAB,
      CHARG     LIKE MSLB-CHARG,
      ENTRY_QNT LIKE IP_MITEM2-ENTRY_QNT,
END OF IT_MSLB .
CLEAR :
    IP_GMHEAD,
    IP_GMCODE ,
    IP_MTHEAD,
    IP_MITEM ,
    I_LOG,
    EP_GOODSMVT_HEADRET.
REFRESH:
    IP_GMHEAD,
    IP_GMCODE ,
    IP_MTHEAD,
    IP_MITEM ,
    I_LOG,
    EP_GOODSMVT_HEADRET.
MOVE-CORRESPONDING IT_ZMKPF002 TO IP_GMHEAD.
**********  HEADER      凭证日期
IF IT_ZMKPF002-DOC_DATE IS NOT INITIAL.
    IP_GMHEAD-DOC_DATE = IT_ZMKPF002-DOC_DATE    .
ELSE.
    IP_GMHEAD-DOC_DATE  = SY-DATUM  .
ENDIF.
**********  HEADER      记帐日期
IF IT_ZMKPF002-PSTNG_DATE IS NOT INITIAL.
    IP_GMHEAD-PSTNG_DATE = IT_ZMKPF002-PSTNG_DATE    .
ELSE.
    IP_GMHEAD-PSTNG_DATE = SY-DATUM .
ENDIF.
READ TABLE IT_ZMKPF001 INDEX 1 .
****特殊入库日期控制
IF SY-DATUM BETWEEN '20161227' AND '20161231' AND IT_ZMKPF001-PLANT <> '1168'.
    IP_GMHEAD-DOC_DATE = '20170101'.
    IP_GMHEAD-PSTNG_DATE = '20170101'.
ENDIF.
*******下面取渠道
IF IT_ZMKPF002-GM_CODE = '03'.
IF IP_GMHEAD-REF_DOC_NO IS INITIAL.
SPLIT IT_ZMKPF002-HEADER_TXT AT '~' INTO IP_GMHEAD-HEADER_TXT IP_GMHEAD-REF_DOC_NO.
ENDIF.
ELSE.
**********  HEADER     外部交货单编号
    IP_GMHEAD-REF_DOC_NO = IT_ZMKPF002-LFSNR.
**********  HEADER     凭证抬头文本
    IP_GMHEAD-HEADER_TXT = IT_ZMKPF002-HEADER_TXT   .
ENDIF.
**********  HEADER 事务代码
  GOODSMVT_CODE = IT_ZMKPF002-GM_CODE.
  IP_GMCODE-GM_CODE  = GOODSMVT_CODE.
**********  HEADER    用户名
  IP_GMHEAD-PR_UNAME = SY-UNAME.
****************处理特殊移动类型
*IF IP_GMHEAD-MOVE
*IP_GMHEAD-ER_GR_GI_SLIP
DATA:TMP_LINE  LIKE IP_MITEM-LINE_ID, TMP_LFSNR LIKE IT_ZMKPF001-LFSNR.
CLEAR:    TMP_LFSNR.
  TMP_LFSNR = IT_ZMKPF002-LFSNR.
***********明细
*----------------CHA JWY-20170811
SORT IT_ZMKPF001.
*----------------CHA JWY-20170811
LOOP AT IT_ZMKPF001.
IF IT_ZMKPF001-LFSNR =  TMP_LFSNR.
MOVE-CORRESPONDING  IT_ZMKPF001  TO IP_MITEM.
SELECT SINGLE LIFNR INTO IP_MITEM-VENDOR FROM EKKO WHERE EBELN = IT_ZMKPF001-PO_NUMBER.
PERFORM F_ADD_ZERO USING IP_MITEM-MATERIAL.
PERFORM F_ADD_ZERO USING IP_MITEM-MOVE_MAT.
PERFORM F_ADD_ZERO USING IP_MITEM-ORDERID.
PERFORM F_ADD_ZERO USING IP_MITEM-COSTCENTER.
      IP_MITEM-PO_PR_QNT = IP_MITEM-ENTRY_QNT.
IF IT_ZMKPF001-PROFIT_CTR = '' AND IT_ZMKPF001-COSTCENTER <> ''.
PERFORM F_ADD_ZERO USING IT_ZMKPF001-COSTCENTER.
SELECT SINGLE PRCTR INTO IP_MITEM-PROFIT_CTR FROM CSKS
WHERE DATBI = '99991231' AND KOSTL = IT_ZMKPF001-COSTCENTER.
ENDIF.
IF IT_ZMKPF001-BATCH IS NOT INITIAL.
**********  ITEM  批号
        IP_MITEM-BATCH = IT_ZMKPF001-BATCH .
* DEL BY XUESONG 20180307 START
*      ELSE.
*        IP_MITEM-BATCH = SY-DATUM.
* DEL BY XUESONG 20180307 END
ENDIF.
*=====行号===========================
CLEAR TMP_LINE.
DESCRIBE TABLE IP_MITEM LINES TMP_LINE .
      TMP_LINE = TMP_LINE + 1.
PERFORM F_ADD_ZERO USING TMP_LINE.
      IP_MITEM-LINE_ID = TMP_LINE .
      IP_MITEM-PARENT_ID = '000000'.
APPEND IP_MITEM.
ENDIF.
ENDLOOP. "end of IT_ZMKPF001
CLEAR IP_MITEM2[].
APPEND LINES OF IP_MITEM TO IP_MITEM2.
DATA:  T_BATCH  LIKE IP_MITEM-BATCH, T_QUNTRY LIKE IP_MITEM-ENTRY_QNT.
*----------------CHA JWY-20170811
SORT IP_MITEM2.
*----------------CHA JWY-20170811
LOOP AT IP_MITEM2 INTO IP_MITEM .
*******============以下是更新组件================
CLEAR : IT_EKPO[],IT_RESB[],IT_MARA[],T_QUNTRY .
SELECT SINGLE * INTO IT_EKPO FROM EKPO WHERE EBELN = IP_MITEM-PO_NUMBER AND EBELP = IP_MITEM-PO_ITEM.
SELECT SINGLE * INTO IT_EKET FROM EKET   WHERE EBELN = IP_MITEM-PO_NUMBER AND EBELP =  IP_MITEM-PO_ITEM.
SELECT * INTO TABLE IT_RESB FROM RESB WHERE RSNUM = IT_EKET-RSNUM. "取到所有组件信息
IF IT_RESB[] IS NOT INITIAL.
SELECT * INTO TABLE IT_MARA FROM MARA FOR ALL ENTRIES IN  IT_RESB WHERE MATNR = IT_RESB-MATNR.
ENDIF.
    T_BATCH =  IP_MITEM-BATCH.
    T_QUNTRY = IP_MITEM-ENTRY_QNT. "记录交货数量
    IP_MITEM-PARENT_ID = IP_MITEM-LINE_ID.
*----------------CHA JWY-20170811
SORT IT_MARA.
*----------------CHA JWY-20170811
LOOP AT IT_MARA .
CLEAR IP_MITEM-MVT_IND . "组件不需要些标识
CLEAR IP_MITEM-PO_PR_QNT . "组件不需要这个数量
      IP_MITEM-MATERIAL = IT_MARA-MATNR.
IF IP_MITEM-MOVE_TYPE   <>  '541'.
        IP_MITEM-MOVE_TYPE   =  '543'.
ELSE.
        IP_MITEM-SPEC_MVMT = 'X'.
ENDIF.
      IP_MITEM-SPEC_STOCK =  'O'. "消耗特殊的库存标识"
      IP_MITEM-BATCH = T_BATCH .
IF IT_MARA-XCHPF = ' '. "如果不需要进行批次管理"
CLEAR:  IP_MITEM-BATCH . "不需要进行批次管理,清除批次
ENDIF.
*----------------CHA JWY-20170811
SORT IT_RESB.
*----------------CHA JWY-20170811
LOOP AT  IT_RESB.
IF IT_RESB-MATNR = IT_MARA-MATNR.
          IP_MITEM-ENTRY_QNT =  T_QUNTRY * IT_RESB-ERFMG / IT_EKPO-MENGE. "计算组件的消耗数量
ENDIF.
ENDLOOP.
CLEAR IP_MITEM-STGE_LOC .
      IP_MITEM-LINE_DEPTH = '01'.
PERFORM F_ADD_ZERO USING IP_MITEM-MATERIAL.
*==================================
CLEAR : TMP_LINE.
DESCRIBE TABLE IP_MITEM LINES TMP_LINE.
      IP_MITEM-LINE_ID = TMP_LINE + 1.
PERFORM F_ADD_ZERO USING IP_MITEM-LINE_ID .
APPEND IP_MITEM.
ENDLOOP. " end of IT_MARA
CLEAR  IP_MITEM.
ENDLOOP." end of IP_MITEM2
***自动查找批次*****************************************
CLEAR IP_MITEM2[].
APPEND LINES OF IP_MITEM TO IP_MITEM2.
*----------------CHA JWY-20170811
SORT IP_MITEM2.
*----------------CHA JWY-20170811
LOOP AT IP_MITEM2 WHERE SPEC_STOCK = 'O'.
DATA: V_LBLAB LIKE MSLB-LBLAB.
CLEAR V_LBLAB.
SELECT SUM( LBLAB ) INTO V_LBLAB
FROM MSLB
WHERE MATNR = IP_MITEM2-MATERIAL
AND LIFNR = IP_MITEM2-VENDOR.
IF V_LBLAB < IP_MITEM2-ENTRY_QNT.
      ET_LOG-TYPE =  'E' .
CONCATENATE IP_MITEM2-MATERIAL '消耗件不足' INTO  ET_LOG-MESSAGE.
CONCATENATE IP_MITEM2-PO_NUMBER '-' IP_MITEM2-PO_ITEM  INTO ET_LOG-ID.
APPEND  ET_LOG.
ENDIF.
ENDLOOP.
READ TABLE ET_LOG WITH KEY TYPE =  'E' .
IF SY-SUBRC = 0.
APPEND LINES OF IP_MITEM TO IT_R. "显示输入参数便于调试
    EP_SUBRC  =  -1.
RETURN.
ENDIF.
*  CHECK SY-SUBRC > 0.
DELETE IP_MITEM2 WHERE SPEC_STOCK <> 'O' OR BATCH IS  INITIAL .
***********项目中存在分包数
IF IP_MITEM2[] IS NOT INITIAL.
*****     第一步找到相关的供应商库存
SELECT *
INTO CORRESPONDING FIELDS OF TABLE IT_MSLB
FROM MSLB
FOR ALL ENTRIES IN IP_MITEM2
WHERE MATNR = IP_MITEM2-MATERIAL
AND LIFNR = IP_MITEM2-VENDOR
AND LBLAB > '0'.
SORT IT_MSLB DESCENDING BY LBLAB DESCENDING.
DATA:   NUM1  LIKE MSLB-LBLAB,
            NUM2  LIKE MSLB-LBLAB,
            CLINE LIKE  IP_MITEM2-LINE_ID.
*****     第二步 根据交货数量自动匹配取得相关供应商库存数量
CLEAR: IP_MITEM2 , IP_MITEM2[] .
*----------------CHA JWY-20170811
SORT IP_MITEM.
*----------------CHA JWY-20170811
LOOP AT IP_MITEM.
IF IP_MITEM-SPEC_STOCK = 'O'.                         "   ------1
IF IP_MITEM-BATCH IS NOT  INITIAL. "-----2如果需要进行批次管理
CLEAR: NUM1,NUM2.
          NUM1 = IP_MITEM-ENTRY_QNT. "取总数量
          CLINE = IP_MITEM2-LINE_ID.
MOVE IP_MITEM TO IP_MITEM2.
DESCRIBE TABLE IP_MITEM LINES CLINE.
          CLINE = CLINE + 1. "注意要取行加1,不然会有重复
DELETE IT_MSLB WHERE LBLAB IS INITIAL. "删除已经使用完的记录
*----------------CHA JWY-20170811
SORT IT_MSLB.
*----------------CHA JWY-20170811
LOOP AT    IT_MSLB WHERE MATNR = IP_MITEM2-MATERIAL."=====3
IF IT_MSLB-LBLAB >= NUM1.                       " =======4
              IP_MITEM2-ENTRY_QNT = NUM1.
              IP_MITEM2-BATCH = IT_MSLB-CHARG.
DESCRIBE TABLE IP_MITEM2 LINES TMP_LINE.
              IP_MITEM2-LINE_ID = TMP_LINE + CLINE.
PERFORM F_ADD_ZERO USING IP_MITEM2-LINE_ID.
APPEND  IP_MITEM2.
              IT_MSLB-LBLAB = IT_MSLB-LBLAB - NUM1.
MODIFY IT_MSLB.
EXIT.
ELSE.
              IP_MITEM2-ENTRY_QNT = IT_MSLB-LBLAB.
              IP_MITEM2-BATCH = IT_MSLB-CHARG.
DESCRIBE TABLE IP_MITEM2 LINES TMP_LINE.
              IP_MITEM2-LINE_ID = TMP_LINE + CLINE.
PERFORM F_ADD_ZERO USING IP_MITEM2-LINE_ID.
APPEND IP_MITEM2.
              NUM1 = NUM1 - IT_MSLB-LBLAB.
              IT_MSLB-LBLAB = 0.
MODIFY IT_MSLB.
ENDIF." ====================4===END
ENDLOOP. "==================3======END
ENDIF.  "==================2======END
ENDIF.   "==================1======END
ENDLOOP.
****第三步 删除原有的供应商库存数量
DELETE IP_MITEM WHERE SPEC_STOCK = 'O' AND BATCH <> '' .
****第四步 加入已经拆分好的供应商库存
APPEND LINES OF IP_MITEM2 TO IP_MITEM.
ENDIF.
*----------------CHA JWY-20170811
SORT IP_MITEM.
*----------------CHA JWY-20170811
LOOP AT IP_MITEM WHERE ORDERID IS NOT INITIAL AND MOVE_TYPE = '261'.
SELECT SINGLE RSNUM RSPOS INTO (IP_MITEM-RESERV_NO, IP_MITEM-RES_ITEM)
FROM RESB WHERE AUFNR = IP_MITEM-ORDERID AND MATNR =  IP_MITEM-MATERIAL.
MODIFY IP_MITEM.
ENDLOOP.
***以下是进行过帐处理*******************************************
* ADD BY XUESONG 20180210 START
CLEAR I_LOG[].
* ADD BY XUESONG 20180210 END
"change by damon at 20171205.
CALL FUNCTION 'ZRFC_WMS_GOODSMVT_SPLIT'
EXPORTING
      IP_GMHEAD = IP_GMHEAD
      IP_GMCODE = IP_GMCODE
TABLES
* ADD BY XUESONG 20180210 START
      I_LOG     = I_LOG[]
* ADD BY XUESONG 20180210 END
      IP_MITEM  = IP_MITEM[]
*     IP_MITEM  =
.
* ADD BY XUESONG 20180210 START
IF I_LOG[] IS NOT INITIAL.
LOOP AT I_LOG.
IF I_LOG-TYPE = 'E'.
CONCATENATE TMP_LFSNR I_LOG-MESSAGE INTO ET_LOG-MESSAGE.
        ET_LOG-TYPE =  I_LOG-TYPE .
        ET_LOG-ID               = I_LOG-ID .
        ET_LOG-NUMBER           = I_LOG-NUMBER.
        ET_LOG-LOG_NO           = I_LOG-LOG_NO.
APPEND  ET_LOG.
ENDIF.
ENDLOOP.
CLEAR I_LOG[].
READ  TABLE   I_LOG WITH  KEY  TYPE   = 'E'.
IF  SY-SUBRC   =   0  .
      EP_SUBRC  =  -1.
ENDIF.
RETURN.
ENDIF.
* ADD BY XUESONG 20180210 END
APPEND LINES OF IP_MITEM TO IT_R. "显示输入参数便于调试
CLEAR I_LOG[].
* DEL BY XUESONG 20180316 START
*  LOOP AT IP_MITEM. "临时方案,解决扣板批次
*
*    DATA: MATNR_201801 TYPE MATNR.
*
*    SELECT SINGLE MATNR INTO MATNR_201801 FROM ZSDA35 WHERE MATNR = IP_MITEM-MATERIAL.
*    IF SY-SUBRC = 0.
*      CLEAR IP_MITEM-BATCH.
*      MODIFY IP_MITEM.
*    ENDIF.
*
*  ENDLOOP.
* DEL BY XUESONG 20180316 END
DATA: ITH LIKE IT_ZMKPF001 OCCURS 0 WITH HEADER LINE.
DATA: ITD LIKE IP_MITEM OCCURS 0 WITH HEADER LINE.
LOOP AT IT_ZMKPF001.
MOVE IT_ZMKPF001-MATERIAL TO ITH-MATERIAL.
MOVE IT_ZMKPF001-ENTRY_QNT TO ITH-ENTRY_QNT.
MOVE IT_ZMKPF001-MOVE_MAT TO ITH-MOVE_MAT.
COLLECT ITH.
ENDLOOP.
DATA: ENTRY_QNT LIKE IT_ZMKPF001-ENTRY_QNT.
LOOP AT ITH.
CLEAR ENTRY_QNT.
LOOP AT IP_MITEM WHERE MATERIAL = ITH-MATERIAL AND MOVE_MAT = ITH-MOVE_MAT.
      ENTRY_QNT =  ENTRY_QNT + IP_MITEM-ENTRY_QNT.
ENDLOOP.
IF ITH-ENTRY_QNT <> ENTRY_QNT.
CLEAR ET_LOG.
CONCATENATE TMP_LFSNR '数量不一致' INTO ET_LOG-MESSAGE.
      ET_LOG-TYPE = 'E'.
MOVE ITH-MATERIAL TO   ET_LOG-MESSAGE_V1.
MOVE ITH-MOVE_MAT TO   ET_LOG-MESSAGE_V2.
MOVE ITH-ENTRY_QNT TO   ET_LOG-MESSAGE_V3.
MOVE ENTRY_QNT TO   ET_LOG-MESSAGE_V4.
APPEND  ET_LOG.
ENDIF.
ENDLOOP.
READ TABLE ET_LOG WITH KEY = 'E'.
CHECK  SY-SUBRC   >   0  .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
      GOODSMVT_HEADER  = IP_GMHEAD
      GOODSMVT_CODE    = IP_GMCODE
IMPORTING
      GOODSMVT_HEADRET = EP_GOODSMVT_HEADRET
      MATERIALDOCUMENT = EP_MATERIALDOCUMENT
      MATDOCUMENTYEAR  = EP_MATDOCUMENTYEAR
TABLES
      GOODSMVT_ITEM    = IP_MITEM
RETURN           = I_LOG.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
CLEAR:IP_MITEM[].
*----------------CHA JWY-20170811
SORT I_LOG.
*----------------CHA JWY-20170811
LOOP AT I_LOG.
IF I_LOG-TYPE = 'E'.
CONCATENATE TMP_LFSNR I_LOG-MESSAGE INTO ET_LOG-MESSAGE.
      ET_LOG-TYPE =  I_LOG-TYPE .
      ET_LOG-ID               = I_LOG-ID .
      ET_LOG-NUMBER           = I_LOG-NUMBER.
      ET_LOG-LOG_NO           = I_LOG-LOG_NO.
APPEND  ET_LOG.
ENDIF.
ENDLOOP.
CLEAR I_LOG[].
READ  TABLE   I_LOG WITH  KEY  TYPE   = 'E'.
IF  SY-SUBRC   =   0  .
    EP_SUBRC  =  -1.
ENDIF.
ENDFUNCTION.

image

image

posted @ 2021-01-25 21:10  無名  阅读(173)  评论(0编辑  收藏  举报
Twttafku@163.com
十年树木,百年树人