2017-03-21 11:26:25 +01:00
|
|
|
import click
|
|
|
|
from tabulate import tabulate
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from pos.config import Config
|
|
|
|
from pos.logging import init_logging, get_logger
|
2017-03-22 17:12:15 +01:00
|
|
|
from pos.database import Database, User, Event, Product, Transaction
|
2017-03-21 11:26:25 +01:00
|
|
|
|
|
|
|
config = Config()
|
|
|
|
conf_db = config.core['DATABASE']
|
|
|
|
|
|
|
|
init_logging(config.logging)
|
|
|
|
log = get_logger('cli')
|
|
|
|
|
|
|
|
db = Database(**conf_db)
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.group('user')
|
|
|
|
def user():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def tabulate_users(users):
|
|
|
|
tab = [["UID", "Username", "Enabled", "Created at"]]
|
|
|
|
for u in users:
|
|
|
|
tab.append([u.uid, u.username, u.is_active, u.created_at])
|
|
|
|
return tabulate(tab, headers='firstrow')
|
|
|
|
|
|
|
|
|
|
|
|
@user.command('add')
|
|
|
|
@click.argument('username')
|
|
|
|
@click.argument('password')
|
|
|
|
def user_add(username, password):
|
|
|
|
user = User(username=username, password=password)
|
|
|
|
with db.get_session() as session:
|
|
|
|
session.add(user)
|
|
|
|
print("User succesfully added.")
|
|
|
|
|
|
|
|
|
|
|
|
@user.command('list')
|
|
|
|
def user_list():
|
|
|
|
users = None
|
|
|
|
with db.get_session() as session:
|
|
|
|
users = session.query(User).all()
|
|
|
|
if len(users) == 0:
|
|
|
|
print("No users found.")
|
|
|
|
return
|
|
|
|
print(tabulate_users(users))
|
|
|
|
|
|
|
|
|
|
|
|
@cli.group('event')
|
|
|
|
def event():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def tabulate_events(events):
|
|
|
|
tab = [["UID", "Name", "Starts at", "Ends at", "Income", "Created at"]]
|
|
|
|
for e in events:
|
|
|
|
sum = 0
|
2017-03-22 17:12:15 +01:00
|
|
|
for t in e.transactions:
|
|
|
|
for o in t.orders:
|
|
|
|
sum += o.product.price * o.quantity
|
2017-03-21 11:26:25 +01:00
|
|
|
tab.append([e.uid, e.name, e.starts_at, e.ends_at, sum, e.created_at])
|
|
|
|
return tabulate(tab, headers='firstrow')
|
|
|
|
|
|
|
|
|
|
|
|
@event.command('add')
|
|
|
|
@click.argument('name')
|
|
|
|
@click.option('--start')
|
|
|
|
@click.option('--end')
|
|
|
|
def event_add(name, start, end):
|
|
|
|
start = datetime.strptime(start, "%Y-%m-%d %H:%M")
|
|
|
|
end = datetime.strptime(end, "%Y-%m-%d %H:%M")
|
|
|
|
event = Event(name=name, starts_at=start, ends_at=end)
|
|
|
|
with db.get_session() as session:
|
|
|
|
session.add(event)
|
|
|
|
print("Event succesfully added.")
|
|
|
|
|
|
|
|
|
|
|
|
@event.command('list')
|
|
|
|
def event_list():
|
|
|
|
events = None
|
|
|
|
with db.get_session() as session:
|
|
|
|
events = session.query(Event).all()
|
|
|
|
if len(events) == 0:
|
|
|
|
print("No events found.")
|
|
|
|
return
|
|
|
|
print(tabulate_events(events))
|
|
|
|
|
|
|
|
|
|
|
|
@cli.group('product')
|
|
|
|
def product():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def tabulate_products(products):
|
|
|
|
tab = [["UID", "Name", "Price", "Currency", "Enabled", "Created at"]]
|
|
|
|
for p in products:
|
|
|
|
tab.append([p.uid, p.name, p.price, p.currency,
|
|
|
|
p.is_active, p.created_at])
|
|
|
|
return tabulate(tab, headers='firstrow')
|
|
|
|
|
|
|
|
|
|
|
|
@product.command('add')
|
|
|
|
@click.argument('name')
|
|
|
|
@click.argument('price')
|
|
|
|
@click.option('--currency')
|
|
|
|
def product_add(name, price, currency):
|
|
|
|
product = Product(name=name, currency=currency, price=price)
|
|
|
|
with db.get_session() as session:
|
|
|
|
session.add(product)
|
|
|
|
print("Product succesfully added.")
|
|
|
|
|
|
|
|
|
|
|
|
@product.command('list')
|
|
|
|
def product_list():
|
|
|
|
products = None
|
|
|
|
with db.get_session() as session:
|
|
|
|
products = session.query(Product).all()
|
|
|
|
if len(products) == 0:
|
|
|
|
print("No products found.")
|
|
|
|
return
|
|
|
|
print(tabulate_products(products))
|
|
|
|
|
|
|
|
|
2017-03-22 17:12:15 +01:00
|
|
|
@cli.group('transaction')
|
|
|
|
def transaction():
|
2017-03-21 11:26:25 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def tabulate_orders(orders):
|
2017-03-22 17:12:15 +01:00
|
|
|
tab = [["Product", "Price", "Quantity", "Total"]]
|
2017-03-21 11:26:25 +01:00
|
|
|
for o in orders:
|
2017-03-22 17:12:15 +01:00
|
|
|
if o.quantity != 0:
|
|
|
|
tab.append([o.product.name, o.product.price, o.quantity,
|
|
|
|
o.product.price * o.quantity])
|
|
|
|
return tabulate(tab, headers='firstrow')
|
|
|
|
|
|
|
|
|
|
|
|
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():
|
2017-03-21 11:26:25 +01:00
|
|
|
with db.get_session() as session:
|
2017-03-22 17:12:15 +01:00
|
|
|
transactions = session.query(Transaction).all()
|
|
|
|
|
|
|
|
if len(transactions) == 0:
|
|
|
|
print("No transactions found.")
|
2017-03-21 11:26:25 +01:00
|
|
|
return
|
2017-03-22 17:12:15 +01:00
|
|
|
|
|
|
|
return print_transactions(transactions)
|
2017-03-21 11:26:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cli()
|