Tuesday, 12 February 2019

Query to Get VENDOR, PO, INVOICE AND PAYMENT DETAILS

--Query to Get VENDOR, PO, INVOICE AND PAYMENT DETAILS
SELECT DISTINCT a.org_id
      ,e.segment1 VENDOR_NUMBER
      ,e.vendor_name
      ,UPPER (e.vendor_type_lookup_code) VENDOR_TYPE
     ,f.vendor_site_code
     ,TO_CHAR (TRUNC(d.creation_date)) PO_DATE
     ,d.segment1 PO_NUM
     ,d.type_lookup_code PO_TYPE
     ,c.quantity_ordered QTY_ORDERED
     ,c.quantity_cancelled QTY_CANCELLED
     ,g.item_description ITEM_DESCRIPTION
     ,g.unit_price UNIT_PRICE
     ,((NVL (c.quantity_ordered, 0) - NVL (c.quantity_cancelled, 0))
      * NVL (g.unit_price, 0)) PO_LINE_AMOUNT
     ,(SELECT DECODE (ph.approved_flag,'Y', 'Approved')
         FROM po.po_headers_all ph
        WHERE ph.po_header_id = d.po_header_id) PO_APPROVED
     ,a.invoice_type_lookup_code INVOICE_TYPE
     ,a.invoice_amount INVOICE_AMOUNT
     ,TO_CHAR (TRUNC (a.invoice_date)) INVOICE_DATE
     ,a.invoice_num INVOICE_NUMBER
     ,(SELECT DECODE (x.match_status_flag,'A', 'Approved')
         FROM ap.ap_invoice_distributions_all x
        WHERE x.invoice_distribution_id = b.invoice_distribution_id) INVOICE_APPROVED
     ,a.amount_paid
     ,h.check_id
     ,i.check_number
     ,h.invoice_payment_id
     ,TO_CHAR (TRUNC(i.check_date)) PAYMENT_DATE
FROM ap_invoices_all a
    ,ap_invoice_distributions_all b
    ,po_distributions_all c
    ,po_headers_all d
    ,ap_suppliers e
    ,ap_supplier_sites_all f
    ,po_lines_all g
    ,ap_invoice_payments_all h
    ,ap_checks_all i
WHERE a.invoice_id = b.invoice_id
  AND b.po_distribution_id = c.po_distribution_id(+)
  AND c.po_header_id = d.po_header_id(+)
  AND e.vendor_id(+) = d.vendor_id
  AND f.vendor_site_id(+) = d.vendor_site_id
  AND d.po_header_id = g.po_header_id
  AND c.po_line_id = g.po_line_id
  AND a.invoice_id = h.invoice_id
  AND h.check_id = i.check_id
  AND c.po_header_id IS NOT NULL
  AND a.payment_status_flag = 'Y'
  AND d.type_lookup_code != 'BLANKET'
  AND EXISTS (SELECT 1
      FROM hr_operating_units
     WHERE 1=1
       AND organization_id = d.org_id
       AND business_group_id = 81)
ORDER BY E.VENDOR_NAME

No comments:

Post a Comment