99 lines
4.7 KiB
SQL
99 lines
4.7 KiB
SQL
CREATE OR REPLACE VIEW public.view_forex_blocked AS
|
|
SELECT forex.number AS forex_number,
|
|
'forex'::text AS line_type,
|
|
NULL::character varying AS purchase_number,
|
|
NULL::text AS purchase_line,
|
|
NULL::text AS qt_type,
|
|
max(forex.rate) AS rate,
|
|
NULL::numeric AS amount_covered,
|
|
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
|
|
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)))
|
|
GROUP BY forex.id, forex.number, forex.buy_amount
|
|
UNION ALL
|
|
SELECT forex.number AS forex_number,
|
|
'contract'::text AS line_type,
|
|
p.number AS purchase_number,
|
|
((((pp.code)::text || ' ('::text) || pl.quantity) || ')'::text) AS purchase_line,
|
|
'open'::text AS qt_type,
|
|
NULL::numeric AS rate,
|
|
fcpc.amount AS amount_covered,
|
|
NULL::numeric AS total_amount_forex,
|
|
NULL::numeric AS amount_remaining,
|
|
cc.code AS currency,
|
|
NULL::date AS maturity_date
|
|
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)))
|
|
WHERE ((ll.lot_type)::text = 'virtual'::text)
|
|
UNION ALL
|
|
SELECT forex.number AS forex_number,
|
|
'contract'::text AS line_type,
|
|
p.number AS purchase_number,
|
|
((((pp.code)::text || ' ('::text) || pl.quantity) || ')'::text) AS purchase_line,
|
|
'shipped'::text AS qt_type,
|
|
NULL::numeric AS rate,
|
|
fcpc.amount AS amount_covered,
|
|
NULL::numeric AS total_amount_forex,
|
|
NULL::numeric AS amount_remaining,
|
|
cc.code AS currency,
|
|
NULL::date AS maturity_date
|
|
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)))
|
|
WHERE (((ll.lot_type)::text = 'physic'::text) AND (ll.invoice_line_prov IS NULL) AND (ll.invoice_line IS NULL))
|
|
UNION ALL
|
|
SELECT forex.number AS forex_number,
|
|
'contract'::text AS line_type,
|
|
p.number AS purchase_number,
|
|
((((pp.code)::text || ' ('::text) || pl.quantity) || ')'::text) AS purchase_line,
|
|
'invoiced'::text AS qt_type,
|
|
NULL::numeric AS rate,
|
|
fcpc.amount AS amount_covered,
|
|
NULL::numeric AS total_amount_forex,
|
|
NULL::numeric AS amount_remaining,
|
|
cc.code AS currency,
|
|
NULL::date AS maturity_date
|
|
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)))
|
|
WHERE (((ll.lot_type)::text = 'physic'::text) AND ((ll.invoice_line_prov > 0) OR (ll.invoice_line > 0)))
|
|
UNION ALL
|
|
SELECT forex.number AS forex_number,
|
|
'contract'::text AS line_type,
|
|
p.number AS purchase_number,
|
|
((((pp.code)::text || ' ('::text) || pl.quantity) || ')'::text) AS purchase_line,
|
|
'paid'::text AS qt_type,
|
|
NULL::numeric AS rate,
|
|
fcpc.amount AS amount_covered,
|
|
NULL::numeric AS total_amount_forex,
|
|
NULL::numeric AS amount_remaining,
|
|
cc.code AS currency,
|
|
NULL::date AS maturity_date
|
|
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 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 (((ll.lot_type)::text = 'physic'::text) AND ((ll.invoice_line_prov > 0) OR (ll.invoice_line > 0)) AND (aml.reconciliation > 0))
|
|
ORDER BY 1, 2 DESC;;
|