From 25298f913cb3c33ed0702166927a1a3c25f53900 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Tue, 7 Apr 2015 04:06:56 +0200 Subject: cashbox: add page with details --- src/database/database.vala | 21 ++++++++ src/database/db-interface.vala | 1 + src/web/web.vala | 109 +++++++++++++++++++++++++++++++++++++++ templates/cashbox/detail.html | 26 ++++++++++ templates/cashbox/index.html | 1 + templates/cashbox/selection.html | 17 ++++++ 6 files changed, 175 insertions(+) create mode 100644 templates/cashbox/detail.html create mode 100644 templates/cashbox/selection.html diff --git a/src/database/database.vala b/src/database/database.vala index 012842b..f0b90c1 100644 --- a/src/database/database.vala +++ b/src/database/database.vala @@ -128,6 +128,7 @@ public class DataBase : Object { 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"; + queries["cashbox_changes"] = "SELECT user, amount, timestamp FROM cashbox_diff WHERE timestamp >= ? and timestamp < ? ORDER BY timestamp ASC"; /* compile queries into statements */ foreach(var entry in queries.entries) { @@ -948,4 +949,24 @@ public class DataBase : Object { return result; } + + public CashboxDiff[] cashbox_changes(int64 start, int64 stop) { + CashboxDiff[] result = {}; + + statements["cashbox_changes"].reset(); + statements["cashbox_changes"].bind_int64(1, start); + statements["cashbox_changes"].bind_int64(2, stop); + + while(statements["cashbox_changes"].step() == Sqlite.ROW) { + CashboxDiff entry = { + statements["cashbox_changes"].column_int(0), + statements["cashbox_changes"].column_int(1), + statements["cashbox_changes"].column_int64(2), + }; + + result += entry; + }; + + return result; + } } diff --git a/src/database/db-interface.vala b/src/database/db-interface.vala index 579d5a9..b61cba7 100644 --- a/src/database/db-interface.vala +++ b/src/database/db-interface.vala @@ -54,6 +54,7 @@ public interface Database : Object { 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 abstract CashboxDiff[] cashbox_changes(int64 start, int64 stop) throws IOError; } public struct StockEntry { diff --git a/src/web/web.vala b/src/web/web.vala index a31aeec..a8ab59f 100644 --- a/src/web/web.vala +++ b/src/web/web.vala @@ -1067,6 +1067,114 @@ public class WebServer { } } + void handler_cashbox_detail_selection(Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client) { + string[] pathparts = path.split("/"); + + if(pathparts.length > 4) { + DateYear year = (DateYear) int.parse(pathparts[3]); + DateMonth month = (DateMonth) int.parse(pathparts[4]); + handler_cashbox_detail(server, msg, path, query, client, year, month); + } else { + try { + var session = new WebSession(server, msg, path, query, client); + var template = new WebTemplate("cashbox/selection.html", session); + template.replace("TITLE", "KtT Shop System: Cashbox Detail"); + template.menu_set_active("cashbox"); + msg.set_response("text/html", Soup.MemoryUse.COPY, template.data); + } catch(TemplateError e) { + stderr.printf(e.message+"\n"); + handler_404(server, msg, path, query, client); + } catch(DatabaseError e) { + stderr.printf(e.message+"\n"); + handler_400(server, msg, path, query, client); + } catch(IOError e) { + stderr.printf(e.message+"\n"); + handler_400(server, msg, path, query, client); + } + } + } + + void handler_cashbox_detail(Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client, DateYear year, DateMonth month) { + try { + var session = new WebSession(server, msg, path, query, client); + + if(!session.superuser) { + handler_403(server, msg, path, query, client); + return; + } + + if(!year.valid() || year > 8000) { + handler_403(server, msg, path, query, client); + return; + } + + if(!month.valid()) { + handler_403(server, msg, path, query, client); + return; + } + + var start = new DateTime.local(year, month, 1, 0, 0, 0); + var stop = start.add_months(1); + + /* guest debit */ + Price debit = 0; + foreach(var e in db.get_invoice(0, start.to_unix(), stop.to_unix())) { + debit += e.price; + } + + Price loss = 0; + string loss_list = ""; + Price donation = 0; + string donation_list = ""; + Price withdrawal = 0; + string withdrawal_list = ""; + foreach(var e in db.cashbox_changes(start.to_unix(), stop.to_unix())) { + var dt = new DateTime.from_unix_local(e.timestamp); + var dts = dt.format("%Y-%m-%d %H:%M:%S"); + + if(e.user == -3) { + if(e.amount < 0) { + loss += e.amount; + loss_list += @"$(dts)$(e.amount) €"; + } else { + donation += e.amount; + donation_list += @"$(dts)$(e.amount) €"; + } + } else { + var ui = db.get_user_info(e.user); + var name = @"$(ui.firstname) $(ui.lastname)"; + withdrawal += e.amount; + withdrawal_list += @"$(dts)$(name)$(e.amount) €"; + } + } + + var template = new WebTemplate("cashbox/detail.html", session); + template.replace("TITLE", "KtT Shop System: Cashbox Detail"); + template.menu_set_active("cashbox"); + + template.replace("DATE", start.format("%B %Y")); + template.replace("DEBIT", debit.to_string()); + template.replace("LOSS", loss.to_string()); + template.replace("DONATION", donation.to_string()); + template.replace("WITHDRAWAL", withdrawal.to_string()); + + template.replace("LOSS_LIST", loss_list); + template.replace("DONATION_LIST", donation_list); + template.replace("WITHDRAWAL_LIST", withdrawal_list); + + msg.set_response("text/html", Soup.MemoryUse.COPY, template.data); + } catch(TemplateError e) { + stderr.printf(e.message+"\n"); + handler_404(server, msg, path, query, client); + } catch(DatabaseError e) { + stderr.printf(e.message+"\n"); + handler_400(server, msg, path, query, client); + } catch(IOError e) { + stderr.printf(e.message+"\n"); + handler_400(server, msg, path, query, client); + } + } + public WebServer(uint port = 8080, TlsCertificate? cert = null) throws Error { srv = new Soup.Server("tls-certificate", cert); Soup.ServerListenOptions options = 0; @@ -1092,6 +1200,7 @@ public class WebServer { /* cashbox */ srv.add_handler("/cashbox", handler_cashbox); srv.add_handler("/cashbox/add", handler_cashbox_add); + srv.add_handler("/cashbox/detail", handler_cashbox_detail_selection); /* products */ srv.add_handler("/products", handler_products); diff --git a/templates/cashbox/detail.html b/templates/cashbox/detail.html new file mode 100644 index 0000000..41d057a --- /dev/null +++ b/templates/cashbox/detail.html @@ -0,0 +1,26 @@ +

Cashbox Account {{{DATE}}}

+ + + + + + +
Debit{{{DEBIT}}} €
Loss{{{LOSS}}} €
Donation{{{DONATION}}} €
Withdrawal{{{WITHDRAWAL}}} €
+ +

Loss

+ + + {{{LOSS_LIST}}} +
Date & TimeAmount
+ +

Donation

+ + + {{{DONATION_LIST}}} +
Date & TimeAmount
+ +

Withdrawals

+ + + {{{WITHDRAWAL_LIST}}} +
Date & TimeNameAmount
diff --git a/templates/cashbox/index.html b/templates/cashbox/index.html index dc5e561..d5a03a0 100644 --- a/templates/cashbox/index.html +++ b/templates/cashbox/index.html @@ -17,4 +17,5 @@ Date & TimeNameAmount {{{CASHBOX_HISTORY}}} + Details for a specific month diff --git a/templates/cashbox/selection.html b/templates/cashbox/selection.html new file mode 100644 index 0000000..e15ee75 --- /dev/null +++ b/templates/cashbox/selection.html @@ -0,0 +1,17 @@ +

Cashbox Account

+ + + +
+ + +
-- cgit v1.2.3