124 lines
4.2 KiB
Python
124 lines
4.2 KiB
Python
from Database.sql_provider import SQLProvider
|
|
from Database.work import select_list, transaction
|
|
from Database.DBconnect import DBContextManager
|
|
|
|
from flask import current_app, session
|
|
from dataclasses import dataclass
|
|
from datetime import date
|
|
from cache.wrapper import fetch_from_cache
|
|
import os
|
|
|
|
@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:
|
|
current_waybill = session.get('waybill',{})
|
|
waybill = []
|
|
for k,v in current_waybill.items():
|
|
_sql = sql_provider.get('one_good.sql', dict(work_id=k))
|
|
product = select_list(current_app.config['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 == 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_plus'):
|
|
# # increasing count in waybill
|
|
|
|
# amount = int(session['waybill'][str(product['work_id'])])
|
|
# session['waybill'][str(product['work_id'])] = str(amount + 1)
|
|
# 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):
|
|
|
|
db_config = current_app.config['db_config']
|
|
waybill = session.get('waybill',{})
|
|
total = session.get('total', 0)
|
|
|
|
# Чтобы всё это шло как одна транзакция
|
|
with DBContextManager(db_config) as cursor:
|
|
|
|
data = dict(e_user_id=user_id, e_order_date=current_date, e_total=total)
|
|
|
|
_sql = sql_provider.get('create_order.sql', data)
|
|
try:
|
|
cursor.execute(_sql)
|
|
except:
|
|
return InfoRespronse((), error_message="Заказ не был создан", status=False)
|
|
|
|
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_prod_id = int(key),
|
|
e_amount = int(value)))
|
|
try:
|
|
cursor.execute(_sql)
|
|
except:
|
|
return InfoRespronse((), error_message="Заказ не был создан", status=False)
|
|
|
|
result = tuple([order_id])
|
|
clear()
|
|
return InfoRespronse(result, error_message="", status=True) |