|
@@ -13,14 +13,24 @@ from psycopg.sql import (
|
|
SQL,
|
|
SQL,
|
|
Literal,
|
|
Literal,
|
|
)
|
|
)
|
|
-from .util import get_select, get_from
|
|
|
|
|
|
+from .util import get_select, get_from, get_groupby
|
|
ALIAS_TO_TABLE = {
|
|
ALIAS_TO_TABLE = {
|
|
'product': 'products',
|
|
'product': 'products',
|
|
'category': 'categories',
|
|
'category': 'categories',
|
|
'group': 'groups',
|
|
'group': 'groups',
|
|
- #'unit': 'units',
|
|
|
|
- #'store': 'stores',
|
|
|
|
|
|
+ 'tags': 'tags',
|
|
|
|
+ 'unit': 'units',
|
|
|
|
+ 'store': 'stores',
|
|
}
|
|
}
|
|
|
|
+NON_IDENTIFIER_COLUMNS = [
|
|
|
|
+ 'ts',
|
|
|
|
+ 'store',
|
|
|
|
+ 'quantity',
|
|
|
|
+ 'unit',
|
|
|
|
+ 'price',
|
|
|
|
+ 'organic',
|
|
|
|
+ 'tags',
|
|
|
|
+]
|
|
|
|
|
|
def get_table_statement(alias):
|
|
def get_table_statement(alias):
|
|
tables = ALIAS_TO_TABLE
|
|
tables = ALIAS_TO_TABLE
|
|
@@ -37,9 +47,9 @@ def get_transactions_statement(name, **kwargs):
|
|
key=Identifier(ALIAS_TO_TABLE[k], 'name'),
|
|
key=Identifier(ALIAS_TO_TABLE[k], 'name'),
|
|
condition=SQL('ILIKE' if k == name else '='),
|
|
condition=SQL('ILIKE' if k == name else '='),
|
|
value=Literal(f'%{v}%' if k == name else v),
|
|
value=Literal(f'%{v}%' if k == name else v),
|
|
- ) if k in ALIAS_TO_TABLE and v else SQL('TRUE'))
|
|
|
|
|
|
+ ) if k in ALIAS_TO_TABLE and (k not in NON_IDENTIFIER_COLUMNS or k == name) and v else SQL('TRUE'))
|
|
statement = SQL('\n').join([
|
|
statement = SQL('\n').join([
|
|
- get_select(SELECT),
|
|
|
|
|
|
+ get_select(dict([ (k,v) for k,v in SELECT.items() if k != 'tags'])),
|
|
get_from("transactions", JOINS),
|
|
get_from("transactions", JOINS),
|
|
SQL('').join([SQL('WHERE '), SQL('\n AND ').join(where)]),
|
|
SQL('').join([SQL('WHERE '), SQL('\n AND ').join(where)]),
|
|
])
|
|
])
|
|
@@ -66,14 +76,32 @@ OVER (
|
|
('category', Identifier('categories', 'name')),
|
|
('category', Identifier('categories', 'name')),
|
|
('product', Identifier('products', 'name')),
|
|
('product', Identifier('products', 'name')),
|
|
('organic', Identifier('organic')),
|
|
('organic', Identifier('organic')),
|
|
- # need a group by to aggregate
|
|
|
|
- #('tags', SQL("""array_agg({tag_name})""").format(
|
|
|
|
- # tag_name=Identifier('tags','name'),
|
|
|
|
- #))
|
|
|
|
- ('tags', Identifier('tags','name')),
|
|
|
|
|
|
+ ('tags', SQL(
|
|
|
|
+ """array_agg({tag_name}) FILTER (WHERE {tag_name} IS NOT NULL)"""
|
|
|
|
+ ).format(
|
|
|
|
+ tag_name=Identifier('tags','name'),
|
|
|
|
+ ))
|
|
|
|
+
|
|
|
|
+])
|
|
|
|
|
|
|
|
+GROUPBY = OrderedDict([
|
|
|
|
+ ('id', Identifier('transactions', 'id')),
|
|
|
|
+ ('ts', Identifier('transactions', 'ts')),
|
|
|
|
+ ('store', Identifier('stores', 'name')),
|
|
|
|
+ ('code', Identifier('stores', 'code')),
|
|
|
|
+ ('description', Identifier('transactions', 'description')),
|
|
|
|
+ ('volume', Identifier('quantity')),
|
|
|
|
+ ('unit', Identifier('units', 'name')),
|
|
|
|
+ ('price', Identifier('price')),
|
|
|
|
+ ('quantity', Identifier('quantity')),
|
|
|
|
+ ('group', Identifier('groups', 'name')),
|
|
|
|
+ ('category', Identifier('categories', 'name')),
|
|
|
|
+ ('product', Identifier('products', 'name')),
|
|
|
|
+ ('organic', Identifier('organic')),
|
|
])
|
|
])
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
JOINS = OrderedDict([
|
|
JOINS = OrderedDict([
|
|
('units', ('id', 'unit_id')),
|
|
('units', ('id', 'unit_id')),
|
|
('stores', ('id', 'store_id')),
|
|
('stores', ('id', 'store_id')),
|
|
@@ -125,6 +153,7 @@ def get_session_transactions_statement(date, store, full_name=False, exact_time=
|
|
full_name=full_name,
|
|
full_name=full_name,
|
|
exact_time=exact_time
|
|
exact_time=exact_time
|
|
),
|
|
),
|
|
|
|
+ get_groupby(GROUPBY),
|
|
get_sort(),
|
|
get_sort(),
|
|
])
|
|
])
|
|
return statement
|
|
return statement
|