Skip to Content
0
Former Member
Jan 17, 2006 at 07:58 AM

Serious Performance Problems in ABAP Reports

544 Views

Hi All,

We are developing ABAP reports for SAP IS-U/CCS Modules and facing Performance issues.Whole scenario is given below.Please suggest some solution.

1. Total No. Of Business Partners = 1500000

2. Reports Selection Criteria are of two types:

a) GSBER(Business Area) - Selection for atleast 200000 Business Partners

b) Cokey(Division) - Selection for around 1000 to 50000 Business Partners

3. For implementing our reports logic we have to access several tables all of which are very large.They are :

Table Name No. of records (Appx)

DBERCHV 20000000

DBERCHZ1 20000000

DBERDLB 20000000

DFKKKO 20000000

DFKKOP 20000000

EANLH 4000000

ERCH 10000000

ERCHC 10000000

ETTIFN 30000000

EVER 1500000

FKKVKP 1500000

TECOKT 500

TGSBT 12

4. Due to large no. of records we are facing problems at two levels:

a) OpenSQL Statement is taking too much time for data selection

b) Since large no. of records are selected Corresponding loops and data processing also takes much time

5. We have tried almost all ABAP Performance optimization techniques such as using Index, SQL optimization techniques,Read Table Optimization, Loop Statement Optimization etc. but there is not much improvement.

6. For example one of our Reports "R15" takes around 1500 seconds for 1000 Business Partners.

Its Code is attached below:

<b>a) ZISU_SCHL_LTR15_BAPI - Program which schedules actual R15 report in background</b>

*&---------------------------------------------------------------------*
*& Report  ZISU_SCHL_LTR15_BAPI                                        *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Developed By : Piyusha Kirwai                                       *
*& Date         : 02/12/2005                                           *
*& Purpose      : To Schedule the LT R 15 prog in background and store
*& the File for Manual R-15 into server.
*&---------------------------------------------------------------------*

REPORT  ZISU_SCHL_LTR15_BAPI  NO STANDARD PAGE HEADING.

TABLES ZEVERFKKVKP.

DATA:  DYFIELDS LIKE DYNPREAD OCCURS 0 WITH HEADER LINE.

DATA: IT_TAB TYPE FILETABLE,
      GD_SUBRC TYPE I.

DATA: LV_GSBER TYPE TGSBT-GSBER.

RANGES R_COKEY FOR ZEVERFKKVKP-COKEY.

DATA: BEGIN OF GT_TECOKT OCCURS 100,
        COKEY TYPE TECOKT-COKEY,
        LTEXT TYPE TECOKT-LTEXT,
      END   OF GT_TECOKT,

      BEGIN OF GT_TE422 OCCURS 100,
        TERMSCHL TYPE TE422-TERMSCHL,
        TERMTEXT TYPE TE422-TERMTEXT,
      END   OF GT_TE422.


DATA: BEGIN OF GWA_MANUAL_DATA,
        COL_1(5)        TYPE C,
        COL_2(10)       TYPE C,
        COL_3(40)       TYPE C,
        COL_4(40)       TYPE C,
        COL_5(10)       TYPE C,
        COL_6(19)       TYPE C,
        COL_7(19)       TYPE C,
        COL_8(19)       TYPE C,
        COL_9(19)       TYPE C,
        COL_10(19)      TYPE C,
        COL_11(19)      TYPE C,
        COL_12(10)      TYPE C,
        COL_13(19)      TYPE C,
        COL_14(19)      TYPE C,
        COL_15(19)      TYPE C,
        COL_16(19)      TYPE C,
        COL_17(19)      TYPE C,
        COL_18(19)      TYPE C,
      END   OF GWA_MANUAL_DATA,

      GT_MANUAL_DATA LIKE GWA_MANUAL_DATA OCCURS 0,

      GT_INTERN_DATA TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

*&-----FOR UPLOADING FILE TO SERVER------------------------------------*
DATA: LV_SERVER_FILE_PREFIX(60) TYPE C.
DATA: LV_SERVER_DATAFILE_NAME(60) TYPE C.
DATA: LV_SERVER_ERRFILE_NAME(60) TYPE C.
DATA: ENDT LIKE SY-UZEIT,
      ENDD LIKE SY-DATUM,
      JOBCOUNT TYPE TBTCJOB-JOBCOUNT.

*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK 001 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: SO_GSBER FOR ZEVERFKKVKP-GSBER OBLIGATORY,
                SO_COKEY FOR ZEVERFKKVKP-COKEY OBLIGATORY,
                SO_MRU   FOR ZEVERFKKVKP-ABLEINH.

PARAMETERS:     P_BMNTH(7) TYPE C OBLIGATORY.
SELECT-OPTIONS: SO_GPART FOR ZEVERFKKVKP-GPART.
SELECTION-SCREEN END  OF BLOCK 001.


SELECTION-SCREEN BEGIN OF BLOCK 002 WITH FRAME TITLE TEXT-003.
PARAMETERS:  P_COMPUT RADIOBUTTON GROUP R15,
             P_INCLUD RADIOBUTTON GROUP R15,
             P_FILE  TYPE  RLGRAP-FILENAME MODIF ID ACT.
SELECTION-SCREEN END   OF BLOCK 002.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 2(83) TEXT-004.
SELECTION-SCREEN END   OF LINE.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 8(40) TEXT-005.
*SELECTION-SCREEN END   OF LINE.

SELECTION-SCREEN BEGIN OF BLOCK 003 WITH FRAME TITLE TEXT-002.
PARAMETERS   P_SCHL TYPE C AS CHECKBOX.
PARAMETERS : P_IMMED RADIOBUTTON GROUP SCH ,
             P_DT_TM  RADIOBUTTON GROUP SCH,
             P_DATE TYPE SY-DATUM MODIF ID SHL,
             P_TIME TYPE SY-UZEIT MODIF ID SHL.
SELECTION-SCREEN END   OF BLOCK 003.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON SO_GSBER.

  IF NOT SO_GSBER-LOW IS INITIAL.

    SELECT SINGLE GSBER FROM TGSBT INTO LV_GSBER
      WHERE GSBER = SO_GSBER-LOW.
    IF SY-SUBRC <> 0.
      MESSAGE E007(ZISU).
    ENDIF.
  ENDIF.

  IF NOT SO_GSBER-HIGH IS INITIAL .
    SELECT SINGLE GSBER FROM TGSBT INTO LV_GSBER
      WHERE GSBER = SO_GSBER-HIGH.
    IF SY-SUBRC <> 0.
      MESSAGE E007(ZISU).
    ENDIF.
  ENDIF.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON P_BMNTH.
**check the validity of Billing month format and for valid billing month
  IF P_BMNTH CO '0123456789/'.
    IF P_BMNTH CP '++++/++'.
      IF P_BMNTH+5(2) > 12.
        MESSAGE E002(ZISU) WITH 'from'.
      ENDIF.

** current year is less than year entered
      IF SY-DATUM(4) < P_BMNTH(4).
        MESSAGE E003(ZISU) WITH 'from'.
      ELSEIF SY-DATUM(4) = P_BMNTH(4).
** month in future
        IF SY-DATUM+4(2) < P_BMNTH+5(2).
          MESSAGE E004(ZISU) WITH 'from'.
        ENDIF.
      ENDIF.
    ELSE.
      MESSAGE E001(ZISU) WITH 'from'.
    ENDIF.
  ELSE.
**  entry have some invalid char
    MESSAGE E010(ZISU).
  ENDIF.
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR SO_COKEY-LOW.
**for geting the CO account assigment key of the entered Business area
  CLEAR: DYFIELDS[], DYFIELDS.

  PERFORM DIVISON_DATA_GET CHANGING SO_COKEY-LOW.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR SO_COKEY-HIGH.
**for geting the CO account assigment key of the entered Business area
  CLEAR: DYFIELDS[], DYFIELDS.

  PERFORM DIVISON_DATA_GET CHANGING SO_COKEY-HIGH.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR SO_MRU-LOW.
  CLEAR: DYFIELDS[], DYFIELDS.

  PERFORM MRU_DATA_GET CHANGING SO_MRU-LOW.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR SO_MRU-HIGH.
  CLEAR: DYFIELDS[], DYFIELDS.

  PERFORM MRU_DATA_GET CHANGING SO_MRU-HIGH.


*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

  REFRESH: IT_TAB.
**Opens File Open Dialog Box for selecting input file.
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE     = 'Select File'
      DEFAULT_FILENAME = '*.xls'
      MULTISELECTION   = ' '
    CHANGING
      FILE_TABLE       = IT_TAB
      RC               = GD_SUBRC.

  LOOP AT IT_TAB INTO P_FILE.
*    so_fpath-sign = 'I'.
*    so_fpath-option = 'EQ'.
*    append so_fpath.
  ENDLOOP.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.

    IF SCREEN-GROUP1 = 'ACT'.
      IF P_INCLUD = ' '.
        SCREEN-INPUT = '0'.
      ELSE.
        SCREEN-INPUT = '1'.
      ENDIF.
    ENDIF.

    IF SCREEN-GROUP1 = 'SHL'.
      IF P_DT_TM = 'X'.
        SCREEN-INPUT = '1'.
      ELSE.
        SCREEN-INPUT = '0'.
      ENDIF.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.
*&---------------------------------------------------------------------*
INITIALIZATION.
  P_DATE = SY-DATUM.

  P_TIME = SY-UZEIT + 180.

*&---------------------------------------------------------------------*
START-OF-SELECTION.
  IF P_INCLUD = 'X' AND P_FILE IS INITIAL.
    MESSAGE 'Enter the Manual R-15 File' TYPE 'E'.
  ENDIF.

*  IF P_INCLUD = 'X'.
*    PERFORM UPLOAD_EXCEL_FILE.
*
*    CONCATENATE 'R15' SY-DATUM '-' SY-UZEIT INTO LV_SERVER_FILE_PREFIX.
*    CONCATENATE LV_SERVER_FILE_PREFIX '-' 'LT.TMP' INTO
*    LV_SERVER_DATAFILE_NAME.
*
*    OPEN DATASET LV_SERVER_DATAFILE_NAME
*      FOR OUTPUT
*      IN TEXT MODE
*      ENCODING DEFAULT.
*
*    LOOP AT GT_MANUAL_DATA INTO GWA_MANUAL_DATA.
*      IF ( GWA_MANUAL_DATA-COL_6 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_7 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_8 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_9 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_10 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_11 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_13 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_14 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_15 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_16 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_17 CN '0123456789 '
*      AND GWA_MANUAL_DATA-COL_18 CN '0123456789 ' ).
*
*        MESSAGE 'Character Data in Numerical Field' TYPE 'E'.
*
*      ENDIF.
*
*      TRANSFER GWA_MANUAL_DATA TO LV_SERVER_DATAFILE_NAME.
*
*    ENDLOOP.
*
*    CLOSE DATASET LV_SERVER_DATAFILE_NAME.
*
*  ENDIF.  "  P_INCLUD = 'X'.

  IF P_SCHL = 'X'.

    IF P_IMMED = 'X'.
      CALL FUNCTION 'C14B_ADD_TIME'
        EXPORTING
          I_STARTTIME = SY-UZEIT
          I_STARTDATE = SY-DATUM
          I_ADDTIME   = '000010'
        IMPORTING
          E_ENDTIME   = ENDT
          E_ENDDATE   = ENDD.

    ELSEIF P_DT_TM = 'X'.
      ENDD = P_DATE.
      ENDT = P_TIME.
    ENDIF.

    CALL FUNCTION 'JOB_OPEN'
      EXPORTING
*     DELANFREP              = ' '
*     JOBGROUP               = ' '
        JOBNAME                = 'R15JOB'
*      SDLSTRTDT              = ENDD
*      SDLSTRTTM              = ENDT
*     JOBCLASS               =
     IMPORTING
       JOBCOUNT               = JOBCOUNT
*   CHANGING
*     RET                    =
*   EXCEPTIONS
*     CANT_CREATE_JOB        = 1
*     INVALID_JOB_DATA       = 2
*     JOBNAME_MISSING        = 3
*     OTHERS                 = 4
              .
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    IF P_INCLUD = 'X'.
      PERFORM UPLOAD_EXCEL_FILE.

     CONCATENATE 'R15' SY-DATUM '-' SY-UZEIT INTO LV_SERVER_FILE_PREFIX.
      CONCATENATE LV_SERVER_FILE_PREFIX '-' JOBCOUNT 'LT.TMP' INTO
      LV_SERVER_DATAFILE_NAME.

      CONDENSE LV_SERVER_DATAFILE_NAME.

      OPEN DATASET LV_SERVER_DATAFILE_NAME
        FOR OUTPUT
        IN TEXT MODE
        ENCODING DEFAULT.

      IF SY-SUBRC NE 0.
        MESSAGE 'Error in Creating File on Server' TYPE 'E'.
      ENDIF.

      LOOP AT GT_MANUAL_DATA INTO GWA_MANUAL_DATA.
        IF ( GWA_MANUAL_DATA-COL_6 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_7 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_8 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_9 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_10 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_11 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_13 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_14 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_15 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_16 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_17 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_18 CN '0123456789 ' ).

          MESSAGE 'Character Data in Numerical Field' TYPE 'E'.

        ENDIF.

        TRANSFER GWA_MANUAL_DATA TO LV_SERVER_DATAFILE_NAME.

      ENDLOOP.

      CLOSE DATASET LV_SERVER_DATAFILE_NAME.

      IF SY-SUBRC NE 0.
        MESSAGE 'Error in Creating File on Server' TYPE 'E'.
      ENDIF.

    ENDIF.  "  P_INCLUD = 'X'.

    SUBMIT ZISU_LTR15_TUNE
      WITH SO_GSBER IN SO_GSBER
      WITH SO_COKEY IN SO_COKEY
      WITH SO_MRU   IN SO_MRU
      WITH SO_GPART IN SO_GPART
      WITH P_BMNTH  =  P_BMNTH
      WITH P_COMPUT = P_COMPUT
      WITH P_INCLUD = P_INCLUD
      WITH P_FILE   = LV_SERVER_DATAFILE_NAME

      USER SY-UNAME VIA JOB 'R15JOB' NUMBER JOBCOUNT AND RETURN.

    CALL FUNCTION 'JOB_CLOSE'
      EXPORTING
*   AT_OPMODE                         = ' '
*   AT_OPMODE_PERIODIC                = ' '
*   CALENDAR_ID                       = ' '
*   EVENT_ID                          = ' '
*   EVENT_PARAM                       = ' '
*   EVENT_PERIODIC                    = ' '
        JOBCOUNT                          = JOBCOUNT
        JOBNAME                           = 'R15JOB'
*   LASTSTRTDT                        = NO_DATE
*   LASTSTRTTM                        = NO_TIME
*   PRDDAYS                           = 0
*   PRDHOURS                          = 0
*   PRDMINS                           = 0
*   PRDMONTHS                         = 0
*   PRDWEEKS                          = 0
*   PREDJOB_CHECKSTAT                 = ' '
*   PRED_JOBCOUNT                     = ' '
*   PRED_JOBNAME                      = ' '
        SDLSTRTDT                         = ENDD
        SDLSTRTTM                         = ENDT
*   STARTDATE_RESTRICTION             = BTC_PROCESS_ALWAYS
        STRTIMMED                         = P_IMMED
*   TARGETSYSTEM                      = ' '
*   START_ON_WORKDAY_NOT_BEFORE       = SY-DATUM
*   START_ON_WORKDAY_NR               = 0
*   WORKDAY_COUNT_DIRECTION           = 0
*   RECIPIENT_OBJ                     =
*   TARGETSERVER                      = ' '
*   DONT_RELEASE                      = ' '
*   TARGETGROUP                       = ' '
*   DIRECT_START                      =
* IMPORTING
*   JOB_WAS_RELEASED                  =
* CHANGING
*   RET                               =
* EXCEPTIONS
*   CANT_START_IMMEDIATE              = 1
*   INVALID_STARTDATE                 = 2
*   JOBNAME_MISSING                   = 3
*   JOB_CLOSE_FAILED                  = 4
*   JOB_NOSTEPS                       = 5
*   JOB_NOTEX                         = 6
*   LOCK_FAILED                       = 7
*   INVALID_TARGET                    = 8
*   OTHERS                            = 9
              .
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.


  ELSE.  " IF NOT TO RUN IN BACKGROUND

    IF P_INCLUD = 'X'.
      PERFORM UPLOAD_EXCEL_FILE.

     CONCATENATE 'R15' SY-DATUM '-' SY-UZEIT INTO LV_SERVER_FILE_PREFIX.
      CONCATENATE LV_SERVER_FILE_PREFIX '-' 'LT.TMP' INTO
      LV_SERVER_DATAFILE_NAME.

      CONDENSE LV_SERVER_DATAFILE_NAME.

      OPEN DATASET LV_SERVER_DATAFILE_NAME
        FOR OUTPUT
        IN TEXT MODE
        ENCODING DEFAULT.

      IF SY-SUBRC NE 0.
        MESSAGE 'Error in Creating File on Server' TYPE 'E'.
      ENDIF.

      LOOP AT GT_MANUAL_DATA INTO GWA_MANUAL_DATA.
        IF ( GWA_MANUAL_DATA-COL_6 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_7 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_8 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_9 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_10 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_11 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_13 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_14 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_15 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_16 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_17 CN '0123456789 '
        AND GWA_MANUAL_DATA-COL_18 CN '0123456789 ' ).

          MESSAGE 'Character Data in Numerical Field' TYPE 'E'.

        ENDIF.

        TRANSFER GWA_MANUAL_DATA TO LV_SERVER_DATAFILE_NAME.

      ENDLOOP.

      CLOSE DATASET LV_SERVER_DATAFILE_NAME.

      IF SY-SUBRC NE 0.
        MESSAGE 'Error in Creating File on Server' TYPE 'E'.
      ENDIF.

    ENDIF.  "  P_INCLUD = 'X'.

    IF NOT SY-BATCH IS INITIAL.

      SUBMIT ZISU_LTR15_TUNE
        WITH SO_GSBER IN SO_GSBER
        WITH SO_COKEY IN SO_COKEY
        WITH SO_MRU   IN SO_MRU
        WITH SO_GPART IN SO_GPART
        WITH P_BMNTH  =  P_BMNTH
        WITH P_COMPUT = P_COMPUT
        WITH P_INCLUD = P_INCLUD
        WITH P_FILE   = LV_SERVER_DATAFILE_NAME
        TO SAP-SPOOL WITHOUT SPOOL DYNPRO.

    ELSE.

      SUBMIT ZISU_LTR15_TUNE
        WITH SO_GSBER IN SO_GSBER
        WITH SO_COKEY IN SO_COKEY
        WITH SO_MRU   IN SO_MRU
        WITH SO_GPART IN SO_GPART
        WITH P_BMNTH  =  P_BMNTH
        WITH P_COMPUT = P_COMPUT
        WITH P_INCLUD = P_INCLUD
        WITH P_FILE   = LV_SERVER_DATAFILE_NAME.

    ENDIF.

  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  divison_data_get
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_SO_COKEY_LOW  text
*----------------------------------------------------------------------*
FORM DIVISON_DATA_GET  CHANGING P_SO_COKEY_LOW.
**to get the search help for division
  DATA : LV_LINES TYPE SY-TFILL,
        LT_RETURN_TAB LIKE DDSHRETVAL OCCURS 0, " with header line.
        LWA_RETURN_TAB LIKE DDSHRETVAL.

  DATA: LV_COKEY TYPE ZEVERFKKVKP-COKEY.

**according to the Business area entered]
*  break csebdev1.
  REFRESH R_COKEY[].
  DESCRIBE TABLE SO_GSBER LINES LV_LINES.
**  when user has neither pressed the enter key nor selected the values
**using search help
  IF LV_LINES = 0.
*  loop at so_gsber.
    CLEAR R_COKEY.
*    if so_gsber-high is initial.
*    break csebdev1.
    DYFIELDS-FIELDNAME = 'SO_GSBER-LOW'.
    APPEND DYFIELDS.
    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        DYNAME     = SY-CPROG
        DYNUMB     = SY-DYNNR
      TABLES
        DYNPFIELDS = DYFIELDS.
*      r_cokey-sign = 'I'.
*      r_cokey-option = 'CP'.
*r_cokey-low = so_gsber-low+0(2).

    CONCATENATE DYFIELDS-FIELDVALUE+0(2) '*' INTO R_COKEY-LOW.

*    elseif not so_gsber-high is initial.



    DYFIELDS-FIELDNAME = 'SO_GSBER-HIGH'.
    APPEND DYFIELDS.
    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        DYNAME     = SY-CPROG
        DYNUMB     = SY-DYNNR
      TABLES
        DYNPFIELDS = DYFIELDS.

    IF NOT DYFIELDS-FIELDVALUE IS INITIAL.
      CONCATENATE DYFIELDS-FIELDVALUE+0(2) '99999999' INTO R_COKEY-HIGH.
    ENDIF.
*    endif.
    IF NOT R_COKEY-HIGH IS INITIAL.
      R_COKEY-SIGN = 'I'.
      R_COKEY-OPTION = 'BT'.
    ELSEIF R_COKEY-HIGH IS INITIAL.
      R_COKEY-SIGN = 'I'.
      R_COKEY-OPTION = 'CP'.
    ENDIF.
    APPEND R_COKEY.
  ENDIF.
*  endloop.
*  APPEND DYFIELDS.
  IF LV_LINES > 0.
*    break-point.
    IF NOT SO_GSBER[] IS INITIAL.
      LOOP AT SO_GSBER.
*        r_cokey-sign = 'I'.
*        r_cokey-option = 'CP'.
*r_cokey-low = so_gsber-low+0(2).
        CONCATENATE SO_GSBER-LOW+0(2) '*' INTO R_COKEY-LOW.
        IF NOT SO_GSBER-HIGH IS INITIAL.
          CONCATENATE SO_GSBER-HIGH+0(2) '99999999' INTO R_COKEY-HIGH.
        ENDIF.
        IF NOT R_COKEY-HIGH IS INITIAL.
          R_COKEY-SIGN = 'I'.
          R_COKEY-OPTION = 'BT'.
        ELSEIF R_COKEY-HIGH IS INITIAL.
          R_COKEY-SIGN = 'I'.
          R_COKEY-OPTION = 'CP'.
        ENDIF.

        APPEND R_COKEY TO R_COKEY.

      ENDLOOP.


    ENDIF.
  ENDIF.

  IF NOT R_COKEY[] IS INITIAL.
* break csebdev1.
    REFRESH GT_TECOKT[].
    SELECT COKEY LTEXT
    FROM TECOKT
    INTO TABLE GT_TECOKT
    WHERE COKEY IN R_COKEY AND SPRAS = 'EN'.
*    %_HINTS ORACLE '("TECOKT","TECOKT~1")'.

  ENDIF.

**now call the search help
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
*   DDIC_STRUCTURE         = ' '
      RETFIELD               = 'COKEY'
*   PVALKEY                = ' '
*   DYNPPROG               = ' '
*   DYNPNR                 = ' '
*   DYNPROFIELD            = ' '
*   STEPL                  = 0
     WINDOW_TITLE           = 'Division'
     VALUE                  = DYFIELDS-FIELDVALUE
     VALUE_ORG              = 'S'
*   MULTIPLE_CHOICE        = ' '
*   DISPLAY                = ' '
*   CALLBACK_PROGRAM       = ' '
*   CALLBACK_FORM          = ' '
*   MARK_TAB               =
* IMPORTING
*   USER_RESET             =
    TABLES
      VALUE_TAB              = GT_TECOKT
*   FIELD_TAB              =
     RETURN_TAB             = LT_RETURN_TAB
*   DYNPFLD_MAPPING        =
* EXCEPTIONS
*   PARAMETER_ERROR        = 1
*   NO_VALUES_FOUND        = 2
*   OTHERS                 = 3
            .
  IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
  READ TABLE LT_RETURN_TAB INTO LWA_RETURN_TAB INDEX 1.
  LV_COKEY = LWA_RETURN_TAB-FIELDVAL.
*break csebdev1.

ENDFORM.                    " divison_data_get
*&---------------------------------------------------------------------*
*&      Form  upload_excel_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM UPLOAD_EXCEL_FILE .
  DATA: LV_INDEX TYPE I.

  FIELD-SYMBOLS <VAL> TYPE ANY.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      FILENAME                = P_FILE
      I_BEGIN_COL             = 1
      I_BEGIN_ROW             = 8
      I_END_COL               = 18
      I_END_ROW               = 94
    TABLES
      INTERN                  = GT_INTERN_DATA
    EXCEPTIONS
      INCONSISTENT_PARAMETERS = 1
      UPLOAD_OLE              = 2
      OTHERS                  = 3.

  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  IF NOT GT_INTERN_DATA[] IS INITIAL.
    SORT GT_INTERN_DATA BY ROW COL.
    LOOP AT GT_INTERN_DATA.
      MOVE GT_INTERN_DATA-COL TO LV_INDEX.
      ASSIGN COMPONENT LV_INDEX OF STRUCTURE GWA_MANUAL_DATA TO <VAL>.
      MOVE GT_INTERN_DATA-VALUE TO <VAL>.
      AT END OF ROW.
        APPEND GWA_MANUAL_DATA TO GT_MANUAL_DATA.
        CLEAR GWA_MANUAL_DATA.
      ENDAT.
    ENDLOOP.
  ENDIF.


ENDFORM.                    " upload_excel_file

*&---------------------------------------------------------------------*
*&      Form  MRU_DATA_GET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_SO_MRU_LOW  text
*----------------------------------------------------------------------*
FORM MRU_DATA_GET  CHANGING LV_MRU.
**to get the search help for Group
  DATA : LV_LINES TYPE SY-TFILL,
        LT_RETURN_TAB LIKE DDSHRETVAL OCCURS 0,
        LWA_RETURN_TAB LIKE DDSHRETVAL.
*        lv_mru_p(3) type c.

  RANGES  R_MRU FOR EANLH-ABLEINH.
**according to the Business area entered
**  break csebdev1.
  REFRESH R_MRU[].
  DESCRIBE TABLE SO_GSBER LINES LV_LINES.
**  when user has neither pressed the enter key nor selected the values
**using search help
  IF LV_LINES = 0.
    CLEAR R_MRU[].

    DYFIELDS-FIELDNAME = 'SO_GSBER-LOW'.
    APPEND DYFIELDS.

    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        DYNAME     = SY-CPROG
        DYNUMB     = SY-DYNNR
      TABLES
        DYNPFIELDS = DYFIELDS.

    CONCATENATE DYFIELDS-FIELDVALUE+0(2) '*' INTO R_MRU-LOW.

    DYFIELDS-FIELDNAME = 'SO_GSBER-HIGH'.
    APPEND DYFIELDS.

    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        DYNAME     = SY-CPROG
        DYNUMB     = SY-DYNNR
      TABLES
        DYNPFIELDS = DYFIELDS.
    IF NOT DYFIELDS-FIELDVALUE IS INITIAL.
      CONCATENATE DYFIELDS-FIELDVALUE+0(2) '99999999' INTO R_MRU-HIGH.
    ENDIF.
    IF NOT R_MRU-HIGH IS INITIAL.
      R_MRU-SIGN   = 'I'.
      R_MRU-OPTION = 'BT'.
    ELSEIF R_MRU-HIGH IS INITIAL.
      R_MRU-SIGN   = 'I'.
      R_MRU-OPTION = 'CP'.
    ENDIF.

    APPEND R_MRU.
  ENDIF. " end lv_lines =0

  IF LV_LINES > 0.
    IF NOT SO_GSBER[] IS INITIAL.
      LOOP AT SO_GSBER.
        CONCATENATE SO_GSBER-LOW+0(2) '*' INTO R_MRU-LOW.
        IF NOT SO_GSBER-HIGH IS INITIAL.
          CONCATENATE SO_GSBER-HIGH+0(2) '99999999' INTO R_MRU-HIGH.
        ENDIF.

        IF R_MRU-HIGH IS INITIAL.
          R_MRU-SIGN   = 'I'.
          R_MRU-OPTION = 'CP'.
        ELSEIF NOT R_MRU-HIGH IS INITIAL.
          R_MRU-SIGN   = 'I'.
          R_MRU-OPTION = 'BT'.
        ENDIF.
        APPEND R_MRU.
      ENDLOOP.
    ENDIF. " end so_GSBER[]
  ENDIF. " end lv_lines > 0



  IF NOT R_MRU[] IS INITIAL.
* break csebdev1.
    REFRESH GT_TE422[].
    SELECT TERMSCHL TERMTEXT FROM TE422 INTO CORRESPONDING FIELDS OF
TABLE GT_TE422 WHERE TERMSCHL IN R_MRU .
  ENDIF.

**now call the search help
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
*   DDIC_STRUCTURE         = ' '
      RETFIELD               = 'TERMSCHL'
*   PVALKEY                = ' '
*   DYNPPROG               = ' '
*   DYNPNR                 = ' '
*   DYNPROFIELD            = ' '
*   STEPL                  = 0
     WINDOW_TITLE           = 'Group'
     VALUE                  = DYFIELDS-FIELDVALUE
     VALUE_ORG              = 'S'
*   MULTIPLE_CHOICE        = ' '
*   DISPLAY                = ' '
*   CALLBACK_PROGRAM       = ' '
*   CALLBACK_FORM          = ' '
*   MARK_TAB               =
* IMPORTING
*   USER_RESET             =
    TABLES
      VALUE_TAB              = GT_TE422
*   FIELD_TAB              =
     RETURN_TAB             = LT_RETURN_TAB
*   DYNPFLD_MAPPING        =
* EXCEPTIONS
*   PARAMETER_ERROR        = 1
*   NO_VALUES_FOUND        = 2
*   OTHERS                 = 3
            .
  IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
  READ TABLE LT_RETURN_TAB INTO LWA_RETURN_TAB INDEX 1.
  LV_MRU = LWA_RETURN_TAB-FIELDVAL.

ENDFORM.                    " MRU_DATA_GET

<b>b) ZISU_LTR15_TUNE - Actual R15 report</b>

*&---------------------------------------------------------------------*
*& Report  ZISU_LTR15_TUNE                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZISU_LTR15_TUNE NO STANDARD PAGE HEADING  MESSAGE-ID ZISU
LINE-SIZE 250 LINE-COUNT 65.


**Tables
TABLES :  EVER,
          FKKVKP,
          EANLH.

*&---------------------------------------------

SELECTION-SCREEN BEGIN OF BLOCK SELECTION
                          WITH FRAME TITLE TEXT-001 . "no intervals.
SELECT-OPTIONS: SO_GSBER FOR EVER-GSBER OBLIGATORY,
                SO_COKEY FOR EVER-COKEY,
                SO_MRU   FOR EANLH-ABLEINH.

PARAMETERS:     P_BMNTH(7) TYPE C OBLIGATORY.

PARAMETERS:     P_CONT TYPE EVER-VERTRAG.
SELECT-OPTIONS: SO_GPART FOR FKKVKP-GPART.
SELECTION-SCREEN END OF BLOCK SELECTION.

SELECTION-SCREEN BEGIN OF BLOCK PROCESS WITH FRAME.
PARAMETERS:     P_COMPUT RADIOBUTTON GROUP R15.
PARAMETERS:     P_INCLUD RADIOBUTTON GROUP R15,
                P_FILE(60) TYPE C.

SELECTION-SCREEN END OF BLOCK PROCESS.
*----------------------------------------------------------------------*
*              Start of Type declaration
*----------------------------------------------------------------------*
TYPES: BEGIN OF ST_TB024 ,
        IND_SECTOR TYPE TB024-IND_SECTOR,
        TEXTLONG TYPE TB024-TEXTLONG,
      END   OF ST_TB024,

      BEGIN OF ST_TECOKT,
        COKEY TYPE TECOKT-COKEY,
        LTEXT TYPE TECOKT-LTEXT,
      END   OF ST_TECOKT,

      BEGIN OF ST_TGSBT,
        GSBER   TYPE TGSBT-GSBER,
        GTEXT   TYPE TGSBT-GTEXT,
      END   OF ST_TGSBT,

      BEGIN OF ST_TE422,
        TERMSCHL  TYPE TE422-TERMSCHL,
        TERMTEXT  TYPE TE422-TERMTEXT,
      END   OF ST_TE422.
*----------------------------------------------------------------------*
*              Start of data declaration
*----------------------------------------------------------------------*

DATA: IT_TAB TYPE FILETABLE,
      GD_SUBRC TYPE I.

DATA : GT_TB024 TYPE STANDARD TABLE OF ST_TB024 WITH HEADER LINE
         INITIAL SIZE 0,

       GT_TECOKT TYPE STANDARD TABLE OF ST_TECOKT
         WITH HEADER LINE,

      GT_TGSBT TYPE SORTED TABLE OF ST_TGSBT
      WITH UNIQUE KEY GSBER WITH HEADER LINE,

      GT_TE422 TYPE TABLE OF ST_TE422 WITH HEADER LINE INITIAL SIZE 0,

      BEGIN OF GWA_EVER,
        VKONT    TYPE EVER-VKONTO,
        ANLAGE   TYPE EVER-ANLAGE,
        ABRSPERR TYPE EVER-ABRSPERR,
      END   OF GWA_EVER,

      GT_EVER LIKE TABLE OF GWA_EVER INITIAL SIZE 0,

      BEGIN OF GWA_EANLH,
      ANLAGE     TYPE EANLH-ANLAGE,
      BRANCHE    TYPE EANLH-BRANCHE,
      END   OF GWA_EANLH,

      GT_EANLH LIKE TABLE OF GWA_EANLH INITIAL SIZE 0,

      BEGIN OF GWA_FKKVKP,
        VKONT   TYPE FKKVKP-VKONT,
        KTOKL   TYPE FKKVKP-KTOKL,
      END   OF GWA_FKKVKP,

      GT_FKKVKP LIKE TABLE OF GWA_FKKVKP INITIAL SIZE 0,

      BEGIN OF GWA_EVER_EANLH_FKKVKP,
        VKONT   TYPE FKKVKP-VKONT,
        ANLAGE   TYPE EVER-ANLAGE,
        ABRSPERR TYPE EVER-ABRSPERR,
        BRANCHE  TYPE EANLH-BRANCHE,
        KTOKL    TYPE FKKVKP-KTOKL,
      END OF GWA_EVER_EANLH_FKKVKP,

      GT_EVER_EANLH_FKKVKP LIKE TABLE OF GWA_EVER_EANLH_FKKVKP
        INITIAL SIZE 0,

      GT_EVER_EANLH_FKKVKP_INACT LIKE STANDARD TABLE OF
        GWA_EVER_EANLH_FKKVKP INITIAL SIZE 0,

      BEGIN OF GWA_ERCH,
        BELNR      TYPE ERCH-BELNR,
        VKONT      TYPE ERCH-VKONT,
      END OF GWA_ERCH,

      GT_ERCH LIKE TABLE OF GWA_ERCH INITIAL SIZE 0,

      BEGIN OF GWA_PRINTDOC,
        OPBEL    TYPE ERDK-OPBEL,
        GPART    TYPE ERDK-PARTNER,
        VKONT    TYPE ERDK-VKONT,
        BUDAT    TYPE ERDK-BUDAT,
        FAEDN    TYPE ERDK-FAEDN,
      END   OF GWA_PRINTDOC,

      GT_PRINTDOC LIKE TABLE OF GWA_PRINTDOC INITIAL SIZE 0,

      BEGIN OF GWA_DBERCHZ1,
        BELNR      TYPE DBERCHZ1-BELNR,
        EIN01      TYPE DBERCHZ1-EIN01,
        V_ABRMENGE TYPE DBERCHZ1-V_ABRMENGE,
      END OF GWA_DBERCHZ1,

      GT_DBERCHZ1 LIKE TABLE OF GWA_DBERCHZ1 INITIAL SIZE 0,

      BEGIN OF GWA_ERCHC,
        BELNR     TYPE ERCHC-BELNR,
        OPBEL     TYPE ERCHC-OPBEL,
        BUDAT     TYPE ERCHC-BUDAT,
      END   OF GWA_ERCHC,

      GT_ERCHC LIKE TABLE OF GWA_ERCHC INITIAL SIZE 0,

* arrears for inactive consumers.
      BEGIN OF GWA_DFKKOP_INACTIVE_ARR,
          OPBEL       TYPE DFKKOP-OPBEL,
          VKONT       TYPE DFKKOP-VKONT,
          BETRH       TYPE DFKKOP-BETRH,
      END OF GWA_DFKKOP_INACTIVE_ARR,

      GT_DFKKOP_INACTIVE_ARR LIKE TABLE OF GWA_DFKKOP_INACTIVE_ARR
        INITIAL SIZE 0,

      BEGIN OF GWA_ETTIFN,
        ANLAGE    TYPE ETTIFN-ANLAGE,
        OPERAND   TYPE ETTIFN-OPERAND,
        WERT1     TYPE ETTIFN-WERT1,
      END OF GWA_ETTIFN,

      GT_ETTIFN LIKE SORTED TABLE OF GWA_ETTIFN
        WITH NON-UNIQUE KEY ANLAGE OPERAND INITIAL SIZE 0,

      BEGIN OF GWA_DBERDLB,
        PRINTDOC     TYPE DBERDLB-PRINTDOC,
        BILLDOC      TYPE DBERDLB-BILLDOC,
        BILLDOCLINE  TYPE DBERDLB-BILLDOCLINE,
        NETTOBTR     TYPE DBERDLB-NETTOBTR,
      END OF GWA_DBERDLB,

      GT_DBERDLB LIKE TABLE OF GWA_DBERDLB INITIAL SIZE 0,

      BEGIN OF GWA_DBERCHZ1_BILL,
        BELNR       TYPE DBERCHZ1-BELNR,
        BELZEILE    TYPE DBERCHZ1-BELZEILE,
        TVORG       TYPE DBERCHZ1-TVORG,
      END   OF GWA_DBERCHZ1_BILL,

      GT_DBERCHZ1_BILL LIKE TABLE OF GWA_DBERCHZ1_BILL INITIAL SIZE 0,

      BEGIN OF GWA_BILLDOC,
        PRINTDOC TYPE ERDK-OPBEL,
        BILLDOC TYPE DBERDLB-BILLDOC,
      END OF GWA_BILLDOC,

      GT_BILLDOC LIKE TABLE OF GWA_BILLDOC INITIAL SIZE 0,


      BEGIN OF GWA_DBERCHV,
        BELNR    TYPE DBERCHV-BELNR,
        OPERAND  TYPE DBERCHV-OPERAND,
        EZ_ABRMENGE TYPE DBERCHV-EZ_ABRMENGE,
        ABLESGR  TYPE DBERCHV-ABLESGR,
        ABLESGRV TYPE DBERCHV-ABLESGRV,
      END OF GWA_DBERCHV,

      GT_DBERCHV LIKE TABLE OF GWA_DBERCHV INITIAL SIZE 0,


      BEGIN OF GWA_DFKKOP_ARREAR,
        OPBEL     TYPE DFKKOP-OPBEL,
        VKONT     TYPE DFKKOP-VKONT,
        HVORG     TYPE DFKKOP-HVORG,
        TVORG     TYPE DFKKOP-TVORG,
        BUDAT     TYPE DFKKOP-BUDAT,
        BETRH     TYPE DFKKOP-BETRH,
        AUGDT     TYPE DFKKOP-AUGDT,
        XBLNR     TYPE DFKKOP-XBLNR,
      END OF GWA_DFKKOP_ARREAR,

      GT_DFKKOP_ARREAR LIKE TABLE OF GWA_DFKKOP_ARREAR INITIAL SIZE 0,

      BEGIN OF GWA_MASTER_DATA,
        SLNO(4)      TYPE C,
        IND_SECTOR   TYPE TB024-IND_SECTOR,
        TEXTLONG     TYPE TB024-TEXTLONG,
        AC_CODE      TYPE TFK033D-FUN01,
      END   OF GWA_MASTER_DATA,

      GT_MASTER_DATA LIKE TABLE OF GWA_MASTER_DATA INITIAL SIZE 0,

**      internal table for final prepared data
      BEGIN OF GWA_FINAL_DATA,
          SLNO(4) TYPE C,
          IND_SECTOR      TYPE TB024-IND_SECTOR, " for testing
          INDUSTRY        TYPE TB024-TEXTLONG,
          AC_CODE         TYPE TFK033D-FUN01,
*    *for urban partners
          UPARTNER        TYPE I , "(6) type n,
          UBAD_METER      TYPE I, "(4) type n,
          UINACTIVE       TYPE I,
          ULOAD(8)        TYPE P DECIMALS 2,
          UUNITS(8)       TYPE P DECIMALS 2,
          UDEMANDS(8)     TYPE P DECIMALS 2,
          UARREARS(8)     TYPE P DECIMALS 2, "FKKMAKO-Msalm,
          UINACT_ARR(8)   TYPE P DECIMALS 2,
          BLANK(10)       TYPE C,
*    *for rural partners
          RPARTNER        TYPE I , "(6) type n,
          RBAD_METER      TYPE I , "(4) type n,
          RINACTIVE       TYPE I,
          RLOAD(8)        TYPE P DECIMALS 2,
          RUNITS(8)       TYPE P DECIMALS 2,
          RDEMANDS(8)     TYPE P DECIMALS 2,
          RARREARS(8)     TYPE P DECIMALS 2,
          RINACT_ARR(8)   TYPE P DECIMALS 2,
      END OF GWA_FINAL_DATA,

      GT_FINAL_DATA LIKE TABLE OF GWA_FINAL_DATA INITIAL SIZE 0,

      BEGIN OF GWA_MANUAL_DATA,
        COL_1(5)        TYPE C,
        COL_2(10)       TYPE C,
        COL_3(40)       TYPE C,
        COL_4(40)       TYPE C,
        COL_5(10)       TYPE C,
        COL_6(19)       TYPE C,
        COL_7(19)       TYPE C,
        COL_8(19)       TYPE C,
        COL_9(19)       TYPE C,
        COL_10(19)      TYPE C,
        COL_11(19)      TYPE C,
        COL_12(10)      TYPE C,
        COL_13(19)      TYPE C,
        COL_14(19)      TYPE C,
        COL_15(19)      TYPE C,
        COL_16(19)      TYPE C,
        COL_17(19)      TYPE C,
        COL_18(19)      TYPE C,
      END   OF GWA_MANUAL_DATA,

      GT_MANUAL_DATA LIKE TABLE OF GWA_MANUAL_DATA INITIAL SIZE 0.

** Variables for grand total of all heads.
DATA : GV_T_UPART        TYPE I,
       GV_T_RPART        TYPE I,
       GV_T_RINACTIVE    TYPE I,
       GV_T_UINACTIVE    TYPE I,
       GV_UDEF_MTR       TYPE I,
       GV_RDEF_MTR       TYPE I,
       GV_UCON_LOAD(16)  TYPE P DECIMALS 2,
       GV_RCON_LOAD(16)  TYPE P DECIMALS 2,
       GV_UUNITS(16)     TYPE P DECIMALS 2,
       GV_RUNITS(16)     TYPE P DECIMALS 2,
       GV_UDEMAND(16)    TYPE P DECIMALS 2,
       GV_RDEMAND(16)    TYPE P DECIMALS 2,
       GV_UARREAR(16)    TYPE P DECIMALS 2,
       GV_RARREAR(16)    TYPE P DECIMALS 2,
       GV_UINACT_ARR(16) TYPE P DECIMALS 2,
       GV_RINACT_ARR(16) TYPE P DECIMALS 2,
       GV_U_SD_DMD(16)       TYPE P DECIMALS 2,
       GV_R_SD_DMD(16)       TYPE P DECIMALS 2,
       GV_U_SD_ARR(16)       TYPE P DECIMALS 2,
       GV_R_SD_ARR(16)       TYPE P DECIMALS 2,

       GV_UR_PART         TYPE I,
       GV_UR_DEF_MTR      TYPE I,
       GV_UR_CON_LOAD(16) TYPE P DECIMALS 2,
       GV_UR_UNITS(16)    TYPE P DECIMALS 2,
       GV_UR_DEMAND(16)   TYPE P DECIMALS 2,
       GV_UR_ARREARS(16)  TYPE P DECIMALS 2,
       GV_LOWDATE         TYPE SY-DATUM,
       GV_HIGHDATE        TYPE SY-DATUM,

       GV_YEAR   LIKE DBERCHV-EZ_ABRMENGE,
       GV_MONTH  LIKE DBERCHZ1-V_ABRMENGE,
       GV_MNTH_NAME TYPE T247-KTX.

DATA:
**total meter rent
      GV_UTOTMETERRENT TYPE P LENGTH 16 DECIMALS 2,
      GV_RTOTMETERRENT TYPE P LENGTH 16 DECIMALS 2,
**for total surcharge
      GV_UTOTSURCHRG TYPE P LENGTH 16 DECIMALS 2, "dberdlb-nettobtr,
      GV_RTOTSURCHRG TYPE P LENGTH 16 DECIMALS 2, "dberdlb-nettobtr,
**for total ED
      GV_UTOTED TYPE P LENGTH 16 DECIMALS 2, "dberdlb-nettobtr,
      GV_RTOTED TYPE P LENGTH 16 DECIMALS 2, "dberdlb-nettobtr,
**for total ED Cess
      GV_UTOTEDCESS TYPE P LENGTH 16 DECIMALS 2, "dberdlb-nettobtr,
      GV_RTOTEDCESS TYPE P LENGTH 16 DECIMALS 2, "dberdlb-nettobtr,
**for total other misc rev
      GV_UTOTMISCREV  TYPE P LENGTH 16 DECIMALS 2,
      GV_RTOTMISCREV TYPE P LENGTH 16 DECIMALS 2,
**for total ED of Free agricultural pump
      GV_RTOTEDAGRI TYPE P LENGTH 16 DECIMALS 2,
      GV_UTOTEDAGRI TYPE P LENGTH 16 DECIMALS 2,
**for ED cess of Free agriculture pump
      GV_RTOTCESSAGRI TYPE P LENGTH 16 DECIMALS 2,
      GV_UTOTCESSAGRI TYPE P LENGTH 16 DECIMALS 2,

***data for selection screen data validation
      GV_GSBER TYPE EVER-GSBER,
      GV_COKEY TYPE EVER-COKEY,

      DYFIELDS LIKE DYNPREAD OCCURS 0 WITH HEADER LINE,

      GT_INTERN_DATA TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

RANGES: R_COKEY FOR TECOKT-COKEY.

AT SELECTION-SCREEN.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON SO_GSBER.
  IF NOT SO_GSBER-LOW IS INITIAL.

    SELECT SINGLE GSBER FROM TGSBT INTO GT_TGSBT-GSBER WHERE
    GSBER = SO_GSBER-LOW.
    IF SY-SUBRC <> 0.
      MESSAGE E007.
    ENDIF.
  ENDIF.

  IF NOT SO_GSBER-HIGH IS INITIAL .
    SELECT SINGLE GSBER FROM TGSBT INTO GT_TGSBT-GSBER WHERE
    GSBER = SO_GSBER-HIGH.
    IF SY-SUBRC <> 0.
      MESSAGE E007.
    ENDIF.
  ENDIF.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON P_BMNTH.
**check the validity of Billing month format and for valid billing month
  IF P_BMNTH CO '0123456789/'.
    IF P_BMNTH CP '++++/++'.
      IF P_BMNTH+5(2) > 12.
        MESSAGE E002 WITH 'from'.
      ENDIF.

** current year is less than year entered
      IF SY-DATUM(4) < P_BMNTH(4).
        MESSAGE E003 WITH 'from'.
      ELSEIF SY-DATUM(4) = P_BMNTH(4).
** month in future
        IF SY-DATUM+4(2) < P_BMNTH+5(2).
          MESSAGE E004 WITH 'from'.
        ENDIF.
      ENDIF.
    ELSE.
      MESSAGE E001 WITH 'from'.
    ENDIF.
  ELSE.
**  entry have some invalid char
    MESSAGE E010.
  ENDIF.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR SO_COKEY-LOW.

**for geting the CO account assigment key of the entered Business area
  CLEAR: DYFIELDS[], DYFIELDS.

  PERFORM DIVISON_DATA_GET CHANGING SO_COKEY-LOW.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR SO_COKEY-HIGH.
  CLEAR: DYFIELDS[], DYFIELDS.

  PERFORM DIVISON_DATA_GET CHANGING SO_COKEY-HIGH.

*&---------------------------------------------------------------------*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*  REFRESH: IT_TAB.
***Opens File Open Dialog Box for selecting input file.
*  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
*    EXPORTING
*      WINDOW_TITLE     = 'Select File'
*      DEFAULT_FILENAME = '*.xls'
*      MULTISELECTION   = ' '
*    CHANGING
*      FILE_TABLE       = IT_TAB
*      RC               = GD_SUBRC.
*
*  LOOP AT IT_TAB INTO P_FILE.
**    so_fpath-sign = 'I'.
**    so_fpath-option = 'EQ'.
**    append so_fpath.
*  ENDLOOP.
*
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.

    IF SCREEN-GROUP1 = 'ACT'.
      IF P_INCLUD = ' '.
        SCREEN-INPUT = '0'.
      ELSE.
        SCREEN-INPUT = '1'.
      ENDIF.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.
*----------------------------------------------------------------------*
*              End of Selection screen processing
*----------------------------------------------------------------------*
TOP-OF-PAGE.

  DATA : LV_TEXT(70) TYPE C.
  .

  FORMAT INTENSIFIED OFF.

  WRITE:/50 'CHHATTISGARH STATE ELECTRICITY BOARD',
        /42 'R-15 REPORT FOR LT CONSUMERS FOR THE MONTH OF',
             GV_MNTH_NAME NO-GAP,'-' NO-GAP, P_BMNTH(4) NO-GAP,
        180 'PAGE-NO. -', SY-PAGNO LEFT-JUSTIFIED.

  SKIP.
  WRITE:/2 'RAO', 15 ':'.
  LOOP AT GT_TGSBT WHERE GSBER IN SO_GSBER.
    IF STRLEN( LV_TEXT ) > 60.
      WRITE: 16 LV_TEXT, / ''.
      CLEAR LV_TEXT.
    ENDIF.
    CONCATENATE LV_TEXT GT_TGSBT-GSBER '-' GT_TGSBT-GTEXT
      INTO LV_TEXT SEPARATED BY SPACE.
  ENDLOOP.
  WRITE: 16 LV_TEXT.
  CLEAR LV_TEXT.

  WRITE: /2 'Division',15 ':'.
  LOOP AT GT_TECOKT.
    IF STRLEN( LV_TEXT ) > 60.
      WRITE: 16 LV_TEXT, /.
      CLEAR LV_TEXT.
    ENDIF.
    CONCATENATE LV_TEXT GT_TECOKT-COKEY '-' GT_TECOKT-LTEXT
      INTO LV_TEXT SEPARATED BY SPACE.
  ENDLOOP.
  WRITE: 16 LV_TEXT.
  CLEAR LV_TEXT.

  WRITE: /2 'Group',15 ':'.
  LOOP AT GT_TE422 .
    IF STRLEN( LV_TEXT ) > 60.
      WRITE: 16 LV_TEXT, /.
      CLEAR LV_TEXT.
    ENDIF.
    CONCATENATE LV_TEXT GT_TE422-TERMSCHL '-' GT_TE422-TERMTEXT
      INTO LV_TEXT SEPARATED BY SPACE.
  ENDLOOP.
  WRITE: 16 LV_TEXT.
  CLEAR LV_TEXT.

**legends
*  write :/ text-028, 15 ':',16 text-029.
**now write the headings on every page
  SET LEFT SCROLL-BOUNDARY COLUMN 50.

  FORMAT COLOR 1 ON.
  WRITE :/1(244) SY-ULINE.
  WRITE :/1 SY-VLINE,6 SY-VLINE,50 SY-VLINE, 58 SY-VLINE, 59
            '<----------------------------------',
            'U R B A N   A R E A',
            '----------------------------------->'.

  WRITE : 151 SY-VLINE, 152
              '<----------------------------------',
              'R U R A L   A R E A',
              '----------------------------------->',
          244 SY-VLINE.

  WRITE :/1 SY-VLINE,
          2 'Slno',
          6 SY-VLINE,
          7 'Revenue Category',
          50 SY-VLINE,
          51 'A/C',
          58 SY-VLINE,
          59 'Cons-',
          67 SY-VLINE,
          68 'Deff',
          75 SY-VLINE,
          76 'Conn',
          89 SY-VLINE,
          90 'Sold',
          108 SY-VLINE,
          109 'Demand',
          129 SY-VLINE,
          130 'Previous',
          151 SY-VLINE,
          152 'Cons-',
          160 SY-VLINE,
          161 'Deff',
          168 SY-VLINE,
          169 'Conn',
          182 SY-VLINE,
          183 'Sold',
          201 SY-VLINE,
          202 'Demand',
          222 SY-VLINE,
          223 'Previous',
          244 SY-VLINE.

  WRITE :/1 SY-VLINE,
          6 SY-VLINE,
          50 SY-VLINE,
          51 'Code',
          58 SY-VLINE,
          59 'umers',
          67 SY-VLINE,
          68 'mtrs',
          75 SY-VLINE,
          76 'Load-KW',
          89 SY-VLINE,
          90 'Units',
          108 SY-VLINE,
          129 SY-VLINE,
          130 'Arrear',
          151 SY-VLINE,
          152 'umers',
          160 SY-VLINE,
          161 'mtrs',
          168 SY-VLINE,
          169 'Load-KW',
          182 SY-VLINE,
          183 'Units',
          201 SY-VLINE,
          222 SY-VLINE,
          223 'Arrear',
          244 SY-VLINE.

  WRITE :/1(244) SY-ULINE.

  SET LEFT SCROLL-BOUNDARY COLUMN 59.

*----------------------------------------------------------------------*
*              Start of Data Selection
*----------------------------------------------------------------------*
START-OF-SELECTION.

  SELECT GSBER GTEXT INTO TABLE GT_TGSBT FROM TGSBT
    WHERE SPRAS = SY-LANGU.

  SELECT IND_SECTOR TEXTLONG INTO TABLE GT_TB024
    FROM TB024 WHERE SPRAS = SY-LANGU
                 AND ( ( IND_SECTOR >= '01' AND IND_SECTOR <= '55' )
                  OR ( IND_SECTOR = '57' OR IND_SECTOR = '58'
                  OR IND_SECTOR = '94' ) ).

  SELECT SINGLE KTX INTO GV_MNTH_NAME FROM T247
   WHERE MNR = P_BMNTH+5(2) AND SPRAS = SY-LANGU.

***master data selection
  PERFORM CONSUMER_DATA_SELECTION.

*&--Get Meter Status Connected Load & MF for each installation
  PERFORM OPERAND_DATA_SELECTION.

*&--Get the Demand corresponding to each Print documents selected
  PERFORM BILLING_DATA_SELECTION.

*&--Get the arrears corresponding to each Print document selected
  PERFORM ARREAR_DATA_SELECTION.

*&--To include Mannual R-15 into computerized R-15.
  IF P_INCLUD = 'X'.

    OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.

    DO.
      READ DATASET P_FILE INTO GWA_MANUAL_DATA.
      IF SY-SUBRC <> 0.
        EXIT.
      ELSE.
        APPEND GWA_MANUAL_DATA TO GT_MANUAL_DATA.
      ENDIF.
    ENDDO.
    CLOSE DATASET P_FILE.
    DELETE DATASET P_FILE.
  ENDIF.

END-OF-SELECTION.

*---------------------------------------------------------------------*
*              End of data Selection
*            Start of Data Processing
*---------------------------------------------------------------------*

  PERFORM FINAL_TABLE_PREPARE.

  PERFORM FINAL_OUTPUT_PREPARE.

  PERFORM FINAL_OUTPUT_DISPLAY.
*---------------------------------------------------------------------*
*              End of Data Processing
*---------------------------------------------------------------------*

  PERFORM FREE_MEMORY.


*&---------------------------------------------------------------------*
*&      Form  master_data_selection
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CONSUMER_DATA_SELECTION .
  DATA: LV_MAX_DAYS TYPE I,
        LV_LAST_DAY(2) TYPE C,
        LV_IMONTH TYPE I,
        LV_IYEAR TYPE I,
        LV_BMNTH TYPE ZERDK_ERCHC-V_ABRMENGE,
        LV_BYEAR TYPE ZERDK_ERCHC-EZ_ABRMENGE,

*&----Temporary tables for global internal tables.
        LT_DBERCHZ1_TEMP LIKE TABLE OF GWA_DBERCHZ1,
        LWA_DBERCHZ1_TEMP LIKE GWA_DBERCHZ1,
        LT_ERCH LIKE TABLE OF GWA_ERCH,
        LWA_ERCH LIKE GWA_ERCH,
        LT_EVER LIKE TABLE OF GWA_EVER.

*DATA: lt_ever_fkkvkp like gwa_ever_eanlh_fkkvkp occurs 0.

  LV_IMONTH = P_BMNTH+5(2).
  LV_IYEAR  = P_BMNTH(4).
  LV_BMNTH  = P_BMNTH+5(2).
  LV_BYEAR  = P_BMNTH(4).


  CALL FUNCTION 'RTP_US_API_MAX_DAYS_IN_MONTH'
    EXPORTING
      I_DATE_MONTH = LV_IMONTH
      I_DATE_YEAR  = LV_IYEAR
    IMPORTING
      E_MAX_DAYS   = LV_MAX_DAYS.

  LV_LAST_DAY = LV_MAX_DAYS.

  CONCATENATE P_BMNTH(4) P_BMNTH+5(2)  LV_LAST_DAY INTO GV_HIGHDATE.


  IF SO_GPART[] IS INITIAL.
    SELECT VKONTO ANLAGE ABRSPERR
    INTO TABLE GT_EVER
    FROM EVER
    WHERE GSBER IN SO_GSBER
      AND COKEY IN SO_COKEY
      AND   BUKRS = 'CSEB'
      AND   SPARTE = '01'
      AND   KOFIZ  = '02'.

**get installation no from contract data and check biling class in eanlh
    IF GT_EVER[] IS INITIAL.
MESSAGE 'No Business Partner exist for entered selection data' TYPE 'A'.
    ENDIF.

    IF NOT GT_EVER[] IS INITIAL.
      SELECT VKONT KTOKL INTO TABLE GT_FKKVKP
        FROM FKKVKP
        FOR ALL ENTRIES IN GT_EVER
        WHERE VKONT = GT_EVER-VKONT
        AND   GPART IN SO_GPART.

      IF GT_FKKVKP[] IS INITIAL.
     MESSAGE 'No Business Partner exist for entered Selection' TYPE 'A'.
      ENDIF.
    ENDIF.
  ELSE.
    SELECT VKONT KTOKL INTO TABLE GT_FKKVKP
      FROM FKKVKP
      WHERE GPART IN SO_GPART.

    IF NOT GT_FKKVKP[] IS INITIAL.
      SELECT VKONTO ANLAGE INTO TABLE GT_EVER
        FROM EVER
        FOR ALL ENTRIES IN GT_FKKVKP
        WHERE VKONTO  EQ GT_FKKVKP-VKONT
          AND GSBER   IN SO_GSBER
          AND COKEY   IN SO_COKEY
          AND BUKRS   EQ 'CSEB'
          AND SPARTE  EQ '01'
          AND KOFIZ   EQ '02'.

    ENDIF.
  ENDIF.

  IF GT_EVER[] IS INITIAL AND GT_FKKVKP[] IS INITIAL.
    MESSAGE 'No Business Partner exist for entered Selection' TYPE 'A'.
  ENDIF.

  SELECT ANLAGE BRANCHE INTO TABLE GT_EANLH
    FROM EANLH
    FOR ALL ENTRIES IN GT_EVER
    WHERE ANLAGE EQ GT_EVER-ANLAGE
    AND   ABLEINH IN SO_MRU
    AND   AKLASSE EQ'0002'
    AND   AB <= GV_HIGHDATE
    AND   BIS => GV_HIGHDATE.

  IF GT_EANLH[] IS INITIAL.
    MESSAGE 'No Business Partner exist for entered Selection' TYPE 'A'.
  ENDIF.



  SORT : GT_EVER BY VKONT ANLAGE,
         GT_FKKVKP BY VKONT,
         GT_EANLH BY ANLAGE.

  LOOP AT GT_EANLH INTO GWA_EANLH.
    READ TABLE GT_EVER INTO GWA_EVER WITH KEY ANLAGE = GWA_EANLH-ANLAGE.
    IF SY-SUBRC = 0.
      READ TABLE GT_FKKVKP INTO GWA_FKKVKP
        WITH KEY VKONT = GWA_EVER-VKONT.
      IF SY-SUBRC = 0.
        GWA_EVER_EANLH_FKKVKP-VKONT = GWA_EVER-VKONT.
        GWA_EVER_EANLH_FKKVKP-ANLAGE = GWA_EVER-ANLAGE.
        GWA_EVER_EANLH_FKKVKP-ABRSPERR = GWA_EVER-ABRSPERR.
        GWA_EVER_EANLH_FKKVKP-BRANCHE  = GWA_EANLH-BRANCHE.
        GWA_EVER_EANLH_FKKVKP-KTOKL    = GWA_FKKVKP-KTOKL.

        APPEND GWA_EVER_EANLH_FKKVKP TO GT_EVER_EANLH_FKKVKP.
        CLEAR GWA_EVER_EANLH_FKKVKP.
      ENDIF.
    ENDIF.
  ENDLOOP.

  IF GT_EVER_EANLH_FKKVKP[] IS INITIAL.
    MESSAGE 'No Business Partner exist for Selection Data' TYPE 'A'.
  ENDIF.

*&----get all the billdocuments for the busines partner's contract
*&----account
  SELECT BELNR VKONT INTO TABLE GT_ERCH
    FROM ERCH
    FOR ALL ENTRIES IN GT_EVER_EANLH_FKKVKP
    WHERE VKONT EQ GT_EVER_EANLH_FKKVKP-VKONT.

*&----get the BILL MONTH & BILL YEAR FOR THE BILLDOCUMENTS.
  IF NOT GT_ERCH[] IS INITIAL.

    SELECT BELNR EIN01 V_ABRMENGE INTO TABLE GT_DBERCHZ1
      FROM DBERCHZ1
      FOR ALL ENTRIES IN GT_ERCH
      WHERE BELNR EQ GT_ERCH-BELNR
        AND AKLASSE = '0002'
        AND ( ( EIN01 = 'BILL_MNTH1'
        AND V_ABRMENGE = P_BMNTH+5(2) )
        OR ( EIN01 = 'BILL_YEAR1'
        AND V_ABRMENGE = P_BMNTH(4) ) ).

*&---- GET THOSE BILL DOCUMENTS WHICH ARE FOR THE ENTERED BILL MONTH
    IF NOT GT_DBERCHZ1[] IS INITIAL.
      LOOP AT GT_DBERCHZ1 INTO GWA_DBERCHZ1 WHERE EIN01 = 'BILL_MNTH1'
                                          AND V_ABRMENGE = P_BMNTH+5(2).

        READ TABLE GT_DBERCHZ1 INTO LWA_DBERCHZ1_TEMP
        WITH KEY BELNR = GWA_DBERCHZ1-BELNR EIN01 = 'BILL_YEAR1'
        V_ABRMENGE = P_BMNTH(4).

        IF SY-SUBRC = 0.
          APPEND LWA_DBERCHZ1_TEMP TO LT_DBERCHZ1_TEMP.
          CLEAR: LWA_DBERCHZ1_TEMP.
        ENDIF.

      ENDLOOP.

      GT_DBERCHZ1[] = LT_DBERCHZ1_TEMP[].
    ENDIF.

  ENDIF.

*&---NOW FIND THE PRINT DOCUMENTS FOR THE SELECTED BILLDOCUMENTS.
  IF NOT GT_DBERCHZ1[] IS INITIAL.
    SELECT BELNR OPBEL BUDAT FROM ERCHC
      INTO TABLE GT_ERCHC
      FOR ALL ENTRIES IN GT_DBERCHZ1
      WHERE BELNR = GT_DBERCHZ1-BELNR
        AND INTOPBEL EQ SPACE
        AND SIMULATED EQ SPACE
        AND INVOICED EQ 'X'.

  ENDIF.

  IF NOT GT_ERCHC[] IS INITIAL.
    LOOP AT GT_ERCHC INTO GWA_ERCHC.
      READ TABLE GT_ERCH INTO GWA_ERCH WITH KEY BELNR = GWA_ERCHC-BELNR.
      IF SY-SUBRC = 0.
        APPEND GWA_ERCH TO LT_ERCH.
        CLEAR GWA_ERCH.
      ENDIF.
      CLEAR GWA_ERCHC.
    ENDLOOP.
  ENDIF.
  GT_ERCH[] = LT_ERCH[].

  FREE: LT_ERCH, LT_DBERCHZ1_TEMP,LWA_DBERCHZ1_TEMP.

  LOOP AT GT_EVER_EANLH_FKKVKP INTO GWA_EVER_EANLH_FKKVKP.

*  READ TABLE gt_erdk_erchc INTO gwa_erdk_erchc
*  WITH KEY vkont = gwa_ever_eanlh_fkkvkp-vkont.
    READ TABLE GT_ERCH INTO GWA_ERCH
      WITH KEY VKONT = GWA_EVER_EANLH_FKKVKP-VKONT.

    IF SY-SUBRC <> 0.
      IF GWA_EVER_EANLH_FKKVKP-ABRSPERR = SPACE.
        GWA_EVER_EANLH_FKKVKP-ABRSPERR = '01'.
        MODIFY GT_EVER_EANLH_FKKVKP FROM GWA_EVER_EANLH_FKKVKP
          TRANSPORTING ABRSPERR.
        APPEND GWA_EVER_EANLH_FKKVKP TO GT_EVER_EANLH_FKKVKP_INACT.
      ENDIF.
    ELSE.
      IF GWA_EVER_EANLH_FKKVKP-ABRSPERR <> SPACE.
        GWA_EVER_EANLH_FKKVKP-ABRSPERR = SPACE.
        MODIFY GT_EVER_EANLH_FKKVKP FROM GWA_EVER_EANLH_FKKVKP
          TRANSPORTING ABRSPERR.
      ENDIF.
    ENDIF.

    CLEAR GWA_ERCH.

  ENDLOOP.

  REFRESH GT_TECOKT[].

  IF NOT SO_COKEY[] IS INITIAL.
    SELECT COKEY LTEXT INTO TABLE GT_TECOKT
    FROM TECOKT
    WHERE COKEY IN SO_COKEY
      AND SPRAS = SY-LANGU .
*    %_HINTS ORACLE '("TECOKT","TECOKT~1")'.
  ENDIF.

  IF NOT SO_MRU[] IS INITIAL.
    SELECT TERMSCHL TERMTEXT INTO TABLE GT_TE422
      FROM TE422
      WHERE TERMSCHL IN SO_MRU.
  ENDIF.

*FREE lt_ever_fkkvkp[].

ENDFORM.                    " consumer_data_selection

*&---------------------------------------------------------------------*
*&      Form  Operand_data_selection
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM OPERAND_DATA_SELECTION .

  DATA: LT_EVER_EANLH_FKKVKP_ACT LIKE TABLE OF GWA_EVER_EANLH_FKKVKP.
*  SORT gt_ever_eanlh_fkkvkp BY vkont.

  CHECK NOT GT_EVER_EANLH_FKKVKP[] IS INITIAL.

**now depending upon the billing month check the time slice date
**the last date of the billing month or the range of billing month
*should fall in between the time slice of the installation

** billing month to is space
**now select operands for processed installation
  LT_EVER_EANLH_FKKVKP_ACT[] = GT_EVER_EANLH_FKKVKP[].

  DELETE LT_EVER_EANLH_FKKVKP_ACT WHERE ABRSPERR NE SPACE.

  IF NOT LT_EVER_EANLH_FKKVKP_ACT[] IS INITIAL.
    SELECT ANLAGE OPERAND WERT1
    FROM ETTIFN
    INTO TABLE GT_ETTIFN
    FOR ALL ENTRIES IN LT_EVER_EANLH_FKKVKP_ACT
    WHERE ANLAGE = LT_EVER_EANLH_FKKVKP_ACT-ANLAGE
      AND OPERAND IN ('MTR_STS','CONN_LOAD','LOAD_CODE','KWH_MF')
      AND ( AB <= GV_HIGHDATE AND BIS >= GV_HIGHDATE ).
*    %_HINTS ORACLE '("ETTIFN","ETTIFN~003")'.
  ENDIF.

ENDFORM.                    " Operand_data_selection

*&---------------------------------------------------------------------*
*&      Form  billing_data_selection
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BILLING_DATA_SELECTION .

  DATA:  LT_BILLDOC LIKE TABLE OF GWA_DBERCHZ1_BILL.
**new insertion on 19/04/2005 for performance testing
  CHECK NOT GT_ERCHC[] IS INITIAL.

  SELECT PRINTDOC BILLDOC BILLDOCLINE NETTOBTR
    INTO TABLE GT_DBERDLB
    FROM DBERDLB
    FOR ALL ENTRIES IN GT_ERCHC
      WHERE PRINTDOC = GT_ERCHC-OPBEL
        AND HVORG = '0100'
        AND BUKRS = 'CSEB'
        AND SPARTE = '01'
        AND TXJCD = 'IN00'
        AND XTOTAL_AMNT = 'X'
        AND NETTOBTR <> 0.

*  LOOP AT GT_DBERDLB INTO GWA_DBERDLB.
*    ON CHANGE OF GWA_DBERDLB-BILLDOC
*              OR GWA_DBERDLB-PRINTDOC.
*      GWA_BILLDOC-PRINTDOC = GWA_DBERDLB-PRINTDOC.
*      GWA_BILLDOC-BILLDOC  = GWA_DBERDLB-BILLDOC.
*      APPEND GWA_BILLDOC TO GT_BILLDOC.
*    ENDON.
*  ENDLOOP.
*
*  SORT GT_BILLDOC BY PRINTDOC BILLDOC.
*  DELETE ADJACENT DUPLICATES FROM GT_BILLDOC.
*
*  IF NOT GT_BILLDOC[] IS INITIAL.

  IF NOT GT_DBERDLB[] IS INITIAL.
    SELECT BELNR BELZEILE TVORG INTO TABLE GT_DBERCHZ1_BILL
      FROM DBERCHZ1
      FOR ALL ENTRIES IN GT_DBERDLB
      WHERE BELNR = GT_DBERDLB-BILLDOC
      AND   BELZEILE = GT_DBERDLB-BILLDOCLINE
      AND AKLASSE = '0002'
      AND BUCHREL = 'X'
      AND TVORG NE SPACE.

    LT_BILLDOC[] = GT_DBERCHZ1_BILL[].
    SORT LT_BILLDOC BY BELNR.
    DELETE ADJACENT DUPLICATES FROM LT_BILLDOC COMPARING BELNR.

    SELECT BELNR OPERAND EZ_ABRMENGE ABLESGR ABLESGRV
      FROM DBERCHV
      INTO TABLE GT_DBERCHV
      FOR ALL ENTRIES IN LT_BILLDOC
      WHERE BELNR = LT_BILLDOC-BELNR
        AND OPERAND = 'CON_KWH'.
  ENDIF.


ENDFORM.                    " billing_data_selection
*&---------------------------------------------------------------------*
*&      Form  arrear_data_selection
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM ARREAR_DATA_SELECTION .

  DATA: S_CNTRDATA TYPE BAPIFKKEPOSC,
        LT_MAINSELSTRUC TYPE BAPIFKKOPSELHEAD OCCURS 0,
        WA_MAINSELSTRUC TYPE BAPIFKKOPSELHEAD,
        LT_DFKKOP_ITEM  TYPE BAPIFKKEPOS OCCURS 0,
        WA_DFKKOP_ITEM  TYPE BAPIFKKEPOS,
*        LT_VALUESELECT  TYPE BAPIFKKOPSELTXT OCCURS 0,
*        WA_VALUESELECT  TYPE BAPIFKKOPSELTXT.

        BEGIN OF LT_DFKKKO OCCURS 0,
          OPBEL    TYPE DFKKKO-OPBEL,
        END   OF LT_DFKKKO.

  SORT GT_ERCHC BY BUDAT DESCENDING.
  READ TABLE GT_ERCHC INTO GWA_ERCHC INDEX 1.
  IF SY-SUBRC <> 0.
    GWA_ERCHC-BUDAT = SY-DATUM.
  ENDIF.

  S_CNTRDATA-CLEARED_ITEMS     = 'X'.
  S_CNTRDATA-OPEN_ITEMS        = 'X'.
  S_CNTRDATA-STATISTICAL_ITEMS = 'X'.
  S_CNTRDATA-KEY_DATE          = GWA_ERCHC-BUDAT.

*  WA_VALUESELECT-SELECTION_NUMBER = '0001'.
*  WA_VALUESELECT-FIELD_NAME       = 'MAIN_TRANS'.
*  WA_VALUESELECT-FROM_VALUE       = '0010'.
*  WA_VALUESELECT-TO_VALUE         = '0300'.
*
*  APPEND WA_VALUESELECT TO LT_VALUESELECT.

  LOOP AT GT_EVER_EANLH_FKKVKP INTO GWA_EVER_EANLH_FKKVKP.
    WA_MAINSELSTRUC-SELECTION_NUMBER = SY-TABIX.
    WA_MAINSELSTRUC-CONT_ACCT        = GWA_EVER_EANLH_FKKVKP-VKONT.

    APPEND WA_MAINSELSTRUC TO LT_MAINSELSTRUC.
    CLEAR WA_MAINSELSTRUC.
  ENDLOOP.

  CALL FUNCTION 'BAPI_CTRACCONTRACTACCOUNT_GBAL'
    EXPORTING
      SELECTIONCONTROL = S_CNTRDATA
    TABLES
      MAINSELECTIONS   = LT_MAINSELSTRUC
      BALANCEITEMS     = LT_DFKKOP_ITEM.
*      VALUESELECTIONS  = LT_VALUESELECT.

  IF NOT LT_DFKKOP_ITEM[] IS INITIAL.
    DELETE LT_DFKKOP_ITEM WHERE ( MAIN_TRANS = '0600' OR
                                  MAIN_TRANS = '0625' OR
                                  CLEAR_REAS = '05' ).

    DELETE LT_DFKKOP_ITEM
          WHERE ( MAIN_TRANS = '0020' AND SUB_TRANS = '0010' ) OR
                ( MAIN_TRANS = '0010' AND SUB_TRANS = '0001' ) .

    LOOP AT LT_DFKKOP_ITEM INTO WA_DFKKOP_ITEM.

      GWA_DFKKOP_ARREAR-OPBEL = WA_DFKKOP_ITEM-DOC_NO.
      GWA_DFKKOP_ARREAR-VKONT = WA_DFKKOP_ITEM-CONT_ACCT.
      GWA_DFKKOP_ARREAR-HVORG = WA_DFKKOP_ITEM-MAIN_TRANS.
      GWA_DFKKOP_ARREAR-TVORG = WA_DFKKOP_ITEM-SUB_TRANS.
      GWA_DFKKOP_ARREAR-BUDAT = WA_DFKKOP_ITEM-POST_DATE.
      GWA_DFKKOP_ARREAR-BETRH = WA_DFKKOP_ITEM-AMOUNT.
      GWA_DFKKOP_ARREAR-AUGDT = WA_DFKKOP_ITEM-CLEARDATE.
      GWA_DFKKOP_ARREAR-XBLNR = WA_DFKKOP_ITEM-REF_DOC_NO.

      APPEND GWA_DFKKOP_ARREAR TO GT_DFKKOP_ARREAR.
      CLEAR: WA_DFKKOP_ITEM.
    ENDLOOP.
  ENDIF.

  IF NOT GT_DFKKOP_ARREAR[] IS INITIAL.
    SELECT OPBEL INTO TABLE LT_DFKKKO
      FROM DFKKKO
      FOR ALL ENTRIES IN GT_DFKKOP_ARREAR
      WHERE OPBEL EQ GT_DFKKOP_ARREAR-OPBEL
      AND   STBEL = SPACE
      AND   STORB = SPACE
      AND   APPLK = 'R'
      AND   WAERS = 'INR'.

  ENDIF.

  IF NOT LT_DFKKKO[] IS INITIAL.
    LOOP AT GT_DFKKOP_ARREAR INTO GWA_DFKKOP_ARREAR.
      READ TABLE LT_DFKKKO WITH KEY OPBEL = GWA_DFKKOP_ARREAR-OPBEL.
      IF SY-SUBRC <> 0.
        DELETE GT_DFKKOP_ARREAR WHERE OPBEL = GWA_DFKKOP_ARREAR-OPBEL.
      ENDIF.

      CLEAR GWA_DFKKOP_ARREAR.
      CLEAR LT_DFKKKO.

    ENDLOOP.
  ENDIF.

  SORT GT_DFKKOP_ARREAR BY VKONT OPBEL.
ENDFORM.                    " arrear_data_selection

*&---------------------------------------------------------------------*
*&      Form  divison_data_get
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DIVISON_DATA_GET CHANGING LV_COKEY.
**to get the search help for division
  DATA : LV_LINES TYPE SY-TFILL,
        LT_RETURN_TAB LIKE DDSHRETVAL OCCURS 0, " with header line.
        LWA_RETURN_TAB LIKE DDSHRETVAL.
**according to the Business area entered]
*  break csebdev1.
  REFRESH R_COKEY[].
  DESCRIBE TABLE SO_GSBER LINES LV_LINES.
**  when user has neither pressed the enter key nor selected the values
**using search help
  IF LV_LINES = 0.
*  loop at so_gsber.
    CLEAR R_COKEY.
*    if so_gsber-high is initial.
*    break csebdev1.
    DYFIELDS-FIELDNAME = 'SO_GSBER-LOW'.
    APPEND DYFIELDS.
    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        DYNAME     = SY-CPROG
        DYNUMB     = SY-DYNNR
      TABLES
        DYNPFIELDS = DYFIELDS.
*      r_cokey-sign = 'I'.
*      r_cokey-option = 'CP'.
*r_cokey-low = so_gsber-low+0(2).

    CONCATENATE DYFIELDS-FIELDVALUE+0(2) '*' INTO R_COKEY-LOW.

*    elseif not so_gsber-high is initial.



    DYFIELDS-FIELDNAME = 'SO_GSBER-HIGH'.
    APPEND DYFIELDS.
    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        DYNAME     = SY-CPROG
        DYNUMB     = SY-DYNNR
      TABLES
        DYNPFIELDS = DYFIELDS.

    IF NOT DYFIELDS-FIELDVALUE IS INITIAL.
      CONCATENATE DYFIELDS-FIELDVALUE+0(2) '99999999' INTO R_COKEY-HIGH.
    ENDIF.
*    endif.
    IF NOT R_COKEY-HIGH IS INITIAL.
      R_COKEY-SIGN = 'I'.
      R_COKEY-OPTION = 'BT'.
    ELSEIF R_COKEY-HIGH IS INITIAL.
      R_COKEY-SIGN = 'I'.
      R_COKEY-OPTION = 'CP'.
    ENDIF.
    APPEND R_COKEY.
  ENDIF.
*  endloop.
*  APPEND DYFIELDS.
  IF LV_LINES > 0.
*    break-point.
    IF NOT SO_GSBER[] IS INITIAL.
      LOOP AT SO_GSBER.
*        r_cokey-sign = 'I'.
*        r_cokey-option = 'CP'.
*r_cokey-low = so_gsber-low+0(2).
        CONCATENATE SO_GSBER-LOW+0(2) '*' INTO R_COKEY-LOW.
        IF NOT SO_GSBER-HIGH IS INITIAL.
          CONCATENATE SO_GSBER-HIGH+0(2) '99999999' INTO R_COKEY-HIGH.
        ENDIF.
        IF NOT R_COKEY-HIGH IS INITIAL.
          R_COKEY-SIGN = 'I'.
          R_COKEY-OPTION = 'BT'.
        ELSEIF R_COKEY-HIGH IS INITIAL.
          R_COKEY-SIGN = 'I'.
          R_COKEY-OPTION = 'CP'.
        ENDIF.

        APPEND R_COKEY TO R_COKEY.

      ENDLOOP.


    ENDIF.
  ENDIF.

  IF NOT R_COKEY[] IS INITIAL.
* break csebdev1.
    REFRESH GT_TECOKT[].
    SELECT COKEY LTEXT
    FROM TECOKT
    INTO TABLE GT_TECOKT
    WHERE COKEY IN R_COKEY AND SPRAS = 'EN'.
*    %_HINTS ORACLE '("TECOKT","TECOKT~1")'.

  ENDIF.

**now call the search help
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
*   DDIC_STRUCTURE         = ' '
      RETFIELD               = 'COKEY'
*   PVALKEY                = ' '
*   DYNPPROG               = ' '
*   DYNPNR                 = ' '
*   DYNPROFIELD            = ' '
*   STEPL                  = 0
     WINDOW_TITLE           = 'Division'
     VALUE                  = DYFIELDS-FIELDVALUE
     VALUE_ORG              = 'S'
*   MULTIPLE_CHOICE        = ' '
*   DISPLAY                = ' '
*   CALLBACK_PROGRAM       = ' '
*   CALLBACK_FORM          = ' '
*   MARK_TAB               =
* IMPORTING
*   USER_RESET             =
    TABLES
      VALUE_TAB              = GT_TECOKT
*   FIELD_TAB              =
     RETURN_TAB             = LT_RETURN_TAB
*   DYNPFLD_MAPPING        =
* EXCEPTIONS
*   PARAMETER_ERROR        = 1
*   NO_VALUES_FOUND        = 2
*   OTHERS                 = 3
            .
  IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
  READ TABLE LT_RETURN_TAB INTO LWA_RETURN_TAB INDEX 1.
  LV_COKEY = LWA_RETURN_TAB-FIELDVAL.
*break csebdev1.

ENDFORM.                    " divison_data_get
*&---------------------------------------------------------------------*
*&      Form  final_table_prepare
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FINAL_TABLE_PREPARE .

  DATA : LWA_INDUSTRY_TEXT_TEMP LIKE GT_TB024.

  DATA : LWA_FINAL_DATA_AC1 LIKE GWA_FINAL_DATA,
         LWA_FINAL_DATA_AC2 LIKE GWA_FINAL_DATA,
         LWA_FINAL_DATA_AC3 LIKE GWA_FINAL_DATA,
         LWA_FINAL_DATA_AC4 LIKE GWA_FINAL_DATA,
         LWA_FINAL_DATA_AC5 LIKE GWA_FINAL_DATA.


  DATA: LV_DEMAND1 TYPE DBERDLB-NETTOBTR,
        LV_DEMAND2 LIKE LV_DEMAND1,
        LV_DEMAND3 LIKE LV_DEMAND1,
        LV_DEMAND4 LIKE LV_DEMAND1,
        LV_DEMAND5 LIKE LV_DEMAND1,
        LV_DEMAND  LIKE LV_DEMAND1,

        LV_ARREARS TYPE DBERDL-NETTOBTR,
        LV_INACT_ARR TYPE DFKKOP-BETRH,
        LV_SURCHARGE TYPE DFKKOP-BETRH,
        LV_CON_LOAD TYPE P DECIMALS 2,
        LV_UNITS TYPE P DECIMALS 2,
        LV_LOADCODE TYPE ETTIFN-WERT1,
        LV_MTR_RENT TYPE DBERDLB-NETTOBTR,
        LV_ED TYPE DBERDLB-NETTOBTR,
        LV_CESS TYPE DBERDLB-NETTOBTR,
        LV_ED_UTTY TYPE DBERDLB-NETTOBTR,
        LV_MISC_REV TYPE DBERDLB-NETTOBTR,
        LV_CESS_UTTY TYPE DBERDLB-NETTOBTR,
        LV_SD_DMD    TYPE DBERDL-NETTOBTR,
        LV_SD_ARR    TYPE DBERDL-NETTOBTR,
        LV_DIF1 TYPE DBERCHV-EZ_ABRMENGE,
        LV_DEFMTR TYPE I,
        LV_UNITS_INT TYPE I,
        LV_DIF2 LIKE LV_DIF1,
        LV_DFR1 LIKE LV_DIF1,
        LV_DIF3 LIKE LV_DIF1,
        LV_DATE TYPE DATS,
        LV_CH   TYPE C,
        LV_MF   TYPE ETTIFN-WERT1 ,

        PRE_BMNTH LIKE P_BMNTH,
        LV_CHKDATE TYPE SY-DATUM,
        LV_BUDAT TYPE SY-DATUM,
        LV_PBUDAT TYPE SY-DATUM.

  FIELD-SYMBOLS <TVORG> TYPE DBERCHZ1-TVORG.
***now put all data in a itab
  SORT GT_EVER_EANLH_FKKVKP BY BRANCHE.
**now put the industry in the order required for output
  READ TABLE GT_TB024 INTO GT_TB024
  WITH KEY IND_SECTOR = '55'.
  IF SY-SUBRC = 0.
    DELETE TABLE GT_TB024 FROM GT_TB024.
    INSERT GT_TB024 INTO GT_TB024 INDEX 3.
  ENDIF.
  CLEAR GT_TB024.

  READ TABLE GT_TB024 INTO GT_TB024
  WITH KEY IND_SECTOR = '49'.
  IF SY-SUBRC = 0.
    DELETE TABLE GT_TB024 FROM GT_TB024.
    INSERT GT_TB024 INTO GT_TB024 INDEX 5.
  ENDIF.
  CLEAR GT_TB024.

  READ TABLE GT_TB024 INTO GT_TB024
    WITH KEY IND_SECTOR = '57'.
  IF SY-SUBRC = 0.
    DELETE TABLE GT_TB024 FROM GT_TB024.
    INSERT GT_TB024 INTO GT_TB024 INDEX 47.
  ENDIF.
  CLEAR GT_TB024.

  READ TABLE GT_TB024 INTO GT_TB024
    WITH KEY IND_SECTOR = '58'.
  IF SY-SUBRC = 0.
    DELETE TABLE GT_TB024 FROM GT_TB024.
    INSERT GT_TB024 INTO GT_TB024 INDEX 48.
  ENDIF.
  CLEAR GT_TB024.

  READ TABLE GT_TB024 INTO GT_TB024
    WITH KEY IND_SECTOR = '94'.
  IF SY-SUBRC = 0.
    DELETE TABLE GT_TB024 FROM GT_TB024.
    INSERT GT_TB024 INTO GT_TB024 INDEX 49.
  ENDIF.
  CLEAR GT_TB024.

  CONCATENATE P_BMNTH(4) P_BMNTH+5(2) '01' INTO LV_CHKDATE.

  LV_CHKDATE = LV_CHKDATE - 1.

  CONCATENATE LV_CHKDATE(4) '/'  LV_CHKDATE+4(2) INTO PRE_BMNTH.

  CLEAR LV_CHKDATE.
*  read table gt_tb024 into gt_tb024 index 1.
*  gv_industry = gt_tb024-ind_sector.
*
  LOOP AT GT_TB024.                                         " Loop 1
* Consumer's Industry Code & text & A/C Code for Revenue Category.
*   gwa_final_data-ind_sector = gt_tb024-ind_sector.
    LWA_FINAL_DATA_AC1-IND_SECTOR = GT_TB024-IND_SECTOR.
    LWA_FINAL_DATA_AC2-IND_SECTOR = GT_TB024-IND_SECTOR.
    LWA_FINAL_DATA_AC3-IND_SECTOR = GT_TB024-IND_SECTOR.
    LWA_FINAL_DATA_AC4-IND_SECTOR = GT_TB024-IND_SECTOR.
    LWA_FINAL_DATA_AC5-IND_SECTOR = GT_TB024-IND_SECTOR.

*   gwa_final_data-industry = gt_tb024-textlong.
    LWA_FINAL_DATA_AC1-INDUSTRY = GT_TB024-TEXTLONG.
    LWA_FINAL_DATA_AC2-INDUSTRY = GT_TB024-TEXTLONG.
    LWA_FINAL_DATA_AC3-INDUSTRY = GT_TB024-TEXTLONG.
    LWA_FINAL_DATA_AC4-INDUSTRY = GT_TB024-TEXTLONG.
    LWA_FINAL_DATA_AC5-INDUSTRY = GT_TB024-TEXTLONG.

    LWA_FINAL_DATA_AC1-AC_CODE = 'I130301'.
    LWA_FINAL_DATA_AC2-AC_CODE = 'I130302'.
    LWA_FINAL_DATA_AC3-AC_CODE = 'I130303'.
    LWA_FINAL_DATA_AC4-AC_CODE = 'I130304'.
    LWA_FINAL_DATA_AC5-AC_CODE = 'I130305'.


*   gwa_final_data-ac_code = 'I130301'.

* Clear all variables.
    CLEAR: LV_DEMAND,
           LV_DEFMTR,
           LV_ARREARS,
           LV_CON_LOAD,
           LV_UNITS,
           LV_LOADCODE,
           LV_ARREARS,
           LV_SURCHARGE,
           LV_MTR_RENT,
           LV_CESS,
           LV_CESS_UTTY,
           LV_ED,
           LV_ED_UTTY,
           LV_MISC_REV,
           LV_DIF1,
           LV_DIF2,
           LV_DFR1,
           LV_DIF3.

*    READ TABLE GT_ERCHC INTO GWA_ERCHC INDEX 1.
*    IF SY-SUBRC = 0.
*      LV_CHKDATE = GWA_ERCHC-BUDAT.
*    ELSE.
*      LV_CHKDATE = SY-DATUM.
*    ENDIF.
*    CLEAR GWA_ERCHC.
    IF GT_ERCHC[] IS INITIAL.
      LV_BUDAT = SY-DATUM.
    ELSE.
      SORT GT_ERCHC BY BUDAT DESCENDING.
      READ TABLE GT_ERCHC INTO GWA_ERCHC INDEX 1.
      LV_BUDAT = GWA_ERCHC-BUDAT.
    ENDIF.

    LOOP AT GT_EVER_EANLH_FKKVKP INTO GWA_EVER_EANLH_FKKVKP
      WHERE BRANCHE = GT_TB024-IND_SECTOR.                  " Loop 2

      IF GWA_EVER_EANLH_FKKVKP-ABRSPERR = SPACE.

        LOOP AT GT_ERCH INTO GWA_ERCH
          WHERE VKONT = GWA_EVER_EANLH_FKKVKP-VKONT.

          READ TABLE GT_ERCHC INTO GWA_ERCHC
            WITH KEY BELNR = GWA_ERCH-BELNR.

          IF SY-SUBRC = 0.
            LV_BUDAT = GWA_ERCHC-BUDAT.

            LOOP AT GT_DBERDLB INTO GWA_DBERDLB
              WHERE PRINTDOC = GWA_ERCHC-OPBEL.

              READ TABLE GT_DBERCHZ1_BILL INTO GWA_DBERCHZ1_BILL
                WITH KEY BELNR = GWA_DBERDLB-BILLDOC
                         BELZEILE = GWA_DBERDLB-BILLDOCLINE.

              IF SY-SUBRC = 0.
                ASSIGN GWA_DBERCHZ1_BILL-TVORG TO <TVORG>.

                IF ( ( <TVORG> = '0001' OR <TVORG> = '0002' )
                  OR ( <TVORG> = '0027' OR <TVORG> = '0028' )
                  OR ( <TVORG> = '0217' OR <TVORG> = '0218' )
                  OR ( <TVORG> = '0229' OR <TVORG> = '0230' )
                  OR ( <TVORG> = '0231' OR <TVORG> = '0232' )
                  OR ( <TVORG> = '0235' OR <TVORG> = '0236' )
                  OR ( <TVORG> = '0243' OR <TVORG> = '0244' ) ).

                  LV_DEMAND1 = LV_DEMAND1 + GWA_DBERDLB-NETTOBTR.

                ELSEIF ( ( <TVORG> = '0003' OR <TVORG> = '0004' )
                      OR ( <TVORG> = '0037' OR <TVORG> = '0038' )
                      OR ( <TVORG> = '0085' OR <TVORG> = '0086' ) ).

                  LV_DEMAND2 = LV_DEMAND2 + GWA_DBERDLB-NETTOBTR.

                ELSEIF ( <TVORG> = '0005' OR <TVORG> = '0006' ).

                  LV_DEMAND3 = LV_DEMAND3 + GWA_DBERDLB-NETTOBTR.

                ELSEIF ( ( <TVORG> = '0023' OR <TVORG> = '0024' )
                      OR ( <TVORG> = '0067' OR <TVORG> = '0068' ) ).

                  LV_DEMAND4 = LV_DEMAND4 + GWA_DBERDLB-NETTOBTR.

                ELSEIF ( <TVORG> = '0085' OR <TVORG> = '0086' ).

                  LV_DEMAND5 = LV_DEMAND5 + GWA_DBERDLB-NETTOBTR.

*&--------Total Electricity Duty-------------------------------------*
                ELSEIF ( ( <TVORG> =  '0007' OR  <TVORG>  = '0008' )
                      OR ( <TVORG> =  '0101' OR  <TVORG> =  '0102' )
                      OR ( <TVORG> = '0225' OR <TVORG> = '0226' ) ).


                  IF ( GWA_EVER_EANLH_FKKVKP-BRANCHE = '57'
                    OR GWA_EVER_EANLH_FKKVKP-BRANCHE = '58' ).
*&-------Total Electricity Duty for Free Agri Pump (Utthan Yojna)----*
                    LV_ED_UTTY = LV_ED_UTTY + GWA_DBERDLB-NETTOBTR.
                  ELSE.
*&--------Total Electricity Duty for Others--------------------------*
                    LV_ED  = LV_ED + GWA_DBERDLB-NETTOBTR.
                  ENDIF.

*&--------Total Miscellenious Revenue--------------------------------*
                ELSEIF ( ( <TVORG> >= '0125' AND <TVORG> <= '0130' )
                      OR ( <TVORG> >= '0219' AND <TVORG> <= '0224' ) ).

                  LV_MISC_REV = LV_MISC_REV + GWA_DBERDLB-NETTOBTR.

*&---------Total Energy Cess-----------------------------------------*
                ELSEIF ( <TVORG>  = '0011'  OR <TVORG>  = '0012'
                      OR <TVORG> = '0227' OR <TVORG> = '0228' ).

                  IF ( GWA_EVER_EANLH_FKKVKP-BRANCHE = '57'
                    OR GWA_EVER_EANLH_FKKVKP-BRANCHE = '58' ).
*&---------Total Energy Cess For Free Agri Pump (Utthan Yojna)-------*
                    LV_CESS_UTTY = LV_CESS_UTTY
                                 + GWA_DBERDLB-NETTOBTR.
                  ELSE.
*&---------Total Energy Cess for Others------------------------------*
                    LV_CESS = LV_CESS + GWA_DBERDLB-NETTOBTR.
                  ENDIF.

*&---------Total Meter Rent------------------------------------------*
                ELSEIF ( <TVORG>  = '0013'  OR <TVORG>  = '0014' ).

                  LV_MTR_RENT = LV_MTR_RENT + GWA_DBERDLB-NETTOBTR.

                ENDIF.

              ENDIF. " SY-SUBRC = 0. " GT_DBERDLB
*&-----FOR CONSUMPTION------------------------------------------------*
              ON CHANGE OF GWA_DBERDLB-BILLDOC.
                LOOP AT GT_DBERCHV INTO GWA_DBERCHV
                  WHERE BELNR = GWA_DBERDLB-BILLDOC.

                  IF GWA_DBERCHV-ABLESGR = '02'.
                    LV_DIF1 = GWA_DBERCHV-EZ_ABRMENGE.
                  ENDIF.

                  IF  GWA_DBERCHV-ABLESGR = '01'.
                    IF GWA_DBERCHV-ABLESGRV = '02'.
                      LV_DIF2 = GWA_DBERCHV-EZ_ABRMENGE.
                    ENDIF.
                    LV_DIF3 = GWA_DBERCHV-EZ_ABRMENGE.
                  ENDIF.

                  CLEAR GWA_DBERCHV.

                ENDLOOP.  " GT_DBERCHV.

              ENDON.

              CLEAR : GWA_DBERCHZ1_BILL, GWA_DBERDLB.
            ENDLOOP. " GT_DBERDLB.

            LV_DFR1 = LV_DIF1 + LV_DIF2.

            CLEAR LV_UNITS.

            READ TABLE GT_ETTIFN INTO GWA_ETTIFN
              WITH KEY ANLAGE = GWA_EVER_EANLH_FKKVKP-ANLAGE
              OPERAND = 'KWH_MF'.
            IF SY-SUBRC = 0.
              LV_MF = GWA_ETTIFN-WERT1.
            ENDIF.
            IF LV_DFR1 > 0.
              LV_UNITS = LV_DFR1 .
            ELSE.
              LV_UNITS = LV_DIF3.
            ENDIF.

            CLEAR: LV_DFR1, LV_DIF3, LV_DIF1, LV_DIF2.
            IF LV_MF > 0.
              LV_UNITS = LV_UNITS * LV_MF.
            ENDIF.

          ELSE.
            LV_BUDAT = SY-DATUM.
          ENDIF.

          CLEAR LV_MF.
          CLEAR GWA_ERCH.
        ENDLOOP. " GT_ERCH.

*&-----GET THE FACTS DATA FOR ONLY ACTIVE CONSUMERS
        READ TABLE GT_ETTIFN INTO GWA_ETTIFN
           WITH KEY ANLAGE = GWA_EVER_EANLH_FKKVKP-ANLAGE
                   OPERAND = 'MTR_STS'.
        IF SY-SUBRC = 0.
          IF GWA_ETTIFN-WERT1 = 4.   " OR GWA_ETTIFN-WERT1 = 5.
            LV_DEFMTR = LV_DEFMTR + 1.
          ENDIF.
        ENDIF.

        CLEAR GWA_ETTIFN.

        READ TABLE GT_ETTIFN INTO GWA_ETTIFN
          WITH KEY ANLAGE  = GWA_EVER_EANLH_FKKVKP-ANLAGE
                   OPERAND = 'LOAD_CODE'.
        IF SY-SUBRC = 0.
          LV_LOADCODE = GWA_ETTIFN-WERT1.
          CLEAR GWA_ETTIFN.

          READ TABLE GT_ETTIFN INTO GWA_ETTIFN
            WITH KEY ANLAGE = GWA_EVER_EANLH_FKKVKP-ANLAGE
                    OPERAND = 'CONN_LOAD'.

        ENDIF.

      ENDIF. " gwa_ever_eanlh_fkkvkp-abrsperr = space.

      READ TABLE GT_ERCH INTO GWA_ERCH
        WITH KEY VKONT = GWA_EVER_EANLH_FKKVKP-VKONT.
      IF SY-SUBRC <> 0.
        LV_BUDAT = SY-DATUM.
      ENDIF.

*&----collect arrears amount for the consumer------------------*
      LOOP AT GT_DFKKOP_ARREAR INTO GWA_DFKKOP_ARREAR
        WHERE VKONT = GWA_EVER_EANLH_FKKVKP-VKONT
          AND BUDAT < LV_BUDAT.

        IF GWA_DFKKOP_ARREAR-HVORG = '0010'
          AND GWA_DFKKOP_ARREAR-TVORG = '0002'
          AND GWA_DFKKOP_ARREAR-XBLNR = PRE_BMNTH
          AND ( GWA_DFKKOP_ARREAR-AUGDT IS INITIAL OR
                GWA_DFKKOP_ARREAR-AUGDT > LV_BUDAT ).

*          IF GWA_EVER_EANLH_FKKVKP-ABRSPERR = SPACE.
          LV_SURCHARGE = LV_SURCHARGE + GWA_DFKKOP_ARREAR-BETRH.
*          ENDIF.

        ELSEIF GWA_DFKKOP_ARREAR-HVORG = '0020'
           AND GWA_DFKKOP_ARREAR-TVORG = '0020'.
          IF GWA_EVER_EANLH_FKKVKP-ABRSPERR = SPACE.

            LV_PBUDAT = LV_BUDAT - 25.

            IF GWA_DFKKOP_ARREAR-BUDAT BETWEEN LV_PBUDAT AND LV_BUDAT.

              LV_SD_DMD = LV_SD_DMD + GWA_DFKKOP_ARREAR-BETRH.

            ELSEIF GWA_DFKKOP_ARREAR-BUDAT < LV_PBUDAT
               AND (  GWA_DFKKOP_ARREAR-AUGDT IS INITIAL
                   OR GWA_DFKKOP_ARREAR-AUGDT > LV_BUDAT ).

              LV_SD_ARR = LV_SD_ARR + GWA_DFKKOP_ARREAR-BETRH.

            ENDIF.
          ENDIF. " gwa_ever_eanlh_fkkvkp-abrsperr = space

        ELSEIF ( GWA_DFKKOP_ARREAR-AUGDT IS INITIAL
              OR GWA_DFKKOP_ARREAR-AUGDT > LV_BUDAT ).

          LV_ARREARS = LV_ARREARS + GWA_DFKKOP_ARREAR-BETRH.

        ENDIF.

        CLEAR GWA_DFKKOP_ARREAR.

      ENDLOOP. " gt_dfkkop_arrear.

*&----STORING DATA INTO FINAL WORK AREA------------------------------
      IF GWA_EVER_EANLH_FKKVKP-KTOKL = 'R'.
        IF GWA_EVER_EANLH_FKKVKP-ABRSPERR = SPACE.
          LWA_FINAL_DATA_AC1-RPARTNER = LWA_FINAL_DATA_AC1-RPARTNER + 1.
* Rural Connected Load.
          IF LV_LOADCODE = '1.0'.
            LWA_FINAL_DATA_AC1-RLOAD = LWA_FINAL_DATA_AC1-RLOAD +
                                  ( GWA_ETTIFN-WERT1 / 1000 ).
          ELSE.
            LWA_FINAL_DATA_AC1-RLOAD = LWA_FINAL_DATA_AC1-RLOAD +
                                  ( GWA_ETTIFN-WERT1 * '0.746' ).
          ENDIF.
        ENDIF.

* Rural Sold Units
       LWA_FINAL_DATA_AC1-RUNITS = LWA_FINAL_DATA_AC1-RUNITS + LV_UNITS.
        CLEAR LV_UNITS.

* Rural Arrears

        LWA_FINAL_DATA_AC1-RARREARS = LWA_FINAL_DATA_AC1-RARREARS
                                + LV_ARREARS." - lv_surcharge.

        LWA_FINAL_DATA_AC1-RINACT_ARR = LWA_FINAL_DATA_AC1-RINACT_ARR
                                  + LV_INACT_ARR .

        CLEAR: LV_ARREARS,LV_INACT_ARR.

* Rural Demand.
        LWA_FINAL_DATA_AC1-RDEMANDS = LWA_FINAL_DATA_AC1-RDEMANDS
                                    + LV_DEMAND1.
        LWA_FINAL_DATA_AC2-RDEMANDS = LWA_FINAL_DATA_AC2-RDEMANDS
                                    + LV_DEMAND2.
        LWA_FINAL_DATA_AC3-RDEMANDS = LWA_FINAL_DATA_AC3-RDEMANDS
                                    + LV_DEMAND3.
        LWA_FINAL_DATA_AC4-RDEMANDS = LWA_FINAL_DATA_AC4-RDEMANDS
                                    + LV_DEMAND4.
        LWA_FINAL_DATA_AC5-RDEMANDS = LWA_FINAL_DATA_AC5-RDEMANDS
                                    + LV_DEMAND5.

        CLEAR: LV_DEMAND1,LV_DEMAND2,LV_DEMAND3,
               LV_DEMAND4,LV_DEMAND5.

* Rural Defectivew Meters.
        LWA_FINAL_DATA_AC1-RBAD_METER = LWA_FINAL_DATA_AC1-RBAD_METER
                                      + LV_DEFMTR.

        CLEAR LV_DEFMTR.

* Rural Total Meter Rent
        GV_RTOTMETERRENT = GV_RTOTMETERRENT + LV_MTR_RENT.

        CLEAR LV_MTR_RENT.

* Rural Total Surcharge.
        GV_RTOTSURCHRG = GV_RTOTSURCHRG + LV_SURCHARGE.

        CLEAR LV_SURCHARGE.

* Rural Total Electricity Duty.
        GV_RTOTED     = GV_RTOTED + LV_ED.

        CLEAR LV_ED.

* Rural Total ED for Utthan Yojna.
        GV_RTOTEDAGRI = GV_RTOTEDAGRI + LV_ED_UTTY.

        CLEAR LV_ED_UTTY.

* Rural Total Cess.
        GV_RTOTEDCESS  = GV_RTOTEDCESS + LV_CESS.

        CLEAR LV_CESS.

* Rural Total Cess for Utthan Yojna.
        GV_RTOTCESSAGRI = GV_RTOTCESSAGRI + LV_CESS_UTTY.

        CLEAR LV_CESS_UTTY.

* Rural Total Miscelleneous Revenue.
        GV_RTOTMISCREV = GV_RTOTMISCREV + LV_MISC_REV.

        CLEAR LV_MISC_REV.

* Rural Total Security Deposit.
        GV_R_SD_DMD = GV_R_SD_DMD + LV_SD_DMD.

        GV_R_SD_ARR = GV_R_SD_ARR + LV_SD_ARR.

        CLEAR: LV_SD_DMD, LV_SD_ARR.

      ELSEIF GWA_EVER_EANLH_FKKVKP-KTOKL = 'U'.
        IF GWA_EVER_EANLH_FKKVKP-ABRSPERR <> SPACE.
        LWA_FINAL_DATA_AC1-UINACTIVE = LWA_FINAL_DATA_AC1-UINACTIVE + 1.

        ELSE.
*        gwa_final_data-upartner = gwa_final_data-upartner .
          LWA_FINAL_DATA_AC1-UPARTNER = LWA_FINAL_DATA_AC1-UPARTNER + 1.
* Urban Connected Load.
          IF LV_LOADCODE = 1.
            LWA_FINAL_DATA_AC1-ULOAD = LWA_FINAL_DATA_AC1-ULOAD +
                                  ( GWA_ETTIFN-WERT1 / 1000 ).
          ELSE.
            LWA_FINAL_DATA_AC1-ULOAD = LWA_FINAL_DATA_AC1-ULOAD +
                                  ( GWA_ETTIFN-WERT1 * '0.746' ).
          ENDIF.
*        lwa_final_data_ac1-uload = gwa_final_data-uload .
        ENDIF.


* Urban Sold Units.
*      gwa_final_data-uunits = gwa_final_data-uunits.
      LWA_FINAL_DATA_AC1-UUNITS = LWA_FINAL_DATA_AC1-UUNITS  + LV_UNITS.

        CLEAR LV_UNITS.

* Urban Arrears.
*      Gwa_final_data-uarrears = gwa_final_data-uarrears
        LWA_FINAL_DATA_AC1-UARREARS = LWA_FINAL_DATA_AC1-UARREARS
                                + LV_ARREARS. " - lv_surcharge.

*      gwa_final_data-uinact_arr = gwa_final_data-uinact_arr
*                                + lv_inact_arr.
        CLEAR: LV_ARREARS, LV_INACT_ARR.

* Urban Demand.
*      IF NOT ( lv_demand2 = 0 AND lv_demand3 = 0 AND
*               lv_demand4 = 0 AND lv_demand5 = 0 ).

        LWA_FINAL_DATA_AC1-UDEMANDS = LWA_FINAL_DATA_AC1-UDEMANDS
                                    + LV_DEMAND1.
        LWA_FINAL_DATA_AC2-UDEMANDS = LWA_FINAL_DATA_AC2-UDEMANDS
                                    + LV_DEMAND2.
        LWA_FINAL_DATA_AC3-UDEMANDS = LWA_FINAL_DATA_AC3-UDEMANDS
                                    + LV_DEMAND3.
        LWA_FINAL_DATA_AC4-UDEMANDS = LWA_FINAL_DATA_AC4-UDEMANDS
                                    + LV_DEMAND4.
        LWA_FINAL_DATA_AC5-UDEMANDS = LWA_FINAL_DATA_AC5-UDEMANDS
                                    + LV_DEMAND5.


*      ELSE.
*        lwa_final_data_ac1-rdemands = lwa_final_data_ac1-rdemands
*                                    + lv_demand1.
*      ENDIF.

        CLEAR: LV_DEMAND, LV_DEMAND1,LV_DEMAND2,LV_DEMAND3,
               LV_DEMAND4,LV_DEMAND5.

* Urban Defective Meters.
*      gwa_final_data-ubad_meter = gwa_final_data-ubad_meter.
        LWA_FINAL_DATA_AC1-UBAD_METER = LWA_FINAL_DATA_AC1-UBAD_METER
                                      + LV_DEFMTR.
        CLEAR LV_DEFMTR.

* Urban Total Meter Rent
        GV_UTOTMETERRENT = GV_UTOTMETERRENT + LV_MTR_RENT.
        CLEAR LV_MTR_RENT.

*break csebdev1.
* Urban Total Surcharge.
        GV_UTOTSURCHRG = GV_UTOTSURCHRG + LV_SURCHARGE.
        CLEAR LV_SURCHARGE.

* Urban Total Electricity Duty.
        GV_UTOTED     = GV_UTOTED + LV_ED.
        CLEAR LV_ED.

* Urban Total ED for Utthan Yojna.
        GV_UTOTEDAGRI = GV_UTOTEDAGRI + LV_ED_UTTY.
        CLEAR LV_ED_UTTY.

* Urban Total Cess.
        GV_UTOTEDCESS  = GV_UTOTEDCESS + LV_CESS.
        CLEAR LV_CESS.

* Urban Total Cess for Utthan Yojna.
        GV_UTOTCESSAGRI = GV_UTOTCESSAGRI + LV_CESS_UTTY.
        CLEAR LV_CESS_UTTY.

* Urban Total Miscelleneous Revenue.
        GV_UTOTMISCREV = GV_UTOTMISCREV + LV_MISC_REV.
        CLEAR LV_MISC_REV.

* Urban Total Security Deposit.
        GV_U_SD_DMD = GV_U_SD_DMD + LV_SD_DMD.
        GV_U_SD_ARR = GV_U_SD_ARR + LV_SD_ARR.
        CLEAR: LV_SD_DMD, LV_SD_ARR.

      ENDIF.

    ENDLOOP. " GT_EVER_EANLH_FKKVKP.

    APPEND LWA_FINAL_DATA_AC1 TO GT_FINAL_DATA.

    IF NOT ( LWA_FINAL_DATA_AC2-UDEMANDS = 0
       AND   LWA_FINAL_DATA_AC2-RDEMANDS = 0 ).
*     lv_ch = 'X'.
      APPEND LWA_FINAL_DATA_AC2 TO GT_FINAL_DATA.
    ENDIF.

    IF NOT ( LWA_FINAL_DATA_AC3-UDEMANDS = 0
       AND   LWA_FINAL_DATA_AC3-RDEMANDS = 0 ).
*     lv_ch = 'X'.
      APPEND LWA_FINAL_DATA_AC3 TO GT_FINAL_DATA.
    ENDIF.

    IF NOT ( LWA_FINAL_DATA_AC4-UDEMANDS = 0
       AND   LWA_FINAL_DATA_AC4-RDEMANDS = 0 ).
*     lv_ch = 'X'.
      APPEND LWA_FINAL_DATA_AC4 TO GT_FINAL_DATA.
    ENDIF.

    IF NOT ( LWA_FINAL_DATA_AC5-UDEMANDS = 0
       AND   LWA_FINAL_DATA_AC5-RDEMANDS = 0 ).
*     lv_ch = 'X'.
      APPEND LWA_FINAL_DATA_AC5 TO GT_FINAL_DATA.
    ENDIF.

*  Find the Grand Total For each Account Class i.e. Urban & Rural.
    GV_T_RPART = GV_T_RPART + LWA_FINAL_DATA_AC1-RPARTNER.
    GV_T_UPART = GV_T_UPART + LWA_FINAL_DATA_AC1-UPARTNER.

    GV_T_RINACTIVE = GV_T_RINACTIVE + LWA_FINAL_DATA_AC1-RINACTIVE.
    GV_T_UINACTIVE = GV_T_UINACTIVE + LWA_FINAL_DATA_AC1-UINACTIVE.

    GV_RDEF_MTR = GV_RDEF_MTR + LWA_FINAL_DATA_AC1-RBAD_METER.
    GV_UDEF_MTR = GV_UDEF_MTR + LWA_FINAL_DATA_AC1-UBAD_METER.

    GV_RCON_LOAD = GV_RCON_LOAD + LWA_FINAL_DATA_AC1-RLOAD.
    GV_UCON_LOAD = GV_UCON_LOAD + LWA_FINAL_DATA_AC1-ULOAD.

    GV_RUNITS = GV_RUNITS + LWA_FINAL_DATA_AC1-RUNITS.
    GV_UUNITS = GV_UUNITS + LWA_FINAL_DATA_AC1-UUNITS.

    GV_RDEMAND = GV_RDEMAND
               + LWA_FINAL_DATA_AC1-RDEMANDS
               + LWA_FINAL_DATA_AC2-RDEMANDS
               + LWA_FINAL_DATA_AC3-RDEMANDS
               + LWA_FINAL_DATA_AC4-RDEMANDS
               + LWA_FINAL_DATA_AC5-RDEMANDS.

    GV_UDEMAND = GV_UDEMAND
               + LWA_FINAL_DATA_AC1-UDEMANDS
               + LWA_FINAL_DATA_AC2-UDEMANDS
               + LWA_FINAL_DATA_AC3-UDEMANDS
               + LWA_FINAL_DATA_AC4-UDEMANDS
               + LWA_FINAL_DATA_AC5-UDEMANDS.

    GV_RARREAR = GV_RARREAR + LWA_FINAL_DATA_AC1-RARREARS.
    GV_UARREAR = GV_UARREAR + LWA_FINAL_DATA_AC1-UARREARS.

    GV_RINACT_ARR = GV_RINACT_ARR + LWA_FINAL_DATA_AC1-RINACT_ARR.
    GV_UINACT_ARR = GV_UINACT_ARR + LWA_FINAL_DATA_AC1-UINACT_ARR.

* Clear the work area.
    CLEAR: LWA_FINAL_DATA_AC1,
           LWA_FINAL_DATA_AC2,
           LWA_FINAL_DATA_AC3,
           LWA_FINAL_DATA_AC4,
           LWA_FINAL_DATA_AC5,
           GWA_ERCHC,
           GWA_EVER_EANLH_FKKVKP,
           GWA_DBERDLB,
           GWA_DBERCHZ1,
*         gwa_dberdl,
           GWA_DBERCHV,
           GWA_DFKKOP_ARREAR,
           GWA_BILLDOC.

  ENDLOOP.                                                  " GT_TB024.
*&----------Insert Total Meter Rent------------------------------------*
  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '8.1' .
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
  GWA_FINAL_DATA-INDUSTRY = TEXT-027.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'I130401'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTMETERRENT.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTMETERRENT.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

*&----------Insert Total Other Miscelleneous Charges-------------------*
  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '8.2' .
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
*for proper display as if it is initial the assumption is that it is a
*heading
  GWA_FINAL_DATA-INDUSTRY = TEXT-036.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'I130710'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTMISCREV.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTMISCREV.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

*&----------Insert Total surcharge-------------------------------------*
  CLEAR GWA_FINAL_DATA.

*break csebdev1.
** Insert Surcharge in final table
  GWA_FINAL_DATA-SLNO = '9' .
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
  GWA_FINAL_DATA-INDUSTRY = TEXT-010.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'I130706'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTSURCHRG.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTSURCHRG.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.
*
  GV_UDEMAND = GV_UDEMAND
             + GV_UTOTSURCHRG
             + GV_UTOTED
             + GV_UTOTEDCESS
             + GV_UTOTMETERRENT
             + GV_UTOTMISCREV
             + GV_UTOTEDAGRI
             + GV_UTOTCESSAGRI.

  GV_RDEMAND = GV_RDEMAND
             + GV_RTOTSURCHRG
             + GV_RTOTED
             + GV_RTOTEDCESS
             + GV_RTOTMETERRENT
             + GV_RTOTMISCREV
             + GV_RTOTEDAGRI
             + GV_RTOTCESSAGRI.

ENDFORM.                    " final_table_prepare
*&---------------------------------------------------------------------*
*&      Form  final_output_prepare
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FINAL_OUTPUT_PREPARE .
**This is for the proper display of Industry i.e. Revenue caegory
  DATA: LV_INT TYPE I,
        LV_PACKED TYPE P DECIMALS 2.

  FIELD-SYMBOLS : <LWA_FINAL_DATA> LIKE GWA_FINAL_DATA.

  LOOP AT GT_FINAL_DATA ASSIGNING <LWA_FINAL_DATA>.
    CASE <LWA_FINAL_DATA>-IND_SECTOR.
      WHEN '01'.
        <LWA_FINAL_DATA>-SLNO = '1.1'.
      WHEN '02'.
        <LWA_FINAL_DATA>-SLNO = '1.2'.
      WHEN '03'.
        <LWA_FINAL_DATA>-SLNO = '1.4'.
      WHEN '04'.
        <LWA_FINAL_DATA>-SLNO = '2.1'.
      WHEN '05'.
        <LWA_FINAL_DATA>-SLNO = '2.2'.
      WHEN '06'.
        <LWA_FINAL_DATA>-SLNO = '2.3'.
      WHEN '07'.
        <LWA_FINAL_DATA>-SLNO = '3'.
      WHEN '08'.
        <LWA_FINAL_DATA>-SLNO = '4.11'.
      WHEN '09'.
        <LWA_FINAL_DATA>-SLNO = '4.12'.
      WHEN '10'.
        <LWA_FINAL_DATA>-SLNO = '4.13'.
      WHEN '11'.
        <LWA_FINAL_DATA>-SLNO = '4.14'.
      WHEN '12'.
        <LWA_FINAL_DATA>-SLNO = '4.15'.
      WHEN '13'.
        <LWA_FINAL_DATA>-SLNO = '4.16'.
      WHEN '14'.
        <LWA_FINAL_DATA>-SLNO = '4.17'.
      WHEN '15'.
        <LWA_FINAL_DATA>-SLNO = '4.21'.
      WHEN '16'.
        <LWA_FINAL_DATA>-SLNO = '4.22'.
      WHEN '17'.
        <LWA_FINAL_DATA>-SLNO = '4.23'.
      WHEN '18'.
        <LWA_FINAL_DATA>-SLNO = '4.24'.
      WHEN '19'.
        <LWA_FINAL_DATA>-SLNO = '4.25'.
      WHEN '20'.
        <LWA_FINAL_DATA>-SLNO = '4.26'.
      WHEN '21'.
        <LWA_FINAL_DATA>-SLNO = '4.27'.
      WHEN '22'.
        <LWA_FINAL_DATA>-SLNO = '4.31'.
      WHEN '23'.
        <LWA_FINAL_DATA>-SLNO = '4.32'.
      WHEN '24'.
        <LWA_FINAL_DATA>-SLNO = '4.33'.
      WHEN '25'.
        <LWA_FINAL_DATA>-SLNO = '4.34'.
      WHEN '26'.
        <LWA_FINAL_DATA>-SLNO = '4.35'.
      WHEN '27'.
        <LWA_FINAL_DATA>-SLNO = '4.36'.
      WHEN '28'.
        <LWA_FINAL_DATA>-SLNO = '4.37'.
      WHEN '29'.
        <LWA_FINAL_DATA>-SLNO = '4.41'.
      WHEN '30'.
        <LWA_FINAL_DATA>-SLNO = '4.42'.
      WHEN '31'.
        <LWA_FINAL_DATA>-SLNO = '4.43'.
      WHEN '32'.
        <LWA_FINAL_DATA>-SLNO = '4.44'.
      WHEN '33'.
        <LWA_FINAL_DATA>-SLNO = '4.45'.
      WHEN '34'.
        <LWA_FINAL_DATA>-SLNO = '4.46'.
      WHEN '35'.
        <LWA_FINAL_DATA>-SLNO = '4.47'.
      WHEN '36'.
        <LWA_FINAL_DATA>-SLNO = '5.11'.
      WHEN '37'.
        <LWA_FINAL_DATA>-SLNO = '5.12'.
      WHEN '38'.
        <LWA_FINAL_DATA>-SLNO = '5.13'.
      WHEN '39'.
        <LWA_FINAL_DATA>-SLNO = '5.14'.
      WHEN '40'.
        <LWA_FINAL_DATA>-SLNO = '5.15'.
      WHEN '41'.
        <LWA_FINAL_DATA>-SLNO = '5.21'.
      WHEN '42'.
        <LWA_FINAL_DATA>-SLNO = '5.22'.
      WHEN '43'.
        <LWA_FINAL_DATA>-SLNO = '5.23'.
      WHEN '44'.
        <LWA_FINAL_DATA>-SLNO = '5.24'.
      WHEN '45'.
        <LWA_FINAL_DATA>-SLNO = '5.41'.
      WHEN '46'.
        <LWA_FINAL_DATA>-SLNO = '5.42'.
      WHEN '47'.
        <LWA_FINAL_DATA>-SLNO = '6.1'.
      WHEN '48'.
        <LWA_FINAL_DATA>-SLNO = '6.2'.
      WHEN '49'.
        <LWA_FINAL_DATA>-SLNO = '1.5'.
      WHEN '50'.
        <LWA_FINAL_DATA>-SLNO = '7.1'.
      WHEN '51'.
        <LWA_FINAL_DATA>-SLNO = '7.2'.
      WHEN '52'.
        <LWA_FINAL_DATA>-SLNO = '7.3'.
      WHEN '53'.
        <LWA_FINAL_DATA>-SLNO = '7.4'.
      WHEN '54'.
        <LWA_FINAL_DATA>-SLNO = '7.5'.
      WHEN '55'.
        <LWA_FINAL_DATA>-SLNO = '1.3'.
      WHEN '57'.
        <LWA_FINAL_DATA>-SLNO = '5.31'.
      WHEN '58'.
        <LWA_FINAL_DATA>-SLNO = '5.32'.
      WHEN '94'.
        <LWA_FINAL_DATA>-SLNO = '5.33'.

    ENDCASE.

  ENDLOOP.
  UNASSIGN <LWA_FINAL_DATA>.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '1'.
  GWA_FINAL_DATA-INDUSTRY = 'Domestic L&F and Power'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '2'.
  GWA_FINAL_DATA-INDUSTRY = 'Non-Domestic/Commercial L&F and Power'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '4'.
  GWA_FINAL_DATA-INDUSTRY = 'Industrial Power'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '4.1'.
  GWA_FINAL_DATA-INDUSTRY = 'Upto 25HP'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '4.2'.
  GWA_FINAL_DATA-INDUSTRY = 'Above 25HP & upto 75HP'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '4.3'.
  GWA_FINAL_DATA-INDUSTRY = 'Above 75HP & upto 100HP'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '4.4'.
  GWA_FINAL_DATA-INDUSTRY = 'Above 100HP'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '5'.
  GWA_FINAL_DATA-INDUSTRY = 'Agricultural Consumers'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '5.1'.
  GWA_FINAL_DATA-INDUSTRY = 'Metered Pumps & tubewells'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '5.2'.
  GWA_FINAL_DATA-INDUSTRY = 'Flat Rate Pumps & Tubewells'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '5.3'.
  GWA_FINAL_DATA-INDUSTRY = 'Beneficiary Agricultural Pump/Thresher'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '5.4'.
GWA_FINAL_DATA-INDUSTRY = 'Metered & Flat Rate Billing Pump & tubewell'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '6'.
  GWA_FINAL_DATA-INDUSTRY = 'Street Light'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '7'.
  GWA_FINAL_DATA-INDUSTRY = 'Temporary Connections'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '8'.
  GWA_FINAL_DATA-INDUSTRY = 'Err Revenue Category'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '8'.
  GWA_FINAL_DATA-INDUSTRY = 'Misc Revenue Included in Energy Bills'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  SORT GT_FINAL_DATA BY SLNO AC_CODE INDUSTRY.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '10'.
  GWA_FINAL_DATA-INDUSTRY = 'Electricity Duty'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.
*&--------Insert Total ED for Free Agri Pump ( Utthan Yojna )----------*
  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '10.1' .
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
  GWA_FINAL_DATA-INDUSTRY = TEXT-037.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'L112006'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTEDAGRI.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTEDAGRI.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.


*&----------Insert Total Electricity Duty for Others-------------------*
  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '10.2' .
  GWA_FINAL_DATA-INDUSTRY = TEXT-011.
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'L112006'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTED.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTED.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

*&--------Insert Total Cess for Free Agri Pump (Utthan Yojna)----------*
  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '11'.
  GWA_FINAL_DATA-INDUSTRY = 'Energy Development Cess'.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.

  CLEAR GWA_FINAL_DATA.
  GWA_FINAL_DATA-SLNO = '11.1' .
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
  GWA_FINAL_DATA-INDUSTRY = TEXT-037.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'L112007'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTCESSAGRI.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTCESSAGRI.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.


*&----------Insert Total Cess for Others-------------------------------*
  CLEAR GWA_FINAL_DATA.
**insert ED cess in final table
  GWA_FINAL_DATA-SLNO = '11.2' .
  GWA_FINAL_DATA-INDUSTRY = TEXT-011.
  GWA_FINAL_DATA-IND_SECTOR = 'XX'.
**G/L Account no
  GWA_FINAL_DATA-AC_CODE = 'L112007'.
  GWA_FINAL_DATA-UDEMANDS = GV_UTOTEDCESS.
  GWA_FINAL_DATA-RDEMANDS = GV_RTOTEDCESS.
  APPEND GWA_FINAL_DATA TO GT_FINAL_DATA.


  IF P_INCLUD = 'X'.
*&------TO ADD THE MANUAL PART OF R-15 REPORT--------------------------*
    LOOP AT GT_FINAL_DATA INTO GWA_FINAL_DATA.

      READ TABLE GT_MANUAL_DATA INTO GWA_MANUAL_DATA
        WITH KEY COL_12 = GWA_FINAL_DATA-IND_SECTOR.
      IF SY-SUBRC = 0.
      REPLACE ALL OCCURRENCES OF ',' IN: GWA_MANUAL_DATA-COL_6 WITH ' ',
                                         GWA_MANUAL_DATA-COL_7 WITH ' ',
                                         GWA_MANUAL_DATA-COL_8 WITH ' ',
                                         GWA_MANUAL_DATA-COL_9 WITH ' ',
                                        GWA_MANUAL_DATA-COL_10 WITH ' ',
                                        GWA_MANUAL_DATA-COL_11 WITH ' ',
                                        GWA_MANUAL_DATA-COL_13 WITH ' ',
                                        GWA_MANUAL_DATA-COL_14 WITH ' ',
                                        GWA_MANUAL_DATA-COL_15 WITH ' ',
                                        GWA_MANUAL_DATA-COL_16 WITH ' ',
                                        GWA_MANUAL_DATA-COL_17 WITH ' ',
                                        GWA_MANUAL_DATA-COL_18 WITH ' '.
        CONDENSE: GWA_MANUAL_DATA-COL_6,
                  GWA_MANUAL_DATA-COL_7,
                  GWA_MANUAL_DATA-COL_8,
                  GWA_MANUAL_DATA-COL_9,
                  GWA_MANUAL_DATA-COL_10,
                  GWA_MANUAL_DATA-COL_11,
                  GWA_MANUAL_DATA-COL_13,
                  GWA_MANUAL_DATA-COL_14,
                  GWA_MANUAL_DATA-COL_15,
                  GWA_MANUAL_DATA-COL_16,
                  GWA_MANUAL_DATA-COL_17,
                  GWA_MANUAL_DATA-COL_18.

        LV_INT = GWA_MANUAL_DATA-COL_6.
        GWA_FINAL_DATA-UPARTNER = GWA_FINAL_DATA-UPARTNER + LV_INT.
        GV_T_UPART = GV_T_UPART + LV_INT.

        CLEAR LV_INT.
        LV_INT = GWA_MANUAL_DATA-COL_7.
        GWA_FINAL_DATA-UBAD_METER = GWA_FINAL_DATA-UBAD_METER + LV_INT.
        GV_UDEF_MTR = GV_UDEF_MTR + LV_INT.

        CLEAR LV_INT.
        LV_PACKED = GWA_MANUAL_DATA-COL_8.
        GWA_FINAL_DATA-ULOAD = GWA_FINAL_DATA-ULOAD + LV_PACKED.
        GV_UCON_LOAD = GV_UCON_LOAD + LV_PACKED.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_9.
        GWA_FINAL_DATA-UUNITS = GWA_FINAL_DATA-UUNITS + LV_PACKED.
        GV_UUNITS = GV_UUNITS + LV_PACKED.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_10.
        GWA_FINAL_DATA-UDEMANDS = GWA_FINAL_DATA-UDEMANDS + LV_PACKED.
        GV_UDEMAND = GV_UDEMAND + LV_PACKED.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_11.
        GWA_FINAL_DATA-UARREARS = GWA_FINAL_DATA-UARREARS + LV_PACKED.
        GV_UARREAR  = GV_UARREAR + LV_PACKED.

        CLEAR LV_INT.
        LV_INT = GWA_MANUAL_DATA-COL_13.
        GWA_FINAL_DATA-RPARTNER = GWA_FINAL_DATA-RPARTNER + LV_INT.
        GV_T_RPART  = GV_T_RPART + LV_INT.

        CLEAR LV_INT.
        LV_INT = GWA_MANUAL_DATA-COL_14.
        GWA_FINAL_DATA-RBAD_METER = GWA_FINAL_DATA-RBAD_METER + LV_INT.
        GV_RDEF_MTR = GV_RDEF_MTR + LV_INT.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_15.
        GWA_FINAL_DATA-RLOAD = GWA_FINAL_DATA-RLOAD + LV_PACKED.
        GV_RCON_LOAD = GV_RCON_LOAD + LV_PACKED.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_16.
        GWA_FINAL_DATA-RUNITS = GWA_FINAL_DATA-RUNITS + LV_PACKED.
        GV_RUNITS = GV_RUNITS + LV_PACKED.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_17.
        GWA_FINAL_DATA-RDEMANDS = GWA_FINAL_DATA-RDEMANDS + LV_PACKED.
        GV_RDEMAND = GV_RDEMAND + LV_PACKED.

        CLEAR LV_PACKED.
        LV_PACKED = GWA_MANUAL_DATA-COL_18.
        GWA_FINAL_DATA-RARREARS = GWA_FINAL_DATA-RARREARS + LV_PACKED.
        GV_RARREAR = GV_RARREAR + LV_PACKED.

        CLEAR: LV_INT, LV_PACKED.

        MODIFY GT_FINAL_DATA FROM GWA_FINAL_DATA.

      ENDIF.
    ENDLOOP.
  ENDIF.

ENDFORM.                    " final_output_prepare
*&---------------------------------------------------------------------*
*&      Form  final_output_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FINAL_OUTPUT_DISPLAY .
**to display the final output
*       for line count to make 64 when < 64 .
  DATA : LV_ADD TYPE I,
         LV_CNT TYPE I,
*         lv_udemands type i,
*         lv_rdemands type i,
         LV_SLNO LIKE GWA_FINAL_DATA-SLNO.
  DATA: LV_URTOT1 TYPE I,
        LV_URTOT2 LIKE GV_UCON_LOAD.

  DATA: LV_UDEMANDS TYPE I ,
        LV_RDEMANDS  TYPE I,
        LV_ULOAD  TYPE I,
        LV_RLOAD TYPE I,
        LV_UUNITS TYPE I,
        LV_RUNITS TYPE I,
        LV_UPARTNER TYPE I,
        LV_RPARTNER TYPE I,
        LV_UBAD_METER TYPE I,
        LV_RBAD_METER TYPE I,
        LV_UARREARS   TYPE I,
        LV_RARREARS   TYPE I.


  FORMAT COLOR 2 ON INTENSIFIED OFF.

*  sort gt_final_data by slno. " if not working then remove.
  LV_CNT = 0.
  LOOP AT GT_FINAL_DATA INTO GWA_FINAL_DATA.
    IF LV_CNT >= 1 AND LV_SLNO <> GWA_FINAL_DATA-SLNO.
      FORMAT COLOR 3 ON INTENSIFIED OFF.
      WRITE : /1 SY-VLINE,
               2 LV_SLNO ,
               6 SY-VLINE,
               7(43) 'Subtotal',
               50 SY-VLINE,
               58 SY-VLINE,
               59(7) LV_UPARTNER,
               67 SY-VLINE,
               68(6) LV_UBAD_METER,
               75 SY-VLINE,
               76(13) LV_ULOAD DECIMALS 0,
               89 SY-VLINE,
               90(19) LV_UUNITS DECIMALS 0,
               108 SY-VLINE,
               109(19)  LV_UDEMANDS DECIMALS 0,
               129 SY-VLINE,
               130(19) LV_UARREARS DECIMALS 0,
               151 SY-VLINE,
               152(7) LV_RPARTNER,
               160 SY-VLINE,
               161(6) LV_RBAD_METER,
               168 SY-VLINE,
               169(13) LV_RLOAD DECIMALS 0,
               182 SY-VLINE,
               183(18) LV_RUNITS DECIMALS 0,
               201 SY-VLINE,
               202(19) LV_RDEMANDS DECIMALS 0,
               222 SY-VLINE,
               223(19) LV_RARREARS DECIMALS 0,
               244 SY-VLINE.
      FORMAT COLOR 2 ON.
      WRITE: /1 SY-VLINE,
              6 SY-VLINE,
              50 SY-VLINE,
              58 SY-VLINE,
              67 SY-VLINE,
              75 SY-VLINE,
              89 SY-VLINE,
              108 SY-VLINE,
              129 SY-VLINE,
*                    137 sy-vline,
              151 SY-VLINE,
              160 SY-VLINE,
              168 SY-VLINE,
*                    176 sy-vline,
              182 SY-VLINE,
              201 SY-VLINE,
              222 SY-VLINE,
*                    237 sy-vline,
              244 SY-VLINE.

      CLEAR:  LV_UDEMANDS,
              LV_RDEMANDS,
              LV_ULOAD,
              LV_RLOAD,
              LV_UUNITS,
              LV_RUNITS,
              LV_UPARTNER,
              LV_RPARTNER,
              LV_UBAD_METER,
              LV_RBAD_METER,
              LV_RARREARS,
              LV_UARREARS,
              LV_CNT.
*           lv_slno = gwa_final_data-slno.
    ELSEIF LV_CNT = 0 AND LV_SLNO <> GWA_FINAL_DATA-SLNO.

      CLEAR:  LV_UDEMANDS,
              LV_RDEMANDS,
              LV_ULOAD,
              LV_RLOAD,
              LV_UUNITS,
              LV_RUNITS,
              LV_UPARTNER,
              LV_RPARTNER,
              LV_UBAD_METER,
              LV_RBAD_METER,
              LV_RARREARS,
              LV_UARREARS,
              LV_CNT.

    ENDIF.

    IF GWA_FINAL_DATA-IND_SECTOR IS INITIAL.
      WRITE :/1 SY-VLINE,
              2 GWA_FINAL_DATA-SLNO ,
              6 SY-VLINE,
              7(43) GWA_FINAL_DATA-INDUSTRY,
              50 SY-VLINE,
              58 SY-VLINE,
              67 SY-VLINE,
              75 SY-VLINE,
              89 SY-VLINE,
              108 SY-VLINE,
              129 SY-VLINE,
*        137 sy-vline,
              151 SY-VLINE,
              160 SY-VLINE,
              168 SY-VLINE,
*        176 sy-vline,
              182 SY-VLINE,
              201 SY-VLINE,
              222 SY-VLINE,
*        237 sy-vline,
              244 SY-VLINE.
      WRITE: /1 SY-VLINE,
              6 SY-VLINE,
              7(43) SY-ULINE,
              50 SY-VLINE,
              58 SY-VLINE,
              67 SY-VLINE,
              75 SY-VLINE,
              89 SY-VLINE,
              108 SY-VLINE,
              129 SY-VLINE,
*        137 sy-vline,
              151 SY-VLINE,
              160 SY-VLINE,
              168 SY-VLINE,
*        176 sy-vline,
              182 SY-VLINE,
              201 SY-VLINE,
              222 SY-VLINE,
*        237 sy-vline,
              244 SY-VLINE.


    ELSE.

      FORMAT COLOR 2 ON.
      IF LV_SLNO = GWA_FINAL_DATA-SLNO.
        WRITE: /1 SY-VLINE,
                6 SY-VLINE.
      ELSE.
        WRITE :/1 SY-VLINE,
       2 GWA_FINAL_DATA-SLNO ,
       6 SY-VLINE,
       7(43) GWA_FINAL_DATA-INDUSTRY.
      ENDIF.
      WRITE: 50 SY-VLINE,
      51(7) GWA_FINAL_DATA-AC_CODE,
      58 SY-VLINE,
      59(7) GWA_FINAL_DATA-UPARTNER,
      67 SY-VLINE,
      68(6) GWA_FINAL_DATA-UBAD_METER,
      75 SY-VLINE,
      76(13) GWA_FINAL_DATA-ULOAD DECIMALS 0,
      89 SY-VLINE,
      90(19) GWA_FINAL_DATA-UUNITS DECIMALS 0,
      108 SY-VLINE,
      109(19)  GWA_FINAL_DATA-UDEMANDS DECIMALS 0,
      129 SY-VLINE,
      130(19) GWA_FINAL_DATA-UARREARS DECIMALS 0,
      151 SY-VLINE,
      152(7) GWA_FINAL_DATA-RPARTNER,
      160 SY-VLINE,
      161(6) GWA_FINAL_DATA-RBAD_METER,
      168 SY-VLINE,
      169(13) GWA_FINAL_DATA-RLOAD DECIMALS 0,
      182 SY-VLINE,
      183(18) GWA_FINAL_DATA-RUNITS DECIMALS 0,
      201 SY-VLINE,
      202(19) GWA_FINAL_DATA-RDEMANDS DECIMALS 0,
      222 SY-VLINE,
      223(19) GWA_FINAL_DATA-RARREARS DECIMALS 0,
      244 SY-VLINE.

      IF LV_SLNO = GWA_FINAL_DATA-SLNO.

        LV_CNT = LV_CNT + 1.
      ENDIF.
*     lv_slno = gwa_final_data-slno.


      WRITE: /1 SY-VLINE,
              6 SY-VLINE,
              50 SY-VLINE,
              58 SY-VLINE,
              67 SY-VLINE,
              75 SY-VLINE,
              89 SY-VLINE,
              108 SY-VLINE,
              129 SY-VLINE,
*        137 sy-vline,
              151 SY-VLINE,
              160 SY-VLINE,
              168 SY-VLINE,
*        176 sy-vline,
              182 SY-VLINE,
              201 SY-VLINE,
              222 SY-VLINE,
*        237 sy-vline,
              244 SY-VLINE.

      LV_UDEMANDS   = LV_UDEMANDS   + GWA_FINAL_DATA-UDEMANDS.
      LV_RDEMANDS   = LV_RDEMANDS   + GWA_FINAL_DATA-RDEMANDS.
      LV_ULOAD      = LV_ULOAD      + GWA_FINAL_DATA-ULOAD.
      LV_RLOAD      = LV_RLOAD      + GWA_FINAL_DATA-RLOAD.
      LV_UUNITS     = LV_UUNITS     + GWA_FINAL_DATA-UUNITS.
      LV_RUNITS     = LV_RUNITS     + GWA_FINAL_DATA-RUNITS.
      LV_UPARTNER   = LV_UPARTNER   + GWA_FINAL_DATA-UPARTNER.
      LV_RPARTNER   = LV_RPARTNER   + GWA_FINAL_DATA-RPARTNER.
      LV_UBAD_METER = LV_UBAD_METER + GWA_FINAL_DATA-UBAD_METER.
      LV_RBAD_METER = LV_RBAD_METER + GWA_FINAL_DATA-RBAD_METER.
      LV_UARREARS   = LV_UARREARS   + GWA_FINAL_DATA-UARREARS.
      LV_RARREARS   = LV_RARREARS   + GWA_FINAL_DATA-RARREARS.

    ENDIF.

    LV_SLNO = GWA_FINAL_DATA-SLNO.

    CLEAR GWA_FINAL_DATA.
  ENDLOOP.


  FORMAT COLOR 3 ON.
  WRITE :/1(244) SY-ULINE.
  WRITE :/1 SY-VLINE,
  2 '****',
*  6 sy-vline,
  7 'Grand Total',
  50 SY-VLINE,
  58 SY-VLINE,
  59(7) GV_T_UPART,
  67 SY-VLINE,
  68(6) GV_UDEF_MTR,
  75 SY-VLINE,
  76(13) GV_UCON_LOAD DECIMALS 0,
  89 SY-VLINE,
  90(18) GV_UUNITS DECIMALS 0,
  108 SY-VLINE,
  109(19) GV_UDEMAND DECIMALS 0,
  129 SY-VLINE,
  130(19) GV_UARREAR DECIMALS 0,
  151 SY-VLINE,
  152(7) GV_T_RPART,
  160 SY-VLINE,
  161(6) GV_RDEF_MTR,
  168 SY-VLINE,
  169(13) GV_RCON_LOAD DECIMALS 0,
  182 SY-VLINE,
  183(18) GV_RUNITS DECIMALS 0,
  201 SY-VLINE,
  202(19) GV_RDEMAND DECIMALS 0,
  222 SY-VLINE,
  223(19) GV_RARREAR DECIMALS 0,
  244 SY-VLINE.

**write the grand total FOR URBAN+ RURAL
  WRITE :/1 SY-VLINE,
  2 '****',
*  6 sy-vline,
  7 'Grand Total (Urban+Rural)',
  50 SY-VLINE,
  58 SY-VLINE.
  LV_URTOT1 = GV_T_UPART + GV_T_RPART.
  WRITE : 59(7) LV_URTOT1,
  67 SY-VLINE.
  LV_URTOT1 = GV_UDEF_MTR + GV_RDEF_MTR.
  WRITE : 68(6) LV_URTOT1,
  75 SY-VLINE.
*  lv_urtot1 = gv_t_uinactive + gv_t_rinactive.
*  write: lv_urtot1,
*  sy-vline.
  LV_URTOT2 = GV_UCON_LOAD + GV_RCON_LOAD.
  WRITE : 76(13) LV_URTOT2 DECIMALS 0,
  89 SY-VLINE.
  LV_URTOT2 = GV_UUNITS + GV_RUNITS.
  WRITE : 90(18) LV_URTOT2 DECIMALS 0,
  108 SY-VLINE.
  LV_URTOT2 = GV_UDEMAND + GV_RDEMAND.
  WRITE : 109(19) LV_URTOT2 DECIMALS 0,
  129 SY-VLINE.
  LV_URTOT2 = GV_UARREAR + GV_RARREAR.
  WRITE : 130(19) LV_URTOT2 DECIMALS 0,
  151 SY-VLINE.
*  lv_urtot2 = gv_uinact_arr + gv_rinact_arr.
  WRITE:                                                    "lv_tot2,
*  sy-vline.
  160 SY-VLINE,
  168 SY-VLINE,
  182 SY-VLINE,
  201 SY-VLINE,
  222 SY-VLINE,
  244 SY-VLINE.

  WRITE :/1(244) SY-ULINE.
  FORMAT INTENSIFIED ON.
  WRITE :/       SY-VLINE,
         2       'SD Details',
         109(19) GV_U_SD_DMD DECIMALS 0,
         130(19) GV_U_SD_ARR DECIMALS 0,
         151     SY-VLINE,
         202(19) GV_R_SD_DMD DECIMALS 0,
         223(19) GV_R_SD_ARR DECIMALS 0,
         244     SY-VLINE.

  CLEAR LV_URTOT2.
  LV_URTOT2 = GV_U_SD_DMD + GV_R_SD_DMD.
  WRITE :/       SY-VLINE,
         109(19) LV_URTOT2  DECIMALS 0.

  CLEAR LV_URTOT2.
  LV_URTOT2 = GV_U_SD_ARR + GV_R_SD_ARR.
  WRITE: 130(19) LV_URTOT2 DECIMALS 0,
         151     SY-VLINE,
         244     SY-VLINE.

  WRITE: /1(244) SY-ULINE.

  FORMAT COLOR OFF.
  WRITE: /9 'Date of Running:', 26 SY-DATUM.
  WRITE: /9 'Time of Running:', 26 SY-UZEIT.
  WRITE: /9 'User           :',26 SY-UNAME, 100 'Signature'.
  WRITE: /9 'Submitted To   :',100 'Prepared by :', / SY-ULINE.

  WRITE: / SY-ULINE.


ENDFORM.                    " final_output_display
*&---------------------------------------------------------------------*
*&      Form  free_memory
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FREE_MEMORY .
  FREE:  GT_EVER_EANLH_FKKVKP,
         GT_ERCH,
         GT_ERCHC,
         GT_DBERCHZ1,
         GT_DBERCHZ1_BILL,
         GT_DBERDLB,
         GT_DFKKOP_ARREAR,
         GT_TB024,
         GT_TGSBT,
         GT_TECOKT,
         GWA_EVER_EANLH_FKKVKP,
         GWA_ERCH,
         GWA_ERCHC,
         GWA_DBERCHZ1,
         GWA_DBERCHZ1_BILL,
         GWA_DBERDLB,
         GWA_DFKKOP_ARREAR.

ENDFORM.                    " free_memory