|
@@ -14,23 +14,36 @@ from psycopg.sql import (
|
|
|
Literal,
|
|
|
)
|
|
|
from .util import get_select, get_from
|
|
|
+ALIAS_TO_TABLE = {
|
|
|
+ 'product': 'products',
|
|
|
+ 'category': 'categories',
|
|
|
+ 'group': 'groups',
|
|
|
+ 'unit': 'units',
|
|
|
+ 'store': 'stores',
|
|
|
+}
|
|
|
|
|
|
def get_table_statement(alias):
|
|
|
- tables = {
|
|
|
- 'product': 'products',
|
|
|
- 'category': 'categories',
|
|
|
- 'group': 'groups',
|
|
|
- 'unit': 'units',
|
|
|
- 'store': 'stores',
|
|
|
- }
|
|
|
+ tables = ALIAS_TO_TABLE
|
|
|
return SQL("SELECT {column} AS {alias} FROM {table}").format(
|
|
|
column=Identifier(tables[alias], 'name'),
|
|
|
table=Identifier(tables[alias]),
|
|
|
alias=Identifier(alias),
|
|
|
)
|
|
|
|
|
|
-def get_transactions_statement():
|
|
|
- return SQL("SELECT * FROM transaction_view")
|
|
|
+def get_transactions_statement(name, **kwargs):
|
|
|
+ where = []
|
|
|
+ for k,v in kwargs.items():
|
|
|
+ where.append(SQL('{key} {condition} {value}').format(
|
|
|
+ key=Identifier(ALIAS_TO_TABLE[k], 'name'),
|
|
|
+ condition=SQL('ILIKE' if k == name else '='),
|
|
|
+ value=Literal(f'%{v}%' if k == name else v),
|
|
|
+ ) if k in ALIAS_TO_TABLE and v else SQL('TRUE'))
|
|
|
+ statement = SQL('\n').join([
|
|
|
+ get_select(SELECT),
|
|
|
+ get_from("transactions", JOINS),
|
|
|
+ SQL('').join([SQL('WHERE '), SQL('\n AND ').join(where)]),
|
|
|
+ ])
|
|
|
+ return statement
|
|
|
|
|
|
SELECT = OrderedDict([
|
|
|
('id', Identifier('transactions', 'id')),
|
|
@@ -41,6 +54,7 @@ SELECT = OrderedDict([
|
|
|
('volume', Identifier('quantity')),
|
|
|
('unit', Identifier('units', 'name')),
|
|
|
('price', Identifier('price')),
|
|
|
+ ('quantity', Identifier('quantity')),
|
|
|
('$/unit', SQL("""TRUNC(price/quantity,4)""")),
|
|
|
('total', SQL("""sum(transactions.price)
|
|
|
OVER (
|