--P5-1367
DROP MATERIALIZED VIEW IF EXISTS v_esales_product;

CREATE MATERIALIZED VIEW public.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 designers.pricepremium / 100::numeric
           FROM designers
          WHERE designers.designerid = main.designerid
         LIMIT 1), 0::numeric) AS designer_margin,
    ( SELECT "product-printproducts".typeid
           FROM "product-printproducts"
          WHERE "product-printproducts".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'::numeric
            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 "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'artNo'::text
                         LIMIT 1))) AS "artNo",
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'name'::text
                         LIMIT 1))) AS name,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS height,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'orgArtNo'::text
                         LIMIT 1))) AS original_article_number,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS width,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'widths'::text
                         LIMIT 1))) AS widths,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'min-height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS min_height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'min-width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS min_width,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'max-height'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS max_height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'max-width'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS max_width,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'photowall_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS photowall_resolution,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'canvas_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS canvas_resolution,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'wallpaper_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS wallpaper_resolution,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'lock_height'::text
                         LIMIT 1))) AS lock_height,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'additionalprice'::text
                         LIMIT 1))))::double precision AS additionalprice,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'ref1'::text
                         LIMIT 1))) AS ref1,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'ref2'::text
                         LIMIT 1))) AS ref2,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'ref3'::text
                         LIMIT 1))) AS ref3,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'copyright'::text
                         LIMIT 1))) AS copyright,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'proportions_warning'::text
                         LIMIT 1))) AS proportions_warning,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_width_max'::text
                         LIMIT 1))) AS margin_width_max,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_width_min'::text
                         LIMIT 1))) AS margin_width_min,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_height_max'::text
                         LIMIT 1))) AS margin_height_max,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'margin_height_min'::text
                         LIMIT 1))) AS margin_height_min,
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'batch'::text
                         LIMIT 1))) AS batch,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'image_resolution'::text
                         LIMIT 1)) AND "product-products_fields".value::text ~ '^\d+$'::text))::integer AS image_resolution,
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusXpoint'::text
                         LIMIT 1))))::double precision AS "focusXpoint",
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusYpoint'::text
                         LIMIT 1))))::double precision AS "focusYpoint",
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusXpoint2'::text
                         LIMIT 1))))::double precision AS "focusXpoint2",
            (( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'focusYpoint2'::text
                         LIMIT 1))))::double precision AS "focusYpoint2",
            ( SELECT "product-products_fields".value
                   FROM "product-products_fields"
                  WHERE "product-products_fields".productid = products.productid AND "product-products_fields".fieldid = (( SELECT "product-fields".fieldid
                           FROM "product-fields"
                          WHERE "product-fields".field::text = 'repeating_pattern'::text
                         LIMIT 1))) AS repeating_pattern
           FROM "product-products" products) main
     LEFT JOIN ( SELECT s.stockid AS id,
            s.productid AS product_id,
            ( SELECT "product-stockprices".price
                   FROM "product-stockprices"
                  WHERE "product-stockprices".priceid = s.priceid
                 LIMIT 1) AS price
           FROM "product-stockproducts" s) stock ON main.product_id = stock.product_id
     LEFT JOIN ( SELECT designers.designerid AS id,
            designers.name,
            designers.path,
                CASE
                    WHEN designers.status = 1 AND designers.visible_productpage = 1 THEN 1
                    ELSE 0
                END AS visible_productpage
           FROM designers) designer ON main.designerid = designer.id
  WHERE main.width IS NOT NULL AND main.height IS NOT NULL
WITH DATA;

ALTER TABLE public.v_esales_product
  OWNER TO photowall;

-- Index: public.v_esales_product_id

CREATE UNIQUE INDEX v_esales_product_id
  ON public.v_esales_product
  USING btree
  (id);