CREATE OR REPLACE VIEW public.view_by_forex AS
 WITH contract_lines AS (
         SELECT forex.id AS forex_id,
            forex.number AS forex_number,
            fcpc.id AS fcpc_id,
            fcpc.amount AS forex_amount,
            fcpc.amount AS forex_amount_for_all,
            ((fcpc.amount)::double precision * (((lh.quantity)::double precision * (unit.factor / unit_line.factor)) / pl.quantity)) AS line_amount,
            ((pl.unit_price)::double precision * ((lh.quantity)::double precision * (unit.factor / unit_line.factor))) AS qt_amount,
            p.id AS purchase_id,
            p.number AS purchase_number,
            pl.id AS purchase_line_id,
            pl.quantity,
            pp.code AS product_code,
            cc.code AS currency,
            ll.lot_type,
            ll.invoice_line,
            ll.invoice_line_prov,
            aml.reconciliation
           FROM (((((((((((forex_forex forex
             LEFT JOIN forex_cover_physical_contract fcpc ON ((forex.id = fcpc.forex)))
             LEFT JOIN purchase_purchase p ON ((fcpc.contract = p.id)))
             LEFT JOIN purchase_line pl ON ((pl.purchase = p.id)))
             LEFT JOIN product_product pp ON ((pl.product = pp.id)))
             LEFT JOIN currency_currency cc ON ((p.currency = cc.id)))
             LEFT JOIN lot_lot ll ON ((pl.id = ll.line)))
             LEFT JOIN lot_qt_hist lh ON ((lh.lot = ll.id)))
             LEFT JOIN lot_qt_type lt ON (((lh.quantity_type = lt.id) AND (lt.sequence = 1))))
             LEFT JOIN product_uom unit ON ((unit.id = pl.unit)))
             LEFT JOIN product_uom unit_line ON ((unit_line.id = ll.lot_unit_line)))
             LEFT JOIN account_move_line aml ON (((split_part((aml.origin)::text, ','::text, 1) = 'invoice.line'::text) AND ((ll.invoice_line_prov = (split_part((aml.origin)::text, ','::text, 2))::integer) OR (ll.invoice_line = (split_part((aml.origin)::text, ','::text, 2))::integer)))))
          WHERE ((forex.move IS NULL) AND (forex.buy_currency = 2))
        ), contracts_by_status AS (
         SELECT contract_lines.forex_id,
            contract_lines.forex_number,
            contract_lines.fcpc_id,
            contract_lines.line_amount,
            contract_lines.forex_amount,
            contract_lines.forex_amount_for_all,
            contract_lines.qt_amount,
            contract_lines.purchase_id,
            contract_lines.purchase_number,
            contract_lines.purchase_line_id,
            contract_lines.quantity,
            contract_lines.product_code,
            contract_lines.currency,
            contract_lines.lot_type,
            contract_lines.invoice_line,
            contract_lines.invoice_line_prov,
            contract_lines.reconciliation,
                CASE
                    WHEN ((contract_lines.lot_type)::text = 'virtual'::text) THEN 'open'::text
                    WHEN (((contract_lines.lot_type)::text = 'physic'::text) AND (contract_lines.invoice_line IS NULL) AND (contract_lines.invoice_line_prov IS NULL)) THEN 'shipped'::text
                    WHEN (((contract_lines.lot_type)::text = 'physic'::text) AND ((contract_lines.invoice_line IS NOT NULL) OR (contract_lines.invoice_line_prov IS NOT NULL)) AND ((contract_lines.reconciliation IS NULL) OR (contract_lines.reconciliation = 0))) THEN 'invoiced'::text
                    WHEN (((contract_lines.lot_type)::text = 'physic'::text) AND ((contract_lines.invoice_line IS NOT NULL) OR (contract_lines.invoice_line_prov IS NOT NULL)) AND (contract_lines.reconciliation > 0)) THEN 'paid'::text
                    ELSE 'total'::text
                END AS qt_type,
                CASE
                    WHEN ((contract_lines.lot_type)::text = 'virtual'::text) THEN 1
                    WHEN (((contract_lines.lot_type)::text = 'physic'::text) AND (contract_lines.invoice_line IS NULL) AND (contract_lines.invoice_line_prov IS NULL)) THEN 2
                    WHEN (((contract_lines.lot_type)::text = 'physic'::text) AND ((contract_lines.invoice_line IS NOT NULL) OR (contract_lines.invoice_line_prov IS NOT NULL)) AND ((contract_lines.reconciliation IS NULL) OR (contract_lines.reconciliation = 0))) THEN 3
                    WHEN (((contract_lines.lot_type)::text = 'physic'::text) AND ((contract_lines.invoice_line IS NOT NULL) OR (contract_lines.invoice_line_prov IS NOT NULL)) AND (contract_lines.reconciliation > 0)) THEN 4
                    ELSE 0
                END AS qt_order
           FROM contract_lines
        ), amounts_adjusted AS (
         SELECT c.forex_id,
            c.fcpc_id,
            c.purchase_id,
            c.purchase_number,
            c.purchase_line_id,
            c.product_code,
            c.currency,
            c.qt_type,
            c.qt_order,
            c.line_amount,
            c.qt_amount,
            NULL::numeric AS forex_amount,
            GREATEST(LEAST(COALESCE(c.line_amount, (0)::double precision), ((COALESCE(c.forex_amount_for_all, (0)::numeric))::double precision - COALESCE(sum(COALESCE(c.line_amount, (0)::double precision)) OVER (PARTITION BY c.forex_id, c.fcpc_id ORDER BY c.qt_order DESC, c.purchase_line_id, c.invoice_line ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), (0)::double precision))), (0)::double precision) AS amount_covered
           FROM contracts_by_status c
          WHERE (c.qt_type = ANY (ARRAY['paid'::text, 'invoiced'::text, 'shipped'::text, 'open'::text]))
        ), amounts_total AS (
         SELECT a.forex_id,
            a.fcpc_id,
            a.purchase_id,
            a.purchase_number,
            NULL::integer AS purchase_line_id,
            NULL::text AS product_code,
            max((a.currency)::text) AS currency,
            'total'::text AS qt_type,
            5 AS qt_order,
            NULL::numeric AS line_amount,
            NULL::numeric AS qt_amount,
            max(fcpc.amount) AS forex_amount,
            NULL::numeric AS amount_covered
           FROM (((forex_cover_physical_contract fcpc
             JOIN purchase_purchase p ON ((fcpc.contract = p.id)))
             JOIN forex_forex f ON ((f.id = fcpc.forex)))
             JOIN amounts_adjusted a ON (((a.fcpc_id = fcpc.id) AND (a.purchase_id = p.id))))
          GROUP BY a.forex_id, a.fcpc_id, a.purchase_id, a.purchase_number
        ), forex_summary AS (
         SELECT forex.number AS forex_number,
            'forex'::text AS line_type,
            NULL::text AS purchase_number,
            NULL::text AS purchase_line,
            NULL::text AS qt_type,
            0 AS qt_order,
            max(forex.rate) AS rate,
            NULL::numeric AS amount_covered,
            NULL::numeric AS forex_amount,
            NULL::numeric AS qt_amount,
            forex.buy_amount AS total_amount_forex,
            (forex.buy_amount - COALESCE(sum(fcpc.amount), (0)::numeric)) AS amount_remaining,
            max((cc.code)::text) AS currency,
            forex.value_date AS maturity_date,
            forex.move
           FROM ((forex_forex forex
             LEFT JOIN forex_cover_physical_contract fcpc ON ((forex.id = fcpc.forex)))
             LEFT JOIN currency_currency cc ON ((forex.buy_currency = cc.id)))
          WHERE ((forex.move IS NULL) AND (forex.buy_currency = 2))
          GROUP BY forex.id, forex.number, forex.buy_amount
        )
 SELECT forex_summary.forex_number,
    forex_summary.line_type,
    forex_summary.purchase_number,
    forex_summary.purchase_line,
    forex_summary.qt_type,
    forex_summary.qt_order,
    forex_summary.rate,
    forex_summary.amount_covered,
    forex_summary.forex_amount,
    forex_summary.qt_amount,
    forex_summary.total_amount_forex,
    forex_summary.amount_remaining,
    forex_summary.currency,
    forex_summary.maturity_date,
        CASE
            WHEN (forex_summary.move > 0) THEN 'executed'::text
            ELSE 'not executed'::text
        END AS executed
   FROM forex_summary
UNION ALL
 SELECT c.number AS forex_number,
    'contract'::text AS line_type,
    a.purchase_number,
    ((a.product_code)::text || COALESCE(((' ('::text || a.purchase_line_id) || ')'::text), ''::text)) AS purchase_line,
    a.qt_type,
    a.qt_order,
    NULL::numeric AS rate,
    a.amount_covered,
    a.forex_amount,
    a.qt_amount,
    NULL::numeric AS total_amount_forex,
    NULL::numeric AS amount_remaining,
    a.currency,
    NULL::date AS maturity_date,
    NULL::text AS executed
   FROM (( SELECT amounts_adjusted.forex_id,
            amounts_adjusted.fcpc_id,
            amounts_adjusted.purchase_id,
            amounts_adjusted.purchase_number,
            amounts_adjusted.purchase_line_id,
            amounts_adjusted.product_code,
            amounts_adjusted.currency,
            amounts_adjusted.qt_type,
            amounts_adjusted.qt_order,
            amounts_adjusted.line_amount,
            amounts_adjusted.qt_amount,
            amounts_adjusted.forex_amount,
            amounts_adjusted.amount_covered
           FROM amounts_adjusted
        UNION ALL
         SELECT amounts_total.forex_id,
            amounts_total.fcpc_id,
            amounts_total.purchase_id,
            amounts_total.purchase_number,
            amounts_total.purchase_line_id,
            amounts_total.product_code,
            amounts_total.currency,
            amounts_total.qt_type,
            amounts_total.qt_order,
            amounts_total.line_amount,
            amounts_total.qt_amount,
            amounts_total.forex_amount,
            amounts_total.amount_covered
           FROM amounts_total) a
     JOIN forex_forex c ON ((c.id = a.forex_id)))
  ORDER BY 1, 2 DESC, 3, 6 DESC;;
