Multiple buyers for 'add stock' menu

This commit is contained in:
Christoffer Viken 2017-02-25 16:28:38 +00:00
parent 8977071b3e
commit a71274ed78
1 changed files with 22 additions and 19 deletions

View File

@ -11,7 +11,10 @@ class AddStockMenu(Menu):
self.help_text = ''' self.help_text = '''
Enter what you have bought for PVVVV here, along with your user name and how 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''' much money you're due in credits for the purchase when prompted.\n'''
self.user = None self.users = []
self.users = []
self.products = {}
self.price = 0
def _execute(self): def _execute(self):
questions = { questions = {
@ -21,13 +24,13 @@ much money you're due in credits for the purchase when prompted.\n'''
(True, True): 'Enter more strings of the form "<number> <product>", or an empty line to confirm' (True, True): 'Enter more strings of the form "<number> <product>", or an empty line to confirm'
} }
self.user = None self.users = []
self.products = {} self.products = {}
self.price = 0 self.price = 0
while True: while True:
self.print_info() self.print_info()
self.printc(questions[bool(self.user), bool(len(self.products))]) self.printc(questions[bool(self.users), bool(len(self.products))])
thing_price = 0 thing_price = 0
# Read in a 'thing' (product or user): # Read in a 'thing' (product or user):
@ -60,28 +63,26 @@ much money you're due in credits for the purchase when prompted.\n'''
self.perform_transaction() self.perform_transaction()
def complete_input(self): def complete_input(self):
return bool(self.user) and len(self.products) and self.price return bool(self.users) and len(self.products) and self.price
def print_info(self): def print_info(self):
print (6 + Product.name_length) * '-' print (6 + Product.name_length) * '-'
if self.price: if self.price:
print("Amount to be credited: %" + str(Product.name_length - 17) + "i") % self.price print "Amount to be credited: {{0:>{0}d}}".format(Product.name_length-17).format(self.price)
if self.user: if self.users:
print("User to credit: %" + str(Product.name_length - 10) + "s") % self.user.name print "Users to credit:"
print('\n%-' + str(Product.name_length - 1) + 's Amount') % "Product" for user in self.users:
print " %s" % str(user.name)
print "\n{{0:s}}{{1:>{0}s}}".format(Product.name_length-1).format("Product", "Amount")
print (6 + Product.name_length) * '-' print (6 + Product.name_length) * '-'
if len(self.products): if len(self.products):
for product in self.products.keys(): for product in self.products.keys():
print('%' + str(-Product.name_length) + 's %5i') % (product.name, self.products[product][0]) print '{{0:<{0}}}{{1:>6d}}'.format(Product.name_length).format(product.name, self.products[product][0])
print (6 + Product.name_length) * '-' print (6 + Product.name_length) * '-'
def add_thing_to_pending(self, thing, amount, price): def add_thing_to_pending(self, thing, amount, price):
if isinstance(thing, User): if isinstance(thing, User):
if self.user: self.users.append(thing)
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(): elif thing in self.products.keys():
print 'Already added this product, adding amounts' print 'Already added this product, adding amounts'
self.products[thing][0] += amount self.products[thing][0] += amount
@ -105,7 +106,8 @@ much money you're due in credits for the purchase when prompted.\n'''
("- Removed hidden status" if old_hidden != product.hidden else "") ("- Removed hidden status" if old_hidden != product.hidden else "")
purchase = Purchase() purchase = Purchase()
Transaction(self.user, purchase=purchase, amount=-self.price, description=description) for user in self.users:
Transaction(user, purchase=purchase, amount=-self.price, description=description)
for product in self.products: for product in self.products:
PurchaseEntry(purchase, product, -self.products[product][0]) PurchaseEntry(purchase, product, -self.products[product][0])
@ -116,6 +118,7 @@ much money you're due in credits for the purchase when prompted.\n'''
self.session.commit() self.session.commit()
print "Success! Transaction performed:" print "Success! Transaction performed:"
# self.print_info() # self.print_info()
print "User %s's credit is now %i" % (self.user.name, self.user.credit) for user in self.users:
print "User %s's credit is now %i" % (user.name, user.credit)
except sqlalchemy.exc.SQLAlchemyError, e: except sqlalchemy.exc.SQLAlchemyError, e:
print 'Could not perform transaction: %s' % e print 'Could not perform transaction: %s' % e