Files
SQL-Scripts/vw_utility_stock_movements.sql
AzureAD\SylvainDUVERNAY 0d02c89192 Test commit
2026-01-06 16:31:18 +01:00

46 lines
2.1 KiB
SQL

-- View: public.vw_utility_stock_movements
-- DROP VIEW public.vw_utility_stock_movements;
--Comments: This view provides a comprehensive overview of stock movements, including details such as stock IDs, company IDs, lot IDs, cost prices, currencies, effective dates, product IDs, locations, origins, movement types, quantities, units, and states.
CREATE OR REPLACE VIEW public.vw_utility_stock_movements AS
SELECT sm.id AS "intStockId",
sm.company AS "intCompanyId",
COALESCE(sm.lot, 0) AS "intLotId",
COALESCE(sm.cost_price, 0::numeric) AS "dblCostPrice",
cur.name AS "strCurrency",
sm.effective_date AS "dtmEffectiveDate",
sm.bldate AS "dtmBLDate",
sm.product AS "intProductId",
sm.from_location AS "intFromLocationId",
sm.to_location AS "intToLocationId",
sm.origin,
split_part(sm.origin::text, ','::text, 1) AS "strSourceType",
COALESCE(split_part(sm.shipment::text, ','::text, 2)::integer, 0) AS "intShipmentId",
CASE
WHEN split_part(sm.origin::text, ','::text, 1) = 'purchase.line'::text THEN 'Inbound'::text
WHEN split_part(sm.origin::text, ','::text, 1) = 'sale.line'::text THEN 'Outbound'::text
ELSE 'Unknown'::text
END AS "strMovementType",
CASE
WHEN split_part(sm.origin::text, ','::text, 1) = 'purchase.line'::text THEN split_part(sm.origin::text, ','::text, 2)::integer
ELSE 0
END AS "intPurchaseLineId",
CASE
WHEN split_part(sm.origin::text, ','::text, 1) = 'sale.line'::text THEN split_part(sm.origin::text, ','::text, 2)::integer
ELSE 0
END AS "intSaleLineId",
sm.quantity AS "dblQuantity",
uom.name AS "strUnit",
round((sm.quantity * uom.factor)::numeric, get_rounding_position(uom.rounding::numeric)) AS "dblQuantityKg",
sm.unit_price,
sm.unit_price_updated AS "ysnPriceUpdated",
sm.state AS "strState"
FROM stock_move sm
JOIN currency_currency cur ON sm.currency = cur.id
LEFT JOIN product_uom uom ON sm.unit = uom.id;
ALTER TABLE public.vw_utility_stock_movements
OWNER TO postgres;