-- Use mm values when exists for width and height fields

DROP VIEW IF EXISTS v_analytics_order_rows;

CREATE VIEW v_analytics_order_rows AS
    SELECT
        CASE
            WHEN country_code.value IS NULL THEN 'Unknown'::text
            WHEN country_code.value = 'GB'::text THEN 'Photowall UK'::text
            WHEN country_code.value = ANY (ARRAY['SE'::text, 'NO'::text, 'DE'::text, 'AT'::text, 'FR'::text, 'ES'::text, 'FI'::text, 'DK'::text, 'PL'::text, 'US'::text, 'NL'::text]) THEN 'Photowall '::text || country_code.value
            ELSE 'Photowall OTHER'::text
        END AS account,
    orderrows.orderid,
    orderrows.rowid,
    orders.inserted::date AS orderdate,
    artno.value AS artno,
    productid.value AS productid,
    name.value AS name,
    inquiryid.value AS inquiryid,
        CASE
            WHEN producttype.value = 'stock'::text AND productpath.value = 'sample'::text THEN 'Sample'::text
            ELSE productgroup.value
        END AS type,
    material.value AS material,
	COALESCE(width_mm.value::float / 10, width.value::float) as width,
	COALESCE(height_mm.value::float / 10, height.value::float) as height,
        CASE
            WHEN status.value = 'process'::text THEN 'Ready for processing'::text
            WHEN status.value = 'print'::text THEN 'Ready for printing'::text
            WHEN status.value = 'sent'::text OR status.value = 'pack'::text THEN 'Ready for packing'::text
            ELSE 'Unknown'::text
        END AS status,
    commission.value AS commission_percent,
    round(commission_amount.value::double precision) AS commission_amount,
    discount_type.value AS discount_type,
    discount_value.value AS discount_value,
        CASE
            WHEN discount_type.value <> '%'::text THEN discount_value.value::double precision / exchange_rate.value::double precision
            ELSE NULL::double precision
        END AS discount_amount_sek,
    order_currency.value AS order_currency,
    designer.value AS designer,
    price.value::double precision / exchange_rate.value::double precision AS item_value_sek,
    framed.value AS canvas_frame,
    poster_hanger.value AS poster_hanger,
    frame_color.value AS framed_print_frame,
    price.value::double precision AS price,
    vat.value AS vat
    FROM "order-rows" orderrows
     LEFT JOIN "order-rows_details" artno ON orderrows.rowid = artno.rowid AND artno.fieldid = 95
     LEFT JOIN "order-rows_details" productid ON orderrows.rowid = productid.rowid AND productid.fieldid = 92
     LEFT JOIN "order-rows_details" name ON orderrows.rowid = name.rowid AND name.fieldid = 97
     LEFT JOIN "order-rows_details" price ON orderrows.rowid = price.rowid AND price.fieldid = 98
     LEFT JOIN "order-rows_details" modifier ON orderrows.rowid = modifier.rowid AND modifier.fieldid = 113
     LEFT JOIN "order-rows_details" inquiryid ON orderrows.rowid = inquiryid.rowid AND inquiryid.fieldid = 114
     LEFT JOIN "order-rows_details" producttype ON orderrows.rowid = producttype.rowid AND producttype.fieldid = 94
     LEFT JOIN "order-rows_details" productpath ON orderrows.rowid = productpath.rowid AND productpath.fieldid = 96
     LEFT JOIN "order-rows_details" productgroup ON orderrows.rowid = productgroup.rowid AND productgroup.fieldid = 93
     LEFT JOIN "order-rows_details" material ON orderrows.rowid = material.rowid AND material.fieldid = 131
     LEFT JOIN "order-rows_details" width ON orderrows.rowid = width.rowid AND width.fieldid = 99
     LEFT JOIN "order-rows_details" height ON orderrows.rowid = height.rowid AND height.fieldid = 100
     LEFT JOIN "order-rows_details" width_mm ON orderrows.rowid = width_mm.rowid AND width_mm.fieldid = 161
     LEFT JOIN "order-rows_details" height_mm ON orderrows.rowid = height_mm.rowid AND height_mm.fieldid = 162
     LEFT JOIN "order-rows_details" status ON orderrows.rowid = status.rowid AND status.fieldid = 108
     LEFT JOIN "order-rows_details" commission ON orderrows.rowid = commission.rowid AND commission.fieldid = 106
     LEFT JOIN "order-rows_details" commission_amount ON orderrows.rowid = commission_amount.rowid AND commission_amount.fieldid = 120
     LEFT JOIN "order-rows_details" discount_type ON orderrows.rowid = discount_type.rowid AND discount_type.fieldid = 142
     LEFT JOIN "order-rows_details" discount_value ON orderrows.rowid = discount_value.rowid AND discount_value.fieldid = 143
     LEFT JOIN "order-rows_details" poster_hanger ON orderrows.rowid = poster_hanger.rowid AND poster_hanger.fieldid = 153
     LEFT JOIN "order-rows_details" framed ON orderrows.rowid = framed.rowid AND framed.fieldid = 122
     LEFT JOIN "order-rows_details" frame_color ON orderrows.rowid = frame_color.rowid AND frame_color.fieldid = 156
     LEFT JOIN "order-orders_fields" order_currency ON orderrows.orderid = order_currency.orderid AND order_currency.fieldid = 169
     LEFT JOIN "order-orders_fields" country_code ON orderrows.orderid = country_code.orderid AND country_code.fieldid = 151
     LEFT JOIN "order-rows_details" designer ON orderrows.rowid = designer.rowid AND designer.fieldid = 104
     LEFT JOIN "order-orders_fields" exchange_rate ON orderrows.orderid = exchange_rate.orderid AND exchange_rate.fieldid = 171
     LEFT JOIN "order-rows_details" vat ON orderrows.rowid = vat.rowid AND vat.fieldid = 110
     LEFT JOIN "order-orders" orders ON orders.orderid = orderrows.orderid
    WHERE modifier.rowid IS NULL;

GRANT SELECT ON v_analytics_order_rows TO datastudio;
