OM: Order Header and Order Line base Advance Query

BEGIN
   mo_global.init('ONT');
END;


BEGIN
   mo_global.init('ONT');
END;


SELECT   ac.customer_name, ac.customer_number, ooh.order_number,
         ooh.cust_po_number, ooh.ordered_date,
         ooh.flow_status_code status_code, ooh.order_firmed_date,
         ood.organization_code warehouse, ooh.fob_point_code fob,
         ooh.freight_terms_code, ooh.shipping_method_code,
         ooh.shipment_priority_code, ooh.transactional_curr_code curr_code,
         ot.NAME order_type,
         (SELECT s.NAME
            FROM jtf.jtf_rs_salesreps s
           WHERE s.org_id = ood.operating_unit
             AND s.salesrep_id = ooh.salesrep_id) sales_parson,
         (SELECT qpl.NAME
            FROM qp_list_headers_vl qpl
           WHERE list_header_id = ooh.price_list_id) price_list,
         (SELECT rat.NAME
            FROM ra_terms rat
           WHERE rat.term_id = ooh.payment_term_id) payment_term_name, 
         ooh.orig_sys_document_ref,
         ooh.attribute1 header_ddf1, ooh.attribute2 header_ddf2,
         ooh.attribute3 header_ddf3, ooh.attribute4 header_ddf4,
         ooh.attribute5 header_ddf5, ooh.attribute6 header_ddf6,
----------------Line Info below-----------
         ool.line_number, ool.ordered_item, ool.ordered_quantity,
         ool.shipped_quantity, ool.schedule_ship_date,
         ool.order_quantity_uom uom, ool.request_date,
         ool.flow_status_code line_status_code,
         ool.tax_value,
        (SELECT transaction_type_code FROM ont.oe_transaction_types_all where transaction_type_id =ool.LINE_TYPE_ID)LINE_TYPE,
        (SELECT process_name FROM oe_workflow_assignments WHERE order_type_id = ooh.order_type_id and line_type_id= ool.line_type_id) WF_PROCESS_NAME,
         (SELECT NAME
            FROM pa_projects_all
           WHERE project_id = ool.project_id) project_name,
         (SELECT task_name
            FROM pa_tasks
           WHERE task_id = ool.task_id) task_name, ool.return_reason_code,
         ool.reference_type, ool.unit_selling_price unit_price, ool.tax_code,
         ool.tax_rate, ool.tax_value, ship_to.address1 ship_to_address1,
         ship_to.address2 ship_to_address2, ship_to.address3 ship_to_address3,
         ship_to.city ship_to_city, ship_to.state ship_to_state,
         ship_to.nls_territory bill_to_territory,
         ship_to.postal_code ship_to_postal_code,
         ship_to.province ship_to_pin, bill_to.address1 bill_to_address1,
         bill_to.address2 bill_to_address2, bill_to.address3 bill_to_address3,
         bill_to.city bill_to_city, bill_to.state bill_to_state,
         bill_to.nls_territory bill_to_territory,
         bill_to.postal_code bill_to_postal_code,
         bill_to.province bill_to_pin, ool.attribute1 line_ddf1,
         ool.attribute2 line_ddf2, ool.attribute3 line_ddf3,
         ool.attribute4 line_ddf4,ool.attribute5 line_ddf5,
         ool.attribute6 line_ddf6,
(select lookup_code from apps.oe_lookups where upper (lookup_type) = 'FOB' and enabled_flag = 'Y' and upper (lookup_code) = upper (ooh.fob_point_code) )FOB,
(select lookup_code from apps.oe_lookups where upper (lookup_type) = 'FREIGHT_TERMS' and enabled_flag = 'Y' and upper (lookup_code) = upper (ooh.freight_terms_code) )Freight_Terms,
(Select CHARGE_AMOUNT from OE_CHARGE_LINES_V  ocl where ocl.HEADER_ID=ool.HEADER_ID and ocl.LINE_ID=ool.LINE_ID) FREIGHT_CHARGE,
    ool.line_category_code,
    (SELECT order_number FROM oe_order_headers_all WHERE header_id = ool.return_attribute1) RMA_SALES_ORDER,
    (SELECT line_number  FROM oe_order_lines_all WHERE header_id = ool.return_attribute1 AND line_id = ool.return_attribute2) RMA_SO_Line_No
    FROM oe_order_headers_all ooh,
         oe_order_lines_all ool,
         oe_transaction_types_tl ot,
         ar_customers ac
       , org_organization_definitions ood
----------------------Bill To Customer-------------------------------
         ,(SELECT hp.party_name, hp.party_number, hca.account_number,
                 hca.cust_account_id, hp.party_id, hps.party_site_id,
                 hcsu.cust_acct_site_id, hps.location_id, hl.address1,
                 hl.address2, hl.address3, hl.city, hl.state,
                 ter.nls_territory, hl.postal_code, hl.province,
                 hcsu.site_use_code, hcsu.site_use_id, hcsa.bill_to_flag
            FROM hz_parties hp,
                 hz_party_sites hps,
                 hz_locations hl,
                 hz_cust_accounts_all hca,
                 hz_cust_acct_sites_all hcsa,
                 hz_cust_site_uses_all hcsu,
                 fnd_territories ter
           WHERE hp.party_id = hps.party_id
             AND hps.location_id = hl.location_id
             AND hp.party_id = hca.party_id
             AND hcsa.party_site_id = hps.party_site_id
             AND hcsu.cust_acct_site_id = hcsa.cust_acct_site_id
             AND hca.cust_account_id = hcsa.cust_account_id
             AND hl.country = ter.territory_code
             AND hcsu.site_use_code = 'BILL_TO') bill_to
----------------------Ship To Customer-------------------------------
         ,(SELECT hp.party_name, hp.party_number, hca.account_number,
                 hca.cust_account_id, hp.party_id, hps.party_site_id,
                 hcsu.cust_acct_site_id, hps.location_id, hl.address1,
                 hl.address2, hl.address3, hl.city, hl.state,
                 ter.nls_territory, hl.postal_code, hl.province,
                 hcsu.site_use_code, hcsu.site_use_id, hcsa.bill_to_flag
            FROM hz_parties hp,
                 hz_party_sites hps,
                 hz_locations hl,
                 hz_cust_accounts_all hca,
                 hz_cust_acct_sites_all hcsa,
                 hz_cust_site_uses_all hcsu,
                 fnd_territories ter
           WHERE hp.party_id = hps.party_id
             AND hps.location_id = hl.location_id
             AND hp.party_id = hca.party_id
             AND hcsa.party_site_id = hps.party_site_id
             AND hcsu.cust_acct_site_id = hcsa.cust_acct_site_id
             AND hca.cust_account_id = hcsa.cust_account_id
             AND hl.country = ter.territory_code
             AND hcsu.site_use_code = 'SHIP_TO') ship_to
   WHERE ooh.header_id = ool.header_id
     AND ooh.order_type_id = ot.transaction_type_id
     AND ac.customer_id = ooh.sold_to_org_id
--     AND ooh.flow_status_code = 'BOOKED'
     AND ood.organization_id = ooh.ship_from_org_id
     AND ood.organization_code = NVL (:p_inv_org_code, ood.organization_code)
     AND ooh.order_number = NVL (:p_sales_order_no, ooh.order_number)
     AND ship_to.cust_account_id = ooh.sold_to_org_id
     AND ship_to.site_use_id = ooh.ship_to_org_id
     AND bill_to.cust_account_id = ooh.sold_to_org_id
     AND bill_to.site_use_id = ooh.invoice_to_org_id
ORDER BY order_number
/


OM: Order header and line base query

BEGIN
mo_global.init('ONT');
END;


SELECT
     AC.CUSTOMER_NAME, AC.CUSTOMER_NUMBER
    ,OOH.ORDER_NUMBER,OOH.CUST_PO_NUMBER
    ,OOH.ORDERED_DATE, OOH.FLOW_STATUS_CODE STATUS_CODE
    ,OOH.ORDER_FIRMED_DATE
    ,OOD.ORGANIZATION_CODE WAREHOUSE
    ,OOH.FOB_POINT_CODE FOB
    ,OOH.FREIGHT_TERMS_CODE
    ,OOH.SHIPPING_METHOD_CODE
    ,OOH.SHIPMENT_PRIORITY_CODE
    ,OOH.TRANSACTIONAL_CURR_CODE CURR_CODE
    ,OOH.ATTRIBUTE1 HEADER_DDF1,OOH.ATTRIBUTE2 HEADER_DDF2,OOH.ATTRIBUTE3 HEADER_DDF3,OOH.ATTRIBUTE4 HEADER_DDF4
    ,OT.NAME ORDER_TYPE
    ,( select s.name  from JTF.jtf_rs_salesreps s where S.ORG_ID=OOD.OPERATING_UNIT AND s.salesrep_id = ooh.salesrep_id) SALES_PARSON
    ,(SELECT NAME FROM qp_list_HEADERS_VL qpl WHERE LIST_HEADER_ID = OOH.PRICE_LIST_ID) PRICE_LIST
    ,(SELECT RAT.NAME FROM RA_TERMS RAT WHERE RAT.TERM_ID=OOH.PAYMENT_TERM_ID) PAYMENT_TERM_NAME
    ,OOL.LINE_NUMBER
    ,OOL.ORDERED_ITEM
    ,OOL.ORDERED_QUANTITY
    ,OOL.SHIPPED_QUANTITY
    ,OOL.SCHEDULE_SHIP_DATE
    ,OOL.ORDER_QUANTITY_UOM UOM
    ,OOL.REQUEST_DATE
    ,OOl.FLOW_STATUS_CODE LINE_STATUS_CODE
    ,(SELECT NAME  FROM pa_projects_all WHERE project_id = OOL.PROJECT_ID) PROJECT_NAME
    ,(SELECT TASK_NAME FROM pa_tasks WHERE task_id = OOL.TASK_ID)TASK_NAME
    ,OOL.RETURN_REASON_CODE
    ,OOL.REFERENCE_TYPE
    ,OOL.UNIT_SELLING_PRICE UNIT_PRICE
    ,OOL.TAX_CODE,OOL.TAX_RATE,OOL.TAX_VALUE
    ,SHIP_TO.address1 SHIP_TO_ADDRESS1,SHIP_TO.address2 SHIP_TO_ADDRESS2,SHIP_TO.address3 SHIP_TO_ADDRESS3,SHIP_TO.city SHIP_TO_CITY
    ,SHIP_TO.state SHIP_TO_STATE,SHIP_TO.nls_territory BILL_TO_TERRITORY,SHIP_TO.postal_code SHIP_TO_POSTAL_CODE,SHIP_TO.province SHIP_TO_PIN
    ,BILL_TO.address1 BILL_TO_ADDRESS1,BILL_TO.address2 BILL_TO_ADDRESS2,BILL_TO.address3 BILL_TO_ADDRESS3,BILL_TO.city BILL_TO_CITY
    ,BILL_TO.state BILL_TO_STATE,BILL_TO.nls_territory BILL_TO_TERRITORY,BILL_TO.postal_code BILL_TO_POSTAL_CODE,BILL_TO.province BILL_TO_PIN
    ,OOL.ATTRIBUTE1 LINE_DDF1,OOH.ATTRIBUTE2 LINE_DDF2,OOH.ATTRIBUTE3 LINE_DDF3,OOH.ATTRIBUTE4 LINE_DDF4
FROM
     OE_ORDER_HEADERS_ALL OOH
    ,OE_ORDER_LINES_ALL OOL
    ,OE_TRANSACTION_TYPES_TL OT
    ,AR_CUSTOMERS AC
--    ,MTL_PARAMETERS MP
   ,ORG_ORGANIZATION_DEFINITIONS OOD
        ----------------------Bill To Customer-------------------------------
,(SELECT hp.party_name,
       hp.party_number,
       hca.account_number,
       hca.cust_account_id,
       hp.party_id,
       hps.party_site_id,
       hcsu.cust_acct_site_id,
       hps.location_id,
       hl.address1,
       hl.address2,
       hl.address3,
       hl.city,
       hl.state,
       ter.nls_territory,
       hl.postal_code,
       hl.province,
       hcsu.site_use_code,
       hcsu.site_use_id,
       hcsa.bill_to_flag
FROM hz_parties hp,
     hz_party_sites hps,
     hz_locations hl,
     hz_cust_accounts_all hca,
     hz_cust_acct_sites_all hcsa,
     hz_cust_site_uses_all hcsu,
     fnd_territories ter
WHERE     hp.party_id = hps.party_id
      AND hps.location_id = hl.location_id
      AND hp.party_id = hca.party_id
      AND hcsa.party_site_id = hps.party_site_id
      AND hcsu.cust_acct_site_id = hcsa.cust_acct_site_id
      AND hca.cust_account_id = hcsa.cust_account_id
      AND hl.country = ter.territory_code
      AND hcsu.site_use_code = 'BILL_TO')BILL_TO
----------------------Ship To Customer-------------------------------
,(SELECT hp.party_name,
       hp.party_number,
       hca.account_number,
       hca.cust_account_id,
       hp.party_id,
       hps.party_site_id,
       hcsu.cust_acct_site_id,
       hps.location_id,
       hl.address1,
       hl.address2,
       hl.address3,
       hl.city,
       hl.state,
       ter.nls_territory,
       hl.postal_code,
       hl.province,
       hcsu.site_use_code,
       hcsu.site_use_id,
       hcsa.bill_to_flag
FROM hz_parties hp,
     hz_party_sites hps,
     hz_locations hl,
     hz_cust_accounts_all hca,
     hz_cust_acct_sites_all hcsa,
     hz_cust_site_uses_all hcsu,
     fnd_territories ter
WHERE     hp.party_id = hps.party_id
      AND hps.location_id = hl.location_id
      AND hp.party_id = hca.party_id
      AND hcsa.party_site_id = hps.party_site_id
      AND hcsu.cust_acct_site_id = hcsa.cust_acct_site_id
      AND hca.cust_account_id = hcsa.cust_account_id
      AND hl.country = ter.territory_code
      AND hcsu.site_use_code = 'SHIP_TO')SHIP_TO
WHERE
     OOH.HEADER_ID=OOL.HEADER_ID
AND  OOH.ORDER_TYPE_ID=OT.TRANSACTION_TYPE_ID
AND  AC.CUSTOMER_ID=OOH.SOLD_TO_ORG_ID
AND OOH.FLOW_STATUS_CODE ='BOOKED'
AND OOD.ORGANIZATION_ID = OOH.SHIP_FROM_ORG_ID
AND OOD.ORGANIZATION_CODE = NVL(:P_INV_ORG_CODE,OOD.ORGANIZATION_CODE) 
AND OOH.ORDER_NUMBER = NVL(:P_SALES_ORDER_NO, OOH.ORDER_NUMBER)
AND SHIP_TO.cust_account_id = OOH.SOLD_TO_ORG_ID 
AND SHIP_TO.site_use_id = OOH.SHIP_TO_ORG_ID  
AND BILL_TO.cust_account_id = OOH.SOLD_TO_ORG_ID 
AND BILL_TO.site_use_id = OOH.INVOICE_TO_ORG_ID 
Order By ORDER_NUMBER