summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2015-04-07 04:06:56 +0200
committerSebastian Reichel <sre@ring0.de>2015-04-07 04:06:56 +0200
commit25298f913cb3c33ed0702166927a1a3c25f53900 (patch)
tree9e59baddecf55461a5571831f9773cdb77b8bd61
parent20afb9ff0575eeb84bb6b00b0d9dfcab001c3127 (diff)
downloadserial-barcode-scanner-25298f913cb3c33ed0702166927a1a3c25f53900.tar.bz2
cashbox: add page with details
-rw-r--r--src/database/database.vala21
-rw-r--r--src/database/db-interface.vala1
-rw-r--r--src/web/web.vala109
-rw-r--r--templates/cashbox/detail.html26
-rw-r--r--templates/cashbox/index.html1
-rw-r--r--templates/cashbox/selection.html17
6 files changed, 175 insertions, 0 deletions
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<string,string>? 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 += @"<tr><td>$(dts)</td><td class=\"text-right\">$(e.amount) €</td></tr>";
+ } else {
+ donation += e.amount;
+ donation_list += @"<tr><td>$(dts)</td><td class=\"text-right\">$(e.amount) €</td></tr>";
+ }
+ } else {
+ var ui = db.get_user_info(e.user);
+ var name = @"$(ui.firstname) $(ui.lastname)";
+ withdrawal += e.amount;
+ withdrawal_list += @"<tr><td>$(dts)</td><td>$(name)</td><td class=\"text-right\">$(e.amount) €</td></tr>";
+ }
+ }
+
+ 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 @@
+<h1>Cashbox Account {{{DATE}}}</h1>
+
+<table class="table table-bordered table-striped table-hover table-condensed table-nonfluid">
+ <tr><th>Debit</th><td class="text-right">{{{DEBIT}}} €</td></tr>
+ <tr><th>Loss</th><td class="text-right">{{{LOSS}}} €</td></tr>
+ <tr><th>Donation</th><td class="text-right">{{{DONATION}}} €</td></tr>
+ <tr><th>Withdrawal</th><td class="text-right">{{{WITHDRAWAL}}} €</td></tr>
+</table>
+
+<h2>Loss</h2>
+<table class="table table-bordered table-striped table-hover table-condensed table-nonfluid">
+ <tr><th>Date &amp; Time</th><th>Amount</th></tr>
+ {{{LOSS_LIST}}}
+</table>
+
+<h2>Donation</h2>
+<table class="table table-bordered table-striped table-hover table-condensed table-nonfluid">
+ <tr><th>Date &amp; Time</th><th>Amount</th></tr>
+ {{{DONATION_LIST}}}
+</table>
+
+<h2>Withdrawals</h2>
+<table class="table table-bordered table-striped table-hover table-condensed table-nonfluid">
+ <tr><th>Date &amp; Time</th><th>Name</th><th>Amount</th></tr>
+ {{{WITHDRAWAL_LIST}}}
+</table>
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 @@
<tr><td>Date &amp; Time</td><td>Name</td><td>Amount</td></tr>
{{{CASHBOX_HISTORY}}}
</table>
+ <a href="/cashbox/details">Details for a specific month</a>
</form>
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 @@
+<h1>Cashbox Account</h1>
+
+<script>
+function onSubmit() {
+ d = document.getElementsByName("date")[0].value
+ if (d.indexOf("-") > 0) {
+ var split = d.split("-")
+ var uri = location.protocol + '//' + location.host + location.pathname + "/" + split[0] + "/" + split[1]
+ self.location = uri;
+ }
+}
+</script>
+
+<form>
+ <input type="month" name="date" />
+ <input type="button" value="Go" onclick="onSubmit()" />
+</form>