BAPI:生成生产计划

TCODE:MD61
BAPI:生成生产计划_第1张图片

CODE:

   DATA: REQ_ITEM LIKE BAPISITEMR,
         RETURN LIKE BAPIRETURN1 OCCURS 0 WITH HEADER LINE,
         MATERTEMP LIKE BAPISITEMR-MATERIAL,
         I_REQUIREMENTS_SCHEDULE_IN LIKE  BAPISSHDIN OCCURS 0 WITH HEADER LINE,
         REQ_DATA LIKE BAPISSHDIN OCCURS 0 WITH HEADER LINE.
   DATA:LW_FLAG TYPE CHAR1,
        LW_KWMENG TYPE KWMENG.
   CLEAR:GT_YTLIST1[],
         GT_ZTSD003[],
         GS_ZTSD003,
         GT_ZTSD003V1,
         LW_KWMENG.
   GT_YTLIST1[] = GT_YTLIST[].
   SORT GT_YTLIST1 BY VBELN.
   DELETE GT_YTLIST1 WHERE FLAG <> ABAP_ON.

   SELECT *
     INTO CORRESPONDING FIELDS OF TABLE GT_ZTSD003
     FROM ZTSD003
     FOR ALL ENTRIES IN GT_YTLIST1
     WHERE VBELN = GT_YTLIST1-VBELN.

   GT_ZTSD003V1[] = GT_ZTSD003[].

   SORT GT_ZTSD003 BY MATNR ETDAT .
   SORT GT_ZTSD003V1 BY MATNR.
   DELETE ADJACENT DUPLICATES FROM GT_ZTSD003V1 COMPARING MATNR.

   LOOP AT GT_ZTSD003V1 INTO GS_ZTSD003V1.
     CLEAR:LW_KWMENG,
           REQ_DATA[],
           I_REQUIREMENTS_SCHEDULE_IN[].
     LOOP AT GT_ZTSD003 INTO GS_ZTSD003
       WHERE MATNR = GS_ZTSD003V1-MATNR.

       LW_KWMENG = GS_ZTSD003-KWMENG + LW_KWMENG.

       AT END OF ETDAT.
         REQ_DATA-DATE_TYPE  =  '1'.             " 对应前台的 D 模式
         REQ_DATA-REQ_DATE   = GS_ZTSD003-ETDAT.
         REQ_DATA-REQ_QTY    = LW_KWMENG.
         APPEND REQ_DATA.
         CLEAR REQ_DATA.

         I_REQUIREMENTS_SCHEDULE_IN-DATE_TYPE = '1'.
         I_REQUIREMENTS_SCHEDULE_IN-REQ_DATE  = GS_ZTSD003-ETDAT.
         I_REQUIREMENTS_SCHEDULE_IN-REQ_QTY   = LW_KWMENG.
         APPEND I_REQUIREMENTS_SCHEDULE_IN.
         CLEAR I_REQUIREMENTS_SCHEDULE_IN.
         CLEAR LW_KWMENG.

       ENDAT.

     ENDLOOP.

     REQ_ITEM-MATERIAL   = GS_ZTSD003V1-MATNR.
     REQ_ITEM-PLANT      = GS_ZTSD003V1-WERKS.
     REQ_ITEM-REQU_TYPE  = ''.
     REQ_ITEM-VERSION    = '00'.
     REQ_ITEM-VERS_ACTIV = ABAP_ON.
     REQ_ITEM-MRP_AREA   = GS_ZTSD003V1-BERID.

     CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE'
       EXPORTING
         REQUIREMENTS_ITEM        = REQ_ITEM
         DO_COMMIT                = SPACE
       IMPORTING
         MATERIAL                 = MATERTEMP
       TABLES
         REQUIREMENTS_SCHEDULE_IN = REQ_DATA
         RETURN                   = RETURN.

     IF MATERTEMP IS INITIAL.

       CLEAR:RETURN[].
       CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
         EXPORTING
           MATERIAL                 = GS_ZTSD003V1-MATNR
           PLANT                    = GS_ZTSD003V1-WERKS
           REQUIREMENTSTYPE         = ''
           VERSION                  = '00'
           REQMTSPLANNUMBER         = ''
           VERS_ACTIV               = ABAP_ON
           DO_COMMIT                = SPACE
           DELETE_OLD               = SPACE
           MRP_AREA                 = GS_ZTSD003V1-BERID
         TABLES
           REQUIREMENTS_SCHEDULE_IN = I_REQUIREMENTS_SCHEDULE_IN
           RETURN                   = RETURN.

       IF RETURN IS NOT INITIAL.
         LW_FLAG = ABAP_ON.
         EXIT.
       ENDIF.

     ENDIF.

   ENDLOOP.

   IF LW_FLAG IS INITIAL.
     MESSAGE '生产计划生成成功' TYPE 'S' .
     COMMIT WORK.
   ELSE.
     LOOP AT RETURN WHERE TYPE  = 'E'.
       MESSAGE RETURN-MESSAGE TYPE 'S' DISPLAY LIKE 'E'.
       EXIT.
     ENDLOOP.
     ROLLBACK WORK.
   ENDIF.

你可能感兴趣的:(ABAP:BAPI)