|
@@ -7,6 +7,8 @@
|
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
import pandas as pd
|
|
|
from txn_view import get_statement as get_session_transactions_statement
|
|
|
+from dateutil.parser import parse as parse_time
|
|
|
+from datetime import datetime
|
|
|
try:
|
|
|
import psycopg2
|
|
|
from psycopg2.sql import SQL
|
|
@@ -14,6 +16,9 @@ try:
|
|
|
MOCK = False
|
|
|
except:
|
|
|
from faker import Faker
|
|
|
+ def cursor_as_dict(cur):
|
|
|
+ yield from cur.fetchall()
|
|
|
+
|
|
|
Faker.seed(4321)
|
|
|
fake = Faker()
|
|
|
MOCK = True
|
|
@@ -114,7 +119,6 @@ import urwid
|
|
|
from urwid import numedit
|
|
|
from decimal import Decimal
|
|
|
import time
|
|
|
-import datetime
|
|
|
import itertools
|
|
|
from collections import (
|
|
|
OrderedDict,
|
|
@@ -135,7 +139,7 @@ class mock_cur(object):
|
|
|
pass
|
|
|
|
|
|
def fetchall(self):
|
|
|
- yield from self.records
|
|
|
+ yield from (i for i in self.records)
|
|
|
|
|
|
def close(self):
|
|
|
pass
|
|
@@ -150,12 +154,16 @@ except:
|
|
|
|
|
|
if MOCK:
|
|
|
records = [{
|
|
|
- 'ts': datetime.datetime(2021, 8, 29, 14, 8, 0, 0),
|
|
|
+ 'ts': datetime(2021, 8, 29, 14, 8, 0, 0),
|
|
|
+ 'id': 2,
|
|
|
+ 'code': fake.store()[:4],
|
|
|
'store': 'Countdown',
|
|
|
'price': 4.30,
|
|
|
+ '$/unit': 4.3/250.0,
|
|
|
'quantity': 250.0,
|
|
|
'unit': 'g',
|
|
|
'organic': False,
|
|
|
+ 'total': 0,
|
|
|
'description': 'Whittakers',
|
|
|
'product': 'Dark Chocolate',
|
|
|
'group': 'Treats',
|
|
@@ -176,12 +184,16 @@ if MOCK:
|
|
|
words.append('organic')
|
|
|
|
|
|
records.append({
|
|
|
- 'ts': datetime.datetime(2021, 8, 29, 14, 8, 0, 0),
|
|
|
+ 'ts': datetime(2021, 8, 29, 14, 8, 0, 0),
|
|
|
+ 'id': 3,
|
|
|
+ 'code': fake.store()[:4],
|
|
|
'store': fake.store(),
|
|
|
'price': price,
|
|
|
+ '$/unit': price/quantity,
|
|
|
'quantity': quantity,
|
|
|
'unit': unit,
|
|
|
'organic': organic,
|
|
|
+ 'total': 0,
|
|
|
'description': ' '.join([
|
|
|
product[0],
|
|
|
*fake.description(ext_word_list=words).split()
|
|
@@ -190,10 +202,7 @@ if MOCK:
|
|
|
'group': product[2],
|
|
|
'category': product[1],
|
|
|
})
|
|
|
- col_idx_map = dict([ ( k, idx ) for idx,k in enumerate(sorted(records[0].keys())) ])
|
|
|
- records = [
|
|
|
- [ r[k] for k in sorted(r.keys()) ] for r in records
|
|
|
- ]
|
|
|
+
|
|
|
conn = mock_conn()
|
|
|
cur = mock_cur(records)
|
|
|
else:
|
|
@@ -254,16 +263,19 @@ display = lambda data, name: display_map[name](data) if name in display_map else
|
|
|
cur.execute("BEGIN")
|
|
|
|
|
|
def get_session_transactions(date, store):
|
|
|
- #print(cur.mogrify(get_session_transactions_statement(date,store,full_name=True)).decode("utf-8"))
|
|
|
+ statement = get_session_transactions_statement(parse_time(date), store, full_name=True, exact_time=True)
|
|
|
+ #print(cur.mogrify(statement).decode("utf-8"))
|
|
|
#input()
|
|
|
- cur.execute(get_session_transactions_statement(date,store,full_name=True))
|
|
|
+ cur.execute(statement)
|
|
|
df = pd.DataFrame(cursor_as_dict(cur))
|
|
|
+ if df.empty:
|
|
|
+ return ''
|
|
|
return df.drop(labels=[
|
|
|
'id', 'ts', 'store', 'code',
|
|
|
], axis=1).to_string(header=[
|
|
|
'Description', 'Volume', 'Unit', 'Price', '$/unit', 'Total',
|
|
|
'Group', 'Category', 'Product', 'Organic',
|
|
|
- ], justify='justify-all', max_colwidth=36, index=False)
|
|
|
+ ], justify='justify-all', max_colwidth=38, index=False)
|
|
|
|
|
|
def get_transactions_statement():
|
|
|
return SQL("SELECT * FROM transaction_view")
|