diff --git a/py/TODO.md b/py/TODO.md new file mode 100644 index 0000000..67803cd --- /dev/null +++ b/py/TODO.md @@ -0,0 +1 @@ +- [ ] Fix date handling in entry diff --git a/py/jon/__init__.py b/py/jon/__init__.py index b9d8bae..7766f92 100644 --- a/py/jon/__init__.py +++ b/py/jon/__init__.py @@ -3,7 +3,13 @@ import json from flask import Flask, render_template -from . import db, inventory, location, template_utils +from . import ( + db, + entry, + inventory, + location, + template_utils +) def create_app(): @@ -21,6 +27,7 @@ def create_app(): app.register_blueprint(location.bp) app.register_blueprint(inventory.bp) + app.register_blueprint(entry.bp) @app.route("/") def index(): return render_template("index.html") diff --git a/py/jon/db/get_groups.sql b/py/jon/db/get_groups.sql new file mode 100644 index 0000000..33d5233 --- /dev/null +++ b/py/jon/db/get_groups.sql @@ -0,0 +1,6 @@ +SELECT + group_id, + group_name +FROM garfield.inventory_item_groups +ORDER BY + group_name ASC diff --git a/py/jon/db/get_snacks_by_barcode.sql b/py/jon/db/get_snacks_by_barcode.sql new file mode 100644 index 0000000..5f67478 --- /dev/null +++ b/py/jon/db/get_snacks_by_barcode.sql @@ -0,0 +1,11 @@ +SELECT + * +FROM garfield.inventory_map +LEFT JOIN garfield.snacks USING (snack_id) +LEFT JOIN garfield.snacks_available USING (snack_id) +LEFT JOIN garfield.tax_groups USING (tax_group_id) +LEFT JOIN garfield.locations USING (location_id) +WHERE snack_barcode = %(snack_barcode)s +ORDER BY + snack_available DESC, + snack_timestamp DESC diff --git a/py/jon/entry.py b/py/jon/entry.py new file mode 100644 index 0000000..d97c423 --- /dev/null +++ b/py/jon/entry.py @@ -0,0 +1,59 @@ +import datetime +import zoneinfo + + +from flask import Blueprint, redirect, render_template, request, session + +from . import db + + +bp = Blueprint("entry", __name__, url_prefix="/entry") + + +@bp.get("/") +def index(): + return render_template("entry/index.html") + + +@bp.route("/edit-item-data", methods=["GET", "POST"]) +def edit_item_data(): + if "entry" not in session: + session["entry"] = dict() + + if request.method == "POST": + session["entry"] = { + "item_bought": datetime.datetime.strptime(request.form.get("item_bought"), "%Y-%m-%d"), + "item_barcode": request.form.get("item_barcode"), + "item_name": request.form.get("item_name"), + "item_group_id": int(request.form.get("item_group")), + "item_net_unit_price": float(request.form.get("item_net_unit_price")), + "item_tax_group_id": int(request.form.get("item_tax_group")), + "item_amount": int(request.form.get("item_amount")), + "item_location_id": int(request.form.get("item_location")) + } + + return redirect("/entry/select-snack-entry") + + groups = db.run_query("get_groups.sql").fetchall() + locations = db.run_query("get_locations.sql").fetchall() + + return render_template("entry/edit-item-data.html", **{ + "groups": groups, + "locations": locations, + "entry": session["entry"] + }) + + +@bp.route("/select-snack-entry", methods=["GET", "POST"]) +def edit_snack_data(): + if "entry" not in session: + return redirect("/entry/edit-item-data") + + snacks = db.run_query("get_snacks_by_barcode.sql", { + "snack_barcode": session["entry"]["item_barcode"] + }).fetchall() + + return render_template("entry/select-snack-entry.html", **{ + "entry": session["entry"], + "snacks": snacks + }) diff --git a/py/jon/template_utils.py b/py/jon/template_utils.py index 486d49f..b192b04 100644 --- a/py/jon/template_utils.py +++ b/py/jon/template_utils.py @@ -1,3 +1,6 @@ +import datetime + + def format_currency(x): # It would be nicer to format this using the German locale # Too lazy to bother tho. @@ -10,3 +13,18 @@ def format_date(d): def format_bool(x): return "✅" if x else "❌" + + +def now(): + return datetime.datetime.now() + + +def get_garfield_price(net_unit_price, tax_group_id): + if tax_group_id == 1: + tax_factor = 1.19 + elif tax_group_id == 2: + tax_factor = 1.07 + else: + raise Error("Unknown tax group ID") + + return net_unit_price * tax_factor + 0.01 diff --git a/py/jon/templates/base.html b/py/jon/templates/base.html index a603e1e..f71f7e1 100644 --- a/py/jon/templates/base.html +++ b/py/jon/templates/base.html @@ -62,6 +62,13 @@ font-size: 8px; } } + .form-input > label { + font-size: .8em; + } + .form-input > input:not([type=radio]), + .form-input > select { + display: block; + } @@ -71,6 +78,7 @@