-- Adapt to orders and order_rows tables

--
DROP VIEW IF EXISTS v_analytics_contract_customer_discounts;

CREATE VIEW v_analytics_contract_customer_discounts AS
  SELECT
  order_rows.order_id as orderid,
  order_rows.id as rowid,
  orders.delivery_vat as vat,
  order_rows.price,
  round((order_rows.data->>'percent')::numeric / 100 , 2) AS percent
  FROM order_rows
  JOIN orders on orders.id = order_rows.order_id
  WHERE order_rows.sub_type = 'contract_customer_discount';

GRANT SELECT ON v_analytics_contract_customer_discounts TO datastudio;

--
DROP VIEW IF EXISTS v_analytics_discounts;

CREATE VIEW v_analytics_discounts AS
 SELECT
 	order_rows.order_id as orderid,
  	order_rows.id as rowid,
  	order_rows.data->>'type' as type,
  	orders.delivery_vat as vat,
  	(abs(price / exchange_rate))::float AS price_sek,
	  order_rows.data->>'code' as code,
   	order_rows.data->>'no_shipping' as no_shipping
   	from order_rows
   	JOIN orders on orders.id = order_rows.order_id
   	WHERE order_rows.sub_type = 'discount_code';

GRANT SELECT ON v_analytics_discounts TO datastudio;

--

DROP VIEW IF EXISTS v_analytics_inquiries;

CREATE VIEW v_analytics_inquiries AS
 SELECT inquiries.inquiryid,
    inquiries.inserted::date AS inserted,
    inquiries.updated::date AS updated,
    inquiries.company,
    inquiries.firstname,
    inquiries.lastname AS email,
    inquiries.market_id,
    inquiries.locale_id AS product_group,
    inquiries.price_m2,
    inquiries.price_effect,
    inquiries.price_retouch,
    inquiries.width,
    inquiries.height,
    inquiries."artNo",
    inquiries.materialid,
    inquiries.zendesk_ticket,
    order_rows.id AS order_row_id,
    order_rows.order_id AS order_id
   FROM inquiries
   	 LEFT JOIN order_rows on order_rows.data->>'inquiryid' = inquiries.inquiryid::text
  ORDER BY (inquiries.inserted::date) DESC;

GRANT SELECT ON v_analytics_inquiries to datastudio;

--

DROP VIEW IF EXISTS v_analytics_order_rows;

CREATE VIEW v_analytics_order_rows AS
  SELECT
    CASE
       WHEN orders.market IS NULL THEN 'Unknown'
       WHEN orders.market = 'GB' THEN 'Photowall UK'
       WHEN orders.market = 'GLOBAL' THEN 'Photowall OTHER'
       ELSE 'Photowall ' || orders.market
    END AS account,
    order_rows.order_id as orderid,
    order_rows.id as rowid,
    orders.inserted::date AS orderdate,
    order_rows.data->>'artNo' AS artno,
    order_rows.data->>'productId' AS productid,
	order_rows.data->>'name' AS name,
	order_rows.data->>'inquiryid' AS inquiryid,
    CASE
        WHEN order_rows.type = 'sample' THEN 'Sample'
        ELSE order_rows.data->>'group'
    END AS type,
    order_rows.data->>'material' as material,
    COALESCE((order_rows.data->>'width_mm')::float / 10::float, (order_rows.data->>'width')::float) AS width,
    COALESCE((order_rows.data->>'height_mm')::float / 10::float, (order_rows.data->>'height')::float) AS height,
    CASE
        WHEN order_rows.status = 'process' THEN 'Ready for processing'
        WHEN  order_rows.status = 'print' THEN 'Ready for packing'
        WHEN  order_rows.status = 'packed' THEN 'Ready for sending'
        WHEN  order_rows.status = 'sent' THEN 'Sent'
        ELSE 'Unknown'
    END AS status,
    order_rows.data->>'commission' AS commission_percent,
    round((order_rows.data->>'height')::float) AS commission_amount,
    order_rows.data->>'discountType' AS discount_type,
    order_rows.data->>'discountValue' AS discount_value,
    CASE
        WHEN order_rows.data->>'discountType' <> '%' THEN (order_rows.data->>'discountValue')::float / orders.exchange_rate
        ELSE NULL::float
    END AS discount_amount_sek,
    orders.currency::TEXT AS order_currency,
    order_rows.data->>'designer' AS designer,
    (order_rows.price / orders.exchange_rate)::float AS item_value_sek,
    order_rows.data->>'framed' AS canvas_frame,
    order_rows.data->>'posterHanger' as poster_hanger,
    order_rows.data->>'frame_color' as framed_print_frame,
    order_rows.price::float AS price,
    orders.delivery_vat AS vat
   FROM order_rows
     LEFT JOIN orders ON orders.id = order_rows.order_id
     WHERE order_rows.type in ('print', 'stock');


GRANT SELECT ON v_analytics_order_rows to datastudio;


--

DROP VIEW IF EXISTS v_analytics_orders_not_delivered;
CREATE VIEW v_analytics_orders_not_delivered AS
 SELECT
    CASE
       WHEN orders.market IS NULL THEN 'Unknown'
       WHEN orders.market = 'GB' THEN 'Photowall UK'
       WHEN orders.market = 'GLOBAL' THEN 'Photowall OTHER'
       ELSE 'Photowall ' || orders.market
    END AS account,
    orders.inserted::date AS orderdate,
    orders.id as orderid,
    md5(billing_address.first_name || 'secret') AS first_name,
    md5(billing_address.last_name || 'secret') AS last_name,
    md5(billing_address.company_name || 'secret') AS company,
    md5(billing_address.address1 || 'secret') AS address,
    billing_address.zipcode AS postal_code,
    billing_address.city,
    billing_address.country_code AS country,
    md5(billing_address.email || 'secret') AS email,
    order_sum::float / exchange_rate::float AS order_value_sek
   FROM orders
     LEFT JOIN addresses billing_address ON billing_address.id = orders.billing_address_id
  WHERE confirmed = 1 AND delivered = 0 AND canceled = 0 AND paid = 1;

GRANT SELECT ON v_analytics_orders_not_delivered to datastudio;


--

DROP VIEW IF EXISTS v_analytics_orders_rows_not_delivered;
CREATE VIEW v_analytics_orders_rows_not_delivered AS
  SELECT order_rows.id AS order_row_id,
        orders.id as orderid,
        orders.inserted AS order_date,
        order_rows.data->>'group' AS product_group,
        order_rows.data->>'artNo' AS artno,
        order_rows.status::TEXT AS status,
        order_rows.data->>'name' AS name,
        order_rows.data->>'material' AS material
    FROM order_rows
    JOIN orders ON order_rows.order_id = orders.id
    WHERE order_rows.type in ('print', 'stock')
    AND orders.confirmed = 1
    AND orders.delivered = 0
    AND orders.canceled = 0
    AND orders.paid = 1;
GRANT SELECT ON v_analytics_orders_rows_not_delivered to datastudio;


-- Update view
CREATE OR REPLACE VIEW public.v_analytics_orders2 AS
 SELECT orders.id AS order_id,
    orders.inserted AS order_date,
    orders.delivered,
    orders.newsletter,
    orders.credit_invoice,
    orders.canceled,
    orders.reminder,
    orders.delivered_date,
    orders.country_code,
    orders.language,
    orders.delivery_country_code,
    orders.delivery_method,
    orders.delivery_price,
    orders.customer_type,
    orders.payment_type,
    orders.currency,
    orders.exchange_rate,
    orders.exchange_rate_eur,
    orders.invoice_id,
    orders.contract_customer_id,
    orders.vat_number,
    orders.cancels_order_id,
    orders.cancelled_by_order_id,
    orders.reseller_store,
    orders.order_sum,
    orders.order_sum / orders.exchange_rate AS order_sum_sek,
    billing_address.email,
    billing_address.phone,
    billing_address.address1 AS billing_address_address1,
    billing_address.address2 AS billing_address_address2,
    billing_address.company_name AS billing_address_company_name,
    billing_address.country_code AS billing_address_country_code,
    billing_address.city AS billing_address_city,
    billing_address.zipcode AS billing_address_zipcode,
    billing_address.first_name AS billing_address_first_name,
    billing_address.last_name AS billing_address_last_name,
    billing_address.state AS billing_address_state,
    delivery_address.address1 AS delivery_address_address1,
    delivery_address.address2 AS delivery_address_address2,
    delivery_address.company_name AS delivery_address_company_name,
    delivery_address.country_code AS delivery_address_country_code,
    delivery_address.city AS delivery_address_city,
    delivery_address.zipcode AS delivery_address_zipcode,
    delivery_address.first_name AS delivery_address_first_name,
    delivery_address.last_name AS delivery_address_last_name,
    delivery_address.state AS delivery_address_state
   FROM orders
     JOIN addresses billing_address ON billing_address.id = orders.billing_address_id
     LEFT JOIN addresses delivery_address ON delivery_address.id = orders.delivery_address_id
  WHERE orders.paid = 1;

GRANT SELECT ON v_analytics_orders2 TO datastudio;

-- Not used
-- DROP VIEW IF EXISTS v_analytics_orders;
-- DROP VIEW IF EXISTS v_order_rows;
-- DROP VIEW IF EXISTS v_orders;
-- DROP MATERIALIZED VIEW IF EXISTS v_sales_report;
-- DROP MATERIALIZED VIEW IF EXISTS test;
