Monday, 5 October 2020

Oracle GL Subledger Drill Down Query

 WITH 

entity_sec_hrchy as(

select regexp_substr (val, '[^|]+', 1, 1)   level0

      ,regexp_substr (val, '[^|]+', 1, 2)   level1

  from (select ltrim(sys_connect_by_path(pk1_start_value, '|'), '|') val

          from (select ftn.pk1_start_value

                      ,ftn.parent_pk1_value

                  from fnd_tree_node        ftn

                      ,fnd_tree_version_tl  ftvt

                 where 1                         = 1

                   and ftn.tree_version_id       = ftvt.tree_version_id

                   and ftvt.tree_version_name    = 'XX_ENTITY_SECURITY_HIERARCHY'

                   and ftvt.tree_code            = 'XX_ENTITY_SECURITY_HIERARCHY'

                   and ftvt.tree_structure_code  = 'GL_ACCT_FLEX'

                   and ftvt.language             = 'US')

         where connect_by_isleaf  = 1

    start with parent_pk1_value is null

    connect by nocycle parent_pk1_value = prior pk1_start_value)

),

data_acc_set as(

select gllv.ledger_id

      ,gllv.ledger_name

      ,entity_sec_hrchy.level0

      ,entity_sec_hrchy.level1      flex_segment_value

      ,gllv.legal_entity_id

      ,gllv.legal_entity_name

      ,null                         ledger_set_name

  from entity_sec_hrchy

      ,gl_legal_entities_bsvs         gleb

      ,gl_ledger_le_v                 gllv

      ,fun_user_role_data_asgnmnts    furda

      ,gl_access_set_norm_assign      gasna

 where 1 = 1

   and gllv.legal_entity_id       = gleb.legal_entity_id

   and gleb.flex_segment_value    = entity_sec_hrchy.level1

   and furda.user_guid            = FND_GLOBAL.USER_GUID

   and gasna.access_set_id        = furda.access_set_id

   and furda.active_flag          = 'Y'

   and trunc(sysdate) between trunc(start_date_active) and trunc(nvl(end_date_active,sysdate + 1))

   and gasna.segment_value        = entity_sec_hrchy.level0

union all

select distinct gl.ledger_id

      ,gllv.ledger_name

      ,null

      ,gleb.flex_segment_value

      ,gllv.legal_entity_id

      ,gllv.legal_entity_name

      ,null                         ledger_set_name

  from fun_user_role_data_asgnmnts    furda

      ,gl_access_set_norm_assign      gasa

      ,gl_ledgers                     gl

      ,gl_ledger_le_v                 gllv

      ,gl_legal_entities_bsvs         gleb

      ,gl_access_sets                 gas

 where furda.user_guid            = FND_GLOBAL.USER_GUID

   and gasa.access_set_id         = furda.access_set_id

   and furda.active_flag          = 'Y'

   and trunc(sysdate) between trunc(start_date_active) and trunc(nvl(end_date_active,sysdate + 1))

   and gasa.ledger_id             = gl.ledger_id

   and gl.object_type_code        = 'L'

   and gllv.legal_entity_id       = gleb.legal_entity_id

   and gasa.access_set_id         = gas.access_set_id

   and gas.security_segment_code  = 'F'

   and gleb.legal_entity_id       = gllv.legal_entity_id

   and gllv.ledger_id             = gasa.ledger_id

union all

select distinct gls1.ledger_id

      ,gls1.name

      ,null

      ,gleb.flex_segment_value

      ,gllv.legal_entity_id

      ,gllv.legal_entity_name

      ,gl.name                     ledger_set_name

  from fun_user_role_data_asgnmnts    furda

      ,gl_access_set_norm_assign      gasa

      ,gl_ledgers                     gl

      ,gl_ledger_set_assignments      glsa

      ,gl_ledgers                     gls1

      ,gl_legal_entities_bsvs         gleb

      ,gl_access_sets                 gas

      ,gl_ledger_le_v                 gllv

 where furda.user_guid             = FND_GLOBAL.USER_GUID

   and gasa.access_set_id          = furda.access_set_id

   and furda.active_flag           = 'Y'

   and trunc(sysdate) between trunc(start_date_active) and trunc(nvl(end_date_active,sysdate + 1))

   and gasa.ledger_id              = gl.ledger_id

   and gasa.all_segment_value_flag = 'Y'

   and gl.ledger_id                = glsa.ledger_set_id

   and gls1.ledger_id              = glsa.ledger_id

   and gasa.access_set_id          = gas.access_set_id

   and gas.security_segment_code   = 'F'

   and gleb.legal_entity_id        = gllv.legal_entity_id

   and gllv.ledger_id              = gls1.ledger_id

),

DYN_GL_XX_DATA AS

  (SELECT glg.NAME LEDGER,

    gjh.period_name PERIOD,

    gjh.je_source,

    gjh.je_category,

    gjh.doc_sequence_value DOC_SEQ,

    TO_CHAR(gjh.default_effective_date, 'DD-MON-YYYY','nls_date_language=american') ACCOUNTING_DATE,

    TO_CHAR(gjh.posted_date, 'DD-MON-YYYY','nls_date_language=american') POSTED_DATE,

data_acc_set.flex_segment_value accounting_entity,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 1, data_acc_set.flex_segment_value) accounting_entity_desc,

    gcc.segment2 cost_centre,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 2, gcc.segment2) cost_centre_desc,

    gcc.segment3 nominal_account,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 3, gcc.segment3) nominal_account_desc,

    gcc.segment4 sub_analysis,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 4, gcc.segment4) sub_analysis_desc,

    gcc.segment5 intercompany,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 5, gcc.segment5) intercompany_desc,

    gcc.segment6 location,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 6, gcc.segment6) location_desc,

    gcc.segment7 project,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 7, gcc.segment7) project_desc,

    gcc.segment8 flow,

    gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 8, gcc.segment8) flow_desc,

gcc.segment9 spare1,

gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 9, gcc.segment9) spare1_desc,

gcc.segment10 spare2,

gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 10, gcc.segment10) spare2_desc,

gcc.code_combination_id,

    gjl.currency_code ENT_CUR,

    gjl.entered_dr ENT_DR,

    gjl.entered_cr ENT_CR,

    NVL(gjl.entered_dr,0)-NVL(gjl.entered_cr,0) ENT_NET,

    gjl.accounted_dr ACCTD_DR,

    gjl.accounted_cr ACCTD_CR,

    NVL(gjl.accounted_dr,0)-NVL(gjl.accounted_cr,0) ACCTD_NET,

    xal.accounting_class_code ACTCLS_CODE,

    xal.entered_dr XENTERED_DR,

    xal.entered_cr XENTERED_CR,

    xal.accounted_dr XACCOUNTED_DR,

    xal.accounted_cr XACCOUNTED_CR,

    nvl(xal.currency_conversion_type,gjl.currency_conversion_type) CUR_CONV_TYPE,

    TO_CHAR(nvl(xal.currency_conversion_date,gjl.currency_conversion_date), 'DD-MON-YYYY','nls_date_language=american') CUR_CONV_DATE,

    nvl(xal.currency_conversion_rate,gjl.currency_conversion_rate) CUR_CONV_RATE,

    gjb.name BATCH,

    gjb.description BATCH_DESC,

    gjh.name JOUR_NAME,

    gjh.description JOUR_DESC,

    gjl.description JOURLINE_DESC,

    gjh.created_by JOUR_PREP,

(SELECT user_id FROM gl_je_action_log WHERE je_batch_id = gjb.je_batch_id AND action_code = 'POSTED') POSTED_BY,

fsv_acc_seq.header_name      ACC_SEQUENCE_NAME,

    gjh.posting_acct_seq_value   ACC_SEQUENCE_NUMBER,

    fsv_rep_seq.header_name      REP_SEQUENCE_NAME,

    gjh.close_acct_seq_value     REP_SEQUENCE_NUMBER,

gjl.je_line_num              JOURNAL_LINE_NUMBER,

(SELECT display_name FROM per_person_names_f WHERE person_id = gjb.approver_employee_id AND name_type = 'GLOBAL') JOUR_APPR,

    --DECODE(gjh.accrual_rev_effective_date, NULL, 'N', 'Y') REV_FLAG,

decode(gjh.accrual_rev_status, 'R', 'Reversed', 'Not Reversed') REV_FLAG,

    TO_CHAR(gjh.accrual_rev_effective_date, 'DD-MON-YYYY','nls_date_language=american') REV_DATE,

    xev.entity_id,

    xah.ae_header_id,

gjl.REFERENCE_1 GL_REF1,

xal.accounting_class_code,

    xal.ae_line_num

  FROM xla_events xev,

    xla_ae_headers xah,

    xla_ae_lines xal,

    gl_import_references gir,

    gl_je_headers gjh,

fun_seq_versions fsv_acc_seq,

    fun_seq_versions fsv_rep_seq,

    gl_je_lines gjl,

    gl_ledgers glg,

    gl_je_batches gjb,

gl_code_combinations gcc,

gl_je_sources gjss,

gl_je_categories gjcc,

data_acc_set

  WHERE 1                        =1

  AND xah.event_id               = xev.event_id

  AND xah.ae_header_id           = xal.ae_header_id

  AND xah.gl_transfer_status_code= 'Y'

  AND xal.gl_sl_link_id          =gir.gl_sl_link_id

  AND gir.gl_sl_link_table       = xal.gl_sl_link_table

  AND gjl.je_header_id           =gjh.je_header_id

  AND gjh.je_header_id           =gir.je_header_id

  AND gjl.je_header_id           =gir.je_header_id

  AND gir.je_line_num            =gjl.je_line_num

  AND glg.ledger_id              = gjh.ledger_id

  AND gjh.je_batch_id            = gjb.je_batch_id

  --AND GCC.SEGMENT3 = '5002002'

  --and gjb.je_batch_id            = gjal_ct.je_batch_id

  AND gcc.code_combination_id    =xal.code_combination_id

  AND gcc.code_combination_id    =gjl.code_combination_id

  and gjh.posting_acct_seq_version_id  = fsv_acc_seq.seq_version_id(+)

  and gjh.close_acct_seq_version_id    = fsv_rep_seq.seq_version_id(+)

  AND gjss.je_source_name = gjh.je_source

  AND gjcc.je_category_name = gjh.je_category

  AND gjss.LANGUAGE = USERENV('LANG')

  AND gjcc.LANGUAGE = USERENV('LANG')

  AND gjh.status                 ='P'

  AND gjh.actual_flag            ='A'

  AND gjh.je_source IN ('Payables','Receivables','Cash Management','Assets')

  --AND glg.NAME                 = :P_LEDGER

   and (data_acc_set.ledger_name = :p_ledger

    or data_acc_set.ledger_set_name = :p_ledger)

   and glg.ledger_id             = data_acc_set.ledger_id

and ((coalesce(null, :p_acct_entity) is null)

    or (data_acc_set.flex_segment_value in (:p_acct_entity)))

  and ((coalesce(null, :p_cost_centre) is null)

    or (gcc.segment2 in (:p_cost_centre)))

  and ((coalesce(null, :p_account) is null)

    or (gcc.segment3 in (:p_account)))

  and ((coalesce(null, :p_sub_analysis) is null)

    or (gcc.segment4 in (:p_sub_analysis)))

  and ((coalesce(null, :p_intercmpny) is null)

    or (gcc.segment5 in (:p_intercmpny)))

  and ((coalesce(null, :p_location) is null)

    or (gcc.segment6 in (:p_location)))

  and ((coalesce(null, :p_project) is null)

    or (gcc.segment7 in (:p_project)))

  and ((coalesce(null, :p_flow) is null)

    or (gcc.segment8 in (:p_flow)))

  and ((coalesce(null, :p_spare1) is null)

    or (gcc.segment9 in (:p_spare1)))

  and ((coalesce(null, :p_spare2) is null)

    or (gcc.segment10 in (:p_spare2)))

 and ((coalesce(null, :P_JE_SOURCE) is null)

    or (gjss.user_je_source_name in (:P_JE_SOURCE)))

 and ((coalesce(null, :P_JE_CATEGORY) is null)

    or (gjcc.user_je_category_name in (:P_JE_CATEGORY)))

and gjh.period_name in (select period_name

                             from gl_periods

                            where start_date     >= (select start_date

                                                       from gl_periods

                                                      where period_name     = :P_PERIOD_FROM

                                                        and period_set_name = 'XX_GLOBAL_CAL')

                              and end_date       <= (select end_date

                                                       from gl_periods

                                                      where period_name     = :P_PERIOD_TO

                                                        and period_set_name = 'XX_GLOBAL_CAL')

                              and period_set_name = 'XX_GLOBAL_CAL')  

 

  ),

  DYN_INVOICE_TYPE AS

  (SELECT MEANING,

    LOOKUP_CODE

  FROM FND_LOOKUP_VALUES

  WHERE LOOKUP_TYPE='INVOICE TYPE'

  AND LANGUAGE     =USERENV('LANG')

  ),

  DYN_VENDOR_TYPE AS

  (SELECT MEANING,

    LOOKUP_CODE

  FROM FND_LOOKUP_VALUES

  WHERE LOOKUP_TYPE='POZ_VENDOR_TYPE'

  AND LANGUAGE     =USERENV('LANG')

  ),

  DYN_INVOICE_LINE_TYPE AS

  (SELECT MEANING,

    LOOKUP_CODE

  FROM FND_LOOKUP_VALUES

  WHERE LOOKUP_TYPE='INVOICE LINE TYPE'

  AND LANGUAGE     =USERENV('LANG')

  )

--Purchase Invoices

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  DGSD.ACTCLS_CODE AP_ACCOUNTING_TYPE,

  aia.invoice_num AP_INV_NUMBER,

  (SELECT MEANING

  FROM DYN_INVOICE_TYPE

  WHERE LOOKUP_CODE = AIA.INVOICE_TYPE_LOOKUP_CODE

  ) AP_INV_TYPE,

  aia.source AP_SOURCE,

  TO_CHAR(aia.invoice_date, 'DD-MON-YYYY','nls_date_language=american') AP_INV_DATE,

  aia.invoice_amount AP_INVOICE_AMOUNT,

  hp.party_name AP_SUP_NAME,

  aps.segment1 AP_SUP_NUM,

  poss.vendor_site_code AP_SUPSITE,

  (SELECT MEANING

  FROM DYN_VENDOR_TYPE

  WHERE LOOKUP_CODE = aps.VENDOR_TYPE_LOOKUP_CODE

  ) AP_SUPTYPE,

  (SELECT MAX(PU.USERNAME)

       FROM ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

PER_USERS PU 

  WHERE aid.invoice_id = ail.invoice_id

  and aid.invoice_distribution_id = xdl.source_distribution_id_num_1

  and aia.invoice_id = ail.invoice_id

  and xdl.source_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

      and PU.PERSON_ID=ail.requester_id

  )  AP_REQUESTOR,

  aia.description AP_INVDESC,

  (SELECT MAX(ail.description)

       FROM ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl

  WHERE aid.invoice_id = ail.invoice_id

  and aid.invoice_distribution_id = xdl.source_distribution_id_num_1

  and aia.invoice_id = ail.invoice_id

  and xdl.source_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVLINEDESC,

  (SELECT MAX(dtf.MEANING)

       FROM ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl,

DYN_INVOICE_LINE_TYPE dtf

  WHERE aid.invoice_id = ail.invoice_id

  and aid.invoice_distribution_id = xdl.source_distribution_id_num_1

  and aia.invoice_id = ail.invoice_id

  and xdl.source_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and dtf.LOOKUP_CODE = AIL.LINE_TYPE_LOOKUP_CODE

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVLINE_TYPE,

  (SELECT SEGMENT1 FROM PO_HEADERS_ALL WHERE PO_HEADER_ID=AIA.PO_HEADER_ID

  ) AP_PO,

  (SELECT MAX(prha.requisition_number)

  FROM po_distributions_all pda ,

    por_req_distributions_all prda ,

    por_requisition_lines_all prla ,

    por_requisition_headers_all prha

  WHERE aia.po_header_id         = pda.po_header_id

  AND pda.req_distribution_id    = prda.distribution_id

  AND prda.requisition_line_id   = prla.requisition_line_id

  AND prla.requisition_header_id = prha.requisition_header_id

  ) AP_POREQ,

  (SELECT LISTAGG(rsh.RECEIPT_NUM,'|') WITHIN GROUP (

  ORDER BY rsh.RECEIPT_NUM)

  FROM rcv_shipment_headers rsh,

    rcv_shipment_lines rsl

  WHERE rsh.shipment_header_id=rsl.shipment_header_id

  AND rsl.po_header_id        = aia.po_header_id

  ) AP_RECEIPT,

  (SELECT LISTAGG(check_number,'|') WITHIN GROUP (ORDER BY check_number)

   FROM (SELECT aca.check_number,sum(aipa.amount) AMT

                 FROM ap_checks_all aca,

                           ap_invoice_payments_all aipa

              WHERE aca.check_id  = aipa.check_id

                   AND aipa.invoice_id = aia.invoice_id

              GROUP BY aca.check_number)

 WHERE AMT!=0 -- To exclude cancelled checks

  ) AP_PMT,

  att.name  AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  to_char(aia.terms_date,'DD-MON-YYYY','nls_date_language=american') AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  ap_invoices_all aia,

  xla_transaction_entities xte,

  poz_suppliers aps,

  ap_terms_tl  att,

  poz_supplier_sites_all_m poss,

  hz_parties hp

WHERE 1                         =1

AND aia.invoice_id              = xte.source_id_int_1

AND aps.party_id                = hp.party_id(+)

AND aia.vendor_id               =aps.vendor_id(+)

AND aia.vendor_site_id          = poss.vendor_site_id(+)

AND aps.vendor_id               = poss.vendor_id(+)

and aia.terms_id                     = att.term_id

and att.language                     = 'US'

AND DGSD.entity_id              = xte.entity_id

AND xte.entity_code             = 'AP_INVOICES'

AND DGSD.je_source              = 'Payables'

AND DGSD.je_category            = 'Purchase Invoices'

UNION ALL

--Payments

SELECT DISTINCT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  DGSD.ACTCLS_CODE AP_ACCOUNTING_TYPE,

  (SELECT max(aia.invoice_num)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INV_NUMBER,

  (SELECT max(dff.MEANING)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

DYN_INVOICE_TYPE dff

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and AIA.INVOICE_TYPE_LOOKUP_CODE = dff.LOOKUP_CODE

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INV_TYPE,  

    (SELECT max(aia.source)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_SOURCE,

   (SELECT max(TO_CHAR(aia.invoice_date, 'DD-MON-YYYY','nls_date_language=american') )

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INV_DATE,  

  (SELECT max(aia.invoice_amount)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVOICE_AMOUNT,

  (SELECT max(hp.party_name)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

hz_parties hp,

poz_suppliers aps

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  and aps.party_id = hp.party_id

  ) AP_SUP_NAME,

  (SELECT max(aps.segment1)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

poz_suppliers aps

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  ) AP_SUP_NUM,

  (SELECT max(poss.vendor_site_code)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

poz_suppliers aps,

poz_supplier_sites_all_m poss

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  and poss.vendor_id = aps.vendor_id

  AND poss.vendor_site_id  = aia.vendor_site_id

  )  AP_SUPSITE,

  (SELECT max(dff.MEANING)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

poz_suppliers aps,

DYN_VENDOR_TYPE dff

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  and dff.LOOKUP_CODE = aps.VENDOR_TYPE_LOOKUP_CODE

  )  AP_SUPTYPE,

  (SELECT max(pu.username)

       from ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

PER_USERS pu

  where aid.invoice_id = ail.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

      and PU.PERSON_ID=ail.requester_id

  ) AP_REQUESTOR,

   (SELECT max(aia.description)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVDESC,

  (SELECT max(ail.description)

       from ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  where aid.invoice_id = ail.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVLINEDESC,

  (SELECT max(dff.MEANING)

       from ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

DYN_INVOICE_LINE_TYPE dff

  where aid.invoice_id = ail.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and dff.LOOKUP_CODE = AIL.LINE_TYPE_LOOKUP_CODE

  )  AP_INVLINE_TYPE,

  (SELECT max(pha.SEGMENT1)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl,

             PO_HEADERS_ALL pha

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and pha.PO_HEADER_ID=AIA.PO_HEADER_ID

  ) AP_PO,

  (SELECT max(prha.requisition_number)

    FROM po_headers_all poh, 

    po_distributions_all pda , 

    por_req_distributions_all prda , 

    por_requisition_lines_all prla , 

    por_requisition_headers_all prha ,

ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

    WHERE poh.po_header_id = pda.po_header_id 

    AND poh.po_header_id = aia.po_header_id

    AND pda.req_distribution_id = prda.distribution_id 

    AND prda.requisition_line_id = prla.requisition_line_id 

    AND prla.requisition_header_id = prha.requisition_header_id

and aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_POREQ,

  (SELECT LISTAGG(RECEIPT_NUM,'|') WITHIN GROUP (

  ORDER BY RECEIPT_NUM)

  FROM

  (SELECT rsh.RECEIPT_NUM

  FROM rcv_shipment_headers rsh,

    rcv_shipment_lines rsl,

ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl 

  WHERE rsh.shipment_header_id=rsl.shipment_header_id

  AND rsl.po_header_id        = aia.po_header_id

  and aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  and aid.invoice_distribution_id = xdl.applied_to_dist_id_num_1

  and xdl.applied_to_distribution_type = 'AP_INV_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  GROUP BY rsh.RECEIPT_NUM)

  )  AP_RECEIPT,

  TO_CHAR(aca.check_number) AP_PMT,

  NULL AP_PAYMENT_TERMS,

  --to_char(aps.due_date,'DD-MON-YYYY','nls_date_language=american')  AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  to_char(aca.check_date,'DD-MON-YYYY','nls_date_language=american') AP_PAYMENT_DATE,

  flv.meaning  AP_PAYMENT_STATUS,

  ieba.bank_account_num AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  ap_checks_all aca,

  ap_invoice_payments_all aipa,

  ap_payment_schedules_all aps,

  fnd_lookup_values flv,

  iby_ext_bank_accounts ieba,

  xla_transaction_entities xte

WHERE 1                              =1

AND aca.check_id                     = xte.source_id_int_1

AND DGSD.entity_id                   = xte.entity_id

and aipa.check_id                    = aca.check_id(+)

and aps.invoice_id  = aipa.invoice_id

and aca.status_lookup_code           = flv.lookup_code(+)

and aca.external_bank_account_id     = ieba.ext_bank_account_id(+)

and flv.lookup_type               (+)= 'CHECK STATE'

and flv.language                  (+)= 'US'

AND DGSD.je_source                   = 'Payables'

AND DGSD.je_category                 = 'Payments'

AND xte.entity_code                  = 'AP_PAYMENTS'

UNION ALL

--Reconciled Payments

SELECT DISTINCT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  DGSD.ACTCLS_CODE AP_ACCOUNTING_TYPE,

  (SELECT max(aia.invoice_num)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INV_NUMBER,

  (SELECT max(dff.MEANING)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

DYN_INVOICE_TYPE dff,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and AIA.INVOICE_TYPE_LOOKUP_CODE = dff.LOOKUP_CODE

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INV_TYPE,  

    (SELECT max(aia.source)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_SOURCE,

   (SELECT max(TO_CHAR(aia.invoice_date, 'DD-MON-YYYY','nls_date_language=american') )

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INV_DATE,  

  (SELECT max(aia.invoice_amount)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVOICE_AMOUNT,

  (SELECT max(hp.party_name)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

hz_parties hp,

poz_suppliers aps,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  and aps.party_id = hp.party_id

  ) AP_SUP_NAME,

  (SELECT max(aps.segment1)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

poz_suppliers aps,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  ) AP_SUP_NUM,

  (SELECT max(poss.vendor_site_code)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

poz_suppliers aps,

poz_supplier_sites_all_m poss ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  and poss.vendor_id = aps.vendor_id

  AND poss.vendor_site_id  = aia.vendor_site_id

  )  AP_SUPSITE,

  (SELECT max(dff.MEANING)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

poz_suppliers aps,

DYN_VENDOR_TYPE dff ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and aps.vendor_id = aia.vendor_id

  and dff.LOOKUP_CODE = aps.VENDOR_TYPE_LOOKUP_CODE

  )  AP_SUPTYPE,

  (SELECT max(pu.username)

       from ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

PER_USERS pu ,

ap_payment_hist_dists aipd

  where aid.invoice_id = ail.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

      and PU.PERSON_ID=ail.requester_id

  ) AP_REQUESTOR,

   (SELECT max(aia.description)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl  ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVDESC,

  (SELECT max(ail.description)

       from ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl  ,

ap_payment_hist_dists aipd

  where aid.invoice_id = ail.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_INVLINEDESC,

  (SELECT max(dff.MEANING)

       from ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

DYN_INVOICE_LINE_TYPE dff ,

ap_payment_hist_dists aipd

  where aid.invoice_id = ail.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and dff.LOOKUP_CODE = AIL.LINE_TYPE_LOOKUP_CODE

  )  AP_INVLINE_TYPE,

  (SELECT max(pha.SEGMENT1)

       from ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl,

             PO_HEADERS_ALL pha ,

ap_payment_hist_dists aipd

  where aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  and pha.PO_HEADER_ID=AIA.PO_HEADER_ID

  ) AP_PO,

  (SELECT max(prha.requisition_number)

    FROM po_headers_all poh, 

    po_distributions_all pda , 

    por_req_distributions_all prda , 

    por_requisition_lines_all prla , 

    por_requisition_headers_all prha ,

ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl  ,

ap_payment_hist_dists aipd

    WHERE poh.po_header_id = pda.po_header_id 

    AND poh.po_header_id = aia.po_header_id

    AND pda.req_distribution_id = prda.distribution_id 

    AND prda.requisition_line_id = prla.requisition_line_id 

    AND prla.requisition_header_id = prha.requisition_header_id

and aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  )  AP_POREQ,

  (SELECT LISTAGG(RECEIPT_NUM,'|') WITHIN GROUP (

  ORDER BY RECEIPT_NUM)

  FROM

  (SELECT rsh.RECEIPT_NUM 

  FROM rcv_shipment_headers rsh,

    rcv_shipment_lines rsl,

ap_invoices_all aia,

             ap_invoice_lines_all ail,

ap_invoice_distributions_all aid,

xla_distribution_links xdl ,

ap_payment_hist_dists aipd

  WHERE rsh.shipment_header_id=rsl.shipment_header_id

  AND rsl.po_header_id        = aia.po_header_id

  and aia.invoice_id = ail.invoice_id

      and ail.line_number = aid.invoice_line_number

  and aid.invoice_id = aia.invoice_id

  AND aipd.payment_hist_dist_id = xdl.applied_to_dist_id_num_1

      AND aipd.invoice_distribution_id = aid.invoice_distribution_id

      AND xdl.applied_to_distribution_type = 'AP_PMT_DIST'

  and xdl.ae_header_id = DGSD.ae_header_id

  and xdl.ae_line_num = DGSD.ae_line_num

  GROUP BY rsh.RECEIPT_NUM)

  )  AP_RECEIPT,

  TO_CHAR(aca.check_number) AP_PMT,

  NULL AP_PAYMENT_TERMS,

  --to_char(aps.due_date,'DD-MON-YYYY','nls_date_language=american')  AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  to_char(aca.check_date,'DD-MON-YYYY','nls_date_language=american') AP_PAYMENT_DATE,

  flv.meaning  AP_PAYMENT_STATUS,

  ieba.bank_account_num AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  ap_checks_all aca,

  ap_invoice_payments_all aipa,

  ap_payment_schedules_all aps,

  fnd_lookup_values flv,

  iby_ext_bank_accounts ieba,

  xla_transaction_entities xte

WHERE 1                              =1

AND aca.check_id                     = xte.source_id_int_1

AND DGSD.entity_id                   = xte.entity_id

and aipa.check_id                    = aca.check_id(+)

and aps.invoice_id  = aipa.invoice_id

and aca.status_lookup_code           = flv.lookup_code(+)

and aca.external_bank_account_id     = ieba.ext_bank_account_id(+)

and flv.lookup_type               (+)= 'CHECK STATE'

and flv.language                  (+)= 'US'

AND DGSD.je_source                   = 'Payables'

AND xte.entity_code                  = 'AP_PAYMENTS'

AND DGSD.je_category                 = 'Reconciled Payments'

UNION ALL

--Receivables Invoice Distributions

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

 DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  DGSD.ACTCLS_CODE AR_ACCOUNTING_TYPE,

  rcta.trx_number AR_TRXNUM,

  (SELECT description

  FROM ra_customer_trx_lines_all

  WHERE customer_trx_id=rcta.customer_trx_id

  AND ROWNUM<2

  )AR_INV_LINE_DESC,

  TO_CHAR(rcta.trx_date, 'DD-MON-YYYY','nls_date_language=american') AR_TRXDATE,

  (SELECT SUM(extended_amount)

  FROM ra_customer_trx_lines_all

  WHERE customer_trx_id=rcta.customer_trx_id

  ) AR_TRXAMT,

  hp.party_name AR_CUSTNAME,

  hcsu.location AR_CUSTSITE,

  hca.account_number AR_CUSTNUM,

  rctta.name AR_TRXTYPE,

  rabs.name AR_TRXSOURCE,

  (SELECT LISTAGG(RECEIPT_NUMBER,'|') WITHIN GROUP (ORDER BY RECEIPT_DATE) 

  FROM (SELECT ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,SUM(AMOUNT_APPLIED) AMT,ARC.RECEIPT_NUMBER

FROM AR_CASH_RECEIPTS_ALL ARC,

          AR_RECEIVABLE_APPLICATIONS_ALL ARAA

WHERE ARC.CASH_RECEIPT_ID   = ARAA.CASH_RECEIPT_ID

  AND ARAA.STATUS                  ='APP'

  AND ARAA.APPLIED_CUSTOMER_TRX_ID = RCTA.CUSTOMER_TRX_ID

GROUP BY ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,ARC.RECEIPT_NUMBER

)

WHERE AMT!=0 -- Exclude cancelled receipts

  ) AR_RECEIPTNUM,

  (SELECT LISTAGG(TO_CHAR(RECEIPT_DATE, 'DD-MON-YYYY','nls_date_language=american'),'|') WITHIN GROUP (ORDER BY RECEIPT_DATE)

  FROM (SELECT ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,SUM(AMOUNT_APPLIED) AMT,ARC.RECEIPT_NUMBER

FROM AR_CASH_RECEIPTS_ALL ARC,

          AR_RECEIVABLE_APPLICATIONS_ALL ARAA

WHERE ARC.CASH_RECEIPT_ID   = ARAA.CASH_RECEIPT_ID

  AND ARAA.STATUS                  ='APP'

  AND ARAA.APPLIED_CUSTOMER_TRX_ID = RCTA.CUSTOMER_TRX_ID

GROUP BY ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,ARC.RECEIPT_NUMBER

)

WHERE AMT!=0 -- Exclude cancelled receipts

  ) AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  ra_customer_trx_all rcta,

  ra_cust_trx_line_gl_dist_all rctda,

  xla_distribution_links xdl,

  hz_cust_accounts hca,

  hz_parties hp,

  hz_party_sites hps,

  hz_cust_acct_sites_all hcsa,

  hz_cust_site_uses_all hcsu,

  ra_cust_trx_types_all rctta,

  ra_batch_sources_all rabs

WHERE 1                              =1

AND hca.cust_account_id              = hcsa.cust_account_id

AND rcta.bill_to_site_use_id         = hcsu.site_use_id

AND hcsa.cust_acct_site_id           = hcsu.cust_acct_site_id

AND hps.party_site_id                = hcsa.party_site_id

AND rcta.bill_to_customer_id         = hca.cust_account_id

AND hca.party_id                     = hp.party_id

AND rctta.cust_trx_type_seq_id       = rcta.cust_trx_type_seq_id

AND rabs.batch_source_seq_id         = rcta.batch_source_seq_id

AND rcta.customer_trx_id             = xte.source_id_int_1

AND DGSD.entity_id                   = xte.entity_id

AND rcta.customer_trx_id             = rctda.customer_trx_id

AND xdl.ae_header_id                 = DGSD.ae_header_id

AND xdl.ae_line_num                  = DGSD.ae_line_num

AND xdl.source_distribution_id_num_1 = rctda.cust_trx_line_gl_dist_id

AND xdl.source_distribution_type     = 'RA_CUST_TRX_LINE_GL_DIST_ALL'

AND DGSD.je_source                   = 'Receivables'

UNION ALL 

-- Receivable Applications

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  DGSD.ACTCLS_CODE AR_ACCOUNTING_TYPE,

  rcta.trx_number AR_TRXNUM,

  (SELECT description

  FROM ra_customer_trx_lines_all

  WHERE customer_trx_id=rcta.customer_trx_id

  AND ROWNUM<2

  )AR_INV_LINE_DESC,

  TO_CHAR(rcta.trx_date, 'DD-MON-YYYY','nls_date_language=american') AR_TRXDATE,

  (SELECT SUM(extended_amount)

  FROM ra_customer_trx_lines_all

  WHERE customer_trx_id=rcta.customer_trx_id

  ) AR_TRXAMT,

  hp.party_name AR_CUSTNAME,

  hcsu.location AR_CUSTSITE,

  hca.account_number AR_CUSTNUM,

  rctta.name AR_TRXTYPE,

  rabs.name AR_TRXSOURCE,

  (SELECT LISTAGG(RECEIPT_NUMBER,'|') WITHIN GROUP (ORDER BY RECEIPT_DATE) 

  FROM (SELECT ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,SUM(AMOUNT_APPLIED) AMT,ARC.RECEIPT_NUMBER

FROM AR_CASH_RECEIPTS_ALL ARC

WHERE ARC.CASH_RECEIPT_ID   = ARAA.CASH_RECEIPT_ID

  AND ARAA.STATUS                  ='APP'

  AND ARAA.APPLIED_CUSTOMER_TRX_ID = RCTA.CUSTOMER_TRX_ID

GROUP BY ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,ARC.RECEIPT_NUMBER

)

WHERE AMT!=0 -- Exclude cancelled receipts

  ) AR_RECEIPTNUM,

  (SELECT LISTAGG(TO_CHAR(RECEIPT_DATE, 'DD-MON-YYYY','nls_date_language=american'),'|') WITHIN GROUP (ORDER BY RECEIPT_DATE)

  FROM (SELECT ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,SUM(AMOUNT_APPLIED) AMT,ARC.RECEIPT_NUMBER

FROM AR_CASH_RECEIPTS_ALL ARC

WHERE ARC.CASH_RECEIPT_ID   = ARAA.CASH_RECEIPT_ID

  AND ARAA.STATUS                  ='APP'

  AND ARAA.APPLIED_CUSTOMER_TRX_ID = RCTA.CUSTOMER_TRX_ID

GROUP BY ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,ARC.RECEIPT_NUMBER

)

WHERE AMT!=0 -- Exclude cancelled receipts

  ) AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  ra_customer_trx_all rcta,

  ar_distributions_all rctda,

  ar_receivable_applications_all araa,

  xla_distribution_links xdl,

  hz_cust_accounts hca,

  hz_parties hp,

  hz_party_sites hps,

  hz_cust_acct_sites_all hcsa,

  hz_cust_site_uses_all hcsu,

  ra_cust_trx_types_all rctta,

  ra_batch_sources_all rabs

WHERE 1                              =1

AND hca.cust_account_id              = hcsa.cust_account_id

AND rcta.bill_to_site_use_id         = hcsu.site_use_id

AND hcsa.cust_acct_site_id           = hcsu.cust_acct_site_id

AND hps.party_site_id                = hcsa.party_site_id

AND rcta.bill_to_customer_id         = hca.cust_account_id

AND hca.party_id                     = hp.party_id

AND rctta.cust_trx_type_seq_id       = rcta.cust_trx_type_seq_id

AND rabs.batch_source_seq_id         = rcta.batch_source_seq_id

AND rcta.customer_trx_id             = xte.source_id_int_1

AND DGSD.entity_id                   = xte.entity_id

AND xdl.ae_header_id                 = DGSD.ae_header_id

AND xdl.ae_line_num                  = DGSD.ae_line_num

AND xdl.source_distribution_id_num_1 = rctda.line_id

AND araa.receivable_application_id = rctda.source_id

AND rcta.customer_trx_id = araa.customer_trx_id

AND xdl.source_distribution_type     = 'AR_DISTRIBUTIONS_ALL'

AND DGSD.je_source                   = 'Receivables'

UNION ALL 

--Receivables Adjustments

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  DGSD.ACTCLS_CODE AR_ACCOUNTING_TYPE,

  rcta.trx_number AR_TRXNUM,

  (SELECT DISTINCT description

  FROM ra_customer_trx_lines_all

  WHERE customer_trx_id=rcta.customer_trx_id

  AND ROWNUM<2

  ) AR_INV_LINE_DESC,

  TO_CHAR(rcta.trx_date, 'DD-MON-YYYY','nls_date_language=american') AR_TRXDATE,

  (SELECT SUM(extended_amount)

  FROM ra_customer_trx_lines_all

  WHERE customer_trx_id=rcta.customer_trx_id

  ) AR_TRXAMT,

  hp.party_name AR_CUSTNAME,

  hcsu.location AR_CUSTSITE,

  hca.account_number AR_CUSTNUM,

  rctta.name AR_TRXTYPE,

  rabs.name AR_TRXSOURCE,

  (SELECT LISTAGG(RECEIPT_NUMBER,'|') WITHIN GROUP (ORDER BY RECEIPT_DATE) 

  FROM (SELECT ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,SUM(AMOUNT_APPLIED) AMT,ARC.RECEIPT_NUMBER

FROM AR_CASH_RECEIPTS_ALL ARC,

          AR_RECEIVABLE_APPLICATIONS_ALL ARAA

WHERE ARC.CASH_RECEIPT_ID   = ARAA.CASH_RECEIPT_ID

  AND ARAA.STATUS                  ='APP'

  AND ARAA.APPLIED_CUSTOMER_TRX_ID = RCTA.CUSTOMER_TRX_ID

GROUP BY ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,ARC.RECEIPT_NUMBER

)

WHERE AMT!=0 -- Exclude cancelled receipts

  ) AR_RECEIPTNUM,

  (SELECT LISTAGG(TO_CHAR(RECEIPT_DATE, 'DD-MON-YYYY','nls_date_language=american'),'|') WITHIN GROUP (ORDER BY RECEIPT_DATE)

  FROM (SELECT ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,SUM(AMOUNT_APPLIED) AMT,ARC.RECEIPT_NUMBER

FROM AR_CASH_RECEIPTS_ALL ARC,

          AR_RECEIVABLE_APPLICATIONS_ALL ARAA

WHERE ARC.CASH_RECEIPT_ID   = ARAA.CASH_RECEIPT_ID

  AND ARAA.STATUS                  ='APP'

  AND ARAA.APPLIED_CUSTOMER_TRX_ID = RCTA.CUSTOMER_TRX_ID

GROUP BY ARAA.CASH_RECEIPT_ID,ARC.RECEIPT_DATE,ARC.RECEIPT_NUMBER

)

WHERE AMT!=0 -- Exclude cancelled receipts

  ) AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  ra_customer_trx_all rcta,

  ar_adjustments_all ada,

  hz_cust_accounts hca,

  hz_parties hp,

  hz_party_sites hps,

  hz_cust_acct_sites_all hcsa,

  hz_cust_site_uses_all hcsu,

  ra_cust_trx_types_all rctta,

  ra_batch_sources_all rabs

WHERE 1                              =1

AND hca.cust_account_id              = hcsa.cust_account_id

AND rcta.bill_to_site_use_id         = hcsu.site_use_id

AND hcsa.cust_acct_site_id           = hcsu.cust_acct_site_id

AND hps.party_site_id                = hcsa.party_site_id

AND rcta.bill_to_customer_id         = hca.cust_account_id

AND hca.party_id                     = hp.party_id

AND rctta.cust_trx_type_seq_id       = rcta.cust_trx_type_seq_id

AND rabs.batch_source_seq_id         = rcta.batch_source_seq_id

AND rcta.customer_trx_id             = ada.customer_trx_id

AND DGSD.entity_id                   = xte.entity_id

AND xte.source_id_int_1 = ada.adjustment_id

 AND ada.customer_trx_id = rcta.customer_trx_id

AND DGSD.je_source                   = 'Receivables'

AND DGSD.je_category = 'Adjustment'

AND xte.entity_code = 'ADJUSTMENTS'

UNION ALL

--Receipts Balancing

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  DGSD.ACTCLS_CODE AR_ACCOUNTING_TYPE,

  (SELECT LISTAGG(trx_number,'|') WITHIN GROUP (ORDER BY customer_trx_id)

     FROM

  (SELECT rcta.trx_number ,rcta.customer_trx_id

      FROM ra_customer_trx_all rcta,

             ar_receivable_applications_all araa

WHERE rcta.customer_trx_id = araa.applied_customer_trx_id

     AND araa.cash_receipt_id = acr.cash_receipt_id

AND araa.status='APP'

GROUP BY rcta.trx_number,rcta.customer_trx_id

  )) AR_TRXNUM,  

  (SELECT DISTINCT rctla.description

  FROM ra_customer_trx_lines_all rctla             

  WHERE 1=1

      AND ROWNUM<2

      AND exists (select 1 from ar_receivable_applications_all araa 

                    where rctla.customer_trx_id=araa.applied_customer_trx_id

                            AND araa.cash_receipt_id = acr.cash_receipt_id

AND araa.status='APP')

  ) AR_INV_LINE_DESC,

  (SELECT LISTAGG(trx_date,'|') WITHIN GROUP (ORDER BY customer_trx_id)

     FROM

  (SELECT TO_CHAR(rcta.trx_date, 'DD-MON-YYYY','nls_date_language=american')  trx_date,rcta.customer_trx_id

      FROM ra_customer_trx_all rcta,

             ar_receivable_applications_all araa

WHERE rcta.customer_trx_id = araa.applied_customer_trx_id

     AND araa.cash_receipt_id = acr.cash_receipt_id

AND araa.status='APP'

GROUP BY rcta.trx_date,rcta.customer_trx_id

  ))  AR_TRXDATE,

  (SELECT SUM(rctla.extended_amount) extended_amount

  FROM ra_customer_trx_lines_all rctla             

  WHERE 1=1

      AND exists (select 1 from ar_receivable_applications_all araa 

                    where rctla.customer_trx_id=araa.applied_customer_trx_id

                            AND araa.cash_receipt_id = acr.cash_receipt_id

AND araa.status='APP')

  ) AR_TRXAMT,

  hp.party_name AR_CUSTNAME,

  (SELECT hcsu.location

  FROM hz_party_sites hps,

    hz_cust_acct_sites_all hcsa,

    hz_cust_site_uses_all hcsu

  WHERE 1                      =1

  AND hca.cust_account_id      = hcsa.cust_account_id

  AND acr.customer_site_use_id = hcsu.site_use_id

  AND hcsa.cust_acct_site_id   = hcsu.cust_acct_site_id

  AND hps.party_site_id        = hcsa.party_site_id

  ) AR_CUSTSITE,

  hca.account_number AR_CUSTNUM,  

  (SELECT LISTAGG(name,'|') WITHIN GROUP (ORDER BY customer_trx_id)

     FROM

  (SELECT rctta.name ,rcta.customer_trx_id

      FROM ra_customer_trx_all rcta,

             ar_receivable_applications_all araa,

ra_cust_trx_types_all rctta

WHERE rcta.customer_trx_id = araa.applied_customer_trx_id

     AND araa.cash_receipt_id = acr.cash_receipt_id

AND araa.status='APP'

AND rctta.cust_trx_type_seq_id = rcta.cust_trx_type_seq_id

GROUP BY rctta.name,rcta.customer_trx_id

  ))  AR_TRXTYPE,

  (SELECT LISTAGG(name,'|') WITHIN GROUP (ORDER BY customer_trx_id)

     FROM

  (SELECT rabs.name ,rcta.customer_trx_id

      FROM ra_customer_trx_all rcta,

             ar_receivable_applications_all araa,

ra_batch_sources_all rabs

WHERE rcta.customer_trx_id = araa.applied_customer_trx_id

     AND araa.cash_receipt_id = acr.cash_receipt_id

AND rabs.batch_source_seq_id = rcta.batch_source_seq_id

AND araa.status='APP'

GROUP BY rabs.name,rcta.customer_trx_id

  )) AR_TRXSOURCE,

  acr.RECEIPT_NUMBER AR_RECEIPTNUM,

  TO_CHAR(acr.RECEIPT_DATE, 'DD-MON-YYYY','nls_date_language=american') AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  ar_cash_receipts_all acr,

  hz_cust_accounts hca,

  hz_parties hp

WHERE 1                            =1

AND acr.cash_receipt_id           = xte.source_id_int_1

AND DGSD.entity_id                  = xte.entity_id

AND DGSD.je_source                = 'Receivables'

AND DGSD.je_category             = 'Receipts'

AND acr.pay_from_customer     = hca.cust_account_id(+)

AND hca.party_id                      = hp.party_id(+)

UNION ALL

--Cash Management

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  NULL FA_ACCOUNTING_TYPE,

  NULL FA_ASSET_NUM,

  NULL FA_TRANS_CATE,

  NULL FA_MAJ_CATE,

  NULL FA_MIN_CATE,

  NULL FA_ASSET_TYPE,

  NULL FA_ASSET_DESC,

  NULL FA_DATE_PLACED,

  NULL FA_ASST_LOC_COUNTRY,

  NULL FA_ASST_LOC_STATE,

  NULL FA_ASST_LOC_CITY,

  NULL FA_ASST_LOC_SPARE,

  NULL FA_ASST_REM_LIFE,

  NULL FA_ASST_DEP_MET,

  NULL FA_USEFUL_LIFE,

  NULL FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  cet.transaction_type CE_TRX_TYPE,

  TO_CHAR(cet.transaction_date, 'DD-MON-YYYY','nls_date_language=american') CE_TRX_DATE,

  cet.reference_text CE_REF,

  cet.description CE_DESC,

  to_char(csh.statement_number) CE_STMT_IDENTIFIER,

  TO_CHAR(csh.statement_date, 'DD-MON-YYYY','nls_date_language=american') CE_STMT_DATE,

  to_char(ieba.bank_account_num) CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  ce_bank_accounts   cba,

  ce_statement_headers  csh,

  iby_ext_bank_accounts  ieba,

  ce_external_transactions cet

WHERE 1                 =1

AND xte.source_id_int_1 = cet.transaction_id

AND xte.entity_id       = DGSD.entity_id

and cet.bank_account_id              = cba.bank_account_id

and csh.bank_account_id              = cba.bank_account_id

and CET.bank_account_id              = ieba.ext_bank_account_id

AND DGSD.je_source      = 'Cash Management'

UNION ALL

--Assets Depreciation

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  DGSD.ACTCLS_CODE FA_ACCOUNTING_TYPE,

  fas.asset_number FA_ASSET_NUM,

  DGSD.je_category FA_TRANS_CATE,

  fac.segment1 FA_MAJ_CATE,

  fac.segment2 FA_MIN_CATE,

  fas.asset_type FA_ASSET_TYPE,

  fasl.description FA_ASSET_DESC,

  TO_CHAR(fab.date_placed_in_service, 'DD-MON-YYYY','nls_date_language=american') FA_DATE_PLACED,

  fl.segment1 FA_ASST_LOC_COUNTRY,

  fl.segment2 FA_ASST_LOC_STATE,

  fl.segment3 FA_ASST_LOC_CITY,

  fl.segment4 FA_ASST_LOC_SPARE,

  ''                FA_ASST_REM_LIFE,

  fam.method_code FA_ASST_DEP_MET,

  fam.life_in_months FA_USEFUL_LIFE,

  fab.book_type_code FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  fa_additions_b fas,

  fa_additions_tl fasl,

  fa_categories_b fac,

  fa_books fab,

  fa_distribution_history fdh,

  fa_locations fl,

  fa_category_book_defaults fcb,

  fa_methods fam

WHERE 1                   =1

AND fas.asset_id          = xte.source_id_int_1

AND fasl.asset_id         = fas.asset_id

AND fab.transaction_header_id_out is null

AND fasl.language         = USERENV('LANG')

AND fab.book_type_code    = xte.source_id_char_1

AND fab.book_type_code    = fdh.book_type_code

AND fab.transaction_header_id_out is null

AND fas.asset_category_id = fac.category_id

AND fab.asset_id          = fas.asset_id

AND fcb.category_id       = fac.category_id

AND fcb.book_type_code    = fab.book_type_code

and fdh.distribution_id in (select max(fdh1.distribution_id)

                             from fa_distribution_history fdh1

where 1=1

and fab.asset_id              = fdh1.asset_id 

AND fab.book_type_code        = fdh1.book_type_code)

--AND DGSD.code_combination_id = fdh.code_combination_id

AND fcb.method_id         = fam.method_id

and fab.asset_id                      = fdh.asset_id

and fdh.location_id                  = fl.location_id

AND DGSD.entity_id        = xte.entity_id

AND DGSD.je_source        = 'Assets'

AND DGSD.je_category      = 'Depreciation'

AND xte.entity_code ='DEPRECIATION'

UNION ALL

--Assets Addition

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  DGSD.ACTCLS_CODE FA_ACCOUNTING_TYPE,

  fas.asset_number FA_ASSET_NUM,

  DGSD.je_category FA_TRANS_CATE,

  fac.segment1 FA_MAJ_CATE,

  fac.segment2 FA_MIN_CATE,

  fas.asset_type FA_ASSET_TYPE,

  fasl.description FA_ASSET_DESC,

  TO_CHAR(fab.date_placed_in_service, 'DD-MON-YYYY','nls_date_language=american') FA_DATE_PLACED,

   fl.segment1 FA_ASST_LOC_COUNTRY,

          fl.segment2 FA_ASST_LOC_STATE,

          fl.segment3 FA_ASST_LOC_CITY,

          fl.segment4 FA_ASST_LOC_SPARE,

  ''                FA_ASST_REM_LIFE,

  fam.method_code FA_ASST_DEP_MET,

  fam.life_in_months FA_USEFUL_LIFE,

  fab.book_type_code FA_BOOK_TYPE_CODE,

  NULL FA_LEASE_NUMBER,

  NULL FA_LEASE_DESCRIPTION,

  NULL FA_LEASE_START_DATE,

  NULL FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  fa_additions_b fas,

  fa_additions_tl fasl,

  fa_categories_b fac,

  fa_books fab,

  fa_category_book_defaults fcb,

  fa_distribution_history fdh,

  fa_locations fl,

  fa_transaction_headers fth,

  fa_methods fam

WHERE 1                       =1

AND fth.transaction_header_id = xte.source_id_int_1

AND fas.asset_id              = fth.asset_id

AND fas.asset_id              = fab.asset_id

AND fasl.asset_id             = fas.asset_id

AND fasl.language             = USERENV('LANG')

AND fab.book_type_code        = xte.source_id_char_1

AND fab.transaction_header_id_out is null

AND fab.book_type_code        = fth.book_type_code

AND fab.book_type_code        = fdh.book_type_code

AND fab.transaction_header_id_out is null

AND fas.asset_category_id     = fac.category_id

AND fcb.category_id           = fac.category_id

and fab.asset_id              = fdh.asset_id

and fdh.location_id           = fl.location_id

AND fcb.book_type_code        = fab.book_type_code

AND fcb.method_id             = fam.method_id

AND DGSD.entity_id            = xte.entity_id

and fdh.distribution_id in (select max(fdh1.distribution_id)

                             from fa_distribution_history fdh1

where 1=1

and fab.asset_id              = fdh1.asset_id 

AND fab.book_type_code        = fdh1.book_type_code)

AND DGSD.JE_SOURCE            = 'Assets'

AND DGSD.je_category         IN ('Transfer', 'Retirement', 'Adjustment', 'CIP Adjustment', 'Addition', 'CIP Addition')

UNION ALL

--FA LEASE

SELECT DGSD.LEDGER,

  DGSD.PERIOD,

  DGSD.ACCOUNTING_DATE,

  DGSD.accounting_entity,

  DGSD.cost_centre,

  DGSD.nominal_account,

  DGSD.sub_analysis,

  DGSD.intercompany,

  DGSD.location,

  DGSD.project,

  DGSD.flow,

  DGSD.spare1,

  DGSD.spare2,

  (DGSD.accounting_entity_desc||'.'||DGSD.cost_centre_desc||'.'||DGSD.nominal_account_desc||'.'||DGSD.sub_analysis_desc||'.'||DGSD.intercompany_desc||'.'||

  DGSD.location_desc||'.'||DGSD.project_desc||'.'||DGSD.flow_desc||'.'||DGSD.spare1_desc||'.'||DGSD.spare2_desc) CODE_COMBINATION_DESC,

  DGSD.JE_SOURCE,

  DGSD.JE_CATEGORY,

  DGSD.DOC_SEQ,

  DGSD.POSTED_DATE,

  DGSD.ENT_CUR,

  DGSD.XENTERED_DR ENT_DR,

  DGSD.XENTERED_CR ENT_CR,

  nvl(DGSD.XENTERED_DR,0)-nvl(DGSD.XENTERED_CR,0) ENT_NET,

  DGSD.XACCOUNTED_DR ACCTD_DR,

  DGSD.XACCOUNTED_CR ACCTD_CR,

  nvl(DGSD.XACCOUNTED_DR,0)-nvl(DGSD.XACCOUNTED_CR,0) ACCTD_NET,

  DGSD.CUR_CONV_TYPE,

  DGSD.CUR_CONV_DATE,

  DGSD.CUR_CONV_RATE,

  DGSD.BATCH,

  DGSD.BATCH_DESC,

  DGSD.JOUR_NAME,

  DGSD.JOUR_DESC,

  DGSD.JOURLINE_DESC,

  DGSD.JOUR_PREP,

  DGSD.JOUR_APPR,

  DGSD.REV_FLAG,

  DGSD.REV_DATE,

  DGSD.ACC_SEQUENCE_NAME,

  DGSD.ACC_SEQUENCE_NUMBER,

  DGSD.REP_SEQUENCE_NAME,

  DGSD.REP_SEQUENCE_NUMBER,

  DGSD.JOURNAL_LINE_NUMBER,

  NULL AP_ACCOUNTING_TYPE,

  NULL AP_INV_NUMBER,

  NULL AP_INV_TYPE,

  NULL AP_SOURCE,

  NULL AP_INV_DATE,

  NULL AP_INVOICE_AMOUNT,

  NULL AP_SUP_NAME,

  NULL AP_SUP_NUM,

  NULL AP_SUPSITE,

  NULL AP_SUPTYPE,

  NULL AP_REQUESTOR,

  NULL AP_INVDESC,

  NULL AP_INVLINEDESC,

  NULL AP_INVLINE_TYPE,

  NULL AP_PO,

  NULL AP_POREQ,

  NULL AP_RECEIPT,

  NULL AP_PMT,

  NULL AP_PAYMENT_TERMS,

  NULL AP_PAYMENT_DUE_DATE,

  NULL AP_PAYMENT_TERM_DATE,

  NULL AP_PAYMENT_DATE,

  NULL AP_PAYMENT_STATUS,

  NULL AP_XX_BANK_ACC_NUMB,

  DGSD.ACTCLS_CODE FA_ACCOUNTING_TYPE,

  fas.asset_number FA_ASSET_NUM,

  DGSD.je_category FA_TRANS_CATE,

  fac.segment1 FA_MAJ_CATE,

  fac.segment2 FA_MIN_CATE,

  fas.asset_type FA_ASSET_TYPE,

  fasl.description FA_ASSET_DESC,

  TO_CHAR(fab.date_placed_in_service, 'DD-MON-YYYY','nls_date_language=american') FA_DATE_PLACED,

  fl.segment1 FA_ASST_LOC_COUNTRY,

  fl.segment2 FA_ASST_LOC_STATE,

  fl.segment3 FA_ASST_LOC_CITY,

  fl.segment4 FA_ASST_LOC_SPARE,

  ''                FA_ASST_REM_LIFE,

  fam.method_code FA_ASST_DEP_MET,

  fam.life_in_months FA_USEFUL_LIFE,

  fab.book_type_code FA_BOOK_TYPE_CODE,

  fal.lease_number     FA_LEASE_NUMBER,

  fal.description      FA_LEASE_DESCRIPTION,

  to_char(fal.lease_start_date, 'DD-MON-YYYY','nls_date_language=american') FA_LEASE_START_DATE,

  to_char(fal.lease_end_date, 'DD-MON-YYYY','nls_date_language=american')   FA_LEASE_END_DATE,

  NULL AR_ACCOUNTING_TYPE,

  NULL AR_TRXNUM,

  NULL AR_INV_LINE_DESC,

  NULL AR_TRXDATE,

  NULL AR_TRXAMT,

  NULL AR_CUSTNAME,

  NULL AR_CUSTSITE,

  NULL AR_CUSTNUM,

  NULL AR_TRXTYPE,

  NULL AR_TRXSOURCE,

  NULL AR_RECEIPTNUM,

  NULL AR_RECEIPTDATE,

  NULL CE_TRX_TYPE,

  NULL CE_TRX_DATE,

  NULL CE_REF,

  NULL CE_DESC,

  NULL CE_STMT_IDENTIFIER,

  NULL CE_STMT_DATE,

  NULL CE_XX_BANK_ACC,

  DGSD.GL_REF1

FROM DYN_GL_XX_DATA DGSD,

  xla_transaction_entities xte,

  fa_additions_b fas,

  fa_additions_tl fasl,

  fa_categories_b fac,

  fa_books fab,

  fa_distribution_history fdh,

  fa_locations fl,

  fa_category_book_defaults fcb,

  FA_LEASES fal,

  fa_methods fam

WHERE 1                   =1

AND fas.asset_id          = xte.source_id_int_1

AND fal.lease_id          = fab.lease_id

AND fasl.asset_id         = fas.asset_id

AND fab.transaction_header_id_out is null

AND fasl.language         = USERENV('LANG')

AND fab.book_type_code    = xte.source_id_char_1

AND fab.transaction_header_id_out is null

AND fas.asset_category_id = fac.category_id

AND fab.asset_id          = fas.asset_id

AND fcb.category_id       = fac.category_id

AND fcb.book_type_code    = fab.book_type_code

AND fab.book_type_code    = fdh.book_type_code

AND fcb.method_id         = fam.method_id

and fab.asset_id          = fdh.asset_id

and fdh.location_id       = fl.location_id

AND DGSD.entity_id        = xte.entity_id

and fdh.distribution_id in (select max(fdh1.distribution_id)

                             from fa_distribution_history fdh1

where 1=1

and fab.asset_id              = fdh1.asset_id 

AND fab.book_type_code        = fdh1.book_type_code)

AND DGSD.je_source        = 'Assets'

AND DGSD.je_category      = 'Lease Expense'

AND xte.entity_code ='LEASE_EXPENSE'

UNION ALL

--All Other Sources

SELECT 

 glg.NAME LEDGER,

 gjh.period_name PERIOD,

 TO_CHAR(gjh.default_effective_date, 'DD-MON-YYYY','nls_date_language=american') ACCOUNTING_DATE,

 data_acc_set.flex_segment_value accounting_entity,

 gcc.segment2 cost_centre,

 gcc.segment3 nominal_account,

 gcc.segment4 sub_analysis,

 gcc.segment5 intercompany,

 gcc.segment6 location,

 gcc.segment7 project,

 gcc.segment8 flow,

 gcc.segment9 spare1,

 gcc.segment10 spare2,

 (gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 1, data_acc_set.flex_segment_value)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 2, gcc.segment2)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 3, gcc.segment3)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 4, gcc.segment4)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 5, gcc.segment5)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 6, gcc.segment6)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 7, gcc.segment7)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 8, gcc.segment8)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 9, gcc.segment9)||'.'||

 gl_flexfields_pkg.get_description_sql(glg.chart_of_accounts_id, 10, gcc.segment10))CODE_COMBINATION_DESC,

 gjs.user_je_source_name JE_SOURCE,

 gjc.user_je_category_name JE_CATEGORY,

 gjh.doc_sequence_value DOC_SEQ, 

 TO_CHAR(gjh.posted_date, 'DD-MON-YYYY','nls_date_language=american') POSTED_DATE,

 gjl.currency_code ENT_CUR,

 nvl(xal.entered_dr, gjl.entered_dr) ENT_DR,

 nvl(xal.entered_cr, gjl.entered_cr) ENT_CR,

 nvl(nvl(xal.entered_dr, gjl.entered_dr),0)-nvl(nvl(xal.entered_cr, gjl.entered_cr),0) ENT_NET,

 nvl(xal.accounted_dr, gjl.accounted_dr) ACCTD_DR,

 nvl(xal.accounted_cr, gjl.accounted_cr) ACCTD_CR,

 nvl(nvl(xal.accounted_dr, gjl.accounted_dr),0)-nvl(nvl(xal.accounted_cr, gjl.accounted_cr),0) ACCTD_NET,

 nvl(xal.currency_conversion_type,gjl.currency_conversion_type) CUR_CONV_TYPE,

 TO_CHAR(nvl(xal.currency_conversion_date,gjl.currency_conversion_date), 'DD-MON-YYYY','nls_date_language=american') CUR_CONV_DATE,

 nvl(xal.currency_conversion_rate,gjl.currency_conversion_rate) CUR_CONV_RATE,

 gjb.name BATCH,

 gjb.description BATCH_DESC,

 gjh.name JOUR_NAME,

 gjh.description JOUR_DESC,

 gjl.description JOURLINE_DESC,

 gjh.created_by JOUR_PREP,

 (SELECT display_name FROM per_person_names_f WHERE person_id = gjb.approver_employee_id AND name_type = 'GLOBAL') JOUR_APPR,

 --DECODE(gjh.accrual_rev_effective_date, NULL, 'N', 'Y') REV_FLAG,

 decode(gjh.accrual_rev_status, 'R', 'Reversed', 'Not Reversed') REV_FLAG,

TO_CHAR(gjh.accrual_rev_effective_date, 'DD-MON-YYYY','nls_date_language=american')  REV_DATE,

fsv_acc_seq.header_name      ACC_SEQUENCE_NAME,

gjh.posting_acct_seq_value   ACC_SEQUENCE_NUMBER,

fsv_rep_seq.header_name      REP_SEQUENCE_NAME,

gjh.close_acct_seq_value     REP_SEQUENCE_NUMBER,

gjl.je_line_num              JOURNAL_LINE_NUMBER,

NULL AP_ACCOUNTING_TYPE,

NULL AP_INV_NUMBER,

NULL AP_INV_TYPE,

NULL AP_SOURCE,

NULL AP_INV_DATE,

NULL AP_INVOICE_AMOUNT,

NULL AP_SUP_NAME,

NULL AP_SUP_NUM,

NULL AP_SUPSITE,

NULL AP_SUPTYPE,

NULL AP_REQUESTOR,

NULL AP_INVDESC,

NULL AP_INVLINEDESC,

NULL AP_INVLINE_TYPE,

NULL AP_PO,

NULL AP_POREQ,

NULL AP_RECEIPT,

NULL AP_PMT,

NULL AP_PAYMENT_TERMS,

NULL AP_PAYMENT_DUE_DATE,

NULL AP_PAYMENT_TERM_DATE,

NULL AP_PAYMENT_DATE,

NULL AP_PAYMENT_STATUS,

NULL AP_XX_BANK_ACC_NUMB,

NULL FA_ACCOUNTING_TYPE,

NULL FA_ASSET_NUM,

NULL FA_TRANS_CATE,

NULL FA_MAJ_CATE,

NULL FA_MIN_CATE,

NULL FA_ASSET_TYPE,

NULL FA_ASSET_DESC,

NULL FA_DATE_PLACED,

NULL FA_ASST_LOC_COUNTRY,

NULL FA_ASST_LOC_STATE,

NULL FA_ASST_LOC_CITY,

NULL FA_ASST_LOC_SPARE,

NULL FA_ASST_REM_LIFE,

NULL FA_ASST_DEP_MET,

NULL FA_USEFUL_LIFE,

NULL FA_BOOK_TYPE_CODE,

NULL FA_LEASE_NUMBER,

NULL FA_LEASE_DESCRIPTION,

NULL FA_LEASE_START_DATE,

NULL FA_LEASE_END_DATE,

NULL AR_ACCOUNTING_TYPE,

NULL AR_TRXNUM,

NULL AR_INV_LINE_DESC,

NULL AR_TRXDATE,

NULL AR_TRXAMT,

NULL AR_CUSTNAME,

NULL AR_CUSTSITE,

NULL AR_CUSTNUM,

NULL AR_TRXTYPE,

NULL AR_TRXSOURCE,

NULL AR_RECEIPTNUM,

NULL AR_RECEIPTDATE,

NULL CE_TRX_TYPE,

NULL CE_TRX_DATE,

NULL CE_REF,

NULL CE_DESC,

NULL CE_STMT_IDENTIFIER,

NULL CE_STMT_DATE,

NULL CE_XX_BANK_ACC,

gjl.REFERENCE_1 GL_REF1 

FROM

  gl_je_headers gjh,

  gl_je_lines  gjl,

  gl_ledgers glg,

  gl_je_batches gjb,

  gl_code_combinations gcc,

  gl_import_references gir,

  fun_seq_versions fsv_acc_seq,

  fun_seq_versions fsv_rep_seq,

  gl_je_sources gjs,

  gl_je_categories gjc,

  xla_ae_lines xal,

  data_acc_set

WHERE 1=1

 AND gjl.je_header_id=gjh.je_header_id

 AND glg.ledger_id = gjh.ledger_id

 AND gjs.je_source_name = gjh.je_source

 AND gjc.je_category_name = gjh.je_category

 AND gjs.LANGUAGE = USERENV('LANG')

 AND gjc.LANGUAGE = USERENV('LANG')

 AND gjh.je_batch_id = gjb.je_batch_id

 AND gjl.je_header_id = gir.je_header_id(+)

 AND gjl.je_line_num = gir.je_line_num(+)

 AND gir.gl_sl_link_id = xal.gl_sl_link_id(+)

 AND gir.gl_sl_link_table = xal.gl_sl_link_table(+)

 AND gcc.code_combination_id=gjl.code_combination_id

 and gjh.posting_acct_seq_version_id  = fsv_acc_seq.seq_version_id(+)

 and gjh.close_acct_seq_version_id    = fsv_rep_seq.seq_version_id(+)

 AND gjh.status='P'

 AND gjh.actual_flag='A'

 AND gjh.je_source NOT IN ('Payables','Receivables','Cash Management','Assets')

-- AND glg.NAME                 = :P_LEDGER

   and (data_acc_set.ledger_name = :p_ledger

    or data_acc_set.ledger_set_name = :p_ledger)

   and glg.ledger_id             = data_acc_set.ledger_id

and ((coalesce(null, :p_acct_entity) is null)

    or (data_acc_set.flex_segment_value in (:p_acct_entity)))

 and ((coalesce(null, :p_cost_centre) is null)

    or (gcc.segment2 in (:p_cost_centre)))

 and ((coalesce(null, :p_account) is null)

    or (gcc.segment3 in (:p_account)))

 and ((coalesce(null, :p_sub_analysis) is null)

    or (gcc.segment4 in (:p_sub_analysis)))

 and ((coalesce(null, :p_intercmpny) is null)

    or (gcc.segment5 in (:p_intercmpny)))

 and ((coalesce(null, :p_location) is null)

    or (gcc.segment6 in (:p_location)))

 and ((coalesce(null, :p_project) is null)

    or (gcc.segment7 in (:p_project)))

 and ((coalesce(null, :p_flow) is null)

    or (gcc.segment8 in (:p_flow)))

 and ((coalesce(null, :p_spare1) is null)

    or (gcc.segment9 in (:p_spare1)))

 and ((coalesce(null, :p_spare2) is null)

    or (gcc.segment10 in (:p_spare2)))

 and ((coalesce(null, :P_JE_SOURCE) is null)

    or (gjs.user_je_source_name in (:P_JE_SOURCE)))

 and ((coalesce(null, :P_JE_CATEGORY) is null)

    or (gjc.user_je_category_name in (:P_JE_CATEGORY)))

and gjh.period_name in (select period_name

                             from gl_periods

                            where start_date     >= (select start_date

                                                       from gl_periods

                                                      where period_name     = :P_PERIOD_FROM

                                                        and period_set_name = 'XX_GLOBAL_CAL')

                              and end_date       <= (select end_date

                                                       from gl_periods

                                                      where period_name     = :P_PERIOD_TO

                                                        and period_set_name = 'XX_GLOBAL_CAL')

                              and period_set_name = 'XX_GLOBAL_CAL')

Friday, 8 May 2020

Enterprise Structure Query in Oracle Fusion Cloud


SELECT   DISTINCT
        hroutl_bg.NAME enterprise,
        hroutl_bg.organization_id bu_id,
       lep.legal_entity_id,
       lep.NAME legal_entity,
       hroutl_ou.NAME bu_name,
       hroutl_ou.organization_id org_id,
      glev.FLEX_SEGMENT_VALUE
  FROM xle_entity_profiles lep,
       xle_registrations reg,
      hz_parties hzp,
       hr_operating_units hro,
       hr_all_organization_units_tl hroutl_bg,
       hr_all_organization_units_tl hroutl_ou,
       hr_organization_units gloperatingunitseo,
      gl_legal_entities_bsvs glev
WHERE lep.transacting_entity_flag = 'Y'
   AND lep.party_id = hzp.party_id
   AND lep.legal_entity_id = reg.source_id
   AND reg.source_table = 'XLE_ENTITY_PROFILES'
  AND reg.identifying_flag = 'Y'
   AND lep.legal_entity_id = hro.default_legal_context_id
   AND gloperatingunitseo.organization_id = hro.organization_id
   AND hroutl_bg.organization_id = hro.business_group_id
   AND hroutl_bg.language ='US'
   AND hroutl_ou.organization_id = hro.organization_id
   AND glev.legal_entity_id = lep.legal_entity_id;

Tuesday, 14 April 2020

AR Customer Tables , Architecture and joins , HZ tables and Joins in oracle Fusion Cloud

HZ tables Tables and Their Joins in Oracle Fusion Cloud  - 




Oracle Fusion Cloud - Customer Interface Status Error Codes

CUSTOMER INTERFACE STATUS ERROR CODES


Problem Description
-------------------

You are running Customer Interface RACUST and there is an error message in the
INTERFACE_STATUS column, but you don't know what it means.


Solution Description
--------------------

INTERFACE_STATUS appears in the following tables:
  RA_CUSTOMERS_INTERFACE_ALL
  RA_CUSTOMER_PROFILES_INT_ALL
  RA_CONTACT_PHONES_INT_ALL
  RA_CUSTOMER_BANKS_INT_ALL
  RA_CUST_PAY_METHOD_INTERFACE

These are the message codes and their meaning:

A1 --> The customer reference for update does not exist in RA_CUSTOMERS

A2 --> The address reference for update does not exist in RA_ADDRESSES

A3 --> Customer reference for insert is already defined in RA_CUSTOMERS

A4 --> Site use for this address reference already exists in the database

A5 --> Customer Number already assigned to a different customer

B1 --> ORIG_SYSTEM_ADDRESS_REF is mandatory when specifying an address

B2 --> ADDRESS1 is mandatory when specifying an address

B3 --> COUNTRY is mandatory when specifying an address

B4 --> SITE_USE_CODE is mandatory when inserting an address

B5 --> PRIMARY_SITE_USE_FLAG is mandatory when inserting an address

B6 --> CUSTOMER_CLASS_CODE is not defined in AR_LOOKUPS

B7 --> CUSTOMER_PROFILE_CLASS_NAME has an invalid value

B8 --> STATE is not defined in AR_LOCATION_VALUES

B9 --> COUNTRY is not defined in fnd_territories

B0 --> SITE_USE_CODE is not defined in AR_LOOKUPS

C1 --> This customer reference has two different customer names defined

C2 --> This customer reference has two different customer numbers defined

C3 --> This customer reference has two different parent customer references

C5 --> Customer reference has two different customer class codes defined

C6 --> This customer reference has two identical primary site uses defined

D1 --> Address reference has two different ADDRESS1 values

D2 --> Address reference has two different ADDRESS2 values

D3 --> Address reference has two different ADDRESS3 values

D4 --> Address reference has two different ADDRESS4 values

D5 --> Address reference has two different cities

D6 --> Address reference has two different postal codes

D7 --> Address reference has two different states

D8 --> Address reference has two different provinces

D9 --> Address reference has two different counties

D0 --> Address reference has two different countries

E1 --> Address reference has two identical site use codes

E2 --> Address reference has two different customers

F1 --> ORIG_SYSTEM_TELEPHONE_REF mandatory for telephone information

F2 --> TELEPHONE is mandatory when specifying telephone information

F3 --> TELEPHONE_TYPE is mandatory when specifying telephone information

F4 --> TELEPHONE_TYPE is not defined in AR_LOOKUPS

F5 --> Telephone reference for insert is already defined in RA_PHONES

F6 --> Telephone reference for update does not exist in RA_PHONES

G1 --> ORIG_SYSTEM_CONTACT_REF mandatory for contact information

G2 --> LAST_NAME is mandatory when specifying a contact

G3 --> CONTACT_TITLE is not defined in AR_LOOKUPS

G4 --> Contact reference for insert is already defined in RA_CONTACTS

G5 --> Contact reference for update is not defined in RA_CONTACTS

G6 --> The address reference specified is not defined for this customer

G7 --> CONTACT_JOB_TITLE must be defined in AR_LOOKUPS

H1 --> Contact reference has two different first names

H2 --> Contact reference has two different last names

H3 --> Contact reference has two different titles

H4 --> Contact reference has two different job titles

H5 --> Contact reference has two different customers

H6 --> Contact reference has two different addresses

I1 --> Telephone reference has two different phone numbers

I2 --> Telephone reference has multiple extensions

I3 --> Telephone reference has two different types

I4 --> Telephone reference has two different area codes

I6 --> Telephone reference has two different customers

I7 --> Telephone reference has two different addresses

J1 --> SITE_USE_CODE is not updateable

J2 --> PRIMARY_SITE_USE_FLAG is not updateable

J3 --> LOCATION is not updateable

J4 --> CUSTOMER_TYPE is not defined in AR_LOOKUPS

J5 --> PRIMARY_SITE_USE_FLAG has an invalid value

J6 --> CUSTOMER_NUMBER must be null when auto-numbering is set to "Yes"

J7 --> CUSTOMER_NUMBER is mandatory when auto-numbering is set to "No"

J8 --> INSERT_UPDATE_FLAG has an invalid value

J9 --> CUSTOMER_STATUS must have a value of 'A' or 'I'

K1 --> Concurrent request failed

K3 --> This customer reference has two different customer types defined

L1 --> COLLECTOR_NAME is mandatory when no profile class specified

L2 --> TOLERANCE is mandatory when no profile class specified

L3 --> DISCOUNT_TERMS is mandatory when no profile class specified

L4 --> DUNNING_LETTERS is mandatory when no profile class specified

L5 --> INTEREST_CHARGES is mandatory when no profile class specified

L6 --> STATEMENTS is mandatory when no profile class specified

L7 --> CREDIT_BALANCE_STATEMENTS mandatory when no profile class specified

L9 --> DUNNING_LETTER_SET_NAME is mandatory when DUNNING_LETTERS is "Yes"

L0 --> CHARGE_ON_FINANCE_CHARGE_FLAG mandatory when INTEREST_CHARGES is "Yes"

M1 --> INTEREST_PERIOD_DAYS is mandatory when INTEREST_CHARGES is "Yes"

M3 --> COLLECTOR_NAME has an invalid value

M4 --> CREDIT_CHECKING has an invalid value

M5 --> TOLERANCE has an invalid value

M6 --> DISCOUNT_TERMS has an invalid value

M7 --> DUNNING_LETTERS has an invalid value

M8 --> INTEREST_CHARGES has an invalid value

M9 --> STATEMENTS has an invalid value

M0 --> CREDIT_BALANCE_STATEMENTS has an invalid value

N1 --> CREDIT_HOLD has an invalid value

N2 --> CREDIT_RATING has an invalid value

N3 --> RISK_CODE has an invalid value

N4 --> STANDARD_TERM_NAME which contains the payment terms has an invalid value

N5 --> OVERRIDE_TERMS has an invalid value

N6 --> DUNNING_LETTER_SET_NAME has an invalid value

N7 --> STATEMENT_CYCLE_NAME has an invalid value

N8 --> ACCOUNT_STATUS has an invalid value

N9 --> PERCENT_COLLECTABLE has an invalid value

N0 --> AUTOCASH_HIERARCHY_NAME which contains the AutoCash rule has
       an invalid value

O1 --> STATEMENT_CYCLE_NAME is mandatory when STATEMENTS is "Yes"

O2 --> LOCATION must be null when auto-numbering is set to "Yes"

O3 --> LOCATION is mandatory when auto-numbering is set to "No"

O4 --> CREDIT_CHECKING is mandatory when profile class is null

O5 --> CHARGE_ON_FINANCE_CHARGE_FLAG must be null if INTEREST_CHARGES is No

O6 --> INTEREST_PERIOD_DAYS must be null if INTEREST_CHARGES is "No"

O7 --> INTEREST_PERIOD_DAYS must be greater than zero

P1 --> Postal Code is not in the defined range of system options

Q1 --> A new location was created for a value in an address segment field

Q2 --> Validation failed for the key location flexfield structure

R1 --> CUST_SHIP_VIA_CODE is not defined in ORG_FREIGHT

R2 --> CUSTOMER_CATEGORY_CODE is not defined in AR_LOOKUPS

R3 --> CUSTOMER_CATEGORY_CODE is not enabled in AR_LOOKUPS

R4 --> CUST_TAX_CODE is not defined in AR_VAT_TAX

R5 --> CUST_TAX_REFERENCE cannot be null when CUST_TAX_CODE is 'EXEMPT'

R6 --> SITE_USE_TAX_CODE is not defined in AR_VAT_TAX

R7 --> SITE_USE_TAX_REFERENCE is required when SITE_USE_TAX_CODE is 'EXEMPT'

R8 --> Invalid demand class code.

R9 --> SITE_SHIP_VIA_CODE not defined in ORG_FREIGHT

S1 --> The customer reference specified is invalid

S2 --> The address reference specified is invalid

S3 --> The address reference specified is not valid for this customer

S4 --> Payment Method is not defined in AR_RECEIPT_METHODS

S5 --> A bank account does not exist for the specified customer

S6 --> The end date specified cannot be before the start date

S7 --> The address specified must have an active BILL_TO site defined

T1 --> Customer payment method already active between the dates specified

T2 --> Customer site payment method already active between the dates specified

T3 --> Customer already has a primary payment method for specified dates

T4 --> Customer site has a primary payment method on the dates specified

T5 --> This customer payment method is already active in this date range

T6 --> Multiple primary payment methods defined

V2 --> The bank account specified must be of type 'EXTERNAL'

V3 --> Customer bank account is already active between the dates specified

V4 --> Customer site bank account already active between these dates

V5 --> This customer already has primary bank account for specified dates

V6 --> Customer site can have only 1 primary bank account for the dates
       specified

V7 --> Duplicate rows exist in Interface table for this Customer Bank and
       date run

V8 --> Duplicate primary customer banks defined within the interface table

W1 --> BANK_NAME is mandatory when creating a new bank account

W2 --> BANK_BRANCH_NAME is mandatory when creating a new bank account

W3 --> BANK_ACCOUNT_CURRENCY_CODE is mandatory creating a new bank account

W4 --> BANK_ACCOUNT_CURRENCY_CODE is not defined in FND_CURRENCIES

W5 --> Bank number already exists.

W6 --> Duplicate bank number in interface table.

W7 --> Primary flag should be 'Y' or 'N'.

W8 --> Duplicate bank and branch name in interface table.

W9 --> Duplicate Location

W0 --> Bank and branch name already exists.

X1 --> AUTO_REC_INCL_DISPUTED_FLAG mandatory when profile class is null

X2 --> TAX_PRINTING_OPTION is mandatory when no profile class specified

X3 --> GROUPING_RULE_NAME is mandatory when no profile class is specified

X4 --> CHARGE_ON_FINANCE_CHARGES_FLAG has an invalid value

X5 --> GROUPING_RULE_NAME has an invalid value

X6 --> CURRENCY_CODE has an invalid value

X7 --> CREDIT_BALANCE_STATEMENTS is mandatory when STATEMENTS is "Yes"

X8 --> CREDIT_BALANCE_STATEMENTS must be "No" when STATEMENTS is "No"

X9 --> STATEMENT_CYCLE_NAME must be null when STATEMENTS is "No"

X0 --> OVERRIDE_TERMS is mandatory when no profile class is specified

Y1 --> PARTY_NUMBER must be null when auto-numbering is set

Y2 --> PARTY_NUMBER is mandatory when auto-numbering is set to "No"

Y3 --> Party Number already assigned to a different party.

Y4 --> This party reference has two different party numbers defined in
       RA_CUSTOMERS_INTERFACE.

Y5 --> PERSON_FLAG has an invalid value

Y6 --> Party Site Number already assigned to a different address

Y7 --> Address reference has two different party site numbers defined in
       RA_CUSTOMERS_INTERFACE.

Y8 --> PARTY_SITE_NUMBER must be null when auto-numbering is set

Y9 --> PARTY_SITE_NUMBER is mandatory when auto-numbering is set to "No"

Z1 --> CREDIT_BALANCE_STATEMENTS must be null when STATEMENTS is null

Z2 --> STATEMENT_CYCLE_NAME must be null when STATEMENTS is null

Z3 --> CHARGE_ON_FINANCE_CHARGE_FLAG must be null when INTEREST_CHARGES is null

Z4 --> INTEREST_PERIOD_DAYS must be null when INTEREST_CHARGES is null

Z5 --> DISCOUNT_GRACE_DAYS must be null when DISCOUNT_TERMS is null

Z6 --> DISCOUNT_GRACE_DAYS must positive

Z7 --> DISCOUNT_GRACE_DAYS must be null when DISCOUNT_TERMS is "No"

Z8 --> DUNNING_LETTER_SET_NAME must be null when DUNNING_LETTERS is "No"

Z9 --> DUNNING_LETTER_SET_NAME must be null when DUNNING_LETTERS is null

Z0 --> CURRENCY_CODE is mandatory when a profile amount value is populated

a1 --> Customer record for insert must have validated profile record defined

a2 --> TAX_PRINTING_OPTION has an invalid value

a3 --> The customer profile for this customer reference already exists

a4 --> The customer profile class for update does not exist

a7 --> Duplicate record within the interface table

a8 --> Conflicting profile classes specified for this customer/site

b1 --> Both TRX_CREDIT_LIMIT and OVERALL_CREDIT_LIMIT must be populated

b2 --> TRX_CREDIT_LIMIT may not be greater than the OVERALL_CREDIT_LIMIT

b3 --> DUNNING_LETTER_SET_NAME must have a unique value

b4 --> COLLECTOR_NAME must have a unique value

b5 --> STANDARD_TERM_NAME must have a unique value

b6 --> STATEMENT_CYCLE_NAME must have a unique value

b7 --> BANK_ACCOUNT_NUM is mandatory when creating a new bank account

b8 --> AUTO_REC_INCL_DISPUTE_FLAG has an invalid value

b9 --> PAYMENT_GRACE_DAYS must be a positive value

e2 --> Bill_to_orig_address_ref should only be defined for Ship-to Addresses

e3 --> Bill_to_orig_address_ref is not a valid bill-to address

f1 --> You may have only one active Dunning site use for each customer

f2 --> For each customer, you may only have one active "Statements" type

f3 --> For each customer, you may only have one active Legal site

f4 --> Clearing Days must be greater than or equal to zero

f5 --> Address language is not installed

f6 --> Address reference has different languages

f7 --> Duplicate telephone reference in table RA_CONTACT_PHONES_INTERFACE

f8 --> A bank and branch with this bank number and branch number already exists

f9 --> Customer Prospect Code must be either CUSTOMER or PROSPECT

g1 --> This customer reference has two different customer prospect codes

u5 --> Contact reference has two different e-mail addresses

w2 --> CREDIT_CLASSIFICATION must have a valid value

w3 --> You cannot update the PARTY_TYPE using Customer Interface.
       Please do not specify a value for PARTY_TYPE when the
       INSERT_UPDATE_FLAG is set to U.

w4 --> When you create a PERSON party_type, you must provide
       PERSON_FIRST_NAME or PERSON_LAST_NAME.

y0 --> CONTACT_JOB_TITLE is not defined

y1 --> PHONE_COUNTRY_CODE is not defined in HZ_PHONE_COUNTRY_CODES

y2 --> This customer is already assigned to a different party

y3 --> This customer is already assigned to a different party

y4 --> LOCKBOX_MATCHING_OPTION must have a valid value

y6 --> TELEPHONE_TYPE cannot be updated from telex to any other type or any
       other type to telex.

y7 --> You cannot update this address. A printed, posted, or applied
       transaction with an associated tax line exists for this address

y8 --> ADDRESS_CATEGORY_CODE does not exist. Please enter a valid adress
       category code or define a new one using the Receivables Lookups
       window.

y9 --> ADDRESS_CATEGORY_CODE is not enabled. Please enable this address
       category by updating the Enabled flag in the Receivables Lookups
       window.

Thursday, 9 April 2020

Query to list All User Roles in Oracle ERP Cloud Fusion

SELECT *
FROM
  (SELECT user_info.user_login,
    user_info.user_id,
    user_roles.role_name,
    user_info.creation_date,
    user_info.first_name,
    user_info.last_name,
    user_info.location_code,
    user_info.location_name,
    user_info.town,
    user_info.country,
    user_info.department,
    user_info.username,
    user_info.active_flag
  FROM
    (SELECT DISTINCT pp.creation_date creation_date,
      ppf.first_name first_name,
      ppf.last_name last_name,
      hl.location_code location_code,
      hl.location_name location_name,
      hl.town_or_city town,
      hl.country country,
      pd.name department,
      pu.username username,
      pu.active_flag active_flag,
      au.user_id user_id,
      au.user_login user_login
    FROM per_persons pp,
      per_all_people_f papf,
      per_person_names_f_v ppf,
      hr_locations_all_f_vl hl,
      per_departments pd,
      per_all_assignments_m paaf,
      per_users pu,
      ase_user_vl au
    WHERE au.user_guid          = pu.user_guid(+)
    AND pu.person_id              = papf.person_id(+)
    AND papf.person_id           = pp.person_id(+)
    AND pp.person_id            = ppf.person_id(+)
    AND ppf.person_id       = paaf.person_id(+)
    AND paaf.location_id     = hl.location_id(+)
    AND paaf.organization_id = pd.organization_id(+)
    AND TRUNC(sysdate) BETWEEN NVL(ppf.effective_start_date,TRUNC(sysdate)) AND NVL(ppf.effective_end_date,TRUNC(sysdate))
    AND TRUNC(sysdate) BETWEEN NVL(papf.effective_start_date,TRUNC(sysdate)) AND NVL(papf.effective_end_date,TRUNC(sysdate))
    AND TRUNC(sysdate) BETWEEN NVL(paaf.effective_start_date,TRUNC(sysdate)) AND NVL(paaf.effective_end_date,TRUNC(sysdate))
    AND TRUNC(sysdate) BETWEEN NVL(hl.effective_start_date,TRUNC(sysdate)) AND NVL(hl.effective_end_date,TRUNC(sysdate))
    AND TRUNC(sysdate) BETWEEN NVL(pd.effective_start_date,TRUNC(sysdate)) AND NVL(pd.effective_end_date,TRUNC(sysdate))
    AND TRUNC(sysdate) BETWEEN NVL(au.effective_start_date,TRUNC(sysdate)) AND NVL(au.effective_end_date,TRUNC(sysdate))
    )user_info
  LEFT JOIN
    (SELECT u.user_login user_login,
      r.role_name role_name,
      r.description description ,
      aurm.user_id user_id
    FROM ase_user_vl u,
      ase_role_vl r,
      ase_user_role_mbr aurm
    WHERE r.role_id              = aurm.role_id
    AND aurm.user_id             =u.user_id
    AND r.effective_end_date    IS NULL
    AND aurm.effective_end_date IS NULL
    )user_roles
  ON user_info.user_login=user_roles.user_login
  ) qrslt
WHERE 1 = 1
  AND UPPER(QRSLT.username) = UPPER('abcd@oracletechcenter.blogspot.com')
ORDER BY user_login

Wednesday, 25 July 2018

Transaction Reconcile query for Payment,Receipts,Bank Statements QUERY

Transaction Reconcile query for Payment,Receipts,Bank Statements QUERY


SELECT cbe.bank_account_num "BANK_ACCOUNT_NUMBER" /* RECEIPTS QUERY*/
 ,  to_char(acrha.gl_date, 'DD-MON-YYYY') "GL_DATE"
 , 'RECEIPT' "TYPE"
 , hp.party_name "NAME"
 , TO_CHAR(acra.receipt_date, 'DD-MON-YYYY') "TRANSACTION_DATE"
 , TO_CHAR(aps.due_date, 'DD-MON-YYYY') "MATURITY_DATE"
 , acrha.status "STATUS"
 , to_char(acra.receipt_number) "NUMBER"
 , acra.currency_code "CURRENCY"
 , NULL "LINE_NUMBER"
 , acrha.acctd_amount "FUNCTIONAL_AMOUNT"
 , acrha.amount "ENTERED_AMOUNT"
FROM apps.ar_cash_receipts_all acra
 , apps.ar_cash_receipt_history_all acrha
 , apps.hz_cust_accounts hca
 , apps.hz_parties hp
 , apps.ar_payment_schedules_all aps
 , apps.ar_receipt_methods arm
 , apps.ar_receipt_classes arc
 , ce_bank_accounts cbe   
 , ce_bank_acct_uses_all cba
 WHERE acra.cash_receipt_id = acrha.cash_receipt_id
 AND acra.org_id = acrha.org_id
 AND cba.bank_acct_use_id = acra.remit_bank_acct_use_id
 AND cba.bank_account_id = cbe.bank_account_id(+)
 AND EXISTS (select 1 from apps.q_bank_account_reconcile_v where bank_account_number=cbe.bank_account_num and exclude='N')
 AND SYSDATE BETWEEN NVL(cbe.start_date,SYSDATE)  AND NVL(cbe.end_date,SYSDATE)
 AND cba.bank_account_id= NVL (:p_num_bank_account_id, cba.bank_account_id)
 AND trunc(acrha.gl_date) <= trunc(:P_DTE_GL_DATE)
 AND acra.pay_from_customer = hca.cust_account_id(+)
 AND hca.party_id = hp.party_id(+)
 AND acra.cash_receipt_id = aps.cash_receipt_id(+)
 AND acra.receipt_method_id = arm.receipt_method_id
 AND acrha.cash_receipt_history_id =
 (SELECT MAX (cash_receipt_history_id)
 FROM apps.ar_cash_receipt_history_all acrha1
 WHERE acra.cash_receipt_id = acrha1.cash_receipt_id
 AND trunc(acrha1.gl_date) <= trunc(:P_DTE_GL_DATE)
 )
 AND acrha.status NOT IN ('CLEARED', 'REVERSED')
 AND acra.receipt_method_id = arm.receipt_method_id
 AND arm.receipt_class_id = arc.receipt_class_id
 AND arc.notes_receivable <> 'Y'
 UNION ALL
 SELECT distinct cbe.bank_account_num "BANK_ACCOUNT_NUMBER"  /* PAYMENT QUERY*/
 , TO_CHAR (NVL (apc.cleared_date, :P_DTE_GL_DATE), 'DD-MON-YYYY') "GL_DATE"
 ,'PAYMENT' "TYPE"
 , apc.vendor_name "NAME"
 , TO_CHAR(apc.check_date, 'DD-MON-YYYY') "TRANSACTION_DATE"
 , NULL "MATURITY_DATE"
 , apc.status_lookup_code "STATUS"
 , to_char(apc.check_number) "NUMBER"
 , apc.currency_code "CURRENCY"
 , NULL "LINE_NUMBER"
 , NVL (apc.base_amount, apc.amount) "FUNCTIONAL_AMOUNT"
 , apc.amount "ENTERED_AMOUNT"
 FROM ap_checks_all apc
 , FND_LOOKUP_VALUES alc
 , ce_bank_accounts cbe
 , ap_invoice_payments_all aip
 , ce_bank_acct_uses_all cba   
 WHERE 1=1
 AND cba.bank_account_id = NVL (:p_num_bank_account_id, cba.bank_account_id)
 AND apc.status_lookup_code = alc.lookup_code
 and aip.check_id = apc.check_id
 AND alc.lookup_type = 'CHECK STATE'
 AND alc.LANGUAGE = USERENV ('LANG')
 AND alc.VIEW_APPLICATION_ID = 200
 AND cba.bank_acct_use_id = apc.ce_bank_acct_use_id
 AND cba.bank_account_id = cbe.bank_account_id(+)
 AND EXISTS (select 1 from apps.Q_BANK_ACCOUNT_RECONCILE_V where bank_account_number=cbe.bank_account_num and exclude='N')
 AND apc.check_date BETWEEN (SELECT MIN(check_date) FROM apps.ap_checks_all) AND trunc(:P_DTE_GL_DATE)
 UNION ALL
 SELECT cbe.bank_account_num "BANK_ACCOUNT_NUMBER" /* JOURNAL QUERY*/
      ,to_char(:P_DTE_GL_DATE,'DD-MON-YYYY') "GL_DATE"
      ,UPPER(l2.meaning) "TYPE"
      , null "NAME"
      --, TO_CHAR (jel.effective_date, 'DD-MON-YYYY') "TRANSACTION_DATE"
      , TO_CHAR(jel.effective_date, 'DD-MON-YYYY') "TRANSACTION_DATE"
      , NULL "MATURITY_DATE"
      , NULL "STATUS"
      , NULL "NUMBER"
      , jeh.currency_code "CURRENCY"
      , NULL "LINE_NUMBER"
      , DECODE (l2.meaning , 'Debit', (DECODE (NVL (jel.entered_dr , NVL (jel.accounted_dr, 0) ) , 0, jel.accounted_cr , jel.accounted_dr ) ) , 'Credit', -1 *
           (DECODE (NVL (jel.entered_dr, NVL (jel.accounted_dr, 0) ) , 0, jel.accounted_cr , jel.accounted_dr ) ) ) "FUNCTIONAL_AMOUNT"
      , DECODE (l2.meaning , 'Debit', (DECODE (NVL (jel.entered_dr , NVL (jel.accounted_dr, 0) ) , 0, jel.entered_cr , jel.entered_dr ) ) , 'Credit', -1 *
           (DECODE (NVL (jel.entered_dr , NVL (jel.accounted_dr, 0) ) , 0, jel.entered_dr , jel.entered_cr ) ) ) "ENTERED_AMOUNT"
 FROM apps.gl_je_lines jel
    , apps.ce_lookups l2
    , apps.gl_sets_of_books sob
    , apps.ce_system_parameters SYS
    , apps.gl_je_headers jeh
    , apps.ce_statement_reconcils_all cre
    , ce_bank_accounts cbe
 WHERE l2.lookup_type = 'TRX_TYPE'
 AND l2.lookup_code = DECODE (DECODE (NVL (jel.entered_dr, 0) , 0, NVL (jel.accounted_dr, 0) , jel.entered_dr ) , 0, 'JE_CREDIT' , 'JE_DEBIT' )
 AND DECODE (cbe.currency_code , sob.currency_code, jeh.currency_code , cbe.currency_code) = jeh.currency_code
 AND jeh.je_header_id = jel.je_header_id
 AND jeh.je_source NOT IN('Payables', 'Receivables', 'AP Translator', 'AR Translator', 'Treasury')
 AND EXISTS (select 1 from apps.q_bank_account_reconcile_v where bank_account_number=cbe.bank_account_num and exclude='N')
 AND cre.je_header_id(+) = jel.je_header_id
 AND cre.reference_id(+) = jel.je_line_num
 AND cre.reference_type(+) = 'JE_LINE'
 AND NVL (cre.status_flag, 'U') = 'U'
 AND NVL (cre.current_record_flag, 'Y') = 'Y'
 AND '' || jel.status(+) = 'P'
 AND jel.code_combination_id = cbe.asset_code_combination_id
 AND sob.set_of_books_id = SYS.set_of_books_id
 AND jel.ledger_id = SYS.set_of_books_id
 AND jel.effective_date >= SYS.cashbook_begin_date
 AND jeh.je_category <> 'Revaluation'
 AND NOT EXISTS (
 SELECT NULL
 FROM apps.ce_statement_reconcils_all cre2
 , apps.ce_system_parameters sys2
 WHERE jel.je_header_id = cre2.je_header_id
 AND jel.je_line_num = cre2.reference_id
 AND cre2.status_flag = 'M'
 AND NVL (cre2.current_record_flag, 'Y') = 'Y'
 AND sys2.set_of_books_id = jel.ledger_id
 )
 AND cbe.bank_account_id = NVL (:p_num_bank_account_id, cbe.bank_account_id)
 AND trunc(jel.effective_date) <= trunc(:P_DTE_GL_DATE)
UNION ALL
 SELECT cbe.bank_account_num "BANK_ACCOUNT_NUMBER",
       TO_CHAR (csha.gl_date, 'DD-MON-YYYY') "GL_DATE",
       DECODE (csl.trx_type,
               'CREDIT', 'BANK RECEIPT',
               'DEBIT', 'BANK PAYMENT',
               csl.trx_type)
          "TYPE",
       csha.statement_number "NAME",
       TO_CHAR (csl.trx_date, 'DD-MON-YYYY') "TRANSACTION_DATE",
       NULL "MATURITY_DATE",
       DECODE(csl.status,'RECONCILED','PARTIAL RECONCILED',csl.status) "STATUS",
       TO_CHAR (csl.bank_trx_number) "NUMBER",
       csl.currency_code "CURRENCY",
       csl.line_number "LINE_NUMBER",
       DECODE(csl.status,'RECONCILED',(select csl.amount-SUM(cr.amount) from ce_statement_reconcils_all cr where csl.STATEMENT_LINE_ID = cr.STATEMENT_LINE_ID),csl.amount) "FUNCTIONAL_AMOUNT",
      DECODE(csl.status,'RECONCILED',(select csl.amount-SUM(cr.amount) from ce_statement_reconcils_all cr where csl.STATEMENT_LINE_ID = cr.STATEMENT_LINE_ID),csl.amount)  "ENTERED_AMOUNT"
     -- csl.amount "ENTERED_AMOUNT"
  FROM apps.CE_STATEMENT_HEADERS csha,
       apps.ce_statement_lines csl,
       ce_bank_accounts cbe
 WHERE     csha.bank_account_id = cbe.bank_account_id
    AND csha.bank_account_id =
              NVL ( :p_num_bank_account_id, csha.bank_account_id)
       AND TRUNC (csha.statement_date) <= TRUNC ( :P_DTE_GL_DATE)
       AND csha.statement_header_id = csl.statement_header_id
       AND EXISTS
              (SELECT 1
                 FROM apps.q_bank_account_reconcile_v
                WHERE     bank_account_number = cbe.bank_account_num
                      AND exclude = 'N')
       AND NOT EXISTS
                  (SELECT 1
                     FROM ce_statement_lines csl1,
                          ce_statement_reconcils_all csr
                    WHERE     csl1.STATEMENT_HEADER_ID =
                                 csl.STATEMENT_HEADER_ID
                          AND csl1.STATEMENT_LINE_ID = csr.STATEMENT_LINE_ID
                          AND csl1.STATEMENT_LINE_ID = csl.STATEMENT_LINE_ID
                          AND csl1.amount = csr.amount
                          AND csr.status_flag = 'M'
                          AND csr.current_record_flag = 'Y')
       AND UPPER (csha.statement_number) NOT LIKE '%DUMMY ST%'
       AND SYSDATE BETWEEN NVL (cbe.start_date, SYSDATE)
                       AND NVL (cbe.end_date, SYSDATE)

Tuesday, 6 February 2018

QUERY TO LINK BETWEEN FA , AP AND RCV SHIPMENTS

SELECT DISTINCT gcc.segment1 bc,
         gcc.segment2 rc,
         gcc.segment3 department_code,
         fad.asset_number asset_number,
         fad.attribute_category_code asset_category,
         fad.description asset_desc,
         fab.cost cost,
         fai.po_number,
         fai.invoice_number,
         CONVERT (fai.description, 'AL32UTF8') line_description,
         fai.fixed_assets_cost line_amount,
         aps.segment1 supplier_number,
         aps.vendor_name supplier_name,
         to_char(fab.date_placed_in_service,'DD-MON-YY') date_in_service
         ,fth.transaction_type_code transaction_type
         ,ftv.period_effective effective_period
         ,ftv.period_entered
         ,aia.doc_sequence_value voucher_num
         ,aia.invoice_currency_code invoice_currency
         ,rsh.receipt_num receipt_number
         ,to_char(rsh.creation_date,'DD-MON-YY') receipt_date
    FROM fa_additions_v fad,
         fa_books fab,
         fa_distribution_history fdh,
         gl_code_combinations gcc,
         fa_asset_invoices fai,
         ap_suppliers aps,
         ap_invoices_all aia,
         fa_book_controls bc,
         ap_invoice_distributions_all aida
         ,fa_transaction_headers fth
         ,fa_transaction_history_trx_v ftv
         ,fa_lookups_tl lotl,
         fa_lookups_b flb
         ,rcv_transactions rt
         ,rcv_shipment_headers rsh
   WHERE     fad.asset_id = fdh.asset_id
         AND fab.book_type_code = :p_book_type
         AND fab.Date_ineffective IS NULL
         AND fai.invoice_id = aia.invoice_id
         AND aida.invoice_id = aia.invoice_id
         AND bc.book_type_code = fab.book_type_code
         AND fdh.transaction_header_id_in = fth.transaction_header_id
         AND flb.lookup_code = lotl.lookup_code
         AND flb.lookup_code = fth.transaction_type_code
         AND rt.transaction_id = aida.rcv_transaction_id
         AND rt.shipment_header_id = rsh.shipment_header_id
         AND ftv.transaction_header_id = fth.transaction_header_id
         AND fdh.date_ineffective IS NULL   
         AND(( (:p_from_period = :p_to_period) AND
                    fdh.date_effective < (NVL((SELECT dp.period_close_date
                   FROM fa_deprn_periods dp
                  WHERE dp.book_type_code = :p_book_type
                        AND dp.period_name = :p_to_period),SYSDATE)))
                OR ( fdh.date_effective between
                (SELECT dp.period_open_date
                   FROM fa_deprn_periods dp
                  WHERE dp.book_type_code = :p_book_type
                        AND dp.period_name = :p_from_period)
            AND NVL((SELECT dp.period_close_date
                   FROM fa_deprn_periods dp
                  WHERE dp.book_type_code = :p_book_type
                        AND dp.period_name = :p_to_period),SYSDATE)) )     
         AND fab.book_type_code = fdh.book_type_code
         AND fab.asset_id = fdh.asset_id
         AND fdh.code_combination_id = gcc.code_combination_id
AND fad.asset_type = DECODE(:p_asset_type,'ALL', fad.asset_type,:p_asset_type)
         AND gcc.segment1 = DECODE (:p_chr_bc, 'ALL', gcc.segment1, :p_chr_bc)
         AND gcc.segment2 = DECODE (:p_chr_rc, 'ALL', gcc.segment2, :p_chr_rc)
         AND fdh.asset_id = fai.asset_id(+)
         AND fai.date_ineffective IS NULL
         AND aps.vendor_id(+) = fai.po_vendor_id
ORDER BY fad.asset_number