-- Experiment to replace listprices table with a view -- Move local price adjustments from yaml file in API2 to table so we can access data CREATE TYPE price_adjustments_product_type AS ENUM ('product', 'inquiry'); CREATE TABLE price_adjustments ( market_id int REFERENCES markets (id) NOT NULL, material_id int REFERENCES "product-materials" (materialid) NOT NULL, product_type price_adjustments_product_type NOT NULL, value numeric NOT NULL, inserted timestamp with time zone DEFAULT now(), updated timestamp with time zone, UNIQUE (market_id, material_id, product_type) ); CREATE TRIGGER updated_timestamp BEFORE UPDATE ON price_adjustments FOR EACH ROW EXECUTE PROCEDURE updated_timestamp(); INSERT INTO price_adjustments (market_id, material_id, product_type, value) VALUES ((select id from markets where name = 'IT'), (select materialid from "product-materials" where material = 'standard-wallpaper'), 'inquiry', 1.1034), ((select id from markets where name = 'GB'), (select materialid from "product-materials" where material = 'standard-wallpaper'), 'inquiry', 1.1154), ((select id from markets where name = 'DE'), (select materialid from "product-materials" where material = 'standard-wallpaper'), 'inquiry', 1.0625), ((select id from markets where name = 'GB'), (select materialid from "product-materials" where material = 'standard-wallpaper'), 'product', 1.1154), ((select id from markets where name = 'DE'), (select materialid from "product-materials" where material = 'standard-wallpaper'), 'product', 1.0625), ((select id from markets where name = 'IT'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'inquiry', 1.1250), ((select id from markets where name = 'SE'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'inquiry', 1.0462), ((select id from markets where name = 'GB'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'inquiry', 1.1034), ((select id from markets where name = 'DE'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'inquiry', 1.1143), ((select id from markets where name = 'NL'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'inquiry', 1.0833), ((select id from markets where name = 'GB'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'product', 1.1034), ((select id from markets where name = 'DE'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'product', 1.1143), ((select id from markets where name = 'SE'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'product', 1.0462), ((select id from markets where name = 'NL'), (select materialid from "product-materials" where material = 'premium-wallpaper'), 'product', 1.0833) ; create view v_listprices as select printid printproduct_id, markets.id market_id, round( -- Base price CASE WHEN groupid IN (1,3) THEN -- Wallpaper (select (price/100.0) from "product-materials" where material = 'standard-wallpaper') WHEN groupid = 2 THEN -- Canvas (select min(price_sek) from external_print_products where group_id = 2) WHEN groupid IN (7,8) THEN -- Poster (select min(price_sek) from external_print_products where group_id in (7,8)) END -- Market price adjustment, and VAT, and currency exchange rate * exchange_rate * price_adjustment * vat -- Designer price premium, if exist * (coalesce( CASE WHEN groupid IN (1,3) THEN pricepremium_wallpaper WHEN groupid = 2 THEN pricepremium_canvas WHEN groupid = 7 THEN pricepremium_poster WHEN groupid = 8 THEN pricepremium_framed_print END , 1) / 100.0 + 1) -- Local product type adjustments, if wallpaper, and if exists * COALESCE( CASE WHEN groupid in (1,3) THEN (select value from price_adjustments where material_id = 1 and product_type = 'product' and market_id = markets.id limit 1) END , 1.0) -- Round to two decimals (to allow for more precision when shown as sq feet) , 2) listprice from "product-printproducts" join "product-products" using (productid) left join designers using (designerid), markets join "product-currencies" on currency = iso3char ; COMMENT ON COLUMN v_listprices.listprice IS 'Canvas and poster listprice: Start with price (SEK) for cheapest canvas/poster size. Multiply with market price adjustment, VAT and currency exchange rate. Multiply with designer price premium, if exists. Round to two decimals. Wallpaper listprice: Same as poster/canvas, but start with the price for 1 m/2 standard wallpaper. Also include local wallpaper price adjustments (per market, product type, and material) if exists.';