ABAP:MIGO收货BAPI

MIGO收货BAPI:

  DATA:lt_zsdt008   TYPE TABLE OF zsdt008,
       ls_zsdt008   LIKE LINE OF lt_zsdt008,
       ls_zsdt008_2 LIKE LINE OF lt_zsdt008,
       ls_zsdt014   TYPE zsdt014,
       lt_zsdt014   TYPE TABLE OF zsdt014,
       lt_zsdt013   TYPE TABLE OF zsdt013,
       ls_zsdt013   LIKE LINE OF lt_zsdt013,
       lt_zsdt013_2 TYPE TABLE OF zsdt013,
       ls_zsdt013_2 LIKE LINE OF lt_zsdt013,
       lt_zsdt012   TYPE TABLE OF zsdt012,
       ls_zsdt012   LIKE LINE OF lt_zsdt012,
       ls_ekpo      TYPE ekpo,
       lt_ekpo      TYPE TABLE OF ekpo.
  DATA:lv_ebeln TYPE ekko-ebeln,
       lv_vbeln TYPE vbap-vbeln,
       lv_tabix TYPE sy-tabix.
  DATA:goodsmvt_header LIKE  bapi2017_gm_head_01,
       goodsmvt_item   LIKE TABLE OF bapi2017_gm_item_create WITH HEADER LINE.
  DATA:lv_zrfcid TYPE zsdt008-zrfcid.
  DATA:lv_mjahr TYPE mjahr.

  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE lt_zsdt012
     FROM zsdt012
     WHERE zrfc_logid = i_id.
  IF lt_zsdt012 IS INITIAL.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.
  READ TABLE lt_zsdt012 INTO ls_zsdt012 INDEX 1.
  SELECT SINGLE zrfcid INTO lv_zrfcid FROM zsdt008 WHERE objkey = ls_zsdt012-ebeln AND objtype = 'PO' AND canum = '0010'.
  SELECT SINGLE *
    FROM zsdt014
    INTO ls_zsdt014
    WHERE zrfcid = ls_zsdt012-zrfcid
    AND canum = i_canum
    AND zzrfcid = lv_zrfcid.

  SORT lt_zsdt012 BY mblnr zeile.
  LOOP AT lt_zsdt012 INTO ls_zsdt012.
    CLEAR:ls_zsdt008,ls_zsdt008_2.
    SELECT SINGLE *
      FROM zsdt008
      INTO ls_zsdt008"源头采购订单
      WHERE objkey = ls_zsdt012-ebeln
        AND zrfcid = ls_zsdt014-zzrfcid.
    CHECK ls_zsdt008 IS NOT INITIAL.
    SELECT SINGLE *
      FROM zsdt008
      INTO ls_zsdt008_2"当前采购订单
      WHERE zrfc_logid = ls_zsdt008-zrfc_logid
        AND canum = ls_zsdt014-zcanum.
    SELECT SINGLE *
      FROM ekpo
      INTO ls_ekpo
      WHERE ebeln = ls_zsdt008_2-objkey
        AND ebelp = ls_zsdt012-ebelp.
    goodsmvt_header-pstng_date = ls_zsdt012-budat."过帐日期.
    goodsmvt_header-doc_date = ls_zsdt012-bldat."凭证日期.
    goodsmvt_header-pr_uname = sy-uname."用户名
    goodsmvt_header-ref_doc_no = ls_zsdt012-xblnr.
    goodsmvt_item-material = ls_ekpo-matnr."物料
    goodsmvt_item-plant = ls_ekpo-werks." 工厂
    IF ls_ekpo-werks = '3189'.
      goodsmvt_item-stge_loc = 'US01'.
    ELSE.
      goodsmvt_item-stge_loc = 'NX00'.
    ENDIF.
*    goodsmvt_item-entry_qnt = ls_zsdt012-zocdqty."数量
*    goodsmvt_item-entry_qnt = ls_ekpo-menge."数量
    SELECT SINGLE bstmg
      INTO goodsmvt_item-entry_qnt
      FROM mseg
     WHERE mblnr = ls_zsdt012-mblnr
       AND mjahr = ls_zsdt012-mjahr
       AND zeile = ls_zsdt012-zeile.
    SELECT COUNT(*)"启用批次
      FROM marc
      WHERE matnr = ls_ekpo-matnr
        AND werks = ls_ekpo-werks
        AND xchpf = 'X'.
    SELECT SINGLE bsart INTO @DATA(lv_bsart) FROM ekko WHERE ebeln = @ls_zsdt008_2-objkey.
    IF sy-subrc = 0.
      goodsmvt_item-batch     = ls_zsdt012-charg."批次
      SELECT COUNT(*) FROM mcha
        WHERE matnr = ls_ekpo-matnr
          AND werks = ls_ekpo-werks
          AND charg = ls_zsdt012-charg.
      IF sy-subrc <> 0.
        IF lv_bsart NE 'ZFAI' OR (
           lv_bsart = 'ZFAI' AND
           ls_zsdt012-charg NE '' ).
          CALL FUNCTION 'Z_BATCH_A'
            EXPORTING
              zmatnr     = ls_ekpo-matnr
              zwerks     = ls_ekpo-werks
              zbatch     = ls_zsdt012-charg
              ztype      = 'Y'
              isrefbatch = 'Y'.
        ENDIF.

      ENDIF.
    ENDIF.
    goodsmvt_item-entry_uom = ls_ekpo-meins."单位
*    goodsmvt_item-move_type = '101'. " 移动类型
    SELECT SINGLE bwart INTO goodsmvt_item-move_type
      FROM mseg
      WHERE mblnr = ls_zsdt012-mblnr AND mjahr = ls_zsdt012-mjahr AND zeile = ls_zsdt012-zeile.
*    GOODSMVT_ITEM-MOVE_REAS = LS_ZSDT012-GRUND."移动原因
    goodsmvt_item-mvt_ind = 'B'."移动标识:B 按照PO进行的货物移动
    goodsmvt_item-po_number = ls_zsdt008_2-objkey.   "采购订单
    goodsmvt_item-po_item = ls_zsdt012-ebelp.          "行项目
    SELECT SINGLE insmk
      INTO goodsmvt_item-stck_type
      FROM ekpo
      WHERE ebeln = goodsmvt_item-po_number
        AND ebelp = goodsmvt_item-po_item.
    APPEND goodsmvt_item.
    CLEAR goodsmvt_item.
    lv_mjahr = ls_zsdt012-mjahr.
    AT END OF mblnr.
      ls_zsdt013_2-canum      = i_canum.
      ls_zsdt013_2-zzrfc_logid = ls_zsdt008_2-zrfc_logid.
      ls_zsdt013_2-zcanum = ls_zsdt008_2-canum.
      ls_zsdt013_2-objkey    = ls_zsdt008_2-objkey.
      ls_zsdt013_2-zrfcid    = ls_zsdt014-zrfcid.
      ls_zsdt013_2-zrfc_logid    = i_id.
      ls_zsdt013_2-mblnr      = ls_zsdt012-mblnr.
      ls_zsdt013_2-mjahr      = lv_mjahr.
      ls_zsdt013_2-zobjkey    = ls_zsdt008-objkey.
      ls_zsdt013_2-zobjtype    = ls_zsdt008-objtype.
      APPEND ls_zsdt013_2 TO lt_zsdt013_2.
    ENDAT.
  ENDLOOP.
  PERFORM create_gr TABLES et_return goodsmvt_item lt_zsdt013_2 USING i_id i_canum goodsmvt_header ls_zsdt014 CHANGING e_code e_msg e_flow_end.
  REFRESH:goodsmvt_item,lt_zsdt013_2.

FORM create_gr  TABLES   p_et_return STRUCTURE zrfct005
                          p_goodsmvt_item STRUCTURE bapi2017_gm_item_create
                          p_lt_zsdt013 STRUCTURE zsdt013
                 USING    p_i_id TYPE zrfc_logid
                          p_i_canum TYPE canum
                          p_goodsmvt_header TYPE bapi2017_gm_head_01
                          p_ls_zsdt014 TYPE zsdt014
                 CHANGING p_e_code
                          p_e_msg
                          p_e_flow_end.
  DATA:message          TYPE c LENGTH 200,
       lt_return        LIKE TABLE OF bapiret2 WITH HEADER LINE,
       goodsmvt_item    LIKE  bapi2017_gm_item_create,
       materialdocument TYPE  bapi2017_gm_head_ret-mat_doc.

  DATA:ls_zrfct005 TYPE zrfct005.
  DATA:ls_zrfct002 TYPE zrfct002.
  DATA:lt_zsdt013 LIKE TABLE OF zsdt013,
       ls_zsdt013 LIKE LINE OF lt_zsdt013.
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = p_goodsmvt_header
      goodsmvt_code    = '01'
    IMPORTING
      materialdocument = materialdocument
    TABLES
      goodsmvt_item    = p_goodsmvt_item[]
      return           = lt_return.
  CLEAR:message.
  LOOP AT lt_return WHERE type = 'A' OR type = 'E' .
    CONCATENATE message lt_return-message INTO message SEPARATED BY  '/'.
  ENDLOOP.
  IF  sy-subrc <> '0'.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = p_i_id
        i_canum    = p_i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.
*          RECEIVING
*            R_SOURCE   = DATA(LS_SOURCE).
    LOOP AT p_lt_zsdt013 INTO ls_zsdt013.
      ls_zsdt013-objtype = ls_zrfct002-objtype_save.
      ls_zsdt013-zmblnr = materialdocument.
      MODIFY p_lt_zsdt013 FROM ls_zsdt013.
    ENDLOOP.
    MODIFY zsdt013 FROM TABLE p_lt_zsdt013.
*    CLEAR LT_ZSDT013[].
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    p_e_code = 'S'.
    CONCATENATE 'GR:' materialdocument '收货成功' INTO p_e_msg.

    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = p_e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = p_e_msg.
    ls_zrfct005-msgv4 = materialdocument.
    APPEND ls_zrfct005 TO p_et_return.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    p_e_code = 'E'.
    p_e_msg = message.
  ENDIF.
ENDFORM.                    " CREATE_GR

 

posted @ 2022-12-19 10:31  阿胖的阿多  阅读(534)  评论(0编辑  收藏  举报