filters, reports
This commit is contained in:
@@ -45,6 +45,7 @@ async def stats_select_period(update: Update, context: ContextTypes.DEFAULT_TYPE
|
||||
context.user_data["selected_hotel"] = hotel_id
|
||||
|
||||
keyboard = [
|
||||
[InlineKeyboardButton("День", callback_data="stats_period_day")],
|
||||
[InlineKeyboardButton("Неделя", callback_data="stats_period_week")],
|
||||
[InlineKeyboardButton("Месяц", callback_data="stats_period_month")],
|
||||
[InlineKeyboardButton("Все время", callback_data="stats_period_all")],
|
||||
@@ -67,19 +68,32 @@ async def generate_statistics(update: Update, context: ContextTypes.DEFAULT_TYPE
|
||||
period = query.data.split("_")[2]
|
||||
|
||||
now = datetime.now()
|
||||
if period == "week":
|
||||
start_date = now - timedelta(days=7)
|
||||
end_date = now
|
||||
if period == "day":
|
||||
start_date = (now - timedelta(days=1)).date() # Вчерашняя дата
|
||||
end_date = now.date() # Сегодняшняя дата
|
||||
elif period == "week":
|
||||
start_date = (now - timedelta(days=7)).date()
|
||||
end_date = now.date()
|
||||
elif period == "month":
|
||||
start_date = now - timedelta(days=30)
|
||||
end_date = now
|
||||
start_date = (now - timedelta(days=30)).date()
|
||||
end_date = now.date()
|
||||
else:
|
||||
start_date = None
|
||||
end_date = None
|
||||
|
||||
reservations = await sync_to_async(list)(
|
||||
Reservation.objects.filter(hotel_id=hotel_id).prefetch_related('guests')
|
||||
)
|
||||
# Фильтрация по "дата заезда"
|
||||
if start_date and end_date:
|
||||
reservations = await sync_to_async(list)(
|
||||
Reservation.objects.filter(
|
||||
hotel_id=hotel_id,
|
||||
check_in__date__gte=start_date,
|
||||
check_in__date__lte=end_date
|
||||
).prefetch_related('guests')
|
||||
)
|
||||
else:
|
||||
reservations = await sync_to_async(list)(
|
||||
Reservation.objects.filter(hotel_id=hotel_id).prefetch_related('guests')
|
||||
)
|
||||
|
||||
if not reservations:
|
||||
await query.edit_message_text("Нет данных для статистики за выбранный период.")
|
||||
@@ -92,6 +106,7 @@ async def generate_statistics(update: Update, context: ContextTypes.DEFAULT_TYPE
|
||||
await query.message.reply_document(document=file, filename=f"{hotel.name}_report.pdf")
|
||||
|
||||
|
||||
|
||||
async def stats_back(update: Update, context):
|
||||
"""Возврат к выбору отеля."""
|
||||
query = update.callback_query
|
||||
|
||||
@@ -4,7 +4,7 @@ import os
|
||||
|
||||
def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||||
"""Генерация PDF отчета."""
|
||||
pdf = FPDF()
|
||||
pdf = FPDF(orientation='L', unit="mm", format="A4")
|
||||
pdf.add_page()
|
||||
|
||||
# Укажите путь к шрифту
|
||||
@@ -33,7 +33,7 @@ def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||||
|
||||
# Заголовки таблицы
|
||||
pdf.set_font("OpenSans", size=10)
|
||||
headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена", "Скидка"]
|
||||
headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена"]
|
||||
for width, header in zip(col_widths, headers):
|
||||
pdf.cell(width, 15, header, border=1, align="C")
|
||||
pdf.ln()
|
||||
@@ -52,14 +52,12 @@ def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||||
pdf.cell(col_widths[2], 10, res.room_number, border=1)
|
||||
pdf.cell(col_widths[3], 10, res.room_type, border=1)
|
||||
pdf.cell(col_widths[4], 10, f"{price:.2f} р.", border=1, align="R")
|
||||
pdf.cell(col_widths[5], 10, f"{discount:.2f} р.", border=1, align="R")
|
||||
pdf.ln()
|
||||
|
||||
pdf.ln(5)
|
||||
pdf.set_font("OpenSans", size=18)
|
||||
pdf.cell(0, 10, "Итоги:", ln=True)
|
||||
pdf.cell(0, 10, f"Общая сумма цен: {total_price:.2f} руб.", ln=True)
|
||||
pdf.cell(0, 10, f"Общая сумма скидок: {total_discount:.2f} руб. ", ln=True)
|
||||
|
||||
file_path = os.path.join("reports", f"{hotel_name}_report.pdf")
|
||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user