Monday, February 25, 2008

Sample ALV program to display Purchase order data

*&---------------------------------------------------------------------*
*& Report  ZMM_PURCHASE_ORDER
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zmm_purchase_order2.

TABLES: ekko,ekpo.

TYPES: BEGIN OF gty_ekko,
        ebeln TYPE ekko-ebeln,
        bukrs TYPE ekko-bukrs,
        bstyp TYPE ekko-bstyp,
        bsart TYPE ekko-bsart,
        lifnr TYPE ekko-lifnr,
        ekorg TYPE ekko-ekorg,
        ekgrp TYPE ekko-ekgrp,
        waers TYPE ekko-waers,
        kunnr TYPE ekko-kunnr,
        END OF gty_ekko.

TYPES: BEGIN OF gty_ekpo,
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
        loekz TYPE ekpo-loekz,
        matnr TYPE ekpo-matnr,
        bukrs TYPE ekpo-bukrs,
        werks TYPE ekpo-werks,
        lgort TYPE ekpo-lgort,
        matkl TYPE ekpo-matkl,
        menge TYPE ekpo-menge,
        meins TYPE ekpo-meins,
        netpr TYPE ekpo-netpr,
        netwr TYPE ekpo-netwr,
        mwskz TYPE ekpo-mwskz,
      END OF gty_ekpo.

TYPES: BEGIN OF gty_final,
        ebeln TYPE ekko-ebeln,
        bukrs TYPE ekko-bukrs,
        bstyp TYPE ekko-bstyp,
        bsart TYPE ekko-bsart,
        lifnr TYPE ekko-lifnr,
        ekorg TYPE ekko-ekorg,
        ekgrp TYPE ekko-ekgrp,
        waers TYPE ekko-waers,
        kunnr TYPE ekko-kunnr,
        ebelp TYPE ekpo-ebelp,
        loekz TYPE ekpo-loekz,
        matnr TYPE ekpo-matnr,
        werks TYPE ekpo-werks,
        lgort TYPE ekpo-lgort,
        matkl TYPE ekpo-matkl,
        menge TYPE ekpo-menge,
        meins TYPE ekpo-meins,
        netpr TYPE ekpo-netpr,
        netwr TYPE ekpo-netwr,
        mwskz TYPE ekpo-mwskz,
      END OF gty_final.


DATA: gt_ekko  TYPE STANDARD TABLE OF gty_ekko,
      gt_ekpo  TYPE STANDARD TABLE OF gty_ekpo,
      gt_final TYPE STANDARD TABLE OF gty_final.

DATA: gtt_ekko  TYPE gty_ekko,
      gtt_ekpo  TYPE gty_ekpo,
      gtt_final TYPE gty_final.

DATA: g_repid           TYPE sy-repid,
      gt_fieldcatalog   TYPE slis_t_fieldcat_alv ,
      gtt_fieldcatalog  TYPE slis_fieldcat_alv,
      gt_layout         TYPE slis_layout_alv ,
      gt_fieldcatalog2  TYPE slis_t_fieldcat_alv ,
      gtt_fieldcatalog2 TYPE slis_fieldcat_alv,
      gt_layout2        TYPE slis_layout_alv ,
      gt_fieldcatalog3  TYPE slis_t_fieldcat_alv ,
      gtt_fieldcatalog3 TYPE slis_fieldcat_alv,
      gt_layout3        TYPE slis_layout_alv ,
      gt_print          TYPE slis_print_alv,
      gt_sort           TYPE slis_t_sortinfo_alv ,
      gt_filter         TYPE slis_t_filter_alv,
      gt_sp_group       TYPE slis_t_sp_group_alv,
      gt_alv_graphics   TYPE dtc_t_tc,
      gt_excluding      TYPE slis_t_extab ,
      gt_events         TYPE slis_t_event ,
      gt_variant        TYPE disvariant,
      gt_variant2       TYPE disvariant,
      gt_variant3       TYPE disvariant,
      g_var2            TYPE i,
      g_user_command    TYPE slis_formname VALUE 'USER_COMMAND',
      g_status          TYPE slis_formname VALUE 'SET_PF_STATUS'.

* selection screen
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECTION-SCREEN:SKIP 1.
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln OBLIGATORY, " Po number
                s_bukrs FOR ekko-bukrs, " company code
                s_aedat FOR ekko-aedat. " creation date
SELECTION-SCREEN:SKIP 1.
SELECTION-SCREEN: END OF BLOCK b1.

INITIALIZATION.
* do nothing

START-OF-SELECTION.
* fetch  the data from tables
  PERFORM select_data.

END-OF-SELECTION.
* display the data using smartform
  PERFORM display_data.

*&---------------------------------------------------------------------*
*&      Form  SELECT_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <-- nbsp="" p2="" p="" text="">*----------------------------------------------------------------------*
FORM select_data .

  SELECT  ebeln
          bukrs
          bstyp
          bsart
          lifnr
          ekorg
          ekgrp
          waers
          kunnr FROM ekko INTO TABLE gt_ekko
    WHERE ebeln IN s_ebeln
    AND bukrs IN s_bukrs
    AND bstyp = 'F'
    AND aedat IN s_aedat.

  IF sy-subrc = 0.

    SELECT  ebeln
            ebelp
            loekz
            matnr
            bukrs
            werks
            lgort
            matkl
            menge
            meins
            netpr
            netwr
            mwskz FROM ekpo INTO TABLE gt_ekpo
      FOR ALL ENTRIES IN gt_ekko
      WHERE ebeln = gt_ekko-ebeln
       AND bukrs = gt_ekko-bukrs.

    IF sy-subrc = 0.
* do nothing
    ENDIF.

  ENDIF.

  LOOP AT gt_ekpo INTO gtt_ekpo.

    CLEAR gtt_final.
    gtt_final-ebeln = gtt_ekpo-ebeln.
    gtt_final-ebelp = gtt_ekpo-ebelp.
    gtt_final-loekz = gtt_ekpo-loekz.
    gtt_final-matnr = gtt_ekpo-matnr.
    gtt_final-bukrs = gtt_ekpo-bukrs.
    gtt_final-werks = gtt_ekpo-werks.
    gtt_final-lgort = gtt_ekpo-lgort.
    gtt_final-matkl = gtt_ekpo-matkl.
    gtt_final-menge = gtt_ekpo-menge.
    gtt_final-meins = gtt_ekpo-ebeln.
    gtt_final-netpr = gtt_ekpo-netpr.
    gtt_final-netwr = gtt_ekpo-netwr.
    gtt_final-mwskz = gtt_ekpo-mwskz.

    CLEAR gtt_ekko.
    READ TABLE gt_ekko INTO gtt_ekko WITH KEY ebeln = gtt_ekpo-ebeln.
    IF  sy-subrc = 0.
      gtt_final-bstyp = gtt_ekko-bstyp.
      gtt_final-bsart = gtt_ekko-bsart.
      gtt_final-lifnr = gtt_ekko-lifnr.
      gtt_final-ekorg = gtt_ekko-ekorg.
      gtt_final-ekgrp = gtt_ekko-ekgrp.
      gtt_final-waers = gtt_ekko-waers.
      gtt_final-kunnr = gtt_ekko-kunnr.
    ENDIF.

    APPEND gtt_final TO gt_final.

  ENDLOOP.

ENDFORM.                    " SELECT_DATA



*&---------------------------------------------------------------------*
*&      Form  DISPLAY_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <-- nbsp="" p2="" p="" text="">*----------------------------------------------------------------------*
FORM display_data .

  set PF-STATUS 'ZSTATUS'.

  REFRESH gt_fieldcatalog.

  g_repid = sy-repid.

  gtt_fieldcatalog-fieldname   = 'EBELN'.
  gtt_fieldcatalog-seltext_m   = 'Po number'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 12.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'EBELP'.
  gtt_fieldcatalog-seltext_m   = 'Item no'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 8.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'BUKRS'.
  gtt_fieldcatalog-seltext_m   = 'Comp code'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 10.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'MATNR'.
  gtt_fieldcatalog-seltext_m   = 'Material'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 18.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'NETPR'.
  gtt_fieldcatalog-seltext_m   = 'Price'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 8.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'WAERS'.
  gtt_fieldcatalog-seltext_m   = 'Currency'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 8.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'MEINS'.
  gtt_fieldcatalog-seltext_m   = 'Unit'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 8.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'WERKS'.
  gtt_fieldcatalog-seltext_m   = 'Plant'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 8.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'EKGRP'.
  gtt_fieldcatalog-seltext_m   = 'Pur  group'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 8.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'LIFNR'.
  gtt_fieldcatalog-seltext_m   = 'Vendor'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 10.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.

  gtt_fieldcatalog-fieldname   = 'KUNNR'.
  gtt_fieldcatalog-seltext_m   = 'Customer'.
  gtt_fieldcatalog-col_pos     = g_var2 + 1.
  gtt_fieldcatalog-outputlen   = 10.
  APPEND gtt_fieldcatalog TO gt_fieldcatalog.
  CLEAR  gtt_fieldcatalog.



  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = g_repid
*      i_callback_pf_status_set = ' '
*      i_callback_user_command  = g_user_command
      is_layout                = gt_layout
      it_fieldcat              = gt_fieldcatalog[]
*      it_special_groups        = gt_sp_group[]
      it_sort                  = gt_sort[]
      it_filter                = gt_filter[]
      i_save                   = 'A'
*      is_variant               = gt_variant
      it_events                = gt_events[]
*      is_print                 = gt_print
*      it_alv_graphics          = gt_alv_graphics[]
*      it_excluding             = gt_excluding
    TABLES
      t_outtab                 = gt_final
    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.                    " DISPLAY_DATA

No comments: