Retab part II

This commit is contained in:
Václav 'Ax' Hůla 2012-10-26 21:45:30 +02:00
parent ec94f1d034
commit ffa119e7f0
2 changed files with 187 additions and 187 deletions

View file

@ -13,49 +13,49 @@ active_inv_item = None
active_credit = None active_credit = None
for line in sys.stdin: for line in sys.stdin:
barcode = line.rstrip() barcode = line.rstrip()
if barcode[0] == "$": if barcode[0] == "$":
credits = {'$02': 20, '$05': 50, '$10': 100, '$20': 200, '$50': 500, '$1k': 1000} credits = {'$02': 20, '$05': 50, '$10': 100, '$20': 200, '$50': 500, '$1k': 1000}
credit = credits[barcode] credit = credits[barcode]
if credit is None: if credit is None:
print("Unknown barcode: " + barcode) print("Unknown barcode: " + barcode)
continue continue
print("CREDIT " + str(credit)) print("CREDIT " + str(credit))
active_inv_item = None active_inv_item = None
active_credit = credit active_credit = credit
continue continue
if barcode == "SCR": if barcode == "SCR":
print("SHOW CREDIT") print("SHOW CREDIT")
active_inv_item = None active_inv_item = None
active_credit = None active_credit = None
continue continue
acct = brmbar.Account.load_by_barcode(db, barcode) acct = brmbar.Account.load_by_barcode(db, barcode)
if acct is None: if acct is None:
print("Unknown barcode: " + barcode) print("Unknown barcode: " + barcode)
continue continue
if acct.acctype == 'debt': if acct.acctype == 'debt':
if active_inv_item is not None: if active_inv_item is not None:
cost = shop.sell(item = active_inv_item, user = acct) cost = shop.sell(item = active_inv_item, user = acct)
print("{} has bought {} for {} and now has {} balance".format(acct.name, active_inv_item.name, currency.str(cost), acct.negbalance_str())) print("{} has bought {} for {} and now has {} balance".format(acct.name, active_inv_item.name, currency.str(cost), acct.negbalance_str()))
elif active_credit is not None: elif active_credit is not None:
shop.add_credit(credit = active_credit, user = acct) shop.add_credit(credit = active_credit, user = acct)
print("{} has added {} credit and now has {} balance".format(acct.name, currency.str(active_credit), acct.negbalance_str())) print("{} has added {} credit and now has {} balance".format(acct.name, currency.str(active_credit), acct.negbalance_str()))
else: else:
print("{} has {} balance".format(acct.name, acct.negbalance_str())) print("{} has {} balance".format(acct.name, acct.negbalance_str()))
active_inv_item = None active_inv_item = None
active_credit = None active_credit = None
elif acct.acctype == 'inventory': elif acct.acctype == 'inventory':
buy, sell = acct.currency.rates(currency) buy, sell = acct.currency.rates(currency)
print("{} costs {} with {} in stock".format(acct.name, currency.str(sell), int(acct.balance()))) print("{} costs {} with {} in stock".format(acct.name, currency.str(sell), int(acct.balance())))
active_inv_item = acct active_inv_item = acct
active_credit = None active_credit = None
else: else:
print("invalid account type {}".format(acct.acctype)) print("invalid account type {}".format(acct.acctype))
active_inv_item = None active_inv_item = None
active_credit = None active_credit = None

View file

@ -9,169 +9,169 @@ import brmbar
class ShopAdapter(QtCore.QObject): class ShopAdapter(QtCore.QObject):
""" Interface between QML and the brmbar package """ """ Interface between QML and the brmbar package """
def __init__(self): def __init__(self):
QtCore.QObject.__init__(self) QtCore.QObject.__init__(self)
def acct_debt_map(self, acct): def acct_debt_map(self, acct):
map = acct.__dict__.copy() map = acct.__dict__.copy()
map["balance"] = str(acct.balance()) map["balance"] = str(acct.balance())
map["negbalance"] = str(-acct.balance()) map["negbalance"] = str(-acct.balance())
map["negbalance_str"] = acct.negbalance_str() map["negbalance_str"] = acct.negbalance_str()
return map return map
def acct_inventory_map(self, acct): def acct_inventory_map(self, acct):
buy, sell = acct.currency.rates(currency) buy, sell = acct.currency.rates(currency)
map = acct.__dict__.copy() map = acct.__dict__.copy()
map["balance"] = "{:.0f}".format(acct.balance()) map["balance"] = "{:.0f}".format(acct.balance())
map["buy_price"] = str(buy) map["buy_price"] = str(buy)
map["price"] = str(sell) map["price"] = str(sell)
return map return map
def acct_map(self, acct): def acct_map(self, acct):
if acct is None: if acct is None:
return None return None
if acct.acctype == 'debt': if acct.acctype == 'debt':
return self.acct_debt_map(acct) return self.acct_debt_map(acct)
elif acct.acctype == "inventory": elif acct.acctype == "inventory":
return self.acct_inventory_map(acct) return self.acct_inventory_map(acct)
else: else:
return None return None
@QtCore.Slot(str, result='QVariant') @QtCore.Slot(str, result='QVariant')
def barcodeInput(self, barcode): def barcodeInput(self, barcode):
""" Evaluate barcode received on input """ Evaluate barcode received on input
Normally, we would return just the account object, but Normally, we would return just the account object, but
passing that to QML appears to be very non-trivial. passing that to QML appears to be very non-trivial.
Therefore, we construct a map that we can pass around easily. Therefore, we construct a map that we can pass around easily.
We return None on unrecognized barcode. """ We return None on unrecognized barcode. """
barcode = str(barcode) barcode = str(barcode)
if barcode and barcode[0] == "$": if barcode and barcode[0] == "$":
credits = {'$02': 20, '$05': 50, '$10': 100, '$20': 200, '$50': 500, '$1k': 1000} credits = {'$02': 20, '$05': 50, '$10': 100, '$20': 200, '$50': 500, '$1k': 1000}
credit = credits[barcode] credit = credits[barcode]
if credit is None: if credit is None:
return None return None
return { "acctype": "recharge", "amount": str(credit)+".00" } return { "acctype": "recharge", "amount": str(credit)+".00" }
acct = self.acct_map(brmbar.Account.load_by_barcode(db, barcode)) acct = self.acct_map(brmbar.Account.load_by_barcode(db, barcode))
db.commit() db.commit()
return acct return acct
@QtCore.Slot('QVariant', result='QVariant') @QtCore.Slot('QVariant', result='QVariant')
def loadAccount(self, dbid): def loadAccount(self, dbid):
acct = self.acct_map(brmbar.Account.load(db, id = dbid)) acct = self.acct_map(brmbar.Account.load(db, id = dbid))
db.commit() db.commit()
return acct return acct
@QtCore.Slot('QVariant', 'QVariant', result='QVariant') @QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def sellItem(self, itemid, userid): def sellItem(self, itemid, userid):
user = brmbar.Account.load(db, id = userid) user = brmbar.Account.load(db, id = userid)
shop.sell(item = brmbar.Account.load(db, id = itemid), user = user) shop.sell(item = brmbar.Account.load(db, id = itemid), user = user)
balance = user.negbalance_str() balance = user.negbalance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot('QVariant', result='QVariant') @QtCore.Slot('QVariant', result='QVariant')
def sellItemCash(self, itemid): def sellItemCash(self, itemid):
shop.sell_for_cash(item = brmbar.Account.load(db, id = itemid)) shop.sell_for_cash(item = brmbar.Account.load(db, id = itemid))
db.commit() db.commit()
@QtCore.Slot('QVariant', 'QVariant', result='QVariant') @QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def chargeCredit(self, credit, userid): def chargeCredit(self, credit, userid):
user = brmbar.Account.load(db, id = userid) user = brmbar.Account.load(db, id = userid)
shop.add_credit(credit = credit, user = user) shop.add_credit(credit = credit, user = user)
balance = user.negbalance_str() balance = user.negbalance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot('QVariant', 'QVariant', result='QVariant') @QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def withdrawCredit(self, credit, userid): def withdrawCredit(self, credit, userid):
user = brmbar.Account.load(db, id = userid) user = brmbar.Account.load(db, id = userid)
shop.withdraw_credit(credit = credit, user = user) shop.withdraw_credit(credit = credit, user = user)
balance = user.negbalance_str() balance = user.negbalance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot(result='QVariant') @QtCore.Slot(result='QVariant')
def balance_cash(self): def balance_cash(self):
balance = shop.cash.balance_str() balance = shop.cash.balance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot(result='QVariant') @QtCore.Slot(result='QVariant')
def balance_profit(self): def balance_profit(self):
balance = shop.profits.balance_str() balance = shop.profits.balance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot(result='QVariant') @QtCore.Slot(result='QVariant')
def balance_inventory(self): def balance_inventory(self):
balance = shop.inventory_balance_str() balance = shop.inventory_balance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot(result='QVariant') @QtCore.Slot(result='QVariant')
def balance_credit(self): def balance_credit(self):
balance = shop.credit_negbalance_str() balance = shop.credit_negbalance_str()
db.commit() db.commit()
return balance return balance
@QtCore.Slot(result='QVariant') @QtCore.Slot(result='QVariant')
def userList(self): def userList(self):
alist = [ self.acct_debt_map(a) for a in shop.account_list("debt") ] alist = [ self.acct_debt_map(a) for a in shop.account_list("debt") ]
db.commit() db.commit()
return alist return alist
@QtCore.Slot(result='QVariant') @QtCore.Slot(result='QVariant')
def itemList(self): def itemList(self):
alist = [ self.acct_inventory_map(a) for a in shop.account_list("inventory") ] alist = [ self.acct_inventory_map(a) for a in shop.account_list("inventory") ]
db.commit() db.commit()
return alist return alist
@QtCore.Slot('QVariant', 'QVariant', result='QVariant') @QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def addBarcode(self, dbid, barcode): def addBarcode(self, dbid, barcode):
acct = brmbar.Account.load(db, id = dbid).add_barcode(barcode) acct = brmbar.Account.load(db, id = dbid).add_barcode(barcode)
db.commit() db.commit()
return acct return acct
@QtCore.Slot('QVariant', 'QVariant', result='QVariant') @QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def saveItem(self, dbid, invmap): def saveItem(self, dbid, invmap):
acct = brmbar.Account.load(db, id = dbid) acct = brmbar.Account.load(db, id = dbid)
if (acct.name != invmap["name"]): if (acct.name != invmap["name"]):
acct.rename(invmap["name"]) acct.rename(invmap["name"])
buy, sell = acct.currency.rates(currency) buy, sell = acct.currency.rates(currency)
if (sell != invmap["price"]): if (sell != invmap["price"]):
acct.currency.update_sell_rate(currency, invmap["price"]) acct.currency.update_sell_rate(currency, invmap["price"])
if (buy != invmap["buy_price"]): if (buy != invmap["buy_price"]):
acct.currency.update_buy_rate(currency, invmap["buy_price"]) acct.currency.update_buy_rate(currency, invmap["buy_price"])
cost = "" cost = ""
if (acct.balance() < int(invmap["balance"])): if (acct.balance() < int(invmap["balance"])):
cost = shop.buy_for_cash(acct, invmap["balance"] - acct.balance()) cost = shop.buy_for_cash(acct, invmap["balance"] - acct.balance())
else: else:
db.commit() db.commit()
return { "dbid": dbid, "cost": (currency.str(cost) if cost != "" else "") } return { "dbid": dbid, "cost": (currency.str(cost) if cost != "" else "") }
@QtCore.Slot('QVariant', result='QVariant') @QtCore.Slot('QVariant', result='QVariant')
def newItem(self, invmap): def newItem(self, invmap):
if (invmap["name"] == "" or invmap["price"] == "" or invmap["buy_price"] == ""): if (invmap["name"] == "" or invmap["price"] == "" or invmap["buy_price"] == ""):
return None return None
invcurrency = brmbar.Currency.create(db, invmap["name"]) invcurrency = brmbar.Currency.create(db, invmap["name"])
invcurrency.update_sell_rate(currency, invmap["price"]) invcurrency.update_sell_rate(currency, invmap["price"])
invcurrency.update_buy_rate(currency, invmap["buy_price"]) invcurrency.update_buy_rate(currency, invmap["buy_price"])
acct = brmbar.Account.create(db, invmap["name"], invcurrency, "inventory") acct = brmbar.Account.create(db, invmap["name"], invcurrency, "inventory")
cost = "" cost = ""
if (int(invmap["balance"]) > 0): if (int(invmap["balance"]) > 0):
cost = shop.buy_for_cash(acct, invmap["balance"]) # implicit db.commit() cost = shop.buy_for_cash(acct, invmap["balance"]) # implicit db.commit()
else: else:
db.commit() db.commit()
return { "dbid": acct.id, "cost": (currency.str(cost) if cost != "" else "") } return { "dbid": acct.id, "cost": (currency.str(cost) if cost != "" else "") }
@QtCore.Slot('QVariant', 'QVariant', 'QVariant', result='QVariant') @QtCore.Slot('QVariant', 'QVariant', 'QVariant', result='QVariant')
def newReceipt(self, userid, description, amount): def newReceipt(self, userid, description, amount):
if (description == "" or amount == ""): if (description == "" or amount == ""):
return None return None
user = brmbar.Account.load(db, id = userid) user = brmbar.Account.load(db, id = userid)
shop.receipt_to_credit(user, amount, description) shop.receipt_to_credit(user, amount, description)
balance = user.negbalance_str() balance = user.negbalance_str()
db.commit() db.commit()
return balance return balance
db = psycopg2.connect("dbname=brmbar") db = psycopg2.connect("dbname=brmbar")
shop = brmbar.Shop.new_with_defaults(db) shop = brmbar.Shop.new_with_defaults(db)