Tuesday 29 October 2013

PO to AP invoice and hold details Query

 SELECT DISTINCT   ai.invoice_num,
                   ai.Invoice_date,
                   ai.Invoice_amount,
                   ail.line_number invoice_line_number,
                   ail.description,
                   ail.period_name,
                   ail.accounting_date,
                   ail.amount Invoice_line_Amount,
                   ail.quantity_invoiced,
                   pv.vendor_name,
                   pvs.vendor_site_code Vendor_SITE,
                   MAX (TRUNC (aha.hold_date)) HOLD_DATE,
                   MAX (TRUNC (aha.last_update_date)) Hold_Update_date,
                   DECODE (AHa.RELEASE_LOOKUP_CODE,
                           NULL, NULL,
                           AHa.LAST_UPDATE_DATE)
                      RELEASE_DATE,
                      aha.HOLD_LOOKUP_CODE HOLD_CODE,
                      aha.hold_reason,
                      ph.segment1 PO_NUMBER,
                      pl.line_num PO_LINE_NUMBER,
                      pl.quantity PO_LINE_QUANTITY,
                      pl.unit_price*pl.quantity PO_LINE_AMOUNT,
                  DECODE (PLT.MATCHING_BASIS,
                           'QUANTITY', PLL.QUANTITY,
                           'AMOUNT', PLL.AMOUNT)
                      SHIPMENT_ORDERED,
                   DECODE (PLT.MATCHING_BASIS,
                           'QUANTITY', PLL.QUANTITY_BILLED,
                           'AMOUNT', PLL.AMOUNT_BILLED)
                      SHIPMENT_BILLED,
                   DECODE (PLT.MATCHING_BASIS,
                           'QUANTITY', PLL.QUANTITY_RECEIVED,
                           'AMOUNT', PLL.AMOUNT_RECEIVED)
                      SHIPMENT_RECEIVED,
                      hrl.location_code,
                        ai.org_id Service_Line
              FROM AP_holds_all aha,
                   po_line_locations_all pll,
                   PO_HEADERS_ALL ph,
                   PO_LINES_ALL PL,
                   PO_LINE_TYPES PLT,
                   PO_DISTRIBUTIONS_ALL pda,
                   PO_VENDORS pv,
                   po_vendor_sites_all pvs,
                   AP_invoices_all ai,
                   AP_Invoice_lines_all ail,
                   AP_INVOICE_DISTRIBUTIONS_ALL aida,
                   ORG_ORGANIZATION_DEFINITIONS OOD,
                   HR_LOCATIONS_ALL_TL HRL
             WHERE     pll.line_location_id = aha.line_location_id
                   AND AHa.RELEASE_LOOKUP_CODE IS NULL
                   AND PLL.PO_LINE_ID = PL.PO_LINE_ID(+)
                   AND PL.LINE_TYPE_ID = PLT.LINE_TYPE_ID(+)
                   AND pda.po_header_id = pl.po_header_id
                   AND pda.po_header_id = ph.po_header_id
                   AND ph.po_header_id = pl.po_header_id
                   and pda.po_line_id = pl.po_line_id
                   AND pda.po_distribution_id(+) = aida.po_distribution_id
                   and ail.line_number = aida.invoice_line_number
                   AND ail.invoice_id = aha.Invoice_id
                   AND ail.cancelled_flag = 'N'
                   AND ail.amount > '0'
                   AND ai.invoice_id = ail.Invoice_id
                   AND pv.vendor_id = ai.vendor_id
                   AND pv.vendor_id = pvs.vendor_id
                   AND pvs.vendor_site_id = ai.vendor_site_id
                   AND HRL.LOCATION_ID(+) = PLL.SHIP_TO_LOCATION_ID
                   AND OOD.ORGANIZATION_ID(+) = PLL.SHIP_TO_ORGANIZATION_ID
                   and aha.org_id in ( :1)
                   AND ai.invoice_num in (:2 )
          GROUP BY aha.invoice_id,
                   aha.line_location_id,
                   aha.HOLD_LOOKUP_CODE,
                   aha.hold_reason,
                   aha.release_lookup_code,
                   aha.release_reason,
                   ai.invoice_num,
                   ai.Invoice_amount,
                   ai.Invoice_date,
                   ail.invoice_id,
                   ail.line_number,
                   ail.description,
                   ail.period_name,
                   ail.accounting_date,
                   ail.amount,
                   ail.quantity_invoiced,
                   pv.vendor_name,
                  pvs.vendor_site_code,
                  ph.segment1,
                      pl.line_num ,
                      pl.quantity,
                      pl.unit_price*pl.quantity ,
                      ai.org_id,
                      hrl.location_code,
                   DECODE (AHa.RELEASE_LOOKUP_CODE,
                           NULL, NULL,
                           AHa.LAST_UPDATE_DATE),
                   DECODE (PLT.MATCHING_BASIS,
                           'QUANTITY', PLL.QUANTITY,
                           'AMOUNT', PLL.AMOUNT),
                   DECODE (PLT.MATCHING_BASIS,
                           'QUANTITY', PLL.QUANTITY_BILLED,
                           'AMOUNT', PLL.AMOUNT_BILLED),
                   DECODE (PLT.MATCHING_BASIS,
                           'QUANTITY', PLL.QUANTITY_RECEIVED,
                           'AMOUNT', PLL.AMOUNT_RECEIVED)
                   order by 2,1,4, 18
                          
                       

No comments:

Post a Comment