CREATE OR REPLACE VIEW public.v_analytics_orders_rows_not_delivered
 AS
 SELECT order_rows.id AS order_row_id,
    orders.id AS orderid,
    orders.inserted AS order_date,
    CASE type
        WHEN 'sample' THEN 'sample'
        ELSE order_rows.data ->> 'group'::text
    END AS product_group,
    order_rows.data ->> 'artNo'::text AS artno,
    order_rows.status::text AS status,
    order_rows.data ->> 'name'::text AS name,
    order_rows.data ->> 'material'::text AS material
   FROM order_rows
     JOIN orders ON order_rows.order_id = orders.id
  WHERE (order_rows.type = ANY (ARRAY['print'::order_row_type, 'stock'::order_row_type, 'sample'::order_row_type])) AND orders.confirmed = 1 AND orders.delivered = 0 AND orders.canceled = 0 AND orders.paid = 1;

GRANT SELECT ON TABLE public.v_analytics_orders_rows_not_delivered TO datastudio;
