diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 23e6d37..a351bee 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import sys +import subprocess from PySide import QtCore, QtGui, QtDeclarative @@ -8,6 +9,15 @@ from brmbar import Database import brmbar +# User credit balance limit; sale will fail when balance is below this limit. +LIMIT_BALANCE = -200 +# When below this credit balance, an alert hook script (see below) is run. +ALERT_BALANCE = 0 +# This script is executed when a user is buying things and their balance is +# below LIMIT_BALANCE (with argument "limit") or below ALERT_BALANCE +# (with argument "alert"). +ALERT_SCRIPT = "./alert.sh" + class ShopAdapter(QtCore.QObject): """ Interface between QML and the brmbar package """ @@ -70,6 +80,18 @@ class ShopAdapter(QtCore.QObject): db.commit() return acct + @QtCore.Slot('QVariant', 'QVariant', result='QVariant') + def canSellItem(self, itemid, userid): + user = brmbar.Account.load(db, id = userid) + if -user.balance() > ALERT_BALANCE: + return True + elif -user.balance() > LIMIT_BALANCE: + subprocess.call(["sh", ALERT_SCRIPT, "alert"]) + return True + else: + subprocess.call(["sh", ALERT_SCRIPT, "limit"]) + return False + @QtCore.Slot('QVariant', 'QVariant', result='QVariant') def sellItem(self, itemid, userid): user = brmbar.Account.load(db, id = userid) @@ -99,6 +121,11 @@ class ShopAdapter(QtCore.QObject): db.commit() return balance + @QtCore.Slot('QVariant', result='QVariant') + def balance_user(self, userid): + user = brmbar.Account.load(db, id=userid) + return user.negbalance_str() + @QtCore.Slot(result='QVariant') def balance_cash(self): balance = shop.cash.balance_str() diff --git a/brmbar3/brmbar-gui-qt4/ItemInfo.qml b/brmbar3/brmbar-gui-qt4/ItemInfo.qml index a39e322..11e959d 100644 --- a/brmbar3/brmbar-gui-qt4/ItemInfo.qml +++ b/brmbar3/brmbar-gui-qt4/ItemInfo.qml @@ -58,6 +58,8 @@ Item { if (acct.acctype == "cash") { //Copied from BarButton.onButtonClick shop.sellItemCash(dbid) status_text.setStatus("Sold! Put " + price + " Kč in the money box.", "#ffff7c") + } else if (!shop.canSellItem(dbid, acct.id)) { + status_text.setStatus("NOT SOLD! "+acct.name+"'s credit is TOO LOW: "+shop.balance_user(acct.id), "#ff4444") } else { var balance = shop.sellItem(dbid, acct.id) status_text.setStatus("Sold! "+acct.name+"'s credit is "+balance+".", "#ffff7c")