from aiogram import types from aiogram.dispatcher import FSMContext import config 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 from keyboard.default.main_menu import back_to_main_menu from keyboard.default.checkout import checkout_btn @dp.message_handler(lambda x: x.text == messages.checkout) async def checkout(message: types.Message, state: FSMContext): items = get_user_cart(message.from_user.id) output: list[str] = [""] cost = 0 for index, count in items: i = Catalog.get_catalog(index) product_info = ( 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"Общая сумма: {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") @dp.message_handler(lambda x: x.text == messages.all_right_message, state="save_or_continue") 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() @dp.message_handler(lambda x: x.text == messages.save_and_continue, state="save_or_continue") async def save_user_info(message: types.Message, state: FSMContext): info = await state.get_data() save_info(user_id=message.from_user.id, **info) await continue_user_form(message, state)