dibbler/text_interface/miscmenus.py

201 lines
7.9 KiB
Python
Raw Normal View History

2017-02-20 13:19:03 +01:00
import conf
import sqlalchemy
from db import Transaction, Product, User
from helpers import less
from text_interface.helpermenus import Menu, Selector
class TransferMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Transfer credit between users',
uses_db=True)
def _execute(self):
self.print_header()
amount = self.input_int('Transfer amount', 1, 100000)
self.set_context(f'Transferring {amount:d} kr', display=False)
user1 = self.input_user('From user')
self.add_to_context(f' from {user1.name}')
user2 = self.input_user('To user')
self.add_to_context(f' to {user2.name}')
comment = self.input_str('Comment')
self.add_to_context(f' (comment) {user2.name}')
2017-02-20 13:19:03 +01:00
t1 = Transaction(user1, amount,
f'transfer to {user2.name} "{comment}"')
2017-02-20 13:19:03 +01:00
t2 = Transaction(user2, -amount,
f'transfer from {user1.name} "{comment}"')
2017-02-20 13:19:03 +01:00
t1.perform_transaction()
t2.perform_transaction()
self.session.add(t1)
self.session.add(t2)
try:
self.session.commit()
print(f"Transferred {amount:d} kr from {user1} to {user2}")
print(f"User {user1}'s credit is now {user1.credit:d} kr")
print(f"User {user2}'s credit is now {user2.credit:d} kr")
print(f"Comment: {comment}")
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not perform transfer: {e}')
2017-02-20 13:19:03 +01:00
# self.pause()
class ShowUserMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Show user', uses_db=True)
def _execute(self):
self.print_header()
user = self.input_user('User name, card number or RFID')
print(f'User name: {user.name}')
print(f'Card number: {user.card}')
print(f'RFID: {user.rfid}')
print(f'Credit: {user.credit} kr')
selector = Selector(f'What do you want to know about {user.name}?',
2017-02-20 13:19:03 +01:00
items=[('transactions', 'Recent transactions (List of last ' + str(
conf.user_recent_transaction_limit) + ')'),
('products', f'Which products {user.name} has bought, and how many'),
2017-02-20 13:19:03 +01:00
('transactions-all', 'Everything (List of all transactions)')])
what = selector.execute()
if what == 'transactions':
self.print_transactions(user, conf.user_recent_transaction_limit)
elif what == 'products':
self.print_purchased_products(user)
elif what == 'transactions-all':
self.print_transactions(user)
2017-02-20 13:19:03 +01:00
else:
2018-08-19 16:38:45 +02:00
print('What what?')
2017-02-20 13:19:03 +01:00
@staticmethod
def print_transactions(user, limit=None):
2017-02-20 13:19:03 +01:00
num_trans = len(user.transactions)
if limit is None:
limit = num_trans
2017-02-20 13:19:03 +01:00
if num_trans <= limit:
string = f"{user.name}'s transactions ({num_trans:d}):\n"
2017-02-20 13:19:03 +01:00
else:
string = f"{user.name}'s transactions ({num_trans:d}, showing only last {limit:d}):\n"
2017-02-20 13:19:03 +01:00
for t in user.transactions[-1:-limit - 1:-1]:
string += f" * {t.time.isoformat(' ')}: {'in' if t.amount < 0 else 'out'} {abs(t.amount)} kr, "
2017-02-20 13:19:03 +01:00
if t.purchase:
products = []
for entry in t.purchase.entries:
if abs(entry.amount) != 1:
amount = f"{abs(entry.amount)}x "
else:
amount = ""
product = f"{amount}{entry.product.name}"
products.append(product)
2017-02-20 13:19:03 +01:00
string += 'purchase ('
string += ', '.join(products)
2017-02-20 13:19:03 +01:00
string += ')'
if t.penalty > 1:
string += f' * {t.penalty:d}x penalty applied'
2017-02-20 13:19:03 +01:00
else:
string += t.description
string += '\n'
less(string)
@staticmethod
def print_purchased_products(user):
products = []
for ref in user.products:
product = ref.product
count = ref.count
if count > 0:
products.append((product, count))
2017-02-20 13:19:03 +01:00
num_products = len(products)
if num_products == 0:
2018-08-19 16:38:45 +02:00
print('No products purchased yet')
2017-02-20 13:19:03 +01:00
else:
text = ''
text += 'Products purchased:\n'
for product, count in products:
text += f'{product.name:<47} {count:>3}\n'
2017-02-20 13:19:03 +01:00
less(text)
class UserListMenu(Menu):
def __init__(self):
Menu.__init__(self, 'User list', uses_db=True)
def _execute(self):
self.print_header()
user_list = self.session.query(User).all()
total_credit = self.session.query(sqlalchemy.func.sum(User.credit)).first()[0]
line_format = '%-12s | %6s\n'
hline = '---------------------\n'
text = ''
text += line_format % ('username', 'credit')
text += hline
for user in user_list:
text += line_format % (user.name, user.credit)
text += hline
text += line_format % ('total credit', total_credit)
less(text)
class AdjustCreditMenu(Menu):
2017-02-20 13:19:03 +01:00
def __init__(self):
Menu.__init__(self, 'Adjust credit', uses_db=True)
def _execute(self):
self.print_header()
user = self.input_user('User')
print(f"User {user.name}'s credit is {user.credit:d} kr")
self.set_context(f'Adjusting credit for user {user.name}', display=False)
2018-08-19 16:38:45 +02:00
print('(Note on sign convention: Enter a positive amount here if you have')
print('added money to the PVVVV money box, a negative amount if you have')
print('taken money from it)')
amount = self.input_int('Add amount', -100000, 100000)
2018-08-19 16:38:45 +02:00
print('(The "log message" will show up in the transaction history in the')
print('"Show user" menu. It is not necessary to enter a message, but it')
print('might be useful to help you remember why you adjusted the credit)')
description = self.input_str('Log message', length_range=(0, 50))
2017-02-20 13:19:03 +01:00
if description == '':
description = 'manually adjusted credit'
transaction = Transaction(user, -amount, description)
transaction.perform_transaction()
self.session.add(transaction)
try:
self.session.commit()
print(f"User {user.name}'s credit is now {user.credit:d} kr")
2018-08-19 16:38:45 +02:00
except sqlalchemy.exc.SQLAlchemyError as e:
print(f'Could not store transaction: {e}')
2017-02-20 13:19:03 +01:00
# self.pause()
class ProductListMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Product list', uses_db=True)
def _execute(self):
self.print_header()
text = ''
product_list = self.session.query(Product).filter(Product.hidden.is_(False)).order_by(Product.stock.desc())
total_value = 0
for p in product_list:
total_value += p.price * p.stock
line_format = '%-15s | %5s | %-' + str(Product.name_length) + 's | %5s \n'
text += line_format % ('bar code', 'price', 'name', 'stock')
text += 78 * '-' + '\n'
for p in product_list:
text += line_format % (p.bar_code, p.price, p.name, p.stock)
text += 78 * '-' + '\n'
text += line_format % ('Total value', total_value, '', '',)
less(text)
class ProductSearchMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Product search', uses_db=True)
def _execute(self):
self.print_header()
self.set_context('Enter (part of) product name or bar code')
product = self.input_product()
2018-08-19 16:38:45 +02:00
print('Result: %s, price: %d kr, bar code: %s, stock: %d, hidden: %s' % (product.name, product.price,
2017-02-20 13:19:03 +01:00
product.bar_code, product.stock,
2018-08-19 16:38:45 +02:00
("Y" if product.hidden else "N")))
2017-02-20 13:19:03 +01:00
# self.pause()