|
@@ -0,0 +1,138 @@
|
|
|
+SELECT
|
|
|
+ 'tab' AS component
|
|
|
+;
|
|
|
+
|
|
|
+SELECT
|
|
|
+ 'Products' AS title,
|
|
|
+ 'products.sql' AS link
|
|
|
+UNION SELECT
|
|
|
+ 'Categories' AS title,
|
|
|
+ 'categories.sql' AS link
|
|
|
+UNION SELECT
|
|
|
+ 'Groups' AS title,
|
|
|
+ 'groups.sql' AS link
|
|
|
+UNION SELECT
|
|
|
+ 'Transactions' AS title,
|
|
|
+ 'transactions.sql' AS link
|
|
|
+
|
|
|
+CREATE TEMPORARY TABLE IF NOT EXISTS filter(
|
|
|
+ p text, fp text,
|
|
|
+ c text, fc text,
|
|
|
+ g text, fg text
|
|
|
+);
|
|
|
+DELETE FROM filter;
|
|
|
+INSERT INTO filter
|
|
|
+SELECT
|
|
|
+ p.name, fp.v#>>'{}',
|
|
|
+ c.name, fc.v#>>'{}',
|
|
|
+ g.name, fg.v#>>'{}'
|
|
|
+FROM groups g
|
|
|
+FULL JOIN
|
|
|
+ json_array_elements($groups::json) fg(v)
|
|
|
+ON g.name = fg.v#>>'{}'
|
|
|
+JOIN categories c
|
|
|
+ON c.group_id = g.id
|
|
|
+FULL JOIN
|
|
|
+ json_array_elements($categories::json) fc(v)
|
|
|
+ON c.name = fc.v#>>'{}'
|
|
|
+JOIN products p
|
|
|
+ON p.category_id = c.id
|
|
|
+FULL JOIN
|
|
|
+ json_array_elements($products::json) fp(v)
|
|
|
+ON p.name = fp.v#>>'{}'
|
|
|
+WHERE
|
|
|
+ (fg.v IS NOT NULL OR $groups IS NULL)
|
|
|
+AND
|
|
|
+ (fc.v IS NOT NULL OR $categories IS NULL)
|
|
|
+AND
|
|
|
+ (fp.v IS NOT NULL OR $products IS NULL)
|
|
|
+;
|
|
|
+
|
|
|
+SELECT
|
|
|
+ 'form' AS component,
|
|
|
+ 'filter' AS id,
|
|
|
+ 'get' AS method,
|
|
|
+ 'Filter' AS title,
|
|
|
+ 'Apply' AS validate
|
|
|
+;
|
|
|
+
|
|
|
+SELECT
|
|
|
+ 'Action' AS label,
|
|
|
+ 'Refresh' AS value,
|
|
|
+ 'submit' AS type,
|
|
|
+ 'transactions.sql' AS formaction
|
|
|
+;
|
|
|
+
|
|
|
+SELECT
|
|
|
+ 'products[]' AS name,
|
|
|
+ 'Product' AS label,
|
|
|
+ 'select' AS "type",
|
|
|
+ TRUE AS dropdown,
|
|
|
+ TRUE AS multiple,
|
|
|
+ json_agg(json_build_object(
|
|
|
+ 'label', COALESCE(f.p, f.fp),
|
|
|
+ 'value', COALESCE(f.p, f.fp),
|
|
|
+ 'selected', f.fp <> ''
|
|
|
+ )) AS options
|
|
|
+FROM filter f
|
|
|
+;
|
|
|
+SELECT
|
|
|
+ 'categories[]' AS name,
|
|
|
+ 'Categories' AS label,
|
|
|
+ 'select' AS "type",
|
|
|
+ TRUE AS dropdown,
|
|
|
+ TRUE AS multiple,
|
|
|
+ json_agg(json_build_object(
|
|
|
+ 'label', COALESCE(f.c, f.fp),
|
|
|
+ 'value', COALESCE(f.c, f.fp),
|
|
|
+ 'selected', f.fc <> ''
|
|
|
+ )) AS options
|
|
|
+FROM filter f
|
|
|
+;
|
|
|
+SELECT
|
|
|
+ 'groups[]' AS name,
|
|
|
+ 'Groups' AS label,
|
|
|
+ 'select' AS "type",
|
|
|
+ TRUE AS dropdown,
|
|
|
+ TRUE AS multiple,
|
|
|
+ json_agg(json_build_object(
|
|
|
+ 'label', COALESCE(f.g, f.fg),
|
|
|
+ 'value', COALESCE(f.g, f.fg),
|
|
|
+ 'selected', f.fg <> ''
|
|
|
+ )) AS options
|
|
|
+FROM filter f
|
|
|
+;
|
|
|
+SELECT
|
|
|
+ 'table' AS component,
|
|
|
+ TRUE AS sort,
|
|
|
+ TRUE AS search,
|
|
|
+ TRUE AS striped_rows,
|
|
|
+ TRUE AS small,
|
|
|
+ 'Price' AS align_right,
|
|
|
+ 'Quantity' AS align_right,
|
|
|
+ 'Unit' AS align_right
|
|
|
+;
|
|
|
+
|
|
|
+
|
|
|
+SELECT
|
|
|
+ to_char(
|
|
|
+ (ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland', --COALESCE('$ParameterTZ', 'Pacific/Auckland'),
|
|
|
+ 'Mon DD HH24:MI:SS'
|
|
|
+ ) AS Time,
|
|
|
+ description AS Description,
|
|
|
+ stores.code AS Store,
|
|
|
+ products.name AS Product,
|
|
|
+ categories.name AS Category,
|
|
|
+ groups.name AS Group,
|
|
|
+ CASE WHEN organic THEN 'yes' ELSE 'no' END AS Organic,
|
|
|
+ to_char(price, 'FM999999999.00') AS "Price",
|
|
|
+ --to_char(quantity, '9.'||repeat('9',3-1)||'EEEE') AS Quantity,
|
|
|
+ to_char(price, 'FM999999999.000') AS "Quantity",
|
|
|
+ units.name AS "Unit"
|
|
|
+FROM transactions t
|
|
|
+JOIN products ON (product_id = products.id)
|
|
|
+JOIN categories ON (category_id = categories.id)
|
|
|
+JOIN groups ON (group_id = groups.id)
|
|
|
+JOIN stores ON (store_id = stores.id)
|
|
|
+JOIN units ON (t.unit_id = units.id)
|
|
|
+LIMIT 100;
|