Commit all views

This commit is contained in:
AzureAD\SylvainDUVERNAY
2026-05-11 14:01:13 +02:00
parent 3986882771
commit 3a3e715336
28 changed files with 5546 additions and 111 deletions

View File

@@ -6,7 +6,7 @@ CREATE OR REPLACE VIEW public.vw_bi_dim_price_curve AS
"strUnit" AS "Unit",
"strPriceCurveType" AS "Type",
"strPriceArea" AS "Area",
"strPriceCalendar" AS "Calendard",
"strPriceCalendar" AS "Calendar",
"strPricingType" AS "Pricing Type",
"dtmPricingStartDate" AS "Pricing Start Date",
"dtmPricingEndDate" AS "Pricing End Date",

View File

@@ -4,5 +4,7 @@ CREATE OR REPLACE VIEW public.vw_bi_fct_prices AS
"dblHighPrice" AS "High",
"dblLowPrice" AS "Low",
"dblOpenPrice" AS "Open",
"dblPriceValue" AS "Price Value"
"dblPriceValue" AS "Price Value",
"dtmMaxPriceDate" AS "Last Price Date",
"ysnIsLatestPrice" AS "Is Latest Price"
FROM vw_utility_price_curve_prices;;

View File

@@ -15,8 +15,8 @@ CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_contract_fees AS
"strUnit" AS "Unit",
(("dblQuantity" * "dblPrice") * ("dblSignMultiplier")::numeric) AS "Amount",
CASE
WHEN (upper(("strProduct")::text) = 'MARITIME FREIGHT'::text) THEN "strProduct"
WHEN (upper(("strProduct")::text) = 'PROFIT SHARING'::text) THEN "strProduct"
WHEN (upper(("strProduct")::text) = 'MARITIME FREIGHT'::text) THEN 'Freight'::character varying
WHEN (upper(("strProduct")::text) = 'PROFIT SHARING'::text) THEN 'Profit Sharing'::character varying
ELSE 'Other Costs'::character varying
END AS "Cost Group"
FROM vw_utility_contract_fees;;

View File

@@ -17,7 +17,8 @@ CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_full_pnl AS
contracts."Open Quantity Signed",
contracts."Price",
contracts."Open Cost Amount" AS amount,
contracts."Estimated BL Date" AS "Delivery Date"
contracts."Estimated BL Date" AS "Delivery Date",
contracts."Line Group"
FROM (vw_bi_itsa_physical_open_costs contracts
JOIN trade trade ON ((contracts."intTradeLineId" = trade."intTradeId")))
UNION ALL
@@ -28,7 +29,8 @@ UNION ALL
shipment."Shipped Quantity" AS "Open Quantity Signed",
shipment."Purchase Price" AS "Price",
(shipment."Shipped Quantity" * (shipment."Purchase Price")::double precision) AS amount,
shipment."BL Date" AS "Delivery Date"
shipment."BL Date" AS "Delivery Date",
'Purchase'::text AS "Line Group"
FROM vw_bi_itsa_fct_shipments shipment
UNION ALL
SELECT 3 AS "intPnlTypeId",
@@ -38,7 +40,8 @@ UNION ALL
shipment."Shipped Quantity" AS "Open Quantity Signed",
shipment."Sale Price" AS "Price",
(shipment."Shipped Quantity" * (shipment."Sale Price")::double precision) AS amount,
shipment."BL Date" AS "Delivery Date"
shipment."BL Date" AS "Delivery Date",
'Sale'::text AS "Line Group"
FROM vw_bi_itsa_fct_shipments shipment
UNION ALL
SELECT 3 AS "intPnlTypeId",
@@ -48,9 +51,21 @@ UNION ALL
shipment."Shipped Quantity" AS "Open Quantity Signed",
fees."Price",
(shipment."Shipped Quantity" * (fees."Price")::double precision) AS amount,
shipment."BL Date" AS "Delivery Date"
shipment."BL Date" AS "Delivery Date",
fees."Cost Group" AS "Line Group"
FROM (vw_bi_itsa_fct_shipment_fees fees
JOIN vw_bi_itsa_fct_shipments shipment ON ((fees."intShipmentId" = shipment."intShipmentId")))
UNION ALL
SELECT fees."intPnlTypeId",
fees."strConformedReferenceId",
fees."intPriceGroupId",
fees."Line Description",
fees."Open Quantity Signed",
fees."Price",
fees."Amount" AS amount,
fees."Delivery Date",
fees."Line Group"
FROM vw_bi_itsa_fct_inherited_expenses_on_shipments fees
UNION ALL
SELECT 1 AS "intPnlTypeId",
trade."strTradeId" AS "strConformedReferenceId",
@@ -59,7 +74,8 @@ UNION ALL
ctr."Contract Quantity" AS "Open Quantity Signed",
ctr."Avg Price" AS "Price",
(ctr."Contract Quantity" * ctr."Avg Price") AS amount,
ctr."Delivery Date"
ctr."Delivery Date",
'Expected'::text AS "Line Group"
FROM (vw_bi_itsa_fct_contract ctr
JOIN vw_bi_dim_trade trade ON ((ctr."TradeLineId" = trade."intTradeId")))
WHERE ((trade."Book")::text ~~ '%Projection%'::text);;

View File

@@ -0,0 +1,60 @@
CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_inherited_expenses_on_shipments AS
WITH all_fees AS (
SELECT 3 AS "intPnlTypeId",
((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId",
2 AS "intPriceGroupId",
ctr_fees."Fee" AS "Line Description",
shipment."Shipped Quantity" AS "Open Quantity Signed",
ctr_fees."Price",
(shipment."Shipped Quantity" * (ctr_fees."Price")::double precision) AS "Amount",
shipment."BL Date" AS "Delivery Date",
ctr_fees."Cost Group" AS "Line Group"
FROM (vw_bi_itsa_fct_shipments shipment
JOIN vw_bi_itsa_fct_contract_fees ctr_fees ON ((ctr_fees."intContractLineId" = shipment."intPurchaseLineId")))
UNION ALL
SELECT 3 AS "intPnlTypeId",
((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId",
2 AS "intPriceGroupId",
fees."Fee" AS "Line Description",
shipment."Shipped Quantity" AS "Open Quantity Signed",
fees."Price",
(shipment."Shipped Quantity" * (fees."Price")::double precision) AS "Amount",
shipment."BL Date" AS "Delivery Date",
fees."Cost Group" AS "Line Group"
FROM (vw_bi_itsa_fct_shipment_fees fees
JOIN vw_bi_itsa_fct_shipments shipment ON ((fees."intShipmentId" = shipment."intShipmentId")))
)
SELECT all_fees."intPnlTypeId",
all_fees."strConformedReferenceId",
all_fees."intPriceGroupId",
all_fees."Line Description",
all_fees."Open Quantity Signed",
all_fees."Price",
all_fees."Amount",
all_fees."Delivery Date",
all_fees."Line Group"
FROM all_fees
WHERE ((all_fees."Line Description")::text <> 'Profit sharing'::text)
UNION ALL
SELECT ranked."intPnlTypeId",
ranked."strConformedReferenceId",
ranked."intPriceGroupId",
ranked."Line Description",
ranked."Open Quantity Signed",
ranked."Price",
ranked."Amount",
ranked."Delivery Date",
ranked."Line Group"
FROM ( SELECT all_fees."intPnlTypeId",
all_fees."strConformedReferenceId",
all_fees."intPriceGroupId",
all_fees."Line Description",
all_fees."Open Quantity Signed",
all_fees."Price",
all_fees."Amount",
all_fees."Delivery Date",
all_fees."Line Group",
row_number() OVER (PARTITION BY all_fees."strConformedReferenceId" ORDER BY all_fees."Price" DESC) AS rn
FROM all_fees
WHERE ((all_fees."Line Description")::text = 'Profit sharing'::text)) ranked
WHERE (ranked.rn = 1);;

View File

@@ -22,8 +22,8 @@ CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_shipment_fees AS
ELSE ("dblPrice" * ("dblSignMultiplier")::numeric)
END) AS "Amount",
CASE
WHEN (upper(("strProduct")::text) = 'MARITIME FREIGHT'::text) THEN "strProduct"
WHEN (upper(("strProduct")::text) = 'PROFIT SHARING'::text) THEN "strProduct"
WHEN (upper(("strProduct")::text) = 'MARITIME FREIGHT'::text) THEN 'Freight'::character varying
WHEN (upper(("strProduct")::text) = 'PROFIT SHARING'::text) THEN 'Profit Sharing'::character varying
ELSE 'Other Costs'::character varying
END AS "Cost Group"
FROM vw_utility_shipment_fees;;

View File

@@ -0,0 +1,20 @@
CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_shipments_v2 AS
SELECT q.valued_lot_id AS "intLotId",
q.product_id AS "intProductId",
q.from_location_id AS "intFromLocationId",
q.to_location_id AS "intToLocationId",
q.shipment_id AS "intShipmentId",
q.purchase_line_id AS "intPurchaseLineId",
q.sale_line_id AS "intSaleLineId",
q.pnl_quantity_after_finished_rule AS "Shipped Quantity",
q.quantity_unit AS "Quantity Unit",
COALESCE(p."dblUnitPrice", (0)::numeric) AS "Purchase Price",
COALESCE(s."dblUnitPrice", (0)::numeric) AS "Sale Price",
q.bl_date AS "BL Date",
q.delivery_date_from AS "Delivery Date From",
q.delivery_date_to AS "Delivery Date To",
q.valued_lot_type AS "Lot Type"
FROM ((vw_utility_trade_pnl_quantities q
LEFT JOIN vw_utility_purchase_physical_contract p ON ((q.purchase_line_id = p."intPurchaseLineId")))
LEFT JOIN vw_utility_sale_physical_contract s ON ((q.sale_line_id = s."intSaleLineId")))
WHERE ((q.ignored_by_finished_rule = false) AND (q.shipment_id IS NOT NULL));;

View File

@@ -58,7 +58,8 @@ CREATE OR REPLACE VIEW public.vw_bi_itsa_physical_open_costs AS
((opq."OpenQuantitySigned" * pur."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
1 AS "intPriceGroupId",
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed"
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
'Price'::text AS "Line Group"
FROM ((vw_utility_dim_physical_purchase_contract ctr
JOIN vw_utility_purchase_physical_contract pur ON ((ctr."intPurchaseLineId" = pur."intPurchaseLineId")))
JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
@@ -73,7 +74,8 @@ UNION ALL
(opq."OpenQuantitySigned" * opq."AvgMtmPrice") AS "Open Cost Amount",
3 AS "intPriceGroupId",
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed"
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
'MTM'::text AS "Line Group"
FROM ((vw_utility_physical_mtm_definition mtm
JOIN vw_utility_dim_physical_purchase_contract ctr ON ((mtm."intContractLineId" = ctr."intPurchaseLineId")))
JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
@@ -92,7 +94,8 @@ UNION ALL
END)::numeric * opq."OpenQuantity") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
2 AS "intPriceGroupId",
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed"
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
fees."strProductGroup" AS "Line Group"
FROM ((vw_utility_contract_fees fees
JOIN vw_utility_dim_physical_purchase_contract ctr ON ((fees."intContractLineId" = ctr."intPurchaseLineId")))
JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
@@ -107,7 +110,8 @@ UNION ALL
((opq."OpenQuantitySigned" * sale."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
1 AS "intPriceGroupId",
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed"
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
'Price'::text AS "Line Group"
FROM ((vw_utility_dim_physical_sale_contract ctr
JOIN vw_utility_sale_physical_contract sale ON ((ctr."intSaleLineId" = sale."intSaleLineId")))
JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
@@ -122,7 +126,8 @@ UNION ALL
(opq."OpenQuantitySigned" * opq."AvgMtmPrice") AS "Open Cost Amount",
3 AS "intPriceGroupId",
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed"
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
'MTM'::text AS "Line Group"
FROM ((vw_utility_physical_mtm_definition mtm
JOIN vw_utility_dim_physical_sale_contract ctr ON ((mtm."intContractLineId" = ctr."intSaleLineId")))
JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
@@ -141,7 +146,8 @@ UNION ALL
END)::numeric * opq."OpenQuantity") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
2 AS "intPriceGroupId",
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed"
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
fees."strProductGroup" AS "Line Group"
FROM ((vw_utility_contract_fees fees
JOIN vw_utility_dim_physical_sale_contract ctr ON ((fees."intContractLineId" = ctr."intSaleLineId")))
JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))

View File

@@ -19,7 +19,12 @@ CREATE OR REPLACE VIEW public.vw_utility_contract_fees AS
CASE
WHEN (upper((f.p_r)::text) = 'REC'::text) THEN '-1'::integer
ELSE 1
END AS "dblSignMultiplier"
END AS "dblSignMultiplier",
CASE
WHEN (upper((p.code)::text) ~~ '%FREIGHT%'::text) THEN 'Freight'::text
WHEN (upper((p.code)::text) ~~ '%PROFIT SHARING%'::text) THEN 'Profit Sharing'::text
ELSE 'Other Costs'::text
END AS "strProductGroup"
FROM ((((fee_fee f
JOIN currency_currency c ON ((f.currency = c.id)))
JOIN product_product p ON ((f.product = p.id)))
@@ -47,7 +52,12 @@ UNION ALL
CASE
WHEN (upper((f.p_r)::text) = 'REC'::text) THEN 1
ELSE '-1'::integer
END AS "dblSignMultiplier"
END AS "dblSignMultiplier",
CASE
WHEN (upper((p.code)::text) ~~ '%FREIGHT%'::text) THEN 'Freight'::text
WHEN (upper((p.code)::text) ~~ '%PROFIT SHARING%'::text) THEN 'Profit Sharing'::text
ELSE 'Other Costs'::text
END AS "strProductGroup"
FROM ((((fee_fee f
JOIN currency_currency c ON ((f.currency = c.id)))
JOIN product_product p ON ((f.product = p.id)))

View File

@@ -1,10 +1,23 @@
CREATE OR REPLACE VIEW public.vw_utility_price_curve_prices AS
WITH lastprice AS (
SELECT ppv_1.price,
max(ppv_1.price_date) AS max_price_date
FROM price_price_value ppv_1
WHERE (ppv_1.active = true)
GROUP BY ppv_1.price
)
SELECT pp.id AS "intPriceCurveId",
ppv.price_date AS "dtmPriceDate",
(ppv.high_price)::numeric(18,6) AS "dblHighPrice",
(ppv.low_price)::numeric(18,6) AS "dblLowPrice",
(ppv.open_price)::numeric(18,6) AS "dblOpenPrice",
(ppv.price_value)::numeric(18,6) AS "dblPriceValue"
FROM (price_price pp
(ppv.price_value)::numeric(18,6) AS "dblPriceValue",
lp.max_price_date AS "dtmMaxPriceDate",
CASE
WHEN (ppv.price_date = lp.max_price_date) THEN true
ELSE false
END AS "ysnIsLatestPrice"
FROM ((price_price pp
LEFT JOIN price_price_value ppv ON (((pp.id = ppv.price) AND (ppv.active = true))))
LEFT JOIN lastprice lp ON ((ppv.price = lp.price)))
WHERE ((1 = 1) AND (pp.active = true));;

View File

@@ -0,0 +1,267 @@
CREATE OR REPLACE VIEW public.vw_utility_shipment_fee_allocation AS
WITH physical_shipment_lots AS (
SELECT l.lot_shipment_in AS shipment_id,
l.id AS lot_id,
l.lot_type,
l.line AS purchase_line_id,
l.sale_line AS sale_line_id,
qh.quantity AS shipment_quantity,
l.lot_unit_line AS shipment_unit_id
FROM (lot_lot l
LEFT JOIN lot_qt_hist qh ON (((qh.lot = l.id) AND (qh.quantity_type = l.lot_state))))
WHERE (((l.lot_type)::text = 'physic'::text) AND (l.lot_shipment_in IS NOT NULL))
), virtual_shipment_lots AS (
SELECT q.lot_shipment_in AS shipment_id,
vp.id AS lot_id,
vp.lot_type,
vp.line AS purchase_line_id,
COALESCE(vp.sale_line, ps.sale_line) AS sale_line_id,
q.lot_quantity AS shipment_quantity,
q.lot_unit AS shipment_unit_id
FROM ((lot_qt q
JOIN lot_lot vp ON (((vp.id = q.lot_p) AND ((vp.lot_type)::text = 'virtual'::text))))
LEFT JOIN lot_lot ps ON ((ps.id = q.lot_s)))
WHERE ((q.lot_shipment_in IS NOT NULL) AND (NOT (EXISTS ( SELECT 1
FROM physical_shipment_lots pl
WHERE ((pl.shipment_id = q.lot_shipment_in) AND (pl.purchase_line_id = vp.line) AND ((pl.sale_line_id = COALESCE(vp.sale_line, ps.sale_line)) OR (COALESCE(vp.sale_line, ps.sale_line) IS NULL)))))))
), shipment_lots AS (
SELECT physical_shipment_lots.shipment_id,
physical_shipment_lots.lot_id,
physical_shipment_lots.lot_type,
physical_shipment_lots.purchase_line_id,
physical_shipment_lots.sale_line_id,
physical_shipment_lots.shipment_quantity,
physical_shipment_lots.shipment_unit_id
FROM physical_shipment_lots
UNION ALL
SELECT virtual_shipment_lots.shipment_id,
virtual_shipment_lots.lot_id,
virtual_shipment_lots.lot_type,
virtual_shipment_lots.purchase_line_id,
virtual_shipment_lots.sale_line_id,
virtual_shipment_lots.shipment_quantity,
virtual_shipment_lots.shipment_unit_id
FROM virtual_shipment_lots
), shipment_context AS (
SELECT shipment_lots.shipment_id,
shipment_lots.purchase_line_id,
shipment_lots.sale_line_id,
shipment_lots.shipment_unit_id,
sum(COALESCE(shipment_lots.shipment_quantity, (0)::numeric)) AS shipment_quantity
FROM shipment_lots
WHERE (shipment_lots.shipment_id IS NOT NULL)
GROUP BY shipment_lots.shipment_id, shipment_lots.purchase_line_id, shipment_lots.sale_line_id, shipment_lots.shipment_unit_id
), shipment_base AS (
SELECT shipment_context.shipment_id,
sum(shipment_context.shipment_quantity) AS shipment_quantity
FROM shipment_context
GROUP BY shipment_context.shipment_id
), shipment_fee_candidates AS (
SELECT f_1.id AS fee_id,
'Shipment'::text AS fee_source,
f_1.type AS fee_type,
CASE
WHEN ((f_1.type)::text = 'ordered'::text) THEN 1
WHEN ((f_1.type)::text = 'scheduled'::text) THEN 2
ELSE NULL::integer
END AS priority,
sb.shipment_id,
NULL::integer AS purchase_line_id,
NULL::integer AS sale_line_id,
f_1.product AS product_id,
f_1.supplier AS supplier_id,
f_1.mode AS packaging,
f_1.p_r AS pay_or_rec,
f_1.state,
f_1.weight_type,
COALESCE(f_1.quantity, (0)::numeric) AS requested_quantity,
sb.shipment_quantity,
f_1.price AS fee_price,
f_1.currency AS currency_id,
f_1.unit AS unit_id,
CASE
WHEN (upper((f_1.p_r)::text) = 'REC'::text) THEN 1
ELSE '-1'::integer
END AS sign_multiplier
FROM (shipment_base sb
JOIN fee_fee f_1 ON ((f_1.shipment_in = sb.shipment_id)))
WHERE ((f_1.type)::text = ANY ((ARRAY['ordered'::character varying, 'scheduled'::character varying])::text[]))
), shipment_fee_allocated AS (
SELECT c.fee_id,
c.fee_source,
c.fee_type,
c.priority,
c.shipment_id,
c.purchase_line_id,
c.sale_line_id,
c.product_id,
c.supplier_id,
c.packaging,
c.pay_or_rec,
c.state,
c.weight_type,
c.requested_quantity,
c.shipment_quantity,
c.fee_price,
c.currency_id,
c.unit_id,
c.sign_multiplier,
LEAST(c.requested_quantity, GREATEST((c.shipment_quantity - COALESCE(sum(c.requested_quantity) OVER (PARTITION BY c.shipment_id, c.product_id, c.supplier_id ORDER BY c.priority, c.fee_id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), (0)::numeric)), (0)::numeric)) AS allocated_quantity
FROM shipment_fee_candidates c
), shipment_fee_coverage AS (
SELECT shipment_fee_allocated.shipment_id,
shipment_fee_allocated.product_id,
shipment_fee_allocated.supplier_id,
max(shipment_fee_allocated.shipment_quantity) AS shipment_quantity,
sum(shipment_fee_allocated.allocated_quantity) AS allocated_quantity
FROM shipment_fee_allocated
GROUP BY shipment_fee_allocated.shipment_id, shipment_fee_allocated.product_id, shipment_fee_allocated.supplier_id
), contract_budgeted_fee_candidates AS (
SELECT f_1.id AS fee_id,
'Purchase Contract'::text AS fee_source,
f_1.type AS fee_type,
3 AS priority,
sc.shipment_id,
sc.purchase_line_id,
NULL::integer AS sale_line_id,
f_1.product AS product_id,
f_1.supplier AS supplier_id,
f_1.mode AS packaging,
f_1.p_r AS pay_or_rec,
f_1.state,
f_1.weight_type,
sc.shipment_quantity,
f_1.price AS fee_price,
f_1.currency AS currency_id,
COALESCE(f_1.unit, sc.shipment_unit_id) AS unit_id,
CASE
WHEN (upper((f_1.p_r)::text) = 'REC'::text) THEN '-1'::integer
ELSE 1
END AS sign_multiplier
FROM (shipment_context sc
JOIN fee_fee f_1 ON ((f_1.line = sc.purchase_line_id)))
WHERE (((f_1.type)::text = 'budgeted'::text) AND (sc.purchase_line_id IS NOT NULL))
UNION ALL
SELECT f_1.id AS fee_id,
'Sale Contract'::text AS fee_source,
f_1.type AS fee_type,
3 AS priority,
sc.shipment_id,
NULL::integer AS purchase_line_id,
sc.sale_line_id,
f_1.product AS product_id,
f_1.supplier AS supplier_id,
f_1.mode AS packaging,
f_1.p_r AS pay_or_rec,
f_1.state,
f_1.weight_type,
sc.shipment_quantity,
f_1.price AS fee_price,
f_1.currency AS currency_id,
COALESCE(f_1.unit, sc.shipment_unit_id) AS unit_id,
CASE
WHEN (upper((f_1.p_r)::text) = 'REC'::text) THEN 1
ELSE '-1'::integer
END AS sign_multiplier
FROM (shipment_context sc
JOIN fee_fee f_1 ON ((f_1.sale_line = sc.sale_line_id)))
WHERE (((f_1.type)::text = 'budgeted'::text) AND (sc.sale_line_id IS NOT NULL))
), contract_budgeted_allocated AS (
SELECT c.fee_id,
c.fee_source,
c.fee_type,
c.priority,
c.shipment_id,
c.purchase_line_id,
c.sale_line_id,
c.product_id,
c.supplier_id,
c.packaging,
c.pay_or_rec,
c.state,
c.weight_type,
c.shipment_quantity,
c.fee_price,
c.currency_id,
c.unit_id,
c.sign_multiplier,
GREATEST((c.shipment_quantity - COALESCE(fc.allocated_quantity, (0)::numeric)), (0)::numeric) AS allocated_quantity
FROM (contract_budgeted_fee_candidates c
LEFT JOIN shipment_fee_coverage fc ON (((fc.shipment_id = c.shipment_id) AND (fc.product_id = c.product_id) AND (fc.supplier_id = c.supplier_id))))
), final_fees AS (
SELECT shipment_fee_allocated.fee_id,
shipment_fee_allocated.fee_source,
shipment_fee_allocated.fee_type,
shipment_fee_allocated.priority,
shipment_fee_allocated.shipment_id,
shipment_fee_allocated.purchase_line_id,
shipment_fee_allocated.sale_line_id,
shipment_fee_allocated.product_id,
shipment_fee_allocated.supplier_id,
shipment_fee_allocated.packaging,
shipment_fee_allocated.pay_or_rec,
shipment_fee_allocated.state,
shipment_fee_allocated.weight_type,
shipment_fee_allocated.allocated_quantity AS fee_quantity,
shipment_fee_allocated.fee_price,
shipment_fee_allocated.currency_id,
shipment_fee_allocated.unit_id,
shipment_fee_allocated.sign_multiplier
FROM shipment_fee_allocated
WHERE (shipment_fee_allocated.allocated_quantity > (0)::numeric)
UNION ALL
SELECT contract_budgeted_allocated.fee_id,
contract_budgeted_allocated.fee_source,
contract_budgeted_allocated.fee_type,
contract_budgeted_allocated.priority,
contract_budgeted_allocated.shipment_id,
contract_budgeted_allocated.purchase_line_id,
contract_budgeted_allocated.sale_line_id,
contract_budgeted_allocated.product_id,
contract_budgeted_allocated.supplier_id,
contract_budgeted_allocated.packaging,
contract_budgeted_allocated.pay_or_rec,
contract_budgeted_allocated.state,
contract_budgeted_allocated.weight_type,
contract_budgeted_allocated.allocated_quantity AS fee_quantity,
contract_budgeted_allocated.fee_price,
contract_budgeted_allocated.currency_id,
contract_budgeted_allocated.unit_id,
contract_budgeted_allocated.sign_multiplier
FROM contract_budgeted_allocated
WHERE (contract_budgeted_allocated.allocated_quantity > (0)::numeric)
)
SELECT f.shipment_id AS "intShipmentId",
f.fee_id AS "intFeeId",
f.fee_source AS "strFeeSource",
f.fee_type AS "strFeeType",
f.priority AS "intPriority",
f.purchase_line_id AS "intPurchaseLineId",
f.sale_line_id AS "intSaleLineId",
f.product_id AS "intProductId",
p.code AS "strFee",
f.supplier_id AS "intSupplierId",
sup.name AS "strSupplier",
f.packaging AS "strPackaging",
f.pay_or_rec AS "strPayOrRec",
f.state AS "strState",
CASE
WHEN (upper((f.weight_type)::text) = 'BRUT'::text) THEN 'Gross'::text
ELSE 'Net'::text
END AS "strWeighingType",
f.fee_quantity AS "dblQuantity",
(f.fee_price * (f.sign_multiplier)::numeric) AS "dblPrice",
cur.name AS "strCurrency",
COALESCE(uom.name, 'Mt'::character varying) AS "strUnit",
((f.fee_quantity * f.fee_price) * (f.sign_multiplier)::numeric) AS "dblAmount",
CASE
WHEN (upper((p.code)::text) ~~ '%FREIGHT%'::text) THEN 'Freight'::text
WHEN (upper((p.code)::text) ~~ '%PROFIT SHARING%'::text) THEN 'ProfitSharing'::text
ELSE 'OtherCosts'::text
END AS "strCostGroup"
FROM ((((final_fees f
JOIN product_product p ON ((p.id = f.product_id)))
JOIN party_party sup ON ((sup.id = f.supplier_id)))
LEFT JOIN currency_currency cur ON ((cur.id = f.currency_id)))
LEFT JOIN product_uom uom ON ((uom.id = f.unit_id)))
ORDER BY f.shipment_id, f.product_id, f.supplier_id, f.priority, f.fee_id;;

View File

@@ -19,7 +19,12 @@ CREATE OR REPLACE VIEW public.vw_utility_shipment_fees AS
CASE
WHEN (upper((f.p_r)::text) = 'REC'::text) THEN 1
ELSE '-1'::integer
END AS "dblSignMultiplier"
END AS "dblSignMultiplier",
CASE
WHEN (upper((p.code)::text) ~~ '%FREIGHT%'::text) THEN 'Freight'::text
WHEN (upper((p.code)::text) ~~ '%PROFIT SHARING%'::text) THEN 'Profit Sharing'::text
ELSE 'Other Costs'::text
END AS "strProductGroup"
FROM ((((fee_fee f
JOIN currency_currency c ON ((f.currency = c.id)))
JOIN product_product p ON ((f.product = p.id)))

View File

@@ -0,0 +1,313 @@
CREATE OR REPLACE VIEW public.vw_utility_trade_pnl_quantities AS
WITH lot_current_qty AS (
SELECT l.id AS lot_id,
round(((COALESCE(lh.quantity, (0)::numeric) * (COALESCE(lot_uom.factor, (1)::double precision))::numeric) / NULLIF((COALESCE(line_uom.factor, (1)::double precision))::numeric, (0)::numeric)), 5) AS pnl_quantity,
lot_uom.name AS quantity_uom
FROM (((((lot_lot l
LEFT JOIN lot_qt_hist lh ON (((lh.lot = l.id) AND (lh.quantity_type = l.lot_state))))
LEFT JOIN product_uom lot_uom ON ((lot_uom.id = l.lot_unit_line)))
LEFT JOIN purchase_line pl ON ((pl.id = l.line)))
LEFT JOIN sale_line sl ON ((sl.id = l.sale_line)))
LEFT JOIN product_uom line_uom ON ((line_uom.id = COALESCE(pl.unit, sl.unit))))
), purchase_pnl_lots AS (
SELECT 'purchase'::text AS pnl_side,
p.id AS purchase_id,
p.number AS purchase_number,
p.reference AS purchase_reference,
pl.id AS purchase_line_id,
pl.finished AS purchase_line_finished,
NULL::integer AS sale_id,
NULL::character varying AS sale_number,
NULL::character varying AS sale_reference,
NULL::integer AS sale_line_id,
NULL::boolean AS sale_line_finished,
l.id AS valued_lot_id,
l.lot_type AS valued_lot_type,
NULL::integer AS lot_qt_id,
lcq.pnl_quantity,
lcq.quantity_uom AS quantity_unit,
pl.product AS product_id,
COALESCE(shi.from_location, sint.from_location, sho.from_location, p.from_location) AS from_location_id,
COALESCE(shi.to_location, sint.to_location, sho.to_location, p.to_location) AS to_location_id,
shi.id AS shipment_id,
shi.bl_date,
pl.from_del AS delivery_date_from,
pl.to_del AS delivery_date_to
FROM ((((((purchase_line pl
JOIN purchase_purchase p ON ((p.id = pl.purchase)))
JOIN lot_lot l ON ((l.line = pl.id)))
JOIN lot_current_qty lcq ON ((lcq.lot_id = l.id)))
LEFT JOIN stock_shipment_in shi ON ((shi.id = l.lot_shipment_in)))
LEFT JOIN stock_shipment_internal sint ON ((sint.id = l.lot_shipment_internal)))
LEFT JOIN stock_shipment_out sho ON ((sho.id = l.lot_shipment_out)))
WHERE (((pl.type)::text = 'line'::text) AND ((l.lot_type)::text = ANY (ARRAY[('virtual'::character varying)::text, ('physic'::character varying)::text])))
), sale_pnl_direct_lots AS (
SELECT 'sale'::text AS pnl_side,
p.id AS purchase_id,
p.number AS purchase_number,
p.reference AS purchase_reference,
pl.id AS purchase_line_id,
pl.finished AS purchase_line_finished,
s.id AS sale_id,
s.number AS sale_number,
s.reference AS sale_reference,
sl.id AS sale_line_id,
sl.finished AS sale_line_finished,
l.id AS valued_lot_id,
l.lot_type AS valued_lot_type,
NULL::integer AS lot_qt_id,
lcq.pnl_quantity,
lcq.quantity_uom AS quantity_unit,
sl.product AS product_id,
COALESCE(shi.from_location, sint.from_location, sho.from_location, s.from_location) AS from_location_id,
COALESCE(shi.to_location, sint.to_location, sho.to_location, s.to_location) AS to_location_id,
shi.id AS shipment_id,
shi.bl_date,
sl.from_del AS delivery_date_from,
sl.to_del AS delivery_date_to
FROM ((((((((purchase_line pl
JOIN purchase_purchase p ON ((p.id = pl.purchase)))
JOIN lot_lot l ON (((l.line = pl.id) AND (l.sale_line IS NOT NULL))))
JOIN sale_line sl ON ((sl.id = l.sale_line)))
JOIN sale_sale s ON ((s.id = sl.sale)))
JOIN lot_current_qty lcq ON ((lcq.lot_id = l.id)))
LEFT JOIN stock_shipment_in shi ON ((shi.id = l.lot_shipment_in)))
LEFT JOIN stock_shipment_internal sint ON ((sint.id = l.lot_shipment_internal)))
LEFT JOIN stock_shipment_out sho ON ((sho.id = l.lot_shipment_out)))
WHERE (((pl.type)::text = 'line'::text) AND ((l.lot_type)::text = 'physic'::text))
), sale_pnl_matched_virtual_lots AS (
SELECT 'sale'::text AS pnl_side,
p.id AS purchase_id,
p.number AS purchase_number,
p.reference AS purchase_reference,
pl.id AS purchase_line_id,
pl.finished AS purchase_line_finished,
s.id AS sale_id,
s.number AS sale_number,
s.reference AS sale_reference,
sl.id AS sale_line_id,
sl.finished AS sale_line_finished,
sale_lot.id AS valued_lot_id,
sale_lot.lot_type AS valued_lot_type,
lqt.id AS lot_qt_id,
lcq.pnl_quantity,
lcq.quantity_uom AS quantity_unit,
sl.product AS product_id,
COALESCE(shi.from_location, sint.from_location, sho.from_location, s.from_location) AS from_location_id,
COALESCE(shi.to_location, sint.to_location, sho.to_location, s.to_location) AS to_location_id,
shi.id AS shipment_id,
shi.bl_date,
sl.from_del AS delivery_date_from,
sl.to_del AS delivery_date_to
FROM ((((((((((purchase_line pl
JOIN purchase_purchase p ON ((p.id = pl.purchase)))
JOIN lot_lot purchase_lot ON (((purchase_lot.line = pl.id) AND ((purchase_lot.lot_type)::text = 'virtual'::text) AND (purchase_lot.sale_line IS NULL))))
JOIN lot_qt lqt ON (((lqt.lot_p = purchase_lot.id) AND (lqt.lot_s IS NOT NULL) AND (lqt.lot_quantity > (0)::numeric))))
JOIN lot_lot sale_lot ON (((sale_lot.id = lqt.lot_s) AND ((sale_lot.lot_type)::text = 'virtual'::text))))
JOIN sale_line sl ON ((sl.id = sale_lot.sale_line)))
JOIN sale_sale s ON ((s.id = sl.sale)))
JOIN lot_current_qty lcq ON ((lcq.lot_id = sale_lot.id)))
LEFT JOIN stock_shipment_in shi ON ((shi.id = lqt.lot_shipment_in)))
LEFT JOIN stock_shipment_internal sint ON ((sint.id = lqt.lot_shipment_internal)))
LEFT JOIN stock_shipment_out sho ON ((sho.id = lqt.lot_shipment_out)))
WHERE ((pl.type)::text = 'line'::text)
), sale_pnl_unmatched_lots AS (
SELECT 'sale'::text AS pnl_side,
NULL::integer AS purchase_id,
NULL::character varying AS purchase_number,
NULL::character varying AS purchase_reference,
NULL::integer AS purchase_line_id,
NULL::boolean AS purchase_line_finished,
s.id AS sale_id,
s.number AS sale_number,
s.reference AS sale_reference,
sl.id AS sale_line_id,
sl.finished AS sale_line_finished,
l.id AS valued_lot_id,
l.lot_type AS valued_lot_type,
NULL::integer AS lot_qt_id,
lcq.pnl_quantity,
lcq.quantity_uom AS quantity_unit,
sl.product AS product_id,
COALESCE(shi.from_location, sint.from_location, sho.from_location, s.from_location) AS from_location_id,
COALESCE(shi.to_location, sint.to_location, sho.to_location, s.to_location) AS to_location_id,
shi.id AS shipment_id,
shi.bl_date,
sl.from_del AS delivery_date_from,
sl.to_del AS delivery_date_to
FROM ((((((sale_line sl
JOIN sale_sale s ON ((s.id = sl.sale)))
JOIN lot_lot l ON (((l.sale_line = sl.id) AND (l.line IS NULL) AND ((l.lot_type)::text = ANY (ARRAY[('virtual'::character varying)::text, ('physic'::character varying)::text])))))
JOIN lot_current_qty lcq ON ((lcq.lot_id = l.id)))
LEFT JOIN stock_shipment_in shi ON ((shi.id = l.lot_shipment_in)))
LEFT JOIN stock_shipment_internal sint ON ((sint.id = l.lot_shipment_internal)))
LEFT JOIN stock_shipment_out sho ON ((sho.id = l.lot_shipment_out)))
WHERE (((sl.type)::text = 'line'::text) AND (NOT (EXISTS ( SELECT 1
FROM lot_qt lqt
WHERE ((lqt.lot_s = l.id) AND (lqt.lot_p IS NOT NULL) AND (lqt.lot_quantity > (0)::numeric))))))
), pnl_lot_rows AS (
SELECT purchase_pnl_lots.pnl_side,
purchase_pnl_lots.purchase_id,
purchase_pnl_lots.purchase_number,
purchase_pnl_lots.purchase_reference,
purchase_pnl_lots.purchase_line_id,
purchase_pnl_lots.purchase_line_finished,
purchase_pnl_lots.sale_id,
purchase_pnl_lots.sale_number,
purchase_pnl_lots.sale_reference,
purchase_pnl_lots.sale_line_id,
purchase_pnl_lots.sale_line_finished,
purchase_pnl_lots.valued_lot_id,
purchase_pnl_lots.valued_lot_type,
purchase_pnl_lots.lot_qt_id,
purchase_pnl_lots.pnl_quantity,
purchase_pnl_lots.quantity_unit,
purchase_pnl_lots.product_id,
purchase_pnl_lots.from_location_id,
purchase_pnl_lots.to_location_id,
purchase_pnl_lots.shipment_id,
purchase_pnl_lots.bl_date,
purchase_pnl_lots.delivery_date_from,
purchase_pnl_lots.delivery_date_to
FROM purchase_pnl_lots
UNION ALL
SELECT sale_pnl_direct_lots.pnl_side,
sale_pnl_direct_lots.purchase_id,
sale_pnl_direct_lots.purchase_number,
sale_pnl_direct_lots.purchase_reference,
sale_pnl_direct_lots.purchase_line_id,
sale_pnl_direct_lots.purchase_line_finished,
sale_pnl_direct_lots.sale_id,
sale_pnl_direct_lots.sale_number,
sale_pnl_direct_lots.sale_reference,
sale_pnl_direct_lots.sale_line_id,
sale_pnl_direct_lots.sale_line_finished,
sale_pnl_direct_lots.valued_lot_id,
sale_pnl_direct_lots.valued_lot_type,
sale_pnl_direct_lots.lot_qt_id,
sale_pnl_direct_lots.pnl_quantity,
sale_pnl_direct_lots.quantity_unit,
sale_pnl_direct_lots.product_id,
sale_pnl_direct_lots.from_location_id,
sale_pnl_direct_lots.to_location_id,
sale_pnl_direct_lots.shipment_id,
sale_pnl_direct_lots.bl_date,
sale_pnl_direct_lots.delivery_date_from,
sale_pnl_direct_lots.delivery_date_to
FROM sale_pnl_direct_lots
UNION ALL
SELECT sale_pnl_matched_virtual_lots.pnl_side,
sale_pnl_matched_virtual_lots.purchase_id,
sale_pnl_matched_virtual_lots.purchase_number,
sale_pnl_matched_virtual_lots.purchase_reference,
sale_pnl_matched_virtual_lots.purchase_line_id,
sale_pnl_matched_virtual_lots.purchase_line_finished,
sale_pnl_matched_virtual_lots.sale_id,
sale_pnl_matched_virtual_lots.sale_number,
sale_pnl_matched_virtual_lots.sale_reference,
sale_pnl_matched_virtual_lots.sale_line_id,
sale_pnl_matched_virtual_lots.sale_line_finished,
sale_pnl_matched_virtual_lots.valued_lot_id,
sale_pnl_matched_virtual_lots.valued_lot_type,
sale_pnl_matched_virtual_lots.lot_qt_id,
sale_pnl_matched_virtual_lots.pnl_quantity,
sale_pnl_matched_virtual_lots.quantity_unit,
sale_pnl_matched_virtual_lots.product_id,
sale_pnl_matched_virtual_lots.from_location_id,
sale_pnl_matched_virtual_lots.to_location_id,
sale_pnl_matched_virtual_lots.shipment_id,
sale_pnl_matched_virtual_lots.bl_date,
sale_pnl_matched_virtual_lots.delivery_date_from,
sale_pnl_matched_virtual_lots.delivery_date_to
FROM sale_pnl_matched_virtual_lots
UNION ALL
SELECT sale_pnl_unmatched_lots.pnl_side,
sale_pnl_unmatched_lots.purchase_id,
sale_pnl_unmatched_lots.purchase_number,
sale_pnl_unmatched_lots.purchase_reference,
sale_pnl_unmatched_lots.purchase_line_id,
sale_pnl_unmatched_lots.purchase_line_finished,
sale_pnl_unmatched_lots.sale_id,
sale_pnl_unmatched_lots.sale_number,
sale_pnl_unmatched_lots.sale_reference,
sale_pnl_unmatched_lots.sale_line_id,
sale_pnl_unmatched_lots.sale_line_finished,
sale_pnl_unmatched_lots.valued_lot_id,
sale_pnl_unmatched_lots.valued_lot_type,
sale_pnl_unmatched_lots.lot_qt_id,
sale_pnl_unmatched_lots.pnl_quantity,
sale_pnl_unmatched_lots.quantity_unit,
sale_pnl_unmatched_lots.product_id,
sale_pnl_unmatched_lots.from_location_id,
sale_pnl_unmatched_lots.to_location_id,
sale_pnl_unmatched_lots.shipment_id,
sale_pnl_unmatched_lots.bl_date,
sale_pnl_unmatched_lots.delivery_date_from,
sale_pnl_unmatched_lots.delivery_date_to
FROM sale_pnl_unmatched_lots
), pnl_lot_rows_with_finished AS (
SELECT pnl_lot_rows.pnl_side,
pnl_lot_rows.purchase_id,
pnl_lot_rows.purchase_number,
pnl_lot_rows.purchase_reference,
pnl_lot_rows.purchase_line_id,
pnl_lot_rows.purchase_line_finished,
pnl_lot_rows.sale_id,
pnl_lot_rows.sale_number,
pnl_lot_rows.sale_reference,
pnl_lot_rows.sale_line_id,
pnl_lot_rows.sale_line_finished,
pnl_lot_rows.valued_lot_id,
pnl_lot_rows.valued_lot_type,
pnl_lot_rows.lot_qt_id,
pnl_lot_rows.pnl_quantity,
pnl_lot_rows.quantity_unit,
pnl_lot_rows.product_id,
pnl_lot_rows.from_location_id,
pnl_lot_rows.to_location_id,
pnl_lot_rows.shipment_id,
pnl_lot_rows.bl_date,
pnl_lot_rows.delivery_date_from,
pnl_lot_rows.delivery_date_to,
CASE
WHEN (pnl_lot_rows.pnl_side = 'purchase'::text) THEN pnl_lot_rows.purchase_line_finished
WHEN (pnl_lot_rows.pnl_side = 'sale'::text) THEN pnl_lot_rows.sale_line_finished
ELSE false
END AS valued_line_finished,
CASE
WHEN (((pnl_lot_rows.valued_lot_type)::text = 'virtual'::text) AND (((pnl_lot_rows.pnl_side = 'purchase'::text) AND (pnl_lot_rows.purchase_line_finished IS TRUE)) OR ((pnl_lot_rows.pnl_side = 'sale'::text) AND (pnl_lot_rows.sale_line_finished IS TRUE)))) THEN true
ELSE false
END AS ignored_by_finished_rule
FROM pnl_lot_rows
)
SELECT pnl_side,
purchase_id,
purchase_number,
purchase_reference,
purchase_line_id,
purchase_line_finished,
sale_id,
sale_number,
sale_reference,
sale_line_id,
sale_line_finished,
valued_lot_id,
valued_lot_type,
valued_line_finished,
ignored_by_finished_rule,
sum(pnl_quantity) AS pnl_quantity_raw,
sum(
CASE
WHEN ignored_by_finished_rule THEN (0)::numeric
ELSE pnl_quantity
END) AS pnl_quantity_after_finished_rule,
quantity_unit,
product_id,
from_location_id,
to_location_id,
shipment_id,
bl_date,
delivery_date_from,
delivery_date_to
FROM pnl_lot_rows_with_finished
GROUP BY pnl_side, purchase_id, purchase_number, purchase_reference, purchase_line_id, purchase_line_finished, sale_id, sale_number, sale_reference, sale_line_id, sale_line_finished, valued_lot_type, valued_lot_id, valued_line_finished, ignored_by_finished_rule, quantity_unit, product_id, from_location_id, to_location_id, shipment_id, bl_date, delivery_date_from, delivery_date_to;;