-- P5-3936 Format date as YYYYMMDD in v_analytics_orders and v_analytics_order_rows

DROP VIEW IF EXISTS v_analytics_orders;

CREATE VIEW v_analytics_orders AS
SELECT
    CASE
        WHEN country_code.value IS NULL THEN 'Unknown'
        WHEN country_code.value = 'GB' THEN 'Photowall UK'
        WHEN country_code.value in ('SE','NO','DE','AT','FR','ES','FI','DK','PL','US','NL') THEN 'Photowall '||country_code.value
        ELSE 'Photowall OTHER'
    END AS account,
    orders.inserted::DATE AS orderdate,
    COALESCE(confirmed.value::boolean, false) AS confirmed,
    COALESCE(paid.value::boolean, false) AS paid,
    COALESCE(delivered.value::boolean, false) AS delivered,
    orders.orderid,
    md5(firstname.value||'secret') AS first_name,
    md5(lastname.value||'secret') AS last_name,
    md5(company.value||'secret') AS company,
    md5(address.value||'secret') AS address,
    zipcode.value AS postal_code,
    city.value AS city,
    country_code.value AS country,
    md5(email.value||'secret') AS email,
    order_sum.value::float / exchange_rate.value::float as order_value_sek
from
    "order-orders" orders
    left join "order-orders_fields" confirmed on orders.orderid = confirmed.orderid AND confirmed.fieldid = 175
    left join "order-orders_fields" delivered on orders.orderid = delivered.orderid AND delivered.fieldid = 176
    left join "order-orders_fields" paid on orders.orderid = paid.orderid AND paid.fieldid = 168
    left join "order-orders_fields" firstname on orders.orderid = firstname.orderid AND firstname.fieldid = 156
    left join "order-orders_fields" lastname on orders.orderid = lastname.orderid AND lastname.fieldid = 157
    left join "order-orders_fields" company on orders.orderid = company.orderid AND company.fieldid = 177
    left join "order-orders_fields" address on orders.orderid = address.orderid AND address.fieldid = 158
    left join "order-orders_fields" zipcode on orders.orderid = zipcode.orderid AND zipcode.fieldid = 159
    left join "order-orders_fields" city on orders.orderid = city.orderid AND city.fieldid = 160
    left join "order-orders_fields" country_code on orders.orderid = country_code.orderid AND country_code.fieldid = 151
    left join "order-orders_fields" email on orders.orderid = email.orderid AND email.fieldid = 184
    left join "order-orders_fields" order_sum on orders.orderid = order_sum.orderid AND order_sum.fieldid = 246
    left join "order-orders_fields" exchange_rate on orders.orderid = exchange_rate.orderid AND exchange_rate.fieldid = 171
;

GRANT SELECT ON v_analytics_orders TO datastudio;

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'
        WHEN country_code.value = 'GB' THEN 'Photowall UK'
        WHEN country_code.value in ('SE','NO','DE','AT','FR','ES','FI','DK','PL','US','NL') THEN 'Photowall '||country_code.value
        ELSE 'Photowall OTHER'
    END AS account,
    orderrows.orderid as orderid,
    orderrows.rowid as 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' AND productpath.value = 'sample' THEN 'Sample'
        ELSE productgroup.value
    END AS type,
    material.value as material,
    width.value as width,
    height.value as height,
    CASE
        WHEN status.value = 'process' THEN 'Ready for processing'
        WHEN status.value = 'print' THEN 'Ready for printing'
        WHEN status.value = 'sent' OR status.value = 'pack' THEN 'Ready for packing'
        ELSE 'Unknown'
    END AS status,
    commission.value as commission_percent,
    round(commission_amount.value::float) as commission_amount,
    discount_type.value as discount_type,
    discount_value.value as discount_value,
    order_currency.value as order_currency,
    designer.value as designer
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" 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" 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-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" orders on orders.orderid = orderrows.orderid
where
    modifier.rowid is null
;

GRANT SELECT ON v_analytics_order_rows TO datastudio;
