diff --git a/migrations/000.362.sql b/migrations/000.362.sql new file mode 100644 index 0000000..0846c37 --- /dev/null +++ b/migrations/000.362.sql @@ -0,0 +1,90 @@ +DROP VIEW IF EXISTS public.v_funnel_orders; + +CREATE OR REPLACE 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, + 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.order_sum, + orders.order_sum / orders.exchange_rate AS order_sum_sek, + orders.market, + orders.locale, + md5((billing_address.email || 'funnelsecret'::text)) AS email, + md5((billing_address.phone || 'funnelsecret'::text)) AS phone, + md5((billing_address.address1 || 'funnelsecret'::text)) AS billing_address_address1, + md5((billing_address.address2 || 'funnelsecret'::text)) AS billing_address_address2, + md5((billing_address.company_name || 'funnelsecret'::text)) 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, + md5((billing_address.first_name || 'funnelsecret'::text)) AS billing_address_first_name, + md5((billing_address.last_name || 'funnelsecret'::text)) AS billing_address_last_name, + billing_address.state AS billing_address_state, + md5((delivery_address.address1 || 'funnelsecret'::text)) AS delivery_address_address1, + md5((delivery_address.address2 || 'funnelsecret'::text)) AS delivery_address_address2, + md5((delivery_address.company_name || 'funnelsecret'::text)) 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, + md5((delivery_address.first_name || 'funnelsecret'::text)) AS delivery_address_first_name, + md5((delivery_address.last_name || 'funnelsecret'::text)) 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; + +DROP VIEW IF EXISTS public.v_funnel_order_rows; + CREATE VIEW v_funnel_order_rows AS + SELECT + r.id, + r.order_id, + r.inserted, + r.type, + r.sub_type, + r.status, + r.price, + r.commission_amount, + r.external_status, + r.data->>'productId' AS product_id, + r.data->>'material' AS material, + r.data->>'artNo' AS artNo, + r.data->>'designer' AS designer + FROM order_rows r; + +DROP VIEW IF EXISTS public.v_funnel_discounts; +CREATE VIEW v_funnel_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_funnel_orders TO funnel; +GRANT SELECT ON v_funnel_order_rows TO funnel; +GRANT SELECT ON v_funnel_discounts TO funnel;