Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

alv

Former Member
0 Likes
827

dear fiends,

i hav a scenario,

for every vbeln, monthly subtotals have to be displayed

like for example

vbeln st.month end.month date amount

..........................................................................................

0011 jan may feb-10-2007 10

feb-14-07 40

feb-15-07 10

Monthly subtotal 60

mar-11-07 30

mar-20-07 40

Monthly subtotal 70

0022 march nov apr-10-2007 10

jun-14-07 20

oct-15-07 20

Monthly subtotal 50

.

.

.

.

.

.

i need to do this using alv,

can some one suggest me what kind of alv should i use,

like alv grid or heirarchical,

i do not want to use oops concept here, bcos its complex.

thanks.

4 REPLIES 4
Read only

praveen_hannu
Contributor
0 Likes
770

Hi Sanjana

You can achieve this through either list or grid, but u need to fill the

sort table. Specify the the month name in a separate field, dont show that

in the output list passing the value 'X' for no_out in the fieldcatalog.

while building the sorting table, pass subtot field value as 'X'. but you should not forget

do the sum on the amount field passing the value 'X' for the field do_sum in the fieldcatalog.

let me know if u need any clarifiacation on this.

Regards

Praveen

Read only

Former Member
0 Likes
770

hi sanjana,

U need to maintain sortinfo table

w_sortinfo TYPE slis_sortinfo_alv, (work area)

t_sortinfo TYPE slis_t_sortinfo_alv, (internal table)

  • And

w_sortinfo-fieldname = 'VBELN'.

w_sortinfo-subtot = 'X'.

APPEND w_sortinfo TO t_sortinfo.

give this t_sortinfo in the REUSE_ALV_GRID_DISPLAY

  • And in fieldcatalog maintain this field

w_fieldcat-do_sum = 'X'.

hope this solves ur problem

regards

shashikanth naram

Read only

Former Member
0 Likes
770

Hi,

Check this.

• do_sum (sum over column)

value set: SPACE, 'X'

'X' = a sum is to be calculated over this internal output table field.

This function can also be called by the user interactively.

• no_sum (sums forbidden)

value set: SPACE, 'X'

'X' = no sum can be calculated over this field, although the data type of the field would allow summing.

Hope this helps.

Regards,

Ramya

Read only

Former Member
0 Likes
770

Hi,

see this code.


*----------------------------------------------------------------------*
* Program Name: ZVLCOPRD
*
* Description : Co-Product Contract vs. Actual
* Transaction Code: ZCOP
*----------------------------------------------------------------------*
REPORT zvlcoprd NO STANDARD PAGE HEADING
                LINE-SIZE 200
                LINE-COUNT 65.

TABLES: kna1,                                               "JC07072004
        vbak,
        vbap,
        vbuk,
        vbfa,
        likp,
        lips,
        t000.

DATA: BEGIN OF icontract OCCURS 0,
      matnr LIKE vbap-matnr,
      kunnr LIKE vbak-kunnr,
      vbeln LIKE vbap-vbeln,
      posnr LIKE vbap-posnr,
      guebg LIKE vbak-guebg,
      gueen LIKE vbak-gueen,
      zmeng LIKE vbap-zmeng,
      zieme LIKE vbap-zieme,
      ntgew LIKE vbap-ntgew,
      gewei LIKE vbap-gewei,
END OF icontract.

DATA: BEGIN OF ivbfa OCCURS 0,
      vbeln LIKE vbfa-vbeln,
      posnn LIKE vbfa-posnn,
      vbtyp_n LIKE vbfa-vbtyp_n,                            "JC04032007
END OF ivbfa.

DATA: BEGIN OF itab OCCURS 0,
      matnr LIKE vbap-matnr,           "material
      kunnr LIKE vbak-kunnr,           "sold-to
      name1 like kna1-name1,           "sold-to-party description
      guebg LIKE vbak-guebg,           "start date
      gueen LIKE vbak-gueen,           "end date
      cqtys LIKE vbap-zmeng,           "contract qty in sales UoM
      cuoms LIKE vbap-zieme,           "sales UoM
      cqtyb LIKE vbap-ntgew,           "contract qty in base UoM
      cuomb LIKE vbap-gewei,           "base UoM
      vbeln LIKE vbak-vbeln,           "contract #
      wadat_mon(6),                    "PGI month
      wadat_ist LIKE likp-wadat_ist,   "PGI date
      traid LIKE likp-traid,           "Means of Transport ID"JC04032007
      order LIKE vbfa-vbeln,           "order
      abgru LIKE vbap-abgru,           "reason of rejection
      vdatu LIKE vbak-vdatu,           "requested delivery date
      month(2),
      oqtys LIKE vbap-kwmeng,          "order qty in sales UoM
      ouoms LIKE vbap-vrkme,           "sales UoM
      oqtyb LIKE vbap-ntgew,           "order qty in base UoM
      ouomb LIKE vbap-gewei,           "base UoM
      pqtys LIKE lips-lfimg,           "PGI qty in sales UoM
      puoms LIKE lips-vrkme,           "sales UoM
      pqtyb LIKE lips-ntgew,           "PGI qty in base UoM
      puomb LIKE lips-gewei,           "base UoM
END OF itab.

DATA: BEGIN OF xltab OCCURS 0,
      vbeln LIKE vbak-vbeln,           "contract #
      cqty LIKE vbap-ntgew,            "contract qty
      cuom LIKE vbap-zieme,            "UoM
      oqty LIKE vbap-kwmeng,           "order qty in sales UoM
      ouom LIKE vbap-vrkme,            "sales UoM
      pqty LIKE lips-ntgew,           "PGI qty
      puom LIKE lips-gewei,           "base UoM
END OF xltab.
DATA: i_xltab LIKE xltab OCCURS 0.

DATA: BEGIN OF t_fname OCCURS 20,
        fname(40),
END OF t_fname.

DATA: BEGIN OF iexcel OCCURS 0,
      col1(18),
      col2(18),
      vbeln(10),
      guebg(10),
      gueen(10),
      cqty LIKE vbap-ntgew,            "contract qty
      cuom(3),                         "sales UoM
      order(10),
      vdatu(12),
      oqty LIKE vbap-kwmeng,           "order qty
      ouom(3),                         "sales UoM
      wadat_ist(10),
      pqty LIKE lips-lfimg,            "PGI qty
      puom(3),                         "sales UoM
      traid(20),                       "TransID             "JC04032007
END OF iexcel.

DATA: toqtys LIKE vbap-kwmeng,
      toqtyb LIKE vbap-ntgew,
      tpqtys LIKE lips-lfimg,
      tpqtyb LIKE lips-ntgew,
      v_ouoms LIKE vbap-vrkme,
      v_ouomb LIKE vbap-gewei,
      v_puoms LIKE lips-vrkme,
      v_puomb LIKE lips-gewei,
      goqtys LIKE vbap-kwmeng,
      goqtyb LIKE vbap-ntgew,
      gpqtys LIKE lips-lfimg,
      gpqtyb LIKE lips-ntgew,
      v_newline,
      v_print_custname.                                     "JC07072004

* JC01312007
DATA: BEGIN OF s_fnam,                    " Structure for field names
        f01_name(30),
        f02_name(30),
        f03_name(30),
        f04_name(30),
        f05_name(30),
        f06_name(30),
        f07_name(30),
      END OF s_fnam.
FIELD-SYMBOLS: <fs1> TYPE STANDARD TABLE.
FIELD-SYMBOLS: <fs2> TYPE STANDARD TABLE.
DATA: i_fnam LIKE s_fnam OCCURS 0,
      v_filename LIKE rlgrap-filename.

TYPE-POOLS:slis.

DATA:fcat TYPE SLIS_T_FIELDCAT_ALV,         "field catlog table type
     afield type slis_fieldcat_alv,         "field catlog line type
     layo TYPE slis_layout_alv,             "layout
     lt_sort TYPE slis_sortinfo_alv,        "sorting and  subtotals
     p_sort TYPE SLIS_T_SORTINFO_ALV,
     head TYPE slis_t_listheader,
     wa_header type slis_listheader,
     col_pos type sy-cucol.                 "column position variable

CONSTANTS:c_y VALUE 'X',
          c_n VALUE ' '.

SELECTION-SCREEN BEGIN OF BLOCK blok1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_vkorg FOR vbak-vkorg," OBLIGATORY,
                s_vtweg FOR vbak-vtweg,
                s_kunnr FOR vbak-kunnr,
                s_matnr FOR vbap-matnr,
                s_werks FOR vbap-werks,
                s_vbeln FOR vbak-vbeln,
                s_condt FOR vbak-guebg NO-EXTENSION." OBLIGATORY.
SELECTION-SCREEN END OF BLOCK blok1.
SELECTION-SCREEN BEGIN OF BLOCK blok2 WITH FRAME TITLE text-002.
PARAMETERS: p_mat RADIOBUTTON GROUP rsrt,
            p_cst RADIOBUTTON GROUP rsrt.
SELECTION-SCREEN END OF BLOCK blok2.
SELECTION-SCREEN BEGIN OF BLOCK blok3 WITH FRAME TITLE text-003.
PARAMETERS: p_opncon AS CHECKBOX,
            p_subtot AS CHECKBOX default 'X'.
SELECTION-SCREEN SKIP.
PARAMETERS: p_sale RADIOBUTTON GROUP ruom,
            p_base RADIOBUTTON GROUP ruom.
SELECTION-SCREEN END OF BLOCK blok3.

AT SELECTION-SCREEN.
*  IF s_condt-high IS INITIAL.
*    MESSAGE e208(00) WITH
*            'Please enter upper range value of contract date'.
*  ENDIF.

INITIALIZATION.
*  IF sy-tcode NE 'ZCOP'.
*    MESSAGE a208(00) WITH
*            'This program must be executed as transaction ZCOP'.
*  ENDIF.

START-OF-SELECTION.
  PERFORM start-of-selection.

END-OF-SELECTION.
  PERFORM end-of-selection.

AT USER-COMMAND.
  CASE sy-ucomm.
    WHEN 'EXCL'.     "Detail download
      PERFORM download_to_excel.
    WHEN 'DLOD'.     "Summary download
      PERFORM download_detail.
  ENDCASE.

*&---------------------------------------------------------------------*
*&      Form  START-OF-SELECTION
*&---------------------------------------------------------------------*
FORM start-of-selection.

* Select and load contract data into ICONTRACT internal table
  REFRESH icontract.
  IF NOT p_opncon IS INITIAL.
*   Open contracts only
    SELECT vbap~matnr vbak~kunnr vbap~vbeln vbap~posnr
           vbak~guebg vbak~gueen vbap~zmeng vbap~zieme
           vbap~ntgew vbap~gewei
      INTO TABLE icontract
      FROM vbap
      JOIN vbak ON ( vbap~vbeln = vbak~vbeln )
      JOIN vbuk ON ( vbap~vbeln = vbuk~vbeln )
     WHERE vbak~vbeln IN s_vbeln
       AND vbak~vbtyp = 'G'
       AND vbak~auart = 'KM'           "Contract (CQ)
       AND vbak~vkorg IN s_vkorg
       AND vbak~vtweg IN s_vtweg
       AND vbak~kunnr IN s_kunnr
       AND ( vbak~guebg IN s_condt OR vbak~gueen IN s_condt OR
            ( vbak~guebg <= s_condt-low AND
             vbak~gueen >= s_condt-high ) )
       AND vbap~matnr IN s_matnr
       AND vbuk~gbstk NE 'C'.
  ELSE.
*   All contracts
    SELECT vbap~matnr vbak~kunnr vbap~vbeln vbap~posnr
           vbak~guebg vbak~gueen vbap~zmeng vbap~zieme
           vbap~ntgew vbap~gewei
      INTO TABLE icontract
      FROM vbap
      JOIN vbak ON ( vbap~vbeln = vbak~vbeln )
     WHERE vbak~vbeln IN s_vbeln
       AND vbak~vbtyp = 'G'
       AND vbak~auart = 'KM'           "Contract (CQ)
       AND vbak~vkorg IN s_vkorg
       AND vbak~vtweg IN s_vtweg
       AND vbak~kunnr IN s_kunnr
       AND ( vbak~guebg IN s_condt OR vbak~gueen IN s_condt OR
             ( vbak~guebg <= s_condt-low AND
             vbak~gueen >= s_condt-high ) )
       AND vbap~matnr IN s_matnr.
  ENDIF.
  IF sy-subrc NE 0.
    MESSAGE i208(00) WITH 'No data found'.
    LEAVE LIST-PROCESSING.
  ENDIF.

ENDFORM.                               " START-OF-SELECTION

*&---------------------------------------------------------------------*
*&      Form  END-OF-SELECTION
*&---------------------------------------------------------------------*
FORM end-of-selection.

  SET PF-STATUS 'MAIN'.

  REFRESH: itab.
  LOOP AT icontract.

*   Get order info
    REFRESH ivbfa.
    SELECT vbeln posnn vbtyp_n INTO TABLE ivbfa
      FROM vbfa
     WHERE vbelv = icontract-vbeln
       AND posnv = icontract-posnr
       AND ( vbtyp_n = 'C' OR vbtyp_n = 'H' ).          "Order & Return
                                                            "JC04032007
*  JC01312007
    IF sy-subrc NE 0.

*     No sales order history
*     Loading itab
      CLEAR itab.
      itab-matnr = icontract-matnr.
      itab-kunnr = icontract-kunnr.
      itab-vbeln = icontract-vbeln.
      itab-guebg = icontract-guebg.
      itab-gueen = icontract-gueen.
      itab-cqtys = icontract-zmeng.
      itab-cuoms = icontract-zieme.
      itab-cqtyb = icontract-ntgew.
      itab-cuomb = icontract-gewei.
      CLEAR: itab-order, itab-abgru, itab-vdatu,
             itab-oqtys, itab-ouoms, itab-oqtyb,
             itab-ouomb, itab-wadat_ist, itab-traid,        "JC04032007
             itab-wadat_mon,
             itab-pqtys, itab-puoms, itab-pqtyb, itab-puomb.
      APPEND itab.
      CLEAR itab.

    ELSE.

*    CHECK sy-subrc EQ 0.                                    "JC01312007
      LOOP AT ivbfa.

*     Get requested delivery date
        CLEAR vbak-vdatu.
        SELECT SINGLE vdatu INTO vbak-vdatu FROM vbak
         WHERE vbeln = ivbfa-vbeln.
*     Get rejection code and order qty in sales and base UoM
        CLEAR: vbap-kwmeng, vbap-vrkme, vbap-ntgew, vbap-gewei,
               vbap-abgru.
        SELECT SINGLE kwmeng vrkme ntgew gewei abgru
           INTO (vbap-kwmeng, vbap-vrkme, vbap-ntgew, vbap-gewei,
                 vbap-abgru)
           FROM vbap
          WHERE vbeln = ivbfa-vbeln
            AND posnr = ivbfa-posnn
            AND werks IN s_werks.
        CHECK sy-subrc EQ 0.

*     Get delivery info
        CLEAR: vbfa-vbeln, vbfa-posnn.
        IF ivbfa-vbtyp_n EQ 'C'.                            "JC04032007
          SELECT vbeln posnn INTO (vbfa-vbeln, vbfa-posnn) UP TO 1 ROWS
            FROM vbfa
           WHERE vbelv = ivbfa-vbeln
             AND posnv = ivbfa-posnn
             AND vbtyp_n = 'J'.            "Delivery
          ENDSELECT.
* JC04032007
        ELSEIF ivbfa-vbtyp_n EQ 'H'.
          SELECT vbeln posnn INTO (vbfa-vbeln, vbfa-posnn) UP TO 1 ROWS
            FROM vbfa
           WHERE vbelv = ivbfa-vbeln
             AND posnv = ivbfa-posnn
             AND vbtyp_n = 'T'.            "Return Delivery for Order
          ENDSELECT.
        ENDIF.

        IF sy-subrc EQ 0.
*       If delivery found
*       Get PGI date
          CLEAR: likp-wadat_ist, likp-traid.                "JC04032007
          SELECT SINGLE wadat_ist traid                     "JC04032007
            INTO (likp-wadat_ist, likp-traid) FROM likp     "JC0403200 
           WHERE vbeln = vbfa-vbeln.

*       Get PGI qty in sales and base UoM
          CLEAR: lips-lfimg, lips-vrkme, lips-ntgew, lips-gewei.
          SELECT SINGLE lfimg vrkme ntgew gewei
            INTO (lips-lfimg, lips-vrkme, lips-ntgew, lips-gewei)
            FROM lips
           WHERE vbeln = vbfa-vbeln
             AND posnr = vbfa-posnn.
        ELSE.
*       If delivery not found
          CLEAR: likp-wadat_ist, likp-traid,                "JC04032007
          lips-lfimg, lips-vrkme, lips-ntgew, lips-gewei.
        ENDIF.

*     Loading itab
        CLEAR itab.
        itab-matnr = icontract-matnr.
        itab-kunnr = icontract-kunnr.
        itab-vbeln = icontract-vbeln.
        itab-guebg = icontract-guebg.
        itab-gueen = icontract-gueen.
        itab-cqtys = icontract-zmeng.
        itab-cuoms = icontract-zieme.
        itab-cqtyb = icontract-ntgew.
        itab-cuomb = icontract-gewei.
        itab-order = ivbfa-vbeln.
        itab-abgru = vbap-abgru.
        itab-vdatu = vbak-vdatu.
        itab-oqtys = vbap-kwmeng.
        itab-ouoms = vbap-vrkme.
        itab-oqtyb = vbap-ntgew.
        itab-ouomb = vbap-gewei.
        IF ivbfa-vbtyp_n EQ 'H'.
          itab-oqtys = itab-oqtys * -1.                     "JC04032007
          itab-oqtyb = itab-oqtyb * -1.                     "JC04032007
        ENDIF.
        itab-wadat_ist = likp-wadat_ist.
        itab-traid = likp-traid.                            "JC04032007
        IF NOT p_subtot IS INITIAL.
          itab-wadat_mon = likp-wadat_ist+0(6).
        ELSE.
          CLEAR itab-wadat_mon.
        ENDIF.
        IF likp-wadat_ist IS INITIAL.
          CLEAR: itab-pqtys, itab-puoms, itab-pqtyb, itab-puomb.
        ELSE.
          itab-pqtys = lips-lfimg.
          itab-puoms = lips-vrkme.
          itab-pqtyb = lips-ntgew.
          itab-puomb = lips-gewei.
          IF ivbfa-vbtyp_n EQ 'H'.
            itab-pqtys = itab-pqtys * -1.                   "JC04032007
            itab-pqtyb = itab-pqtyb * -1.                   "JC04032007
          ENDIF.
        ENDIF.
        APPEND itab.
        CLEAR itab.

      ENDLOOP.
    ENDIF.

  ENDLOOP.

  CASE 'X'.
    WHEN p_mat.
      IF p_subtot IS INITIAL.
        SORT itab BY matnr kunnr vbeln wadat_ist order.
      ELSE.
        SORT itab BY matnr kunnr vbeln wadat_mon wadat_ist order.
      ENDIF.
    WHEN p_cst.
      IF p_subtot IS INITIAL.
        SORT itab BY kunnr matnr vbeln wadat_ist order.
      ELSE.
        SORT itab BY kunnr matnr vbeln wadat_mon wadat_ist order.
      ENDIF.
  ENDCASE.

  PERFORM print-report.

ENDFORM.                               " END-OF-SELECTION

*&---------------------------------------------------------------------*
*&      Form  PRINT-REPORT
*&---------------------------------------------------------------------*
FORM print-report.

  CLEAR: toqtys, toqtyb, tpqtys, tpqtyb,
         goqtys, goqtyb, gpqtys, gpqtyb,
         v_ouoms, v_ouomb, v_puoms, v_puomb.

  REFRESH: xltab.
  CLEAR: xltab.

    LOOP AT itab.
      AT NEW vbeln.
        xltab-vbeln = itab-vbeln.
        IF NOT p_sale IS INITIAL.
          xltab-cqty = itab-cqtys.
          xltab-cuom = itab-cuoms.
        ELSEIF NOT p_base IS INITIAL.
          xltab-cqty = itab-cqtyb.
          xltab-cuom = itab-cuomb.
        ENDIF.
      ENDAT.

      IF itab-abgru <> '99'.
        clear itab-abgru.
        MODIFY itab.
      ENDIF.

      IF NOT p_sale IS INITIAL.
        MOVE itab-ouoms TO v_ouoms.
      ELSEIF NOT p_base IS INITIAL.
        MOVE itab-ouomb TO v_ouomb.
      ENDIF.

      IF NOT p_sale IS INITIAL.
        IF NOT itab-pqtys IS INITIAL.
          MOVE itab-puoms TO v_puoms.
        ENDIF.
      ELSEIF NOT p_base IS INITIAL.
        IF NOT itab-pqtyb IS INITIAL.
          MOVE itab-puomb TO v_puomb.
        ENDIF.
      ENDIF.

      AT END OF vbeln.
        IF NOT p_sale IS INITIAL.
          xltab-oqty = goqtys.
          xltab-ouom = v_ouoms.
          IF NOT gpqtys IS INITIAL.
            xltab-pqty = gpqtys.                            "JC04032007
            xltab-puom = v_puoms.                           "JC04032007
          ENDIF.
        ELSEIF NOT p_base IS INITIAL.
          xltab-oqty = goqtyb.
          xltab-ouom = v_ouomb.
          IF NOT gpqtyb IS INITIAL.
            xltab-pqty = gpqtyb.                            "JC04032007
            xltab-puom = v_puomb.                           "JC04032007
          ENDIF.
        ENDIF.
        APPEND xltab.
        CLEAR xltab.
      ENDAT.

    ENDLOOP.

    PERFORM create_fcat.
    PERFORM sort_grid.
    PERFORM fill_data.
    layo-cell_merge = 'X'.
    layo-no_vline = 'X'.
    layo-no_hline = 'X'.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
*       I_INTERFACE_CHECK                 = ' '
*       I_BYPASSING_BUFFER                = ' '
*       I_BUFFER_ACTIVE                   = ' '
       I_CALLBACK_PROGRAM                = sy-repid
*       I_CALLBACK_PF_STATUS_SET          = ' '
*       I_CALLBACK_USER_COMMAND           = ' '
       I_CALLBACK_TOP_OF_PAGE            = 'TOP_OF_PAGE'
*       I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*       I_CALLBACK_HTML_END_OF_LIST       = ' '
*       I_STRUCTURE_NAME                  =
*       I_BACKGROUND_ID                   = ' '
*       I_GRID_TITLE                      =
*       I_GRID_SETTINGS                   =
       IS_LAYOUT                         = layo
       IT_FIELDCAT                       = fcat[]
*       IT_EXCLUDING                      =
*       IT_SPECIAL_GROUPS                 =
       IT_SORT                           = p_sort[]
*       IT_FILTER                         =
*       IS_SEL_HIDE                       =
*       I_DEFAULT                         = 'X'
*       I_SAVE                            = ' '
*       IS_VARIANT                        =
*       IT_EVENTS                         =
*       IT_EVENT_EXIT                     =
*       IS_PRINT                          =
*       IS_REPREP_ID                      =
*       I_SCREEN_START_COLUMN             = 0
*       I_SCREEN_START_LINE               = 0
*       I_SCREEN_END_COLUMN               = 0
*       I_SCREEN_END_LINE                 = 0
*       I_HTML_HEIGHT_TOP                 = 0
*       I_HTML_HEIGHT_END                 = 0
*       IT_ALV_GRAPHICS                   =
*       IT_HYPERLINK                      =
*       IT_ADD_FIELDCAT                   =
*       IT_EXCEPT_QINFO                   =
*       IR_SALV_FULLSCREEN_ADAPTER        =
*     IMPORTING
*       E_EXIT_CAUSED_BY_CALLER           =
*       ES_EXIT_CAUSED_BY_USER            =
      TABLES
        T_OUTTAB                          = 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.                               " PRINT-REPORT

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_TO_EXCEL
*&---------------------------------------------------------------------*
FORM download_to_excel.

  REFRESH t_fname.
  CLEAR t_fname.
  IF NOT p_mat IS INITIAL.
    t_fname-fname = 'Material'. APPEND t_fname.
    t_fname-fname = 'Sold-To'. APPEND t_fname.
  ELSEIF NOT p_cst IS INITIAL.
    t_fname-fname = 'Sold-To'. APPEND t_fname.
    t_fname-fname = 'Material'. APPEND t_fname.
  ENDIF.
  t_fname-fname = 'Contract'. APPEND t_fname.
  t_fname-fname = 'Start Date'. APPEND t_fname.
  t_fname-fname = 'End Date'. APPEND t_fname.
  t_fname-fname = 'Contract Quantity'. APPEND t_fname.
  t_fname-fname = 'UoM'. APPEND t_fname.
  t_fname-fname = 'Order'. APPEND t_fname.
  t_fname-fname = 'Req.Delv.Dt.'. APPEND t_fname.
  t_fname-fname = 'Order Quantity'. APPEND t_fname.
  t_fname-fname = 'UoM'. APPEND t_fname.
  t_fname-fname = 'PGI Date'. APPEND t_fname.
  t_fname-fname = 'PGI Quantity'. APPEND t_fname.
  t_fname-fname = 'UoM'. APPEND t_fname.
  t_fname-fname = 'TransID'. APPEND t_fname.

  REFRESH iexcel.
  CLEAR iexcel.

  LOOP AT itab.
    IF NOT p_mat IS INITIAL.
      iexcel-col1 = itab-matnr.
      iexcel-col2 = itab-kunnr.
    ELSEIF NOT p_cst IS INITIAL.
      iexcel-col1 = itab-kunnr.
      iexcel-col2 = itab-matnr.
    ENDIF.
    iexcel-vbeln = itab-vbeln.
    CONCATENATE itab-guebg+4(2) '/' itab-guebg+6(2) '/'
                itab-guebg+0(4) INTO iexcel-guebg.
    CONCATENATE itab-gueen+4(2) '/' itab-gueen+6(2) '/'
                itab-gueen+0(4) INTO iexcel-gueen.
    IF NOT p_sale IS INITIAL.
      iexcel-cqty = itab-cqtys.
      iexcel-cuom = itab-cuoms.
    ELSEIF NOT p_base IS INITIAL.
      iexcel-cqty = itab-cqtyb.
      iexcel-cuom = itab-cuomb.
    ENDIF.
    iexcel-order = itab-order.
    CONCATENATE itab-vdatu+4(2) '/' itab-vdatu+6(2) '/'
                itab-vdatu+0(4) INTO iexcel-vdatu.
    IF NOT p_sale IS INITIAL.
      iexcel-oqty = itab-oqtys.
      iexcel-ouom = itab-ouoms.
    ELSEIF NOT p_base IS INITIAL.
      iexcel-oqty = itab-oqtyb.
      iexcel-ouom = itab-ouomb.
    ENDIF.
    IF NOT itab-wadat_ist IS INITIAL.
      CONCATENATE itab-wadat_ist+4(2) '/' itab-wadat_ist+6(2) '/'
                  itab-wadat_ist+0(4) INTO iexcel-wadat_ist.
    ELSE.
      CLEAR iexcel-wadat_ist.
    ENDIF.
    IF NOT p_sale IS INITIAL.
      iexcel-pqty = itab-pqtys.
      iexcel-puom = itab-puoms.
    ELSEIF NOT p_base IS INITIAL.
      iexcel-pqty = itab-pqtyb.
      iexcel-puom = itab-puomb.
    ENDIF.
    iexcel-traid = itab-traid.                              "JC04032007
    APPEND iexcel.
    CLEAR iexcel.
  ENDLOOP.

  CALL FUNCTION 'DOWNLOAD'
    EXPORTING
      filetype            = 'DAT'
      filetype_no_show    = 'X'
    TABLES
      data_tab            = iexcel
      fieldnames          = t_fname
    EXCEPTIONS
      invalid_filesize    = 1
      invalid_table_width = 2
      invalid_type        = 3
      no_batch            = 4
      unknown_error       = 5
      OTHERS              = 6.

  IF sy-subrc NE 0.
    MESSAGE e899(id) WITH 'Download failed - ' sy-subrc.
  ENDIF.

ENDFORM.                               " DOWNLOAD_TO_EXCEL

*&---------------------------------------------------------------------*
*&      Form  download_detail
*&---------------------------------------------------------------------*
FORM download_detail .

  REFRESH i_fnam.
  CLEAR i_fnam.
  s_fnam-f01_name = 'Contract'.
  s_fnam-f02_name = 'Contract Qty'.
  s_fnam-f03_name = 'Contract UoM'.
  s_fnam-f04_name = 'Order Qty'.
  s_fnam-f05_name = 'Order UoM'.
  s_fnam-f06_name = 'PGI Qty'.                              "JC04032007
  s_fnam-f07_name = 'PGI UoM'.                              "JC04032007
  APPEND s_fnam TO i_fnam.
  ASSIGN i_fnam TO <fs1>.

  i_xltab[] = xltab[].
  ASSIGN i_xltab TO <fs2>.
  PERFORM file_download_process.

  IF sy-subrc = 0.
    SKIP 5.
    WRITE:/010 'File Successfully downloaded at:',
                v_filename COLOR COL_BACKGROUND INTENSIFIED OFF.
  ENDIF.
ENDFORM.                    " download_detail

*&---------------------------------------------------------------------*
*&      Form  file_download_process
*&---------------------------------------------------------------------*
FORM file_download_process .

  DATA : ucode_filename TYPE string,
         ucode_path TYPE string,
         ucode_fullpath TYPE string,
         user_action TYPE i.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title = 'Download to Excel'
    CHANGING
      filename     = ucode_filename
      path         = ucode_path
      fullpath     = ucode_fullpath
      user_action  = user_action
    EXCEPTIONS
      OTHERS       = 4.

  IF sy-subrc NE 0.
    MESSAGE e899(id) WITH 'Download failed - ' sy-subrc.
  ENDIF.

  IF user_action <> cl_gui_frontend_services=>action_ok.
    EXIT.
  ENDIF.

  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename                = ucode_fullpath
      filetype                = 'DAT'
    CHANGING
      data_tab                = <fs1>
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      not_supported_by_gui    = 22
      error_no_gui            = 23
      OTHERS                  = 24.

  IF sy-subrc <> 0.
    MESSAGE 'Download failed ! ' TYPE 'E'.
  ELSE.
    CALL METHOD cl_gui_frontend_services=>gui_download
      EXPORTING
        filename                = ucode_fullpath
        filetype                = 'DAT'
        append                  = 'X'
      CHANGING
        data_tab               = <fs2>
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        not_supported_by_gui    = 22
        error_no_gui            = 23
        OTHERS                  = 24.
    IF sy-subrc NE 0.
      MESSAGE e899(id) WITH 'Download failed - ' sy-subrc.
    ENDIF.
  ENDIF.
  v_filename = ucode_fullpath.

ENDFORM.                    " file_download_process

*&---------------------------------------------------------------------*
*&      Form  CREATE_FCAT
*&---------------------------------------------------------------------*
FORM CREATE_FCAT .
  IF NOT p_mat IS INITIAL.
    perform write_fcat using 'MATNR' 'ITAB' 'VBAP' c_n 'Material' c_n c_n.
    perform write_fcat using 'KUNNR' 'ITAB' 'VBAP' c_n 'Sold-To' c_n c_n.
    perform write_fcat using 'NAME1' 'ITAB' 'KNA1' c_n 'Desc' c_n c_n.
  ELSEIF NOT p_cst IS INITIAL.
    perform write_fcat using 'KUNNR' 'ITAB' 'VBAP' c_n 'Sold-To' c_n c_n.
    perform write_fcat using 'NAME1' 'ITAB' 'KNA1' c_n 'Desc' c_n c_n.
    perform write_fcat using 'MATNR' 'ITAB' 'VBAP' c_n 'Material' c_n c_n.
  ENDIF.
  perform write_fcat using 'VBELN' 'ITAB' 'VBAK' c_n 'Contract' c_n c_n.
  perform write_fcat using 'GUEBG' 'ITAB' 'VBAK' c_n 'Start Date' c_n c_n.
  perform write_fcat using 'GUEEN' 'ITAB' 'VBAK' c_n 'End Date' c_n c_n.
  IF NOT p_sale IS INITIAL.
    perform write_fcat using 'CQTYS' 'ITAB' c_n 'CUOMS' 'Contract Qty-Sales' c_n c_n.
    perform write_fcat using 'CUOMS' 'ITAB' c_n c_n 'UoM' c_n c_n.
  ELSEIF NOT p_base IS INITIAL.
    perform write_fcat using 'CQTYB' 'ITAB' c_n 'CUOMB' 'Contract Qty-Base' c_n c_n.
    perform write_fcat using 'CUOMB' 'ITAB' c_n c_n 'UoM' c_n c_n.
  ENDIF.
  perform write_fcat using 'ORDER' 'ITAB' c_n c_n 'Order' c_n c_n.
  perform write_fcat using 'ABGRU' 'ITAB' 'VBAP' c_n ' ' c_n c_n.
  perform write_fcat using 'VDATU' 'ITAB' 'VBAK' c_n 'Rq.Delv.Dt.' c_n c_n.
  perform write_fcat using 'MONTH' 'ITAB' c_n c_n 'Month' c_n c_y.
  IF NOT p_sale IS INITIAL.
    perform write_fcat using 'OQTYS' 'ITAB' c_n 'OUOMS' 'Order Qty-Sales' c_y c_n.
    perform write_fcat using 'OUOMS' 'ITAB' c_n c_n 'UoM' c_y c_n.
  ELSEIF NOT p_base IS INITIAL.
    perform write_fcat using 'OQTYB' 'ITAB' c_n 'OUOMB' 'Order Qty-Base' c_y c_n.
    perform write_fcat using 'OUOMB' 'ITAB' c_n c_n 'UoM' c_y c_n.
  ENDIF.
  perform write_fcat using 'WADAT_IST' 'ITAB' c_n c_n 'PGI Date' c_n c_n.
  IF NOT p_sale IS INITIAL.
    perform write_fcat using 'PQTYS' 'ITAB' c_n 'PUOMS' 'PGI Qty-Sales' c_y c_n.
    perform write_fcat using 'PUOMS' 'ITAB' c_n c_n 'UoM' c_y c_n.
  ELSEIF NOT p_base IS INITIAL.
    perform write_fcat using 'PQTYB' 'ITAB' c_n 'PUOMB' 'PGI Qty-Base' c_y c_n.
    perform write_fcat using 'PUOMB' 'ITAB' c_n c_n 'UoM' c_y c_n.
  ENDIF.
  perform write_fcat using 'TRAID' 'ITAB' 'LIKP' c_n 'Trans ID' c_y c_n.
ENDFORM.                    " CREATE_FCAT

*&---------------------------------------------------------------------*
*&      Form  write_fcat
*&---------------------------------------------------------------------*
FORM write_fcat USING name table reftab qfield col_text sum hide.
  col_pos = col_pos + 1.        "column position
  afield-fieldname = name.      "field name
  afield-tabname = table.       "table name
  afield-ref_tabname = reftab.  "reference table name
  afield-key = ' '.             "key
  afield-col_pos = col_pos.     "column position
  afield-qfieldname = qfield.   "Quantity field
  afield-seltext_l = col_text.  "long text
  afield-seltext_m = col_text.  "medium text
  afield-seltext_s = col_text.  "short text
  afield-reptext_ddic = col_text."text in datadictionary
  afield-do_sum = sum.            "sum
  afield-no_out = hide.           "hiding
  APPEND afield to fcat.
  CLEAR afield.
ENDFORM.                    " write_fcat

*&---------------------------------------------------------------------*
*&      Form  fill_data
*&---------------------------------------------------------------------*
FORM fill_data .
  LOOP AT itab.
    SELECT SINGLE name1 INTO itab-name1 FROM kna1
         WHERE kunnr = itab-kunnr.
    itab-month = itab-vdatu+4(2).
    MODIFY itab.
  ENDLOOP.
ENDFORM.                    " fill_data

DEFINE m_sort.
  add 1 to lt_sort-spos.
  lt_sort-fieldname = &1.
  lt_sort-up        = 'X'.
  lt_sort-subtot    = &2.
  append lt_sort to p_sort.
END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
FORM top_of_page .
  CLEAR t000-mtext.
  SELECT SINGLE mtext INTO t000-mtext FROM t000
   WHERE mandt = sy-mandt.

  REFRESH head.

  wa_header-typ  = 'A'.
  CONCATENATE 'Program:' sy-repid text-000
                INTO wa_header-info SEPARATED BY space.
  append wa_header to head.

  wa_header-typ  = 'A'.
  CONCATENATE 'Date:' sy-datum INTO wa_header-info SEPARATED BY space.
  append wa_header to head.

  wa_header-typ  = 'A'.
  CONCATENATE 'Client :' sy-mandt '-' t000-mtext INTO wa_header-info SEPARATED BY space.
  append wa_header to head.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY       = head[].

ENDFORM.                    " top_of_page

*&---------------------------------------------------------------------*
*&      Form  sort_grid
*&---------------------------------------------------------------------*
FORM sort_grid .
  m_sort 'MATNR' ' '.
  m_sort 'KUNNR' ' '.
  m_sort 'NAME1' ' '.
  m_sort 'VBELN' ' '.
  m_sort 'GUEBG' ' '.
  m_sort 'GUEEN' ' '.
  IF p_sale = 'X'.
    m_sort 'CQTYS' ' '.
    m_sort 'CUOMS' ' '.
  ELSE.
    m_sort 'CQTYB' ' '.
    m_sort 'CUOMB' ' '.
  ENDIF.
  m_sort 'VBELN' 'X'.
  IF p_subtot = 'X'.
    m_sort 'MONTH' 'X'.
  ENDIF.
ENDFORM.                    " sort_grid

rgds,

bharat.