BAPI_GOODSMVT_CREATE移库操作(WMS TO SAP)

移动代码

01 -- MB01
02 -- MB31
03 -- MB1A
04 -- MB1B
05 -- MB1C
06 -- MB11
07 -- MB04

一、收货(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.
endform.                    "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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 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.

你可能感兴趣的:(ABAP)