0% found this document useful (0 votes)
89 views14 pages

R1-TP 1 - Aging - 4 Buckets Report AvKare - SQL Review - 2021

This document contains a SQL query that selects data from various tables to generate a report. The query selects payment schedule data, customer details, transaction details, site information, and other related data. It joins multiple tables together on primary and foreign keys and applies various filters to the data, such as payment schedule date and currency. Bucket logic is also applied to categorize amounts into different ageing periods. The query is selecting data to generate an aged receivables or accounts receivable report.

Uploaded by

ganesh_vijay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views14 pages

R1-TP 1 - Aging - 4 Buckets Report AvKare - SQL Review - 2021

This document contains a SQL query that selects data from various tables to generate a report. The query selects payment schedule data, customer details, transaction details, site information, and other related data. It joins multiple tables together on primary and foreign keys and applies various filters to the data, such as payment schedule date and currency. Bucket logic is also applied to categorize amounts into different ageing periods. The query is selecting data to generate an aged receivables or accounts receivable report.

Uploaded by

ganesh_vijay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

select ps.

org_id invoice_org_id,
decode (UPPER(:p_order_by),'CUSTOMER',NULL,
types.cust_trx_type_id ) dummy_id_inv,
decode (UPPER(:p_order_by),'CUSTOMER',NULL,types.name)
dummy_type_inv,
/* bug2820114 Unidentified payment must be at end of
report. */
decode(party.party_name, null , '2', rtrim(rpad(substrb('1' ||
party.party_name,1,50),36) ) )
customer_name_inv,
cust_acct.cust_account_id
customer_id_inv,
cust_acct.account_number
customer_number_inv,
DECODE(UPPER(:p_format_option_low),'B',0, site.site_use_id)
contact_site_id_inv,
DECODE(UPPER(:p_format_option_low),'B',NULL,rtrim(rpad(loc.state,2)))
cust_state_inv ,
DECODE(UPPER(:p_format_option_low),'B',NULL,rtrim(rpad(loc.city,25)))
cust_city_inv ,
DECODE(UPPER(:p_format_option_low),'B',0,addr.cust_acct_site_id )
addr_id_inv ,
-- rtrim(rpad(trx.ct_reference,12))
reference_number ,
trx.ct_reference
reference_number ,
ps.payment_schedule_id
payment_sched_id_inv,
ps.class
class_inv,
ps.due_date
due_date_inv ,
decode( :rp_convert_flag, 'Y', ps.acctd_amount_due_remaining,
ps.amount_due_remaining )
amt_due_remaining_inv ,
decode( :rp_convert_flag, 'Y', round( (ps.amount_due_original *
nvl( ps.exchange_rate,1)),
:func_curr_precision), ps.amount_due_original )
amt_due_original_inv ,
ps.trx_number
invnum ,
types.name
invoice_type_inv,
ceil(:P_AS_OF_DATE - ps.due_date)
days_past_due_inv,
ps.amount_adjusted
amount_adjusted_inv,
ps.amount_applied
amount_applied_inv,
ps.amount_credited
amount_credited_inv,
ps.gl_date
gl_date_inv,
decode(ps.invoice_currency_code, :functional_currency, NULL,
decode(ps.exchange_rate, NULL, '*', NULL))
data_converted_inv,
nvl(ps.exchange_rate, 1)
ps_exchange_rate_inv,
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_inv,
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_inv,
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_inv,
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_inv,
cc.segment1
bal_segment_value_inv,
ci.cons_billing_number cons_billing_number,
rtrim(rpad(nvl(substrb(party.party_name,1,50),:rp_short_unid_phrase),36))
customer_name_inv_dsp,
trx.comments,
trx.purchase_order
from ra_cust_trx_types_all types,
hz_cust_accounts cust_acct,
hz_parties party,
AR_PAYMENT_SCHEDULES_ALL ps,
ar_cons_inv_all ci,
RA_CUSTOMER_TRX_ALL trx,
hz_cust_site_uses_all site,
hz_cust_acct_sites_all addr,
hz_party_sites party_site,
hz_locations loc,
ra_cust_trx_line_gl_dist_all gld,
/* Enhancement 7346488.
xla_distribution_links lk,
xla_ae_lines ae, */
gl_code_combinations cc
where TRUNC(ps.gl_date) <= :p_as_of_date
and ps.customer_trx_id+0 = trx.customer_trx_id
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
-- bug1960831
and nvl(ps.org_id,-99) = nvl(types.org_id,-99)

and ps.customer_site_use_id+0 = site.site_use_id(+)


and site.cust_acct_site_id = addr.cust_acct_site_id(+)
and addr.party_site_id = party_site.party_site_id(+)
and loc.location_id (+) = party_site.location_id
and ps.gl_date_closed > :P_AS_OF_DATE
and UPPER(:p_curr_code) = ps.invoice_currency_code
and ps.customer_trx_id+0 = gld.customer_trx_id
and gld.account_class = 'REC'
and gld.latest_rec_flag = 'Y'
/* Enhancement 7346488.
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 gld.code_combination_id = cc.code_combination_id
-- Bug# 1233291
-- and ps.payment_schedule_id+0 < :rp_ps_max_id
and ps.cons_inv_id = ci.cons_inv_id(+)
--XLA_MO_REPORTING_API.Get_Predicate('TYPES', null)
--XLA_MO_REPORTING_API.Get_Predicate('PS', null);
-- &P_ORG_WHERE_TRX
-- &P_ORG_WHERE_SITE
--XLA_MO_REPORTING_API.Get_Predicate('ADDR', null);
--XLA_MO_REPORTING_API.Get_Predicate('GLD', null);
UNION ALL
select ps.org_id,
decode (UPPER(:p_order_by),'CUSTOMER',NULL, -999) ,
decode (UPPER(:p_order_by),'CUSTOMER',NULL,
decode(app.applied_payment_schedule_id, -
4, :c_claim_meaning, : c_payment_meaning) ),
/* bug2820114 Unidentified payment must be at end of
report */
decode(party.party_name , null , '2' , rtrim(rpad(substrb('1' ||
party.party_name,1,50),36))),
nvl(cust_acct.cust_account_id, -999),
cust_acct.account_number,
DECODE(UPPER(:p_format_option_low),'B',0, site.site_use_id) ,
DECODE(UPPER(:p_format_option_low),'B',NULL,substr(loc.state,1,2)) ,
DECODE(UPPER(:p_format_option_low),'B',NULL,substr(loc.city,1,25)) ,
DECODE(UPPER(:p_format_option_low),'B',0,addr.cust_acct_site_id )
,
NULL,
ps.payment_schedule_id,
ps.class,
ps.due_date,
-sum ( decode( :rp_convert_flag, 'Y',
app.acctd_amount_applied_from,
app.amount_applied ) ),
ps.amount_due_original,
ps.trx_number
invnum ,
decode (UPPER(:p_order_by),'XXX',NULL,
decode(app.applied_payment_schedule_id, -
4, :c_claim_meaning, :c_payment_meaning)) ,
ceil(:P_AS_OF_DATE - ps.due_date),
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.gl_date,
decode(ps.invoice_currency_code, :functional_currency, NULL,
decode(ps.exchange_rate, NULL, '*', NULL)),
nvl(ps.exchange_rate, 1),
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_inv,
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_inv,
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_inv,
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_inv,
cc.segment1
bal_segment_value_inv,
ci.cons_billing_number cons_billing_number,
rtrim(rpad(nvl(substrb(party.party_name,1,50),:rp_short_unid_phrase),36)),
null,
null
from AR_PAYMENT_SCHEDULES_ALL ps,
ar_cons_inv_all ci,
AR_RECEIVABLE_APPLICATIONS_ALL app,
gl_code_combinations cc,
hz_cust_accounts cust_acct,
hz_parties party,
hz_cust_site_uses_all site,
hz_cust_acct_sites_all addr,
hz_party_sites party_site,
hz_locations loc
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(+)
/* Bug fix 5376284
Replacing 2 parameters with 1 parameters */

and ps.cash_receipt_id+0 = app.cash_receipt_id


and ps.customer_site_use_id+0 = site.site_use_id(+)
and site.cust_acct_site_id = addr.cust_acct_site_id(+)
and addr.party_site_id = party_site.party_site_id(+)
and loc.location_id(+) = party_site.location_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 UPPER(:p_curr_code) = ps.invoice_currency_code
and nvl( ps.receipt_confirmed_flag, 'Y' ) = 'Y'

-- Bug# 1233291
-- and app.receivable_application_id+0 < :rp_app_max_id
-- and ps.payment_schedule_id+0 < :rp_ps_max_id
and ps.cons_inv_id = ci.cons_inv_id(+)
--XLA_MO_REPORTING_API.Get_Predicate('PS', null);
--XLA_MO_REPORTING_API.Get_Predicate('APP', null);
-- &p_org_where_site
--XLA_MO_REPORTING_API.Get_Predicate('ADDR', null);
group by ps.org_id,
party.party_name,
site.site_use_id,
loc.state,
loc.city,
addr.cust_acct_site_id,
cust_acct.cust_account_id,
cust_acct.account_number,
ps.payment_schedule_id,
ps.class,
ps.due_date,
ps.amount_due_original,
ps.trx_number,
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,
cc.segment1 ,
decode( app.status, 'UNID', 'UNID',
'UNAPP'),
ci.cons_billing_number,
app.applied_payment_schedule_id
UNION ALL
select ps.org_id,
decode (UPPER(:p_order_by), 'CUSTOMER', NULL, -999) ,
decode (UPPER(:p_order_by), 'CUSTOMER', NULL, : c_risk_meaning) ,
/* bug2820114 Unidentified payment must be at end of report
*/
decode(party.party_name, null, '2' , rtrim(rpad(substrb('1' ||
party.party_name,1,50),36) )) ,
cust_acct.cust_account_id ,
cust_acct.account_number ,
DECODE(UPPER(:p_format_option_low),'B', 0, site.site_use_id) ,
DECODE(UPPER(:p_format_option_low),'B', NULL,
rtrim(rpad(loc.state,2))) ,
DECODE(UPPER(:p_format_option_low),'B', NULL,
rtrim(rpad(loc.city,25))) ,
DECODE(UPPER(:p_format_option_low),'B', 0, addr.cust_acct_site_id ) ,
null ,
ps.payment_schedule_id ,
decode (UPPER(:p_order_by),'XXX',NULL,:c_risk_meaning) ,
ps.due_date ,
decode( :rp_convert_flag, 'Y', crh.acctd_amount ,
crh.amount ) ,
decode( :rp_convert_flag, 'Y', round( (ps.amount_due_original *

nvl( ps.exchange_rate,1)), :func_curr_precision),


ps.amount_due_original ) ,
ps.trx_number ,
decode (UPPER(:p_order_by),'XXX',NULL,:c_risk_meaning) ,
ceil(:P_AS_OF_DATE - ps.due_date) ,
ps.amount_adjusted ,
ps.amount_applied ,
ps.amount_credited ,
crh.gl_date ,
decode(ps.invoice_currency_code, :functional_currency, NULL,
decode(ps.exchange_rate, NULL, '*', NULL)) ,
nvl(ps.exchange_rate, 1) ,
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_inv,
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_0,
ceil(:P_AS_OF_DATE-ps.due_date)),1,
0) )
b1_inv,
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_0,
ceil(:P_AS_OF_DATE-ps.due_date)),1,
0) )
b2_inv,

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_0,
ceil(:P_AS_OF_DATE-ps.due_date)),1,
0) )
b3_inv,
cc.segment1 ,
ci.cons_billing_number cons_billing_number,
rtrim(rpad(nvl(substrb(party.party_name,1,50),:rp_short_unid_phrase),36)),
null,
null
from hz_cust_accounts cust_acct,
hz_parties party,
AR_PAYMENT_SCHEDULES_ALL ps,
ar_cons_inv_all ci,
hz_cust_site_uses_all site,
hz_cust_acct_sites_all addr,
hz_party_sites party_site,
hz_locations loc,
ar_cash_receipts_all cr,
ar_cash_receipt_history_all crh,
gl_code_combinations cc
where TRUNC(crh.gl_date) <= :p_as_of_date
and ps.trx_number is not null
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 ps.customer_site_use_id = site.site_use_id(+)
and site.cust_acct_site_id = addr.cust_acct_site_id(+)
and addr.party_site_id = party_site.party_site_id(+)
and loc.location_id(+) = party_site.location_id
and UPPER(:p_curr_code) = ps.invoice_currency_code
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')
/* Bug 4127480 : exclude receipts applied to short term debt */
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)

--Bug 2124653
--and ps.payment_schedule_id+0 < :rp_ps_max_id
and ps.cons_inv_id = ci.cons_inv_id(+)
--XLA_MO_REPORTING_API.Get_Predicate('PS', null);
-- &p_org_where_site
--XLA_MO_REPORTING_API.Get_Predicate('ADDR', null);
--XLA_MO_REPORTING_API.Get_Predicate('CR', null);
--XLA_MO_REPORTING_API.Get_Predicate('CRH', null);
UNION ALL
--------------------------
-- Bills Receivable
-----------------
select ps.org_id,
decode (UPPER(:p_order_by),'CUSTOMER',NULL,
types.cust_trx_type_id ) dummy_id_inv,
decode (UPPER(:p_order_by),'CUSTOMER',NULL,types.name)
dummy_type_inv,
/* bug2820114 Unidentified payment must be at end of report
*/
decode(party.party_name , null , '2' , rtrim(rpad(substrb('1' ||
party.party_name,1,50),36)) )
customer_name_inv,
cust_acct.cust_account_id
customer_id_inv,
cust_acct.account_number
customer_number_inv,
DECODE(UPPER(:p_format_option_low),'B',0, site.site_use_id)
contact_site_id_inv,
DECODE(UPPER(:p_format_option_low),'B',NULL,rtrim(rpad(loc.state,2)))
cust_state_inv ,
DECODE(UPPER(:p_format_option_low),'B',NULL,rtrim(rpad(loc.city,25)))
cust_city_inv ,
DECODE(UPPER(:p_format_option_low),'B',0,addr.cust_acct_site_id )
addr_id_inv ,
null ,
ps.payment_schedule_id
payment_sched_id_inv,
ps.class
class_inv,
ps.due_date
due_date_inv ,
decode( :rp_convert_flag, 'Y', ps.acctd_amount_due_remaining,
ps.amount_due_remaining )
amt_due_remaining_inv ,
decode( :rp_convert_flag, 'Y', round( (ps.amount_due_original *
nvl( ps.exchange_rate,1)),
:func_curr_precision), ps.amount_due_original )
amt_due_original_inv ,
ps.trx_number
invnum ,
types.name
invoice_type_inv,
ceil(:P_AS_OF_DATE - ps.due_date)
days_past_due_inv,
ps.amount_adjusted
amount_adjusted_inv,
ps.amount_applied
amount_applied_inv,
ps.amount_credited
amount_credited_inv,
ps.gl_date
gl_date_inv,
decode(ps.invoice_currency_code, :functional_currency, NULL,
decode(ps.exchange_rate, NULL, '*', NULL))
data_converted_inv,
nvl(ps.exchange_rate, 1)
ps_exchange_rate_inv,
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_inv,
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_inv,
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_inv,
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_inv,
cc.segment1
bal_segment_value_inv,
ci.cons_billing_number cons_billing_number,
rtrim(rpad(nvl(substrb(party.party_name,1,50),:rp_short_unid_phrase),36)),
trx.comments,
trx.purchase_order
from ra_cust_trx_types_all types,
hz_cust_accounts cust_acct,
hz_parties party,
AR_PAYMENT_SCHEDULES_ALL ps,
ar_cons_inv_all ci,
RA_CUSTOMER_TRX_ALL trx,
hz_cust_site_uses_all site,
hz_cust_acct_sites_all addr,
hz_party_sites party_site,
hz_locations loc,
ar_transaction_history_all th,
ar_distributions_all dist,
gl_code_combinations cc
where TRUNC(ps.gl_date) <= :p_as_of_date
and ps.class = 'BR'
and ps.customer_trx_id+0 = trx.customer_trx_id
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


-- bug1960831
and nvl(ps.org_id ,-99) = nvl(types.org_id,-99)

and ps.customer_site_use_id+0 = site.site_use_id(+)


and site.cust_acct_site_id = addr.cust_acct_site_id(+)
and addr.party_site_id = party_site.party_site_id(+)
and loc.location_id(+) = party_site.location_id
and ps.gl_date_closed > :P_AS_OF_DATE
and UPPER(:p_curr_code) = ps.invoice_currency_code
and ps.customer_trx_id+0 = th.customer_trx_id
--4147868 and th.current_accounted_flag = 'Y'
and th.transaction_history_id = dist.source_id
and dist.source_table = 'TH'
--4147868 and dist.source_type = 'REC'
/*4147868 : Added following code */
and th.transaction_history_id =
(select max(transaction_history_id)
from ar_transaction_history_all th2,
ar_distributions_all dist2
where th2.transaction_history_id = dist2.source_id
and dist2.source_table = 'TH'
and th2.gl_date <= :p_as_of_date /*GSCC Compliance*/
and dist2.amount_dr is not null
and th2.customer_trx_id = ps.customer_trx_id)
and dist.amount_dr is not null
/*4147868 end*/
and dist.source_table_secondary is NULL
and dist.code_combination_id = cc.code_combination_id

-- Bug# 1233291
-- and ps.payment_schedule_id+0 < :rp_ps_max_id
and ps.cons_inv_id = ci.cons_inv_id(+)
XLA_MO_REPORTING_API.Get_Predicate('TYPES', null)
--XLA_MO_REPORTING_API.Get_Predicate('PS', null);
-- &P_ORG_WHERE_TRX
-- &P_ORG_WHERE_SITE
--XLA_MO_REPORTING_API.Get_Predicate('ADDR', null);
order by 30, 4, 6, 17, 14, 15 desc;

You might also like