Основа для оформления накладных

This commit is contained in:
2024-12-01 17:57:16 +03:00
parent d708baf2dd
commit 988d8fe006
10 changed files with 128 additions and 80 deletions

View File

@@ -1,11 +1,16 @@
from flask import request, Blueprint, render_template, session, current_app, redirect, url_for
from checker import check_auth
from .model import waybill_model
waybill_bp = Blueprint('waybill_bp', __name__, template_folder='templates')
@waybill_bp.route('/', methods=['GET', 'POST'])
@check_auth
def waybill():
if request.method == 'GET':
return render_template('waybill.html')
pack = waybill_model()
if pack.status:
return render_template('waybill.html', items=pack.result)
else:
return render_template('error.html', error_message=pack.error_message)
print(request.form)
return 'OK'

View File

@@ -0,0 +1,34 @@
import pymysql
from pymysql.err import *
class DBContextManager:
def __init__(self, db_config : dict):
self.db_config = db_config
self.connection = None
self.cursor = None
def __enter__(self):
try:
self.connection = pymysql.connect(
host=self.db_config['host'],
port=self.db_config['port'],
user=self.db_config['user'],
password=self.db_config['password'],
db=self.db_config['db']
)
self.cursor = self.connection.cursor()
return self.cursor
except (OperationalError, KeyError) as err:
print(err.args)
return None
def __exit__(self, exc_type, exc_val, exc_tb):
if self.connection and self.cursor:
if exc_type:
print(exc_type, '\n', exc_val)
self.connection.rollback()
else:
self.connection.commit()
self.cursor.close()
self.connection.close()
return True

View File

View File

@@ -0,0 +1,14 @@
import os
from string import Template
class SQLProvider:
def __init__(self, file_path):
self.scripts = {}
for file in os.listdir(file_path):
_sql = open(f'{file_path}/{file}').read()
self.scripts[file] = Template(_sql)
def get(self, name, params) -> dict:
if name not in self.scripts:
raise ValueError(f'SQL template {name} not found')
return self.scripts[name].substitute(**params)

12
App/Waybill/db/work.py Normal file
View File

@@ -0,0 +1,12 @@
from .DBconnect import DBContextManager
def select_list(db_config, sql) -> list:
with DBContextManager(db_config) as cursor:
if cursor is None:
raise ValueError("Cursor not created")
else:
cursor.execute(sql)
result = cursor.fetchall()
schema = [item[0] for item in cursor.description]
lst = [dict(zip(schema, row)) for row in result]
return lst

View File

@@ -1,5 +1,5 @@
from Database.sql_provider import SQLProvider
from Database.select import select_list
from .db.sql_provider import SQLProvider
from .db.work import select_list
from flask import current_app
from dataclasses import dataclass
import os
@@ -13,8 +13,8 @@ class InfoRespronse:
sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql'))
def waybill_model(input_data) -> InfoRespronse:
_sql = sql_provider.get('waybill.sql', input_data)
def waybill_model() -> InfoRespronse:
_sql = sql_provider.get('goods.sql', {})
result = select_list(current_app.config['db_config'], _sql)
if result is None:
return InfoRespronse((),

View File

@@ -0,0 +1 @@
SELECT work_id, name, price, material, count FROM workpiece

View File

@@ -0,0 +1,27 @@
{% macro render_item(item, show_amount = False, show_form = False) %}
<div class="card">
<div class="card-body">
<h6 class="card-title">{{ item['name'] }}</h6>
<p class="card-text">Цена: {{ item['price'] }} ₽</p>
{% if show_amount %}
<span>Количество: {{item['count']}}</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>
</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" />
</form>
{% endif %}
</div>
</div>
{% endmacro %}

View File

@@ -1,48 +1,41 @@
{% import 'card.html' as card %}
<!DOCTYPE html>
<html lang="en">
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<title>Авторизация</title>
<title>Оформление накладной</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/waybill.css" type="text/css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="/static/css/main.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>Заглушка для примера составления накладной</h1>
<form method="POST">
<div class="container">
<!-- Пример карточки -->
<div class="card">
<img src="https://via.placeholder.com/250x150" alt="Фото товара">
<h3>Название товара 1</h3>
<p class="price">Цена: 500 руб.</p>
<p class="quantity">Количество: 10 шт.</p>
<div class="input-container">
<input type="number" name="1" placeholder="Введите количество" min="1" max="1000" value="0">
<div class="container mt-4">
<div class="row">
<!-- Секция товаров -->
<div class="col-md-8">
<h4>Список товаров</h4>
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
{% for item in items %}
<div class="col">
{{ card.render_item(item, show_form = True, show_amount = False) }}
</div>
{% endfor %}
</div>
</div>
<div class="card">
<img src="https://via.placeholder.com/250x150" alt="Фото товара">
<h3>Название товара 2</h3>
<p class="price">Цена: 1200 руб.</p>
<p class="quantity">Количество: 5 шт.</p>
<div class="input-container">
<input type="number" name="2" placeholder="Введите количество" min="0" max="1000" value="0">
</div>
</div>
<div class="card">
<img src="https://via.placeholder.com/250x150" alt="Фото товара">
<h3>Название товара 3</h3>
<p class="price">Цена: 800 руб.</p>
<p class="quantity">Количество: 7 шт.</p>
<div class="input-container">
<input type="number" name="3" placeholder="Введите количество" min="1" max="1000" value="0">
<!-- Секция корзины -->
<div class="col-md-4">
<h4>Корзина</h4>
<ul class="list-group" id="cart-items">
<li class="list-group-item">Корзина пуста</li>
</ul>
<div class="mt-3">
<h5>Итог: <span id="total-price">0</span></h5>
</div>
</div>
</div>
<div class="button-container">
<button type="submit">Создать накладную</button>
<a href="{{ url_for('index') }}"><button>На главную страницу</button></a>
</div>
</form>
</div>
</body>
</html>
</html>

View File

@@ -1,48 +1,10 @@
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
padding: 20px;
justify-content: center;
}
.card {
background: #fff;
border: 1px solid #ddd;
color: black;
border-radius: 10px;
width: 250px;
padding: 15px;
margin-bottom: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.card img {
width: 100%;
height: 150px;
object-fit: cover;
border-radius: 5px;
}
.card h3 {
margin: 10px 0;
font-size: 18px;
}
.price {
font-weight: bold;
color: #28a745;
}
.quantity {
margin: 10px 0;
font-weight: bold;
color: #007bff;
}
.input-container {
margin: 10px 0;
}
.input-container input {
width: 80%;
padding: 8px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 5px;
}
.button-container {
text-align: center;
margin: 20px 0;
}