Browse Source

refactored with all features restored

Daniel Sheffield 8 months ago
parent
commit
d080be2c8b
8 changed files with 125 additions and 120 deletions
  1. 9 0
      sqlpage/apply.sql
  2. 5 8
      sqlpage/clear.sql
  3. 23 33
      sqlpage/cookie.sql
  4. 20 14
      sqlpage/data/Transactions.sql
  5. 45 0
      sqlpage/entry.sql
  6. 22 57
      sqlpage/nav.sql
  7. 0 7
      sqlpage/products.sql
  8. 1 1
      sqlpage/tags.sql

+ 9 - 0
sqlpage/apply.sql

@@ -0,0 +1,9 @@
+SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
+SELECT 'redirect' AS component
+, (
+  SELECT CASE
+    WHEN $title IN ('Products', 'Categories', 'Groups', 'Tags')
+    THEN lower($title)||'.sql' ELSE 'transactions.sql'
+  END
+) AS link
+;

+ 5 - 8
sqlpage/clear.sql

@@ -3,11 +3,8 @@ SET categories = NULL;
 SET groups = NULL;
 SET groups = NULL;
 SET start = NULL;
 SET start = NULL;
 SET end = NULL;
 SET end = NULL;
-SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
-SELECT
-  'redirect' AS component,
-  (SELECT CASE
-    WHEN $title IN ('Products', 'Categories', 'Groups', 'Tags')
-    THEN lower($title)||'.sql' ELSE 'transactions.sql'
-    END
-  ) AS link;
+SET tags = NULL;
+SET unit_volume = NULL;
+SET unit_mass = NULL;
+SET unit_count = NULL;
+SELECT 'dynamic' AS component, sqlpage.run_sql('apply.sql') AS properties;

+ 23 - 33
sqlpage/cookie.sql

@@ -5,53 +5,43 @@ SELECT
 WHERE sqlpage.cookie('session') IS NULL
 WHERE sqlpage.cookie('session') IS NULL
 ;
 ;
 
 
-SELECT
-  'cookie' AS component,
-  'products' AS name,
-  $products AS value
-WHERE $products IS NOT NULL
-;
-SELECT
-  'cookie' AS component,
+SELECT 'cookie' AS component,
   'products' AS name,
   'products' AS name,
   $products AS value,
   $products AS value,
-  TRUE AS remove
-WHERE $products IS NULL
+  $products IS NULL AS remove
 ;
 ;
-SELECT
-  'cookie' AS component,
-  'categories' AS name,
-  $categories AS value
-WHERE $categories IS NOT NULL
-;
-SELECT
-  'cookie' AS component,
+SELECT 'cookie' AS component,
   'categories' AS name,
   'categories' AS name,
   $categories AS value,
   $categories AS value,
-  TRUE AS remove
-WHERE $categories IS NULL
+  $categories IS NULL AS remove
 ;
 ;
-SELECT
-  'cookie' AS component,
-  'groups' AS name,
-  $groups AS value
-WHERE $groups IS NOT NULL
-;
-SELECT
-  'cookie' AS component,
+SELECT 'cookie' AS component,
   'groups' AS name,
   'groups' AS name,
   $groups AS value,
   $groups AS value,
-  TRUE AS remove
-WHERE $groups IS NULL
+  $groups IS NULL AS remove
 ;
 ;
-
 SELECT 'cookie' AS component
 SELECT 'cookie' AS component
 , 'start' AS name
 , 'start' AS name
 , $start AS value
 , $start AS value
-, $start IS NULL AS remove
+, ($start IS NULL OR $start = '') AS remove
 ;
 ;
 SELECT 'cookie' AS component
 SELECT 'cookie' AS component
 , 'end' AS name
 , 'end' AS name
 , $end AS value
 , $end AS value
-, $end IS NULL AS remove
+, ($end IS NULL OR $end = '') AS remove
+;
+SELECT 'cookie' AS component
+, 'unit_volume' AS name
+, $unit_volume AS value
+, ($unit_volume IS NULL OR $unit_volume = '') AS remove
+;
+SELECT 'cookie' AS component
+, 'unit_mass' AS name
+, $unit_mass AS value
+, ($unit_mass IS NULL OR $unit_mass = '') AS remove
+;
+SELECT 'cookie' AS component
+, 'unit_count' AS name
+, $unit_count AS value
+, $unit_count IS NULL AS remove
 ;
 ;

+ 20 - 14
sqlpage/data/Transactions.sql

@@ -11,19 +11,20 @@ SELECT
   c.name AS "Category",
   c.name AS "Category",
   g.name AS "Group",
   g.name AS "Group",
   CASE WHEN t.organic THEN 'yes' ELSE 'no' END AS "Organic",
   CASE WHEN t.organic THEN 'yes' ELSE 'no' END AS "Organic",
-  to_char(t.price, 'FM999999999.00') AS "Price"
+  to_char(t.price, 'FM999999999.00') AS "Price",
   --to_char(quantity, '9.'||repeat('9',3-1)||'EEEE') AS Quantity,
   --to_char(quantity, '9.'||repeat('9',3-1)||'EEEE') AS Quantity,
-  -- to_char(CASE
-  -- WHEN ut.name = 'Count' THEN t.price
-  -- ELSE t.quantity * convert_unit(u.name, CASE
-  -- WHEN ut.name = 'Volume' THEN ouv.name
-  -- WHEN ut.name = 'Mass' THEN oum.name
-  -- END, p.name) END, 'FM999999999.000') AS "Quantity",
-  -- CASE
-  -- WHEN ut.name = 'Count' THEN u.name
-  -- WHEN ut.name = 'Volume' THEN ouv.name
-  -- WHEN ut.name = 'Mass' THEN oum.name
-  -- END AS "Unit"
+  to_char(CASE
+    WHEN ut.name = 'Count' THEN t.quantity
+    ELSE t.quantity * convert_unit(u.name, CASE
+      WHEN ut.name = 'Volume' THEN $unit_volume
+      WHEN ut.name = 'Mass' THEN $unit_mass
+    END, p.name)
+    END, 'FM999999999.000') AS "Quantity",
+  CASE
+    WHEN ut.name = 'Count' THEN u.name
+    WHEN ut.name = 'Volume' THEN $unit_volume
+    WHEN ut.name = 'Mass' THEN $unit_mass
+  END AS "Unit"
 FROM transactions t
 FROM transactions t
 JOIN products p ON (product_id = p.id)
 JOIN products p ON (product_id = p.id)
 JOIN categories c ON (category_id = c.id)
 JOIN categories c ON (category_id = c.id)
@@ -31,13 +32,18 @@ JOIN groups g ON (group_id = g.id)
 JOIN stores s ON (store_id = s.id)
 JOIN stores s ON (store_id = s.id)
 JOIN units u ON (t.unit_id = u.id)
 JOIN units u ON (t.unit_id = u.id)
 JOIN unit_types ut ON (u.unit_type_id = ut.id)
 JOIN unit_types ut ON (u.unit_type_id = ut.id)
---LEFT JOIN sqlpage_options ouv ON ouv.type = 'unit_volume' AND ut.name = 'Volume'
 WHERE
 WHERE
   (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
   (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
 AND
 AND
   (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
   (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
 AND
 AND
   (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
   (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
+AND
+  CASE
+    WHEN ut.name = 'Count'
+    THEN (u.name IN (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v)) OR $unit_count IS NULL)
+    ELSE TRUE
+  END
 AND
 AND
   ((ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland')::date
   ((ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland')::date
   BETWEEN $start::date AND $end::date
   BETWEEN $start::date AND $end::date
@@ -50,4 +56,4 @@ AND
     )
     )
   ) OR $tags IS NULL)
   ) OR $tags IS NULL)
 ORDER BY ts DESC
 ORDER BY ts DESC
-LIMIT 100);
+LIMIT 1000);

+ 45 - 0
sqlpage/entry.sql

@@ -1,6 +1,18 @@
 SET products = COALESCE($products, sqlpage.cookie('products'));
 SET products = COALESCE($products, sqlpage.cookie('products'));
 SET categories = COALESCE($categories, sqlpage.cookie('categories'));
 SET categories = COALESCE($categories, sqlpage.cookie('categories'));
 SET groups = COALESCE($groups, sqlpage.cookie('groups'));
 SET groups = COALESCE($groups, sqlpage.cookie('groups'));
+SET tags = COALESCE($tags, sqlpage.cookie('tags'));
+SET unit_volume = (CASE
+  WHEN $unit_volume = '' OR $unit_volume IS NULL
+  THEN COALESCE(sqlpage.cookie('unit_volume'), 'L')
+  ELSE $unit_volume
+END);
+SET unit_mass = (CASE
+  WHEN $unit_mass = '' OR $unit_mass IS NULL
+  THEN COALESCE(sqlpage.cookie('unit_mass'), 'kg')
+  ELSE $unit_mass
+END);
+SET unit_count = COALESCE($unit_count, sqlpage.cookie('unit_count'));
 SET start = (CASE
 SET start = (CASE
   WHEN $start = '' OR $start IS NULL
   WHEN $start = '' OR $start IS NULL
   THEN COALESCE(sqlpage.cookie('start'), to_char(now()-'30 days'::interval, 'YYYY-MM-DD'))
   THEN COALESCE(sqlpage.cookie('start'), to_char(now()-'30 days'::interval, 'YYYY-MM-DD'))
@@ -80,6 +92,39 @@ FROM (
 WHERE value IS NOT NULL
 WHERE value IS NOT NULL
 );
 );
 
 
+DROP TABLE IF EXISTS sqlpage_units;
+CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_units AS
+SELECT u.name, ut.name AS type FROM units u
+JOIN unit_types ut ON ut.id = u.unit_type_id
+;
+SET unit_volume_options = (
+SELECT json_agg(json_build_object(
+   'label', o.name,
+   'value', o.name,
+   'selected', o.name = $unit_volume
+ ) ORDER BY o.name)
+FROM sqlpage_units o
+WHERE type = 'Volume'
+);
+SET unit_mass_options = (
+SELECT json_agg(json_build_object(
+   'label', o.name,
+   'value', o.name,
+   'selected', o.name = $unit_mass
+ ) ORDER BY o.name)
+FROM sqlpage_units o
+WHERE type = 'Mass'
+);
+SET unit_count_options = (
+SELECT json_agg(json_build_object(
+   'label', o.name,
+   'value', o.name,
+   'selected', o.name IN (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v))
+ ) ORDER BY o.name)
+FROM sqlpage_units o
+WHERE type = 'Count'
+);
+
 SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('nav.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('nav.sql') AS properties;

+ 22 - 57
sqlpage/nav.sql

@@ -31,13 +31,9 @@ SELECT 'form' AS component
 , 'get' AS method
 , 'get' AS method
 , '' AS title
 , '' AS title
 , 'Apply' AS validate
 , 'Apply' AS validate
+, 'apply.sql' AS action
 ;
 ;
 
 
--- SELECT
---   'path' AS name,
---   sqlpage.path() AS value,
---   'hidden' AS type
--- ;
 SELECT 'hidden' AS type
 SELECT 'hidden' AS type
 , 'title' AS name
 , 'title' AS name
 , $title AS value
 , $title AS value
@@ -86,58 +82,27 @@ SELECT 'Tags' AS label
 , TRUE AS multiple
 , TRUE AS multiple
 , $tag_options AS options
 , $tag_options AS options
 ;
 ;
---SELECT
---  'unit_volume' AS name,
---  'Unit (vol.)' AS label,
---  1 AS width,
---  'select' AS type,
---  json_agg(json_build_object(
---    'label', o.name,
---    'value', o.name,
---    'selected', COALESCE(o.selected, FALSE)
---  ) ORDER BY o.name) AS options
---FROM sqlpage_options o
---WHERE
---  o.type = 'unit_volume'
---AND
---  o.session = sqlpage.cookie('session')
---;
---SELECT
---  'unit_mass' AS name,
---  'Unit (mass)' AS label,
---  1 AS width,
---  'select' AS type,
---  $unit_mass AS value,
---  json_agg(json_build_object(
---    'label', o.name,
---    'value', o.name,
---    'selected', COALESCE(o.selected, FALSE)
---  ) ORDER BY o.name) AS options
---FROM sqlpage_options o
---WHERE
---  o.type = 'unit_mass'
---AND
---  o.session = sqlpage.cookie('session')
---;
---SELECT
---  'unit_count[]' AS name,
---  'Unit (count)' AS label,
---  2 AS width,
---  'select' AS type,
---  TRUE AS multiple,
---  TRUE AS dropdown,
---  '' AS value,
---  json_agg(json_build_object(
---    'label', o.name,
---    'value', o.name,
---    'selected', COALESCE(o.selected, FALSE)
---  ) ORDER BY o.name) AS options
---FROM sqlpage_options o
---WHERE
---  o.type = 'unit_count'
---AND
---  o.session = sqlpage.cookie('session')
---;
+SELECT 'Unit (vol.)' AS label
+, 'unit_volume' AS name
+, 1 AS width
+, 'select' AS type
+, $unit_volume_options AS options
+;
+SELECT 'Unit (mass)' AS label
+, 'unit_mass' AS name
+, 1 AS width
+, 'select' AS type
+, $unit_mass AS value
+, $unit_mass_options AS options
+;
+SELECT 'Unit (count)' AS label
+, 'unit_count[]' AS name
+, 2 AS width
+, 'select' AS type
+, TRUE AS multiple
+, TRUE AS dropdown
+, '' AS value
+, $unit_count_options AS options;
 SELECT 'Clear' AS value
 SELECT 'Clear' AS value
 , 'clear' AS name
 , 'clear' AS name
 , '' AS label
 , '' AS label

+ 0 - 7
sqlpage/products.sql

@@ -1,9 +1,2 @@
 SET title = 'Products';
 SET title = 'Products';
 SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;
--- SELECT
---   'redirect' AS component,
---   (SELECT CASE
---     WHEN $title IN ('Products', 'Categories', 'Groups', 'Tags')
---     THEN 'entry.sql?title='||$title ELSE 'transactions.sql'
---     END
---   ) AS link;

+ 1 - 1
sqlpage/tags.sql

@@ -1,2 +1,2 @@
 SET title = 'Tags';
 SET title = 'Tags';
-SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;
+SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;