brmbar v3: Properly record payment by cash in database

This commit is contained in:
Petr Baudis 2012-09-05 13:47:31 +02:00
parent e428d28e20
commit 5b63e54687
3 changed files with 22 additions and 3 deletions

View file

@ -51,6 +51,10 @@ class ShopAdapter(QtCore.QObject):
shop.sell(item = brmbar.Account.load(db, id = itemid), user = user)
return user.negbalance_str()
@QtCore.Slot('QVariant', result='QVariant')
def sellItemCash(self, itemid):
shop.sell_for_cash(item = brmbar.Account.load(db, id = itemid))
@QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def chargeCredit(self, credit, userid):
user = brmbar.Account.load(db, id = userid)

View file

@ -70,7 +70,7 @@ Item {
text: "Pay by cash"
fontSize: 44
onButtonClick: {
// TODO
shop.sellItemCash(dbid)
status_text.setStatus("Sold! Put " + price + " Kč in the money box.", "#ffff7c")
loadPage("MainPage")
}

View file

@ -37,14 +37,29 @@ class Shop:
return cost
def sell_for_cash(self, item, amount = 1):
# Sale: Currency conversion from item currency to shop currency
(buy, sell) = item.currency.rates(self.currency)
cost = amount * sell
profit = amount * (sell - buy)
transaction = self._transaction(description = "BrmBar sale of {}x {} for cash".format(amount, item.name))
item.credit(transaction, amount, "Cash")
self.cash.debit(transaction, cost, item.name)
self.profits.debit(transaction, profit, "Margin on " + item.name)
self.db.commit()
return cost
def add_credit(self, credit, user):
transaction = self._transaction(responsible = user, description = "BrmBar credit replenishment for " + user.name)
self.cash.debit(transaction, credit, user.name)
user.credit(transaction, credit, "Credit replenishment")
self.db.commit()
def _transaction(self, responsible, description):
def _transaction(self, responsible = None, description = None):
with closing(self.db.cursor()) as cur:
cur.execute("INSERT INTO transactions (responsible, description) VALUES (%s, %s) RETURNING id", [responsible.id, description])
cur.execute("INSERT INTO transactions (responsible, description) VALUES (%s, %s) RETURNING id",
[responsible.id if responsible else None, description])
transaction = cur.fetchone()[0]
return transaction