-- Revert to the previous version of v_funnel_orders

-- Note that the current view definition exists in 000.515.sql, if we want to reapply it.

-- Below is copied as is from previous version 000.505.sql:
DROP VIEW public.v_funnel_orders;

CREATE VIEW public.v_funnel_orders
 AS
 SELECT orders.id AS order_id,
    orders.inserted AS order_date,
    orders.delivered,
    orders.newsletter,
    orders.credit_invoice,
    orders.canceled,
    orders.delivered_date,
    orders.country_code,
    orders.language,
    orders.delivery_country_code,
    orders.delivery_method,
    orders.delivery_price,
    orders.delivery_vat AS vat,
    orders.customer_type,
    COALESCE(pm.payment_method, orders.payment_type) AS 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.order_sum,
    orders.order_sum / orders.exchange_rate AS order_sum_sek,
    orders.market,
    orders.locale,
    orders.attention,
    orders.gdpr_removal,
    encode(digest(billing_address.first_name::text, 'sha256'::text), 'hex'::text) AS billing_address_first_name,
    encode(digest(billing_address.last_name::text, 'sha256'::text), 'hex'::text) AS billing_address_last_name,
    encode(digest(billing_address.email, 'sha256'::text), 'hex'::text) AS email,
    encode(digest(billing_address.phone, 'sha256'::text), 'hex'::text) AS phone,
    encode(digest(billing_address.address1::text, 'sha256'::text), 'hex'::text) AS billing_address_address1,
    encode(digest(billing_address.address2::text, 'sha256'::text), 'hex'::text) 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.state AS billing_address_state,
    encode(digest(delivery_address.first_name::text, 'sha256'::text), 'hex'::text) AS delivery_address_first_name,
    encode(digest(delivery_address.last_name::text, 'sha256'::text), 'hex'::text) AS delivery_address_last_name,
    encode(digest(delivery_address.address1::text, 'sha256'::text), 'hex'::text) AS delivery_address_address1,
    encode(digest(delivery_address.address2::text, 'sha256'::text), 'hex'::text) 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.state AS delivery_address_state
   FROM orders
     LEFT JOIN ( SELECT adyen_sessions.order_id,
            (((adyen_sessions.authorisation_webhook::jsonb -> 'notificationItems'::text) -> 0) -> 'NotificationRequestItem'::text) ->> 'paymentMethod'::text AS payment_method
           FROM adyen_sessions
             JOIN orders orders_1 ON adyen_sessions.order_id = orders_1.id
          WHERE adyen_sessions.order_id IS NOT NULL) pm ON orders.id = pm.order_id
     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 TABLE public.v_funnel_orders TO funnel;
