Debtor Ledger Report Query
SELECT DISTINCT b.gl_date, c.type type1, --d.CUSTOMER_CATEGORY_CODE , -- d.customer_name, party.party_name customer_name, -- c.NAME Vch_Type, decode(c.type, 'INV', 'Invoice', 'CM', 'Credit Memo', 'DM', 'Debit Memo') vch_type, a.trx_number doc_no, a.trx_date doc_dt, --c.DESCRIPTION particular, d.description particular, b.amount * nvl(a.exchange_rate, 1) amount, --a.customer_trx_id, -- a.INTERFACE_HEADER_ATTRIBUTE1 ref_no, --d.customer_id, cust.cust_account_id, NULL, NULL, NULL, NULL FROM ra_customer_trx_all a, ra_cust_trx_line_gl_dist_all b, ra_cust_trx_types_all c, hz_cust_accounts cust, hz_parties party, hz_cust_acct_sites_all hcasa, hz_cust_site_uses_all hcsua, ( SELECT DISTINCT gcc.code_combination_id, gcc.segment4, ffvl.description, lgd.customer_trx_id FROM ra_customer_trx_all a, ra_cust_trx_line_gl_dist_all lgd, gl_code_combinations gcc, fnd_flex_values_vl ffvl WHERE a.customer_trx_id = lgd.customer_trx_id AND lgd.code_combination_id = gcc.code_combination_id AND gcc.segment4 = ffvl.flex_value AND lgd.account_class = 'REV' ) d WHERE cust.party_id = party.party_id AND cust.cust_account_id = hcasa.cust_account_id AND a.customer_trx_id = d.customer_trx_id AND hcasa.cust_acct_site_id = hcsua.cust_acct_site_id AND a.customer_trx_id = b.customer_trx_id AND a.cust_trx_type_id = c.cust_trx_type_id AND a.bill_to_customer_id = cust.cust_account_id AND a.complete_flag = 'Y' AND b.org_id = nvl(:p_org_id, b.org_id) AND b.latest_rec_flag = 'Y' AND c.type IN ( 'INV', 'CM', 'DM' ) AND b.gl_date IS NOT NULL AND trunc(b.gl_date) BETWEEN nvl(:p_from_date, b.gl_date) AND nvl(:p_to_date, b.gl_date) AND cust.cust_account_id = nvl(:p_from_customer_id, cust.cust_account_id) UNION SELECT DISTINCT ( g.gl_date ), 'REC' type1, party.party_name customer_name, --e.TYPE vch_type, 'Receipt' vch_type, e.receipt_number doc_no, e.receipt_date doc_dt, abca.bank_account_name particular, e.amount amount, cust.cust_account_id, -- e.cash_receipt_id, NULL, NULL, NULL, NULL FROM ar_cash_receipts_all e, ar_cash_receipt_history_all g, ce_bank_accounts abca, ce_bank_acct_uses_all abcau, hz_cust_accounts cust, hz_parties party, ar_receivable_applications_all f, ar_payment_schedules_all d WHERE e.cash_receipt_id = g.cash_receipt_id AND abcau.bank_account_id = abca.bank_account_id AND e.remit_bank_acct_use_id = abcau.bank_acct_use_id AND e.pay_from_customer = cust.cust_account_id AND party.party_id = cust.party_id --and hcasa.CUST_ACCT_SITE_ID=hcsua.CUST_ACCT_SITE_ID --and cust.CUST_ACCOUNT_ID=hcasa.cust_account_id -- and cust.CUST_ACCOUNT_ID=hcasa.cust_account_id AND e.cash_receipt_id = f.receivable_application_id AND d.payment_schedule_id = f.payment_schedule_id -- AND g.status = 'CLEARED' AND g.gl_date BETWEEN nvl(:p_from_date, g.gl_date) AND nvl(:p_to_date, g.gl_date) AND cust.cust_account_id = nvl(:p_from_customer_id, cust.cust_account_id) AND g.org_id = nvl(:p_org_id, g.org_id) UNION SELECT DISTINCT ( g.gl_date ), 'REV' type1, --rc.CUSTOMER_CATEGORY_CODE, party.party_name customer_name, -- e.TYPE vch_type, 'Receipt' vch_type, e.receipt_number doc_no, e.receipt_date doc_dt, abca.bank_account_name particular, e.amount amount, -- null, --e.COMMENTS,--a.TRX_NUMBER||' '||ABCA.BANK_ACCOUNT_NAME ref_no , -- hp.PARTY_NAME, --e.cash_receipt_id, --e.REVERSAL_COMMENTS cust.cust_account_id, NULL, NULL, NULL, NULL FROM ar_payment_schedules_all d, ar_cash_receipts_all e, ar_receivable_applications_all f, ar_cash_receipt_history_all g, --RA_CUSTOMER_TRX_ALL A , hz_cust_accounts cust, hz_parties party, hz_cust_acct_sites_all hcasa, hz_cust_site_uses_all hcsua, ce_bank_accounts abca WHERE d.payment_schedule_id = f.payment_schedule_id AND f.cash_receipt_id = e.cash_receipt_id (+) -- AND f.AMOUNT_APPLIED IS NOT NULL AND f.cash_receipt_id = g.cash_receipt_id --and d.CUSTOMER_ID = nvl(:P_FROM_CUSTOMER_ID,d.customer_id) AND d.customer_id IN ( SELECT DISTINCT cust_account_id FROM hz_cust_acct_sites_all WHERE cust_acct_site_id IN ( SELECT DISTINCT cust_acct_site_id FROM hz_cust_site_uses_all --where GL_ID_REC=:P_CODE) ) ) AND g.gl_date BETWEEN nvl(:p_from_date, g.gl_date) AND nvl(:p_to_date, g.gl_date) AND g.first_posted_record_flag = 'N' AND g.org_id = nvl(:p_org_id, g.org_id) -- and rc.customer_id=d.CUSTOMER_ID -- and rc.customer_id=d.CUSTOMER_ID --and rc.CUSTOMER_ID=hcasa.CUST_ACCOUNT_ID AND cust.party_id = party.party_id -- and D.customer_id = hcasa.cust_account_id AND cust.cust_account_id = hcasa.cust_account_id AND d.customer_id = hcasa.cust_account_id AND hcasa.cust_acct_site_id = hcsua.cust_acct_site_id AND e.receipt_method_id NOT IN ( SELECT receipt_method_id FROM ar_receipt_methods WHERE upper(name) LIKE 'ADVANCE%' ) AND e.reversal_date IS NOT NULL AND e.remittance_bank_account_id = abca.bank_account_id --AND HCSUA.GL_ID_REC=:P_CODE AND e.status IN ( 'REV', 'NSF', 'STOP' ) -- and e.STATUS='REV' AND e.cash_receipt_id IN ( SELECT cash_receipt_id FROM ar_cash_receipt_history_all a WHERE a.status = 'REVERSED' ) AND g.reversal_gl_date IS NULL AND g.reversal_cash_receipt_hist_id IS NULL AND cust.cust_account_id = nvl(:p_from_customer_id, cust.cust_account_id) UNION SELECT DISTINCT h.gl_date, 'EXGL' type, --rc.CUSTOMER_CATEGORY_CODE, party.party_name customer_name, 'Receipt' vch_type, c.receipt_number doc_no,--trx number c.receipt_date doc_dt, abca.bank_account_name particular, --sum((c.EXCHANGE_RATE-t.EXCHANGE_RATE)*(AMOUNT_APPLIED)) SUM((nvl(c.exchange_rate, r.trans_to_receipt_rate) - t.exchange_rate) *(r.amount_applied)) exgainloss, --(c.EXCHANGE_RATE-t.EXCHANGE_RATE)*(AMOUNT_APPLIED) AMOUNT, --rc.CUSTOMER_ID , cust.cust_account_id, NULL, NULL, NULL, NULL FROM ra_customer_trx_all t, ar_receivable_applications_all r, ar_cash_receipts_all c, ar_cash_receipt_history_all h, ce_bank_accounts abca, hz_cust_accounts cust, hz_parties party WHERE r.applied_customer_trx_id = t.customer_trx_id AND c.cash_receipt_id = r.cash_receipt_id AND cust.party_id = party.party_id AND c.cash_receipt_id = h.cash_receipt_id AND c.pay_from_customer = cust.cust_account_id -- AND c.REMITTANCE_BANK_ACCOUNT_ID=ABCA.BANK_ACCOUNT_ID AND c.remit_bank_acct_use_id = abca.bank_account_id AND cust.party_id = party.party_id AND c.pay_from_customer = cust.cust_account_id --and r.CASH_RECEIPT_ID = h.CASH_RECEIPT_ID --and c.PAY_FROM_CUSTOMER = 1040 AND h.status = 'CLEARED' -- and h.STATUS = 'REMITTED' AND h.gl_date BETWEEN nvl(:p_from_date, h.gl_date) AND nvl(:p_to_date, h.gl_date) AND h.org_id = nvl(:p_org_id, h.org_id) AND cust.cust_account_id = nvl(:p_from_customer_id, cust.cust_account_id) --and c.CURRENCY_CODE <> 'INR' --AND RC.CUSTOMER_ID =:P_FROM_CUSTOMER_ID--new --and (nvl(c.exchange_rate,r.TRANS_TO_RECEIPT_RATE)-t.EXCHANGE_RATE)*(r.AMOUNT_APPLIED)<>0 GROUP BY h.gl_date, type, c.receipt_number, c.receipt_date, NULL, NULL, c.cash_receipt_id, c.comments, party.party_name, abca.bank_account_name, cust.cust_account_id UNION ALL SELECT DISTINCT b.gl_date, 'ADJ' type1, -- d.CUSTOMER_CATEGORY_CODE, party.party_name, c.name vch_type, a.trx_number, a.trx_date, -- c.DESCRIPTION particular, d.description particular, b.amount * nvl(a.exchange_rate, 1) amount, a.customer_trx_id, a.interface_header_attribute1 ref_no, -- d.customer_id, NULL, NULL, NULL FROM ra_customer_trx_all a, ar_adjustments_all b, ra_cust_trx_types_all c, --AR_CUSTOMERS d, hz_cust_accounts cust, hz_parties party, hz_cust_acct_sites_all hcasa, hz_cust_site_uses_all hcsua, ( SELECT DISTINCT gcc.code_combination_id, gcc.segment4, ffvl.description, lgd.customer_trx_id FROM ra_customer_trx_all a, ra_cust_trx_line_gl_dist_all lgd, gl_code_combinations gcc, fnd_flex_values_vl ffvl WHERE a.customer_trx_id = lgd.customer_trx_id AND lgd.code_combination_id = gcc.code_combination_id AND gcc.segment4 = ffvl.flex_value AND lgd.account_class = 'REV' ) d WHERE a.customer_trx_id = b.customer_trx_id AND a.cust_trx_type_id = c.cust_trx_type_id AND cust.party_id = party.party_id AND a.bill_to_customer_id = cust.cust_account_id AND cust.cust_account_id = hcasa.cust_account_id AND hcasa.cust_acct_site_id = hcsua.cust_acct_site_id AND a.customer_trx_id = d.customer_trx_id --and a.customer_trx_id <> '69735' --AND a.sold_to_customer_id =d.customer_id AND b.org_id = nvl(:p_org_id, b.org_id) --AND c.type in ('INV','CM','DM') AND b.gl_date IS NOT NULL --AND a.trx_number =:P_TRX AND trunc(b.gl_date) BETWEEN nvl(:p_from_date, b.gl_date) AND nvl(:p_to_date, b.gl_date) AND cust.cust_account_id = nvl(:p_from_customer_id, cust.cust_account_id) --AND HCSUA.GL_ID_REC=:P_CODE; ORDER BY 1, 3