This commit is contained in:
robertem 2015-10-03 19:47:05 +00:00
parent c8a6f6c209
commit d9b6a3ab6d
1 changed files with 17 additions and 11 deletions

View File

@ -279,8 +279,8 @@ class Menu():
result = self.search_for_thing(" ".join(search_lst[1:]), permitted_things,add_nonexisting) result = self.search_for_thing(" ".join(search_lst[1:]), permitted_things,add_nonexisting)
# Her kan det legges inn en except ValueError, # Her kan det legges inn en except ValueError,
# men da blir det fort mye plaging av brukeren # men da blir det fort mye plaging av brukeren
except: except Exception as e:
pass print(e)
return (result,num) return (result,num)
@ -478,7 +478,7 @@ class ConfirmMenu(Menu):
Menu.__init__(self, 'question', prompt=prompt, Menu.__init__(self, 'question', prompt=prompt,
exit_disallowed_msg='Please answer yes or no') exit_disallowed_msg='Please answer yes or no')
self.default=default self.default=default
self.timeout=0 self.timeout=timeout
def _execute(self): def _execute(self):
options = {True: '[y]/n', False: 'y/[n]', None: 'y/n'}[self.default] options = {True: '[y]/n', False: 'y/[n]', None: 'y/n'}[self.default]
@ -918,7 +918,7 @@ When finished, write an empty line to confirm the purchase.
print '--------------------------------------------' print '--------------------------------------------'
if not self.credit_check(thing): if not self.credit_check(thing):
if self.low_credit_warning(thing, self.superfast_mode ): if self.low_credit_warning(user=thing, timeout=self.superfast_mode ):
Transaction(thing, purchase=self.purchase, penalty_ratio=2) Transaction(thing, purchase=self.purchase, penalty_ratio=2)
else: else:
return False return False
@ -1297,6 +1297,7 @@ much money you're due in credits for the purchase when prompted.
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.user), bool(len(self.products))])
thing_price = 0
# Read in a 'thing' (product or user): # Read in a 'thing' (product or user):
line = self.input_multiple(add_nonexisting=('user','product'), empty_input_permitted=True) line = self.input_multiple(add_nonexisting=('user','product'), empty_input_permitted=True)
@ -1306,7 +1307,8 @@ much money you're due in credits for the purchase when prompted.
if isinstance(thing, Product): if isinstance(thing, Product):
self.printc("%d of %s registered" % (amount, thing.name)) self.printc("%d of %s registered" % (amount, thing.name))
self.price += self.input_int('What did you pay a piece? ', (1,100000), default=thing.price) * amount 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 # once we get something in the
# purchase, we want to protect the # purchase, we want to protect the
@ -1323,7 +1325,7 @@ much money you're due in credits for the purchase when prompted.
break break
# Add the thing to the pending adjustments: # Add the thing to the pending adjustments:
self.add_thing_to_pending(thing,amount) self.add_thing_to_pending(thing, amount, thing_price)
if self.confirm('Do you want to change the total amount?', default=False): if self.confirm('Do you want to change the total amount?', default=False):
self.price = self.input_int('Total amount> ', (1,100000), default=self.price) self.price = self.input_int('Total amount> ', (1,100000), default=self.price)
@ -1345,11 +1347,11 @@ much money you're due in credits for the purchase when prompted.
# print "Products added:" # print "Products added:"
# print (6+Product.name_length)*'-' # print (6+Product.name_length)*'-'
for product in self.products.keys(): for product in self.products.keys():
print ('%'+str(-Product.name_length)+'s %5i') % (product.name, self.products[product]) print ('%'+str(-Product.name_length)+'s %5i') % (product.name, self.products[product][0])
print (6+Product.name_length)*'-' print (6+Product.name_length)*'-'
def add_thing_to_pending(self,thing,amount): def add_thing_to_pending(self, thing, amount, price):
if isinstance(thing,User): if isinstance(thing,User):
if self.user: if self.user:
print "Only one user may be credited for a purchase, transfer credit manually afterwards" print "Only one user may be credited for a purchase, transfer credit manually afterwards"
@ -1358,9 +1360,10 @@ much money you're due in credits for the purchase when prompted.
self.user = thing 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] += amount self.products[thing][0] += amount
self.products[thing][1] += price
else: else:
self.products[thing] = amount self.products[thing] = [amount, price]
def perform_transaction(self): def perform_transaction(self):
# self.user.credit += self.price # self.user.credit += self.price
@ -1371,7 +1374,10 @@ much money you're due in credits for the purchase when prompted.
transaction.perform_transaction() transaction.perform_transaction()
self.session.add(transaction) self.session.add(transaction)
for product in self.products: for product in self.products:
product.stock += self.products[product] value = max(product.stock, 0)*product.price + self.products[product][1]
product.price = int(ceil(float(value)/(max(product.stock, 0) + self.products[product][0])))
product.stock += self.products[product][0]
print "New stock for", product.name, "- New price:", product.price
try: try:
self.session.commit() self.session.commit()
print "Success! Transaction performed:" print "Success! Transaction performed:"