欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【SAP】ABAP——动态模板

程序员文章站 2022-05-01 19:53:42
需求:根据输入条件,动态显示输出的列。REPORT ZMM001.TABLES:rsddstat_olap, rsddstatheader .DATA:lv_infoprov(30), lv_objname(30), lv_name_first(40), lv_name_last(40).TYPES:BEGIN OF is_datail, "明细 uname TYPE rsddstat_olap-uname, "用户ID uname_l(80)...

需求:根据输入条件,动态显示输出的列。

REPORT ZMM001.
TABLES:rsddstat_olap, rsddstatheader .

DATA:lv_infoprov(30),
     lv_objname(30),
     lv_name_first(40),
     lv_name_last(40).

TYPES:BEGIN OF is_datail, "明细
  uname    TYPE rsddstat_olap-uname,   "用户ID
  uname_l(80),                         "用户名称
  calday   TYPE rsddstat_olap-calday,  "运行日期
  utime    TYPE rsddstat_olap-utime,   "运行时间
  infoprov TYPE rsddstat_olap-infoprov,"信息提供者
  objname  TYPE rsddstat_olap-objname, "QURRY ID
  txtlg    TYPE rszelttxt-txtlg,       "QURRY 名称
END OF is_datail.
DATA: ls_detail TYPE is_datail,
      wa_detail TYPE is_datail,
      lt_detail TYPE TABLE OF is_datail,
      lt_out    TYPE TABLE OF is_datail.


TYPES:BEGIN OF is_total,
  objname  TYPE rsddstat_olap-objname, "QURRY ID
  txtlg    TYPE rszelttxt-txtlg, " QURRY 名称
  uname    TYPE rsddstat_olap-uname,   "用户ID
  uname_l(80),                         "用户名称
  calday   TYPE rsddstat_olap-calday,  "运行日期
  utime    TYPE rsddstat_olap-utime,   "运行时间
  infoprov TYPE rsddstat_olap-infoprov,"信息提供者
END OF is_total.
DATA:ls_total TYPE is_total,
     lt_total TYPE TABLE OF is_total.


DATA:fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
     layout   TYPE slis_layout_alv.
DATA lv_repid LIKE sy-repid value sy-repid.

TYPES:BEGIN OF is_period,"汇总数据包含的月份期间
  period(6),  "期间
  pertx(10),  "期间描述
END OF is_period.
DATA:ls_period TYPE is_period,
     lt_period TYPE TABLE OF is_period.

TYPES:BEGIN OF is_compid,
  compid TYPE v_rep_join-compuid, "QURRY ID
  txtlg   TYPE rszelttxt-txtlg,   "QURRY 名称
END OF is_compid.
DATA:lt_compid TYPE TABLE OF is_compid.

DATA: lv_year(4),
      lv_line TYPE i,
      lv_index TYPE i,
      lv_last TYPE i.

DATA:dy_table    TYPE REF TO data,
     dy_line     TYPE REF TO data,
     ls_fieldcat TYPE lvc_s_fcat,
     lt_fieldcat TYPE lvc_t_fcat.


FIELD-SYMBOLS: <it_table> TYPE STANDARD TABLE,
               <wa_table>,
               <wa_field>,
               <wa_sum>.

SELECT-OPTIONS:s_calday FOR   rsddstat_olap-calday  OBLIGATORY,"运行日期
               s_uname  FOR   rsddstat_olap-uname,             "用户名
               s_oname  FOR  rsddstatheader-objname.           "Query ID
PARAMETERS:r_total  RADIOBUTTON GROUP g1 DEFAULT 'X',          "显示汇总
           r_detail RADIOBUTTON GROUP g1.                      "显示明细

INITIALIZATION.
  CONCATENATE 'Z' '%' INTO lv_infoprov.
  CONCATENATE 'Z' '%' INTO lv_objname.

"搜索帮助
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_oname-low.
  CLEAR:lt_compid.
  SELECT compid txtlg
    INTO CORRESPONDING FIELDS OF TABLE lt_compid
    FROM v_rep_join
    INNER JOIN rszelttxt
            ON v_rep_join~compuid = rszelttxt~eltuid
    WHERE v_rep_join~objvers = 'A'
      AND v_rep_join~deftp = 'REP'
      AND v_rep_join~compid LIKE lv_objname
      AND rszelttxt~langu = sy-langu.
  SORT lt_compid BY compid.
  DELETE ADJACENT DUPLICATES FROM lt_compid.
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = 'COMPID'
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
      dynprofield     = 'S_ONAME-LOW'
      value_org       = 'S'
    TABLES
      value_tab       = lt_compid
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_oname-high.
  CLEAR:lt_compid.
  SELECT compid txtlg
    INTO CORRESPONDING FIELDS OF TABLE lt_compid
    FROM v_rep_join
    INNER JOIN rszelttxt
            ON v_rep_join~compuid = rszelttxt~eltuid
    WHERE v_rep_join~objvers = 'A'
      AND v_rep_join~deftp = 'REP'
      AND v_rep_join~compid LIKE lv_objname
      AND rszelttxt~langu = sy-langu.
  SORT lt_compid BY compid.
  DELETE ADJACENT DUPLICATES FROM lt_compid.
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = 'COMPID'
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
      dynprofield     = 'S_ONAME-HIGH'
      value_org       = 'S'
    TABLES
      value_tab       = lt_compid
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.


START-OF-SELECTION.
IF r_total = 'X'."获取汇总数据
  PERFORM get_total_data.
  PERFORM dis_alv.
ELSEIF r_detail = 'X'."获取明细数据
  PERFORM get_detail_data.
  PERFORM display_alv.
ENDIF.


*&---------------------------------------------------------------------*
*& Form GET_DETAIL_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM get_detail_data.
  SELECT a~objname    a~infoprov
         b~uname      b~calday
         b~utime
    FROM rsddstatheader AS a
    INNER JOIN rsddstatinfo AS b
            ON b~stepuid = a~stepuid
    INNER JOIN rsddstatevdata AS c
            ON c~stepuid = a~stepuid
           AND c~handleid = a~handleid
           AND c~handletp = a~handletp
    INTO CORRESPONDING FIELDS OF TABLE lt_detail
    WHERE c~handletp = 'OLAP'
      AND c~eventid = '3010'
      AND b~steptp = 'AOE'
      AND a~infoprov LIKE lv_infoprov
      AND a~objname LIKE lv_objname
      AND b~calday IN s_calday
      AND b~uname IN s_uname
      AND a~objname IN s_oname.

  LOOP AT lt_detail INTO ls_detail.
    "在用户ID在表ZMM001中的排除
    SELECT COUNT(*) FROM ZMW001 WHERE uname = ls_detail-uname.
    CHECK sy-subrc <> 0.

    SELECT SINGLE name_first name_last INTO (lv_name_first,lv_name_last)
      FROM adrp
      INNER JOIN usr21 on usr21~PERSNUMBER = adrp~PERSNUMBER
      WHERE usr21~BNAME = ls_detail-uname.
    CONCATENATE lv_name_first lv_name_last INTO ls_detail-uname_l.

    SELECT SINGLE txtlg INTO ls_detail-txtlg
      FROM rszelttxt
      INNER JOIN v_rep_join ON v_rep_join~compuid = rszelttxt~eltuid
      WHERE v_rep_join~compid = ls_detail-objname
        AND v_rep_join~objvers = 'A'
        AND v_rep_join~deftp = 'REP'
        AND rszelttxt~langu = sy-langu.

    APPEND ls_detail TO lt_out.

    MOVE-CORRESPONDING ls_detail TO ls_total.
    APPEND ls_total TO lt_total.

    CLEAR:ls_detail,ls_total.
  ENDLOOP.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM display_alv .

  layout-colwidth_optimize = 'X'.

  REFRESH fieldcat.
  fieldcat-fieldname = 'UNAME'.
  fieldcat-seltext_m  = '用户ID'.
  APPEND fieldcat.
  CLEAR fieldcat.

  fieldcat-fieldname = 'UNAME_L'.
  fieldcat-seltext_m  = '用户名称'.
  APPEND fieldcat.
  CLEAR fieldcat.

  fieldcat-fieldname = 'CALDAY'.
  fieldcat-seltext_m  = '运行日期'.
  APPEND fieldcat.
  CLEAR fieldcat.

  fieldcat-fieldname = 'UTIME'.
  fieldcat-seltext_m  = '运行时间'.
  APPEND fieldcat.
  CLEAR fieldcat.

  fieldcat-fieldname = 'INFOPROV'.
  fieldcat-seltext_m  = '信息提供者'.
  APPEND fieldcat.
  CLEAR fieldcat.

  fieldcat-fieldname = 'OBJNAME'.
  fieldcat-seltext_m  = 'QURRY ID'.
  APPEND fieldcat.
  CLEAR fieldcat.

  fieldcat-fieldname = 'TXTLG'.
  fieldcat-seltext_m  = '报表名称'.
  APPEND fieldcat.
  CLEAR fieldcat.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM                = lv_repid
      IS_LAYOUT                         = layout
      IT_FIELDCAT                       = fieldcat[]
    TABLES
      T_OUTTAB                          = lt_out
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_TOTAL_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM get_total_data .
  PERFORM get_detail_data.

  "获取查询期间
  IF s_calday-high IS INITIAL.
    ls_period-period = s_calday-low.
    CONCATENATE ls_period-period(4) '年'  ls_period-period+4(2) '月' INTO ls_period-pertx.
    APPEND ls_period TO lt_period.
  ELSEIF s_calday-low IS INITIAL.
    ls_period-period = s_calday-high.
    CONCATENATE ls_period-period(4) '年'  ls_period-period+4(2) '月' INTO ls_period-pertx.
    APPEND ls_period TO lt_period.
  ELSE.
    ls_period-period = s_calday-low(6).
    CONCATENATE ls_period-period(4) '年'  ls_period-period+4(2) '月' INTO ls_period-pertx.
    APPEND ls_period TO lt_period.

    DO.
      IF ls_period-period < s_calday-high(6).
        IF ls_period-period+4(2) <> 12.
          ls_period-period = ls_period-period + 1.
        ELSE.
          lv_year = ls_period-period(4) + 1.
          CONCATENATE lv_year '01' into ls_period-period.
        ENDIF.
        CONCATENATE ls_period-period(4) '年'  ls_period-period+4(2) '月' INTO ls_period-pertx.
        APPEND ls_period TO lt_period.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.

  ENDIF.
  SORT lt_period BY period ASCENDING.


  PERFORM dynamic_table.
  DESCRIBE TABLE lt_period lines lv_line.
  lv_last = lv_line + 2."汇总列所处的列序号

  SORT lt_total BY objname txtlg calday ASCENDING.
  LOOP AT lt_total INTO ls_total.
    AT NEW txtlg.
      ASSIGN COMPONENT 1 OF STRUCTURE <wa_table> to <wa_field>.
      <wa_field> = ls_total-txtlg.

      lv_index = 2."期间开始的列序号
      ASSIGN COMPONENT lv_last OF STRUCTURE <wa_table> to <wa_sum>.
      DO lv_line TIMES.
        ASSIGN COMPONENT lv_index OF STRUCTURE <wa_table> TO <wa_field>.
        READ TABLE lt_fieldcat INTO ls_fieldcat INDEX lv_index.
        IF SY-SUBRC = 0.
          CONDENSE ls_fieldcat-fieldname NO-GAPS.
          LOOP AT lt_total INTO ls_total WHERE txtlg = ls_total-txtlg
                                           AND objname = ls_total-objname
                                           AND calday(6) = ls_fieldcat-fieldname.
            <wa_field> = <wa_field> + 1.
          ENDLOOP.
          <wa_sum> = <wa_sum> + <wa_field>.
        ENDIF.
        lv_index = lv_index + 1.
      ENDDO.
      APPEND <wa_table> TO <it_table>.
      CLEAR: <wa_table>,ls_total.
    ENDAT.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM dynamic_table .
  DEFINE init_field.
    CLEAR &1.
    &1-fieldname = &2.
    &1-reptext_ddic = &3.
    &1-do_sum = &4.
    &1-no_zero = 'X'.
    &1-hotspot = &5.
    APPEND &1.
  END-OF-DEFINITION.

  INIT_FIELD fieldcat 'TXTLG' '报表名称' '' ''.
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'TXTLG'.
  ls_fieldcat-datatype  = 'CHAR'.
  ls_fieldcat-inttype = 'C'.
  ls_fieldcat-intlen = 60.
  ls_fieldcat-decimals = 0.
  APPEND ls_fieldcat TO lt_fieldcat.

  LOOP AT lt_period into ls_period.
    init_field fieldcat ls_period-period ls_period-pertx '' ''.
    CLEAR ls_fieldcat.

    ls_fieldcat-fieldname = ls_period-period.
    ls_fieldcat-datatype  = 'CHAR'.
    ls_fieldcat-inttype = 'C'.
    ls_fieldcat-intlen = 10.
    ls_fieldcat-decimals = 0.
    APPEND ls_fieldcat TO lt_fieldcat.

  ENDLOOP.

  INIT_FIELD fieldcat 'TOTAL' '总计' '' ''.
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'TOTAL'.
  ls_fieldcat-datatype  = 'CHAR'.
  ls_fieldcat-inttype = 'C'.
  ls_fieldcat-intlen = 10.
  ls_fieldcat-decimals = 0.
  APPEND ls_fieldcat TO lt_fieldcat.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = lt_fieldcat
    IMPORTING
      ep_table                  = dy_table.

  ASSIGN dy_table->* TO <it_table>.
  CREATE DATA dy_line LIKE LINE OF <it_table>.
  ASSIGN dy_line->* TO <wa_table>.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form DIS_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM dis_alv .

  layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = lv_repid
      is_layout               = layout
      i_save                  = 'A'
      it_fieldcat             = fieldcat[]
    TABLES
      T_OUTTAB                = <it_table>
    EXCEPTIONS
      PROGRAM_ERROR           = 1
      OTHERS                  = 2
            .
  IF SY-SUBRC <> 0.
    WRITE: 'SY-SUBRC: ', sy-subrc, 'REUSE_ALV_GRID_DISPLAY'.
  ENDIF.

ENDFORM.

 

本文地址:https://blog.csdn.net/Mona_1220/article/details/108981582