|
@@ -40,17 +40,37 @@ SELECT
|
|
|
CASE WHEN t.organic THEN 'yes' ELSE 'no' END AS "Organic",
|
|
|
to_char(t.price, 'FM999999999.00') AS "Price",
|
|
|
--to_char(quantity, '9.'||repeat('9',3-1)||'EEEE') AS Quantity,
|
|
|
- to_char(t.price, 'FM999999999.000') AS "Quantity",
|
|
|
- u.name AS "Unit"
|
|
|
+ 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"
|
|
|
FROM transactions t
|
|
|
JOIN products p ON (product_id = p.id)
|
|
|
JOIN categories c ON (category_id = c.id)
|
|
|
JOIN groups g ON (group_id = g.id)
|
|
|
JOIN stores s ON (store_id = s.id)
|
|
|
JOIN units u ON (t.unit_id = u.id)
|
|
|
+JOIN unit_types ut ON (u.unit_type_id = ut.id)
|
|
|
JOIN sqlpage_options op ON op.type = 'product' AND p.name = op.name
|
|
|
JOIN sqlpage_options oc ON oc.type = 'category' AND c.name = oc.name
|
|
|
JOIN sqlpage_options og ON og.type = 'group' AND g.name = og.name
|
|
|
+LEFT JOIN sqlpage_options ouv ON ouv.type = 'unit_volume' AND ut.name = 'Volume'
|
|
|
+AND
|
|
|
+ ouv.session = sqlpage.cookie('session')
|
|
|
+AND
|
|
|
+ COALESCE(ouv.selected, TRUE)
|
|
|
+LEFT JOIN sqlpage_options oum ON oum.type = 'unit_mass' AND ut.name = 'Mass'
|
|
|
+AND
|
|
|
+ oum.session = sqlpage.cookie('session')
|
|
|
+AND
|
|
|
+ COALESCE(oum.selected, TRUE)
|
|
|
WHERE
|
|
|
COALESCE(op.selected, oc.selected, og.selected, TRUE)
|
|
|
AND
|