diff options
Diffstat (limited to 'src/web')
-rw-r--r-- | src/web/Makefile | 2 | ||||
-rw-r--r-- | src/web/main.vala | 11 | ||||
-rw-r--r-- | src/web/web.vala | 145 |
3 files changed, 153 insertions, 5 deletions
diff --git a/src/web/Makefile b/src/web/Makefile index e6094f6..94aab97 100644 --- a/src/web/Makefile +++ b/src/web/Makefile @@ -1,7 +1,7 @@ all: web @echo > /dev/null -web: main.vala web.vala websession.vala csv.vala template.vala ../database/db-interface.vala ../pgp/pgp-interface.vala ../price.vapi ../config/config-interface.vala ../audio/audio-interface.vala +web: main.vala web.vala websession.vala csv.vala template.vala ../database/db-interface.vala ../pgp/pgp-interface.vala ../price.vapi ../config/config-interface.vala ../audio/audio-interface.vala ../pdf-stock/pdf-stock-interface.vala valac -X -w -o $@ --vapidir=../../vapi --enable-experimental --pkg gee-0.8 --pkg gio-2.0 --pkg libsoup-2.4 --pkg posix $^ clean: diff --git a/src/web/main.vala b/src/web/main.vala index 7070e66..aefe7fd 100644 --- a/src/web/main.vala +++ b/src/web/main.vala @@ -18,6 +18,7 @@ public CSVMemberFile csvimport; public PGP pgp; public Config cfg; public AudioPlayer audio; +public PDFStock pdfStock; string templatedir; public static int main(string[] args) { @@ -27,10 +28,12 @@ public static int main(string[] args) { uint port = 8080; try { - db = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Database", "/io/mainframe/shopsystem/database"); - pgp = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.PGP", "/io/mainframe/shopsystem/pgp"); - cfg = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Config", "/io/mainframe/shopsystem/config"); - audio = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.AudioPlayer", "/io/mainframe/shopsystem/audio"); + db = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Database", "/io/mainframe/shopsystem/database"); + pgp = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.PGP", "/io/mainframe/shopsystem/pgp"); + cfg = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Config", "/io/mainframe/shopsystem/config"); + audio = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.AudioPlayer", "/io/mainframe/shopsystem/audio"); + pdfStock = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.StockPDF", "/io/mainframe/shopsystem/stockpdf"); + templatedir = cfg.get_string("WEB", "filepath"); port = cfg.get_integer("WEB", "port"); diff --git a/src/web/web.vala b/src/web/web.vala index ea0c667..5a44409 100644 --- a/src/web/web.vala +++ b/src/web/web.vala @@ -721,6 +721,149 @@ public class WebServer { } } + void handler_stock_as_pdf(Soup.Server server, Soup.Message msg, string path, GLib.HashTable<string,string>? query, Soup.ClientContext client) { + try { + var session = new WebSession(server, msg, path, query, client); + if(!session.superuser && !session.auth_products) { + handler_403(server, msg, path, query, client); + return; + } + + var allProducts = query.contains("all"); + + var pdfdata = pdfStock.generate(allProducts); + msg.set_status(200); + msg.set_response("application/pdf", Soup.MemoryUse.COPY, pdfdata); + } catch(DatabaseError e) { + handler_400(server, msg, path, query, client, e.message); + } catch(IOError e) { + handler_400(server, msg, path, query, client, e.message); + } + } + + void handler_products_inventory(Soup.Server server, Soup.Message msg, string path, GLib.HashTable<string,string>? query, Soup.ClientContext client) { + try { + var session = new WebSession(server, msg, path, query, client); + var template = new WebTemplate("products/inventory.html", session); + template.replace("TITLE", "KtT Shop System: Inventory"); + template.menu_set_active("products"); + + if(!session.superuser && !session.auth_products) { + handler_403(server, msg, path, query, client); + return; + } + + string table = ""; + string actionTemplate = ""; + string restockClassTemplate = "hidden"; + string suppliersTemplate = ""; + string usersTemplate = ""; + string successClassTemplate = "hidden"; + if (msg.method == "POST") { + var postdata = Soup.Form.decode((string) msg.request_body.data); + + if (!postdata.contains("apply_inventory")) { + // PUT / show changes and request an apply + foreach(var e in db.get_stock()) { + var realAmountStr = postdata.get(e.id); + if (realAmountStr != null && realAmountStr.length > 0) { + var realAmount = int.parse(realAmountStr); + var amountStyleClass = "success"; + if (realAmount < e.amount) { + amountStyleClass = "error"; + } else if (realAmount > e.amount) { + amountStyleClass = "info"; + } + var diff = realAmount - e.amount; + table += @"<tr class='$(amountStyleClass)'><td>$(e.id)</td><td>$(e.name)</td><td>$(e.category)</td><td>$(e.amount)</td><td>" + + @"$(realAmount) <strong>[ $(diff) ]</strong><input type=\"hidden\" name=\"$(e.id)\" value=\"$(realAmount)\"></td></tr>"; + } + } + actionTemplate = """<input type="hidden" name="apply_inventory" value="true"><button type="submit" class="btn btn-primary">Apply Changes</button>"""; + + // a list of suppliers to choose + suppliersTemplate = "<option value=\"0\">Unknown</option>"; + foreach(var e in db.get_supplier_list()) { + suppliersTemplate += "<option value=\"%lld\">%s</option>".printf(e.id, e.name); + } + + // a list of users to choose + foreach(var uId in db.get_system_member_ids()) { + var user = db.get_user_info(uId); + usersTemplate += "<option value=\"%d\">%s %s (%d)</option>".printf(uId, user.firstname, user.lastname, uId); + } + + restockClassTemplate = ""; // this shows the option list + } else { + // PUT / apply changes + + var supplierId = int.parse(postdata.get("supplierId")); + var userId = int.parse(postdata.get("userId")); + foreach(var e in db.get_stock()) { + var realAmountStr = postdata.get(e.id); + if (realAmountStr != null && realAmountStr.length > 0) { + var pId = uint64.parse(e.id); + var realAmount = int.parse(realAmountStr); + if (realAmount < e.amount) { + // Loss transaction + + for (int i=0; i< e.amount - realAmount; i++) { + db.buy(userId, pId); + } + } else if (realAmount > e.amount) { + // Restock + + var amountDiff = realAmount - e.amount; + // find the latest bbd date + int64 maxBbd = 0; + foreach(var restock in db.get_restocks(pId, true)) { + if (restock.best_before_date > maxBbd) { + maxBbd = restock.best_before_date; + } + } + + //stderr.printf("restock %d for %d, diff %d, supplier %d\n", (int)maxBbd, (int)pId, amountDiff, supplierId); + db.restock(session.user, pId, amountDiff, 0, supplierId, maxBbd); + } + } + } + + msg.set_redirect(302, "/products/inventory?success=x"); + return; + } + } else { + // default GET / list products with a form + var tabindexCounter = 1; + foreach(var e in db.get_stock()) { + table += @"<tr><td><a href=\"/products/$(e.id)\">$(e.id)</a></td><td><a href=\"/products/$(e.id)\">$(e.name)</a></td><td>$(e.category)</td><td>$(e.amount)</td><td><input type=\"number\" name=\"$(e.id)\" tabindex=\"$(tabindexCounter)\"></td></tr>"; + tabindexCounter++; + } + actionTemplate = """<button type="submit" class="btn btn-primary">Preview</button>"""; + + if (query.contains("success")) { + successClassTemplate = ""; + } + } + + template.replace("SUCESS_HIDDEN", successClassTemplate); + template.replace("USERS", usersTemplate); + template.replace("SUPPLIERS", suppliersTemplate); + template.replace("RESTOCK_HIDDEN", restockClassTemplate); + template.replace("DATA", table); + template.replace("ACTION", actionTemplate); + msg.set_response("text/html", Soup.MemoryUse.COPY, template.data); + msg.set_status(200); + return; + } catch(TemplateError e) { + stderr.printf(e.message+"\n"); + handler_404(server, msg, path, query, client); + } catch(DatabaseError e) { + handler_400(server, msg, path, query, client, e.message); + } catch(IOError e) { + handler_400(server, msg, path, query, client, e.message); + } + } + void handler_products_new(Soup.Server server, Soup.Message msg, string path, GLib.HashTable<string,string>? query, Soup.ClientContext client) { try { var session = new WebSession(server, msg, path, query, client); @@ -1417,6 +1560,8 @@ public class WebServer { srv.add_handler("/products", handler_products); srv.add_handler("/products/new", handler_products_new); srv.add_handler("/products/bestbefore", handler_product_bestbefore); + srv.add_handler("/products/inventory", handler_products_inventory); + srv.add_handler("/products/inventory-pdf", handler_stock_as_pdf); srv.add_handler("/aliases", handler_alias_list); srv.add_handler("/aliases/new", handler_alias_new); |