diff options
author | Sebastian Reichel <sre@ring0.de> | 2012-08-01 20:39:39 +0200 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2012-08-01 20:39:39 +0200 |
commit | 501c116cee7bd4839eb68314129e26c0b2861307 (patch) | |
tree | 0a98a265c29a035b4fa3ad8fabaad40694231f74 | |
parent | ce1b833aec12f16c066c11c9b049f5c1270fd8ec (diff) | |
download | serial-barcode-scanner-501c116cee7bd4839eb68314129e26c0b2861307.tar.bz2 |
print price for purchases
-rw-r--r-- | db.vala | 32 | ||||
-rw-r--r-- | main.vala | 2 |
2 files changed, 33 insertions, 1 deletions
@@ -8,6 +8,7 @@ public class Database { private Sqlite.Statement undo_stmt3; private Sqlite.Statement stock_stmt1; private Sqlite.Statement stock_stmt2; + private Sqlite.Statement price_stmt; int32 user = 0; uint64 product = 0; bool logged_in = false; @@ -20,6 +21,7 @@ public class Database { private static string undo_query3 = "UPDATE products SET amount = amount + 1 WHERE id = ?"; private static string stock_query1 = "INSERT INTO restock ('user', 'product', 'amount', 'timestamp') VALUES (?, ?, ?, ?)"; private static string stock_query2 = "UPDATE products SET amount = amount + ? WHERE id = ?"; + private static string price_query = "SELECT memberprice, guestprice FROM prices WHERE product = ? AND valid_from <= ? ORDER BY valid_from DESC LIMIT 1"; public Database(string file) { int rc; @@ -69,6 +71,11 @@ public class Database { error("could not prepare second stock statement!"); } + rc = this.db.prepare_v2(price_query, -1, out price_stmt); + if(rc != Sqlite.OK) { + error("could not prepare price statement!"); + } + } public bool login(int32 id) { @@ -127,6 +134,31 @@ public class Database { } } + public int get_product_price(uint64 article) { + int64 timestamp = (new DateTime.now_utc()).to_unix(); + bool member = user != 0; + + this.price_stmt.reset(); + this.price_stmt.bind_text(1, "%llu".printf(article)); + this.price_stmt.bind_text(1, "%lld".printf(timestamp)); + + int rc = this.price_stmt.step(); + + switch(rc) { + case Sqlite.ROW: + if(member) + return this.price_stmt.column_int(0); + else + return this.price_stmt.column_int(1); + case Sqlite.DONE: + stderr.printf("unbekanntes Produkt: %llu", article); + return 0; + default: + stderr.printf("[interner Fehler: %d]", rc); + return 0; + } + } + public bool undo() { if(is_logged_in()) { uint64 pid = 0; @@ -106,7 +106,7 @@ public static bool interpret(string data) { } if(db.buy(id)) { - stdout.printf("[%lld] gekaufter Artikel: %s\n", timestamp, db.get_product_name(id)); + stdout.printf("[%lld] gekaufter Artikel: %s (%d,%02d €)\n", timestamp, db.get_product_name(id), db.get_product_price(id)/100, db.get_product_price(id) % 100); return true; } else { stdout.printf("[%lld] Kauf fehlgeschlagen!\n", timestamp); |