Tuesday, June 25, 2019

PO Transactions

Oracle APPS Query to get all PO Transactions with PO_Headers_all and PO_lines_all

SELECT
  , mp.organization_code as "Org"
  , poh.segment1 "PO_Number"
  , pol.line_num "Line_Number"
  , pda.DESTINATION_TYPE_CODE "Inv or Expense"
  , msib.segment1  "Item"
  , case
        when msib.description is null
            then 'Non-Inventory'
            else msib.description
    end "Item Description"
  , Round(pol.unit_price * cast
        (
            case
                when conversion_rate is null
                    then 1
                    else conversion_rate
            end as decimal
        )
    , 2) "Unit_Cost"
  , plla.quantity "Ordered Quantity"
  , SUM
        (
            case
                when rct.quantity=0
                    then plla.quantity
                    else rct.quantity
            end
        )
    as "Received_Quantity"
  , CAST(rct.transaction_date as date) "Receipt Date"
  , ROUND ( ( SUM
        (
            case
                when rct.quantity=0
                    then plla.quantity
                    else rct.quantity
            end
        )
    ) * pol.unit_price * cast
        (
            case
                when conversion_rate is null
                    then 1
                    else conversion_rate
            end as decimal
        )
    ,2 )           Spend
  , pv.vendor_name Supplier
  , CAST(poh.CREATION_DATE as  date) "PO_Creation_Date"
  , CAST(plla.need_by_date as  date) "Need By Date"
  , CAST(plla.promised_date as date) "Promised Date"
  , pol.unit_price "Unit_Cost_native"
  , poh.currency_code
  , cast
        (
            case
                when conversion_rate is NULL
                    then 1
                    else conversion_rate
            end as decimal
        )
    "Conversion_Rate"
  , msib.organization_id
  , poh.po_header_id
  , pol.po_line_id
  , pv.vendor_id
FROM
    po_headers_all poh
    inner join
        po_lines_all pol
        on
            poh.po_header_id = pol.po_header_id
    inner join
        po_line_locations_all plla
        on
            plla.po_header_id   = pol.po_header_id
            AND plla.po_line_id = pol.po_line_id
    left outer join
        rcv_transactions rct
        on
            rct.po_header_id            = poh.po_header_id
            and rct.po_line_id          = pol.po_line_id
            and rct.po_line_location_id = plla.line_location_id
    inner join
        po_distributions_all pda
        on
            pda.po_header_id         = poh.po_header_id
            and pda.po_line_id       = pol.po_line_id
            and pda.line_location_id = plla.line_location_id
    left outer join
        mtl_system_items_b msib
        on
            pol.item_id                      = msib.inventory_item_id
            and plla.ship_to_organization_id = msib.organization_id
    inner join
        po_vendors pv
        on
            pv.vendor_id = poh.vendor_id
    left outer join
        mtl_parameters mp
        on
            msib.organization_id = mp.organization_id
    left outer join
        gl_daily_rates gdr
        on
            gdr.from_currency       = poh.currency_code
            and gdr.to_currency     = 'USD'
            and gdr.conversion_date = poh.rate_date
            and gdr.conversion_type = 'Corporate'
WHERE
    poh.po_header_id             = pol.po_header_id
    AND poh.authorization_status = 'APPROVED'
    AND poh.type_lookup_code     = 'STANDARD'
    and
    (
        rct.transaction_type          = 'RECEIVE'
        or rct.transaction_type IS NULL
    )
   group by
    wlv.dc
  , mp.organization_code
  , poh.segment1
  , pol.line_num
  , pda.DESTINATION_TYPE_CODE
  , msib.segment1
  , msib.description
  , Round(pol.unit_price * cast
        (
            case
                when conversion_rate is null
                    then 1
                    else conversion_rate
            end as decimal
        )
    , 2)
  , plla.quantity
  , CAST(rct.transaction_date as date)
  , pv.vendor_name
  , CAST(poh.CREATION_DATE as  date)
  , CAST(plla.need_by_date as  date)
  , CAST(plla.promised_date as date)
  , 'PO'
  , pol.unit_price
  , poh.currency_code
  , cast
        (
            case
                when conversion_rate is NULL
                    then 1
                    else conversion_rate
            end as decimal
        )
  , msib.organization_id
  , poh.po_header_id
  , pol.po_line_id
  , pv.vendor_id