-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
343 lines (285 loc) · 15.8 KB
/
Copy pathmain.py
File metadata and controls
343 lines (285 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import os
from dotenv import load_dotenv
import telebot
from telebot import types
import mysql.connector
from mysql.connector import Error
from tabulate import tabulate
import db_utils
import ui
load_dotenv()
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
bot = telebot.TeleBot(TOKEN)
try:
connection = mysql.connector.connect(
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
database=os.getenv('DB_NAME')
)
if connection.is_connected():
print("Подключение к базе данных успешно установлено")
except Error as e:
print(f"Ошибка подключения: {e}")
@bot.message_handler(commands=['start'])
def start(message):
chat_id = message.chat.id
if connection:
cursor = connection.cursor()
cursor.execute("SELECT 'Подключение к базе данных успешно' AS status;")
result = cursor.fetchone()
bot.send_message(chat_id, f"Добро пожаловать! {result[0]}")
else:
bot.send_message(chat_id, "Не удалось подключиться к базе данных")
bot.send_message(chat_id, "Выберите действие:", reply_markup=ui.main_menu())
@bot.message_handler(commands=['reset'])
def reset_database(message):
cursor = connection.cursor()
cursor.execute("SET FOREIGN_KEY_CHECKS = 0;")
tables = ["SaleDetails", "Sales"]
for table in tables:
cursor.execute(f"DELETE FROM {table};")
cursor.execute("SET FOREIGN_KEY_CHECKS = 1;")
cursor.execute("UPDATE UserInfo SET balance = 30000")
connection.commit()
cursor.close()
def purchase_product(message, order_id = db_utils.get_id_of_last_order(connection) + 1):
chat_id = message.chat.id
bot.send_message(chat_id, db_utils.get_products_from_warehouse(connection), parse_mode='Markdown')
bot.send_message(chat_id, "Введите id товара который вы хотите купить", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, process_product_choice, order_id)
def process_product_choice(message, order_id):
chat_id = message.chat.id
if message.text.strip().lower() == 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
return
product_id = message.text.strip().lower()
offers = db_utils.get_supplier_offers(connection, product_id)
headers = ['id', 'Название', 'Цена', 'Скидка за опт', 'Кол-во для опта']
table = tabulate(offers, headers, tablefmt='fancy_grid', floatfmt=".2f")
if not offers:
bot.send_message(chat_id, "Такого товара нет")
purchase_product(message)
bot.send_message(chat_id, f"```\n{table}\n```", parse_mode='Markdown')
bot.send_message(chat_id, "Введите id продавца и количество товара через пробел", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, init_purchase, product_id, order_id)
def init_purchase(message, product_id, order_id):
chat_id = message.chat.id
if message.text.strip().lower() == 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
return
try:
parts = message.text.strip().split()
supplier_choice = int(parts[0])
qty = int(parts[1])
except (IndexError, ValueError):
bot.send_message(chat_id, "Неверный формат. Пожалуйста, введите данные в формате: номер поставщика и количество (например, `1 20`).")
message.text = str(product_id)
process_product_choice(message)
return
if supplier_choice <= 0 or supplier_choice > db_utils.get_suppliers_num(connection):
bot.send_message(chat_id, "Выбран неверный номер поставщика.")
purchase_product(message)
return
print(db_utils.get_offer(connection, supplier_choice, product_id))
base_price, discount, bulk_min_quantity = db_utils.get_offer(connection, supplier_choice, product_id)
if qty >= bulk_min_quantity:
final_unit_price = base_price * (1 - discount)
else:
discount = 0
final_unit_price = base_price
total_price = final_unit_price * qty
if total_price > db_utils.check_balance(connection):
bot.send_message(chat_id, "Недостаточно средств!")
purchase_product(message)
return
db_utils.change_balance(connection, -total_price)
db_utils.add_product_to_warehouse(connection, product_id, qty)
try:
db_utils.add_new_order_detail(connection, order_id, product_id, base_price, qty, discount, supplier_choice)
except Error:
db_utils.add_new_order(connection, order_id)
db_utils.add_new_order_detail(connection, order_id, product_id, base_price, qty, discount, supplier_choice)
bot.send_message(chat_id, "Товары успешно заказаны! Продолжить заказ?", reply_markup=ui.choice_menu())
bot.register_next_step_handler(message, process_next_step_for_ordering, order_id)
def process_next_step_for_ordering(message, order_id):
chat_id = message.chat.id
answer = message.text.strip().lower()
if answer == 'да':
purchase_product(message, order_id)
else:
bot.send_message(chat_id, "Спасибо за покупку, хорошего дня!", reply_markup=ui.main_menu())
def sell_product(message, sale_id = db_utils.get_id_of_last_sale(connection) + 1):
chat_id = message.chat.id
bot.send_message(chat_id, db_utils.get_products_from_warehouse(connection), parse_mode='Markdown')
bot.send_message(chat_id, "Введите id товара который хотят купить и количество через пробел (пример: 1 2)", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, process_product_choice_for_sale, sale_id)
def order_missing_products(product_id, quantity):
offers = db_utils.get_supplier_offers(connection, product_id)
base_price = db_utils.get_price_of_product(connection, product_id)
min_cost = base_price * quantity
seller_id = offers[0][0]
for row in offers:
id, name, price, discount, bulk_min_quantity = row
if quantity >= bulk_min_quantity:
price *= (1 - discount)
if quantity * price < min_cost:
min_cost = quantity * price
seller_id = id
order_id = db_utils.get_id_of_last_order(connection) + 1
db_utils.add_new_order(connection, order_id)
db_utils.add_new_order_detail(connection, order_id, product_id, base_price, quantity, discount, seller_id)
db_utils.change_balance(connection, -min_cost)
db_utils.add_product_to_warehouse(connection, product_id, quantity)
return True
def process_product_choice_for_sale(message, sale_id):
chat_id = message.chat.id
if message.text.strip().lower() == 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
return
try:
parts = message.text.strip().split()
buyer_choice = int(parts[0])
qty = int(parts[1])
except (IndexError, ValueError):
bot.send_message(chat_id, "Неверный формат. Пожалуйста, введите данные в формате: номер поставщика и количество (например, `1 20`).")
sell_product(message)
return
quantity_in_warehouse = db_utils.check_products(connection, buyer_choice)
if quantity_in_warehouse < qty:
bot.send_message(chat_id, f"Не хватает {qty - quantity_in_warehouse} единиц товара, инициализируем покупку товара...")
success = order_missing_products(buyer_choice, qty - quantity_in_warehouse)
if success:
bot.send_message(chat_id, f"Товар успешно докуплен, инициализируем продажу...")
price = db_utils.get_price_of_product(connection, buyer_choice)
db_utils.change_balance(connection, price * qty)
db_utils.add_product_to_warehouse(connection, buyer_choice, -qty)
try:
db_utils.add_new_sale_detail(connection, sale_id, buyer_choice, price, qty)
except Error:
db_utils.add_new_sale(connection, sale_id)
db_utils.add_new_sale_detail(connection, sale_id, buyer_choice, price, qty)
bot.send_message(chat_id, "Товар успешно продан, продолжить покупку?", reply_markup=ui.choice_menu())
bot.register_next_step_handler(message, process_next_step_for_buying, sale_id)
def process_next_step_for_buying(message, sale_id):
chat_id = message.chat.id
answer = message.text.strip().lower()
if answer == 'да':
sell_product(message, sale_id)
else:
bot.send_message(chat_id, "Спасибо за покупку, хорошего дня!", reply_markup=ui.main_menu())
def return_product(message):
chat_id = message.chat.id
bot.send_message(chat_id, db_utils.get_sale_history(connection), parse_mode='Markdown')
bot.send_message(chat_id, "Введите id чека", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, process_check_to_return)
def process_check_to_return(message):
chat_id = message.chat.id
if message.text.strip().lower() == 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
return
try:
sale_id = int(message.text.strip())
rows = db_utils.get_sale_details(connection, sale_id)
if not rows:
bot.send_message(chat_id, "Такого чека нет")
return_product(message)
headers = ['id', 'Наименование товара', 'Проданное количество', 'Можно ли вернуть']
table = tabulate(rows, headers, tablefmt='fancy_grid', floatfmt='.2f')
bot.send_message(chat_id, f"```\n{table}\n```", parse_mode='markdown')
bot.send_message(chat_id, "Введите id товара и возвращаемое количество через пробел (пример 1 5)", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, init_product_return, sale_id)
except ValueError:
bot.send_message(chat_id, "Неккоректное значение id")
return_product(message)
return
def init_product_return(message, sale_id):
chat_id = message.chat.id
if message.text.strip().lower() == 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
return
try:
parts = message.text.strip().split()
product_id = int(parts[0])
qty = int(parts[1])
except (IndexError, ValueError):
bot.send_message(chat_id, "Неверный формат. Пожалуйста, введите данные в формате: id продукта количество (например, `1 20`).")
message.text = str(sale_id)
process_check_to_return(message)
price = db_utils.get_price_of_product(connection, product_id)
if db_utils.reduce_products_from_returns(connection, sale_id, product_id, qty):
db_utils.change_balance(connection, -price * qty)
db_utils.add_product_to_warehouse(connection, product_id, qty)
bot.send_message(chat_id, "Товар успешно возвращен", reply_markup=ui.main_menu())
db_utils.add_new_return(connection, sale_id, product_id, qty)
else:
bot.send_message(chat_id, "Недостаточное количества товара в чеке или такого товара нет в чеке")
message.text = str(sale_id)
process_check_to_return(message)
def get_financial_report(message):
chat_id = message.chat.id
bot.send_message(chat_id, "Введите год за который Вы хотите получить финансовый отчет (все 4 цифры)", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, print_report)
def print_report(message):
chat_id = message.chat.id
year = message.text.lower()
if year == 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
return
if not(year and year.isdigit()):
bot.send_message(chat_id, "Некорректный год")
get_financial_report(message)
return
for quartal in range(1, 5):
bot.send_message(chat_id, f"{quartal} квартал\n" + db_utils.get_financial_report_by_quartal(connection, year, quartal), parse_mode='Markdown')
bot.send_message(chat_id, "Итог по году\n" + db_utils.get_financial_report_by_year(connection, year), parse_mode='Markdown', reply_markup=ui.main_menu())
def sale_history(message):
chat_id = message.chat.id
bot.send_message(chat_id, db_utils.get_sale_history(connection), parse_mode='Markdown')
bot.send_message(chat_id, "Введите id чека", reply_markup=ui.back_menu())
bot.register_next_step_handler(message, check_details)
def check_details(message):
chat_id = message.chat.id
id = message.text.strip()
if not id.isdigit():
bot.send_message(chat_id, "Некорректный id чека")
sale_history(message)
return
id = int(id)
details = db_utils.get_sale_details(connection, id)
headers = ['Артикул', 'Название', 'Кол-во', 'Возвратный']
table = tabulate(details, headers, tablefmt='fancy_grid', floatfmt='.2f')
bot.send_message(chat_id, f"```\n{table}\n```", parse_mode='Markdown', reply_markup=ui.main_menu())
# Основной обработчик текстовых сообщений
@bot.message_handler(func=lambda message: True)
def menu_handler(message):
text = message.text.strip().lower()
chat_id = message.chat.id
match text:
case 'оформить заказ':
bot.send_message(chat_id, "Выберите тип заказа:", reply_markup=ui.order_menu())
case 'исполнить заказ для потребителя':
sell_product(message)
case 'заказать товар у поставщика':
purchase_product(message)
case 'возврат товара':
return_product(message)
case 'отчёты':
bot.send_message(chat_id, "Выберите тип отчёта:", reply_markup=ui.reports_menu())
case 'состояние склада':
bot.send_message(chat_id, db_utils.get_products_from_warehouse(connection), parse_mode='Markdown', reply_markup=ui.reports_menu())
case 'удовлетворённые заказы':
sale_history(message)
case 'финансовая картина':
bot.send_message(chat_id, db_utils.get_financial_situation(connection), parse_mode='Markdown', reply_markup=ui.reports_menu())
case 'фин. отчёт (квартал/год)':
get_financial_report(message)
case 'поставщики':
bot.send_message(chat_id, db_utils.get_sellers(connection), parse_mode='Markdown', reply_markup=ui.main_menu())
case 'назад':
bot.send_message(chat_id, "Возврат в главное меню", reply_markup=ui.main_menu())
case _:
bot.send_message(chat_id, "Неизвестная команда. Пожалуйста, выберите действие из меню.", reply_markup=ui.main_menu())
if __name__ == '__main__':
print("Бот запущен...")
bot.polling(none_stop=True)