CREATE OR REPLACE VIEW public.vw_fx_by_maturity_by_contract AS
 WITH combined AS (
         SELECT p.number AS purchase_number,
            'contract'::text AS line_type,
            NULL::character varying AS forex_number,
            NULL::numeric AS amount_covered,
            string_agg(DISTINCT (pp.code)::text, ', '::text) AS products,
            max((pl.quantity * (pl.unit_price)::double precision)) AS total_contract_value,
            (max((pl.quantity * (pl.unit_price)::double precision)) - COALESCE((sum(fcpc.amount))::double precision, (0)::double precision)) AS amount_remaining,
            max((cc.code)::text) AS currency,
            NULL::numeric AS rate,
            COALESCE(max((pe.estimated_date + ptld.days)), max(p.purchase_date)) AS maturity_date,
            NULL::text AS status
           FROM ((((((((purchase_purchase p
             LEFT JOIN forex_cover_physical_contract fcpc 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 account_invoice_payment_term pt ON ((pt.id = p.payment_term)))
             LEFT JOIN account_invoice_payment_term_line ptl ON ((ptl.payment = pt.id)))
             LEFT JOIN account_invoice_payment_term_line_delta ptld ON ((ptld.line = ptl.id)))
             LEFT JOIN pricing_estimated pe ON (((pe.purchase = p.id) AND ((pe.trigger)::text = (ptl.trigger_event)::text))))
          WHERE (p.currency = 2)
          GROUP BY p.number
        UNION ALL
         SELECT p.number AS purchase_number,
            'forex'::text AS line_type,
            forex.number AS forex_number,
            fcpc.amount AS amount_covered,
            NULL::text AS products,
            NULL::numeric AS total_contract_value,
            NULL::numeric AS amount_remaining,
            cc.code AS currency,
            forex.rate,
            forex.value_date AS maturity_date,
                CASE
                    WHEN (forex.move > 0) THEN 'executed'::text
                    ELSE 'not executed'::text
                END AS status
           FROM (((purchase_purchase p
             JOIN forex_cover_physical_contract fcpc ON ((fcpc.contract = p.id)))
             JOIN forex_forex forex ON ((forex.id = fcpc.forex)))
             LEFT JOIN currency_currency cc ON ((forex.buy_currency = cc.id)))
          WHERE (forex.buy_currency = 2)
        )
 SELECT to_char((max(maturity_date))::timestamp with time zone, 'MONYY'::text) AS maturity_month,
    max(maturity_date) AS maturity_date,
    line_type,
    purchase_number,
    forex_number,
    max(status) AS status,
    max(products) AS products,
    COALESCE(sum(amount_covered), (0)::numeric) AS amount_covered,
    COALESCE(sum(total_contract_value), (0)::double precision) AS total_contract_value,
    COALESCE(sum(amount_remaining), (0)::double precision) AS amount_remaining,
    ((COALESCE(sum(amount_covered), (0)::numeric))::double precision / COALESCE(sum(total_contract_value), (1)::double precision)) AS percent_covered,
    max(currency) AS currency,
    max(rate) AS rate
   FROM combined
  GROUP BY line_type, purchase_number, forex_number
  ORDER BY (max(maturity_date)), purchase_number, line_type, forex_number;;
