From 4d41098481d2b0f8606e2ca7bafc7ee385f26c7a Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 6 Mar 2020 17:59:05 +0100 Subject: [PATCH] Send and save price of command --- app/models.py | 3 ++- app/sockets.py | 13 ++++++++++++- app/static/js/pc.js | 5 ++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 50e2266..c309202 100644 --- a/app/models.py +++ b/app/models.py @@ -42,6 +42,7 @@ class Command(db.Model): sandwich_id = db.Column(db.Integer, db.ForeignKey("user.id")) client_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) + price = db.Column(db.Float, nullable=False) date = db.Column(db.Date, default=datetime.datetime.now().date) take = db.Column(db.Time, default=datetime.datetime.now().time) done = db.Column(db.Time) @@ -63,7 +64,7 @@ class Plate(db.Model): id = db.Column(db.String, primary_key=True) name = db.Column(db.String, nullable=False) - price = db.Column(db.Integer, default=0) + price = db.Column(db.Float, default=0) avoid_ingredient = db.Column(db.Boolean, default=False) avoid_sauce = db.Column(db.Boolean, default=False) diff --git a/app/sockets.py b/app/sockets.py index f051cfa..35dbe3d 100644 --- a/app/sockets.py +++ b/app/sockets.py @@ -72,7 +72,6 @@ def addcmd(json): Command.number.desc()).first().number + 1 except AttributeError: c.number = 1 - c.pc_id = json["pc"] if all(i in json and json[i] for i in ["firstname", "lastname", "client"]): db.session.add(User(username=json["client"], firstname=json["firstname"], lastname=json["lastname"])) if "client" in json: @@ -80,6 +79,18 @@ def addcmd(json): c.client_id = User.query.filter_by(username=json["client"]).first().id except AttributeError: c.client_id = User.query.filter_by(username="dummy").first().id + if "pc" in json: + try: + c.pc_id = User.query.filter_by(username=json["pc"]).first().id + except AttributeError: + c.pc_id = User.query.filter_by(username="dummy").first().id + if "price" in json: + try: + c.price = json["price"] + except AttributeError: + c.price = -1 + else: + c.price = -1 if "plate" in json: try: c.plate_id = Plate.query.get(json["plate"]).id diff --git a/app/static/js/pc.js b/app/static/js/pc.js index 57f9811..56bbffd 100644 --- a/app/static/js/pc.js +++ b/app/static/js/pc.js @@ -168,13 +168,14 @@ function error(e) { list.appendChild(e); } -function price () { +function price() { let p = 0; for (let i in current["price"]) { p += current["price"][i] } p = p.toFixed(2); document.querySelector("#resume h2").innerHTML = p+"€"; + return p; } socket.on("connect", data => { @@ -296,6 +297,7 @@ document.querySelector(".validation").addEventListener("click", ev => { current["client"] = user.value; current["pc"] = pc_id; + current["price"] = price(); socket.emit("add command", current); current = {"plate": null, "ingredient": [], "sauce": [], "drink": null, "dessert": null, "price": {}}; document.querySelectorAll("input[name=plate],input[name=drink],input[name=dessert]").forEach( e => { @@ -311,6 +313,7 @@ document.querySelector(".validation").addEventListener("click", ev => { user.value = ""; user.style.color = ""; document.getElementById("user_list").innerHTML = ""; + document.querySelector("#resume h2").innerHTML = "0€"; }); document.getElementById("user").addEventListener("keyup", ev => {hinter(ev)});