from flask import Blueprint, redirect, render_template, request, session from . import db bp = Blueprint("inventory", __name__, url_prefix="/inventory") @bp.get("/") def index(): location = session.get("location", None) items = db.run_query("get_inventory_overview.sql", { "location_id": None if location is None else location["location_id"] }).fetchall() return render_template("inventory/index.html", **{ "items": items }) @bp.get("/report") def read_report(): location = session.get("location", None) items = db.run_query("get_inventory_report.sql", { "location_id": None if location is None else location["location_id"] }).fetchall() return render_template("inventory/read_report.html", **{ "items": items }) @bp.get("/item/") def read_item(item_id: int): item = db.run_query("get_item_by_id.sql", { "item_id": item_id }).fetchone() snacks = db.run_query("get_snacks_by_item_id.sql", { "item_id": item_id }).fetchall() same_barcode_items = db.run_query("get_items_by_barcode.sql", { "item_barcode": item["item_barcode"], "location_id": item["location"] }).fetchall() return render_template("inventory/read_item.html", **{ "item": item, "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) @bp.post("/transfer") def transfer_items(): try: from_item_id = int(request.form.get("from_item_id")) to_item_id = int(request.form.get("to_item_id")) amount = int(request.form.get("amount")) except: return "Incomplete or mistyped form", 400 if amount == 0: return "Amount may not be 0", 400 if from_item_id == to_item_id: return "Transfers must be between different items", 400 db.run_query("transfer_items.sql", { "from_item_id": from_item_id, "to_item_id": to_item_id, "amount": amount }) db.get_db().commit() return redirect(request.referrer)