This repository has been archived on 2025-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
RIS/App/Waybill/model.py

129 lines
4.1 KiB
Python

import os
from dataclasses import dataclass
from datetime import date
from cache.wrapper import fetch_from_cache
from Database.DBconnect import DBContextManager
from Database.sql_provider import SQLProvider
from Database.work import select_list
from flask import current_app, session
@dataclass
class InfoRespronse:
result: tuple
error_message: str
status: bool
sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), "sql"))
def clear():
if session.get("waybill", {}):
session.pop("waybill")
def form_waybill() -> list:
db_config = current_app.config["db_config"]
cache_config = current_app.config["cache_config"]
current_waybill = session.get("waybill", {})
waybill = []
for k, v in current_waybill.items():
_sql = sql_provider.get("one_good.sql", dict(work_id=k))
cache_select = fetch_from_cache(f"product_{k}", cache_config)(select_list)
product = cache_select(db_config, _sql)[0]
product["amount"] = v
waybill.append(product)
return waybill
def index_waybill() -> list:
db_config = current_app.config["db_config"]
cache_config = current_app.config["cache_config"]
cache_select = fetch_from_cache("items_cached", cache_config)(select_list)
_sql = sql_provider.get("goods.sql", {})
products = cache_select(db_config, _sql)
return products
def button_click(request):
db_config = current_app.config["db_config"]
data = dict(work_id=int(request.form["product_display"]))
_sql = sql_provider.get("one_good.sql", data)
result = select_list(db_config, _sql)
if result is None:
return False
product = result[0]
if request.form.get("add"):
if "waybill" not in session:
session["waybill"] = dict()
session["total"] = "0"
if str(product["work_id"]) in session["waybill"]:
pr_id = product["work_id"]
price = product["price"]
amount = int(session["waybill"][str(pr_id)])
session["waybill"][str(pr_id)] = str(amount + 1)
session["total"] = str(int(session["total"]) + price)
session.modified = True
else:
# print("NEW WORKPIECE")
pr_id = product["work_id"]
price = product["price"]
session["waybill"][str(pr_id)] = "1"
session["total"] = str(int(session["total"]) + price)
# print(session["waybill"])
session.modified = True
elif request.form.get("product_display_minus"):
# decreasing count in waybill
amount = int(session["waybill"][str(product["work_id"])])
if amount == 1:
session["waybill"].pop(str(product["work_id"]))
else:
session["waybill"][str(product["work_id"])] = str(amount - 1)
session["total"] = str(int(session["total"]) - product["price"])
session.modified = True
return True
def transaction_order_model(user_id: int, current_date: date) -> InfoRespronse:
db_config = current_app.config["db_config"]
waybill = session.get("waybill", {})
total = session.get("total", 0)
result = None
# Чтобы всё это шло как одна транзакция
with DBContextManager(db_config) as cursor:
if cursor is None:
raise ValueError("Cursor not created")
data = dict(e_user_id=user_id, e_order_date=current_date, e_total=total)
_sql = sql_provider.get("create_order.sql", data)
cursor.execute(_sql)
order_id = cursor.lastrowid
for key, value in waybill.items():
_sql = sql_provider.get(
"insert_order_line.sql",
dict(
e_order_id=order_id,
e_price=0,
e_prod_id=int(key),
e_amount=int(value),
),
)
cursor.execute(_sql)
result = tuple([order_id])
if result is None:
return InfoRespronse((), error_message="Заказ не был создан", status=False)
clear()
return InfoRespronse(result, error_message="", status=True)