From payable invoice (this split is thorough)
Select gjl. je_header_id,
Gjl. je_line_num,
Gjh. je_source,
Gjh. name,
Gjh.doc _ sequence_value,
Pv. vendor_id,
Pv. vendor_name,
Vs. vendor_site_id,
Vs. vendor_site_code vendor_address,
Null gl_desc,
Ai. description invoice_desc, -- invoice description (Abstract)
Gjh. default_1_tive_date, -- GL date
Gcc. segment4, -- Subject
Ael. entered_dr,
Ael. entered_cr,
Decode (ael. entered_cr, null, 'borrower ', 'lender') entered_cls,
'B'
From gl_je_headers_v gjh,
Gl_je_lines_v gjl,
Gl_import_references r,
Ap_ AE _lines_all ael,
Ap_ AE _headers_all aeh,
Ap_accounting_events_all AE,
Ap_invoices_all ai,
Gl_code_combinations_kfv gcc,
Po_vendors pv,
Po_vendor_sites_all
Where gjh. je_source = 'payables'
And gjl. je_header_id = gjh. je_header_id
And gjh. status = 'P'
And r. je_header_id = gjl. je_header_id
And r. je_line_num = gjl. je_line_num
And gjl. code_combination_id = gcc. code_combination_id
And gcc. segment4 = '000000' -- subject section --- ticket payable
And ael. third_party_id = pv. vendor_id
And ael. third_party_sub_id = vs. vendor_site_id
And ael. gl_sl_link_id = r. gl_sl_link_id
And AE. accounting_event_id = aeh. accounting_event_id
And ai. invoice_id = AE. source_id
And AE. source_table = 'ap _ INVOICES'
And aeh. AE _header_id = ael. AE _header_id
And gcc. segment2 = nvl (p_syb_name, gcc. segment2)-Business Department
And pv. vendor_id = nvl (p_vendor_name, pv. vendor_id)
And vs. vendor_site_id = nvl (p_vendor_site, vs. vendor_site_id)
And gjh. period_name between p_begin_date and p_end_date
And r. gl_sl_link_table = 'apache'