-- P5-4777: product list prices

-- remove listprice from v_esales_printproduct (re-create the view without the listprice column)
DROP MATERIALIZED VIEW IF EXISTS v_esales_printproduct;

CREATE MATERIALIZED VIEW v_esales_printproduct AS
 SELECT main.id,
    main.product_id,
    main.path,
    main.article_number,
    main.title,
    main.group_id,
    main.group_name,
    main.type_id,
    main.type_name,
    main.image,
    main.thumbnail,
    main.width,
    main.height,
        CASE
            WHEN main.width::integer > main.height::integer THEN 'landscape'::text
            WHEN main.width::integer < main.height::integer THEN 'portrait'::text
            ELSE 'square'::text
        END AS orientation
   FROM ( SELECT pp.printid AS id,
            pp.productid AS product_id,
            ( SELECT "product-products".path
                   FROM "product-products"
                  WHERE "product-products".productid = pp.productid
                 LIMIT 1) AS path,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = pp.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'artNo'::text
                         LIMIT 1))) AS article_number,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = pp.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'name'::text
                         LIMIT 1))) AS title,
            pp.groupid AS group_id,
            ( SELECT "product-groups"."group"
                   FROM "product-groups"
                  WHERE "product-groups".groupid = pp.groupid
                 LIMIT 1) AS group_name,
            pp.typeid AS type_id,
            ( SELECT "product-types".type
                   FROM "product-types"
                  WHERE "product-types".typeid = pp.typeid
                 LIMIT 1) AS type_name,
            ('//images.photowall.com/products/'::text || pp.productid) || '.jpg'::text AS image,
            ('//images.photowall.com/products/'::text || pp.productid) || '.jpg?w=80'::text AS thumbnail,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = pp.productid AND "product-products_fields".fieldid = 5
                 LIMIT 1) AS width,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = pp.productid AND "product-products_fields".fieldid = 3
                 LIMIT 1) AS height
           FROM "product-printproducts" pp) main;

CREATE UNIQUE INDEX v_esales_printproduct_id
 ON v_esales_printproduct (id);

CREATE INDEX v_esales_printproduct_product_id
 ON v_esales_printproduct (product_id);

ALTER MATERIALIZED VIEW v_esales_printproduct OWNER TO photowall;

-- remove listprice from printproducts
ALTER TABLE "product-printproducts"
  DROP COLUMN IF EXISTS listprice;


-- create new table for listprices
CREATE TABLE IF NOT EXISTS listprices
(
    printproduct_id INTEGER NOT NULL,
    market_id INTEGER NOT NULL,
    listprice INTEGER,
    UNIQUE (printproduct_id, market_id)
);

