From 58ed948975fdbe561b04d937be3e20d7abd902f2 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Fri, 30 Jun 2023 09:52:53 +0200 Subject: [PATCH] Add inventory correction stuff --- py/jon/db/create_correction.sql | 3 ++ py/jon/db/deactivate_item.sql | 9 +++++ py/jon/inventory.py | 46 ++++++++++++++++++++++- py/jon/templates/inventory/index.html | 2 +- py/jon/templates/inventory/read_item.html | 24 +++++++++++- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 py/jon/db/create_correction.sql create mode 100644 py/jon/db/deactivate_item.sql diff --git a/py/jon/db/create_correction.sql b/py/jon/db/create_correction.sql new file mode 100644 index 0000000..161a0e4 --- /dev/null +++ b/py/jon/db/create_correction.sql @@ -0,0 +1,3 @@ +INSERT INTO garfield.inventory_correction (item_id, delta, correction_comment) +VALUES (%(item_id)s, %(correction_delta)s, %(correction_comment)s) + diff --git a/py/jon/db/deactivate_item.sql b/py/jon/db/deactivate_item.sql new file mode 100644 index 0000000..25d6d32 --- /dev/null +++ b/py/jon/db/deactivate_item.sql @@ -0,0 +1,9 @@ +UPDATE garfield.inventory_items +SET available = FALSE +WHERE item_id = %(item_id)s; + +-- Call garfield.snack_delete for every snack entry associated with this item. +SELECT snack_id +FROM garfield.inventory_map, + LATERAL garfield.snack_delete(snack_id::integer) +WHERE inventory_id = %(item_id)s diff --git a/py/jon/inventory.py b/py/jon/inventory.py index 42ad120..de47720 100644 --- a/py/jon/inventory.py +++ b/py/jon/inventory.py @@ -1,4 +1,4 @@ -from flask import Blueprint, render_template, session +from flask import Blueprint, redirect, render_template, request, session from . import db @@ -18,7 +18,7 @@ def index(): }) -@bp.get("/") +@bp.get("/item/") def read_item(item_id: int): item = db.run_query("get_item_by_id.sql", { "item_id": item_id @@ -38,3 +38,45 @@ def read_item(item_id: int): "snacks": snacks, "same_barcode_items": same_barcode_items }) + + +@bp.post("/item//deactivate") +def deactivate_item(item_id: int): + item = db.run_query("get_item_by_id.sql", { + "item_id": item_id + }).fetchone() + + if item["units_left"] != 0: + return "Only items without stock can be deactivated", 400 + + db.run_query("deactivate_item.sql", { + "item_id": item_id + }) + db.get_db().commit() + + return redirect(request.referrer) + + +@bp.post("/correction") +def create_correction(): + try: + item_id = int(request.form.get("item_id")) + correction_delta = int(request.form.get("correction_delta")) + correction_comment = request.form.get("correction_comment") + except: + return "Incomplete or mistyped form", 400 + + if correction_delta == 0: + return "Correction delta may not be 0", 400 + + if correction_comment == "": + return "Correction comment may not be empty", 400 + + db.run_query("create_correction.sql", { + "item_id": item_id, + "correction_delta": correction_delta, + "correction_comment": correction_comment + }) + db.get_db().commit() + + return redirect(request.referrer) diff --git a/py/jon/templates/inventory/index.html b/py/jon/templates/inventory/index.html index bbcfd52..f87e979 100644 --- a/py/jon/templates/inventory/index.html +++ b/py/jon/templates/inventory/index.html @@ -17,7 +17,7 @@ {% for item in items %} - {{ item.item_id }} + {{ item.item_id }} {{ item.item_barcode }} {{ item.name }} {{ format_currency(item.unit_price) }} diff --git a/py/jon/templates/inventory/read_item.html b/py/jon/templates/inventory/read_item.html index 8170309..a545199 100644 --- a/py/jon/templates/inventory/read_item.html +++ b/py/jon/templates/inventory/read_item.html @@ -53,6 +53,28 @@ +
+ Aktionen + +
+ + + + Inventar zu 0 korrigieren + + +
+ + + + +
+ +
+ Inventar- und Snackeintrag deaktivieren + +
+
Snackeinträge für {{ item.item_id }} @@ -101,7 +123,7 @@ {% for item in same_barcode_items %} - {{ item.item_id }} + {{ item.item_id }} {{ item.item_barcode }} {{ item.name }} {{ format_currency(item.unit_price) }}