CREATE OR REPLACE FORCE VIEW dev_fin.dv_asset_mass_addition_v (operating_unit,
                                                               ou_id,
                                                               invoice_number,
                                                               voucher_number,
                                                               distribution_line,
                                                               invoice_line,
                                                               status,
                                                               description,
                                                               TYPE,
                                                               units,
                                                               COST,
                                                               asset_category,
                                                               asset_category_id,
                                                               supplier_name,
                                                               supplier_number,
                                                               supplier_id,
                                                               supplier_site,
                                                               supplier_site_id,
                                                               po_number,
                                                               po_header_id,
                                                               po_line_id,
                                                               po_line_no,
                                                               project_name,
                                                               project_id,
                                                               task_name,
                                                               task_id,
                                                               SOURCE,
                                                               invoice_date,
                                                               inv_currency,
                                                               asset_type,
                                                               asset_book,
                                                               asset_book_id,
                                                               date_in_service,
                                                               depreciate_method,
                                                               life_years,
                                                               months,
                                                               currency_id,
                                                               invoice_id,
                                                               invoice_type,
                                                               invoice_line_id,
                                                               LOCATION,
                                                               location_id,
                                                               distribution_id,
                                                               gl_date,
                                                               salvage_percentage,
                                                               parent_line_id,
                                                               mass_addition_id
                                                              )
AS
   SELECT   "OPERATING_UNIT", "OU_ID", "INVOICE_NUMBER", "VOUCHER_NUMBER",
            "DISTRIBUTION_LINE", "INVOICE_LINE", "STATUS", "DESCRIPTION",
            "TYPE", "UNITS", "COST", "ASSET_CATEGORY", "ASSET_CATEGORY_ID",
            "SUPPLIER_NAME", "SUPPLIER_NUMBER", "SUPPLIER_ID",
            "SUPPLIER_SITE", "SUPPLIER_SITE_ID", "PO_NUMBER", "PO_HEADER_ID",
            "PO_LINE_ID", "PO_LINE_NO", "PROJECT_NAME", "PROJECT_ID",
            "TASK_NAME", "TASK_ID", "SOURCE", "INVOICE_DATE", "INV_CURRENCY",
            "ASSET_TYPE", "ASSET_BOOK", "ASSET_BOOK_ID", "DATE_IN_SERVICE",
            "DEPRECIATE_METHOD", "LIFE_YEARS", "MONTHS", "CURRENCY_ID",
            "INVOICE_ID", "INVOICE_TYPE", "INVOICE_LINE_ID", "LOCATION",
            "LOCATION_ID", "DISTRIBUTION_ID", "GL_DATE", "SALVAGE_PERCENTAGE",
            "PARENT_LINE_ID", "MASS_ADDITION_ID"
       FROM (SELECT h.operating_unit, h.ou_id, h.invoice_no invoice_number,
                    h.voucher_number, d.line_no distribution_line,
                    l.line_no invoice_line,
                    DECODE (b.mass_addition_id,
                            '', DECODE (l.TYPE, 'Tax', 'Merge', 'Open'),
                            'Created'
                           ) status,
                    l.description description, l.TYPE,
                    DECODE (l.TYPE,
                            'Item', DECODE (l.unit_price,
                                            '', 1,
                                            l.amount / l.unit_price
                                           ),
                            ''
                           ) units,
                    d.amount COST, d.asset_category, d.asset_category_id,
                    h.supplier_name supplier_name,
                    h.supplier_no supplier_number, h.supplier_id,
                    h.supplier_site, h.supplier_site_id, l.po_no po_number,
                    l.po_header_id, l.po_line_id, l.po_line_no,
                    l.project_name, l.project_id, l.task_name, l.task_id,
                    DECODE (l.project_id, '', 'Payable', 'Projects') SOURCE,
                    h.invoice_date invoice_date, h.inv_currency,
                    'CAPITALIZED' asset_type, d.asset_book, d.asset_book_id,
                    h.invoice_date date_in_service, a.depreciate_method,
                    a.life_years, a.months, h.currency_id, h.invoice_id,
                    h.invoice_type, l.invoice_line_id, l.LOCATION,
                    l.location_id, d.distribution_id, d.gl_date,
                    a.salvage_value salvage_percentage, l.parent_line_id,
                    b.mass_addition_id
               FROM dv_ap_invoices h,
                    dv_ap_invoice_lines l,
                    dv_ap_invoice_distributions d,
                    dv_asset_category a,
                    dv_asset_mass_add_source_lines b
              WHERE h.invoice_id = l.invoice_id
                AND l.invoice_line_id = d.invoice_line_id
                AND NVL (d.track_as_asset, 'N') = 'Y'
                AND d.asset_book IS NOT NULL
                AND l.TYPE <> 'Tax'
                AND NVL (h.accounted, 'N') = 'Accounted'
                AND h.amount_paid = h.amount
                AND a.category_id(+) = d.asset_category_id
                AND b.inv_dist_id(+) = d.distribution_id
             UNION
             SELECT h.operating_unit, h.ou_id, h.invoice_no invoice_number,
                    h.voucher_number, d.line_no distribution_line,
                    l.line_no invoice_line,
                    DECODE (b.mass_addition_id,
                            '', DECODE (l.TYPE, 'Tax', 'Merge', 'Open'),
                            'Created'
                           ) status,
                    l.description description, l.TYPE,
                    DECODE (l.TYPE,
                            'Item', DECODE (l.unit_price,
                                            '', 1,
                                            l.amount / l.unit_price
                                           ),
                            ''
                           ) units,
                    d.amount COST, d.asset_category, d.asset_category_id,
                    h.supplier_name supplier_name,
                    h.supplier_no supplier_number, h.supplier_id,
                    h.supplier_site, h.supplier_site_id, l.po_no po_number,
                    l.po_header_id, l.po_line_id, l.po_line_no,
                    l.project_name, l.project_id, l.task_name, l.task_id,
                    DECODE (l.project_id, '', 'Payable', 'Projects') SOURCE,
                    h.invoice_date invoice_date, h.inv_currency,
                    'CAPITALIZED' asset_type, d.asset_book, d.asset_book_id,
                    h.invoice_date date_in_service, a.depreciate_method,
                    a.life_years, a.months, h.currency_id, h.invoice_id,
                    h.invoice_type, l.invoice_line_id, l.LOCATION,
                    l.location_id, d.distribution_id, d.gl_date,
                    a.salvage_value salvage_percentage, l.parent_line_id,
                    b.mass_addition_id
               FROM dv_ap_invoices h,
                    dv_ap_invoice_lines l,
                    dv_ap_invoice_distributions d,
                    dv_asset_category a,
                    dv_asset_mass_add_source_lines b,
                    dv_inv_lines_tax c
              WHERE h.invoice_id = l.invoice_id
                AND l.invoice_line_id = d.invoice_line_id
                AND l.TYPE = 'Tax'
                AND NVL (h.accounted, 'N') = 'Accounted'
                AND h.amount_paid = h.amount
                AND c.tax_line_id = l.tax_line_id
                AND NVL (c.RECOVERABLE, 'N') = 'N'
                AND a.category_id(+) = d.asset_category_id
                AND b.inv_dist_id(+) = d.distribution_id
                AND l.parent_line_id IN (SELECT DISTINCT invoice_line_id
                                                    FROM dv_ap_invoice_distributions
                                                   WHERE asset_book_id IS NOT NULL))
   ORDER BY invoice_id DESC, invoice_line_id;