126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
from .db.sql_provider import SQLProvider
|
|
from .db.work import select_list
|
|
from db.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 workpiece_list() -> InfoRespronse:
|
|
_sql = sql_provider.get('goods.sql', {})
|
|
result = select_list(current_app.config['db_config'], _sql)
|
|
if result is None:
|
|
return InfoRespronse((),
|
|
error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором',
|
|
status=False)
|
|
return InfoRespronse(result, error_message='', status=True)
|
|
|
|
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(prod_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('all_goods.sql', {})
|
|
products = cache_select(db_config, _sql)
|
|
|
|
return products
|
|
|
|
def button_click(request):
|
|
db_config = current_app.config['db_config']
|
|
data = dict(prod_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('buy'):
|
|
if 'waybill' not in session:
|
|
session['waybill'] = dict()
|
|
|
|
if str(product['prod_id']) in session['waybill']:
|
|
pr_id = product['prod_id']
|
|
amount = int(session['waybill'][str(pr_id)])
|
|
session['waybill'][str(pr_id)] = str(amount+1)
|
|
session.modified = True
|
|
else:
|
|
print("NEW WORKPIECE")
|
|
pr_id = product['prod_id']
|
|
session['waybill'][str(pr_id)] = '1'
|
|
print(session['waybill'])
|
|
session.modified = True
|
|
|
|
elif request.form.get('product_display_plus'):
|
|
# increasing count in waybill
|
|
|
|
amount = int(session['waybill'][str(product['prod_id'])])
|
|
session['waybill'][str(product['prod_id'])] = str(amount + 1)
|
|
session.modified = True
|
|
|
|
elif request.form.get('product_display_minus'):
|
|
# decreasing count in waybill
|
|
|
|
amount = int(session['waybill'][str(product['prod_id'])])
|
|
if amount == 1:
|
|
session['waybill'].pop(str(product['prod_id']))
|
|
else:
|
|
session['waybill'][str(product['prod_id'])] = str(amount-1)
|
|
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',{})
|
|
|
|
# Чтобы всё это шло как одна транзакция
|
|
with DBContextManager(db_config) as cursor:
|
|
|
|
data = dict(e_user_id=user_id, e_order_date=current_date)
|
|
|
|
_sql = sql_provider.get('create_order.sql', data)
|
|
try:
|
|
cursor.execute(_sql)
|
|
except:
|
|
return InfoRespronse(tuple(), 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(tuple(), error_message="Заказ не был создан", status=False)
|
|
|
|
result = tuple([order_id])
|
|
clear()
|
|
return InfoRespronse(result, error_message="", status=True) |