首页 > 代码库 > 收货MIGO

收货MIGO

FUNCTION zrfc_mm003.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(CALLNO) TYPE  ZCALLNO
*"     VALUE(BUDAT) LIKE  BAPI2017_GM_HEAD_01-PSTNG_DATE DEFAULT
*"       SY-DATUM
*"     VALUE(BLDAT) LIKE  BAPI2017_GM_HEAD_01-DOC_DATE DEFAULT SY-DATUM
*"  EXPORTING
*"     VALUE(FLAG) LIKE  BAPIRET2-TYPE
*"     VALUE(MESSAGE) LIKE  BAPIRET2-MESSAGE
*"  TABLES
*"      ZGOODSMVT STRUCTURE  ZGOODSMVT
*"      ZMSEG STRUCTURE  ZMSEG
*"----------------------------------------------------------------------
DATA:ls_goodsmvt_header LIKE bapi2017_gm_head_01,
     ls_goodsmvt_code   LIKE bapi2017_gm_code VALUE 01,
     lt_goodsmvt_item   LIKE TABLE OF bapi2017_gm_item_create,
     ls_goodsmvt_item   LIKE LINE  OF lt_goodsmvt_item,
     lt_zgoodsmvt       LIKE TABLE OF zgoodsmvt,
     ls_zgoodsmvt       LIKE LINE  OF lt_zgoodsmvt,
     ls_mvt_ind         LIKE bapi2017_gm_item_create-mvt_ind VALUE B, "采购订单收货
     ls_ex_headret      LIKE bapi2017_gm_head_ret,
     lv_mat_doc         LIKE bapi2017_gm_head_ret-mat_doc,
     lv_doc_year        LIKE bapi2017_gm_head_ret-doc_year,
     lv_message         LIKE bapiret2-message,
     lv_bwart           LIKE mseg-bwart VALUE 101.
DATA: entry_qnt(16),
      lv_datano         TYPE zdatano,
      lt_zmms003        TYPE TABLE OF zmms003,
      ls_zmms003        TYPE zmms003.

DATA: lt_zmmt002        TYPE TABLE OF zmmt002,                          "记录会计凭证
      ls_zmmt002        TYPE zmmt002,
      lv_awkey          TYPE awkey,
      lv_bukrs          TYPE bkpf-bukrs,                                "公司代码
      lv_belnr          TYPE bkpf-belnr,                                "会计凭证号
      lv_gjahrs         TYPE bkpf-gjahr,                                "会计年度号
      lv_blart          TYPE bkpf-blart.                                "会计凭证类型

     ls_goodsmvt_header-pstng_date = budat.                             "过账日期
     ls_goodsmvt_header-doc_date   = bldat.                             "凭证日期

     lt_zgoodsmvt = zgoodsmvt[].
     LOOP AT lt_zgoodsmvt INTO ls_zgoodsmvt.
      ls_goodsmvt_item-move_type = lv_bwart .                           "移动类型
      ls_goodsmvt_item-entry_qnt = ls_zgoodsmvt-entry_qnt.              "收货数量
      ls_goodsmvt_item-po_number = ls_zgoodsmvt-po_number.              "采购订单编号
      ls_goodsmvt_item-po_item   = ls_zgoodsmvt-po_item.                "采购订单项目
      ls_goodsmvt_item-mvt_ind   = ls_mvt_ind.                          "
      ls_goodsmvt_item-item_text = ls_zgoodsmvt-sgtxt.                  "项目文本,这边传输的是预算号
      APPEND ls_goodsmvt_item TO lt_goodsmvt_item.

      ls_zmms003-bstnr           = ls_zgoodsmvt-po_number.              "采购订单编号
      ls_zmms003-ebelp           = ls_zgoodsmvt-po_item.                "采购订单项目
      ls_zmms003-xref2           = ls_zgoodsmvt-xref2.                  "参考码2(预算号)
      ls_zmms003-zuonr           = ls_zgoodsmvt-zuonr.                  "分配(ECM单号+username)
      APPEND ls_zmms003 TO lt_zmms003.
      CLEAR: ls_goodsmvt_item,ls_zmms003.
     ENDLOOP.

*--内存传输参考码2 和ECM单号
EXPORT  zrfc_mm003 = lt_zmms003  TO  MEMORY ID ZRFC_MM003.

*收货过账
CLEAR:gt_return,ls_ex_headret,lv_mat_doc,lv_doc_year.
CALL FUNCTION BAPI_GOODSMVT_CREATE
  EXPORTING
    goodsmvt_header               = ls_goodsmvt_header
    goodsmvt_code                 = ls_goodsmvt_code
 IMPORTING
   goodsmvt_headret               = ls_ex_headret
   materialdocument               = lv_mat_doc
   matdocumentyear                = lv_doc_year
  TABLES
    goodsmvt_item                 = lt_goodsmvt_item
    return                        = gt_return.

  READ TABLE gt_return INTO gs_return WITH KEY type = E.
  IF sy-subrc NE 0.
    flag = S.
    CALL FUNCTION BAPI_TRANSACTION_COMMIT
      EXPORTING
        wait = X.

*    mat_doc  = lv_mat_doc.                                              "物料凭证
*    doc_year = lv_doc_year.                                             "年度
    CONCATENATE 生成凭证号: lv_mat_doc 年度: lv_doc_year INTO message.
  ELSE.
    CALL FUNCTION BAPI_TRANSACTION_ROLLBACK.

    LOOP AT gt_return INTO gs_return WHERE type CA AEX.
        MESSAGE ID     gs_return-id
                TYPE   gs_return-type
                NUMBER gs_return-number
                WITH   gs_return-message_v1 gs_return-message_v2
                       gs_return-message_v3 gs_return-message_v4
                       INTO lv_message.
        CONCATENATE message lv_message INTO message SEPARATED BY .
    ENDLOOP.
    SHIFT message LEFT DELETING LEADING  .
    flag = E.
  ENDIF.

*--产生的物料凭证项目信息
  IF flag = S.
     SELECT mblnr  mjahr  zeile INTO TABLE zmseg[] FROM mseg
       WHERE mblnr = lv_mat_doc
        AND  mjahr = lv_doc_year.
  ENDIF.

*---记录暂估会计凭证
  CLEAR: lv_awkey,lv_bukrs,lv_belnr,lv_gjahrs,lv_blart.
  CONCATENATE lv_mat_doc lv_doc_year INTO lv_awkey.

IF lv_awkey IS NOT INITIAL.
SELECT SINGLE bukrs belnr gjahr blart INTO (lv_bukrs,lv_belnr,lv_gjahrs,lv_blart)
  FROM bkpf WHERE awtyp = MKPF
             AND  awkey = lv_awkey.
ENDIF.

SELECT  bukrs belnr gjahr buzei zuonr sgtxt xref1 xref2 xref3
  INTO CORRESPONDING FIELDS OF TABLE lt_zmmt002
  FROM bseg
  WHERE bukrs  = lv_bukrs
  AND   belnr  = lv_belnr
  AND   gjahr  = lv_gjahrs.

  ls_zmmt002-blart = lv_blart.
  MODIFY lt_zmmt002 FROM  ls_zmmt002 TRANSPORTING blart WHERE blart IS INITIAL .

  INSERT zmmt002 FROM TABLE lt_zmmt002.
  IF sy-subrc = 0.
     COMMIT WORK AND WAIT.
  ELSE.
     ROLLBACK WORK.
  ENDIF.


*----记录日志
  CLEAR:gt_log,gs_log,ls_zgoodsmvt,entry_qnt.

  LOOP AT lt_zgoodsmvt INTO ls_zgoodsmvt.
   lv_datano     = lv_datano + 1.
   gs_log-datano = lv_datano.
   gs_log-name   = ZRFC_MM003.
   gs_log-cdate  = sy-datum.
   GET TIME.
   gs_log-ctime  = sy-uzeit.
   gs_log-callno = callno.
   gs_log-flag   = flag.
   gs_log-log    = message.
   entry_qnt = ls_zgoodsmvt-entry_qnt.
   CONCATENATE budat bldat
     entry_qnt  ls_zgoodsmvt-po_number ls_zgoodsmvt-po_item
     ls_zgoodsmvt-sgtxt
     ls_zgoodsmvt-xref2
     ls_zgoodsmvt-zuonr
   INTO gs_log-content SEPARATED BY |.

   CLEAR ls_goodsmvt_item.
   CONDENSE gs_log-content NO-GAPS.
   gs_log-length = STRLEN( gs_log-content ).
   APPEND gs_log TO gt_log.
   CLEAR:gs_log,ls_zgoodsmvt,entry_qnt.
  ENDLOOP.

 IF gt_log IS NOT INITIAL.
   INSERT zrfc_mm01in_log FROM TABLE gt_log.
   IF sy-subrc = 0.
     COMMIT WORK.
   ENDIF.
 ENDIF.


ENDFUNCTION.

 

收货MIGO