diff options
author | Sebastian Reichel <sre@ring0.de> | 2015-03-08 17:10:52 +0100 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2015-03-08 17:10:52 +0100 |
commit | 064552cadcaf96ad9d22aaee7103e1e59826e5bf (patch) | |
tree | b37f395f65d13cd4778377586fbc4127769f19f1 | |
parent | cc8960519c0a1b625c4a4073920277f39c848d4c (diff) | |
download | serial-barcode-scanner-064552cadcaf96ad9d22aaee7103e1e59826e5bf.tar.bz2 |
web: cashbox: try makeing update function foolproof and add history
-rw-r--r-- | src/database/database.vala | 18 | ||||
-rw-r--r-- | src/database/db-interface.vala | 7 | ||||
-rw-r--r-- | src/web/web.vala | 54 | ||||
-rw-r--r-- | templates/cashbox/index.html | 19 | ||||
-rw-r--r-- | templates/css/base.css | 8 |
5 files changed, 95 insertions, 11 deletions
diff --git a/src/database/database.vala b/src/database/database.vala index 9c4e495..012842b 100644 --- a/src/database/database.vala +++ b/src/database/database.vala @@ -127,6 +127,7 @@ public class DataBase : Object { queries["user_invoice_sum"] = "SELECT SUM(CASE WHEN user < 0 THEN (SELECT price FROM purchaseprices WHERE purchaseprices.product = id) else (SELECT CASE WHEN user=0 THEN guestprice else memberprice END FROM prices WHERE product = id AND valid_from <= timestamp ORDER BY valid_from DESC LIMIT 1) END) FROM sales INNER JOIN products ON sales.product = products.id WHERE user = ? AND timestamp >= ? AND timestamp <= ? ORDER BY timestamp"; queries["cashbox_status"] = "SELECT amount FROM current_cashbox_status"; queries["cashbox_add"] = "INSERT INTO cashbox_diff ('user', 'amount', 'timestamp') VALUES (?, ?, ?)"; + queries["cashbox_history"] = "SELECT user, amount, timestamp FROM cashbox_diff ORDER BY timestamp DESC LIMIT 10"; /* compile queries into statements */ foreach(var entry in queries.entries) { @@ -928,6 +929,23 @@ public class DataBase : Object { if(rc != Sqlite.DONE) { throw new DatabaseError.INTERNAL_ERROR("internal error: %d", rc); } + } + + public CashboxDiff[] cashbox_history() { + CashboxDiff[] result = {}; + + statements["cashbox_history"].reset(); + + while(statements["cashbox_history"].step() == Sqlite.ROW) { + CashboxDiff entry = { + statements["cashbox_history"].column_int(0), + statements["cashbox_history"].column_int(1), + statements["cashbox_history"].column_int64(2), + }; + + result += entry; + }; + return result; } } diff --git a/src/database/db-interface.vala b/src/database/db-interface.vala index 979abf5..579d5a9 100644 --- a/src/database/db-interface.vala +++ b/src/database/db-interface.vala @@ -53,6 +53,7 @@ public interface Database : Object { public abstract Price get_user_invoice_sum(int user, int64 timestamp_from, int64 timestamp_to) throws IOError; public abstract Price cashbox_status() throws IOError; public abstract void cashbox_add(int user, Price amount, int64 timestamp) throws IOError, DatabaseError; + public abstract CashboxDiff[] cashbox_history() throws IOError; } public struct StockEntry { @@ -133,6 +134,12 @@ public struct InvoiceEntry { Price price; } +public struct CashboxDiff { + public int user; + public Price amount; + public int64 timestamp; +} + public struct StatsInfo { public int count_articles; public int count_users; diff --git a/src/web/web.vala b/src/web/web.vala index 05a9b77..a31aeec 100644 --- a/src/web/web.vala +++ b/src/web/web.vala @@ -957,8 +957,30 @@ public class WebServer { } var template = new WebTemplate("cashbox/index.html", session); + var status = db.cashbox_status().to_string(); + var history = db.cashbox_history(); + + var hist = ""; + foreach(var diff in history) { + var dt = new DateTime.from_unix_local(diff.timestamp); + var dts = dt.format("%Y-%m-%d %H:%M:%S"); + var name = ""; + if(diff.user != -3) { + var ui = db.get_user_info(diff.user); + name = @"$(ui.firstname) $(ui.lastname)"; + } else if(diff.amount < 0) { + name = "Loss"; + } else { + name = "Donation"; + } + hist += "<tr>"; + hist += @"<td>$(dts)</td><td>$(name)</td><td class=\"text-right\">$(diff.amount) €</td>"; + hist += "</tr>\n"; + } + template.replace("TITLE", "KtT Shop System: Cashbox"); - template.replace("CASHBOX_STATUS", db.cashbox_status().to_string()); + template.replace("CASHBOX_STATUS", status); + template.replace("CASHBOX_HISTORY", hist); template.menu_set_active("cashbox"); msg.set_response("text/html", Soup.MemoryUse.COPY, template.data); } catch(TemplateError e) { @@ -994,11 +1016,31 @@ public class WebServer { Price amount = Price.parse(query["amount"]); string type = query["type"]; - if(type != "user" && type != "loss") - error = true; - - if(!error) - db.cashbox_add(type == "user" ? session.user : -3, amount, timestamp); + switch(type) { + case "withdrawal": + if(amount > 0) + amount *= -1; + db.cashbox_add(session.user, amount, timestamp); + break; + case "deposit": + if(amount < 0) + amount *= -1; + db.cashbox_add(session.user, amount, timestamp); + break; + case "loss": + if(amount > 0) + amount *= -1; + db.cashbox_add(-3, amount, timestamp); + break; + case "donation": + if(amount < 0) + amount *= -1; + db.cashbox_add(-3, amount, timestamp); + break; + default: + error = true; + break; + } if(error) { template.replace("TYPE", ""); diff --git a/templates/cashbox/index.html b/templates/cashbox/index.html index 1466272..dc5e561 100644 --- a/templates/cashbox/index.html +++ b/templates/cashbox/index.html @@ -1,11 +1,20 @@ -Current Cashbox Status: <input name"status" type="number" readonly="readonly" value="{{{CASHBOX_STATUS}}}"/> +<legend>Current Cashbox Status</legend> + +<input name"status" type="number" readonly="readonly" value="{{{CASHBOX_STATUS}}}"/> <form action="/cashbox/add" class="form-horizontal"> - <legend>Cashbox Balance/Withdrawal/Deposit</legend> + <legend>Update Cashbox</legend> <select name="type" size="1"> - <option value="loss">Balance (Loss)</option> - <option value="user">Withdrawal / Deposit</option> + <option value="loss">Loss (Money is missing from the cashbox)</option> + <option value="withdrawal">Withdrawal (You removed money from the cashbox)</option> + <option value="donation">Donation (Cashbox has more money, than there should be)</option> + <option value="deposit">Deposit (You added money to the cashbox)</option> </select> - <input class="input-medium" name="amount" type="number" placeholder="Amount" /> + <input class="input-medium" name="amount" type="number" placeholder="Amount (in Cent)" /> <button type="submit" class="btn btn-primary"><i class="icon-plus"></i></button> + <legend>History (last ten updates)</legend> + <table class="table table-bordered table-striped table-hover table-condensed table-nonfluid"> + <tr><td>Date & Time</td><td>Name</td><td>Amount</td></tr> + {{{CASHBOX_HISTORY}}} + </table> </form> diff --git a/templates/css/base.css b/templates/css/base.css index 48407c8..3b30711 100644 --- a/templates/css/base.css +++ b/templates/css/base.css @@ -67,3 +67,11 @@ .hidden { display: none; } + +.table-nonfluid { + width: auto !important; +} + +.text-right { + text-align: right !important; +} |