import os
from config import dp, db, bot, supp_group_id, oper_group_id, notification_group_id, use_chats
from aiogram import types
from aiogram.dispatcher import FSMContext
from functions import get_categories_user, get_subcategories_user, send_good
from markups import menu_mkp, promo_mkp
from states import NewBuy
from personnel import *
@dp.callback_query_handler(text='shop')
@dp.callback_query_handler(text='toshop')
async def toshopcall(call: types.CallbackQuery):
if db.check_ban(call.from_user.id):
await call.message.delete()
await call.message.answer('Выберите категорию:', reply_markup=get_categories_user())
@dp.callback_query_handler(text_contains='usercat_')
async def usercatcall(call: types.CallbackQuery):
if db.check_ban(call.from_user.id):
catid = call.data.split('_')[1]
await call.message.delete()
await call.message.answer('Выберите подкатегорию:', reply_markup=get_subcategories_user(int(catid)))
@dp.callback_query_handler(text_contains='usersubcat_', state=NewBuy.Paying)
@dp.callback_query_handler(text_contains='usersubcat_', state=NewBuy.Promo)
@dp.callback_query_handler(text_contains='usersubcat_')
async def usersubcatcall(call: types.CallbackQuery, state: FSMContext):
if db.check_ban(call.from_user.id):
try:
await state.finish()
except:
pass
subcatid = call.data.split('_')[1]
if len(db.check_goods(int(subcatid))) == 0:
await call.answer('К сожалению тут пусто', show_alert=True)
else:
await call.message.delete()
await send_good(0, int(subcatid), call.from_user.id)
@dp.callback_query_handler(text_contains='catback_')
async def catbackcall(call: types.CallbackQuery):
if db.check_ban(call.from_user.id):
await call.message.delete()
subcatid = call.data.split('_')[1]
step = call.data.split('_')[2]
await send_good(int(step), int(subcatid), call.from_user.id)
@dp.callback_query_handler(text_contains='catnext_')
async def catnextcall(call: types.CallbackQuery):
if db.check_ban(call.from_user.id):
await call.message.delete()
subcatid = call.data.split('_')[1]
step = call.data.split('_')[2]
await send_good(int(step), int(subcatid), call.from_user.id)
@dp.callback_query_handler(text_contains='buyGood_')
async def buyGood(call: types.CallbackQuery, state: FSMContext):
if db.check_ban(call.from_user.id):
await call.message.delete()
goodId = call.data.split('_')[1]
subCatId = call.data.split('_')[2]
goodInfo = db.get_goodinfo(goodId)
await NewBuy.Promo.set()
async with state.proxy() as data:
data['GoodId'] = {"goodId": goodId, "subCatId": subCatId}
await call.message.answer(f'⌛ Покупка {goodInfo[0]}\nЦена: {goodInfo[2]}₽ \n\nВведите промокод (если есть) :', reply_markup=promo_mkp(subCatId))
@dp.message_handler(state=NewBuy.Promo)
async def newBuyPromo(message: types.Message, state: FSMContext):
promocode = message.text
promoInfo = db.get_promo_info(promocode)
async with state.proxy() as data:
goodId = data['GoodId']["goodId"]
subCatId = data['GoodId']["subCatId"]
if promoInfo != None:
async with state.proxy() as data:
data['Promo'] = promocode
promoPercent = promoInfo[1]
goodInfo = db.get_goodinfo(goodId)
mkp = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton('✅ Оформить заказ', callback_data='buyOrder')
btn2 = types.InlineKeyboardButton('❌ Отменить покупку', callback_data=f'usersubcat_{subCatId}')
mkp.add(btn1).add(btn2)
newPrice = float(goodInfo[2]) * ((100 - int(promoPercent))/100)
await message.answer(f'📦 Применен промокод {promocode}\nЗаказ:\n\nТовар: {goodInfo[0]}\nЦена: {goodInfo[2]}₽ \nС учетом скидок: {newPrice}₽\n\nХотите оформить заказ?', reply_markup=mkp)
await NewBuy.next()
else:
await message.answer(f'⏳ Промокод {promocode} введен неверно или он не существует! Введите промокод повторно (если есть :', reply_markup=promo_mkp(subCatId))
@dp.callback_query_handler(text='buyOrder', state=NewBuy.Paying)
@dp.callback_query_handler(text='skipPromo', state=NewBuy.Promo)
async def buyOrder(call: types.CallbackQuery, state: FSMContext):
if db.check_ban(call.from_user.id):
async with state.proxy() as data:
goodId = data['GoodId']["goodId"]
subCatId = data['GoodId']["subCatId"]
goodInfo = db.get_goodinfo(goodId)
if call.data == "buyOrder":
promocode = data['Promo']
promoInfo = db.get_promo_info(promocode)
promoPercent = promoInfo[1]
newPrice = float(goodInfo[2]) * ((100 - int(promoPercent))/100)
else:
promocode = None
newPrice = float(goodInfo[2])
await call.message.delete()
orderId = db.add_order(call.from_user.id, goodId, promocode, newPrice)
mkp = types.InlineKeyboardMarkup(row_width=4)
userId = call.from_user.id
client = db.get_usernamerev(int(userId))
if use_chats == True:
await bot.send_message(oper_group_id, f"💡 Новый заказ! \n\nПользователь {client}\nНомер заказа: {orderId} \nТовар: {goodInfo[0]} \nЦена: {goodInfo[2]}₽ \nС учетом скидок: {newPrice}₽")
else:
await bot.send_message(operators, f"💡 Новый заказ! \n\nПользователь:{client}\nНомер заказа: {orderId} \nТовар: {goodInfo[0]}\nЦена: {goodInfo[2]}₽ \nС учетом скидок: {newPrice}₽")
await call.message.answer(f"Спасибо {client} за обращение! \n\nЗаказ передан операторам, ожидайте дальнейших инструкций, во избежание мошенничества, спросите у оператора номер вашего заказа.\n\nНомер заказа: {orderId} \nТовар: {goodInfo[0]} \nЦена: {goodInfo[2]}₽ \nС учетом скидок: {newPrice}₽")
await state.finish()