SQL for aging

user12032282
Posts: 11 Newbie
Comments
-
Hi Eric
This is the SQL Agins 4 Buckets is running:
select ps . org_id customer_org_id , decode ( UPPER ( : p_order_by ) ,
'CUSTOMER' , NULL , types . cust_trx_type_id ) dummy_id , decode ( UPPER (
: p_order_by ) , 'CUSTOMER' , NULL , types . name ) dummy_type , rtrim (
rpad ( substrb ( party . party_name , 1 , 50 ) , 36 ) ) customer_name ,
cust_acct . cust_account_id customer_id , cust_acct . account_number
customer_number , types . name type , ps . payment_schedule_id
payment_sched_id , ps . class class , ps . due_date due_date , decode ( :
rp_convert_flag , 'Y' , ps . acctd_amount_due_remaining , ps .
amount_due_remaining ) amt_due_remaining , ps . amount_due_original
amt_due_original , ceil ( : P_AS_OF_DATE - ps . due_date ) days_past_due ,
ps . amount_adjusted amount_adjusted , ps . amount_applied amount_applied ,
ps . amount_credited amount_credited , ps . gl_date gl_date , decode ( ps .
invoice_currency_code , : Functional_Currency , NULL , decode ( ps .
exchange_rate , NULL , '*' , NULL ) ) data_converted , nvl ( ps .
exchange_rate , 1 ) ps_exchange_rate , decode ( : rp_bucket_line_type_0 ,
'DISPUTE_ONLY' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) ,
'PENDADJ_ONLY' , decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0
, 1 ) , 'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0
, decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_0 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_0 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b0 , decode ( : rp_bucket_line_type_1 , 'DISPUTE_ONLY' , decode (
nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode
( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) ,
'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_1 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_1 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b1 , decode ( : rp_bucket_line_type_2 , 'DISPUTE_ONLY' , decode (
nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode
( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) ,
'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_2 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_2 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b2 , decode ( : rp_bucket_line_type_3 , 'DISPUTE_ONLY' , decode (
nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode
( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) ,
'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_3 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_3 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b3 , cc.SEGMENT1 bal_segment_value , rtrim ( rpad ( nvl ( substrb
( party . party_name , 1 , 50 ) , : rp_unid_phrase ) , 36 ) )
customer_name_dsp
from
ra_cust_trx_types_all types , hz_cust_accounts cust_acct , hz_parties party ,
AR_PAYMENT_SCHEDULES_ALL ps , ra_cust_trx_line_gl_dist_all gld ,
xla_distribution_links lk , xla_ae_lines ae , gl_code_combinations cc
WHERE ( TRUNC ( ps.gl_date ) <= : p_as_of_date and ps.customer_id =
cust_acct.cust_account_id and cust_acct.party_id = party.party_id and
ps.cust_trx_type_id = types.cust_trx_type_id and nvl ( ps.org_id , - 99 ) =
nvl ( types.org_id , - 99 ) and ps.gl_date_closed > : P_AS_OF_DATE and
ps.customer_trx_id + 0 = gld.customer_trx_id 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 ,
'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 ) = cc.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 ) = 21 ) ) AND ( TYPES.org_id IS NULL
OR EXISTS ( SELECT 1 FROM hr_organization_information org_info WHERE
TYPES.org_id = org_info.organization_id AND
org_info.org_information_context = 'Operating Unit Information' AND
TO_NUMBER ( org_info.org_information3 ) = 21 ) ) AND ( GLD.org_id IS NULL
OR EXISTS ( SELECT 1 FROM hr_organization_information org_info WHERE
GLD.org_id = org_info.organization_id AND org_info.org_information_context =
'Operating Unit Information' AND TO_NUMBER ( org_info.org_information3 ) =
21 ) ) ) AND ( :qc_org_id = ps.org_id) UNION ALL select ps . org_id ,
decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , - 999 ) dummy_id ,
decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , decode ( app .
applied_payment_schedule_id , - 4 , : c_claim_meaning , : c_payment_meaning
) ) dummy_type , rtrim ( rpad ( substrb ( party . party_name , 1 , 50 ) ,
36 ) ) customer_name , nvl ( cust_acct . cust_account_id , - 999 )
customer_id , cust_acct . account_number customer_number , decode ( :
p_order_by , 'XXX' , NULL , decode ( app . applied_payment_schedule_id , -
4 , : c_claim_meaning , : c_payment_meaning ) ) type , ps .
payment_schedule_id payment_sched_id , ps . class class , ps . due_date
due_date , - sum ( decode ( : rp_convert_flag , 'Y' , app .
acctd_amount_applied_from , app . amount_applied ) ) amt_due_remaining , ps
. amount_due_original amt_due_original , ceil ( : P_AS_OF_DATE - ps .
due_date ) days_past_due , ps . amount_adjusted amount_adjusted , ps .
amount_applied amount_applied , ps . amount_credited amount_credited , ps .
gl_date gl_date , decode ( ps . invoice_currency_code , :
Functional_Currency , NULL , decode ( ps . exchange_rate , NULL , '*' ,
NULL ) ) data_converted , nvl ( ps . exchange_rate , 1 ) ps_exchange_rate ,
decode ( : rp_bucket_line_type_0 , 'DISPUTE_ONLY' , decode ( nvl ( ps .
amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl ( ps
. amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' , decode
( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b0 , decode ( : rp_bucket_line_type_1 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b1 , decode ( : rp_bucket_line_type_2 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b2 , decode ( : rp_bucket_line_type_3 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b3 , cc.SEGMENT1 bal_segment_value , rtrim ( rpad ( nvl ( substrb ( party
. party_name , 1 , 50 ) , : rp_unid_phrase ) , 36 ) ) customer_name_dsp
from AR_PAYMENT_SCHEDULES_ALL ps , AR_RECEIVABLE_APPLICATIONS_ALL app ,
gl_code_combinations cc , hz_cust_accounts cust_acct , hz_parties party
WHERE ( app.gl_date + 0 <= : P_AS_OF_DATE and ps.customer_id =
cust_acct.cust_account_id (+) and cust_acct.party_id = party.party_id (+)
and ps.cash_receipt_id + 0 = app.cash_receipt_id and
app.code_combination_id = cc.code_combination_id and app.status in ( 'ACC' ,
'UNAPP' , 'UNID' , 'OTHER ACC' ) and nvl ( app.confirmed_flag , 'Y' ) =
'Y' and ps.gl_date_closed > : P_AS_OF_DATE and ( ( app.reversal_gl_date is
not null AND ps.gl_date <= : P_AS_OF_DATE ) OR app.reversal_gl_date is null
) and nvl ( ps.receipt_confirmed_flag , 'Y' ) = 'Y' 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 ) =
21 ) ) AND ( APP.org_id IS NULL OR EXISTS ( SELECT 1 FROM
hr_organization_information org_info WHERE APP.org_id =
org_info.organization_id AND org_info.org_information_context = 'Operating
Unit Information' AND TO_NUMBER ( org_info.org_information3 ) = 21 ) ) )
AND ( :qc_org_id = ps.org_id) group by ps.org_id , party.party_name ,
cust_acct.account_number , cust_acct.cust_account_id ,
ps.payment_schedule_id , app.applied_payment_schedule_id , ps.due_date ,
ps.amount_due_original , ps.amount_adjusted , ps.amount_applied ,
ps.amount_credited , ps.gl_date , ps.amount_in_dispute ,
ps.amount_adjusted_pending , ps.invoice_currency_code , ps.exchange_rate ,
ps.class , cc.SEGMENT1 , decode ( app.status , 'UNID' , 'UNID' , 'UNAPP' )
UNION ALL select ps . org_id , decode ( UPPER ( : p_order_by ) , 'CUSTOMER'
, NULL , - 999 ) , decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , :
c_risk_meaning ) dummy_type , rtrim ( rpad ( substrb ( party . party_name ,
1 , 50 ) , 36 ) ) customer_name , nvl ( cust_acct . cust_account_id , - 999
) customer_id , cust_acct . account_number customer_number , :
c_risk_meaning type , ps . payment_schedule_id payment_sched_id , :
c_risk_meaning class , ps . due_date due_date , decode ( : rp_convert_flag ,
'Y' , crh . acctd_amount , crh . amount ) amt_due_remaining , ps .
amount_due_original amt_due_original , ceil ( : P_AS_OF_DATE - ps .
due_date ) days_past_due , ps . amount_adjusted amount_adjusted , ps .
amount_applied amount_applied , ps . amount_credited amount_credited , crh
. gl_date gl_date , decode ( ps . invoice_currency_code , :
Functional_Currency , NULL , decode ( ps . exchange_rate , NULL , '*' ,
NULL ) ) data_converted , nvl ( ps . exchange_rate , 1 ) ps_exchange_rate ,
decode ( : rp_bucket_line_type_0 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_0 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_0 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b0 , decode ( :
rp_bucket_line_type_1 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_1 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_1 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b1 , decode ( :
rp_bucket_line_type_2 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_2 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_2 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b2 , decode ( :
rp_bucket_line_type_3 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_3 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_3 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b3 , cc.SEGMENT1
bal_segment_value , rtrim ( rpad ( nvl ( substrb ( party . party_name , 1 ,
50 ) , : rp_unid_phrase ) , 36 ) ) customer_name_dsp from hz_cust_accounts
cust_acct , hz_parties party , AR_PAYMENT_SCHEDULES_ALL ps ,
AR_CASH_RECEIPTS_ALL cr , AR_CASH_RECEIPT_HISTORY_ALL crh ,
gl_code_combinations cc WHERE ( crh.gl_date + 0 <= : P_AS_OF_DATE and
upper ( : p_risk_option ) != 'NONE' and ps.customer_id =
cust_acct.cust_account_id (+) and cust_acct.party_id = party.party_id (+)
and ps.cash_receipt_id = cr.cash_receipt_id and cr.cash_receipt_id =
crh.cash_receipt_id and crh.account_code_combination_id =
cc.code_combination_id and ( crh.current_record_flag = 'Y' or
crh.reversal_gl_date > : p_as_of_date ) and crh.status not in ( decode (
crh.factor_flag , 'Y' , 'RISK_ELIMINATED' , 'N' , 'CLEARED' ) , 'REVERSED' )
and not exists ( select 'x' from AR_RECEIVABLE_APPLICATIONS_ALL ra where
ra.cash_receipt_id = cr.cash_receipt_id and ra.status = 'ACTIVITY' and
applied_payment_schedule_id = - 2 ) 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 ) = 21 ) ) AND
( CR.org_id IS NULL OR EXISTS ( SELECT 1 FROM hr_organization_information
org_info WHERE CR.org_id = org_info.organization_id AND
org_info.org_information_context = 'Operating Unit Information' AND
TO_NUMBER ( org_info.org_information3 ) = 21 ) ) AND ( CRH.org_id IS NULL
OR EXISTS ( SELECT 1 FROM hr_organization_information org_info WHERE
CRH.org_id = org_info.organization_id AND org_info.org_information_context =
'Operating Unit Information' AND TO_NUMBER ( org_info.org_information3 ) =
21 ) ) ) AND ( :qc_org_id = ps.org_id) UNION ALL select ps . org_id ,
decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , types .
cust_trx_type_id ) dummy_id , decode ( UPPER ( : p_order_by ) , 'CUSTOMER' ,
NULL , types . name ) dummy_type , rtrim ( rpad ( substrb ( party .
party_name , 1 , 50 ) , 36 ) ) customer_name , cust_acct . cust_account_id
customer_id , cust_acct . account_number customer_number , types . name
type , ps . payment_schedule_id payment_sched_id , ps . class class , ps .
due_date due_date , decode ( : rp_convert_flag , 'Y' , ps .
acctd_amount_due_remaining , ps . amount_due_remaining ) amt_due_remaining ,
ps . amount_due_original amt_due_original , ceil ( : P_AS_OF_DATE - ps .
due_date ) days_past_due , ps . amount_adjusted amount_adjusted , ps .
amount_applied amount_applied , ps . amount_credited amount_credited , ps .
gl_date gl_date , decode ( ps . invoice_currency_code , :
Functional_Currency , NULL , decode ( ps . exchange_rate , NULL , '*' ,
NULL ) ) data_converted , nvl ( ps . exchange_rate , 1 ) ps_exchange_rate ,
decode ( : rp_bucket_line_type_0 , 'DISPUTE_ONLY' , decode ( nvl ( ps .
amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl ( ps
. amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' , decode
( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b0 , decode ( : rp_bucket_line_type_1 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b1 , decode ( : rp_bucket_line_type_2 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b2 , decode ( : rp_bucket_line_type_3 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b3 , cc.SEGMENT1 bal_segment_value , rtrim ( rpad ( nvl ( substrb ( party
. party_name , 1 , 50 ) , : rp_unid_phrase ) , 36 ) ) customer_name_dsp
from ra_cust_trx_types_all types , 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 cc WHERE ( TRUNC (
ps.gl_date ) <= : p_as_of_date and ps.class = 'BR' and ps.customer_id =
cust_acct.cust_account_id and cust_acct.party_id = party.party_id and
ps.cust_trx_type_id = types.cust_trx_type_id and nvl ( ps.org_id , - 99 ) =
nvl ( types.org_id , - 99 ) and ps.gl_date_closed > : P_AS_OF_DATE and
ps.customer_trx_id + 0 = th.customer_trx_id and th.transaction_history_id =
dist.source_id and dist.source_table = 'TH' and th.transaction_history_id =
( select max ( transaction_history_id ) from ar_transaction_history_all th2
, AR_XLA_ARD_LINES_V dist2 where th2.transaction_history_id =
dist2.source_id and dist2.source_table = 'TH' and th2.gl_date <= :
p_as_of_date and dist2.amount_dr is not null and th2.customer_trx_id =
ps.customer_trx_id ) and dist.amount_dr is not null and
dist.source_table_secondary is NULL and dist.code_combination_id =
cc.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 ) = 21 ) ) AND
( TYPES.org_id IS NULL OR EXISTS ( SELECT 1 FROM
hr_organization_information org_info WHERE TYPES.org_id =
org_info.organization_id AND org_info.org_information_context = 'Operating
Unit Information' AND TO_NUMBER ( org_info.org_information3 ) = 21 ) ) )
AND ( :qc_org_id = ps.org_id) ORDER BY 24 ASC,3 ASC,4 ASC,5 ASC,6 ASC , 24
, 4 , 6
Hope it helps, regards, José Manuel
0 -
select ps . org_id customer_org_id , decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , types . cust_trx_type_id ) dummy_id , decode ( UPPER (
: p_order_by ) , 'CUSTOMER' , NULL , types . name ) dummy_type , rtrim (
rpad ( substrb ( party . party_name , 1 , 50 ) , 36 ) ) customer_name ,
cust_acct . cust_account_id customer_id , cust_acct . account_number
customer_number , types . name type , ps . payment_schedule_id
payment_sched_id , ps . class class , ps . due_date due_date , decode ( :
rp_convert_flag , 'Y' , ps . acctd_amount_due_remaining , ps .
amount_due_remaining ) amt_due_remaining , ps . amount_due_original
amt_due_original , ceil ( : P_AS_OF_DATE - ps . due_date ) days_past_due ,
ps . amount_adjusted amount_adjusted , ps . amount_applied amount_applied ,
ps . amount_credited amount_credited , ps . gl_date gl_date , decode ( ps .
invoice_currency_code , : Functional_Currency , NULL , decode ( ps .
exchange_rate , NULL , '*' , NULL ) ) data_converted , nvl ( ps .
exchange_rate , 1 ) ps_exchange_rate , decode ( : rp_bucket_line_type_0 ,
'DISPUTE_ONLY' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) ,
'PENDADJ_ONLY' , decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0
, 1 ) , 'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0
, decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_0 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_0 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b0 , decode ( : rp_bucket_line_type_1 , 'DISPUTE_ONLY' , decode (
nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode
( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) ,
'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_1 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_1 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b1 , decode ( : rp_bucket_line_type_2 , 'DISPUTE_ONLY' , decode (
nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode
( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) ,
'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_2 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_2 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b2 , decode ( : rp_bucket_line_type_3 , 'DISPUTE_ONLY' , decode (
nvl ( ps . amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode
( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) ,
'DISPUTE_PENDADJ' , decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) ,
decode ( greatest ( : rp_bucket_days_from_3 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , least ( : rp_bucket_days_to_3 , ceil ( : P_AS_OF_DATE - ps .
due_date ) ) , 1 , 0 ) * decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 ,
1 , decode ( : rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' ,
0 , 1 ) ) * decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 ,
decode ( : rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 ,
1 ) ) ) b3 , cc.SEGMENT1 bal_segment_value , rtrim ( rpad ( nvl ( substrb
( party . party_name , 1 , 50 ) , : rp_unid_phrase ) , 36 ) )
customer_name_dsp
from
ra_cust_trx_types_all types , hz_cust_accounts cust_acct , hz_parties party ,
AR_PAYMENT_SCHEDULES_ALL ps , ra_cust_trx_line_gl_dist_all gld ,
xla_distribution_links lk , xla_ae_lines ae , gl_code_combinations cc
WHERE ( TRUNC ( ps.gl_date ) <= : p_as_of_date and ps.customer_id =
cust_acct.cust_account_id and cust_acct.party_id = party.party_id and
ps.cust_trx_type_id = types.cust_trx_type_id and nvl ( ps.org_id , - 99 ) =
nvl ( types.org_id , - 99 ) and ps.gl_date_closed > : P_AS_OF_DATE and
ps.customer_trx_id + 0 = gld.customer_trx_id 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 ,
'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 ) = cc.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 ) = 21 ) ) AND ( TYPES.org_id IS NULL
OR EXISTS ( SELECT 1 FROM hr_organization_information org_info WHERE
TYPES.org_id = org_info.organization_id AND
org_info.org_information_context = 'Operating Unit Information' AND
TO_NUMBER ( org_info.org_information3 ) = 21 ) ) AND ( GLD.org_id IS NULL
OR EXISTS ( SELECT 1 FROM hr_organization_information org_info WHERE
GLD.org_id = org_info.organization_id AND org_info.org_information_context =
'Operating Unit Information' AND TO_NUMBER ( org_info.org_information3 ) =
21 ) ) ) AND ( :qc_org_id = ps.org_id) UNION ALL select ps . org_id ,
decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , - 999 ) dummy_id ,
decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , decode ( app .
applied_payment_schedule_id , - 4 , : c_claim_meaning , : c_payment_meaning
) ) dummy_type , rtrim ( rpad ( substrb ( party . party_name , 1 , 50 ) ,
36 ) ) customer_name , nvl ( cust_acct . cust_account_id , - 999 )
customer_id , cust_acct . account_number customer_number , decode ( :
p_order_by , 'XXX' , NULL , decode ( app . applied_payment_schedule_id , -
4 , : c_claim_meaning , : c_payment_meaning ) ) type , ps .
payment_schedule_id payment_sched_id , ps . class class , ps . due_date
due_date , - sum ( decode ( : rp_convert_flag , 'Y' , app .
acctd_amount_applied_from , app . amount_applied ) ) amt_due_remaining , ps
. amount_due_original amt_due_original , ceil ( : P_AS_OF_DATE - ps .
due_date ) days_past_due , ps . amount_adjusted amount_adjusted , ps .
amount_applied amount_applied , ps . amount_credited amount_credited , ps .
gl_date gl_date , decode ( ps . invoice_currency_code , :
Functional_Currency , NULL , decode ( ps . exchange_rate , NULL , '*' ,
NULL ) ) data_converted , nvl ( ps . exchange_rate , 1 ) ps_exchange_rate ,
decode ( : rp_bucket_line_type_0 , 'DISPUTE_ONLY' , decode ( nvl ( ps .
amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl ( ps
. amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' , decode
( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b0 , decode ( : rp_bucket_line_type_1 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b1 , decode ( : rp_bucket_line_type_2 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b2 , decode ( : rp_bucket_line_type_3 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b3 , cc.SEGMENT1 bal_segment_value , rtrim ( rpad ( nvl ( substrb ( party
. party_name , 1 , 50 ) , : rp_unid_phrase ) , 36 ) ) customer_name_dsp
from AR_PAYMENT_SCHEDULES_ALL ps , AR_RECEIVABLE_APPLICATIONS_ALL app ,
gl_code_combinations cc , hz_cust_accounts cust_acct , hz_parties party
WHERE ( app.gl_date + 0 <= : P_AS_OF_DATE and ps.customer_id =
cust_acct.cust_account_id (+) and cust_acct.party_id = party.party_id (+)
and ps.cash_receipt_id + 0 = app.cash_receipt_id and
app.code_combination_id = cc.code_combination_id and app.status in ( 'ACC' ,
'UNAPP' , 'UNID' , 'OTHER ACC' ) and nvl ( app.confirmed_flag , 'Y' ) =
'Y' and ps.gl_date_closed > : P_AS_OF_DATE and ( ( app.reversal_gl_date is
not null AND ps.gl_date <= : P_AS_OF_DATE ) OR app.reversal_gl_date is null
) and nvl ( ps.receipt_confirmed_flag , 'Y' ) = 'Y' 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 ) =
21 ) ) AND ( APP.org_id IS NULL OR EXISTS ( SELECT 1 FROM
hr_organization_information org_info WHERE APP.org_id =
org_info.organization_id AND org_info.org_information_context = 'Operating
Unit Information' AND TO_NUMBER ( org_info.org_information3 ) = 21 ) ) )
AND ( :qc_org_id = ps.org_id) group by ps.org_id , party.party_name ,
cust_acct.account_number , cust_acct.cust_account_id ,
ps.payment_schedule_id , app.applied_payment_schedule_id , ps.due_date ,
ps.amount_due_original , ps.amount_adjusted , ps.amount_applied ,
ps.amount_credited , ps.gl_date , ps.amount_in_dispute ,
ps.amount_adjusted_pending , ps.invoice_currency_code , ps.exchange_rate ,
ps.class , cc.SEGMENT1 , decode ( app.status , 'UNID' , 'UNID' , 'UNAPP' )
UNION ALL select ps . org_id , decode ( UPPER ( : p_order_by ) , 'CUSTOMER'
, NULL , - 999 ) , decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , :
c_risk_meaning ) dummy_type , rtrim ( rpad ( substrb ( party . party_name ,
1 , 50 ) , 36 ) ) customer_name , nvl ( cust_acct . cust_account_id , - 999
) customer_id , cust_acct . account_number customer_number , :
c_risk_meaning type , ps . payment_schedule_id payment_sched_id , :
c_risk_meaning class , ps . due_date due_date , decode ( : rp_convert_flag ,
'Y' , crh . acctd_amount , crh . amount ) amt_due_remaining , ps .
amount_due_original amt_due_original , ceil ( : P_AS_OF_DATE - ps .
due_date ) days_past_due , ps . amount_adjusted amount_adjusted , ps .
amount_applied amount_applied , ps . amount_credited amount_credited , crh
. gl_date gl_date , decode ( ps . invoice_currency_code , :
Functional_Currency , NULL , decode ( ps . exchange_rate , NULL , '*' ,
NULL ) ) data_converted , nvl ( ps . exchange_rate , 1 ) ps_exchange_rate ,
decode ( : rp_bucket_line_type_0 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_0 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_0 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b0 , decode ( :
rp_bucket_line_type_1 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_1 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_1 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b1 , decode ( :
rp_bucket_line_type_2 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_2 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_2 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b2 , decode ( :
rp_bucket_line_type_3 , 'DISPUTE_ONLY' , 0 , 'PENDADJ_ONLY' , 0 ,
'DISPUTE_PENDADJ' , 0 , decode ( greatest ( : rp_bucket_days_from_3 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , least ( : rp_bucket_days_to_3 , ceil
( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 ) ) b3 , cc.SEGMENT1
bal_segment_value , rtrim ( rpad ( nvl ( substrb ( party . party_name , 1 ,
50 ) , : rp_unid_phrase ) , 36 ) ) customer_name_dsp from hz_cust_accounts
cust_acct , hz_parties party , AR_PAYMENT_SCHEDULES_ALL ps ,
AR_CASH_RECEIPTS_ALL cr , AR_CASH_RECEIPT_HISTORY_ALL crh ,
gl_code_combinations cc WHERE ( crh.gl_date + 0 <= : P_AS_OF_DATE and
upper ( : p_risk_option ) != 'NONE' and ps.customer_id =
cust_acct.cust_account_id (+) and cust_acct.party_id = party.party_id (+)
and ps.cash_receipt_id = cr.cash_receipt_id and cr.cash_receipt_id =
crh.cash_receipt_id and crh.account_code_combination_id =
cc.code_combination_id and ( crh.current_record_flag = 'Y' or
crh.reversal_gl_date > : p_as_of_date ) and crh.status not in ( decode (
crh.factor_flag , 'Y' , 'RISK_ELIMINATED' , 'N' , 'CLEARED' ) , 'REVERSED' )
and not exists ( select 'x' from AR_RECEIVABLE_APPLICATIONS_ALL ra where
ra.cash_receipt_id = cr.cash_receipt_id and ra.status = 'ACTIVITY' and
applied_payment_schedule_id = - 2 ) 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 ) = 21 ) ) AND
( CR.org_id IS NULL OR EXISTS ( SELECT 1 FROM hr_organization_information
org_info WHERE CR.org_id = org_info.organization_id AND
org_info.org_information_context = 'Operating Unit Information' AND
TO_NUMBER ( org_info.org_information3 ) = 21 ) ) AND ( CRH.org_id IS NULL
OR EXISTS ( SELECT 1 FROM hr_organization_information org_info WHERE
CRH.org_id = org_info.organization_id AND org_info.org_information_context =
'Operating Unit Information' AND TO_NUMBER ( org_info.org_information3 ) =
21 ) ) ) AND ( :qc_org_id = ps.org_id) UNION ALL select ps . org_id ,
decode ( UPPER ( : p_order_by ) , 'CUSTOMER' , NULL , types .
cust_trx_type_id ) dummy_id , decode ( UPPER ( : p_order_by ) , 'CUSTOMER' ,
NULL , types . name ) dummy_type , rtrim ( rpad ( substrb ( party .
party_name , 1 , 50 ) , 36 ) ) customer_name , cust_acct . cust_account_id
customer_id , cust_acct . account_number customer_number , types . name
type , ps . payment_schedule_id payment_sched_id , ps . class class , ps .
due_date due_date , decode ( : rp_convert_flag , 'Y' , ps .
acctd_amount_due_remaining , ps . amount_due_remaining ) amt_due_remaining ,
ps . amount_due_original amt_due_original , ceil ( : P_AS_OF_DATE - ps .
due_date ) days_past_due , ps . amount_adjusted amount_adjusted , ps .
amount_applied amount_applied , ps . amount_credited amount_credited , ps .
gl_date gl_date , decode ( ps . invoice_currency_code , :
Functional_Currency , NULL , decode ( ps . exchange_rate , NULL , '*' ,
NULL ) ) data_converted , nvl ( ps . exchange_rate , 1 ) ps_exchange_rate ,
decode ( : rp_bucket_line_type_0 , 'DISPUTE_ONLY' , decode ( nvl ( ps .
amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl ( ps
. amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' , decode
( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_0 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b0 , decode ( : rp_bucket_line_type_1 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_1 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b1 , decode ( : rp_bucket_line_type_2 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_2 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b2 , decode ( : rp_bucket_line_type_3 , 'DISPUTE_ONLY' , decode ( nvl ( ps
. amount_in_dispute , 0 ) , 0 , 0 , 1 ) , 'PENDADJ_ONLY' , decode ( nvl (
ps . amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 'DISPUTE_PENDADJ' ,
decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , decode ( nvl ( ps .
amount_adjusted_pending , 0 ) , 0 , 0 , 1 ) , 1 ) , decode ( greatest ( :
rp_bucket_days_from_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , least (
: rp_bucket_days_to_3 , ceil ( : P_AS_OF_DATE - ps . due_date ) ) , 1 , 0 )
* decode ( nvl ( ps . amount_in_dispute , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'DISPUTE_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) *
decode ( nvl ( ps . amount_adjusted_pending , 0 ) , 0 , 1 , decode ( :
rp_bucket_category , 'PENDADJ_ONLY' , 0 , 'DISPUTE_PENDADJ' , 0 , 1 ) ) )
b3 , cc.SEGMENT1 bal_segment_value , rtrim ( rpad ( nvl ( substrb ( party
. party_name , 1 , 50 ) , : rp_unid_phrase ) , 36 ) ) customer_name_dsp
from ra_cust_trx_types_all types , 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 cc WHERE ( TRUNC (
ps.gl_date ) <= : p_as_of_date and ps.class = 'BR' and ps.customer_id =
cust_acct.cust_account_id and cust_acct.party_id = party.party_id and
ps.cust_trx_type_id = types.cust_trx_type_id and nvl ( ps.org_id , - 99 ) =
nvl ( types.org_id , - 99 ) and ps.gl_date_closed > : P_AS_OF_DATE and
ps.customer_trx_id + 0 = th.customer_trx_id and th.transaction_history_id =
dist.source_id and dist.source_table = 'TH' and th.transaction_history_id =
( select max ( transaction_history_id ) from ar_transaction_history_all th2
, AR_XLA_ARD_LINES_V dist2 where th2.transaction_history_id =
dist2.source_id and dist2.source_table = 'TH' and th2.gl_date <= :
p_as_of_date and dist2.amount_dr is not null and th2.customer_trx_id =
ps.customer_trx_id ) and dist.amount_dr is not null and
dist.source_table_secondary is NULL and dist.code_combination_id =
cc.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 ) = 21 ) ) AND
( TYPES.org_id IS NULL OR EXISTS ( SELECT 1 FROM
hr_organization_information org_info WHERE TYPES.org_id =
org_info.organization_id AND org_info.org_information_context = 'Operating
Unit Information' AND TO_NUMBER ( org_info.org_information3 ) = 21 ) ) )
AND ( :qc_org_id = ps.org_id) ORDER BY 24 ASC,3 ASC,4 ASC,5 ASC,6 ASC , 24
, 4 , 60 -
https://communities.oracle.com/portal/server.pt?open=514&objID=226&mode=2&docID=87541&aggregatorResults=D42752D42759D42764D42769D42772D42773D42775D42777D42778D42781D42783D42787D42788T43891D45999D46002D46956T47503D52399T55138D57470D57676D57911T58735D59041T59582T60894D61613D62958D65134T66790T67280T67469T68243D68408T71144D74125T74485T78040T79406T79426T79533T80428D80448T81802D81810D84006T84637D86898D87541&sourceCommunityId=219&sourcePortletId=367&doPagination=true&pagedAggregatorPageNo=2&returnUrl=https://communities.oracle.com/portal/server.pt?open=space&name=CommunityPage&id=37&psname=Opener&psid=34&cached=true&in_hi_userid=12156&control=SetCommunity&PageID=0&CommunityID=219&&Portlet=Communities Content&PrevPage=Communities-MainHome
Where you can find attached the sql script a customer used to create an excel download of aging
Hope you find it useful0