Database schema change. 'Order' becomes 'Transaction'. 'OrderEntry' becomes 'Order'.

This commit is contained in:
crudo 2017-03-22 17:12:15 +01:00
parent 54e814e801
commit 27983883d7
3 changed files with 50 additions and 40 deletions

62
cli.py
View File

@ -4,7 +4,7 @@ from datetime import datetime
from pos.config import Config
from pos.logging import init_logging, get_logger
from pos.database import Database, User, Event, Product, Order
from pos.database import Database, User, Event, Product, Transaction
config = Config()
conf_db = config.core['DATABASE']
@ -62,9 +62,9 @@ def tabulate_events(events):
tab = [["UID", "Name", "Starts at", "Ends at", "Income", "Created at"]]
for e in events:
sum = 0
for order in e.orders:
for entry in order.entries:
sum += entry.product.price * entry.quantity
for t in e.transactions:
for o in t.orders:
sum += o.product.price * o.quantity
tab.append([e.uid, e.name, e.starts_at, e.ends_at, sum, e.created_at])
return tabulate(tab, headers='firstrow')
@ -128,37 +128,47 @@ def product_list():
print(tabulate_products(products))
@cli.group('order')
def order():
@cli.group('transaction')
def transaction():
pass
def tabulate_orders(orders):
text = []
tab = [["Product", "Price", "Quantity", "Total"]]
for o in orders:
text.append("Listing order #{} ({}):".format(o.uid, o.created_at))
tab = [["Product", "Quantity", "Total"]]
total = 0
for e in o.entries:
if e.quantity != 0:
tab.append([e.product.name, e.quantity,
e.product.price * e.quantity])
total += e.product.price * e.quantity
text.append(tabulate(tab, headers='firstrow'))
text.append("Total: {}".format(total))
text.append('\n')
return '\n'.join(text)
if o.quantity != 0:
tab.append([o.product.name, o.product.price, o.quantity,
o.product.price * o.quantity])
return tabulate(tab, headers='firstrow')
@order.command('list')
def order_list():
orders = None
def print_transactions(transactions):
sum = 0
for t in transactions:
print("Listing transaction #{} ({}):".format(t.uid, t.created_at))
print(tabulate_orders(t.orders))
tot = 0
for o in t.orders:
tot += o.product.price * o.quantity
sum += tot
print("Total: {}".format(tot))
print()
print("Total income: {}".format(sum))
print()
@transaction.command('list')
def transaction_list():
with db.get_session() as session:
orders = session.query(Order).all()
if len(orders) == 0:
print("No orders found.")
transactions = session.query(Transaction).all()
if len(transactions) == 0:
print("No transactions found.")
return
print(tabulate_orders(orders))
return print_transactions(transactions)
if __name__ == '__main__':

View File

@ -112,7 +112,7 @@ class Event(Base):
starts_at = Column(DateTime, nullable=False, default=datetime.now)
ends_at = Column(DateTime)
orders = relationship('Order', lazy='joined')
transactions = relationship('Transaction', lazy='joined')
class Product(Base):
@ -127,24 +127,24 @@ class Product(Base):
order_entry_association = Table(
'order_entry_associations', Base.metadata,
Column('order_uid', Integer, ForeignKey('orders.uid')),
Column('order_entry_uid', Integer, ForeignKey('order_entries.uid'))
Column('transaction_uid', Integer, ForeignKey('transactions.uid')),
Column('order_uid', Integer, ForeignKey('orders.uid'))
)
class Order(Base):
__tablename__ = 'orders'
class Transaction(Base):
__tablename__ = 'transactions'
uid = Column(Integer, primary_key=True)
created_at = Column(DateTime, nullable=False, default=datetime.now)
event_uid = Column(Integer, ForeignKey('events.uid'), nullable=False)
event = relationship('Event')
entries = relationship('OrderEntry', lazy='joined',
orders = relationship('Order', lazy='joined',
secondary=order_entry_association)
class OrderEntry(Base):
__tablename__ = 'order_entries'
class Order(Base):
__tablename__ = 'orders'
uid = Column(Integer, primary_key=True)
product_uid = Column(Integer, ForeignKey('products.uid'), nullable=False)
quantity = Column(Integer, nullable=False)

10
web.py
View File

@ -5,7 +5,7 @@ from flask_login import LoginManager, login_user, logout_user, login_required
from pos.config import Config
from pos.logging import init_logging, get_logger
from pos.database import Database, User, Product, Event, Order, OrderEntry
from pos.database import Database, User, Product, Event, Transaction, Order
config = Config()
@ -128,14 +128,14 @@ def sell():
with db.get_session() as session:
items = request.form.items()
entries = [
OrderEntry(product_uid=uid, quantity=qty)
orders = [
Order(product_uid=uid, quantity=qty)
for uid, qty in items
if int(qty) > 0
]
order = Order(event_uid=event.uid, entries=entries)
session.add(order)
transaction = Transaction(event_uid=event.uid, orders=orders)
session.add(transaction)
flash("Success!", 'success')
return sell_page()