ABAP:MIGO创建物料凭证BAPI

MIGO物料凭证冲销BAPI

FUNCTION zsdfu014.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_ID) TYPE  ZRFC_LOGID OPTIONAL
*"     VALUE(I_CANUM) TYPE  CANUM OPTIONAL
*"  EXPORTING
*"     VALUE(E_CODE) TYPE  MSGTY
*"     VALUE(E_MSG) TYPE  MSGTXT
*"     VALUE(E_FLOW_END) TYPE  CHAR01
*"  TABLES
*"      ET_RETURN STRUCTURE  ZRFCT005 OPTIONAL
*"----------------------------------------------------------------------
  DATA:ls_zsdt022 TYPE zsdt022.
  DATA:ls_zsdt013_1 TYPE zsdt013,
       lt_zsdt013   TYPE TABLE OF zsdt013,
       ls_zsdt013_2 TYPE zsdt013.

  SELECT * INTO TABLE @DATA(lt_zsdt022) FROM zsdt022 WHERE zrfc_logid = @i_id.
  IF lt_zsdt022 IS INITIAL.
    e_code = 'E'.
    e_msg = '无可执行数据'.
    RETURN.
  ENDIF.

  DATA:ls_head     TYPE bapi2017_gm_head_02,
       ls_head_ret TYPE bapi2017_gm_head_ret,
       lt_return   TYPE TABLE OF bapiret2,
       ls_return   TYPE bapiret2.

  DATA:message TYPE c LENGTH 200.
  DATA:ls_zrfct002 TYPE zrfct002,
       ls_zsdt013  TYPE zsdt013,
       ls_zrfct005 TYPE zrfct005.

  DATA:ls_head_im TYPE bapi2017_gm_head_01,
       ls_head_ex TYPE bapi2017_gm_head_ret,
       lt_item    TYPE TABLE OF bapi2017_gm_item_create,
       ls_item    TYPE bapi2017_gm_item_create.
  DATA:lv_mblnr TYPE bapi2017_gm_serialnumber.
  LOOP AT lt_zsdt022 INTO ls_zsdt022.

    ls_head_im-pstng_date = ls_zsdt022-budat.
    ls_head_im-ref_doc_no = ls_zsdt022-xblnr.
    ls_head_im-doc_date = ls_zsdt022-budat.
    ls_head_im-pr_uname = sy-uname.

    ls_item-material = ls_zsdt022-matnr.
    ls_item-plant = ls_zsdt022-werks.
    ls_item-stge_loc = ls_zsdt022-lgort.
    ls_item-batch = ls_zsdt022-charg.
    ls_item-move_type = ls_zsdt022-bwart.
    ls_item-entry_qnt = ls_zsdt022-menge.
    ls_item-mvt_ind = 'B'.
    ls_item-po_number = ls_zsdt022-ebeln.
    ls_item-po_item = ls_zsdt022-ebelp.
    ls_item-ref_doc = ls_zsdt022-zmblnr.
    ls_item-ref_doc_it = ls_zsdt022-zeile.
    ls_item-move_reas = ls_zsdt022-grund.
    APPEND ls_item TO lt_item.

    SELECT SINGLE * INTO ls_zsdt013_1 FROM zsdt013 WHERE zmblnr = ls_zsdt022-zmblnr.
    ls_zsdt013-zrfc_logid = i_id.
    ls_zsdt013-canum = i_canum.
    ls_zsdt013-zzrfc_logid = ls_zsdt013_1-zrfc_logid.
    ls_zsdt013-zcanum = ls_zsdt013_1-canum.
    ls_zsdt013-mblnr = ls_zsdt022-mblnr.
    ls_zsdt013-mjahr = ls_zsdt022-mjahr.
    ls_zsdt013-zrfc_logid_2 = ls_zsdt013_1-zrfc_logid.
    ls_zsdt013-canum_2 = ls_zsdt013_1-canum.
    ls_zsdt013-zzrfc_logid_2 = ls_zsdt013_1-zzrfc_logid.
    ls_zsdt013-zcanum_2 = ls_zsdt013_1-zcanum.
    ls_zsdt013-objtype = 'GS'.
    ls_zsdt013-objkey = ls_zsdt013_1-objkey.
    ls_zsdt013-zobjtype = 'PO'.
    ls_zsdt013-zobjkey = ls_zsdt013_1-zobjkey.
    ls_zsdt013-zrfcid = ls_zsdt013_1-zrfcid.
    APPEND ls_zsdt013 TO lt_zsdt013.
    CLEAR:ls_zsdt022,ls_item,ls_zsdt013,ls_zsdt013_1.
  ENDLOOP.


  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = ls_head_im
      goodsmvt_code    = '01'
    IMPORTING
      goodsmvt_headret = ls_head_ex
    TABLES
      goodsmvt_item    = lt_item
      return           = lt_return.

  LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
    CONCATENATE message ls_return-message INTO message SEPARATED BY  '/'.
  ENDLOOP.
  IF sy-subrc NE 0.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = i_id
        i_canum    = i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    e_code = 'S'.
    CONCATENATE  ls_head-mat_doc '冲销成功' INTO e_msg.

    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = e_msg.
    ls_zrfct005-msgv4 = ls_head_ex-mat_doc.
    APPEND ls_zrfct005 TO et_return.


    ls_zsdt013_2-zmblnr = ls_head_ex-mat_doc.
    ls_zsdt013_2-fkstol = 'X'.
    MODIFY lt_zsdt013 FROM ls_zsdt013_2
                      TRANSPORTING zmblnr fkstol
                      WHERE zmblnr IS INITIAL.

    MODIFY zsdt013 FROM TABLE lt_zsdt013.

  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    e_code = 'E'.
    e_msg = message.
  ENDIF.
ENDFUNCTION.

 MIGO:工单发料

*----------------------------------------------------------------------*
***INCLUDE LZMMFG_OA002F01.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form frm_generate_mblnr
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> IT_INFO
*&      <-- EV_TYPE
*&      <-- EV_MESSAGE
*&---------------------------------------------------------------------*
FORM frm_generate_mblnr TABLES pt_info STRUCTURE zsmmfu002_info
                         USING pv_bldat pv_budat pv_bktxt
                         CHANGING pv_type pv_message pv_mblnr pv_mjahr.

  DATA:goodsmvt_header LIKE  bapi2017_gm_head_01,
       itab            TYPE TABLE OF bapi2017_gm_item_create WITH HEADER LINE,
       return          LIKE TABLE OF bapiret2 WITH HEADER LINE,
       mat_doc         LIKE  bapi2017_gm_head_ret-mat_doc,
       mjahr           TYPE bapi2017_gm_head_ret-doc_year.

  DATA:ls_ztsnro TYPE ztsnro,
       lt_ztsnro2 TYPE TABLE OF ztsnro,
       lt_data TYPE TABLE OF zsmmfu002_info.

  CLEAR:goodsmvt_header.

  goodsmvt_header-doc_date = pv_bldat.    "凭证中的凭证日期
  goodsmvt_header-pstng_date = pv_budat.  "凭证中的过帐日期
  goodsmvt_header-pr_uname = sy-datum.    "用户名
  goodsmvt_header-header_txt = pv_bktxt.  "抬头文本


  SORT pt_info BY zeile.

  READ TABLE pt_info INDEX 1.
  IF sy-subrc EQ 0 AND pt_info-bwart EQ 'Z26'.
    lt_data = pt_info[].
    "查询物料对应的流水号-日期
    WITH +i AS ( SELECT DISTINCT matnr
                   FROM @lt_data AS g )
  SELECT a~*
    FROM +i AS i
    LEFT JOIN ztsnro AS a
      ON i~matnr EQ a~matnr
   WHERE datum EQ @sy-datum
    INTO TABLE @DATA(lt_ztsnro).

    SORT lt_ztsnro BY matnr.
  ENDIF.

  LOOP AT pt_info.

    itab-plant = pt_info-werks."工厂
    itab-material = pt_info-matnr."物料编码
    itab-entry_qnt = pt_info-menge."数量
    itab-entry_uom = pt_info-meins."单位
    itab-move_type = pt_info-bwart."移动类型
    itab-costcenter = pt_info-kostl."成本中心
    itab-stge_loc = pt_info-lgort."库存地点

    IF pt_info-bwart EQ 'Z26'."散件需要自动生成批次号

      READ TABLE lt_ztsnro INTO ls_ztsnro
                           WITH KEY matnr = pt_info-matnr
                           BINARY SEARCH.
      IF sy-subrc EQ 0.
        "查到后,流水号+1
        ls_ztsnro-snro = ls_ztsnro-snro + 1.
      ELSE.
        "没查到,重新生成流水号
        ls_ztsnro-matnr = pt_info-matnr.
        ls_ztsnro-snro = 1.
        ls_ztsnro-datum = sy-datum.
      ENDIF.

      "流水号
      itab-batch = |{ sy-datum+2(6) }{ ls_ztsnro-snro }|.

      APPEND ls_ztsnro TO lt_ztsnro2.
      CLEAR:ls_ztsnro.
    ELSE.
      itab-batch = pt_info-charg."成品批次编号-手动赋值
    ENDIF.
    APPEND itab.
    CLEAR itab.
  ENDLOOP.

  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = goodsmvt_header
      goodsmvt_code    = '03'
    IMPORTING
      materialdocument = mat_doc
      matdocumentyear  = mjahr
    TABLES
      goodsmvt_item    = itab
      return           = return.

  LOOP AT return WHERE type CA 'EAX'.

    IF pv_message IS INITIAL.
      pv_message = return-message.
    ELSE.
      pv_message = |{ pv_message },{ return-message }|.
    ENDIF.

  ENDLOOP.
  IF  sy-subrc = 0.

    pv_type = 'E'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.

    pv_type = 'S'.
    pv_message = '物料凭证创建成功'.
    pv_mblnr = mat_doc.
    pv_mjahr = mjahr.

    "更新流水号表
    IF lt_ztsnro2 IS NOT INITIAL.
      MODIFY ztsnro FROM TABLE lt_ztsnro2.
      COMMIT WORK AND WAIT.
    ENDIF.
  ENDIF.
ENDFORM.

 

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