ZPPR032 :Production Order Diff Qty

38 篇文章 2 订阅

*&---------------------------------------------------------------------*
*& Progarm      :  ZPPR032                        Author : Jimmy Wong
*& Created      :  05 Dec 2012                    App    : PP
*& Title        :  Production Order Diff Qty
*& Description  :  Production Order Diff Qty
*&---------------------------------------------------------------------*
*&  Version       Author      Date        description
*&                Jimmy       05 Dec 2012 the first version
*                 Lisa Wong 2013.02.23    delete TECO order
*                 Lisa Wong 2013.03.01    Add selection of monthend data and del finial issue
*& M1             Jimmy       07 Mar 2013 Get WIP by CO03
*& M2             Jimmy       14 Mar 2013 CO03 RESB have Multiple record
*&  the last update time  2013.03.12 16:00
*&---------------------------------------------------------------------*


report  zppr032 no standard page heading.

include zppr032top.
include zppr032sel.
include zppr032f01.

initialization.
  p_budat = sy-datum.

at selection-screen output.
  if sy-tcode ne 'ZPP032A'.
    loop at screen.
      if screen-group1 = 'SAV'.
        screen-intensified = '0'.
        screen-required = '0'.
        screen-active = 0.
        screen-display_3d = '0'.
        modify screen.
      endif.
    endloop.
  else.
    sy-title = 'Production Order WIP Detail Monthly'  .
  endif.
*&---------------------------------------------------------------------*
* start of process
*&---------------------------------------------------------------------*
start-of-selection.
  if p_online = 'X'.         "Add by Lisa Wong 03/01/2013
    perform get_data.

    if gt_order[] is initial  .
      message 'No Record Found' type 'I'.
      exit.
    endif.
*    perform process_data.   "M1 Remark
    perform process_data_co03.                              "M1 add
  else.
    perform get_monthend.     "Add by Lisa Wong 03/01/2013
  endif.

  if itab[] is initial  .
    message 'No Record Found' type 'I'.
    exit.
  endif.
  if p_save = 'X' and p_online = 'X'.
    perform save_data.
  endif.
  perform display_data.

*&---------------------------------------------------------------------*
*&  Include           ZPPR032TOP
*&---------------------------------------------------------------------*
tables:afko,afpo,aufk,mara,mkpf .
data: begin of gt_order occurs 0,
      aufnr like afko-aufnr,
      rsnum like afko-rsnum,
      matnr like afpo-matnr ,
      werks like aufk-werks,
      psmng like afpo-psmng,
      wemng like afpo-wemng,
      meins_m like afpo-meins,
      matnr_d like afpo-matnr ,
      useqty like resb-enmng,
      enmng like resb-enmng,
      diffqty like resb-enmng,
      bdmng like resb-bdmng ,
      meins like afpo-meins,
      meins_d like afpo-meins,
      verpr   like mbew-verpr,  "net price
      stprs   like mbew-verpr,  "str price
      peinh   like mbew-peinh,
      netwr   like ekpo-netwr,
      mtart like mara-mtart,
      objnr like aufk-objnr,
      budat like mkpf-budat,
      projn like afpo-projn,
      sortf like resb-sortf,
      kzear like resb-kzear,        "Add by Lisa Wong 03/05/2013
      rspos like resb-rspos,"M2 add RSPOS
      end of gt_order.
data: itab like table of gt_order with header line.
data:begin of it_mbew occurs 0,
    matnr like mbew-matnr,
    bwkey like mbew-bwkey ,
    verpr like mbew-verpr ,
    stprs like mbew-stprs ,
    peinh like mbew-peinh ,
    end of it_mbew.
data:begin of it_mara occurs 0,
    matnr like mbew-matnr,
    mtart like mara-mtart,
    meins like mara-meins,
    end of it_mara.
data: begin of gt_header occurs 0,
      aufnr like afko-aufnr,
      matnr like afpo-matnr ,
      werks like aufk-werks,
      end of gt_header.
data: gt_detail like gt_header occurs 0 with header line.
data: begin of gt_matnr occurs 0,
      matnr like afpo-matnr ,
      werks like aufk-werks,
      end of gt_matnr.
data: begin of gt_gr occurs 0,
      aufnr like afko-aufnr,
      matnr like afpo-matnr,
      werks like aufk-werks,
      bwart like mseg-bwart,
      dmbtr like mseg-dmbtr,
      menge like mseg-menge,
      meins like mseg-meins,
      budat like mkpf-budat,
      mblnr like mkpf-mblnr,
      mjahr like mseg-mjahr,
      zeile like mseg-zeile,
      end of gt_gr.
data:it_issue like gt_gr occurs 0 with header line.
data: begin of gt_gr_total occurs 0,
      aufnr like afko-aufnr,
      matnr like afpo-matnr,
      werks like aufk-werks,
      menge like mseg-menge,
      end of gt_gr_total.
data:it_issue_total like gt_gr_total occurs 0 with header line.
data:it_zpp032 like zpp032 occurs 0  with header line .
data:begin of it_mkpf occurs 0,
     mblnr like mkpf-mblnr,
     mjahr like mseg-mjahr,
     budat like mkpf-budat,
    end of it_mkpf.
data:lv_lfgja like bapi0002_4-fiscal_year,
     lv_lfmon like bapi0002_4-fiscal_period .
ranges:r_mblnr for mkpf-mblnr.
type-pools: slis.
data: fc_hier type slis_t_fieldcat_alv,
      wa_hier type slis_fieldcat_alv .

data:  t_zpp032 like table of zpp032 with header line.   "Add by Lisa wong 03/01/2013


*&---------------------------------------------------------------------*
*&  Include           ZPPR032SEL
*&---------------------------------------------------------------------*
selection-screen begin of block 1 with frame title text-001.
select-options : s_werks for aufk-werks  default '8101'.
select-options : s_aufnr for aufk-aufnr .
select-options : s_matnr for mara-matnr .
parameters  : p_budat like mkpf-budat .
parameters : p_diff as checkbox default 'X'.
parameters : p_save as checkbox modif id sav.

parameters:   p_online radiobutton group rad1 default 'X' modif id pr.       "Add by Lisa Wong 03/01/2013
parameters:   p_month  radiobutton group rad1 modif id  pr.                  "Add by Lisa Wong 03/01/2013

selection-screen end of block 1.

*&---------------------------------------------------------------------*
*&  Include           ZPPR032F01
*&---------------------------------------------------------------------*

*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_data .
  select  a~aufnr a~rsnum
          b~matnr  b~psmng   b~meins as meins_m  b~projn b~wemng
          c~werks c~objnr
          e~meins as meins_d  e~matnr as matnr_d e~bdmng e~enmng e~sortf e~kzear e~rspos "M2 add RSPOS
          into corresponding fields of table gt_order
          from afko as a
          join afpo as b on a~aufnr = b~aufnr
          join aufk as c on a~aufnr = c~aufnr
          join resb as e on a~rsnum = e~rsnum
          where a~aufnr in s_aufnr
            and a~ftrmi ne space
            and c~werks in s_werks
            and c~loekz eq space
            and b~matnr in s_matnr
            and b~psmng > 0
            and e~xloek = space
            and e~dumps = space
            and c~idat2 eq '00000000'.        "Add by Lisa Wong 02/23/2013

  delete  gt_order where ( sortf ne space and sortf <> 'X') ." Add By Jimmy 02/28/2013


  if p_online = 'X'.                                        "M1 add
    loop at gt_order.
      gt_matnr-matnr = gt_order-matnr_d.
      gt_matnr-werks = gt_order-werks.

      append: gt_matnr.
      clear: gt_matnr.
    endloop.
    sort gt_matnr by  matnr werks.
    delete adjacent duplicates from gt_matnr comparing all fields.

  else.
    loop at gt_order.
      gt_header-aufnr = gt_order-aufnr.
      gt_header-matnr = gt_order-matnr.
      gt_header-werks = gt_order-werks.

      gt_detail-aufnr = gt_order-aufnr.
      gt_detail-matnr = gt_order-matnr_d.
      gt_detail-werks = gt_order-werks.

      gt_matnr-matnr = gt_order-matnr_d.
      gt_matnr-werks = gt_order-werks.

      append: gt_header,gt_detail,gt_matnr.
      clear: gt_header,gt_detail,gt_matnr.
    endloop.

    sort gt_header by  matnr werks aufnr.
    sort gt_detail by  matnr werks aufnr.
    sort gt_matnr by  matnr werks.

    delete adjacent duplicates from gt_header comparing all fields.
    delete adjacent duplicates from gt_detail comparing all fields.
    delete adjacent duplicates from gt_matnr comparing all fields.
  endif.
endform.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA_CO03
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form process_data_co03 .
  sort gt_order by aufnr matnr werks.

  select matnr  bwkey verpr stprs peinh
      into  table it_mbew
      from mbew
      for all entries in gt_matnr
      where matnr = gt_matnr-matnr
        and bwkey =  gt_matnr-werks.
  sort it_mbew by matnr bwkey.

  select matnr  mtart meins  into  table it_mara
      from mara
      for all entries in gt_matnr
      where matnr = gt_matnr-matnr.
  sort it_mara by matnr.


  loop at gt_order.
    move-corresponding gt_order to itab.

    if itab-kzear = 'X' and itab-wemng = itab-psmng.        "M1 add
      continue.
    endif.

    itab-useqty = itab-wemng * itab-bdmng / itab-psmng .
    itab-diffqty = itab-enmng - itab-useqty .

    if  p_diff = 'X' .
      check itab-diffqty <> 0.
    endif.

    read table it_mara with  key matnr = itab-matnr_d binary search.
    if sy-subrc eq 0.
      itab-mtart = it_mara-mtart.
      itab-meins = it_mara-meins.
    endif.

    if itab-diffqty <> 0 .
      read table it_mbew with  key matnr = itab-matnr_d bwkey = itab-werks binary search .
      if sy-subrc eq 0.
        itab-verpr  = it_mbew-verpr .
        itab-stprs  = it_mbew-stprs .
        itab-peinh   = it_mbew-peinh .
        if itab-peinh ne 0.
          if itab-mtart = 'ZVFG' or itab-mtart = 'ZUFG'
             or itab-mtart = 'ZIFG'   or itab-mtart = 'ZPCB'.
            itab-verpr = it_mbew-stprs.
          else.
            if itab-verpr eq 0.
              itab-verpr = it_mbew-stprs.
            endif.
          endif.
          itab-netwr  = itab-verpr  * itab-diffqty / itab-peinh .
        endif.
      endif.
      append itab.
    endif.
    clear:itab.
  endloop.
  free: gt_order,it_mbew.
endform.                    " PROCESS_DATA_CO03

*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form process_data .
  sort gt_order by aufnr matnr werks.
  sort gt_header by  matnr werks aufnr.
  sort gt_detail by  matnr werks aufnr.
  sort gt_matnr by  matnr werks.

  select  b~aufnr b~matnr b~werks b~bwart b~menge b~meins a~budat  b~dmbtr b~mblnr b~mjahr b~zeile
      into corresponding fields of table gt_gr
      from mkpf as a join mseg as b on a~mblnr = b~mblnr and a~mjahr = b~mjahr
      for all entries in gt_header
      where a~budat <= p_budat
        and b~matnr = gt_header-matnr
        and b~werks = gt_header-werks
        and b~bwart in ('101','102')
        and b~aufnr = gt_header-aufnr.
  sort gt_gr by aufnr matnr werks budat.

  loop at gt_gr.
    gt_gr_total-aufnr = gt_gr-aufnr.
    gt_gr_total-matnr = gt_gr-matnr.
    gt_gr_total-werks  = gt_gr-werks.
    if gt_gr-bwart = '101'.
      gt_gr_total-menge =   gt_gr-menge.
    else.
      gt_gr_total-menge = 0  - gt_gr-menge.
    endif.
    collect gt_gr_total.
    clear:gt_gr_total.
  endloop.
  delete gt_gr_total where menge = 0.
  sort gt_gr_total  by aufnr matnr werks.


  select mblnr mjahr
      into corresponding fields of table it_mkpf
      from mkpf
      where budat > p_budat
        and mblnr in r_mblnr.
  if it_mkpf[] is not initial.
    sort it_mkpf by mblnr mjahr.
    select  b~aufnr b~matnr b~werks b~bwart b~menge b~meins   b~dmbtr b~mblnr b~mjahr b~zeile
        into corresponding fields of table it_issue
        from  mseg as b
        for all entries in it_mkpf
        where mblnr = it_mkpf-mblnr
          and mjahr = it_mkpf-mjahr
          and werks in s_werks
          and b~bwart in ('261','262')
          and b~aufnr in s_aufnr.
    loop at it_issue.
      read table gt_detail with key matnr = it_issue-matnr werks = it_issue-werks  aufnr = it_issue-aufnr binary search.
      if sy-subrc eq 0.
        it_issue_total-aufnr = it_issue-aufnr.
        it_issue_total-matnr = it_issue-matnr.
        it_issue_total-werks  = it_issue-werks.
        if it_issue-bwart = '261'.
          it_issue_total-menge =   it_issue-menge.
        else.
          it_issue_total-menge = 0  - it_issue-menge.
        endif.
        collect it_issue_total.
        clear:it_issue_total.
      endif.
    endloop.
    delete it_issue_total where menge = 0.
  endif.
  sort it_issue_total  by aufnr matnr werks.

  select matnr  bwkey verpr stprs peinh
      into corresponding fields of table it_mbew
      from mbew
      for all entries in gt_matnr
      where matnr = gt_matnr-matnr
        and bwkey =  gt_matnr-werks.
  sort it_mbew by matnr bwkey.

  select matnr  mtart meins  into  table it_mara
      from mara
      for all entries in gt_matnr
      where matnr = gt_matnr-matnr.
  sort it_mara by matnr.

  loop at gt_order.
    move-corresponding gt_order to itab.
    clear: itab-wemng ,itab-enmng.

    read table gt_gr_total with  key  aufnr = itab-aufnr matnr = itab-matnr werks = itab-werks binary search.
    if sy-subrc eq 0.
      itab-wemng  =  gt_gr_total-menge .
      read table gt_gr with  key  aufnr = itab-aufnr matnr = itab-matnr werks = itab-werks.
      if sy-subrc eq 0.
        itab-budat = gt_gr-budat.
      endif.
    endif.

    read table it_issue_total with  key  aufnr = itab-aufnr matnr = itab-matnr_d werks = itab-werks binary search.
    if sy-subrc eq 0.
      itab-enmng  = itab-enmng - it_issue_total-menge .
    endif.

    perform convert_meins using itab-matnr itab-meins_d
                            itab-meins
                   changing itab-bdmng  .
    perform convert_meins using itab-matnr itab-meins_d
                      itab-meins
             changing itab-enmng.

    itab-useqty = itab-wemng * itab-bdmng / itab-psmng .
    itab-diffqty = itab-enmng - itab-useqty .
    if  p_diff = 'X' .
      check itab-diffqty <> 0.
    endif.

    read table it_mara with  key matnr = itab-matnr_d binary search.
    if sy-subrc eq 0.
      itab-mtart = it_mara-mtart.
      itab-meins = it_mara-meins.
    endif.

    if itab-diffqty <> 0 .
      read table it_mbew with  key matnr = itab-matnr_d bwkey = itab-werks binary search .
      if sy-subrc eq 0.
        itab-verpr  = it_mbew-verpr .
        itab-stprs  = it_mbew-stprs .
        itab-peinh   = it_mbew-peinh .
        if itab-peinh ne 0.
          if itab-mtart = 'ZVFG' or itab-mtart = 'ZUFG'
             or itab-mtart = 'ZIFG'   or itab-mtart = 'ZPCB'.
            itab-verpr = it_mbew-stprs.
          else.
            if itab-verpr eq 0.
              itab-verpr = it_mbew-stprs.
            endif.
          endif.
          itab-netwr  = itab-verpr  * itab-diffqty / itab-peinh .
        endif.
      endif.
      append itab.
    endif.
    clear:itab.
  endloop.
  free: gt_order,gt_header,gt_detail,it_mbew,gt_gr_total,gt_gr,it_issue,it_issue_total,gt_matnr.
endform.                    " PROCESS_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  DISPLAY_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form display_data .
  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.
  refresh: fc_hier.
  alv_field  'AUFNR' '' '' '' 'L' '' 'AFKO' 'AUFNR'.
  alv_field  'PROJN' '' '' '10' 'L' '' 'PRPS' 'PSPNR'.
  alv_field  'MATNR' '' 'Material' '18' 'L' '' 'MARA' 'MATNR'.
  alv_field  'MATNR_D' '' 'Sub Material' '18' 'L' '' 'MARA' 'MATNR'.
  alv_field  'WERKS' 'Plant' '' '5' 'L' '' '' ''.
  alv_field  'PSMNG' '' 'Total Qty' '10' 'R' '' '' ''.
  alv_field  'WEMNG' '' 'Delivered Qty' '10' 'R' '' '' ''.
  alv_field  'MEINS_M' 'Unit' '' '5' 'L' '' '' ''.
  alv_field  'BDMNG' '' 'Requirement Qty' '10' 'R' '' '' ''.
  alv_field  'ENMNG' '' 'Issue Qty' '10' 'R' 'X' '' ''.
  alv_field  'USEQTY' '' 'Consumed Qty' '10' 'R' 'X' '' ''.
  alv_field  'DIFFQTY' '' 'Diff Qty' '10' 'R' 'X' '' ''.
  alv_field  'VERPR' '' 'Net Price' '10' 'R' '' '' ''.
  alv_field  'PEINH' '' 'Price Unit' '10' 'R' '' '' ''.
  alv_field  'NETWR' '' 'WIP Amount' '10' 'R' 'X' '' ''.

  sort itab by aufnr matnr matnr_d.

  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.
endform.                    " DISPLAY_DATA
*&---------------------------------------------------------------------*
*&      Form  SAVE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form save_data .
  data:lv_subrc like sy-subrc.
  perform get_period changing lv_subrc.
  if lv_subrc eq 0.
    perform delete_data.
    loop at itab.
      move-corresponding itab to it_zpp032.
      it_zpp032-lfgja =  lv_lfgja.
      it_zpp032-lfmon = lv_lfmon.
      append it_zpp032.
      clear: it_zpp032.
    endloop.
    if not it_zpp032[] is initial.
      sort  it_zpp032 by aufnr matnr matnr_d werks lfgja lfmon rspos.
      modify zpp032 from table it_zpp032.
*      write: 'Excute Successed.'.
      refresh it_zpp032.
      free:it_zpp032.
    endif.
  else.
    write: 'Get period Error.' .
  endif.

endform.                    " SAVE_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_PERIOD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_period changing p_subrc type sy-subrc.
  data:lv_date  like bapi0002_4-posting_date,
       it_return like bapireturn1.
  lv_date  = p_budat .
  call function 'BAPI_COMPANYCODE_GET_PERIOD'
    exporting
      companycodeid = '8000'
      posting_date  = lv_date
    importing
      fiscal_year   = lv_lfgja
      fiscal_period = lv_lfmon
      return        = it_return.
  p_subrc = sy-subrc .
endform.                    " GET_PERIOD
*&---------------------------------------------------------------------*
*&      Form  DELETE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form delete_data .
  data:begin of it_del occurs 20,
       werks like zpp032-werks,
       lfgja like zpp032-lfgja,
       lfmon like zpp032-lfmon,
      end of it_del.
  select distinct werks lfgja lfmon
      into  corresponding fields of table it_del
      from zpp032
      where werks in s_werks
      and lfgja = lv_lfgja
      and lfmon = lv_lfmon.
  if it_del[] is not initial.
    loop at it_del.
      delete from zpp032 where werks = it_del-werks and  lfgja = it_del-lfgja and lfmon = it_del-lfmon.
      commit work and wait.
    endloop.
  endif.
endform.                    " DELETE_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_MONTHEND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_monthend .
  data: l_lfgja like zpp032-lfgja,
        l_lfmon like zpp032-lfmon,
        t_return like bapireturn1,
        l_budat  like sy-datum.
  concatenate sy-datum+0(4) sy-datum+4(2) '01'  into l_budat .
  l_budat  = l_budat - 1.
  call function 'BAPI_COMPANYCODE_GET_PERIOD'
    exporting
      companycodeid = '8000'
      posting_date  = l_budat
    importing
      fiscal_year   = l_lfgja
      fiscal_period = l_lfmon
      return        = t_return.

  select *  into corresponding fields of table itab
  from zpp032
  where zpp032~werks in s_werks
    and zpp032~lfgja = l_lfgja
    and zpp032~lfmon = l_lfmon
    and zpp032~aufnr   in s_aufnr
    and zpp032~matnr_d in s_matnr.
  if p_diff = 'X'.
    delete itab where netwr = 0.
  endif.

endform.                    " GET_MONTHEND

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值