首页 > 代码库 > 工单发料,退料等一些物料的移动

工单发料,退料等一些物料的移动

今天遇到个小问题,引以为戒。。。就复制代码过来吧

中间加了段查询的代码,RSART在BOM设置为中止,后继相关的时候会设置个值。。。这个值在过账的时候就很重要了。。。

吃一堑长一智

*&---------------------------------------------------------------------*
*&      Form  PRM_POST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PRM_POST .
  DATA: LV_LINE_ID TYPE I.
  DATA:GV_ERROR(1) TYPE C.
  DATA:LV_RSNUM TYPE RESB-RSNUM.
  DATA:LV_RSART TYPE RESB-RSART."add by ly 20161101 fro 中止后继物料

  IF MAT_DOC IS NOT INITIAL.
    MESSAGE 请不要重复过账! TYPE S DISPLAY LIKE E.
    EXIT.
  ENDIF.

  CLEAR:LV_LINE_ID,GMHEAD,GOODSMVT_ITEM[],GOODSMVT_SERIALNUMBER[],LT_ERRMSG[],GMCODE,MTHEAD,GT_MESSAGE[],GT_ZMMT001[].

  CASE GV_ZRENO+0(1).
    WHEN A OR B."生产领退料
      GMCODE-GM_CODE = 03.
    WHEN D OR E."反冲发退料
      GMCODE-GM_CODE = 04.
    WHEN C OR F."超领
      GMCODE-GM_CODE = 03.
    WHEN G."大工单领料
      GMCODE-GM_CODE = 03.
    WHEN OTHERS.
  ENDCASE.


  GMHEAD-PSTNG_DATE = SY-DATUM."mkpf-bldat.
  GMHEAD-DOC_DATE = SY-DATUM."mkpf-budat.
  GMHEAD-PR_UNAME = SY-UNAME.
  GMHEAD-REF_DOC_NO = GV_ZRENO.
  GMHEAD-BILL_OF_LADING = GV_ZRENO.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_ZMMT001 FROM ZMMT001 WHERE ZRENO = GV_ZRENO AND STATU = ‘‘.

  LOOP AT GT_TABLE INTO GW_TABLE.
    IF GW_TABLE-ZSHLT = 0.
      CONTINUE.
    ENDIF.
    ADD 1 TO LV_LINE_ID.
    GOODSMVT_ITEM-LINE_ID = LV_LINE_ID.
    GOODSMVT_ITEM-MATERIAL = GW_TABLE-MATNR.
    IF GV_ZRENO+0(1) = A.
      GOODSMVT_ITEM-PLANT  = GW_TABLE-WERKZ.
    ELSE.
      GOODSMVT_ITEM-PLANT  = GW_TABLE-WERKS.
    ENDIF.

    GOODSMVT_ITEM-BATCH    = GW_TABLE-CHARG.
    GOODSMVT_ITEM-STGE_LOC = GW_TABLE-FROM_LGORT.
    CLEAR:LV_RSNUM,LV_RSART.

    CASE GW_TABLE-ZRENO+0(1).
      WHEN A."订单发料
        GOODSMVT_ITEM-MOVE_TYPE  = 261.
        GOODSMVT_ITEM-RESERV_NO    =  GW_TABLE-RSNUM.
        GOODSMVT_ITEM-RES_ITEM    =  GW_TABLE-RSPOS.
        SELECT SINGLE RSNUM RSART  INTO (LV_RSNUM,LV_RSART)
          FROM RESB WHERE RSNUM = GW_TABLE-RSNUM
          AND RSPOS = GW_TABLE-RSPOS
          AND   MATNR = GW_TABLE-MATNR.
        IF LV_RSNUM IS INITIAL.
          GV_ERROR = X.
          GS_MESSAGE-MSGID = ZMSG.
          GS_MESSAGE-MSGTY = E.
          GS_MESSAGE-MSGNO = 000.
          GS_MESSAGE-MSGV1 = GW_TABLE-ZPOSN.
          GS_MESSAGE-MSGV2 = 该行订单物料已变更,不允许发料!.
          APPEND GS_MESSAGE TO GT_MESSAGE.
        ENDIF.
        GOODSMVT_ITEM-RES_TYPE = LV_RSART.

      WHEN B."订单退料
        GOODSMVT_ITEM-MOVE_TYPE  = 261.
        GOODSMVT_ITEM-RESERV_NO    =  GW_TABLE-RSNUM.
        GOODSMVT_ITEM-RES_ITEM    =  GW_TABLE-RSPOS.
        GOODSMVT_ITEM-ITEM_TEXT    =  GW_TABLE-SGTXT.
        GOODSMVT_ITEM-XSTOB    = X. "使用冲销移动类型标识符
        SELECT SINGLE RSNUM RSART INTO (LV_RSNUM,LV_RSART)
          FROM RESB WHERE RSNUM = GW_TABLE-RSNUM
          AND RSPOS = GW_TABLE-RSPOS
          AND   MATNR = GW_TABLE-MATNR.
        IF LV_RSNUM IS INITIAL.
          GV_ERROR = X.
          GS_MESSAGE-MSGID = ZMSG.
          GS_MESSAGE-MSGTY = E.
          GS_MESSAGE-MSGNO = 000.
          GS_MESSAGE-MSGV1 = GW_TABLE-ZPOSN.
          GS_MESSAGE-MSGV2 = 该行订单物料已变更,不允许发料!.
          APPEND GS_MESSAGE TO GT_MESSAGE.
        ENDIF.
        GOODSMVT_ITEM-RES_TYPE = LV_RSART.

      WHEN C."超领发料
        GOODSMVT_ITEM-MOVE_TYPE  = 261.
        GOODSMVT_ITEM-ORDERID    =  GW_TABLE-AUFNR.
      WHEN D."反冲发料
        GOODSMVT_ITEM-MOVE_TYPE  = 311.
        GOODSMVT_ITEM-MOVE_STLOC = GW_TABLE-LGORT.
      WHEN E."反冲退料
        GOODSMVT_ITEM-MOVE_TYPE  = 311.
        GOODSMVT_ITEM-MOVE_STLOC = GW_TABLE-LGORT.
      WHEN F."超领退料
        GOODSMVT_ITEM-MOVE_TYPE  = 261.
        GOODSMVT_ITEM-ORDERID    =  GW_TABLE-AUFNR.
      WHEN G."大工单领料
        GOODSMVT_ITEM-MOVE_TYPE  = 261.
        GOODSMVT_ITEM-ORDERID    =  GW_TABLE-AUFNR.
      WHEN OTHERS.
    ENDCASE.

    IF GW_TABLE-ZSHLT < 0.
      GOODSMVT_ITEM-MOVE_TYPE  = 531.
      GW_TABLE-ZSHLT = GW_TABLE-ZSHLT * -1.
    ENDIF.



    GOODSMVT_ITEM-ENTRY_QNT  = GW_TABLE-ZSHLT.  "结余数量.
    GOODSMVT_ITEM-ENTRY_UOM  = GW_TABLE-MEINS.  "
    IF GW_TABLE-ZSHLT > GW_TABLE-ZJHLT AND ( GW_TABLE-ZRENO+0(1) = A OR GW_TABLE-ZRENO+0(1) = B ) AND GW_TABLE-ZJHLT > 0.
      GV_ERROR = X.
      MESSAGE 普通领料不能超量发料 TYPE S DISPLAY LIKE E.
      EXIT.
    ENDIF.

    IF GW_TABLE-LGORT = ‘‘ AND GOODSMVT_ITEM-MOVE_TYPE = 311 AND GW_TABLE-AUFNR <> ‘‘.
      GV_ERROR = X.
      MESSAGE 超领/退料单需要有接收库位的工单 TYPE S DISPLAY LIKE E.
      EXIT.
    ENDIF.

    APPEND GOODSMVT_ITEM.

    LOOP AT GT_SEA INTO GW_SEA WHERE ZRENO = GW_TABLE-ZRENO AND ZPOSN = GW_TABLE-ZPOSN.
      GOODSMVT_SERIALNUMBER-MATDOC_ITM = LV_LINE_ID.
      IF GW_SEA-SERNR IS INITIAL.
        GV_ERROR = X.
        MESSAGE 请输入所有的序列号! TYPE S DISPLAY LIKE E.
        EXIT.
      ENDIF.
      GOODSMVT_SERIALNUMBER-SERIALNO = GW_SEA-SERNR.
      APPEND GOODSMVT_SERIALNUMBER.
    ENDLOOP.
  ENDLOOP.

  IF GOODSMVT_ITEM[] IS NOT INITIAL AND GV_ERROR = ‘‘.
    CALL FUNCTION BAPI_GOODSMVT_CREATE
      EXPORTING
        GOODSMVT_HEADER       = GMHEAD
        GOODSMVT_CODE         = GMCODE
      IMPORTING
        GOODSMVT_HEADRET      = MTHEAD
        MATERIALDOCUMENT      = MAT_DOC
      TABLES
        GOODSMVT_ITEM         = GOODSMVT_ITEM
        GOODSMVT_SERIALNUMBER = GOODSMVT_SERIALNUMBER
        RETURN                = LT_ERRMSG.

    IF MAT_DOC IS NOT INITIAL.
      CALL FUNCTION BAPI_TRANSACTION_COMMIT
        EXPORTING
          WAIT = X.

      LOOP AT GT_ZMMT001 INTO GW_ZMMT001.
        LOOP AT GT_TABLE INTO GW_TABLE WHERE ZRENO = GW_ZMMT001-ZRENO AND ZPOSN = GW_ZMMT001-ZPOSN.
          GW_ZMMT001-ZJHLT = GW_TABLE-ZJHLT.
          GW_ZMMT001-ZSHLT = GW_TABLE-ZSHLT.
*          IF GW_ZMMT001-ZJHLT = 0.
          GW_ZMMT001-STATU = C.
          GW_ZMMT001-ZNAME = SY-UNAME.
          GW_ZMMT001-ZDATE = SY-DATUM.
          GW_ZMMT001-ZTIME = SY-UZEIT.
*            UPDATE ZMMT001 SET ZSHLT = GW_TABLE-ZSHLT STATU = ‘C‘ WHERE ZRENO = GW_TABLE-ZRENO AND ZPOSN = GW_TABLE-ZPOSN.
*          ENDIF.
        ENDLOOP.
        MODIFY GT_ZMMT001 FROM GW_ZMMT001.
      ENDLOOP.

      MODIFY ZMMT001 FROM TABLE GT_ZMMT001.
*      COMMIT WORK.

*      MESSAGE MAT_DOC TYPE ‘S‘.
      GS_MESSAGE-MSGID = ZMSG.
      GS_MESSAGE-MSGTY = S.
      GS_MESSAGE-MSGNO = 000.
      GS_MESSAGE-MSGV1 = 已生成物料凭证.
      GS_MESSAGE-MSGV2 = MAT_DOC.
      APPEND GS_MESSAGE TO GT_MESSAGE.
    ELSE.
      LOOP AT LT_ERRMSG.
        GS_MESSAGE-MSGID = LT_ERRMSG-ID.
        GS_MESSAGE-MSGTY = LT_ERRMSG-TYPE.
        GS_MESSAGE-MSGNO = LT_ERRMSG-NUMBER.
        GS_MESSAGE-MSGV1 = LT_ERRMSG-MESSAGE_V1.
        GS_MESSAGE-MSGV2 = LT_ERRMSG-MESSAGE_V2.
        GS_MESSAGE-MSGV3 = LT_ERRMSG-MESSAGE_V3.
        GS_MESSAGE-MSGV4 = LT_ERRMSG-MESSAGE_V4.
        APPEND GS_MESSAGE TO GT_MESSAGE.
      ENDLOOP.
      CALL FUNCTION BAPI_TRANSACTION_ROLLBACK.
    ENDIF.
  ENDIF.

  IF GT_MESSAGE[] IS NOT INITIAL.
    CALL FUNCTION USMD_MESSAGE_POPUP
      EXPORTING
        IT_MESSAGE        = GT_MESSAGE[]
        IF_SAVE_NECESSARY = GV_NECESSARY.
  ENDIF.

ENDFORM.

 

工单发料,退料等一些物料的移动