CREATE OR REPLACE VIEW public.view_forex_summary AS
 SELECT forex.number AS forex_number,
    fcpc.id AS coverage_id,
    p.number AS purchase_number,
    pp.code AS product_code,
    pl.quantity AS purchase_quantity,
    fcpc.amount AS amount_covered,
    forex.for_amount AS forex_amount,
    sum(fcpc.amount) OVER (PARTITION BY forex.id ORDER BY fcpc.id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_covered,
    (forex.for_amount - sum(fcpc.amount) OVER (PARTITION BY forex.id ORDER BY fcpc.id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS balance
   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)));;
