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
|
from math import ceil
from aiogram import Bot, Router
from aiogram.enums import ButtonStyle
from aiogram.filters import Command
from aiogram.types import (
CallbackQuery,
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
)
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.sql.functions import count
from libs.invoice import get_payment_status
from libs.msg import eclipse_text
from libs.user import load_user_cache
from models import Invoice, Payment, PaymentStatus, User
from models.callback_data import (
PayInvoiceClb,
PaymentItemClb,
PaymentPageClb,
PaymentStatusClb,
)
router = Router(name="payments")
PAGE_SIZE = 5
PAYMENT_STATUS = {
PaymentStatus.PENDING: "🟡",
PaymentStatus.ACCEPTED: "🟢",
PaymentStatus.REJECTED: "🔴",
}
def get_text(user: User) -> str:
if user.is_admin():
return "Выберете платёж для управления:"
else:
return "Выберете счёт для оплаты:"
def get_no_data_text(user: User) -> str:
if user.is_admin():
return "Нету платежей."
else:
return "Нету счетов для оплаты."
def build_markup(
page: int,
total_pages: int,
item_buttons: list[list[InlineKeyboardButton]],
) -> InlineKeyboardMarkup:
page_buttons = []
if page > 0:
page_buttons.append(
InlineKeyboardButton(
text="◀️",
callback_data=PaymentPageClb(page=page - 1).pack(),
)
)
if page < total_pages - 1:
page_buttons.append(
InlineKeyboardButton(
text="▶️",
callback_data=PaymentPageClb(page=page + 1).pack(),
)
)
return InlineKeyboardMarkup(inline_keyboard=[*item_buttons, page_buttons])
async def get_user_reply_markup(
page: int,
user: User,
session: AsyncSession,
) -> InlineKeyboardMarkup | None:
total = await session.scalar(
select(count()).select_from(Invoice).where(Invoice.datetime >= user.datetime)
)
assert total is not None
total_pages = ceil(total / PAGE_SIZE)
if total == 0:
return None
page = max(0, min(page, total_pages - 1))
invoices = await session.scalars(
select(Invoice)
.where(Invoice.datetime >= user.datetime)
.offset(PAGE_SIZE * page)
.limit(PAGE_SIZE)
.order_by(Invoice.id.desc())
)
invoice_buttons = []
for i in invoices:
status = PAYMENT_STATUS[await get_payment_status(session, i.id, user.id)]
button = InlineKeyboardButton(
text=(
f"{status} "
f"{eclipse_text(i.message.text, 10)} "
f"({i.datetime.strftime('%d %b %y г.')})"
),
callback_data=PayInvoiceClb(invoice_id=i.id).pack(),
)
invoice_buttons.append([button])
return build_markup(page, total_pages, invoice_buttons)
async def get_admin_reply_markup(
page: int,
bot: Bot,
session: AsyncSession,
) -> InlineKeyboardMarkup | None:
total = await session.scalar(select(count()).select_from(Payment))
assert total is not None
total_pages = ceil(total / PAGE_SIZE)
if total == 0:
return None
page = max(0, min(page, total_pages - 1))
payments = await session.scalars(
select(Payment)
.offset(PAGE_SIZE * page)
.limit(PAGE_SIZE)
.order_by(Payment.id.desc())
)
payment_buttons = []
for p in payments:
invoice = await session.get(Invoice, p.invoice_id)
assert invoice is not None
chat = await bot.get_chat(p.user_id)
button = InlineKeyboardButton(
text=(
f"{PAYMENT_STATUS[p.status]} "
f"{chat.full_name} - "
f"{eclipse_text(invoice.message.text, 10)} "
f"({p.datetime.strftime('%d %b %y г.')})"
),
callback_data=PaymentItemClb(page=page, payment_id=p.id).pack(),
)
payment_buttons.append([button])
return build_markup(page, total_pages, payment_buttons)
async def get_reply_markup(
page: int,
bot: Bot,
user: User,
session: AsyncSession,
) -> InlineKeyboardMarkup | None:
if user.is_admin():
return await get_admin_reply_markup(page, bot, session)
else:
return await get_user_reply_markup(page, user, session)
@router.message(Command("payments"))
async def command(msg: Message, bot: Bot, session: AsyncSession, user: User) -> None:
reply_markup = await get_reply_markup(0, bot, user, session)
if reply_markup is None:
await msg.answer(get_no_data_text(user))
return
await msg.answer(get_text(user), reply_markup=reply_markup)
@router.callback_query(PaymentPageClb.filter())
async def page(
clb: CallbackQuery,
bot: Bot,
callback_data: PaymentPageClb,
session: AsyncSession,
user: User,
) -> None:
assert isinstance(clb.message, Message)
text = get_text(user)
reply_markup = await get_reply_markup(callback_data.page, bot, user, session)
if clb.message.photo is not None or clb.message.document is not None:
await clb.message.delete()
await bot.send_message(clb.from_user.id, text, reply_markup=reply_markup)
else:
await clb.message.edit_text(text, reply_markup=reply_markup)
await clb.answer()
@router.callback_query(PaymentItemClb.filter())
async def item(
clb: CallbackQuery,
bot: Bot,
callback_data: PaymentItemClb,
session: AsyncSession,
user: User,
) -> None:
assert isinstance(clb.message, Message)
if not user.is_admin():
await clb.answer("У вас нет прав для данного действия.", show_alert=True)
return
payment = await session.get(Payment, callback_data.payment_id)
if payment is None:
await clb.answer("Платёж был удален.", show_alert=True)
return
invoice = await session.get(Invoice, payment.invoice_id)
assert invoice is not None
user_cache = await load_user_cache(bot, payment.user_id)
status_buttons = []
if payment.status != PaymentStatus.ACCEPTED:
status_buttons.append(
InlineKeyboardButton(
text="Подтвердить",
style=ButtonStyle.SUCCESS,
callback_data=PaymentStatusClb(
payment_id=payment.id,
payment_status=PaymentStatus.ACCEPTED,
).pack(),
)
)
if payment.status != PaymentStatus.REJECTED:
status_buttons.append(
InlineKeyboardButton(
text="Отклонить",
style=ButtonStyle.DANGER,
callback_data=PaymentStatusClb(
payment_id=payment.id,
payment_status=PaymentStatus.REJECTED,
).pack(),
)
)
back_button = InlineKeyboardButton(
text="Назад к выбору",
callback_data=PaymentPageClb(page=callback_data.page).pack(),
)
reply_markup = InlineKeyboardMarkup(inline_keyboard=[status_buttons, [back_button]])
caption = (
f"Платёж от {user_cache.mention}\n"
f"Счёт: {eclipse_text(invoice.message.text, 30)}\n"
f"Дата: {payment.datetime.strftime('%d %b %y г.')}\n"
f"Статус: {PAYMENT_STATUS[payment.status]}"
)
await clb.message.delete()
await payment.receipt_file.send(
bot,
clb.from_user.id,
caption=caption,
reply_markup=reply_markup,
)
await clb.answer()
|