DROP VIEW IF EXISTS public.v_funnel_discounts;

CREATE VIEW public.v_funnel_discounts
 AS
 SELECT order_rows.order_id AS orderid,
    order_rows.id AS rowid,
    order_rows.data ->> 'type'::text AS type,
    orders.delivery_vat AS vat,
    abs(order_rows.price / orders.exchange_rate)::double precision AS price_sek,
    order_rows.data ->> 'code'::text AS code,
    discount_usage_type,
    order_rows.data ->> 'no_shipping'::text AS no_shipping
   FROM order_rows
     JOIN orders ON orders.id = order_rows.order_id
     LEFT JOIN discount_codes dc ON dc.code = order_rows.data ->> 'code'::text
  WHERE order_rows.sub_type = 'discount_code'::order_row_sub_type;

GRANT SELECT ON TABLE public.v_funnel_discounts TO funnel;
