Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

---aging from data

select sum(amt_due_remaining) from (


select
ps.payment_schedule_id payment_sched_id,
ps.class,
ps.amt_due_remaining
from (
select a.customer_id,
a.customer_site_use_id ,
a.customer_trx_id,
a.payment_schedule_id,
a.class ,
sum(a.primary_salesrep_id) primary_salesrep_id,
a.due_date ,
sum(a.amount_due_remaining) amt_due_remaining,
a.trx_number,
a.amount_adjusted,
a.amount_applied ,
a.amount_credited ,
a.amount_adjusted_pending,
a.gl_date ,
a.cust_trx_type_id,
a.invoice_currency_code,
a.exchange_rate
from (
SELECT
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class ,
0 primary_salesrep_id,
ps.due_date ,
nvl(sum(nvl(adj.acctd_amount, 0)),0) * (-1) amount_due_remaining,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate,1) exchange_rate
from ar_payment_schedules_all ps,
ar_adjustments_all adj,
hz_cust_accounts cust_acct,
hz_parties party
where ps.gl_date <= to_date('02-JUL-2017')-1
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and ps.customer_id > 0
and ps.gl_date_closed > to_date('02-JUL-2017') -1
and adj.payment_schedule_id = ps.payment_schedule_id
and adj.status = 'A'
and adj.gl_date > to_date('02-JUL-2017' ) - 1
AND ADJ.set_of_books_id = 2043
group by ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.class ,
ps.due_date,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate,1),
ps.payment_schedule_id
UNION ALL
SELECT
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class ,
0 primary_salesrep_id,
ps.due_date ,
nvl(sum ( decode
( 'Y', 'Y',
(decode(ps.class, 'CM',
decode ( app.application_type, 'CM',
app.acctd_amount_applied_from,
app.acctd_amount_applied_to
),
app.acctd_amount_applied_to)+
nvl(app.acctd_earned_discount_taken,0) +
nvl(app.acctd_unearned_discount_taken,0))
,
( app.amount_applied +
nvl(app.earned_discount_taken,0) +
nvl(app.unearned_discount_taken,0) )
) *
decode
( ps.class, 'CM',
decode(app.application_type, 'CM', -1, 1), 1 )
), 0) amount_due_remaining_inv,
ps.trx_number ,
ps.amount_adjusted,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date gl_date_inv,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate
from ar_payment_schedules_all ps,
ar_receivable_applications_all app,
hz_cust_accounts cust_acct,
hz_parties party
where ps.gl_date <= to_date('02-JUL-2017') -1
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and ps.customer_id > 0
and ps.gl_date_closed > to_date('02-JUL-2017') -1
and ( app.applied_payment_schedule_id = ps.payment_schedule_id
or app.payment_schedule_id = ps.payment_schedule_id )
and app.status = 'APP'
and nvl( app.confirmed_flag, 'Y' ) = 'Y'
and app.gl_date > to_date('02-JUL-2017') -1
and app.set_of_books_id = 2043
group by ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.class ,
ps.due_date,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1),
ps.payment_schedule_id
UNION ALL
SELECT
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class class_inv,
nvl(ct.primary_salesrep_id, -3) primary_salesrep_id,
ps.due_date due_date_inv,
decode( 'Y', 'Y',
ps.acctd_amount_due_remaining,
ps.amount_due_remaining) amt_due_remaining_inv,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate
from ar_payment_schedules_all ps,
ra_customer_trx_all ct,
hz_cust_accounts cust_acct,
hz_parties party
where ps.gl_date <=to_date('02-JUL-2017') -1
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and ps.gl_date_closed > to_date('02-JUL-2017') -1
and ps.customer_trx_id = ct.customer_trx_id
AND(ct.org_id IS NULL OR EXISTS
(SELECT
/* */ 1
FROM hr_organization_information org_info
WHERE ct.org_id = org_info.organization_id
AND org_info.org_information_context = 'Operating Unit Information'
AND to_number(org_info.org_information3) = 2043))
) a
group by a.customer_id,
a.customer_site_use_id ,
a.customer_trx_id,
a.payment_schedule_id,
a.class ,
a.due_date ,
a.trx_number,
a.amount_adjusted,
a.amount_applied ,
a.amount_credited ,
a.amount_adjusted_pending,
a.gl_date ,
a.cust_trx_type_id,
a.invoice_currency_code,
a.exchange_rate
)
ps, ar_dispute_history dh,ra_cust_trx_line_gl_dist_all gld
,xla_distribution_links lk
,xla_ae_lines ae
,gl_code_combinations c
,hz_cust_accounts cust_acct
,hz_parties party where
ps.payment_schedule_id = dh.payment_schedule_id(+)
and ((to_date('02-JUL-2017') -1 >= trunc(dh.start_date)) or dh.start_date
is null)
and ((to_date('02-JUL-2017')-1 < trunc(dh.end_date)) or dh.end_date is
null)
and gld.account_class = 'REC'
and gld.latest_rec_flag = 'Y'
and gld.cust_trx_line_gl_dist_id = lk.source_distribution_id_num_1(+)
and lk.source_distribution_type(+) = 'RA_CUST_TRX_LINE_GL_DIST_ALL'
and lk.application_id(+) = 222
and ae.application_id(+) = 222
and lk.ae_header_id = ae.ae_header_id(+)
and lk.ae_line_num = ae.ae_line_num(+)
and decode(lk.accounting_line_code, '', 'Y',
'CM_EXCH_GAIN_LOSS', 'N',
'AUTO_GEN_GAIN_LOSS', 'N', 'Y') = 'Y'
and decode(ae.ledger_id,'',decode(gld.posting_control_id,-3,-
999999,gld.code_combination_id),gld.set_of_books_id,ae.code_combination_id,-
999999)= c.code_combination_id
and cust_acct.party_id = party.party_id
and ps.customer_id = cust_acct.cust_account_id
and ps.customer_trx_id = gld.customer_trx_id
and gld.set_of_books_id = 2043
UNION ALL
select ps.payment_schedule_id,
DECODE(app.applied_payment_schedule_id,-4,'CLAIM',ps.class),
decode
( 'Y', 'Y', nvl(-sum(app.acctd_amount_applied_from),0),
nvl(-sum(app.amount_applied),0)
)
from ar_payment_schedules_all ps,
ar_receivable_applications_all app,
hz_cust_accounts cust_acct,
hz_parties party,
gl_code_combinations c where app.gl_date <= to_date('02-JUL-
2017')-1
and ps.customer_id = cust_acct.cust_account_id(+)
and cust_acct.party_id= party.party_id (+)
and ps.cash_receipt_id = app.cash_receipt_id
and app.code_combination_id = c.code_combination_id
and app.status in ( 'ACC', 'UNAPP', 'UNID','OTHER ACC')
and nvl(app.confirmed_flag, 'Y') = 'Y'
and ps.gl_date_closed > to_date('02-JUL-2017')-1
and ((app.reversal_gl_date is not null AND
ps.gl_date <= to_date('02-JUL-2017')-1)
OR
app.reversal_gl_date is null )
and nvl( ps.receipt_confirmed_flag, 'Y' ) = 'Y'
and app.set_of_books_id = 2043
GROUP BY ps.payment_schedule_id,
DECODE(app.applied_payment_schedule_id,-4,'CLAIM',ps.class)
UNION ALL
select ps.payment_schedule_id payment_sched_id,
ps.class class,
decode( 'Y', 'Y', ps.acctd_amount_due_remaining,
ps.amount_due_remaining) amt_due_remaining
from ra_cust_trx_types_all ctt,
hz_cust_accounts cust_acct,
hz_parties party,
ar_payment_schedules_all ps,
ar_transaction_history_all th,
ar_xla_ard_lines_v dist,
gl_code_combinations c where ps.gl_date <= to_date('02-JUL-
2017')-1
and ps.gl_date_closed > to_date('02-JUL-2017')-1
and ps.class = 'BR'
and ps.cust_trx_type_id = ctt.cust_trx_type_id
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and th.transaction_history_id = dist.source_id
and th.transaction_history_id =
(select max(transaction_history_id)
from ar_transaction_history th2,
ar_xla_ard_lines_v dist2
where th2.transaction_history_id = dist2.source_id
and dist2.source_table = 'TH'
and th2.gl_date <= to_date('02-JUL-2017')-1
and dist2.amount_dr is not null
and th2.customer_trx_id = ps.customer_trx_id)
and dist.source_table = 'TH'
and dist.amount_dr is not null
and dist.source_table_secondary is NULL
and dist.code_combination_id = c.code_combination_id
AND(ps.org_id IS NULL OR EXISTS
(SELECT
/* */ 1
FROM hr_organization_information org_info
WHERE ps.org_id = org_info.organization_id
AND org_info.org_information_context = 'Operating Unit Information'
AND to_number(org_info.org_information3) = 2043)))

-----aging to data

select
ps.payment_schedule_id payment_sched_id,
ps.class,
ps.amt_due_remaining
from (select a.customer_id,
a.customer_site_use_id ,
a.customer_trx_id,
a.payment_schedule_id,
a.class ,
sum(a.primary_salesrep_id) primary_salesrep_id,
a.due_date ,
sum(a.amount_due_remaining) amt_due_remaining,
a.trx_number,
a.amount_adjusted,
a.amount_applied ,
a.amount_credited ,
a.amount_adjusted_pending,
a.gl_date ,
a.cust_trx_type_id,
a.invoice_currency_code,
a.exchange_rate
from (
SELECT
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class ,
0 primary_salesrep_id,
ps.due_date ,
nvl(sum(nvl(adj.acctd_amount, 0)),0) * (-1) amount_due_remaining,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate,1) exchange_rate
from ar_payment_schedules_all ps,
ar_adjustments_all adj,
hz_cust_accounts cust_acct,
hz_parties party
where trunc(ps.gl_date) <= '05-AUG-2017'
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and ps.customer_id > 0
and TRUNC(ps.gl_date_closed) > '05-AUG-2017'
and adj.payment_schedule_id = ps.payment_schedule_id
and adj.status = 'A'
and trunc(adj.gl_date) > '05-AUG-2017'
and adj.set_of_books_id = 2043
group by ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.class ,
ps.due_date,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate,1),
ps.payment_schedule_id
UNION ALL
SELECT
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class ,
0 primary_salesrep_id,
ps.due_date ,
nvl(sum ( decode
( 'Y', 'Y',
(decode(ps.class, 'CM',
decode ( app.application_type, 'CM',
app.acctd_amount_applied_from,
app.acctd_amount_applied_to
),
app.acctd_amount_applied_to)+
nvl(app.acctd_earned_discount_taken,0) +
nvl(app.acctd_unearned_discount_taken,0))
,
( app.amount_applied +
nvl(app.earned_discount_taken,0) +
nvl(app.unearned_discount_taken,0) )
) *
decode
( ps.class, 'CM',
decode(app.application_type, 'CM', -1, 1), 1 )
), 0) amount_due_remaining_inv,
ps.trx_number ,
ps.amount_adjusted,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date gl_date_inv,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate
from ar_payment_schedules_all ps,
ar_receivable_applications_all app,
hz_cust_accounts cust_acct,
hz_parties party
where trunc( ps.gl_date) <= '05-AUG-2017'
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and ps.customer_id > 0
and trunc( ps.gl_date_closed) > '05-AUG-2017'
and ( app.applied_payment_schedule_id = ps.payment_schedule_id
or app.payment_schedule_id = ps.payment_schedule_id )
and app.status = 'APP'
and nvl( app.confirmed_flag, 'Y' ) = 'Y'
and trunc( app.gl_date) >'05-AUG-2017' and
app.set_of_books_id = 2043
group by
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.class ,
ps.due_date,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1),
ps.payment_schedule_id
UNION ALL SELECT
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class class_inv,
nvl(ct.primary_salesrep_id, -3) primary_salesrep_id,
ps.due_date due_date_inv,
decode( 'Y', 'Y',
ps.acctd_amount_due_remaining,
ps.amount_due_remaining) amt_due_remaining_inv,
ps.trx_number,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
ps.amount_adjusted_pending,
ps.gl_date ,
ps.cust_trx_type_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate
from ar_payment_schedules_all ps,
ra_customer_trx_all ct,
hz_cust_accounts cust_acct,
hz_parties party
where trunc( ps.gl_date) <= '05-AUG-2017'
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and trunc(ps.gl_date_closed) > '05-AUG-2017'
and ps.customer_trx_id = ct.customer_trx_id
AND(ps.org_id IS NULL OR EXISTS
(SELECT
/* */ 1
FROM hr_organization_information org_info
WHERE ps.org_id = org_info.organization_id
AND org_info.org_information_context = 'Operating Unit Information'
AND to_number(org_info.org_information3) = 2043))
) a
group by a.customer_id,
a.customer_site_use_id ,
a.customer_trx_id,
a.payment_schedule_id,
a.class ,
a.due_date ,
a.trx_number,
a.amount_adjusted,
a.amount_applied ,
a.amount_credited ,
a.amount_adjusted_pending,
a.gl_date ,
a.cust_trx_type_id,
a.invoice_currency_code,
a.exchange_rate) ps, ar_dispute_history dh,ra_cust_trx_line_gl_dist_all gld
,xla_distribution_links lk
,xla_ae_lines ae
,gl_code_combinations c
,hz_cust_accounts cust_acct
,hz_parties party where
ps.payment_schedule_id = dh.payment_schedule_id(+)
and (( '05-AUG-2017' >= trunc(dh.start_date)) or dh.start_date is null)
and (('05-AUG-2017' < trunc(dh.end_date)) or dh.end_date is null)
and gld.account_class = 'REC'
and gld.latest_rec_flag = 'Y'
and gld.cust_trx_line_gl_dist_id = lk.source_distribution_id_num_1(+)
and lk.source_distribution_type(+) = 'RA_CUST_TRX_LINE_GL_DIST_ALL'
and lk.application_id(+) = 222
and ae.application_id(+) = 222
and lk.ae_header_id = ae.ae_header_id(+)
and lk.ae_line_num = ae.ae_line_num(+)
and decode(lk.accounting_line_code, '', 'Y',
'CM_EXCH_GAIN_LOSS', 'N',
'AUTO_GEN_GAIN_LOSS', 'N', 'Y') = 'Y'
and decode(ae.ledger_id,'',decode(gld.posting_control_id,-3,-
999999,gld.code_combination_id),gld.set_of_books_id,ae.code_combination_id,-
999999)= c.code_combination_id
and cust_acct.party_id = party.party_id
and ps.customer_id = cust_acct.cust_account_id
and ps.customer_trx_id = gld.customer_trx_id
AND gld.set_of_books_id = 2043
UNION ALL
select ps.payment_schedule_id,
DECODE(app.applied_payment_schedule_id,-4,'CLAIM',ps.class),
decode
( 'Y', 'Y', nvl(-sum(app.acctd_amount_applied_from),0),
nvl(-sum(app.amount_applied),0)
)
from ar_payment_schedules_all ps,
ar_receivable_applications_all app,
hz_cust_accounts cust_acct,
hz_parties party,
gl_code_combinations c where trunc( app.gl_date) <= '05-AUG-2017'
and ps.customer_id = cust_acct.cust_account_id(+)
and cust_acct.party_id= party.party_id (+)
and ps.cash_receipt_id = app.cash_receipt_id
and app.code_combination_id = c.code_combination_id
and app.status in ( 'ACC', 'UNAPP', 'UNID','OTHER ACC')
and nvl(app.confirmed_flag, 'Y') = 'Y'
and trunc( ps.gl_date_closed) > '05-AUG-2017'
and ((app.reversal_gl_date is not null AND ps.gl_date <='05-AUG-2017')
OR
app.reversal_gl_date is null )
and nvl( ps.receipt_confirmed_flag, 'Y' ) = 'Y'
and app.set_of_books_id = 2043
GROUP BY ps.payment_schedule_id,
DECODE(app.applied_payment_schedule_id,-4,'CLAIM',ps.class)
UNION ALL
select ps.payment_schedule_id payment_sched_id,
ps.class class,
decode( 'Y', 'Y', ps.acctd_amount_due_remaining,
ps.amount_due_remaining) amt_due_remaining
from ra_cust_trx_types_all ctt,
hz_cust_accounts cust_acct,
hz_parties party,
ar_payment_schedules_all ps,
ar_transaction_history_all th,
ar_xla_ard_lines_v dist,
gl_code_combinations c where trunc( ps.gl_date) <= '05-AUG-2017'
and trunc(ps.gl_date_closed) > '05-AUG-2017'
and ps.class = 'BR'
and ps.cust_trx_type_id = ctt.cust_trx_type_id
and ps.customer_id = cust_acct.cust_account_id
and cust_acct.party_id = party.party_id
and th.transaction_history_id = dist.source_id
and th.transaction_history_id =
(select max(transaction_history_id)
from ar_transaction_history th2,
ar_xla_ard_lines_v dist2
where th2.transaction_history_id = dist2.source_id
and dist2.source_table = 'TH'
and trunc(th2.gl_date) <= '05-AUG-2017'
and dist2.amount_dr is not null
and th2.customer_trx_id = ps.customer_trx_id)
and dist.source_table = 'TH'
and dist.amount_dr is not null
and dist.source_table_secondary is NULL
and dist.code_combination_id = c.code_combination_id
AND(th.org_id IS NULL OR EXISTS
(SELECT
/* */ 1
FROM hr_organization_information org_info
WHERE ps.org_id = org_info.organization_id
AND org_info.org_information_context = 'Operating Unit Information'
AND to_number(org_info.org_information3) = 2043))

create table aging_data as


(select decode(ps.class, 'PMT',ps.cash_receipt_id,
ps.customer_trx_id) id,
decode(ps.class,'PMT','CR','TRX') type,
sum(to_aging.amt_due_remaining)-
sum(from_aging.amt_due_remaining) balance
from ar_payment_schedules_all ps,
aging_from_data from_aging,
aging_to_data to_aging
where ps.payment_schedule_id = from_aging.payment_sched_id
and from_aging.payment_sched_id = to_aging.payment_sched_id
AND(ps.org_id IS NULL OR EXISTS
(SELECT
/* */ 1
FROM hr_organization_information org_info
WHERE ps.org_id = org_info.organization_id
AND org_info.org_information_context = 'Operating Unit Information'
AND to_number(org_info.org_information3) = &ledger_id))
group by decode(ps.class, 'PMT',ps.cash_receipt_id,
ps.customer_trx_id),
decode(ps.class,'PMT','CR','TRX')
having sum(to_aging.amt_due_remaining)- sum(from_aging.amt_due_remaining) <> 0
union all
select decode(ps.class, 'PMT',ps.cash_receipt_id,
ps.customer_trx_id) id,
decode(ps.class,'PMT','CR','TRX') type,
-1*sum(from_aging.amt_due_remaining) balance
from ar_payment_schedules_all ps,
aging_from_data from_aging
where ps.payment_schedule_id = from_aging.payment_sched_id
and not exists (select to_aging.payment_sched_id
from aging_to_data to_aging
where from_aging.payment_sched_id =
to_aging.payment_sched_id)
group by decode(ps.class, 'PMT',ps.cash_receipt_id,
ps.customer_trx_id),
decode(ps.class,'PMT','CR','TRX')
having sum(from_aging.amt_due_remaining) <> 0
union all
select decode(ps.class, 'PMT',ps.cash_receipt_id,
ps.customer_trx_id) id,
decode(ps.class,'PMT','CR','TRX') type,
sum(to_aging.amt_due_remaining) balance
from ar_payment_schedules_all ps,
aging_to_data to_aging
where ps.payment_schedule_id = to_aging.payment_sched_id
and not exists (select from_aging.payment_sched_id
from aging_from_data from_aging
where from_aging.payment_sched_id =
to_aging.payment_sched_id)
group by decode(ps.class, 'PMT',ps.cash_receipt_id,
ps.customer_trx_id),
decode(ps.class,'PMT','CR','TRX')
having sum(to_aging.amt_due_remaining) <> 0

You might also like