-- P5-994
DROP MATERIALIZED VIEW IF EXISTS v_esales_product;
-- REFRESH MATERIALIZED VIEW v_esales_product;
CREATE MATERIALIZED VIEW v_esales_product AS
SELECT	main.product_id AS id,
        main."artNo",
        stock.id AS stock_id,
        stock.price AS stock_price,
        main.path,
        main.visible,
        main.browsable,
        main.designerid,
        designer.name AS designer_name,
        designer.path AS designer_path,
        designer.visible_productpage AS designer_visible_productpage,
        main.designercollection_id,
        COALESCE((
            SELECT  pricepremium / 100
              FROM  designers
             WHERE  designerid = main.designerid
             LIMIT  1
        ), 0) AS designer_margin,
        (
            SELECT  typeid
              FROM  "product-printproducts"
             WHERE  productid = main.product_id
             LIMIT  1
        ) AS producttype_id,
        main.name,
        main.height,
        main.original_article_number,
        main.width,
        main.widths,
        main.min_height,
        main.min_width,
        main.max_height,
        main.max_width,
        main.photowall_resolution,
        main.canvas_resolution,
        main.wallpaper_resolution,
        main.lock_height,
        COALESCE(main.additionalprice, 0::double precision) AS additionalprice,
        main.ref1,
        main.ref2,
        main.ref3,
        main.copyright,
        main.proportions_warning,
        main.margin_width_max,
        main.margin_width_min,
        main.margin_height_max,
        main.margin_height_min,
        main.width::numeric /
        CASE
            WHEN main.height = 0 THEN (-1.0)
            ELSE main.height::numeric
        END AS width_height_ratio,
        main.batch,
        main.image_resolution,
        main."focusXpoint",
        main."focusYpoint",
        main."focusXpoint2",
        main."focusYpoint2",
        main.repeating_pattern
        FROM
        (
            SELECT  products.productid AS product_id,
                    products.path,
                    products.visible,
                    products.browsable,
                    products.designerid,
                    products.designercollectionid AS designercollection_id,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'artNo' LIMIT 1)
                    ) AS "artNo",

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'name' LIMIT 1)
                    ) AS name,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'height' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS height,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'orgArtNo' LIMIT 1)
                    ) AS original_article_number,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'width' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS width,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'widths' LIMIT 1)
                    ) AS widths,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'min-height' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS min_height,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'min-width' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS min_width,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'max-height' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS max_height,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'max-width' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS max_width,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'photowall_resolution' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS photowall_resolution,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'canvas_resolution' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS canvas_resolution,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'wallpaper_resolution' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS wallpaper_resolution,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'lock_height' LIMIT 1)
                    ) AS lock_height,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'additionalprice' LIMIT 1)
                    )::double precision AS additionalprice,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'ref1' LIMIT 1)
                    ) AS ref1,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'ref2' LIMIT 1)
                    ) AS ref2,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'ref3' LIMIT 1)
                    ) AS ref3,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'copyright' LIMIT 1)
                    ) AS copyright,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'proportions_warning' LIMIT 1)
                    ) AS proportions_warning,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'margin_width_max' LIMIT 1)
                    ) AS margin_width_max,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'margin_width_min' LIMIT 1)
                    ) AS margin_width_min,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'margin_height_max' LIMIT 1)
                    ) AS margin_height_max,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'margin_height_min' LIMIT 1)
                    ) AS margin_height_min,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'batch' LIMIT 1)
                    ) AS batch,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'image_resolution' LIMIT 1)
                           AND  value ~ '^\d+$'
                    )::integer AS image_resolution,

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'focusXpoint' LIMIT 1)
                    )::double precision AS "focusXpoint",

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'focusYpoint' LIMIT 1)
                    )::double precision AS "focusYpoint",

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'focusXpoint2' LIMIT 1)
                    )::double precision AS "focusXpoint2",

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'focusYpoint2' LIMIT 1)
                    )::double precision AS "focusYpoint2",

                    (
                        SELECT  value FROM "product-products_fields" WHERE productid = products.productid
                           AND  fieldid = (SELECT fieldid FROM "product-fields" WHERE field = 'repeating_pattern' LIMIT 1)
                    ) AS repeating_pattern
              FROM  "product-products" products
        ) main
  LEFT  JOIN (
                SELECT  stockid AS id,
                        productid AS product_id,
                        (
                            SELECT  price
                              FROM  "product-stockprices"
                             WHERE  priceid = s.priceid LIMIT 1
                        ) AS price
                  FROM  "product-stockproducts" s
             ) stock ON main.product_id = stock.product_id
  LEFT  JOIN (
                SELECT  designerid AS id,
                        name,
                        path,
                        CASE
                            WHEN status = 1 AND visible_productpage = 1 THEN 1
                        ELSE 0
                        END AS visible_productpage
                  FROM  designers
    ) designer ON main.designerid = designer.id;

-- DROP INDEX v_product_id;
CREATE UNIQUE INDEX v_esales_product_id
  ON v_esales_product (id);
