Make your own free website on Tripod.com

Table of Contents:



REPORT zf_profitability_per_contract NO STANDARD PAGE HEADING
       MESSAGE-ID zz LINE-SIZE 250 LINE-COUNT 65.
*@----------------------------------------------------------------------
*@ Program id    : ZF_PROFITABILITY_PER_CONTRACT
*@ Program Desc  : Profitability Per Contract
*@ Transaction Code : ZDCM
*@ Input files   :
*@
*@ Output files  :
*@
*@ Tables Updated: Table - none
*@
*@ Author        : Becky Wang, LABK933883
*@ Date          : October 25, 2002
*@ Requested by  : Christina Tam
*@ Description   : Profitability Revenue or Direct Cost.  Can be
*@                 expanded to detailed GL account balance, and then
*@                 detailed FI document for each GL account, then
*@                 go to FB03 directly
*@----------------------------------------------------------------------
*@ Algorithm:
*@----------------------------------------------------------------------

INCLUDE zsmaster.
INCLUDE zsmacros.
INCLUDE .

*&-------------------
*& Tables
*&-------------------
TABLES: ce1anoc,
        bseg,
        t001.

*&-------------------
*& Types
*&-------------------

* type for detailed internal table
TYPES: BEGIN OF t_result,
         rev_cost_flg TYPE c,           "Revenue or Cost flag
         rev_cost_id LIKE zfrev_cost_defin-id,  "Revenue or Cost ID
         hkont LIKE bseg-hkont,         "GL account
         bukrs LIKE ce1anoc-bukrs,      "company
         rkaufnr LIKE ce1anoc-rkaufnr,      "Sales order number
         belnr LIKE bseg-belnr,         "document number
         buzei LIKE bseg-buzei,         "document Line number
         dmbtr LIKE bseg-dmbtr,         "amount in Local Currency
         pswsl LIKE bseg-pswsl,         "local currency
         dmbe2 LIKE bseg-dmbe2,         "amount in USD
         kvgr1 LIKE ce1anoc-kvgr1,      "job type
         kvgr2 LIKE ce1anoc-kvgr2,      "product line
         land1 LIKE ce1anoc-land1,      "country
         kndnr LIKE ce1anoc-kndnr,      "Customer number
         wwctr LIKE ce1anoc-wwctr,      "Contract number
         zuonr LIKE bseg-zuonr,         "assignment
         budat LIKE bkpf-budat,         "posting date
         usnam LIKE bkpf-usnam,         "posted by
         insp_geop_dmbtr LIKE bseg-dmbtr,
         insp_mfl_dmbtr LIKE bseg-dmbtr,
         insp_both_dmbtr LIKE bseg-dmbtr,
         intg_rep_dmbtr LIKE bseg-dmbtr,
         intg_intg_dmbtr LIKE bseg-dmbtr,
         insp_intg_geop_dmbtr LIKE bseg-dmbtr,
         insp_intg_mfl_dmbtr LIKE bseg-dmbtr,
         insp_intg_rep_dmbtr LIKE bseg-dmbtr,
         insp_intg_intg_dmbtr LIKE bseg-dmbtr,
         insp_intg_both_dmbtr LIKE bseg-dmbtr,
         insp_geop_dmbe2 LIKE bseg-dmbe2,
         insp_mfl_dmbe2 LIKE bseg-dmbe2,
         insp_both_dmbe2 LIKE bseg-dmbe2,
         intg_rep_dmbe2 LIKE bseg-dmbe2,
         intg_intg_dmbe2 LIKE bseg-dmbe2,
         insp_intg_geop_dmbe2 LIKE bseg-dmbe2,
         insp_intg_mfl_dmbe2 LIKE bseg-dmbe2,
         insp_intg_rep_dmbe2 LIKE bseg-dmbe2,
         insp_intg_intg_dmbe2 LIKE bseg-dmbe2,
         insp_intg_both_dmbe2 LIKE bseg-dmbe2,
         state TYPE c,
       END OF t_result.

* type for the summary table
TYPES: BEGIN OF t_summary,
         rev_cost_flg TYPE c,           "Revenue or Cost flag
         rev_cost_id LIKE zfrev_cost_defin-id,  "Revenue or Cost ID
         hkont LIKE bseg-hkont,         "GL account
         bukrs LIKE ce1anoc-bukrs,      "company
         insp_geop_dmbtr LIKE bseg-dmbtr,
         insp_mfl_dmbtr LIKE bseg-dmbtr,
         insp_both_dmbtr LIKE bseg-dmbtr,
         intg_rep_dmbtr LIKE bseg-dmbtr,
         intg_intg_dmbtr LIKE bseg-dmbtr,
         insp_intg_geop_dmbtr LIKE bseg-dmbtr,
         insp_intg_mfl_dmbtr LIKE bseg-dmbtr,
         insp_intg_rep_dmbtr LIKE bseg-dmbtr,
         insp_intg_intg_dmbtr LIKE bseg-dmbtr,
         insp_intg_both_dmbtr LIKE bseg-dmbtr,
         insp_geop_dmbtr_per LIKE bseg-dmbtr,
         insp_mfl_dmbtr_per LIKE bseg-dmbtr,
         insp_both_dmbtr_per LIKE bseg-dmbtr,
         intg_rep_dmbtr_per LIKE bseg-dmbtr,
         intg_intg_dmbtr_per LIKE bseg-dmbtr,
         insp_intg_geop_dmbtr_per LIKE bseg-dmbtr,
         insp_intg_mfl_dmbtr_per LIKE bseg-dmbtr,
         insp_intg_rep_dmbtr_per LIKE bseg-dmbtr,
         insp_intg_intg_dmbtr_per LIKE bseg-dmbtr,
         insp_intg_both_dmbtr_per LIKE bseg-dmbtr,
         insp_geop_dmbe2 LIKE bseg-dmbe2,
         insp_mfl_dmbe2 LIKE bseg-dmbe2,
         insp_both_dmbe2 LIKE bseg-dmbe2,
         intg_rep_dmbe2 LIKE bseg-dmbe2,
         intg_intg_dmbe2 LIKE bseg-dmbe2,
         insp_intg_geop_dmbe2 LIKE bseg-dmbe2,
         insp_intg_mfl_dmbe2 LIKE bseg-dmbe2,
         insp_intg_rep_dmbe2 LIKE bseg-dmbe2,
         insp_intg_intg_dmbe2 LIKE bseg-dmbe2,
         insp_intg_both_dmbe2 LIKE bseg-dmbe2,
         total_dmbe2 LIKE bseg-dmbe2,
         total_dmbtr LIKE bseg-dmbtr,
         insp_geop_dmbe2_per LIKE bseg-dmbe2,
         insp_mfl_dmbe2_per LIKE bseg-dmbe2,
         insp_both_dmbe2_per LIKE bseg-dmbe2,
         intg_rep_dmbe2_per LIKE bseg-dmbe2,
         intg_intg_dmbe2_per LIKE bseg-dmbe2,
         insp_intg_geop_dmbe2_per LIKE bseg-dmbe2,
         insp_intg_mfl_dmbe2_per LIKE bseg-dmbe2,
         insp_intg_rep_dmbe2_per LIKE bseg-dmbe2,
         insp_intg_intg_dmbe2_per LIKE bseg-dmbe2,
         insp_intg_both_dmbe2_per LIKE bseg-dmbe2,
         total_dmbe2_per LIKE bseg-dmbe2,
         total_dmbtr_per LIKE bseg-dmbtr,
         state TYPE c,
       END OF t_summary.

* type for the download internal table
TYPES: BEGIN OF t_down,
         bukrs LIKE ce1anoc-bukrs,      "company
         belnr LIKE bseg-belnr,         "document number
         buzei LIKE bseg-buzei,         "document Line number
         hkont LIKE bseg-hkont,         "GL account
         rkaufnr LIKE ce1anoc-rkaufnr,      "Sales order number
         kvgr2 LIKE ce1anoc-kvgr2,      "product line
         kvgr1 LIKE ce1anoc-kvgr1,      "job type
         land1 LIKE ce1anoc-land1,      "country
         kndnr LIKE ce1anoc-kndnr,      "Customer number
         wwctr LIKE ce1anoc-wwctr,      "Contract number
         rev_cost_id LIKE zfrev_cost_defin-id,  "Revenue or Cost ID
         dmbe2 LIKE bseg-dmbe2,         "amount in USD
         dmbtr LIKE bseg-dmbtr,         "amount in Local Currency
      END OF t_down.

*&-------------------
*& Global Data
*&-------------------
DATA: it_anoc LIKE ce1anoc OCCURS 0 WITH HEADER LINE,
      wa_bseg LIKE bseg,
      wa_bkpf LIKE bkpf,
      wa_rev_cost_defin LIKE zfrev_cost_defin,
      wa_rev_cost_descp LIKE zfrev_cost_descp,
      it_result TYPE t_result OCCURS 0 WITH HEADER LINE,
      it_doc TYPE t_result OCCURS 0 WITH HEADER LINE,
      it_summary TYPE t_summary OCCURS 0 WITH HEADER LINE,
      wa_summary TYPE t_summary,
      it_company LIKE t001 OCCURS 0 WITH HEADER LINE,
      it_temp_comp LIKE t001 OCCURS 0 WITH HEADER LINE,
      it_download TYPE t_down OCCURS 0 WITH HEADER LINE.

DATA: g_year LIKE bseg-gjahr,
      g_second_line(150) TYPE c,
      g_header(100) TYPE c,
      g_from_period(2) TYPE c,
      g_to_period(2) TYPE c,
      g_from_year(4) TYPE c,
      g_to_year(4) TYPE c,
      g_bukrs LIKE ce1anoc-bukrs,
      g_currency LIKE t001-waers,
      g_report(4) TYPE c,
      g_gl LIKE bseg-hkont.

DATA: g_fis_period TYPE mac_t_period,
      g_fis_year TYPE mac_t_year,
      g_fiscal LIKE ce1anoc-perio.
*&-------------------
*& Constants
*&-------------------
CONSTANTS: c_inspection LIKE ce1anoc-kvgr2 VALUE 'Z01',
           c_integrity LIKE ce1anoc-kvgr2 VALUE 'Z02',
           c_insp_intg LIKE ce1anoc-kvgr2 VALUE 'Z3'.

CONSTANTS: c_geopig LIKE ce1anoc-kvgr1 VALUE 'Z01',
           c_mfl LIKE ce1anoc-kvgr1 VALUE 'Z02',
           c_geop_mfl LIKE ce1anoc-kvgr1 VALUE 'Z03',
           c_integ LIKE ce1anoc-kvgr1 VALUE 'Z04',
           c_repair LIKE ce1anoc-kvgr1 VALUE 'Z05'.

CONSTANTS: c_country(4) TYPE c VALUE 'CNTY',
           c_customer(4) TYPE c VALUE 'CUST',
           c_9090 TYPE mac_t_bukrs VALUE '9090'.

CONSTANTS: c_close TYPE c VALUE 'C',
           c_open TYPE c VALUE 'O',
           c_nwsl LIKE skat-ktopl VALUE 'NWSL'.
*&-------------------
*& Ranges
*&-------------------
RANGES: r_prod_line FOR ce1anoc-kvgr2.

*&-------------------
*& Macros
*&-------------------
DEFINE mac_print_group1.
  write: /1 sy-vline no-gap,
          2(38) &1.
END-OF-DEFINITION.

DEFINE mac_print_group2.
  write: /1 sy-vline no-gap,
          3(38) &1.
END-OF-DEFINITION.

DEFINE mac_print_group3.
  write: /1 sy-vline no-gap,
          4(38) &1.
END-OF-DEFINITION.

DEFINE mac_print_group4.
  write: /1 sy-vline no-gap,
          5(38) &1.
END-OF-DEFINITION.

* for closed folder
DEFINE mac_print_group5.
  write: /1 sy-vline no-gap,
          4 icon_positive as icon hotspot no-gap,
          8(38) &1.
END-OF-DEFINITION.

* for openned folder
DEFINE mac_print_group6.
  write: /1 sy-vline no-gap,
          4 icon_negative as icon hotspot no-gap,
          8(38) &1.
END-OF-DEFINITION.

DEFINE mac_print_group7.
  write: /1 sy-vline no-gap,
          6 icon_positive as icon hotspot no-gap,
          10(30) &1.
END-OF-DEFINITION.

DEFINE mac_print_group8.
  write: /1 sy-vline no-gap,
          6 icon_negative as icon hotspot no-gap,
          10(30) &1.
END-OF-DEFINITION.

DEFINE mac_print_group9.
  write: /1 sy-vline no-gap,
          12(28) &1 hotspot.
END-OF-DEFINITION.

DEFINE mac_print_header.
  write: /1 sy-vline no-gap,
          40 sy-vline no-gap,
          41(56) &1 centered,                    "Pipeline Inspection
          97 sy-vline no-gap,
          98(37) &2 centered,                    "Pipeline Integrity
          135 sy-vline no-gap,
          136(94) &3 centered,                   "Inspection & integrity
          230 sy-vline no-gap,
          231(19) &4 centered,                   "Totals
          250 sy-vline.
END-OF-DEFINITION.

DEFINE mac_print_1.
  write:  40 sy-vline no-gap,
          41(11)   &1 right-justified decimals 0,      "geopig
          52 sy-vline no-gap,
          53(6)    &2 right-justified decimals 1,      "% of revenue
          59 sy-vline no-gap,
          60(11)   &3 right-justified decimals 0,      "MFL
          71 sy-vline no-gap,
          72(6)    &4 right-justified decimals 1,      "%
          78 sy-vline no-gap,
          79(11)   &5 right-justified decimals 0,      "geopig & MFL
          90 sy-vline no-gap,
          91(6)    &6 right-justified decimals 1,      "%
          97 sy-vline no-gap,
          98(11)   &7 right-justified decimals 0,      "repair work
          109 sy-vline no-gap,
          110(6)   &8 right-justified decimals 1,      "%
          116 sy-vline.
END-OF-DEFINITION.

DEFINE mac_print_2.
  write:  117(11)   &1 right-justified decimals 0,      "Integrity
          128  sy-vline no-gap,
          129(6)    &2 right-justified decimals 1,      "%
          135  sy-vline no-gap,
          136(11)   &3 right-justified decimals 0,      "geopig
          147 sy-vline no-gap,
          148(6)    &4 right-justified decimals 1,      "% of revenue
          154 sy-vline no-gap,
          155(11)   &5 right-justified decimals 0,      "MFL
          166 sy-vline no-gap,
          167(6)    &6 right-justified decimals 1,      "%
          173 sy-vline no-gap,
          174(11)   &7 right-justified decimals 0,      "geopig & MFL
          185 sy-vline no-gap,
          186(6)    &8 right-justified decimals 1,      "%0
          192 sy-vline.
END-OF-DEFINITION.

DEFINE mac_print_3.
  write:  193(11)   &1 right-justified decimals 0,      "Repair
          204 sy-vline no-gap,
          205(6)    &2 right-justified decimals 1,      "%
          211 sy-vline no-gap,
          212(11)   &3 right-justified decimals 0,      "Integrity
          223 sy-vline no-gap,
          224(6)    &4 right-justified decimals 1,      "%
          230 sy-vline no-gap,
          231(12)   &5 right-justified decimals 0,      "Totals
          243 sy-vline no-gap,
          244(6)    &6 right-justified decimals 1,      "%
          250 sy-vline no-gap.
END-OF-DEFINITION.

DEFINE mac_print_vline.
  write:  /1 sy-vline no-gap,
          40 sy-vline no-gap,
          52 sy-vline no-gap,
          59 sy-vline no-gap,
          71 sy-vline no-gap,
          78 sy-vline no-gap,
          90 sy-vline no-gap,
          97 sy-vline no-gap,
          109 sy-vline no-gap,
          116 sy-vline no-gap,
          128  sy-vline no-gap,
          135  sy-vline no-gap,
          147 sy-vline no-gap,
          154 sy-vline no-gap,
          166 sy-vline no-gap,
          173 sy-vline no-gap,
          185 sy-vline no-gap,
          192 sy-vline no-gap,
          204 sy-vline no-gap,
          211 sy-vline no-gap,
          223 sy-vline no-gap,
          230 sy-vline no-gap,
          243 sy-vline no-gap,
          250 sy-vline no-gap.
END-OF-DEFINITION.

*&-------------------
*& Selection Screen
*&-------------------
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_bukrs FOR ce1anoc-bukrs DEFAULT '9090',
                s_perio FOR ce1anoc-perio NO-EXTENSION,
                s_wwctr FOR ce1anoc-wwctr,
                s_land1 FOR ce1anoc-land1,
                s_kaufn FOR ce1anoc-rkaufnr,
                s_kndnr FOR ce1anoc-kndnr.
SELECTION-SCREEN SKIP 1.
PARAMETER: p_zero AS CHECKBOX DEFAULT ''.
* for debug use, When it is on, detailed data will be download to Excel
PARAMETER: p_debug TYPE c NO-DISPLAY DEFAULT ''.
SELECTION-SCREEN END OF BLOCK blk1.


************************ START OF MAIN PROGRAM *************************
*&------------------------
*& At selection-screen
*&------------------------

INITIALIZATION.

* initialize the selection screen
  mac_get_period_for_date c_9090 sy-datum g_fis_year g_fis_period.
  g_fiscal+0(4) = g_fis_year.
  g_fiscal+4(3) = g_fis_period.
  s_perio-low = g_fiscal.
  s_perio-high = g_fiscal.
  s_perio-sign = 'I'.
  s_perio-option = 'BT'.
  APPEND s_perio.


*&------------------------
*& at selection-screen
*&------------------------

AT SELECTION-SCREEN.
  CLEAR: it_company, it_temp_comp.
  REFRESH: it_company, it_temp_comp.
* validation
* check to make sure all of the companies selected have the
* same local currency
  IF NOT s_bukrs IS INITIAL.
    SELECT * FROM t001 INTO TABLE it_company
      WHERE bukrs IN s_bukrs.
    it_temp_comp[] = it_company[].

    LOOP AT it_company.
      LOOP AT it_temp_comp.
        IF it_temp_comp-waers <> it_company-waers.
          MESSAGE e001(zz) WITH 'Can not proceed.'
              'Companies selected do not have same Local Currency'.
        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
*&------------------------
*& Start of Selection
*&------------------------

START-OF-SELECTION.

  SET PF-STATUS 'STATUS1'.

* Put product line code for Inspection, Integrity, Inspection &
* Integrity into a selection range
  PERFORM get_product_line_range.

* Create the report title
  g_from_period = s_perio-low+5(2).
  g_from_year = s_perio-low+0(4).

  CONCATENATE 'Period' g_from_period 'FY' g_from_year INTO
    g_second_line SEPARATED BY space.

  IF NOT s_perio-high IS INITIAL.
    g_to_period = s_perio-high+5(2).
    g_to_year = s_perio-high+0(4).
  CONCATENATE g_second_line 'To Period' g_to_period 'FY' g_to_year INTO
                                       g_second_line SEPARATED BY space.
  ENDIF.

  g_header =
    'BJ PIPELINE INSPECTION SERVICE - PROFITABILITY PER CONTRACT'.

* select records
  SELECT * FROM ce1anoc INTO TABLE it_anoc
    WHERE bukrs IN s_bukrs
    AND perio IN s_perio
    AND wwctr IN s_wwctr
    AND land1 IN s_land1
    AND rkaufnr IN s_kaufn
    AND kndnr IN s_kndnr
    AND kvgr2 IN r_prod_line.

  IF sy-subrc <> 0.
    WRITE: / 'No records found!'.
  ELSE.
    LOOP AT it_anoc.
      CLEAR: wa_bseg, g_year, wa_bkpf.
      g_year = it_anoc-perio+0(4).

      SELECT SINGLE * FROM bkpf INTO wa_bkpf
        WHERE bukrs = it_anoc-bukrs
        AND gjahr = g_year
        AND belnr = it_anoc-wwref.

*     for each selected records in ce1anoc, find the corresponding
*     record in bseg
      SELECT SINGLE * FROM bseg INTO wa_bseg
        WHERE bukrs = it_anoc-bukrs
        AND gjahr = g_year
        AND belnr = it_anoc-wwref
        AND buzei = it_anoc-wwitm.

      CHECK sy-subrc = 0.

*     determine credit or debit
      IF wa_bseg-shkzg = 'H'.
        wa_bseg-dmbtr = 0 - wa_bseg-dmbtr.
        wa_bseg-dmbe2 = 0 - wa_bseg-dmbe2.
      ENDIF.

*     get Revenue or Cost definition using the new table
      CLEAR: wa_rev_cost_defin.
      SELECT SINGLE * FROM zfrev_cost_defin
        INTO wa_rev_cost_defin
        WHERE options = 'EQ'
        AND acct_low = wa_bseg-hkont.
      IF sy-subrc <> 0.
        CLEAR: wa_rev_cost_defin.
        SELECT SINGLE * FROM zfrev_cost_defin
          INTO wa_rev_cost_defin
          WHERE options = 'BT'
          AND acct_low <= wa_bseg-hkont
          AND acct_high >= wa_bseg-hkont.
      ENDIF.

*     If the GL account number is found in the Revenue &
*     Cost definition table, store this record, otherwise
*     ignore it.
      IF sy-subrc = 0.
        it_result-bukrs = it_anoc-bukrs.         "company
        it_result-land1 = it_anoc-land1.         "country
        it_result-kndnr = it_anoc-kndnr.         "customer
        it_result-wwctr = it_anoc-wwctr.         "contract
        it_result-rkaufnr = it_anoc-rkaufnr.     "order
        it_result-belnr = wa_bseg-belnr.         "document number
        it_result-buzei = wa_bseg-buzei.         "document Line number
        it_result-zuonr = wa_bseg-zuonr.         "assignment
        it_result-budat = wa_bkpf-budat.         "posting date
        it_result-usnam = wa_bkpf-usnam.         "posted by
        it_result-hkont = wa_bseg-hkont.         "GL account
        it_result-dmbtr = wa_bseg-dmbtr.         "amount in Local Cur
        it_result-pswsl = wa_bseg-pswsl.         "Local currency
        it_result-dmbe2 = wa_bseg-dmbe2.         "amount in USD
        it_result-kvgr1 = it_anoc-kvgr1.         "job type
        it_result-kvgr2 = it_anoc-kvgr2.         "product line
        it_result-rev_cost_id = wa_rev_cost_defin-id.
        it_result-rev_cost_flg = wa_rev_cost_defin-id+0(1).
        it_result-state = c_close.
        APPEND it_result.

*       store these records in the download table
        MOVE-CORRESPONDING it_result TO it_download.
        IF it_result-rev_cost_flg = 'R'.
          it_download-dmbtr = 0 - it_download-dmbtr.
          it_download-dmbe2 = 0 - it_download-dmbe2.
        ENDIF.
        APPEND it_download.
      ENDIF.
    ENDLOOP.
  ENDIF.

* get all of the GL accounts for all of the Revenue and Direct Cost
* even if they don't have balance
  PERFORM get_all_gl_acct.

* put the amount into corresponding field of the internal table
* according to if it is revenue or cost, and it's job type
  PERFORM arrange_data.

* calculate summary for each revenue and cost and create the summary
* table
  PERFORM calculate_summary.

* write the report using default currency USD
  PERFORM write_report USING 'USD'.

* if debug is on, download the detailed records to Excel
  IF NOT p_debug IS INITIAL.
    PERFORM download_excel.
  ENDIF.

*&------------------------
*& At User Command
*&------------------------
AT USER-COMMAND.
  CASE sy-ucomm.
    WHEN 'LOCL'.
*     user want to display report in Local Currency
      SET PF-STATUS 'STATUS2'.
      PERFORM write_report USING 'LOCL'.
    WHEN 'USD'.
*     user want to display report in
      SET PF-STATUS 'STATUS1'.
      PERFORM write_report USING 'USD'.
    WHEN 'EXP'.
*     user want to expand all of the summarized Revenue and
*     direct cost.  Set the GUI according to the current
*     currency
      IF g_currency = 'USD'.
        SET PF-STATUS 'STATUS1'.
      ELSE.
        SET PF-STATUS 'STATUS2'.
      ENDIF.
      PERFORM expand_all.
      PERFORM write_report USING g_currency.
    WHEN 'COLP'.
*     user want to collapse all of the summarized Revenue and
*     direct cost.  Set the GUI according to the current
*     currency
      IF g_currency = 'USD'.
        SET PF-STATUS 'STATUS1'.
      ELSE.
        SET PF-STATUS 'STATUS2'.
      ENDIF.
      PERFORM collapse_all.
      PERFORM write_report USING g_currency.
  ENDCASE.

*&------------------------
*& At Line Selection
*&------------------------
AT LINE-SELECTION.
  sy-lsind = 0.
  CLEAR: wa_summary.

  IF NOT it_summary IS INITIAL.
*   when user clicked on a summary line, close or open the detailed
*   GL account line according to current stage
    wa_summary-state = it_summary-state.
    wa_summary-rev_cost_flg = it_summary-rev_cost_flg.
    wa_summary-rev_cost_id = it_summary-rev_cost_id.
    CLEAR: it_summary.

*   if current state is close, change it to open in order to open the
*   folder.  if current state is open, change it to close in order to
*   close the folder.
    IF wa_summary-state = c_close.
      wa_summary-state = c_open.
    ELSE.
      wa_summary-state = c_close.
    ENDIF.

*   modify the summary internal table.
    LOOP AT it_summary WHERE rev_cost_flg = wa_summary-rev_cost_flg
                         AND rev_cost_id = wa_summary-rev_cost_id.
      it_summary-state = wa_summary-state.
      MODIFY it_summary.
    ENDLOOP.
*   display the report
    PERFORM write_report USING g_currency.
  ELSEIF NOT it_doc IS INITIAL.
*   if clicked on document level, go to FB03 with the document number
*   and company code
    SET PARAMETER ID 'BLN' FIELD it_doc-belnr.
    SET PARAMETER ID 'BUK' FIELD it_doc-bukrs.
    CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
  ELSE.
*   if clicked on GL account level, close or open the detailed
*   FI documents belong to this GL account according to current stage
    LOOP AT it_result WHERE hkont = g_gl.

*   if current state is close, change it to open in order to open the
*   folder.  if current state is open, change it to close in order to
*   close the folder.
      IF it_result-state = c_close.
        it_result-state = c_open.
      ELSE.
        it_result-state = c_close.
      ENDIF.
      MODIFY it_result.
    ENDLOOP.
*   display the report
    PERFORM write_report USING g_currency.
  ENDIF.

*&------------------------
*& Top of Page
*&------------------------
TOP-OF-PAGE.
* Write report header and table header
  mac_generic_report_header g_header g_second_line
                            mac_c_true  mac_c_true .
  WRITE: / 'Display Currency: ' COLOR COL_POSITIVE,
           g_currency COLOR COL_POSITIVE.

  PERFORM write_header.

*&----------------------------------
*& Top of Page During Line selection
*&----------------------------------
TOP-OF-PAGE DURING LINE-SELECTION.
* Write report header and table header
  mac_generic_report_header g_header g_second_line
                            mac_c_true  mac_c_true .
  WRITE: / 'Display Currency: ' COLOR COL_POSITIVE,
           g_currency COLOR COL_POSITIVE.

  PERFORM write_header.


*&---------------------------------------------------------------------*
*&      Form  WRITE_HEADER
*&---------------------------------------------------------------------*
*&  write table column header
*&---------------------------------------------------------------------*
FORM write_header.

  FORMAT COLOR COL_HEADING.
  ULINE.
  mac_print_header 'Pipeline Inspection'
                   'Pipeline Integrity'
                   'Inspection and Integrity Combined'
                   'Totals'.
  ULINE.
  FORMAT RESET.
  FORMAT COLOR COL_HEADING INTENSIFIED OFF.
  mac_print_group1 ''.
  mac_print_1 '' '' '' '' 'geopig' '' '' ''.
  mac_print_2 'Repair' '' '' '' '' '' 'geopig' ''.
  mac_print_3 '' '' 'Repair' '' '' ''.

  mac_print_group1 ''.
  mac_print_1 'geopig' '%' 'MFL' '%' '& MFL' '%' 'Integrity' '%'.
  mac_print_2 'Work' '%' 'geopig' '%' 'MFL' '%' '&MFL' '%'.
  mac_print_3 'Integrity' '%' 'Work' '%' 'Amount' '%'.
  ULINE.
  FORMAT RESET.
  FORMAT INTENSIFIED OFF.
ENDFORM.                    " WRITE_HEADER

*&---------------------------------------------------------------------*
*&      Form  Calculate_Summary
*&---------------------------------------------------------------------*
*& Calculate Summary
*&---------------------------------------------------------------------*
FORM calculate_summary.

  DATA: l_totals_dmbe2 LIKE bseg-dmbe2,
        l_totals_dmbtr LIKE bseg-dmbtr,
        l_total_rev LIKE bseg-dmbe2,
        l_temp LIKE bseg-dmbe2,
        l_comp LIKE bseg-bukrs.

  DATA: wa_result LIKE it_result.

* sort the internal table
  SORT it_result BY rev_cost_flg DESCENDING
                    rev_cost_id ASCENDING.

  LOOP AT it_result.
    l_comp = it_result-bukrs.

    AT END OF rev_cost_id.
*     get total revenue for each job type for this revenue or
*     direct cost
      CLEAR: wa_result.
      PERFORM get_total_revenue USING it_result-bukrs
                                CHANGING wa_result.

      CLEAR: l_totals_dmbe2, l_totals_dmbtr.
      SUM.
*     calculate the total for this revenue or direct cost
      l_totals_dmbe2 =  it_result-insp_geop_dmbe2 +
                        it_result-insp_mfl_dmbe2 +
                        it_result-insp_both_dmbe2 +
                        it_result-intg_rep_dmbe2 +
                        it_result-intg_intg_dmbe2 +
                        it_result-insp_intg_geop_dmbe2 +
                        it_result-insp_intg_mfl_dmbe2 +
                        it_result-insp_intg_rep_dmbe2 +
                        it_result-insp_intg_intg_dmbe2 +
                        it_result-insp_intg_both_dmbe2.

      l_totals_dmbtr =  it_result-insp_geop_dmbtr +
                        it_result-insp_mfl_dmbtr +
                        it_result-insp_both_dmbtr +
                        it_result-intg_intg_dmbtr +
                        it_result-intg_rep_dmbtr +
                        it_result-insp_intg_geop_dmbtr +
                        it_result-insp_intg_mfl_dmbtr +
                        it_result-insp_intg_intg_dmbtr +
                        it_result-insp_intg_rep_dmbtr +
                        it_result-insp_intg_both_dmbtr.

      CLEAR: it_summary.
      MOVE-CORRESPONDING it_result TO it_summary.
      it_summary-total_dmbe2 = l_totals_dmbe2.
      it_summary-total_dmbtr = l_totals_dmbtr.
      it_summary-bukrs = l_comp.
      it_summary-state = c_close.

*     calculate percentage for each job type in both USD and Local
*     Currency
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_geop_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_geop_dmbe2_per = it_summary-insp_geop_dmbe2 /
                                     wa_result-insp_geop_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_mfl_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_mfl_dmbe2_per = it_summary-insp_mfl_dmbe2 /
                         wa_result-insp_mfl_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_both_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_both_dmbe2_per = it_summary-insp_both_dmbe2 /
                          wa_result-insp_both_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-intg_rep_dmbe2 ).
      IF l_temp > 1.
        it_summary-intg_rep_dmbe2_per = it_summary-intg_rep_dmbe2 /
                         wa_result-intg_rep_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-intg_intg_dmbe2 ).
      IF l_temp > 1.
        it_summary-intg_intg_dmbe2_per = it_summary-intg_intg_dmbe2 /
                         wa_result-intg_intg_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_geop_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_intg_geop_dmbe2_per =
                               it_summary-insp_intg_geop_dmbe2 /
                               wa_result-insp_intg_geop_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_mfl_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_intg_mfl_dmbe2_per =
                              it_summary-insp_intg_mfl_dmbe2 /
                              wa_result-insp_intg_mfl_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_rep_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_intg_rep_dmbe2_per =
                              it_summary-insp_intg_rep_dmbe2 /
                              wa_result-insp_intg_rep_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_intg_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_intg_intg_dmbe2_per =
                              it_summary-insp_intg_intg_dmbe2 /
                              wa_result-insp_intg_intg_dmbe2 * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_both_dmbe2 ).
      IF l_temp > 1.
        it_summary-insp_intg_both_dmbe2_per =
                               it_summary-insp_intg_both_dmbe2 /
                               wa_result-insp_intg_both_dmbe2 * 100.
      ENDIF.

      l_total_rev = wa_result-insp_geop_dmbe2 +
                    wa_result-insp_mfl_dmbe2 +
                    wa_result-insp_both_dmbe2 +
                    wa_result-intg_rep_dmbe2 +
                    wa_result-intg_intg_dmbe2 +
                    wa_result-insp_intg_geop_dmbe2 +
                    wa_result-insp_intg_mfl_dmbe2 +
                    wa_result-insp_intg_rep_dmbe2 +
                    wa_result-insp_intg_intg_dmbe2 +
                    wa_result-insp_intg_both_dmbe2.
      CLEAR: l_temp.
      l_temp = abs( l_total_rev ).
      IF l_temp > 1.
        it_summary-total_dmbe2_per = it_summary-total_dmbe2 /
                                 l_total_rev * 100.
      ENDIF.

      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_geop_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_geop_dmbtr_per = it_summary-insp_geop_dmbtr /
                                     wa_result-insp_geop_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_mfl_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_mfl_dmbtr_per = it_summary-insp_mfl_dmbtr /
                         wa_result-insp_mfl_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_both_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_both_dmbtr_per = it_summary-insp_both_dmbtr /
                          wa_result-insp_both_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-intg_rep_dmbtr ).
      IF l_temp > 1.
        it_summary-intg_rep_dmbtr_per = it_summary-intg_rep_dmbtr /
                         wa_result-intg_rep_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-intg_intg_dmbtr ).
      IF l_temp > 1.
        it_summary-intg_intg_dmbtr_per = it_summary-intg_intg_dmbtr /
                         wa_result-intg_intg_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_geop_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_intg_geop_dmbtr_per =
                               it_summary-insp_intg_geop_dmbtr /
                               wa_result-insp_intg_geop_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_mfl_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_intg_mfl_dmbe2_per =
                              it_summary-insp_intg_mfl_dmbtr /
                              wa_result-insp_intg_mfl_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_rep_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_intg_rep_dmbe2_per =
                              it_summary-insp_intg_rep_dmbtr /
                              wa_result-insp_intg_rep_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_intg_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_intg_intg_dmbtr_per =
                              it_summary-insp_intg_intg_dmbtr /
                              wa_result-insp_intg_intg_dmbtr * 100.
      ENDIF.
      CLEAR: l_temp.
      l_temp = abs( wa_result-insp_intg_both_dmbtr ).
      IF l_temp > 1.
        it_summary-insp_intg_both_dmbtr_per =
                               it_summary-insp_intg_both_dmbtr /
                               wa_result-insp_intg_both_dmbtr * 100.
      ENDIF.

      l_total_rev = wa_result-insp_geop_dmbtr +
                    wa_result-insp_mfl_dmbtr +
                    wa_result-insp_both_dmbtr +
                    wa_result-intg_rep_dmbtr +
                    wa_result-intg_intg_dmbtr +
                    wa_result-insp_intg_geop_dmbtr +
                    wa_result-insp_intg_mfl_dmbtr +
                    wa_result-insp_intg_rep_dmbtr +
                    wa_result-insp_intg_intg_dmbtr +
                    wa_result-insp_intg_both_dmbtr.
      CLEAR: l_temp.
      l_temp = abs( l_total_rev ).
      IF l_temp > 1.
        it_summary-total_dmbtr_per = it_summary-total_dmbtr /
                                 l_total_rev * 100.
      ENDIF.

*     append to the summary table
      APPEND it_summary.
    ENDAT.
  ENDLOOP.
ENDFORM.                    " calculate_summary


*&---------------------------------------------------------------------*
*&      Form  WRITE_REPORT
*&---------------------------------------------------------------------*
*& Write report
*&---------------------------------------------------------------------*
FORM write_report USING p_ucomm.

  DATA: l_country LIKE t005t-landx,
        l_customer LIKE kna1-name1.

  DATA: l_tot_rev_insp_geop LIKE bseg-dmbe2,
        l_tot_rev_insp_mfl  LIKE bseg-dmbe2,
        l_tot_rev_insp_both LIKE bseg-dmbe2,
        l_tot_rev_intg_rep LIKE bseg-dmbe2,
        l_tot_rev_intg_intg LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_geop LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_mfl  LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_rep  LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_intg  LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_both LIKE bseg-dmbe2,
        l_tot_rev_totals LIKE bseg-dmbe2,

        l_tot_rev_insp_geop_per LIKE bseg-dmbe2,
        l_tot_rev_insp_mfl_per  LIKE bseg-dmbe2,
        l_tot_rev_insp_both_per LIKE bseg-dmbe2,
        l_tot_rev_intg_rep_per LIKE bseg-dmbe2,
        l_tot_rev_intg_intg_per LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_geop_per LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_mfl_per  LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_rep_per  LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_intg_per  LIKE bseg-dmbe2,
        l_tot_rev_insp_intg_both_per LIKE bseg-dmbe2,
        l_tot_rev_totals_per LIKE bseg-dmbe2.

  DATA: l_tot_cost_insp_geop LIKE bseg-dmbe2,
        l_tot_cost_insp_mfl  LIKE bseg-dmbe2,
        l_tot_cost_insp_both LIKE bseg-dmbe2,
        l_tot_cost_intg_rep LIKE bseg-dmbe2,
        l_tot_cost_intg_intg LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_geop LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_mfl  LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_rep  LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_intg  LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_both LIKE bseg-dmbe2,
        l_tot_cost_totals LIKE bseg-dmbe2,

        l_tot_cost_insp_geop_per LIKE bseg-dmbe2,
        l_tot_cost_insp_mfl_per  LIKE bseg-dmbe2,
        l_tot_cost_insp_both_per LIKE bseg-dmbe2,
        l_tot_cost_intg_rep_per LIKE bseg-dmbe2,
        l_tot_cost_intg_intg_per LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_geop_per LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_mfl_per  LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_rep_per  LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_intg_per  LIKE bseg-dmbe2,
        l_tot_cost_insp_intg_both_per LIKE bseg-dmbe2,
        l_tot_cost_totals_per LIKE bseg-dmbe2.

  DATA: l_diff_insp_geop LIKE bseg-dmbe2,
        l_diff_insp_mfl  LIKE bseg-dmbe2,
        l_diff_insp_both LIKE bseg-dmbe2,
        l_diff_intg_rep LIKE bseg-dmbe2,
        l_diff_intg_intg LIKE bseg-dmbe2,
        l_diff_insp_intg_geop LIKE bseg-dmbe2,
        l_diff_insp_intg_mfl  LIKE bseg-dmbe2,
        l_diff_insp_intg_rep  LIKE bseg-dmbe2,
        l_diff_insp_intg_intg  LIKE bseg-dmbe2,
        l_diff_insp_intg_both LIKE bseg-dmbe2,
        l_diff_totals LIKE bseg-dmbe2,

        l_diff_insp_geop_per LIKE bseg-dmbe2,
        l_diff_insp_mfl_per  LIKE bseg-dmbe2,
        l_diff_insp_both_per LIKE bseg-dmbe2,
        l_diff_intg_rep_per LIKE bseg-dmbe2,
        l_diff_intg_intg_per LIKE bseg-dmbe2,
        l_diff_insp_intg_geop_per LIKE bseg-dmbe2,
        l_diff_insp_intg_mfl_per  LIKE bseg-dmbe2,
        l_diff_insp_intg_rep_per  LIKE bseg-dmbe2,
        l_diff_insp_intg_intg_per  LIKE bseg-dmbe2,
        l_diff_insp_intg_both_per LIKE bseg-dmbe2,
        l_diff_totals_per LIKE bseg-dmbe2.

  DATA: l_rev_cost LIKE zfrev_cost_descp-description,
        l_temp LIKE bseg-dmbe2,
        wa_result LIKE it_result,
        l_state TYPE c.

* sort the internal table
  SORT it_summary BY rev_cost_flg DESCENDING
                     rev_cost_id ASCENDING
                     hkont ASCENDING.

* set indicator that report is the highest level
  sy-lsind = 0.

  LOOP AT it_summary.
*   if the currency is not USD, get the local currency
*   from the system
    IF p_ucomm <> 'USD'.
      SELECT SINGLE waers FROM t001 INTO g_currency
        WHERE bukrs = it_summary-bukrs.
    ELSE.
      g_currency = p_ucomm.
    ENDIF.
    l_state = it_summary-state.

*   write subtitle Revenu or Direct Cost
    AT NEW rev_cost_flg.
      FORMAT INTENSIFIED OFF.

      IF it_summary-rev_cost_flg = 'R'.
        mac_print_group2 'REVENUE'.
      ELSE.
        mac_print_group2 'DIRECT COST'.
      ENDIF.
      mac_print_1 '' '' '' '' '' '' '' ''.
      mac_print_2 '' '' '' '' '' '' '' ''.
      mac_print_3 '' '' '' '' '' ''.
      mac_print_vline.
*     get total revenue for each job type for this revenue or
*     direct cost
      CLEAR: wa_result.
      PERFORM get_total_revenue USING it_summary-bukrs
                                CHANGING wa_result.

    ENDAT.

*   get description for each revenue or direct cost
    CLEAR: l_rev_cost.
    SELECT SINGLE description FROM zfrev_cost_descp INTO l_rev_cost
      WHERE id = it_summary-rev_cost_id.

*   if option of showing 0 balanced entries, then display all of the
*   revenue and direct cost
*   if option is not selected, then just display the ones with balances
    IF g_currency = 'USD'.
      IF ( NOT p_zero IS INITIAL ) OR
         ( p_zero IS INITIAL AND
           ( it_summary-insp_geop_dmbe2 <> 0 OR
             it_summary-insp_mfl_dmbe2 <> 0 OR
             it_summary-insp_both_dmbe2 <> 0 OR
             it_summary-intg_intg_dmbe2 <> 0 OR
             it_summary-intg_rep_dmbe2 <> 0 OR
             it_summary-insp_intg_geop_dmbe2 <> 0 OR
             it_summary-insp_intg_mfl_dmbe2 <> 0 OR
             it_summary-insp_intg_both_dmbe2 <> 0 OR
             it_summary-insp_intg_intg_dmbe2 <> 0 OR
             it_summary-insp_intg_rep_dmbe2 <> 0 OR
             it_summary-total_dmbe2 <> 0 ) ).
        IF it_summary-state = c_close.
          mac_print_group5 l_rev_cost.
        ELSEIF it_summary-state = c_open.
          mac_print_group6 l_rev_cost.
        ENDIF.
      ENDIF.
    ELSE.
      IF ( NOT p_zero IS INITIAL ) OR
        ( p_zero IS INITIAL AND
          ( it_summary-insp_geop_dmbtr <> 0 OR
            it_summary-insp_mfl_dmbtr <> 0 OR
            it_summary-insp_both_dmbtr <> 0 OR
            it_summary-intg_intg_dmbtr <> 0 OR
            it_summary-intg_rep_dmbtr <> 0 OR
            it_summary-insp_intg_geop_dmbtr <> 0 OR
            it_summary-insp_intg_mfl_dmbtr <> 0 OR
            it_summary-insp_intg_both_dmbtr <> 0 OR
            it_summary-insp_intg_intg_dmbtr <> 0 OR
            it_summary-insp_intg_rep_dmbtr <> 0 OR
            it_summary-total_dmbtr <> 0 ) ) .

        IF it_summary-state = c_close.
          mac_print_group5 l_rev_cost.
        ELSEIF it_summary-state = c_open.
          mac_print_group6 l_rev_cost.
        ENDIF.
      ENDIF.
    ENDIF.

*     if the report currency is in USD
    IF ( g_currency = 'USD' ) AND
            ( ( NOT p_zero IS INITIAL ) OR
              ( p_zero IS INITIAL AND
              ( it_summary-insp_geop_dmbe2 <> 0 OR
                it_summary-insp_mfl_dmbe2 <> 0 OR
                it_summary-insp_both_dmbe2 <> 0 OR
                it_summary-intg_intg_dmbe2 <> 0 OR
                it_summary-intg_rep_dmbe2 <> 0 OR
                it_summary-insp_intg_geop_dmbe2 <> 0 OR
                it_summary-insp_intg_mfl_dmbe2 <> 0 OR
                it_summary-insp_intg_both_dmbe2 <> 0 OR
                it_summary-insp_intg_intg_dmbe2 <> 0 OR
                it_summary-insp_intg_rep_dmbe2 <> 0 OR
                it_summary-total_dmbe2 <> 0 ) ) ).

*       write amount for each column
      mac_print_1 it_summary-insp_geop_dmbe2
                  it_summary-insp_geop_dmbe2_per
                  it_summary-insp_mfl_dmbe2
                  it_summary-insp_mfl_dmbe2_per
                  it_summary-insp_both_dmbe2
                  it_summary-insp_both_dmbe2_per
                  it_summary-intg_intg_dmbe2
                  it_summary-intg_intg_dmbe2_per.

      mac_print_2 it_summary-intg_rep_dmbe2
                  it_summary-intg_rep_dmbe2_per
                  it_summary-insp_intg_geop_dmbe2
                  it_summary-insp_intg_geop_dmbe2_per
                  it_summary-insp_intg_mfl_dmbe2
                  it_summary-insp_intg_mfl_dmbe2_per
                  it_summary-insp_intg_both_dmbe2
                  it_summary-insp_intg_both_dmbe2_per.

      mac_print_3 it_summary-insp_intg_intg_dmbe2
                  it_summary-insp_intg_intg_dmbe2_per
                  it_summary-insp_intg_rep_dmbe2
                  it_summary-insp_intg_rep_dmbe2_per
                  it_summary-total_dmbe2
                  it_summary-total_dmbe2_per.

      HIDE: it_summary-rev_cost_flg, it_summary-rev_cost_id,
            it_summary-state.

*   write in local currency
    ELSEIF ( g_currency <> 'USD' ) AND
           ( ( NOT p_zero IS INITIAL ) OR
             ( p_zero IS INITIAL AND
             ( it_summary-insp_geop_dmbtr <> 0 OR
               it_summary-insp_mfl_dmbtr <> 0 OR
               it_summary-insp_both_dmbtr <> 0 OR
               it_summary-intg_intg_dmbtr <> 0 OR
               it_summary-intg_rep_dmbtr <> 0 OR
               it_summary-insp_intg_geop_dmbtr <> 0 OR
               it_summary-insp_intg_mfl_dmbtr <> 0 OR
               it_summary-insp_intg_both_dmbtr <> 0 OR
               it_summary-insp_intg_intg_dmbtr <> 0 OR
               it_summary-insp_intg_rep_dmbtr <> 0 OR
               it_summary-total_dmbtr <> 0 ) ) ).

*       write amount for each column
      mac_print_1 it_summary-insp_geop_dmbtr
                  it_summary-insp_geop_dmbtr_per
                  it_summary-insp_mfl_dmbtr
                  it_summary-insp_mfl_dmbtr_per
                  it_summary-insp_both_dmbtr
                  it_summary-insp_both_dmbtr_per
                  it_summary-intg_intg_dmbtr
                  it_summary-intg_intg_dmbtr_per.

      mac_print_2 it_summary-intg_rep_dmbtr
                  it_summary-intg_rep_dmbtr_per
                  it_summary-insp_intg_geop_dmbtr
                  it_summary-insp_intg_geop_dmbtr_per
                  it_summary-insp_intg_mfl_dmbtr
                  it_summary-insp_intg_mfl_dmbtr_per
                  it_summary-insp_intg_both_dmbtr
                  it_summary-insp_intg_both_dmbtr_per.

      mac_print_3 it_summary-insp_intg_intg_dmbtr
                  it_summary-insp_intg_intg_dmbtr_per
                  it_summary-insp_intg_rep_dmbtr
                  it_summary-insp_intg_rep_dmbtr_per
                  it_summary-total_dmbtr
                  it_summary-total_dmbtr_per.

      HIDE: it_summary-rev_cost_flg, it_summary-rev_cost_id,
            it_summary-state.
    ENDIF.

*   if the state of this summary line is open, display all
*   the detailed GL accounts
    IF it_summary-state = c_open.
      PERFORM write_gl_details USING it_summary wa_result.
    ENDIF.

*   Write subtotal
    AT END OF rev_cost_flg.
      FORMAT COLOR COL_TOTAL INTENSIFIED OFF.
      IF it_summary-rev_cost_flg = 'R'.
        mac_print_group2 'TOTAL REVENUE'.
      ELSE.
        mac_print_group2 'TOTAL DIRECT COST'.
      ENDIF.

      SUM.

*     write sub total in USD and store sub total for revenues
*     and direct cost
      IF g_currency = 'USD'.
*       store sub total for the difference calculation
        IF it_summary-rev_cost_flg = 'R'.
          CLEAR:l_tot_rev_insp_geop, l_tot_rev_insp_mfl,
                l_tot_rev_insp_both,
                l_tot_rev_intg_rep, l_tot_rev_intg_intg,
                l_tot_rev_insp_intg_geop, l_tot_rev_insp_intg_mfl,
                l_tot_rev_insp_intg_intg, l_tot_rev_insp_intg_rep,
                l_tot_rev_insp_intg_both,
                l_tot_rev_totals.

          CLEAR:l_tot_rev_insp_geop_per, l_tot_rev_insp_mfl_per,
                l_tot_rev_insp_both_per,
                l_tot_rev_intg_rep_per, l_tot_rev_intg_intg_per,
                l_tot_rev_insp_intg_geop_per,
                l_tot_rev_insp_intg_mfl_per,
                l_tot_rev_insp_intg_intg_per,
                l_tot_rev_insp_intg_rep_per,
                l_tot_rev_insp_intg_both_per,
                l_tot_rev_totals_per.

          l_tot_rev_insp_geop = it_summary-insp_geop_dmbe2.
          l_tot_rev_insp_mfl = it_summary-insp_mfl_dmbe2.
          l_tot_rev_insp_both = it_summary-insp_both_dmbe2.
          l_tot_rev_intg_intg = it_summary-intg_intg_dmbe2.
          l_tot_rev_intg_rep = it_summary-intg_rep_dmbe2.
          l_tot_rev_insp_intg_geop = it_summary-insp_intg_geop_dmbe2.
          l_tot_rev_insp_intg_mfl = it_summary-insp_intg_mfl_dmbe2.
          l_tot_rev_insp_intg_intg = it_summary-insp_intg_intg_dmbe2.
          l_tot_rev_insp_intg_rep = it_summary-insp_intg_rep_dmbe2.
          l_tot_rev_insp_intg_both = it_summary-insp_intg_both_dmbe2.
          l_tot_rev_totals = it_summary-total_dmbe2.

          l_tot_rev_totals_per = it_summary-total_dmbe2_per.
          l_tot_rev_insp_geop_per = it_summary-insp_geop_dmbe2_per.
          l_tot_rev_insp_mfl_per = it_summary-insp_mfl_dmbe2_per.
          l_tot_rev_insp_both_per = it_summary-insp_both_dmbe2_per.
          l_tot_rev_intg_intg_per = it_summary-intg_intg_dmbe2_per.
          l_tot_rev_intg_rep_per = it_summary-intg_rep_dmbe2_per.
          l_tot_rev_insp_intg_geop_per =
                              it_summary-insp_intg_geop_dmbe2_per.
          l_tot_rev_insp_intg_mfl_per =
                              it_summary-insp_intg_mfl_dmbe2_per.
          l_tot_rev_insp_intg_intg_per =
                              it_summary-insp_intg_intg_dmbe2_per.
          l_tot_rev_insp_intg_rep_per =
                              it_summary-insp_intg_rep_dmbe2_per.
          l_tot_rev_insp_intg_both_per =
                              it_summary-insp_intg_both_dmbe2_per.

        ELSE.
          CLEAR: l_tot_cost_insp_geop, l_tot_cost_insp_mfl,
                 l_tot_cost_insp_both,
                 l_tot_cost_intg_rep, l_tot_cost_intg_intg,
                 l_tot_cost_insp_intg_geop, l_tot_cost_insp_intg_mfl,
                 l_tot_cost_insp_intg_intg, l_tot_cost_insp_intg_rep,
                 l_tot_cost_insp_intg_both,
                 l_tot_cost_totals.

          CLEAR: l_tot_cost_insp_geop_per, l_tot_cost_insp_mfl_per,
                 l_tot_cost_insp_both_per,
                 l_tot_cost_intg_rep_per, l_tot_cost_intg_intg_per,
                 l_tot_cost_insp_intg_geop_per,
                 l_tot_cost_insp_intg_mfl_per,
                 l_tot_cost_insp_intg_intg_per,
                 l_tot_cost_insp_intg_rep_per,
                 l_tot_cost_insp_intg_both_per,
                 l_tot_cost_totals_per.


          l_tot_cost_insp_geop = it_summary-insp_geop_dmbe2.
          l_tot_cost_insp_mfl = it_summary-insp_mfl_dmbe2.
          l_tot_cost_insp_both = it_summary-insp_both_dmbe2.
          l_tot_cost_intg_intg = it_summary-intg_intg_dmbe2.
          l_tot_cost_intg_rep = it_summary-intg_rep_dmbe2.
          l_tot_cost_insp_intg_geop = it_summary-insp_intg_geop_dmbe2.
          l_tot_cost_insp_intg_mfl = it_summary-insp_intg_mfl_dmbe2.
          l_tot_cost_insp_intg_intg = it_summary-insp_intg_intg_dmbe2.
          l_tot_cost_insp_intg_rep = it_summary-insp_intg_rep_dmbe2.
          l_tot_cost_insp_intg_both = it_summary-insp_intg_both_dmbe2.
          l_tot_cost_totals = it_summary-total_dmbe2.

          l_tot_cost_totals_per = it_summary-total_dmbe2_per.
          l_tot_cost_insp_geop_per = it_summary-insp_geop_dmbe2_per.
          l_tot_cost_insp_mfl_per = it_summary-insp_mfl_dmbe2_per.
          l_tot_cost_insp_both_per = it_summary-insp_both_dmbe2_per.
          l_tot_cost_intg_intg_per = it_summary-intg_intg_dmbe2_per.
          l_tot_cost_intg_rep_per = it_summary-intg_rep_dmbe2_per.
          l_tot_cost_insp_intg_geop_per =
                                it_summary-insp_intg_geop_dmbe2_per.
          l_tot_cost_insp_intg_mfl_per =
                                it_summary-insp_intg_mfl_dmbe2_per.
          l_tot_cost_insp_intg_intg_per =
                                it_summary-insp_intg_intg_dmbe2_per.
          l_tot_cost_insp_intg_rep_per =
                                it_summary-insp_intg_rep_dmbe2_per.
          l_tot_cost_insp_intg_both_per =
                                it_summary-insp_intg_both_dmbe2_per.
        ENDIF.

*       write subtotal
        mac_print_1 it_summary-insp_geop_dmbe2
                    it_summary-insp_geop_dmbe2_per
                    it_summary-insp_mfl_dmbe2
                    it_summary-insp_mfl_dmbe2_per
                    it_summary-insp_both_dmbe2
                    it_summary-insp_both_dmbe2_per
                    it_summary-intg_intg_dmbe2
                    it_summary-intg_intg_dmbe2_per.

        mac_print_2 it_summary-intg_rep_dmbe2
                    it_summary-intg_rep_dmbe2_per
                    it_summary-insp_intg_geop_dmbe2
                    it_summary-insp_intg_geop_dmbe2_per
                    it_summary-insp_intg_mfl_dmbe2
                    it_summary-insp_intg_mfl_dmbe2_per
                    it_summary-insp_intg_both_dmbe2
                    it_summary-insp_intg_both_dmbe2_per.

        mac_print_3 it_summary-insp_intg_intg_dmbe2
                    it_summary-insp_intg_intg_dmbe2_per
                    it_summary-insp_intg_rep_dmbe2
                    it_summary-insp_intg_rep_dmbe2_per
                    it_summary-total_dmbe2 it_summary-total_dmbe2_per.

      ELSE.
*       same as above but in local currency
        IF it_summary-rev_cost_flg = 'R'.
          CLEAR:l_tot_rev_insp_geop, l_tot_rev_insp_mfl,
                l_tot_rev_insp_both,
                l_tot_rev_intg_rep, l_tot_rev_intg_intg,
                l_tot_rev_insp_intg_geop, l_tot_rev_insp_intg_mfl,
                l_tot_rev_insp_intg_intg, l_tot_rev_insp_intg_rep,
                l_tot_rev_insp_intg_both,
                l_tot_rev_totals.

          CLEAR:l_tot_rev_insp_geop_per, l_tot_rev_insp_mfl_per,
                l_tot_rev_insp_both_per,
                l_tot_rev_intg_rep_per, l_tot_rev_intg_intg_per,
                l_tot_rev_insp_intg_geop_per,
                l_tot_rev_insp_intg_mfl_per,
                l_tot_rev_insp_intg_intg_per,
                l_tot_rev_insp_intg_rep_per,
                l_tot_rev_insp_intg_both_per,
                l_tot_rev_totals_per.

          l_tot_rev_insp_geop = it_summary-insp_geop_dmbtr.
          l_tot_rev_insp_mfl = it_summary-insp_mfl_dmbtr.
          l_tot_rev_insp_both = it_summary-insp_both_dmbtr.
          l_tot_rev_intg_intg = it_summary-intg_intg_dmbtr.
          l_tot_rev_intg_rep = it_summary-intg_rep_dmbtr.
          l_tot_rev_insp_intg_geop = it_summary-insp_intg_geop_dmbtr.
          l_tot_rev_insp_intg_mfl = it_summary-insp_intg_mfl_dmbtr.
          l_tot_rev_insp_intg_intg = it_summary-insp_intg_intg_dmbtr.
          l_tot_rev_insp_intg_rep = it_summary-insp_intg_rep_dmbtr.
          l_tot_rev_insp_intg_both = it_summary-insp_intg_both_dmbtr.
          l_tot_rev_totals = it_summary-total_dmbtr.

          l_tot_rev_totals_per = it_summary-total_dmbtr_per.
          l_tot_rev_insp_geop_per = it_summary-insp_geop_dmbtr_per.
          l_tot_rev_insp_mfl_per = it_summary-insp_mfl_dmbtr_per.
          l_tot_rev_insp_both_per = it_summary-insp_both_dmbtr_per.
          l_tot_rev_intg_intg_per = it_summary-intg_intg_dmbtr_per.
          l_tot_rev_intg_rep_per = it_summary-intg_rep_dmbtr_per.
          l_tot_rev_insp_intg_geop_per =
                                  it_summary-insp_intg_geop_dmbtr_per.
          l_tot_rev_insp_intg_mfl_per =
                                  it_summary-insp_intg_mfl_dmbtr_per.
          l_tot_rev_insp_intg_intg_per =
                                  it_summary-insp_intg_intg_dmbtr_per.
          l_tot_rev_insp_intg_rep_per =
                                  it_summary-insp_intg_rep_dmbtr_per.
          l_tot_rev_insp_intg_both_per =
                                  it_summary-insp_intg_both_dmbtr_per.

        ELSE.
          CLEAR: l_tot_cost_insp_geop, l_tot_cost_insp_mfl,
                 l_tot_cost_insp_both,
                 l_tot_cost_intg_rep, l_tot_cost_intg_intg,
                 l_tot_cost_insp_intg_geop, l_tot_cost_insp_intg_mfl,
                 l_tot_cost_insp_intg_intg, l_tot_cost_insp_intg_rep,
                 l_tot_cost_insp_intg_both,
                 l_tot_cost_totals.

          CLEAR: l_tot_cost_insp_geop_per, l_tot_cost_insp_mfl_per,
                 l_tot_cost_insp_both_per,
                 l_tot_cost_intg_rep_per, l_tot_cost_intg_intg_per,
                 l_tot_cost_insp_intg_geop_per,
                 l_tot_cost_insp_intg_mfl_per,
                 l_tot_cost_insp_intg_intg_per,
                 l_tot_cost_insp_intg_rep_per,
                 l_tot_cost_insp_intg_both_per,
                 l_tot_cost_totals_per.

          l_tot_cost_insp_geop = it_summary-insp_geop_dmbtr.
          l_tot_cost_insp_mfl = it_summary-insp_mfl_dmbtr.
          l_tot_cost_insp_both = it_summary-insp_both_dmbtr.
          l_tot_cost_intg_intg = it_summary-intg_intg_dmbtr.
          l_tot_cost_intg_rep = it_summary-intg_rep_dmbtr.
          l_tot_cost_insp_intg_geop = it_summary-insp_intg_geop_dmbtr.
          l_tot_cost_insp_intg_mfl = it_summary-insp_intg_mfl_dmbtr.
          l_tot_cost_insp_intg_intg = it_summary-insp_intg_intg_dmbtr.
          l_tot_cost_insp_intg_rep = it_summary-insp_intg_rep_dmbtr.
          l_tot_cost_insp_intg_both = it_summary-insp_intg_both_dmbtr.
          l_tot_cost_totals = it_summary-total_dmbtr.

          l_tot_cost_totals_per = it_summary-total_dmbtr_per.
          l_tot_cost_insp_geop_per = it_summary-insp_geop_dmbtr_per.
          l_tot_cost_insp_mfl_per = it_summary-insp_mfl_dmbtr_per.
          l_tot_cost_insp_both_per = it_summary-insp_both_dmbtr_per.
          l_tot_cost_intg_intg_per = it_summary-intg_intg_dmbtr_per.
          l_tot_cost_intg_rep_per = it_summary-intg_rep_dmbtr_per.
          l_tot_cost_insp_intg_geop_per =
                                  it_summary-insp_intg_geop_dmbtr_per.
          l_tot_cost_insp_intg_mfl_per =
                                  it_summary-insp_intg_mfl_dmbtr_per.
          l_tot_cost_insp_intg_intg_per =
                                  it_summary-insp_intg_intg_dmbtr_per.
          l_tot_cost_insp_intg_rep_per =
                                  it_summary-insp_intg_rep_dmbtr_per.
          l_tot_cost_insp_intg_both_per =
                                  it_summary-insp_intg_both_dmbtr_per.

        ENDIF.
        mac_print_1 it_summary-insp_geop_dmbtr
                    it_summary-insp_geop_dmbtr_per
                    it_summary-insp_mfl_dmbtr
                    it_summary-insp_mfl_dmbtr_per
                    it_summary-insp_both_dmbtr
                    it_summary-insp_both_dmbtr_per
                    it_summary-intg_intg_dmbtr
                    it_summary-intg_intg_dmbtr_per.
        mac_print_2 it_summary-intg_rep_dmbtr
                    it_summary-intg_rep_dmbtr_per
                    it_summary-insp_intg_geop_dmbtr
                    it_summary-insp_intg_geop_dmbtr_per
                    it_summary-insp_intg_mfl_dmbtr
                    it_summary-insp_intg_mfl_dmbtr_per
                    it_summary-insp_intg_both_dmbtr
                    it_summary-insp_intg_both_dmbtr_per.
        mac_print_3 it_summary-insp_intg_intg_dmbtr
                    it_summary-insp_intg_intg_dmbtr_per
                    it_summary-insp_intg_rep_dmbtr
                    it_summary-insp_intg_rep_dmbtr_per
                    it_summary-total_dmbtr it_summary-total_dmbtr_per.

      ENDIF.
      FORMAT RESET.
      mac_print_vline.
    ENDAT.

    AT LAST.
*     at the end calculate the difference between
*     total cost and total revenue
      FORMAT COLOR COL_TOTAL INTENSIFIED ON.
      mac_print_group1 'DIRECT CONTRIBUTION MARGIN'.

      CLEAR: l_diff_insp_geop, l_diff_insp_mfl, l_diff_insp_both,
             l_diff_intg_intg, l_diff_intg_rep,
             l_diff_insp_intg_geop, l_diff_insp_intg_mfl,
             l_diff_insp_intg_intg, l_diff_insp_intg_rep,
             l_diff_insp_intg_both,
             l_diff_totals.

      CLEAR: l_diff_insp_geop_per, l_diff_insp_mfl_per,
             l_diff_insp_both_per,
             l_diff_intg_intg_per, l_diff_intg_rep_per,
             l_diff_insp_intg_geop_per, l_diff_insp_intg_mfl_per,
             l_diff_insp_intg_intg_per, l_diff_insp_intg_rep_per,
             l_diff_insp_intg_both_per,
             l_diff_totals_per.

      l_diff_insp_geop = l_tot_rev_insp_geop - l_tot_cost_insp_geop.
      l_diff_insp_mfl  = l_tot_rev_insp_mfl  - l_tot_cost_insp_mfl.
      l_diff_insp_both = l_tot_rev_insp_both - l_tot_cost_insp_both.
      l_diff_intg_intg = l_tot_rev_intg_intg - l_tot_cost_intg_intg.
      l_diff_intg_rep = l_tot_rev_intg_rep - l_tot_cost_intg_rep.
      l_diff_insp_intg_geop = l_tot_rev_insp_intg_geop -
                                  l_tot_cost_insp_intg_geop.
      l_diff_insp_intg_mfl = l_tot_rev_insp_intg_mfl -
                                 l_tot_cost_insp_intg_mfl.
      l_diff_insp_intg_intg = l_tot_rev_insp_intg_intg -
                                  l_tot_cost_insp_intg_intg.
      l_diff_insp_intg_rep = l_tot_rev_insp_intg_rep -
                                  l_tot_cost_insp_intg_rep.
      l_diff_insp_intg_both = l_tot_rev_insp_intg_both -
                                  l_tot_cost_insp_intg_both.
      l_diff_totals = l_tot_rev_totals - l_tot_cost_totals.

      l_diff_totals_per = l_tot_rev_totals_per
                            - l_tot_cost_totals_per.
      l_diff_insp_geop_per = l_tot_rev_insp_geop_per -
                              l_tot_cost_insp_geop_per.
      l_diff_insp_mfl_per  = l_tot_rev_insp_mfl_per  -
                              l_tot_cost_insp_mfl_per.
      l_diff_insp_both_per = l_tot_rev_insp_both_per -
                              l_tot_cost_insp_both_per.
      l_diff_intg_intg_per = l_tot_rev_intg_intg_per -
                              l_tot_cost_intg_intg_per.
      l_diff_intg_rep_per = l_tot_rev_intg_rep_per -
                              l_tot_cost_intg_rep_per.
      l_diff_insp_intg_geop_per = l_tot_rev_insp_intg_geop_per -
                                  l_tot_cost_insp_intg_geop_per.
      l_diff_insp_intg_mfl_per = l_tot_rev_insp_intg_mfl_per -
                                 l_tot_cost_insp_intg_mfl_per.
      l_diff_insp_intg_intg_per = l_tot_rev_insp_intg_intg_per -
                                  l_tot_cost_insp_intg_intg_per.
      l_diff_insp_intg_rep_per = l_tot_rev_insp_intg_rep_per -
                                  l_tot_cost_insp_intg_rep_per.
      l_diff_insp_intg_both_per = l_tot_rev_insp_intg_both_per -
                                  l_tot_cost_insp_intg_both_per.

      mac_print_1 l_diff_insp_geop l_diff_insp_geop_per
                  l_diff_insp_mfl l_diff_insp_mfl_per
                  l_diff_insp_both l_diff_insp_both_per
                  l_diff_intg_intg l_diff_intg_intg_per.
      mac_print_2 l_diff_intg_rep l_diff_intg_rep_per
                  l_diff_insp_intg_geop l_diff_insp_intg_geop_per
                  l_diff_insp_intg_mfl l_diff_insp_intg_mfl_per
                  l_diff_insp_intg_both l_diff_insp_intg_both_per.
      mac_print_3 l_diff_insp_intg_intg l_diff_insp_intg_intg_per
                  l_diff_insp_intg_rep l_diff_insp_intg_rep_per
                  l_diff_totals l_diff_totals_per.
      FORMAT RESET.
      ULINE.

*     clear all local variables
      CLEAR:l_tot_rev_insp_geop, l_tot_rev_insp_mfl,
            l_tot_rev_insp_both,
            l_tot_rev_intg_rep, l_tot_rev_intg_intg,
            l_tot_rev_insp_intg_geop, l_tot_rev_insp_intg_mfl,
            l_tot_rev_insp_intg_rep, l_tot_rev_insp_intg_intg,
            l_tot_rev_insp_intg_both,
            l_tot_rev_totals.

      CLEAR:l_tot_cost_insp_geop, l_tot_cost_insp_mfl,
            l_tot_cost_insp_both,
            l_tot_cost_intg_rep, l_tot_cost_intg_intg,
            l_tot_cost_insp_intg_geop, l_tot_cost_insp_intg_mfl,
            l_tot_cost_insp_intg_rep, l_tot_cost_insp_intg_intg,
            l_tot_cost_insp_intg_both,
            l_tot_cost_totals.

      CLEAR:l_diff_insp_geop, l_diff_insp_mfl, l_diff_insp_both,
            l_diff_intg_rep, l_diff_intg_intg,
            l_diff_insp_intg_geop, l_diff_insp_intg_mfl,
            l_diff_insp_intg_rep, l_diff_insp_intg_intg,
            l_diff_insp_intg_both,
            l_diff_totals.

      CLEAR:l_tot_rev_insp_geop_per, l_tot_rev_insp_mfl_per,
            l_tot_rev_insp_both_per,
            l_tot_rev_intg_rep_per, l_tot_rev_intg_intg_per,
            l_tot_rev_insp_intg_geop_per, l_tot_rev_insp_intg_mfl_per,
            l_tot_rev_insp_intg_rep_per, l_tot_rev_insp_intg_intg_per,
            l_tot_rev_insp_intg_both_per,
            l_tot_rev_totals_per.

      CLEAR:l_tot_cost_insp_geop_per, l_tot_cost_insp_mfl_per,
            l_tot_cost_insp_both_per,
            l_tot_cost_intg_rep_per, l_tot_cost_intg_intg_per,
            l_tot_cost_insp_intg_geop_per, l_tot_cost_insp_intg_mfl_per,
            l_tot_cost_insp_intg_rep_per, l_tot_cost_insp_intg_intg_per,
            l_tot_cost_insp_intg_both_per,
            l_tot_cost_totals_per.

      CLEAR:l_diff_insp_geop_per, l_diff_insp_mfl_per,
            l_diff_insp_both_per,
            l_diff_intg_rep_per, l_diff_intg_intg_per,
            l_diff_insp_intg_geop_per, l_diff_insp_intg_mfl_per,
            l_diff_insp_intg_rep_per, l_diff_insp_intg_intg_per,
            l_diff_insp_intg_both_per,
            l_diff_totals_per.

    ENDAT.
    CLEAR: it_summary.
  ENDLOOP.

* display the selection options at the end of the report
  PERFORM display_selection_options.

ENDFORM.                    " WRITE_REPORT

*&---------------------------------------------------------------------*
*&      Form  WRITE_GL_DETAILS
*&---------------------------------------------------------------------*
*&  Display detailed GL account lines
*&---------------------------------------------------------------------*
FORM write_gl_details USING    p_summary STRUCTURE it_summary
                               p_result STRUCTURE it_result.
  DATA: l_state TYPE c,
        l_gl_temp LIKE bseg-hkont,
        l_total_rev LIKE bseg-dmbtr,
        l_temp LIKE bseg-dmbtr,
        l_gl_descript LIKE skat-txt50.

  DATA: l_insp_geop_dmbtr LIKE bseg-dmbtr,
        l_insp_mfl_dmbtr LIKE bseg-dmbtr,
        l_insp_both_dmbtr LIKE bseg-dmbtr,
        l_intg_rep_dmbtr LIKE bseg-dmbtr,
        l_intg_intg_dmbtr LIKE bseg-dmbtr,
        l_insp_intg_geop_dmbtr LIKE bseg-dmbtr,
        l_insp_intg_mfl_dmbtr LIKE bseg-dmbtr,
        l_insp_intg_rep_dmbtr LIKE bseg-dmbtr,
        l_insp_intg_intg_dmbtr LIKE bseg-dmbtr,
        l_insp_intg_both_dmbtr LIKE bseg-dmbtr,
        l_insp_geop_dmbtr_per LIKE bseg-dmbtr,
        l_insp_mfl_dmbtr_per LIKE bseg-dmbtr,
        l_insp_both_dmbtr_per LIKE bseg-dmbtr,
        l_intg_rep_dmbtr_per LIKE bseg-dmbtr,
        l_intg_intg_dmbtr_per LIKE bseg-dmbtr,
        l_insp_intg_geop_dmbtr_per LIKE bseg-dmbtr,
        l_insp_intg_mfl_dmbtr_per LIKE bseg-dmbtr,
        l_insp_intg_rep_dmbtr_per LIKE bseg-dmbtr,
        l_insp_intg_intg_dmbtr_per LIKE bseg-dmbtr,
        l_insp_intg_both_dmbtr_per LIKE bseg-dmbtr,
        l_insp_geop_dmbe2 LIKE bseg-dmbe2,
        l_insp_mfl_dmbe2 LIKE bseg-dmbe2,
        l_insp_both_dmbe2 LIKE bseg-dmbe2,
        l_intg_rep_dmbe2 LIKE bseg-dmbe2,
        l_intg_intg_dmbe2 LIKE bseg-dmbe2,
        l_insp_intg_geop_dmbe2 LIKE bseg-dmbe2,
        l_insp_intg_mfl_dmbe2 LIKE bseg-dmbe2,
        l_insp_intg_rep_dmbe2 LIKE bseg-dmbe2,
        l_insp_intg_intg_dmbe2 LIKE bseg-dmbe2,
        l_insp_intg_both_dmbe2 LIKE bseg-dmbe2,
        l_total_dmbe2 LIKE bseg-dmbe2,
        l_total_dmbtr LIKE bseg-dmbtr,
        l_insp_geop_dmbe2_per LIKE bseg-dmbe2,
        l_insp_mfl_dmbe2_per LIKE bseg-dmbe2,
        l_insp_both_dmbe2_per LIKE bseg-dmbe2,
        l_intg_rep_dmbe2_per LIKE bseg-dmbe2,
        l_intg_intg_dmbe2_per LIKE bseg-dmbe2,
        l_insp_intg_geop_dmbe2_per LIKE bseg-dmbe2,
        l_insp_intg_mfl_dmbe2_per LIKE bseg-dmbe2,
        l_insp_intg_rep_dmbe2_per LIKE bseg-dmbe2,
        l_insp_intg_intg_dmbe2_per LIKE bseg-dmbe2,
        l_insp_intg_both_dmbe2_per LIKE bseg-dmbe2,
        l_total_dmbe2_per LIKE bseg-dmbe2,
        l_total_dmbtr_per LIKE bseg-dmbtr.

  SORT it_result BY rev_cost_flg DESCENDING
                    rev_cost_id ASCENDING
                    hkont ASCENDING.

  CLEAR: g_gl.

  LOOP AT it_result WHERE rev_cost_flg = p_summary-rev_cost_flg
                      AND rev_cost_id = p_summary-rev_cost_id.

    IF g_gl IS INITIAL.
      g_gl = it_result-hkont.
      l_state = it_result-state.
      CLEAR: l_gl_descript.
*     for each different GL account get the description for it
      SELECT SINGLE txt20 INTO l_gl_descript FROM skat
        WHERE spras = sy-langu
        AND ktopl = c_nwsl
        AND saknr = g_gl.

*     display GL account number and description  together
      l_gl_temp = g_gl.
      SHIFT l_gl_temp LEFT DELETING LEADING '0'.
      CONCATENATE l_gl_temp l_gl_descript INTO l_gl_descript
          SEPARATED BY space.
    ENDIF.


*   at the end of each GL account, calculate percentages, and
*   total
    IF ( g_gl <> it_result-hkont ).
      IF g_currency = 'USD'.
        l_total_dmbe2 =  l_insp_geop_dmbe2 +
                          l_insp_mfl_dmbe2 +
                          l_insp_both_dmbe2 +
                          l_intg_rep_dmbe2 +
                          l_intg_intg_dmbe2 +
                          l_insp_intg_geop_dmbe2 +
                          l_insp_intg_mfl_dmbe2 +
                          l_insp_intg_rep_dmbe2 +
                          l_insp_intg_intg_dmbe2 +
                          l_insp_intg_both_dmbe2.

*     calculate percentage for each job type
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_geop_dmbe2 ).
        IF l_temp > 1.
          l_insp_geop_dmbe2_per = l_insp_geop_dmbe2 /
                                  p_result-insp_geop_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_mfl_dmbe2 ).
        IF l_temp > 1.
          l_insp_mfl_dmbe2_per = l_insp_mfl_dmbe2 /
                           p_result-insp_mfl_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_both_dmbe2 ).
        IF l_temp > 1.
          l_insp_both_dmbe2_per = l_insp_both_dmbe2 /
                            p_result-insp_both_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-intg_rep_dmbe2 ).
        IF l_temp > 1.
          l_intg_rep_dmbe2_per = l_intg_rep_dmbe2 /
                           p_result-intg_rep_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-intg_intg_dmbe2 ).
        IF l_temp > 1.
          l_intg_intg_dmbe2_per = l_intg_intg_dmbe2 /
                           p_result-intg_intg_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_geop_dmbe2 ).
        IF l_temp > 1.
          l_insp_intg_geop_dmbe2_per =
                                 l_insp_intg_geop_dmbe2 /
                                 p_result-insp_intg_geop_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_mfl_dmbe2 ).
        IF l_temp > 1.
          l_insp_intg_mfl_dmbe2_per =
                                l_insp_intg_mfl_dmbe2 /
                                p_result-insp_intg_mfl_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_rep_dmbe2 ).
        IF l_temp > 1.
          l_insp_intg_rep_dmbe2_per =
                                l_insp_intg_rep_dmbe2 /
                                p_result-insp_intg_rep_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_intg_dmbe2 ).
        IF l_temp > 1.
          l_insp_intg_intg_dmbe2_per =
                                l_insp_intg_intg_dmbe2 /
                                p_result-insp_intg_intg_dmbe2 * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_both_dmbe2 ).
        IF l_temp > 1.
          l_insp_intg_both_dmbe2_per =
                                 l_insp_intg_both_dmbe2 /
                                 p_result-insp_intg_both_dmbe2 * 100.
        ENDIF.

        l_total_rev = p_result-insp_geop_dmbe2 +
                      p_result-insp_mfl_dmbe2 +
                      p_result-insp_both_dmbe2 +
                      p_result-intg_rep_dmbe2 +
                      p_result-intg_intg_dmbe2 +
                      p_result-insp_intg_geop_dmbe2 +
                      p_result-insp_intg_mfl_dmbe2 +
                      p_result-insp_intg_rep_dmbe2 +
                      p_result-insp_intg_intg_dmbe2 +
                      p_result-insp_intg_both_dmbe2.
        CLEAR: l_temp.
        l_temp = abs( l_total_rev ).
        IF l_temp > 1.
          l_total_dmbe2_per = l_total_dmbe2 / l_total_rev * 100.
        ENDIF.

*       if show 0 balance option is selected, display all GL accounts
*       else, only show the GL accounts with balances
        IF ( NOT p_zero IS INITIAL ) OR
           ( p_zero IS INITIAL AND
             ( l_insp_geop_dmbe2 <> 0 OR
               l_insp_mfl_dmbe2 <> 0 OR
               l_insp_both_dmbe2 <> 0 OR
               l_intg_intg_dmbe2 <> 0 OR
               l_intg_rep_dmbe2 <> 0 OR
               l_insp_intg_geop_dmbe2 <> 0 OR
               l_insp_intg_mfl_dmbe2 <> 0 OR
               l_insp_intg_both_dmbe2 <> 0 OR
               l_insp_intg_intg_dmbe2 <> 0 OR
               l_insp_intg_rep_dmbe2 <> 0 OR
               l_total_dmbe2 <> 0 ) ).

          IF l_state = c_close.
            mac_print_group7 l_gl_descript.
          ELSE.
            mac_print_group8 l_gl_descript.
          ENDIF.

          mac_print_1 l_insp_geop_dmbe2
                      l_insp_geop_dmbe2_per
                      l_insp_mfl_dmbe2
                      l_insp_mfl_dmbe2_per
                      l_insp_both_dmbe2
                      l_insp_both_dmbe2_per
                      l_intg_intg_dmbe2
                      l_intg_intg_dmbe2_per.

          mac_print_2 l_intg_rep_dmbe2
                      l_intg_rep_dmbe2_per
                      l_insp_intg_geop_dmbe2
                      l_insp_intg_geop_dmbe2_per
                      l_insp_intg_mfl_dmbe2
                      l_insp_intg_mfl_dmbe2_per
                      l_insp_intg_both_dmbe2
                      l_insp_intg_both_dmbe2_per.

          mac_print_3 l_insp_intg_intg_dmbe2
                      l_insp_intg_intg_dmbe2_per
                      l_insp_intg_rep_dmbe2
                      l_insp_intg_rep_dmbe2_per
                      l_total_dmbe2
                      l_total_dmbe2_per.
          HIDE: g_gl.

        ENDIF.

      ELSE.
*       do calculation in local currency
        l_total_dmbtr =  l_insp_geop_dmbtr +
                          l_insp_mfl_dmbtr +
                          l_insp_both_dmbtr +
                          l_intg_intg_dmbtr +
                          l_intg_rep_dmbtr +
                          l_insp_intg_geop_dmbtr +
                          l_insp_intg_mfl_dmbtr +
                          l_insp_intg_intg_dmbtr +
                          l_insp_intg_rep_dmbtr +
                          l_insp_intg_both_dmbtr.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_geop_dmbtr ).
        IF l_temp > 1.
          l_insp_geop_dmbtr_per = l_insp_geop_dmbtr /
                                       p_result-insp_geop_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_mfl_dmbtr ).
        IF l_temp > 1.
          l_insp_mfl_dmbtr_per = l_insp_mfl_dmbtr /
                           p_result-insp_mfl_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_both_dmbtr ).
        IF l_temp > 1.
          l_insp_both_dmbtr_per = l_insp_both_dmbtr /
                            p_result-insp_both_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-intg_rep_dmbtr ).
        IF l_temp > 1.
          l_intg_rep_dmbtr_per = l_intg_rep_dmbtr /
                           p_result-intg_rep_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-intg_intg_dmbtr ).
        IF l_temp > 1.
          l_intg_intg_dmbtr_per = l_intg_intg_dmbtr /
                           p_result-intg_intg_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_geop_dmbtr ).
        IF l_temp > 1.
          l_insp_intg_geop_dmbtr_per =
                                 l_insp_intg_geop_dmbtr /
                                 p_result-insp_intg_geop_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_mfl_dmbtr ).
        IF l_temp > 1.
          l_insp_intg_mfl_dmbe2_per =
                                l_insp_intg_mfl_dmbtr /
                                p_result-insp_intg_mfl_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_rep_dmbtr ).
        IF l_temp > 1.
          l_insp_intg_rep_dmbe2_per =
                                l_insp_intg_rep_dmbtr /
                                p_result-insp_intg_rep_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_intg_dmbtr ).
        IF l_temp > 1.
          l_insp_intg_intg_dmbtr_per =
                                l_insp_intg_intg_dmbtr /
                                p_result-insp_intg_intg_dmbtr * 100.
        ENDIF.
        CLEAR: l_temp.
        l_temp = abs( p_result-insp_intg_both_dmbtr ).
        IF l_temp > 1.
          l_insp_intg_both_dmbtr_per =
                                 l_insp_intg_both_dmbtr /
                                 p_result-insp_intg_both_dmbtr * 100.
        ENDIF.

        l_total_rev = p_result-insp_geop_dmbtr +
                      p_result-insp_mfl_dmbtr +
                      p_result-insp_both_dmbtr +
                      p_result-intg_rep_dmbtr +
                      p_result-intg_intg_dmbtr +
                      p_result-insp_intg_geop_dmbtr +
                      p_result-insp_intg_mfl_dmbtr +
                      p_result-insp_intg_rep_dmbtr +
                      p_result-insp_intg_intg_dmbtr +
                      p_result-insp_intg_both_dmbtr.
        CLEAR: l_temp.
        l_temp = abs( l_total_rev ).
        IF l_temp > 1.
          l_total_dmbtr_per = l_total_dmbtr / l_total_rev * 100.
        ENDIF.

*       display all GL accounts if show 0 balance option is selected
*       else only show the ones with balance
        IF ( NOT p_zero IS INITIAL ) OR
           ( p_zero IS INITIAL AND
             ( l_insp_geop_dmbtr <> 0 OR
               l_insp_mfl_dmbtr <> 0 OR
               l_insp_both_dmbtr <> 0 OR
               l_intg_intg_dmbtr <> 0 OR
               l_intg_rep_dmbtr <> 0 OR
               l_insp_intg_geop_dmbtr <> 0 OR
               l_insp_intg_mfl_dmbtr <> 0 OR
               l_insp_intg_both_dmbtr <> 0 OR
               l_insp_intg_intg_dmbtr <> 0 OR
               l_insp_intg_rep_dmbtr <> 0 OR
               l_total_dmbtr <> 0 ) ).

          IF l_state = c_close.
            mac_print_group7 l_gl_descript.
          ELSE.
            mac_print_group8 l_gl_descript.
          ENDIF.

          mac_print_1 l_insp_geop_dmbtr
                      l_insp_geop_dmbtr_per
                      l_insp_mfl_dmbtr
                      l_insp_mfl_dmbtr_per
                      l_insp_both_dmbtr
                      l_insp_both_dmbtr_per
                      l_intg_intg_dmbtr
                      l_intg_intg_dmbtr_per.

          mac_print_2 l_intg_rep_dmbtr
                      l_intg_rep_dmbtr_per
                      l_insp_intg_geop_dmbtr
                      l_insp_intg_geop_dmbtr_per
                      l_insp_intg_mfl_dmbtr
                      l_insp_intg_mfl_dmbtr_per
                      l_insp_intg_both_dmbtr
                      l_insp_intg_both_dmbtr_per.

          mac_print_3 l_insp_intg_intg_dmbtr
                      l_insp_intg_intg_dmbtr_per
                      l_insp_intg_rep_dmbtr
                      l_insp_intg_rep_dmbtr_per
                      l_total_dmbtr
                      l_total_dmbtr_per.
          HIDE: g_gl.
        ENDIF.
      ENDIF.

      IF l_state = c_open.
        PERFORM write_document_detail.
      ENDIF.

*     clear all the variables and move to the next GL account
      g_gl = it_result-hkont.
      l_state = it_result-state.

      CLEAR: l_gl_descript.
      SELECT SINGLE txt20 INTO l_gl_descript FROM skat
        WHERE spras = sy-langu
        AND ktopl = c_nwsl
        AND saknr = g_gl.

      l_gl_temp = g_gl.
      SHIFT l_gl_temp LEFT DELETING LEADING '0'.
      CONCATENATE l_gl_temp l_gl_descript INTO l_gl_descript
          SEPARATED BY space.

      CLEAR: l_insp_geop_dmbtr, l_insp_geop_dmbtr_per,
             l_insp_mfl_dmbtr,l_insp_mfl_dmbtr_per,
             l_insp_both_dmbtr, l_insp_both_dmbtr_per,
             l_intg_intg_dmbtr, l_intg_intg_dmbtr_per,
             l_intg_rep_dmbtr, l_intg_rep_dmbtr_per,
             l_insp_intg_geop_dmbtr, l_insp_intg_geop_dmbtr_per,
             l_insp_intg_mfl_dmbtr, l_insp_intg_mfl_dmbtr_per,
             l_insp_intg_intg_dmbtr, l_insp_intg_intg_dmbtr_per,
             l_insp_intg_rep_dmbtr, l_insp_intg_rep_dmbtr_per,
             l_insp_intg_both_dmbtr, l_insp_intg_both_dmbtr,
             l_total_dmbtr, l_total_dmbtr_per.

      CLEAR: l_insp_geop_dmbe2, l_insp_geop_dmbe2_per,
             l_insp_mfl_dmbe2,l_insp_mfl_dmbe2_per,
             l_insp_both_dmbe2, l_insp_both_dmbe2_per,
             l_intg_intg_dmbe2, l_intg_intg_dmbe2_per,
             l_intg_rep_dmbe2, l_intg_rep_dmbe2_per,
             l_insp_intg_geop_dmbe2, l_insp_intg_geop_dmbe2_per,
             l_insp_intg_mfl_dmbe2, l_insp_intg_mfl_dmbe2_per,
             l_insp_intg_intg_dmbe2, l_insp_intg_intg_dmbe2_per,
             l_insp_intg_rep_dmbe2, l_insp_intg_rep_dmbe2_per,
             l_insp_intg_both_dmbe2, l_insp_intg_both_dmbe2,
             l_total_dmbe2, l_total_dmbe2_per.

    ENDIF.

*   start to add up the values for the GL account
    IF g_currency <> 'USD'.
      l_insp_geop_dmbtr = l_insp_geop_dmbtr + it_result-insp_geop_dmbtr.
      l_insp_mfl_dmbtr = l_insp_mfl_dmbtr + it_result-insp_mfl_dmbtr.
      l_insp_both_dmbtr = l_insp_both_dmbtr + it_result-insp_both_dmbtr.
      l_intg_rep_dmbtr = l_intg_rep_dmbtr + it_result-intg_rep_dmbtr.
      l_intg_intg_dmbtr = l_intg_intg_dmbtr + it_result-intg_intg_dmbtr.
      l_insp_intg_geop_dmbtr = l_insp_intg_geop_dmbtr +
                               it_result-insp_intg_geop_dmbtr.
      l_insp_intg_mfl_dmbtr = l_insp_intg_mfl_dmbtr +
                               it_result-insp_intg_mfl_dmbtr.
      l_insp_intg_intg_dmbtr = l_insp_intg_intg_dmbtr +
                               it_result-insp_intg_intg_dmbtr.
      l_insp_intg_rep_dmbtr = l_insp_intg_rep_dmbtr +
                               it_result-insp_intg_rep_dmbtr.
      l_insp_intg_both_dmbtr = l_insp_intg_both_dmbtr +
                               it_result-insp_intg_both_dmbtr.
    ELSE.
      l_insp_geop_dmbe2 = l_insp_geop_dmbe2 + it_result-insp_geop_dmbe2.
      l_insp_mfl_dmbe2 = l_insp_mfl_dmbe2 + it_result-insp_mfl_dmbe2.
      l_insp_both_dmbe2 = l_insp_both_dmbe2 + it_result-insp_both_dmbe2.
      l_intg_rep_dmbe2 = l_intg_rep_dmbe2 + it_result-intg_rep_dmbe2.
      l_intg_intg_dmbe2 = l_intg_intg_dmbe2 + it_result-intg_intg_dmbe2.
      l_insp_intg_geop_dmbe2 = l_insp_intg_geop_dmbe2 +
                               it_result-insp_intg_geop_dmbe2.
      l_insp_intg_mfl_dmbe2 = l_insp_intg_mfl_dmbe2 +
                               it_result-insp_intg_mfl_dmbe2.
      l_insp_intg_intg_dmbe2 = l_insp_intg_intg_dmbe2 +
                               it_result-insp_intg_intg_dmbe2.
      l_insp_intg_rep_dmbe2 = l_insp_intg_rep_dmbe2 +
                               it_result-insp_intg_rep_dmbe2.
      l_insp_intg_both_dmbe2 = l_insp_intg_both_dmbe2 +
                               it_result-insp_intg_both_dmbe2.
    ENDIF.

  ENDLOOP.

* write the last GL account information
  IF g_currency = 'USD'.
    l_total_dmbe2 =  l_insp_geop_dmbe2 +
                      l_insp_mfl_dmbe2 +
                      l_insp_both_dmbe2 +
                      l_intg_rep_dmbe2 +
                      l_intg_intg_dmbe2 +
                      l_insp_intg_geop_dmbe2 +
                      l_insp_intg_mfl_dmbe2 +
                      l_insp_intg_rep_dmbe2 +
                      l_insp_intg_intg_dmbe2 +
                      l_insp_intg_both_dmbe2.

*     calculate percentage for each job type
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_geop_dmbe2 ).
    IF l_temp > 1.
      l_insp_geop_dmbe2_per = l_insp_geop_dmbe2 /
                              p_result-insp_geop_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_mfl_dmbe2 ).
    IF l_temp > 1.
      l_insp_mfl_dmbe2_per = l_insp_mfl_dmbe2 /
                       p_result-insp_mfl_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_both_dmbe2 ).
    IF l_temp > 1.
      l_insp_both_dmbe2_per = l_insp_both_dmbe2 /
                        p_result-insp_both_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-intg_rep_dmbe2 ).
    IF l_temp > 1.
      l_intg_rep_dmbe2_per = l_intg_rep_dmbe2 /
                       p_result-intg_rep_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-intg_intg_dmbe2 ).
    IF l_temp > 1.
      l_intg_intg_dmbe2_per = l_intg_intg_dmbe2 /
                       p_result-intg_intg_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_geop_dmbe2 ).
    IF l_temp > 1.
      l_insp_intg_geop_dmbe2_per =
                             l_insp_intg_geop_dmbe2 /
                             p_result-insp_intg_geop_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_mfl_dmbe2 ).
    IF l_temp > 1.
      l_insp_intg_mfl_dmbe2_per =
                            l_insp_intg_mfl_dmbe2 /
                            p_result-insp_intg_mfl_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_rep_dmbe2 ).
    IF l_temp > 1.
      l_insp_intg_rep_dmbe2_per =
                            l_insp_intg_rep_dmbe2 /
                            p_result-insp_intg_rep_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_intg_dmbe2 ).
    IF l_temp > 1.
      l_insp_intg_intg_dmbe2_per =
                            l_insp_intg_intg_dmbe2 /
                            p_result-insp_intg_intg_dmbe2 * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_both_dmbe2 ).
    IF l_temp > 1.
      l_insp_intg_both_dmbe2_per =
                             l_insp_intg_both_dmbe2 /
                             p_result-insp_intg_both_dmbe2 * 100.
    ENDIF.

    l_total_rev = p_result-insp_geop_dmbe2 +
                  p_result-insp_mfl_dmbe2 +
                  p_result-insp_both_dmbe2 +
                  p_result-intg_rep_dmbe2 +
                  p_result-intg_intg_dmbe2 +
                  p_result-insp_intg_geop_dmbe2 +
                  p_result-insp_intg_mfl_dmbe2 +
                  p_result-insp_intg_rep_dmbe2 +
                  p_result-insp_intg_intg_dmbe2 +
                  p_result-insp_intg_both_dmbe2.
    CLEAR: l_temp.
    l_temp = abs( l_total_rev ).
    IF l_temp > 1.
      l_total_dmbe2_per = l_total_dmbe2 / l_total_rev * 100.
    ENDIF.

    IF ( NOT p_zero IS INITIAL ) OR
       ( p_zero IS INITIAL AND
         ( l_insp_geop_dmbe2 <> 0 OR
           l_insp_mfl_dmbe2 <> 0 OR
           l_insp_both_dmbe2 <> 0 OR
           l_intg_intg_dmbe2 <> 0 OR
           l_intg_rep_dmbe2 <> 0 OR
           l_insp_intg_geop_dmbe2 <> 0 OR
           l_insp_intg_mfl_dmbe2 <> 0 OR
           l_insp_intg_both_dmbe2 <> 0 OR
           l_insp_intg_intg_dmbe2 <> 0 OR
           l_insp_intg_rep_dmbe2 <> 0 OR
           l_total_dmbe2 <> 0 ) ).


      IF l_state = c_close.
        mac_print_group7 l_gl_descript.
      ELSE.
        mac_print_group8 l_gl_descript.
      ENDIF.
      mac_print_1 l_insp_geop_dmbe2
                  l_insp_geop_dmbe2_per
                  l_insp_mfl_dmbe2
                  l_insp_mfl_dmbe2_per
                  l_insp_both_dmbe2
                  l_insp_both_dmbe2_per
                  l_intg_intg_dmbe2
                  l_intg_intg_dmbe2_per.

      mac_print_2 l_intg_rep_dmbe2
                  l_intg_rep_dmbe2_per
                  l_insp_intg_geop_dmbe2
                  l_insp_intg_geop_dmbe2_per
                  l_insp_intg_mfl_dmbe2
                  l_insp_intg_mfl_dmbe2_per
                  l_insp_intg_both_dmbe2
                  l_insp_intg_both_dmbe2_per.

      mac_print_3 l_insp_intg_intg_dmbe2
                  l_insp_intg_intg_dmbe2_per
                  l_insp_intg_rep_dmbe2
                  l_insp_intg_rep_dmbe2_per
                  l_total_dmbe2
                  l_total_dmbe2_per.

      HIDE: g_gl.
    ENDIF.
  ELSE.
    l_total_dmbtr =  l_insp_geop_dmbtr +
                      l_insp_mfl_dmbtr +
                      l_insp_both_dmbtr +
                      l_intg_intg_dmbtr +
                      l_intg_rep_dmbtr +
                      l_insp_intg_geop_dmbtr +
                      l_insp_intg_mfl_dmbtr +
                      l_insp_intg_intg_dmbtr +
                      l_insp_intg_rep_dmbtr +
                      l_insp_intg_both_dmbtr.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_geop_dmbtr ).
    IF l_temp > 1.
      l_insp_geop_dmbtr_per = l_insp_geop_dmbtr /
                                   p_result-insp_geop_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_mfl_dmbtr ).
    IF l_temp > 1.
      l_insp_mfl_dmbtr_per = l_insp_mfl_dmbtr /
                       p_result-insp_mfl_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_both_dmbtr ).
    IF l_temp > 1.
      l_insp_both_dmbtr_per = l_insp_both_dmbtr /
                        p_result-insp_both_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-intg_rep_dmbtr ).
    IF l_temp > 1.
      l_intg_rep_dmbtr_per = l_intg_rep_dmbtr /
                       p_result-intg_rep_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-intg_intg_dmbtr ).
    IF l_temp > 1.
      l_intg_intg_dmbtr_per = l_intg_intg_dmbtr /
                       p_result-intg_intg_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_geop_dmbtr ).
    IF l_temp > 1.
      l_insp_intg_geop_dmbtr_per =
                             l_insp_intg_geop_dmbtr /
                             p_result-insp_intg_geop_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_mfl_dmbtr ).
    IF l_temp > 1.
      l_insp_intg_mfl_dmbe2_per =
                            l_insp_intg_mfl_dmbtr /
                            p_result-insp_intg_mfl_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_rep_dmbtr ).
    IF l_temp > 1.
      l_insp_intg_rep_dmbe2_per =
                            l_insp_intg_rep_dmbtr /
                            p_result-insp_intg_rep_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_intg_dmbtr ).
    IF l_temp > 1.
      l_insp_intg_intg_dmbtr_per =
                            l_insp_intg_intg_dmbtr /
                            p_result-insp_intg_intg_dmbtr * 100.
    ENDIF.
    CLEAR: l_temp.
    l_temp = abs( p_result-insp_intg_both_dmbtr ).
    IF l_temp > 1.
      l_insp_intg_both_dmbtr_per =
                             l_insp_intg_both_dmbtr /
                             p_result-insp_intg_both_dmbtr * 100.
    ENDIF.

    l_total_rev = p_result-insp_geop_dmbtr +
                  p_result-insp_mfl_dmbtr +
                  p_result-insp_both_dmbtr +
                  p_result-intg_rep_dmbtr +
                  p_result-intg_intg_dmbtr +
                  p_result-insp_intg_geop_dmbtr +
                  p_result-insp_intg_mfl_dmbtr +
                  p_result-insp_intg_rep_dmbtr +
                  p_result-insp_intg_intg_dmbtr +
                  p_result-insp_intg_both_dmbtr.
    CLEAR: l_temp.
    l_temp = abs( l_total_rev ).
    IF l_temp > 1.
      l_total_dmbtr_per = l_total_dmbtr / l_total_rev * 100.
    ENDIF.

    IF ( NOT p_zero IS INITIAL ) OR
       ( p_zero IS INITIAL AND
         ( l_insp_geop_dmbtr <> 0 OR
           l_insp_mfl_dmbtr <> 0 OR
           l_insp_both_dmbtr <> 0 OR
           l_intg_intg_dmbtr <> 0 OR
           l_intg_rep_dmbtr <> 0 OR
           l_insp_intg_geop_dmbtr <> 0 OR
           l_insp_intg_mfl_dmbtr <> 0 OR
           l_insp_intg_both_dmbtr <> 0 OR
           l_insp_intg_intg_dmbtr <> 0 OR
           l_insp_intg_rep_dmbtr <> 0 OR
           l_total_dmbtr <> 0 ) ).

      IF l_state = c_close.
        mac_print_group7 l_gl_descript.
      ELSE.
        mac_print_group8 l_gl_descript.
      ENDIF.
      mac_print_1 l_insp_geop_dmbtr
                  l_insp_geop_dmbtr_per
                  l_insp_mfl_dmbtr
                  l_insp_mfl_dmbtr_per
                  l_insp_both_dmbtr
                  l_insp_both_dmbtr_per
                  l_intg_intg_dmbtr
                  l_intg_intg_dmbtr_per.

      mac_print_2 l_intg_rep_dmbtr
                  l_intg_rep_dmbtr_per
                  l_insp_intg_geop_dmbtr
                  l_insp_intg_geop_dmbtr_per
                  l_insp_intg_mfl_dmbtr
                  l_insp_intg_mfl_dmbtr_per
                  l_insp_intg_both_dmbtr
                  l_insp_intg_both_dmbtr_per.

      mac_print_3 l_insp_intg_intg_dmbtr
                  l_insp_intg_intg_dmbtr_per
                  l_insp_intg_rep_dmbtr
                  l_insp_intg_rep_dmbtr_per
                  l_total_dmbtr
                  l_total_dmbtr_per.
      HIDE: g_gl.
    ENDIF.
  ENDIF.
  IF l_state = c_open.
    PERFORM write_document_detail.
  ENDIF.

ENDFORM.                    " WRITE_GL_DETAILS



*&---------------------------------------------------------------------*
*&      Form  GET_PRODUCT_LINE_RANGE
*&---------------------------------------------------------------------*
*&  Put product line code for Inspection, Integrity, Inspection &
*&  integrity combined into a selection range for record selection use
*&---------------------------------------------------------------------*
FORM get_product_line_range.

  r_prod_line-sign = 'I'.
  r_prod_line-option = 'BT'.
  r_prod_line-low = 'Z01'.
  r_prod_line-high = 'Z03'.
  APPEND r_prod_line.

ENDFORM.                    " GET_PRODUCT_LINE_RANGE

*&---------------------------------------------------------------------*
*&      Form  ARRANGE_DATA
*&---------------------------------------------------------------------*
*&  according to Produect Line and Job Type put the amount into
*&  corresponding column of the internal table for report display use
*&---------------------------------------------------------------------*
FORM arrange_data.

  LOOP AT it_result.
    IF it_result-rev_cost_flg = 'R'.
      it_result-dmbtr = 0 - it_result-dmbtr.
      it_result-dmbe2 = 0 - it_result-dmbe2.
    ENDIF.

    IF it_result-kvgr2 = c_inspection.
      IF it_result-kvgr1 = c_geopig.
        it_result-insp_geop_dmbtr = it_result-dmbtr.
        it_result-insp_geop_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_mfl.
        it_result-insp_mfl_dmbtr = it_result-dmbtr.
        it_result-insp_mfl_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_geop_mfl.
        it_result-insp_both_dmbtr = it_result-dmbtr.
        it_result-insp_both_dmbe2 = it_result-dmbe2.
      ENDIF.
    ELSEIF it_result-kvgr2 = c_integrity.
      IF it_result-kvgr1 = c_repair.
        it_result-intg_rep_dmbtr = it_result-dmbtr.
        it_result-intg_rep_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_integ.
        it_result-intg_intg_dmbtr = it_result-dmbtr.
        it_result-intg_intg_dmbe2 = it_result-dmbe2.
      ENDIF.
    ELSEIF it_result-kvgr2 = c_insp_intg.
      IF it_result-kvgr1 = c_geopig.
        it_result-insp_intg_geop_dmbtr = it_result-dmbtr.
        it_result-insp_intg_geop_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_mfl.
        it_result-insp_intg_mfl_dmbtr = it_result-dmbtr.
        it_result-insp_intg_mfl_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_geop_mfl.
        it_result-insp_intg_both_dmbtr = it_result-dmbtr.
        it_result-insp_intg_both_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_repair.
        it_result-insp_intg_rep_dmbtr = it_result-dmbtr.
        it_result-insp_intg_rep_dmbe2 = it_result-dmbe2.
      ELSEIF it_result-kvgr1 = c_integ.
        it_result-insp_intg_intg_dmbtr = it_result-dmbtr.
        it_result-insp_intg_intg_dmbe2 = it_result-dmbe2.
      ENDIF.
    ENDIF.
    MODIFY it_result.
  ENDLOOP.

ENDFORM.                    " ARRANGE_DATA

*&---------------------------------------------------------------------*
*&      Form  GET_TOTAL_REVENUE
*&---------------------------------------------------------------------*
*& Calculate the total Revenue for each Job Type in each product line
*&---------------------------------------------------------------------*
FORM get_total_revenue USING    p_bukrs
                       CHANGING p_temp STRUCTURE it_result.

  DATA: l_result TYPE t_result OCCURS 0 WITH HEADER LINE.
  l_result[] = it_result[].

  LOOP AT l_result WHERE rev_cost_flg = 'R'.
    IF l_result-kvgr2 = c_inspection.
      IF l_result-kvgr1 = c_geopig.
        p_temp-insp_geop_dmbtr = p_temp-insp_geop_dmbtr +
                                  l_result-insp_geop_dmbtr.
        p_temp-insp_geop_dmbe2 = p_temp-insp_geop_dmbe2 +
                                  l_result-insp_geop_dmbe2.
      ELSEIF l_result-kvgr1 = c_mfl.
        p_temp-insp_mfl_dmbtr = p_temp-insp_mfl_dmbtr +
                                  l_result-insp_mfl_dmbtr.
        p_temp-insp_mfl_dmbe2 = p_temp-insp_mfl_dmbe2 +
                                  l_result-insp_mfl_dmbe2.
      ELSEIF l_result-kvgr1 = c_geop_mfl.
        p_temp-insp_both_dmbtr = p_temp-insp_both_dmbtr +
                                  l_result-insp_both_dmbtr.
        p_temp-insp_both_dmbe2 = p_temp-insp_both_dmbe2 +
                                  l_result-insp_both_dmbe2.
      ENDIF.
    ELSEIF l_result-kvgr2 = c_integrity.
      IF l_result-kvgr1 = c_repair.
        p_temp-intg_rep_dmbtr = p_temp-intg_rep_dmbtr +
                                  l_result-intg_rep_dmbtr.
        p_temp-intg_rep_dmbe2 = p_temp-intg_rep_dmbe2 +
                                  l_result-intg_rep_dmbe2.
      ENDIF.
    ELSEIF l_result-kvgr2 = c_insp_intg.
      IF l_result-kvgr1 = c_geopig.
        p_temp-insp_intg_geop_dmbtr = p_temp-insp_intg_geop_dmbtr +
                                       l_result-insp_intg_geop_dmbtr.
        p_temp-insp_intg_geop_dmbe2 = p_temp-insp_intg_geop_dmbe2 +
                                       l_result-insp_intg_geop_dmbe2.
      ELSEIF l_result-kvgr1 = c_mfl.
        p_temp-insp_intg_mfl_dmbtr = p_temp-insp_intg_mfl_dmbtr +
                                      l_result-insp_intg_mfl_dmbtr.
        p_temp-insp_intg_mfl_dmbe2 = p_temp-insp_intg_mfl_dmbe2 +
                                      l_result-insp_intg_mfl_dmbe2.
      ELSEIF l_result-kvgr1 = c_geop_mfl.
        p_temp-insp_intg_both_dmbtr = p_temp-insp_intg_both_dmbtr +
                                      l_result-insp_intg_both_dmbtr.
        p_temp-insp_intg_both_dmbe2 = p_temp-insp_intg_both_dmbe2 +
                                      l_result-insp_intg_both_dmbe2.
      ENDIF.
    ENDIF.

  ENDLOOP.
ENDFORM.                    " GET_TOTAL_REVENUE


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_SELECTION_OPTIONS
*&---------------------------------------------------------------------*

FORM display_selection_options.

  RESERVE 19 LINES.

  SKIP 2.
  WRITE: /1(160) '----------- Selection Criteria -----------' CENTERED.

  mac_select_options_display2 s_bukrs 'Company          :' 'All' 50.
  mac_select_options_display2 s_perio 'Fiscal Period    :' 'All' 50.
  mac_select_options_display2 s_wwctr 'Contract         :' 'All' 50.
  mac_select_options_display2 s_land1 'Country          :' 'All' 50.
  mac_select_options_display2 s_kaufn 'Order Number     :' 'All' 50.
  mac_select_options_display2 s_kndnr 'Customer         :' 'All' 50.

  IF p_zero IS INITIAL.
    WRITE: / 'Show Zero Balance G/L Account:', ' ON'.
  ELSE.
    WRITE: / 'Show Zero Balance G/L Account:', ' OFF'.
  ENDIF.

ENDFORM.                    " DISPLAY_SELECTION_OPTIONS

*&---------------------------------------------------------------------*
*&      Form  EXPAND_ALL
*&---------------------------------------------------------------------*
*& set all of the summary record's state to open
*&---------------------------------------------------------------------*
FORM expand_all.

  LOOP AT it_summary.
    it_summary-state = c_open.
    MODIFY it_summary.
  ENDLOOP.

  LOOP AT it_result.
    it_result-state = c_open.
    MODIFY it_result.
  ENDLOOP.
ENDFORM.                    " EXPAND_ALL

*&---------------------------------------------------------------------*
*&      Form  COLLAPSE_ALL
*&---------------------------------------------------------------------*
*& set all of the summary record's state to close
*&---------------------------------------------------------------------*
FORM collapse_all.
  LOOP AT it_summary.
    it_summary-state = c_close.
    MODIFY it_summary.
  ENDLOOP.

  LOOP AT it_result.
    it_result-state = c_close.
    MODIFY it_result.
  ENDLOOP.
ENDFORM.                    " COLLAPSE_ALL

*&---------------------------------------------------------------------*
*&      Form  GET_ALL_GL_ACCT
*&---------------------------------------------------------------------*
*&  Get all of the GL accounts for each Revenue or Direct Cost even they
*&  do not have a balance
*&---------------------------------------------------------------------*
FORM get_all_gl_acct.
  DATA: l_it_gl LIKE ska1 OCCURS 0 WITH HEADER LINE,
        l_gl LIKE it_result-hkont,
        l_gl_range LIKE zfrev_cost_defin OCCURS 0 WITH HEADER LINE,
        l_temp LIKE zfrev_cost_defin OCCURS 0 WITH HEADER LINE,
        wa_gl_range LIKE zfrev_cost_defin.

  CLEAR: l_gl_range.
  REFRESH: l_gl_range.

* get all of the Revenue and Direct Cost definitions
  SELECT * FROM zfrev_cost_defin INTO TABLE l_gl_range.
  l_temp[] = l_gl_range[].
  READ TABLE s_bukrs INDEX 1.

  LOOP AT l_gl_range.

*   if the definition is a single value, check if this gl account
*   is already in our internal table.  If not, add it.
    IF l_gl_range-options = 'EQ'.
      READ TABLE it_result WITH KEY hkont = l_gl_range-acct_low.
      IF sy-subrc <> 0.
        CLEAR: it_result.
        it_result-rev_cost_flg = l_gl_range-id+0(1).
        it_result-rev_cost_id = l_gl_range-id.
        it_result-hkont = l_gl_range-acct_low.
        it_result-bukrs = s_bukrs-low.
        it_result-state = c_close.
        APPEND it_result.
      ENDIF.
    ELSE.

*     if the definition is a range of values, get all of the GL account
*     between this range.
      CLEAR: l_it_gl.
      REFRESH: l_it_gl.
      SELECT * FROM ska1 INTO TABLE l_it_gl
        WHERE ktopl = c_nwsl
        AND saknr >= l_gl_range-acct_low
        AND saknr <= l_gl_range-acct_high.

      LOOP AT l_it_gl.
*      check each of the GL account number in this range to see if we
*      already have it in the internal table.  If it is not in the
*      internal table, check the Revenue and Cost definition table to
*      see if it is a single value entry for a different Revenue or
*      direct cost.  If not, add it to the internal table.  If it is
*      a single value entry in the definition table, it should be taken
*      care of when we check for the single values.
        READ TABLE it_result WITH KEY hkont = l_it_gl-saknr.
        IF sy-subrc <> 0.
          CLEAR: wa_gl_range, l_temp.
          READ TABLE l_temp WITH KEY acct_low = l_it_gl-saknr
                                     options = 'EQ'.
          IF sy-subrc <> 0.
            CLEAR: it_result.
            it_result-rev_cost_flg = l_gl_range-id+0(1).
            it_result-rev_cost_id = l_gl_range-id.
            it_result-hkont = l_it_gl-saknr.
            it_result-bukrs = s_bukrs-low.
            it_result-state = c_close.
            APPEND it_result.
          ENDIF.
        ENDIF.
      ENDLOOP.
    ENDIF.

  ENDLOOP.


ENDFORM.                    " GET_ALL_GL_ACCT

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_EXCEL
*&---------------------------------------------------------------------*
*& if the debug flag is on, download the detailed internal table to
*& Excel for testing
*&---------------------------------------------------------------------*
FORM download_excel.
  DATA: l_filename TYPE mac_t_filename.
  DATA: BEGIN OF l_title OCCURS 0,
          colm_head(30) TYPE c,
        END OF l_title.

* set header to each column
  l_title-colm_head = 'Company'.
  APPEND l_title.
  l_title-colm_head = 'Document Number'.
  APPEND l_title.
  l_title-colm_head = 'Line Number'.
  APPEND l_title.
  l_title-colm_head = 'GL Account'.
  APPEND l_title.
  l_title-colm_head = 'Order Number'.
  APPEND l_title.
  l_title-colm_head = 'Product Line'.
  APPEND l_title.
  l_title-colm_head = 'Job Type'.
  APPEND l_title.
  l_title-colm_head = 'Country'.
  APPEND l_title.
  l_title-colm_head = 'Customer'.
  APPEND l_title.
  l_title-colm_head = 'Contract'.
  APPEND l_title.
  l_title-colm_head = 'Revenue or Cost ID'.
  APPEND l_title.
  l_title-colm_head = 'Amt in USD'.
  APPEND l_title.
  l_title-colm_head = 'Amt in Local Currency'.
  APPEND l_title.

* download the table with column header
* the function will ask for the file name
  CALL FUNCTION 'DOWNLOAD'
       EXPORTING
            filetype     = 'DAT'
       IMPORTING
            act_filename = l_filename
       TABLES
            data_tab     = it_download
            fieldnames   = l_title.

ENDFORM.                    " DOWNLOAD_EXCEL

*&---------------------------------------------------------------------*
*&      Form  WRITE_DOCUMENT_DETAIL
*&---------------------------------------------------------------------*
*& write document level detail
*&---------------------------------------------------------------------*
FORM write_document_detail.
  DATA: l_amt LIKE bseg-dmbtr,
        l_doc(20) TYPE c,
        l_line(3) TYPE c,
        l_flag TYPE c.

  it_doc[] = it_result[].
  CLEAR: l_flag.

  SORT it_doc BY hkont belnr buzei.

* for the GL account that is click on, display all the documents belongs
* to this GL account.
  LOOP AT it_doc WHERE hkont = g_gl
                      AND belnr <> ''
                      AND buzei <> ''.
    CLEAR: l_amt, l_doc, l_line.
    IF g_currency = 'USD'.
      l_amt = it_doc-dmbe2.
    ELSE.
      l_amt = it_doc-dmbtr.
    ENDIF.

    SHIFT it_doc-belnr LEFT DELETING LEADING '0'.
    l_line = it_doc-buzei.
    CONCATENATE it_doc-belnr l_line INTO l_doc
      SEPARATED BY ' - '.

    mac_print_group9 l_doc.
    mac_print_1 l_amt ''
                it_doc-budat ''
                it_doc-usnam ''
                it_doc-zuonr ''.

    mac_print_2 '' '' '' ''
                '' '' '' ''.

    mac_print_3 '' '' '' '' '' ''.
    l_flag = 'X'.
    HIDE: it_doc.
    CLEAR: it_doc.
  ENDLOOP.

  IF NOT l_flag IS INITIAL.
    mac_print_vline.
  ENDIF.

ENDFORM.                    " WRITE_DOCUMENT_DETAIL