dibbler/text_interface/addstock.py

125 lines
5.3 KiB
Python
Raw Normal View History

2017-02-20 13:19:03 +01:00
from math import ceil
import sqlalchemy
from db import Product, User, Transaction
from text_interface.helpermenus import Menu
class AddStockMenu(Menu):
def __init__(self):
Menu.__init__(self, 'Add stock and adjust credit', uses_db=True)
self.help_text = '''
Enter what you have bought for PVVVV here, along with your user name and how
much money you're due in credits for the purchase when prompted.\n'''
self.user = None
def _execute(self):
questions = {
(False, False): 'Enter user id or a string of the form "<number> <product>"',
(False, True): 'Enter user id or more strings of the form "<number> <product>"',
(True, False): 'Enter a string of the form "<number> <product>"',
(True, True): 'Enter more strings of the form "<number> <product>", or an empty line to confirm'
}
self.user = None
self.products = {}
self.price = 0
while True:
self.print_info()
self.printc(questions[bool(self.user), bool(len(self.products))])
thing_price = 0
# Read in a 'thing' (product or user):
line = self.input_multiple(add_nonexisting=('user', 'product'), empty_input_permitted=True,
find_hidden_products=False)
if line:
(thing, amount) = line
if isinstance(thing, Product):
self.printc("%d of %s registered" % (amount, thing.name))
thing_price = self.input_int('What did you pay a piece? ', (1, 100000),
default=thing.price) * amount
self.price += thing_price
# once we get something in the
# purchase, we want to protect the
# user from accidentally killing it
self.exit_confirm_msg = 'Abort transaction?'
else:
# thing = None
if not self.complete_input():
if self.confirm('Not enough information entered. Abort transaction?', default=True):
return False
continue
break
# Add the thing to the pending adjustments:
self.add_thing_to_pending(thing, amount, thing_price)
if self.confirm('Do you want to change the credited amount?', default=False):
self.price = self.input_int('Total amount> ', (1, 100000), default=self.price)
self.perform_transaction()
def complete_input(self):
return bool(self.user) and len(self.products) and self.price
def print_info(self):
print(6 + Product.name_length) * '-'
if self.price:
print("Amount to be credited: %" + str(Product.name_length - 17) + "i") % self.price
if self.user:
print("User to credit: %" + str(Product.name_length - 10) + "s") % self.user.name
print('\n%-' + str(Product.name_length - 1) + 's Amount') % "Product"
print(6 + Product.name_length) * '-'
if len(self.products):
# print "Products added:"
# print (6+Product.name_length)*'-'
for product in self.products.keys():
print('%' + str(-Product.name_length) + 's %5i') % (product.name, self.products[product][0])
print(6 + Product.name_length) * '-'
def add_thing_to_pending(self, thing, amount, price):
if isinstance(thing, User):
if self.user:
print "Only one user may be credited for a purchase, transfer credit manually afterwards"
return
else:
self.user = thing
elif thing in self.products.keys():
print 'Already added this product, adding amounts'
self.products[thing][0] += amount
self.products[thing][1] += price
else:
self.products[thing] = [amount, price]
def perform_transaction(self):
# self.user.credit += self.price
description = self.input_str('Log message> ', length_range=(0, 50))
if description == '':
description = 'Purchased products for PVVVV, adjusted credit ' + str(self.price)
transaction = Transaction(self.user, -self.price, description)
transaction.perform_transaction()
self.session.add(transaction)
for product in self.products:
value = max(product.stock, 0) * product.price + self.products[product][1]
old_price = product.price
old_hidden = product.hidden
product.price = int(ceil(float(value) / (max(product.stock, 0) + self.products[product][0])))
product.stock += self.products[product][0]
product.hidden = False
print "New stock for %s: %d" % (product.name, product.stock), \
("- New price: " + str(product.price) if old_price != product.price else ""), \
("- Removed hidden status" if old_hidden != product.hidden else "")
try:
self.session.commit()
print "Success! Transaction performed:"
# self.print_info()
print "User %s's credit is now %i" % (self.user.name, self.user.credit)
except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not perform transaction: %s' % e