From 29dce6f165c45ab209912e7ca0d240ae590fc3c4 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Sun, 27 May 2012 23:42:00 +0200 Subject: add method to send info about current stock --- invoice/generate-invoice.py | 48 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'invoice') diff --git a/invoice/generate-invoice.py b/invoice/generate-invoice.py index 3ac65b2..0af75c4 100755 --- a/invoice/generate-invoice.py +++ b/invoice/generate-invoice.py @@ -203,7 +203,51 @@ def monthly(timestamp = time.time()): def backup(timestamp = time.time()): print("backup()") -def stock(timestamp = time.time()): - print("stock()") +def get_stock_data(): + connection = sqlite3.connect('shop.db') + c = connection.cursor() + result = [] + + c.execute("SELECT name,amount FROM products") + + for row in c: + result.append((row[0],row[1])) + + c.close() + + return result + +def gen_stock_asciitable(): + stock = get_stock_data() + longest_name = 0 + longest_amount = 0 + asciitable = "" + for element in stock: + if len(element[0]) > longest_name: + longest_name = len(element[0]) + if len(str(element[1])) > longest_amount: + longest_amount = len(str(element[1])) + + asciitable = "+-" + longest_name * "-" + "-+-" + longest_amount * "-" + "-+\n" + asciitable += "| " + "Produkt" + (longest_name - len("Produkt")) * " " + " | " + (longest_amount - 1) * " " + "#" + " |\n" + asciitable += "+-" + longest_name * "-" + "-+-" + longest_amount * "-" + "-+\n" + for product in stock: + asciitable += "| " + product[0] + (longest_name - len(product[0])) * " " + " | " + (longest_amount - len(str(product[1]))) * " " + str(product[1]) + " |\n" + asciitable += "+-" + longest_name * "-" + "-+-" + longest_amount * "-" + "-+\n" + + return asciitable + +def gen_stock_mail(): + msg = MIMEMultipart() + msg["From"] = "KtT Shop System " + msg["To"] = "KtT Einkaufsteam " + msg["Subject"] = Header("Aktueller Warenbestand", 'utf-8') + msg.preamble = "Please use a MIME aware email client!" + msg.attach(MIMEText(gen_stock_asciitable(), 'plain', 'utf-8')) + return msg + +def stock(): + send_mail(gen_stock_mail(), "einkauf@kreativitaet-trifft-technik.de") +#stock() daily() -- cgit v1.2.3