From 5b63e54687eab34d3e3978382dce410e6d0dc023 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 5 Sep 2012 13:47:31 +0200 Subject: [PATCH] brmbar v3: Properly record payment by cash in database --- brmbar3/brmbar-gui-qt4.py | 4 ++++ brmbar3/brmbar-gui-qt4/ItemInfo.qml | 2 +- brmbar3/brmbar/Shop.py | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 3f53876..65bf9a7 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -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) diff --git a/brmbar3/brmbar-gui-qt4/ItemInfo.qml b/brmbar3/brmbar-gui-qt4/ItemInfo.qml index 3445e7d..5f13ed3 100644 --- a/brmbar3/brmbar-gui-qt4/ItemInfo.qml +++ b/brmbar3/brmbar-gui-qt4/ItemInfo.qml @@ -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") } diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 422e118..fe2e9da 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -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