-- P5-5418: remove ready_for_handling from v_analytics_orders_rows_not_delivered

DROP VIEW IF EXISTS v_analytics_orders_rows_not_delivered;

CREATE VIEW v_analytics_orders_rows_not_delivered AS

SELECT rows.rowid AS order_row_id,
    orders.orderid,
    orders.inserted AS order_date,
    product_group.value AS product_group,
    artno.value AS artno,
    status.value AS status,
    name.value AS name,
    material.value AS material
FROM "order-rows" rows
 JOIN "order-orders" orders ON rows.orderid = orders.orderid
 LEFT JOIN "order-orders_fields" order_confirmed ON orders.orderid = order_confirmed.orderid AND order_confirmed.fieldid = 175
 LEFT JOIN "order-orders_fields" order_delivered ON orders.orderid = order_delivered.orderid AND order_delivered.fieldid = 176
 LEFT JOIN "order-orders_fields" order_canceled ON orders.orderid = order_canceled.orderid AND order_canceled.fieldid = 200
 LEFT JOIN "order-orders_fields" order_paid ON orders.orderid = order_paid.orderid AND order_paid.fieldid = 168
 LEFT JOIN "order-rows_details" product_group ON rows.rowid = product_group.rowid AND product_group.fieldid = 93
 LEFT JOIN "order-rows_details" artno ON rows.rowid = artno.rowid AND artno.fieldid = 95
 LEFT JOIN "order-rows_details" status ON rows.rowid = status.rowid AND status.fieldid = 108
 LEFT JOIN "order-rows_details" name ON rows.rowid = name.rowid AND name.fieldid = 97
 LEFT JOIN "order-rows_details" modifier ON rows.rowid = modifier.rowid AND modifier.fieldid = 113
 LEFT JOIN "order-rows_details" material ON rows.rowid = material.rowid AND material.fieldid = 131
WHERE modifier.value IS NULL AND order_confirmed.value = 'true'::text AND order_delivered.value = 'false'::text AND order_canceled.value IS NULL AND order_paid.value = 'true'::text;


GRANT SELECT ON v_analytics_orders_not_delivered TO datastudio;
