This commit is contained in:
robertem 2015-10-03 18:41:29 +00:00
parent e53b680dd2
commit c8a6f6c209
2 changed files with 1298 additions and 1198 deletions

205
db.py
View File

@ -9,141 +9,142 @@ Base = declarative_base()
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
class User(Base): class User(Base):
__tablename__ = 'users' __tablename__ = 'users'
name = Column(String(10), primary_key=True) name = Column(String(10), primary_key=True)
card = Column(String(10)) card = Column(String(10))
rfid = Column(String(10)) rfid = Column(String(10))
credit = Column(Integer) credit = Column(Integer)
name_re = r"[a-z]+" name_re = r"[a-z]+"
card_re = r"(([Nn][Tt][Nn][Uu])?[0-9]+)?" card_re = r"(([Nn][Tt][Nn][Uu])?[0-9]+)?"
rfid_re = r"[0-9]*" rfid_re = r"[0-9]*"
def __init__(self, name, card, rfid, credit=0): def __init__(self, name, card, rfid, credit=0):
self.name = name self.name = name
if card == '': if card == '':
card = None card = None
self.card = card self.card = card
if rfid == '': if rfid == '':
rfid = None rfid = None
self.rfid = rfid self.rfid = rfid
self.credit = credit self.credit = credit
def __repr__(self): def __repr__(self):
return "<User('%s')>" % self.name return "<User('%s')>" % self.name
def __str__(self): def __str__(self):
return self.name return self.name
def is_anonymous(self): def is_anonymous(self):
return self.card == '11122233' return self.card == '11122233'
class Product(Base): class Product(Base):
__tablename__ = 'products' __tablename__ = 'products'
bar_code = Column(String(13), primary_key=True) bar_code = Column(String(13), primary_key=True)
name = Column(String(45)) name = Column(String(45))
price = Column(Integer) price = Column(Integer)
stock = Column(Integer) stock = Column(Integer)
bar_code_re = r"[0-9]+" bar_code_re = r"[0-9]+"
name_re = r".+" name_re = r".+"
name_length = 45 name_length = 45
def __init__(self, bar_code, name, price, stock=0): def __init__(self, bar_code, name, price, stock=0):
self.name = name self.name = name
self.bar_code = bar_code self.bar_code = bar_code
self.price = price self.price = price
self.stock = stock self.stock = stock
def __repr__(self): def __repr__(self):
return "<Product('%s', '%s', '%s', '%s')>" % (self.name, self.bar_code, self.price, self.stock) return "<Product('%s', '%s', '%s', '%s')>" % (self.name, self.bar_code, self.price, self.stock)
def __str__(self): def __str__(self):
return self.name return self.name
class PurchaseEntry(Base): class PurchaseEntry(Base):
__tablename__ = 'purchase_entries' __tablename__ = 'purchase_entries'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
purchase_id = Column(Integer,ForeignKey("purchases.id")) purchase_id = Column(Integer,ForeignKey("purchases.id"))
product_bar_code = Column(String(13),ForeignKey("products.bar_code")) product_bar_code = Column(String(13),ForeignKey("products.bar_code"))
amount = Column(Integer) amount = Column(Integer)
product = relationship(Product,backref="purchases") product = relationship(Product,backref="purchases")
def __init__(self, purchase, product, amount): def __init__(self, purchase, product, amount):
self.product = product self.product = product
self.product_bar_code = product.bar_code self.product_bar_code = product.bar_code
self.purchase = purchase self.purchase = purchase
self.amount = amount self.amount = amount
def __repr__(self): def __repr__(self):
return "<PurchaseEntry('%s', '%s')>" % (self.product.name, self.amount ) return "<PurchaseEntry('%s', '%s')>" % (self.product.name, self.amount )
class Transaction(Base): class Transaction(Base):
__tablename__ = 'transactions' __tablename__ = 'transactions'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
time = Column(DateTime) time = Column(DateTime)
user_name = Column(String(10), ForeignKey('users.name')) user_name = Column(String(10), ForeignKey('users.name'))
amount = Column(Integer) amount = Column(Integer)
description = Column(String(50)) description = Column(String(50))
purchase_id = Column(Integer, ForeignKey('purchases.id')) purchase_id = Column(Integer, ForeignKey('purchases.id'))
user = relationship(User, backref=backref('transactions', order_by=time)) user = relationship(User, backref=backref('transactions', order_by=time))
def __init__(self, user, amount=0, description=None, purchase=None): def __init__(self, user, amount=0, description=None, purchase=None, penalty_ratio=1):
self.user = user self.user = user
self.amount = amount self.amount = amount
self.description = description self.description = description
self.purchase = purchase self.purchase = purchase
self.penalty_ratio = penalty_ratio
def perform_transaction(self): def perform_transaction(self):
self.time = datetime.datetime.now() self.time = datetime.datetime.now()
self.user.credit -= self.amount self.user.credit -= self.amount * self.penalty_ratio
if self.purchase: if self.purchase:
for entry in self.purchase.entries: for entry in self.purchase.entries:
entry.product.stock -= entry.amount entry.product.stock -= entry.amount
class Purchase(Base): class Purchase(Base):
__tablename__ = 'purchases' __tablename__ = 'purchases'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
time = Column(DateTime) time = Column(DateTime)
# user_name = Column(Integer, ForeignKey('users.name')) # user_name = Column(Integer, ForeignKey('users.name'))
price = Column(Integer) price = Column(Integer)
# performed = Column(Boolean) # performed = Column(Boolean)
# user = relationship(User, backref=backref('purchases', order_by=id)) # user = relationship(User, backref=backref('purchases', order_by=id))
# users = relationship(User, secondary=purchase_user, backref='purhcases' # users = relationship(User, secondary=purchase_user, backref='purhcases'
transactions = relationship(Transaction, order_by=Transaction.user_name, backref='purchase') transactions = relationship(Transaction, order_by=Transaction.user_name, backref='purchase')
entries = relationship(PurchaseEntry, backref=backref("purchase")) entries = relationship(PurchaseEntry, backref=backref("purchase"))
def __init__(self): def __init__(self):
pass pass
def __repr__(self): def __repr__(self):
return "<Purchase(%d, %d, '%s')>" % (self.id, self.price, self.time.strftime('%c')) return "<Purchase(%d, %d, '%s')>" % (self.id, self.price, self.time.strftime('%c'))
def is_complete(self): def is_complete(self):
return len(self.transactions) > 0 and len(self.entries) > 0 return len(self.transactions) > 0 and len(self.entries) > 0
def price_per_transaction(self): def price_per_transaction(self):
return self.price/len(self.transactions) return self.price/len(self.transactions)
def set_price(self): def set_price(self):
self.price = 0 self.price = 0
for entry in self.entries: for entry in self.entries:
self.price += entry.amount*entry.product.price self.price += entry.amount*entry.product.price
if len(self.transactions) > 0: if len(self.transactions) > 0:
for t in self.transactions: for t in self.transactions:
t.amount = self.price_per_transaction() t.amount = self.price_per_transaction()
def perform_purchase(self): def perform_purchase(self):
self.time = datetime.datetime.now() self.time = datetime.datetime.now()
self.set_price() self.set_price()
for t in self.transactions: for t in self.transactions:
t.perform_transaction() t.perform_transaction()
# self.user.credit -= self.price # self.user.credit -= self.price
# self.performed = True # self.performed = True

File diff suppressed because it is too large Load Diff