GL Account Analysis - Script
GL Account Analysis - Script
source,
category,
gl_date,
line_description,
segment3,
transaction_number,
debit,
credit,
round(conv_rate * debit, 3) debit_usd,
round(conv_rate * credit, 3) credit_usd,
begin_balance,
closing_balance
FROM
(
SELECT DISTINCT
gjb.name batch_name,
gjs.user_je_source_name source,
gjc.user_je_category_name category,
gjh.name journal_name,
xal.accounting_date gl_date,
gjh.je_header_id
|| '_'
|| gjl.je_line_num unique_id,
xal.description line_description,
gcc.segment3,
aia.invoice_num transaction_number,
gjh.period_name,
gjl.accounted_dr debit,
gjl.accounted_cr credit,
gjh.doc_sequence_value document_no,
decode(gjl.status, 'P', 'POSTED', 'U', 'UNPOSTED',
gjl.status) status,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) begin_balance,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr +
bal.period_net_dr - bal.period_net_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) closing_balance,
(
SELECT DISTINCT
decode(conversion_rate, NULL, 1, conversion_rate)
FROM
apps.gl_daily_rates
WHERE
1 = 1
AND from_currency = 'KWD'
AND to_currency = 'USD'
AND trunc(conversion_date) LIKE gjl.effective_date
AND ROWNUM = 1
) conv_rate
FROM
apps.gl_je_headers gjh,
apps.gl_je_lines gjl,
apps.gl_je_sources gjs,
apps.gl_je_categories gjc,
apps.gl_je_batches gjb,
apps.gl_code_combinations gcc,
apps.gl_import_references gir,
apps.xla_ae_lines xal,
apps.xla_ae_headers xah,
apps.xla_distribution_links xdl,
apps.ap_invoice_distributions_all aida,
apps.ap_invoices_all aia,
apps.ap_invoice_lines_all aila
WHERE
gjh.je_header_id = gjl.je_header_id
AND gjh.je_category = gjc.je_category_name (+)
AND gjh.je_source = gjs.je_source_name (+)
AND gjb.je_batch_id = gjh.je_batch_id
AND gir.je_line_num (+) = gjl.je_line_num
AND gir.je_header_id (+) = gjl.je_header_id
AND gcc.code_combination_id = gjl.code_combination_id
AND xal.gl_sl_link_id (+) = gir.gl_sl_link_id
AND xal.application_id = xah.application_id
AND xal.ae_header_id = xah.ae_header_id
AND xdl.ae_header_id (+) = xal.ae_header_id
AND xdl.ae_line_num (+) = xal.ae_line_num
AND aida.invoice_distribution_id (+) = xdl.source_distribution_id_num_1
AND aia.invoice_id (+) = aida.invoice_id
AND gjl.status = 'P'
AND aia.invoice_id (+) = xdl.applied_to_source_id_num_1
AND aia.invoice_id = aila.invoice_id (+)
AND gjh.je_source IN ( 'Payables' )
AND gjh.je_category NOT IN ( 'Payments', 'Reconciled Payments' )
AND aila.line_number (+) = aida.invoice_line_number
AND gjh.period_name BETWEEN :p_from_period AND :p_to_period
)
UNION
SELECT
source,
category,
gl_date,
line_description,
segment3,
transaction_number,
debit,
credit,
round(conv_rate * debit, 3) debit_usd,
round(conv_rate * credit, 3) credit_usd,
begin_balance,
closing_balance
FROM
(
SELECT
gjb.name batch_name,
gjs.user_je_source_name source,
gjc.user_je_category_name category,
gjh.name journal_name,
xal.accounting_date gl_date,
gjh.je_header_id
|| '_'
|| gjl.je_line_num unique_id,
xal.description line_description,
gcc.segment3,
CAST(aca.check_number AS VARCHAR2(30)) transaction_number,
gjh.period_name,
gjl.accounted_dr debit,
gjl.accounted_cr credit,
gjh.doc_sequence_value document_no,
decode(gjl.status, 'P', 'POSTED', 'U', 'UNPOSTED',
gjl.status) status,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) begin_balance,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr +
bal.period_net_dr - bal.period_net_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) closing_balance,
(
SELECT DISTINCT
decode(conversion_rate, NULL, 1, conversion_rate)
FROM
apps.gl_daily_rates
WHERE
1 = 1
AND from_currency = 'KWD'
AND to_currency = 'USD'
AND trunc(conversion_date) LIKE gjl.effective_date
AND ROWNUM = 1
) conv_rate
FROM
apps.xla_ae_headers xah,
apps.gl_je_categories gjc,
apps.gl_je_sources gjs,
apps.xla_ae_lines xal,
apps.gl_je_batches gjb,
apps.gl_je_lines gjl,
apps.gl_import_references gir,
apps.gl_je_headers gjh,
apps.gl_code_combinations gcc,
apps.ap_suppliers asup,
apps.ap_checks_all aca
WHERE
xah.ae_header_id = xal.ae_header_id
AND gjh.je_category = gjc.je_category_name (+)
AND gjh.je_source = gjs.je_source_name (+)
AND gjl.je_line_num = gir.je_line_num
AND gjl.je_header_id = gir.je_header_id
AND gjb.je_batch_id = gjh.je_batch_id
AND gir.gl_sl_link_table = xal.gl_sl_link_table
AND gir.gl_sl_link_id = xal.gl_sl_link_id
AND gjl.je_header_id = gjh.je_header_id
AND gjl.code_combination_id = gcc.code_combination_id
AND asup.vendor_id (+) = xal.party_id
AND aca.doc_sequence_id (+) = xah.doc_sequence_id
AND aca.doc_sequence_value (+) = xah.doc_sequence_value
AND gjh.status = 'P'
AND gjh.je_category NOT IN ( 'Purchase Invoices' )
AND gjh.period_name BETWEEN :p_from_period AND :p_to_period
AND gjh.je_source = 'Payables'
)
UNION
SELECT
source,
category,
gl_date,
line_description,
segment3,
transaction_number,
debit,
credit,
round(conv_rate * debit, 3) debit_usd,
round(conv_rate * credit, 3) credit_usd,
begin_balance,
closing_balance
FROM
(
SELECT DISTINCT
gjb.name batch_name,
gjs.user_je_source_name source,
gjc.user_je_category_name category,
gjh.name journal_name,
xal.accounting_date gl_date,
gjh.je_header_id
|| '_'
|| gjl.je_line_num unique_id,
xal.description line_description,
gcc.segment3,
decode(rcta.trx_number,
NULL,
regexp_substr(xah.description, '[0-9]+'),
rcta.trx_number) transaction_number,
gjh.period_name,
gjl.accounted_dr debit,
gjl.accounted_cr credit,
gjh.doc_sequence_value document_no,
decode(gjl.status, 'P', 'POSTED', 'U', 'UNPOSTED',
gjl.status) status,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) begin_balance,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr +
bal.period_net_dr - bal.period_net_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) closing_balance,
(
SELECT DISTINCT
decode(conversion_rate, NULL, 1, conversion_rate)
FROM
apps.gl_daily_rates
WHERE
1 = 1
AND from_currency = 'KWD'
AND to_currency = 'USD'
AND trunc(conversion_date) LIKE gjl.effective_date
AND ROWNUM = 1
) conv_rate
FROM
apps.gl_je_headers gjh,
apps.gl_je_categories gjc,
apps.gl_je_sources gjs,
apps.gl_je_lines gjl,
apps.gl_je_batches gjb,
apps.gl_code_combinations gcc,
apps.gl_import_references gir,
apps.xla_ae_lines xal,
apps.xla_ae_headers xah,
apps.xla_distribution_links xdl,
apps.ra_cust_trx_line_gl_dist_all rctlga,
apps.ra_customer_trx_all rcta,
apps.ra_customer_trx_lines_all rctla
WHERE
gjh.je_header_id = gjl.je_header_id
AND gjh.je_category = gjc.je_category_name (+)
AND gjh.je_source = gjs.je_source_name (+)
AND gjb.je_batch_id = gjh.je_batch_id
AND gir.je_line_num (+) = gjl.je_line_num
AND gir.je_header_id (+) = gjl.je_header_id
AND gcc.code_combination_id = gjl.code_combination_id
AND gcc.code_combination_id = rctlga.code_combination_id (+)
AND xal.gl_sl_link_id = gir.gl_sl_link_id
AND xal.application_id = xah.application_id
AND xal.ae_header_id = xah.ae_header_id
AND xdl.ae_header_id (+) = xal.ae_header_id
AND xdl.ae_line_num (+) = xal.ae_line_num
AND gjl.status = 'P'
AND rctlga.cust_trx_line_gl_dist_id (+) =
xdl.source_distribution_id_num_1
AND rcta.customer_trx_id (+) = rctlga.customer_trx_id
AND rctlga.customer_trx_line_id = rctla.customer_trx_line_id (+)
AND gjh.je_source = 'Receivables'
AND gjh.je_category NOT IN ( 'Receipts', 'Misc Receipts' )
AND ( gjh.period_name ) BETWEEN ( :p_from_period ) AND ( :p_to_period )
)
UNION
SELECT
source,
category,
gl_date,
line_description,
segment3,
transaction_number,
debit,
credit,
round(conv_rate * debit, 3) debit_usd,
round(conv_rate * credit, 3) credit_usd,
begin_balance,
closing_balance
FROM
(
SELECT DISTINCT
gjb.name batch_name,
gjs.user_je_source_name source,
gjc.user_je_category_name category,
gjh.name journal_name,
xal.accounting_date gl_date,
gjh.je_header_id
|| '_'
|| gjl.je_line_num unique_id,
xal.description line_description,
gcc.segment3,
acra.receipt_number transaction_number,
gjh.period_name,
gjl.accounted_dr debit,
gjl.accounted_cr credit,
gjh.doc_sequence_value document_no,
decode(gjl.status, 'P', 'POSTED', 'U', 'UNPOSTED',
gjl.status) status,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) begin_balance,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr +
bal.period_net_dr - bal.period_net_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) closing_balance,
(
SELECT DISTINCT
decode(conversion_rate, NULL, 1, conversion_rate)
FROM
apps.gl_daily_rates
WHERE
1 = 1
AND from_currency = 'KWD'
AND to_currency = 'USD'
AND trunc(conversion_date) LIKE gjl.effective_date
AND ROWNUM = 1
) conv_rate
FROM
apps.gl_je_headers gjh,
apps.gl_je_lines gjl,
apps.gl_je_categories gjc,
apps.gl_je_sources gjs,
apps.gl_je_batches gjb,
apps.gl_code_combinations gcc,
apps.gl_import_references gir,
apps.xla_ae_lines xal,
apps.xla_ae_headers xah,
apps.xla_distribution_links xdl,
apps.ar_cash_receipts_all acra,
apps.ar_distributions_all ada,
apps.ar_cash_receipt_history_all acrh
WHERE
gjh.je_header_id = gjl.je_header_id
AND gjh.je_category = gjc.je_category_name (+)
AND gjh.je_source = gjs.je_source_name (+)
AND gjb.je_batch_id = gjh.je_batch_id
AND gir.je_line_num (+) = gjl.je_line_num
AND gir.je_header_id (+) = gjl.je_header_id
AND gcc.code_combination_id = gjl.code_combination_id
AND xal.gl_sl_link_id = gir.gl_sl_link_id
AND xal.application_id = xah.application_id
AND xal.ae_header_id = xah.ae_header_id
AND xdl.ae_header_id (+) = xal.ae_header_id
AND xdl.ae_line_num (+) = xal.ae_line_num
AND gjl.status = 'P'
AND acra.cash_receipt_id = acrh.cash_receipt_id
AND acrh.cash_receipt_history_id = ada.source_id
AND xdl.source_distribution_id_num_1 = ada.line_id (+)
AND gjh.je_source = 'Receivables'
AND gjh.je_category IN ( 'Receipts', 'Misc Receipts' )
AND ( gjh.period_name ) BETWEEN ( :p_from_period ) AND ( :p_to_period )
)
UNION
SELECT
source,
category,
gl_date,
line_description,
segment3,
transaction_number,
debit,
credit,
round(conv_rate * debit, 3) debit_usd,
round(conv_rate * credit, 3) credit_usd,
begin_balance,
closing_balance
FROM
(
SELECT DISTINCT
gjb.name batch_name,
gjs.user_je_source_name source,
gjc.user_je_category_name category,
gjh.name journal_name,
xal.accounting_date gl_date,
gjh.je_header_id
|| '_'
|| gjl.je_line_num unique_id,
xal.description line_description,
gcc.segment3,
acra.receipt_number transaction_number,
gjh.period_name,
gjl.accounted_dr debit,
gjl.accounted_cr credit,
gjh.doc_sequence_value document_no,
decode(gjl.status, 'P', 'POSTED', 'U', 'UNPOSTED',
gjl.status) status,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) begin_balance,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr +
bal.period_net_dr - bal.period_net_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) closing_balance,
(
SELECT DISTINCT
decode(conversion_rate, NULL, 1, conversion_rate)
FROM
apps.gl_daily_rates
WHERE
1 = 1
AND from_currency = 'KWD'
AND to_currency = 'USD'
AND trunc(conversion_date) LIKE gjl.effective_date
AND ROWNUM = 1
) conv_rate
FROM
apps.gl_je_headers gjh,
apps.gl_je_lines gjl,
apps.gl_je_categories gjc,
apps.gl_je_sources gjs,
apps.gl_je_batches gjb,
apps.gl_code_combinations gcc,
apps.gl_import_references gir,
apps.xla_ae_lines xal,
apps.xla_ae_headers xah,
apps.xla_distribution_links xdl,
apps.ar_cash_receipts_all acra,
apps.ar_distributions_all ada,
apps.ar_misc_cash_distributions_all mcd
WHERE
gjh.je_header_id = gjl.je_header_id
AND gjh.je_category = gjc.je_category_name (+)
AND gjh.je_source = gjs.je_source_name (+)
AND gjb.je_batch_id = gjh.je_batch_id
AND gir.je_line_num (+) = gjl.je_line_num
AND gir.je_header_id (+) = gjl.je_header_id
AND gcc.code_combination_id = gjl.code_combination_id
AND xal.gl_sl_link_id = gir.gl_sl_link_id
AND xal.application_id = xah.application_id
AND xal.ae_header_id = xah.ae_header_id
AND xdl.ae_header_id (+) = xal.ae_header_id
AND xdl.ae_line_num (+) = xal.ae_line_num
AND gjl.status = 'P'
AND mcd.misc_cash_distribution_id = ada.source_id
AND acra.cash_receipt_id = mcd.cash_receipt_id
AND xdl.source_distribution_id_num_1 = ada.line_id (+)
AND gjh.je_source = 'Receivables'
AND gjh.je_category NOT IN ( 'Receipts' )
AND ( gjh.period_name ) BETWEEN ( :p_from_period ) AND ( :p_to_period )
)
UNION
SELECT
source,
category,
gl_date,
line_description,
segment3,
transaction_number,
debit,
credit,
round(conv_rate * debit, 3) debit_usd,
round(conv_rate * credit, 3) credit_usd,
begin_balance,
closing_balance
FROM
(
SELECT DISTINCT
gjb.name batch_name,
gjs.user_je_source_name source,
gjc.user_je_category_name category,
gjh.name journal_name,
gjh.default_effective_date gl_date,
gjh.je_header_id
|| '_'
|| gjl.je_line_num unique_id,
gjl.description line_description,
gcc.segment3,
NULL transaction_number,
gjh.period_name,
gjl.accounted_dr debit,
gjl.accounted_cr credit,
gjh.doc_sequence_value document_no,
decode(gjl.status, 'P', 'POSTED', 'U', 'UNPOSTED',
gjl.status) status,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) begin_balance,
(
SELECT
nvl(SUM(bal.begin_balance_dr - bal.begin_balance_cr +
bal.period_net_dr - bal.period_net_cr),
0)
FROM
apps.gl_balances bal
WHERE
1 = 1
AND gcc.code_combination_id = bal.code_combination_id
AND bal.period_name = gjh.period_name
AND gcc.template_id IS NULL
AND bal.translated_flag IS NULL
) closing_balance,
(
SELECT DISTINCT
decode(conversion_rate, NULL, 1, conversion_rate)
FROM
apps.gl_daily_rates
WHERE
1 = 1
AND from_currency = 'KWD'
AND to_currency = 'USD'
AND trunc(conversion_date) LIKE gjl.effective_date
AND ROWNUM = 1
) conv_rate
FROM
apps.gl_je_headers gjh,
apps.gl_je_categories gjc,
apps.gl_je_sources gjs,
apps.gl_je_lines gjl,
apps.gl_je_batches gjb,
apps.gl_code_combinations gcc,
apps.gl_import_references gir,
apps.xla_ae_lines xal
WHERE
gjh.je_header_id = gjl.je_header_id
AND gjh.je_category = gjc.je_category_name (+)
AND gjh.je_source = gjs.je_source_name (+)
AND gjb.je_batch_id = gjh.je_batch_id
AND gir.je_line_num (+) = gjl.je_line_num
AND gir.je_header_id (+) = gjl.je_header_id
AND gcc.code_combination_id = gjl.code_combination_id
AND xal.gl_sl_link_id (+) = gir.gl_sl_link_id
AND gjl.status = 'P'
AND gjh.je_source NOT IN ( 'Payables', 'Receivables' )
AND gjh.period_name BETWEEN :p_from_period AND :p_to_period
);