Sunday 31 July 2016

New Fiscal Year Period giving Error on Opening (JUL) - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/new-fiscal-year-period-giving-error-on.html

New Fiscal Year Period giving Error on Opening (JUL) - Oracle EBS R12

Error:

When Opening Period JUL for the New Fiscal Year the Below Error Occurs:

Invalid Combination (Unit will remain unspecified with Balance sheet accounts)
The following combination is erroneous and does not exist:
05.000.10.010.4110120201.000.00


SHRD0015: fdfkfa-FFSEGSID failed:

Solution:

The Nature of the above Account is Owner's Equity.

This Error is due to Cross Validation Rule.  The Third Segment containing Value 10 has Qualifier Enabled of Secondary Tracking. User's have booked Expenses and Revenues on the Third Segment which is Restricted in Cross Validation Rule.

Disable the Cross Validation Rule and Open the Period "OR" Include the Code Combination in Cross Validation Rule

Wednesday 27 July 2016

FSG Rowset Account Ranges Listing - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/fsg-rowset-account-ranges-lisitng.html

PLSQL Query to Get FSG Rowset Account Ranges Listing

select bb.sequence,
       bb.description,
       tt.SIGN,
       tt.SEGMENT1_LOW || '-' || SEGMENT2_LOW || '-' || SEGMENT3_LOW || '-' ||
       SEGMENT4_LOW || '-' || SEGMENT5_LOW
     
       || '-' || SEGMENT6_LOW || '-' || SEGMENT7_LOW || '-' || SEGMENT8_LOW || '-' ||
       SEGMENT9_LOW,
     
       tt.SEGMENT1_HIGH || '-' || tt.SEGMENT2_HIGH || '-' ||
       tt.SEGMENT3_HIGH || '-' || tt.SEGMENT4_HIGH || '-' ||
       tt.SEGMENT5_HIGH
     
       || '-' || tt.SEGMENT6_HIGH || '-' || tt.SEGMENT7_HIGH || '-' ||
       tt.SEGMENT8_HIGH || '-' || tt.SEGMENT9_HIGH,
     
       tt.SEGMENT1_TYPE || tt.SEGMENT2_TYPE || tt.SEGMENT3_TYPE ||
     
       tt.SEGMENT4_TYPE || tt.SEGMENT5_TYPE || tt.SEGMENT6_TYPE ||
       tt.SEGMENT7_TYPE || tt.SEGMENT8_TYPE Display

  from RG_REPORT_AXIS_CONTENTS tt, RG_REPORT_AXES_V bb

 where tt.AXIS_SET_ID = bb.axis_set_id
     
   and tt.AXIS_SEQ = bb.sequence
     
   and bb.axis_set_id = 15207

 order by bb.sequence asc

Monday 25 July 2016

How to disable Autocopy GL Batch functionality - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/how-to-disable-autocopy-gl-batch.html

How to disable Autocopy GL Batch functionality

1) One can use personalizations hide the button (help -> diagnostics -> custom code -> personalize). The button is BATCH.BATCH_AUTOCOPY.

2) Another option you have is to disable the concurrent program GLJRNAUTOC.
System Administrator > Concurrent > Program > Define
Query 'GLJRNAUTOC'.
Uncheck the Enable checkbox and Save.

The button will still be visible on the form, but when pressed an error message is received:
APP-FND-00150: You cannot submit concurrent request for disabled concurrent program GLJRNAUTOC. Contact your system administrator to enable the concurrent program.

----------------------------------------------------------------------------------------------------------------------------------

3) Use the Below Personalization to Hide AutoCopy





Tuesday 12 July 2016

To Check Reversal Periods not assigned to Auto Reversal Category (Query) - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/to-check-reversal-periods-not-assigned.html

To Check Reversal Periods not assigned to Auto Reversal Category

select * from gl_je_headers yy
where 1=1
and yy.JE_CATEGORY in ('2','35','9','41','47')
and yy.ACCRUAL_REV_PERIOD_NAME is null
and yy.REVERSED_JE_HEADER_ID is null
and (yy.RUNNING_TOTAL_DR <>0 or yy.RUNNING_TOTAL_CR <>0)

Data Transferred from XLA to GL (Query) - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/gl-tables-and-xla-tables-match-on.html

PLSQL to Get Data Transferred from XLA to GL 

select pp.Acv Account,
     
       NVL(pp.IN_XLA, 0) XLA_AE_LINES_A,
     
       NVL(mm.IN_GL_IMPORT, 0) GL_IMPOERT_REFERENCES_B,
     
       nvl(pp.IN_XLA, 0) - nvl(mm.IN_GL_IMPORT, 0) Difference_AB,
     
       NVL(cc. IN_GL_INTERFACE, 0) GL_INTERFACE_C,
     
       nvl(qq.XLA_TO_GL, 0) XLA_TO_GL_Transferred_D,
     
       nvl(mm.IN_GL_IMPORT - qq.XLA_TO_GL, 0) Difeerence_BD,
     
       NVL(bb.POSTED_IN_GL, 0) POSTED_IN_GL,
     
       NVL(ss.Reversed_in_GL, 0) Reversed_In_GL,
     
       NVL(qq.XLA_TO_GL, 0) - NVL(bb.POSTED_IN_GL, 0) UNPOSTED_IN_GL

-- ww.ala + qq.XLA_TO_GL - ww.ala IN_GL

/*,ww.ala+qq.XLA_TO_GL-ww.ala IN_GL,qq.XLA_TO_GL-ww.ala Difference,bb.fad Reversal, zz.nn Manual_ENT_in_GL , ww.ala+zz.nn Ending_balance

*/

  from (select gcc.segment4 Acv,
             
               NVL(sum(xa.ACCOUNTED_DR), 0) - NVL(sum(xa.ACCOUNTED_CR), 0) IN_XLA
       
          from xla_ae_lines xa, xla_ae_headers xe, gl_code_combinations gcc
       
        -- gl_import_references gi
       
         where 1 = 1
             
           and xa.AE_HEADER_ID = xe.AE_HEADER_ID
             
              --  and xa.GL_SL_LINK_ID = gi.GL_SL_LINK_ID
             
              ---  and xa.GL_SL_LINK_TABLE = gi.GL_SL_LINK_TABLE
             
           and gcc.CODE_COMBINATION_ID = xa.CODE_COMBINATION_ID
             
           and xa.APPLICATION_ID = xe.APPLICATION_ID
             
           and gcc.SEGMENT4 between NVL('&From_Account', gcc.SEGMENT4) and
             
               NVL('&To_Account', gcc.SEGMENT4)
             
           and xa.LEDGER_ID = NVL('&Ledger_id', xa.LEDGER_ID)
             
           and xe.GL_TRANSFER_STATUS_CODE = 'Y'
       
        --and xa.APPLICATION_ID in(260,140,222,200)
       
         group by gcc.segment4) pp,
     
       (select gcc.segment4 Account,
             
               NVL(sum(xa.ACCOUNTED_DR), 0) - NVL(sum(xa.ACCOUNTED_CR), 0) IN_GL_IMPORT
       
          from xla_ae_lines xa,
             
               xla_ae_headers xe,
             
               gl_code_combinations gcc,
             
               gl_import_references gi
       
         where 1 = 1
             
           and xa.AE_HEADER_ID = xe.AE_HEADER_ID
             
           and xa.GL_SL_LINK_ID = gi.GL_SL_LINK_ID
             
           and xa.GL_SL_LINK_TABLE = gi.GL_SL_LINK_TABLE
             
           and gcc.CODE_COMBINATION_ID = xa.CODE_COMBINATION_ID
             
           and xa.APPLICATION_ID = xe.APPLICATION_ID
             
           and gcc.SEGMENT4 between NVL('&From_Account', gcc.SEGMENT4) and
             
               NVL('&To_Account', gcc.SEGMENT4)
             
           and xa.LEDGER_ID = NVL('&Ledger_id', xa.LEDGER_ID)
             
           and xe.GL_TRANSFER_STATUS_CODE = 'Y'
       
        --and xa.APPLICATION_ID in(260,140,222,200)
       
         group by gcc.segment4) mm,
     
       (SELECT gc.SEGMENT4 Acx,
             
               NVL(sum(tt.ACCOUNTED_DR), 0) - NVL(sum(tt.ACCOUNTED_CR), 0) IN_GL_INTERFACE
       
          FROM gl_interface tt, gl_code_combinations gc
       
         WHERE tt.CODE_COMBINATION_ID = gc.CODE_COMBINATION_ID
             
           and tt.LEDGER_ID = nvl('&Ledger_id', tt.LEDGER_ID)
             
              --and USER_JE_SOURCE_NAME='Payables' AND USER_JE_CATEGORY_NAME='Purchase Invoices'
             
           AND (USER_JE_SOURCE_NAME, USER_JE_CATEGORY_NAME, LEDGER_ID,
             
                ACTUAL_FLAG, PERIOD_NAME, GL_SL_LINK_ID) NOT IN -- 413
             
               (SELECT
               
                --H.NAME,
               
                --H.DEFAULT_EFFECTIVE_DATE,
               
                --H.DESCRIPTION,
               
                --H.JE_HEADER_ID,
               
                --A.JE_HEADER_ID,
               
                 H.JE_SOURCE,
               
                 H.JE_CATEGORY,
               
                 H.LEDGER_ID,
               
                 H.ACTUAL_FLAG,
               
                 H.PERIOD_NAME,
               
                 A.GL_SL_LINK_ID
               
                --H.CODE_COMBINATION_ID,
               
                --H.ENTERED_DR,
               
                --H.ENTERED_CR
               
                  FROM (SELECT *
                       
                           FROM gl_interface
                       
                          WHERE --USER_JE_SOURCE_NAME='Payables' AND USER_JE_CATEGORY_NAME='Purchase Invoices'
                       
                          LEDGER_ID = NVL('&Ledger_id', LEDGER_ID)
                       
                       AND ACTUAL_FLAG = 'A' -- 783
                       
                         ) A,
                     
                       (SELECT H.JE_SOURCE,
                             
                               H.JE_CATEGORY,
                             
                               H.LEDGER_ID,
                             
                               H.ACTUAL_FLAG,
                             
                               H.PERIOD_NAME,
                             
                               gll.CODE_COMBINATION_ID,
                             
                               ael.ENTERED_DR,
                             
                               ael.ENTERED_CR,
                             
                               ael.gl_sl_link_id,
                             
                               H.NAME,
                             
                               H.DEFAULT_EFFECTIVE_DATE,
                             
                               H.JE_HEADER_ID,
                             
                               H.DESCRIPTION
                       
                          FROM GL_JE_HEADERS H,
                             
                               GL_JE_LINES gll,
                             
                               XLA_AE_HEADERS aeh,
                             
                               XLA_AE_LINES ael,
                             
                               XLA_EVENTS aea,
                             
                               GL_IMPORT_REFERENCES gir
                       
                         WHERE 1 = 1
                             
                           AND H.JE_HEADER_ID = gll.JE_HEADER_ID
                             
                           AND ael.gl_sl_link_id = gir.gl_sl_link_id
                             
                              -- AND gir.gl_sl_link_table in ('APECL', 'XLAJEL','XLAJEL')
                             
                              --   AND aea.application_id = 200
                             
                           AND aea.event_id = aeh.event_id
                             
                           AND aeh.ae_header_id = ael.ae_header_id
                             
                           AND gll.je_header_id = gir.je_header_id
                             
                           AND gll.je_line_num = gir.je_line_num
                             
                              -- AND H.JE_SOURCE='Payables'
                             
                              -- AND H.JE_CATEGORY='Purchase Invoices'
                             
                           AND H.LEDGER_ID = NVL('&Ledger_id', H.LEDGER_ID)
                             
                           AND H.ACTUAL_FLAG = 'A'
                       
                        --AND H.DEFAULT_EFFECTIVE_DATE='20-JUL-2009'
                       
                        --AND H.PERIOD_NAME='JUL-09'
                       
                        --AND H.DESCRIPTION LIKE '%200900160%'
                       
                        ) H
               
                 WHERE 1 = 1
                     
                   AND H.JE_SOURCE = A.USER_JE_SOURCE_NAME
                     
                   AND H.JE_CATEGORY = A.USER_JE_CATEGORY_NAME
                     
                   AND H.LEDGER_ID = A.LEDGER_ID
                     
                   AND H.ACTUAL_FLAG = A.ACTUAL_FLAG
                     
                   AND H.PERIOD_NAME = A.PERIOD_NAME
                     
                      --AND H.CODE_COMBINATION_ID=A.CODE_COMBINATION_ID
                     
                      --AND (H.ENTERED_DR=A.ENTERED_DR or H.ENTERED_CR=A.ENTERED_CR)
                     
                   AND H.GL_SL_LINK_ID = A.GL_SL_LINK_ID
               
                )
       
         group by gc.SEGMENT4) cc
     
      ,
     
       (select gcc.segment4 Acc,
             
               NVL(sum(ael.ACCOUNTED_DR), 0) - NVL(sum(ael.ACCOUNTED_CR), 0) XLA_TO_GL
       
          from gl_je_lines ln,
             
               gl_code_combinations gcc,
             
               gl_je_headers hdr,
             
               gl_import_references gir,
             
               xla.xla_transaction_entities ent,
             
               xla_ae_headers aeh,
             
               xla_ae_lines ael,
             
               fnd_user fu,
             
               gl_je_categories glc
       
         where 1 = 1
             
              -- and hdr.status = 'P'
             
              --and ln.status = 'P'
             
           and hdr.actual_flag = 'A'
             
           and ln.je_header_id = hdr.je_header_id
             
           and ln.period_name = hdr.period_name
             
           and ln.code_combination_id = gcc.code_combination_id
             
           and hdr.JE_CATEGORY = glc.je_category_name
             
           and fu.USER_ID = hdr.CREATED_BY
             
           and ln.je_header_id = gir.je_header_id
             
           and ln.je_line_num = gir.je_line_num
             
           and hdr.je_header_id = gir.je_header_id
             
           and gir.gl_sl_link_id = ael.gl_sl_link_id
             
           and gir.gl_sl_link_table = ael.gl_sl_link_table
             
           and aeh.ae_header_id = ael.ae_header_id
             
           and aeh.application_id = ael.application_id
             
           and aeh.period_name = hdr.period_name
             
           and aeh.entity_id = ent.entity_id
             
           and hdr.JE_SOURCE not in
             
               ('Manual',
                'Spreadsheet',
                'Recurring',
                'AutoCopy',
               
                'Revaluation',
                'Closing Journal')
             
           and ael.LEDGER_ID = NVL('&Ledger_id', ael.LEDGER_ID)
             
           and ln.LEDGER_ID = NVL('&Ledger_id', ln.LEDGER_ID)
             
           and gcc.SEGMENT4 between NVL('&From_Account', gcc.SEGMENT4) and
             
               NVL('&To_Account', gcc.SEGMENT4)
       
         group by gcc.segment4) qq,
     
       (select gcc.segment4 Acco,
             
               NVL(sum(ael.ACCOUNTED_DR), 0) - NVL(sum(ael.ACCOUNTED_CR), 0) POSTED_IN_GL
       
          from gl_je_lines ln,
             
               gl_code_combinations gcc,
             
               gl_je_headers hdr,
             
               gl_import_references gir,
             
               xla.xla_transaction_entities ent,
             
               xla_ae_headers aeh,
             
               xla_ae_lines ael,
             
               fnd_user fu,
             
               gl_je_categories glc
       
         where 1 = 1
             
           and hdr.status = 'P'
             
           and ln.status = 'P'
             
           and hdr.actual_flag = 'A'
             
           and ln.je_header_id = hdr.je_header_id
             
           and ln.period_name = hdr.period_name
             
           and ln.code_combination_id = gcc.code_combination_id
             
           and hdr.JE_CATEGORY = glc.je_category_name
             
           and fu.USER_ID = hdr.CREATED_BY
             
           and ln.je_header_id = gir.je_header_id
             
           and ln.je_line_num = gir.je_line_num
             
           and hdr.je_header_id = gir.je_header_id
             
           and gir.gl_sl_link_id = ael.gl_sl_link_id
             
           and gir.gl_sl_link_table = ael.gl_sl_link_table
             
           and aeh.ae_header_id = ael.ae_header_id
             
           and aeh.application_id = ael.application_id
             
           and aeh.period_name = hdr.period_name
             
           and aeh.entity_id = ent.entity_id
             
           and hdr.JE_SOURCE not in
             
               ('Manual',
                'Spreadsheet',
                'Recurring',
                'AutoCopy',
               
                'Revaluation',
                'Closing Journal')
             
           and ael.LEDGER_ID = NVL('&Ledger_id', ael.LEDGER_ID)
             
           and ln.LEDGER_ID = NVL('&Ledger_id', ln.LEDGER_ID)
             
           and gcc.SEGMENT4 between NVL('&From_Account', gcc.SEGMENT4) and
             
               NVL('&To_Account', gcc.SEGMENT4)
       
         group by gcc.segment4) bb,
     
       /*
     
       (select gg.segment4 Aco,
     
       NVL(sum(al.ACCOUNTED_DR)-sum(al.ACCOUNTED_CR), 0) ala
     
       from gl_je_lines al, gl_code_combinations gg, gl_je_headers vv
     
       where al.JE_HEADER_ID = vv.JE_HEADER_ID
     
       and gg.CODE_COMBINATION_ID = al.CODE_COMBINATION_ID
     
       -- and al.STATUS = 'P'
     
       --and vv.STATUS = 'P'
     
       and vv.ACTUAL_FLAG = 'A'
     
       and gg.SEGMENT4 between NVL('&From_Account', gg.SEGMENT4) and
     
       NVL('&To_Account', gg.SEGMENT4)
     
       and vv.LEDGER_ID = al.LEDGER_ID
     
       and vv.LEDGER_ID = '&Ledger_id'
     
                                     
     
       and vv.JE_SOURCE not in
     
       ('Manual', 'Spreadsheet', 'Recurring', 'AutoCopy',
     
       'Revaluation', 'Closing Journal')
     
       group by gg.SEGMENT4
     
                               
     
       ) ww,*/
     
       (select ll.segment4 Ack,
             
               NVL(sum(jj.ACCOUNTED_DR), 0) - NVL(sum(jj.ACCOUNTED_CR), 0) Reversed_in_GL
       
          from gl_je_lines jj, gl_code_combinations ll, gl_je_headers nn
       
         where jj.CODE_COMBINATION_ID = ll.CODE_COMBINATION_ID
             
           and jj.JE_HEADER_ID = nn.JE_HEADER_ID
             
           and jj.LEDGER_ID = NVL('&Ledger_id', jj.LEDGER_ID)
             
           and nn.ACTUAL_FLAG = 'A'
             
              --and jj.STATUS='P'and nn.STATUS='P'
             
           and ll.SEGMENT4 between NVL('&From_Account', ll.SEGMENT4) and
             
               NVL('&To_Account', ll.SEGMENT4)
             
           and nn.JE_SOURCE not in
             
               ('Manual',
                'Spreadsheet',
                'Recurring',
                'AutoCopy',
               
                'Revaluation',
                'Closing Journal')
             
           and nn.REVERSED_JE_HEADER_ID is not null
       
         group by ll.SEGMENT4) ss

 where pp.Acv = mm.Account(+)
     
      -- and qq.Acc=pp.Acv
     
      --and ww.Aco=pp.Acv
     
   and mm.Account = qq.Acc(+)
     
   and pp.Acv = cc.Acx(+)
     
      --and mm.Account = ww.Aco
     
      --  and qq.Acc = ww.Aco
     
   and bb.Acco(+) = mm.Account
     
   and mm.Account = ss.Ack(+)

Journal Totals Differ in Debit and Credit even the Journal Lines are Balances - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/journal-totals-are-showing-with-zero.html

Journal Totals Differ in Debit and Credit even the Journal Lines are Balances


Immediate workaround to correct the effected journal totals:
In the Enter Journals form, change the conversion rate (and the currency and conversion type if necessary) of the journal. Commit this change.
Change it back again to the original currency

Friday 8 July 2016

Trial Balance Complete PTD & YTD (Account Wise, Month Wise) - Oracle EBS R12

http://generalledger-gl.blogspot.com/2016/07/trial-balance-complete-ptd-ytd-account.html

Trial Balance Complete PTD & YTD (Account Wise, Month Wise)

--Ledger_ID & Fiscal_Year is Mandatory
--Fiscal_Year in which Month Falls

Currency Type = Total

select   gcc.SEGMENT4,gb.PERIOD_NAME,gb.PERIOD_YEAR,gb.PERIOD_NUM,sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR) Opening_Balance_PTD,
sum(gb.PERIOD_NET_DR) PTD_DEBIT, sum(gb.PERIOD_NET_CR) PTD_CREDIT,
NVL(hh.OPENING_BALANCE_YTD,0) OPENING_BALANCE_YTD,
/*sum(gb.PROJECT_TO_DATE_DR+gb.PERIOD_NET_DR),
sum(gb.PROJECT_TO_DATE_CR+gb.PERIOD_NET_CR),
kk.Opening_DR,kk.Opening_CR,*/
 sum(gb.PROJECT_TO_DATE_DR+gb.PERIOD_NET_DR)-NVL(kk.Opening_DR,0) YTD_DEBIT,
 sum(gb.PROJECT_TO_DATE_CR+gb.PERIOD_NET_CR)-NVL(kk.Opening_CR,0) YTD_CREDIT,
 sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
       sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR) ENDING_BALANCE     
          from gl_balances gb, gl_code_combinations gcc,
   
             (select gcc.segment4 Acc,sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
               sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR) OPENING_BALANCE_YTD
          from gl_balances          gb,
               gl_code_combinations gcc,
               FND_FLEX_VALUES_VL   FND
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           AND FND.FLEX_VALUE = GCC.SEGMENT4
           and ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = 'PKR'
           and gb.ACTUAL_FLAG = 'A'
           and gb.PERIOD_NUM = 1
           and gb.PERIOD_YEAR = '&Fiscal_Year'
           group by gcc.segment4) hh,
   
(select gb.PERIOD_NAME,gcc.SEGMENT4,
 sum(gb.PROJECT_TO_DATE_DR) Opening_DR ,sum(gb.PROJECT_TO_DATE_CR) Opening_CR     
          from gl_balances gb, gl_code_combinations gcc, gl_periods gp
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
         and gb.PERIOD_YEAR=gp.PERIOD_YEAR
         and gb.PERIOD_NUM=gp.PERIOD_NUM       
           and gb.ledger_id = '&Ledger_id'   
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = 'PKR'
           and gb.ACTUAL_FLAG = 'A'
            and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
          and gp.PERIOD_YEAR ='&Fiscal_Year'
          group by gb.PERIOD_NAME,gcc.SEGMENT4 ) kk

         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
         and kk.segment4(+)=gcc.SEGMENT4
         and hh.Acc(+)=gcc.segment4
         --and gb.PERIOD_NAME=yy.period_name
           and gb.ledger_id = '&Ledger_id'
   
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = 'PKR'
           and gb.ACTUAL_FLAG = 'A'
          and gb.PERIOD_YEAR = '&Fiscal_Year'
          and gb.PERIOD_NAME = NVL('&Period_Name',gb.PERIOD_NAME)
          group by gcc.SEGMENT4,gb.PERIOD_NAME, gb.PERIOD_year,gb.PERIOD_NUM, hh.OPENING_BALANCE_YTD,kk.Opening_DR, kk.Opening_CR
          order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc

--------------------------------------------------------------------------------------------------------------------------

Currency Type = Entered

select gcc.SEGMENT4,
       gb.PERIOD_NAME,
       gb.PERIOD_YEAR,
       gb.PERIOD_NUM,
       Decode(gb.CURRENCY_CODE,
              'PKR',
              sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
              sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)) Opening_Balance_PTD,
       Decode(gb.CURRENCY_CODE,
              'PKR',
              sum(gb.PERIOD_NET_DR_beq),
              sum(gb.PERIOD_NET_DR)) PTD_DEBIT,
       Decode(gb.CURRENCY_CODE,
              'PKR',
              sum(gb.PERIOD_NET_CR_beq),
              sum(gb.PERIOD_NET_CR)) PTD_CREDIT,
       NVL(hh.OPENING_BALANCE_YTD, 0) OPENING_BALANCE_YTD,
       /*sum(gb.PROJECT_TO_DATE_DR_beq+gb.PERIOD_NET_DR_beq),
       sum(gb.PROJECT_TO_DATE_CR_beq+gb.PERIOD_NET_CR_beq),
       kk.Opening_DR,kk.Opening_CR,*/
       Decode(gb.CURRENCY_CODE,
              'PKR',
              sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
              NVL(kk.Opening_DR, 0),
              sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
              NVL(kk.Opening_DR, 0)) YTD_DEBIT,
       Decode(gb.CURRENCY_CODE,
              'PKR',
              sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
              NVL(kk.Opening_CR, 0),
              sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
              NVL(kk.Opening_CR, 0)) YTD_CREDIT,
       Decode(gb.CURRENCY_CODE,
              'PKR',
              sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
              sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
              sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
              sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)
         
              ) ENDING_BALANCE
  from gl_balances          gb,
       gl_code_combinations gcc,
   
       (select gcc.segment4 Acc,
               Decode(gb.CURRENCY_CODE,
                      'PKR',
                      sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                      sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
                      sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                      sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)) OPENING_BALANCE_YTD
          from gl_balances          gb,
               gl_code_combinations gcc,
               FND_FLEX_VALUES_VL   FND
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           AND FND.FLEX_VALUE = GCC.SEGMENT4
           and ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
           and gb.ACTUAL_FLAG = 'A'
           and gb.PERIOD_NUM = 1
           and gb.PERIOD_YEAR = '&Fiscal_Year'
         group by gcc.segment4, gb.CURRENCY_CODE) hh,
   
       (select gb.PERIOD_NAME,
               gcc.SEGMENT4,
               Decode(gb.CURRENCY_CODE,
                      'PKR',
                      sum(gb.PROJECT_TO_DATE_DR_beq),
                      sum(gb.PROJECT_TO_DATE_DR)) Opening_DR,
               Decode(gb.CURRENCY_CODE,
                      'PKR',
                      sum(gb.PROJECT_TO_DATE_CR_beq),
                      sum(gb.PROJECT_TO_DATE_CR)) Opening_CR
          from gl_balances gb, gl_code_combinations gcc, gl_periods gp
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           and gb.PERIOD_YEAR = gp.PERIOD_YEAR
           and gb.PERIOD_NUM = gp.PERIOD_NUM
           and gb.ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
           and gb.ACTUAL_FLAG = 'A'
            and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
           and gp.PERIOD_YEAR = '&Fiscal_Year'
         group by gb.PERIOD_NAME, gcc.SEGMENT4, gb.CURRENCY_CODE) kk

 where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
   and kk.segment4(+) = gcc.SEGMENT4
   and hh.Acc(+) = gcc.segment4
      --and gb.PERIOD_NAME=yy.period_name
   and gb.ledger_id = '&Ledger_id'
 
   and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
   and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
   and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
   and gb.ACTUAL_FLAG = 'A'
   and gb.PERIOD_YEAR = '&Fiscal_Year'
   and gb.PERIOD_NAME = NVL('&Period_Name', gb.PERIOD_NAME)
 group by gcc.SEGMENT4,
          gb.PERIOD_NAME,
          gb.PERIOD_year,
          gb.PERIOD_NUM,
          gb.CURRENCY_CODE,
          hh.OPENING_BALANCE_YTD,
          kk.Opening_DR,
          kk.Opening_CR
 order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc   

--------------------------------------------------------------------------------------------------------------------------

P_Currency_Type = Entered/Total (Parameter)

select gcc.SEGMENT4,
       gb.PERIOD_NAME,
       gb.PERIOD_YEAR,
       gb.PERIOD_NUM,
       Decode('&Currency',
              '&Ledger_Currency',
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
                     'Total',
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     'Total',
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) Opening_Balance_PTD,
   
       Decode('&Currency',
              '&Ledger_Currency',
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PERIOD_NET_DR_beq),
                     'Total',
                     sum(gb.PERIOD_NET_DR),
                     sum(gb.PERIOD_NET_DR)),
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PERIOD_NET_DR),
                     'Total',
                     sum(gb.PERIOD_NET_DR_beq))) PTD_DEBIT,
   
       Decode('&Currency',
              '&Ledger_Currency',
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PERIOD_NET_CR_beq),
                     'Total',
                     sum(gb.PERIOD_NET_CR),
                     sum(gb.PERIOD_NET_CR)),
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PERIOD_NET_CR),
                     'Total',
                     sum(gb.PERIOD_NET_CR_beq))) PTD_CREDIT,
   
       NVL(hh.OPENING_BALANCE_YTD, 0) OPENING_BALANCE_YTD,
       /*sum(gb.PROJECT_TO_DATE_DR_beq+gb.PERIOD_NET_DR_beq),
              sum(gb.PROJECT_TO_DATE_CR_beq+gb.PERIOD_NET_CR_beq),
              kk.Opening_DR,kk.Opening_CR,*/
       Decode('&Currency',
              '&Ledger_Currency',
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
                     NVL(kk.Opening_DR, 0),
                     'Total',
                     sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
                     NVL(kk.Opening_DR, 0),
                     sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
                     NVL(kk.Opening_DR, 0)),
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
                     NVL(kk.Opening_DR, 0),
                     'Total',
                     sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
                     NVL(kk.Opening_DR, 0))) YTD_DEBIT,
   
       Decode('&Currency',
              '&Ledger_Currency',
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
                     NVL(kk.Opening_CR, 0),
                     'Total',
                     sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
                     NVL(kk.Opening_CR, 0),
                     sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
                     NVL(kk.Opening_CR, 0)),
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
                     NVL(kk.Opening_CR, 0),
                     'Total',
                     sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
                     NVL(kk.Opening_CR, 0))) YTD_CREDIT,
   
       Decode('&Currency',
              '&Ledger_Currency',
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
                     'Total',
                     sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
              Decode('&P_Currency_type',
                     'Entered',
                     sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     'Total',
                     sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) ENDING_BALANCE
  from gl_balances          gb,
       gl_code_combinations gcc,
   
       (select gcc.segment4 Acc,
           
               Decode('&Currency',
                      '&Ledger_Currency',
                      Decode('&P_Currency_type',
                             'Entered',
                             sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                             sum(gb.BEGIN_BALANCE_DR_beq -
                                 gb.BEGIN_BALANCE_CR_beq),
                             'Total',
                             sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                             sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                             sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                             sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
                      Decode('&P_Currency_type',
                             'Entered',
                             sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                             sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                             'Total',
                             sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                             sum(gb.BEGIN_BALANCE_DR_beq -
                                 gb.BEGIN_BALANCE_CR_beq))) OPENING_BALANCE_YTD
   
          from gl_balances          gb,
               gl_code_combinations gcc,
               FND_FLEX_VALUES_VL   FND
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           AND FND.FLEX_VALUE = GCC.SEGMENT4
           and ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
           and gb.ACTUAL_FLAG = 'A'
           and gb.PERIOD_NUM = 1
           and gb.PERIOD_YEAR = '&Fiscal_Year'
         group by gcc.segment4, gb.CURRENCY_CODE) hh,
   
       (select gb.PERIOD_NAME,
               gcc.SEGMENT4,
               Decode('&Currency',
                      '&Ledger_Currency',
                      Decode('&P_Currency_type',
                             'Entered',
                             sum(gb.PROJECT_TO_DATE_DR_beq),
                             'Total',
                             sum(gb.PROJECT_TO_DATE_DR),
                             sum(gb.PROJECT_TO_DATE_DR)),
                      Decode('&P_Currency_type',
                             'Entered',
                             sum(gb.PROJECT_TO_DATE_DR),
                             'Total',
                             sum(gb.PROJECT_TO_DATE_DR_beq))) Opening_DR,
               Decode('&Currency',
                      '&Ledger_Currency',
                      Decode('&P_Currency_type',
                             'Entered',
                             sum(gb.PROJECT_TO_DATE_CR_beq),
                             'Total',
                             sum(gb.PROJECT_TO_DATE_CR),
                             sum(gb.PROJECT_TO_DATE_CR)),
                      Decode('&P_Currency_type',
                             'Entered',
                             sum(gb.PROJECT_TO_DATE_CR),
                             'Total',
                             sum(gb.PROJECT_TO_DATE_CR_beq))) Opening_CR
          from gl_balances gb, gl_code_combinations gcc, gl_periods gp
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           and gb.PERIOD_YEAR = gp.PERIOD_YEAR
           and gb.PERIOD_NUM = gp.PERIOD_NUM
           and gb.ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
           and gb.ACTUAL_FLAG = 'A'
           and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
           and gp.PERIOD_YEAR = '&Fiscal_Year'
         group by gb.PERIOD_NAME, gcc.SEGMENT4, gb.CURRENCY_CODE) kk

 where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
   and kk.segment4(+) = gcc.SEGMENT4
   and hh.Acc(+) = gcc.segment4
      --and gb.PERIOD_NAME=yy.period_name
   and gb.ledger_id = '&Ledger_id'
 
   and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
   and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
   and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
   and gb.ACTUAL_FLAG = 'A'
   and gb.PERIOD_YEAR = '&Fiscal_Year'
   and gb.PERIOD_NAME = NVL('&Period_Name', gb.PERIOD_NAME)
 group by gcc.SEGMENT4,
          gb.PERIOD_NAME,
          gb.PERIOD_year,
          gb.PERIOD_NUM,
          gb.CURRENCY_CODE,
          hh.OPENING_BALANCE_YTD,
          kk.Opening_DR,
          kk.Opening_CR
 order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc

--------------------------------------------------------------------------------------------------------------------------

Currency Type = E/T/C (Parameter)

select gcc.SEGMENT4,
       gb.PERIOD_NAME,
       gb.PERIOD_YEAR,
       gb.PERIOD_NUM,
       Decode(ga.CURRENCY_CODE,'&Currency',
              Decode('&Currency_type',
                     'E',
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
                     'T',
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
              Decode('&Currency_type',
                     'E',
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     'C',
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) Opening_Balance_PTD,
   
    Decode(ga.CURRENCY_CODE,'&Currency',
              Decode('&Currency_type',
                     'E',
                     sum(gb.PERIOD_NET_DR_beq),
                     'T',
                     sum(gb.PERIOD_NET_DR),
                     sum(gb.PERIOD_NET_DR)),
              Decode('&Currency_type',
                     'E',
                     sum(gb.PERIOD_NET_DR),
                     'C',
                     sum(gb.PERIOD_NET_DR_beq))) PTD_DEBIT,
   
       Decode(ga.CURRENCY_CODE,'&Currency',
              Decode('&Currency_type',
                     'E',
                     sum(gb.PERIOD_NET_CR_beq),
                     'T',
                     sum(gb.PERIOD_NET_CR),
                     sum(gb.PERIOD_NET_CR)),
              Decode('&Currency_type',
                     'E',
                     sum(gb.PERIOD_NET_CR),
                     'C',
                     sum(gb.PERIOD_NET_CR_beq))) PTD_CREDIT,
   
       NVL(hh.OPENING_BALANCE_YTD, 0) OPENING_BALANCE_YTD,
       /*sum(gb.PROJECT_TO_DATE_DR_beq+gb.PERIOD_NET_DR_beq),
              sum(gb.PROJECT_TO_DATE_CR_beq+gb.PERIOD_NET_CR_beq),
              kk.Opening_DR,kk.Opening_CR,*/
      Decode(ga.CURRENCY_CODE,'&Currency',
              Decode('&Currency_type',
                     'E',
                     sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
                     NVL(kk.Opening_DR, 0),
                     'T',
                     sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
                     NVL(kk.Opening_DR, 0),
                     sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
                     NVL(kk.Opening_DR, 0)),
              Decode('&Currency_type',
                     'E',
                     sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
                     NVL(kk.Opening_DR, 0),
                     'C',
                     sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
                     NVL(kk.Opening_DR, 0))) YTD_DEBIT,
   
       Decode(ga.CURRENCY_CODE,'&Currency',
              Decode('&Currency_type',
                     'E',
                     sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
                     NVL(kk.Opening_CR, 0),
                     'T',
                     sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
                     NVL(kk.Opening_CR, 0),
                     sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
                     NVL(kk.Opening_CR, 0)),
              Decode('&Currency_type',
                     'E',
                     sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
                     NVL(kk.Opening_CR, 0),
                     'C',
                     sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
                     NVL(kk.Opening_CR, 0))) YTD_CREDIT,
   
       Decode(ga.CURRENCY_CODE,'&Currency',
              Decode('&Currency_type',
                     'E',
                     sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
                     'T',
                     sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
              Decode('&Currency_type',
                     'E',
                     sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                     sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                     'C',
                     sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                     sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) ENDING_BALANCE
  from gl_balances          gb,
       gl_code_combinations gcc,
       gl_ledgers ga,
   
       (select gcc.segment4 Acc,
           
               Decode(ga.CURRENCY_CODE,'&Currency',
                      Decode('&Currency_type',
                             'E',
                             sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                             sum(gb.BEGIN_BALANCE_DR_beq -
                                 gb.BEGIN_BALANCE_CR_beq),
                             'T',
                             sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                             sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                             sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                             sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
                      Decode('&Currency_type',
                             'E',
                             sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
                             sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
                             'C',
                             sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
                             sum(gb.BEGIN_BALANCE_DR_beq -
                                 gb.BEGIN_BALANCE_CR_beq))) OPENING_BALANCE_YTD
   
          from gl_balances          gb,
               gl_code_combinations gcc,
               FND_FLEX_VALUES_VL   FND,
               gl_ledgers ga
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           AND FND.FLEX_VALUE = GCC.SEGMENT4
           and ga.LEDGER_ID=gb.LEDGER_ID
           and gcc.CHART_OF_ACCOUNTS_ID=ga.CHART_OF_ACCOUNTS_ID
           and gb.ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = '&Currency'
           and gb.ACTUAL_FLAG = 'A'
           and gb.PERIOD_NUM = 1
           and gb.PERIOD_YEAR = '&Fiscal_Year'
         group by gcc.segment4, ga.CURRENCY_CODE) hh,
   
       (select gb.PERIOD_NAME,
               gcc.SEGMENT4,
              Decode(ga.CURRENCY_CODE,'&Currency',
                      Decode('&Currency_type',
                             'E',
                             sum(gb.PROJECT_TO_DATE_DR_beq),
                             'T',
                             sum(gb.PROJECT_TO_DATE_DR),
                             sum(gb.PROJECT_TO_DATE_DR)),
                      Decode('&Currency_type',
                             'E',
                             sum(gb.PROJECT_TO_DATE_DR),
                             'C',
                             sum(gb.PROJECT_TO_DATE_DR_beq))) Opening_DR,
              Decode(ga.CURRENCY_CODE,'&Currency',
                      Decode('&Currency_type',
                             'E',
                             sum(gb.PROJECT_TO_DATE_CR_beq),
                             'T',
                             sum(gb.PROJECT_TO_DATE_CR),
                             sum(gb.PROJECT_TO_DATE_CR)),
                      Decode('&Currency_type',
                             'E',
                             sum(gb.PROJECT_TO_DATE_CR),
                             'C',
                             sum(gb.PROJECT_TO_DATE_CR_beq))) Opening_CR
          from gl_balances gb, gl_code_combinations gcc, gl_periods gp, gl_ledgers ga
         where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
           and gb.PERIOD_YEAR = gp.PERIOD_YEAR
           and gb.PERIOD_NUM = gp.PERIOD_NUM
           and ga.LEDGER_ID=gb.LEDGER_ID
           and gcc.CHART_OF_ACCOUNTS_ID=ga.CHART_OF_ACCOUNTS_ID
           and gb.ledger_id = '&Ledger_id'
           and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
           and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
           and gb.CURRENCY_CODE = '&Currency'
           and gb.ACTUAL_FLAG = 'A'
           and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
           and gp.PERIOD_YEAR = '&Fiscal_Year'
         group by gb.PERIOD_NAME, gcc.SEGMENT4, ga.CURRENCY_CODE) kk

 where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
   and kk.segment4(+) = gcc.SEGMENT4
   and hh.Acc(+) = gcc.segment4
   and ga.LEDGER_ID=gb.LEDGER_ID
   and gcc.CHART_OF_ACCOUNTS_ID=ga.CHART_OF_ACCOUNTS_ID
      --and gb.PERIOD_NAME=yy.period_name
   and gb.ledger_id = '&Ledger_id'
 
   and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
   and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
   and gb.CURRENCY_CODE = '&Currency'
   and gb.ACTUAL_FLAG = 'A'
   and gb.PERIOD_YEAR = '&Fiscal_Year'
   and gb.PERIOD_NAME = NVL('&Period_Name', gb.PERIOD_NAME)
 group by gcc.SEGMENT4,
          gb.PERIOD_NAME,
          gb.PERIOD_year,
          gb.PERIOD_NUM,
          ga.CURRENCY_CODE,
          hh.OPENING_BALANCE_YTD,
          kk.Opening_DR,
          kk.Opening_CR
 order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc