Console version kind of ready.

master
q3k 2012-04-27 08:38:51 +00:00
parent e0c0dd3e58
commit 70b94f9b5e
2 changed files with 92 additions and 13 deletions

View File

@ -8,6 +8,7 @@ actions = {
"topup-add": (logic.add_topup, [("user_id", "string"), ("value", "string")], False),
"purchase-add": (logic.add_purchase, [("user_id", "string"), ("product_id", "strings")], False),
"product-add": (logic.add_product, [("name", "string"), ("code", "string"), ("value", "int"), ("cost", "int")], False),
"restock-add": (logic.add_restock, [("product_id", "string"), ("title", "string"), ("count", "integer")], False),
}
def start():

View File

@ -28,7 +28,7 @@ def initialize(db):
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(40) NOT NULL,
code VARCHAR(100) NOT NULL,
cost INT(11), NOT NULL,
cost INT(11) NOT NULL,
value INT(11) NOT NULL);""" % config.db_prefix)
__sql_execute(db, """CREATE TABLE %srestocks (
@ -48,16 +48,16 @@ def initialize(db):
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
value INT(11) NOT NULL,
datetime DATETIME NOT NULL,
user_id INT(11) NOT NULL);""" % config.db_prefix)
user_id VARCHAR(40) NOT NULL);""" % config.db_prefix)
__sql_execute(db, """CREATE TABLE %spurchases (
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
datetime DATETIME,
product_id INT(11) NOT NULL,
user_id INT(11) NOT NULL);""" % config.db_prefix)
user_id VARCHAR(40) NOT NULL);""" % config.db_prefix)
def add_product(db, name, code, value, cost):
__sql_execute(db, "INSERT INTO %sproducts (name, code, value, cost) VALUES (%%s, %%s, %%s);" % config.db_prefix, (name, code, value))
__sql_execute(db, "INSERT INTO %sproducts (name, code, value, cost) VALUES (%%s, %%s, %%s, %%s);" % config.db_prefix, (name, code, value, cost))
def add_restock(db, product_id, title, count):
@ -74,7 +74,10 @@ def add_purchase(db, user_id, product_id):
def print_info(db):
f = get_financing_data(db)
u = get_user_data(db)
u = get_users_data(db)
s = get_summary(db)
r = get_restocks_data(db)
p = get_products_data(db)
print " FINANCING"
print "-" * 76
@ -95,15 +98,28 @@ def print_info(db):
print "% 63s | % 10.2f" % (user_id, balance/100.0)
print "-" * 76
real_money = 0
for topup in u["topups"]:
real_money += topup["value"]
print "\n\n PRODUCTS"
print "-" * 76
print "% 24s | % 10s | % 10s | % 10s | % 10s" % ("name", "sold", "in stock", "revenue", "profit")
print "-" * 76
for product_id, product in p["products"].iteritems():
print "% 24s | % 10i | % 10i | % 10.2f | % 10.2f" % (product["name"], product["sold"], product["available"], product["revenue"]/100.0, product["profit"]/100.0)
print "-" * 76
print "\n\n RESTOCKS"
print "-" * 76
print "% 21s | % 15s | % 15s | % 6s | % 6s" % ("title", "date", "product", "count", "cost")
print "-" * 76
for restock in r["restocks"]:
print "% 21s | % 15s | % 15s | % 6i | % 6.2f" % (restock["title"], restock["datetime"].strftime("%H:%M %m/%d/%y"), restock["name"], restock["count"], restock["count"] * restock["cost"]/100.0)
print "-" * 76
print "\n\n"
print "Revenue (without financing): %.2f" % (real_money/100.0)
real_money += f["financings_amount"]
print "Revenue (with financing): %.2f" % (real_money/100.0)
print "Revenue (without financing): %.2f" % (s["revenue"]/100.0)
print "Revenue (with financing): %.2f" % (s["revenue_financing"]/100.0)
print "Expenses : %.2f" % (s["expenses"]/100.0)
print "Profit : %.2f" % (s["profit"]/100.0)
print "Vault : %.2f" % (s["real_money"]/100.0)
def get_financing_data(db):
financings = __sql_execute(db, "SELECT title, datetime, value from %sfinancing;" % config.db_prefix)
@ -113,8 +129,70 @@ def get_financing_data(db):
return {"financings": financings, "financings_amount": financings_amount}
def get_restocks_data(db):
restocks = __sql_execute(db, "SELECT %srestocks.product_id, %srestocks.title, %srestocks.datetime, %srestocks.count, %sproducts.cost, %sproducts.name FROM %srestocks LEFT JOIN %sproducts on %srestocks.product_id = %sproducts.id;" % (config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix))
return {"restocks": restocks}
def get_user_data(db):
def get_products_data(db):
products = __sql_execute(db, "SELECT id, name, cost, value, code FROM %sproducts;" % config.db_prefix)
purchases = __sql_execute(db, "SELECT COUNT(id), product_id FROM %spurchases GROUP BY product_id;" % config.db_prefix)
purchases_dict = dict([(purchase["product_id"], purchase["COUNT(id)"]) for purchase in purchases])
restocks = get_restocks_data(db)["restocks"]
restocks_dict = {}
for restock in restocks:
if restock["product_id"] not in restocks_dict:
restocks_dict[restock["product_id"]] = 0
restocks_dict[restock["product_id"]] += restock["count"]
r = {}
for product in products:
p = {}
sold = 0
if product["id"] in purchases_dict:
sold += purchases_dict[product["id"]]
stocked = 0
if product["id"] in restocks_dict:
stocked = restocks_dict[product["id"]]
p["name"] = product["name"]
p["sold"] = sold
p["stocked"] = stocked
p["available"] = stocked - sold
p["value"] = product["value"]
p["cost"] = product["cost"]
p["revenue"] = product["value"] * sold
p["profit"] = p["revenue"] - product["cost"] * sold
p["code"] = product["code"]
r[product["id"]] = p
return {"products": r}
def get_summary(db):
u = get_users_data(db)
f = get_financing_data(db)
real_money = 0
for topup in u["topups"]:
real_money += topup["value"]
revenue = real_money
real_money += f["financings_amount"]
revenue_financing = real_money
restocks = get_restocks_data(db)["restocks"]
expenses = 0
for restock in restocks:
expenses += int(restock["count"]) * int (restock["cost"])
profit = revenue - expenses
real_money -= expenses
return {"revenue": revenue, "revenue_financing": revenue_financing, "expenses": expenses, "profit": profit, "real_money": real_money}
def get_users_data(db):
topups = [{"user_id": topup["user_id"], "value": int(topup["SUM(value)"])} for topup in __sql_execute(db, "SELECT user_id, SUM(value) FROM %stopups GROUP BY user_id" % config.db_prefix)]
users = dict([(topup["user_id"], topup["value"]) for topup in topups])