Add discount data to v_funnel_order_rows (#429)
* Initial scaffolding * Add discount data to v_funnel_order_rows * Display discounts for each row where applicable * Get discount/sales info from subquery instead * Use generated view to simplify diff * Try another approach * Use inline queries to get discount data * Re-add lost printId to view * Re-add v_funnel_order_rows.discount * Remove dead code * Grant SELECT for funnel on v_funnel_order_rows * Disregard discount calculations for refunded order rows * Bump migration version
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
-- View: public.v_funnel_order_rows
|
||||
|
||||
DROP VIEW public.v_funnel_order_rows;
|
||||
|
||||
CREATE OR REPLACE VIEW public.v_funnel_order_rows
|
||||
AS
|
||||
SELECT r.id,
|
||||
r.order_id,
|
||||
r.inserted,
|
||||
r.type,
|
||||
o.customer_type,
|
||||
o.market,
|
||||
pp.path,
|
||||
pp.publishing_date,
|
||||
pp.ref1,
|
||||
pp.ref2,
|
||||
COALESCE(((r.data ->> 'width_mm'::text)::numeric)::integer / 10, (r.data ->> 'width'::text)::integer) AS width,
|
||||
COALESCE(((r.data ->> 'height_mm'::text)::numeric)::integer / 10, (r.data ->> 'height'::text)::integer) AS height,
|
||||
r.sub_type,
|
||||
r.status,
|
||||
r.price,
|
||||
r.commission_amount,
|
||||
r.external_status,
|
||||
r.data ->> 'printId'::text AS print_id,
|
||||
r.data ->> 'productId'::text AS product_id,
|
||||
r.data ->> 'material'::text AS material,
|
||||
r.data ->> 'artNo'::text AS artno,
|
||||
r.data ->> 'designer'::text AS designer,
|
||||
(r.data ->> 'inquiryid'::text)::integer AS inquiry_id,
|
||||
CASE r.type
|
||||
WHEN 'refund'::order_row_type THEN COALESCE((r.data ->> 'discount_amount'::text)::numeric, 0::numeric)
|
||||
ELSE 0::numeric - COALESCE((r.data ->> 'discount_amount'::text)::numeric, 0::numeric)
|
||||
END AS discount,
|
||||
CASE r.type
|
||||
WHEN 'refund'::order_row_type THEN NULL::numeric
|
||||
ELSE ( SELECT 0::numeric - ((order_rows.data ->> 'discount_amount'::text)::numeric)
|
||||
FROM order_rows
|
||||
WHERE order_rows.id = r.id AND NOT order_rows.sub_type = 'contract_customer_discount'::order_row_sub_type AND (EXISTS ( SELECT 1
|
||||
FROM order_rows s1
|
||||
WHERE order_rows.order_id = s1.order_id AND s1.sub_type = 'contract_customer_discount'::order_row_sub_type)) AND (order_rows.data ->> 'discount_amount'::text) IS NOT NULL)
|
||||
END AS discount_cc,
|
||||
( SELECT
|
||||
CASE
|
||||
WHEN order_rows.type = 'refund'::order_row_type THEN NULL::numeric
|
||||
ELSE 0::numeric - ((order_rows.data ->> 'discount_amount'::text)::numeric)
|
||||
END AS discount
|
||||
FROM order_rows
|
||||
WHERE order_rows.id = r.id AND NOT order_rows.type = 'discount'::order_row_type AND NOT (EXISTS ( SELECT 1
|
||||
FROM order_rows s1
|
||||
WHERE order_rows.order_id = s1.order_id AND s1.sub_type = 'contract_customer_discount'::order_row_sub_type)) AND (order_rows.data ->> 'discount_amount'::text) IS NOT NULL) AS discount_code,
|
||||
CASE
|
||||
WHEN r.type = 'refund'::order_row_type THEN NULL::numeric
|
||||
ELSE ( SELECT order_rows.price - (((order_rows.data -> 'applied_sale'::text) ->> 'original_unit_price_excl_vat'::text)::numeric) AS discount
|
||||
FROM order_rows
|
||||
WHERE order_rows.id = r.id AND (order_rows.data ->> 'applied_sale'::text) IS NOT NULL AND NOT (EXISTS ( SELECT 1
|
||||
FROM order_rows s1
|
||||
WHERE order_rows.order_id = s1.order_id AND s1.type = 'discount'::order_row_type)))
|
||||
END AS discount_sale,
|
||||
CASE r.type
|
||||
WHEN 'refund'::order_row_type THEN NULL::text
|
||||
ELSE r.data ->> 'segment'::text
|
||||
END AS segment,
|
||||
CASE r.type
|
||||
WHEN 'refund'::order_row_type THEN NULL::numeric
|
||||
ELSE (r.data ->> 'segment_price'::text)::numeric
|
||||
END AS segment_price,
|
||||
r.data ->> 'reprinted_order_row_id'::text AS reprinted_order_row_id
|
||||
FROM order_rows r
|
||||
JOIN orders o ON r.order_id = o.id
|
||||
LEFT JOIN "product-products" pp ON ((r.data ->> 'productId'::text)::integer) = pp.productid;
|
||||
|
||||
GRANT SELECT ON TABLE public.v_funnel_order_rows TO funnel;
|
||||
Reference in New Issue
Block a user