SAP 物料订单创建、下达、报工、收货与投料(ABAP代码)

对主体订单下的某一类物料通过MRP控制者的判断,可以对此类物料进行自动创建生产订单,自动下达,报工、收货,最后对主体订单投料。

1、新增加一个MRP控制者:泵送钢管类物料的MRP控制者必须设置为168.

2、根据输入条件读取主体订单(剔除CLSD、TECO、DLT)的OBOM清单,挑取MRP控制者为钢管(168)胶管(170)(泵送钢管订单、泵送胶管订单)下达钢管、胶管的生产订单(订单类型为PP51)。

3、程序在下达钢管、胶管的生产订单时候,要判断物料的MRP类型是否是ND,若是ND的话,程序报错,则提示需要将该物料改为PD。计划员手动修正MRP参数。

 4、下达生产订单时,按物料编码将数量汇总下达钢管、胶管订单,基本完成日期取当天。

 5、订单下达后(REL状态)模拟CO15报完工。

6、订单报完工后模拟MB31收货(移动类型为101,库存地点取MRP2视图生产仓储地)。

7、入库后模拟MB1A(移动类型为261,库存地点取MRP2视图的生产仓储地)对预留投料到对应的主体生产订单。

8、可选择部分MRP控制者未168的物料下生产订单


9、在下了生产订单后,如果OBOM中增加了物料的数量,则对差异数量下单

REPORT  ZR_MM_GLDDCL.
TABLES: AFKO,RESB,MARC,MAKT,ZGLDDCL.

DATA: BEGIN OF GT_RESB OCCURS 0,
    AUFNR LIKE RESB-AUFNR,
    WERKS LIKE RESB-WERKS,
    RSNUM LIKE RESB-RSNUM,
    RSPOS LIKE RESB-RSPOS,
    MATNR LIKE RESB-MATNR,
    MEINS LIKE RESB-MEINS,
    BDMNG LIKE RESB-BDMNG,
    ENMNG LIKE RESB-ENMNG,
END OF GT_RESB.

DATA: BEGIN OF ITAB_SHOW OCCURS 0,
    CHKID(1),
    WERKS LIKE RESB-WERKS,
    MATNR LIKE RESB-MATNR,
    MAKTX LIKE MAKT-MAKTX,  "物料描述
    MEINS LIKE RESB-MEINS,  "基本单位
    XLOEK LIKE RESB-XLOEK,  "删除标识
    BDMNG LIKE RESB-BDMNG,  "需求数量
    ENMNG LIKE RESB-ENMNG,  "提取数量
    RGEKZ LIKE RESB-RGEKZ,  "反冲
    DISPO LIKE MARC-DISPO,  "MRP控制者
    DISMM LIKE MARC-DISMM,  "MRP类型
    LGPRO LIKE MARC-LGPRO,  "生产仓储地
    SCDDH LIKE RESB-AUFNR,  "生成的生产订单号
    YCXX1(100),             "PD,ND异常判断
    YCXX2(100),             "订单创建
    YCXX3(100),             "下达
    YCXX4(100),             "报工
    YCXX5(100),             "收货
END OF ITAB_SHOW.

DATA: BEGIN OF GT_DDTL OCCURS 0,
    AUFNR LIKE RESB-AUFNR,
    WERKS LIKE RESB-WERKS,
    RSNUM LIKE RESB-RSNUM,
    RSPOS LIKE RESB-RSPOS,
    MATNR LIKE RESB-MATNR,
    MEINS LIKE RESB-MEINS,
    BDMNG LIKE RESB-BDMNG,
    ENMNG LIKE RESB-ENMNG,
    LGPRO LIKE MARC-LGPRO,
    YCXX1(100),
END OF GT_DDTL.

DATA: BEGIN OF GT_AFKO OCCURS 0,
    RSNUM LIKE RESB-RSNUM,
    AUFNR LIKE AFKO-AUFNR,
END OF GT_AFKO.

DATA: BEGIN OF GT_MARC OCCURS 0,
    MATNR LIKE MARC-MATNR,
    DISPO LIKE MARC-DISPO,
    DISMM LIKE MARC-DISMM,
    LGPRO LIKE MARC-LGPRO,
    WERKS LIKE MARC-WERKS,
END OF GT_MARC.

DATA: BEGIN OF GT_MAKT OCCURS 0,
    MATNR LIKE MAKT-MATNR,
    MAKTX LIKE MAKT-MAKTX,
END OF GT_MAKT.

DATA: BEGIN OF BDCDATA OCCURS 0.
        INCLUDE STRUCTURE BDCDATA.
DATA: END OF BDCDATA.

DATA: BDCMESS LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA: BDC_MODE(1).
DATA: P_MODE  LIKE CTU_PARAMS-DISMODE VALUE 'N'.
DATA: IN_ZGLDDCL LIKE ZGLDDCL OCCURS 0 WITH HEADER LINE.
DATA: GT_ZGLDDCL LIKE ZGLDDCL OCCURS 0 WITH HEADER LINE.
DATA: TL_ZGLDDCL LIKE ZGLDDCL OCCURS 0 WITH HEADER LINE.
DATA: FLAG_SUCCESS(1).

RANGES: R_RSNUM FOR RESB-RSNUM.
RANGES: R_MATNR FOR RESB-MATNR.
*&--------------------------------------------------------------------*
*&      屏幕显示
*&--------------------------------------------------------------------*
SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_WERKS LIKE RESB-WERKS OBLIGATORY DEFAULT '1802',"工厂
            P_DISPO LIKE MARC-DISPO DEFAULT '168'.    "MRP控制者
SELECT-OPTIONS: S_AUFNR FOR AFKO-AUFNR."生产订单
SELECTION-SCREEN: END OF BLOCK B1.

*&--------------------------------------------------------------------*
*&      程序执行
*&--------------------------------------------------------------------*
START-OF-SELECTION.
    PERFORM AUTHORITY_CHECK.
    PERFORM INPUT_CHECK.
    PERFORM GET_DATA.
    PERFORM SHOW.


*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
FORM GET_DATA .
  DATA:
    LV_OBJNR TYPE AUFK-OBJNR,
    LV_LINE  TYPE BSVX-STTXT,
    LV_LOEKZ TYPE AUFK-LOEKZ,
    LV_FLAG  TYPE CHAR1.

* 剔除订单状态为 CLSD、TECO、DLT、CRTD OBOM清单
  CLEAR: GT_AFKO,GT_AFKO[].
  SELECT RSNUM AUFNR INTO TABLE GT_AFKO
     FROM AFKO
     WHERE AUFNR IN S_AUFNR.

  IF GT_AFKO[] IS INITIAL.
    MESSAGE '该订单预留不存在!' TYPE 'S'.
    STOP.
  ENDIF.

  LOOP AT GT_AFKO.
* 订单的对象号
    SELECT SINGLE OBJNR INTO LV_OBJNR
      FROM AUFK
     WHERE AUFNR = GT_AFKO-AUFNR
       AND LOEKZ = ''.

    IF LV_OBJNR IS NOT INITIAL.
* 取得订单的订单状态
      CALL FUNCTION 'STATUS_TEXT_EDIT'
        EXPORTING
          OBJNR            = LV_OBJNR
          SPRAS            = SY-LANGU
        IMPORTING
          LINE             = LV_LINE
        EXCEPTIONS
          OBJECT_NOT_FOUND = 1
          OTHERS           = 2.
      IF LV_LINE CS 'TECO' OR LV_LINE CS 'CLSD' OR
         LV_LINE CS 'DLT' OR LV_LINE CS 'CRTD'.
            DELETE GT_AFKO.
      ENDIF.
    ENDIF.
  ENDLOOP.

  IF GT_AFKO[] IS INITIAL.
    MESSAGE '该订单预留不存在!' TYPE 'S'.
    STOP.
  ELSE.
      LOOP AT GT_AFKO.
          R_RSNUM-SIGN = 'I'.
          R_RSNUM-OPTION = 'EQ'.
          R_RSNUM-LOW = GT_AFKO-RSNUM.
          APPEND R_RSNUM.
          CLEAR R_RSNUM.
      ENDLOOP.
  ENDIF.

  SELECT AUFNR WERKS RSNUM RSPOS MATNR MEINS BDMNG ENMNG INTO
    CORRESPONDING FIELDS OF TABLE GT_RESB
    FROM RESB
    WHERE RSNUM IN R_RSNUM
      AND XLOEK <> 'X'   "删除的
      AND DUMPS <> 'X'   "虚拟件
      AND DBSKZ <> 'E'   "父子订单物料,排除
      AND BDMNG <> '0'.

  LOOP AT GT_RESB.
      MOVE-CORRESPONDING GT_RESB TO ITAB_SHOW.
      COLLECT ITAB_SHOW.
  ENDLOOP.

* 判断物料是否为钢管或胶管
  IF ITAB_SHOW[] IS NOT INITIAL.
    SELECT MATNR DISPO DISMM LGPRO WERKS INTO TABLE GT_MARC
    FROM MARC
    FOR ALL ENTRIES IN ITAB_SHOW
    WHERE MATNR = ITAB_SHOW-MATNR
      AND WERKS = ITAB_SHOW-WERKS.
  ENDIF.
  SORT GT_MARC BY MATNR.
  LOOP AT ITAB_SHOW.
    READ TABLE GT_MARC WITH KEY MATNR = ITAB_SHOW-MATNR BINARY SEARCH.
    IF SY-SUBRC = 0.
        ITAB_SHOW-DISPO = GT_MARC-DISPO.
        ITAB_SHOW-DISMM = GT_MARC-DISMM.
        ITAB_SHOW-LGPRO = GT_MARC-LGPRO.
        IF GT_MARC-DISMM = 'ND'.
            ITAB_SHOW-YCXX1 = 'MRP类型为ND,请手动修正MRP参数。'.
        ENDIF.
    ENDIF.
    MODIFY ITAB_SHOW.
  ENDLOOP.

* 删除MRP控制者不为168的。
  DELETE ITAB_SHOW WHERE DISPO <> P_DISPO.

* 获取物料描述
  IF ITAB_SHOW[] IS NOT INITIAL.
    SELECT MATNR MAKTX INTO TABLE GT_MAKT
      FROM MAKT
      FOR ALL ENTRIES IN ITAB_SHOW
      WHERE MATNR = ITAB_SHOW-MATNR
        AND SPRAS = SY-LANGU.
  ENDIF.
  SORT GT_MAKT BY MATNR.
  LOOP AT ITAB_SHOW.
      READ TABLE GT_MAKT WITH KEY MATNR = ITAB_SHOW-MATNR BINARY SEARCH.
      IF SY-SUBRC = 0.
          ITAB_SHOW-MAKTX = GT_MAKT-MAKTX.
      ENDIF.
      MODIFY ITAB_SHOW.
  ENDLOOP.

ENDFORM.                    " GET_DATA

*&---------------------------------------------------------------------*
*&      Form  SHOW
*&---------------------------------------------------------------------*
FORM SHOW .
  TYPE-POOLS SLIS.
  DATA: WS_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
        WS_FIELDGROUPS_TAB TYPE SLIS_T_SP_GROUP_ALV,
        WS_LAYOUT          TYPE SLIS_LAYOUT_ALV,
        WS_SORTFIELDS_TAB  TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE,
        WS_EVENTS          TYPE SLIS_T_EVENT.
  DATA: V_REPID LIKE SY-REPID.
  DATA: NN TYPE I VALUE 0.
  DATA: TITLE TYPE LVC_TITLE.

  DEFINE HOUT.
    NN = NN + 1.
    WS_FIELDCAT-TABNAME   = 'ITAB_SHOW'.
    WS_FIELDCAT-FIELDNAME = '&1'.
    WS_FIELDCAT-SELTEXT_M =  &2.
    WS_FIELDCAT-COL_POS   =  NN.
    WS_FIELDCAT-OUTPUTLEN =  &3.
    WS_FIELDCAT-DATATYPE  = '&4'.
*    ws_fieldcat-do_sum    = '&5'.
    WS_FIELDCAT-LZERO     = 'X'.

    APPEND WS_FIELDCAT.
    CLEAR WS_FIELDCAT.
  END-OF-DEFINITION.

  HOUT MATNR      '物料'                15 CHAR.
  HOUT MAKTX      '物料描述'            30 CHAR.
  HOUT MEINS      '单位'                 6 CHAR.
  HOUT BDMNG      '需求数量'            13 QUAN.
  HOUT ENMNG      '提货数量'            13 QUAN.
  HOUT SCDDH      '生产订单'            12 CHAR.
  HOUT DISMM      'MRP类型'              2 CHAR.
  HOUT LGPRO      '生产仓储地'           4 CHAR.
  HOUT YCXX1      '异常信息1'          100 CHAR.
  HOUT YCXX2      '生产订单创建'       100 CHAR.
  HOUT YCXX3      '生产订单下达'       100 CHAR.
  HOUT YCXX4      '生产订单报工'       100 CHAR.
  HOUT YCXX5      '生产订单收货'       100 CHAR.

*  title = '泵送清单打印报表'.
  WS_LAYOUT-ZEBRA             = 'X'.
  WS_LAYOUT-GET_SELINFOS      = ''.
  WS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  WS_LAYOUT-DETAIL_POPUP      = 'X'.
  WS_LAYOUT-NO_KEYFIX         = 'X'.
  WS_LAYOUT-BOX_FIELDNAME     = 'CHKID'.


  V_REPID = SY-REPID.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM       = V_REPID
      I_CALLBACK_PF_STATUS_SET = 'MENU_SET'
      I_CALLBACK_USER_COMMAND  = 'EXECUTE_COMMAND'
      IS_LAYOUT                = WS_LAYOUT
      IT_FIELDCAT              = WS_FIELDCAT[]
      IT_SPECIAL_GROUPS        = WS_FIELDGROUPS_TAB[]
      IT_SORT                  = WS_SORTFIELDS_TAB[]
      IT_EVENTS                = WS_EVENTS[]
      I_GRID_TITLE             = TITLE
    TABLES
      T_OUTTAB                 = ITAB_SHOW
    EXCEPTIONS
      PROGRAM_ERROR            = 1
      OTHERS                   = 2.

ENDFORM.                    " SHOW

*&---------------------------------------------------------------------*
*&      Form  AUTHORITY_CHECK
*&---------------------------------------------------------------------*
FORM AUTHORITY_CHECK .

  AUTHORITY-CHECK OBJECT 'M_MATE_WRK'
           ID 'ACTVT' FIELD '03'
           ID 'WERKS' FIELD P_WERKS.
  IF SY-SUBRC <> 0.
    MESSAGE '没有工厂的权限!' TYPE 'I'.
    STOP.
  ENDIF.

ENDFORM.                    " AUTHORITY_CHECK

*---------------------------------------------------------------------*
*       FORM MENU_SET                                                 *
*---------------------------------------------------------------------*
FORM MENU_SET USING RT_EXTAB TYPE SLIS_T_EXTAB.
  SET PF-STATUS 'S001' .
ENDFORM.                    "menu_set

*---------------------------------------------------------------------*
*       FORM EXECUTE_COMMAND                                          *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  R_UCOMM                                                       *
*  -->  RS_SELFIELD                                                   *
*---------------------------------------------------------------------*
FORM EXECUTE_COMMAND USING R_UCOMM LIKE SY-UCOMM
                        RS_SELFIELD TYPE SLIS_SELFIELD.

*从ALV读取更新数据
   READ TABLE ITAB_SHOW INDEX RS_SELFIELD-TABINDEX.
   DATA: LR_GRID TYPE REF TO CL_GUI_ALV_GRID.
   DATA: ERR(1).
   CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
         IMPORTING  E_GRID = LR_GRID.
   CALL METHOD LR_GRID->CHECK_CHANGED_DATA.

   RS_SELFIELD-REFRESH = 'X'.  "自动刷新

  CASE R_UCOMM.

    WHEN 'DDPCL'.
      CLEAR : R_MATNR,R_MATNR[].
      LOOP AT ITAB_SHOW WHERE CHKID = 'X'.
* 选中的物料 ADD BY LIURB 20110720
        R_MATNR-SIGN = 'I'.
        R_MATNR-OPTION = 'EQ'.
        R_MATNR-LOW = ITAB_SHOW-MATNR.
        APPEND R_MATNR.
        CLEAR R_MATNR.

* 生成生产订单-CO01/CO02
        PERFORM CREAT_PO.

        SELECT * INTO CORRESPONDING FIELDS OF TABLE IN_ZGLDDCL
          FROM ZGLDDCL
          WHERE MATNR = ITAB_SHOW-MATNR
            AND WERKS = ITAB_SHOW-WERKS
            AND SCDDH = ITAB_SHOW-SCDDH.
        READ TABLE IN_ZGLDDCL INDEX 1.
* 订单下达
        IF SY-SUBRC = 0.
            IF IN_ZGLDDCL-FLAG2 = 'X'.
                ITAB_SHOW-YCXX3 = '订单已下达,不能重复下达'.
            ELSE.
                WAIT UP TO 1 SECONDS.
                PERFORM CALL_BDC_DDXD.
            ENDIF.
*订单工序报工-CO15
            IF IN_ZGLDDCL-FLAG3 = 'X'.
                ITAB_SHOW-YCXX4 = '工序已报工,不能重复报工'.
            ELSE.
                WAIT UP TO 1 SECONDS.
                PERFORM GET_GXBG.
            ENDIF.
*   订单收货 - MB31
            IF IN_ZGLDDCL-FLAG4 = 'X'.
                ITAB_SHOW-YCXX5 = '订单已收货,不能重复收货'.
            ELSE.
                WAIT UP TO 1 SECONDS.
                PERFORM GET_DDSH.
            ENDIF.
        ENDIF.
        MODIFY ITAB_SHOW.
      ENDLOOP.

*   订单投料 - MB1A
      WAIT UP TO 1 SECONDS.
      PERFORM GET_DDTL.

*   投料信息显示
    WHEN 'XSDDTLJG'.
      PERFORM SHOW_DDTLXX.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'BACK1' OR 'CANCEL'.
      SUBMIT ZR_MM_GLDDCL  VIA SELECTION-SCREEN.

  ENDCASE.
ENDFORM.                    "execute_command
*&---------------------------------------------------------------------*
*&      Form  CREAT_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM CREAT_PO .
    DATA: I_ORDDATA LIKE BAPI_PP_ORDER_CREATE OCCURS 0 WITH HEADER LINE,
          I_RETURN LIKE BAPIRET2,
          I_ORDER_NUMBER LIKE BAPI_ORDER_KEY-ORDER_NUMBER,
          I_ORDER_TYPE LIKE BAPI_ORDER_COPY-ORDER_TYPE.
    DATA: TEMP_AUFNR LIKE AFPO-AUFNR.
    DATA: FLAG(1),
          FLAG_RSNUM LIKE RESB-RSNUM,
          FLAG_MATNR LIKE RESB-MATNR,
          FLAG_AUFNR LIKE AFKO-AUFNR,
          FLAG_BDMNG LIKE RESB-BDMNG,
          CL_BDMNG LIKE RESB-BDMNG.

* 判断是否已经对该订单的物料生成了生产订单
      CLEAR : FLAG,FLAG_RSNUM,FLAG_MATNR,FLAG_AUFNR,FLAG_BDMNG.
      CLEAR : GT_ZGLDDCL,GT_ZGLDDCL[].
      SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_ZGLDDCL
        FROM ZGLDDCL
        WHERE MATNR = ITAB_SHOW-MATNR
          AND WERKS = ITAB_SHOW-WERKS.
      LOOP AT GT_ZGLDDCL WHERE MATNR = ITAB_SHOW-MATNR
                           AND WERKS = ITAB_SHOW-WERKS.
          IF GT_ZGLDDCL-RSNUM IN R_RSNUM.
              FLAG_BDMNG = FLAG_BDMNG + GT_ZGLDDCL-BDMNG.
              IF GT_ZGLDDCL-SCDDH IS NOT INITIAL.
                  FLAG = 'X'.
                  FLAG_AUFNR = GT_ZGLDDCL-SCDDH.
              ELSE.
                  FLAG_RSNUM = GT_ZGLDDCL-RSNUM.
              ENDIF.
          ENDIF.
      ENDLOOP.
* 判断需求数量与已下单的数量是否相同,
* 如果小于大于下单数量,则允许下单,数量为差异量
* ADD BY LIURB 20110720
      CL_BDMNG = ITAB_SHOW-BDMNG.
      IF FLAG_BDMNG <> ITAB_SHOW-BDMNG.
          CL_BDMNG = ITAB_SHOW-BDMNG - FLAG_BDMNG.
          FLAG = ''.
          ITAB_SHOW-BDMNG = CL_BDMNG.
      ENDIF.

      CLEAR : I_ORDDATA ,I_ORDDATA[].
      I_ORDDATA-MATERIAL = ITAB_SHOW-MATNR.
      I_ORDDATA-PLANT = ITAB_SHOW-WERKS.
      I_ORDDATA-PLANNING_PLANT = ITAB_SHOW-WERKS.
      I_ORDDATA-ORDER_TYPE = 'PP51'.
      I_ORDDATA-QUANTITY = CL_BDMNG.
      I_ORDDATA-BASIC_START_DATE = SY-DATUM.
      I_ORDDATA-BASIC_END_DATE = SY-DATUM.
      I_ORDDATA-QUANTITY_UOM = ITAB_SHOW-MEINS.
      APPEND I_ORDDATA.

      IF FLAG = 'X'.
          ITAB_SHOW-YCXX2 = '订单已创建,不能重复创建'.
          ITAB_SHOW-SCDDH = FLAG_AUFNR.
      ELSE.
        CLEAR: I_RETURN.
        CALL FUNCTION 'BAPI_PRODORD_CREATE'
        EXPORTING
          ORDERDATA        = I_ORDDATA
        IMPORTING
          RETURN           = I_RETURN
          ORDER_NUMBER     = I_ORDER_NUMBER
          ORDER_TYPE       = I_ORDER_TYPE.

        IF I_RETURN-TYPE <>  'E'.
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                WAIT = 'X'.
            ITAB_SHOW-SCDDH = I_ORDER_NUMBER.
            ITAB_SHOW-YCXX2 = '订单创建成功'.

*** 更新自定义表
            FLAG_MATNR = ITAB_SHOW-MATNR.
            PERFORM UPDATE_ZGLDDCL USING
                    FLAG_RSNUM I_ORDER_NUMBER FLAG_MATNR.

        ELSE.
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
            FLAG_SUCCESS = ''.
            WAIT UP TO 1 SECONDS.
            ITAB_SHOW-YCXX2 = I_RETURN-MESSAGE.
        ENDIF.
      ENDIF.
ENDFORM.                    " CREAT_PO
*&---------------------------------------------------------------------*
*&      Form  INPUT_CHECK
*&---------------------------------------------------------------------*
FORM INPUT_CHECK .
    IF S_AUFNR IS INITIAL.
        MESSAGE '该订单预留不存在!' TYPE 'S'.
        STOP.
    ENDIF.

    IF P_DISPO IS INITIAL.
        MESSAGE 'MRP控制者不能为空!' TYPE 'S'.
        STOP.
    ENDIF.
*钢管的MRP控制者为168,胶管的MRP控制者为170
*目前只处理钢管2011.7.14
    IF P_DISPO <> '168'.
        MESSAGE 'MRP控制者必须为168!' TYPE 'S'.
        STOP.
    ENDIF.

ENDFORM.                    " INPUT_CHECK
*&---------------------------------------------------------------------*
*&      Form  CALL_BDC_DDXD
*&---------------------------------------------------------------------*
*       订单下达
*----------------------------------------------------------------------*
FORM CALL_BDC_DDXD.
* START BDC
  DO 2 TIMES.
    CLEAR: BDCDATA,BDCDATA[],BDCMESS,BDCMESS[].
    PERFORM BDC_DYNPRO      USING 'SAPLCOKO1' '0110'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' '/00'.
    PERFORM BDC_FIELD       USING 'CAUFVD-AUFNR'
                                  ITAB_SHOW-SCDDH.
    PERFORM BDC_FIELD       USING 'R62CLORD-FLG_OVIEW'
                                  'X'.
    PERFORM BDC_DYNPRO      USING 'SAPLCOKO1' '0115'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' '=FREI'.
    PERFORM BDC_DYNPRO      USING 'SAPLCOKO1' '0115'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE'
                                    '=BU'.
    BDC_MODE = P_MODE.
    WAIT UP TO 1 SECONDS.
    CALL TRANSACTION 'CO02' USING BDCDATA
                              UPDATE 'S' MODE  BDC_MODE
                              MESSAGES INTO BDCMESS.
    IF SY-SUBRC = 0.
        COMMIT WORK.
        ITAB_SHOW-YCXX3 = '下达成功'.
        UPDATE ZGLDDCL SET FLAG2 = 'X'
                     WHERE SCDDH = ITAB_SHOW-SCDDH
                       AND MATNR = ITAB_SHOW-MATNR.
        EXIT.
    ELSE.
        ROLLBACK WORK.
        ITAB_SHOW-YCXX3 = '下达失败,请手动下达'.
    ENDIF.
  ENDDO.
* END BDC

ENDFORM.                    " CALL_BDC_DDXD

*&---------------------------------------------------------------------*
*&      Form  bdc_dynpro
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM = PROGRAM.
  BDCDATA-DYNPRO = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.                    "bdc_dynpro
*&---------------------------------------------------------------------*
*&      Form  bdc_dynpro
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
  CLEAR BDCDATA.
  BDCDATA-FNAM = FNAM.
  BDCDATA-FVAL = FVAL.
  APPEND BDCDATA.
ENDFORM.                    "bdc_field

*&---------------------------------------------------------------------*
*&      Form  GET_GXBG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM GET_GXBG.
* START BDC
    CLEAR: BDCDATA,BDCDATA[],BDCMESS,BDCMESS[].
    PERFORM BDC_DYNPRO      USING 'SAPLCORU' '0400'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' '/00'.
    PERFORM BDC_FIELD       USING 'CORUF-AUFNR' ITAB_SHOW-SCDDH.
    PERFORM BDC_DYNPRO      USING 'SAPLCORU' '0410'.
    PERFORM BDC_FIELD       USING 'BDC_OKCODE' '=BU'.
    PERFORM BDC_FIELD       USING 'CORUF-AUTER' 'X'.
    PERFORM BDC_FIELD       USING 'CORUF-AUSBU' ''.

    BDC_MODE = P_MODE.
    CALL TRANSACTION 'CO15' USING BDCDATA
                              UPDATE 'S' MODE  BDC_MODE
                              MESSAGES INTO BDCMESS.
    IF SY-SUBRC = 0.
        COMMIT WORK.
        UPDATE ZGLDDCL SET FLAG3 = 'X'
                     WHERE SCDDH = ITAB_SHOW-SCDDH
                       AND MATNR = ITAB_SHOW-MATNR.
        ITAB_SHOW-YCXX4 = '报工成功'.
    ELSE.
        ROLLBACK WORK.
        ITAB_SHOW-YCXX4 = '报工失败,请手动报工'.
    ENDIF.
* END BDC

ENDFORM.                    " GET_GXBG
*&---------------------------------------------------------------------*
*&      Form  GET_DDSH
*&---------------------------------------------------------------------*
*       订单收货
*----------------------------------------------------------------------*
FORM GET_DDSH .
    DATA:
    LW_AUFNR TYPE AUFNR,
    LW_MATNR TYPE MATNR,
    LW_MESSAGE(100).

  DATA:
    I_GHEADER   TYPE BAPI2017_GM_HEAD_01,
    I_MDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
    I_MDOCYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR,
    I_GITEM     TYPE TABLE OF BAPI2017_GM_ITEM_CREATE WITH HEADER LINE,
    I_RETURN    TYPE TABLE OF BAPIRET2 WITH HEADER LINE.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          INPUT   = ITAB_SHOW-SCDDH
        IMPORTING
          OUTPUT  = LW_AUFNR.

      I_GHEADER-DOC_DATE   = SY-DATUM.
      I_GHEADER-PSTNG_DATE = SY-DATUM.

      SELECT MATNR
        INTO LW_MATNR
        FROM AFPO
          UP TO 1 ROWS
       WHERE AUFNR = LW_AUFNR.
      ENDSELECT.

      I_GITEM-MOVE_TYPE  = '101'.
      I_GITEM-MVT_IND    = 'F'. "有关生产单的货物移动
      I_GITEM-PLANT      = ITAB_SHOW-WERKS.
      I_GITEM-STGE_LOC   = ITAB_SHOW-LGPRO.
      I_GITEM-MATERIAL   = LW_MATNR.
      I_GITEM-ENTRY_QNT  = ITAB_SHOW-BDMNG.
      I_GITEM-ORDERID    = LW_AUFNR.
      APPEND I_GITEM.
      CLEAR : I_RETURN,I_RETURN[].
      CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
        EXPORTING
          GOODSMVT_HEADER       = I_GHEADER
          GOODSMVT_CODE         = '02' "MB31
        IMPORTING
          MATERIALDOCUMENT      = I_MDOCUMENT
          MATDOCUMENTYEAR       = I_MDOCYEAR
        TABLES
          GOODSMVT_ITEM         = I_GITEM
          RETURN                = I_RETURN.

      IF I_MDOCUMENT IS NOT INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  WAIT = 'X'.
        UPDATE ZGLDDCL SET FLAG4 = 'X'
                     WHERE SCDDH = ITAB_SHOW-SCDDH
                       AND MATNR = ITAB_SHOW-MATNR.
        ITAB_SHOW-YCXX5 = '收货成功'.
      ELSE.
       ITAB_SHOW-YCXX5 = I_RETURN-MESSAGE.
      ENDIF.

ENDFORM.                    " GET_DDSH
*&---------------------------------------------------------------------*
*&      Form  GET_DDTL
*&---------------------------------------------------------------------*
*       订单投料
*----------------------------------------------------------------------*
FORM GET_DDTL .
    DATA:
    LV_ERRFLG   TYPE CHAR1,
    LV_XCHPF    TYPE MARA-XCHPF,
    LV_BDMNG    TYPE RESBD-BDMNG,
    LT_MCHB     TYPE MCHB OCCURS 0 WITH HEADER LINE,
    I_GHEADER   TYPE TABLE OF BAPI2017_GM_HEAD_01 WITH HEADER LINE ,
    I_GHEADRET  TYPE TABLE OF BAPI2017_GM_HEAD_RET WITH HEADER LINE ,
    I_MDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
    I_MDOCYEAR  TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR,
    I_GITEM     TYPE TABLE OF BAPI2017_GM_ITEM_CREATE WITH HEADER LINE ,
    I_GSNUMBER  TYPE TABLE OF BAPI2017_GM_SERIALNUMBER WITH HEADER LINE,
    I_RETURN    TYPE TABLE OF BAPIRET2 WITH HEADER LINE,
    LS_MSEG     TYPE MSEG,
    TLSL        LIKE RESB-BDMNG.

    CONCATENATE SY-UNAME ' ' SY-TCODE INTO I_GHEADER-HEADER_TXT.
    I_GHEADER-DOC_DATE   = SY-DATUM.
    I_GHEADER-PSTNG_DATE = SY-DATUM.

    APPEND I_GHEADER.

    CLEAR : GT_DDTL,GT_DDTL[],TL_ZGLDDCL,TL_ZGLDDCL[].

* 比较ITAB_SHOW和GT_RESB,删除GT_RESB中不满足条件的物料
    DELETE GT_RESB WHERE MATNR NOT IN R_MATNR.
    IF GT_RESB[] IS NOT INITIAL.
      SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_DDTL
        FROM RESB
        FOR ALL ENTRIES IN GT_RESB
        WHERE MATNR = GT_RESB-MATNR
          AND RSNUM IN R_RSNUM
          AND XLOEK <> 'X'   "删除的
          AND DUMPS <> 'X'   "虚拟件
          AND DBSKZ <> 'E'   "父子订单物料,排除
          AND BDMNG <> '0'.
    ENDIF.

    IF GT_DDTL[] IS NOT INITIAL.
       SELECT * INTO CORRESPONDING FIELDS OF TABLE TL_ZGLDDCL
          FROM ZGLDDCL
          FOR ALL ENTRIES IN GT_DDTL
          WHERE MATNR = GT_DDTL-MATNR
            AND WERKS = GT_DDTL-WERKS
            AND AUFNR = GT_DDTL-AUFNR.
    ENDIF.

    SORT ITAB_SHOW BY MATNR.
    SORT TL_ZGLDDCL BY AUFNR MATNR WERKS.
    LOOP AT GT_DDTL.
        READ TABLE GT_MARC WITH KEY
              MATNR = GT_DDTL-MATNR BINARY SEARCH.
        IF SY-SUBRC = 0.
            GT_DDTL-LGPRO = GT_MARC-LGPRO.
            MODIFY GT_DDTL.
        ENDIF.

        READ TABLE TL_ZGLDDCL WITH KEY
              AUFNR = GT_DDTL-AUFNR
              MATNR = GT_DDTL-MATNR
              FLAG1 = 'X'
              FLAG2 = 'X'
              FLAG3 = 'X'
              FLAG4 = 'X'
              BINARY SEARCH.
*如果该物料的所有操作没有成功,则不对该物料投料
        IF SY-SUBRC <> 0.
            GT_DDTL-YCXX1 = '订单物料异常,不能投料'.
            MODIFY GT_DDTL.
            CONTINUE.
        ENDIF.
        CLEAR: I_RETURN,I_RETURN[],I_GITEM,I_GITEM[],LV_ERRFLG,TLSL.
        TLSL = GT_DDTL-BDMNG - GT_DDTL-ENMNG.
*如果已经投过料,则不投
        IF TL_ZGLDDCL-FLAG5 = 'X' AND TLSL = 0.
            GT_DDTL-YCXX1 = '订单已投料,不能重复投料'.
            MODIFY GT_DDTL.
            CONTINUE.
        ENDIF.

        I_GITEM-MATERIAL   = GT_DDTL-MATNR.
        I_GITEM-STGE_LOC   = GT_DDTL-LGPRO.
        I_GITEM-ENTRY_QNT  = TLSL.
        I_GITEM-ENTRY_UOM  = GT_DDTL-MEINS.
        I_GITEM-MOVE_TYPE  = '261'.
        I_GITEM-PLANT      = GT_DDTL-WERKS.
        I_GITEM-ORDERID    = GT_DDTL-AUFNR.
        I_GITEM-RESERV_NO  = GT_DDTL-RSNUM.
        I_GITEM-RES_ITEM   = GT_DDTL-RSPOS.
        APPEND I_GITEM.
*       向生产订单的预留发料
        CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
          EXPORTING
            GOODSMVT_HEADER       = I_GHEADER
            GOODSMVT_CODE         = '03' "MB1A
          IMPORTING
            GOODSMVT_HEADRET      = I_GHEADRET
            MATERIALDOCUMENT      = I_MDOCUMENT
            MATDOCUMENTYEAR       = I_MDOCYEAR
          TABLES
            GOODSMVT_ITEM         = I_GITEM
            GOODSMVT_SERIALNUMBER = I_GSNUMBER
            RETURN                = I_RETURN.

        LOOP AT I_RETURN.
          IF I_RETURN-TYPE = 'E' OR
             I_RETURN-TYPE = 'A'.
            LV_ERRFLG = 'X'.
          ENDIF.
        ENDLOOP.

        IF LV_ERRFLG = 'X'.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          WAIT UP TO 1 SECONDS.
          GT_DDTL-YCXX1 = I_RETURN-MESSAGE.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              WAIT = 'X'.
          UPDATE ZGLDDCL SET FLAG5 = 'X'
                             ENMNG = TLSL
                       WHERE AUFNR = GT_DDTL-AUFNR
                         AND MATNR = GT_DDTL-MATNR
                         AND WERKS = GT_DDTL-WERKS.
          GT_DDTL-YCXX1 = '订单投料成功'.
        ENDIF.
        MODIFY GT_DDTL.
    ENDLOOP.
ENDFORM.                    " GET_DDTL
*&---------------------------------------------------------------------*
*&      Form  UPDATE_ZGLDDCL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM UPDATE_ZGLDDCL  USING    P_FLAG_RSNUM
                              P_O_ORDER_NUMBER
                              P_FLAG_MATNR.
* 比较ITAB_SHOW和GT_RESB,删除GT_RESB中不满足条件的物料
    DELETE GT_RESB WHERE MATNR NOT IN R_MATNR.
    CLEAR : GT_ZGLDDCL,GT_ZGLDDCL[].
    LOOP AT GT_RESB WHERE MATNR = P_FLAG_MATNR.
        MOVE-CORRESPONDING GT_RESB TO GT_ZGLDDCL.
        COLLECT GT_ZGLDDCL.
    ENDLOOP.

* 删除重复项
    SORT GT_ZGLDDCL BY AUFNR MATNR.
    DELETE ADJACENT DUPLICATES FROM GT_ZGLDDCL.

    LOOP AT GT_ZGLDDCL.
        IF GT_ZGLDDCL-RSNUM = P_FLAG_RSNUM.
            GT_ZGLDDCL-SCDDH = P_O_ORDER_NUMBER.
        ELSE.
            IF GT_ZGLDDCL-SCDDH IS INITIAL.
                GT_ZGLDDCL-SCDDH = P_O_ORDER_NUMBER.
            ENDIF.
        ENDIF.
        GT_ZGLDDCL-FLAG1 = 'X'."订单创建成功
        MODIFY GT_ZGLDDCL.
    ENDLOOP.

* 将数据插入自定义表中
    IF GT_ZGLDDCL[] IS NOT INITIAL.
        MODIFY ZGLDDCL FROM TABLE GT_ZGLDDCL.
    ENDIF.
ENDFORM.                    " UPDATE_ZGLDDCL
*&---------------------------------------------------------------------*
*&      Form  SHOW_DDTLXX
*&---------------------------------------------------------------------*
FORM SHOW_DDTLXX .
  TYPE-POOLS SLIS.
  DATA: WS_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
        WS_FIELDGROUPS_TAB TYPE SLIS_T_SP_GROUP_ALV,
        WS_LAYOUT          TYPE SLIS_LAYOUT_ALV,
        WS_SORTFIELDS_TAB  TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE,
        WS_EVENTS          TYPE SLIS_T_EVENT.
  DATA: V_REPID LIKE SY-REPID.
  DATA: NN TYPE I VALUE 0.
  DATA: TITLE TYPE LVC_TITLE.

  DEFINE HOUT.
    NN = NN + 1.
    WS_FIELDCAT-TABNAME   = 'GT_DDTL'.
    WS_FIELDCAT-FIELDNAME = '&1'.
    WS_FIELDCAT-SELTEXT_M =  &2.
    WS_FIELDCAT-COL_POS   =  NN.
    WS_FIELDCAT-OUTPUTLEN =  &3.
    WS_FIELDCAT-DATATYPE  = '&4'.
*    ws_fieldcat-do_sum    = '&5'.
    WS_FIELDCAT-LZERO     = 'X'.

    IF WS_FIELDCAT-FIELDNAME = 'AUFNR'.
      WS_FIELDCAT-REF_FIELDNAME = 'AUFNR' .
      WS_FIELDCAT-REF_TABNAME = 'AFPO'.
    ENDIF.

    APPEND WS_FIELDCAT.
    CLEAR WS_FIELDCAT.
  END-OF-DEFINITION.

  HOUT AUFNR      '订单'                12 CHAR.
  HOUT MATNR      '物料'                15 CHAR.
  HOUT MEINS      '单位'                 6 CHAR.
  HOUT BDMNG      '需求数量'            13 QUAN.
  HOUT ENMNG      '提货数量'            13 QUAN.
  HOUT LGPRO      '生产仓储地'           4 CHAR.
  HOUT YCXX1      '异常信息1'          100 CHAR.

*  title = ''.
  WS_LAYOUT-ZEBRA             = 'X'.
  WS_LAYOUT-GET_SELINFOS      = ''.
  WS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  WS_LAYOUT-DETAIL_POPUP      = 'X'.
  WS_LAYOUT-NO_KEYFIX         = 'X'.
*  WS_LAYOUT-BOX_FIELDNAME     = 'CHKID'.


  V_REPID = SY-REPID.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM       = V_REPID
*      i_callback_pf_status_set = 'MENU_SET'
*      i_callback_user_command  = 'EXECUTE_COMMAND'
      IS_LAYOUT                = WS_LAYOUT
      IT_FIELDCAT              = WS_FIELDCAT[]
      IT_SPECIAL_GROUPS        = WS_FIELDGROUPS_TAB[]
      IT_SORT                  = WS_SORTFIELDS_TAB[]
      IT_EVENTS                = WS_EVENTS[]
      I_GRID_TITLE             = TITLE
    TABLES
      T_OUTTAB                 = GT_DDTL
    EXCEPTIONS
      PROGRAM_ERROR            = 1
      OTHERS                   = 2.
ENDFORM.                    " SHOW_DDTLXX


转载自: 

http://blog.csdn.net/reacleliu/article/details/6606290



你可能感兴趣的:(SAP,SAP,abap,abap)