FUNCTION ZWWW_PREPARE_TABLE.
*"----------------------------------------------------------------------
*"*"######### #########:
*"  IMPORTING
*"     VALUE(HEADER_NAME) TYPE  ZWWW_VALUES-VAR_NAME OPTIONAL
*"     VALUE(LINE_NAME) TYPE  ZWWW_VALUES-VAR_NAME
*"     VALUE(VAL_TYPE) TYPE  ZWWW_VALUES-VAL_TYPE DEFAULT ''
*"  TABLES
*"      IT_ANY_TABLE
*"      IT_VALUES TYPE  ZWWW_VALUES_T
*"      IT_FIELDS_CATALOG STRUCTURE  ZWWW_FIELD_CATALOG OPTIONAL
*"      IT_GROUPS_CATALOG STRUCTURE  ZWWW_GROUP_CATALOG OPTIONAL
*"----------------------------------------------------------------------
  TYPES:
    BEGIN OF T_OLD,
      NUM TYPE ZWWW_GROUP_CATALOG-GROUP_NUM,
      VAR_NUM TYPE ZWWW_VALUES-VAR_NUM,
      KEY TYPE REF TO DATA,
      SUM TYPE REF TO DATA,
    END OF T_OLD.

  DATA:
    IT_STRUC TYPE TT_STRUC,
    ANY_TABIX TYPE SY-TABIX,
    ValueStr type ZWWW_VALUES-VALUE, "(255),
    W_IT_VALUES TYPE ZWWW_VALUES,
    W_GRPVAL TYPE ZWWW_VALUES,
    IT_OLD TYPE SORTED TABLE OF T_OLD
      WITH UNIQUE KEY NUM
      WITH HEADER LINE,
    W_KEY TYPE REF TO DATA,
    W_SUM TYPE REF TO DATA,
    LINENUM TYPE I.

  FIELD-SYMBOLS:
    <IT_STRUC> LIKE LINE OF IT_STRUC,
    <IT_FLDCTLG> LIKE LINE OF IT_FIELDS_CATALOG,
    <IT_GRPCTLG> LIKE LINE OF IT_GROUPS_CATALOG,
    <GRPLINE>    LIKE LINE OF IT_GROUPS_CATALOG,

*    <it_Values> type ZWWW_VALUES,
    <IT_LINE>,
    <FLD>,
    <KEY>,
    <SUM>,
    <OLDKEY>,
    <KEYFLD>,
    <OLDSUM>,
    <SUMFLD>.

*  Perform AssignCharX.

  CREATE DATA W_KEY LIKE LINE OF IT_ANY_TABLE.
  ASSIGN W_KEY->* TO <KEY>.
  CREATE DATA W_SUM LIKE LINE OF IT_ANY_TABLE.
  ASSIGN W_SUM->* TO <SUM>.

  PERFORM INIT_DECIMAL_POINT.

  READ TABLE IT_ANY_TABLE ASSIGNING <IT_LINE> INDEX 1.
  CHECK SY-SUBRC = 0.
  PERFORM GETSTRUC USING <IT_LINE> IT_STRUC[].

  IF IT_GROUPS_CATALOG IS REQUESTED.
    SORT IT_GROUPS_CATALOG BY GROUP_NUM.
    LOOP AT IT_GROUPS_CATALOG ASSIGNING <IT_GRPCTLG>.
      TRANSLATE <IT_GRPCTLG>-FIELD_NAME TO UPPER CASE.
      AT NEW GROUP_NUM.
        IT_OLD-NUM = <IT_GRPCTLG>-GROUP_NUM.
        CREATE DATA IT_OLD-KEY LIKE LINE OF IT_ANY_TABLE.
        CREATE DATA IT_OLD-SUM LIKE LINE OF IT_ANY_TABLE.
        INSERT TABLE IT_OLD.
        CLEAR IT_OLD.
      ENDAT.
    ENDLOOP.
    SORT IT_GROUPS_CATALOG BY GROUP_NUM FIELD_NAME.
  ENDIF.

  IF IT_FIELDS_CATALOG IS REQUESTED.
    Sort it_Struc by FIELD_NAME.
    LOOP AT IT_FIELDS_CATALOG ASSIGNING <IT_FLDCTLG>.
      TRANSLATE <IT_FLDCTLG>-FIELD_NAME TO UPPER CASE.
      If <IT_FLDCTLG>-COL_POS <> 0.
        Read table it_Struc assigning <IT_STRUC>
          with key FIELD_NAME = <IT_FLDCTLG>-FIELD_NAME
          binary search.
        If sy-subrc = 0.
          <IT_STRUC>-COL_POS = <IT_FLDCTLG>-COL_POS.
        EndIf.
      EndIf.
    ENDLOOP.
    SORT IT_FIELDS_CATALOG BY FIELD_NAME.

    Sort it_Struc by COL_POS.

    LOOP AT IT_STRUC ASSIGNING <IT_STRUC>.
      READ TABLE IT_FIELDS_CATALOG ASSIGNING <IT_FLDCTLG>
        WITH KEY FIELD_NAME = <IT_STRUC>-FIELD_NAME
        BINARY SEARCH.
      IF SY-SUBRC = 0.
        IF NOT <IT_FLDCTLG>-FIND_TEXT IS INITIAL.
          <IT_STRUC>-FIND_TEXT = <IT_FLDCTLG>-FIND_TEXT.
        ENDIF.
        <IT_STRUC>-FIELD_HEADER = <IT_FLDCTLG>-FIELD_HEADER.
        <IT_STRUC>-NO_OUT  = <IT_FLDCTLG>-NO_OUT.
        <IT_STRUC>-NO_ZERO = <IT_FLDCTLG>-NO_ZERO.
        <it_struc>-No_ConvExt = <it_fldctlg>-No_ConvExt.
      ENDIF.
    ENDLOOP.
  ENDIF.

  IF NOT HEADER_NAME IS INITIAL.
    CLEAR W_IT_VALUES.
    W_IT_VALUES-VAR_NAME  = HEADER_NAME.

    IF VAL_TYPE IS INITIAL or VAL_TYPE = 'S'.
      LOOP AT IT_STRUC ASSIGNING <IT_STRUC>
        WHERE NO_OUT IS INITIAL.
        W_IT_VALUES-FIND_TEXT = <IT_STRUC>-FIND_TEXT.
        W_IT_VALUES-VAL_TYPE = VAL_TYPE.
        W_IT_VALUES-VALUE = <IT_STRUC>-FIELD_HEADER.
        APPEND W_IT_VALUES TO IT_VALUES.
      ENDLOOP.
    ELSE.
      LOOP AT IT_STRUC ASSIGNING <IT_STRUC>
        WHERE NO_OUT IS INITIAL.
        IF W_IT_VALUES-VALUE IS INITIAL. "sy-tabix = 1.
          W_IT_VALUES-FIND_TEXT = <IT_STRUC>-FIND_TEXT.
          W_IT_VALUES-VAL_TYPE = 'R'. "VAL_TYPE.
          W_IT_VALUES-VALUE = <IT_STRUC>-FIELD_HEADER.
        ELSE.
          CONCATENATE W_IT_VALUES-VALUE <IT_STRUC>-FIELD_HEADER
            INTO W_IT_VALUES-VALUE
            SEPARATED BY CHAR_TAB.
        ENDIF.
      ENDLOOP.
      APPEND W_IT_VALUES TO IT_VALUES.
    ENDIF.
  ENDIF.

  LINENUM = 0.
  LOOP AT IT_ANY_TABLE ASSIGNING <IT_LINE>.
    ANY_TABIX = SY-TABIX.
    IF NOT VAL_TYPE = 'T'.
      CLEAR W_IT_VALUES.
    ENDIF.
    W_IT_VALUES-VAR_NAME  = LINE_NAME.
*    w_IT_VALUES-VAR_NUM   = sy-tabix.
    LINENUM = LINENUM + 1.

*   #v### #####
    CLEAR <KEY>.
    LOOP AT IT_GROUPS_CATALOG ASSIGNING <IT_GRPCTLG>.
      AT NEW GROUP_NUM.
*        Clear <Key>.
        READ TABLE IT_OLD WITH KEY NUM = <IT_GRPCTLG>-GROUP_NUM.
        ASSIGN IT_OLD-KEY->* TO <OLDKEY>.
        ASSIGN IT_OLD-SUM->* TO <OLDSUM>.
        <SUM> = <OLDSUM>.
      ENDAT.

      ASSIGN COMPONENT <IT_GRPCTLG>-FIELD_NAME
        OF STRUCTURE <IT_LINE> TO <FLD>.
      IF <IT_GRPCTLG>-DO_SUM IS INITIAL.
        ASSIGN COMPONENT <IT_GRPCTLG>-FIELD_NAME
          OF STRUCTURE <KEY> TO <KEYFLD>.
        <KEYFLD> = <FLD>.
      ENDIF.

      AT END OF GROUP_NUM.
        IF <OLDKEY> <> <KEY> OR ANY_TABIX = 1.

******      #v### ####v# #####v
          IF ANY_TABIX <> 1.
            PERFORM WRITEGROUPLINE
              TABLES IT_STRUC
                     IT_GROUPS_CATALOG
                     IT_VALUES
              USING <OLDKEY>
                    <OLDSUM>
                    LINE_NAME
                    IT_OLD-VAR_NUM
                    VAL_TYPE
                    <IT_GRPCTLG>-GROUP_NUM.
          ENDIF.
******      ##### #v#### ####v# #####v
          <OLDKEY> = <KEY>.
          <OLDSUM> = <SUM>.
          IT_OLD-VAR_NUM = LINENUM.
          MODIFY TABLE IT_OLD.
          LINENUM = LINENUM + 1.
        ENDIF.
      ENDAT.

      IF NOT <IT_GRPCTLG>-DO_SUM IS INITIAL.
        ASSIGN COMPONENT <IT_GRPCTLG>-FIELD_NAME
          OF STRUCTURE <SUM> TO <SUMFLD>.
        <SUMFLD> = <SUMFLD> + <FLD>.
      ENDIF.
    ENDLOOP.

*   #v### ######## ######
    DATA: ISFIRSTLINE_STRUC.
    ISFIRSTLINE_STRUC = 'X'.
    LOOP AT IT_STRUC ASSIGNING <IT_STRUC>
      WHERE NO_OUT IS INITIAL.
      ASSIGN COMPONENT <IT_STRUC>-tabix OF STRUCTURE <IT_LINE> TO <FLD>.
      CHECK SY-SUBRC = 0.

      Clear ValueStr.
      If <IT_STRUC>-NO_ZERO is initial or
         not <FLD> is initial.

        Perform ValueToText
          using <FLD> ValueStr <IT_STRUC>."-TYPE.
      EndIf.

      IF VAL_TYPE IS INITIAL.
        W_IT_VALUES-FIND_TEXT = <IT_STRUC>-FIND_TEXT.
        W_IT_VALUES-VAL_TYPE = VAL_TYPE.
        W_IT_VALUES-VALUE = VALUESTR.
        W_IT_VALUES-VAR_NUM = LINENUM.
        APPEND W_IT_VALUES TO IT_VALUES.
      ELSE.
        IF ISFIRSTLINE_STRUC = 'X'.
          W_IT_VALUES-FIND_TEXT = <IT_STRUC>-FIND_TEXT.
          W_IT_VALUES-VAL_TYPE = VAL_TYPE.
          IF VAL_TYPE = 'T' AND ANY_TABIX <> 1.
            CONCATENATE W_IT_VALUES-VALUE VALUESTR
              INTO W_IT_VALUES-VALUE.
          ELSE.
            W_IT_VALUES-VAR_NUM = LINENUM.
            W_IT_VALUES-VALUE = VALUESTR.
          ENDIF.
        ELSE.
          CONCATENATE W_IT_VALUES-VALUE VALUESTR
            INTO W_IT_VALUES-VALUE
            SEPARATED BY CHAR_TAB.
        ENDIF.
      ENDIF.
      CLEAR: ISFIRSTLINE_STRUC.
    ENDLOOP.
    SET COUNTRY SPACE.

    IF NOT VAL_TYPE IS INITIAL.
      IF VAL_TYPE = 'T'.
        CONCATENATE W_IT_VALUES-VALUE CHAR_0D CHAR_0A
              INTO W_IT_VALUES-VALUE.
      ELSE.
        W_IT_VALUES-VAL_TYPE = VAL_TYPE.
        W_IT_VALUES-VAR_NUM = LINENUM.
        APPEND W_IT_VALUES TO IT_VALUES.
      ENDIF.
    ENDIF.
  ENDLOOP.
  IF VAL_TYPE = 'T'.
    W_IT_VALUES-VAL_TYPE = VAL_TYPE.
    APPEND W_IT_VALUES TO IT_VALUES.
  ENDIF.

ENDFUNCTION.
