ShopBot/handlers/user/checkout.py

96 lines
3.0 KiB
Python
Raw Normal View History

2022-06-04 22:02:51 +03:00
from aiogram import types
2022-06-16 00:32:17 +03:00
from aiogram.dispatcher import FSMContext
2022-06-04 22:02:51 +03:00
2022-06-16 00:32:17 +03:00
import config
2022-06-04 22:02:51 +03:00
from load import dp, bot, messages
from utils.database.cart import get_user_cart
from utils.database.market import Catalog
from utils.database.ordering import save_info
2022-06-16 00:32:17 +03:00
from keyboard.default.main_menu import back_to_main_menu
2022-06-04 22:02:51 +03:00
from keyboard.default.checkout import checkout_btn
@dp.message_handler(lambda x: x.text == messages.checkout)
2022-06-16 00:32:17 +03:00
async def checkout(message: types.Message, state: FSMContext):
2022-06-04 22:02:51 +03:00
items = get_user_cart(message.from_user.id)
2022-06-16 00:32:17 +03:00
output: list[str] = [""]
2022-06-04 22:02:51 +03:00
cost = 0
2022-06-16 00:32:17 +03:00
2022-06-04 22:02:51 +03:00
for index, count in items:
i = Catalog.get_catalog(index)
2022-06-16 00:32:17 +03:00
product_info = (
f'Имя: {i["name"]} '
f'цена: {i["price"]} - {count}шт.\n'
)
if (len(output[-1]) + len(product_info)) >= 4095:
output.append("")
output[-1] += product_info
2022-06-04 22:02:51 +03:00
cost += i["price"] * count
2022-06-16 00:32:17 +03:00
total_amount = (
f"Общая сумма: {cost}\n"
"Всё верно? Следующий этап: заполнение заявки"
)
if (len(output[-1]) + len(total_amount)) >= 4095:
output.append("")
output[-1] += total_amount
for msg in output:
await bot.send_message(message.chat.id, msg, reply_markup=checkout_btn())
await state.set_state("load_or_create_profile")
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
@dp.message_handler(lambda x: x.text == messages.all_right_message, state="save_or_continue")
2022-06-16 00:32:17 +03:00
async def continue_user_form(message: types.Message, state: FSMContext):
chat_id = config.service_chat
items = get_user_cart(message.from_user.id)
data = await state.get_data()
output: list[str] = [
(f"Имя: {data['first_name']}\n"
f"Фамилия: {data['last_name']}\n"
f"Номер телефона: {data['phone_number']}\n"
f"Адрес: {data['address']}\n\n"
)
]
cost = 0
for index, count in items:
i = Catalog.get_catalog(index)
product_info = (
f'ID: {i["id"]} '
f'Имя: {i["name"]} '
f'цена: {i["price"]} - {count}шт.\n'
)
if (len(output[-1]) + len(product_info)) >= 4095:
output.append("")
output[-1] += product_info
cost += i["price"] * count
total_amount = (
f"\nИтоговая сумма: {cost}"
)
if (len(output[-1]) + len(total_amount)) >= 4095:
output.append("")
output[-1] += total_amount
await bot.send_message(message.chat.id, messages.order, reply_markup=back_to_main_menu)
for msg in output:
await bot.send_message(
chat_id,
msg,
parse_mode='Markdown'
)
await state.finish()
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
@dp.message_handler(lambda x: x.text == messages.save_and_continue, state="save_or_continue")
2022-06-16 00:32:17 +03:00
async def save_user_info(message: types.Message, state: FSMContext):
2022-06-04 22:02:51 +03:00
info = await state.get_data()
save_info(user_id=message.from_user.id, **info)
2022-06-16 00:32:17 +03:00
await continue_user_form(message, state)