ABAP ALV多层显示及分类汇总

2021-02-20 14:51:24 浏览数 (1)

代码语言:javascript复制
tables: sscrfields,bseg,bkpf,mara. 
type-pools: slis,kkblo. 
data: w_vari            type disvariant, 
      w_layo            type slis_layout_alv, 
      i_fcat            type slis_t_fieldcat_alv, 
      w_fcat            type slis_fieldcat_alv, 
      i_evts            type slis_t_event, 
      i_evts_exit       type slis_t_event_exit, 
      w_keyinfo         type slis_keyinfo_alv. 
constants: cns_x type c value 'X'. 
    field-symbols: type table. 
    field-symbols: type table. 
    data:str_name1(30) type c, 
         str_name2(30) type c. 
data: l_total type i value 0. 
data: x_total type i value 0. 
data: g_belnr like range of bkpf-belnr, 
      g_belnr_line like line of g_belnr. 
data: x_belnr like range of bkpf-belnr, 
      x_belnr_line like line of x_belnr. 
types: begin of st_bkpf, 
          belnr like bkpf-belnr, 
          xblnr like bkpf-xblnr, 
          budat like bkpf-budat, 
       end of st_bkpf. 
data:wa_bkpf type st_bkpf, 
     it_bkpf type standard table of st_bkpf. 
data:bef_day like bkpf-budat. 
types: begin of st_bseg, 
          bukrs like bseg-bukrs, 
          belnr like bseg-belnr, "会计凭证编号 
          gjahr like bseg-gjahr, 
          buzei like bseg-buzei, "会计凭证中的行项目数 
          dmbtr like bseg-dmbtr, 
          shkzg like bseg-shkzg, "借贷 
          koart like bseg-koart,"科目类型 
          budat like mkpf-budat,"过帐日期 
          sgtxt like bseg-sgtxt,"文本 
          menge like bseg-menge,"数量 
          matnr like bseg-matnr, 
          prodh like vbrp-prodh, 
          hkont like bseg-hkont, 
          xblnr like bkpf-xblnr,"参考发票 
*          AUGBL LIKE bseg-AUGBL,"清帐凭证 
          vbeln like bseg-vbeln, 
          xref1_hd like bkpf-xref1_hd, 
       end of st_bseg. 
data:wa_bseg type st_bseg, 
     it_bseg type standard table of st_bseg. 
data:tmp_bseg type st_bseg. 
"定义上期结转 
"定义发票用的: 
data:begin of st_vbrp, 
  vbeln like vbrp-vbeln, 
  posnr like vbrp-posnr, 
  fkimg like vbrp-fkimg, 
  netwr like vbrp-netwr, 
  matnr like vbrp-matnr, 
  prodh like vbrp-prodh, 
  end of st_vbrp. 
data:it_vbrp like table of st_vbrp, 
     wa_vbrp like st_vbrp. 
"显示的struct 
data: begin of headertab1, "物料 
          matnr like mara-matnr,   "KEY 
          gjahr like bkpf-gjahr,   "KEY 
          maktx like makt-maktx, 
          chk type c,              "是否够选 
          expand type c, 
      end of headertab1. 
data: begin of itemtab1, 
          matnr like mara-matnr, 
          gjahr like bkpf-gjahr, 
          monat like bkpf-monat, 
          day(2) type c, 
          xref1_hd like bkpf-xref1_hd, 
          summy(12) type c, 
          s_num like bseg-menge, 
          s_money like bseg-dmbtr, 
          h_num like bseg-menge, 
          h_money like bseg-dmbtr, 
          diec(2) type c, 
          o_num like bseg-menge, 
          o_unit like bseg-dmbtr, 
          o_money like bseg-dmbtr, 
      end of itemtab1. 
data: begin of headertab2 ,         "产品层次 
          prdha like mara-prdha, 
          gjahr like bkpf-gjahr, 
          vtext like t179t-vtext, 
          chk type c, 
          expand type c, 
      end of headertab2. 
data: begin of itemtab2, 
          prdha like mara-prdha, 
          gjahr like bkpf-gjahr, 
          monat like bkpf-monat, 
          day(2) type c, 
          xref1_hd like bkpf-xref1_hd, 
          summy(12) type c, 
          s_num like bseg-menge, 
          s_money like bseg-dmbtr, 
          h_num like bseg-menge, 
          h_money like bseg-dmbtr, 
          diec(2) type c, 
          o_num like bseg-menge, 
          o_unit like bseg-dmbtr, 
          o_money like bseg-dmbtr, 
       end of itemtab2. 
data:it_headertab1 like table of  headertab1. 
data:it_itemtab1 like table of  itemtab1. 
data:it_headertab2 like table of  headertab2. 
data:it_itemtab2 like table of  itemtab2. 
 
data: xslt_error type ref to cx_xslt_exception, 
      xslt_message type string  . 
data:opt_budat like zoptions. 
data:opt_matnr like zoptions. 
data g_program_name type sy-cprog value sy-repid. 
 
select-options : p_xref1 for bkpf-xref1_hd . 
select-options : p_budat for bkpf-budat . 
select-options : p_matnr for mara-matnr. 
select-options : p_hkont for bseg-hkont. 
select-options:  p_prdha for mara-prdha. 
parameters: p_gjahr like bseg-gjahr obligatory default sy-datlo(4), 
              p_bukrs like bseg-bukrs, 
              p_groes like mara-groes. 
 
at selection-screen. 
w_vari-report = g_program_name. 
perform. f_get_data. 
if p_matnr is not initial. 
str_name1 = 'HEADERTAB1'. 
str_name2 = 'ITEMTAB1'. 
else. 
str_name1 = 'HEADERTAB2'. 
str_name2 = 'ITEMTAB2'. 
endif. 
perform. f_set_fcat:using str_name1, using str_name2. "获得结构 
"修改显示 
loop at i_fcat into w_fcat. 
  case w_fcat-fieldname. 
    when 'DAY'. 
      w_fcat-seltext_l = '日'. 
    when 'MONAT'. 
      w_fcat-seltext_l = '月'. 
    when 'GJAHR'. 
      w_fcat-seltext_l = '年'. 
      w_fcat-seltext_m = '年'. 
      w_fcat-seltext_s = '年'. 
      w_fcat-reptext_ddic = '年'. 
    when 'SUMMY'. 
      w_fcat-seltext_l = '摘要'. 
     w_fcat-outputlen = '4'. 
    when 'S_NUM'. 
      w_fcat-seltext_s = '借方数量'. 
      w_fcat-do_sum = 'X'.  "分类排序时算汇总 
     w_fcat-outputlen = '10'. 
    when 'S_MONEY'. 
      w_fcat-seltext_s = '借方金额'. 
      w_fcat-seltext_l = '借方金额'. 
      w_fcat-seltext_m = '借方金额'. 
      w_fcat-reptext_ddic = '借方金额'. 
      w_fcat-do_sum = 'X'. 
     w_fcat-outputlen = '10'. 
    when 'H_NUM'. 
      w_fcat-seltext_s = '贷方数量'. 
      w_fcat-do_sum = 'X'. 
     w_fcat-outputlen = '10'. 
    when 'H_MONEY'. 
      w_fcat-seltext_s = '贷方金额'. 
      w_fcat-seltext_l = '贷方金额'. 
      w_fcat-seltext_m = '贷方金额'. 
      w_fcat-reptext_ddic = '贷方金额'. 
      w_fcat-do_sum = 'X'. 
     w_fcat-outputlen = '10'. 
    when 'DIEC'. 
      w_fcat-seltext_s = '方向'. 
     w_fcat-outputlen = '4'. 
    when 'O_NUM'. 
      w_fcat-seltext_s = '余额数量'. 
      w_fcat-do_sum = 'X'. 
     w_fcat-outputlen = '10'. 
    when 'O_UNIT'. 
      w_fcat-seltext_s = '余额单位'. 
      w_fcat-seltext_l = '余额单位'. 
      w_fcat-seltext_m = '余额单位'. 
      w_fcat-reptext_ddic = '余额单位'. 
      w_fcat-do_sum = 'X'.   
     w_fcat-outputlen = '10'. 
    when 'O_MONEY'. 
      w_fcat-seltext_s = '余额金额'. 
      w_fcat-seltext_l = '余额金额'. 
      w_fcat-seltext_m = '余额金额'. 
      w_fcat-reptext_ddic = '余额金额'. 
      w_fcat-do_sum = 'X'. 
     w_fcat-outputlen = '10'. 
   WHEN 'MATNR'. 
     w_fcat-key           = 'X'. 
    WHEN 'PRDHA'. 
     w_fcat-key           = 'X'. 
  endcase. 
modify i_fcat from w_fcat. 
endloop. 
  perform. f_set_layo. 
  perform. f_set_keyinfo. 
  perform. f_call_alv. 
 
*&---------------------------------------------------------------------* 
*&      Form  F_GET_DATA 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
form. f_get_data . 
"取数 
  clear :g_belnr,it_bseg,it_bkpf. 
  refresh: g_belnr,x_belnr. 
  select bkpf~belnr bkpf~xblnr bkpf~budat 
    into corresponding fields of table it_bkpf  from bkpf 
        where bkpf~bukrs = p_bukrs and bkpf~gjahr = p_gjahr and 
        bkpf~budat in p_budat 
        and xref1_hd in p_xref1. 
  if sy-subrc = 0. 
    sort it_bkpf by belnr. 
    delete adjacent duplicates from it_bkpf comparing belnr. 
    l_total = 0. 
    "查询 
    loop at it_bkpf into wa_bkpf. 
      l_total = l_total   1. 
      g_belnr_line-sign = 'I'. 
      g_belnr_line-option = 'EQ'. 
      g_belnr_line-low = wa_bkpf-belnr. 
      append g_belnr_line to g_belnr. 
      if l_total = 3000. 
        select bukrs belnr gjahr buzei dmbtr shkzg koart sgtxt menge 
        matnr hkont vbeln 
           appending corresponding fields of table it_bseg from bseg 
          where bseg~bukrs = p_bukrs and bseg~belnr in g_belnr and 
          bseg~gjahr = p_gjahr 
          and ( matnr in p_matnr or matnr = '' ) and hkont in p_hkont. 
        l_total = 0. 
        clear g_belnr_line. 
        refresh g_belnr. 
      endif. 
    endloop. 
    "查询 
    if l_total <> 0. 
      select bukrs belnr gjahr buzei dmbtr shkzg koart sgtxt menge matnr 
      hkont vbeln 
        appending corresponding fields of table it_bseg from bseg 
        where bseg~bukrs = p_bukrs and bseg~belnr in g_belnr and 
        bseg~gjahr = p_gjahr 
        and matnr in p_matnr and hkont in p_hkont. 
      l_total = 0. 
      clear g_belnr_line. 
      refresh g_belnr. 
    endif. 
  endif. 
  "补全物料为空的0054010101参考发票 
  loop at it_bseg into wa_bseg where matnr = '' and hkont = '0054010101'  . 
    clear wa_bkpf. 
    read table it_bkpf with key belnr = wa_bseg-belnr into wa_bkpf. 
    if sy-subrc = 0. 
      wa_bseg-xblnr = wa_bkpf-xblnr. 
    endif. 
    clear it_vbrp. 
    select vbeln posnr fkimg netwr prodh matnr 
      appending corresponding fields of table it_vbrp 
      from vbrp where vbeln = wa_bseg-xblnr. 
    "匹配对应的行项目并取物料和产品层次 
    loop at it_vbrp into wa_vbrp. 
      clear tmp_bseg. 
      read table it_bseg with key 
      belnr = wa_bseg-belnr dmbtr = wa_vbrp-netwr menge = wa_vbrp-fkimg 
      into tmp_bseg. 
      if sy-subrc = 0. 
        tmp_bseg-matnr = wa_vbrp-matnr.tmp_bseg-prodh = wa_vbrp-prodh. 
        modify it_bseg from tmp_bseg index sy-tabix. 
      endif. 
    endloop. 
  endloop. 
  "补全0054010101科目的物料后,物料还是空全部去掉 
  delete it_bseg where matnr = ''. 
  "过滤掉不要的规格和产品层次 
  loop at it_bseg into wa_bseg. 
    if p_groes is not initial. 
      select count(*) from mara where matnr = wa_bseg-matnr and groes = 
      p_groes. 
      if sy-subrc <> 0. 
        delete it_bseg where matnr = wa_bseg-matnr. 
      endif. 
    endif. 
    if p_prdha is not initial. 
      select count(*) from mara where matnr = wa_bseg-matnr and prdha = 
      wa_bseg-prodh . 
      if sy-subrc <> 0. 
        delete it_bseg where matnr = wa_bseg-matnr and prodh = 
        wa_bseg-prodh. 
      endif. 
    endif. 
  endloop. 
  "汇总显示 
  if p_matnr is not initial. 
    loop at it_bseg into wa_bseg. 
      "日期 
      read table it_bkpf with key belnr = wa_bseg-belnr into wa_bkpf. 
      if sy-subrc = 0. 
        wa_bseg-budat = wa_bkpf-budat. 
      endif. 
      clear: itemtab1,headertab1. 
      "头 
      select single maktx 
        into headertab1-maktx from makt where matnr = wa_bseg-matnr. 
      headertab1-matnr = wa_bseg-matnr. 
      headertab1-gjahr = wa_bseg-gjahr. 
      append headertab1 to it_headertab1. 
      "体 
      itemtab1-matnr = wa_bseg-matnr. 
      itemtab1-gjahr = wa_bseg-gjahr. 
      itemtab1-monat = wa_bseg-budat 4(2). 
      itemtab1-day = wa_bseg-budat 6(2). 
      itemtab1-xref1_hd = wa_bseg-xref1_hd. 
      if wa_bseg-shkzg = 'S'. 
        itemtab1-summy = '购进'. 
        itemtab1-s_num = wa_bseg-menge. 
        itemtab1-s_money = wa_bseg-dmbtr. 
        itemtab1-diec = '借'. 
      elseif wa_bseg-shkzg = 'H'. 
        itemtab1-summy = '销售'. 
        itemtab1-h_num = wa_bseg-menge. 
        itemtab1-h_money = wa_bseg-dmbtr. 
        itemtab1-diec = '贷'. 
      endif. 
      itemtab1-o_num = itemtab1-s_num - itemtab1-h_num. 
      itemtab1-o_money = itemtab1-s_money - itemtab1-h_money. 
      if itemtab1-o_num <> 0. 
        itemtab1-o_unit = itemtab1-o_money / itemtab1-o_num. 
      endif. 
      collect itemtab1 into it_itemtab1. 
    endloop. 
    sort it_headertab1 by matnr gjahr. 
    delete adjacent duplicates from it_headertab1 comparing matnr gjahr. 
    sort it_headertab1 by matnr. 
    sort it_itemtab1 by matnr. 
    assign it_headertab1 to . 
    assign it_itemtab1 to . 
elseif p_prdha is not initial. 
    loop at it_bseg into wa_bseg. 
      clear:headertab2,itemtab2. 
      "日期 
      read table it_bkpf with key belnr = wa_bseg-belnr into wa_bkpf. 
      if sy-subrc = 0. 
        wa_bseg-budat = wa_bkpf-budat. 
      endif. 
      "头 
      select single vtext 
        into headertab2-vtext from t179t where prodh = wa_bseg-prodh. 
      headertab2-prdha = wa_bseg-prodh. 
      headertab2-gjahr = wa_bseg-gjahr. 
      append headertab2 to it_headertab2. 
      "体 
      itemtab2-prdha = wa_bseg-prodh. 
      itemtab2-gjahr = wa_bseg-gjahr. 
      itemtab2-monat = wa_bseg-budat 4(2). 
      itemtab2-day = wa_bseg-budat 6(2). 
      itemtab2-xref1_hd = wa_bseg-xref1_hd. 
      if wa_bseg-shkzg = 'S'. 
        itemtab2-summy = '购进'. 
        itemtab2-s_num = wa_bseg-menge. 
        itemtab2-s_money = wa_bseg-dmbtr. 
        itemtab2-diec = '借'. 
      elseif wa_bseg-shkzg = 'H'. 
        itemtab2-summy = '销售'. 
        itemtab2-h_num = wa_bseg-menge. 
        itemtab2-h_money = wa_bseg-dmbtr. 
        itemtab2-diec = '贷'. 
      endif. 
      itemtab2-o_num = itemtab2-s_num - itemtab2-h_num. 
      itemtab2-o_money = itemtab2-s_money - itemtab2-h_money. 
      if itemtab2-o_num <> 0. 
        itemtab2-o_unit = itemtab2-o_money / itemtab2-o_num. 
      endif. 
      collect itemtab2 into it_itemtab2. 
    endloop. 
  sort it_headertab2 by prdha gjahr. 
  delete adjacent duplicates from it_headertab2 comparing prdha gjahr. 
  sort it_headertab2 by prdha. 
  sort it_itemtab2 by prdha. 
    assign it_headertab2 to . 
    assign it_itemtab2 to . 
endif. 
endform.                    " F_GET_DATA 
 
form. f_set_fcat  using l_tabname type lvc_fname. 
  data: w_fcat type slis_fieldcat_alv, 
        i_fcat_tmp type slis_t_fieldcat_alv with header line. 
  field-symbols: type slis_fieldcat_alv. 
  call function 'REUSE_ALV_FIELDCATALOG_MERGE' 
    exporting 
     i_program_name               = g_program_name 
     i_internal_tabname           = l_tabname 
*      I_STRUCTURE_NAME             = 'mara' 
*     I_CLIENT_NEVER_DISPLAY       = CNS_X 
     i_inclname                   = g_program_name 
*      I_BYPASSING_BUFFER           = 
*      I_BUFFER_ACTIVE              = 
    changing 
      ct_fieldcat                  = i_fcat_tmp[] 
    exceptions 
      inconsistent_interface       = 1 
      program_error                = 2 
      others                       = 3. 
**--上面是获得W_HEAD的组成情况,获得它的字段和所在table 
  append lines of  i_fcat_tmp to i_fcat. 
  check l_tabname eq str_name1. 
      read table i_fcat assigning 
                        with key fieldname = 'EXPAND'. 
      -no_out = cns_x. 
**--上面是说EXPAND这个字段不显示 
endform.                    " f_set_fcat 
*&---------------------------------------------------------------------* 
*&      Form  f_set_layo 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
form. f_set_layo . 
  w_layo-zebra                  = cns_x. 
*... Interaction 
  w_layo-box_fieldname          = 'CHK'. 
  w_layo-box_tabname            = str_name1. 
****上面两行是设置CHEKC BOX的 
  w_layo-get_selinfos           = cns_x. 
  w_layo-expand_fieldname       = 'EXPAND'. 
****上面是设置按照那个字段来分层,必须要有,字段要用大写 
  w_layo-expand_all             = cns_x. 
  w_layo-numc_sum = 'X'. 
endform.                    " f_set_layo 
*&---------------------------------------------------------------------* 
*&      Form  f_set_keyinfo 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
form. f_set_keyinfo . 
  if p_matnr is not initial. 
  w_keyinfo-header01 = 'MATNR'. 
  w_keyinfo-item01 = 'MATNR'. 
  else. 
  w_keyinfo-header01 = 'PRDHA'. 
  w_keyinfo-item01 = 'PRDHA'. 
  endif. 
endform.                    " f_set_keyinfo 
*&---------------------------------------------------------------------* 
*&      Form  f_call_alv 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*  -->  p1        text 
*  <--  p2        text 
*----------------------------------------------------------------------* 
form. f_call_alv . 
  DATA:it_sort TYPE slis_t_sortinfo_alv. 
  PERFORM. e02_layout_sort_build CHANGING it_sort."分类排序 
  call function 'REUSE_ALV_HIERSEQ_LIST_DISPLAY' 
    exporting 
      i_callback_program             = g_program_name 
      I_CALLBACK_PF_STATUS_SET       = 'F_SET_STATUS' 
      I_CALLBACK_USER_COMMAND        = 'F_USR_COMMAND' 
      is_layout                      = w_layo 
      it_fieldcat                    = i_fcat[] 
      i_default                      = 'X' 
      i_save                         = 'A' 
      IT_SORT                        = it_sort[] 
      is_variant                     = w_vari 
      i_tabname_header               = str_name1  "这个是第一层 
      i_tabname_item                 = str_name2  "这个是第二层 
      is_keyinfo                     = w_keyinfo 
    tables 
      t_outtab_header                =   "这个是第一层 
      t_outtab_item                  =   "这个是第二层 
    exceptions 
      program_error                  = 1 
      others                         = 2. 
  if sy-subrc <> 0. 
    message id sy-msgid type sy-msgty number sy-msgno 
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
  endif. 
endform.                    " f_call_alv 
FORM. F_USR_COMMAND USING r_ucomm LIKE sy-ucomm 
                  rs_selfield TYPE slis_selfield. 
  CASE r_ucomm. 
    WHEN 'BACK' OR 'UP' OR 'CANCEL'. 
      CLEAR:it_bseg,it_bkpf,it_itemtab1,it_itemtab2,it_headertab1, 
      it_headertab2. 
      LEAVE TO SCREEN 0. 
    WHEN '&PNT'. 
   ENDCASE. 
ENDFORM. 
form. F_SET_STATUS using rt_extab type slis_t_extab. 
  set pf-status 'ZSTANDARD' . 
endform. "Set_pf_status 
*&---------------------------------------------------------------------* 
*&      Form  e02_layout_sort_build 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
*      <--P_IT_SORT  text 
*----------------------------------------------------------------------* 
FORM. e02_layout_sort_build  CHANGING lt_sort TYPE slis_t_sortinfo_alv. 
DATA ls_sort TYPE slis_sortinfo_alv. 
IF p_matnr IS NOT INITIAL. 
CLEAR ls_sort. 
ls_sort-fieldname = 'MATNR'. "#EC * 
ls_sort-spos = 1. 
ls_sort-up = 'X'. 
ls_sort-subtot = 'X'. 
APPEND ls_sort TO lt_sort. 
ELSE. 
CLEAR ls_sort. 
ls_sort-fieldname = 'PRDHA'. "#EC * 
ls_sort-spos = 1. 
ls_sort-up = 'X'. 
ls_sort-subtot = 'X'. 
APPEND ls_sort TO lt_sort. 
ENDIF. 
ENDFORM.                    " e02_layout_sort_build

0 人点赞