100 lines
2.6 KiB
Python
100 lines
2.6 KiB
Python
from flask import Blueprint, redirect, render_template, request, session
|
|
|
|
|
|
from . import db
|
|
|
|
|
|
bp = Blueprint("entry", __name__, url_prefix="/entry")
|
|
|
|
|
|
@bp.route("/", methods=["GET", "POST"])
|
|
def index():
|
|
cart = session.get("cart", default=[])
|
|
return render_template(
|
|
"entry/index.html",
|
|
cart=cart
|
|
)
|
|
|
|
|
|
@bp.post("/delete-order")
|
|
def delete_order():
|
|
try:
|
|
order_index = int(request.form["order-index"])
|
|
except:
|
|
return f"Incomplete or mistyped form", 400
|
|
|
|
cart = session.get("cart", default=[])
|
|
del cart[order_index]
|
|
session["cart"] = cart
|
|
|
|
return redirect(request.referrer)
|
|
|
|
|
|
@bp.route("/new-order", methods=["GET", "POST"])
|
|
def new_order():
|
|
if request.method == "POST":
|
|
try:
|
|
barcode = request.form["barcode"]
|
|
name = request.form["name"]
|
|
sales_units = int(request.form["sales-units"])
|
|
group_id = int(request.form["group"])
|
|
location_id = int(request.form["location"])
|
|
tax_group_id = int(request.form["tax-group"])
|
|
net_unit_price = float(request.form["net-unit-price"])
|
|
gross_unit_price = float(request.form["gross-unit-price"])
|
|
except:
|
|
return f"Incomplete or mistyped form", 400
|
|
|
|
cart = session.get("cart", default=[])
|
|
print(cart)
|
|
cart.append({
|
|
"barcode": barcode,
|
|
"name": name,
|
|
"sales_units": sales_units,
|
|
"group_id": group_id,
|
|
"location_id": location_id,
|
|
"tax_group_id": tax_group_id,
|
|
"net_unit_price": net_unit_price,
|
|
"gross_unit_price": gross_unit_price
|
|
})
|
|
session["cart"] = cart
|
|
return redirect("/entry")
|
|
|
|
with db.run_query("entry/get_groups.sql") as cursor:
|
|
groups = cursor.fetchall()
|
|
|
|
with db.run_query("entry/get_locations.sql") as cursor:
|
|
locations = cursor.fetchall()
|
|
|
|
with db.run_query("entry/get_tax_groups.sql") as cursor:
|
|
tax_groups = cursor.fetchall()
|
|
|
|
return render_template(
|
|
"entry/new-order.html",
|
|
groups=groups,
|
|
locations=locations,
|
|
tax_groups=tax_groups
|
|
)
|
|
|
|
|
|
|
|
# API routes for interactive JS stuff
|
|
|
|
|
|
@bp.get("/api/search-items")
|
|
def api_search_items():
|
|
try:
|
|
search_term = request.args["search-term"]
|
|
except:
|
|
return {"error": "Missing query parameter `search-term`"}, 400
|
|
|
|
location = session.get("location", None)
|
|
|
|
with db.run_query("search_items.sql", {
|
|
"location_id": None if location is None else location["location_id"],
|
|
"search_term": search_term
|
|
}) as cursor:
|
|
items = cursor.fetchall()
|
|
|
|
return items
|