select
aps.vendor_name,
apss.vendor_site_code,
aia.invoice_num,
DECODE(aia.PAYMENT_STATUS_FLAG,'N','UN-PAID','P','Partial Paid','Y','PAID') PAYMENT_STATUS_FLAG ,
aia.invoice_date,
aia.invoice_amount,
pha.segment1 po_number,
apt.name Term_name,
aca.check_number,
aipa.amount payment_amount,
apsa.amount_remaining,
aipa.invoice_payment_type,
hou.name operating_unit
from apps.ap_invoices_all aia,
ap_suppliers aps,
ap_supplier_sites_all apss,
po_headers_all pha,
ap_invoice_payments_all aipa,
ap_checks_all aca,
ap_payment_schedules_all apsa,
ap_terms apt,
hr_operating_units hou
where 1=1
and aia.vendor_id=aps.vendor_id
and aia.VENDOR_SITE_ID=APSS.VENDOR_SITE_ID
AND aps.vendor_id=apss.VENDOR_ID
and aia.po_header_id=pha.po_header_id(+)
and aipa.invoice_id=aia.invoice_id
and aca.check_id=aipa.check_id
and apsa.invoice_id=aia.invoice_id
and apt.term_id=aia.terms_id
and hou.organization_id=aia.org_id
--and aia.ORG_ID=:P_ORG_ID;