SAP: ZMM031 Consolidation inventory report

44 篇文章 0 订阅

*&---------------------------------------------------------------------*
*& Progarm      :  ZMMR031                        Author : Jimmy Wong
*& Created      :  08 Jan 2013                    App    : MM
*& Title        :  Consolidation inventory report
*& Description  :  Consolidation inventory report
*&---------------------------------------------------------------------*
*&  Version       Author      Date        description
*&                Jimmy       08 Jan 2013 the first version
*&  the last update time  2013.03.20 10:00
*&---------------------------------------------------------------------*

report  zmmr031 no standard page heading.

tables:mara,marc.
include zmmr031f01.

*&---------------------------------------------------------------------*
* parameter & select-options
*&---------------------------------------------------------------------*
selection-screen begin of block with frame title text-010.
select-options: s_werks  for marc-werks default '8101'.  "Plant
select-options: s_matnr  for mara-matnr.  "material code"
select-options: s_mtart  for mara-mtart.   "Material Type
select-options: s_matnr1  for mara-matnr.   "Componenet
select-options: s_mtart1  for mara-mtart no intervals .   "Material Type
parameters:p_bom radiobutton group gr1  default 'X',
           p_nobom radiobutton group gr1.
selection-screen end of block 1.

initialization.
  s_mtart1-sign 'I'.
  s_mtart1-option 'EQ'.
  s_mtart1-low 'ZVFG'.
  append s_mtart1.

  s_mtart1-sign 'I'.
  s_mtart1-option 'EQ'.
  s_mtart1-low 'ZUFG'.
  append s_mtart1.

  s_mtart1-sign 'I'.
  s_mtart1-option 'EQ'.
  s_mtart1-low 'ZPCB'.
  append s_mtart1.

  s_mtart1-sign 'I'.
  s_mtart1-option 'EQ'.
  s_mtart1-low 'ZIAG'.
  append s_mtart1.
*&---------------------------------------------------------------------*
* start of process
*&---------------------------------------------------------------------*
start-of-selection.
  perform get_onhand_qty.
  if it_mchb[] is initial  .
    message i003(zmm).
    exit.
  endif.
  r_matnr[] s_matnr1[].
  perform get_bom_data.
  perform process_nobom.
  perform check_similar_bom.
  if p_bom 'X'.
    perform get_all_zraw_data.
    perform get_other_data.
    perform process_data.
    perform display_data.
  elseif p_nobom 'X'.
    "perform process_nobom.
    if it_nobom[] is initial  .
      message i003(zmm).
      exit.
    endif.
    perform display_nobom_data.
  endif.


*&---------------------------------------------------------------------*
*&      Form  GET_ONHAND_QTY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_onhand_qty .
  call function 'SAPGUI_PROGRESS_INDICATOR'
    exporting
      text 'Getting Data........'.
  " main stock
  select  a~matnr a~werks  a~clabs b~mtart a~lgort
      into corresponding fields of table it_mchb1
    from mchb as a
      inner join mara as on a~matnr b~matnr
    where a~matnr in s_matnr
        and a~werks in s_werks
        and a~clabs > 0
        and b~mtart in s_mtart.
  " project stock - Q
  select a~matnr a~werks  a~prlab  as clabs b~mtart a~lgort
      appending corresponding fields of table it_mchb1
    from mspr as a
      inner join mara as on a~matnr b~matnr
    where a~matnr in s_matnr
        and a~werks in s_werks
        and a~prlab > 0
        and b~mtart in s_mtart.
  " project stock - W
  select a~matnr a~werks a~kulab as clabs b~mtart
      appending corresponding fields of table it_mchb1
    from msku as a
      inner join mara as on a~matnr b~matnr
      where a~matnr in s_matnr
        and a~werks in s_werks
        and a~kulab > 0
        and b~mtart in s_mtart.
  " project stock - K
  select  a~matnr a~werks   a~slabs  as clabs b~mtart a~lgort
      appending corresponding fields of table it_mchb1
    from mkol as a
      inner join mara as on a~matnr b~matnr
    where a~matnr in s_matnr
        and a~werks in s_werks
        and a~slabs > 0
        and b~mtart in s_mtart.
  " project stock - O
  select a~matnr a~werks  a~lblab as clabs b~mtart
      appending corresponding fields of table it_mchb1
    from mslb as a
      inner join mara as on a~matnr b~matnr
      where a~matnr in s_matnr
        and a~werks in s_werks
        and a~lblab > 0
        and b~mtart in s_mtart.
  "Storage Location Data for Material
*  select a~matnr a~werks  a~labst as clabs  a~lgort b~mtart a~lgort
*      appending corresponding fields of table it_mchb1
*    from mard as a
*      inner join mara as b on a~matnr = b~matnr
*      where a~matnr in s_matnr
*        and a~werks in s_werks
*        and a~labst > 0
*        and b~mtart in s_mtart.
  select a~matnr a~werks    a~lgort  b~mtart suma~labst )  as clabs
      appending corresponding fields of table it_mard
    from mard as a
      inner join mara as on a~matnr b~matnr
      inner join marc as on a~matnr c~matnr and a~werks c~werks  " delete duplication record
      where a~matnr in s_matnr
        and a~werks in s_werks
        and a~labst > 0
        and b~mtart in s_mtart
        and c~xchar space   " delete duplication record
      group by a~matnr a~werks  a~lgort b~mtart.


  loop at it_mchb1.
    move-corresponding it_mchb1 to it_mchb.
    if it_mchb-lgort is not initial.
      if  it_mchb-lgort+0(1'Z'.
        clear:it_mchb.
        continue.
      endif.
    endif.
    clear:it_mchb-lgort.
    it_mchb-werks_s it_mchb-werks.
    if it_mchb-werks+2(1'9'.
      it_mchb-werks+2(1'0'.
    endif.
    collect: it_mchb.
    clear:it_mchb.
  endloop.
  "check duplication stock
  if it_mard[] is not initial.
    clear :it_mchb1,it_mchb1[].
    it_mchb1[] it_mchb[].
    sort it_mchb1 by matnr werks_s lgort clabs.
    loop at it_mard.
      read table it_mchb1 with  key  matnr it_mard-matnr
                                    werks_s it_mard-werks
                                    lgort it_mard-lgort
                                    clabs it_mard-clabs.
      if sy-subrc ne 0.
        move-corresponding it_mard to it_mchb.
        it_mchb-werks_s it_mchb-werks.
        if it_mchb-werks+2(1'9'.
          it_mchb-werks+2(1'0'.
        endif.
        append it_mchb.
      endif.
    endloop.
  endif.
  free :it_mchb1.
endform.                    " GET_ONHAND_QTY
*&---------------------------------------------------------------------*
*&      Form  PROCESS_NOBOM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form process_nobom .
  loop at it_mchb.
    check it_mchb-mtart in s_mtart1.
    move-corresponding it_mchb to it_nobom.
    read table it_bom with  key werks_h it_mchb-werks matnr_h it_mchb-matnr .
    if sy-subrc ne 0.
      clear: it_nobom-lgort.
      collect it_nobom.
      clear :it_nobom.
    endif.
  endloop.
endform.                    " PROCESS_NOBOM
*&---------------------------------------------------------------------*
*&      Form  CHECK_SIMILAR_BOM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form check_similar_bom .
  data: lv_matnr like mara-matnr,
        wa_matnr type length 9.
  loop at it_nobom.
    read table it_bom with  key werks_h it_nobom-werks matnr_h+0(8it_nobom-matnr+0(8) .
    if sy-subrc eq 0.
      it_replace-werks it_nobom-werks .
      it_replace-matnr_o it_nobom-matnr .
      it_replace-matnr_n it_bom-matnr_h .
      collect it_replace.
      delete it_nobom.
    else.
      wa_matnr it_nobom-matnr+0(8) .
      concatenate  wa_matnr '%' into wa_matnr.
      select single matnr_h into lv_matnr
        from zctp001
        where werks_h it_nobom-werks
        and   matnr_h like wa_matnr.
      if lv_matnr is not initial.
        select werks_h matnr_h matnr_m  matnr_l  menge_h  mtart ewahr mmein meins "
          appending corresponding fields of table it_bom
          from zctp001
          where werks_h it_mchb-werks
            and matnr_h lv_matnr
            and matnr_l in r_matnr
            and mtart in ('ZRAW','ZPCK','ZSUB','ZCCM').
        if sy-subrc eq 0.
          it_replace-werks it_nobom-werks .
          it_replace-matnr_o it_nobom-matnr .
          it_replace-matnr_n lv_matnr .
          collect it_replace.
          delete it_nobom.
        endif.
      endif.
    endif.
    clear: wa_matnr,lv_matnr,it_replace.
  endloop.
endform.                    " CHECK_SIMILAR_BOM
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_NOBOM_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form display_nobom_data .
  refresh: fc_hier.

  alv_field  'WERKS' 'Plant' '' '5' 'L' '' '' ''.
  alv_field  'MATNR' 'Material' '' '18' 'L' ''  'MARA' 'MATNR'.
  alv_field  'CLABS' '' 'Onhand qty' '12' 'R' 'X'  '' ''.

  sort it_nobom by matnr werks.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      it_fieldcat        fc_hier[]
      i_save             'A'
      i_callback_program sy-repid
    tables
      t_outtab           it_nobom[]
    exceptions
      program_error      1
      others             2.
  if sy-subrc ne 0.
    write: sy-msgv1,sy-msgv2, sy-msgv3, sy-msgv4.
  endif.
endform.                    " DISPLAY_NOBOM_DATA

 

*&---------------------------------------------------------------------*
*&  Include           ZMMR031F01
*&---------------------------------------------------------------------*
data:begin of itab occurs 0,
      matnr   like mara-matnr,  "material
      werks   like marc-werks,  "Plant
      clabs   like mchb-clabs,  "
      maktx   like makt-maktx,
      stawn   like marc-stawn,
      text1   like t604t-text1,
      text2   like t604t-text2,
      text7   like t604t-text7,
      meins   like mara-meins,
      ntgew   like mara-ntgew,
      gewei   like mara-gewei,
      tlgew   like mara-ntgew,
     end of itab.
data:begin of it_mchb occurs 0,
      matnr   like mara-matnr,  "material
      werks   like marc-werks,  "Plant
      clabs   like mchb-clabs,  "
      mtart   like mara-mtart,
      werks_s   like marc-werks,  "Plant
      lgort like mchb-lgort,
     end of it_mchb.
data:it_mchb1 like it_mchb occurs 0  with header line.
data:it_mard like it_mchb occurs 0  with header line.
data:it_matnr like it_mchb occurs 0  with header line.
data:begin of it_bom occurs 0,
    werks_h like zctp001-werks_h,
    matnr_h like zctp001-matnr_h,
    matnr_m like zctp001-matnr_m,
    matnr_l like zctp001-matnr_l,
    menge_h like zctp001-menge_h,
    mtart   like zctp001-mtart,
    ewahr   like zctp001-ewahr,
    meins   like zctp001-meins,
    mmein   like zctp001-meins,
    end of it_bom.
data:it_mara like mara occurs 0   with header line.
data:it_marc like marc occurs 0   with header line.
data:it_marc_s like marc occurs 0   with header line.
data:it_t604t like t604t occurs 0   with header line.
data:begin of it_makt occurs 0,
    matnr like makt-matnr,
    maktx like makt-maktx,
    end of it_makt.
data:begin of it_zlgp occurs 0,
    matnr like mara-matnr,
    end of it_zlgp.
ranges : r_matnr for mara-matnr.
data: it_nobom like it_mchb occurs with header line.
data: begin of it_replace  occurs 0,
      werks   like marc-werks,  "Plant
      matnr_o   like mara-matnr,  "material
      matnr_n   like mara-matnr,  "material
      end of it_replace.
type-pools: slis.
data: fc_hier type slis_t_fieldcat_alv,
      wa_hier type slis_fieldcat_alv .
define alv_field.
  clear:wa_hier.
  wa_hier-fieldname &1.
  wa_hier-seltext_m &2.
  wa_hier-seltext_l &3.
  wa_hier-outputlen &4.
  wa_hier-just &5.
  wa_hier-do_sum &6.
  wa_hier-ref_tabname &7 .
  wa_hier-ref_fieldname &8 .
  append wa_hier to fc_hier.
end-of-definition.

*&---------------------------------------------------------------------*
*&      Form  GET_BOM_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_bom_data .
  call function 'SAPGUI_PROGRESS_INDICATOR'
    exporting
      text 'Getting BOM Data........'.
  sort it_mchb by matnr werks.

  select werks_h matnr_h matnr_m  matnr_l  menge_h  mtart ewahr mmein meins "
    into corresponding fields of table it_bom
    from zctp001
    for all entries in it_mchb
    where werks_h it_mchb-werks
      and matnr_h it_mchb-matnr
      and matnr_l in r_matnr
      and mtart in ('ZRAW','ZPCK','ZSUB','ZCCM').
  "delete it_bom where mtart ne 'ZRAW'.
endform.                    " GET_BOM_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_ALL_ZRAW_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_all_zraw_data .
  data: lv_matnr like mara-matnr.
  sort it_bom by matnr_m.
  select matnr
      into table it_zlgp
      from mara
      for all entries in it_bom
      where matnr it_bom-matnr_m
        and mtart 'ZLGP'.
  sort it_zlgp by matnr.

  call function 'SAPGUI_PROGRESS_INDICATOR'
    exporting
      text 'Processing BOM Data........'.

  sort it_bom by werks_h matnr_h matnr_l .
  sort it_replace by werks matnr_o.
  loop at it_mchb.
    check it_mchb-matnr in r_matnr.
    read table it_replace with  key  werks =  it_mchb-werks matnr_o it_mchb-matnr binary search.
    if sy-subrc eq 0.
      lv_matnr it_replace-matnr_n.
    else.
      lv_matnr it_mchb-matnr.
    endif.
    read table it_bom with  key werks_h it_mchb-werks matnr_h lv_matnr .
    if sy-subrc eq 0.
      loop at it_bom where  werks_h it_mchb-werks and matnr_h lv_matnr.
        read table it_zlgp with  key matnr it_bom-matnr_m binary search.
        if sy-subrc eq 0.
          it_matnr-clabs =  it_mchb-clabs * it_bom-menge_h * it_bom-ewahr / 100.
        else.
          it_matnr-clabs =  it_mchb-clabs * it_bom-menge_h .
        endif.
        if it_matnr-clabs <> 0.
          it_matnr-matnr it_bom-matnr_l.
          it_matnr-werks it_mchb-werks .
          it_matnr-mtart it_bom-mtart.
          it_matnr-werks_s it_mchb-werks_s.
          perform convert_meins using it_matnr-matnr it_bom-meins it_bom-mmein changing it_matnr-clabs.
          collect it_matnr.
          clear: it_matnr.
        endif.
      endloop.
    else.
      if it_mchb-mtart 'ZRAW'
        or it_mchb-mtart 'ZPCK'
        or it_mchb-mtart 'ZSUB'
        or it_mchb-mtart 'ZCCM'.
        move-corresponding it_mchb to it_matnr.
        "it_matnr-werks = it_mchb-werks_s .
        collect it_matnr.
        clear: it_matnr.
      endif.
    endif.
    clear:lv_matnr.
  endloop.
  free :it_mchb,it_mchb1,it_bom,it_zlgp.
endform.                    " GET_ALL_ZRAW_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_OTHER_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_other_data .
  check it_matnr[] is not initial.
  sort it_matnr by matnr werks.

  select *
    into corresponding fields of table it_t604t
    from t604t
    where spras sy-langu .
  sort it_t604t by land1 stawn .

  select *
    into table it_mara
    from mara
    for all entries in it_matnr
    where matnr it_matnr-matnr.
  sort it_mara by matnr .

  select *
    into table it_marc
    from marc
    for all entries in it_matnr
    where matnr it_matnr-matnr
      and werks it_matnr-werks.
  sort it_marc by matnr werks.

  select *
    into table it_marc_s
    from marc
    for all entries in it_matnr
    where matnr it_matnr-matnr
      and werks it_matnr-werks_s.
  sort  it_marc_s by matnr werks.


  select matnr maktx
    into table it_makt
    from makt
    for all entries in it_matnr
    where matnr it_matnr-matnr.
  sort it_makt by matnr .
endform.                    " GET_OTHER_DATA
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form process_data .

  call function 'SAPGUI_PROGRESS_INDICATOR'
    exporting
      text 'Processing Data........'.

  loop at it_matnr.
    move-corresponding it_matnr to itab.
    " material desc
    read table it_makt with key matnr itab-matnr binary search.
    if sy-subrc eq 0.
      itab-maktx it_makt-maktx.
    endif.
    read table it_mara with key matnr itab-matnr binary search.
    if sy-subrc eq 0.
      itab-meins it_mara-meins.
      itab-ntgew it_mara-ntgew.
      itab-gewei it_mara-gewei.
      if itab-ntgew >= '88888'.
        itab-ntgew '0.01'.
        itab-gewei 'G'.
      endif.
    endif.

    read table it_marc_s with key matnr itab-matnr werks it_matnr-werks_s binary search.
    if sy-subrc eq 0.
      itab-stawn  it_marc_s-stawn .
    else.
      read table it_marc with key matnr itab-matnr werks itab-werks binary search.
      if sy-subrc eq 0.
        itab-stawn  it_marc-stawn .
      endif.
    endif.
    if itab-stawn is not initial.
      read table it_t604t with key stawn itab-stawn.
      if sy-subrc eq 0.
        itab-text1  =  it_t604t-text1 .
        itab-text2  =  it_t604t-text2 .
        itab-text7  =  it_t604t-text7 .
      endif.
    endif.

    if itab-meins 'EA'.
      perform rounding_dec   changing  itab-clabs.
    endif.
    if itab-gewei ne 'G' and itab-gewei is not initial..
      if itab-gewei eq 'KG'.
        itab-ntgew  itab-ntgew * 1000.
      elseif   itab-gewei eq 'MG'.
        itab-ntgew  itab-ntgew / 1000.
      endif.
    endif.
    itab-tlgew =  itab-ntgew * itab-clabs / 1000.
    itab-werks it_matnr-werks_s.
    append itab.
    clear:itab.
  endloop.
endform.                    " PROCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form display_data .
  refresh: fc_hier.

  alv_field  'WERKS' 'Plant' '' '5' 'L' '' '' ''.
  alv_field  'MATNR' 'Material' '' '18' 'L' ''  'MARA' 'MATNR'.
  alv_field  'MAKTX' '' 'Material desc' '20' 'L' ''  '' ''.
  alv_field  'STAWN' '' 'Customs code' '15' 'L' ''  '' ''.
  alv_field  'TEXT1' '' 'Chinese desc' '15' 'L' ''  '' ''.
  alv_field  'TEXT2' '' 'HS Code' '10' 'L' ''  '' ''.
  alv_field  'CLABS' '' 'Onhand qty' '12' 'R' 'X'  '' ''.
  alv_field  'MEINS' '' 'Stocking UM' '8' 'L' ''  '' ''.
  alv_field  'TEXT7' '' 'Customs UM' '6' 'L' ''  '' ''.
  alv_field  'NTGEW' '' 'Unit weight(G)' '10' 'R' ''  '' ''.
  alv_field  'TLGEW' '' 'Total weight(KG)' '10' 'R' ''  '' ''.

  sort itab by matnr werks.
  free :it_mara,it_marc,it_t604t,it_makt,it_matnr.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      it_fieldcat        fc_hier[]
      i_save             'A'
      i_callback_program sy-repid
    tables
      t_outtab           itab[]
    exceptions
      program_error      1
      others             2.
  if sy-subrc ne 0.
    write: sy-msgv1,sy-msgv2, sy-msgv3, sy-msgv4.
  endif.
endform.                    " DISPLAY_DATA
*&---------------------------------------------------------------------*
*&      Form  convert_meins
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_MATNR      text
*      -->P_MEINS_IN   text
*      -->P_MEINS_OUT  text
*      -->P_VAL        text
*----------------------------------------------------------------------*
form convert_meins using p_matnr type mchb-matnr
                         p_meins_in type meins
                         p_meins_out type meins
                   changing p_val type mseg-menge.

  if p_meins_in <> p_meins_out.
    call function 'MD_CONVERT_MATERIAL_UNIT'
      exporting
        i_matnr              p_matnr
        i_in_me              p_meins_in
        i_menge              p_val
        i_out_me             p_meins_out
      importing
        e_menge              p_val
      exceptions
        error_in_application 1
        error                2
        others               3.
  endif.
endform.                    "convert_meins
*&---------------------------------------------------------------------*
*&      Form  Rounding_dec
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form rounding_dec  changing p_val.
  if p_val > 0.
    p_val truncp_val + '0.5').
  endif.
endform.                    "rounding_dec

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值