Tuesday, 6 February 2018

QUERY TO LINK BETWEEN FA , AP AND RCV SHIPMENTS

SELECT DISTINCT gcc.segment1 bc,
         gcc.segment2 rc,
         gcc.segment3 department_code,
         fad.asset_number asset_number,
         fad.attribute_category_code asset_category,
         fad.description asset_desc,
         fab.cost cost,
         fai.po_number,
         fai.invoice_number,
         CONVERT (fai.description, 'AL32UTF8') line_description,
         fai.fixed_assets_cost line_amount,
         aps.segment1 supplier_number,
         aps.vendor_name supplier_name,
         to_char(fab.date_placed_in_service,'DD-MON-YY') date_in_service
         ,fth.transaction_type_code transaction_type
         ,ftv.period_effective effective_period
         ,ftv.period_entered
         ,aia.doc_sequence_value voucher_num
         ,aia.invoice_currency_code invoice_currency
         ,rsh.receipt_num receipt_number
         ,to_char(rsh.creation_date,'DD-MON-YY') receipt_date
    FROM fa_additions_v fad,
         fa_books fab,
         fa_distribution_history fdh,
         gl_code_combinations gcc,
         fa_asset_invoices fai,
         ap_suppliers aps,
         ap_invoices_all aia,
         fa_book_controls bc,
         ap_invoice_distributions_all aida
         ,fa_transaction_headers fth
         ,fa_transaction_history_trx_v ftv
         ,fa_lookups_tl lotl,
         fa_lookups_b flb
         ,rcv_transactions rt
         ,rcv_shipment_headers rsh
   WHERE     fad.asset_id = fdh.asset_id
         AND fab.book_type_code = :p_book_type
         AND fab.Date_ineffective IS NULL
         AND fai.invoice_id = aia.invoice_id
         AND aida.invoice_id = aia.invoice_id
         AND bc.book_type_code = fab.book_type_code
         AND fdh.transaction_header_id_in = fth.transaction_header_id
         AND flb.lookup_code = lotl.lookup_code
         AND flb.lookup_code = fth.transaction_type_code
         AND rt.transaction_id = aida.rcv_transaction_id
         AND rt.shipment_header_id = rsh.shipment_header_id
         AND ftv.transaction_header_id = fth.transaction_header_id
         AND fdh.date_ineffective IS NULL   
         AND(( (:p_from_period = :p_to_period) AND
                    fdh.date_effective < (NVL((SELECT dp.period_close_date
                   FROM fa_deprn_periods dp
                  WHERE dp.book_type_code = :p_book_type
                        AND dp.period_name = :p_to_period),SYSDATE)))
                OR ( fdh.date_effective between
                (SELECT dp.period_open_date
                   FROM fa_deprn_periods dp
                  WHERE dp.book_type_code = :p_book_type
                        AND dp.period_name = :p_from_period)
            AND NVL((SELECT dp.period_close_date
                   FROM fa_deprn_periods dp
                  WHERE dp.book_type_code = :p_book_type
                        AND dp.period_name = :p_to_period),SYSDATE)) )     
         AND fab.book_type_code = fdh.book_type_code
         AND fab.asset_id = fdh.asset_id
         AND fdh.code_combination_id = gcc.code_combination_id
AND fad.asset_type = DECODE(:p_asset_type,'ALL', fad.asset_type,:p_asset_type)
         AND gcc.segment1 = DECODE (:p_chr_bc, 'ALL', gcc.segment1, :p_chr_bc)
         AND gcc.segment2 = DECODE (:p_chr_rc, 'ALL', gcc.segment2, :p_chr_rc)
         AND fdh.asset_id = fai.asset_id(+)
         AND fai.date_ineffective IS NULL
         AND aps.vendor_id(+) = fai.po_vendor_id
ORDER BY fad.asset_number