Вы находитесь на странице: 1из 2

SELECT

inv.invoice_num,
inv.invoice_id,
inv.invoice_type_lookup_code
invoice_type,
inv.vendor_id,
pv.vendor_name,
ph.segment1 po_number,
ph.creation_date po_date,
invoice_date,
inv.creation_date transaction_date,
inv.gl_date,
att.NAME,
inv.invoice_amount,
NVL (inv.tax_amount,
(SELECT SUM (aid2.amount)
FROM ap_invoice_distributions_all aid2
WHERE aid2.invoice_id = inv.invoice_id
AND line_type_lookup_code = 'TAX')
) tax_amount,
(SELECT SUM (aid2.amount)
FROM ap_invoice_distributions_all aid2
WHERE aid2.invoice_id = inv.invoice_id
AND line_type_lookup_code = 'FREIGHT') feight_amount,
(SELECT SUM (aid2.amount)
FROM ap_invoice_distributions_all aid2
WHERE aid2.invoice_id = inv.invoice_id
AND line_type_lookup_code = 'MISCELLANEOUS') total_other_c

harges,
aid.distribution_line_number inv_line,
NVL (msib.segment1, pl.vendor_product_num) part_number,
NVL (msib.description,REPLACE (pl.item_description, CHR (10),
' ')) itm_desc,
inv.invoice_currency_code,
aid.unit_price,
aid.quantity_invoiced,
aid.matched_uom_lookup_code,
aid.amount inv_extended_price,
--line_tax,
--line_freight,
--line_other_charges,
DECODE (tc.NAME, 'EXEMPT', 'Y', NULL, 'Y', 'N') tax_exempt,
aid.tax_code_id,
pll.ship_to_location_id,
hl.location_code,
hl.address_line_1 ship_to_address,
hl.town_or_city ship_to_city,
hl.region_2 ship_to_state,
hl.postal_code ship_to_zip,
hl.country ship_to_country,
gcc.segment1,
ffv1.description entity,
gcc.segment2,
ffv2.description branch,
gcc.segment3,
ffv3.description cost_center,
gcc.segment4,
ffv4.description ACCOUNT,
line_type_lookup_code
FROM ap_invoices_all inv,
ap_invoice_distributions_all aid,

gl_code_combinations gcc,
po_vendors pv,
po_distributions_all pd,
po_headers_all ph,
po_lines_all pl,
po_line_locations_all pll,
ap_terms att,
financials_system_params_all fsp,
mtl_system_items_tl msit,
hr_locations hl,
mtl_system_items_b msib,
ap_tax_codes_all tc,
fnd_flex_values_vl ffv1,
fnd_flex_values_vl ffv2,
fnd_flex_values_vl ffv3,
fnd_flex_values_vl ffv4
WHERE aid.invoice_id = inv.invoice_id
AND pv.vendor_id = inv.vendor_id
AND aid.po_distribution_id = pd.po_distribution_id(+)
AND pd.po_header_id = ph.po_header_id(+)
AND pd.line_location_id = pll.line_location_id(+)
AND att.term_id = inv.terms_id
AND pll.po_line_id = pl.po_line_id(+)
AND aid.dist_code_combination_id = gcc.code_combination_id
AND pl.item_id = msib.inventory_item_id(+)
AND pll.ship_to_location_id = hl.location_id(+)
AND msib.inventory_item_id = msit.inventory_item_id(+)
AND msib.organization_id = msit.organization_id(+)
AND pl.org_id = fsp.org_id(+)
AND aid.tax_code_id = tc.tax_id(+)
AND gcc.segment1 IN (6501, 3501, 3929, 9910)
AND TO_CHAR (gcc.segment1) = ffv1.flex_value
AND ffv1.flex_value_set_id = 1007703
AND TO_CHAR (gcc.segment2) = ffv2.flex_value
AND ffv2.flex_value_set_id = 1007704
AND TO_CHAR (gcc.segment3) = ffv3.flex_value
AND ffv3.flex_value_set_id = 1007705
AND TO_CHAR (gcc.segment4) = ffv4.flex_value
AND ffv4.flex_value_set_id = 1007706
AND ((pd.destination_type_code = 'EXPENSE' and pd.destination_typ
e_code is not null) or pd.destination_type_code is null)
-- and (gcc.Segment4 between 160000 and 169999 Or gcc.segment4
between 660100 and 660199)
AND inv.invoice_amount <> NVL (cancelled_amount, -999999999)
AND ((pl.item_id IS NOT NULL AND msib.organization_id = fsp.invent
ory_organization_id) OR pl.item_id IS NULL)
AND USERENV ('LANG') = msit.LANGUAGE(+)
AND inv.gl_date BETWEEN :p_start_date AND :p_end_date
ORDER BY inv.invoice_id, aid.distribution_line_number;