SAP 程序代码记录-ALV

2023-10-13 14:43:14 浏览数 (1)

代码:

代码语言:javascript复制
  REPORT  ZZWEI_ALV_EXAMPLE.

  TABLES:LFA1,"供应商主数据 (一般地区)
         EKPO,"采购凭证项目
         EINE,"采购信息记录 - 采购组织数据
         EINA."采购信息记录 - 一般数据

  INCLUDE <LIST>.
  TYPE-POOLS:SLIS.

  DATA:i_fieldcat_alv TYPE SLIS_T_FIELDCAT_ALV,"定义列标题(属性信息)
       i_layout TYPE SLIS_LAYOUT_ALV,
       i_fieldcat TYPE SLIS_FIELDCAT_ALV,"定义对象
       i_events TYPE SLIS_T_EVENT,"alv事件
       w_events LIKE LINE OF i_events,
       i_list_comments TYPE SLIS_T_LISTHEADER,"alv表单标题区域设置
       w_list_comments LIKE LINE OF i_list_comments,
       w_repid LIKE SY-REPID."当前程序


  *定义内表字段
  DATA:BEGIN OF hd_itab OCCURS 0,
        infnr LIKE EINE-INFNR,"采购信息记录
        ekorg LIKE EINE-EKORG,"采购组织
        matnr LIKE EINA-MATNR,"物料编号
        lifnr LIKE EINA-LIFNR,"供应商
        erdat LIKE EINE-ERDAT,"创建日期
        loekz LIKE EINE-LOEKZ,"删除标记
        urznr LIKE EINA-URZNR,"批准号
        color(4) TYPE C, "定义颜色
        aplfz LIKE EINE-APLFZ,"计划交货时间
        netpr LIKE EINE-NETPR,"净价
        peinh LIKE EINE-PEINH,"单位价格
        dwjj LIKE EINE-NETPR,"单位净价
        bprme LIKE EINE-BPRME,"订单价格单位
        maktx LIKE MAKT-MAKTX,"物料名称
        name1 LIKE LFA1-NAME1,"供应商名称
        iicon LIKE ICON-NAME, "ICON 图标
       END OF hd_itab.

  *选择屏幕
  SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE t1.
    SELECT-OPTIONS:s_infnr FOR EINE-INFNR MEMORY ID INF."信息记录号码
    PARAMETERS:PR_LOEKZ TYPE C AS CHECKBOX DEFAULT ''."显示有删除标志的信息记录
    SELECT-OPTIONS:s_ekorg FOR EINE-EKORG MEMORY ID EKO."采购组织
    SELECT-OPTIONS:s_matnr FOR EINA-MATNR MEMORY ID MAT."物料编号
    SELECT-OPTIONS:s_lifnr FOR EINA-LIFNR MEMORY ID LIF."供应商编号
    SELECT-OPTIONS:s_erdat FOR EINA-ERDAT MEMORY ID erd."创建日期
    SELECT-OPTIONS:s_urznr FOR EINA-URZNR MEMORY ID urz."批准号
  SELECTION-SCREEN END OF BLOCK block1.

  *初始化事件
  INITIALIZATION.
        t1 = '采购查询'.

  *屏幕开始事件
  START-OF-SELECTION.
     PERFORM getdata.
     PERFORM events_build.
     PERFORM layout_build.
     PERFORM fieldcat_build.
     PERFORM alv_display_data.

  *&--------------------------------------------------------------------*
  *&      Form  getdata
  *&--------------------------------------------------------------------*
  FORM getdata.
    CLEAR hd_itab.
    CLEAR hd_itab[].

    IF PR_LOEKZ = ''.
      SELECT * FROM EINE INNER JOIN EINA ON EINE~INFNR = EINA~INFNR INTO CORRESPONDING FIELDS OF hd_itab
    WHERE EINE~INFNR IN s_infnr AND EINE~EKORG IN s_ekorg AND EINA~MATNR IN s_matnr AND EINA~LIFNR IN s_lifnr AND EINA~LOEKZ <> 'X' AND EINE~ERDAT IN s_erdat AND EINA~URZNR IN s_urznr
      ORDER BY EINE~INFNR DESCENDING.
        SELECT SINGLE MAKTX FROM MAKT INTO CORRESPONDING FIELDS OF hd_itab WHERE matnr = hd_itab-matnr.
        SELECT SINGLE NAME1 FROM LFA1 INTO CORRESPONDING FIELDS OF hd_itab WHERE lifnr = hd_itab-lifnr.
        APPEND hd_itab.
        CLEAR hd_itab.
      ENDSELECT.
    ELSE.
      SELECT * FROM EINE INNER JOIN EINA ON EINE~INFNR = EINA~INFNR INTO CORRESPONDING FIELDS OF hd_itab
    WHERE EINE~INFNR IN s_infnr AND EINE~EKORG IN s_ekorg AND EINA~MATNR IN s_matnr AND EINA~LIFNR IN s_lifnr AND EINE~ERDAT IN s_erdat AND EINA~URZNR IN s_urznr
      ORDER BY EINE~INFNR DESCENDING.
        SELECT SINGLE MAKTX FROM MAKT INTO CORRESPONDING FIELDS OF hd_itab WHERE matnr = hd_itab-matnr.
        SELECT SINGLE NAME1 FROM LFA1 INTO CORRESPONDING FIELDS OF hd_itab WHERE lifnr = hd_itab-lifnr.
        APPEND hd_itab.
        CLEAR hd_itab.
      ENDSELECT.

   ENDIF.

   LOOP AT hd_itab.
     IF hd_itab-loekz = 'X'.
       hd_itab-color = 'C610'.
       hd_itab-iicon = ICON_INCOMPLETE. "ICON圖標信息
     ELSE.
       hd_itab-iicon = ICON_CHECKED. "ICON圖標信息
     ENDIF.
     hd_itab-dwjj = hd_itab-netpr / hd_itab-peinh.  "净价/价格单位

     MODIFY hd_itab."更新
     CLEAR hd_itab."CLEAR HEADER LINE
   ENDLOOP.
  ENDFORM.

  *&---------------------------------------------------------------------------*
  *&    FORM LAYOUT BUILD
  *&---------------------------------------------------------------------------*
  FORM layout_build.
    i_layout-ZEBRA = 'X'. "ALV表格按斑马线条纹显示
    "i_layout-NO_VLINE = 'X'. "ALV表格顯示時無堅向線
    i_layout-COLWIDTH_OPTIMIZE = 'X'. "将ALV字段宽度设置为最优化
*    i_layout-detail_initial_lines = 'X'.
*    i_layout-detail_titlebar = '详细内容'.
*    i_layout-box_fieldname  = 'LINE'.
    i_layout-INFO_FIELDNAME = 'COLOR'. "設置顏色FIELD
    i_layout-NO_COLHEAD = ''.
    w_repid = SY-REPID.
  ENDFORM.



  *&---------------------------------------------------------------------------*
  *&    FORM FIELDCAT BUILD
  *&---------------------------------------------------------------------------*
  FORM fieldcat_build.
    REFRESH i_fieldcat_alv.
    CLEAR i_fieldcat.
    DATA column TYPE I VALUE 1.

    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'INFNR'.
    i_fieldcat-SELTEXT_S = '采购信息记录'.
    i_fieldcat-SELTEXT_M = '采购信息记录'.
    i_fieldcat-SELTEXT_L = '采购信息记录'.
    "i_fieldcat-reptext_ddic = '采购信息记录号'.
    i_fieldcat-LZERO = 'X'. "字义字段前导以"0"的形式显示
    i_fieldcat-KEY = 'X'.
    i_fieldcat-HOTSPOT = 'X'."设置字段是否有热点(热点字段显示有下划线)
    i_fieldcat-EMPHASIZE = 'C300'."设置字段颜色
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'LIFNR'.
    i_fieldcat-SELTEXT_S = '供应商号码'.
    i_fieldcat-SELTEXT_M = '供应商号码'.
    i_fieldcat-SELTEXT_L = '供应商号码'.
    "i_fieldcat-reptext_ddic = '供应商号码'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    i_fieldcat-HOTSPOT = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'NAME1'.
    i_fieldcat-SELTEXT_S = '供应商名称'.
    i_fieldcat-SELTEXT_M = '供应商名称'.
    i_fieldcat-SELTEXT_L = '供应商名称'.
    "i_fieldcat-reptext_ddic = '供应商名称'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'MATNR'.
    i_fieldcat-SELTEXT_S = '物料编号'.
    i_fieldcat-SELTEXT_M = '物料编号'.
    i_fieldcat-SELTEXT_L = '物料编号'.
    "i_fieldcat-reptext_ddic = '物料编号'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'MAKTX'.
    i_fieldcat-SELTEXT_S = '物料名称'.
    i_fieldcat-SELTEXT_M = '物料名称'.
    i_fieldcat-SELTEXT_L = '物料名称'.
    "i_fieldcat-reptext_ddic = '物料名称'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'NETPR'.
    i_fieldcat-SELTEXT_S = '净价'.
    i_fieldcat-SELTEXT_M = '净价'.
    i_fieldcat-SELTEXT_L = '净价'.
    "i_fieldcat-reptext_ddic = '净价'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'PEINH'.
    i_fieldcat-SELTEXT_S = '价格单位'.
    i_fieldcat-SELTEXT_M = '价格单位'.
    i_fieldcat-SELTEXT_L = '价格单位'.
    "i_fieldcat-reptext_ddic = '价格单位'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'DWJJ'.
    i_fieldcat-SELTEXT_S = '单位净价'.
    i_fieldcat-SELTEXT_M = '单位净价'.
    i_fieldcat-SELTEXT_L = '单位净价'.
    "i_fieldcat-reptext_ddic = '单位净价'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'BPRME'.
    i_fieldcat-SELTEXT_S = '计量单位'.
    i_fieldcat-SELTEXT_M = '计量单位'.
    i_fieldcat-SELTEXT_L = '计量单位'.
    "i_fieldcat-reptext_ddic = '计量单位'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'EKORG'.
    i_fieldcat-SELTEXT_S = '采购组织'.
    i_fieldcat-SELTEXT_M = '采购组织'.
    i_fieldcat-SELTEXT_L = '采购组织'.
    "i_fieldcat-reptext_ddic = '采购组织'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'APLFZ'.
    i_fieldcat-SELTEXT_S = '计划交货时间'.
    i_fieldcat-SELTEXT_M = '计划交货时间'.
    i_fieldcat-SELTEXT_L = '计划交货时间'.
    "i_fieldcat-reptext_ddic = '计划交货时间'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    i_fieldcat-Emphasize = 'C211'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'ERDAT'.
    i_fieldcat-SELTEXT_S = '创建日期'.
    i_fieldcat-SELTEXT_M = '创建日期'.
    i_fieldcat-SELTEXT_L = '创建日期'.
    "i_fieldcat-reptext_ddic = '创建日期'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'LOEKZ'.
    i_fieldcat-SELTEXT_S = '删除标志'.
    i_fieldcat-SELTEXT_M = '删除标志'.
    i_fieldcat-SELTEXT_L = '删除标志'.
    "i_fieldcat-reptext_ddic = '删除标志'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'URZNR'.
    i_fieldcat-SELTEXT_S = '批准标记'.
    i_fieldcat-SELTEXT_M = '批准标记'.
    i_fieldcat-SELTEXT_L = '批准标记'.
    "i_fieldcat-reptext_ddic = '批准标记'.
    i_fieldcat-LZERO = 'X'.
    i_fieldcat-KEY = 'X'.
    i_fieldcat-Emphasize = 'C211'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

    column = column   1.
    i_fieldcat-COL_POS = column.
    i_fieldcat-FIELDNAME = 'IICON'.
*    i_fieldcat-SELTEXT_S = '图标'.
*    i_fieldcat-SELTEXT_M = 'ICON'.
    i_fieldcat-SELTEXT_L = 'ICON'.
    i_fieldcat-HOTSPOT = 'X'.
    i_fieldcat-ICON = 'X'.
    "i_fieldcat-reptext_ddic = '批准标记'.
    "i_fieldcat-LZERO = 'X'.
    "i_fieldcat-KEY = 'X'.
    "i_fieldcat-Emphasize = 'C211'.
    APPEND i_fieldcat TO i_fieldcat_alv.
    CLEAR i_fieldcat.

  ENDFORM.


  *&---------------------------------------------------------------------------*
  *&    FORM ALV DISPLAY DATA
  *&---------------------------------------------------------------------------*
  FORM alv_display_data.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
        I_CALLBACK_PROGRAM                = w_repid
        I_GRID_TITLE                      = '采购信息记录汇总查询'
        IS_LAYOUT                         = i_layout
        IT_FIELDCAT                       = i_fieldcat_alv[]
        IT_EVENTS                         = i_events[]
        "I_SAVE  = 'A'
        I_CALLBACK_USER_COMMAND           = 'USER_COMMAND' "USER_COMMAND EVENT
      TABLES
        T_OUTTAB                          = hd_itab[]
     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.

  FORM user_command USING i_ucomm TYPE SY-UCOMM i_selfield TYPE SLIS_SELFIELD.

    CASE i_ucomm.
      WHEN '&IC1'.  "对于热点链接,所对应的动作码为"&IC1"
        IF i_selfield-FIELDNAME EQ 'INFNR'. "判断用户当前单击的是哪列
          PERFORM call_tran USING i_selfield. "调用自定义事务
        ENDIF.
    ENDCASE.
  ENDFORM.



  FORM call_tran USING i_selfield TYPE SLIS_SELFIELD.
    "SET SCREEN 0.  "返回主屏幕
    READ TABLE hd_itab INDEX i_selfield-tabindex.  "i_selfield-tabindex 当击行索引,从内表中提取数据

    IF SY-SUBRC = 0.
       SET PARAMETER ID 'MAT' FIELD hd_itab-MATNR. "MAT指参数ID
       SET PARAMETER ID 'LIF' FIELD hd_itab-LIFNR.
       SET PARAMETER ID 'EKO' FIELD hd_itab-EKORG.
       SET PARAMETER ID 'INF' FIELD hd_itab-INFNR.

       CALL TRANSACTION 'ME13'."AND SKIP FIRST SCREEN "调用其它事务 ME13是我们要调用的事务
    ENDIF.
  ENDFORM.


  FORM ALV_TOP_OF_PAGE. "ALV表头信息
    CLEAR:i_list_comments.
    w_list_comments-typ = 'H'.
    w_list_comments-key = ''.
    w_list_comments-info = '采购信息记录查询报表'.
    APPEND w_list_comments TO i_list_comments.
    CLEAR w_list_comments.

    DATA:date_temp(30) TYPE C.
    WRITE: SY-DATUM TO date_temp MM/DD/YYYY.
    CONCATENATE '日期:' date_temp INTO date_temp.
    CONCATENATE date_temp  '.   IT部' INTO date_temp.


    w_list_comments-typ = 'S'.
    w_list_comments-key = ''.
    w_list_comments-info = date_temp.
    APPEND w_list_comments TO i_list_comments.
    CLEAR w_list_comments.

    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'  "输出注释
      EXPORTING
        IT_LIST_COMMENTARY       = i_list_comments
        I_LOGO                   = 'ENJOYSAP_LOGO'
        I_END_OF_LIST_GRID       = 'X'
*       I_ALV_FORM               =
              .
  ENDFORM.

  FORM ALV_END_OF_LIST.
    CLEAR: i_list_comments.
*    w_list_comments-typ = 'S'.
*    w_list_comments-info = 'Create By IT DEPARTMENT'.
*    APPEND w_list_comments TO i_list_comments.
*    CLEAR w_list_comments.
*
*    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
*      EXPORTING
*        IT_LIST_COMMENTARY       = i_list_comments
**        I_LOGO                   = 'ENJOYSAP_LOGO'
*        I_END_OF_LIST_GRID       = 'X'
**       I_ALV_FORM               =
*            .

    CLEAR:i_list_comments.
    w_list_comments-typ = 'S'.
    w_list_comments-info = '确认:________采购申请人:___________________'.
    APPEND w_list_comments TO i_list_comments.
    CLEAR w_list_comments.

    w_list_comments-typ = 'S'.
    w_list_comments-info = 'Create By Information Department'.
    APPEND w_list_comments TO i_list_comments.
    CLEAR w_list_comments.
 
    DATA:data_temp(30) TYPE C.
    CONCATENATE '打印者:' SY-UNAME INTO data_temp.
    w_list_comments-typ = 'S'.
    w_list_comments-info = data_temp.
    APPEND w_list_comments TO i_list_comments.
    CLEAR w_list_comments.

    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
      EXPORTING
        IT_LIST_COMMENTARY       = i_list_comments
        I_LOGO                   = 'ENJOYSAP_LOGO'
        I_END_OF_LIST_GRID       = 'X'
*       I_ALV_FORM               =
            .
  ENDFORM.


  FORM events_build.
    CALL FUNCTION 'REUSE_ALV_EVENTS_GET' "输出一个内表类型是SLIS_T_EVENT,行项为SLIS_ALV_EVENT,包含两个字段,一个是事件名称,另一个是处理事件的FORM名称 
     EXPORTING
       I_LIST_TYPE           = 0
     IMPORTING
       ET_EVENTS             = i_events
     EXCEPTIONS
       LIST_TYPE_WRONG       = 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.

    READ TABLE i_events WITH KEY NAME = 'TOP_OF_PAGE' INTO w_events.
    IF SY-SUBRC = 0.
      MOVE 'ALV_TOP_OF_PAGE' TO w_events-form.
      MODIFY i_events FROM w_events INDEX SY-TABIX.
    ENDIF.

    READ TABLE i_events INTO w_events WITH KEY NAME = 'USER_COMMAND'.
    IF SY-SUBRC EQ 0.
      w_events-name = 'USER_COMMAND'.
      MODIFY i_events FROM w_events INDEX SY-TABIX.
    ENDIF.

    READ TABLE i_events WITH KEY NAME = 'END_OF_LIST' INTO w_events.
    IF SY-SUBRC = 0.
      MOVE 'ALV_END_OF_LIST' TO w_events-form.
      MODIFY i_events FROM w_events INDEX SY-TABIX.
    ENDIF.

  ENDFORM.

*&---------------------------------------------------------------------*
*& Report  ZZWEI_ALV_EXAMPLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZZWEI_ALV_EXAMPLE.

TABLES:LFA1,"供应商主数据 (一般地区)
       EKPO,"采购凭证项目
       EINE,"采购信息记录 - 采购组织数据
       EINA."采购信息记录 - 一般数据

INCLUDE <LIST>.
TYPE-POOLS:SLIS.

DATA:i_fieldcat_alv TYPE SLIS_T_FIELDCAT_ALV,"定义列标题(属性信息)
     i_layout TYPE SLIS_LAYOUT_ALV,
     i_fieldcat TYPE SLIS_FIELDCAT_ALV,"定义对象
     i_events TYPE SLIS_T_EVENT,"alv事件
     w_events LIKE LINE OF i_events,
     i_list_comments TYPE SLIS_T_LISTHEADER,"alv表单标题区域设置
     w_list_comments LIKE LINE OF i_list_comments,
     w_repid LIKE SY-REPID."当前程序





*定义内表字段
DATA:BEGIN OF hd_itab OCCURS 0,
      infnr LIKE EINE-INFNR,"采购信息记录
      ekorg LIKE EINE-EKORG,"采购组织
      matnr LIKE EINA-MATNR,"物料编号
      lifnr LIKE EINA-LIFNR,"供应商
      erdat LIKE EINE-ERDAT,"创建日期
      loekz LIKE EINE-LOEKZ,"删除标记
      urznr LIKE EINA-URZNR,"批准号
      color(4) TYPE C, "定义颜色
      aplfz LIKE EINE-APLFZ,"计划交货时间
      netpr LIKE EINE-NETPR,"净价
      peinh LIKE EINE-PEINH,"单位价格
      dwjj LIKE EINE-NETPR,"单位净价
      bprme LIKE EINE-BPRME,"订单价格单位
      maktx LIKE MAKT-MAKTX,"物料名称
      name1 LIKE LFA1-NAME1,"供应商名称
      iicon LIKE ICON-NAME, "ICON 图标
     END OF hd_itab.



*选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE t1.
  SELECT-OPTIONS:s_infnr FOR EINE-INFNR MEMORY ID INF."信息记录号码
  PARAMETERS:PR_LOEKZ TYPE C AS CHECKBOX DEFAULT ''."显示有删除标志的信息记录
  SELECT-OPTIONS:s_ekorg FOR EINE-EKORG MEMORY ID EKO."采购组织
  SELECT-OPTIONS:s_matnr FOR EINA-MATNR MEMORY ID MAT."物料编号
  SELECT-OPTIONS:s_lifnr FOR EINA-LIFNR MEMORY ID LIF."供应商编号
  SELECT-OPTIONS:s_erdat FOR EINA-ERDAT MEMORY ID erd."创建日期
  SELECT-OPTIONS:s_urznr FOR EINA-URZNR MEMORY ID urz."批准号
SELECTION-SCREEN END OF BLOCK block1.



*初始化事件
INITIALIZATION.
      t1 = '采购查询'.


*屏幕开始事件
START-OF-SELECTION.
   PERFORM getdata.
   PERFORM events_build.
   PERFORM layout_build.
   PERFORM fieldcat_build.
   PERFORM alv_display_data.






*&--------------------------------------------------------------------*
*&      Form  getdata
*&--------------------------------------------------------------------*
FORM getdata.
  CLEAR hd_itab.
  CLEAR hd_itab[].

  IF PR_LOEKZ = ''.
    SELECT * FROM EINE INNER JOIN EINA ON EINE~INFNR = EINA~INFNR INTO CORRESPONDING FIELDS OF hd_itab
    WHERE EINE~INFNR IN s_infnr AND EINE~EKORG IN s_ekorg AND EINA~MATNR IN s_matnr AND EINA~LIFNR IN s_lifnr AND EINA~LOEKZ <> 'X' AND EINE~ERDAT IN s_erdat AND EINA~URZNR IN s_urznr
    ORDER BY EINE~INFNR DESCENDING.
      SELECT SINGLE MAKTX FROM MAKT INTO CORRESPONDING FIELDS OF hd_itab WHERE matnr = hd_itab-matnr.
      SELECT SINGLE NAME1 FROM LFA1 INTO CORRESPONDING FIELDS OF hd_itab WHERE lifnr = hd_itab-lifnr.
      APPEND hd_itab.
      CLEAR hd_itab.
    ENDSELECT.
  ELSE.
    SELECT * FROM EINE INNER JOIN EINA ON EINE~INFNR = EINA~INFNR INTO CORRESPONDING FIELDS OF hd_itab
    WHERE EINE~INFNR IN s_infnr AND EINE~EKORG IN s_ekorg AND EINA~MATNR IN s_matnr AND EINA~LIFNR IN s_lifnr AND EINE~ERDAT IN s_erdat AND EINA~URZNR IN s_urznr
    ORDER BY EINE~INFNR DESCENDING.
      SELECT SINGLE MAKTX FROM MAKT INTO CORRESPONDING FIELDS OF hd_itab WHERE matnr = hd_itab-matnr.
      SELECT SINGLE NAME1 FROM LFA1 INTO CORRESPONDING FIELDS OF hd_itab WHERE lifnr = hd_itab-lifnr.
      APPEND hd_itab.
      CLEAR hd_itab.
    ENDSELECT.

 ENDIF.

 LOOP AT hd_itab.
   IF hd_itab-loekz = 'X'.
     hd_itab-color = 'C610'.
     hd_itab-iicon = ICON_INCOMPLETE. "ICON圖標信息
   ELSE.
     hd_itab-iicon = ICON_CHECKED. "ICON圖標信息
   ENDIF.
   hd_itab-dwjj = hd_itab-netpr / hd_itab-peinh.  "净价/价格单位

   MODIFY hd_itab."更新
   CLEAR hd_itab."CLEAR HEADER LINE
 ENDLOOP.
ENDFORM.



*&---------------------------------------------------------------------------*
*&    FORM LAYOUT BUILD
*&---------------------------------------------------------------------------*
FORM layout_build.
  i_layout-ZEBRA = 'X'. "ALV表格按斑马线条纹显示
  "i_layout-NO_VLINE = 'X'. "ALV表格顯示時無堅向線
  i_layout-COLWIDTH_OPTIMIZE = 'X'. "将ALV字段宽度设置为最优化
*  i_layout-detail_initial_lines = 'X'.
*  i_layout-detail_titlebar = '详细内容'.
*  i_layout-box_fieldname  = 'LINE'.
  i_layout-INFO_FIELDNAME = 'COLOR'. "設置顏色FIELD
  i_layout-NO_COLHEAD = ''.
  w_repid = SY-REPID.
ENDFORM.



*&---------------------------------------------------------------------------*
*&    FORM FIELDCAT BUILD
*&---------------------------------------------------------------------------*
FORM fieldcat_build.
  REFRESH i_fieldcat_alv.
  CLEAR i_fieldcat.
  DATA column TYPE I VALUE 1.

  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'INFNR'.
  i_fieldcat-SELTEXT_S = '采购信息记录'.
  i_fieldcat-SELTEXT_M = '采购信息记录'.
  i_fieldcat-SELTEXT_L = '采购信息记录'.
  "i_fieldcat-reptext_ddic = '采购信息记录号'.
  i_fieldcat-LZERO = 'X'. "字义字段前导以"0"的形式显示
  i_fieldcat-KEY = 'X'.
  i_fieldcat-HOTSPOT = 'X'."设置字段是否有热点(热点字段显示有下划线)
  i_fieldcat-EMPHASIZE = 'C300'."设置字段颜色
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'LIFNR'.
  i_fieldcat-SELTEXT_S = '供应商号码'.
  i_fieldcat-SELTEXT_M = '供应商号码'.
  i_fieldcat-SELTEXT_L = '供应商号码'.
  "i_fieldcat-reptext_ddic = '供应商号码'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  i_fieldcat-HOTSPOT = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'NAME1'.
  i_fieldcat-SELTEXT_S = '供应商名称'.
  i_fieldcat-SELTEXT_M = '供应商名称'.
  i_fieldcat-SELTEXT_L = '供应商名称'.
  "i_fieldcat-reptext_ddic = '供应商名称'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'MATNR'.
  i_fieldcat-SELTEXT_S = '物料编号'.
  i_fieldcat-SELTEXT_M = '物料编号'.
  i_fieldcat-SELTEXT_L = '物料编号'.
  "i_fieldcat-reptext_ddic = '物料编号'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'MAKTX'.
  i_fieldcat-SELTEXT_S = '物料名称'.
  i_fieldcat-SELTEXT_M = '物料名称'.
  i_fieldcat-SELTEXT_L = '物料名称'.
  "i_fieldcat-reptext_ddic = '物料名称'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'NETPR'.
  i_fieldcat-SELTEXT_S = '净价'.
  i_fieldcat-SELTEXT_M = '净价'.
  i_fieldcat-SELTEXT_L = '净价'.
  "i_fieldcat-reptext_ddic = '净价'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'PEINH'.
  i_fieldcat-SELTEXT_S = '价格单位'.
  i_fieldcat-SELTEXT_M = '价格单位'.
  i_fieldcat-SELTEXT_L = '价格单位'.
  "i_fieldcat-reptext_ddic = '价格单位'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'DWJJ'.
  i_fieldcat-SELTEXT_S = '单位净价'.
  i_fieldcat-SELTEXT_M = '单位净价'.
  i_fieldcat-SELTEXT_L = '单位净价'.
  "i_fieldcat-reptext_ddic = '单位净价'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'BPRME'.
  i_fieldcat-SELTEXT_S = '计量单位'.
  i_fieldcat-SELTEXT_M = '计量单位'.
  i_fieldcat-SELTEXT_L = '计量单位'.
  "i_fieldcat-reptext_ddic = '计量单位'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'EKORG'.
  i_fieldcat-SELTEXT_S = '采购组织'.
  i_fieldcat-SELTEXT_M = '采购组织'.
  i_fieldcat-SELTEXT_L = '采购组织'.
  "i_fieldcat-reptext_ddic = '采购组织'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'APLFZ'.
  i_fieldcat-SELTEXT_S = '计划交货时间'.
  i_fieldcat-SELTEXT_M = '计划交货时间'.
  i_fieldcat-SELTEXT_L = '计划交货时间'.
  "i_fieldcat-reptext_ddic = '计划交货时间'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  i_fieldcat-Emphasize = 'C211'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'ERDAT'.
  i_fieldcat-SELTEXT_S = '创建日期'.
  i_fieldcat-SELTEXT_M = '创建日期'.
  i_fieldcat-SELTEXT_L = '创建日期'.
  "i_fieldcat-reptext_ddic = '创建日期'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'LOEKZ'.
  i_fieldcat-SELTEXT_S = '删除标志'.
  i_fieldcat-SELTEXT_M = '删除标志'.
  i_fieldcat-SELTEXT_L = '删除标志'.
  "i_fieldcat-reptext_ddic = '删除标志'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'URZNR'.
  i_fieldcat-SELTEXT_S = '批准标记'.
  i_fieldcat-SELTEXT_M = '批准标记'.
  i_fieldcat-SELTEXT_L = '批准标记'.
  "i_fieldcat-reptext_ddic = '批准标记'.
  i_fieldcat-LZERO = 'X'.
  i_fieldcat-KEY = 'X'.
  i_fieldcat-Emphasize = 'C211'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

  column = column   1.
  i_fieldcat-COL_POS = column.
  i_fieldcat-FIELDNAME = 'IICON'.
*  i_fieldcat-SELTEXT_S = '图标'.
*  i_fieldcat-SELTEXT_M = 'ICON'.
  i_fieldcat-SELTEXT_L = 'ICON'.
  i_fieldcat-HOTSPOT = 'X'.
  i_fieldcat-ICON = 'X'.
  "i_fieldcat-reptext_ddic = '批准标记'.
  "i_fieldcat-LZERO = 'X'.
  "i_fieldcat-KEY = 'X'.
  "i_fieldcat-Emphasize = 'C211'.
  APPEND i_fieldcat TO i_fieldcat_alv.
  CLEAR i_fieldcat.

ENDFORM.


*&---------------------------------------------------------------------------*
*&    FORM ALV DISPLAY DATA
*&---------------------------------------------------------------------------*
FORM alv_display_data.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
      I_CALLBACK_PROGRAM                = w_repid
      I_GRID_TITLE                      = '采购信息记录汇总查询'
      IS_LAYOUT                         = i_layout
      IT_FIELDCAT                       = i_fieldcat_alv[]
      IT_EVENTS                         = i_events[]
      "I_SAVE  = 'A'
      I_CALLBACK_USER_COMMAND           = 'USER_COMMAND' "USER_COMMAND EVENT
    TABLES
      T_OUTTAB                          = hd_itab[]
   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.

FORM user_command USING i_ucomm TYPE SY-UCOMM i_selfield TYPE SLIS_SELFIELD.

  CASE i_ucomm.
    WHEN '&IC1'.  "对于热点链接,所对应的动作码为"&IC1"
      IF i_selfield-FIELDNAME EQ 'INFNR'. "判断用户当前单击的是哪列
        PERFORM call_tran USING i_selfield. "调用自定义事务
      ENDIF.
  ENDCASE.
ENDFORM.



FORM call_tran USING i_selfield TYPE SLIS_SELFIELD.
  "SET SCREEN 0.  "返回主屏幕
  READ TABLE hd_itab INDEX i_selfield-tabindex.  "i_selfield-tabindex 当击行索引,从内表中提取数据

  IF SY-SUBRC = 0.
     SET PARAMETER ID 'MAT' FIELD hd_itab-MATNR. "MAT指参数ID
     SET PARAMETER ID 'LIF' FIELD hd_itab-LIFNR.
     SET PARAMETER ID 'EKO' FIELD hd_itab-EKORG.
     SET PARAMETER ID 'INF' FIELD hd_itab-INFNR.

     CALL TRANSACTION 'ME13'."AND SKIP FIRST SCREEN "调用其它事务 ME13是我们要调用的事务
  ENDIF.
ENDFORM.


FORM ALV_TOP_OF_PAGE. "ALV表头信息
  CLEAR:i_list_comments.
  w_list_comments-typ = 'H'.
  w_list_comments-key = ''.
  w_list_comments-info = '采购信息记录查询报表'.
  APPEND w_list_comments TO i_list_comments.
  CLEAR w_list_comments.

  DATA:date_temp(30) TYPE C.
  WRITE: SY-DATUM TO date_temp MM/DD/YYYY.
  CONCATENATE '日期:' date_temp INTO date_temp.
  CONCATENATE date_temp  '.   IT部' INTO date_temp.


  w_list_comments-typ = 'S'.
  w_list_comments-key = ''.
  w_list_comments-info = date_temp.
  APPEND w_list_comments TO i_list_comments.
  CLEAR w_list_comments.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'  "输出注释
    EXPORTING
      IT_LIST_COMMENTARY       = i_list_comments
      I_LOGO                   = 'ENJOYSAP_LOGO'
      I_END_OF_LIST_GRID       = 'X'
*     I_ALV_FORM               =
            .

ENDFORM.

FORM ALV_END_OF_LIST.
  CLEAR: i_list_comments.
*  w_list_comments-typ = 'S'.
*  w_list_comments-info = 'Create By IT DEPARTMENT'.
*  APPEND w_list_comments TO i_list_comments.
*  CLEAR w_list_comments.
*
*  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
*    EXPORTING
*      IT_LIST_COMMENTARY       = i_list_comments
**      I_LOGO                   = 'ENJOYSAP_LOGO'
*      I_END_OF_LIST_GRID       = 'X'
**     I_ALV_FORM               =
*            .

  CLEAR:i_list_comments.
  w_list_comments-typ = 'S'.
  w_list_comments-info = '确认:________采购申请人:___________________'.
  APPEND w_list_comments TO i_list_comments.
  CLEAR w_list_comments.

  w_list_comments-typ = 'S'.
  w_list_comments-info = 'Create By Information Department'.
  APPEND w_list_comments TO i_list_comments.
  CLEAR w_list_comments.
 
  DATA:data_temp(30) TYPE C.
  CONCATENATE '打印者:' SY-UNAME INTO data_temp.
  w_list_comments-typ = 'S'.
  w_list_comments-info = data_temp.
  APPEND w_list_comments TO i_list_comments.
  CLEAR w_list_comments.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY       = i_list_comments
      I_LOGO                   = 'ENJOYSAP_LOGO'
      I_END_OF_LIST_GRID       = 'X'
*     I_ALV_FORM               =
            .

ENDFORM.


FORM events_build.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET' "输出一个内表类型是SLIS_T_EVENT,行项为SLIS_ALV_EVENT,包含两个字段,一个是事件名称,另一个是处理事件的FORM名称 
   EXPORTING
     I_LIST_TYPE           = 0
   IMPORTING
     ET_EVENTS             = i_events
   EXCEPTIONS
     LIST_TYPE_WRONG       = 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.

  READ TABLE i_events WITH KEY NAME = 'TOP_OF_PAGE' INTO w_events.

  IF SY-SUBRC = 0.
    MOVE 'ALV_TOP_OF_PAGE' TO w_events-form.
    MODIFY i_events FROM w_events INDEX SY-TABIX.
  ENDIF.

  READ TABLE i_events INTO w_events WITH KEY NAME = 'USER_COMMAND'.

  IF SY-SUBRC EQ 0.
    w_events-name = 'USER_COMMAND'.
    MODIFY i_events FROM w_events INDEX SY-TABIX.
  ENDIF.

  READ TABLE i_events WITH KEY NAME = 'END_OF_LIST' INTO w_events.

  IF SY-SUBRC = 0.
    MOVE 'ALV_END_OF_LIST' TO w_events-form.
    MODIFY i_events FROM w_events INDEX SY-TABIX.
  ENDIF.

ENDFORM.

0 人点赞