Rounds up when adding multiple users to purchase instead of rounding down, meaning you cannot abuse the system by adding yourself multiple times.

Correctly logs transaction amount when applying penalty.
Better shows penalty effect in purchase menu.
Preparations for database format update.
This commit is contained in:
robertem 2017-02-04 21:10:50 +00:00
parent 033a93bd4c
commit 8954ab4304
2 changed files with 19 additions and 5 deletions

9
db.py
View File

@ -1,6 +1,7 @@
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine, DateTime, Boolean, or_
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
from math import ceil
import datetime
import conf
@ -89,6 +90,7 @@ class Transaction(Base):
amount = Column(Integer)
description = Column(String(50))
purchase_id = Column(Integer, ForeignKey('purchases.id'))
#penalty = Column(Integer)
user = relationship(User, backref=backref('transactions', order_by=time))
@ -97,11 +99,12 @@ class Transaction(Base):
self.amount = amount
self.description = description
self.purchase = purchase
self.penalty_ratio = penalty_ratio
self.penalty = penalty_ratio
def perform_transaction(self):
self.time = datetime.datetime.now()
self.user.credit -= self.amount * self.penalty_ratio
self.amount *= self.penalty
self.user.credit -= self.amount
if self.purchase:
for entry in self.purchase.entries:
entry.product.stock -= entry.amount
@ -131,7 +134,7 @@ class Purchase(Base):
return len(self.transactions) > 0 and len(self.entries) > 0
def price_per_transaction(self):
return self.price/len(self.transactions)
return int(ceil(float(self.price)/len(self.transactions)))
def set_price(self):
self.price = 0

View File

@ -787,6 +787,8 @@ class ShowUserMenu(Menu):
string += ', '.join(map(lambda e: e.product.name,
t.purchase.entries))
string += ')'
#if t.penalty > 1:
# string += ' * %dx penalty applied' % t.penalty
else:
string += t.description
string += '\n'
@ -808,6 +810,8 @@ class ShowUserMenu(Menu):
string += ', '.join(map(lambda e: e.product.name,
t.purchase.entries))
string += ')'
#if t.penalty > 1:
# string += ' * %dx penalty applied' % t.penalty
else:
string += t.description
string += '\n'
@ -1022,7 +1026,7 @@ When finished, write an empty line to confirm the purchase.
if len(transactions) == 0:
string += '(empty)'
else:
string += ', '.join(map(lambda t: t.user.name,
string += ', '.join(map(lambda t: t.user.name + ("*" if t.user.credit < conf.user_recent_transaction_limit else ""),
transactions))
string += '\n products: '
if len(entries) == 0:
@ -1032,7 +1036,14 @@ When finished, write an empty line to confirm the purchase.
entries))
if len(transactions) > 1:
string += '\n price per person: %d kr' % self.purchase.price_per_transaction()
if any(t.penalty > 1 for t in transactions):
string += ' *(%d kr)' % (self.purchase.price_per_transaction() * 2)
string += '\n total price: %d kr' % self.purchase.price
if any(t.penalty > 1 for t in transactions):
string += '\n *total with penalty: %d kr' % sum(self.purchase.price_per_transaction() * t.penalty for t in transactions)
return string
def print_purchase(self):