Основной бизнес-процесс
Оформление накладных
This commit is contained in:
@@ -1,16 +1,28 @@
|
||||
from flask import request, Blueprint, render_template, session, current_app, redirect, url_for
|
||||
from flask import request, Blueprint, render_template, session, redirect, url_for
|
||||
from checker import check_auth
|
||||
from .model import workpiece_list, clear
|
||||
from .model import index_waybill, form_waybill, clear, button_click, transaction_order_model
|
||||
from datetime import date
|
||||
|
||||
waybill_bp = Blueprint('waybill_bp', __name__, template_folder='templates', static_folder='static')
|
||||
|
||||
@waybill_bp.route('/', methods=['GET'])
|
||||
@check_auth
|
||||
def index():
|
||||
lst = workpiece_list()
|
||||
if lst.status:
|
||||
return render_template('waybill.html', items=lst.result)
|
||||
lst = index_waybill()
|
||||
if lst is not None:
|
||||
waybill = form_waybill()
|
||||
return render_template('waybill.html', items=lst, waybill=waybill)
|
||||
else:
|
||||
return render_template('error.html', error_message=lst.error_message)
|
||||
return render_template('error.html', error_message="Ошибка в подключении к СУБД")
|
||||
|
||||
@waybill_bp.route('/', methods=['POST'])
|
||||
@check_auth
|
||||
def waybill_main():
|
||||
status = button_click(request)
|
||||
if status:
|
||||
return redirect(url_for('waybill_bp.index'))
|
||||
else:
|
||||
return render_template("error.html", error_message="Товар не был добавлен в корзину")
|
||||
|
||||
@waybill_bp.route('/clear', methods=['GET'])
|
||||
@check_auth
|
||||
@@ -18,7 +30,19 @@ def clear_waybill():
|
||||
clear()
|
||||
return redirect(url_for('waybill_bp.index'))
|
||||
|
||||
@waybill_bp.route('/', methods=['POST'])
|
||||
def waybill():
|
||||
print(request.form)
|
||||
return 'OK'
|
||||
@waybill_bp.route('/save_order')
|
||||
@check_auth
|
||||
def save_order():
|
||||
if not session.get('waybill',{}):
|
||||
return redirect(url_for('waybill_bp.index'))
|
||||
|
||||
user_id = session.get('user_id',"")
|
||||
current_date = date.today().strftime("%Y-%m-%d") + ' ' + date.today().strftime("%H:%M:%S")
|
||||
|
||||
result = transaction_order_model(user_id, current_date)
|
||||
if result.status:
|
||||
print("Order success")
|
||||
return "OK"
|
||||
# return render_template("order_finish.html", order_id = result.result[0])
|
||||
else:
|
||||
return render_template("error.html", error_message=result.error_message)
|
||||
@@ -9,4 +9,8 @@ def select_list(db_config, sql) -> list:
|
||||
result = cursor.fetchall()
|
||||
schema = [item[0] for item in cursor.description]
|
||||
lst = [dict(zip(schema, row)) for row in result]
|
||||
return lst
|
||||
return lst
|
||||
|
||||
def transaction(cursor, sql):
|
||||
cursor.execute(sql)
|
||||
return True
|
||||
@@ -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()
|
||||
|
||||
1
App/Waybill/sql/create_order.sql
Normal file
1
App/Waybill/sql/create_order.sql
Normal file
@@ -0,0 +1 @@
|
||||
INSERT INTO `waybill` (`user_id`,`waybill_date`, `total`) VALUES ($e_user_id, '$e_order_date', $e_total);
|
||||
@@ -1 +1 @@
|
||||
SELECT work_id, name, price, material, count FROM workpiece
|
||||
SELECT work_id, name, price, material, count FROM workpiece
|
||||
2
App/Waybill/sql/insert_order_line.sql
Normal file
2
App/Waybill/sql/insert_order_line.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
INSERT INTO `waybill_lines` VALUES ($e_order_id, $e_prod_id,
|
||||
(SELECT price FROM workpiece WHERE work_id = $e_prod_id), $e_amount);
|
||||
3
App/Waybill/sql/one_good.sql
Normal file
3
App/Waybill/sql/one_good.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
SELECT `work_id`, `name`, `price`, `weight`
|
||||
FROM `workpiece`
|
||||
WHERE `work_id` = $work_id;
|
||||
@@ -5,20 +5,17 @@
|
||||
<h6 class="card-title">{{ item['name'] }}</h6>
|
||||
<p class="card-text">Цена: {{ item['price'] }} ₽</p>
|
||||
{% if show_amount %}
|
||||
<span>Количество: {{item['count']}}</span><br>
|
||||
<span>Количество: {{item['amount']}}</span><br>
|
||||
<form method="POST" action="">
|
||||
<input type="hidden" name="product_display" value="{{item['work_id']}}" />
|
||||
<button class="btn btn-primary">
|
||||
Добавить в накладную
|
||||
</button>
|
||||
<button type="submit" name="product_display_minus" value="minus" class="btn btn-danger">-</button>
|
||||
<button type="submit" name="product_display_plus" value="plus" class="btn btn-success">+</button>
|
||||
<!-- <button type="submit" name="product_display_plus" value="plus" class="btn btn-success">+</button> -->
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if show_form %}
|
||||
<form method="POST" action="">
|
||||
<input type="hidden" name="product_display" value="{{item['work_id']}}" />
|
||||
<input type="submit" class="btn btn-primary" value="Купить" name="buy" />
|
||||
<input type="submit" class="btn btn-primary" value="Добавить" name="add" />
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<h4>Накладная</h4>
|
||||
{% if waybill %}
|
||||
{% for item in waybill %}
|
||||
{{ components.render_item(item, show_form = False, show_amount = True) }}
|
||||
{{ card.render_item(item, show_form = False, show_amount = True) }}
|
||||
{% endfor %}
|
||||
<a href="{{url_for('waybill_bp.save_order')}}"><button class="btn btn-primary">Оформить накладную</button></a>
|
||||
{% else %}
|
||||
|
||||
Reference in New Issue
Block a user