Основной бизнес-процесс

Оформление накладных
This commit is contained in:
2024-12-03 17:46:41 +03:00
parent 453801f2e9
commit ee6a2f9756
9 changed files with 77 additions and 48 deletions

View File

@@ -1,6 +1,6 @@
from .db.sql_provider import SQLProvider
from .db.work import select_list
from db.DBconnect import DBContextManager
from .db.work import select_list, transaction
from .db.DBconnect import DBContextManager
from flask import current_app, session
from dataclasses import dataclass
@@ -20,20 +20,11 @@ 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))
_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)
@@ -44,14 +35,14 @@ def index_waybill() -> list:
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', {})
_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(prod_id=int(request.form['product_display']))
data = dict(work_id=int(request.form['product_display']))
_sql = sql_provider.get('one_good.sql', data)
result = select_list(db_config, _sql)
@@ -60,37 +51,43 @@ def button_click(request):
product = result[0]
if request.form.get('buy'):
if request.form.get('add'):
if 'waybill' not in session:
session['waybill'] = dict()
session['total'] = '0'
if str(product['prod_id']) in session['waybill']:
pr_id = product['prod_id']
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['prod_id']
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
# 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
# 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['prod_id'])])
amount = int(session['waybill'][str(product['work_id'])])
if amount == 1:
session['waybill'].pop(str(product['prod_id']))
session['waybill'].pop(str(product['work_id']))
else:
session['waybill'][str(product['prod_id'])] = str(amount-1)
session['waybill'][str(product['work_id'])] = str(amount-1)
session['total'] = str(int(session['total']) - product['price'])
session.modified = True
return True
@@ -98,17 +95,18 @@ 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)
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(tuple(), error_message="Заказ не был создан", status=False)
return InfoRespronse((), error_message="Заказ не был создан", status=False)
order_id = cursor.lastrowid
for key, value in waybill.items():
@@ -119,7 +117,7 @@ def transaction_order_model(user_id: int, current_date: date):
try:
cursor.execute(_sql)
except:
return InfoRespronse(tuple(), error_message="Заказ не был создан", status=False)
return InfoRespronse((), error_message="Заказ не был создан", status=False)
result = tuple([order_id])
clear()