转载:co11n——生产订单确认的BAPI

通过C#访问SAP RFC没问题,但如果在RFC里使用BDC程序,就不能实际,返回错误信息。

现只能通过BAPI来解决。

 

REPORT TEST.
DATA: WA_ITEM      TYPE ZDT_PRD_ENSURE_ITEM,
        WA_HEADER    TYPE ZDT_PRD_ENSURE_RPHEADER,
        IT_TT        TYPE TABLE OF BAPI_PP_TIMETICKET,
        WA_TT        TYPE BAPI_PP_TIMETICKET,
        IT_DR        TYPE TABLE OF BAPI_CORU_RETURN,
        WA_DR        TYPE BAPI_CORU_RETURN,
        WA_RETURN    TYPE BAPIRET1,
*        wa_e_header TYPE zstr_xi_err_header,
*        wa_e_items   TYPE zstr_xi_err_items,
*        it_e_items   TYPE TABLE OF zstr_xi_err_items,

        I_ERRMSG     TYPE BAPIRET2-MESSAGE,"记录返回消息中的错误消息
        IMPORT_PARA TYPE ZMT_AS_MS_MES2SAP_OUT,
        RPHEADER     TYPE ZDT_AS_MS_MES2SAP_RPHEADER,
        ITEM         TYPE ZDT_AS_MS_MES2SAP_ITEM,
        I_TIME(14)   TYPE C, "传送时间
*        i_text(255) TYPE c,
        I_ERRFLG     TYPE C,
*        i_log        TYPE c,
        I_COUNT      TYPE I."用于对导入数据的行数记录
PARAMETERS : P_AUFNR LIKE AFRUD-AUFNR OBLIGATORY DEFAULT '50000319',
               P_VORNR LIKE AFRUD-VORNR OBLIGATORY DEFAULT '0010',
               P_LMNGA LIKE AFRUD-LMNGA OBLIGATORY DEFAULT '3' ,
*               p_unit like AFRUD-MEINH OBLIGATORY DEFAULT 'TON',
               P_BUDAT LIKE AFRUD-BUDAT OBLIGATORY DEFAULT '20071101'.

* LOOP AT input-mt_prd_ensure_in-item INTO wa_item.
    CLEAR: RPHEADER,ITEM,WA_TT,I_COUNT,I_ERRMSG,
           I_ERRFLG,WA_RETURN.
*    ADD 1 TO i_count.
** 订单号转换

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT = P_AUFNR
      IMPORTING
        OUTPUT = WA_TT-ORDERID.
    WA_TT-OPERATION = P_VORNR.
    WA_TT-YIELD      = P_LMNGA.
*    wa_tt-scrap      = wa_item-xmnga.
    WA_TT-POSTG_DATE = P_BUDAT.

***新增 2008-05-16*******
*********计算工时********

DATA:
       C_VORNR TYPE AFVC-VORNR,
       C_AUFNR TYPE AFKO-AUFNR,
       C_APLZL TYPE AFVC-APLZL,
       C_AUFPL TYPE AFVC-AUFPL,
       WA_AFVV TYPE AFVV.

    SELECT SINGLE AFVC~APLZL AFVC~AUFPL
                  INTO (C_APLZL, C_AUFPL)
                  FROM AFVC INNER JOIN AFKO
                  ON    AFVC~AUFPL = AFKO~AUFPL
                  WHERE AFKO~AUFNR = WA_TT-ORDERID
                  AND   AFVC~VORNR = P_VORNR.

    SELECT SINGLE * FROM AFVV INTO WA_AFVV
                    WHERE AUFPL = C_AUFPL
                    AND   APLZL = C_APLZL.
*    wa_tt-CONF_QUAN_UNIT = p_unit.
    WA_TT-CONF_ACTI_UNIT1 = WA_AFVV-VGE01.
    WA_TT-CONF_ACTI_UNIT2 = WA_AFVV-VGE02.
    WA_TT-CONF_ACTI_UNIT3 = WA_AFVV-VGE03.
    WA_TT-CONF_ACTI_UNIT4 = WA_AFVV-VGE04.
    WA_TT-CONF_ACTI_UNIT5 = WA_AFVV-VGE05.
    WA_TT-CONF_ACTI_UNIT6 = WA_AFVV-VGE06.
    WA_TT-CONF_ACTIVITY1 = WA_TT-YIELD * WA_AFVV-VGW01 / WA_AFVV-BMSCH.
    WA_TT-CONF_ACTIVITY2 = WA_TT-YIELD * WA_AFVV-VGW02 / WA_AFVV-BMSCH.
    WA_TT-CONF_ACTIVITY3 = WA_TT-YIELD * WA_AFVV-VGW03 / WA_AFVV-BMSCH.
    WA_TT-CONF_ACTIVITY4 = WA_TT-YIELD * WA_AFVV-VGW04 / WA_AFVV-BMSCH.
    WA_TT-CONF_ACTIVITY5 = WA_TT-YIELD * WA_AFVV-VGW05 / WA_AFVV-BMSCH.
    WA_TT-CONF_ACTIVITY6 = WA_TT-YIELD * WA_AFVV-VGW06 / WA_AFVV-BMSCH.
*************************

    APPEND WA_TT TO IT_TT.
    CALL FUNCTION 'BAPI_PRODORDCONF_CREATE_TT'   “主要是这个BAPI
      IMPORTING
        RETURN        = WA_RETURN
      TABLES
        TIMETICKETS   = IT_TT
        DETAIL_RETURN = IT_DR.

*   处理BAPI错误
    LOOP AT IT_DR INTO WA_DR.
      IF WA_DR-TYPE = 'E'.
        I_ERRFLG = 'X'.
        RPHEADER-FREEUSE1 = 'ERR'.
* 生成outbound数据item段error message
        ITEM-TYPE = WA_DR-TYPE.
* 合并错误message
        CALL FUNCTION 'Z_XI_GET_ERRMESSAGE'
          EXPORTING
            I_MESSAGE1 = WA_DR-MESSAGE
            I_MESSAGE2 = I_ERRMSG
          IMPORTING
            O_MESSAGE = ITEM-MESSAGE.
        I_ERRMSG = WA_DR-MESSAGE.
      ELSE.
        RPHEADER-FREEUSE1 = 'OK'.
        ITEM-MAT_DO     = WA_DR-CONF_NO. "生产订单确认号
        ITEM-HEADER_TXT = WA_TT-ORDERID. "生产订单号
        ITEM-BILL_OF_LADING = WA_DR-CONF_CNT. "确认计数器
        SHIFT ITEM-MAT_DO LEFT DELETING LEADING '0'.
        SHIFT ITEM-HEADER_TXT LEFT DELETING LEADING '0'.
        SHIFT ITEM-BILL_OF_LADING LEFT DELETING LEADING '0'.
      ENDIF.

      IF I_ERRFLG IS INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            WAIT = 'X'.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDIF.
    ENDLOOP.

* 处理返回信息
    APPEND ITEM TO IMPORT_PARA-MT_AS_MS_MES2SAP_OUT-ITEM.

* 对上传数据进行判断

    RPHEADER-FREEUSE1 = 'ERR'.
    ITEM-TYPE = 'E'.
    ITEM-MESSAGE = 'Item数据为空,无法确认生产订单'.
    APPEND ITEM TO IMPORT_PARA-MT_AS_MS_MES2SAP_OUT-ITEM.

* 生成outbound数据head段
RPHEADER-TLGNAME = WA_HEADER-TLGNAME.
RPHEADER-TLGID    = WA_HEADER-TLGID.
RPHEADER-RECEIVER = WA_HEADER-SENDER.

CALL FUNCTION 'Z_XI_ASYNCHRONISM_SEND'
    TABLES
      ITEM         = IMPORT_PARA-MT_AS_MS_MES2SAP_OUT-ITEM
    CHANGING
      RPHEADER     = RPHEADER
    EXCEPTIONS
      SYSTEM_ERROR = 1.
IF SY-SUBRC = 1.
ENDIF.

你可能感兴趣的:(function,header,table,SAP,Parameters,import)