SAP BAPI BAPI_GOODSMVT_CREATE Goods movement

ielxzhang發表於2019-02-27

這個BAPI一直還活著,不知道為什麼,可能SAP沒工夫升級吧。。。

 

今天主要是GM_CODE的問題,對應如下,同時下面轉一個同學的文章。。。

GM_CODETcode描述MovementType
01MB01對採購訂單收貨101/
02MB31按生產訂單收貨101/
03MB1A消耗出貨201/261/541/551
04MB1B轉移過賬301/311/321/325/344
05MB1C其他收貨Z67/Z68/501/511
06MB11貨物移動 
07MB04委外調整 

   

Movement Type:

101 倉庫收料 102退料

201 工程領料 202工程退料 cost center

261 倉庫發料 262退料

411 轉倉 412退回 sales order GR/GI Slip No.

501 倉庫without PO 收料 by G/L Account 502 退回

 

561 初始化庫存 562退回

Movement Type

1.物料的移動在SAP中的反映,也可以理解為物流業務在SAP中的反映

2.移動型別可以根據物料型別的Quantity/Value屬性,更新(數量(Stock)或價值(FI憑證/科目的確定)

3.移動型別的欄位顯示(Screen)可以在IMG中設定

4.MRP,ATP check 等計算的基礎

5.連結SAP各模組

Mvt type Description

1xx GR from Pur/Prd and return

2xx GI for Consumption

3xx,4xx Transfer

5xx GR W/O ref to Pur/Prd Order

6xx LE-SHP movement types

7xx Physical inventory(IM:70x/WM:71x)

8xx Brazil

Customer 9xx,Xxx,Yxx,Zxx

    MM模組通過移動型別來區分物料的移動,MB11可以處理工廠內所有的移動,而MB11不便於許可權管理,因此在工廠往往使用MB1A,MB1B,MB1C來處理廠內物流。
    MB1A通常用於發貨類,例如201 成本中心消耗, 261 內部訂單消耗,541 委外加工發貨,551 庫房報損均使用MB1A。
    MB1B通常用於轉儲類,例如301 工廠間轉儲(一步) 311 儲存地點間轉儲 309 編碼轉換 321-325 庫存類為凍結/質檢狀態的記帳修改 移動
    MB1C通常用於收貨類,例如101 採購訂單收貨 103 採購訂單收貨至凍結狀態 122 無採購訂單退貨 161 按退貨採購訂單退貨 131 從生產訂單501 無採購訂單收貨 511 免費收貨 

 

一、收貨(101)

說明:採購訂單、生產訂單收貨

form recvfromord tables pt_tran structure zwttran.

  "移庫定義

  data: goodsmvt_header  type   bapi2017_gm_head_01,

        goodsmvt_code    type   bapi2017_gm_code,

        goodsmvt_headret type   bapi2017_gm_head_ret,

        materialdocument type   bapi2017_gm_head_ret-mat_doc,

        matdocumentyear  type   bapi2017_gm_head_ret-doc_year,

        goodsmvt_item    type standard table of   /afs/bapi2017_gm_item_create,

        return           type standard table of   bapiret2,

        w_creturn        type   bapiret2,

        w_goodsmvt_item  type                     /afs/bapi2017_gm_item_create,

        w_return         type   bapiret2.

  data:lt_retmsg like table of bapiret2 with header line,

       lt_return like table of bapiret2 with header line.

  data:l_tran_nbr like zwttran-tran_nbr,

       l_tranpo   like zwttran-tranpo,

       l_matdoc   like zwttran-matdoc,

       l_flag     like zwttran-flag,

       l_line_id type i.

  data:l_asn_nbr type ze_ref_field, "asn

       l_po type ze_ref_field,"po

       l_po_line(5) type n,"po行號

       l_matnr type matnr."貨號

  data:l_type type char1,"型別

       l_afpo type afpo,"生產訂單

       l_j_3abssi type  j_3abssi,"生產訂單計劃行

       l_eket type eket,"採購訂單計劃行

       l_ekpo type ekpo."採購訂單行專案

  data:lt_bom like table of zwmbom with header line.

  refresh:t_tran_nbr.

  loop at pt_tran.

    clear :t_tran_nbr.

    t_tran_nbr-tran_nbr = pt_tran-tran_nbr.

    if pt_tran-tran_type = '603' and pt_tran-tran_code = '02' and pt_tran-proc_stat_code = '10'  and pt_tran-ref_field_6 = 'P'.

      t_tran_nbr-po = pt_tran-ref_field_3.

    endif.

    append t_tran_nbr.

  endloop.

  sort t_tran_nbr by tran_nbr po.

  delete adjacent duplicates from t_tran_nbr.

 

  loop at t_tran_nbr.

    clear:l_tran_nbr,l_matdoc,l_matnr,l_asn_nbr,l_po,l_matnr,l_type,l_afpo,l_j_3abssi,l_eket,l_ekpo,l_tranpo,lt_retmsg,lt_return,pt_tran.

    refresh:lt_return.

 

    clear:goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.

    refresh:goodsmvt_item ,return.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where tran_type = '603' and tran_code = '02'  and proc_stat_code = '10' and units_rcvd > 0

                      and tran_nbr = t_tran_nbr-tran_nbr and ref_field_3 = t_tran_nbr-po.

      if pt_tran-ref_field_6 = 'P'.

        l_tran_nbr =  pt_tran-tran_nbr.

        "獲取asn

        l_asn_nbr = pt_tran-ref_field_1.

        l_po = pt_tran-ref_field_3.

        concatenate l_tran_nbr l_po into l_tranpo.

        if pt_tran-ref_field_4 is not initial.

          l_po_line = pt_tran-ref_field_4.

        else.

          clear l_po_line.

        endif.

        l_matnr = pt_tran-matnr.

        clear:lt_retmsg.

        refresh:lt_retmsg.

        call function 'ZLC_GET_PREMOVE101'

          EXPORTING

            p_matnr    = l_matnr

            p_po       = l_po

            p_po_line  = l_po_line

          IMPORTING

            p_type     = l_type

            p_afpo     = l_afpo

            p_j_3abssi = l_j_3abssi

            p_ekpo     = l_ekpo

            p_eket     = l_eket

          TABLES

            pt_retmsg  = lt_retmsg.

        read table lt_retmsg with key type = 'E'.

        if sy-subrc <> 0.

          case l_type.

            when 'E'.

              "事務程式碼

              goodsmvt_code-gm_code = '02'.                 "mb31

              "明細

              w_goodsmvt_item-line_id = w_goodsmvt_item-line_id + 1.

              w_goodsmvt_item-move_type = '101'."移動型別

              w_goodsmvt_item-material = l_j_3abssi-matnr."大貨號

              w_goodsmvt_item-grid_value = l_j_3abssi-j_3asize."網格值

 

              w_goodsmvt_item-plant = g_plant."工廠

              w_goodsmvt_item-stge_loc = g_stge_loc."地點

 

              w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd."數量

 

              w_goodsmvt_item-orderid = l_j_3abssi-j_3absnr."訂單號

              w_goodsmvt_item-order_itno = l_j_3abssi-j_3ahbsp."行專案

              w_goodsmvt_item-sched_line_sku = l_j_3abssi-j_3aebsp."計劃行專案

 

              w_goodsmvt_item-mvt_ind = 'F'."生產訂單

              w_goodsmvt_item-stck_type = 'E'.

              w_goodsmvt_item-gr_rcpt  = pt_tran-user_id.

              append w_goodsmvt_item to goodsmvt_item.

            when 'F'.

              "事務程式碼

              goodsmvt_code-gm_code = '01'.                 "mb01

              "明細

              w_goodsmvt_item-line_id = w_goodsmvt_item-line_id + 1.

              l_line_id = w_goodsmvt_item-line_id.

              w_goodsmvt_item-move_type = '101'."移動型別

              w_goodsmvt_item-material = l_ekpo-matnr."大貨號

              w_goodsmvt_item-grid_value = l_eket-j_3asize."網格值

 

              w_goodsmvt_item-plant = g_plant."工廠

              w_goodsmvt_item-stge_loc = g_stge_loc."地點

              clear w_goodsmvt_item-vendor.

              w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd."數量

              clear w_goodsmvt_item-batch.

              w_goodsmvt_item-po_number = l_eket-ebeln."採購訂單

              w_goodsmvt_item-po_item = l_eket-ebelp."採購訂單行號

              w_goodsmvt_item-sched_line_sku = l_eket-etenr."計劃行

 

              clear:w_goodsmvt_item-parent_id,w_goodsmvt_item-line_depth.

              w_goodsmvt_item-mvt_ind = 'B'."採購訂單

              w_goodsmvt_item-stck_type = 'E'."庫存型別

              w_goodsmvt_item-gr_rcpt  = pt_tran-user_id.

              append w_goodsmvt_item to goodsmvt_item.

 

              refresh:lt_bom.

              call function 'ZLC_GET_BOM'

                EXPORTING

                  p_ebeln = l_eket-ebeln

                  p_ebelp = l_eket-ebelp

                  p_etenr = l_eket-etenr

                TABLES

                  pt_bom  = lt_bom.

              if lines( lt_bom ) > 0.

                loop at lt_bom.

                  w_goodsmvt_item-line_id = w_goodsmvt_item-line_id + 1.

                  w_goodsmvt_item-move_type = '543'."移動型別

                  w_goodsmvt_item-material = lt_bom-matnr."貨號

                  if lt_bom-grdv is not initial.

                    w_goodsmvt_item-grid_value = lt_bom-grdv.

                  else.

                    clear w_goodsmvt_item-grid_value.

                  endif.

                  w_goodsmvt_item-plant = '1000'."工廠

                  clear w_goodsmvt_item-stge_loc.

                  w_goodsmvt_item-vendor = lt_bom-vendor."供應商

                  w_goodsmvt_item-entry_qnt = lt_bom-entry_qnt * pt_tran-units_rcvd."數量

 

                  if lt_bom-charg is not initial.

                    w_goodsmvt_item-batch = lt_bom-charg.

                  else.

                    clear w_goodsmvt_item-batch.

                  endif.

                  w_goodsmvt_item-parent_id = l_line_id.

                  w_goodsmvt_item-line_depth = 1.

                  w_goodsmvt_item-mvt_ind = 'B'."採購訂單

                  w_goodsmvt_item-stck_type = 'E'."庫存型別

                  w_goodsmvt_item-gr_rcpt  = pt_tran-user_id.

                  append w_goodsmvt_item to goodsmvt_item.

                endloop.

              endif.

          endcase.

        else.

          append lines of lt_retmsg to lt_return.

        endif.

      endif.

    endloop.

 

    read table lt_return with key type = 'E'.

    if sy-subrc <> 0.

      if goodsmvt_item[] is not initial.

        "刪除日誌

        perform deletepixtranlog using l_tranpo.

        "執行操作

        call function '/AFS/BAPI_GOODSMVT_CREATE'

          EXPORTING

            goodsmvt_header   = goodsmvt_header

            goodsmvt_code     = goodsmvt_code

          IMPORTING

            goodsmvt_headret  = goodsmvt_headret

            materialdocument  = materialdocument

            matdocumentyear   = matdocumentyear

          TABLES

            afs_goodsmvt_item = goodsmvt_item

            return            = return.

        read table return with key type = 'E' into w_return.

        if sy-subrc <> 0.

          "提交

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

          l_flag = 'S'.

          l_matdoc = materialdocument.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          l_flag = 'F'.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc where tranpo = l_tranpo.

        if l_flag = 'F'.

          perform writepixtranlog tables return using l_tranpo.

        endif.

      endif.

    else.

      update zwttran set flag = 'F' where tranpo = l_tranpo.

      perform writepixtranlog tables lt_return using l_tranpo.

    endif.

  endloop.

  1. "recvfromord


 

二、移庫、凍結、解凍、內部領用、盤點、報廢、轉包、門店退貨等

form dopixtran tables pt_tran structure zwttran.

  data: l_tran_nbr like zwttran-tran_nbr,

        l_matdoc   like zwttran-matdoc,

        l_flag     like zwttran-flag,

        l_mara     like mara,

        lt_mard like table of mard with header line.

  data:goodsmvt_header  type   bapi2017_gm_head_01,

        goodsmvt_code    type   bapi2017_gm_code,

        goodsmvt_headret type   bapi2017_gm_head_ret,

        materialdocument type   bapi2017_gm_head_ret-mat_doc,

        matdocumentyear  type   bapi2017_gm_head_ret-doc_year,

        goodsmvt_item    type standard table of   bapi2017_gm_item_create,

        return           type standard table of   bapiret2,

        w_goodsmvt_item  type                     bapi2017_gm_item_create,

        w_return         type bapiret2.

  data:lt_retmsg like table of bapiret2 with header line.

  refresh:t_tran_nbr.

  loop at pt_tran.

    clear:t_tran_nbr.

    t_tran_nbr-tran_nbr = pt_tran-tran_nbr.

    collect t_tran_nbr.

  endloop.

  sort t_tran_nbr by tran_nbr.

 

  loop at t_tran_nbr.

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 3,4倉移庫到物流中心倉庫

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.

    refresh:goodsmvt_item ,return.

    "事物碼

    goodsmvt_code-gm_code = '04'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where tran_type = '603' and tran_code = '02' and proc_stat_code = '10' and units_rcvd > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-ref_field_6 = 'Y'.

        l_tran_nbr = pt_tran-tran_nbr.

        w_goodsmvt_item-move_type = '311'."移庫

        w_goodsmvt_item-plant = g_plant.

        w_goodsmvt_item-stge_loc = pt_tran-ref_field_7.

        w_goodsmvt_item-material = pt_tran-matnr.

        w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd.

        w_goodsmvt_item-move_plant = g_plant.

        w_goodsmvt_item-move_stloc = g_stge_loc.

        append w_goodsmvt_item to goodsmvt_item.

      endif.

    endloop.

    "執行操作

    if goodsmvt_item[] is not initial.

      l_flag = 'F'.

      call function 'BAPI_GOODSMVT_CREATE'

        EXPORTING

          goodsmvt_header  = goodsmvt_header

          goodsmvt_code    = goodsmvt_code

        IMPORTING

          goodsmvt_headret = goodsmvt_headret

          materialdocument = materialdocument

          matdocumentyear  = matdocumentyear

        TABLES

          goodsmvt_item    = goodsmvt_item

          return           = return.

      read table return with key type = 'E' into w_return.

      if sy-subrc <> 0.

        call function 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            wait = 'X'.

        if materialdocument is not initial.

          l_flag = 'S'.

          l_matdoc = materialdocument.

        endif.

      else.

        call function 'BAPI_TRANSACTION_ROLLBACK'.

        perform writepixtranlog tables return using l_tran_nbr.

      endif.

      update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 移入

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 從物流中心倉庫移至3,4

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.

    refresh:goodsmvt_item ,return.

    "事物碼

    goodsmvt_code-gm_code = '04'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-rsn_code = 'YC'.

        l_tran_nbr = pt_tran-tran_nbr.

        w_goodsmvt_item-move_type = '311'."移庫

        w_goodsmvt_item-plant = g_plant.

        w_goodsmvt_item-stge_loc =  g_stge_loc.

        w_goodsmvt_item-material = pt_tran-matnr.

        w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.

        w_goodsmvt_item-move_plant = g_plant.

        w_goodsmvt_item-move_stloc = pt_tran-ref_field_5.

        append w_goodsmvt_item to goodsmvt_item.

      endif.

    endloop.

    "執行操作

    if goodsmvt_item[] is not initial.

      l_flag = 'F'.

      call function 'BAPI_GOODSMVT_CREATE'

        EXPORTING

          goodsmvt_header  = goodsmvt_header

          goodsmvt_code    = goodsmvt_code

        IMPORTING

          goodsmvt_headret = goodsmvt_headret

          materialdocument = materialdocument

          matdocumentyear  = matdocumentyear

        TABLES

          goodsmvt_item    = goodsmvt_item

          return           = return.

      read table return with key type = 'E' into w_return.

      if sy-subrc <> 0.

        call function 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            wait = 'X'.

        if materialdocument is not initial.

          l_flag = 'S'.

          l_matdoc = materialdocument.

        endif.

      else.

        call function 'BAPI_TRANSACTION_ROLLBACK'.

        perform writepixtranlog tables return using l_tran_nbr.

      endif.

      update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 移出

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& begin 凍結庫存

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.

    refresh:goodsmvt_item ,return.

    "事物碼

    goodsmvt_code-gm_code = '04'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where tran_type = '300' and tran_code = '01' and actn_code = '05' and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      l_tran_nbr = pt_tran-tran_nbr.

      w_goodsmvt_item-move_type = '344'."凍結庫存

      w_goodsmvt_item-plant = g_plant.

      w_goodsmvt_item-stge_loc = g_stge_loc.

      w_goodsmvt_item-material = pt_tran-matnr.

      w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.

      append w_goodsmvt_item to goodsmvt_item.

    endloop.

    "執行操作

    if goodsmvt_item[] is not initial.

      l_flag = 'F'.

      call function 'BAPI_GOODSMVT_CREATE'

        EXPORTING

          goodsmvt_header  = goodsmvt_header

          goodsmvt_code    = goodsmvt_code

        IMPORTING

          goodsmvt_headret = goodsmvt_headret

          materialdocument = materialdocument

          matdocumentyear  = matdocumentyear

        TABLES

          goodsmvt_item    = goodsmvt_item

          return           = return.

      read table return with key type = 'E' into w_return.

      if sy-subrc <> 0.

        call function 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            wait = 'X'.

        if materialdocument is not initial.

          l_flag = 'S'.

          l_matdoc = materialdocument.

        endif.

      else.

        call function 'BAPI_TRANSACTION_ROLLBACK'.

        perform writepixtranlog tables return using l_tran_nbr.

      endif.

      update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 凍結庫存

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& begin 解凍庫存

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.

    refresh:goodsmvt_item ,return.

    "事物碼

    goodsmvt_code-gm_code = '04'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where tran_type = '606' and tran_code = '02' and actn_code = '06' and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      l_tran_nbr = pt_tran-tran_nbr.

      w_goodsmvt_item-move_type = '343'."解凍庫存

      w_goodsmvt_item-plant = g_plant.

      w_goodsmvt_item-stge_loc = g_stge_loc.

      w_goodsmvt_item-material = pt_tran-matnr.

      w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.

      append w_goodsmvt_item to goodsmvt_item.

    endloop.

    "執行操作

    if goodsmvt_item[] is not initial.

      l_flag = 'F'.

      call function 'BAPI_GOODSMVT_CREATE'

        EXPORTING

          goodsmvt_header  = goodsmvt_header

          goodsmvt_code    = goodsmvt_code

        IMPORTING

          goodsmvt_headret = goodsmvt_headret

          materialdocument = materialdocument

          matdocumentyear  = matdocumentyear

        TABLES

          goodsmvt_item    = goodsmvt_item

          return           = return.

      read table return with key type = 'E' into w_return.

      if sy-subrc <> 0.

        call function 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            wait = 'X'.

        if materialdocument is not initial.

          l_flag = 'S'.

          l_matdoc = materialdocument.

        endif.

      else.

        call function 'BAPI_TRANSACTION_ROLLBACK'.

        perform writepixtranlog tables return using l_tran_nbr.

      endif.

      update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 解凍庫存

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 內部領用

    "&--------------------------------------------------------------------------------------------------------------&

    data:l_kostl like csks-kostl,"成本中心

         l_prctr like cepc-prctr,"利潤中心

         l_csks like csks,"成本中心結構

         l_cepc like cepc."利潤中心結構

    data:lt_tpmsg like table of bapiret2 with header line.

    clear:l_kostl,l_prctr,l_csks,l_cepc.

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.

    refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.

    "事物碼

    goodsmvt_code-gm_code = '03'.                           "MB1A

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-rsn_code = 'NL'.

        clear:l_kostl,l_csks,lt_tpmsg.

        refresh:lt_tpmsg.

        l_tran_nbr = pt_tran-tran_nbr.

        l_kostl = pt_tran-ref_field_5.

        call function 'ZLC_GET_CSKS'

          EXPORTING

            P_KOKRS   = '1000'

            P_KOSTL   = l_kostl

          IMPORTING

            p_csks    = l_csks

          TABLES

            pt_retmsg = lt_tpmsg.

        read table lt_tpmsg with key type = 'E'.

        if sy-subrc <> 0.

          w_goodsmvt_item-move_type = '201'."內部領用

          w_goodsmvt_item-plant = g_plant.

          w_goodsmvt_item-stge_loc = g_stge_loc.

          w_goodsmvt_item-material = pt_tran-matnr.

          w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.

          w_goodsmvt_item-costcenter = l_kostl."成本中心

          w_goodsmvt_item-profit_ctr = l_csks-prctr."利潤中心

          append w_goodsmvt_item to goodsmvt_item.

        else.

          append lines of lt_tpmsg to lt_retmsg.

        endif.

      endif.

    endloop.

    "執行操作

    read table lt_retmsg with key type = 'E'.

    if sy-subrc <> 0.

      if goodsmvt_item[] is not initial.

        l_flag = 'F'.

        call function 'BAPI_GOODSMVT_CREATE'

          EXPORTING

            goodsmvt_header  = goodsmvt_header

            goodsmvt_code    = goodsmvt_code

          IMPORTING

            goodsmvt_headret = goodsmvt_headret

            materialdocument = materialdocument

            matdocumentyear  = matdocumentyear

          TABLES

            goodsmvt_item    = goodsmvt_item

            return           = return.

        read table return with key type = 'E' into w_return.

        if sy-subrc <> 0.

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

          if materialdocument is not initial.

            l_flag = 'S'.

            l_matdoc = materialdocument.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          perform writepixtranlog tables return using l_tran_nbr.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

      endif.

    else.

      update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.

      perform writepixtranlog tables lt_retmsg using l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 內部領用

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 內部領用退回

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_kostl,l_prctr,l_csks,l_cepc.

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.

    refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.

    "事物碼

    goodsmvt_code-gm_code = '03'.                           "MB1A

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where tran_type = '603' and tran_code = '02' and proc_stat_code = '10' and units_rcvd > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-ref_field_6 = 'N'.

        clear:l_kostl,l_csks,lt_tpmsg.

        refresh:lt_tpmsg.

        l_tran_nbr = pt_tran-tran_nbr.

        l_kostl = pt_tran-ref_field_5.

        call function 'ZLC_GET_CSKS'

          EXPORTING

            P_KOKRS   = '1000'

            P_KOSTL   = l_kostl

          IMPORTING

            p_csks    = l_csks

          TABLES

            pt_retmsg = lt_tpmsg.

        read table lt_tpmsg with key type = 'E'.

        if sy-subrc <> 0.

          w_goodsmvt_item-move_type = '202'."內部領用沖銷

          w_goodsmvt_item-plant = g_plant.

          w_goodsmvt_item-stge_loc = g_stge_loc.

          w_goodsmvt_item-material = pt_tran-matnr.

          w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd.

          w_goodsmvt_item-costcenter = l_kostl."成本中心

          w_goodsmvt_item-profit_ctr = l_csks-prctr."利潤中心

          append w_goodsmvt_item to goodsmvt_item.

        else.

          append lines of lt_tpmsg to lt_retmsg.

        endif.

      endif.

    endloop.

    "執行操作

    read table lt_retmsg with key type = 'E'.

    if sy-subrc <> 0.

      if goodsmvt_item[] is not initial.

        l_flag = 'F'.

        call function 'BAPI_GOODSMVT_CREATE'

          EXPORTING

            goodsmvt_header  = goodsmvt_header

            goodsmvt_code    = goodsmvt_code

          IMPORTING

            goodsmvt_headret = goodsmvt_headret

            materialdocument = materialdocument

            matdocumentyear  = matdocumentyear

          TABLES

            goodsmvt_item    = goodsmvt_item

            return           = return.

        read table return with key type = 'E' into w_return.

        if sy-subrc <> 0.

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

          if materialdocument is not initial.

            l_flag = 'S'.

            l_matdoc = materialdocument.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          perform writepixtranlog tables return using l_tran_nbr.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

      endif.

    else.

      update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.

      perform writepixtranlog tables lt_retmsg using l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 內部領用退回

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 報廢

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_kostl,l_prctr,l_csks,l_cepc.

    clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.

    refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.

    "事物碼

    goodsmvt_code-gm_code = '03'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-rsn_code = 'BF'.

        clear:l_prctr,l_cepc.

        refresh:lt_tpmsg.

        l_tran_nbr = pt_tran-tran_nbr.

        l_prctr = pt_tran-ref_field_5.

        call function 'ZLC_GET_CEPC'

          EXPORTING

            p_kokrs   = '1000'

            p_prctr   = l_prctr

          IMPORTING

            p_cepc    = l_cepc

          TABLES

            pt_retmsg = lt_tpmsg.

        read table lt_tpmsg with key type = 'E'.

        if sy-subrc <> 0.

          w_goodsmvt_item-move_type = '551'."報廢

          w_goodsmvt_item-plant = g_plant.

          w_goodsmvt_item-stge_loc = g_stge_loc.

          w_goodsmvt_item-material = pt_tran-matnr.

          w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.

          w_goodsmvt_item-profit_ctr = l_prctr."利潤中心

          append w_goodsmvt_item to goodsmvt_item.

        else.

          append lines of lt_tpmsg to lt_retmsg.

        endif.

      endif.

    endloop.

    "執行操作

    read table lt_retmsg with key type = 'E'.

    if sy-subrc <> 0.

      if goodsmvt_item[] is not initial.

        l_flag = 'F'.

        call function 'BAPI_GOODSMVT_CREATE'

          EXPORTING

            goodsmvt_header  = goodsmvt_header

            goodsmvt_code    = goodsmvt_code

          IMPORTING

            goodsmvt_headret = goodsmvt_headret

            materialdocument = materialdocument

            matdocumentyear  = matdocumentyear

          TABLES

            goodsmvt_item    = goodsmvt_item

            return           = return.

        read table return with key type = 'E' into w_return.

        if sy-subrc <> 0.

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

          if materialdocument is not initial.

            l_flag = 'S'.

            l_matdoc = materialdocument.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          perform writepixtranlog tables return using l_tran_nbr.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

      endif.

    else.

      update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.

      perform writepixtranlog tables lt_retmsg using l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 報廢

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 轉包

    "&--------------------------------------------------------------------------------------------------------------&

    data:l_lifnr like lfa1-lifnr.

    clear:l_lifnr,l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.

    refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.

    "事物碼

    goodsmvt_code-gm_code = '04'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-rsn_code = 'ZB'.

        clear:l_lifnr.

        refresh:lt_tpmsg.

        l_tran_nbr = pt_tran-tran_nbr.

        l_lifnr = pt_tran-ref_field_5.

        call function 'CONVERSION_EXIT_ALPHA_INPUT'

          EXPORTING

            input  = l_lifnr

          IMPORTING

            output = l_lifnr.

        select count(*) from lfa1 where lifnr = l_lifnr.

        if sy-subrc <> 0.

          lt_tpmsg-type = 'E'.

          concatenate '供應商' l_lifnr '不存在!' into lt_tpmsg-message.

          append lt_tpmsg.

        endif.

        read table lt_tpmsg with key type = 'E'.

        if sy-subrc <> 0.

          w_goodsmvt_item-move_type = '541'."轉包

          w_goodsmvt_item-plant = g_plant.

          w_goodsmvt_item-stge_loc = g_stge_loc.

          w_goodsmvt_item-material = pt_tran-matnr.

          w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.

          w_goodsmvt_item-vendor = l_lifnr."供應商

          append w_goodsmvt_item to goodsmvt_item.

        else.

          append lines of lt_tpmsg to lt_retmsg.

        endif.

      endif.

    endloop.

    "執行操作

    read table lt_retmsg with key type = 'E'.

    if sy-subrc <> 0.

      if goodsmvt_item[] is not initial.

        l_flag = 'F'.

        call function 'BAPI_GOODSMVT_CREATE'

          EXPORTING

            goodsmvt_header  = goodsmvt_header

            goodsmvt_code    = goodsmvt_code

          IMPORTING

            goodsmvt_headret = goodsmvt_headret

            materialdocument = materialdocument

            matdocumentyear  = matdocumentyear

          TABLES

            goodsmvt_item    = goodsmvt_item

            return           = return.

        read table return with key type = 'E' into w_return.

        if sy-subrc <> 0.

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

          if materialdocument is not initial.

            l_flag = 'S'.

            l_matdoc = materialdocument.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          perform writepixtranlog tables return using l_tran_nbr.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

      endif.

    else.

      update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.

      perform writepixtranlog tables lt_retmsg using l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 轉包

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 轉包沖銷

    "&--------------------------------------------------------------------------------------------------------------&

    clear:l_lifnr,l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.

    refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.

    "事物碼

    goodsmvt_code-gm_code = '04'.

    "

    goodsmvt_header-pstng_date = sy-datum.

    goodsmvt_header-doc_date = sy-datum.

    "明細

    loop at pt_tran where tran_type = '603' and tran_code = '02' and proc_stat_code = '10' and units_rcvd > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      if pt_tran-ref_field_6 = 'T'.

        clear:l_lifnr.

        refresh:lt_tpmsg.

        l_tran_nbr = pt_tran-tran_nbr.

        l_lifnr = pt_tran-ref_field_7.

        call function 'CONVERSION_EXIT_ALPHA_INPUT'

          EXPORTING

            input  = l_lifnr

          IMPORTING

            output = l_lifnr.

        select count(*) from lfa1 where lifnr = l_lifnr.

        if sy-subrc <> 0.

          lt_tpmsg-type = 'E'.

          concatenate '供應商' l_lifnr '不存在!' into lt_tpmsg-message.

          append lt_tpmsg.

        endif.

        read table lt_tpmsg with key type = 'E'.

        if sy-subrc <> 0.

          w_goodsmvt_item-move_type = '542'."轉包沖銷

          w_goodsmvt_item-plant = g_plant.

          w_goodsmvt_item-stge_loc = g_stge_loc.

          w_goodsmvt_item-material = pt_tran-matnr.

          w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd.

          w_goodsmvt_item-vendor = l_lifnr."供應商

          append w_goodsmvt_item to goodsmvt_item.

        else.

          append lines of lt_tpmsg to lt_retmsg.

        endif.

      endif.

    endloop.

    "執行操作

    read table lt_retmsg with key type = 'E'.

    if sy-subrc <> 0.

      if goodsmvt_item[] is not initial.

        l_flag = 'F'.

        call function 'BAPI_GOODSMVT_CREATE'

          EXPORTING

            goodsmvt_header  = goodsmvt_header

            goodsmvt_code    = goodsmvt_code

          IMPORTING

            goodsmvt_headret = goodsmvt_headret

            materialdocument = materialdocument

            matdocumentyear  = matdocumentyear

          TABLES

            goodsmvt_item    = goodsmvt_item

            return           = return.

        read table return with key type = 'E' into w_return.

        if sy-subrc <> 0.

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

          if materialdocument is not initial.

            l_flag = 'S'.

            l_matdoc = materialdocument.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          perform writepixtranlog tables return using l_tran_nbr.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.

      endif.

    else.

      update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.

      perform writepixtranlog tables lt_retmsg using l_tran_nbr.

    endif.

    "&--------------------------------------------------------------------------------------------------------------&

    "& END 轉包沖銷

    "&--------------------------------------------------------------------------------------------------------------&

 

    "&--------------------------------------------------------------------------------------------------------------&

    "& BEGIN 盤點

    "&--------------------------------------------------------------------------------------------------------------&

    data:l_physinv_head type bapi_physinv_create_head,

         lt_physinv_item   like table of bapi_physinv_create_items with header line.

    data:it_count_item like table of bapi_physinv_count_items with header line,

         lt_temp like table of bapi_physinv_count_items with header line,

         l_invdoc type iblnr,

         l_nblnr(10) type c,

         l_count type i.

    "&--------------------------------------------盤點-------------------------------------------------------------&

    "初始化

    clear:l_tran_nbr,l_flag,l_matdoc,l_physinv_head,lt_physinv_item, lt_retmsg,it_count_item,lt_temp,l_invdoc,l_nblnr,l_count.

    refresh:lt_physinv_item, lt_retmsg,lt_temp,it_count_item.

    "盤點單頭

    l_physinv_head-plant = g_plant.

    l_physinv_head-stge_loc = g_stge_loc.

    l_physinv_head-doc_date = sy-datum.

    l_physinv_head-plan_date = sy-datum.

 

    loop at pt_tran where  ( ( tran_type = '300' and ( tran_code = '04' or tran_code = '01') and actn_code = '14' )

      or ( tran_type = '300' and tran_code = '04' and actn_code = '' ) ) and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.

      clear:lt_temp.

      l_tran_nbr = pt_tran-tran_nbr.

      "盤點單計數

      lt_temp-material = pt_tran-matnr.

      case pt_tran-invn_adjmt_type.

        when 'A'.

          lt_temp-entry_qnt = pt_tran-invn_adjmt_qty.

        when 'S'.

          lt_temp-entry_qnt = - pt_tran-invn_adjmt_qty.

      endcase.

      collect lt_temp.

    endloop.

 

    loop at lt_temp.

      add 1 to l_count.

      "初始化

      clear:lt_mard.

      refresh:lt_mard.

      "獲取當前庫存

      call function 'ZLC_GET_STOCK'

        EXPORTING

          p_matnr = lt_temp-material

          p_werks = g_plant

          p_lgort = g_stge_loc

        TABLES

          pt_mard = lt_mard.

      read table lt_mard index 1.

      "盤點處理

      if sy-subrc = 0.

        "盤點單物料

        lt_physinv_item-material = lt_temp-material.

        append lt_physinv_item.

        "盤點單計數

        it_count_item-item = l_count.

        it_count_item-material = lt_temp-material.

        it_count_item-entry_qnt = lt_mard-labst + lt_temp-entry_qnt.

        clear:l_mara.

        call function 'ZLC_GET_MARA'

          EXPORTING

            p_matnr = lt_temp-material

          IMPORTING

            p_mara  = l_mara.

        if l_mara is not initial.

          it_count_item-entry_uom = l_mara-meins.

        endif.

        append it_count_item.

      else.

        clear:lt_retmsg.

        lt_retmsg-type = 'E'.

        concatenate '商品' lt_temp-material ',' g_plant ',' g_stge_loc  '不存在!' into lt_retmsg-message.

        append lt_retmsg.

      endif.

    endloop.

 

    read table lt_retmsg with key type = 'E'.

    if sy-subrc <> 0.

      if it_count_item[] is not initial.

        l_flag = 'F'.

        clear:lt_retmsg.

        refresh:lt_retmsg.

        call function 'BAPI_MATPHYSINV_CREATE_MULT'"建立盤點憑證

        exporting

          head   = l_physinv_head

        tables

          items  = lt_physinv_item

          return = lt_retmsg.

 

        loop at lt_retmsg.

          if lt_retmsg-type = 'S' and lt_retmsg-id = 'M7'

          and lt_retmsg-number = '710'.

            l_nblnr = lt_retmsg-message_v1.

            shift l_nblnr right deleting trailing space.

            overlay l_nblnr with '0000000000'.

            l_invdoc = l_nblnr.

          endif.

        endloop.

 

        read table lt_retmsg with key type = 'E'.

        if sy-subrc <> 0.

 

          call function 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              wait = 'X'.

 

          clear:lt_retmsg.

          refresh:lt_retmsg.

          call function 'BAPI_MATPHYSINV_COUNT'"輸入盤點數量

          exporting

            physinventory = l_invdoc

            fiscalyear    = sy-datum+0(4)

            count_date    = sy-datum

          tables

            items         = it_count_item

            return        = lt_retmsg.

 

          read table lt_retmsg with key type = 'E'.

          if sy-subrc <> 0.

            call function 'BAPI_TRANSACTION_COMMIT'

              EXPORTING

                wait = 'X'.

 

            clear:lt_retmsg.

            refresh:lt_retmsg.

            call function 'BAPI_MATPHYSINV_POSTDIFF'"處理差異

            exporting

              physinventory = l_invdoc

              fiscalyear    = sy-datum+0(4)

              pstng_date    = sy-datum

            tables

              return        = lt_retmsg.

 

            read table lt_retmsg with key type = 'E'.

            if sy-subrc <> 0.

              call function 'BAPI_TRANSACTION_COMMIT'

                EXPORTING

                  wait = 'X'.

              loop at lt_retmsg.

                if lt_retmsg-type = 'S' and lt_retmsg-id = 'M7'

                and lt_retmsg-number = '716'.

                  l_nblnr = lt_retmsg-message_v2.

                  shift l_nblnr right deleting trailing space.

                  overlay l_nblnr with '0000000000'.

                  l_matdoc = l_nblnr.

                endif.

              endloop.

              l_flag = 'S'.

            else.

              call function 'BAPI_TRANSACTION_ROLLBACK'.

            endif.

          else.

            call function 'BAPI_TRANSACTION_ROLLBACK'.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

        endif.

        update zwttran set flag = l_flag matdoc = l_matdoc iblnr = l_invdoc where tran_nbr = l_tran_nbr.

        if l_flag = 'F'.

          perform writepixtranlog tables lt_retmsg using l_tran_nbr.

        endif.

      endif.

    else.

      update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.

      perform writepixtranlog tables lt_retmsg using l_tran_nbr.

    endif.

    "  &--------------------------------------------------------------------------------------------------------------&

    "  & end 報損、報溢、盤點

    "  &--------------------------------------------------------------------------------------------------------------&

  endloop.

 

相關文章