Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

----REPORT ZMMINVREPT MESSAGE-ID 5d.

************************************************************************
* MM Inventory Report                                                  *
*                                                                      *
************************************************************************

CONSTANTS: c_fax_email(10) VALUE '@fax.'.

TABLES: mara,                                   "material
        marc,                                   "sku
        mard,                                   "storage location
        mslb,                                   "STOCK
        zm008.                                  "usage

* ALV control fields

TYPE-POOLS slis.

* begin of insert SD0K954028
CONSTANTS: c_logical_file_name LIKE  filename-fileintern
                              VALUE 'Z_MM_INVENTORY_RPT'.

DATA: rt_dbfieldcat     TYPE STANDARD TABLE OF ltdxdata,
      rt_dbfieldcat_len TYPE STANDARD TABLE OF ltdxdata,
      rt_dbsortinfo     TYPE STANDARD TABLE OF ltdxdata,
      rt_dbfilter       TYPE STANDARD TABLE OF ltdxdata,
      rt_dblayout       TYPE STANDARD TABLE OF ltdxdata,
      st_dbfieldcat     TYPE ltdxdata,
      st_dbfieldcat_len TYPE ltdxdata.
* end of insert SD0K954028


DATA is_sort     TYPE slis_sortinfo_alv.
DATA it_sort     TYPE slis_t_sortinfo_alv.
DATA is_layout   TYPE slis_layout_alv.
DATA is_print    TYPE slis_print_alv.
DATA gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA g_repid     LIKE sy-repid.
DATA g_title(70) TYPE c.

TYPES: BEGIN OF tstructure,
       werks     LIKE marc-werks,              "plant
       matnr     LIKE marc-matnr,              "material
       maktx     LIKE makt-maktx,              "material text
       meins     LIKE mara-meins,              "base uom
       mmsta     LIKE marc-mmsta,              "mm/pp status
       minbe     LIKE marc-minbe,              "rop qty
       labst     LIKE mard-labst,              "on-hand qty -unrestr
       speme     LIKE mard-speme,              "on-hand qty - blocked
       openq     LIKE mdbs-menge,              "open order qty
       usag1     LIKE zm008-menge,             "usage qty 1
       usag2     LIKE zm008-menge,             "usage qty 2
       usag3     LIKE zm008-menge,             "usage qty 3
       usat1     LIKE zm008-menge_t,           "transfer usage qty 1
       usat2     LIKE zm008-menge_t,           "transfer usage qty 2
       usat3     LIKE zm008-menge_t,           "transfer usage qty 3
       usaa1     LIKE zm008-menge,             "total usage qty 1
       usaa2     LIKE zm008-menge,             "total usage qty 2
       usaa3     LIKE zm008-menge,             "total usage qty 3
       valu1     LIKE zm008-dmbtr,             "usage value 1
       valu2     LIKE zm008-dmbtr,             "usage value 2
       valu3     LIKE zm008-dmbtr,             "usage value 3
       valt1     LIKE zm008-dmbtr_t,           "transfer value qty 1
       valt2     LIKE zm008-dmbtr_t,           "transfer value qty 2
       valt3     LIKE zm008-dmbtr_t,           "transfer value qty 3
       vala1     LIKE zm008-dmbtr,             "total value qty 1
       vala2     LIKE zm008-dmbtr,             "total value qty 2
       vala3     LIKE zm008-dmbtr,             "total value qty 3
       lbkum     LIKE mbew-lbkum,              "on-hand qty
       salk3     LIKE mbew-salk3,              "on-hand value
       dispo     LIKE marc-dispo,              "mrp controller
       ekgrp     LIKE marc-ekgrp,              "purchasing group
       trame     LIKE marc-trame,              "in-transit qty
       verpr     LIKE mbew-verpr,              "moving avg price
       stprs     LIKE mbew-stprs,              "standard price
       vprsv     LIKE mbew-vprsv,              "price control
       dismm     LIKE marc-dismm,              "mrp type
       plifz     LIKE marc-plifz,              "planned delivery time
       disls     LIKE marc-disls,              "lot size key
       beskz     LIKE marc-beskz,              "procurement type
       sobsl     LIKE marc-sobsl,              "special proc key
       eisbe     LIKE marc-eisbe,              "safety stock qty
       bstrf     LIKE marc-bstrf,              "rounding value qty
       disgr     LIKE marc-disgr,              "mrp group
       peinh     LIKE mbew-peinh,              "price unit
       vmkum     LIKE mbew-vmkum,              "total stock - prev per
       vmsal     LIKE mbew-vmsal,              "stock value - prev per
       ssval(16) TYPE p DECIMALS 2,              "safety stock value
       mtart     LIKE mara-mtart,              "material type
       matkl     LIKE mara-matkl,              "material group
       lvorm     LIKE marc-lvorm,              "deletion flag
       maabc     LIKE marc-maabc,              "abc indicator
       kzkri     LIKE marc-kzkri,              "critical part
       bstmi     LIKE marc-bstmi,              "minimum lot size
       bstma     LIKE marc-bstma,              "maximum lot size
       mabst     LIKE marc-mabst,              "maximum stock level
       bstfe     LIKE marc-bstfe,              "fixed lot size
       fhori     LIKE marc-fhori,              "schedule margin key
       lgrad     LIKE marc-lgrad,              "service level
       kautb     LIKE marc-kautb,              "auto po indicator
       abcin     LIKE marc-abcin,              "cc physical inv ind
       ccfix     LIKE marc-ccfix,              "cc fixed indicator
       perkz     LIKE marc-perkz,              "period indicator
       periv     LIKE marc-periv,              "fiscal year variant
       waers     LIKE t001-waers,              "currency
       lfgja     LIKE mbew-lfgja,              "current fiscal year
       lfmon     LIKE mbew-lfmon,              "current month
       labst2    LIKE mslb-lblab, "Stock mat. to vendor unrestric use
       insme2    LIKE mslb-lbins, "Stock mat. to vendor Qual. inspec
       total_quantity(13) TYPE p DECIMALS 3,    "TOTAL QUANTITY
       END OF tstructure.

DATA g_rpt       TYPE tstructure OCCURS 0 WITH HEADER LINE.

TYPES: BEGIN OF storage_type,
       werks     LIKE marc-werks,              "plant
       matnr     LIKE marc-matnr,              "material
       maktx     LIKE makt-maktx,              "material text
       meins     LIKE mara-meins,              "base uom
       mmsta     LIKE marc-mmsta,              "mm/pp status
       minbe     LIKE marc-minbe,              "rop qty
       labst     LIKE mard-labst,              "on-hand qty -unrestr
       speme     LIKE mard-speme,              "on-hand qty - blocked
       openq     LIKE mdbs-menge,              "open order qty
       usag1     LIKE zm008-menge,             "usage qty 1
       usag2     LIKE zm008-menge,             "usage qty 2
       usag3     LIKE zm008-menge,             "usage qty 3
       usat1     LIKE zm008-menge_t,           "transfer usage qty 1
       usat2     LIKE zm008-menge_t,           "transfer usage qty 2
       usat3     LIKE zm008-menge_t,           "transfer usage qty 3
       usaa1     LIKE zm008-menge,             "total usage qty 1
       usaa2     LIKE zm008-menge,             "total usage qty 2
       usaa3     LIKE zm008-menge,             "total usage qty 3
       valu1     LIKE zm008-dmbtr,             "usage value 1
       valu2     LIKE zm008-dmbtr,             "usage value 2
       valu3     LIKE zm008-dmbtr,             "usage value 3
       valt1     LIKE zm008-dmbtr_t,           "transfer value qty 1
       valt2     LIKE zm008-dmbtr_t,           "transfer value qty 2
       valt3     LIKE zm008-dmbtr_t,           "transfer value qty 3
       vala1     LIKE zm008-dmbtr,             "total value qty 1
       vala2     LIKE zm008-dmbtr,             "total value qty 2
       vala3     LIKE zm008-dmbtr,             "total value qty 3
       lbkum     LIKE mbew-lbkum,              "on-hand qty
       salk3     LIKE mbew-salk3,              "on-hand value
       dispo     LIKE marc-dispo,              "mrp controller
       ekgrp     LIKE marc-ekgrp,              "purchasing group
       trame     LIKE marc-trame,              "in-transit qty
       verpr     LIKE mbew-verpr,              "moving avg price
       stprs     LIKE mbew-stprs,              "standard price
       vprsv     LIKE mbew-vprsv,              "price control
       dismm     LIKE marc-dismm,              "mrp type
       plifz     LIKE marc-plifz,              "planned delivery time
       disls     LIKE marc-disls,              "lot size key
       beskz     LIKE marc-beskz,              "procurement type
       sobsl     LIKE marc-sobsl,              "special proc key
       eisbe     LIKE marc-eisbe,              "safety stock qty
       bstrf     LIKE marc-bstrf,              "rounding value qty
       disgr     LIKE marc-disgr,              "mrp group
       peinh     LIKE mbew-peinh,              "price unit
       vmkum     LIKE mbew-vmkum,              "total stock - prev per
       vmsal     LIKE mbew-vmsal,              "stock value - prev per
       ssval     LIKE mbew-vmsal,              "safety stock value
       mtart     LIKE mara-mtart,              "material type
       matkl     LIKE mara-matkl,              "material group
       lvorm     LIKE marc-lvorm,              "deletion flag
       maabc     LIKE marc-maabc,              "abc indicator
       kzkri     LIKE marc-kzkri,              "critical part
       bstmi     LIKE marc-bstmi,              "minimum lot size
       bstma     LIKE marc-bstma,              "maximum lot size
       mabst     LIKE marc-mabst,              "maximum stock level
       bstfe     LIKE marc-bstfe,              "fixed lot size
       fhori     LIKE marc-fhori,              "schedule margin key
       lgrad     LIKE marc-lgrad,              "service level
       kautb     LIKE marc-kautb,              "auto po indicator
       abcin     LIKE marc-abcin,              "cc physical inv ind
       ccfix     LIKE marc-ccfix,              "cc fixed indicator
       perkz     LIKE marc-perkz,              "period indicator
       periv     LIKE marc-periv,              "fiscal year variant
       waers     LIKE t001-waers,              "currency
       lfgja     LIKE mbew-lfgja,              "current fiscal year
       lfmon     LIKE mbew-lfmon,              "current month
       lgort     LIKE mard-lgort,              "Storage location
       lvorm1     LIKE mard-lvorm,    "Flag Mat.for Del at Stor lev
       labst1     LIKE mard-labst,   "Val stock with unrestrict use
       einme     LIKE mard-einme,    "Tot Stock All Restrict Batches
       speme1     LIKE mard-speme,             "Blocked stock
       retme     LIKE mard-retme,              "Blocked Stock Returns
       diskz     LIKE mard-diskz,        "Storage locat MRP indicator
       lminb     LIKE mard-lminb,   "Reorder point for stor locat MRP
       lbstf     LIKE mard-lbstf,  "Replenish quant for stor locat MRP
       lgpbe     LIKE mard-lgpbe,              "Storage bin
       dlinl     LIKE mard-dlinl,  " date of unrestricted stock
       ersda     LIKE mard-ersda,              "Creation date
       END OF storage_type.
* work fields
DATA: storage_tab TYPE TABLE OF storage_type.

TYPES: BEGIN OF temp_storage_type,
       matnr     LIKE mard-matnr,
       werks     LIKE mard-werks,
       lgort     LIKE mard-lgort,              "Storage location
       lvorm     LIKE mard-lvorm,    "Flag Mat.for Del at Stor lev
       labst     LIKE mard-labst,   "Val stock with unrestrict use
       einme     LIKE mard-einme,    "Tot Stock All Restrict Batches
       speme     LIKE mard-speme,             "Blocked stock
       retme     LIKE mard-retme,              "Blocked Stock Returns
       diskz     LIKE mard-diskz,        "Storage locat MRP indicator
       lminb     LIKE mard-lminb,   "Reorder point for stor locat MRP
       lbstf     LIKE mard-lbstf,  "Replenish quant for stor locat MRP
       lgpbe     LIKE mard-lgpbe,              "Storage bin
       dlinl     LIKE mard-dlinl,  " date of unrestricted stock
       ersda     LIKE mard-ersda,              "Creation date
       END OF temp_storage_type.

DATA: temp_stor_tab TYPE TABLE OF temp_storage_type.

DATA: z_date     LIKE sy-datum.

************************************************************************
* PARAMETERS/SELECT-OPTIONS                                            *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b0 WITH FRAME TITLE text-p01.
SELECT-OPTIONS:
  s_werks      FOR marc-werks OBLIGATORY,    "plant
  s_matnr      FOR marc-matnr,               "material
  s_dispo      FOR marc-dispo,               "mrp controller
  s_mtart      FOR mara-mtart,               "material type
  s_matkl      FOR mara-matkl,               "material group
  s_lgort      FOR mard-lgort,               "storage location
  s_lgpbe      FOR mard-lgpbe,               "storage bin
  s_lblab      FOR mslb-lblab.               "Material to Vendor
SELECTION-SCREEN SKIP.
SELECT-OPTIONS:
  s_peri1      FOR zm008-spmon NO-EXTENSION, "1st usage period
  s_peri2      FOR zm008-spmon NO-EXTENSION, "2nd usage period
  s_peri3      FOR zm008-spmon NO-EXTENSION. "3rd usage period
SELECTION-SCREEN SKIP.
PARAMETERS: p_zero AS CHECKBOX DEFAULT 'X'.

* George Pavle Dec20 2006 -------------ins---------------------------%GT%GT%GT
* for email version of the report
PARAMETERS p_delet AS CHECKBOX.
PARAMETERS p_email AS CHECKBOX.
*--------------------------------------------------------------------%LT%LT%LT


SELECTION-SCREEN END OF BLOCK b0.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-p02.
PARAMETERS: s_vari TYPE slis_vari MEMORY ID fit_alv_ap.
PARAMETERS: s_vari2 TYPE slis_vari MEMORY ID fit_alv_ap.
SELECTION-SCREEN END OF BLOCK b1.

* start of insert SD0K954028
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-p03.
PARAMETERS: p_down.
PARAMETERS: p_varid TYPE slis_vari.
SELECTION-SCREEN END OF BLOCK b2.
* end of insert SD0K954028

************************************************************************
INITIALIZATION.
************************************************************************

  g_repid = sy-repid.

* initialize usage periods ... period 1 = current year to date
*                          ... period 2 = last year
*                          ... period 3 = 2 years ago

  s_peri1-sign   = 'I'.
  s_peri1-option = 'BT'.
  z_date         = sy-datum.
  s_peri1-high   = z_date+0(6).
  z_date+4(4)    = '0101'.
  s_peri1-low    = z_date+0(6).
  APPEND s_peri1.

  s_peri2-sign   = 'I'.
  s_peri2-option = 'BT'.
  z_date(4)      = z_date(4) - 1.
  s_peri2-low    = z_date+0(6).
  z_date+4(4)    = '1231'.
  s_peri2-high   = z_date+0(6).
  APPEND s_peri2.

  s_peri3-sign   = 'I'.
  s_peri3-option = 'BT'.
  z_date(4)      = z_date(4) - 1.
  s_peri3-high   = z_date+0(6).
  z_date+4(4)    = '0101'.
  s_peri3-low    = z_date+0(6).
  APPEND s_peri3.

***********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_vari.
************************************************************************

  PERFORM alv_variant_f4 USING ' ' CHANGING s_vari.

***********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_vari2.
***********************************************************************

  PERFORM alv_variant_f4 USING '2' CHANGING s_vari2.

* start of insert SD0K954028
***********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_varid.
***********************************************************************

  PERFORM alv_variant_f4 USING ' ' CHANGING p_varid.
* end of insert SD0K954028

************************************************************************
AT SELECTION-SCREEN.
************************************************************************
* start of insert SD0K954028

* has download been requested ?
  perform check_download_inputs.

* end of insert SD0K954028

* George Pavle Dec22 2006 ------------------ins----------------------%GT%GT%GT
* for the email form of this report, we are setting fixed dates:
AT SELECTION-SCREEN OUTPUT. "on p_EMAIL.
  IF p_email = 'X'.
*    email report requested. So enforce the dates
    LOOP AT SCREEN.
      CHECK screen-name CS 'S_PERI'.
      screen-input = 0.
      MODIFY SCREEN.
    ENDLOOP.

    READ TABLE s_lgort INDEX 1.
    IF sy-subrc %LT%GT 0 OR s_lgort %LT%GT 'EEQISSU'.
      INSERT 'EEQISSU' INTO s_lgort INDEX 1.
    ENDIF.
    REFRESH: s_peri1, s_peri2, s_peri3.
    DATA: l_date LIKE sy-datum.
    l_date = sy-datum.
    l_date+6(2) = '15'.
    l_date = l_date - 365.
    s_peri1 = 'IBT'.
    s_peri1-high = sy-datum(6).
    s_peri1-low  = l_date(6).
    APPEND s_peri1.
  ELSE.
*    on-line report requested. Enable dates:
    LOOP AT SCREEN.
      CHECK screen-name CS 'S_PERI'.
      screen-input = 1.
      MODIFY SCREEN.
    ENDLOOP.
  ENDIF.
*--------------------------------------------------------------------%LT%LT%LT

************************************************************************
START-OF-SELECTION.
************************************************************************

  DATA: l_matnr  LIKE marc-matnr,
        l_werks  LIKE marc-werks,
        l_meins  LIKE mara-meins,
        l_maktx  LIKE makt-maktx,
        l_mtart  LIKE mara-mtart,
        l_matkl  LIKE mara-matkl,
        l_menge  LIKE mdbs-menge,
        l_wemng  LIKE mdbs-wemng,
        l_waers  LIKE t001-waers,
        l_ebeln  LIKE ekpo-ebeln,
        l_ebelp  LIKE ekpo-ebelp,
        l_qtyord LIKE ekpo-menge,
        l_qtyrec LIKE eket-wemng,
        l_shkzg  LIKE ekbe-shkzg,
        l_qtyinv LIKE ekbe-menge,
        l_totinv LIKE ekbe-menge,
        l_bsart  LIKE ekko-bsart.

  g_title = 'MM Inventory Report'.

* build table of skus to be reported

  SELECT werks matnr mmsta minbe dispo ekgrp trame dismm plifz disls
         beskz sobsl eisbe bstrf disgr lvorm maabc kzkri bstmi bstma
         mabst bstfe fhori lgrad kautb abcin ccfix perkz periv
    FROM marc
    INTO CORRESPONDING FIELDS OF TABLE g_rpt
   WHERE werks IN s_werks
     AND matnr IN s_matnr
     AND dispo IN s_dispo.

* add "basic data" fields from MARA/MAKT

  SORT g_rpt BY matnr werks.
  LOOP AT g_rpt.

* George Pavle Dec29 2006 ----------------------ins------------------%GT%GT%GT
*
*   exclude material marked for deletion in MARC:
    IF p_delet = 'X' AND g_rpt-lvorm = 'X'.
      DELETE g_rpt.
      CONTINUE.
    ENDIF.
*--------------------------------------------------------------------%LT%LT%LT

    IF g_rpt-matnr %LT%GT l_matnr.
      l_matnr = g_rpt-matnr.
      CLEAR l_meins.
      CLEAR l_maktx.
      CLEAR l_mtart.
      CLEAR l_matkl.
      SELECT SINGLE meins maktx mtart matkl
        FROM marav
        INTO (l_meins, l_maktx, l_mtart, l_matkl)
       WHERE matnr =  g_rpt-matnr
         AND mtart IN s_mtart
         AND matkl IN s_matkl
         AND spras =  sy-langu.
    ENDIF.
    IF NOT l_meins IS INITIAL.
      g_rpt-meins = l_meins.
      g_rpt-maktx = l_maktx.
      g_rpt-mtart = l_mtart.
      g_rpt-matkl = l_matkl.
      MODIFY g_rpt.
    ELSE.
      DELETE g_rpt.
    ENDIF.
  ENDLOOP.

* add storage location fields from MARD

  LOOP AT g_rpt.
    SELECT SUM( labst ) AS labst
           SUM( speme ) AS speme
      FROM mard
      INTO CORRESPONDING FIELDS OF g_rpt
     WHERE matnr = g_rpt-matnr
       AND werks = g_rpt-werks.
    MODIFY g_rpt.
  ENDLOOP.

* add pricing/valuation fields from MBEW

  LOOP AT g_rpt.
    SELECT SINGLE lbkum salk3 verpr stprs vprsv peinh lfgja lfmon
      FROM mbew
      INTO CORRESPONDING FIELDS OF g_rpt
     WHERE matnr = g_rpt-matnr
       AND bwkey = g_rpt-werks.

    IF g_rpt-lfgja %LT sy-datum(4)
    OR g_rpt-lfmon %LT sy-datum+4(2).
      g_rpt-vmkum = g_rpt-lbkum.
      g_rpt-vmsal = g_rpt-salk3.
    ELSE.
      SELECT lfgja lfmon lbkum salk3
        FROM mbewh
        INTO (g_rpt-lfgja, g_rpt-lfmon, g_rpt-vmkum, g_rpt-vmsal)
       WHERE matnr = g_rpt-matnr
         AND bwkey = g_rpt-werks
       ORDER BY lfgja DESCENDING
                lfmon DESCENDING.
        EXIT.
      ENDSELECT.
    ENDIF.
    IF g_rpt-vprsv = 'S'.
      g_rpt-ssval = g_rpt-eisbe * g_rpt-stprs / g_rpt-peinh.
    ELSE.
      g_rpt-ssval = g_rpt-eisbe * g_rpt-verpr / g_rpt-peinh.
    ENDIF.
    MODIFY g_rpt.
  ENDLOOP.

* add open order (unreceipted) qtys

  LOOP AT g_rpt.

*   get order qty from EKPO

    SELECT ebeln ebelp menge
      FROM ekpo
      INTO (l_ebeln, l_ebelp, l_qtyord)
     WHERE matnr = g_rpt-matnr
       AND werks = g_rpt-werks
       AND loekz = ' '                                    "not deleted
       AND elikz = ' '.                                   "not completed

*     get sum of receipt qtys from EKET

      SELECT SUM( wemng )
        FROM eket
        INTO l_qtyrec
       WHERE ebeln = l_ebeln
         AND ebelp = l_ebelp.

      IF l_qtyrec %LT l_qtyord.

*       sum of receipt qtys is less than order qty, but we still have to
*       check if item is fully invoiced ... get invoice qty from EKBE

        CLEAR l_totinv.
        SELECT shkzg menge
          FROM ekbe
          INTO (l_shkzg, l_qtyinv)
         WHERE ebeln =  l_ebeln
           AND ebelp =  l_ebelp
           AND vgabe IN ('2', '3').
          IF l_shkzg = 'S'.
            l_totinv = l_totinv + l_qtyinv.
          ELSE.
            l_totinv = l_totinv - l_qtyinv.
          ENDIF.
        ENDSELECT.

        IF l_totinv %GT= l_qtyord.

*         item is fully invoiced ... check if po is destock

          SELECT SINGLE bsart
            FROM ekko
            INTO l_bsart
           WHERE ebeln = l_ebeln.
          IF  l_bsart    %LT%GT 'ZD'
          AND l_ebeln(1) %LT%GT 'D'.

*           po is not destock ... report it

            g_rpt-openq = g_rpt-openq + l_qtyord - l_qtyrec.
          ENDIF.
        ELSE.

*         item is not fully invoiced ... report it

          g_rpt-openq = g_rpt-openq + l_qtyord - l_qtyrec.
        ENDIF.
      ENDIF.
    ENDSELECT.
    MODIFY g_rpt.
  ENDLOOP.

* add usage

  LOOP AT g_rpt.
    IF NOT s_peri1 IS INITIAL.
      SELECT SUM( menge ) SUM( menge_t ) SUM( dmbtr ) SUM( dmbtr_t )
        FROM zm008
        INTO (g_rpt-usag1, g_rpt-usat1, g_rpt-valu1, g_rpt-valt1)
       WHERE spmon IN s_peri1
         AND werks =  g_rpt-werks
         AND matnr =  g_rpt-matnr.
      g_rpt-usaa1 = g_rpt-usag1 + g_rpt-usat1.
      g_rpt-vala1 = g_rpt-valu1 + g_rpt-valt1.
    ENDIF.

    IF NOT s_peri2 IS INITIAL.
      SELECT SUM( menge ) SUM( menge_t ) SUM( dmbtr ) SUM( dmbtr_t )
        FROM zm008
        INTO (g_rpt-usag2, g_rpt-usat2, g_rpt-valu2, g_rpt-valt2)
       WHERE spmon IN s_peri2
         AND werks =  g_rpt-werks
         AND matnr =  g_rpt-matnr.
      g_rpt-usaa2 = g_rpt-usag2 + g_rpt-usat2.
      g_rpt-vala2 = g_rpt-valu2 + g_rpt-valt2.
    ENDIF.

    IF NOT s_peri3 IS INITIAL.
      SELECT SUM( menge ) SUM( menge_t ) SUM( dmbtr ) SUM( dmbtr_t )
        FROM zm008
        INTO (g_rpt-usag3, g_rpt-usat3, g_rpt-valu3, g_rpt-valt3)
       WHERE spmon IN s_peri3
         AND werks =  g_rpt-werks
         AND matnr =  g_rpt-matnr.
      g_rpt-usaa3 = g_rpt-usag3 + g_rpt-usat3.
      g_rpt-vala3 = g_rpt-valu3 + g_rpt-valt3.
    ENDIF.

    MODIFY g_rpt.
  ENDLOOP.

* add currency

  SORT g_rpt BY werks.
  LOOP AT g_rpt.
    IF g_rpt-werks %LT%GT l_werks.
      l_werks = g_rpt-werks.
      CLEAR l_waers.
      SELECT SINGLE waers
        FROM v_bam_t001
        INTO l_waers
       WHERE bwkey =  g_rpt-werks.
    ENDIF.
    g_rpt-waers = l_waers.
    MODIFY g_rpt.
  ENDLOOP.

* drop skus with all qtys = zero

  IF p_zero = 'X'.
    DELETE g_rpt WHERE labst = 0
                   AND speme = 0
                   AND openq = 0
                   AND usag1 = 0
                   AND usag2 = 0
                   AND usag3 = 0
                   AND usat1 = 0
                   AND usat2 = 0
                   AND usat3 = 0
                   AND trame = 0
                   AND vmkum = 0.
  ENDIF.
* were any skus selected?

  DESCRIBE TABLE g_rpt LINES sy-tabix.

  IF sy-tabix = 0.
    MESSAGE ID 'AQ' TYPE 'S' NUMBER '260'.             "no data selected
  ENDIF.

  SORT g_rpt BY werks matnr.

* retrieve the fields from the MARD table

  DATA: storage_wa TYPE storage_type.
  DATA: temp_stor_wa TYPE temp_storage_type.
  REFRESH storage_tab.
  SELECT matnr werks lgort lvorm labst einme speme retme diskz
    lminb lbstf lgpbe dlinl ersda FROM mard
    INTO CORRESPONDING FIELDS OF TABLE temp_stor_tab
    FOR ALL ENTRIES IN g_rpt
    WHERE matnr = g_rpt-matnr
      AND werks = g_rpt-werks
      AND lgort IN s_lgort
      AND lgpbe IN s_lgpbe.

  SORT temp_stor_tab BY matnr werks lgort.
  DELETE ADJACENT DUPLICATES FROM temp_stor_tab.

  LOOP AT g_rpt.
    LOOP AT temp_stor_tab INTO temp_stor_wa
      WHERE matnr = g_rpt-matnr AND werks = g_rpt-werks.
      CLEAR storage_wa.
      MOVE-CORRESPONDING g_rpt TO storage_wa.
      storage_wa-lgort = temp_stor_wa-lgort.
      storage_wa-lvorm1 = temp_stor_wa-lvorm.
      storage_wa-labst1 = temp_stor_wa-labst.
      storage_wa-einme = temp_stor_wa-einme.
      storage_wa-speme1 = temp_stor_wa-speme.
      storage_wa-retme = temp_stor_wa-retme.
      storage_wa-diskz = temp_stor_wa-diskz.
      storage_wa-lminb = temp_stor_wa-lminb.
      storage_wa-lbstf = temp_stor_wa-lbstf.
      storage_wa-lgpbe = temp_stor_wa-lgpbe.
      storage_wa-dlinl = temp_stor_wa-dlinl.
      storage_wa-ersda = temp_stor_wa-ersda.
      APPEND storage_wa TO storage_tab.
    ENDLOOP.
  ENDLOOP.
  REFRESH temp_stor_tab.

  DATA: templblab LIKE mslb-lblab.
  DATA: templbins LIKE mslb-lbins.
  DATA: BEGIN OF itab_index OCCURS 0,
          tabix LIKE sy-tabix,
        END OF itab_index.

  LOOP AT g_rpt.
    SELECT SUM( lblab ) SUM( lbins )
      FROM mslb INTO (templblab, templbins)
    WHERE matnr = g_rpt-matnr
        AND werks = g_rpt-werks
        AND sobkz = 'O'.
    IF sy-subrc = 0.
      g_rpt-labst2 = templblab.
      g_rpt-insme2 = templbins.
      MODIFY g_rpt.
    ENDIF.
  ENDLOOP.

  LOOP AT g_rpt WHERE NOT labst2 IN s_lblab.
    itab_index-tabix = sy-tabix.
    APPEND itab_index.
  ENDLOOP.

  SORT itab_index BY tabix DESCENDING.

  LOOP AT itab_index.
    DELETE g_rpt INDEX itab_index-tabix.
  ENDLOOP.

  REFRESH itab_index.

  LOOP AT g_rpt.
    g_rpt-total_quantity = g_rpt-labst + g_rpt-labst2 + g_rpt-insme2
      + g_rpt-trame + g_rpt-speme.
    MODIFY g_rpt.
  ENDLOOP.

************************************************************************
END-OF-SELECTION.
************************************************************************

* George Pavle Dec20 2006 -------------ins---------------------------%GT%GT%GT
* for email version of the report, execute this new subroutine:
  IF p_email = 'X'.
    PERFORM send_emails.
    EXIT.
  ELSE.
*    for ALV on-line report, execute the rest of the origoinal code...
  ENDIF.
*--------------------------------------------------------------------%LT%LT%LT

  REFRESH gt_fieldcat[].
  CLEAR   gt_fieldcat.

* set the default list format

  PERFORM default_field_catalog USING gt_fieldcat[].

* get the user-specified list format

  PERFORM user_field_catalog USING gt_fieldcat[] '1'.

* set layout options

  CLEAR is_layout.
  CLEAR is_print.

  is_layout-numc_sum          = 'X'.
  is_layout-zebra             = 'X'.
  is_layout-colwidth_optimize = 'X'.

* display the table of po items as an alv grid

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      =  g_repid
*            I_CALLBACK_USER_COMMAND = 'CALL_ME23N'
            i_callback_pf_status_set = 'SET_PF_STATUS'
            i_callback_user_command = 'USER_COMMAND'
            it_fieldcat             =  gt_fieldcat[]
            i_save                  = 'A'
            it_sort                 =  it_sort
            is_layout               =  is_layout
            is_print                =  is_print
            i_grid_title            =  g_title
       TABLES
            t_outtab                =  g_rpt.

*Start insert SD0K954028
  IF NOT p_down IS INITIAL. "download to file ?
    PERFORM download.
  ENDIF.
*End insert SD0K954028

************************************************************************
************************************************************************
* FORMS
************************************************************************
************************************************************************

************************************************************************
* Set the gui status with 1 custom defined function.
************************************************************************
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZSTATUS' EXCLUDING rt_extab.
ENDFORM.                    "SET_PF_STATUS

************************************************************************
* CUSTOM USER COMMAND PROCESSING FOR THE FIRST ALV GRID
************************************************************************
FORM user_command USING r_ucomm LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield.
  DATA: h_repid LIKE sy-repid.
  h_repid = sy-repid.
  REFRESH gt_fieldcat[].
  CLEAR   gt_fieldcat.
  DATA: variant2 TYPE disvariant.
* set the default list format

  variant2-report = h_repid.
  variant2-handle = '2'.

  PERFORM default_field_catalog USING gt_fieldcat[].
  PERFORM storage_field_cat USING gt_fieldcat[].
  PERFORM user_field_catalog USING gt_fieldcat[] '2'.

  g_title = 'MM Inventory Report (Storage View)'.
  CASE r_ucomm.
    WHEN '&SWITCH'.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
            i_callback_program      =  h_repid
*            I_CALLBACK_USER_COMMAND = 'CALL_ME23N'
            i_callback_pf_status_set = 'SET_PF_STATUS'
            i_callback_user_command = 'USER_COMMAND2'
            it_fieldcat             =  gt_fieldcat[]
            i_save                  = 'A'
            is_variant              = variant2
            it_sort                 =  it_sort
            is_layout               =  is_layout
            is_print                =  is_print
            i_grid_title            =  g_title
       TABLES
            t_outtab                =  storage_tab.
  ENDCASE.
ENDFORM.                    "USER_COMMAND

************************************************************************
* CUSTOM USER COMMAND PROCESSING FOR THE FIRST ALV GRID
************************************************************************
FORM user_command2 USING r_ucomm LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
    WHEN '&SWITCH'.
      SET SCREEN 0.
  ENDCASE.
ENDFORM.                    "USER_COMMAND2
************************************************************************
* Format the default field catalog, which defines the report format ...
* the fields to display, their column positions, sort fields, headings,
* etc.
************************************************************************
FORM default_field_catalog
      USING rt_fieldcat TYPE slis_t_fieldcat_alv.

  DATA ls_fieldcat TYPE slis_fieldcat_alv.

  REFRESH it_sort.
  CLEAR   it_sort.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'WERKS'.
  ls_fieldcat-ref_fieldname = 'WERKS'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-key           = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR is_sort.
  is_sort-fieldname         =  ls_fieldcat-fieldname.
  is_sort-up                = 'X'.
  APPEND is_sort TO it_sort.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MATNR'.
  ls_fieldcat-ref_fieldname = 'MATNR'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-key           = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR is_sort.
  is_sort-fieldname         =  ls_fieldcat-fieldname.
  is_sort-up                = 'X'.
  APPEND is_sort TO it_sort.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MAKTX'.
  ls_fieldcat-ref_fieldname = 'MAKTX'.
  ls_fieldcat-ref_tabname   = 'MAKT'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MEINS'.
  ls_fieldcat-ref_fieldname = 'MEINS'.
  ls_fieldcat-ref_tabname   = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MMSTA'.
  ls_fieldcat-ref_fieldname = 'MMSTA'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'LABST'.
  ls_fieldcat-ref_fieldname = 'LABST'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'SALK3'.
  ls_fieldcat-fieldname     = 'SALK3'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'OPENQ'.
  ls_fieldcat-seltext_s     = 'Open Order'.
  ls_fieldcat-seltext_l     = 'Open Order Qty'.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MINBE'.
  ls_fieldcat-ref_fieldname = 'MINBE'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAG1'.
  ls_fieldcat-seltext_s     = 'Usage 1'.
  ls_fieldcat-seltext_l     = 'Usage '.
  WRITE s_peri1-low TO ls_fieldcat-seltext_l+6.
  ls_fieldcat-seltext_l+13  = ' - '.
  WRITE s_peri1-high TO ls_fieldcat-seltext_l+16.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAG2'.
  ls_fieldcat-seltext_s     = 'Usage 2'.
  ls_fieldcat-seltext_l     = 'Usage '.
  WRITE s_peri2-low TO ls_fieldcat-seltext_l+6.
  ls_fieldcat-seltext_l+13  = ' - '.
  WRITE s_peri2-high TO ls_fieldcat-seltext_l+16.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAG3'.
  ls_fieldcat-seltext_s     = 'Usage 3'.
  ls_fieldcat-seltext_l     = 'Usage '.
  WRITE s_peri3-low TO ls_fieldcat-seltext_l+6.
  ls_fieldcat-seltext_l+13  = ' - '.
  WRITE s_peri3-high TO ls_fieldcat-seltext_l+16.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAT1'.
  ls_fieldcat-seltext_s     = 'Tsfr Use 1'.
  ls_fieldcat-seltext_l     = 'Tsfr Usage '.
  WRITE s_peri1-low TO ls_fieldcat-seltext_l+11.
  ls_fieldcat-seltext_l+18  = ' - '.
  WRITE s_peri1-high TO ls_fieldcat-seltext_l+21.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAT2'.
  ls_fieldcat-seltext_s     = 'Tsfr Use 2'.
  ls_fieldcat-seltext_l     = 'Tsfr Usage '.
  WRITE s_peri2-low TO ls_fieldcat-seltext_l+11.
  ls_fieldcat-seltext_l+18  = ' - '.
  WRITE s_peri2-high TO ls_fieldcat-seltext_l+21.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAT3'.
  ls_fieldcat-seltext_s     = 'Tsfr Use 3'.
  ls_fieldcat-seltext_l     = 'Tsfr Usage '.
  WRITE s_peri3-low TO ls_fieldcat-seltext_l+11.
  ls_fieldcat-seltext_l+18  = ' - '.
  WRITE s_peri3-high TO ls_fieldcat-seltext_l+21.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAA1'.
  ls_fieldcat-seltext_s     = 'Totl Use 1'.
  ls_fieldcat-seltext_l     = 'Total Usage '.
  WRITE s_peri1-low TO ls_fieldcat-seltext_l+12.
  ls_fieldcat-seltext_l+19  = ' - '.
  WRITE s_peri1-high TO ls_fieldcat-seltext_l+22.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAA2'.
  ls_fieldcat-seltext_s     = 'Totl Use 2'.
  ls_fieldcat-seltext_l     = 'Total Usage '.
  WRITE s_peri2-low TO ls_fieldcat-seltext_l+12.
  ls_fieldcat-seltext_l+19  = ' - '.
  WRITE s_peri2-high TO ls_fieldcat-seltext_l+22.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'USAA3'.
  ls_fieldcat-seltext_s     = 'Totl Use 3'.
  ls_fieldcat-seltext_l     = 'Total Usage '.
  WRITE s_peri3-low TO ls_fieldcat-seltext_l+12.
  ls_fieldcat-seltext_l+19  = ' - '.
  WRITE s_peri3-high TO ls_fieldcat-seltext_l+22.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALU1'.
  ls_fieldcat-seltext_s     = 'Usage $ 1'.
  ls_fieldcat-seltext_l     = 'Usage $ '.
  WRITE s_peri1-low TO ls_fieldcat-seltext_l+8.
  ls_fieldcat-seltext_l+15  = ' - '.
  WRITE s_peri1-high TO ls_fieldcat-seltext_l+18.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALU2'.
  ls_fieldcat-seltext_s     = 'Usage $ 2'.
  ls_fieldcat-seltext_l     = 'Usage $ '.
  WRITE s_peri2-low TO ls_fieldcat-seltext_l+8.
  ls_fieldcat-seltext_l+15  = ' - '.
  WRITE s_peri2-high TO ls_fieldcat-seltext_l+18.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALU3'.
  ls_fieldcat-seltext_s     = 'Usage $ 3'.
  ls_fieldcat-seltext_l     = 'Usage $ '.
  WRITE s_peri3-low TO ls_fieldcat-seltext_l+8.
  ls_fieldcat-seltext_l+15  = ' - '.
  WRITE s_peri3-high TO ls_fieldcat-seltext_l+18.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALT1'.
  ls_fieldcat-seltext_s     = 'Tsfr $ 1'.
  ls_fieldcat-seltext_l     = 'Tsfr Usage $ '.
  WRITE s_peri1-low TO ls_fieldcat-seltext_l+13.
  ls_fieldcat-seltext_l+20  = ' - '.
  WRITE s_peri1-high TO ls_fieldcat-seltext_l+23.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALT2'.
  ls_fieldcat-seltext_s     = 'Tsfr $ 2'.
  ls_fieldcat-seltext_l     = 'Tsfr Usage $ '.
  WRITE s_peri2-low TO ls_fieldcat-seltext_l+13.
  ls_fieldcat-seltext_l+20  = ' - '.
  WRITE s_peri2-high TO ls_fieldcat-seltext_l+23.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALT3'.
  ls_fieldcat-seltext_s     = 'Tsfr $ 3'.
  ls_fieldcat-seltext_l     = 'Tsfr Usage $ '.
  WRITE s_peri3-low TO ls_fieldcat-seltext_l+13.
  ls_fieldcat-seltext_l+20  = ' - '.
  WRITE s_peri3-high TO ls_fieldcat-seltext_l+23.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALA1'.
  ls_fieldcat-seltext_s     = 'Total $ 1'.
  ls_fieldcat-seltext_l     = 'Totl Usage $ '.
  WRITE s_peri1-low TO ls_fieldcat-seltext_l+13.
  ls_fieldcat-seltext_l+20  = ' - '.
  WRITE s_peri1-high TO ls_fieldcat-seltext_l+23.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALA2'.
  ls_fieldcat-seltext_s     = 'Total $ 2'.
  ls_fieldcat-seltext_l     = 'Totl Usage $ '.
  WRITE s_peri2-low TO ls_fieldcat-seltext_l+13.
  ls_fieldcat-seltext_l+20  = ' - '.
  WRITE s_peri2-high TO ls_fieldcat-seltext_l+23.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VALA3'.
  ls_fieldcat-seltext_s     = 'Total $ 3'.
  ls_fieldcat-seltext_l     = 'Totl Usage $ '.
  WRITE s_peri3-low TO ls_fieldcat-seltext_l+13.
  ls_fieldcat-seltext_l+20  = ' - '.
  WRITE s_peri3-high TO ls_fieldcat-seltext_l+23.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'EISBE'.
  ls_fieldcat-ref_fieldname = 'EISBE'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'SSVAL'.
  ls_fieldcat-seltext_s     = 'SStk Value'.
  ls_fieldcat-seltext_l     = 'Valuated Safety Stock'.
  ls_fieldcat-outputlen     = 17.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-inttype       = 'P'.
  ls_fieldcat-intlen        = '16'.
  ls_fieldcat-decimals_out      = '2'.
*  LS_FIELDCAT-CFIELDNAME    = 'WAERS'.
*  LS_FIELDCAT-CTABNAME      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'SPEME'.
  ls_fieldcat-ref_fieldname = 'SPEME'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'DISPO'.
  ls_fieldcat-ref_fieldname = 'DISPO'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'EKGRP'.
  ls_fieldcat-ref_fieldname = 'EKGRP'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'TRAME'.
  ls_fieldcat-ref_fieldname = 'TRAME'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VERPR'.
  ls_fieldcat-ref_fieldname = 'VERPR'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'STPRS'.
  ls_fieldcat-ref_fieldname = 'STPRS'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VPRSV'.
  ls_fieldcat-ref_fieldname = 'VPRSV'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'DISMM'.
  ls_fieldcat-ref_fieldname = 'DISMM'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'PLIFZ'.
  ls_fieldcat-ref_fieldname = 'PLIFZ'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'DISLS'.
  ls_fieldcat-ref_fieldname = 'DISLS'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'BESKZ'.
  ls_fieldcat-ref_fieldname = 'BESKZ'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'SOBSL'.
  ls_fieldcat-ref_fieldname = 'SOBSL'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'BSTRF'.
  ls_fieldcat-ref_fieldname = 'BSTRF'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'DISGR'.
  ls_fieldcat-ref_fieldname = 'DISGR'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'PEINH'.
  ls_fieldcat-ref_fieldname = 'PEINH'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VMKUM'.
  ls_fieldcat-ref_fieldname = 'VMKUM'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'VMSAL'.
  ls_fieldcat-ref_fieldname = 'VMSAL'.
  ls_fieldcat-ref_tabname   = 'MBEW'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-cfieldname    = 'WAERS'.
  ls_fieldcat-ctabname      = 'T001'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MTART'.
  ls_fieldcat-ref_fieldname = 'MTART'.
  ls_fieldcat-ref_tabname   = 'MARA'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MATKL'.
  ls_fieldcat-ref_fieldname = 'MATKL'.
  ls_fieldcat-ref_tabname   = 'MARA'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'LVORM'.
  ls_fieldcat-ref_fieldname = 'LVORM'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MAABC'.
  ls_fieldcat-ref_fieldname = 'MAABC'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'KZKRI'.
  ls_fieldcat-ref_fieldname = 'KZKRI'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'BSTMI'.
  ls_fieldcat-ref_fieldname = 'BSTMI'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'BSTMA'.
  ls_fieldcat-ref_fieldname = 'BSTMA'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'MABST'.
  ls_fieldcat-ref_fieldname = 'MABST'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'BSTFE'.
  ls_fieldcat-ref_fieldname = 'BSTFE'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  ls_fieldcat-qtabname      = 'MARA'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'FHORI'.
  ls_fieldcat-ref_fieldname = 'FHORI'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'LGRAD'.
  ls_fieldcat-ref_fieldname = 'LGRAD'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'KAUTB'.
  ls_fieldcat-ref_fieldname = 'KAUTB'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'ABCIN'.
  ls_fieldcat-ref_fieldname = 'ABCIN'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'CCFIX'.
  ls_fieldcat-ref_fieldname = 'CCFIX'.
  ls_fieldcat-ref_tabname   = 'MARC'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname     = 'WAERS'.
  ls_fieldcat-ref_fieldname = 'WAERS'.
  ls_fieldcat-ref_tabname   = 'T001'.
  ls_fieldcat-no_out        = 'X'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LABST2'.
  ls_fieldcat-ref_fieldname = 'LBLAB'.
  ls_fieldcat-ref_tabname   = 'MSLB'.
  ls_fieldcat-seltext_s = 'Mat. to Vend. Unrest. Use'.
  ls_fieldcat-seltext_m = 'Mat. to Vend. Unrest. Use'.
  ls_fieldcat-seltext_l = 'Mat. to Vend. Unrest. Use'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'INSME2'.
  ls_fieldcat-ref_fieldname = 'LBINS'.
  ls_fieldcat-ref_tabname   = 'MSLB'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'TOTAL_QUANTITY'.
  ls_fieldcat-datatype = 'QUAN'.
  ls_fieldcat-inttype = 'P'.
  ls_fieldcat-intlen = '13'.
  ls_fieldcat-seltext_s = 'Total Quantity'.
  ls_fieldcat-seltext_m = 'Total Quantity'.
  ls_fieldcat-seltext_l = 'Total Quantity'.
  APPEND ls_fieldcat TO rt_fieldcat.
ENDFORM.                    "DEFAULT_FIELD_CATALOG

*&---------------------------------------------------------------------*
*&      Form  STORAGE_FIELD_CAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      --%GTRT_FIELDCAT  text
*----------------------------------------------------------------------*
FORM storage_field_cat
      USING rt_fieldcat TYPE slis_t_fieldcat_alv.
  DATA ls_fieldcat TYPE slis_fieldcat_alv.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LGORT'.
  ls_fieldcat-ref_fieldname = 'LGORT'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-key = 'X'.
  ls_fieldcat-seltext_s = 'Stor. loc.'.
  ls_fieldcat-seltext_m = 'Stor. location'.
  ls_fieldcat-seltext_l = 'Storage location'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LVORM1'.
  ls_fieldcat-ref_fieldname = 'LVORM'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)SLoc level'.
  ls_fieldcat-seltext_m = '(SL)DF stor.loc.lvl'.
  ls_fieldcat-seltext_l = '(SL)DF stor. loc. level'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LABST1'.
  ls_fieldcat-ref_fieldname = 'LABST'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Unrestr.'.
  ls_fieldcat-seltext_m = '(SL)Unrestricted'.
  ls_fieldcat-seltext_l = '(SL)Unrestr.-use stock'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'EINME'.
  ls_fieldcat-ref_fieldname = 'EINME'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Restr.-use'.
  ls_fieldcat-seltext_m = '(SL)Restricted-use'.
  ls_fieldcat-seltext_l = '(SL)Restricted-use stock'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'SPEME1'.
  ls_fieldcat-ref_fieldname = 'SPEME'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Block'.
  ls_fieldcat-seltext_m = '(SL)Block'.
  ls_fieldcat-seltext_l = '(SL)Block'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'RETME'.
  ls_fieldcat-ref_fieldname = 'RETME'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Returns'.
  ls_fieldcat-seltext_m = '(SL)Returns'.
  ls_fieldcat-seltext_l = '(SL)Returns'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'DISKZ'.
  ls_fieldcat-ref_fieldname = 'DISKZ'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)MRP ind.'.
  ls_fieldcat-seltext_m = '(SL)MRP ind.'.
  ls_fieldcat-seltext_l = '(SL)MRP ind.'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LMINB'.
  ls_fieldcat-ref_fieldname = 'LMINB'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Reorder point'.
  ls_fieldcat-seltext_m = '(SL)Reorder point'.
  ls_fieldcat-seltext_l = '(SL)Reorder point'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LBSTF'.
  ls_fieldcat-ref_fieldname = 'LBSTF'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)ReplenshQt'.
  ls_fieldcat-seltext_m = '(SL)Replenishmt qty'.
  ls_fieldcat-seltext_l = '(SL)Replenishment qty'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'LGPBE'.
  ls_fieldcat-ref_fieldname = 'LGPBE'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Bin'.
  ls_fieldcat-seltext_m = '(SL)Storage bin'.
  ls_fieldcat-seltext_l = '(SL)Storage bin'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'DLINL'.
  ls_fieldcat-ref_fieldname = 'DLINL'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)PhyInvDate'.
  ls_fieldcat-seltext_m = '(SL)Date last ph.in'.
  ls_fieldcat-seltext_l = '(SL)Date last phys. inv.'.
  APPEND ls_fieldcat TO rt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ERSDA'.
  ls_fieldcat-ref_fieldname = 'ERSDA'.
  ls_fieldcat-ref_tabname   = 'MARD'.
  ls_fieldcat-seltext_s = '(SL)Created on'.
  ls_fieldcat-seltext_m = '(SL)Created on'.
  ls_fieldcat-seltext_l = '(SL)Created on'.
  APPEND ls_fieldcat TO rt_fieldcat.

ENDFORM.                    "STORAGE_FIELD_CAT
************************************************************************
* Get the field catalog for the user-specified list variant.
************************************************************************
FORM user_field_catalog
      USING rt_fieldcat TYPE slis_t_fieldcat_alv
        variant_flag TYPE c.

  DATA ls_variant TYPE disvariant.
  CASE variant_flag.
    WHEN '1'.
      IF NOT s_vari IS INITIAL.
        ls_variant-report    = g_repid.
        ls_variant-handle    = ' '.
        ls_variant-log_group = ' '.
        ls_variant-variant   = s_vari.

*   Get selected list variant specifications

        CALL FUNCTION 'REUSE_ALV_VARIANT_SELECT'
          EXPORTING
            i_dialog            = ' '
            i_user_specific     = 'X'
            it_default_fieldcat = gt_fieldcat[]
            i_layout            = is_layout
            i_buffer_active     = 'X'
          IMPORTING
            et_fieldcat         = gt_fieldcat[]
            et_sort             = it_sort[]
            es_layout           = is_layout
          CHANGING
            cs_variant          = ls_variant
          EXCEPTIONS
            wrong_input         = 1
            fc_not_complete     = 2
            not_found           = 3
            program_error       = 4
            OTHERS              = 5.

*   If problems, use standard variant

        IF sy-subrc %LT%GT 0.
        ENDIF.
      ENDIF.
    WHEN '2'.
      IF NOT s_vari2 IS INITIAL.
        ls_variant-report    = g_repid.
        ls_variant-handle    = '2'.
        ls_variant-log_group = ' '.
        ls_variant-variant   = s_vari2.

*   Get selected list variant specifications

        CALL FUNCTION 'REUSE_ALV_VARIANT_SELECT'
          EXPORTING
            i_dialog            = ' '
            i_user_specific     = 'X'
            it_default_fieldcat = gt_fieldcat[]
            i_layout            = is_layout
            i_buffer_active     = 'X'
          IMPORTING
            et_fieldcat         = gt_fieldcat[]
            et_sort             = it_sort[]
            es_layout           = is_layout
          CHANGING
            cs_variant          = ls_variant
          EXCEPTIONS
            wrong_input         = 1
            fc_not_complete     = 2
            not_found           = 3
            program_error       = 4
            OTHERS              = 5.

*   If problems, use standard variant

        IF sy-subrc %LT%GT 0.
        ENDIF.
      ENDIF.
  ENDCASE.

ENDFORM.                    "USER_FIELD_CATALOG

************************************************************************
* Allow the user to select a list variant.
************************************************************************
FORM alv_variant_f4 USING handle TYPE c
                    CHANGING l_vari.
  DATA rs_variant LIKE disvariant.

  rs_variant-report   = sy-repid.
  rs_variant-username = sy-uname.
  rs_variant-handle = handle.
  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
    EXPORTING
      is_variant = rs_variant
      i_save     = 'A'
    IMPORTING
      es_variant = rs_variant
    EXCEPTIONS
      OTHERS     = 1.

  IF sy-subrc = 0.
    l_vari = rs_variant-variant.
  ENDIF.

ENDFORM.                    "ALV_VARIANT_F4



***********************************************************************
FORM send_emails.
***********************************************************************
* George Pavle Dec20 2006
* for email version of the report, execute this new subroutine

* STEPS:
* - sort it
* - summarize storage locations separating AORI
* - calculate price
* - format and send a separate email for each plant

  DATA: storage_wa LIKE LINE OF storage_tab. "type STORAGE_TYPE.
  DATA: document_data LIKE sodocchgi1,
        receivers LIKE STANDARD TABLE OF somlreci1 WITH HEADER LINE,
        object_content LIKE STANDARD TABLE OF solisti1 WITH HEADER LINE.


  DATA: BEGIN OF lt_report OCCURS 1,
         werks     LIKE marc-werks,              "plant
         matnr     LIKE marc-matnr,              "material
         maktx     LIKE makt-maktx,              "material text
         meins     LIKE mara-meins,              "base uom
         eisbe     LIKE marc-eisbe,              "safety stock qty
         labst1    LIKE mard-labst,              "on-hand qty -unrestr
         openq     LIKE mdbs-menge,              "open order qty
         aoriq     LIKE marc-eisbe,              "stock in AORI
         usage     LIKE zm008-menge,             "usage qty
         value     LIKE mbew-salk3,              "on-hand value
        END OF lt_report.

* sort it:
  SORT storage_tab BY werks matnr lgort.
  DATA: l_plant_count TYPE i,
        l_email_count TYPE i,
        l_count_per_werks TYPE i.
  LOOP AT storage_tab INTO storage_wa.
    AT NEW werks.
      REFRESH: lt_report, receivers, object_content.
      SELECT recipient INTO receivers-receiver
             FROM zmm_inv_email
             WHERE werks = storage_wa-werks.
        receivers-rec_type = 'U'.
        APPEND receivers.
      ENDSELECT.
      ADD 1 TO l_plant_count.
    ENDAT.
    AT NEW matnr.
      l_count_per_werks = 0.
    ENDAT.

*  if we have multiple storage locations per plant, we do not want to
*  summarize plant level quantities:
    ADD 1 TO l_count_per_werks.
    IF l_count_per_werks %GT 1.
      CLEAR: storage_wa-eisbe,
             storage_wa-usag1,
             storage_wa-usag2,
             storage_wa-usag3,
             storage_wa-openq.
    ENDIF.

    CLEAR lt_report.
    MOVE-CORRESPONDING storage_wa TO lt_report.
    lt_report-usage = storage_wa-usag1
                    + storage_wa-usag2
                    + storage_wa-usag3.
*  separate out AORI qty:
    IF storage_wa-lgort = 'AORI'.
*     move AOURI stor.location qty to a special column:
      MOVE lt_report-labst1 TO lt_report-aoriq.
      CLEAR lt_report-labst1.
    ENDIF.

*  calculate the O/H dollar value:
    IF storage_wa-peinh = 0.
*     do not devide by zero
    ELSEIF storage_wa-vprsv = 'V'.
      lt_report-value = storage_wa-labst1
                      * storage_wa-verpr / storage_wa-peinh.
    ELSE. "if STORAGE_WA-VPRSV = 'S'
      lt_report-value = storage_wa-labst1
                      * storage_wa-stprs / storage_wa-peinh.
    ENDIF.
*  summarize all storage locations each material:
    COLLECT lt_report.

    AT END OF werks.
*     send an email for each plant:
      CHECK NOT receivers[] IS INITIAL.

*     get the email subject from TVARV:
      SELECT SINGLE low INTO document_data-obj_descr
              FROM tvarv
             WHERE name = 'ZI_MM018_EMAIL_SUBJECT'
               AND type  = 'P'.

      IF document_data-obj_descr IS INITIAL.
        document_data-obj_descr = sy-title.
      ELSE.
*        substitute the pland palceholder with plant number
        REPLACE ALL OCCURRENCES OF '&WERKS&' IN document_data-obj_descr
WITH storage_wa-werks.
      ENDIF.
      document_data-obj_name  = 'ZI_MM018'.

*     read the email text from standard text:
      DATA: t_line LIKE STANDARD TABLE OF tline WITH HEADER LINE,
            l_date(10),
            l_first_char LIKE sy-tabix,
            l_offset,
            l_text(255),
            lt_longtext       TYPE TABLE OF string  WITH HEADER LINE,
            lt_longtext_fax   TYPE TABLE OF string  WITH HEADER LINE,
            lt_longtext_email LIKE object_content[] WITH HEADER LINE.

      WRITE sy-datum TO l_date.
      REFRESH:t_line, lt_longtext_fax, lt_longtext_email,object_content.
      CALL FUNCTION 'READ_STDTEXT'
        EXPORTING
          id       = 'ST'
          language = sy-langu
          name     = 'ZI_MM018_EMAIL_TEXT'
        TABLES
          lines    = t_line
        EXCEPTIONS
          OTHERS   = 8.
*     substitute placeholders for plant and date:
      LOOP AT t_line.
        MOVE t_line-tdline TO lt_longtext_email-line.
        REPLACE ALL OCCURRENCES OF '&WERKS&' IN lt_longtext_email-line
WITH storage_wa-werks.
        REPLACE ALL OCCURRENCES OF '&DATE&'  IN lt_longtext_email-line
WITH l_date.
        APPEND lt_longtext_email.

*         split it by words to be able to format it better:
        SPLIT lt_longtext_email AT space INTO TABLE lt_longtext.
        CLEAR:l_first_char, l_offset.
        LOOP AT lt_longtext.
          IF lt_longtext IS INITIAL AND l_first_char IS INITIAL.
*               count leading spaces but do not keep empty lines:
            CONTINUE.
          ELSEIF NOT lt_longtext IS INITIAL AND l_offset IS INITIAL.
*               count leading spaces:
            l_first_char = sy-tabix.
            l_offset = 'X'.
          ELSE.
            CONTINUE.
          ENDIF.
          IF l_offset = 'X'.
*               apply leading spaces to the next word:
            SUBTRACT 1 FROM l_first_char.
            CLEAR l_text.
            l_text+l_first_char = lt_longtext.
            lt_longtext = l_text.
            MODIFY lt_longtext.
          ENDIF.
        ENDLOOP.
        APPEND LINES OF lt_longtext TO lt_longtext_fax.
      ENDLOOP.

*     contuinue building the email with the actual report:
      MOVE sy-uline(115)  TO object_content-line.       APPEND
object_content.
      CLEAR object_content.
      MOVE  'PARTNO'       TO object_content-line+0(10).
      MOVE  'DESCRIPTION'  TO object_content-line+10(41).
      MOVE  'UOM'          TO object_content-line+51(3).
      WRITE '     S/S'     TO object_content-line+54(9).
      WRITE '      O/H'    TO object_content-line+63(10).
      WRITE '      O/O'    TO object_content-line+73(10).
      WRITE '     AORI'    TO object_content-line+83(10).
      WRITE '   12 MTH'    TO object_content-line+93(10).
      WRITE '        O/H$' TO object_content-line+103(13).
      APPEND object_content.

      CLEAR object_content.
      WRITE '    USAGE'     TO object_content-line+93(10). APPEND
object_content.

      MOVE sy-uline(115) TO object_content-line. APPEND object_content.
      LOOP AT lt_report.
        CLEAR object_content.
        MOVE  lt_report-matnr TO object_content-line+0(10).
        MOVE  lt_report-maktx TO object_content-line+10(41).
        MOVE  lt_report-meins TO object_content-line+51(3).
        WRITE lt_report-eisbe TO object_content-line+54(9)  NO-ZERO UNIT
 lt_report-meins.
        WRITE lt_report-labst1 TO object_content-line+63(10)  NO-ZERO
UNIT lt_report-meins.
        WRITE lt_report-openq TO object_content-line+73(10)  NO-ZERO
UNIT lt_report-meins.
        WRITE lt_report-aoriq TO object_content-line+83(10)  NO-ZERO
UNIT lt_report-meins.
        WRITE lt_report-usage TO object_content-line+93(10)  NO-ZERO
UNIT lt_report-meins.
        WRITE lt_report-value TO object_content-line+103(13).

        APPEND object_content.
      ENDLOOP.

* George Pavle March 21 2007
*---------------ins-------------------------------%LT%LT%LT
*
* for fax email recipients (e.g.1403319000@fax.cpr.ca) the email needs
* to be sent
* as PDF to ensure proper formating:
      DATA: fax_receivers LIKE receivers[] WITH HEADER LINE.
      REFRESH fax_receivers.
      LOOP AT receivers INTO fax_receivers WHERE receiver CS c_fax_email
. "'@fax.'.
        APPEND fax_receivers.
        DELETE receivers.
      ENDLOOP.
      IF NOT receivers[] IS INITIAL.
*-----------------------------------------------------------------------
*------%LT%LT%LT

*     send email:
        APPEND LINES OF object_content TO lt_longtext_email.
        CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
          EXPORTING
            document_data                    = document_data
*         DOCUMENT_TYPE                    = 'RAW'
*         PUT_IN_OUTBOX                    = ' '
            commit_work                      = 'X'
*       IMPORTING
*         SENT_TO_ALL                      =
*         NEW_OBJECT_ID                    =
          TABLES
*         OBJECT_HEADER                    =
            object_content                   = lt_longtext_email
*         CONTENTS_HEX                     =
*         OBJECT_PARA                      =
*         OBJECT_PARB                      =
            receivers                        = receivers
          EXCEPTIONS
            OTHERS                           = 8
                  .
        IF sy-subrc %LT%GT 0.
          MESSAGE e311 WITH 'Error sending email for plant:'
storage_wa-werks.
        ENDIF.
        DATA: l_lines TYPE i.
        CLEAR l_lines.
        DESCRIBE TABLE receivers LINES l_lines.
        ADD l_lines TO l_email_count.

* George Pavle March 21 2007
**---------------ins-------------------------------%LT%LT%LT
*
* for fax email recipients (e.g.1403319000@fax.cpr.ca) the email needs
**to be sent
* as PDF to ensure proper formating. To acieve this we need to create a
**spool for
* the message, then convert ot into PDF and finaly send this PDF as an
**email to
* the fax recipient e.g.1403319000@fax.cpr.ca

      ENDIF.
      IF NOT fax_receivers[] IS INITIAL.
*        need to convert this into PDF and fax it
        PERFORM fax_it TABLES lt_longtext_fax
                              fax_receivers
                              object_content
                       USING l_email_count
                             document_data-obj_descr.
      ENDIF.
*-----------------------------------------------------------------------
**------%LT%LT%LT
    ENDAT.
  ENDLOOP.


  WRITE:/ l_plant_count, 'plants have been selected.'.
  WRITE:/ l_email_count, 'emails have been sent.'.

ENDFORM.                    "send_emails



************************************************************************
*********
FORM fax_it
************************************************************************
*********
            TABLES lt_longtext_fax
                   fax_receivers STRUCTURE somlreci1
                   object_content STRUCTURE solisti1
            USING l_email_count
                  l_subject.


  DATA: l_params TYPE pri_params,
        l_valid_flag,
        l_rqident LIKE tsp01-rqident,
        l_rqcretime LIKE  tsp01-rqcretime,
        l_bytecount TYPE i,
        lt_pdf_output TYPE STANDARD TABLE OF tline WITH HEADER LINE,
        l_text(255).

*        get current print parameters to prepare a new spool:
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      mode           = 'CURRENT'
      no_dialog      = 'X'
      line_size      = 116 "sy-LINSZ
    IMPORTING
      out_parameters = l_params
      valid          = l_valid_flag.
  l_params-primm = ' '. "Do not print immediatelly
  CONCATENATE 'MM' storage_wa-werks sy-uzeit INTO l_params-plist.
  l_params-pexpi = 1.  "retention for 1 day only

*        the following block will create a separate spool for this fax:
  NEW-PAGE PRINT ON PARAMETERS l_params NO DIALOG.
  LOOP AT lt_longtext_fax.
    CHECK NOT lt_longtext_fax IS INITIAL.
    l_text = lt_longtext_fax+0(1).
    IF l_text IS INITIAL.
*                  just a line break
      WRITE / lt_longtext_fax.
    ELSE.
      WRITE lt_longtext_fax.
    ENDIF.
  ENDLOOP.
  LOOP AT object_content.
    WRITE / object_content-line.
  ENDLOOP.
  NEW-PAGE PRINT OFF.


*        to make sure that the following SELECT will have the new spool
**available:
  COMMIT WORK AND WAIT.

  CLEAR l_rqident.

*        now we get the spool number:
  SELECT rqident rqcretime INTO (l_rqident, l_rqcretime)
         UP TO 1 ROWS
         FROM tsp01
         WHERE rq2name = l_params-plist
           AND rqowner = sy-uname
         ORDER BY rqcretime DESCENDING.
  ENDSELECT.

  CHECK NOT l_rqident IS INITIAL.

*        convert the spool into PDF:
  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid   = l_rqident
      no_dialog     = ' '
      dst_device    = 'LOCL'
    IMPORTING
      pdf_bytecount = l_bytecount
    TABLES
      pdf           = lt_pdf_output
    EXCEPTIONS
      OTHERS        = 11.


  IF sy-subrc %LT%GT 0.
    MESSAGE e311 WITH 'Error sending fax-email for plant:'
storage_wa-werks.
  ENDIF.

*        Transfer the 132-long strings to 255-long strings
*        as per Shelley's ZFI_SEND_SPOOL_PDF
  DATA: g_buffer TYPE string,
        it_mess_att LIKE solisti1 OCCURS 0 WITH HEADER LINE.

  LOOP AT lt_pdf_output.
    TRANSLATE lt_pdf_output USING ' ~'.
    CONCATENATE g_buffer lt_pdf_output INTO g_buffer.
  ENDLOOP.

  TRANSLATE g_buffer USING '~ '.

  DO.
    it_mess_att = g_buffer.
    APPEND it_mess_att.
    SHIFT g_buffer LEFT BY 255 PLACES.
    IF g_buffer IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.



*        create and send a fax-email for each fax recipient:
  LOOP AT fax_receivers.
    PERFORM process_email TABLES it_mess_att
                           USING fax_receivers-receiver
                                 l_subject.
  ENDLOOP.

*        update the counter:
  DATA: l_lines TYPE i.
  CLEAR l_lines.
  DESCRIBE TABLE fax_receivers LINES l_lines.
  ADD l_lines TO l_email_count.
ENDFORM.                    "fax_it





************************************************************************
***********
* The following subroutines were copied from Shelley's program
**ZFI_SEND_SPOOL_PDF *
************************************************************************
***********

*---------------------------------------------------------------------*
*       FORM process_email                                            *
*---------------------------------------------------------------------*
FORM process_email TABLES it_mess_att STRUCTURE solisti1
                   USING recipient
                         l_subject.
  DATA: l_recsize TYPE i.

  DESCRIBE TABLE it_mess_att LINES l_recsize.
  CHECK l_recsize %GT 0.
  PERFORM send_faxemail TABLES it_mess_att
                         USING recipient
                               l_subject.
ENDFORM.                    "process_email
*&---------------------------------------------------------------------*
*&      Form  send_email
*&---------------------------------------------------------------------*
FORM send_faxemail TABLES it_mess_att STRUCTURE solisti1
                   USING  p_email
                          l_title LIKE sodocchgi1-obj_descr.

  CHECK NOT ( p_email IS INITIAL ).

  DATA: it_mess_bod LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        g_attachment_desc TYPE so_obj_nam,
        g_sender LIKE somlreci1-receiver,
        g_sender_type     LIKE soextreci1-adr_typ,
        g_error    TYPE sy-subrc,
        g_reciever TYPE sy-subrc.

  g_attachment_desc = 'Attachname'.
  IF g_sender EQ space.
    g_sender_type  = space.
  ELSE.
    g_sender_type  = 'INT'.
  ENDIF.


* Send file by email as .xls speadsheet
  PERFORM send_file_as_email_attachment
                               TABLES it_mess_bod
                                      it_mess_att
                                USING p_email
                                      l_title
                                      'PDF'
                                      'attach_name' "g_attachment_name
                                      g_attachment_desc
                                      g_sender
                                      g_sender_type
                             CHANGING g_error
                                      g_reciever.


ENDFORM.                    " send_email
*&---------------------------------------------------------------------*
*&      Form  send_file_as_email_attachment
*&---------------------------------------------------------------------*
FORM send_file_as_email_attachment TABLES it_message
                                          it_attach
                                    USING p_email
                                          p_mtitle
                                          p_format
                                          p_filename
                                          p_attdescription
                                          p_sender_address
                                          p_sender_addres_type
                                 CHANGING p_error
                                          p_reciever.


  DATA: ld_error    TYPE sy-subrc,
        ld_reciever TYPE sy-subrc,
        ld_mtitle LIKE sodocchgi1-obj_descr,
        ld_email LIKE  somlreci1-receiver,
        ld_format TYPE  so_obj_tp ,
        ld_attdescription TYPE  so_obj_nam ,
        ld_attfilename TYPE  so_obj_des ,
        ld_sender_address LIKE  soextreci1-receiver,
        ld_sender_address_type LIKE  soextreci1-adr_typ,
        ld_receiver LIKE  sy-subrc.

  DATA:   t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
          t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
          t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
          t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
          t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,
          w_cnt TYPE i,
          w_sent_all(1) TYPE c,
          w_doc_data LIKE sodocchgi1.


  ld_email   = p_email.
  ld_mtitle = p_mtitle.
  ld_format              = p_format.
  ld_attdescription      = p_attdescription.
  ld_attfilename         = p_filename.
  ld_sender_address      = p_sender_address.
  ld_sender_address_type = p_sender_addres_type.


* Fill the document data.
  w_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
  w_doc_data-obj_langu = sy-langu.
  w_doc_data-obj_name  = 'SAPRPT'.
  w_doc_data-obj_descr = ld_mtitle .
  w_doc_data-sensitivty = 'F'.

* Fill the document data and get size of attachment
  CLEAR w_doc_data.
  READ TABLE it_attach INDEX w_cnt.
  w_doc_data-doc_size =
     ( w_cnt - 1 ) * 255 + STRLEN( it_attach ).
  w_doc_data-obj_langu  = sy-langu.
  w_doc_data-obj_name   = 'SAPRPT'.
  w_doc_data-obj_descr  = ld_mtitle.
  w_doc_data-sensitivty = 'F'.
  CLEAR t_attachment.
  REFRESH t_attachment.
  t_attachment[] = it_attach[].

* Describe the body of the message
  CLEAR t_packing_list.
  REFRESH t_packing_list.
  t_packing_list-transf_bin = space.
  t_packing_list-head_start = 1.
  t_packing_list-head_num = 0.
  t_packing_list-body_start = 1.
  DESCRIBE TABLE it_message LINES t_packing_list-body_num.
  t_packing_list-doc_type = 'RAW'.
  APPEND t_packing_list.

* Create attachment notification
  t_packing_list-transf_bin = 'X'.
  t_packing_list-head_start = 1.
  t_packing_list-head_num   = 1.
  t_packing_list-body_start = 1.

  DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
  t_packing_list-doc_type   =  ld_format.
  t_packing_list-obj_descr  =  ld_attdescription.
  t_packing_list-obj_name   =  ld_attfilename.
  t_packing_list-doc_size   =  t_packing_list-body_num * 255.
  APPEND t_packing_list.

* Add the recipients email address
  CLEAR t_receivers.
  REFRESH t_receivers.
  t_receivers-receiver = ld_email.
  t_receivers-rec_type = 'U'.
  t_receivers-com_type = 'INT'.
  t_receivers-notif_del = 'X'.
  t_receivers-notif_ndel = 'X'.
  APPEND t_receivers.

  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
       EXPORTING
            document_data              = w_doc_data
            put_in_outbox              = 'X'
            sender_address             = ld_sender_address
            sender_address_type        = ld_sender_address_type
*            commit_work                = 'X'
       IMPORTING
            sent_to_all                = w_sent_all
       TABLES
            packing_list               = t_packing_list
            contents_bin               = t_attachment
            contents_txt               = it_message
            receivers                  = t_receivers
       EXCEPTIONS
            too_many_receivers         = 1
            document_not_sent          = 2
            document_type_not_exist    = 3
            operation_no_authorization = 4
            parameter_error            = 5
            x_error                    = 6
            enqueue_error              = 7
            OTHERS                     = 8.

*--- Start of change by santala on 6th Feb'07 by SD0K951414
  IF sy-subrc = 0.
    COMMIT WORK.
  ENDIF.
*--- End of change by santala on 6th Feb'07 by SD0K951414

* Populate zerror return code
  ld_error = sy-subrc.

* Populate zreceiver return code
  LOOP AT t_receivers.
    ld_receiver = t_receivers-retrn_code.
  ENDLOOP.
ENDFORM.                    " send_file_as_email_attachment

* begin of insert SD0K954028
*&---------------------------------------------------------------------*
*&      Form  download
*&---------------------------------------------------------------------*
*       download data to server - fields based on download layout from
*                                 selection screen
*----------------------------------------------------------------------*
FORM download .

  DATA: l_physical_file TYPE char255,
        l_file_msg(100) TYPE c,
        l_cccategory    TYPE cccategory,
        l_field(6)      VALUE 'G_RPT-',
        l_field1(20)    TYPE c,
        l_index(3)      TYPE n,
        l_filename_part(7),
        l_file(400),
        l_char(40),
        l_offset(3)     TYPE n.
  .

  FIELD-SYMBOLS: %LTl_field1%GT TYPE ANY.

  IF g_rpt[] IS INITIAL.   "no records to write to a file
    EXIT.
  ENDIF.

  PERFORM get_fieldcat.
  IF rt_dbfieldcat IS INITIAL.
* message - no fields to download
    MESSAGE s002(zm) WITH
    'No fields to download. Check the download layout.'.
    EXIT.
  ENDIF.


* read the file name
  CALL FUNCTION 'FILE_GET_NAME'
    EXPORTING
      logical_filename = c_logical_file_name
      eleminate_blanks = ' '
    IMPORTING
      file_name        = l_physical_file
    EXCEPTIONS
      file_not_found   = 1.
  IF sy-subrc NE 0.
    WRITE:
    / '************************ Error ******************************',
    / 'Logical File name', c_logical_file_name, 'not defined',
    / 'Call support to check Filename/Path maintenance',
    / '************************ Error ******************************'.
    STOP.
  ENDIF.

* modify file name if not a production client
  SELECT SINGLE cccategory INTO l_cccategory
         FROM t000
         WHERE mandt EQ sy-mandt.
  IF l_cccategory EQ 'P'.
  ELSE.
    CONCATENATE sy-sysid '.txt' INTO l_filename_part.
    REPLACE '.txt' WITH l_filename_part INTO l_physical_file.
  ENDIF.

  DELETE DATASET l_physical_file.

** open file
  OPEN DATASET l_physical_file FOR APPENDING
       MESSAGE l_file_msg IN LEGACY TEXT MODE.

  IF sy-subrc NE 0.
    WRITE:
    / '************************ Error ******************************',
    / 'Open Error:', l_file_msg, 'file', l_physical_file,
    / '************************ Error ******************************'.
    STOP.
  ENDIF.

* for each data record, find fields to be downloaded from fieldcat table
** sourced
* from the download layout

  LOOP AT g_rpt.
    l_offset = 0.

    LOOP AT rt_dbfieldcat INTO st_dbfieldcat.
      l_index = sy-tabix.
      CONCATENATE l_field st_dbfieldcat-key1 INTO l_field1.
      ASSIGN (l_field1) TO %LTl_field1%GT.
      READ TABLE rt_dbfieldcat_len INTO st_dbfieldcat_len
        WITH KEY key1 = st_dbfieldcat-key1.
      IF sy-subrc NE 0.
        CONTINUE.
      ENDIF.
* inserting spaces between fields
      l_char = %LTl_field1%GT.
      CONDENSE l_char.
      l_file+l_offset = l_char.
      l_offset = l_offset + st_dbfieldcat_len-value + 2.

*      concatenate l_file  into l_file separated by space.
    ENDLOOP.
    TRANSFER l_file TO l_physical_file.
  ENDLOOP.

  CLOSE DATASET l_physical_file.
  IF sy-subrc = 0.
    MESSAGE s002(zm) WITH 'Data downloaded successfully'.
  ENDIF.

ENDFORM.                    " download
*&---------------------------------------------------------------------*
*&      Form  get_fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_fieldcat .

* gets fields from download variant to be used in output file to server
  DATA: l_rs_varkey     LIKE ltdxkey.
  DATA: l_variant       LIKE disvariant.
  DATA: l_dbfieldcat    TYPE STANDARD TABLE OF ltdxdata,
        l_st_dbfieldcat TYPE ltdxdata.

  CLEAR: rt_dbfieldcat,
         rt_dbfieldcat_len,
         rt_dbsortinfo,
         rt_dbfilter,
         rt_dblayout.

  MOVE sy-repid TO l_variant-report.
  MOVE p_varid  TO l_variant-variant.

  CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
    EXPORTING
      i_save        = 'A'
    CHANGING
      cs_variant    = l_variant
    EXCEPTIONS
      wrong_input   = 1
      not_found     = 2
      program_error = 3
      OTHERS        = 4.
  CASE sy-subrc.
    WHEN 0.
    WHEN 2.
* message - layout not found
      MESSAGE e002(zm) WITH 'Download layout not found.'.
      EXIT.
    WHEN OTHERS.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
         WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDCASE.

  SELECT  type INTO l_rs_varkey-type FROM v_ltdx
    WHERE relid    = 'LT' AND
          report   = sy-repid AND
          username = l_variant-username AND
          variant  = p_varid.
  ENDSELECT.
  IF sy-subrc NE 0.
    EXIT.
  ENDIF.

  MOVE-CORRESPONDING l_variant TO l_rs_varkey.

  IMPORT lt_dbfieldcat TO rt_dbfieldcat
         lt_dbsortinfo TO rt_dbsortinfo
         lt_dbfilter   TO rt_dbfilter
         lt_dblayout   TO rt_dblayout
                       FROM  DATABASE ltdx(lt)
                       ID l_rs_varkey.

*process table.
  l_dbfieldcat[] = rt_dbfieldcat[].
  LOOP AT l_dbfieldcat INTO l_st_dbfieldcat.
    IF l_st_dbfieldcat-param = 'NO_OUT' AND
        l_st_dbfieldcat-value = 'X'.
      DELETE rt_dbfieldcat WHERE key1 = l_st_dbfieldcat-key1.
    ENDIF.
  ENDLOOP.

  LOOP AT rt_dbfieldcat INTO l_st_dbfieldcat WHERE param EQ 'OUTPUTLEN'.
    APPEND l_st_dbfieldcat TO rt_dbfieldcat_len.
  ENDLOOP.

  DELETE rt_dbfieldcat WHERE param NE 'COL_POS'.
  SORT rt_dbfieldcat BY value.

ENDFORM.                    " get_fieldcat
*&---------------------------------------------------------------------*
*&      Form  check_download_inputs
*&---------------------------------------------------------------------*
form check_download_inputs .

  DATA: l_variant       LIKE disvariant.

  if p_down is initial.  "download not requested
    exit.
  endif.

  IF p_varid IS INITIAL.
    MESSAGE e002(zm) WITH
     'Download requested but no file layout specified'."SD0K954028
  ENDIF.


  MOVE sy-repid TO l_variant-report.
  MOVE p_varid  TO l_variant-variant.

  CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
    EXPORTING
      i_save        = 'A'
    CHANGING
      cs_variant    = l_variant
    EXCEPTIONS
      wrong_input   = 1
      not_found     = 2
      program_error = 3
      OTHERS        = 4.
  CASE sy-subrc.
    WHEN 0.
    WHEN 2.
* message - layout not found
      MESSAGE e002(zm) WITH 'Download layout not found.'.
      EXIT.
    WHEN OTHERS.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
         WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDCASE.

endform.                    " check_download_inputs
* end of insert SD0K954028


*Selection texts
*----------------------------------------------------------
* P_DELET         Delete
* P_DOWN         Downlolad
* P_EMAIL         Email
* P_VARID         Variant ID
* P_ZERO         Zero
* S_DISPO         MRP Controller (Materials Plan
* S_LBLAB         Change in Stock: Provision
* S_LGORT         Storage Location
* S_LGPBE         Storage Bin
* S_MATKL         Material Group
* S_MATNR         Material Number
* S_MTART         Material Type
* S_PERI1         Period 1
* S_PERI2         Period 2
* S_PERI3         Period 3
* S_VARI         Variant 1
* S_VARI2         Variant 2
* S_WERKS         Plant


*Messages
*----------------------------------------------------------
*
* Message class: 5D
*311   & & & &
*
* Message class: AQ
*260   No data was selected
*
* Message class: ZM
*002


*Selection texts
*----------------------------------------------------------
* P_DELET         Delete
* P_DOWN         Downlolad
* P_EMAIL         Email
* P_VARID         Variant ID
* P_ZERO         Zero
* S_DISPO         MRP Controller (Materials Plan
* S_LBLAB         Change in Stock: Provision
* S_LGORT         Storage Location
* S_LGPBE         Storage Bin
* S_MATKL         Material Group
* S_MATNR         Material Number
* S_MTART         Material Type
* S_PERI1         Period 1
* S_PERI2         Period 2
* S_PERI3         Period 3
* S_VARI         Variant 1
* S_VARI2         Variant 2
* S_WERKS         Plant


*Messages
*----------------------------------------------------------
*
* Message class: 5D
*311   & & & &
*
* Message class: AQ
*260   No data was selected
*
* Message class: ZM
*002


*Selection texts
*----------------------------------------------------------
* P_DELET         Delete
* P_DOWN         Downlolad
* P_EMAIL         Email
* P_VARID         Variant ID
* P_ZERO         Zero
* S_DISPO         MRP Controller (Materials Plan
* S_LBLAB         Change in Stock: Provision
* S_LGORT         Storage Location
* S_LGPBE         Storage Bin
* S_MATKL         Material Group
* S_MATNR         Material Number
* S_MTART         Material Type
* S_PERI1         Period 1
* S_PERI2         Period 2
* S_PERI3         Period 3
* S_VARI         Variant 1
* S_VARI2         Variant 2
* S_WERKS         Plant


*Messages
*----------------------------------------------------------
*
* Message class: 5D
*311   & & & &
*
* Message class: AQ
*260   No data was selected
*
* Message class: ZM
*002


*Selection texts
*----------------------------------------------------------
* P_DELET         Delete
* P_DOWN         Downlolad
* P_EMAIL         Email
* P_VARID         Variant ID
* P_ZERO         Zero
* S_DISPO         MRP Controller (Materials Plan
* S_LBLAB         Change in Stock: Provision
* S_LGORT         Storage Location
* S_LGPBE         Storage Bin
* S_MATKL         Material Group
* S_MATNR         Material Number
* S_MTART         Material Type
* S_PERI1         Period 1
* S_PERI2         Period 2
* S_PERI3         Period 3
* S_VARI         Variant 1
* S_VARI2         Variant 2
* S_WERKS         Plant


*Messages
*----------------------------------------------------------
*
* Message class: 5D
*311   & & & &
*
* Message class: AQ
*260   No data was selected
*
* Message class: ZM
*002

----
  • No labels